]> git.tdb.fi Git - r2c2.git/commitdiff
Store the Transform in TrackPart instead of creating it on the fly
authorMikko Rasa <tdb@tdb.fi>
Wed, 2 Apr 2014 20:41:06 +0000 (23:41 +0300)
committerMikko Rasa <tdb@tdb.fi>
Wed, 2 Apr 2014 20:41:06 +0000 (23:41 +0300)
source/libr2c2/trackpart.cpp
source/libr2c2/trackpart.h

index 8aab4f2e88133b319a2eb3ef7b46cc5b9709370b..9622b28e7c8fd0fd283a6d152c04afbeb5faae68 100644 (file)
@@ -41,17 +41,15 @@ OrientedPoint TrackPart::get_point(float d) const
 {
        OrientedPoint result;
 
-       Transform dir_trans = Transform::rotation(dir, Vector(0, 0, 1));
        if(radius)
        {
                Angle a = Angle::from_radians(d/radius);
-               Vector r = dir_trans.transform(Vector(0, -radius, 0));
-               result.position = pos-r+Transform::rotation(a, Vector(0, 0, 1)).transform(r);
+               result.position = transform.transform(Vector(sin(a)*abs(radius), radius-cos(a)*radius, 0));
                result.rotation = dir+a;
        }
        else
        {
-               result.position = pos+dir_trans.transform(Vector(d, 0, 0));
+               result.position = transform.transform(Vector(d, 0, 0));
                result.rotation = dir;
        }
 
@@ -61,11 +59,9 @@ OrientedPoint TrackPart::get_point(float d) const
 OrientedPoint TrackPart::get_nearest_point(const Vector &p) const
 {
        OrientedPoint result;
-       Transform dir_trans = Transform::rotation(dir, Vector(0, 0, 1));
        if(radius)
        {
-               Vector r = dir_trans.transform(Vector(0, -radius, 0));
-               Vector v = p-pos+r;
+               Vector v = p-transform.transform(Vector(0, radius, 0));
                Angle a = Geometry::atan2(v.y, v.x)+Angle::quarter_turn()-dir;
                if(radius<0)
                        a = Angle::half_turn()-a;
@@ -73,13 +69,13 @@ OrientedPoint TrackPart::get_nearest_point(const Vector &p) const
                a = min(max(a, Angle::zero()), Angle::from_radians(length));
                if(radius<0)    
                        a = -a;
-               result.position = pos-r+Transform::rotation(a, Vector(0, 0, 1)).transform(r);
+               result.position = transform.transform(Vector(sin(a)*abs(radius), radius-cos(a)*radius, 0));
                result.rotation = dir+a;
        }
        else
        {
                Vector v = p-pos;
-               Vector dir_vec = dir_trans.transform(Vector(1, 0, 0));
+               Vector dir_vec = transform.transform_linear(Vector(1, 0, 0));
                float d = min(max(dot(dir_vec, v), 0.0f), length);
                result.position = pos+dir_vec*d;
                result.rotation = dir;
@@ -121,8 +117,6 @@ TrackPart *TrackPart::get_link(unsigned i) const
 
 void TrackPart::create_shape()
 {
-       Transform trans = Transform::translation(pos)*
-               Transform::rotation(dir, LinAl::Vector<float, 3>(0, 0, 1));
        if(radius)
        {
                Geometry::ExtrudedShape<float, 3> ring(
@@ -147,14 +141,14 @@ void TrackPart::create_shape()
                shapes.push_back(&bounds);
                shape = new Geometry::TransformedShape<float, 3>(
                        Geometry::Intersection<float, 3>::from_iterator_range(shapes.begin(), shapes.end()),
-                       trans*Transform::translation(Vector(0, radius, 0.005)));
+                       transform*Transform::translation(Vector(0, radius, 0.005)));
        }
        else
        {
                // TODO Get the track profile dimensions from somewhere
                shape = new Geometry::TransformedShape<float, 3>(
                        Geometry::Box<float>(length, 0.04, 0.01),
-                       trans*Transform::translation(Vector(length/2, 0, 0.005)));
+                       transform*Transform::translation(Vector(length/2, 0, 0.005)));
        }
 }
 
@@ -180,6 +174,9 @@ void TrackPart::Loader::finish()
                obj.length /= 1000;
 
        obj.pos /= 1000;
+
+       obj.transform = Transform::translation(obj.pos)*
+               Transform::rotation(obj.dir, Vector(0, 0, 1));
 }
 
 void TrackPart::Loader::start(float x, float y, float d)
index e0f3144e3d348f582137079b338390c44f7e09ef..97e76349d138e769c96f23d34cf0dd268121f8a2 100644 (file)
@@ -27,6 +27,7 @@ private:
        bool dead_end;
        TrackPart *links[2];
        Shape *shape;
+       Transform transform;
 
 public:
        TrackPart();