X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Flibmarklin%2Ftracktype.cpp;h=5bbd40a411e07632e161dbb9d1a44a099dfeb41e;hb=651698847d5293cfb15b6fb23a394701388c0151;hp=0e84a3125ced9bea789757b3e0e92bd880e53a04;hpb=3e9c210ddc036cd015228504cc0803c909e27f84;p=r2c2.git diff --git a/source/libmarklin/tracktype.cpp b/source/libmarklin/tracktype.cpp index 0e84a31..5bbd40a 100644 --- a/source/libmarklin/tracktype.cpp +++ b/source/libmarklin/tracktype.cpp @@ -1,7 +1,7 @@ /* $Id$ This file is part of the MSP Märklin suite -Copyright © 2006-2008 Mikkosoft Productions, Mikko Rasa +Copyright © 2006-2010 Mikkosoft Productions, Mikko Rasa Distributed under the GPL */ @@ -18,32 +18,49 @@ TrackType::TrackType(unsigned a): float TrackType::get_total_length() const { - return get_route_length(-1); + return get_path_length(-1); } -float TrackType::get_route_length(int r) const +float TrackType::get_path_length(int p) const { - float len=0; + float len = 0; for(vector::const_iterator i=parts.begin(); i!=parts.end(); ++i) - if(r<0 || i->route==static_cast(r)) + if(p<0 || i->path==static_cast(p)) { - float l=i->length; + float l = i->length; if(i->radius) - l*=abs(i->radius); - len+=l; + l *= abs(i->radius); + len += l; } return len; } -unsigned TrackType::get_n_routes() const +unsigned TrackType::get_paths() const { - unsigned n=1; + unsigned mask = 0; for(vector::const_iterator i=parts.begin(); i!=parts.end(); ++i) - if(i->route>=n) - n=i->route+1; + mask |= 1<path; + return mask; +} + +unsigned TrackType::get_n_paths() const +{ + unsigned n = 0; + for(unsigned mask = get_paths(); mask; ++n) + mask &= mask-1; return n; } +bool TrackType::is_turnout() const +{ + return endpoints.size()>2; +} + +bool TrackType::is_double_address() const +{ + return get_n_paths()>2; +} + void TrackType::collect_endpoints() { endpoints.clear(); @@ -53,29 +70,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; } @@ -83,7 +100,7 @@ void TrackType::collect_endpoints() TrackType::Loader::Loader(TrackType &t): - ttype(t) + Msp::DataFile::BasicLoader(t) { add("description", &TrackType::description); add("part", &Loader::part); @@ -91,14 +108,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