X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Flibr2c2%2Ftrack.cpp;h=4708db5d40de6c79d26de28ef61af696afe6f48d;hb=a5e1363ceac1ab552849640c9021fcda362c4416;hp=ecb86bbad33ad02e20699a05ef7655eb3d2d30a0;hpb=2922bc5d2ba68979d186fd09df8b4ec6ba98402b;p=r2c2.git diff --git a/source/libr2c2/track.cpp b/source/libr2c2/track.cpp index ecb86bb..4708db5 100644 --- a/source/libr2c2/track.cpp +++ b/source/libr2c2/track.cpp @@ -1,14 +1,30 @@ #include +#include #include "block.h" #include "catalogue.h" #include "driver.h" #include "layout.h" #include "track.h" +#include "trackattachment.h" #include "tracktype.h" using namespace std; using namespace Msp; +namespace { + +struct AttachmentCompare +{ + unsigned entry; + + AttachmentCompare(unsigned e): entry(e) { } + + bool operator()(const R2C2::TrackAttachment *a1, const R2C2::TrackAttachment *a2) const + { return a1->get_offset_from_endpoint(entry)get_offset_from_endpoint(entry); } +}; + +} + namespace R2C2 { Track::Track(Layout &l, const TrackType &t): @@ -69,22 +85,25 @@ 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(); + signal_moved.emit(); + propagate_slope(); } void Track::set_rotation(const Angle &r) { rotation = wrap_positive(r); + signal_moved.emit(); } -void Track::set_slope(float s) +void Track::set_tilt(const Angle &t) { if(links.size()!=2) return; - slope = s; + tilt = t; + slope = tan(tilt)*type.get_path_length(0); + signal_moved.emit(); + propagate_slope(); } void Track::set_flex(bool f) @@ -92,6 +111,13 @@ void Track::set_flex(bool f) flex = f; } +void Track::propagate_slope() +{ + for(vector::const_iterator i=links.begin(); i!=links.end(); ++i) + if(*i) + (*i)->check_slope(); +} + void Track::check_slope() { if(links.size()!=2) @@ -103,6 +129,7 @@ void Track::check_slope() Vector epp1 = links[1]->get_snap_node(links[1]->get_link_slot(*this)).position; position.z = epp0.z; slope = epp1.z-position.z; + tilt = Geometry::atan(slope/type.get_path_length(0)); } else { @@ -117,6 +144,8 @@ void Track::check_slope() position.z = epp.z-slope; } } + + signal_moved.emit(); } void Track::set_turnout_id(unsigned i) @@ -140,8 +169,6 @@ void Track::set_sensor_id(unsigned i) sensor_id = i; layout.create_blocks(*this); - if(layout.has_driver() && sensor_id) - layout.get_driver().add_sensor(sensor_id); } void Track::set_active_path(unsigned p) @@ -151,36 +178,43 @@ void Track::set_active_path(unsigned p) if(!(type.get_paths()&(1< &st) const { st.push_back((DataFile::Statement("position"), position.x, position.y, position.z)); st.push_back((DataFile::Statement("rotation"), rotation.radians())); - st.push_back((DataFile::Statement("slope"), slope)); + st.push_back((DataFile::Statement("tilt"), tilt.radians())); if(turnout_id) st.push_back((DataFile::Statement("turnout_id"), turnout_id)); if(sensor_id) @@ -350,20 +406,23 @@ Track::Loader::Loader(Track &t): { add("position", &Loader::position); add("rotation", &Loader::rotation); - add("slope", &Track::slope); + add("tilt", &Loader::tilt); add("turnout_id", &Loader::turnout_id); add("sensor_id", &Loader::sensor_id); add("flex", &Track::flex); + + // deprecated + add("slope", &Loader::slope); } void Track::Loader::position(float x, float y, float z) { - obj.position = Vector(x, y, z); + obj.set_position(Vector(x, y, z)); } void Track::Loader::rotation(float r) { - obj.rotation = Angle::from_radians(r); + obj.set_rotation(Angle::from_radians(r)); } void Track::Loader::sensor_id(unsigned id) @@ -371,6 +430,16 @@ void Track::Loader::sensor_id(unsigned id) obj.set_sensor_id(id); } +void Track::Loader::slope(float s) +{ + obj.set_tilt(Geometry::atan(s/obj.type.get_path_length(0))); +} + +void Track::Loader::tilt(float t) +{ + obj.set_tilt(Angle::from_radians(t)); +} + void Track::Loader::turnout_id(unsigned id) { obj.set_turnout_id(id);