Code

1) Improving snapping logic 2) When skewing, don't snap to selection itself
authordvlierop2 <dvlierop2@users.sourceforge.net>
Sun, 18 Nov 2007 19:14:14 +0000 (19:14 +0000)
committerdvlierop2 <dvlierop2@users.sourceforge.net>
Sun, 18 Nov 2007 19:14:14 +0000 (19:14 +0000)
src/line-snapper.cpp
src/seltrans.cpp
src/snap.cpp
src/snapped-line.cpp
src/snapped-point.cpp
src/snapped-point.h

index 3b5bd047c6ec346682c780f25946d5800261d11f..be35ec2db930b1faaf717ecad13bb2f7d379853e 100644 (file)
@@ -56,7 +56,7 @@ void Inkscape::LineSnapper::_doConstrainedSnap(SnappedConstraints &sc,
             const NR::Coord dist = L2(t - p);
             //Store any line that's within snapping range
             if (dist < getDistance()) {
-                _addSnappedLine(sc, t, dist, c.getDirection(), t);
+                               _addSnappedLine(sc, t, dist, c.getDirection(), t);
                 //SnappedLine dummy = SnappedLine(t, dist, c.getDirection(), t);
                 //sc.infinite_lines.push_back(dummy);
             }
index 3650b18b66c46d011f7a00acdc9342e3deca4b3e..512d2c0ca30e3d6c04fcf1aba7e950d4a7004d46 100644 (file)
@@ -1151,18 +1151,26 @@ gboolean Inkscape::SelTrans::skewRequest(SPSelTransHandle const &handle, NR::Poi
         }
         skew[dim_a] = tan(radians) * s[dim_a];
     } else {
+        /* Get a STL list of the selected items.
+           ** FIXME: this should probably be done by Inkscape::Selection.
+           */
+           std::list<SPItem const*> it;
+           for (GSList const *i = _selection->itemList(); i != NULL; i = i->next) {
+               it.push_back(reinterpret_cast<SPItem*>(i->data));
+           }
+        
         SnapManager const &m = _desktop->namedview->snap_manager;
 
         std::pair<NR::Coord, bool> bb = m.freeSnapSkew(Inkscape::Snapper::SNAPPOINT_BBOX,
                                                        _bbox_points,
-                                                       std::list<SPItem const *>(),
+                                                       it,
                                                        skew[dim_a],
                                                        _origin_for_bboxpoints,
                                                        dim_b);
 
         std::pair<NR::Coord, bool> sn = m.freeSnapSkew(Inkscape::Snapper::SNAPPOINT_NODE,
                                                        _snap_points,
-                                                       std::list<SPItem const *>(),
+                                                       it,
                                                        skew[dim_a],
                                                        _origin_for_specpoints,
                                                        dim_b);
index 28b0664c74930b3491212ebede09616a7f17066a..1ff5d3d059b5d5f69aa693955148a958c778ccfb 100644 (file)
@@ -258,7 +258,7 @@ Inkscape::SnappedPoint SnapManager::constrainedSnap(Inkscape::Snapper::PointType
 Inkscape::SnappedPoint SnapManager::constrainedSnap(Inkscape::Snapper::PointType t,
                                                     NR::Point const &p,
                                                     bool const &first_point,
-                                                     std::vector<NR::Point> &points_to_snap,
+                                                    std::vector<NR::Point> &points_to_snap,
                                                     Inkscape::Snapper::ConstraintLine const &c,
                                                     std::list<SPItem const *> const &it) const
 {
@@ -390,8 +390,8 @@ std::pair<NR::Point, bool> SnapManager::_snapTransformed(
             constrainedSnap(type, *j, i == points.begin(), transformed_points, constraint, ignore) : freeSnap(type, *j, i == points.begin(), transformed_points, ignore);
 
         NR::Point result;
-        NR::Coord metric;
-        NR::Coord second_metric;
+        NR::Coord metric = NR_HUGE;
+        NR::Coord second_metric = NR_HUGE;
         
         if (snapped.getDistance() < NR_HUGE) {
             /* We snapped.  Find the transformation that describes where the snapped point has
@@ -410,7 +410,7 @@ std::pair<NR::Point, bool> SnapManager::_snapTransformed(
                      * and not to the intersection itself! 
                      */
                     metric = snapped.getDistance(); //used to be: metric = NR::L2(result);
-                    second_metric = NR::L2(*j - snapped.getPoint());
+                    second_metric = snapped.getSecondDistance();
                     break;
                 case SCALE:
                 {
@@ -439,8 +439,6 @@ std::pair<NR::Point, bool> SnapManager::_snapTransformed(
                 default:
                     g_assert_not_reached();
             }
-
-            
             
             /* Note it if it's the best so far */
             bool const c1 = metric < best_metric;
@@ -450,9 +448,6 @@ std::pair<NR::Point, bool> SnapManager::_snapTransformed(
             
             if (c1 || c2 || c3a && c3b) {
                 best_transformation = result;
-                //if (c1) {std::cout << "c1 ";}
-                //if (c2) {std::cout << "c2 ";}
-                //if (c3a && c3b) {std::cout << "c3 ";}
                 best_metric = metric;
                 best_second_metric = second_metric;
                 best_at_intersection = snapped.getAtIntersection(); 
@@ -464,7 +459,7 @@ std::pair<NR::Point, bool> SnapManager::_snapTransformed(
         j++;
     }
     
-    // Using " < 1e6" instead of " < NR::HUGE" for catching some rounding errors
+    // Using " < 1e6" instead of " < NR_HUGE" for catching some rounding errors
     // These rounding errors might be caused by NRRects, see bug #1584301
     return std::make_pair(best_transformation, best_metric < 1e6);
 }
@@ -699,21 +694,23 @@ Inkscape::SnappedPoint SnapManager::findBestSnap(NR::Point const &p, SnappedCons
     // now let's see which snapped point gets a thumbs up
     Inkscape::SnappedPoint bestPoint(p, NR_HUGE);
     for (std::list<std::pair<Inkscape::SnappedPoint, NR::Coord> >::const_iterator i = sp_list.begin(); i != sp_list.end(); i++) {
-         // first find out if this snapped point is within snapping range
-         if ((*i).first.getDistance() <= (*i).second) {
-             // if it's the first point
-             bool c1 = (i == sp_list.begin());  
-             // or, if it's closer
-             bool c2 = (*i).first.getDistance() < bestPoint.getDistance(); 
-             // or, if it's just as close but at an intersection
-             bool c3 = ((*i).first.getDistance() == bestPoint.getDistance()) && (*i).first.getAtIntersection(); 
-             // then prefer this point over the previous one
-             if (c1 || c2 || c3) {
-                 bestPoint = (*i).first;
-             }
-         }
-     }
-     return bestPoint;         
+               // first find out if this snapped point is within snapping range
+           if ((*i).first.getDistance() <= (*i).second) {
+               // if it's the first point
+               bool c1 = (i == sp_list.begin());  
+               // or, if it's closer
+               bool c2 = (*i).first.getDistance() < bestPoint.getDistance(); 
+               // or, if it's just as close then consider the second distance
+               // (which is only relevant for points at an intersection)
+               bool c3a = ((*i).first.getDistance() == bestPoint.getDistance()); 
+               bool c3b = (*i).first.getSecondDistance() < bestPoint.getSecondDistance();
+               // then prefer this point over the previous one
+               if (c1 || c2 || c3a && c3b) {
+                bestPoint = (*i).first;
+            }
+        }
+    }
+    return bestPoint;         
 }
 
 /*
index 7ae94e4f4b79181834cc5d0a36e8f4b5aa6af8e8..67fec627a85670a71a093d3e70d43880c76785f7 100644 (file)
@@ -18,6 +18,7 @@ Inkscape::SnappedLineSegment::SnappedLineSegment(NR::Point snapped_point, NR::Co
        _distance = snapped_distance;
        _point = snapped_point;
        _at_intersection = false;
+       _second_distance = NR_HUGE;
 }
 
 Inkscape::SnappedLineSegment::SnappedLineSegment() 
@@ -27,6 +28,7 @@ Inkscape::SnappedLineSegment::SnappedLineSegment()
        _distance = NR_HUGE;
        _point = NR::Point(0,0);
        _at_intersection = false;
+       _second_distance = NR_HUGE;
 }
 
 
@@ -38,6 +40,7 @@ Inkscape::SnappedPoint Inkscape::SnappedLineSegment::intersect(SnappedLineSegmen
 {
        Geom::Point intersection_2geom(NR_HUGE, NR_HUGE);
        NR::Coord distance = NR_HUGE;
+       NR::Coord second_distance = NR_HUGE;
         
        Geom::IntersectorKind result = segment_intersect(_start_point_of_line.to_2geom(), _end_point_of_line.to_2geom(),
                                                                         line._start_point_of_line.to_2geom(), line._end_point_of_line.to_2geom(),
@@ -49,8 +52,9 @@ Inkscape::SnappedPoint Inkscape::SnappedLineSegment::intersect(SnappedLineSegmen
                distance to the intersection. See the comment in Inkscape::SnappedLine::intersect
                */
                distance = std::min(_distance, line.getDistance());
+               second_distance = std::max(_distance, line.getDistance());
        }
-       return SnappedPoint(intersection, distance, result == Geom::intersects);
+       return SnappedPoint(intersection, distance, result == Geom::intersects, second_distance);
 };
 
 
@@ -59,6 +63,7 @@ Inkscape::SnappedLine::SnappedLine(NR::Point snapped_point, NR::Coord snapped_di
     : _normal_to_line(normal_to_line), _point_on_line(point_on_line)
 {
        _distance = snapped_distance;
+       _second_distance = NR_HUGE;
        _point = snapped_point;
        _at_intersection = false;
 }
@@ -68,6 +73,7 @@ Inkscape::SnappedLine::SnappedLine()
        _normal_to_line = NR::Point(0,0);
        _point_on_line = NR::Point(0,0);
        _distance = NR_HUGE;
+       _second_distance = NR_HUGE;
        _point = NR::Point(0,0);
        _at_intersection = false;
 }
@@ -83,6 +89,7 @@ Inkscape::SnappedPoint Inkscape::SnappedLine::intersect(SnappedLine const &line)
        
        Geom::Point intersection_2geom(NR_HUGE, NR_HUGE);
        NR::Coord distance = NR_HUGE;
+       NR::Coord second_distance = NR_HUGE;
        
     Geom::IntersectorKind result = Geom::line_intersection(getNormal().to_2geom(), getConstTerm(), 
                                    line.getNormal().to_2geom(), line.getConstTerm(), intersection_2geom);
@@ -98,9 +105,10 @@ Inkscape::SnappedPoint Inkscape::SnappedLine::intersect(SnappedLine const &line)
                than it, as that would rule the intersection out
                */
                distance = std::min(_distance, line.getDistance());
+               second_distance = std::max(_distance, line.getDistance());
        }
 
-    return SnappedPoint(intersection, distance, result == Geom::intersects);
+    return SnappedPoint(intersection, distance, result == Geom::intersects, second_distance);
 }
 
 // search for the closest snapped line segment
@@ -129,8 +137,15 @@ bool getClosestIntersectionSLS(std::list<Inkscape::SnappedLineSegment> &list, In
                for (; j != list.end(); j++) {
                        Inkscape::SnappedPoint sp = (*i).intersect(*j);
                        if (sp.getAtIntersection()) {
-                               if (!success || sp.getDistance() < result.getDistance()) {  
-                                       // !success because the first intersection cannot be compared to a previous one
+                               // if it's the first point
+               bool const c1 = !success;
+                               // or, if it's closer             
+                               bool const c2 = sp.getDistance() < result.getDistance();
+                               // or, if it's just then look at the other distance 
+                               // (only relevant for snapped points which are at an intersection
+                               bool const c3 = (sp.getDistance() == result.getDistance()) && (sp.getSecondDistance() < result.getSecondDistance()); 
+                               // then prefer this point over the previous one
+                               if (c1 || c2 || c3) {  
                                        result = sp;
                                        success = true;
                                }
@@ -167,8 +182,15 @@ bool getClosestIntersectionSL(std::list<Inkscape::SnappedLine> &list, Inkscape::
                for (; j != list.end(); j++) {
                        Inkscape::SnappedPoint sp = (*i).intersect(*j);
                        if (sp.getAtIntersection()) {
-                               if (!success || sp.getDistance() < result.getDistance()) {  
-                                       // !success because the first intersection cannot be compared to a previous one
+                               // if it's the first point
+               bool const c1 = !success;
+                               // or, if it's closer             
+                               bool const c2 = sp.getDistance() < result.getDistance();
+                               // or, if it's just then look at the other distance 
+                               // (only relevant for snapped points which are at an intersection
+                               bool const c3 = (sp.getDistance() == result.getDistance()) && (sp.getSecondDistance() < result.getSecondDistance()); 
+                               // then prefer this point over the previous one
+                               if (c1 || c2 || c3) {  
                                        result = sp;
                                        success = true;
                                }
@@ -188,8 +210,15 @@ bool getClosestIntersectionSL(std::list<Inkscape::SnappedLine> &list1, std::list
                for (std::list<Inkscape::SnappedLine>::const_iterator j = list2.begin(); j != list2.end(); j++) {
                        Inkscape::SnappedPoint sp = (*i).intersect(*j);
                        if (sp.getAtIntersection()) {
-                               if (!success || sp.getDistance() < result.getDistance()) {
-                                       // !success because the first intersection cannot be compared to a previous one
+                               // if it's the first point
+               bool const c1 = !success;
+                               // or, if it's closer             
+                               bool const c2 = sp.getDistance() < result.getDistance();
+                               // or, if it's just then look at the other distance 
+                               // (only relevant for snapped points which are at an intersection
+                               bool const c3 = (sp.getDistance() == result.getDistance()) && (sp.getSecondDistance() < result.getSecondDistance()); 
+                               // then prefer this point over the previous one
+                               if (c1 || c2 || c3) {  
                                        result = sp;
                                        success = true;
                                }
index ed82c47c0e0570cc9f01d3038ac4efda621be108..d97abda1d0a7567b952a2e49c9aa741a2ac44a69 100644 (file)
@@ -4,15 +4,15 @@
  *
  *  Authors:
  *    Mathieu Dimanche <mdimanche@free.fr>
+ *    Diederik van Lierop <mail@diedenrezi.nl>
  *
  *  Released under GNU GPL, read the file 'COPYING' for more information.
  */
 
 #include "snapped-point.h"
-#include <libnr/nr-values.h>
 
-Inkscape::SnappedPoint::SnappedPoint(NR::Point p, NR::Coord d, bool at_intersection)
-    : _distance(d), _point(p), _at_intersection(at_intersection)
+Inkscape::SnappedPoint::SnappedPoint(NR::Point p, NR::Coord d, bool at_intersection, NR::Coord d2)
+    : _distance(d), _point(p), _at_intersection(at_intersection), _second_distance(d2)
 {
 }
 
@@ -21,6 +21,7 @@ Inkscape::SnappedPoint::SnappedPoint()
     _distance = NR_HUGE;
     _point = NR::Point(0,0);
     _at_intersection = false;
+    _second_distance = NR_HUGE;
 }
 
 
@@ -34,6 +35,12 @@ NR::Coord Inkscape::SnappedPoint::getDistance() const
     return _distance;
 }
 
+NR::Coord Inkscape::SnappedPoint::getSecondDistance() const
+{
+    return _second_distance;
+}
+
+
 NR::Point Inkscape::SnappedPoint::getPoint() const
 {
     return _point;
index 4d8751f5f18b7e13a8bc594945bf8a7dfba7e212..e4b08a302624d2122bfc38bc854b41323d44446e 100644 (file)
@@ -7,6 +7,7 @@
  *
  *    Authors:
  *      Mathieu Dimanche <mdimanche@free.fr>
+ *      Diederik van Lierop <mail@diedenrezi.nl>
  *
  *    Released under GNU GPL, read the file 'COPYING' for more information.
  */
@@ -15,6 +16,7 @@
 #include <list>
 #include "libnr/nr-coord.h"
 #include "libnr/nr-point.h"
+#include <libnr/nr-values.h>
 
 namespace Inkscape
 {
@@ -24,17 +26,28 @@ class SnappedPoint
 {
 public:
     SnappedPoint();
-    SnappedPoint(::NR::Point p, ::NR::Coord d, bool at_intersection = false);
+    SnappedPoint(::NR::Point p, ::NR::Coord d, bool at_intersection = false, NR::Coord d2 = NR_HUGE);
     ~SnappedPoint();
 
     NR::Coord getDistance() const;
+    NR::Coord getSecondDistance() const;
     NR::Point getPoint() const;
     bool getAtIntersection() const {return _at_intersection;}
     
 protected:
-    NR::Coord _distance;
-    NR::Point _point;
-    bool _at_intersection;
+    NR::Point _point; // Location of the snapped point
+    bool _at_intersection; // If true, the snapped point is at an intersection 
+    
+    /* Distance from original point to snapped point. If the snapped point is at
+       an intersection of e.g. two lines, then this is the distance to the closest
+       line */    
+    NR::Coord _distance; 
+    
+    /* If the snapped point is at an intersection of e.g. two lines, then this is
+       the distance to the fartest line */    
+    NR::Coord _second_distance; 
+    
+    
 };    
 
 }