]> git.tdb.fi Git - r2c2.git/blobdiff - source/libr2c2/track.cpp
Make use of the mspmath library
[r2c2.git] / source / libr2c2 / track.cpp
index 6a6cbd97ee36f51d5ea3adf9031b9d984ea49007..7b5e1a2315a81f079fbec8670a1905bfae2f9838 100644 (file)
@@ -74,13 +74,9 @@ void Track::set_position(const Vector &p)
                        (*i)->check_slope();
 }
 
-void Track::set_rotation(float r)
+void Track::set_rotation(const Angle &r)
 {
-       rotation = r;
-       while(rotation<0)
-               rotation += M_PI*2;
-       while(rotation>M_PI*2)
-               rotation -= M_PI*2;
+       rotation = wrap_positive(r);
 }
 
 void Track::set_slope(float s)
@@ -161,10 +157,8 @@ void Track::set_active_path(unsigned p)
 TrackPoint Track::get_point(unsigned epi, unsigned path, float d) const
 {
        TrackPoint p = type.get_point(epi, path, d);
-       float c = cos(rotation);
-       float s = sin(rotation);
 
-       p.pos = Vector(position.x+c*p.pos.x-s*p.pos.y, position.y+s*p.pos.x+c*p.pos.y, position.z);
+       p.pos = position+rotated_vector(p.pos, rotation);
        p.dir += rotation;
        if(type.get_endpoints().size()==2)
        {
@@ -204,10 +198,7 @@ Snap Track::get_snap_node(unsigned i) const
        Snap result;
        const TrackType::Endpoint &ep = eps[i];
 
-       float c = cos(rotation);
-       float s = sin(rotation);
-
-       result.position = Vector(position.x+c*ep.pos.x-s*ep.pos.y, position.y+s*ep.pos.x+c*ep.pos.y, position.z);
+       result.position = position+rotated_vector(ep.pos, rotation);
        if(eps.size()==2 && i==1)
                result.position.z += slope;
 
@@ -223,16 +214,13 @@ bool Track::snap(Snap &sn, float limit, SnapType what) const
 
        if(what&SNAP_SEGMENT)
        {
-               Vector local(sn.position.x-position.x, sn.position.y-position.y, sn.position.z-position.z);
-               float c = cos(rotation);
-               float s = sin(rotation);
-               local = Vector(c*local.x+s*local.y, c*local.y-s*local.x, local.z);
+               Vector local = rotated_vector(sn.position-position, -rotation);
 
                TrackPoint tp = type.get_nearest_point(local);
-               Vector d(local.x-tp.pos.x, local.y-tp.pos.y, local.z-tp.pos.z);
-               if(d.x*d.x+d.y*d.y+d.z*d.z<=limit*limit)
+               Vector span = local-tp.pos;
+               if(dot(span, span)<=limit*limit)
                {
-                       sn.position = Vector(position.x+tp.pos.x*c-tp.pos.y*s, position.y+tp.pos.y*c+tp.pos.x*s, position.z+tp.pos.z);
+                       sn.position = position+rotated_vector(tp.pos, rotation);
                        sn.rotation = tp.dir+rotation;
                        return true;
                }
@@ -290,14 +278,10 @@ bool Track::link_to(Object &other)
                for(unsigned j=0; j<other_nsn; ++j)
                {
                        Snap osn = other.get_snap_node(j);
-                       Vector d(osn.position.x-sn.position.x, osn.position.y-sn.position.y, osn.position.z-sn.position.z);
-                       float da = osn.rotation-sn.rotation-M_PI;
-                       while(da<-M_PI)
-                               da += M_PI*2;
-                       while(da>M_PI)
-                               da -= M_PI*2;
-
-                       if(d.x*d.x+d.y*d.y<limit && d.z*d.z<limit && da>-0.01 && da<0.01)
+                       Vector span = osn.position-sn.position;
+                       Angle da = wrap_balanced(osn.rotation-sn.rotation-Angle::half_turn());
+
+                       if(dot(span, span)<limit && abs(da).radians()<0.01)
                        {
                                break_link(i);
                                links[i] = otrack;
@@ -335,11 +319,9 @@ bool Track::break_link(unsigned i)
 
 bool Track::collide_ray(const Vector &start, const Vector &ray) const
 {
-       Vector local_start(start.x-position.x, start.y-position.y, start.z-position.z);
-       float c = cos(rotation);
-       float s = sin(rotation);
-       local_start = Vector(c*local_start.x+s*local_start.y, c*local_start.y-s*local_start.x, local_start.z);
-       Vector local_ray(c*ray.x+s*ray.y, c*ray.y-s*ray.x, ray.z);
+       Transform trans = Transform::rotation(-rotation, Vector(0, 0, 1));
+       Vector local_start = trans.transform(start-position);
+       Vector local_ray = trans.transform(ray);
 
        float width = layout.get_catalogue().get_ballast_profile().get_width();
 
@@ -349,7 +331,7 @@ bool Track::collide_ray(const Vector &start, const Vector &ray) const
 void Track::save(list<DataFile::Statement> &st) const
 {
        st.push_back((DataFile::Statement("position"), position.x, position.y, position.z));
-       st.push_back((DataFile::Statement("rotation"), rotation));
+       st.push_back((DataFile::Statement("rotation"), rotation.radians()));
        st.push_back((DataFile::Statement("slope"), slope));
        if(turnout_id)
                st.push_back((DataFile::Statement("turnout_id"), turnout_id));
@@ -391,7 +373,7 @@ void Track::Loader::position(float x, float y, float z)
 
 void Track::Loader::rotation(float r)
 {
-       obj.rotation = r;
+       obj.rotation = Angle::from_radians(r);
 }
 
 void Track::Loader::sensor_id(unsigned id)