Code

* Implement node snapping.
[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-2009 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: SnapManager::setup() must have been called before calling this method,
163  *  but only once for a set of points
164  *
165  *  \param point_type Category of points to which the source point belongs: node, guide or bounding box
166  *  \param p Current position of the snap source; will be overwritten by the position of the snap target if snapping has occurred
167  *  \param source_type Detailed description of the source type, will be used by the snap indicator
168  *  \param first_point If true then this point is the first one from a set of points, all from the same selection and having the same transformation
169  *  \param bbox_to_snap Bounding box hulling the set of points, all from the same selection and having the same transformation
170  */
172 void SnapManager::freeSnapReturnByRef(Inkscape::SnapPreferences::PointType point_type,
173                                       Geom::Point &p,
174                                       Inkscape::SnapSourceType const source_type,
175                                       bool first_point,
176                                       Geom::OptRect const &bbox_to_snap) const
178     //TODO: PointType and source_type are somewhat redundant; can't we get rid of the point_type parameter?
179     Inkscape::SnappedPoint const s = freeSnap(point_type, p, source_type, first_point, bbox_to_snap);
180     s.getPoint(p);
184 /**
185  *  \brief Try to snap a point to grids, guides or objects.
186  *
187  *  Try to snap a point to grids, guides or objects, in two degrees-of-freedom,
188  *  i.e. snap in any direction on the two dimensional canvas to the nearest
189  *  snap target. freeSnap() is equal in snapping behavior to
190  *  freeSnapReturnByRef(). Please read the comments of the latter for more details
191  *
192  *  PS: SnapManager::setup() must have been called before calling this method,
193  *  but only once for a set of points
194  *
195  *  \param point_type Category of points to which the source point belongs: node, guide or bounding box
196  *  \param p Current position of the snap source
197  *  \param source_type Detailed description of the source type, will be used by the snap indicator
198  *  \param first_point If true then this point is the first one from a set of points, all from the same selection and having the same transformation
199  *  \param bbox_to_snap Bounding box hulling the set of points, all from the same selection and having the same transformation
200  *  \return An instance of the SnappedPoint class, which holds data on the snap source, snap target, and various metrics
201  */
204 Inkscape::SnappedPoint SnapManager::freeSnap(Inkscape::SnapPreferences::PointType point_type,
205                                              Geom::Point const &p,
206                                              Inkscape::SnapSourceType const &source_type,
207                                              bool first_point,
208                                              Geom::OptRect const &bbox_to_snap) const
210     if (!someSnapperMightSnap()) {
211         return Inkscape::SnappedPoint(p, source_type, Inkscape::SNAPTARGET_UNDEFINED, NR_HUGE, 0, false, false);
212     }
214     SnappedConstraints sc;
215     SnapperList const snappers = getSnappers();
217     for (SnapperList::const_iterator i = snappers.begin(); i != snappers.end(); i++) {
218         (*i)->freeSnap(sc, point_type, p, source_type, first_point, bbox_to_snap, &_items_to_ignore, _unselected_nodes);
219     }
221     return findBestSnap(p, source_type, sc, false);
224 /**
225  * \brief Snap to the closest multiple of a grid pitch
226  *
227  * When pasting, we would like to snap to the grid. Problem is that we don't know which
228  * nodes were aligned to the grid at the time of copying, so we don't know which nodes
229  * to snap. If we'd snap an unaligned node to the grid, previously aligned nodes would
230  * become unaligned. That's undesirable. Instead we will make sure that the offset
231  * between the source and its pasted copy is a multiple of the grid pitch. If the source
232  * was aligned, then the copy will therefore also be aligned.
233  *
234  * PS: Whether we really find a multiple also depends on the snapping range! Most users
235  * will have "always snap" enabled though, in which case a multiple will always be found.
236  * PS2: When multiple grids are present then the result will become ambiguous. There is no
237  * way to control to which grid this method will snap.
238  *
239  * \param t Vector that represents the offset of the pasted copy with respect to the original
240  * \return Offset vector after snapping to the closest multiple of a grid pitch
241  */
243 Geom::Point SnapManager::multipleOfGridPitch(Geom::Point const &t) const
245     if (!snapprefs.getSnapEnabledGlobally()) // No need to check for snapprefs.getSnapPostponedGlobally() here
246         return t;
248     if (_desktop && _desktop->gridsEnabled()) {
249         bool success = false;
250         Geom::Point nearest_multiple;
251         Geom::Coord nearest_distance = NR_HUGE;
253         // It will snap to the grid for which we find the closest snap. This might be a different
254         // grid than to which the objects were initially aligned. I don't see an easy way to fix
255         // this, so when using multiple grids one can get unexpected results
257         // Cannot use getGridSnappers() because we need both the grids AND their snappers
258         // Therefore we iterate through all grids manually
259         for (GSList const *l = _named_view->grids; l != NULL; l = l->next) {
260             Inkscape::CanvasGrid *grid = (Inkscape::CanvasGrid*) l->data;
261             const Inkscape::Snapper* snapper = grid->snapper;
262             if (snapper && snapper->ThisSnapperMightSnap()) {
263                 // To find the nearest multiple of the grid pitch for a given translation t, we
264                 // will use the grid snapper. Simply snapping the value t to the grid will do, but
265                 // only if the origin of the grid is at (0,0). If it's not then compensate for this
266                 // in the translation t
267                 Geom::Point const t_offset = t + grid->origin;
268                 SnappedConstraints sc;
269                 // Only the first three parameters are being used for grid snappers
270                 snapper->freeSnap(sc, Inkscape::SnapPreferences::SNAPPOINT_NODE, t_offset, Inkscape::SNAPSOURCE_UNDEFINED, TRUE, Geom::OptRect(), NULL, NULL);
271                 // Find the best snap for this grid, including intersections of the grid-lines
272                 Inkscape::SnappedPoint s = findBestSnap(t_offset, Inkscape::SNAPSOURCE_UNDEFINED, sc, false);
273                 if (s.getSnapped() && (s.getSnapDistance() < nearest_distance)) {
274                     // use getSnapDistance() instead of getWeightedDistance() here because the pointer's position
275                     // doesn't tell us anything about which node to snap
276                     success = true;
277                     nearest_multiple = s.getPoint() - to_2geom(grid->origin);
278                     nearest_distance = s.getSnapDistance();
279                 }
280             }
281         }
283         if (success)
284             return nearest_multiple;
285     }
287     return t;
290 /**
291  *  \brief Try to snap a point along a constraint line to grids, guides or objects.
292  *
293  *  Try to snap a point to grids, guides or objects, in only one degree-of-freedom,
294  *  i.e. snap in a specific direction on the two dimensional canvas to the nearest
295  *  snap target.
296  *
297  *  constrainedSnapReturnByRef() is equal in snapping behavior to
298  *  constrainedSnap(), but the former returns the snapped point trough the referenced
299  *  parameter p. This parameter p initially contains the position of the snap
300  *  source and will we overwritten by the target position if snapping has occurred.
301  *  This makes snapping transparent to the calling code. If this is not desired
302  *  because either the calling code must know whether snapping has occurred, or
303  *  because the original position should not be touched, then constrainedSnap() should
304  *  be called instead.
305  *
306  *  PS: SnapManager::setup() must have been called before calling this method,
307  *  but only once for a set of points
308  *
309  *  \param point_type Category of points to which the source point belongs: node, guide or bounding box
310  *  \param p Current position of the snap source; will be overwritten by the position of the snap target if snapping has occurred
311  *  \param source_type Detailed description of the source type, will be used by the snap indicator
312  *  \param constraint The direction or line along which snapping must occur
313  *  \param first_point If true then this point is the first one from a set of points, all from the same selection and having the same transformation
314  *  \param bbox_to_snap Bounding box hulling the set of points, all from the same selection and having the same transformation
315  */
317 void SnapManager::constrainedSnapReturnByRef(Inkscape::SnapPreferences::PointType point_type,
318                                              Geom::Point &p,
319                                              Inkscape::SnapSourceType const source_type,
320                                              Inkscape::Snapper::ConstraintLine const &constraint,
321                                              bool first_point,
322                                              Geom::OptRect const &bbox_to_snap) const
324     Inkscape::SnappedPoint const s = constrainedSnap(point_type, p, source_type, constraint, first_point, bbox_to_snap);
325     s.getPoint(p);
328 /**
329  *  \brief Try to snap a point along a constraint line to grids, guides or objects.
330  *
331  *  Try to snap a point to grids, guides or objects, in only one degree-of-freedom,
332  *  i.e. snap in a specific direction on the two dimensional canvas to the nearest
333  *  snap target. constrainedSnap is equal in snapping behavior to
334  *  constrainedSnapReturnByRef(). Please read the comments of the latter for more details.
335  *
336  *  PS: SnapManager::setup() must have been called before calling this method,
337  *  but only once for a set of points
338  *
339  *  \param point_type Category of points to which the source point belongs: node, guide or bounding box
340  *  \param p Current position of the snap source
341  *  \param source_type Detailed description of the source type, will be used by the snap indicator
342  *  \param constraint The direction or line along which snapping must occur
343  *  \param first_point If true then this point is the first one from a set of points, all from the same selection and having the same transformation
344  *  \param bbox_to_snap Bounding box hulling the set of points, all from the same selection and having the same transformation
345  */
347 Inkscape::SnappedPoint SnapManager::constrainedSnap(Inkscape::SnapPreferences::PointType point_type,
348                                                     Geom::Point const &p,
349                                                     Inkscape::SnapSourceType const &source_type,
350                                                     Inkscape::Snapper::ConstraintLine const &constraint,
351                                                     bool first_point,
352                                                     Geom::OptRect const &bbox_to_snap) const
354     if (!someSnapperMightSnap()) {
355         return Inkscape::SnappedPoint(p, source_type, Inkscape::SNAPTARGET_UNDEFINED, NR_HUGE, 0, false, false);
356     }
358     // First project the mouse pointer onto the constraint
359     Geom::Point pp = constraint.projection(p);
360     // Then try to snap the projected point
362     SnappedConstraints sc;
363     SnapperList const snappers = getSnappers();
364     for (SnapperList::const_iterator i = snappers.begin(); i != snappers.end(); i++) {
365         (*i)->constrainedSnap(sc, point_type, pp, source_type, first_point, bbox_to_snap, constraint, &_items_to_ignore);
366     }
368     return findBestSnap(pp, source_type, sc, true);
371 /**
372  *  \brief Try to snap a point of a guide to another guide or to a node
373  *
374  *  Try to snap a point of a guide to another guide or to a node in two degrees-
375  *  of-freedom, i.e. snap in any direction on the two dimensional canvas to the
376  *  nearest snap target. This method is used when dragging or rotating a guide
377  *
378  *  PS: SnapManager::setup() must have been called before calling this method,
379  *
380  *  \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
381  *  \param guide_normal Vector normal to the guide line
382  */
383 void SnapManager::guideFreeSnap(Geom::Point &p, Geom::Point const &guide_normal, SPGuideDragType drag_type) const
385     if (!snapprefs.getSnapEnabledGlobally() || snapprefs.getSnapPostponedGlobally()) {
386         return;
387     }
389     if (!(object.GuidesMightSnap() || snapprefs.getSnapToGuides())) {
390         return;
391     }
393     Inkscape::SnapSourceType source_type = Inkscape::SNAPSOURCE_GUIDE_ORIGIN;
394     if (drag_type == SP_DRAG_ROTATE) {
395         source_type = Inkscape::SNAPSOURCE_GUIDE;
396     }
398     // Snap to nodes
399     SnappedConstraints sc;
400     if (object.GuidesMightSnap()) {
401         object.guideFreeSnap(sc, p, guide_normal);
402     }
404     // Snap to guides & grid lines
405     SnapperList snappers = getGridSnappers();
406     snappers.push_back(&guide);
407         for (SnapperList::const_iterator i = snappers.begin(); i != snappers.end(); i++) {
408                 (*i)->freeSnap(sc, Inkscape::SnapPreferences::SNAPPOINT_GUIDE, p, source_type, true, Geom::OptRect(), NULL, NULL);
409         }
411     // Snap to intersections of curves, but not to the curves themselves! (see _snapTranslatingGuideToNodes in object-snapper.cpp)
412     Inkscape::SnappedPoint const s = findBestSnap(p, source_type, sc, false, true);
414     s.getPoint(p);
417 /**
418  *  \brief Try to snap a point on a guide to the intersection with another guide or a path
419  *
420  *  Try to snap a point on a guide to the intersection of that guide with another
421  *  guide or with a path. The snapped point will lie somewhere on the guide-line,
422  *  making this is a constrained snap, i.e. in only one degree-of-freedom.
423  *  This method is used when dragging the origin of the guide along the guide itself.
424  *
425  *  PS: SnapManager::setup() must have been called before calling this method,
426  *
427  *  \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
428  *  \param guide_normal Vector normal to the guide line
429  */
431 void SnapManager::guideConstrainedSnap(Geom::Point &p, SPGuide const &guideline) const
433         if (!snapprefs.getSnapEnabledGlobally() || snapprefs.getSnapPostponedGlobally()) {
434         return;
435     }
437     if (!(object.ThisSnapperMightSnap() || snapprefs.getSnapToGuides())) {
438         return;
439     }
441     Inkscape::SnapSourceType source_type = Inkscape::SNAPSOURCE_GUIDE_ORIGIN;
443     // Snap to nodes or paths
444     SnappedConstraints sc;
445     Inkscape::Snapper::ConstraintLine cl(guideline.point_on_line, Geom::rot90(guideline.normal_to_line));
446     if (object.ThisSnapperMightSnap()) {
447         object.constrainedSnap(sc, Inkscape::SnapPreferences::SNAPPOINT_GUIDE, p, source_type, true, Geom::OptRect(), cl, NULL);
448     }
450     // Snap to guides & grid lines
451         SnapperList snappers = getGridSnappers();
452         snappers.push_back(&guide);
453         for (SnapperList::const_iterator i = snappers.begin(); i != snappers.end(); i++) {
454                 (*i)->constrainedSnap(sc, Inkscape::SnapPreferences::SNAPPOINT_GUIDE, p, source_type, true, Geom::OptRect(), cl, NULL);
455         }
457     Inkscape::SnappedPoint const s = findBestSnap(p, source_type, sc, false);
458     s.getPoint(p);
461 /**
462  *  \brief Method for snapping sets of points while they are being transformed
463  *
464  *  Method for snapping sets of points while they are being transformed, when using
465  *  for example the selector tool. This method is for internal use only, and should
466  *  not have to be called directly. Use freeSnapTransalation(), constrainedSnapScale(),
467  *  etc. instead.
468  *
469  *  This is what is being done in this method: transform each point, find out whether
470  *  a free snap or constrained snap is more appropriate, do the snapping, calculate
471  *  some metrics to quantify the snap "distance", and see if it's better than the
472  *  previous snap. Finally, the best ("nearest") snap from all these points is returned.
473  *
474  *  \param type Category of points to which the source point belongs: node or bounding box.
475  *  \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.
476  *  \param pointer Location of the mouse pointer at the time dragging started (i.e. when the selection was still untransformed).
477  *  \param constrained true if the snap is constrained, e.g. for stretching or for purely horizontal translation.
478  *  \param constraint The direction or line along which snapping must occur, if 'constrained' is true; otherwise undefined.
479  *  \param transformation_type Type of transformation to apply to points before trying to snap them.
480  *  \param transformation Description of the transformation; details depend on the type.
481  *  \param origin Origin of the transformation, if applicable.
482  *  \param dim Dimension to which the transformation applies, if applicable.
483  *  \param uniform true if the transformation should be uniform; only applicable for stretching and scaling.
484  *  \return An instance of the SnappedPoint class, which holds data on the snap source, snap target, and various metrics.
485  */
487 Inkscape::SnappedPoint SnapManager::_snapTransformed(
488     Inkscape::SnapPreferences::PointType type,
489     std::vector<std::pair<Geom::Point, int> > const &points,
490     Geom::Point const &pointer,
491     bool constrained,
492     Inkscape::Snapper::ConstraintLine const &constraint,
493     Transformation transformation_type,
494     Geom::Point const &transformation,
495     Geom::Point const &origin,
496     Geom::Dim2 dim,
497     bool uniform) const
499     /* We have a list of points, which we are proposing to transform in some way.  We need to see
500     ** if any of these points, when transformed, snap to anything.  If they do, we return the
501     ** appropriate transformation with `true'; otherwise we return the original scale with `false'.
502     */
504     /* Quick check to see if we have any snappers that are enabled
505     ** Also used to globally disable all snapping
506     */
507     if (someSnapperMightSnap() == false) {
508         return Inkscape::SnappedPoint();
509     }
511     std::vector<std::pair<Geom::Point, int> > transformed_points;
512     Geom::Rect bbox;
514     for (std::vector<std::pair<Geom::Point, int> >::const_iterator i = points.begin(); i != points.end(); i++) {
516         /* Work out the transformed version of this point */
517         Geom::Point transformed = _transformPoint(*i, transformation_type, transformation, origin, dim, uniform);
519         // add the current transformed point to the box hulling all transformed points
520         if (i == points.begin()) {
521             bbox = Geom::Rect(transformed, transformed);
522         } else {
523             bbox.expandTo(transformed);
524         }
526         transformed_points.push_back(std::make_pair(transformed, (*i).second));
527     }
529     /* The current best transformation */
530     Geom::Point best_transformation = transformation;
532     /* The current best metric for the best transformation; lower is better, NR_HUGE
533     ** means that we haven't snapped anything.
534     */
535     Geom::Point best_scale_metric(NR_HUGE, NR_HUGE);
536     Inkscape::SnappedPoint best_snapped_point;
537     g_assert(best_snapped_point.getAlwaysSnap() == false); // Check initialization of snapped point
538     g_assert(best_snapped_point.getAtIntersection() == false);
540     std::vector<std::pair<Geom::Point, int> >::const_iterator j = transformed_points.begin();
542     // std::cout << std::endl;
543     for (std::vector<std::pair<Geom::Point, int> >::const_iterator i = points.begin(); i != points.end(); i++) {
545         /* Snap it */
546         Inkscape::SnappedPoint snapped_point;
547         Inkscape::Snapper::ConstraintLine dedicated_constraint = constraint;
548         Geom::Point const b = ((*i).first - origin); // vector to original point
550         if (constrained) {
551             if ((transformation_type == SCALE || transformation_type == STRETCH) && uniform) {
552                 // When uniformly scaling, each point will have its own unique constraint line,
553                 // running from the scaling origin to the original untransformed point. We will
554                 // calculate that line here
555                 dedicated_constraint = Inkscape::Snapper::ConstraintLine(origin, b);
556             } else if (transformation_type == STRETCH) { // when non-uniform stretching {
557                 dedicated_constraint = Inkscape::Snapper::ConstraintLine((*i).first, component_vectors[dim]);
558             } else if (transformation_type == TRANSLATION) {
559                 // When doing a constrained translation, all points will move in the same direction, i.e.
560                 // either horizontally or vertically. The lines along which they move are therefore all
561                 // parallel, but might not be colinear. Therefore we will have to set the point through
562                 // which the constraint-line runs here, for each point individually.
563                 dedicated_constraint.setPoint((*i).first);
564             } // else: leave the original constraint, e.g. for skewing
565             if (transformation_type == SCALE && !uniform) {
566                 g_warning("Non-uniform constrained scaling is not supported!");
567             }
568             snapped_point = constrainedSnap(type, (*j).first, static_cast<Inkscape::SnapSourceType>((*j).second), dedicated_constraint, i == points.begin(), bbox);
569         } else {
570             bool const c1 = fabs(b[Geom::X]) < 1e-6;
571             bool const c2 = fabs(b[Geom::Y]) < 1e-6;
572             if (transformation_type == SCALE && (c1 || c2) && !(c1 && c2)) {
573                 // When scaling, a point aligned either horizontally or vertically with the origin can only
574                 // move in that specific direction; therefore it should only snap in that direction, otherwise
575                 // we will get snapped points with an invalid transformation
576                 dedicated_constraint = Inkscape::Snapper::ConstraintLine(origin, component_vectors[c1]);
577                 snapped_point = constrainedSnap(type, (*j).first, static_cast<Inkscape::SnapSourceType>((*j).second), dedicated_constraint, i == points.begin(), bbox);
578             } else {
579                 snapped_point = freeSnap(type, (*j).first, static_cast<Inkscape::SnapSourceType>((*j).second), i == points.begin(), bbox);
580             }
581         }
582         // std::cout << "dist = " << snapped_point.getSnapDistance() << std::endl;
583         snapped_point.setPointerDistance(Geom::L2(pointer - (*i).first));
585         Geom::Point result;
586         Geom::Point scale_metric(NR_HUGE, NR_HUGE);
588         if (snapped_point.getSnapped()) {
589             /* We snapped.  Find the transformation that describes where the snapped point has
590             ** ended up, and also the metric for this transformation.
591             */
592             Geom::Point const a = (snapped_point.getPoint() - origin); // vector to snapped point
593             //Geom::Point const b = (*i - origin); // vector to original point
595             switch (transformation_type) {
596                 case TRANSLATION:
597                     result = snapped_point.getPoint() - (*i).first;
598                     /* Consider the case in which a box is almost aligned with a grid in both
599                      * horizontal and vertical directions. The distance to the intersection of
600                      * the grid lines will always be larger then the distance to a single grid
601                      * line. If we prefer snapping to an intersection instead of to a single
602                      * grid line, then we cannot use "metric = Geom::L2(result)". Therefore the
603                      * snapped distance will be used as a metric. Please note that the snapped
604                      * distance is defined as the distance to the nearest line of the intersection,
605                      * and not to the intersection itself!
606                      */
607                     // Only for translations, the relevant metric will be the real snapped distance,
608                     // so we don't have to do anything special here
609                     break;
610                 case SCALE:
611                 {
612                     result = Geom::Point(NR_HUGE, NR_HUGE);
613                     // If this point *i is horizontally or vertically aligned with
614                     // the origin of the scaling, then it will scale purely in X or Y
615                     // We can therefore only calculate the scaling in this direction
616                     // and the scaling factor for the other direction should remain
617                     // untouched (unless scaling is uniform ofcourse)
618                     for (int index = 0; index < 2; index++) {
619                         if (fabs(b[index]) > 1e-6) { // if SCALING CAN occur in this direction
620                             if (fabs(fabs(a[index]/b[index]) - fabs(transformation[index])) > 1e-12) { // if SNAPPING DID occur in this direction
621                                 result[index] = a[index] / b[index]; // then calculate it!
622                             }
623                             // we might leave result[1-index] = NR_HUGE
624                             // if scaling didn't occur in the other direction
625                         }
626                     }
627                     // Compare the resulting scaling with the desired scaling
628                     scale_metric = result - transformation; // One or both of its components might be NR_HUGE
629                     break;
630                 }
631                 case STRETCH:
632                     result = Geom::Point(NR_HUGE, NR_HUGE);
633                     if (fabs(b[dim]) > 1e-6) { // if STRETCHING will occur for this point
634                         result[dim] = a[dim] / b[dim];
635                         result[1-dim] = uniform ? result[dim] : 1;
636                     } else { // STRETCHING might occur for this point, but only when the stretching is uniform
637                         if (uniform && fabs(b[1-dim]) > 1e-6) {
638                            result[1-dim] = a[1-dim] / b[1-dim];
639                            result[dim] = result[1-dim];
640                         }
641                     }
642                     // Store the metric for this transformation as a virtual distance
643                     snapped_point.setSnapDistance(std::abs(result[dim] - transformation[dim]));
644                     snapped_point.setSecondSnapDistance(NR_HUGE);
645                     break;
646                 case SKEW:
647                     result[0] = (snapped_point.getPoint()[dim] - ((*i).first)[dim]) / (((*i).first)[1 - dim] - origin[1 - dim]); // skew factor
648                     result[1] = transformation[1]; // scale factor
649                     // Store the metric for this transformation as a virtual distance
650                     snapped_point.setSnapDistance(std::abs(result[0] - transformation[0]));
651                     snapped_point.setSecondSnapDistance(NR_HUGE);
652                     break;
653                 default:
654                     g_assert_not_reached();
655             }
657             // When scaling, we're considering the best transformation in each direction separately. We will have a metric in each
658             // direction, whereas for all other transformation we only a single one-dimensional metric. That's why we need to handle
659             // the scaling metric differently
660             if (transformation_type == SCALE) {
661                 for (int index = 0; index < 2; index++) {
662                     if (fabs(scale_metric[index]) < fabs(best_scale_metric[index])) {
663                         best_transformation[index] = result[index];
664                         best_scale_metric[index] = fabs(scale_metric[index]);
665                         // When scaling, we're considering the best transformation in each direction separately
666                         // Therefore two different snapped points might together make a single best transformation
667                         // We will however return only a single snapped point (e.g. to display the snapping indicator)
668                         best_snapped_point = snapped_point;
669                         // std::cout << "SEL ";
670                     } // else { std::cout << "    ";}
671                 }
672                 if (uniform) {
673                     if (best_scale_metric[0] < best_scale_metric[1]) {
674                         best_transformation[1] = best_transformation[0];
675                         best_scale_metric[1] = best_scale_metric[0];
676                     } else {
677                         best_transformation[0] = best_transformation[1];
678                         best_scale_metric[0] = best_scale_metric[1];
679                     }
680                 }
681             } else { // For all transformations other than scaling
682                 if (best_snapped_point.isOtherSnapBetter(snapped_point, true)) {
683                     best_transformation = result;
684                     best_snapped_point = snapped_point;
685                 }
686             }
687         }
689         j++;
690     }
692     Geom::Coord best_metric;
693     if (transformation_type == SCALE) {
694         // When scaling, don't ever exit with one of scaling components set to NR_HUGE
695         for (int index = 0; index < 2; index++) {
696             if (best_transformation[index] == NR_HUGE) {
697                 if (uniform && best_transformation[1-index] < NR_HUGE) {
698                     best_transformation[index] = best_transformation[1-index];
699                 } else {
700                     best_transformation[index] = transformation[index];
701                 }
702             }
703         }
704         best_metric = std::min(best_scale_metric[0], best_scale_metric[1]);
705     } else { // For all transformations other than scaling
706         best_metric = best_snapped_point.getSnapDistance();
707     }
709     best_snapped_point.setTransformation(best_transformation);
710     // Using " < 1e6" instead of " < NR_HUGE" for catching some rounding errors
711     // These rounding errors might be caused by NRRects, see bug #1584301
712     best_snapped_point.setSnapDistance(best_metric < 1e6 ? best_metric : NR_HUGE);
713     return best_snapped_point;
717 /**
718  *  \brief Apply a translation to a set of points and try to snap freely in 2 degrees-of-freedom
719  *
720  *  \param point_type Category of points to which the source point belongs: node or bounding box.
721  *  \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.
722  *  \param pointer Location of the mouse pointer at the time dragging started (i.e. when the selection was still untransformed).
723  *  \param tr Proposed translation; the final translation can only be calculated after snapping has occurred
724  *  \return An instance of the SnappedPoint class, which holds data on the snap source, snap target, and various metrics.
725  */
727 Inkscape::SnappedPoint SnapManager::freeSnapTranslation(Inkscape::SnapPreferences::PointType point_type,
728                                                         std::vector<std::pair<Geom::Point, int> > const &p,
729                                                         Geom::Point const &pointer,
730                                                         Geom::Point const &tr) const
732     if (p.size() == 1) {
733         _displaySnapsource(point_type, std::make_pair(_transformPoint(p.at(0), TRANSLATION, tr, Geom::Point(0,0), Geom::X, false), (p.at(0)).second));
734     }
736     return _snapTransformed(point_type, p, pointer, false, Geom::Point(0,0), TRANSLATION, tr, Geom::Point(0,0), Geom::X, false);
739 /**
740  *  \brief Apply a translation to a set of points and try to snap along a constraint
741  *
742  *  \param point_type Category of points to which the source point belongs: node or bounding box.
743  *  \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.
744  *  \param pointer Location of the mouse pointer at the time dragging started (i.e. when the selection was still untransformed).
745  *  \param constraint The direction or line along which snapping must occur.
746  *  \param tr Proposed translation; the final translation can only be calculated after snapping has occurred.
747  *  \return An instance of the SnappedPoint class, which holds data on the snap source, snap target, and various metrics.
748  */
750 Inkscape::SnappedPoint SnapManager::constrainedSnapTranslation(Inkscape::SnapPreferences::PointType point_type,
751                                                                std::vector<std::pair<Geom::Point, int> > const &p,
752                                                                Geom::Point const &pointer,
753                                                                Inkscape::Snapper::ConstraintLine const &constraint,
754                                                                Geom::Point const &tr) const
756     if (p.size() == 1) {
757         _displaySnapsource(point_type, std::make_pair(_transformPoint(p.at(0), TRANSLATION, tr, Geom::Point(0,0), Geom::X, false), (p.at(0)).second));
758     }
760     return _snapTransformed(point_type, p, pointer, true, constraint, TRANSLATION, tr, Geom::Point(0,0), Geom::X, false);
764 /**
765  *  \brief Apply a scaling to a set of points and try to snap freely in 2 degrees-of-freedom
766  *
767  *  \param point_type Category of points to which the source point belongs: node or bounding box.
768  *  \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.
769  *  \param pointer Location of the mouse pointer at the time dragging started (i.e. when the selection was still untransformed).
770  *  \param s Proposed scaling; the final scaling can only be calculated after snapping has occurred
771  *  \param o Origin of the scaling
772  *  \return An instance of the SnappedPoint class, which holds data on the snap source, snap target, and various metrics.
773  */
775 Inkscape::SnappedPoint SnapManager::freeSnapScale(Inkscape::SnapPreferences::PointType point_type,
776                                                   std::vector<std::pair<Geom::Point, int> > const &p,
777                                                   Geom::Point const &pointer,
778                                                   Geom::Scale const &s,
779                                                   Geom::Point const &o) const
781     if (p.size() == 1) {
782         _displaySnapsource(point_type, std::make_pair(_transformPoint(p.at(0), SCALE, Geom::Point(s[Geom::X], s[Geom::Y]), o, Geom::X, false), (p.at(0)).second));
783     }
785     return _snapTransformed(point_type, p, pointer, false, Geom::Point(0,0), SCALE, Geom::Point(s[Geom::X], s[Geom::Y]), o, Geom::X, false);
789 /**
790  *  \brief Apply a scaling to a set of points and snap such that the aspect ratio of the selection is preserved
791  *
792  *  \param point_type Category of points to which the source point belongs: node or bounding box.
793  *  \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.
794  *  \param pointer Location of the mouse pointer at the time dragging started (i.e. when the selection was still untransformed).
795  *  \param s Proposed scaling; the final scaling can only be calculated after snapping has occurred
796  *  \param o Origin of the scaling
797  *  \return An instance of the SnappedPoint class, which holds data on the snap source, snap target, and various metrics.
798  */
800 Inkscape::SnappedPoint SnapManager::constrainedSnapScale(Inkscape::SnapPreferences::PointType point_type,
801                                                          std::vector<std::pair<Geom::Point, int> > const &p,
802                                                          Geom::Point const &pointer,
803                                                          Geom::Scale const &s,
804                                                          Geom::Point const &o) const
806     // When constrained scaling, only uniform scaling is supported.
807     if (p.size() == 1) {
808         _displaySnapsource(point_type, std::make_pair(_transformPoint(p.at(0), SCALE, Geom::Point(s[Geom::X], s[Geom::Y]), o, Geom::X, true), (p.at(0)).second));
809     }
811     return _snapTransformed(point_type, p, pointer, true, Geom::Point(0,0), SCALE, Geom::Point(s[Geom::X], s[Geom::Y]), o, Geom::X, true);
814 /**
815  *  \brief Apply a stretch to a set of points and snap such that the direction of the stretch is preserved
816  *
817  *  \param point_type Category of points to which the source point belongs: node or bounding box.
818  *  \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.
819  *  \param pointer Location of the mouse pointer at the time dragging started (i.e. when the selection was still untransformed).
820  *  \param s Proposed stretch; the final stretch can only be calculated after snapping has occurred
821  *  \param o Origin of the stretching
822  *  \param d Dimension in which to apply proposed stretch.
823  *  \param u true if the stretch should be uniform (i.e. to be applied equally in both dimensions)
824  *  \return An instance of the SnappedPoint class, which holds data on the snap source, snap target, and various metrics.
825  */
827 Inkscape::SnappedPoint SnapManager::constrainedSnapStretch(Inkscape::SnapPreferences::PointType point_type,
828                                                             std::vector<std::pair<Geom::Point, int> > const &p,
829                                                             Geom::Point const &pointer,
830                                                             Geom::Coord const &s,
831                                                             Geom::Point const &o,
832                                                             Geom::Dim2 d,
833                                                             bool u) const
835     if (p.size() == 1) {
836         _displaySnapsource(point_type, std::make_pair(_transformPoint(p.at(0), STRETCH, Geom::Point(s, s), o, d, u), (p.at(0)).second));
837     }
839     return _snapTransformed(point_type, p, pointer, true, Geom::Point(0,0), STRETCH, Geom::Point(s, s), o, d, u);
842 /**
843  *  \brief Apply a skew to a set of points and snap such that the direction of the skew is preserved
844  *
845  *  \param point_type Category of points to which the source point belongs: node or bounding box.
846  *  \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.
847  *  \param pointer Location of the mouse pointer at the time dragging started (i.e. when the selection was still untransformed).
848  *  \param constraint The direction or line along which snapping must occur.
849  *  \param s Proposed skew; the final skew can only be calculated after snapping has occurred
850  *  \param o Origin of the proposed skew
851  *  \param d Dimension in which to apply proposed skew.
852  *  \return An instance of the SnappedPoint class, which holds data on the snap source, snap target, and various metrics.
853  */
855 Inkscape::SnappedPoint SnapManager::constrainedSnapSkew(Inkscape::SnapPreferences::PointType point_type,
856                                                  std::vector<std::pair<Geom::Point, int> > const &p,
857                                                  Geom::Point const &pointer,
858                                                  Inkscape::Snapper::ConstraintLine const &constraint,
859                                                  Geom::Point const &s,
860                                                  Geom::Point const &o,
861                                                  Geom::Dim2 d) const
863     // "s" contains skew factor in s[0], and scale factor in s[1]
865     // Snapping the nodes of the bounding box of a selection that is being transformed, will only work if
866     // the transformation of the bounding box is equal to the transformation of the individual nodes. This is
867     // NOT the case for example when rotating or skewing. The bounding box itself cannot possibly rotate or skew,
868     // so it's corners have a different transformation. The snappers cannot handle this, therefore snapping
869     // of bounding boxes is not allowed here.
870     g_assert(!(point_type & Inkscape::SnapPreferences::SNAPPOINT_BBOX));
872     if (p.size() == 1) {
873         _displaySnapsource(point_type, std::make_pair(_transformPoint(p.at(0), SKEW, s, o, d, false), (p.at(0)).second));
874     }
876     return _snapTransformed(point_type, p, pointer, true, constraint, SKEW, s, o, d, false);
879 /**
880  * \brief Given a set of possible snap targets, find the best target (which is not necessarily
881  * also the nearest target), and show the snap indicator if requested
882  *
883  * \param p Current position of the snap source
884  * \param source_type Detailed description of the source type, will be used by the snap indicator
885  * \param sc A structure holding all snap targets that have been found so far
886  * \param constrained True if the snap is constrained, e.g. for stretching or for purely horizontal translation.
887  * \param noCurves If true, then do consider snapping to intersections of curves, but not to the curves themself
888  * \return An instance of the SnappedPoint class, which holds data on the snap source, snap target, and various metrics
889  */
891 Inkscape::SnappedPoint SnapManager::findBestSnap(Geom::Point const &p,
892                                                                                              Inkscape::SnapSourceType const source_type,
893                                                                                              SnappedConstraints &sc,
894                                                                                              bool constrained,
895                                                                                              bool noCurves) const
898     /*
899     std::cout << "Type and number of snapped constraints: " << std::endl;
900     std::cout << "  Points      : " << sc.points.size() << std::endl;
901     std::cout << "  Lines       : " << sc.lines.size() << std::endl;
902     std::cout << "  Grid lines  : " << sc.grid_lines.size()<< std::endl;
903     std::cout << "  Guide lines : " << sc.guide_lines.size()<< std::endl;
904     std::cout << "  Curves      : " << sc.curves.size()<< std::endl;
905     */
907     // Store all snappoints
908     std::list<Inkscape::SnappedPoint> sp_list;
910     // search for the closest snapped point
911     Inkscape::SnappedPoint closestPoint;
912     if (getClosestSP(sc.points, closestPoint)) {
913         sp_list.push_back(closestPoint);
914     }
916     // search for the closest snapped curve
917     if (!noCurves) {
918                 Inkscape::SnappedCurve closestCurve;
919                 if (getClosestCurve(sc.curves, closestCurve)) {
920                         sp_list.push_back(Inkscape::SnappedPoint(closestCurve));
921                 }
922     }
924     if (snapprefs.getSnapIntersectionCS()) {
925         // search for the closest snapped intersection of curves
926         Inkscape::SnappedPoint closestCurvesIntersection;
927         if (getClosestIntersectionCS(sc.curves, p, closestCurvesIntersection, _desktop->dt2doc())) {
928             closestCurvesIntersection.setSource(source_type);
929             sp_list.push_back(closestCurvesIntersection);
930         }
931     }
933     // search for the closest snapped grid line
934     Inkscape::SnappedLine closestGridLine;
935     if (getClosestSL(sc.grid_lines, closestGridLine)) {
936         sp_list.push_back(Inkscape::SnappedPoint(closestGridLine));
937     }
939     // search for the closest snapped guide line
940     Inkscape::SnappedLine closestGuideLine;
941     if (getClosestSL(sc.guide_lines, closestGuideLine)) {
942         sp_list.push_back(Inkscape::SnappedPoint(closestGuideLine));
943     }
945     // When freely snapping to a grid/guide/path, only one degree of freedom is eliminated
946     // Therefore we will try get fully constrained by finding an intersection with another grid/guide/path
948     // When doing a constrained snap however, we're already at an intersection of the constrained line and
949     // the grid/guide/path we're snapping to. This snappoint is therefore fully constrained, so there's
950     // no need to look for additional intersections
951     if (!constrained) {
952         // search for the closest snapped intersection of grid lines
953         Inkscape::SnappedPoint closestGridPoint;
954         if (getClosestIntersectionSL(sc.grid_lines, closestGridPoint)) {
955             closestGridPoint.setSource(source_type);
956             closestGridPoint.setTarget(Inkscape::SNAPTARGET_GRID_INTERSECTION);
957             sp_list.push_back(closestGridPoint);
958         }
960         // search for the closest snapped intersection of guide lines
961         Inkscape::SnappedPoint closestGuidePoint;
962         if (getClosestIntersectionSL(sc.guide_lines, closestGuidePoint)) {
963             closestGuidePoint.setSource(source_type);
964             closestGuidePoint.setTarget(Inkscape::SNAPTARGET_GUIDE_INTERSECTION);
965             sp_list.push_back(closestGuidePoint);
966         }
968         // search for the closest snapped intersection of grid with guide lines
969         if (snapprefs.getSnapIntersectionGG()) {
970             Inkscape::SnappedPoint closestGridGuidePoint;
971             if (getClosestIntersectionSL(sc.grid_lines, sc.guide_lines, closestGridGuidePoint)) {
972                 closestGridGuidePoint.setSource(source_type);
973                 closestGridGuidePoint.setTarget(Inkscape::SNAPTARGET_GRID_GUIDE_INTERSECTION);
974                 sp_list.push_back(closestGridGuidePoint);
975             }
976         }
977     }
979     // now let's see which snapped point gets a thumbs up
980     Inkscape::SnappedPoint bestSnappedPoint = Inkscape::SnappedPoint(p, Inkscape::SNAPSOURCE_UNDEFINED, Inkscape::SNAPTARGET_UNDEFINED, NR_HUGE, 0, false, false);
981     // std::cout << "Finding the best snap..." << std::endl;
982     for (std::list<Inkscape::SnappedPoint>::const_iterator i = sp_list.begin(); i != sp_list.end(); i++) {
983         // first find out if this snapped point is within snapping range
984         // std::cout << "sp = " << from_2geom((*i).getPoint());
985         if ((*i).getSnapDistance() <= (*i).getTolerance()) {
986             // if it's the first point, or if it is closer than the best snapped point so far
987             if (i == sp_list.begin() || bestSnappedPoint.isOtherSnapBetter(*i, false)) {
988                 // then prefer this point over the previous one
989                 bestSnappedPoint = *i;
990             }
991         }
992         // std::cout << std::endl;
993     }
995     // Update the snap indicator, if requested
996     if (_snapindicator) {
997         if (bestSnappedPoint.getSnapped()) {
998             _desktop->snapindicator->set_new_snaptarget(bestSnappedPoint);
999         } else {
1000             _desktop->snapindicator->remove_snaptarget();
1001         }
1002     }
1004     // std::cout << "findBestSnap = " << bestSnappedPoint.getPoint() << " | dist = " << bestSnappedPoint.getSnapDistance() << std::endl;
1005     return bestSnappedPoint;
1008 /// Convenience shortcut when there is only one item to ignore
1009 void SnapManager::setup(SPDesktop const *desktop,
1010                         bool snapindicator,
1011                         SPItem const *item_to_ignore,
1012                         std::vector<std::pair<Geom::Point, int> > *unselected_nodes,
1013                         SPGuide *guide_to_ignore)
1015     g_assert(desktop != NULL);
1016     _items_to_ignore.clear();
1017     _items_to_ignore.push_back(item_to_ignore);
1018     _desktop = desktop;
1019     _snapindicator = snapindicator;
1020     _unselected_nodes = unselected_nodes;
1021     _guide_to_ignore = guide_to_ignore;
1024 /**
1025  * \brief Prepare the snap manager for the actual snapping, which includes building a list of snap targets
1026  * to ignore and toggling the snap indicator
1027  *
1028  * There are two overloaded setup() methods, of which the other one only allows for a single item to be ignored
1029  * whereas this one will take a list of items to ignore
1030  *
1031  * \param desktop Reference to the desktop to which this snap manager is attached
1032  * \param snapindicator If true then a snap indicator will be displayed automatically (when enabled in the preferences)
1033  * \param items_to_ignore These items will not be snapped to, e.g. the items that are currently being dragged. This avoids "self-snapping"
1034  * \param unselected_nodes Stationary nodes of the path that is currently being edited in the node tool and
1035  * that can be snapped too. Nodes not in this list will not be snapped to, to avoid "self-snapping". Of each
1036  * unselected node both the position (Geom::Point) and the type (Inkscape::SnapTargetType) will be stored
1037  * \param guide_to_ignore Guide that is currently being dragged and should not be snapped to
1038  */
1040 void SnapManager::setup(SPDesktop const *desktop,
1041                         bool snapindicator,
1042                         std::vector<SPItem const *> const &items_to_ignore,
1043                         std::vector<std::pair<Geom::Point, int> > *unselected_nodes,
1044                         SPGuide *guide_to_ignore)
1046     g_assert(desktop != NULL);
1047     _items_to_ignore = items_to_ignore;
1048     _desktop = desktop;
1049     _snapindicator = snapindicator;
1050     _unselected_nodes = unselected_nodes;
1051     _guide_to_ignore = guide_to_ignore;
1054 /// Setup, taking the list of items to ignore from the desktop's selection.
1055 void SnapManager::setupIgnoreSelection(SPDesktop const *desktop,
1056                                       bool snapindicator,
1057                                       std::vector<std::pair<Geom::Point, int> > *unselected_nodes,
1058                                       SPGuide *guide_to_ignore)
1060     _desktop = desktop;
1061     _snapindicator = snapindicator;
1062     _unselected_nodes = unselected_nodes;
1063     _guide_to_ignore = guide_to_ignore;
1064     _items_to_ignore.clear();
1066     Inkscape::Selection *sel = _desktop->selection;
1067     GSList const *items = sel->itemList();
1068     for (GSList *i = const_cast<GSList*>(items); i; i = i->next) {
1069         _items_to_ignore.push_back(static_cast<SPItem const *>(i->data));
1070     }
1073 SPDocument *SnapManager::getDocument() const
1075     return _named_view->document;
1078 /**
1079  * \brief Takes an untransformed point, applies the given transformation, and returns the transformed point. Eliminates lots of duplicated code
1080  *
1081  * \param p The untransformed position of the point, paired with an identifier of the type of the snap source.
1082  * \param transformation_type Type of transformation to apply.
1083  * \param transformation Mathematical description of the transformation; details depend on the type.
1084  * \param origin Origin of the transformation, if applicable.
1085  * \param dim Dimension to which the transformation applies, if applicable.
1086  * \param uniform true if the transformation should be uniform; only applicable for stretching and scaling.
1087  * \return The position of the point after transformation
1088  */
1090 Geom::Point SnapManager::_transformPoint(std::pair<Geom::Point, int> const &p,
1091                                         Transformation const transformation_type,
1092                                         Geom::Point const &transformation,
1093                                         Geom::Point const &origin,
1094                                         Geom::Dim2 const dim,
1095                                         bool const uniform) const
1097     /* Work out the transformed version of this point */
1098     Geom::Point transformed;
1099     switch (transformation_type) {
1100         case TRANSLATION:
1101             transformed = p.first + transformation;
1102             break;
1103         case SCALE:
1104             transformed = (p.first - origin) * Geom::Scale(transformation[Geom::X], transformation[Geom::Y]) + origin;
1105             break;
1106         case STRETCH:
1107         {
1108             Geom::Scale s(1, 1);
1109             if (uniform)
1110                 s[Geom::X] = s[Geom::Y] = transformation[dim];
1111             else {
1112                 s[dim] = transformation[dim];
1113                 s[1 - dim] = 1;
1114             }
1115             transformed = ((p.first - origin) * s) + origin;
1116             break;
1117         }
1118         case SKEW:
1119             // Apply the skew factor
1120             transformed[dim] = (p.first)[dim] + transformation[0] * ((p.first)[1 - dim] - origin[1 - dim]);
1121             // While skewing, mirroring and scaling (by integer multiples) in the opposite direction is also allowed.
1122             // Apply that scale factor here
1123             transformed[1-dim] = (p.first - origin)[1 - dim] * transformation[1] + origin[1 - dim];
1124             break;
1125         default:
1126             g_assert_not_reached();
1127     }
1129     return transformed;
1132 /**
1133  * \brief Mark the location of the snap source (not the snap target!) on the canvas by drawing a symbol
1134  *
1135  * \param point_type Category of points to which the source point belongs: node, guide or bounding box
1136  * \param p The transformed position of the source point, paired with an identifier of the type of the snap source.
1137  */
1139 void SnapManager::_displaySnapsource(Inkscape::SnapPreferences::PointType point_type, std::pair<Geom::Point, int> const &p) const {
1141     Inkscape::Preferences *prefs = Inkscape::Preferences::get();
1142     if (prefs->getBool("/options/snapclosestonly/value")) {
1143         bool p_is_a_node = point_type & Inkscape::SnapPreferences::SNAPPOINT_NODE;
1144         bool p_is_a_bbox = point_type & Inkscape::SnapPreferences::SNAPPOINT_BBOX;
1145         if (snapprefs.getSnapEnabledGlobally() && ((p_is_a_node && snapprefs.getSnapModeNode()) || (p_is_a_bbox && snapprefs.getSnapModeBBox()))) {
1146             _desktop->snapindicator->set_new_snapsource(p);
1147         } else {
1148             _desktop->snapindicator->remove_snapsource();
1149         }
1150     }
1153 /*
1154   Local Variables:
1155   mode:c++
1156   c-file-style:"stroustrup"
1157   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1158   indent-tabs-mode:nil
1159   fill-column:99
1160   End:
1161 */
1162 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :