]> git.tdb.fi Git - r2c2.git/blobdiff - source/libr2c2/tracktype.cpp
Make use of the mspmath library
[r2c2.git] / source / libr2c2 / tracktype.cpp
index 29bf47be5409d42eb79fbd9644fbf1a62e08e1bc..fbed4c1d8d6020b9b19ed4c234b1c002d43f8b64 100644 (file)
@@ -7,7 +7,7 @@ using namespace Msp;
 namespace R2C2 {
 
 TrackType::TrackType(const ArticleNumber &an):
-       art_nr(an),
+       ObjectType(an),
        state_bits(0),
        autofit_preference(1)
 { }
@@ -69,16 +69,15 @@ TrackPoint TrackType::get_point(unsigned epi, unsigned path, float d) const
        unsigned part_ep = 0;
        for(vector<TrackPart>::const_iterator i=parts.begin(); i!=parts.end(); ++i)
        {
-               if((endpoints[epi].paths&(1<<path)) && i->get_path()!=path)
+               if(endpoints[epi].has_path(path) && i->get_path()!=path)
                        continue;
 
                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);
-                       float dx = p.pos.x-endpoints[epi].pos.x;
-                       float dy = p.pos.y-endpoints[epi].pos.y;
-                       if(dx*dx+dy*dy<1e-6)
+                       Vector span = p.pos-endpoints[epi].pos;
+                       if(dot(span, span)<1e-6)
                        {
                                part = &*i;
                                part_ep = j;
@@ -98,7 +97,7 @@ TrackPoint TrackType::get_point(unsigned epi, unsigned path, float d) const
                                d = plen-d;
                        TrackPoint p = part->get_point(d);
                        if(part_ep==1)
-                               p.dir += M_PI;
+                               p.dir += Angle::half_turn();
                        return p;
                }
                else
@@ -113,6 +112,25 @@ TrackPoint TrackType::get_point(unsigned epi, unsigned path, float d) const
        }
 }
 
+TrackPoint TrackType::get_nearest_point(const Vector &p) const
+{
+       TrackPoint 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);
+               if(d<dist || dist<0)
+               {
+                       result = n;
+                       dist = d;
+               }
+       }
+
+       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)
@@ -137,21 +155,16 @@ void TrackType::collect_endpoints()
                        {
                                TrackPoint p = i->get_point(j ? i->get_length() : 0);
                                if(j==0)
-                                       p.dir += M_PI;
+                                       p.dir += Angle::half_turn();
 
                                bool found = false;
                                for(vector<Endpoint>::iterator k=endpoints.begin(); k!=endpoints.end(); ++k)
                                {
-                                       float dx = k->pos.x-p.pos.x;
-                                       float dy = k->pos.y-p.pos.y;
+                                       Vector d = k->pos-p.pos;
 
-                                       float da = k->dir-p.dir;
-                                       while(da>M_PI)
-                                               da -= M_PI*2;
-                                       while(da<-M_PI)
-                                               da += M_PI*2;
+                                       Angle da = wrap_balanced(k->dir-p.dir);
 
-                                       if(dx*dx+dy*dy<1e-6 && da>-0.01 && da<0.01)
+                                       if(dot(d, d)<1e-6 && abs(da).radians()<0.01)
                                        {
                                                k->paths |= 1<<i->get_path();
                                                found = true;
@@ -165,19 +178,18 @@ void TrackType::collect_endpoints()
        }
 }
 
-TrackType::Endpoint::Endpoint(float x, float y, float d, unsigned p):
-       pos(x, y),
+TrackType::Endpoint::Endpoint(float x, float y, const Angle &d, unsigned p):
+       pos(x, y, 0),
        dir(d),
        paths(p)
 { }
 
 
 TrackType::Loader::Loader(TrackType &t):
-       Msp::DataFile::ObjectLoader<TrackType>(t),
+       DataFile::DerivedObjectLoader<TrackType, ObjectType::Loader>(t),
        state_bits_set(false)
 {
        add("autofit_preference", &TrackType::autofit_preference);
-       add("description", &TrackType::description);
        add("object",      &TrackType::object);
        add("state_bits",  &Loader::state_bits);
        add("part",        &Loader::part);