3 This file is part of the MSP Märklin suite
4 Copyright © 2007-2010 Mikkosoft Productions, Mikko Rasa
5 Distributed under the GPL
9 #include <msp/strings/formatter.h>
13 #include "trackiter.h"
14 #include "tracktype.h"
21 using namespace Marklin;
23 typedef std::pair<Track *, unsigned> Key;
33 track(0), ep(0), prev(0), dist(0)
36 Node(Track &t, unsigned e):
37 track(&t), ep(e), prev(0), dist(0)
40 Node(Track &t, unsigned e, Node &r, float d):
41 track(&t), ep(e), prev(&r), dist(prev->dist+d)
44 bool operator<(const Node &other) const
45 { return dist>other.dist; }
52 TrackMatch(Track &t): track(t) { }
54 bool operator()(Track &t) const { return &t==&track; }
59 const set<Track *> &tracks;
61 TrackInSet(const set<Track *> &t): tracks(t) { }
63 bool operator()(Track &t) const { return tracks.count(&t); }
66 template<typename Pred>
67 list<Track *> dijkstra(Track &from, unsigned ep, const Pred &goal)
69 map<Key, Node> track_nodes;
70 priority_queue<Node> nodes;
73 nodes.push(Node(from, ep));
77 Node lowest = nodes.top();
80 Key key(lowest.track, lowest.ep);
81 if(track_nodes.count(key))
84 Node &ref = track_nodes[key] = lowest;
85 if(goal(*lowest.track))
91 const TrackType &type = lowest.track->get_type();
92 const vector<TrackType::Endpoint> &eps = type.get_endpoints();
93 const vector<Track *> &links = lowest.track->get_links();
94 for(unsigned i=0; i<eps.size(); ++i)
96 if(i==lowest.ep || !links[i])
99 unsigned link_ep = links[i]->get_endpoint_by_link(*lowest.track);
100 if(track_nodes.count(Key(links[i], link_ep)))
103 unsigned mask = eps[i].paths&eps[lowest.ep].paths;
108 for(; !(mask&1); ++path, mask>>=1) ;
109 nodes.push(Node(*links[i], links[i]->get_endpoint_by_link(*lowest.track), ref, type.get_path_length(path)));
113 list<Track *> result;
114 for(Node *node=final; node; node=node->prev)
115 result.push_front(node->track);
120 template<typename Pred>
121 Route *create_route(Track &from, unsigned ep, const Pred &goal)
123 list<Track *> tracks = dijkstra(from, ep, goal);
128 Route *route = new Route(from.get_layout());
129 for(list<Track *>::iterator i=tracks.begin(); i!=tracks.end(); ++i)
130 route->add_track(**i);
132 route->set_name("Pathfinder");
133 route->set_temporary(true);
143 Route::Route(Layout &l):
147 layout.add_route(*this);
148 layout.signal_track_removed.connect(sigc::mem_fun(this, &Route::track_removed));
153 layout.remove_route(*this);
156 void Route::set_name(const string &n)
159 signal_name_changed.emit(name);
162 void Route::set_temporary(bool t)
167 void Route::set_turnout(unsigned addr, unsigned path)
170 throw InvalidParameterValue("Invalid turnout address");
171 map<unsigned, int>::iterator i = turnouts.find(addr);
172 if(i==turnouts.end())
173 throw KeyError("Turnout is not in this route");
174 if(i->second>=0 && path!=static_cast<unsigned>(i->second))
175 throw InvalidState("Setting conflicts with route");
179 void Route::update_turnouts()
182 for(set<Track *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
183 if(unsigned tid = (*i)->get_turnout_id())
187 const vector<TrackType::Endpoint> &endpoints = (*i)->get_type().get_endpoints();
188 const vector<Track *> &links = (*i)->get_links();
190 // Build a combined path mask from linked endpoints
192 for(unsigned j=0; j<endpoints.size(); ++j)
194 if(!tracks.count(links[j]))
197 if(unsigned tid2 = links[j]->get_turnout_id())
199 const TrackType::Endpoint &ep = links[j]->get_type().get_endpoints()[links[j]->get_endpoint_by_link(**i)];
200 int p = get_turnout(tid2);
201 if(p>=0 && !(ep.paths&(1<<p)))
203 // The linked track is a turnout and has a path which is incompatible with this endpoint
204 mask &= ~endpoints[j].paths;
208 mask &= endpoints[j].paths;
211 if(mask && !(mask&(mask-1)))
213 // Exactly one possible choice, set the path accordingly
215 for(; (mask && !(mask&1)); mask>>=1, ++path) ;
216 turnouts[tid] = path;
218 else if(!turnouts.count(tid))
219 // More than one possible choice, and no existing entry - set as undecided
223 // Remove any turnouts that do not exist in the route
224 for(map<unsigned, int>::iterator i=turnouts.begin(); i!=turnouts.end();)
226 if(!found.count(i->first))
233 int Route::get_turnout(unsigned id) const
235 map<unsigned, int>::const_iterator i = turnouts.find(id);
236 if(i!=turnouts.end())
241 void Route::add_track(Track &trk)
243 if(tracks.count(&trk))
248 unsigned valid = check_validity(trk);
250 throw Exception("Not linked to existing tracks");
252 throw Exception("Branching routes not allowed");
254 throw Exception("Route must be smooth");
261 void Route::add_tracks(const set<Track *> &trks)
263 set<Track *> pending;
264 for(set<Track *>::const_iterator i=trks.begin(); i!=trks.end(); ++i)
265 if(!tracks.count(*i))
268 while(!pending.empty())
271 for(set<Track *>::const_iterator i=pending.begin(); i!=pending.end(); ++i)
272 if(tracks.empty() || check_validity(**i)==7)
281 throw Exception("Could not add all tracks to route");
287 void Route::add_track_chain(Track &start, unsigned ep, const TurnoutMap &trnts)
289 TrackIter iter(&start, ep);
292 if(iter->get_type().is_dead_end())
299 if(iter->get_turnout_id())
301 TurnoutMap::const_iterator i = trnts.find(iter->get_turnout_id());
310 iter = iter.next(path);
314 bool Route::has_track(Track &t) const
316 return tracks.count(&t);
319 void Route::save(list<DataFile::Statement> &st) const
321 st.push_back((DataFile::Statement("name"), name));
322 for(map<unsigned, int>::const_iterator i=turnouts.begin(); i!=turnouts.end(); ++i)
323 st.push_back((DataFile::Statement("turnout"), i->first, i->second));
326 unsigned Route::check_validity(Track &trk) const
329 for(set<Track *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
331 int epi=(*i)->get_endpoint_by_link(trk);
334 // Linked to an existing track - good
336 const vector<TrackType::Endpoint> &endpoints = (*i)->get_type().get_endpoints();
337 if(unsigned tid=(*i)->get_turnout_id())
339 int r = get_turnout(tid);
342 // Linking to a turnout with path set is only good if we're continuing that path
343 if(endpoints[epi].paths&(1<<r))
348 // Linked to a turnout with no path set - find out other linked tracks
350 const vector<Track *> &links = (*i)->get_links();
352 for(unsigned k=0; k<endpoints.size(); ++k)
353 if(tracks.count(links[k]))
359 // Only good if at most one other track is linked to the turnout
363 if(epj>=0 && !(endpoints[epi].paths&endpoints[epj].paths))
364 // Impossible path through the turnout - not good
370 // Linked to something linear - good
378 void Route::track_removed(Track &t)
383 Route *Route::find(Track &from, unsigned ep, Track &to)
385 return create_route(from, ep, TrackMatch(to));
388 Route *Route::find(Track &from, unsigned ep, const Route &to)
390 return create_route(from, ep, TrackInSet(to.get_tracks()));
393 Route *Route::find(Track &from, unsigned ep, const set<Track *> &to)
395 return create_route(from, ep, TrackInSet(to));
399 Route::Loader::Loader(Route &r):
400 DataFile::BasicLoader<Route>(r)
402 add("name", &Route::name);
403 add("turnout", &Loader::turnout);
406 void Route::Loader::finish()
408 const set<Track *> <racks = obj.layout.get_tracks();
409 for(set<Track *>::const_iterator i=ltracks.begin(); i!=ltracks.end(); ++i)
411 unsigned tid = (*i)->get_turnout_id();
415 TurnoutMap::iterator j = turnouts.find(tid);
416 if(j==turnouts.end())
419 unsigned path_mask = 1<<j->second;
420 const vector<TrackType::Endpoint> &eps = (*i)->get_type().get_endpoints();
421 for(unsigned k=0; k<eps.size(); ++k)
422 if(eps[k].paths&path_mask)
424 Track *link = (*i)->get_link(k);
425 if(!obj.tracks.count(link))
426 obj.add_track_chain(*link, link->get_endpoint_by_link(**i), turnouts);
427 if(!obj.tracks.count(*i))
428 obj.add_track_chain(**i, k, turnouts);
436 void Route::Loader::turnout(unsigned id, unsigned path)
441 } // namespace Marklin