]> git.tdb.fi Git - r2c2.git/blobdiff - source/libmarklin/route.cpp
Fix pathfinder to not block itself in some situations
[r2c2.git] / source / libmarklin / route.cpp
index 20017d240a429562a1e7e3907999eb40c2a2ebb6..ace8d0b950812c56a22e23a0ba381f4d389b9f7b 100644 (file)
@@ -1,10 +1,12 @@
 /* $Id$
 
 This file is part of the MSP Märklin suite
-Copyright © 2007-2009  Mikkosoft Productions, Mikko Rasa
+Copyright © 2007-2010  Mikkosoft Productions, Mikko Rasa
 Distributed under the GPL
 */
 
+#include <queue>
+#include <msp/strings/formatter.h>
 #include "layout.h"
 #include "route.h"
 #include "track.h"
@@ -13,14 +15,142 @@ Distributed under the GPL
 using namespace std;
 using namespace Msp;
 
+namespace {
+
+using namespace Marklin;
+
+typedef std::pair<const Track *, unsigned> Key;
+
+struct Node
+{
+       const Track *track;
+       unsigned ep;
+       Node *prev;
+       float dist;
+
+       Node():
+               track(0), ep(0), prev(0), dist(0)
+       { }
+
+       Node(const Track &t, unsigned e):
+               track(&t), ep(e), prev(0), dist(0)
+       { }
+
+       Node(const Track &t, unsigned e, Node &r, float d):
+               track(&t), ep(e), prev(&r), dist(prev->dist+d)
+       { }
+
+       bool operator<(const Node &other) const
+       { return dist>other.dist; }
+};
+
+struct TrackMatch
+{
+       const Track &track;
+
+       TrackMatch(const Track &t): track(t) { }
+
+       bool operator()(const Track &t) const { return &t==&track; }
+};
+
+struct TrackInRoute
+{
+       const Route &route;
+
+       TrackInRoute(const Route &r): route(r) { }
+
+       bool operator()(const Track &t) const { return route.get_tracks().count(&t); }
+};
+
+template<typename Pred>
+list<const Track *> dijkstra(const Track &from, unsigned ep, const Pred &goal)
+{
+       map<Key, Node> track_nodes;
+       priority_queue<Node> nodes;
+       Node *final = 0;
+
+       nodes.push(Node(from, ep));
+
+       while(!nodes.empty())
+       {
+               Node lowest = nodes.top();
+               nodes.pop();
+
+               Key key(lowest.track, lowest.ep);
+               if(track_nodes.count(key))
+                       continue;
+
+               Node &ref = track_nodes[key] = lowest;
+               if(goal(*lowest.track))
+               {
+                       final = &ref;
+                       break;
+               }
+
+               const TrackType &type = lowest.track->get_type();
+               const vector<Endpoint> &eps = type.get_endpoints();
+               const vector<Track *> &links = lowest.track->get_links();
+               for(unsigned i=0; i<eps.size(); ++i)
+               {
+                       if(i==lowest.ep || !links[i])
+                               continue;
+
+                       unsigned link_ep = links[i]->get_endpoint_by_link(*lowest.track);
+                       if(track_nodes.count(Key(links[i], link_ep)))
+                               continue;
+
+                       unsigned mask = eps[i].paths&eps[lowest.ep].paths;
+                       if(!mask)
+                               continue;
+
+                       unsigned path=0;
+                       for(; !(mask&1); ++path, mask>>=1) ;
+                       nodes.push(Node(*links[i], links[i]->get_endpoint_by_link(*lowest.track), ref, type.get_path_length(path)));
+               }
+       }
+
+       if(!final)
+               throw InvalidParameterValue("Could not find a route");
+
+       list<const Track *> result;
+       for(Node *node=final; node; node=node->prev)
+               result.push_front(node->track);
+
+       return result;
+}
+
+unsigned count = 0;
+
+template<typename Pred>
+Route *create_route(const Track &from, unsigned ep, const Pred &goal)
+{
+       list<const Track *> tracks = dijkstra(from, ep, goal);
+
+       Route *route = new Route(from.get_layout(), format("-%d-", ++count));
+       for(list<const Track *>::iterator i=tracks.begin(); i!=tracks.end(); ++i)
+               route->add_track(**i);
+
+       return route;
+}
+
+}
+
+
 namespace Marklin {
 
-Route::Route(Layout &layout, const string &n):
+Route::Route(Layout &l, const string &n):
+       layout(l),
        name(n)
 {
+       layout.add_route(*this);
        layout.signal_track_removed.connect(sigc::mem_fun(this, &Route::track_removed));
 }
 
+Route::~Route()
+{
+       layout.remove_route(*this);
+}
+
 int Route::get_turnout(unsigned id) const
 {
        map<unsigned, int>::const_iterator i = turnouts.find(id);
@@ -192,6 +322,16 @@ void Route::track_removed(Track &t)
        tracks.erase(&t);
 }
 
+Route *Route::find(const Track &from, unsigned ep, const Track &to)
+{
+       return create_route(from, ep, TrackMatch(to));
+}
+
+Route *Route::find(const Track &from, unsigned ep, const Route &to)
+{
+       return create_route(from, ep, TrackInRoute(to));
+}
+
 
 Route::Loader::Loader(Route &r):
        DataFile::BasicLoader<Route>(r)