Inkscape
Vector Graphics Editor
Loading...
Searching...
No Matches
snapped-point.cpp
Go to the documentation of this file.
1// SPDX-License-Identifier: GPL-2.0-or-later
13#include <utility>
14#include "snapped-point.h"
15#include "preferences.h"
16
17// overloaded constructor
18Inkscape::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())
38{
39}
40
41Inkscape::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())
62{
63}
64
65Inkscape::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())
86{
87}
88
89Inkscape::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())
109{
110}
111
112Inkscape::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())
134{
135}
136
137Inkscape::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())
155{
156}
157
158Inkscape::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())
178{
179}
180
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())
199{
200}
201
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())
220{
221}
222
224= default;
225
227{
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
233}
234
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.
239//
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
242bool getClosestSP(std::list<Inkscape::SnappedPoint> const &list, Inkscape::SnappedPoint &result)
243{
244 bool success = false;
245 bool aligned_success = false;
246
248
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;
262 }
263 } else {
264 aligned = *i;
265 aligned_success = true;
266 }
267 }
268 } else if (!success || (*i).getSnapDistance() < result.getSnapDistance()){
269 result = *i;
270 success = true;
271 }
272
273 }
274
275 if (!success && aligned_success)
276 result = aligned;
277
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();
282
284 switch (type) {
288 if (aligned.getPoint().y() == aligned.getAlignmentTarget()->y()) {
289 result.setPoint({result.getPoint().x(), aligned.getPoint().y()});
291 result.setAlignmentTarget(aligned.getAlignmentTarget());
292
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());
297 }
298 }
299 break;
300
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());
309 }
310 } else if (aligned.getPoint().x() == aligned.getAlignmentTarget2()->x()) {
311 result.setPoint({aligned.getPoint().x(), result.getPoint().y()});
313 result.setAlignmentTarget(aligned.getAlignmentTarget2());
314
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());
319 }
320 }
321 break;
322
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());
329 }
330 break;
331
332 default:
333 g_warning("getClosestSP(): unknown distribution snap target %i", result.getTarget());
334 break;
335 }
336
337 return true;
338 }
339 }
340
341 return success ? success : aligned_success;
342}
343
344bool Inkscape::SnappedPoint::isOtherSnapBetter(Inkscape::SnappedPoint const &other_one, bool weighted) const
345{
346
347 if (getSnapped() && !other_one.getSnapped()) {
348 return false;
349 }
350
351 if (!getSnapped() && other_one.getSnapped()) {
352 return true;
353 }
354
355 double dist_other = other_one.getSnapDistance();
356 double dist_this = getSnapDistance();
357
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)
367 double w = prefs->getDoubleLimited("/options/snapweight/value", 0.5, 0, 1);
368 if (prefs->getBool("/options/snapclosestonly/value", false)) {
369 w = 1;
370 }
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;
387 }
388 }
389 }
390
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;
395 }
396 if (getTarget() == SNAPTARGET_CONSTRAINT) {
397 dist_this += 1e6;
398 }
399
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());
410
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();
416
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();
422
423 bool other_is_better = (c1 || c2 || c3 || c4 || (c5a && (c5b || c5c))) && !c2n && (!c3n || c2) && !c4n;
424
425 /*
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;
429 */
430
431 return other_is_better;
432}
433
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
436 std::vector<Inkscape::SnapTargetType> snaptargets_on_path{
446 };
447
448 return std::find(snaptargets_on_path.begin(), snaptargets_on_path.end(), _target) != snaptargets_on_path.end();
449}
450
451
452/*
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:
460*/
461// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :
pair< double, double > Point
Definition parser.cpp:7
Axis-aligned rectangle that can be empty.
Definition rect.h:203
Two-dimensional point that doubles as a vector.
Definition point.h:66
constexpr Coord y() const noexcept
Definition point.h:106
constexpr Coord x() const noexcept
Definition point.h:104
Axis aligned, non-empty rectangle.
Definition rect.h:92
Preference storage class.
Definition preferences.h:61
bool getBool(Glib::ustring const &pref_path, bool def=false)
Retrieve a Boolean value.
static Preferences * get()
Access the singleton Preferences object.
double getDoubleLimited(Glib::ustring const &pref_path, double def=0.0, double min=DBL_MIN, double max=DBL_MAX, Glib::ustring const &unit="")
Retrieve a limited floating point value.
Class to store data for points which are snap candidates, either as a source or as a target.
Class describing the result of an attempt to snap.
std::optional< Geom::Point > getAlignmentTarget() const
bool getConstrainedSnap() const
void getPointIfSnapped(Geom::Point &p) const
SnapTargetType getTarget() const
std::optional< Geom::Point > getAlignmentTarget2() const
Geom::Point getPoint() const
Geom::Coord getTolerance() const
bool getAlwaysSnap() const
Geom::Coord getPointerDistance() const
Geom::Coord getSnapDistance() const
bool isOtherSnapBetter(SnappedPoint const &other_one, bool weighted) const
bool getAtIntersection() const
SnapTargetType getAlignmentTargetType() const
bool getFullyConstrained() const
Geom::Coord getSecondSnapDistance() const
Geom::Coord getDistanceToAlignTarget() const
const double w
Definition conic-4.cpp:19
Css & result
constexpr Coord infinity()
Get a value representing infinity.
Definition coord.h:88
double Coord
Floating point type used to store coordinates.
Definition coord.h:76
Various utility functions.
Definition affine.h:22
SBasis L2(D2< SBasis > const &a, unsigned k)
Definition d2-sbasis.cpp:42
SnapSourceType
enumerations of snap source types and snap target types.
Definition snap-enums.h:18
@ SNAPSOURCE_UNDEFINED
Definition snap-enums.h:19
@ SNAPTARGET_UNDEFINED
Definition snap-enums.h:71
@ SNAPTARGET_PATH_GUIDE_INTERSECTION
Definition snap-enums.h:89
@ SNAPTARGET_CONSTRAINT
Definition snap-enums.h:121
@ SNAPTARGET_PATH
Definition snap-enums.h:85
@ SNAPTARGET_DISTRIBUTION_XY
Definition snap-enums.h:146
@ SNAPTARGET_DISTRIBUTION_LEFT
Definition snap-enums.h:143
@ SNAPTARGET_DISTRIBUTION_DOWN
Definition snap-enums.h:145
@ SNAPTARGET_LINE_MIDPOINT
Definition snap-enums.h:84
@ SNAPTARGET_ALIGNMENT_CATEGORY
Definition snap-enums.h:125
@ SNAPTARGET_ALIGNMENT_INTERSECTION
Definition snap-enums.h:135
@ SNAPTARGET_PATH_CLIP
Definition snap-enums.h:90
@ SNAPTARGET_DISTRIBUTION_UP
Definition snap-enums.h:144
@ SNAPTARGET_ELLIPSE_QUADRANT_POINT
Definition snap-enums.h:92
@ SNAPTARGET_DISTRIBUTION_CATEGORY
Definition snap-enums.h:139
@ SNAPTARGET_DISTRIBUTION_X
Definition snap-enums.h:140
@ SNAPTARGET_PATH_MASK
Definition snap-enums.h:91
@ SNAPTARGET_ALIGNMENT_BBOX_CORNER
Definition snap-enums.h:126
@ SNAPTARGET_DISTRIBUTION_Y
Definition snap-enums.h:141
@ SNAPTARGET_PATH_TANGENTIAL
Definition snap-enums.h:87
@ SNAPTARGET_PATH_PERPENDICULAR
Definition snap-enums.h:86
@ SNAPTARGET_PATH_INTERSECTION
Definition snap-enums.h:88
@ SNAPTARGET_DISTRIBUTION_RIGHT
Definition snap-enums.h:142
STL namespace.
Singleton class to access the preferences file in a convenient way.
bool getClosestSP(std::list< Inkscape::SnappedPoint > const &list, Inkscape::SnappedPoint &result)
SnappedPoint class.