X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Flibr2c2%2Ftracktype.cpp;h=59b8f9ca275b4fe508b94fe77c32ec220ab83472;hb=145ba6391812e2b1040a78a8f0fda5888bfea610;hp=bf038c52d76c2d19576e5b9a1b30e059a4837306;hpb=e621dd4120cb253417167b4295e436cee095ccb0;p=r2c2.git diff --git a/source/libr2c2/tracktype.cpp b/source/libr2c2/tracktype.cpp index bf038c5..59b8f9c 100644 --- a/source/libr2c2/tracktype.cpp +++ b/source/libr2c2/tracktype.cpp @@ -1,4 +1,5 @@ #include +#include #include "tracktype.h" using namespace std; @@ -6,8 +7,9 @@ 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) { } @@ -42,6 +44,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; @@ -60,7 +92,7 @@ const TrackType::Endpoint &TrackType::get_endpoint(unsigned i) const 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 out_of_range("TrackType::get_point"); @@ -75,10 +107,9 @@ TrackPoint TrackType::get_point(unsigned epi, unsigned path, float d) const 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; @@ -96,9 +127,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,15 +144,15 @@ TrackPoint TrackType::get_point(unsigned epi, unsigned path, float d) const } } -TrackPoint TrackType::get_nearest_point(const Vector &p) const +OrientedPoint TrackType::get_nearest_point(const Vector &p) const { - TrackPoint result; + OrientedPoint result; float dist = -1; for(vector::const_iterator i=parts.begin(); i!=parts.end(); ++i) { - TrackPoint n = i->get_nearest_point(p); - float d = distance(n.pos, p); + OrientedPoint n = i->get_nearest_point(p); + float d = distance(n.position, p); if(d::const_iterator i=parts.begin(); i!=parts.end(); ++i) - if(i->collide_ray(start, dir, width)) - return true; - - return false; -} - void TrackType::collect_endpoints() { endpoints.clear(); @@ -154,23 +176,18 @@ void TrackType::collect_endpoints() for(unsigned j=0; jget_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; @@ -179,24 +196,23 @@ 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::ObjectLoader(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); @@ -205,6 +221,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()