X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Flibr2c2%2Ftrack.cpp;h=8ecb9b9bac00833eaa70190982fcb1933b43e9ae;hb=d41f66805bc9fe0b33e3d46b47f52e67b5782028;hp=697e67843080ec68fba6898e3ad3eae81d3d60d4;hpb=107a7f787d406f1f664c4986557f9a896e0845ea;p=r2c2.git diff --git a/source/libr2c2/track.cpp b/source/libr2c2/track.cpp index 697e678..8ecb9b9 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,12 +85,14 @@ Block &Track::get_block() const void Track::set_position(const Vector &p) { position = p; + signal_moved.emit(); propagate_slope(); } void Track::set_rotation(const Angle &r) { rotation = wrap_positive(r); + signal_moved.emit(); } void Track::set_tilt(const Angle &t) @@ -84,6 +102,7 @@ void Track::set_tilt(const Angle &t) tilt = t; slope = tan(tilt)*type.get_path_length(0); + signal_moved.emit(); propagate_slope(); } @@ -125,6 +144,8 @@ void Track::check_slope() position.z = epp.z-slope; } } + + signal_moved.emit(); } void Track::set_turnout_id(unsigned i) @@ -148,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) @@ -164,31 +183,31 @@ 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 +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); } @@ -225,12 +244,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; } } @@ -326,6 +345,28 @@ bool Track::break_link(unsigned i) 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)); @@ -369,12 +410,12 @@ Track::Loader::Loader(Track &t): 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) @@ -384,16 +425,12 @@ void Track::Loader::sensor_id(unsigned id) void Track::Loader::slope(float s) { - tilt(atan(s/obj.type.get_path_length(0))); + obj.set_tilt(Geometry::atan(s/obj.type.get_path_length(0))); } void Track::Loader::tilt(float t) { - if(obj.links.size()!=2) - return; - - obj.tilt = Angle::from_radians(t); - obj.slope = tan(t)*obj.type.get_path_length(0); + obj.set_tilt(Angle::from_radians(t)); } void Track::Loader::turnout_id(unsigned id)