X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;ds=sidebyside;f=source%2Flibr2c2%2Ftrack.cpp;h=23f1b9e2b6da280d7179276de2d8e1d1afa1a112;hb=2399146e4235d9923ee4256c413574d4395312e3;hp=43bacc3d8bc10f6a4653716064cbffb331e2ed0c;hpb=f23c8d8cbc4e72b45e3a719b2cf974ce35d054e9;p=r2c2.git diff --git a/source/libr2c2/track.cpp b/source/libr2c2/track.cpp index 43bacc3..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); } @@ -139,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) @@ -153,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) @@ -169,31 +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 grade = tan(tilt); + 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); } @@ -230,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; } } @@ -323,14 +357,41 @@ 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)); @@ -346,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;