Context for the "About" label
[inkscape.git] / src / snapped-point.cpp
blob4134d13441d9c9a6b1e695f78a868d3584564a99
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /**
3 * \file src/snapped-point.cpp
4 * SnappedPoint class.
6 * Authors:
7 * Mathieu Dimanche <mdimanche@free.fr>
8 * Diederik van Lierop <mail@diedenrezi.nl>
10 * Released under GNU GPL v2+, read the file 'COPYING' for more information.
13 #include <utility>
14 #include "snapped-point.h"
15 #include "preferences.h"
17 // overloaded constructor
18 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) :
19 _point(p),
20 //_alignment_target(Geom::Point(0,0)),
21 //_alignment_target2(Geom::Point(0,0)),
22 _equal_distance(Geom::infinity()),
23 _tangent(Geom::Point(0,0)),
24 _source(source),
25 _source_num(source_num),
26 _target(target),
27 _at_intersection (false),
28 _constrained_snap (constrained_snap),
29 _fully_constrained (fully_constrained),
30 _distance(d),
31 _tolerance(std::max(t,1.0)),// tolerance should never be smaller than 1 px, as it is used for normalization in isOtherSnapBetter. We don't want a division by zero.
32 _always_snap(a),
33 _second_distance (Geom::infinity()),
34 _second_tolerance (1),
35 _second_always_snap (false),
36 _target_bbox(std::move(target_bbox)),
37 _pointer_distance (Geom::infinity())
41 Inkscape::SnappedPoint::SnappedPoint(Geom::Point const &p, Geom::Point const &ap, 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) :
42 _point(p),
43 _alignment_target(ap),
44 //_alignment_target2(Geom::Point(0,0)),
45 _alignment_target_type(target),
46 _equal_distance(Geom::infinity()),
47 _tangent(Geom::Point(0,0)),
48 _source(source),
49 _source_num(source_num),
50 _target(target),
51 _at_intersection (false),
52 _constrained_snap (constrained_snap),
53 _fully_constrained (fully_constrained),
54 _distance(d),
55 _tolerance(std::max(t,1.0)),// tolerance should never be smaller than 1 px, as it is used for normalization in isOtherSnapBetter. We don't want a division by zero.
56 _always_snap(a),
57 _second_distance (Geom::infinity()),
58 _second_tolerance (1),
59 _second_always_snap (false),
60 _target_bbox(std::move(target_bbox)),
61 _pointer_distance (Geom::infinity())
65 Inkscape::SnappedPoint::SnappedPoint(Geom::Point const &p, Geom::Point const &ap, Geom::Point const &ap2, 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) :
66 _point(p),
67 _alignment_target(ap),
68 _alignment_target2(ap2),
69 _alignment_target_type(target),
70 _equal_distance(Geom::infinity()),
71 _tangent(Geom::Point(0,0)),
72 _source(source),
73 _source_num(source_num),
74 _target(target),
75 _at_intersection (false),
76 _constrained_snap (constrained_snap),
77 _fully_constrained (fully_constrained),
78 _distance(d),
79 _tolerance(std::max(t,1.0)),// tolerance should never be smaller than 1 px, as it is used for normalization in isOtherSnapBetter. We don't want a division by zero.
80 _always_snap(a),
81 _second_distance (Geom::infinity()),
82 _second_tolerance (1),
83 _second_always_snap (false),
84 _target_bbox(std::move(target_bbox)),
85 _pointer_distance (Geom::infinity())
89 Inkscape::SnappedPoint::SnappedPoint(Geom::Point const &p, std::vector<Geom::Rect> const &bboxes, Geom::Rect const &source_bbox, Geom::Coord equal_dist, 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) :
90 _point(p),
91 _equal_distance(equal_dist),
92 _distribution_bboxes(std::move(bboxes)),
93 _source_bbox(source_bbox),
94 _tangent(Geom::Point(0,0)),
95 _source(source),
96 _source_num(source_num),
97 _target(target),
98 _at_intersection (false),
99 _constrained_snap (constrained_snap),
100 _fully_constrained (fully_constrained),
101 _distance(d),
102 _tolerance(std::max(t,1.0)),// tolerance should never be smaller than 1 px, as it is used for normalization in isOtherSnapBetter. We don't want a division by zero.
103 _always_snap(a),
104 _second_distance (Geom::infinity()),
105 _second_tolerance (1),
106 _second_always_snap (false),
107 //_target_bbox(std::move(target_bbox)),
108 _pointer_distance (Geom::infinity())
112 Inkscape::SnappedPoint::SnappedPoint(Geom::Point const &p, std::vector<Geom::Rect> const &bboxes, std::vector<Geom::Rect> const &bboxes2, Geom::Rect const &source_bbox, Geom::Coord equal_dist, Geom::Coord equal_dist2, 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) :
113 _point(p),
114 _equal_distance(equal_dist),
115 _equal_distance2(equal_dist2),
116 _distribution_bboxes(std::move(bboxes)),
117 _distribution_bboxes2(std::move(bboxes2)),
118 _source_bbox(source_bbox),
119 _tangent(Geom::Point(0,0)),
120 _source(source),
121 _source_num(source_num),
122 _target(target),
123 _at_intersection (false),
124 _constrained_snap (constrained_snap),
125 _fully_constrained (fully_constrained),
126 _distance(d),
127 _tolerance(std::max(t,1.0)),// tolerance should never be smaller than 1 px, as it is used for normalization in isOtherSnapBetter. We don't want a division by zero.
128 _always_snap(a),
129 _second_distance (Geom::infinity()),
130 _second_tolerance (1),
131 _second_always_snap (false),
132 //_target_bbox(std::move(target_bbox)),
133 _pointer_distance (Geom::infinity())
137 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) :
138 _point (p.getPoint()),
139 _equal_distance(Geom::infinity()),
140 _tangent (Geom::Point(0,0)),
141 _source (p.getSourceType()),
142 _source_num (p.getSourceNum()),
143 _target(target),
144 _at_intersection (false),
145 _constrained_snap (constrained_snap),
146 _fully_constrained (fully_constrained),
147 _distance(d),
148 _tolerance(std::max(t,1.0)),
149 _always_snap(a),
150 _second_distance (Geom::infinity()),
151 _second_tolerance (1),
152 _second_always_snap (false),
153 _target_bbox (p.getTargetBBox()),
154 _pointer_distance (Geom::infinity())
158 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) :
159 _point(p),
160 _equal_distance(Geom::infinity()),
161 _tangent (Geom::Point(0,0)),
162 _source(source),
163 _source_num(source_num),
164 _target(target),
165 _at_intersection(at_intersection),
166 _constrained_snap(constrained_snap),
167 _fully_constrained(fully_constrained),
168 _distance(d),
169 _tolerance(std::max(t,1.0)),
170 _always_snap(a),
171 _second_distance(d2),
172 _second_tolerance(std::max(t2,1.0)),
173 _second_always_snap(a2),
174 // tolerance should never be smaller than 1 px, as it is used for normalization in
175 // isOtherSnapBetter. We don't want a division by zero.
176 _target_bbox (Geom::OptRect()),
177 _pointer_distance (Geom::infinity())
181 Inkscape::SnappedPoint::SnappedPoint():
182 _point (Geom::Point(0,0)),
183 _equal_distance(Geom::infinity()),
184 _tangent (Geom::Point(0,0)),
185 _source (SNAPSOURCE_UNDEFINED),
186 _source_num (-1),
187 _target (SNAPTARGET_UNDEFINED),
188 _at_intersection (false),
189 _constrained_snap (false),
190 _fully_constrained (false),
191 _distance (Geom::infinity()),
192 _tolerance (1),
193 _always_snap (false),
194 _second_distance (Geom::infinity()),
195 _second_tolerance (1),
196 _second_always_snap (false),
197 _target_bbox (Geom::OptRect()),
198 _pointer_distance (Geom::infinity())
202 Inkscape::SnappedPoint::SnappedPoint(Geom::Point const &p):
203 _point (p),
204 _equal_distance(Geom::infinity()),
205 _tangent (Geom::Point(0,0)),
206 _source (SNAPSOURCE_UNDEFINED),
207 _source_num (-1),
208 _target (SNAPTARGET_UNDEFINED),
209 _at_intersection (false),
210 _constrained_snap (false),
211 _fully_constrained (false),
212 _distance (Geom::infinity()),
213 _tolerance (1),
214 _always_snap (false),
215 _second_distance (Geom::infinity()),
216 _second_tolerance (1),
217 _second_always_snap (false),
218 _target_bbox (Geom::OptRect()),
219 _pointer_distance (Geom::infinity())
223 Inkscape::SnappedPoint::~SnappedPoint()
224 = default;
226 void Inkscape::SnappedPoint::getPointIfSnapped(Geom::Point &p) const
228 // When we have snapped
229 if (getSnapped()) {
230 // then return the snapped point by overwriting p
231 p = _point;
232 } //otherwise p will be left untouched; this way the caller doesn't have to check whether we've snapped
235 // search for the closest snapped point
236 // This function give preference to the snapped points that are not in SNAPTARGET_ALIGNMENT_CATEGORY
237 // ie. for example, a longer Corner to Corner snap will be given prefrence over
238 // a Corner to alignment snap with lesser snapDistance.
240 // Incase there is an alignment snap along with a distribution snap possible, this
241 // function returns a new SnappedPoint that is are mix of the both
242 bool getClosestSP(std::list<Inkscape::SnappedPoint> const &list, Inkscape::SnappedPoint &result)
244 bool success = false;
245 bool aligned_success = false;
247 Inkscape::SnappedPoint aligned;
249 for (std::list<Inkscape::SnappedPoint>::const_iterator i = list.begin(); i != list.end(); ++i) {
250 bool alignment = (*i).getTarget() & Inkscape::SNAPTARGET_ALIGNMENT_CATEGORY;
251 if (i == list.begin()) {
252 result = *i;
253 success = !alignment;
254 aligned = *i;
255 aligned_success = alignment;
256 } else if (alignment) {
257 if (!aligned_success || (*i).getSnapDistance() <= aligned.getSnapDistance()) {
258 if ((*i).getSnapDistance() == aligned.getSnapDistance()) {
259 if ((*i).getDistanceToAlignTarget() < aligned.getDistanceToAlignTarget()) {
260 aligned = *i;
261 aligned_success = true;
263 } else {
264 aligned = *i;
265 aligned_success = true;
268 } else if (!success || (*i).getSnapDistance() < result.getSnapDistance()){
269 result = *i;
270 success = true;
275 if (!success && aligned_success)
276 result = aligned;
278 // the following code merges an alignment snap and a distribution snap
279 if (success && aligned_success) {
280 bool align_intersection = aligned.getTarget() == Inkscape::SNAPTARGET_ALIGNMENT_INTERSECTION;
281 auto type = result.getTarget();
283 if (type & Inkscape::SNAPTARGET_DISTRIBUTION_CATEGORY) {
284 switch (type) {
285 case Inkscape::SNAPTARGET_DISTRIBUTION_X:
286 case Inkscape::SNAPTARGET_DISTRIBUTION_RIGHT:
287 case Inkscape::SNAPTARGET_DISTRIBUTION_LEFT:
288 if (aligned.getPoint().y() == aligned.getAlignmentTarget()->y()) {
289 result.setPoint({result.getPoint().x(), aligned.getPoint().y()});
290 result.setAlignmentTargetType(Inkscape::SNAPTARGET_ALIGNMENT_BBOX_CORNER);
291 result.setAlignmentTarget(aligned.getAlignmentTarget());
293 if (align_intersection && abs(result.getPoint().x() - aligned.getAlignmentTarget2()->x()) <1e-4) {
294 result.setPoint(aligned.getPoint());
295 result.setAlignmentTarget2(aligned.getAlignmentTarget2());
296 result.setAlignmentTargetType(aligned.getAlignmentTargetType());
299 break;
301 case Inkscape::SNAPTARGET_DISTRIBUTION_Y:
302 case Inkscape::SNAPTARGET_DISTRIBUTION_UP:
303 case Inkscape::SNAPTARGET_DISTRIBUTION_DOWN:
304 if (!align_intersection) {
305 if (aligned.getPoint().x() == aligned.getAlignmentTarget()->x()) {
306 result.setPoint({aligned.getPoint().x(), result.getPoint().y()});
307 result.setAlignmentTargetType(aligned.getAlignmentTargetType());
308 result.setAlignmentTarget(aligned.getAlignmentTarget());
310 } else if (aligned.getPoint().x() == aligned.getAlignmentTarget2()->x()) {
311 result.setPoint({aligned.getPoint().x(), result.getPoint().y()});
312 result.setAlignmentTargetType(Inkscape::SNAPTARGET_ALIGNMENT_BBOX_CORNER);
313 result.setAlignmentTarget(aligned.getAlignmentTarget2());
315 if (abs(result.getPoint().y() - aligned.getAlignmentTarget()->y()) <1e-4) {
316 result.setPoint(aligned.getPoint());
317 result.setAlignmentTarget2(aligned.getAlignmentTarget());
318 result.setAlignmentTargetType(aligned.getAlignmentTargetType());
321 break;
323 case Inkscape::SNAPTARGET_DISTRIBUTION_XY:
324 if (Geom::L2(result.getPoint() - aligned.getPoint()) < 1e-4) {
325 result.setPoint(aligned.getPoint());
326 result.setAlignmentTargetType(aligned.getAlignmentTargetType());
327 result.setAlignmentTarget(aligned.getAlignmentTarget());
328 result.setAlignmentTarget2(aligned.getAlignmentTarget2());
330 break;
332 default:
333 g_warning("getClosestSP(): unknown distribution snap target %i", result.getTarget());
334 break;
337 return true;
341 return success ? success : aligned_success;
344 bool Inkscape::SnappedPoint::isOtherSnapBetter(Inkscape::SnappedPoint const &other_one, bool weighted) const
347 if (getSnapped() && !other_one.getSnapped()) {
348 return false;
351 if (!getSnapped() && other_one.getSnapped()) {
352 return true;
355 double dist_other = other_one.getSnapDistance();
356 double dist_this = getSnapDistance();
358 // The distance to the pointer should only be taken into account when finding the best snapped source node (when
359 // there's more than one). It is not useful when trying to find the best snapped target point.
360 // (both the snap distance and the pointer distance are measured in document pixels, not in screen pixels)
361 if (weighted) {
362 Geom::Coord const dist_pointer_other = other_one.getPointerDistance();
363 Geom::Coord const dist_pointer_this = getPointerDistance();
364 // Weight factor: controls which node should be preferred for snapping, which is either
365 // the node with the closest snap (w = 0), or the node closest to the mousepointer (w = 1)
366 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
367 double w = prefs->getDoubleLimited("/options/snapweight/value", 0.5, 0, 1);
368 if (prefs->getBool("/options/snapclosestonly/value", false)) {
369 w = 1;
371 if (w > 0) {
372 if (!(w == 1 && dist_pointer_this == dist_pointer_other)) {
373 // When accounting for the distance to the mouse pointer, then at least one of the snapped points should
374 // have that distance set. If not, then this is a bug. Either "weighted" must be set to false, or the
375 // mouse pointer distance must be set.
376 g_assert(dist_pointer_this != Geom::infinity() || dist_pointer_other != Geom::infinity());
377 // The snap distance will always be smaller than the tolerance set for the snapper. The pointer distance can
378 // however be very large. To compare these in a fair way, we will have to normalize these metrics first
379 // The closest pointer distance will be normalized to 1.0; the other one will be > 1.0
380 // The snap distance will be normalized to 1.0 if it's equal to the snapper tolerance
381 double const norm_p = std::min(dist_pointer_this, dist_pointer_other) + 1;
382 // make sure norm_p is never too close to zero (e.g. when snapping the bbox-corner that was grabbed), by incr. with 1
383 double const norm_t_other = std::min(50.0, other_one.getTolerance());
384 double const norm_t_this = std::min(50.0, getTolerance());
385 dist_other = w * dist_pointer_other / norm_p + (1-w) * dist_other / norm_t_other;
386 dist_this = w * dist_pointer_this / norm_p + (1-w) * dist_this / norm_t_this;
391 // When snapping to a constraint line only, which is not really a snap but merely a projection
392 // to the constraint line, then give this snap a very low priority. Basically, any other snap will do
393 if (other_one.getTarget() == SNAPTARGET_CONSTRAINT) {
394 dist_other += 1e6;
396 if (getTarget() == SNAPTARGET_CONSTRAINT) {
397 dist_this += 1e6;
400 // If it's closer
401 bool c1 = dist_other < dist_this;
402 // or, if it's for a snapper with "always snap" turned on, and the previous wasn't
403 bool c2 = other_one.getAlwaysSnap() && !getAlwaysSnap();
404 // But in no case fall back from a snapper with "always snap" on to one with "always snap" off
405 bool c2n = !other_one.getAlwaysSnap() && getAlwaysSnap();
406 // 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)
407 bool c3 = (other_one.getFullyConstrained() && !other_one.getConstrainedSnap()) && !getFullyConstrained(); // Do not consider constrained snaps here, because these will always be fully constrained anyway
408 // But in no case fall back; (has less priority than c3n, so it is allowed to fall back when c3 is true, see below)
409 bool c3n = !other_one.getFullyConstrained() && (getFullyConstrained() && !getConstrainedSnap());
411 // When both are fully constrained AND coincident, then prefer nodes over intersections
412 bool d = other_one.getFullyConstrained() && getFullyConstrained() && (Geom::L2(other_one.getPoint() - getPoint()) < 1e-9);
413 bool c4 = d && !other_one.getAtIntersection() && getAtIntersection();
414 // But don't fall back...
415 bool c4n = d && other_one.getAtIntersection() && !getAtIntersection();
417 // or, if it's just as close then consider the second distance ...
418 bool c5a = (dist_other == dist_this);
419 bool c5b = (other_one.getSecondSnapDistance() < getSecondSnapDistance()) && (getSecondSnapDistance() < Geom::infinity());
420 // ... or prefer free snaps over constrained snaps
421 bool c5c = !other_one.getConstrainedSnap() && getConstrainedSnap();
423 bool other_is_better = (c1 || c2 || c3 || c4 || (c5a && (c5b || c5c))) && !c2n && (!c3n || c2) && !c4n;
426 std::cout << other_one.getPoint() << " (Other one, dist = " << dist_other << ") vs. " << getPoint() << " (this one, dist = " << dist_this << ") ---> ";
427 std::cout << "c1 = " << c1 << " | c2 = " << c2 << " | c2n = " << c2n << " | c3 = " << c3 << " | c3n = " << c3n << " | c4 = " << c4 << " | c4n = " << c4n << " | c5a = " << c5a << " | c5b = " << c5b << " | c5c = " << c5c << std::endl;
428 std::cout << "Other one provides a better snap: " << other_is_better << std::endl;
431 return other_is_better;
434 // Returns true if the snapped point is on a path (or on a line); returns false for e.g. bounding box edges, page border, and text baseline
435 bool Inkscape::SnappedPoint::getOnPath() const {
436 std::vector<Inkscape::SnapTargetType> snaptargets_on_path{
437 Inkscape::SNAPTARGET_LINE_MIDPOINT,
438 Inkscape::SNAPTARGET_PATH,
439 Inkscape::SNAPTARGET_PATH_PERPENDICULAR,
440 Inkscape::SNAPTARGET_PATH_TANGENTIAL,
441 Inkscape::SNAPTARGET_PATH_INTERSECTION,
442 Inkscape::SNAPTARGET_PATH_GUIDE_INTERSECTION,
443 Inkscape::SNAPTARGET_PATH_CLIP,
444 Inkscape::SNAPTARGET_PATH_MASK,
445 Inkscape::SNAPTARGET_ELLIPSE_QUADRANT_POINT
448 return std::find(snaptargets_on_path.begin(), snaptargets_on_path.end(), _target) != snaptargets_on_path.end();
453 Local Variables:
454 mode:c++
455 c-file-style:"stroustrup"
456 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
457 indent-tabs-mode:nil
458 fill-column:99
459 End:
461 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :