Code

- Constrained snap: proper implementation of the preference to snap the mouse pointer...
[inkscape.git] / src / snapped-point.cpp
1 /**
2  *  \file src/snapped-point.cpp
3  *  \brief SnappedPoint class.
4  *
5  *  Authors:
6  *    Mathieu Dimanche <mdimanche@free.fr>
7  *    Diederik van Lierop <mail@diedenrezi.nl>
8  *
9  *  Released under GNU GPL, read the file 'COPYING' for more information.
10  */
12 #include <gtk/gtk.h>
13 #include "snapped-point.h"
14 #include "preferences.h"
16 // overloaded constructor
17 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)
18     : _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)
19 {
20     // tolerance should never be smaller than 1 px, as it is used for normalization in isOtherSnapBetter. We don't want a division by zero.
21     _at_intersection = false;
22     _constrained_snap = constrained_snap;
23     _fully_constrained = fully_constrained;
24     _second_distance = NR_HUGE;
25     _second_tolerance = 1;
26     _second_always_snap = false;
27     _transformation = Geom::Point(1,1);
28     _pointer_distance = NR_HUGE;
29 }
31 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)
32     : _target(target), _distance(d), _tolerance(std::max(t,1.0)), _always_snap(a)
33 {
34     _point = p.getPoint();
35     _source = p.getSourceType();
36     _source_num = p.getSourceNum();
37     _at_intersection = false;
38     _constrained_snap = constrained_snap;
39     _fully_constrained = fully_constrained;
40     _second_distance = NR_HUGE;
41     _second_tolerance = 1;
42     _second_always_snap = false;
43     _transformation = Geom::Point(1,1);
44     _pointer_distance = NR_HUGE;
45     _target_bbox = p.getTargetBBox();
47 }
49 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)
50     : _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),
51     _second_distance(d2), _second_tolerance(std::max(t2,1.0)), _second_always_snap(a2)
52 {
53     // tolerance should never be smaller than 1 px, as it is used for normalization in
54     // isOtherSnapBetter. We don't want a division by zero.
55     _transformation = Geom::Point(1,1);
56     _pointer_distance = NR_HUGE;
57     _target_bbox = Geom::OptRect();
58 }
60 Inkscape::SnappedPoint::SnappedPoint()
61 {
62     _point = Geom::Point(0,0);
63     _source = SNAPSOURCE_UNDEFINED,
64     _source_num = -1,
65     _target = SNAPTARGET_UNDEFINED,
66     _at_intersection = false;
67     _constrained_snap = false;
68     _fully_constrained = false;
69     _distance = NR_HUGE;
70     _tolerance = 1;
71     _always_snap = false;
72     _second_distance = NR_HUGE;
73     _second_tolerance = 1;
74     _second_always_snap = false;
75     _transformation = Geom::Point(1,1);
76     _pointer_distance = NR_HUGE;
77     _target_bbox = Geom::OptRect();
78 }
80 Inkscape::SnappedPoint::SnappedPoint(Geom::Point const &p)
81 {
82     _point = p;
83     _source = SNAPSOURCE_UNDEFINED,
84     _source_num = -1,
85     _target = SNAPTARGET_UNDEFINED,
86     _at_intersection = false;
87     _fully_constrained = false;
88     _distance = NR_HUGE;
89     _tolerance = 1;
90     _always_snap = false;
91     _second_distance = NR_HUGE;
92     _second_tolerance = 1;
93     _second_always_snap = false;
94     _transformation = Geom::Point(1,1);
95     _pointer_distance = NR_HUGE;
96     _target_bbox = Geom::OptRect();
97 }
99 Inkscape::SnappedPoint::~SnappedPoint()
103 void Inkscape::SnappedPoint::getPointIfSnapped(Geom::Point &p) const
105     // When we have snapped
106     if (getSnapped()) {
107         // then return the snapped point by overwriting p
108         p = _point;
109     } //otherwise p will be left untouched; this way the caller doesn't have to check whether we've snapped
112 // search for the closest snapped point
113 bool getClosestSP(std::list<Inkscape::SnappedPoint> const &list, Inkscape::SnappedPoint &result)
115     bool success = false;
117     for (std::list<Inkscape::SnappedPoint>::const_iterator i = list.begin(); i != list.end(); i++) {
118         if ((i == list.begin()) || (*i).getSnapDistance() < result.getSnapDistance()) {
119             result = *i;
120             success = true;
121         }
122     }
124     return success;
127 bool Inkscape::SnappedPoint::isOtherSnapBetter(Inkscape::SnappedPoint const &other_one, bool weighted) const
130     if (!other_one.getSnapped()) {
131         return false;
132     }
134     double dist_other = other_one.getSnapDistance();
135     double dist_this = getSnapDistance();
137     // The distance to the pointer should only be taken into account when finding the best snapped source node (when
138     // there's more than one). It is not useful when trying to find the best snapped target point.
139     // (both the snap distance and the pointer distance are measured in document pixels, not in screen pixels)
140     if (weighted) {
141         Geom::Coord const dist_pointer_other = other_one.getPointerDistance();
142         Geom::Coord const dist_pointer_this = getPointerDistance();
143         // Weight factor: controls which node should be preferred for snapping, which is either
144         // the node with the closest snap (w = 0), or the node closest to the mousepointer (w = 1)
145         Inkscape::Preferences *prefs = Inkscape::Preferences::get();
146         double w = prefs->getDoubleLimited("/options/snapweight/value", 0.5, 0, 1);
147         if (prefs->getBool("/options/snapclosestonly/value", false)) {
148             w = 1;
149         }
150         if (w > 0) {
151             if (!(w == 1 && dist_pointer_this == dist_pointer_other)) {
152                 // When accounting for the distance to the mouse pointer, then at least one of the snapped points should
153                 // have that distance set. If not, then this is a bug. Either "weighted" must be set to false, or the
154                 // mouse pointer distance must be set.
155                 g_assert(dist_pointer_this != NR_HUGE || dist_pointer_other != NR_HUGE);
156                 // The snap distance will always be smaller than the tolerance set for the snapper. The pointer distance can
157                 // however be very large. To compare these in a fair way, we will have to normalize these metrics first
158                 // The closest pointer distance will be normalized to 1.0; the other one will be > 1.0
159                 // The snap distance will be normalized to 1.0 if it's equal to the snapper tolerance
160                 double const norm_p = std::min(dist_pointer_this, dist_pointer_other) + 1;
161                 // make sure norm_p is never too close to zero (e.g. when snapping the bbox-corner that was grabbed), by incr. with 1
162                 double const norm_t_other = std::min(50.0, other_one.getTolerance());
163                 double const norm_t_this = std::min(50.0, getTolerance());
164                 dist_other = w * dist_pointer_other / norm_p + (1-w) * dist_other / norm_t_other;
165                 dist_this = w * dist_pointer_this / norm_p + (1-w) * dist_this / norm_t_this;
166             }
167         }
168     }
170     // When snapping to a constraint line only, which is not really a snap but merely a projection
171     // to the constraint line, then give this snap a very low priority. Basically, any other snap will do
172     if (other_one.getTarget() == SNAPTARGET_CONSTRAINT) {
173         dist_other += 1e6;
174     }
175     if (getTarget() == SNAPTARGET_CONSTRAINT) {
176         dist_this += 1e6;
177     }
179     // If it's closer
180     bool c1 = dist_other < dist_this;
181     // or, if it's for a snapper with "always snap" turned on, and the previous wasn't
182     bool c2 = other_one.getAlwaysSnap() && !getAlwaysSnap();
183     // But in no case fall back from a snapper with "always snap" on to one with "always snap" off
184     bool c2n = !other_one.getAlwaysSnap() && getAlwaysSnap();
185     // 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)
186     bool c3 = (other_one.getFullyConstrained() && !other_one.getConstrainedSnap()) && !getFullyConstrained(); // Do not consider constrained snaps here, because these will always be fully constrained anyway
187     // But in no case fall back; (has less priority than c3n, so it is allowed to fall back when c3 is true, see below)
188     bool c3n = !other_one.getFullyConstrained() && (getFullyConstrained() && !getConstrainedSnap());
190     // When both are fully constrained AND coincident, then prefer nodes over intersections
191     bool d = other_one.getFullyConstrained() && getFullyConstrained() && (Geom::L2(other_one.getPoint() - getPoint()) < 1e-9);
192     bool c4 = d && !other_one.getAtIntersection() && getAtIntersection();
193     // But don't fall back...
194     bool c4n = d && other_one.getAtIntersection() && !getAtIntersection();
196     // or, if it's just as close then consider the second distance ...
197     bool c5a = (dist_other == dist_this);
198     bool c5b = (other_one.getSecondSnapDistance() < getSecondSnapDistance()) && (getSecondSnapDistance() < NR_HUGE);
199     // ... or prefer free snaps over constrained snaps
200     bool c5c = !other_one.getConstrainedSnap() && getConstrainedSnap();
202     bool other_is_better = (c1 || c2 || c3 || c4 || (c5a && (c5b || c5c))) && !c2n && (!c3n || c2) && !c4n;
204     /*
205     std::cout << other_one.getPoint() << " (Other one, dist = " << dist_other << ") vs. " << getPoint() << " (this one, dist = " << dist_this << ") ---> ";
206     std::cout << "c1 = " << c1 << " | c2 = " << c2 << " | c2n = " << c2n << " | c3 = " << c3 << " | c3n = " << c3n << " | c4 = " << c4 << " | c4n = " << c4n << " | c5a = " << c5a << " | c5b = " << c5b << " | c5c = " << c5c << std::endl;
207     std::cout << "Other one provides a better snap: " << other_is_better << std::endl;
208     */
210     return other_is_better;
213 /*
214   Local Variables:
215   mode:c++
216   c-file-style:"stroustrup"
217   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
218   indent-tabs-mode:nil
219   fill-column:99
220   End:
221 */
222 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :