X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Flibmarklin%2Ftracktype.cpp;h=c6757cb221b12b9a206bdf6b0295f13fb835c789;hb=02c9a9779954d993cb73fe5f7a72b0847e87f633;hp=70d031699ff71ed3fcca68b28b2b254a45f064f4;hpb=52cbe8d99669f843f8f75c51128e2748584dd03a;p=r2c2.git diff --git a/source/libmarklin/tracktype.cpp b/source/libmarklin/tracktype.cpp index 70d0316..c6757cb 100644 --- a/source/libmarklin/tracktype.cpp +++ b/source/libmarklin/tracktype.cpp @@ -1,3 +1,10 @@ +/* $Id$ + +This file is part of the MSP Märklin suite +Copyright © 2006-2009 Mikkosoft Productions, Mikko Rasa +Distributed under the GPL +*/ + #include #include "tracktype.h" @@ -11,23 +18,29 @@ TrackType::TrackType(unsigned a): float TrackType::get_total_length() const { - float len=0; + return get_path_length(-1); +} + +float TrackType::get_path_length(int p) const +{ + float len = 0; for(vector::const_iterator i=parts.begin(); i!=parts.end(); ++i) - { - float l=i->length; - if(i->radius) - l*=i->radius; - len+=l; - } + if(p<0 || i->path==static_cast(p)) + { + float l = i->length; + if(i->radius) + l *= abs(i->radius); + len += l; + } return len; } -unsigned TrackType::get_n_routes() const +unsigned TrackType::get_n_paths() const { - unsigned n=1; + unsigned n = 1; for(vector::const_iterator i=parts.begin(); i!=parts.end(); ++i) - if(i->route>=n) - n=i->route+1; + if(i->path>=n) + n = i->path+1; return n; } @@ -40,29 +53,29 @@ void TrackType::collect_endpoints() for(vector::iterator i=endpoints.begin(); i!=endpoints.end();) { - bool rm=false; + bool rm = false; for(vector::iterator j=i+1; j!=endpoints.end();) { - float dx=i->x-j->x; - float dy=i->y-j->y; + float dx = i->pos.x-j->pos.x; + float dy = i->pos.y-j->pos.y; if(dx*dx+dy*dy<0.0001) { - float da=i->dir-j->dir; + float da = i->dir-j->dir; if(da<-M_PI) - da+=M_PI*2; + da += M_PI*2; if(da>M_PI) - da-=M_PI*2; + da -= M_PI*2; if(da<-3.1 || da>3.1) - rm=true; - i->routes|=j->routes; - j=endpoints.erase(j); + rm = true; + i->paths |= j->paths; + j = endpoints.erase(j); } else ++j; } if(rm) - i=endpoints.erase(i); + i = endpoints.erase(i); else ++i; } @@ -70,7 +83,7 @@ void TrackType::collect_endpoints() TrackType::Loader::Loader(TrackType &t): - ttype(t) + Msp::DataFile::BasicLoader(t) { add("description", &TrackType::description); add("part", &Loader::part); @@ -78,14 +91,14 @@ TrackType::Loader::Loader(TrackType &t): void TrackType::Loader::finish() { - ttype.collect_endpoints(); + obj.collect_endpoints(); } void TrackType::Loader::part() { TrackPart p; load_sub(p); - ttype.parts.push_back(p); + obj.parts.push_back(p); } } // namespace Marklin