]> git.tdb.fi Git - r2c2.git/blobdiff - source/libr2c2/track.cpp
Add a generic snapping interface in Object
[r2c2.git] / source / libr2c2 / track.cpp
index b679af783867f72746793f205179f004a9e02baa..e4d7badfb9f9e053d2dc7fa7650b039bf890cf90 100644 (file)
@@ -100,8 +100,8 @@ void Track::check_slope()
 
        if(links[0] && links[1])
        {
-               Vector epp0 = links[0]->get_endpoint_position(links[0]->get_endpoint_by_link(*this));
-               Vector epp1 = links[1]->get_endpoint_position(links[1]->get_endpoint_by_link(*this));
+               Vector epp0 = links[0]->get_snap_node(links[0]->get_endpoint_by_link(*this)).position;
+               Vector epp1 = links[1]->get_snap_node(links[1]->get_endpoint_by_link(*this)).position;
                position.z = epp0.z;
                slope = epp1.z-position.z;
        }
@@ -110,12 +110,12 @@ void Track::check_slope()
                slope = 0;
                if(links[0])
                {
-                       Vector epp = links[0]->get_endpoint_position(links[0]->get_endpoint_by_link(*this));
+                       Vector epp = links[0]->get_snap_node(links[0]->get_endpoint_by_link(*this)).position;
                        position.z = epp.z;
                }
                else if(links[1])
                {
-                       Vector epp = links[1]->get_endpoint_position(links[1]->get_endpoint_by_link(*this));
+                       Vector epp = links[1]->get_snap_node(links[1]->get_endpoint_by_link(*this)).position;
                        position.z = epp.z;
                }
        }
@@ -164,34 +164,6 @@ int Track::get_endpoint_by_link(Track &other) const
        return -1;
 }
 
-Vector Track::get_endpoint_position(unsigned epi) const
-{
-       const vector<TrackType::Endpoint> &eps = type.get_endpoints();
-       if(epi>=eps.size())
-               throw out_of_range("Track::get_endpoint_position");
-
-       const TrackType::Endpoint &ep = eps[epi];
-
-       float c = cos(rotation);
-       float s = sin(rotation);
-
-       Vector p(position.x+c*ep.pos.x-s*ep.pos.y, position.y+s*ep.pos.x+c*ep.pos.y, position.z);
-       if(eps.size()==2 && epi==1)
-               p.z += slope;
-       return p;
-}
-
-float Track::get_endpoint_direction(unsigned epi) const
-{
-       const vector<TrackType::Endpoint> &eps = type.get_endpoints();
-       if(epi>=eps.size())
-               throw out_of_range("Track::get_endpoint_direction");
-
-       const TrackType::Endpoint &ep = eps[epi];
-
-       return rotation+ep.dir;
-}
-
 bool Track::snap_to(Track &other, bool link, float limit)
 {
        if(!limit || link)
@@ -207,14 +179,14 @@ bool Track::snap_to(Track &other, bool link, float limit)
 
        for(unsigned i=0; i<eps.size(); ++i)
        {
-               Vector epp = get_endpoint_position(i);
+               Vector epp = get_snap_node(i).position;
 
                for(unsigned j=0; j<other_eps.size(); ++j)
                {
                        if(other.get_link(j))
                                continue;
 
-                       Vector epp2 = other.get_endpoint_position(j);
+                       Vector epp2 = other.get_snap_node(j).position;
                        float dx = epp2.x-epp.x;
                        float dy = epp2.y-epp.y;
                        float dz = epp2.z-epp.z;
@@ -251,26 +223,6 @@ bool Track::snap_to(Track &other, bool link, float limit)
        return false;
 }
 
-bool Track::snap(Vector &pt, float &d) const
-{
-       const vector<TrackType::Endpoint> &eps = type.get_endpoints();
-
-       for(unsigned i=0; i<eps.size(); ++i)
-       {
-               Vector epp = get_endpoint_position(i);
-               float dx = pt.x-epp.x;
-               float dy = pt.y-epp.y;
-               if(dx*dx+dy*dy<1e-4)
-               {
-                       pt = epp;
-                       d = rotation+eps[i].dir;
-                       return true;
-               }
-       }
-
-       return false;
-}
-
 void Track::break_link(Track &trk)
 {
        for(vector<Track *>::iterator i=links.begin(); i!=links.end(); ++i)
@@ -335,17 +287,63 @@ TrackPoint Track::get_point(unsigned epi, float d) const
        return get_point(epi, active_path, d);
 }
 
-TrackPoint Track::get_nearest_point(const Vector &p) const
+unsigned Track::get_n_snap_nodes() const
 {
-       Vector local(p.x-position.x, p.y-position.y, p.z-position.z);
+       return type.get_endpoints().size();
+}
+
+Snap Track::get_snap_node(unsigned i) const
+{
+       const vector<TrackType::Endpoint> &eps = type.get_endpoints();
+       if(i>=eps.size())
+               throw out_of_range("Track::get_snap_node");
+
+       Snap result;
+       const TrackType::Endpoint &ep = eps[i];
+
        float c = cos(rotation);
        float s = sin(rotation);
-       local = Vector(c*local.x+s*local.y, c*local.y-s*local.x, local.z);
 
-       TrackPoint tp = type.get_nearest_point(local);
-       tp.pos = 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);
-       tp.dir += rotation;
-       return tp;
+       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);
+       if(eps.size()==2 && i==1)
+               result.position.z += slope;
+
+       result.rotation = rotation+ep.dir;
+
+       return result;
+}
+
+bool Track::snap(Snap &sn, float limit, SnapType what) const
+{
+       if(Object::snap(sn, limit, what))
+               return true;
+
+       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);
+
+               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)
+               {
+                       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.rotation = tp.dir+rotation;
+                       return true;
+               }
+       }
+
+       return false;
+}
+
+SnapType Track::get_default_snap_type_to(const Object &other) const
+{
+       if(dynamic_cast<const Track *>(&other))
+               return SNAP_NODE;
+
+       return NO_SNAP;
 }
 
 bool Track::collide_ray(const Vector &start, const Vector &ray) const