X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Flibr2c2%2Ftrainroutemetric.cpp;h=1fc4fcea2389d917ecaa90627791801dc0ee46e2;hb=5b71cb905051d654c150cb0e098cade0cb502105;hp=7e4d4856fe91a484f794470ce8a2e66a03151b96;hpb=3dd660ffad729fbd6e75e6401f5c7f27b9013faf;p=r2c2.git diff --git a/source/libr2c2/trainroutemetric.cpp b/source/libr2c2/trainroutemetric.cpp index 7e4d485..1fc4fce 100644 --- a/source/libr2c2/trainroutemetric.cpp +++ b/source/libr2c2/trainroutemetric.cpp @@ -9,6 +9,9 @@ namespace R2C2 { TrainRouteMetric::TrainRouteMetric(const TrackChain &tc, TrackChain::Direction dir) { + /* Initialize goals for tracks in the target chain. We travel outwards from + the target in the search phase so the iters appear to point the wrong way. */ + TrackChain::Direction reverse_dir = (dir==TrackChain::DOWN ? TrackChain::UP : TrackChain::DOWN); const TrackChain::TrackSet &ctracks = tc.get_tracks(); for(TrackChain::TrackSet::const_iterator i=ctracks.begin(); i!=ctracks.end(); ++i) { @@ -31,6 +34,9 @@ TrainRouteMetric::TrainRouteMetric(const TrackChain &tc, TrackChain::Direction d queue.push_back(i->track); } + /* Use Dijkstra's algorithm to find the shortest distance from the goal to + every reachable track in the layout. Entry points here become exit points + when looking up distances to the goal. */ while(!queue.empty()) { TrackIter track = queue.front(); @@ -62,6 +68,22 @@ void TrainRouteMetric::chain_to(const TrainRouteMetric &metric) i->base_distance = metric.get_distance_from(*i->track.track(), i->track.entry()); } +float TrainRouteMetric::get_distance_from(const Track &track) const +{ + map::const_iterator i = tracks.lower_bound(Key(&track, 0)); + map::const_iterator j = tracks.upper_bound(Key(&track, 255)); + + float result = -1; + for(; i!=j; ++i) + { + float d = i->second.distance+i->second.goal->base_distance; + if(result<0 || d::const_iterator i = tracks.find(Key(&track, exit));