]> git.tdb.fi Git - r2c2.git/blobdiff - source/libr2c2/signal.cpp
Add a generic snapping interface in Object
[r2c2.git] / source / libr2c2 / signal.cpp
index 72cf235193e6a9c537e67a003ede7a221b8c7673..650c36232e87d82559fc36cfbb6aba2bc160ab55 100644 (file)
@@ -56,12 +56,15 @@ void Signal::set_position(const Vector &p)
        for(set<Track *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
                if(!(*i)->get_type().is_turnout())
                {
-                       TrackPoint n = (*i)->get_nearest_point(p);
-                       float d = distance(p, n.pos);
+                       Snap sn;
+                       sn.position = p;
+                       sn.rotation = rotation;
+                       (*i)->snap(sn, 1000, SNAP_SEGMENT);
+                       float d = distance(p, sn.position);
                        if(d<dist || dist<0)
                        {
-                               position = n.pos;
-                               rotation = n.dir;
+                               position = sn.position;
+                               rotation = sn.rotation;
                                track = *i;
                                dist = d;
                        }
@@ -77,7 +80,7 @@ void Signal::normalize_location()
        unsigned n_endpoints = track->get_type().get_endpoints().size();
        for(unsigned j=0; j<n_endpoints; ++j)
        {
-               float a = track->get_endpoint_direction(j)-rotation;
+               float a = track->get_snap_node(j).rotation-rotation;
                while(a<-M_PI/2)
                        a += M_PI*2;
                while(a>M_PI*3/2)
@@ -107,6 +110,30 @@ void Signal::set_rotation(float r)
        normalize_location();
 }
 
+unsigned Signal::get_n_snap_nodes() const
+{
+       return 1;
+}
+
+Snap Signal::get_snap_node(unsigned i) const
+{
+       if(i>=1)
+               throw out_of_range("Signal::get_snap_node");
+
+       Snap sn;
+       sn.position = position;
+       sn.rotation = rotation;
+       return sn;
+}
+
+SnapType Signal::get_default_snap_type_to(const Object &other) const
+{
+       if(dynamic_cast<const Track *>(&other))
+               return SNAP_SEGMENT;
+
+       return NO_SNAP;
+}
+
 bool Signal::collide_ray(const Vector &start, const Vector &ray) const
 {
        // XXX Totally hardcoded stuff, should be replaced with a geometry system