3 #include "trackchain.h"
4 #include "trainroutemetric.h"
10 TrainRouteMetric::TrainRouteMetric(const TrackChain &tc)
12 const TrackChain::TrackSet &ctracks = tc.get_tracks();
13 for(TrackChain::TrackSet::const_iterator i=ctracks.begin(); i!=ctracks.end(); ++i)
15 unsigned nls = (*i)->get_n_link_slots();
16 for(unsigned j=0; j<nls; ++j)
17 if(Track *link = (*i)->get_link(j))
18 if(!ctracks.count(link))
19 goals.push_back(TrackIter(*i, j));
22 list<TrackIter> queue;
23 for(vector<Goal>::iterator i=goals.begin(); i!=goals.end(); ++i)
25 tracks[Key(i->track.track(), i->track.entry())] = Data(0, &*i);
26 queue.push_back(i->track);
31 TrackIter track = queue.front();
33 const Data &data = tracks[Key(track.track(), track.entry())];
35 const TrackType::Endpoint &ep = track.endpoint();
36 for(unsigned i=0; ep.paths>>i; ++i)
39 TrackIter next = track.next(i);
43 Data &target = tracks[Key(next.track(), next.entry())];
44 float dist = data.distance+track->get_type().get_path_length(i);
45 if(target.distance<0 || target.distance>dist)
47 target = Data(dist, data.goal);
48 queue.push_back(next);
54 void TrainRouteMetric::chain_to(const TrainRouteMetric &metric)
56 for(vector<Goal>::iterator i=goals.begin(); i!=goals.end(); ++i)
57 i->base_distance = metric.get_distance_from(*i->track.track(), i->track.entry());
60 float TrainRouteMetric::get_distance_from(const Track &track, unsigned exit) const
62 map<Key, Data>::const_iterator i = tracks.find(Key(&track, exit));
66 return i->second.distance+i->second.goal->base_distance;
70 TrainRouteMetric::Goal::Goal():
74 TrainRouteMetric::Goal::Goal(const TrackIter &t):
80 TrainRouteMetric::Data::Data():
85 TrainRouteMetric::Data::Data(float d, const Goal *g):