Code

Patch by Martin Sucha to add preferences to for grouping on Clip/Mask
[inkscape.git] / src / snap.cpp
1 #define __SP_DESKTOP_SNAP_C__
3 /**
4  * \file snap.cpp
5  * \brief SnapManager class.
6  *
7  * Authors:
8  *   Lauris Kaplinski <lauris@kaplinski.com>
9  *   Frank Felfe <innerspace@iname.com>
10  *   Nathan Hurst <njh@njhurst.com>
11  *   Carl Hetherington <inkscape@carlh.net>
12  *   Diederik van Lierop <mail@diedenrezi.nl>
13  *
14  * Copyright (C) 2006-2007 Johan Engelen <johan@shouraizou.nl>
15  * Copyrigth (C) 2004      Nathan Hurst
16  * Copyright (C) 1999-2010 Authors
17  *
18  * Released under GNU GPL, read the file 'COPYING' for more information
19  */
21 #include <utility>
23 #include "sp-namedview.h"
24 #include "snap.h"
25 #include "snapped-line.h"
26 #include "snapped-curve.h"
28 #include "display/canvas-grid.h"
29 #include "display/snap-indicator.h"
31 #include "inkscape.h"
32 #include "desktop.h"
33 #include "selection.h"
34 #include "sp-guide.h"
35 #include "preferences.h"
36 #include "event-context.h"
37 using std::vector;
39 /**
40  *  Construct a SnapManager for a SPNamedView.
41  *
42  *  \param v `Owning' SPNamedView.
43  */
45 SnapManager::SnapManager(SPNamedView const *v) :
46     guide(this, 0),
47     object(this, 0),
48     snapprefs(),
49     _named_view(v)
50 {
51 }
53 /**
54  *  \brief Return a list of snappers
55  *
56  *  Inkscape snaps to objects, grids, and guides. For each of these snap targets a
57  *  separate class is used, which has been derived from the base Snapper class. The
58  *  getSnappers() method returns a list of pointers to instances of this class. This
59  *  list contains exactly one instance of the guide snapper and of the object snapper
60  *  class, but any number of grid snappers (because each grid has its own snapper
61  *  instance)
62  *
63  *  \return List of snappers that we use.
64  */
65 SnapManager::SnapperList
66 SnapManager::getSnappers() const
67 {
68     SnapManager::SnapperList s;
69     s.push_back(&guide);
70     s.push_back(&object);
72     SnapManager::SnapperList gs = getGridSnappers();
73     s.splice(s.begin(), gs);
75     return s;
76 }
78 /**
79  *  \brief Return a list of gridsnappers
80  *
81  *  Each grid has its own instance of the snapper class. This way snapping can
82  *  be enabled per grid individually. A list will be returned containing the
83  *  pointers to these instances, but only for grids that are being displayed
84  *  and for which snapping is enabled.
85  *
86  *  \return List of gridsnappers that we use.
87  */
88 SnapManager::SnapperList
89 SnapManager::getGridSnappers() const
90 {
91     SnapperList s;
93     if (_desktop && _desktop->gridsEnabled() && snapprefs.getSnapToGrids()) {
94         for ( GSList const *l = _named_view->grids; l != NULL; l = l->next) {
95             Inkscape::CanvasGrid *grid = (Inkscape::CanvasGrid*) l->data;
96             s.push_back(grid->snapper);
97         }
98     }
100     return s;
103 /**
104  * \brief Return true if any snapping might occur, whether its to grids, guides or objects
105  *
106  * Each snapper instance handles its own snapping target, e.g. grids, guides or
107  * objects. This method iterates through all these snapper instances and returns
108  * true if any of the snappers might possible snap, considering only the relevant
109  * snapping preferences.
110  *
111  * \return true if one of the snappers will try to snap to something.
112  */
114 bool SnapManager::someSnapperMightSnap() const
116     if ( !snapprefs.getSnapEnabledGlobally() || snapprefs.getSnapPostponedGlobally() ) {
117         return false;
118     }
120     SnapperList const s = getSnappers();
121     SnapperList::const_iterator i = s.begin();
122     while (i != s.end() && (*i)->ThisSnapperMightSnap() == false) {
123         i++;
124     }
126     return (i != s.end());
129 /**
130  * \return true if one of the grids might be snapped to.
131  */
133 bool SnapManager::gridSnapperMightSnap() const
135     if ( !snapprefs.getSnapEnabledGlobally() || snapprefs.getSnapPostponedGlobally() ) {
136         return false;
137     }
139     SnapperList const s = getGridSnappers();
140     SnapperList::const_iterator i = s.begin();
141     while (i != s.end() && (*i)->ThisSnapperMightSnap() == false) {
142         i++;
143     }
145     return (i != s.end());
148 /**
149  *  \brief Try to snap a point to grids, guides or objects.
150  *
151  *  Try to snap a point to grids, guides or objects, in two degrees-of-freedom,
152  *  i.e. snap in any direction on the two dimensional canvas to the nearest
153  *  snap target. freeSnapReturnByRef() is equal in snapping behavior to
154  *  freeSnap(), but the former returns the snapped point trough the referenced
155  *  parameter p. This parameter p initially contains the position of the snap
156  *  source and will we overwritten by the target position if snapping has occurred.
157  *  This makes snapping transparent to the calling code. If this is not desired
158  *  because either the calling code must know whether snapping has occurred, or
159  *  because the original position should not be touched, then freeSnap() should be
160  *  called instead.
161  *
162  *  PS:
163  *  1) SnapManager::setup() must have been called before calling this method,
164  *  but only once for a set of points
165  *  2) Only to be used when a single source point is to be snapped; it assumes
166  *  that source_num = 0, which is inefficient when snapping sets our source points
167  *
168  *  \param p Current position of the snap source; will be overwritten by the position of the snap target if snapping has occurred
169  *  \param source_type Detailed description of the source type, will be used by the snap indicator
170  *  \param bbox_to_snap Bounding box hulling the set of points, all from the same selection and having the same transformation
171  */
173 void SnapManager::freeSnapReturnByRef(Geom::Point &p,
174                                       Inkscape::SnapSourceType const source_type,
175                                       Geom::OptRect const &bbox_to_snap) const
177     //TODO: SnapCandidatePoint and point_type are somewhat redundant; can't we get rid of the point_type parameter?
178     Inkscape::SnappedPoint const s = freeSnap(Inkscape::SnapCandidatePoint(p, source_type), bbox_to_snap);
179     s.getPoint(p);
183 /**
184  *  \brief Try to snap a point to grids, guides or objects.
185  *
186  *  Try to snap a point to grids, guides or objects, in two degrees-of-freedom,
187  *  i.e. snap in any direction on the two dimensional canvas to the nearest
188  *  snap target. freeSnap() is equal in snapping behavior to
189  *  freeSnapReturnByRef(). Please read the comments of the latter for more details
190  *
191  *  PS: SnapManager::setup() must have been called before calling this method,
192  *  but only once for a set of points
193  *
194  *  \param p Source point to be snapped
195  *  \param bbox_to_snap Bounding box hulling the set of points, all from the same selection and having the same transformation
196  *  \return An instance of the SnappedPoint class, which holds data on the snap source, snap target, and various metrics
197  */
200 Inkscape::SnappedPoint SnapManager::freeSnap(Inkscape::SnapCandidatePoint const &p,
201                                              Geom::OptRect const &bbox_to_snap) const
203     if (!someSnapperMightSnap()) {
204         return Inkscape::SnappedPoint(p, Inkscape::SNAPTARGET_UNDEFINED, NR_HUGE, 0, false, false);
205     }
207     SnappedConstraints sc;
208     SnapperList const snappers = getSnappers();
210     for (SnapperList::const_iterator i = snappers.begin(); i != snappers.end(); i++) {
211         (*i)->freeSnap(sc, p, bbox_to_snap, &_items_to_ignore, _unselected_nodes);
212     }
214     return findBestSnap(p, sc, false);
217 void SnapManager::preSnap(Inkscape::SnapCandidatePoint const &p)
219     // setup() must have been called before calling this method!
221     if (_snapindicator) {
222         _snapindicator = false; // prevent other methods from drawing a snap indicator; we want to control this here
223         Inkscape::SnappedPoint s = freeSnap(p);
224         if (s.getSnapped()) {
225             _desktop->snapindicator->set_new_snaptarget(s, true);
226         } else {
227             _desktop->snapindicator->remove_snaptarget();
228         }
229         _snapindicator = true; // restore the original value
230     }
233 /**
234  * \brief Snap to the closest multiple of a grid pitch
235  *
236  * When pasting, we would like to snap to the grid. Problem is that we don't know which
237  * nodes were aligned to the grid at the time of copying, so we don't know which nodes
238  * to snap. If we'd snap an unaligned node to the grid, previously aligned nodes would
239  * become unaligned. That's undesirable. Instead we will make sure that the offset
240  * between the source and its pasted copy is a multiple of the grid pitch. If the source
241  * was aligned, then the copy will therefore also be aligned.
242  *
243  * PS: Whether we really find a multiple also depends on the snapping range! Most users
244  * will have "always snap" enabled though, in which case a multiple will always be found.
245  * PS2: When multiple grids are present then the result will become ambiguous. There is no
246  * way to control to which grid this method will snap.
247  *
248  * \param t Vector that represents the offset of the pasted copy with respect to the original
249  * \return Offset vector after snapping to the closest multiple of a grid pitch
250  */
252 Geom::Point SnapManager::multipleOfGridPitch(Geom::Point const &t) const
254     if (!snapprefs.getSnapEnabledGlobally()) // No need to check for snapprefs.getSnapPostponedGlobally() here
255         return t;
257     if (_desktop && _desktop->gridsEnabled()) {
258         bool success = false;
259         Geom::Point nearest_multiple;
260         Geom::Coord nearest_distance = NR_HUGE;
262         // It will snap to the grid for which we find the closest snap. This might be a different
263         // grid than to which the objects were initially aligned. I don't see an easy way to fix
264         // this, so when using multiple grids one can get unexpected results
266         // Cannot use getGridSnappers() because we need both the grids AND their snappers
267         // Therefore we iterate through all grids manually
268         for (GSList const *l = _named_view->grids; l != NULL; l = l->next) {
269             Inkscape::CanvasGrid *grid = (Inkscape::CanvasGrid*) l->data;
270             const Inkscape::Snapper* snapper = grid->snapper;
271             if (snapper && snapper->ThisSnapperMightSnap()) {
272                 // To find the nearest multiple of the grid pitch for a given translation t, we
273                 // will use the grid snapper. Simply snapping the value t to the grid will do, but
274                 // only if the origin of the grid is at (0,0). If it's not then compensate for this
275                 // in the translation t
276                 Geom::Point const t_offset = t + grid->origin;
277                 SnappedConstraints sc;
278                 // Only the first three parameters are being used for grid snappers
279                 snapper->freeSnap(sc, Inkscape::SnapCandidatePoint(t_offset, Inkscape::SNAPSOURCE_UNDEFINED),Geom::OptRect(), NULL, NULL);
280                 // Find the best snap for this grid, including intersections of the grid-lines
281                 Inkscape::SnappedPoint s = findBestSnap(Inkscape::SnapCandidatePoint(t_offset, Inkscape::SNAPSOURCE_UNDEFINED), sc, false);
282                 if (s.getSnapped() && (s.getSnapDistance() < nearest_distance)) {
283                     // use getSnapDistance() instead of getWeightedDistance() here because the pointer's position
284                     // doesn't tell us anything about which node to snap
285                     success = true;
286                     nearest_multiple = s.getPoint() - to_2geom(grid->origin);
287                     nearest_distance = s.getSnapDistance();
288                 }
289             }
290         }
292         if (success)
293             return nearest_multiple;
294     }
296     return t;
299 /**
300  *  \brief Try to snap a point along a constraint line to grids, guides or objects.
301  *
302  *  Try to snap a point to grids, guides or objects, in only one degree-of-freedom,
303  *  i.e. snap in a specific direction on the two dimensional canvas to the nearest
304  *  snap target.
305  *
306  *  constrainedSnapReturnByRef() is equal in snapping behavior to
307  *  constrainedSnap(), but the former returns the snapped point trough the referenced
308  *  parameter p. This parameter p initially contains the position of the snap
309  *  source and will we overwritten by the target position if snapping has occurred.
310  *  This makes snapping transparent to the calling code. If this is not desired
311  *  because either the calling code must know whether snapping has occurred, or
312  *  because the original position should not be touched, then constrainedSnap() should
313  *  be called instead.
314  *
315  *  PS:
316  *  1) SnapManager::setup() must have been called before calling this method,
317  *  but only once for a set of points
318  *  2) Only to be used when a single source point is to be snapped; it assumes
319  *  that source_num = 0, which is inefficient when snapping sets our source points
321  *
322  *  \param p Current position of the snap source; will be overwritten by the position of the snap target if snapping has occurred
323  *  \param source_type Detailed description of the source type, will be used by the snap indicator
324  *  \param constraint The direction or line along which snapping must occur
325  *  \param bbox_to_snap Bounding box hulling the set of points, all from the same selection and having the same transformation
326  */
328 void SnapManager::constrainedSnapReturnByRef(Geom::Point &p,
329                                              Inkscape::SnapSourceType const source_type,
330                                              Inkscape::Snapper::ConstraintLine const &constraint,
331                                              Geom::OptRect const &bbox_to_snap) const
333     Inkscape::SnappedPoint const s = constrainedSnap(Inkscape::SnapCandidatePoint(p, source_type, 0), constraint, bbox_to_snap);
334     s.getPoint(p);
337 /**
338  *  \brief Try to snap a point along a constraint line to grids, guides or objects.
339  *
340  *  Try to snap a point to grids, guides or objects, in only one degree-of-freedom,
341  *  i.e. snap in a specific direction on the two dimensional canvas to the nearest
342  *  snap target. constrainedSnap is equal in snapping behavior to
343  *  constrainedSnapReturnByRef(). Please read the comments of the latter for more details.
344  *
345  *  PS: SnapManager::setup() must have been called before calling this method,
346  *  but only once for a set of points
347  *
348  *  \param p Source point to be snapped
349  *  \param constraint The direction or line along which snapping must occur
350  *  \param bbox_to_snap Bounding box hulling the set of points, all from the same selection and having the same transformation
351  */
353 Inkscape::SnappedPoint SnapManager::constrainedSnap(Inkscape::SnapCandidatePoint const &p,
354                                                     Inkscape::Snapper::ConstraintLine const &constraint,
355                                                     Geom::OptRect const &bbox_to_snap) const
357     if (!someSnapperMightSnap()) {
358         return Inkscape::SnappedPoint(p, Inkscape::SNAPTARGET_UNDEFINED, NR_HUGE, 0, false, false);
359     }
361     // First project the mouse pointer onto the constraint
362     Geom::Point pp = constraint.projection(p.getPoint());
363     // Then try to snap the projected point
364     Inkscape::SnapCandidatePoint candidate(pp, p.getSourceType(), p.getSourceNum(), Inkscape::SNAPTARGET_UNDEFINED, Geom::Rect());
366     SnappedConstraints sc;
367     SnapperList const snappers = getSnappers();
368     for (SnapperList::const_iterator i = snappers.begin(); i != snappers.end(); i++) {
369         (*i)->constrainedSnap(sc, candidate, bbox_to_snap, constraint, &_items_to_ignore);
370     }
372     return findBestSnap(candidate, sc, true);
375 /**
376  *  \brief Try to snap a point of a guide to another guide or to a node
377  *
378  *  Try to snap a point of a guide to another guide or to a node in two degrees-
379  *  of-freedom, i.e. snap in any direction on the two dimensional canvas to the
380  *  nearest snap target. This method is used when dragging or rotating a guide
381  *
382  *  PS: SnapManager::setup() must have been called before calling this method,
383  *
384  *  \param p Current position of the point on the guide that is to be snapped; will be overwritten by the position of the snap target if snapping has occurred
385  *  \param guide_normal Vector normal to the guide line
386  */
387 void SnapManager::guideFreeSnap(Geom::Point &p, Geom::Point const &guide_normal, SPGuideDragType drag_type) const
389     if (!snapprefs.getSnapEnabledGlobally() || snapprefs.getSnapPostponedGlobally()) {
390         return;
391     }
393     if (!(object.GuidesMightSnap() || snapprefs.getSnapToGuides())) {
394         return;
395     }
397     Inkscape::SnapCandidatePoint candidate(p, Inkscape::SNAPSOURCE_GUIDE_ORIGIN);
398     if (drag_type == SP_DRAG_ROTATE) {
399         candidate = Inkscape::SnapCandidatePoint(p, Inkscape::SNAPSOURCE_GUIDE);
400     }
402     // Snap to nodes
403     SnappedConstraints sc;
404     if (object.GuidesMightSnap()) {
405         object.guideFreeSnap(sc, p, guide_normal);
406     }
408     // Snap to guides & grid lines
409     SnapperList snappers = getGridSnappers();
410     snappers.push_back(&guide);
411     for (SnapperList::const_iterator i = snappers.begin(); i != snappers.end(); i++) {
412         (*i)->freeSnap(sc, candidate, Geom::OptRect(), NULL, NULL);
413     }
415     // Snap to intersections of curves, but not to the curves themselves! (see _snapTranslatingGuideToNodes in object-snapper.cpp)
416     Inkscape::SnappedPoint const s = findBestSnap(candidate, sc, false, true);
418     s.getPoint(p);
421 /**
422  *  \brief Try to snap a point on a guide to the intersection with another guide or a path
423  *
424  *  Try to snap a point on a guide to the intersection of that guide with another
425  *  guide or with a path. The snapped point will lie somewhere on the guide-line,
426  *  making this is a constrained snap, i.e. in only one degree-of-freedom.
427  *  This method is used when dragging the origin of the guide along the guide itself.
428  *
429  *  PS: SnapManager::setup() must have been called before calling this method,
430  *
431  *  \param p Current position of the point on the guide that is to be snapped; will be overwritten by the position of the snap target if snapping has occurred
432  *  \param guide_normal Vector normal to the guide line
433  */
435 void SnapManager::guideConstrainedSnap(Geom::Point &p, SPGuide const &guideline) const
437     if (!snapprefs.getSnapEnabledGlobally() || snapprefs.getSnapPostponedGlobally()) {
438         return;
439     }
441     if (!(object.ThisSnapperMightSnap() || snapprefs.getSnapToGuides())) {
442         return;
443     }
445     Inkscape::SnapCandidatePoint candidate(p, Inkscape::SNAPSOURCE_GUIDE_ORIGIN, Inkscape::SNAPTARGET_UNDEFINED);
447     // Snap to nodes or paths
448     SnappedConstraints sc;
449     Inkscape::Snapper::ConstraintLine cl(guideline.point_on_line, Geom::rot90(guideline.normal_to_line));
450     if (object.ThisSnapperMightSnap()) {
451         object.constrainedSnap(sc, candidate, Geom::OptRect(), cl, NULL);
452     }
454     // Snap to guides & grid lines
455     SnapperList snappers = getGridSnappers();
456     snappers.push_back(&guide);
457     for (SnapperList::const_iterator i = snappers.begin(); i != snappers.end(); i++) {
458         (*i)->constrainedSnap(sc, candidate, Geom::OptRect(), cl, NULL);
459     }
461     Inkscape::SnappedPoint const s = findBestSnap(candidate, sc, false);
462     s.getPoint(p);
465 /**
466  *  \brief Method for snapping sets of points while they are being transformed
467  *
468  *  Method for snapping sets of points while they are being transformed, when using
469  *  for example the selector tool. This method is for internal use only, and should
470  *  not have to be called directly. Use freeSnapTransalation(), constrainedSnapScale(),
471  *  etc. instead.
472  *
473  *  This is what is being done in this method: transform each point, find out whether
474  *  a free snap or constrained snap is more appropriate, do the snapping, calculate
475  *  some metrics to quantify the snap "distance", and see if it's better than the
476  *  previous snap. Finally, the best ("nearest") snap from all these points is returned.
477  *
478  *  \param points Collection of points to snap (snap sources), at their untransformed position, all points undergoing the same transformation. Paired with an identifier of the type of the snap source.
479  *  \param pointer Location of the mouse pointer at the time dragging started (i.e. when the selection was still untransformed).
480  *  \param constrained true if the snap is constrained, e.g. for stretching or for purely horizontal translation.
481  *  \param constraint The direction or line along which snapping must occur, if 'constrained' is true; otherwise undefined.
482  *  \param transformation_type Type of transformation to apply to points before trying to snap them.
483  *  \param transformation Description of the transformation; details depend on the type.
484  *  \param origin Origin of the transformation, if applicable.
485  *  \param dim Dimension to which the transformation applies, if applicable.
486  *  \param uniform true if the transformation should be uniform; only applicable for stretching and scaling.
487  *  \return An instance of the SnappedPoint class, which holds data on the snap source, snap target, and various metrics.
488  */
490 Inkscape::SnappedPoint SnapManager::_snapTransformed(
491     std::vector<Inkscape::SnapCandidatePoint> const &points,
492     Geom::Point const &pointer,
493     bool constrained,
494     Inkscape::Snapper::ConstraintLine const &constraint,
495     Transformation transformation_type,
496     Geom::Point const &transformation,
497     Geom::Point const &origin,
498     Geom::Dim2 dim,
499     bool uniform) const
501     /* We have a list of points, which we are proposing to transform in some way.  We need to see
502     ** if any of these points, when transformed, snap to anything.  If they do, we return the
503     ** appropriate transformation with `true'; otherwise we return the original scale with `false'.
504     */
506     /* Quick check to see if we have any snappers that are enabled
507     ** Also used to globally disable all snapping
508     */
509     if (someSnapperMightSnap() == false) {
510         return Inkscape::SnappedPoint(pointer);
511     }
513     std::vector<Inkscape::SnapCandidatePoint> transformed_points;
514     Geom::Rect bbox;
516     long source_num = 0;
517     for (std::vector<Inkscape::SnapCandidatePoint>::const_iterator i = points.begin(); i != points.end(); i++) {
519         /* Work out the transformed version of this point */
520         Geom::Point transformed = _transformPoint(*i, transformation_type, transformation, origin, dim, uniform);
522         // add the current transformed point to the box hulling all transformed points
523         if (i == points.begin()) {
524             bbox = Geom::Rect(transformed, transformed);
525         } else {
526             bbox.expandTo(transformed);
527         }
529         transformed_points.push_back(Inkscape::SnapCandidatePoint(transformed, (*i).getSourceType(), source_num));
530         source_num++;
531     }
533     /* The current best transformation */
534     Geom::Point best_transformation = transformation;
536     /* The current best metric for the best transformation; lower is better, NR_HUGE
537     ** means that we haven't snapped anything.
538     */
539     Geom::Point best_scale_metric(NR_HUGE, NR_HUGE);
540     Inkscape::SnappedPoint best_snapped_point;
541     g_assert(best_snapped_point.getAlwaysSnap() == false); // Check initialization of snapped point
542     g_assert(best_snapped_point.getAtIntersection() == false);
544     std::vector<Inkscape::SnapCandidatePoint>::const_iterator j = transformed_points.begin();
547     // std::cout << std::endl;
548     for (std::vector<Inkscape::SnapCandidatePoint>::const_iterator i = points.begin(); i != points.end(); i++) {
550         /* Snap it */
551         Inkscape::SnappedPoint snapped_point;
552         Inkscape::Snapper::ConstraintLine dedicated_constraint = constraint;
553         Geom::Point const b = ((*i).getPoint() - origin); // vector to original point
555         if (constrained) {
556             if ((transformation_type == SCALE || transformation_type == STRETCH) && uniform) {
557                 // When uniformly scaling, each point will have its own unique constraint line,
558                 // running from the scaling origin to the original untransformed point. We will
559                 // calculate that line here
560                 dedicated_constraint = Inkscape::Snapper::ConstraintLine(origin, b);
561             } else if (transformation_type == STRETCH) { // when non-uniform stretching {
562                 dedicated_constraint = Inkscape::Snapper::ConstraintLine((*i).getPoint(), component_vectors[dim]);
563             } else if (transformation_type == TRANSLATION) {
564                 // When doing a constrained translation, all points will move in the same direction, i.e.
565                 // either horizontally or vertically. The lines along which they move are therefore all
566                 // parallel, but might not be colinear. Therefore we will have to set the point through
567                 // which the constraint-line runs here, for each point individually.
568                 dedicated_constraint.setPoint((*i).getPoint());
569             } // else: leave the original constraint, e.g. for skewing
570             if (transformation_type == SCALE && !uniform) {
571                 g_warning("Non-uniform constrained scaling is not supported!");
572             }
573             snapped_point = constrainedSnap(*j, dedicated_constraint, bbox);
574         } else {
575             bool const c1 = fabs(b[Geom::X]) < 1e-6;
576             bool const c2 = fabs(b[Geom::Y]) < 1e-6;
577             if (transformation_type == SCALE && (c1 || c2) && !(c1 && c2)) {
578                 // When scaling, a point aligned either horizontally or vertically with the origin can only
579                 // move in that specific direction; therefore it should only snap in that direction, otherwise
580                 // we will get snapped points with an invalid transformation
581                 dedicated_constraint = Inkscape::Snapper::ConstraintLine(origin, component_vectors[c1]);
582                 snapped_point = constrainedSnap(*j, dedicated_constraint, bbox);
583             } else {
584                 snapped_point = freeSnap(*j, bbox);
585             }
586         }
587         // std::cout << "dist = " << snapped_point.getSnapDistance() << std::endl;
588         snapped_point.setPointerDistance(Geom::L2(pointer - (*i).getPoint()));
590         Geom::Point result;
592         if (snapped_point.getSnapped()) {
593             /* We snapped.  Find the transformation that describes where the snapped point has
594             ** ended up, and also the metric for this transformation.
595             */
596             Geom::Point const a = (snapped_point.getPoint() - origin); // vector to snapped point
597             //Geom::Point const b = (*i - origin); // vector to original point
599             switch (transformation_type) {
600                 case TRANSLATION:
601                     result = snapped_point.getPoint() - (*i).getPoint();
602                     /* Consider the case in which a box is almost aligned with a grid in both
603                      * horizontal and vertical directions. The distance to the intersection of
604                      * the grid lines will always be larger then the distance to a single grid
605                      * line. If we prefer snapping to an intersection instead of to a single
606                      * grid line, then we cannot use "metric = Geom::L2(result)". Therefore the
607                      * snapped distance will be used as a metric. Please note that the snapped
608                      * distance is defined as the distance to the nearest line of the intersection,
609                      * and not to the intersection itself!
610                      */
611                     // Only for translations, the relevant metric will be the real snapped distance,
612                     // so we don't have to do anything special here
613                     break;
614                 case SCALE:
615                 {
616                     result = Geom::Point(NR_HUGE, NR_HUGE);
617                     // If this point *i is horizontally or vertically aligned with
618                     // the origin of the scaling, then it will scale purely in X or Y
619                     // We can therefore only calculate the scaling in this direction
620                     // and the scaling factor for the other direction should remain
621                     // untouched (unless scaling is uniform ofcourse)
622                     for (int index = 0; index < 2; index++) {
623                         if (fabs(b[index]) > 1e-6) { // if SCALING CAN occur in this direction
624                             if (fabs(fabs(a[index]/b[index]) - fabs(transformation[index])) > 1e-12) { // if SNAPPING DID occur in this direction
625                                 result[index] = a[index] / b[index]; // then calculate it!
626                             }
627                             // we might leave result[1-index] = NR_HUGE
628                             // if scaling didn't occur in the other direction
629                         }
630                     }
631                     if (uniform) {
632                         if (fabs(result[0]) < fabs(result[1])) {
633                             result[1] = result[0];
634                         } else {
635                             result[0] = result[1];
636                         }
637                     }
638                     // Compare the resulting scaling with the desired scaling
639                     Geom::Point scale_metric = Geom::abs(result - transformation); // One or both of its components might be NR_HUGE
640                     snapped_point.setSnapDistance(std::min(scale_metric[0], scale_metric[1]));
641                     snapped_point.setSecondSnapDistance(std::max(scale_metric[0], scale_metric[1]));
642                     break;
643                 }
644                 case STRETCH:
645                     result = Geom::Point(NR_HUGE, NR_HUGE);
646                     if (fabs(b[dim]) > 1e-6) { // if STRETCHING will occur for this point
647                         result[dim] = a[dim] / b[dim];
648                         result[1-dim] = uniform ? result[dim] : 1;
649                     } else { // STRETCHING might occur for this point, but only when the stretching is uniform
650                         if (uniform && fabs(b[1-dim]) > 1e-6) {
651                            result[1-dim] = a[1-dim] / b[1-dim];
652                            result[dim] = result[1-dim];
653                         }
654                     }
655                     // Store the metric for this transformation as a virtual distance
656                     snapped_point.setSnapDistance(std::abs(result[dim] - transformation[dim]));
657                     snapped_point.setSecondSnapDistance(NR_HUGE);
658                     break;
659                 case SKEW:
660                     result[0] = (snapped_point.getPoint()[dim] - ((*i).getPoint())[dim]) / (((*i).getPoint())[1 - dim] - origin[1 - dim]); // skew factor
661                     result[1] = transformation[1]; // scale factor
662                     // Store the metric for this transformation as a virtual distance
663                     snapped_point.setSnapDistance(std::abs(result[0] - transformation[0]));
664                     snapped_point.setSecondSnapDistance(NR_HUGE);
665                     break;
666                 default:
667                     g_assert_not_reached();
668             }
670             // When scaling, we're considering the best transformation in each direction separately. We will have a metric in each
671             // direction, whereas for all other transformation we only a single one-dimensional metric. That's why we need to handle
672             // the scaling metric differently
673             if (best_snapped_point.isOtherSnapBetter(snapped_point, true)) {
674                 best_transformation = result;
675                 best_snapped_point = snapped_point;
676             }
677         }
679         j++;
680     }
682     Geom::Coord best_metric;
683     if (transformation_type == SCALE) {
684         // When scaling, don't ever exit with one of scaling components set to NR_HUGE
685         for (int index = 0; index < 2; index++) {
686             if (best_transformation[index] == NR_HUGE) {
687                 if (uniform && best_transformation[1-index] < NR_HUGE) {
688                     best_transformation[index] = best_transformation[1-index];
689                 } else {
690                     best_transformation[index] = transformation[index];
691                 }
692             }
693         }
694     }
696     best_metric = best_snapped_point.getSnapDistance();
697     best_snapped_point.setTransformation(best_transformation);
698     // Using " < 1e6" instead of " < NR_HUGE" for catching some rounding errors
699     // These rounding errors might be caused by NRRects, see bug #1584301
700     best_snapped_point.setSnapDistance(best_metric < 1e6 ? best_metric : NR_HUGE);
701     return best_snapped_point;
705 /**
706  *  \brief Apply a translation to a set of points and try to snap freely in 2 degrees-of-freedom
707  *
708  *  \param p Collection of points to snap (snap sources), at their untransformed position, all points undergoing the same transformation. Paired with an identifier of the type of the snap source.
709  *  \param pointer Location of the mouse pointer at the time dragging started (i.e. when the selection was still untransformed).
710  *  \param tr Proposed translation; the final translation can only be calculated after snapping has occurred
711  *  \return An instance of the SnappedPoint class, which holds data on the snap source, snap target, and various metrics.
712  */
714 Inkscape::SnappedPoint SnapManager::freeSnapTranslation(std::vector<Inkscape::SnapCandidatePoint> const &p,
715                                                         Geom::Point const &pointer,
716                                                         Geom::Point const &tr) const
718     if (p.size() == 1) {
719         Geom::Point pt = _transformPoint(p.at(0), TRANSLATION, tr, Geom::Point(0,0), Geom::X, false);
720         _displaySnapsource(Inkscape::SnapCandidatePoint(pt, p.at(0).getSourceType()));
721     }
723     return _snapTransformed(p, pointer, false, Geom::Point(0,0), TRANSLATION, tr, Geom::Point(0,0), Geom::X, false);
726 /**
727  *  \brief Apply a translation to a set of points and try to snap along a constraint
728  *
729  *  \param point_type Category of points to which the source point belongs: node or bounding box.
730  *  \param p Collection of points to snap (snap sources), at their untransformed position, all points undergoing the same transformation. Paired with an identifier of the type of the snap source.
731  *  \param pointer Location of the mouse pointer at the time dragging started (i.e. when the selection was still untransformed).
732  *  \param constraint The direction or line along which snapping must occur.
733  *  \param tr Proposed translation; the final translation can only be calculated after snapping has occurred.
734  *  \return An instance of the SnappedPoint class, which holds data on the snap source, snap target, and various metrics.
735  */
737 Inkscape::SnappedPoint SnapManager::constrainedSnapTranslation(std::vector<Inkscape::SnapCandidatePoint> const &p,
738                                                                Geom::Point const &pointer,
739                                                                Inkscape::Snapper::ConstraintLine const &constraint,
740                                                                Geom::Point const &tr) const
742     if (p.size() == 1) {
743         Geom::Point pt = _transformPoint(p.at(0), TRANSLATION, tr, Geom::Point(0,0), Geom::X, false);
744         _displaySnapsource(Inkscape::SnapCandidatePoint(pt, p.at(0).getSourceType()));
745     }
747     return _snapTransformed(p, pointer, true, constraint, TRANSLATION, tr, Geom::Point(0,0), Geom::X, false);
751 /**
752  *  \brief Apply a scaling to a set of points and try to snap freely in 2 degrees-of-freedom
753  *
754  *  \param point_type Category of points to which the source point belongs: node or bounding box.
755  *  \param p Collection of points to snap (snap sources), at their untransformed position, all points undergoing the same transformation. Paired with an identifier of the type of the snap source.
756  *  \param pointer Location of the mouse pointer at the time dragging started (i.e. when the selection was still untransformed).
757  *  \param s Proposed scaling; the final scaling can only be calculated after snapping has occurred
758  *  \param o Origin of the scaling
759  *  \return An instance of the SnappedPoint class, which holds data on the snap source, snap target, and various metrics.
760  */
762 Inkscape::SnappedPoint SnapManager::freeSnapScale(std::vector<Inkscape::SnapCandidatePoint> const &p,
763                                                   Geom::Point const &pointer,
764                                                   Geom::Scale const &s,
765                                                   Geom::Point const &o) const
767     if (p.size() == 1) {
768         Geom::Point pt = _transformPoint(p.at(0), SCALE, Geom::Point(s[Geom::X], s[Geom::Y]), o, Geom::X, false);
769         _displaySnapsource(Inkscape::SnapCandidatePoint(pt, p.at(0).getSourceType()));
770     }
772     return _snapTransformed(p, pointer, false, Geom::Point(0,0), SCALE, Geom::Point(s[Geom::X], s[Geom::Y]), o, Geom::X, false);
776 /**
777  *  \brief Apply a scaling to a set of points and snap such that the aspect ratio of the selection is preserved
778  *
779  *  \param point_type Category of points to which the source point belongs: node or bounding box.
780  *  \param p Collection of points to snap (snap sources), at their untransformed position, all points undergoing the same transformation. Paired with an identifier of the type of the snap source.
781  *  \param pointer Location of the mouse pointer at the time dragging started (i.e. when the selection was still untransformed).
782  *  \param s Proposed scaling; the final scaling can only be calculated after snapping has occurred
783  *  \param o Origin of the scaling
784  *  \return An instance of the SnappedPoint class, which holds data on the snap source, snap target, and various metrics.
785  */
787 Inkscape::SnappedPoint SnapManager::constrainedSnapScale(std::vector<Inkscape::SnapCandidatePoint> const &p,
788                                                          Geom::Point const &pointer,
789                                                          Geom::Scale const &s,
790                                                          Geom::Point const &o) const
792     // When constrained scaling, only uniform scaling is supported.
793     if (p.size() == 1) {
794         Geom::Point pt = _transformPoint(p.at(0), SCALE, Geom::Point(s[Geom::X], s[Geom::Y]), o, Geom::X, true);
795         _displaySnapsource(Inkscape::SnapCandidatePoint(pt, p.at(0).getSourceType()));
796     }
798     return _snapTransformed(p, pointer, true, Geom::Point(0,0), SCALE, Geom::Point(s[Geom::X], s[Geom::Y]), o, Geom::X, true);
801 /**
802  *  \brief Apply a stretch to a set of points and snap such that the direction of the stretch is preserved
803  *
804  *  \param point_type Category of points to which the source point belongs: node or bounding box.
805  *  \param p Collection of points to snap (snap sources), at their untransformed position, all points undergoing the same transformation. Paired with an identifier of the type of the snap source.
806  *  \param pointer Location of the mouse pointer at the time dragging started (i.e. when the selection was still untransformed).
807  *  \param s Proposed stretch; the final stretch can only be calculated after snapping has occurred
808  *  \param o Origin of the stretching
809  *  \param d Dimension in which to apply proposed stretch.
810  *  \param u true if the stretch should be uniform (i.e. to be applied equally in both dimensions)
811  *  \return An instance of the SnappedPoint class, which holds data on the snap source, snap target, and various metrics.
812  */
814 Inkscape::SnappedPoint SnapManager::constrainedSnapStretch(std::vector<Inkscape::SnapCandidatePoint> const &p,
815                                                             Geom::Point const &pointer,
816                                                             Geom::Coord const &s,
817                                                             Geom::Point const &o,
818                                                             Geom::Dim2 d,
819                                                             bool u) const
821     if (p.size() == 1) {
822         Geom::Point pt = _transformPoint(p.at(0), STRETCH, Geom::Point(s, s), o, d, u);
823         _displaySnapsource(Inkscape::SnapCandidatePoint(pt, p.at(0).getSourceType()));
824     }
826     return _snapTransformed(p, pointer, true, Geom::Point(0,0), STRETCH, Geom::Point(s, s), o, d, u);
829 /**
830  *  \brief Apply a skew to a set of points and snap such that the direction of the skew is preserved
831  *
832  *  \param point_type Category of points to which the source point belongs: node or bounding box.
833  *  \param p Collection of points to snap (snap sources), at their untransformed position, all points undergoing the same transformation. Paired with an identifier of the type of the snap source.
834  *  \param pointer Location of the mouse pointer at the time dragging started (i.e. when the selection was still untransformed).
835  *  \param constraint The direction or line along which snapping must occur.
836  *  \param s Proposed skew; the final skew can only be calculated after snapping has occurred
837  *  \param o Origin of the proposed skew
838  *  \param d Dimension in which to apply proposed skew.
839  *  \return An instance of the SnappedPoint class, which holds data on the snap source, snap target, and various metrics.
840  */
842 Inkscape::SnappedPoint SnapManager::constrainedSnapSkew(std::vector<Inkscape::SnapCandidatePoint> const &p,
843                                                  Geom::Point const &pointer,
844                                                  Inkscape::Snapper::ConstraintLine const &constraint,
845                                                  Geom::Point const &s,
846                                                  Geom::Point const &o,
847                                                  Geom::Dim2 d) const
849     // "s" contains skew factor in s[0], and scale factor in s[1]
851     // Snapping the nodes of the bounding box of a selection that is being transformed, will only work if
852     // the transformation of the bounding box is equal to the transformation of the individual nodes. This is
853     // NOT the case for example when rotating or skewing. The bounding box itself cannot possibly rotate or skew,
854     // so it's corners have a different transformation. The snappers cannot handle this, therefore snapping
855     // of bounding boxes is not allowed here.
856     if (p.size() > 0) {
857         g_assert(!(p.at(0).getSourceType() & Inkscape::SNAPSOURCE_BBOX_CATEGORY));
858     }
860     if (p.size() == 1) {
861         Geom::Point pt = _transformPoint(p.at(0), SKEW, s, o, d, false);
862         _displaySnapsource(Inkscape::SnapCandidatePoint(pt, p.at(0).getSourceType()));
863     }
865     return _snapTransformed(p, pointer, true, constraint, SKEW, s, o, d, false);
868 /**
869  * \brief Given a set of possible snap targets, find the best target (which is not necessarily
870  * also the nearest target), and show the snap indicator if requested
871  *
872  * \param p Source point to be snapped
873  * \param sc A structure holding all snap targets that have been found so far
874  * \param constrained True if the snap is constrained, e.g. for stretching or for purely horizontal translation.
875  * \param noCurves If true, then do consider snapping to intersections of curves, but not to the curves themselves
876  * \return An instance of the SnappedPoint class, which holds data on the snap source, snap target, and various metrics
877  */
879 Inkscape::SnappedPoint SnapManager::findBestSnap(Inkscape::SnapCandidatePoint const &p,
880                                                  SnappedConstraints const &sc,
881                                                  bool constrained,
882                                                  bool noCurves) const
885     /*
886     std::cout << "Type and number of snapped constraints: " << std::endl;
887     std::cout << "  Points      : " << sc.points.size() << std::endl;
888     std::cout << "  Lines       : " << sc.lines.size() << std::endl;
889     std::cout << "  Grid lines  : " << sc.grid_lines.size()<< std::endl;
890     std::cout << "  Guide lines : " << sc.guide_lines.size()<< std::endl;
891     std::cout << "  Curves      : " << sc.curves.size()<< std::endl;
892     */
894     // Store all snappoints
895     std::list<Inkscape::SnappedPoint> sp_list;
897     // search for the closest snapped point
898     Inkscape::SnappedPoint closestPoint;
899     if (getClosestSP(sc.points, closestPoint)) {
900         sp_list.push_back(closestPoint);
901     }
903     // search for the closest snapped curve
904     if (!noCurves) {
905         Inkscape::SnappedCurve closestCurve;
906         if (getClosestCurve(sc.curves, closestCurve)) {
907             sp_list.push_back(Inkscape::SnappedPoint(closestCurve));
908         }
909     }
911     if (snapprefs.getSnapIntersectionCS()) {
912         // search for the closest snapped intersection of curves
913         Inkscape::SnappedPoint closestCurvesIntersection;
914         if (getClosestIntersectionCS(sc.curves, p.getPoint(), closestCurvesIntersection, _desktop->dt2doc())) {
915             closestCurvesIntersection.setSource(p.getSourceType());
916             sp_list.push_back(closestCurvesIntersection);
917         }
918     }
920     // search for the closest snapped grid line
921     Inkscape::SnappedLine closestGridLine;
922     if (getClosestSL(sc.grid_lines, closestGridLine)) {
923         sp_list.push_back(Inkscape::SnappedPoint(closestGridLine));
924     }
926     // search for the closest snapped guide line
927     Inkscape::SnappedLine closestGuideLine;
928     if (getClosestSL(sc.guide_lines, closestGuideLine)) {
929         sp_list.push_back(Inkscape::SnappedPoint(closestGuideLine));
930     }
932     // When freely snapping to a grid/guide/path, only one degree of freedom is eliminated
933     // Therefore we will try get fully constrained by finding an intersection with another grid/guide/path
935     // When doing a constrained snap however, we're already at an intersection of the constrained line and
936     // the grid/guide/path we're snapping to. This snappoint is therefore fully constrained, so there's
937     // no need to look for additional intersections
938     if (!constrained) {
939         // search for the closest snapped intersection of grid lines
940         Inkscape::SnappedPoint closestGridPoint;
941         if (getClosestIntersectionSL(sc.grid_lines, closestGridPoint)) {
942             closestGridPoint.setSource(p.getSourceType());
943             closestGridPoint.setTarget(Inkscape::SNAPTARGET_GRID_INTERSECTION);
944             sp_list.push_back(closestGridPoint);
945         }
947         // search for the closest snapped intersection of guide lines
948         Inkscape::SnappedPoint closestGuidePoint;
949         if (getClosestIntersectionSL(sc.guide_lines, closestGuidePoint)) {
950             closestGuidePoint.setSource(p.getSourceType());
951             closestGuidePoint.setTarget(Inkscape::SNAPTARGET_GUIDE_INTERSECTION);
952             sp_list.push_back(closestGuidePoint);
953         }
955         // search for the closest snapped intersection of grid with guide lines
956         if (snapprefs.getSnapIntersectionGG()) {
957             Inkscape::SnappedPoint closestGridGuidePoint;
958             if (getClosestIntersectionSL(sc.grid_lines, sc.guide_lines, closestGridGuidePoint)) {
959                 closestGridGuidePoint.setSource(p.getSourceType());
960                 closestGridGuidePoint.setTarget(Inkscape::SNAPTARGET_GRID_GUIDE_INTERSECTION);
961                 sp_list.push_back(closestGridGuidePoint);
962             }
963         }
964     }
966     // now let's see which snapped point gets a thumbs up
967     Inkscape::SnappedPoint bestSnappedPoint(p.getPoint());
968     // std::cout << "Finding the best snap..." << std::endl;
969     for (std::list<Inkscape::SnappedPoint>::const_iterator i = sp_list.begin(); i != sp_list.end(); i++) {
970         // first find out if this snapped point is within snapping range
971         // std::cout << "sp = " << (*i).getPoint() << " | source = " << (*i).getSource() << " | target = " << (*i).getTarget();
972         if ((*i).getSnapDistance() <= (*i).getTolerance()) {
973             // if it's the first point, or if it is closer than the best snapped point so far
974             if (i == sp_list.begin() || bestSnappedPoint.isOtherSnapBetter(*i, false)) {
975                 // then prefer this point over the previous one
976                 bestSnappedPoint = *i;
977             }
978         }
979         // std::cout << std::endl;
980     }
982     // Update the snap indicator, if requested
983     if (_snapindicator) {
984         if (bestSnappedPoint.getSnapped()) {
985             _desktop->snapindicator->set_new_snaptarget(bestSnappedPoint);
986         } else {
987             _desktop->snapindicator->remove_snaptarget();
988         }
989     }
991     // std::cout << "findBestSnap = " << bestSnappedPoint.getPoint() << " | dist = " << bestSnappedPoint.getSnapDistance() << std::endl;
992     return bestSnappedPoint;
995 /// Convenience shortcut when there is only one item to ignore
996 void SnapManager::setup(SPDesktop const *desktop,
997                         bool snapindicator,
998                         SPItem const *item_to_ignore,
999                         std::vector<Inkscape::SnapCandidatePoint> *unselected_nodes,
1000                         SPGuide *guide_to_ignore)
1002     g_assert(desktop != NULL);
1003     _items_to_ignore.clear();
1004     _items_to_ignore.push_back(item_to_ignore);
1005     _desktop = desktop;
1006     _snapindicator = snapindicator;
1007     _unselected_nodes = unselected_nodes;
1008     _guide_to_ignore = guide_to_ignore;
1011 /**
1012  * \brief Prepare the snap manager for the actual snapping, which includes building a list of snap targets
1013  * to ignore and toggling the snap indicator
1014  *
1015  * There are two overloaded setup() methods, of which the other one only allows for a single item to be ignored
1016  * whereas this one will take a list of items to ignore
1017  *
1018  * \param desktop Reference to the desktop to which this snap manager is attached
1019  * \param snapindicator If true then a snap indicator will be displayed automatically (when enabled in the preferences)
1020  * \param items_to_ignore These items will not be snapped to, e.g. the items that are currently being dragged. This avoids "self-snapping"
1021  * \param unselected_nodes Stationary nodes of the path that is currently being edited in the node tool and
1022  * that can be snapped too. Nodes not in this list will not be snapped to, to avoid "self-snapping". Of each
1023  * unselected node both the position (Geom::Point) and the type (Inkscape::SnapTargetType) will be stored
1024  * \param guide_to_ignore Guide that is currently being dragged and should not be snapped to
1025  */
1027 void SnapManager::setup(SPDesktop const *desktop,
1028                         bool snapindicator,
1029                         std::vector<SPItem const *> &items_to_ignore,
1030                         std::vector<Inkscape::SnapCandidatePoint> *unselected_nodes,
1031                         SPGuide *guide_to_ignore)
1033     g_assert(desktop != NULL);
1034     _items_to_ignore = items_to_ignore;
1035     _desktop = desktop;
1036     _snapindicator = snapindicator;
1037     _unselected_nodes = unselected_nodes;
1038     _guide_to_ignore = guide_to_ignore;
1041 /// Setup, taking the list of items to ignore from the desktop's selection.
1042 void SnapManager::setupIgnoreSelection(SPDesktop const *desktop,
1043                                       bool snapindicator,
1044                                       std::vector<Inkscape::SnapCandidatePoint> *unselected_nodes,
1045                                       SPGuide *guide_to_ignore)
1047     _desktop = desktop;
1048     _snapindicator = snapindicator;
1049     _unselected_nodes = unselected_nodes;
1050     _guide_to_ignore = guide_to_ignore;
1051     _items_to_ignore.clear();
1053     Inkscape::Selection *sel = _desktop->selection;
1054     GSList const *items = sel->itemList();
1055     for (GSList *i = const_cast<GSList*>(items); i; i = i->next) {
1056         _items_to_ignore.push_back(static_cast<SPItem const *>(i->data));
1057     }
1060 SPDocument *SnapManager::getDocument() const
1062     return _named_view->document;
1065 /**
1066  * \brief Takes an untransformed point, applies the given transformation, and returns the transformed point. Eliminates lots of duplicated code
1067  *
1068  * \param p The untransformed position of the point, paired with an identifier of the type of the snap source.
1069  * \param transformation_type Type of transformation to apply.
1070  * \param transformation Mathematical description of the transformation; details depend on the type.
1071  * \param origin Origin of the transformation, if applicable.
1072  * \param dim Dimension to which the transformation applies, if applicable.
1073  * \param uniform true if the transformation should be uniform; only applicable for stretching and scaling.
1074  * \return The position of the point after transformation
1075  */
1077 Geom::Point SnapManager::_transformPoint(Inkscape::SnapCandidatePoint const &p,
1078                                         Transformation const transformation_type,
1079                                         Geom::Point const &transformation,
1080                                         Geom::Point const &origin,
1081                                         Geom::Dim2 const dim,
1082                                         bool const uniform) const
1084     /* Work out the transformed version of this point */
1085     Geom::Point transformed;
1086     switch (transformation_type) {
1087         case TRANSLATION:
1088             transformed = p.getPoint() + transformation;
1089             break;
1090         case SCALE:
1091             transformed = (p.getPoint() - origin) * Geom::Scale(transformation[Geom::X], transformation[Geom::Y]) + origin;
1092             break;
1093         case STRETCH:
1094         {
1095             Geom::Scale s(1, 1);
1096             if (uniform)
1097                 s[Geom::X] = s[Geom::Y] = transformation[dim];
1098             else {
1099                 s[dim] = transformation[dim];
1100                 s[1 - dim] = 1;
1101             }
1102             transformed = ((p.getPoint() - origin) * s) + origin;
1103             break;
1104         }
1105         case SKEW:
1106             // Apply the skew factor
1107             transformed[dim] = (p.getPoint())[dim] + transformation[0] * ((p.getPoint())[1 - dim] - origin[1 - dim]);
1108             // While skewing, mirroring and scaling (by integer multiples) in the opposite direction is also allowed.
1109             // Apply that scale factor here
1110             transformed[1-dim] = (p.getPoint() - origin)[1 - dim] * transformation[1] + origin[1 - dim];
1111             break;
1112         default:
1113             g_assert_not_reached();
1114     }
1116     return transformed;
1119 /**
1120  * \brief Mark the location of the snap source (not the snap target!) on the canvas by drawing a symbol
1121  *
1122  * \param point_type Category of points to which the source point belongs: node, guide or bounding box
1123  * \param p The transformed position of the source point, paired with an identifier of the type of the snap source.
1124  */
1126 void SnapManager::_displaySnapsource(Inkscape::SnapCandidatePoint const &p) const {
1128     Inkscape::Preferences *prefs = Inkscape::Preferences::get();
1129     if (prefs->getBool("/options/snapclosestonly/value")) {
1130         bool p_is_a_node = p.getSourceType() & Inkscape::SNAPSOURCE_NODE_CATEGORY;
1131         bool p_is_a_bbox = p.getSourceType() & Inkscape::SNAPSOURCE_BBOX_CATEGORY;
1133         if (snapprefs.getSnapEnabledGlobally() && ((p_is_a_node && snapprefs.getSnapModeNode()) || (p_is_a_bbox && snapprefs.getSnapModeBBox()))) {
1134             _desktop->snapindicator->set_new_snapsource(p);
1135         } else {
1136             _desktop->snapindicator->remove_snapsource();
1137         }
1138     }
1141 /*
1142   Local Variables:
1143   mode:c++
1144   c-file-style:"stroustrup"
1145   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1146   indent-tabs-mode:nil
1147   fill-column:99
1148   End:
1149 */
1150 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :