From: Mikko Rasa Date: Wed, 2 Apr 2014 20:41:06 +0000 (+0300) Subject: Store the Transform in TrackPart instead of creating it on the fly X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=3ea934a7c6be204f82e5d41c3be8d5256efdac64;p=r2c2.git Store the Transform in TrackPart instead of creating it on the fly --- diff --git a/source/libr2c2/trackpart.cpp b/source/libr2c2/trackpart.cpp index 8aab4f2..9622b28 100644 --- a/source/libr2c2/trackpart.cpp +++ b/source/libr2c2/trackpart.cpp @@ -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(0, 0, 1)); if(radius) { Geometry::ExtrudedShape ring( @@ -147,14 +141,14 @@ void TrackPart::create_shape() shapes.push_back(&bounds); shape = new Geometry::TransformedShape( Geometry::Intersection::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( Geometry::Box(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) diff --git a/source/libr2c2/trackpart.h b/source/libr2c2/trackpart.h index e0f3144..97e7634 100644 --- a/source/libr2c2/trackpart.h +++ b/source/libr2c2/trackpart.h @@ -27,6 +27,7 @@ private: bool dead_end; TrackPart *links[2]; Shape *shape; + Transform transform; public: TrackPart();