]> git.tdb.fi Git - r2c2.git/blobdiff - source/libmarklin/route.cpp
Make LCD output selectable at runtime through an extra I/O pin
[r2c2.git] / source / libmarklin / route.cpp
index 2ff6962fe67551a17be8bde9052595b6933bf171..79886d28c371c793ed223909ef343b79edc0b726 100644 (file)
@@ -10,6 +10,7 @@ Distributed under the GPL
 #include "layout.h"
 #include "route.h"
 #include "track.h"
+#include "trackiter.h"
 #include "tracktype.h"
 
 using namespace std;
@@ -19,23 +20,24 @@ namespace {
 
 using namespace Marklin;
 
+typedef std::pair<Track *, unsigned> Key;
+
 struct Node
 {
-       const Track *track;
-       unsigned ep;
+       TrackIter track;
        Node *prev;
        float dist;
 
        Node():
-               track(0), ep(0), prev(0), dist(0)
+               prev(0), dist(0)
        { }
 
-       Node(const Track &t, unsigned e):
-               track(&t), ep(e), prev(0), dist(0)
+       Node(const TrackIter &t):
+               track(t), prev(0), dist(0)
        { }
 
-       Node(const Track &t, unsigned e, Node &r, float d):
-               track(&t), ep(e), prev(&r), dist(prev->dist+d)
+       Node(const TrackIter &t, Node &r, float d):
+               track(t), prev(&r), dist(prev->dist+d)
        { }
 
        bool operator<(const Node &other) const
@@ -44,86 +46,84 @@ struct Node
 
 struct TrackMatch
 {
-       const Track &track;
+       Track &track;
 
-       TrackMatch(const Track &t): track(t) { }
+       TrackMatch(Track &t): track(t) { }
 
-       bool operator()(const Track &t) const { return &t==&track; }
+       bool operator()(Track &t) const { return &t==&track; }
 };
 
-struct TrackInRoute
+struct TrackInSet
 {
-       const Route &route;
+       const set<Track *> &tracks;
 
-       TrackInRoute(const Route &r): route(r) { }
+       TrackInSet(const set<Track *> &t): tracks(t) { }
 
-       bool operator()(const Track &t) const { return route.get_tracks().count(&t); }
+       bool operator()(Track &t) const { return tracks.count(&t); }
 };
 
 template<typename Pred>
-list<const Track *> dijkstra(const Track &from, unsigned ep, const Pred &goal)
+list<Track *> dijkstra(const TrackIter &from, const Pred &goal)
 {
-       // XXX Fails to find some routes - should use track+ep as key
-       map<const Track *, Node> track_nodes;
+       map<Key, Node> track_nodes;
        priority_queue<Node> nodes;
        Node *final = 0;
 
-       nodes.push(Node(from, ep));
+       nodes.push(from);
 
        while(!nodes.empty())
        {
                Node lowest = nodes.top();
                nodes.pop();
 
-               if(track_nodes.count(lowest.track))
+               Key key(lowest.track.track(), lowest.track.entry());
+               if(track_nodes.count(key))
                        continue;
 
-               Node &ref = track_nodes[lowest.track] = lowest;
+               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] || track_nodes.count(links[i]))
-                               continue;
+               unsigned paths = lowest.track.endpoint().paths;
+               for(unsigned i=0; paths>>i; ++i)
+                       if(paths&(1<<i))
+                       {
+                               TrackIter next = lowest.track.next(i);
+                               if(!next)
+                                       continue;
 
-                       unsigned mask = eps[i].paths&eps[lowest.ep].paths;
-                       if(!mask)
-                               continue;
+                               if(track_nodes.count(Key(next.track(), next.entry())))
+                                       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)));
-               }
+                               nodes.push(Node(next, ref, lowest.track->get_type().get_path_length(i)));
+                       }
        }
 
-       if(!final)
-               throw InvalidParameterValue("Could not find a route");
-
-       list<const Track *> result;
+       list<Track *> result;
        for(Node *node=final; node; node=node->prev)
-               result.push_front(node->track);
+               result.push_front(&*node->track);
 
        return result;
 }
 
-unsigned count = 0;
-
 template<typename Pred>
-Route *create_route(const Track &from, unsigned ep, const Pred &goal)
+Route *create_route(const TrackIter &from, const Pred &goal)
 {
-       list<const Track *> tracks = dijkstra(from, ep, goal);
+       list<Track *> tracks = dijkstra(from, goal);
 
-       Route *route = new Route(from.get_layout(), format("-%d-", ++count));
-       for(list<const Track *>::iterator i=tracks.begin(); i!=tracks.end(); ++i)
+       if(tracks.empty())
+               return 0;
+
+       Route *route = new Route(from->get_layout());
+       for(list<Track *>::iterator i=tracks.begin(); i!=tracks.end(); ++i)
                route->add_track(**i);
 
+       route->set_name("Pathfinder");
+       route->set_temporary(true);
+
        return route;
 }
 
@@ -132,9 +132,9 @@ Route *create_route(const Track &from, unsigned ep, const Pred &goal)
 
 namespace Marklin {
 
-Route::Route(Layout &l, const string &n):
+Route::Route(Layout &l):
        layout(l),
-       name(n)
+       temporary(false)
 {
        layout.add_route(*this);
        layout.signal_track_removed.connect(sigc::mem_fun(this, &Route::track_removed));
@@ -145,6 +145,83 @@ Route::~Route()
        layout.remove_route(*this);
 }
 
+void Route::set_name(const string &n)
+{
+       name = n;
+       signal_name_changed.emit(name);
+}
+
+void Route::set_temporary(bool t)
+{
+       temporary = t;
+}
+
+void Route::set_turnout(unsigned addr, unsigned path)
+{
+       if(!addr)
+               throw InvalidParameterValue("Invalid turnout address");
+       map<unsigned, int>::iterator i = turnouts.find(addr);
+       if(i==turnouts.end())
+               throw KeyError("Turnout is not in this route");
+       if(i->second>=0 && path!=static_cast<unsigned>(i->second))
+               throw InvalidState("Setting conflicts with route");
+       i->second = path;
+}
+
+void Route::update_turnouts()
+{
+       set<unsigned> found;
+       for(set<Track *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
+               if(unsigned tid = (*i)->get_turnout_id())
+               {
+                       found.insert(tid);
+
+                       const vector<TrackType::Endpoint> &endpoints = (*i)->get_type().get_endpoints();
+                       const vector<Track *> &links = (*i)->get_links();
+
+                       // Build a combined path mask from linked endpoints
+                       unsigned mask = (*i)->get_type().get_paths();
+                       for(unsigned j=0; j<endpoints.size(); ++j)
+                       {
+                               if(!tracks.count(links[j]))
+                                       continue;
+
+                               if(unsigned tid2 = links[j]->get_turnout_id())
+                               {
+                                       const TrackType::Endpoint &ep = links[j]->get_type().get_endpoint(links[j]->get_endpoint_by_link(**i));
+                                       int p = get_turnout(tid2);
+                                       if(p>=0 && !(ep.paths&(1<<p)))
+                                       {
+                                               // The linked track is a turnout and has a path which is incompatible with this endpoint
+                                               mask &= ~endpoints[j].paths;
+                                               continue;
+                                       }
+                               }
+                               mask &= endpoints[j].paths;
+                       }
+
+                       if(mask && !(mask&(mask-1)))
+                       {
+                               // Exactly one possible choice, set the path accordingly
+                               unsigned path = 0;
+                               for(; (mask && !(mask&1)); mask>>=1, ++path) ;
+                               turnouts[tid] = path;
+                       }
+                       else if(!turnouts.count(tid))
+                               // More than one possible choice, and no existing entry - set as undecided
+                               turnouts[tid] = -1;
+               }
+
+       // Remove any turnouts that do not exist in the route
+       for(map<unsigned, int>::iterator i=turnouts.begin(); i!=turnouts.end();)
+       {
+               if(!found.count(i->first))
+                       turnouts.erase(i++);
+               else
+                       ++i;
+       }
+}
+
 int Route::get_turnout(unsigned id) const
 {
        map<unsigned, int>::const_iterator i = turnouts.find(id);
@@ -153,7 +230,18 @@ int Route::get_turnout(unsigned id) const
        return -1;
 }
 
-void Route::add_track(const Track &trk)
+unsigned Route::get_path(Track &trk) const
+{
+       if(unsigned tid = trk.get_turnout_id())
+       {
+               map<unsigned, int>::const_iterator i = turnouts.find(tid);
+               if(i!=turnouts.end())
+                       return i->second;
+       }
+       return trk.get_active_path();
+}
+
+void Route::add_track(Track &trk)
 {
        if(tracks.count(&trk))
                return;
@@ -173,17 +261,17 @@ void Route::add_track(const Track &trk)
        update_turnouts();
 }
 
-void Route::add_tracks(const set<const Track *> &trks)
+void Route::add_tracks(const set<Track *> &trks)
 {
-       set<const Track *> pending;
-       for(set<const Track *>::const_iterator i=trks.begin(); i!=trks.end(); ++i)
+       set<Track *> pending;
+       for(set<Track *>::const_iterator i=trks.begin(); i!=trks.end(); ++i)
                if(!tracks.count(*i))
                        pending.insert(*i);
 
        while(!pending.empty())
        {
                bool found = false;
-               for(set<const Track *>::const_iterator i=pending.begin(); i!=pending.end(); ++i)
+               for(set<Track *>::const_iterator i=pending.begin(); i!=pending.end(); ++i)
                        if(tracks.empty() || check_validity(**i)==7)
                        {
                                tracks.insert(*i);
@@ -199,113 +287,103 @@ void Route::add_tracks(const set<const Track *> &trks)
        update_turnouts();
 }
 
-void Route::save(list<DataFile::Statement> &st) const
+void Route::add_track_chain(Track &start, unsigned ep, const TurnoutMap &trnts)
 {
-       for(map<unsigned, int>::const_iterator i=turnouts.begin(); i!=turnouts.end(); ++i)
-               st.push_back((DataFile::Statement("turnout"), i->first, i->second));
-}
+       TrackIter iter(&start, ep);
+       while(iter)
+       {
+               if(iter->get_type().is_dead_end())
+                       break;
 
-void Route::update_turnouts()
-{
-       set<unsigned> found;
-       for(set<const Track *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
-               if(unsigned tid=(*i)->get_turnout_id())
+               if(has_track(*iter))
+                       break;
+
+               int path = 0;
+               if(iter->get_turnout_id())
                {
-                       found.insert(tid);
+                       TurnoutMap::const_iterator i = trnts.find(iter->get_turnout_id());
+                       if(i==trnts.end())
+                               break;
 
-                       const vector<Endpoint> &endpoints = (*i)->get_type().get_endpoints();
-                       const vector<Track *> &links = (*i)->get_links();
+                       path = i->second;
+               }
 
-                       // Build a combined path mask from linked endpoints
-                       unsigned mask = 15;
-                       for(unsigned j=0; j<endpoints.size(); ++j)
-                       {
-                               if(!tracks.count(links[j]))
-                                       continue;
+               add_track(*iter);
 
-                               if(unsigned tid2=links[j]->get_turnout_id())
-                               {
-                                       const Endpoint &ep = links[j]->get_type().get_endpoints()[links[j]->get_endpoint_by_link(**i)];
-                                       int p = get_turnout(tid2);
-                                       if(p>=0 && !(ep.paths&(1<<p)))
-                                       {
-                                               // The linked track is a turnout and has a path which is incompatible with this endpoint
-                                               mask &= ~endpoints[j].paths;
-                                               continue;
-                                       }
-                               }
-                               mask &= endpoints[j].paths;
-                       }
+               iter = iter.next(path);
+       }
+}
 
-                       if(mask && !(mask&(mask-1)))
-                       {
-                               // Exactly one possible choice, set the path accordingly
-                               unsigned path = 0;
-                               for(; (mask && !(mask&1)); mask>>=1, ++path) ;
-                               turnouts[tid] = path;
-                       }
-                       else if(!turnouts.count(tid))
-                               // More than one possible choice, and no existing entry - set as undecided
-                               turnouts[tid] = -1;
-               }
+bool Route::has_track(Track &t) const
+{
+       return tracks.count(&t);
+}
 
-       // Remove any turnouts that do not exist in the route
-       for(map<unsigned, int>::iterator i=turnouts.begin(); i!=turnouts.end();)
-       {
-               if(!found.count(i->first))
-                       turnouts.erase(i++);
-               else
-                       ++i;
-       }
+void Route::save(list<DataFile::Statement> &st) const
+{
+       st.push_back((DataFile::Statement("name"), name));
+       for(map<unsigned, int>::const_iterator i=turnouts.begin(); i!=turnouts.end(); ++i)
+               st.push_back((DataFile::Statement("turnout"), i->first, i->second));
 }
 
-unsigned Route::check_validity(const Track &trk) const
+unsigned Route::check_validity(Track &trk) const
 {
        unsigned result = 4;
-       for(set<const Track *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
+       const vector<Track *> &links = trk.get_links();
+       for(vector<Track *>::const_iterator i=links.begin(); i!=links.end(); ++i)
        {
-               int epi=(*i)->get_endpoint_by_link(trk);
-               if(epi>=0)
+               if(!*i)
+                       continue;
+               if(!tracks.count(*i))
+                       continue;
+
+               // Linked to an existing track - good
+               result |= 1;
+
+               if(unsigned tid = (*i)->get_turnout_id())
                {
-                       // Linked to an existing track - good
-                       result |= 1;
-                       const vector<Endpoint> &endpoints = (*i)->get_type().get_endpoints();
-                       if(unsigned tid=(*i)->get_turnout_id())
+                       const TrackType::Endpoint &ep = (*i)->get_type().get_endpoint((*i)->get_endpoint_by_link(trk));
+                       int path = get_turnout(tid);
+                       if(path>=0)
                        {
-                               int r = get_turnout(tid);
-                               if(r>=0)
-                               {
-                                       // Linking to a turnout with path set is only good if we're continuing that path
-                                       if(endpoints[epi].paths&(1<<r))
-                                               result |= 2;
-                               }
-                               else
-                               {
-                                       // Linked to a turnout with no path set - find out other linked tracks 
-                                       unsigned count = 0;
-                                       const vector<Track *> &links = (*i)->get_links();
-                                       int epj = -1;
-                                       for(unsigned k=0; k<endpoints.size(); ++k)
-                                               if(tracks.count(links[k]))
+                               // Linking to a turnout with path set is only good if we're continuing that path
+                               if(ep.paths&(1<<path))
+                                       result |= 2;
+                       }
+                       else
+                       {
+                               // Linked to a turnout with no path set - check other linked tracks 
+                               const vector<Track *> &tlinks = (*i)->get_links();
+                               unsigned count = 0;
+                               for(unsigned j=0; j<tlinks.size(); ++j)
+                                       if(tracks.count(tlinks[j]))
+                                       {
+                                               unsigned tid2 = tlinks[j]->get_turnout_id();
+                                               if(tid2)
                                                {
-                                                       ++count;
-                                                       epj = k;
+                                                       const TrackType::Endpoint &ep2 = tlinks[j]->get_type().get_endpoint(tlinks[j]->get_endpoint_by_link(**i));
+                                                       path = get_turnout(tid2);
+                                                       // Ignore a linked turnout with some other path set
+                                                       if(path>0 && !(ep2.paths&(1<<path)))
+                                                               continue;
                                                }
 
-                                       // Only good if at most one other track is linked to the turnout
-                                       if(count<=1)
-                                       {
-                                               result |= 2;
-                                               if(epj>=0 && !(endpoints[epi].paths&endpoints[epj].paths))
+                                               ++count;
+
+                                               const TrackType::Endpoint &ep2 = (*i)->get_type().get_endpoint(j);
+                                               if(!(ep.paths&ep2.paths))
                                                        // Impossible path through the turnout - not good
                                                        result &= 3;
                                        }
-                               }
+
+                               // Only good if at most one other track is linked to the turnout
+                               if(count<=1)
+                                       result |= 2;
                        }
-                       else
-                               // Linked to something linear - good
-                               result |= 2;
                }
+               else
+                       // Linked to something linear - good
+                       result |= 2;
        }
 
        return result;
@@ -316,26 +394,62 @@ void Route::track_removed(Track &t)
        tracks.erase(&t);
 }
 
-Route *Route::find(const Track &from, unsigned ep, const Track &to)
+Route *Route::find(const TrackIter &from, Track &to)
+{
+       return create_route(from, TrackMatch(to));
+}
+
+Route *Route::find(const TrackIter &from, const Route &to)
 {
-       return create_route(from, ep, TrackMatch(to));
+       return create_route(from, TrackInSet(to.get_tracks()));
 }
 
-Route *Route::find(const Track &from, unsigned ep, const Route &to)
+Route *Route::find(const TrackIter &from, const set<Track *> &to)
 {
-       return create_route(from, ep, TrackInRoute(to));
+       return create_route(from, TrackInSet(to));
 }
 
 
 Route::Loader::Loader(Route &r):
        DataFile::BasicLoader<Route>(r)
 {
+       add("name",    &Route::name);
        add("turnout", &Loader::turnout);
 }
 
+void Route::Loader::finish()
+{
+       const set<Track *> &ltracks = obj.layout.get_tracks();
+       for(set<Track *>::const_iterator i=ltracks.begin(); i!=ltracks.end(); ++i)
+       {
+               unsigned tid = (*i)->get_turnout_id();
+               if(!tid)
+                       continue;
+
+               TurnoutMap::iterator j = turnouts.find(tid);
+               if(j==turnouts.end())
+                       continue;
+
+               unsigned path_mask = 1<<j->second;
+               const vector<TrackType::Endpoint> &eps = (*i)->get_type().get_endpoints();
+               for(unsigned k=0; k<eps.size(); ++k)
+                       if(eps[k].paths&path_mask)
+                       {
+                               Track *link = (*i)->get_link(k);
+                               if(!obj.tracks.count(link))
+                                       obj.add_track_chain(*link, link->get_endpoint_by_link(**i), turnouts);
+                               if(!obj.tracks.count(*i))
+                                       obj.add_track_chain(**i, k, turnouts);
+                               break;
+                       }
+
+               break;
+       }
+}
+
 void Route::Loader::turnout(unsigned id, unsigned path)
 {
-       obj.turnouts[id] = path;
+       turnouts[id] = path;
 }
 
 } // namespace Marklin