X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Flibr2c2%2Ftrack.cpp;h=7b5e1a2315a81f079fbec8670a1905bfae2f9838;hb=7a36d396eded897c421424905b2c938d770df341;hp=b679af783867f72746793f205179f004a9e02baa;hpb=2910db1364914c0ab98a0f80250cc39137821577;p=r2c2.git diff --git a/source/libr2c2/track.cpp b/source/libr2c2/track.cpp index b679af7..7b5e1a2 100644 --- a/source/libr2c2/track.cpp +++ b/source/libr2c2/track.cpp @@ -69,15 +69,14 @@ Block &Track::get_block() const void Track::set_position(const Vector &p) { position = p; + for(vector::const_iterator i=links.begin(); i!=links.end(); ++i) + if(*i) + (*i)->check_slope(); } -void Track::set_rotation(float r) +void Track::set_rotation(const Angle &r) { - rotation = r; - while(rotation<0) - rotation += M_PI*2; - while(rotation>M_PI*2) - rotation -= M_PI*2; + rotation = wrap_positive(r); } void Track::set_slope(float s) @@ -100,8 +99,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 +109,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 +154,74 @@ 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); + p.pos = position+rotated_vector(p.pos, rotation); + 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; + } + } - 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; 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,58 +250,78 @@ 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; ilinks[j] = this; + check_slope(); + 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 { - Vector local_start(start.x-position.x, start.y-position.y, start.z-position.z); - float c = cos(rotation); - float s = sin(rotation); - local_start = Vector(c*local_start.x+s*local_start.y, c*local_start.y-s*local_start.x, local_start.z); - Vector local_ray(c*ray.x+s*ray.y, c*ray.y-s*ray.x, ray.z); + Transform trans = Transform::rotation(-rotation, Vector(0, 0, 1)); + Vector local_start = trans.transform(start-position); + Vector local_ray = trans.transform(ray); float width = layout.get_catalogue().get_ballast_profile().get_width(); @@ -364,7 +331,7 @@ bool Track::collide_ray(const Vector &start, const Vector &ray) const void Track::save(list &st) const { st.push_back((DataFile::Statement("position"), position.x, position.y, position.z)); - st.push_back((DataFile::Statement("rotation"), rotation)); + st.push_back((DataFile::Statement("rotation"), rotation.radians())); st.push_back((DataFile::Statement("slope"), slope)); if(turnout_id) st.push_back((DataFile::Statement("turnout_id"), turnout_id)); @@ -406,7 +373,7 @@ void Track::Loader::position(float x, float y, float z) void Track::Loader::rotation(float r) { - obj.rotation = r; + obj.rotation = Angle::from_radians(r); } void Track::Loader::sensor_id(unsigned id)