X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Flibr2c2%2Ftracktype.cpp;h=3c6876b0cc8b5bd72a7e0f476f45b6d631f2aa6a;hb=2875ea548e0567ad2ce1b8f9bda58e6452813204;hp=1679dc3f93f8a736efa4f54718cb7a6040bb9db3;hpb=247742fbc1c27bfc9fdef4630afcdc2408cdd550;p=r2c2.git diff --git a/source/libr2c2/tracktype.cpp b/source/libr2c2/tracktype.cpp index 1679dc3..3c6876b 100644 --- a/source/libr2c2/tracktype.cpp +++ b/source/libr2c2/tracktype.cpp @@ -1,11 +1,6 @@ -/* $Id$ - -This file is part of R²C² -Copyright © 2006-2010 Mikkosoft Productions, Mikko Rasa -Distributed under the GPL -*/ - #include +#include +#include "trackappearance.h" #include "tracktype.h" using namespace std; @@ -13,12 +8,18 @@ using namespace Msp; namespace R2C2 { -TrackType::TrackType(const ArticleNumber &an): - art_nr(an), +TrackType::TrackType(const ArticleNumber &an, const TrackAppearance &ta): + ObjectType(an), + appearance(ta), state_bits(0), autofit_preference(1) { } +float TrackType::get_gauge() const +{ + return appearance.get_gauge(); +} + float TrackType::get_total_length() const { return get_path_length(-1); @@ -49,6 +50,36 @@ unsigned TrackType::get_n_paths() const return n; } +unsigned TrackType::coerce_path(unsigned entry, unsigned path) const +{ + const Endpoint &ep = get_endpoint(entry); + if(ep.has_path(path)) + return path; + + unsigned paths = get_paths(); + if(paths>>(1<>p; p+=step) + if(ep.has_path(p)) + return p; + } + + // Find an endpoint that's connected to the entry and has the requested path + for(vector::const_iterator i=endpoints.begin(); i!=endpoints.end(); ++i) + if(i->has_path(path) && i->has_common_paths(ep)) + { + unsigned p = 1; + for(unsigned m=i->paths&ep.paths; m>>p; ++p) ; + return p-1; + } + + // TODO crossings fall here + throw logic_error("TrackType::coerce_path"); +} + bool TrackType::is_turnout() const { return endpoints.size()>2; @@ -62,30 +93,29 @@ bool TrackType::is_dead_end() const const TrackType::Endpoint &TrackType::get_endpoint(unsigned i) const { if(i>=endpoints.size()) - throw InvalidParameterValue("Endpoint index out of range"); + throw out_of_range("TrackType::get_endpoint"); return endpoints[i]; } -TrackPoint TrackType::get_point(unsigned epi, unsigned path, float d) const +OrientedPoint TrackType::get_point(unsigned epi, unsigned path, float d) const { if(epi>=endpoints.size()) - throw InvalidParameterValue("Endpoint index out of range"); + throw out_of_range("TrackType::get_point"); const TrackPart *part = 0; unsigned part_ep = 0; for(vector::const_iterator i=parts.begin(); i!=parts.end(); ++i) { - if((endpoints[epi].paths&(1<get_path()!=path) + if(endpoints[epi].has_path(path) && i->get_path()!=path) continue; unsigned n_part_eps = (i->is_dead_end() ? 1 : 2); for(unsigned j=0; jget_point(j ? i->get_length() : 0); - float dx = p.pos.x-endpoints[epi].pos.x; - float dy = p.pos.y-endpoints[epi].pos.y; - if(dx*dx+dy*dy<1e-6) + OrientedPoint p = i->get_point(j ? i->get_length() : 0); + Vector span = p.position-endpoints[epi].pos; + if(dot(span, span)<1e-6) { part = &*i; part_ep = j; @@ -94,7 +124,7 @@ TrackPoint TrackType::get_point(unsigned epi, unsigned path, float d) const } if(!part) - throw Exception("Internal error (endpoint does not match any part)"); + throw logic_error("internal error (endpoint does not match any part)"); while(1) { @@ -103,9 +133,9 @@ TrackPoint TrackType::get_point(unsigned epi, unsigned path, float d) const { if(part_ep==1) d = plen-d; - TrackPoint p = part->get_point(d); + OrientedPoint p = part->get_point(d); if(part_ep==1) - p.dir += M_PI; + p.rotation += Angle::half_turn(); return p; } else @@ -113,13 +143,32 @@ TrackPoint TrackType::get_point(unsigned epi, unsigned path, float d) const d -= plen; TrackPart *next = part->get_link(1-part_ep); if(!next) - throw InvalidParameterValue("Distance out of range"); + throw invalid_argument("TrackType::get_point"); part_ep = (next->get_link(0)==part ? 0 : 1); part = next; } } } +OrientedPoint TrackType::get_nearest_point(const Vector &p) const +{ + OrientedPoint result; + float dist = -1; + + for(vector::const_iterator i=parts.begin(); i!=parts.end(); ++i) + { + OrientedPoint n = i->get_nearest_point(p); + float d = distance(n.position, p); + if(dget_link(j)) { - TrackPoint p = i->get_point(j ? i->get_length() : 0); + OrientedPoint p = i->get_point(j ? i->get_length() : 0); if(j==0) - p.dir += M_PI; + p.rotation += Angle::half_turn(); bool found = false; for(vector::iterator k=endpoints.begin(); k!=endpoints.end(); ++k) { - float dx = k->pos.x-p.pos.x; - float dy = k->pos.y-p.pos.y; + Vector d = k->pos-p.position; - float da = k->dir-p.dir; - while(da>M_PI) - da -= M_PI*2; - while(da<-M_PI) - da += M_PI*2; + Angle da = wrap_balanced(k->dir-p.rotation); - if(dx*dx+dy*dy<1e-6 && da>-0.01 && da<0.01) + if(dot(d, d)<1e-6 && abs(da).radians()<0.01) { k->paths |= 1<get_path(); found = true; @@ -158,24 +202,24 @@ void TrackType::collect_endpoints() } if(!found) - endpoints.push_back(Endpoint(p.pos.x, p.pos.y, p.dir, 1<get_path())); + endpoints.push_back(Endpoint(p.position.x, p.position.y, p.rotation, 1<get_path())); } } } -TrackType::Endpoint::Endpoint(float x, float y, float d, unsigned p): - pos(x, y), +TrackType::Endpoint::Endpoint(float x, float y, const Angle &d, unsigned p): + pos(x, y, 0), dir(d), paths(p) { } TrackType::Loader::Loader(TrackType &t): - Msp::DataFile::BasicLoader(t), + DataFile::DerivedObjectLoader(t), state_bits_set(false) { add("autofit_preference", &TrackType::autofit_preference); - add("description", &TrackType::description); + add("object", &TrackType::object); add("state_bits", &Loader::state_bits); add("part", &Loader::part); } @@ -183,6 +227,13 @@ TrackType::Loader::Loader(TrackType &t): void TrackType::Loader::finish() { obj.collect_endpoints(); + vector shapes; + for(vector::iterator i=obj.parts.begin(); i!=obj.parts.end(); ++i) + { + i->create_shape(); + shapes.push_back(&i->get_shape()); + } + obj.shape = Geometry::Union::from_iterator_range(shapes.begin(), shapes.end()).clone(); } void TrackType::Loader::part()