]> git.tdb.fi Git - r2c2.git/blobdiff - source/libr2c2/tracktype.cpp
Move track appearance properties into a separate class
[r2c2.git] / source / libr2c2 / tracktype.cpp
index fbed4c1d8d6020b9b19ed4c234b1c002d43f8b64..7a1f27ad5adcc69ee282a028d10755dabf68d37a 100644 (file)
@@ -1,4 +1,5 @@
 #include <cmath>
+#include <msp/geometry/union.h>
 #include "tracktype.h"
 
 using namespace std;
@@ -6,8 +7,9 @@ using namespace Msp;
 
 namespace R2C2 {
 
-TrackType::TrackType(const ArticleNumber &an):
+TrackType::TrackType(const ArticleNumber &an, const TrackAppearance &ta):
        ObjectType(an),
+       appearance(ta),
        state_bits(0),
        autofit_preference(1)
 { }
@@ -60,7 +62,7 @@ const TrackType::Endpoint &TrackType::get_endpoint(unsigned i) const
        return endpoints[i];
 }
 
-TrackPoint TrackType::get_point(unsigned epi, unsigned path, float d) const
+OrientedPoint TrackType::get_point(unsigned epi, unsigned path, float d) const
 {
        if(epi>=endpoints.size())
                throw out_of_range("TrackType::get_point");
@@ -75,8 +77,8 @@ TrackPoint TrackType::get_point(unsigned epi, unsigned path, float d) const
                unsigned n_part_eps = (i->is_dead_end() ? 1 : 2);
                for(unsigned j=0; j<n_part_eps; ++j)
                {
-                       TrackPoint p = i->get_point(j ? i->get_length() : 0);
-                       Vector span = p.pos-endpoints[epi].pos;
+                       OrientedPoint p = i->get_point(j ? i->get_length() : 0);
+                       Vector span = p.position-endpoints[epi].pos;
                        if(dot(span, span)<1e-6)
                        {
                                part = &*i;
@@ -95,9 +97,9 @@ TrackPoint TrackType::get_point(unsigned epi, unsigned path, float d) const
                {
                        if(part_ep==1)
                                d = plen-d;
-                       TrackPoint p = part->get_point(d);
+                       OrientedPoint p = part->get_point(d);
                        if(part_ep==1)
-                               p.dir += Angle::half_turn();
+                               p.rotation += Angle::half_turn();
                        return p;
                }
                else
@@ -112,15 +114,15 @@ TrackPoint TrackType::get_point(unsigned epi, unsigned path, float d) const
        }
 }
 
-TrackPoint TrackType::get_nearest_point(const Vector &p) const
+OrientedPoint TrackType::get_nearest_point(const Vector &p) const
 {
-       TrackPoint result;
+       OrientedPoint result;
        float dist = -1;
 
        for(vector<TrackPart>::const_iterator i=parts.begin(); i!=parts.end(); ++i)
        {
-               TrackPoint n = i->get_nearest_point(p);
-               float d = distance(n.pos, p);
+               OrientedPoint n = i->get_nearest_point(p);
+               float d = distance(n.position, p);
                if(d<dist || dist<0)
                {
                        result = n;
@@ -131,15 +133,6 @@ TrackPoint TrackType::get_nearest_point(const Vector &p) const
        return result;
 }
 
-bool TrackType::collide_ray(const Vector &start, const Vector &dir, float width) const
-{
-       for(vector<TrackPart>::const_iterator i=parts.begin(); i!=parts.end(); ++i)
-               if(i->collide_ray(start, dir, width))
-                       return true;
-
-       return false;
-}
-
 void TrackType::collect_endpoints()
 {
        endpoints.clear();
@@ -153,16 +146,16 @@ void TrackType::collect_endpoints()
                for(unsigned j=0; j<n_part_eps; ++j)
                        if(!i->get_link(j))
                        {
-                               TrackPoint p = i->get_point(j ? i->get_length() : 0);
+                               OrientedPoint p = i->get_point(j ? i->get_length() : 0);
                                if(j==0)
-                                       p.dir += Angle::half_turn();
+                                       p.rotation += Angle::half_turn();
 
                                bool found = false;
                                for(vector<Endpoint>::iterator k=endpoints.begin(); k!=endpoints.end(); ++k)
                                {
-                                       Vector d = k->pos-p.pos;
+                                       Vector d = k->pos-p.position;
 
-                                       Angle da = wrap_balanced(k->dir-p.dir);
+                                       Angle da = wrap_balanced(k->dir-p.rotation);
 
                                        if(dot(d, d)<1e-6 && abs(da).radians()<0.01)
                                        {
@@ -173,7 +166,7 @@ void TrackType::collect_endpoints()
                                }
 
                                if(!found)
-                                       endpoints.push_back(Endpoint(p.pos.x, p.pos.y, p.dir, 1<<i->get_path()));
+                                       endpoints.push_back(Endpoint(p.position.x, p.position.y, p.rotation, 1<<i->get_path()));
                        }
        }
 }
@@ -198,6 +191,13 @@ TrackType::Loader::Loader(TrackType &t):
 void TrackType::Loader::finish()
 {
        obj.collect_endpoints();
+       vector<const Shape *> shapes;
+       for(vector<TrackPart>::iterator i=obj.parts.begin(); i!=obj.parts.end(); ++i)
+       {
+               i->create_shape();
+               shapes.push_back(&i->get_shape());
+       }
+       obj.shape = Geometry::Union<float, 3>::from_iterator_range(shapes.begin(), shapes.end()).clone();
 }
 
 void TrackType::Loader::part()