Code

Patch from Diedrik for 296778
[inkscape.git] / src / snapped-line.cpp
index 27e024821e418ddbdcaf9dfc6c66b4a656a722e7..3ebbeaf70d3962e5b38baa4bdbe06249707d1ba5 100644 (file)
@@ -9,7 +9,7 @@
  */
 
 #include "snapped-line.h"
-#include <2geom/geom.h>
+#include <2geom/line.h>
 
 Inkscape::SnappedLineSegment::SnappedLineSegment(Geom::Point const &snapped_point, Geom::Coord const &snapped_distance, SnapSourceType const &source, SnapTargetType const &target, Geom::Coord const &snapped_tolerance, bool const &always_snap, Geom::Point const &start_point_of_line, Geom::Point const &end_point_of_line)
     : _start_point_of_line(start_point_of_line), _end_point_of_line(end_point_of_line)
@@ -49,16 +49,22 @@ Inkscape::SnappedLineSegment::~SnappedLineSegment()
 
 Inkscape::SnappedPoint Inkscape::SnappedLineSegment::intersect(SnappedLineSegment const &line) const
 {
-    Geom::Point intersection_2geom(NR_HUGE, NR_HUGE);
-    Geom::IntersectorKind result = segment_intersect(_start_point_of_line, _end_point_of_line,
-                                                       line._start_point_of_line, line._end_point_of_line,
-                                                       intersection_2geom);
-      Geom::Point intersection(intersection_2geom);
-
-    if (result == Geom::intersects) {
-        /* If a snapper has been told to "always snap", then this one should be preferred
+    Geom::OptCrossing inters = Geom::OptCrossing(); // empty by default
+       try
+       {
+               inters = Geom::intersection(getLineSegment(), line.getLineSegment());
+       }
+       catch (Geom::InfiniteSolutions e)
+       {
+               // We're probably dealing with parallel lines, so they don't really cross
+               inters = Geom::OptCrossing();
+       }
+
+    if (inters) {
+        Geom::Point inters_pt = getLineSegment().pointAt((*inters).ta);
+       /* If a snapper has been told to "always snap", then this one should be preferred
          * over the other, if that other one has not been told so. (The preferred snapper
-         * will be labelled "primary" below)
+         * will be labeled "primary" below)
         */
         bool const c1 = this->getAlwaysSnap() && !line.getAlwaysSnap(); //do not use _tolerance directly!
         /* If neither or both have been told to "always snap", then cast a vote based on
@@ -70,14 +76,14 @@ Inkscape::SnappedPoint Inkscape::SnappedLineSegment::intersect(SnappedLineSegmen
         bool const use_this_as_primary = c1 || c2;
         Inkscape::SnappedLineSegment const *primarySLS = use_this_as_primary ? this : &line;
         Inkscape::SnappedLineSegment const *secondarySLS = use_this_as_primary ? &line : this;
-        Geom::Coord primaryDist = use_this_as_primary ? Geom::L2(intersection_2geom - this->getPoint()) : Geom::L2(intersection_2geom - line.getPoint());
-        Geom::Coord secondaryDist = use_this_as_primary ? Geom::L2(intersection_2geom - line.getPoint()) : Geom::L2(intersection_2geom - this->getPoint());
-        return SnappedPoint(intersection, SNAPSOURCE_UNDEFINED, SNAPTARGET_PATH_INTERSECTION, primaryDist, primarySLS->getTolerance(), primarySLS->getAlwaysSnap(), true, true,
+        Geom::Coord primaryDist = use_this_as_primary ? Geom::L2(inters_pt - this->getPoint()) : Geom::L2(inters_pt - line.getPoint());
+        Geom::Coord secondaryDist = use_this_as_primary ? Geom::L2(inters_pt - line.getPoint()) : Geom::L2(inters_pt - this->getPoint());
+        return SnappedPoint(inters_pt, SNAPSOURCE_UNDEFINED, SNAPTARGET_PATH_INTERSECTION, primaryDist, primarySLS->getTolerance(), primarySLS->getAlwaysSnap(), true, true,
                                           secondaryDist, secondarySLS->getTolerance(), secondarySLS->getAlwaysSnap());
     }
 
     // No intersection
-    return SnappedPoint(intersection, SNAPSOURCE_UNDEFINED, SNAPTARGET_UNDEFINED, NR_HUGE, 0, false, false, false, NR_HUGE, 0, false);
+    return SnappedPoint(Geom::Point(NR_HUGE, NR_HUGE), SNAPSOURCE_UNDEFINED, SNAPTARGET_UNDEFINED, NR_HUGE, 0, false, false, false, NR_HUGE, 0, false);
 };
 
 
@@ -123,13 +129,20 @@ Inkscape::SnappedPoint Inkscape::SnappedLine::intersect(SnappedLine const &line)
     // One could be a grid line, whereas the other could be a guide line
     // The point of intersection should be considered for snapping, but might be outside the snapping range
 
-    Geom::Point intersection_2geom(NR_HUGE, NR_HUGE);
-    Geom::IntersectorKind result = Geom::line_intersection(getNormal(), getConstTerm(),
-                                   line.getNormal(), line.getConstTerm(), intersection_2geom);
-    Geom::Point intersection(intersection_2geom);
-
-    if (result == Geom::intersects) {
-        /* If a snapper has been told to "always snap", then this one should be preferred
+    Geom::OptCrossing inters = Geom::OptCrossing(); // empty by default
+       try
+       {
+               inters = Geom::intersection(getLine(), line.getLine());
+       }
+       catch (Geom::InfiniteSolutions e)
+       {
+               // We're probably dealing with parallel lines, so they don't really cross
+               inters = Geom::OptCrossing();
+       }
+
+    if (inters) {
+       Geom::Point inters_pt = getLine().pointAt((*inters).ta);
+       /* If a snapper has been told to "always snap", then this one should be preferred
          * over the other, if that other one has not been told so. (The preferred snapper
          * will be labelled "primary" below)
         */
@@ -142,16 +155,16 @@ Inkscape::SnappedPoint Inkscape::SnappedLine::intersect(SnappedLine const &line)
         bool const use_this_as_primary = c1 || c2;
         Inkscape::SnappedLine const *primarySL = use_this_as_primary ? this : &line;
         Inkscape::SnappedLine const *secondarySL = use_this_as_primary ? &line : this;
-        Geom::Coord primaryDist = use_this_as_primary ? Geom::L2(intersection_2geom - this->getPoint()) : Geom::L2(intersection_2geom - line.getPoint());
-        Geom::Coord secondaryDist = use_this_as_primary ? Geom::L2(intersection_2geom - line.getPoint()) : Geom::L2(intersection_2geom - this->getPoint());
-        return SnappedPoint(intersection, Inkscape::SNAPSOURCE_UNDEFINED, Inkscape::SNAPTARGET_UNDEFINED, primaryDist, primarySL->getTolerance(), primarySL->getAlwaysSnap(), true, true,
+        Geom::Coord primaryDist = use_this_as_primary ? Geom::L2(inters_pt - this->getPoint()) : Geom::L2(inters_pt - line.getPoint());
+        Geom::Coord secondaryDist = use_this_as_primary ? Geom::L2(inters_pt - line.getPoint()) : Geom::L2(inters_pt - this->getPoint());
+        return SnappedPoint(inters_pt, Inkscape::SNAPSOURCE_UNDEFINED, Inkscape::SNAPTARGET_UNDEFINED, primaryDist, primarySL->getTolerance(), primarySL->getAlwaysSnap(), true, true,
                                           secondaryDist, secondarySL->getTolerance(), secondarySL->getAlwaysSnap());
         // The type of the snap target is yet undefined, as we cannot tell whether
         // we're snapping to grid or the guide lines; must be set by on a higher level
     }
 
     // No intersection
-    return SnappedPoint(intersection, SNAPSOURCE_UNDEFINED, SNAPTARGET_UNDEFINED, NR_HUGE, 0, false, false, false, NR_HUGE, 0, false);
+    return SnappedPoint(Geom::Point(NR_HUGE, NR_HUGE), SNAPSOURCE_UNDEFINED, SNAPTARGET_UNDEFINED, NR_HUGE, 0, false, false, false, NR_HUGE, 0, false);
 }
 
 // search for the closest snapped line segment