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 "tracktype.h"
20 using namespace Marklin;
22 typedef std::pair<const Track *, unsigned> Key;
32 track(0), ep(0), prev(0), dist(0)
35 Node(const Track &t, unsigned e):
36 track(&t), ep(e), prev(0), dist(0)
39 Node(const Track &t, unsigned e, Node &r, float d):
40 track(&t), ep(e), prev(&r), dist(prev->dist+d)
43 bool operator<(const Node &other) const
44 { return dist>other.dist; }
51 TrackMatch(const Track &t): track(t) { }
53 bool operator()(const Track &t) const { return &t==&track; }
60 TrackInRoute(const Route &r): route(r) { }
62 bool operator()(const Track &t) const { return route.get_tracks().count(&t); }
65 template<typename Pred>
66 list<const Track *> dijkstra(const Track &from, unsigned ep, const Pred &goal)
68 map<Key, Node> track_nodes;
69 priority_queue<Node> nodes;
72 nodes.push(Node(from, ep));
76 Node lowest = nodes.top();
79 Key key(lowest.track, lowest.ep);
80 if(track_nodes.count(key))
83 Node &ref = track_nodes[key] = lowest;
84 if(goal(*lowest.track))
90 const TrackType &type = lowest.track->get_type();
91 const vector<Endpoint> &eps = type.get_endpoints();
92 const vector<Track *> &links = lowest.track->get_links();
93 for(unsigned i=0; i<eps.size(); ++i)
95 if(i==lowest.ep || !links[i])
98 unsigned link_ep = links[i]->get_endpoint_by_link(*lowest.track);
99 if(track_nodes.count(Key(links[i], link_ep)))
102 unsigned mask = eps[i].paths&eps[lowest.ep].paths;
107 for(; !(mask&1); ++path, mask>>=1) ;
108 nodes.push(Node(*links[i], links[i]->get_endpoint_by_link(*lowest.track), ref, type.get_path_length(path)));
113 throw InvalidParameterValue("Could not find a route");
115 list<const Track *> result;
116 for(Node *node=final; node; node=node->prev)
117 result.push_front(node->track);
122 template<typename Pred>
123 Route *create_route(const Track &from, unsigned ep, const Pred &goal)
125 list<const Track *> tracks = dijkstra(from, ep, goal);
127 Route *route = new Route(from.get_layout());
128 for(list<const Track *>::iterator i=tracks.begin(); i!=tracks.end(); ++i)
129 route->add_track(**i);
131 route->set_name("Pathfinder");
132 route->set_temporary(true);
142 Route::Route(Layout &l):
146 layout.add_route(*this);
147 layout.signal_track_removed.connect(sigc::mem_fun(this, &Route::track_removed));
152 layout.remove_route(*this);
155 void Route::set_name(const string &n)
158 signal_name_changed.emit(name);
161 void Route::set_temporary(bool t)
166 int Route::get_turnout(unsigned id) const
168 map<unsigned, int>::const_iterator i = turnouts.find(id);
169 if(i!=turnouts.end())
174 void Route::add_track(const Track &trk)
176 if(tracks.count(&trk))
181 unsigned valid = check_validity(trk);
183 throw Exception("Not linked to existing tracks");
185 throw Exception("Branching routes not allowed");
187 throw Exception("Route must be smooth");
194 void Route::add_tracks(const set<const Track *> &trks)
196 set<const Track *> pending;
197 for(set<const Track *>::const_iterator i=trks.begin(); i!=trks.end(); ++i)
198 if(!tracks.count(*i))
201 while(!pending.empty())
204 for(set<const Track *>::const_iterator i=pending.begin(); i!=pending.end(); ++i)
205 if(tracks.empty() || check_validity(**i)==7)
214 throw Exception("Could not add all tracks to route");
220 void Route::add_track_chain(const Track &start, unsigned ep, const TurnoutMap &trnts)
222 const Track *track = &start;
225 if(track->get_type().is_dead_end())
228 if(tracks.count(track))
232 if(track->get_turnout_id())
234 TurnoutMap::const_iterator i = trnts.find(track->get_turnout_id());
243 unsigned out_ep = track->traverse(ep, path);
244 Track *next = track->get_links()[out_ep];
248 ep = next->get_endpoint_by_link(*track);
253 void Route::update_turnouts()
256 for(set<const Track *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
257 if(unsigned tid = (*i)->get_turnout_id())
261 const vector<Endpoint> &endpoints = (*i)->get_type().get_endpoints();
262 const vector<Track *> &links = (*i)->get_links();
264 // Build a combined path mask from linked endpoints
266 for(unsigned j=0; j<endpoints.size(); ++j)
268 if(!tracks.count(links[j]))
271 if(unsigned tid2 = links[j]->get_turnout_id())
273 const Endpoint &ep = links[j]->get_type().get_endpoints()[links[j]->get_endpoint_by_link(**i)];
274 int p = get_turnout(tid2);
275 if(p>=0 && !(ep.paths&(1<<p)))
277 // The linked track is a turnout and has a path which is incompatible with this endpoint
278 mask &= ~endpoints[j].paths;
282 mask &= endpoints[j].paths;
285 if(mask && !(mask&(mask-1)))
287 // Exactly one possible choice, set the path accordingly
289 for(; (mask && !(mask&1)); mask>>=1, ++path) ;
290 turnouts[tid] = path;
292 else if(!turnouts.count(tid))
293 // More than one possible choice, and no existing entry - set as undecided
297 // Remove any turnouts that do not exist in the route
298 for(map<unsigned, int>::iterator i=turnouts.begin(); i!=turnouts.end();)
300 if(!found.count(i->first))
307 void Route::save(list<DataFile::Statement> &st) const
309 st.push_back((DataFile::Statement("name"), name));
310 for(map<unsigned, int>::const_iterator i=turnouts.begin(); i!=turnouts.end(); ++i)
311 st.push_back((DataFile::Statement("turnout"), i->first, i->second));
314 unsigned Route::check_validity(const Track &trk) const
317 for(set<const Track *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
319 int epi=(*i)->get_endpoint_by_link(trk);
322 // Linked to an existing track - good
324 const vector<Endpoint> &endpoints = (*i)->get_type().get_endpoints();
325 if(unsigned tid=(*i)->get_turnout_id())
327 int r = get_turnout(tid);
330 // Linking to a turnout with path set is only good if we're continuing that path
331 if(endpoints[epi].paths&(1<<r))
336 // Linked to a turnout with no path set - find out other linked tracks
338 const vector<Track *> &links = (*i)->get_links();
340 for(unsigned k=0; k<endpoints.size(); ++k)
341 if(tracks.count(links[k]))
347 // Only good if at most one other track is linked to the turnout
351 if(epj>=0 && !(endpoints[epi].paths&endpoints[epj].paths))
352 // Impossible path through the turnout - not good
358 // Linked to something linear - good
366 void Route::track_removed(Track &t)
371 Route *Route::find(const Track &from, unsigned ep, const Track &to)
373 return create_route(from, ep, TrackMatch(to));
376 Route *Route::find(const Track &from, unsigned ep, const Route &to)
378 return create_route(from, ep, TrackInRoute(to));
382 Route::Loader::Loader(Route &r):
383 DataFile::BasicLoader<Route>(r)
385 add("name", &Route::name);
386 add("turnout", &Loader::turnout);
389 void Route::Loader::finish()
391 const set<Track *> <racks = obj.layout.get_tracks();
392 for(set<Track *>::const_iterator i=ltracks.begin(); i!=ltracks.end(); ++i)
394 unsigned tid = (*i)->get_turnout_id();
398 TurnoutMap::iterator j = turnouts.find(tid);
399 if(j==turnouts.end())
402 unsigned path_mask = 1<<j->second;
403 const vector<Endpoint> &eps = (*i)->get_type().get_endpoints();
404 for(unsigned k=0; k<eps.size(); ++k)
405 if(eps[k].paths&path_mask)
407 Track *link = (*i)->get_link(k);
408 if(!obj.tracks.count(link))
409 obj.add_track_chain(*link, link->get_endpoint_by_link(**i), turnouts);
410 if(!obj.tracks.count(*i))
411 obj.add_track_chain(**i, k, turnouts);
417 void Route::Loader::turnout(unsigned id, unsigned path)
422 } // namespace Marklin