X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Flibr2c2%2Ftrack.cpp;h=7b9a4a2d70b30febaf96ebf917cb284153ba44ac;hb=0b75458245997b9df6da47cc4534341c8426084b;hp=b679af783867f72746793f205179f004a9e02baa;hpb=2910db1364914c0ab98a0f80250cc39137821577;p=r2c2.git diff --git a/source/libr2c2/track.cpp b/source/libr2c2/track.cpp index b679af7..7b9a4a2 100644 --- a/source/libr2c2/track.cpp +++ b/source/libr2c2/track.cpp @@ -100,8 +100,8 @@ void Track::check_slope() if(links[0] && links[1]) { - Vector epp0 = links[0]->get_endpoint_position(links[0]->get_endpoint_by_link(*this)); - Vector epp1 = links[1]->get_endpoint_position(links[1]->get_endpoint_by_link(*this)); + Vector epp0 = links[0]->get_snap_node(links[0]->get_link_slot(*this)).position; + Vector epp1 = links[1]->get_snap_node(links[1]->get_link_slot(*this)).position; position.z = epp0.z; slope = epp1.z-position.z; } @@ -110,12 +110,12 @@ void Track::check_slope() slope = 0; if(links[0]) { - Vector epp = links[0]->get_endpoint_position(links[0]->get_endpoint_by_link(*this)); + Vector epp = links[0]->get_snap_node(links[0]->get_link_slot(*this)).position; position.z = epp.z; } else if(links[1]) { - Vector epp = links[1]->get_endpoint_position(links[1]->get_endpoint_by_link(*this)); + Vector epp = links[1]->get_snap_node(links[1]->get_link_slot(*this)).position; position.z = epp.z; } } @@ -155,115 +155,82 @@ void Track::set_active_path(unsigned p) layout.get_driver().set_turnout(turnout_id, p); } -int Track::get_endpoint_by_link(Track &other) const -{ - for(unsigned i=0; i &eps = type.get_endpoints(); - if(epi>=eps.size()) - throw out_of_range("Track::get_endpoint_position"); - - const TrackType::Endpoint &ep = eps[epi]; - + TrackPoint p = type.get_point(epi, path, d); float c = cos(rotation); float s = sin(rotation); - Vector p(position.x+c*ep.pos.x-s*ep.pos.y, position.y+s*ep.pos.x+c*ep.pos.y, position.z); - if(eps.size()==2 && epi==1) - p.z += slope; + p.pos = Vector(position.x+c*p.pos.x-s*p.pos.y, position.y+s*p.pos.x+c*p.pos.y, position.z); + p.dir += rotation; + if(type.get_endpoints().size()==2) + { + float len = type.get_path_length(path); + float grade = slope/len; + if(epi==0) + { + p.pos.z += grade*d; + p.grade = grade; + } + else + { + p.pos.z += slope-grade*d; + p.grade = -grade; + } + } + return p; } -float Track::get_endpoint_direction(unsigned epi) const +TrackPoint Track::get_point(unsigned epi, float d) const { - const vector &eps = type.get_endpoints(); - if(epi>=eps.size()) - throw out_of_range("Track::get_endpoint_direction"); - - const TrackType::Endpoint &ep = eps[epi]; - - return rotation+ep.dir; + return get_point(epi, active_path, d); } -bool Track::snap_to(Track &other, bool link, float limit) +unsigned Track::get_n_snap_nodes() const { - if(!limit || link) - { - limit = layout.get_catalogue().get_gauge(); - if(link && !flex && !other.get_flex()) - limit /= 10; - } - limit *= limit; + return type.get_endpoints().size(); +} +Snap Track::get_snap_node(unsigned i) const +{ const vector &eps = type.get_endpoints(); - const vector &other_eps = other.get_type().get_endpoints(); + if(i>=eps.size()) + throw out_of_range("Track::get_snap_node"); - for(unsigned i=0; i &eps = type.get_endpoints(); + if(Object::snap(sn, limit, what)) + return true; - for(unsigned i=0; i::iterator i=links.begin(); i!=links.end(); ++i) - if(*i==&trk) - { - *i = 0; - trk.break_link(*this); - // XXX Creates the blocks twice - layout.create_blocks(*this); - signal_link_changed.emit(i-links.begin(), 0); - return; - } + if(dynamic_cast(&other)) + return SNAP_NODE; + + return NO_SNAP; } -void Track::break_links() +unsigned Track::get_n_link_slots() const { - for(vector::iterator i=links.begin(); i!=links.end(); ++i) - if(Track *trk=*i) - { - *i = 0; - trk->break_link(*this); - } + return links.size(); } Track *Track::get_link(unsigned i) const @@ -303,49 +259,74 @@ Track *Track::get_link(unsigned i) const return links[i]; } -TrackPoint Track::get_point(unsigned epi, unsigned path, float d) const +int Track::get_link_slot(const Object &other) const { - TrackPoint p = type.get_point(epi, path, d); - float c = cos(rotation); - float s = sin(rotation); + for(unsigned i=0; i(&other); + if(!otrack) + return false; + + float limit = layout.get_catalogue().get_gauge(); + if(!flex && !otrack->get_flex()) + limit /= 10; + limit *= limit; + + unsigned nsn = get_n_snap_nodes(); + unsigned other_nsn = other.get_n_snap_nodes(); + for(unsigned i=0; iM_PI) + da -= M_PI*2; + + if(d.x*d.x+d.y*d.y-0.01 && da<0.01) + { + break_link(i); + links[i] = otrack; + otrack->links[j] = this; + layout.create_blocks(*this); + + signal_link_changed.emit(i, otrack); + otrack->signal_link_changed.emit(j, this); + return true; + } } } - return p; + return false; } -TrackPoint Track::get_point(unsigned epi, float d) const +bool Track::break_link(unsigned i) { - return get_point(epi, active_path, d); -} + if(i>=links.size()) + throw out_of_range("Track::break_link"); -TrackPoint Track::get_nearest_point(const Vector &p) const -{ - Vector local(p.x-position.x, p.y-position.y, p.z-position.z); - float c = cos(rotation); - float s = sin(rotation); - local = Vector(c*local.x+s*local.y, c*local.y-s*local.x, local.z); + Track *other = links[i]; + if(!other) + return false; + + links[i] = 0; + other->break_link(*this); + // XXX Creates the blocks twice, because the other track calls this too + layout.create_blocks(*this); + signal_link_changed.emit(i, 0); - TrackPoint tp = type.get_nearest_point(local); - tp.pos = Vector(position.x+tp.pos.x*c-tp.pos.y*s, position.y+tp.pos.y*c+tp.pos.x*s, position.z+tp.pos.z); - tp.dir += rotation; - return tp; + return true; } bool Track::collide_ray(const Vector &start, const Vector &ray) const