]> git.tdb.fi Git - r2c2.git/blobdiff - source/libmarklin/route.cpp
Add a pathfinder function to Route
[r2c2.git] / source / libmarklin / route.cpp
index 7db3f324b61e9ce5d753786aac251982cff6ec90..13f4e3efff012e57622d2e387ac25e8613ffc6bc 100644 (file)
@@ -5,6 +5,8 @@ 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,6 +15,98 @@ Distributed under the GPL
 using namespace std;
 using namespace Msp;
 
+namespace {
+
+using namespace Marklin;
+
+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) { return &t==&track; }
+};
+
+template<typename Pred>
+list<const Track *> dijkstra(const Track &from, unsigned ep, Pred goal)
+{
+       // XXX Fails to find some routes - should use track+ep as key
+       map<const Track *, Node> track_nodes;
+       priority_queue<Node> nodes;
+       Node *final = 0;
+
+       nodes.push(Node(from, ep));
+
+       while(!nodes.empty())
+       {
+               Node lowest = nodes.top();
+               nodes.pop();
+
+               if(track_nodes.count(lowest.track))
+                       continue;
+
+               Node &ref = track_nodes[lowest.track] = 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] || track_nodes.count(links[i]))
+                               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;
+}
+
+}
+
+
 namespace Marklin {
 
 Route::Route(Layout &l, const string &n):
@@ -199,6 +293,19 @@ void Route::track_removed(Track &t)
        tracks.erase(&t);
 }
 
+Route *Route::find(const Track &from, unsigned ep, const Track &to)
+{
+       TrackMatch goal(to);
+       list<const Track *> tracks = dijkstra(from, ep, goal);
+
+       static unsigned n = 0;
+       Route *route = new Route(from.get_layout(), format("Route::find %d", ++n));
+       for(list<const Track *>::iterator i=tracks.begin(); i!=tracks.end(); ++i)
+               route->add_track(**i);
+
+       return route;
+}
+
 
 Route::Loader::Loader(Route &r):
        DataFile::BasicLoader<Route>(r)