X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;ds=sidebyside;f=source%2Flibr2c2%2Ftrack.cpp;h=23f1b9e2b6da280d7179276de2d8e1d1afa1a112;hb=8a31a3ab3ab7abda30de0ed3a6d0753760f3bb1d;hp=71c570cee86ccd251d6269c080b1c619bac18054;hpb=4eea980c210c3f3bf5520c1e3d68426a4669db8a;p=r2c2.git diff --git a/source/libr2c2/track.cpp b/source/libr2c2/track.cpp index 71c570c..23f1b9e 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): @@ -24,12 +40,19 @@ Track::Track(Layout &l, const TrackType &t): path_changing(false) { if(type.is_turnout()) + { turnout_id = layout.allocate_turnout_id(); + if(layout.has_driver()) + { + Driver &driver = layout.get_driver(); + driver.add_turnout(turnout_id, type); + driver.signal_turnout.connect(sigc::mem_fun(this, &Track::turnout_event)); + } + } + layout.add(*this); - if(layout.has_driver()) - layout.get_driver().signal_turnout.connect(sigc::mem_fun(this, &Track::turnout_event)); for(unsigned paths = type.get_paths(); !(paths&1); ++active_path, paths>>=1) ; } @@ -37,6 +60,8 @@ Track::Track(Layout &l, const TrackType &t): Track::~Track() { break_links(); + if(layout.has_driver() && turnout_id) + layout.get_driver().remove_turnout(turnout_id); layout.remove(*this); } @@ -69,22 +94,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 +120,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 +138,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 +153,8 @@ void Track::check_slope() position.z = epp.z-slope; } } + + signal_moved.emit(); } void Track::set_turnout_id(unsigned i) @@ -126,11 +164,15 @@ void Track::set_turnout_id(unsigned i) if(!i) throw invalid_argument("Track::set_turnout_id"); + Driver *driver = (layout.has_driver() ? &layout.get_driver() : 0); + + if(driver && turnout_id) + driver->remove_turnout(turnout_id); turnout_id = i; layout.create_blocks(*this); layout.update_routes(); - if(layout.has_driver() && turnout_id) - layout.get_driver().add_turnout(turnout_id, type); + if(driver && turnout_id) + driver->add_turnout(turnout_id, type); } void Track::set_sensor_id(unsigned i) @@ -140,8 +182,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) @@ -156,32 +196,38 @@ void Track::set_active_path(unsigned p) layout.get_driver().set_turnout(turnout_id, p); } -TrackPoint Track::get_point(unsigned epi, unsigned path, float d) const +float Track::get_path_length(int p) const +{ + if(p<0) + p = active_path; + return type.get_path_length(p); +} + +OrientedPoint Track::get_point(unsigned epi, unsigned path, float d) const { - TrackPoint p = type.get_point(epi, path, d); + OrientedPoint p = type.get_point(epi, path, d); - p.pos = position+rotated_vector(p.pos, rotation); - p.dir += rotation; + p.position = position+rotated_vector(p.position, rotation); + p.rotation += rotation; if(type.get_endpoints().size()==2) { - float len = type.get_path_length(path); - float grade = slope/len; + float dz = tan(tilt)*d; if(epi==0) { - p.pos.z += grade*d; - p.grade = grade; + p.position.z += dz; + p.tilt = tilt; } else { - p.pos.z += slope-grade*d; - p.grade = -grade; + p.position.z += slope-dz; + p.tilt = -tilt; } } return p; } -TrackPoint Track::get_point(unsigned epi, float d) const +OrientedPoint Track::get_point(unsigned epi, float d) const { return get_point(epi, active_path, d); } @@ -218,12 +264,12 @@ bool Track::snap(Snap &sn, float limit, SnapType what) const { Vector local = rotated_vector(sn.position-position, -rotation); - TrackPoint tp = type.get_nearest_point(local); - Vector span = local-tp.pos; + OrientedPoint np = type.get_nearest_point(local); + Vector span = local-np.position; if(dot(span, span)<=limit*limit) { - sn.position = position+rotated_vector(tp.pos, rotation); - sn.rotation = tp.dir+rotation; + sn.position = position+rotated_vector(np.position, rotation); + sn.rotation = np.rotation+rotation; return true; } } @@ -311,19 +357,46 @@ bool Track::break_link(unsigned i) 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); + if(!other->break_link(*this)) + { + /* If the call doesn't succeed, it means that the other track already + broke the link and is calling us right now. Recreate blocks in the inner + call so it occurs before any signals are emitted. */ + layout.create_blocks(*this); + } + signal_link_changed.emit(i, 0); return true; } +void Track::add_attachment(TrackAttachment &a) +{ + if(find(attachments.begin(), attachments.end(), &a)!=attachments.end()) + throw key_error(&a); + attachments.push_back(&a); +} + +void Track::remove_attachment(TrackAttachment &a) +{ + AttachmentList::iterator i = find(attachments.begin(), attachments.end(), &a); + if(i==attachments.end()) + throw key_error(&a); + attachments.erase(i); +} + +Track::AttachmentList Track::get_attachments_ordered(unsigned epi) const +{ + AttachmentList result = attachments; + result.sort(AttachmentCompare(epi)); + return result; +} + 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.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) @@ -334,9 +407,6 @@ void Track::save(list &st) const void Track::turnout_event(unsigned addr, unsigned state) { - if(!turnout_id) - return; - if(addr==turnout_id) { active_path = state; @@ -351,20 +421,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) @@ -372,6 +445,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);