Code

Node tool: special case node duplication for endnodes - select new endnode
[inkscape.git] / src / snapped-point.cpp
index 089aa4323e39b66fd6c91853bebeddcd00e43bd8..52f0a7839b0468c10035ed913711822124733bdd 100644 (file)
 #include "preferences.h"
 
 // overloaded constructor
-Inkscape::SnappedPoint::SnappedPoint(Geom::Point const &p, SnapSourceType const &source, long source_num, SnapTargetType const &target, Geom::Coord const &d, Geom::Coord const &t, bool const &a, bool const &fully_constrained, Geom::OptRect target_bbox)
+Inkscape::SnappedPoint::SnappedPoint(Geom::Point const &p, SnapSourceType const &source, long source_num, SnapTargetType const &target, Geom::Coord const &d, Geom::Coord const &t, bool const &a, bool const &constrained_snap, bool const &fully_constrained, Geom::OptRect target_bbox)
     : _point(p), _source(source), _source_num(source_num), _target(target), _distance(d), _tolerance(std::max(t,1.0)), _always_snap(a), _target_bbox(target_bbox)
 {
     // tolerance should never be smaller than 1 px, as it is used for normalization in isOtherSnapBetter. We don't want a division by zero.
     _at_intersection = false;
+    _constrained_snap = constrained_snap;
     _fully_constrained = fully_constrained;
     _second_distance = NR_HUGE;
     _second_tolerance = 1;
@@ -27,13 +28,14 @@ Inkscape::SnappedPoint::SnappedPoint(Geom::Point const &p, SnapSourceType const
     _pointer_distance = NR_HUGE;
 }
 
-Inkscape::SnappedPoint::SnappedPoint(Inkscape::SnapCandidatePoint const &p, SnapTargetType const &target, Geom::Coord const &d, Geom::Coord const &t, bool const &a, bool const &fully_constrained)
+Inkscape::SnappedPoint::SnappedPoint(Inkscape::SnapCandidatePoint const &p, SnapTargetType const &target, Geom::Coord const &d, Geom::Coord const &t, bool const &a, bool const &constrained_snap, bool const &fully_constrained)
     : _target(target), _distance(d), _tolerance(std::max(t,1.0)), _always_snap(a)
 {
     _point = p.getPoint();
     _source = p.getSourceType();
     _source_num = p.getSourceNum();
     _at_intersection = false;
+    _constrained_snap = constrained_snap;
     _fully_constrained = fully_constrained;
     _second_distance = NR_HUGE;
     _second_tolerance = 1;
@@ -44,8 +46,8 @@ Inkscape::SnappedPoint::SnappedPoint(Inkscape::SnapCandidatePoint const &p, Snap
 
 }
 
-Inkscape::SnappedPoint::SnappedPoint(Geom::Point const &p, SnapSourceType const &source, long source_num, SnapTargetType const &target, Geom::Coord const &d, Geom::Coord const &t, bool const &a, bool const &at_intersection, bool const &fully_constrained, Geom::Coord const &d2, Geom::Coord const &t2, bool const &a2)
-    : _point(p), _source(source), _source_num(source_num), _target(target), _at_intersection(at_intersection), _fully_constrained(fully_constrained), _distance(d), _tolerance(std::max(t,1.0)), _always_snap(a),
+Inkscape::SnappedPoint::SnappedPoint(Geom::Point const &p, SnapSourceType const &source, long source_num, SnapTargetType const &target, Geom::Coord const &d, Geom::Coord const &t, bool const &a, bool const &at_intersection, bool const &constrained_snap, bool const &fully_constrained, Geom::Coord const &d2, Geom::Coord const &t2, bool const &a2)
+    : _point(p), _source(source), _source_num(source_num), _target(target), _at_intersection(at_intersection), _constrained_snap(constrained_snap), _fully_constrained(fully_constrained), _distance(d), _tolerance(std::max(t,1.0)), _always_snap(a),
     _second_distance(d2), _second_tolerance(std::max(t2,1.0)), _second_always_snap(a2)
 {
     // tolerance should never be smaller than 1 px, as it is used for normalization in
@@ -59,9 +61,10 @@ Inkscape::SnappedPoint::SnappedPoint()
 {
     _point = Geom::Point(0,0);
     _source = SNAPSOURCE_UNDEFINED,
-    _source_num = 0,
+    _source_num = -1,
     _target = SNAPTARGET_UNDEFINED,
     _at_intersection = false;
+    _constrained_snap = false;
     _fully_constrained = false;
     _distance = NR_HUGE;
     _tolerance = 1;
@@ -78,7 +81,7 @@ Inkscape::SnappedPoint::SnappedPoint(Geom::Point const &p)
 {
     _point = p;
     _source = SNAPSOURCE_UNDEFINED,
-    _source_num = 0,
+    _source_num = -1,
     _target = SNAPTARGET_UNDEFINED,
     _at_intersection = false;
     _fully_constrained = false;
@@ -97,7 +100,7 @@ Inkscape::SnappedPoint::~SnappedPoint()
 {
 }
 
-void Inkscape::SnappedPoint::getPoint(Geom::Point &p) const
+void Inkscape::SnappedPoint::getPointIfSnapped(Geom::Point &p) const
 {
     // When we have snapped
     if (getSnapped()) {
@@ -124,7 +127,7 @@ bool getClosestSP(std::list<Inkscape::SnappedPoint> const &list, Inkscape::Snapp
 bool Inkscape::SnappedPoint::isOtherSnapBetter(Inkscape::SnappedPoint const &other_one, bool weighted) const
 {
 
-    if (!other_one.getSnapped()) {
+    if (getSnapped() && !other_one.getSnapped()) {
         return false;
     }
 
@@ -135,7 +138,6 @@ bool Inkscape::SnappedPoint::isOtherSnapBetter(Inkscape::SnappedPoint const &oth
     // there's more than one). It is not useful when trying to find the best snapped target point.
     // (both the snap distance and the pointer distance are measured in document pixels, not in screen pixels)
     if (weighted) {
-
         Geom::Coord const dist_pointer_other = other_one.getPointerDistance();
         Geom::Coord const dist_pointer_this = getPointerDistance();
         // Weight factor: controls which node should be preferred for snapping, which is either
@@ -155,7 +157,8 @@ bool Inkscape::SnappedPoint::isOtherSnapBetter(Inkscape::SnappedPoint const &oth
                 // however be very large. To compare these in a fair way, we will have to normalize these metrics first
                 // The closest pointer distance will be normalized to 1.0; the other one will be > 1.0
                 // The snap distance will be normalized to 1.0 if it's equal to the snapper tolerance
-                double const norm_p = std::min(dist_pointer_this, dist_pointer_other);
+                double const norm_p = std::min(dist_pointer_this, dist_pointer_other) + 1;
+                // make sure norm_p is never too close to zero (e.g. when snapping the bbox-corner that was grabbed), by incr. with 1
                 double const norm_t_other = std::min(50.0, other_one.getTolerance());
                 double const norm_t_this = std::min(50.0, getTolerance());
                 dist_other = w * dist_pointer_other / norm_p + (1-w) * dist_other / norm_t_other;
@@ -164,6 +167,15 @@ bool Inkscape::SnappedPoint::isOtherSnapBetter(Inkscape::SnappedPoint const &oth
         }
     }
 
+    // When snapping to a constraint line only, which is not really a snap but merely a projection
+    // to the constraint line, then give this snap a very low priority. Basically, any other snap will do
+    if (other_one.getTarget() == SNAPTARGET_CONSTRAINT) {
+        dist_other += 1e6;
+    }
+    if (getTarget() == SNAPTARGET_CONSTRAINT) {
+        dist_this += 1e6;
+    }
+
     // If it's closer
     bool c1 = dist_other < dist_this;
     // or, if it's for a snapper with "always snap" turned on, and the previous wasn't
@@ -171,9 +183,9 @@ bool Inkscape::SnappedPoint::isOtherSnapBetter(Inkscape::SnappedPoint const &oth
     // But in no case fall back from a snapper with "always snap" on to one with "always snap" off
     bool c2n = !other_one.getAlwaysSnap() && getAlwaysSnap();
     // or, if we have a fully constrained snappoint (e.g. to a node or an intersection), while the previous one was only partly constrained (e.g. to a line)
-    bool c3 = other_one.getFullyConstrained() && !getFullyConstrained();
+    bool c3 = (other_one.getFullyConstrained() && !other_one.getConstrainedSnap()) && !getFullyConstrained(); // Do not consider constrained snaps here, because these will always be fully constrained anyway
     // But in no case fall back; (has less priority than c3n, so it is allowed to fall back when c3 is true, see below)
-    bool c3n = !other_one.getFullyConstrained() && getFullyConstrained();
+    bool c3n = !other_one.getFullyConstrained() && (getFullyConstrained() && !getConstrainedSnap());
 
     // When both are fully constrained AND coincident, then prefer nodes over intersections
     bool d = other_one.getFullyConstrained() && getFullyConstrained() && (Geom::L2(other_one.getPoint() - getPoint()) < 1e-9);
@@ -181,13 +193,21 @@ bool Inkscape::SnappedPoint::isOtherSnapBetter(Inkscape::SnappedPoint const &oth
     // But don't fall back...
     bool c4n = d && other_one.getAtIntersection() && !getAtIntersection();
 
-    // or, if it's just as close then consider the second distance
+    // or, if it's just as close then consider the second distance ...
     bool c5a = (dist_other == dist_this);
-    bool c5b = other_one.getSecondSnapDistance() < getSecondSnapDistance();
+    bool c5b = (other_one.getSecondSnapDistance() < getSecondSnapDistance()) && (getSecondSnapDistance() < NR_HUGE);
+    // ... or prefer free snaps over constrained snaps
+    bool c5c = !other_one.getConstrainedSnap() && getConstrainedSnap();
+
+    bool other_is_better = (c1 || c2 || c3 || c4 || (c5a && (c5b || c5c))) && !c2n && (!c3n || c2) && !c4n;
+
+    /*
+    std::cout << other_one.getPoint() << " (Other one, dist = " << dist_other << ") vs. " << getPoint() << " (this one, dist = " << dist_this << ") ---> ";
+    std::cout << "c1 = " << c1 << " | c2 = " << c2 << " | c2n = " << c2n << " | c3 = " << c3 << " | c3n = " << c3n << " | c4 = " << c4 << " | c4n = " << c4n << " | c5a = " << c5a << " | c5b = " << c5b << " | c5c = " << c5c << std::endl;
+    std::cout << "Other one provides a better snap: " << other_is_better << std::endl;
+    */
 
-    // std::cout << other_one.getPoint() << " (Other one, dist = " << dist_other << ") vs. " << getPoint() << " (this one, dist = " << dist_this << ") ---> ";
-    // std::cout << "c1 = " << c1 << " | c2 = " << c2 << " | c2n = " << c2n << " | c3 = " << c3 << " | c3n = " << c3n << " | c4 = " << c4 << " | c4n = " << c4n << " | c5a = " << c5a << " | c5b = " << c5b << std::endl;
-    return (c1 || c2 || c3 || c4 || (c5a && c5b)) && !c2n && (!c3n || c2) && !c4n;
+    return other_is_better;
 }
 
 /*