]> git.tdb.fi Git - r2c2.git/blobdiff - source/libr2c2/track.cpp
Add a utility function to get path length from Track
[r2c2.git] / source / libr2c2 / track.cpp
index 26fb59e0fa7e756b619ad718d090dec6b6f05314..4708db5d40de6c79d26de28ef61af696afe6f48d 100644 (file)
@@ -5,11 +5,26 @@
 #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)<a2->get_offset_from_endpoint(entry); }
+};
+
+}
+
 namespace R2C2 {
 
 Track::Track(Layout &l, const TrackType &t):
@@ -154,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)
@@ -170,31 +183,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
 {
-       TrackPoint p = type.get_point(epi, path, d);
+       if(p<0)
+               p = active_path;
+       return type.get_path_length(p);
+}
+
+OrientedPoint Track::get_point(unsigned epi, unsigned path, float d) const
+{
+       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);
 }
@@ -231,12 +251,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;
                }
        }
@@ -347,6 +367,13 @@ void Track::remove_attachment(TrackAttachment &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<DataFile::Statement> &st) const
 {
        st.push_back((DataFile::Statement("position"), position.x, position.y, position.z));