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 "sp-guide.h"
34 #include "preferences.h"
35 #include "event-context.h"
36 using std::vector;
38 /**
39 * Construct a SnapManager for a SPNamedView.
40 *
41 * \param v `Owning' SPNamedView.
42 */
44 SnapManager::SnapManager(SPNamedView const *v) :
45 guide(this, 0),
46 object(this, 0),
47 snapprefs(),
48 _named_view(v)
49 {
50 }
52 /**
53 * \brief Return a list of snappers
54 *
55 * Inkscape snaps to objects, grids, and guides. For each of these snap targets a
56 * separate class is used, which has been derived from the base Snapper class. The
57 * getSnappers() method returns a list of pointers to instances of this class. This
58 * list contains exactly one instance of the guide snapper and of the object snapper
59 * class, but any number of grid snappers (because each grid has its own snapper
60 * instance)
61 *
62 * \return List of snappers that we use.
63 */
64 SnapManager::SnapperList
65 SnapManager::getSnappers() const
66 {
67 SnapManager::SnapperList s;
68 s.push_back(&guide);
69 s.push_back(&object);
71 SnapManager::SnapperList gs = getGridSnappers();
72 s.splice(s.begin(), gs);
74 return s;
75 }
77 /**
78 * \brief Return a list of gridsnappers
79 *
80 * Each grid has its own instance of the snapper class. This way snapping can
81 * be enabled per grid individually. A list will be returned containing the
82 * pointers to these instances, but only for grids that are being displayed
83 * and for which snapping is enabled.
84 *
85 * \return List of gridsnappers that we use.
86 */
87 SnapManager::SnapperList
88 SnapManager::getGridSnappers() const
89 {
90 SnapperList s;
92 if (_desktop && _desktop->gridsEnabled() && snapprefs.getSnapToGrids()) {
93 for ( GSList const *l = _named_view->grids; l != NULL; l = l->next) {
94 Inkscape::CanvasGrid *grid = (Inkscape::CanvasGrid*) l->data;
95 s.push_back(grid->snapper);
96 }
97 }
99 return s;
100 }
102 /**
103 * \brief Return true if any snapping might occur, whether its to grids, guides or objects
104 *
105 * Each snapper instance handles its own snapping target, e.g. grids, guides or
106 * objects. This method iterates through all these snapper instances and returns
107 * true if any of the snappers might possible snap, considering only the relevant
108 * snapping preferences.
109 *
110 * \return true if one of the snappers will try to snap to something.
111 */
113 bool SnapManager::someSnapperMightSnap() const
114 {
115 if ( !snapprefs.getSnapEnabledGlobally() || snapprefs.getSnapPostponedGlobally() ) {
116 return false;
117 }
119 SnapperList const s = getSnappers();
120 SnapperList::const_iterator i = s.begin();
121 while (i != s.end() && (*i)->ThisSnapperMightSnap() == false) {
122 i++;
123 }
125 return (i != s.end());
126 }
128 /**
129 * \return true if one of the grids might be snapped to.
130 */
132 bool SnapManager::gridSnapperMightSnap() const
133 {
134 if ( !snapprefs.getSnapEnabledGlobally() || snapprefs.getSnapPostponedGlobally() ) {
135 return false;
136 }
138 SnapperList const s = getGridSnappers();
139 SnapperList::const_iterator i = s.begin();
140 while (i != s.end() && (*i)->ThisSnapperMightSnap() == false) {
141 i++;
142 }
144 return (i != s.end());
145 }
147 /**
148 * \brief Try to snap a point to grids, guides or objects.
149 *
150 * Try to snap a point to grids, guides or objects, in two degrees-of-freedom,
151 * i.e. snap in any direction on the two dimensional canvas to the nearest
152 * snap target. freeSnapReturnByRef() is equal in snapping behavior to
153 * freeSnap(), but the former returns the snapped point trough the referenced
154 * parameter p. This parameter p initially contains the position of the snap
155 * source and will we overwritten by the target position if snapping has occurred.
156 * This makes snapping transparent to the calling code. If this is not desired
157 * because either the calling code must know whether snapping has occurred, or
158 * because the original position should not be touched, then freeSnap() should be
159 * called instead.
160 *
161 * PS:
162 * 1) SnapManager::setup() must have been called before calling this method,
163 * but only once for a set of points
164 * 2) Only to be used when a single source point is to be snapped; it assumes
165 * that source_num = 0, which is inefficient when snapping sets our source points
166 *
167 * \param point_type Category of points to which the source point belongs: node, guide or bounding box
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(Inkscape::SnapPreferences::PointType point_type,
174 Geom::Point &p,
175 Inkscape::SnapSourceType const source_type,
176 Geom::OptRect const &bbox_to_snap) const
177 {
178 //TODO: SnapCandidatePoint and point_type are somewhat redundant; can't we get rid of the point_type parameter?
179 Inkscape::SnappedPoint const s = freeSnap(point_type, Inkscape::SnapCandidatePoint(p, source_type), bbox_to_snap);
180 s.getPoint(p);
181 }
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 Source point to be snapped
197 * \param bbox_to_snap Bounding box hulling the set of points, all from the same selection and having the same transformation
198 * \return An instance of the SnappedPoint class, which holds data on the snap source, snap target, and various metrics
199 */
202 Inkscape::SnappedPoint SnapManager::freeSnap(Inkscape::SnapPreferences::PointType const point_type,
203 Inkscape::SnapCandidatePoint const &p,
204 Geom::OptRect const &bbox_to_snap) const
205 {
206 if (!someSnapperMightSnap()) {
207 return Inkscape::SnappedPoint(p, Inkscape::SNAPTARGET_UNDEFINED, NR_HUGE, 0, false, false);
208 }
210 std::vector<SPItem const *> *items_to_ignore;
211 if (_item_to_ignore) { // If we have only a single item to ignore
212 // then build a list containing this single item;
213 // This single-item list will prevail over any other _items_to_ignore list, should that exist
214 items_to_ignore = new std::vector<SPItem const *>;
215 items_to_ignore->push_back(_item_to_ignore);
216 } else {
217 items_to_ignore = _items_to_ignore;
218 }
220 SnappedConstraints sc;
221 SnapperList const snappers = getSnappers();
223 for (SnapperList::const_iterator i = snappers.begin(); i != snappers.end(); i++) {
224 (*i)->freeSnap(sc, point_type, p, bbox_to_snap, items_to_ignore, _unselected_nodes);
225 }
227 if (_item_to_ignore) {
228 delete items_to_ignore;
229 }
231 return findBestSnap(p, sc, false);
232 }
234 /**
235 * \brief Snap to the closest multiple of a grid pitch
236 *
237 * When pasting, we would like to snap to the grid. Problem is that we don't know which
238 * nodes were aligned to the grid at the time of copying, so we don't know which nodes
239 * to snap. If we'd snap an unaligned node to the grid, previously aligned nodes would
240 * become unaligned. That's undesirable. Instead we will make sure that the offset
241 * between the source and its pasted copy is a multiple of the grid pitch. If the source
242 * was aligned, then the copy will therefore also be aligned.
243 *
244 * PS: Whether we really find a multiple also depends on the snapping range! Most users
245 * will have "always snap" enabled though, in which case a multiple will always be found.
246 * PS2: When multiple grids are present then the result will become ambiguous. There is no
247 * way to control to which grid this method will snap.
248 *
249 * \param t Vector that represents the offset of the pasted copy with respect to the original
250 * \return Offset vector after snapping to the closest multiple of a grid pitch
251 */
253 Geom::Point SnapManager::multipleOfGridPitch(Geom::Point const &t) const
254 {
255 if (!snapprefs.getSnapEnabledGlobally()) // No need to check for snapprefs.getSnapPostponedGlobally() here
256 return t;
258 if (_desktop && _desktop->gridsEnabled()) {
259 bool success = false;
260 Geom::Point nearest_multiple;
261 Geom::Coord nearest_distance = NR_HUGE;
263 // It will snap to the grid for which we find the closest snap. This might be a different
264 // grid than to which the objects were initially aligned. I don't see an easy way to fix
265 // this, so when using multiple grids one can get unexpected results
267 // Cannot use getGridSnappers() because we need both the grids AND their snappers
268 // Therefore we iterate through all grids manually
269 for (GSList const *l = _named_view->grids; l != NULL; l = l->next) {
270 Inkscape::CanvasGrid *grid = (Inkscape::CanvasGrid*) l->data;
271 const Inkscape::Snapper* snapper = grid->snapper;
272 if (snapper && snapper->ThisSnapperMightSnap()) {
273 // To find the nearest multiple of the grid pitch for a given translation t, we
274 // will use the grid snapper. Simply snapping the value t to the grid will do, but
275 // only if the origin of the grid is at (0,0). If it's not then compensate for this
276 // in the translation t
277 Geom::Point const t_offset = t + grid->origin;
278 SnappedConstraints sc;
279 // Only the first three parameters are being used for grid snappers
280 snapper->freeSnap(sc, Inkscape::SnapPreferences::SNAPPOINT_NODE, Inkscape::SnapCandidatePoint(t_offset, Inkscape::SNAPSOURCE_UNDEFINED),Geom::OptRect(), NULL, NULL);
281 // Find the best snap for this grid, including intersections of the grid-lines
282 Inkscape::SnappedPoint s = findBestSnap(Inkscape::SnapCandidatePoint(t_offset, Inkscape::SNAPSOURCE_UNDEFINED), sc, false);
283 if (s.getSnapped() && (s.getSnapDistance() < nearest_distance)) {
284 // use getSnapDistance() instead of getWeightedDistance() here because the pointer's position
285 // doesn't tell us anything about which node to snap
286 success = true;
287 nearest_multiple = s.getPoint() - to_2geom(grid->origin);
288 nearest_distance = s.getSnapDistance();
289 }
290 }
291 }
293 if (success)
294 return nearest_multiple;
295 }
297 return t;
298 }
300 /**
301 * \brief Try to snap a point along a constraint line to grids, guides or objects.
302 *
303 * Try to snap a point to grids, guides or objects, in only one degree-of-freedom,
304 * i.e. snap in a specific direction on the two dimensional canvas to the nearest
305 * snap target.
306 *
307 * constrainedSnapReturnByRef() is equal in snapping behavior to
308 * constrainedSnap(), but the former returns the snapped point trough the referenced
309 * parameter p. This parameter p initially contains the position of the snap
310 * source and will we overwritten by the target position if snapping has occurred.
311 * This makes snapping transparent to the calling code. If this is not desired
312 * because either the calling code must know whether snapping has occurred, or
313 * because the original position should not be touched, then constrainedSnap() should
314 * be called instead.
315 *
316 * PS:
317 * 1) SnapManager::setup() must have been called before calling this method,
318 * but only once for a set of points
319 * 2) Only to be used when a single source point is to be snapped; it assumes
320 * that source_num = 0, which is inefficient when snapping sets our source points
322 *
323 * \param point_type Category of points to which the source point belongs: node, guide or bounding box
324 * \param p Current position of the snap source; will be overwritten by the position of the snap target if snapping has occurred
325 * \param source_type Detailed description of the source type, will be used by the snap indicator
326 * \param constraint The direction or line along which snapping must occur
327 * \param bbox_to_snap Bounding box hulling the set of points, all from the same selection and having the same transformation
328 */
330 void SnapManager::constrainedSnapReturnByRef(Inkscape::SnapPreferences::PointType point_type,
331 Geom::Point &p,
332 Inkscape::SnapSourceType const source_type,
333 Inkscape::Snapper::ConstraintLine const &constraint,
334 Geom::OptRect const &bbox_to_snap) const
335 {
336 Inkscape::SnappedPoint const s = constrainedSnap(point_type, Inkscape::SnapCandidatePoint(p, source_type, 0), constraint, bbox_to_snap);
337 s.getPoint(p);
338 }
340 /**
341 * \brief Try to snap a point along a constraint line to grids, guides or objects.
342 *
343 * Try to snap a point to grids, guides or objects, in only one degree-of-freedom,
344 * i.e. snap in a specific direction on the two dimensional canvas to the nearest
345 * snap target. constrainedSnap is equal in snapping behavior to
346 * constrainedSnapReturnByRef(). Please read the comments of the latter for more details.
347 *
348 * PS: SnapManager::setup() must have been called before calling this method,
349 * but only once for a set of points
350 *
351 * \param point_type Category of points to which the source point belongs: node, guide or bounding box
352 * \param p Source point to be snapped
353 * \param constraint The direction or line along which snapping must occur
354 * \param bbox_to_snap Bounding box hulling the set of points, all from the same selection and having the same transformation
355 */
357 Inkscape::SnappedPoint SnapManager::constrainedSnap(Inkscape::SnapPreferences::PointType const point_type,
358 Inkscape::SnapCandidatePoint const &p,
359 Inkscape::Snapper::ConstraintLine const &constraint,
360 Geom::OptRect const &bbox_to_snap) const
361 {
362 if (!someSnapperMightSnap()) {
363 return Inkscape::SnappedPoint(p, Inkscape::SNAPTARGET_UNDEFINED, NR_HUGE, 0, false, false);
364 }
366 std::vector<SPItem const *> *items_to_ignore;
367 if (_item_to_ignore) { // If we have only a single item to ignore
368 // then build a list containing this single item;
369 // This single-item list will prevail over any other _items_to_ignore list, should that exist
370 items_to_ignore = new std::vector<SPItem const *>;
371 items_to_ignore->push_back(_item_to_ignore);
372 } else {
373 items_to_ignore = _items_to_ignore;
374 }
377 // First project the mouse pointer onto the constraint
378 Geom::Point pp = constraint.projection(p.getPoint());
379 // Then try to snap the projected point
380 Inkscape::SnapCandidatePoint candidate(pp, p.getSourceType(), p.getSourceNum(), Inkscape::SNAPTARGET_UNDEFINED, Geom::Rect());
382 SnappedConstraints sc;
383 SnapperList const snappers = getSnappers();
384 for (SnapperList::const_iterator i = snappers.begin(); i != snappers.end(); i++) {
385 (*i)->constrainedSnap(sc, point_type, candidate, bbox_to_snap, constraint, items_to_ignore);
386 }
388 if (_item_to_ignore) {
389 delete items_to_ignore;
390 }
392 return findBestSnap(candidate, sc, true);
393 }
395 /**
396 * \brief Try to snap a point of a guide to another guide or to a node
397 *
398 * Try to snap a point of a guide to another guide or to a node in two degrees-
399 * of-freedom, i.e. snap in any direction on the two dimensional canvas to the
400 * nearest snap target. This method is used when dragging or rotating a guide
401 *
402 * PS: SnapManager::setup() must have been called before calling this method,
403 *
404 * \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
405 * \param guide_normal Vector normal to the guide line
406 */
407 void SnapManager::guideFreeSnap(Geom::Point &p, Geom::Point const &guide_normal, SPGuideDragType drag_type) const
408 {
409 if (!snapprefs.getSnapEnabledGlobally() || snapprefs.getSnapPostponedGlobally()) {
410 return;
411 }
413 if (!(object.GuidesMightSnap() || snapprefs.getSnapToGuides())) {
414 return;
415 }
417 Inkscape::SnapCandidatePoint candidate(p, Inkscape::SNAPSOURCE_GUIDE_ORIGIN);
418 if (drag_type == SP_DRAG_ROTATE) {
419 candidate = Inkscape::SnapCandidatePoint(p, Inkscape::SNAPSOURCE_GUIDE);
420 }
422 // Snap to nodes
423 SnappedConstraints sc;
424 if (object.GuidesMightSnap()) {
425 object.guideFreeSnap(sc, p, guide_normal);
426 }
428 // Snap to guides & grid lines
429 SnapperList snappers = getGridSnappers();
430 snappers.push_back(&guide);
431 for (SnapperList::const_iterator i = snappers.begin(); i != snappers.end(); i++) {
432 (*i)->freeSnap(sc, Inkscape::SnapPreferences::SNAPPOINT_OTHER, candidate, Geom::OptRect(), NULL, NULL);
433 }
435 // Snap to intersections of curves, but not to the curves themselves! (see _snapTranslatingGuideToNodes in object-snapper.cpp)
436 Inkscape::SnappedPoint const s = findBestSnap(candidate, sc, false, true);
438 s.getPoint(p);
439 }
441 /**
442 * \brief Try to snap a point on a guide to the intersection with another guide or a path
443 *
444 * Try to snap a point on a guide to the intersection of that guide with another
445 * guide or with a path. The snapped point will lie somewhere on the guide-line,
446 * making this is a constrained snap, i.e. in only one degree-of-freedom.
447 * This method is used when dragging the origin of the guide along the guide itself.
448 *
449 * PS: SnapManager::setup() must have been called before calling this method,
450 *
451 * \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
452 * \param guide_normal Vector normal to the guide line
453 */
455 void SnapManager::guideConstrainedSnap(Geom::Point &p, SPGuide const &guideline) const
456 {
457 if (!snapprefs.getSnapEnabledGlobally() || snapprefs.getSnapPostponedGlobally()) {
458 return;
459 }
461 if (!(object.ThisSnapperMightSnap() || snapprefs.getSnapToGuides())) {
462 return;
463 }
465 Inkscape::SnapCandidatePoint candidate(p, Inkscape::SNAPSOURCE_GUIDE_ORIGIN, Inkscape::SNAPTARGET_UNDEFINED);
467 // Snap to nodes or paths
468 SnappedConstraints sc;
469 Inkscape::Snapper::ConstraintLine cl(guideline.point_on_line, Geom::rot90(guideline.normal_to_line));
470 if (object.ThisSnapperMightSnap()) {
471 object.constrainedSnap(sc, Inkscape::SnapPreferences::SNAPPOINT_OTHER, candidate, Geom::OptRect(), cl, NULL);
472 }
474 // Snap to guides & grid lines
475 SnapperList snappers = getGridSnappers();
476 snappers.push_back(&guide);
477 for (SnapperList::const_iterator i = snappers.begin(); i != snappers.end(); i++) {
478 (*i)->constrainedSnap(sc, Inkscape::SnapPreferences::SNAPPOINT_OTHER, candidate, Geom::OptRect(), cl, NULL);
479 }
481 Inkscape::SnappedPoint const s = findBestSnap(candidate, sc, false);
482 s.getPoint(p);
483 }
485 /**
486 * \brief Method for snapping sets of points while they are being transformed
487 *
488 * Method for snapping sets of points while they are being transformed, when using
489 * for example the selector tool. This method is for internal use only, and should
490 * not have to be called directly. Use freeSnapTransalation(), constrainedSnapScale(),
491 * etc. instead.
492 *
493 * This is what is being done in this method: transform each point, find out whether
494 * a free snap or constrained snap is more appropriate, do the snapping, calculate
495 * some metrics to quantify the snap "distance", and see if it's better than the
496 * previous snap. Finally, the best ("nearest") snap from all these points is returned.
497 *
498 * \param type Category of points to which the source point belongs: node or bounding box.
499 * \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.
500 * \param pointer Location of the mouse pointer at the time dragging started (i.e. when the selection was still untransformed).
501 * \param constrained true if the snap is constrained, e.g. for stretching or for purely horizontal translation.
502 * \param constraint The direction or line along which snapping must occur, if 'constrained' is true; otherwise undefined.
503 * \param transformation_type Type of transformation to apply to points before trying to snap them.
504 * \param transformation Description of the transformation; details depend on the type.
505 * \param origin Origin of the transformation, if applicable.
506 * \param dim Dimension to which the transformation applies, if applicable.
507 * \param uniform true if the transformation should be uniform; only applicable for stretching and scaling.
508 * \return An instance of the SnappedPoint class, which holds data on the snap source, snap target, and various metrics.
509 */
511 Inkscape::SnappedPoint SnapManager::_snapTransformed(
512 Inkscape::SnapPreferences::PointType type,
513 std::vector<Inkscape::SnapCandidatePoint> const &points,
514 Geom::Point const &pointer,
515 bool constrained,
516 Inkscape::Snapper::ConstraintLine const &constraint,
517 Transformation transformation_type,
518 Geom::Point const &transformation,
519 Geom::Point const &origin,
520 Geom::Dim2 dim,
521 bool uniform) const
522 {
523 /* We have a list of points, which we are proposing to transform in some way. We need to see
524 ** if any of these points, when transformed, snap to anything. If they do, we return the
525 ** appropriate transformation with `true'; otherwise we return the original scale with `false'.
526 */
528 /* Quick check to see if we have any snappers that are enabled
529 ** Also used to globally disable all snapping
530 */
531 if (someSnapperMightSnap() == false) {
532 return Inkscape::SnappedPoint(pointer);
533 }
535 std::vector<Inkscape::SnapCandidatePoint> transformed_points;
536 Geom::Rect bbox;
538 long source_num = 0;
539 for (std::vector<Inkscape::SnapCandidatePoint>::const_iterator i = points.begin(); i != points.end(); i++) {
541 /* Work out the transformed version of this point */
542 Geom::Point transformed = _transformPoint(*i, transformation_type, transformation, origin, dim, uniform);
544 // add the current transformed point to the box hulling all transformed points
545 if (i == points.begin()) {
546 bbox = Geom::Rect(transformed, transformed);
547 } else {
548 bbox.expandTo(transformed);
549 }
551 transformed_points.push_back(Inkscape::SnapCandidatePoint(transformed, (*i).getSourceType(), source_num));
552 source_num++;
553 }
555 /* The current best transformation */
556 Geom::Point best_transformation = transformation;
558 /* The current best metric for the best transformation; lower is better, NR_HUGE
559 ** means that we haven't snapped anything.
560 */
561 Geom::Point best_scale_metric(NR_HUGE, NR_HUGE);
562 Inkscape::SnappedPoint best_snapped_point;
563 g_assert(best_snapped_point.getAlwaysSnap() == false); // Check initialization of snapped point
564 g_assert(best_snapped_point.getAtIntersection() == false);
566 std::vector<Inkscape::SnapCandidatePoint>::const_iterator j = transformed_points.begin();
569 // std::cout << std::endl;
570 for (std::vector<Inkscape::SnapCandidatePoint>::const_iterator i = points.begin(); i != points.end(); i++) {
572 /* Snap it */
573 Inkscape::SnappedPoint snapped_point;
574 Inkscape::Snapper::ConstraintLine dedicated_constraint = constraint;
575 Geom::Point const b = ((*i).getPoint() - origin); // vector to original point
577 if (constrained) {
578 if ((transformation_type == SCALE || transformation_type == STRETCH) && uniform) {
579 // When uniformly scaling, each point will have its own unique constraint line,
580 // running from the scaling origin to the original untransformed point. We will
581 // calculate that line here
582 dedicated_constraint = Inkscape::Snapper::ConstraintLine(origin, b);
583 } else if (transformation_type == STRETCH) { // when non-uniform stretching {
584 dedicated_constraint = Inkscape::Snapper::ConstraintLine((*i).getPoint(), component_vectors[dim]);
585 } else if (transformation_type == TRANSLATION) {
586 // When doing a constrained translation, all points will move in the same direction, i.e.
587 // either horizontally or vertically. The lines along which they move are therefore all
588 // parallel, but might not be colinear. Therefore we will have to set the point through
589 // which the constraint-line runs here, for each point individually.
590 dedicated_constraint.setPoint((*i).getPoint());
591 } // else: leave the original constraint, e.g. for skewing
592 if (transformation_type == SCALE && !uniform) {
593 g_warning("Non-uniform constrained scaling is not supported!");
594 }
595 snapped_point = constrainedSnap(type, *j, dedicated_constraint, bbox);
596 } else {
597 bool const c1 = fabs(b[Geom::X]) < 1e-6;
598 bool const c2 = fabs(b[Geom::Y]) < 1e-6;
599 if (transformation_type == SCALE && (c1 || c2) && !(c1 && c2)) {
600 // When scaling, a point aligned either horizontally or vertically with the origin can only
601 // move in that specific direction; therefore it should only snap in that direction, otherwise
602 // we will get snapped points with an invalid transformation
603 dedicated_constraint = Inkscape::Snapper::ConstraintLine(origin, component_vectors[c1]);
604 snapped_point = constrainedSnap(type, *j, dedicated_constraint, bbox);
605 } else {
606 snapped_point = freeSnap(type, *j, bbox);
607 }
608 }
609 // std::cout << "dist = " << snapped_point.getSnapDistance() << std::endl;
610 snapped_point.setPointerDistance(Geom::L2(pointer - (*i).getPoint()));
612 Geom::Point result;
613 Geom::Point scale_metric(NR_HUGE, NR_HUGE);
615 if (snapped_point.getSnapped()) {
616 /* We snapped. Find the transformation that describes where the snapped point has
617 ** ended up, and also the metric for this transformation.
618 */
619 Geom::Point const a = (snapped_point.getPoint() - origin); // vector to snapped point
620 //Geom::Point const b = (*i - origin); // vector to original point
622 switch (transformation_type) {
623 case TRANSLATION:
624 result = snapped_point.getPoint() - (*i).getPoint();
625 /* Consider the case in which a box is almost aligned with a grid in both
626 * horizontal and vertical directions. The distance to the intersection of
627 * the grid lines will always be larger then the distance to a single grid
628 * line. If we prefer snapping to an intersection instead of to a single
629 * grid line, then we cannot use "metric = Geom::L2(result)". Therefore the
630 * snapped distance will be used as a metric. Please note that the snapped
631 * distance is defined as the distance to the nearest line of the intersection,
632 * and not to the intersection itself!
633 */
634 // Only for translations, the relevant metric will be the real snapped distance,
635 // so we don't have to do anything special here
636 break;
637 case SCALE:
638 {
639 result = Geom::Point(NR_HUGE, NR_HUGE);
640 // If this point *i is horizontally or vertically aligned with
641 // the origin of the scaling, then it will scale purely in X or Y
642 // We can therefore only calculate the scaling in this direction
643 // and the scaling factor for the other direction should remain
644 // untouched (unless scaling is uniform ofcourse)
645 for (int index = 0; index < 2; index++) {
646 if (fabs(b[index]) > 1e-6) { // if SCALING CAN occur in this direction
647 if (fabs(fabs(a[index]/b[index]) - fabs(transformation[index])) > 1e-12) { // if SNAPPING DID occur in this direction
648 result[index] = a[index] / b[index]; // then calculate it!
649 }
650 // we might leave result[1-index] = NR_HUGE
651 // if scaling didn't occur in the other direction
652 }
653 }
654 // Compare the resulting scaling with the desired scaling
655 scale_metric = result - transformation; // One or both of its components might be NR_HUGE
656 break;
657 }
658 case STRETCH:
659 result = Geom::Point(NR_HUGE, NR_HUGE);
660 if (fabs(b[dim]) > 1e-6) { // if STRETCHING will occur for this point
661 result[dim] = a[dim] / b[dim];
662 result[1-dim] = uniform ? result[dim] : 1;
663 } else { // STRETCHING might occur for this point, but only when the stretching is uniform
664 if (uniform && fabs(b[1-dim]) > 1e-6) {
665 result[1-dim] = a[1-dim] / b[1-dim];
666 result[dim] = result[1-dim];
667 }
668 }
669 // Store the metric for this transformation as a virtual distance
670 snapped_point.setSnapDistance(std::abs(result[dim] - transformation[dim]));
671 snapped_point.setSecondSnapDistance(NR_HUGE);
672 break;
673 case SKEW:
674 result[0] = (snapped_point.getPoint()[dim] - ((*i).getPoint())[dim]) / (((*i).getPoint())[1 - dim] - origin[1 - dim]); // skew factor
675 result[1] = transformation[1]; // scale factor
676 // Store the metric for this transformation as a virtual distance
677 snapped_point.setSnapDistance(std::abs(result[0] - transformation[0]));
678 snapped_point.setSecondSnapDistance(NR_HUGE);
679 break;
680 default:
681 g_assert_not_reached();
682 }
684 // When scaling, we're considering the best transformation in each direction separately. We will have a metric in each
685 // direction, whereas for all other transformation we only a single one-dimensional metric. That's why we need to handle
686 // the scaling metric differently
687 if (transformation_type == SCALE) {
688 for (int index = 0; index < 2; index++) {
689 if (fabs(scale_metric[index]) < fabs(best_scale_metric[index])) {
690 best_transformation[index] = result[index];
691 best_scale_metric[index] = fabs(scale_metric[index]);
692 // When scaling, we're considering the best transformation in each direction separately
693 // Therefore two different snapped points might together make a single best transformation
694 // We will however return only a single snapped point (e.g. to display the snapping indicator)
695 best_snapped_point = snapped_point;
696 // std::cout << "SEL ";
697 } // else { std::cout << " ";}
698 }
699 if (uniform) {
700 if (best_scale_metric[0] < best_scale_metric[1]) {
701 best_transformation[1] = best_transformation[0];
702 best_scale_metric[1] = best_scale_metric[0];
703 } else {
704 best_transformation[0] = best_transformation[1];
705 best_scale_metric[0] = best_scale_metric[1];
706 }
707 }
708 } else { // For all transformations other than scaling
709 if (best_snapped_point.isOtherSnapBetter(snapped_point, true)) {
710 best_transformation = result;
711 best_snapped_point = snapped_point;
712 }
713 }
714 }
716 j++;
717 }
719 Geom::Coord best_metric;
720 if (transformation_type == SCALE) {
721 // When scaling, don't ever exit with one of scaling components set to NR_HUGE
722 for (int index = 0; index < 2; index++) {
723 if (best_transformation[index] == NR_HUGE) {
724 if (uniform && best_transformation[1-index] < NR_HUGE) {
725 best_transformation[index] = best_transformation[1-index];
726 } else {
727 best_transformation[index] = transformation[index];
728 }
729 }
730 }
731 best_metric = std::min(best_scale_metric[0], best_scale_metric[1]);
732 } else { // For all transformations other than scaling
733 best_metric = best_snapped_point.getSnapDistance();
734 }
736 best_snapped_point.setTransformation(best_transformation);
737 // Using " < 1e6" instead of " < NR_HUGE" for catching some rounding errors
738 // These rounding errors might be caused by NRRects, see bug #1584301
739 best_snapped_point.setSnapDistance(best_metric < 1e6 ? best_metric : NR_HUGE);
740 return best_snapped_point;
741 }
744 /**
745 * \brief Apply a translation to a set of points and try to snap freely in 2 degrees-of-freedom
746 *
747 * \param point_type Category of points to which the source point belongs: node or bounding box.
748 * \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.
749 * \param pointer Location of the mouse pointer at the time dragging started (i.e. when the selection was still untransformed).
750 * \param tr Proposed translation; the final translation can only be calculated after snapping has occurred
751 * \return An instance of the SnappedPoint class, which holds data on the snap source, snap target, and various metrics.
752 */
754 Inkscape::SnappedPoint SnapManager::freeSnapTranslation(Inkscape::SnapPreferences::PointType point_type,
755 std::vector<Inkscape::SnapCandidatePoint> const &p,
756 Geom::Point const &pointer,
757 Geom::Point const &tr) const
758 {
759 if (p.size() == 1) {
760 Geom::Point pt = _transformPoint(p.at(0), TRANSLATION, tr, Geom::Point(0,0), Geom::X, false);
761 _displaySnapsource(point_type, Inkscape::SnapCandidatePoint(pt, p.at(0).getSourceType()));
762 }
764 return _snapTransformed(point_type, p, pointer, false, Geom::Point(0,0), TRANSLATION, tr, Geom::Point(0,0), Geom::X, false);
765 }
767 /**
768 * \brief Apply a translation to a set of points and try to snap along a constraint
769 *
770 * \param point_type Category of points to which the source point belongs: node or bounding box.
771 * \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.
772 * \param pointer Location of the mouse pointer at the time dragging started (i.e. when the selection was still untransformed).
773 * \param constraint The direction or line along which snapping must occur.
774 * \param tr Proposed translation; the final translation can only be calculated after snapping has occurred.
775 * \return An instance of the SnappedPoint class, which holds data on the snap source, snap target, and various metrics.
776 */
778 Inkscape::SnappedPoint SnapManager::constrainedSnapTranslation(Inkscape::SnapPreferences::PointType point_type,
779 std::vector<Inkscape::SnapCandidatePoint> const &p,
780 Geom::Point const &pointer,
781 Inkscape::Snapper::ConstraintLine const &constraint,
782 Geom::Point const &tr) const
783 {
784 if (p.size() == 1) {
785 Geom::Point pt = _transformPoint(p.at(0), TRANSLATION, tr, Geom::Point(0,0), Geom::X, false);
786 _displaySnapsource(point_type, Inkscape::SnapCandidatePoint(pt, p.at(0).getSourceType()));
787 }
789 return _snapTransformed(point_type, p, pointer, true, constraint, TRANSLATION, tr, Geom::Point(0,0), Geom::X, false);
790 }
793 /**
794 * \brief Apply a scaling to a set of points and try to snap freely in 2 degrees-of-freedom
795 *
796 * \param point_type Category of points to which the source point belongs: node or bounding box.
797 * \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.
798 * \param pointer Location of the mouse pointer at the time dragging started (i.e. when the selection was still untransformed).
799 * \param s Proposed scaling; the final scaling can only be calculated after snapping has occurred
800 * \param o Origin of the scaling
801 * \return An instance of the SnappedPoint class, which holds data on the snap source, snap target, and various metrics.
802 */
804 Inkscape::SnappedPoint SnapManager::freeSnapScale(Inkscape::SnapPreferences::PointType point_type,
805 std::vector<Inkscape::SnapCandidatePoint> const &p,
806 Geom::Point const &pointer,
807 Geom::Scale const &s,
808 Geom::Point const &o) const
809 {
810 if (p.size() == 1) {
811 Geom::Point pt = _transformPoint(p.at(0), SCALE, Geom::Point(s[Geom::X], s[Geom::Y]), o, Geom::X, false);
812 _displaySnapsource(point_type, Inkscape::SnapCandidatePoint(pt, p.at(0).getSourceType()));
813 }
815 return _snapTransformed(point_type, p, pointer, false, Geom::Point(0,0), SCALE, Geom::Point(s[Geom::X], s[Geom::Y]), o, Geom::X, false);
816 }
819 /**
820 * \brief Apply a scaling to a set of points and snap such that the aspect ratio of the selection is preserved
821 *
822 * \param point_type Category of points to which the source point belongs: node or bounding box.
823 * \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.
824 * \param pointer Location of the mouse pointer at the time dragging started (i.e. when the selection was still untransformed).
825 * \param s Proposed scaling; the final scaling can only be calculated after snapping has occurred
826 * \param o Origin of the scaling
827 * \return An instance of the SnappedPoint class, which holds data on the snap source, snap target, and various metrics.
828 */
830 Inkscape::SnappedPoint SnapManager::constrainedSnapScale(Inkscape::SnapPreferences::PointType point_type,
831 std::vector<Inkscape::SnapCandidatePoint> const &p,
832 Geom::Point const &pointer,
833 Geom::Scale const &s,
834 Geom::Point const &o) const
835 {
836 // When constrained scaling, only uniform scaling is supported.
837 if (p.size() == 1) {
838 Geom::Point pt = _transformPoint(p.at(0), SCALE, Geom::Point(s[Geom::X], s[Geom::Y]), o, Geom::X, true);
839 _displaySnapsource(point_type, Inkscape::SnapCandidatePoint(pt, p.at(0).getSourceType()));
840 }
842 return _snapTransformed(point_type, p, pointer, true, Geom::Point(0,0), SCALE, Geom::Point(s[Geom::X], s[Geom::Y]), o, Geom::X, true);
843 }
845 /**
846 * \brief Apply a stretch to a set of points and snap such that the direction of the stretch is preserved
847 *
848 * \param point_type Category of points to which the source point belongs: node or bounding box.
849 * \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.
850 * \param pointer Location of the mouse pointer at the time dragging started (i.e. when the selection was still untransformed).
851 * \param s Proposed stretch; the final stretch can only be calculated after snapping has occurred
852 * \param o Origin of the stretching
853 * \param d Dimension in which to apply proposed stretch.
854 * \param u true if the stretch should be uniform (i.e. to be applied equally in both dimensions)
855 * \return An instance of the SnappedPoint class, which holds data on the snap source, snap target, and various metrics.
856 */
858 Inkscape::SnappedPoint SnapManager::constrainedSnapStretch(Inkscape::SnapPreferences::PointType point_type,
859 std::vector<Inkscape::SnapCandidatePoint> const &p,
860 Geom::Point const &pointer,
861 Geom::Coord const &s,
862 Geom::Point const &o,
863 Geom::Dim2 d,
864 bool u) const
865 {
866 if (p.size() == 1) {
867 Geom::Point pt = _transformPoint(p.at(0), STRETCH, Geom::Point(s, s), o, d, u);
868 _displaySnapsource(point_type, Inkscape::SnapCandidatePoint(pt, p.at(0).getSourceType()));
869 }
871 return _snapTransformed(point_type, p, pointer, true, Geom::Point(0,0), STRETCH, Geom::Point(s, s), o, d, u);
872 }
874 /**
875 * \brief Apply a skew to a set of points and snap such that the direction of the skew is preserved
876 *
877 * \param point_type Category of points to which the source point belongs: node or bounding box.
878 * \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.
879 * \param pointer Location of the mouse pointer at the time dragging started (i.e. when the selection was still untransformed).
880 * \param constraint The direction or line along which snapping must occur.
881 * \param s Proposed skew; the final skew can only be calculated after snapping has occurred
882 * \param o Origin of the proposed skew
883 * \param d Dimension in which to apply proposed skew.
884 * \return An instance of the SnappedPoint class, which holds data on the snap source, snap target, and various metrics.
885 */
887 Inkscape::SnappedPoint SnapManager::constrainedSnapSkew(Inkscape::SnapPreferences::PointType point_type,
888 std::vector<Inkscape::SnapCandidatePoint> const &p,
889 Geom::Point const &pointer,
890 Inkscape::Snapper::ConstraintLine const &constraint,
891 Geom::Point const &s,
892 Geom::Point const &o,
893 Geom::Dim2 d) const
894 {
895 // "s" contains skew factor in s[0], and scale factor in s[1]
897 // Snapping the nodes of the bounding box of a selection that is being transformed, will only work if
898 // the transformation of the bounding box is equal to the transformation of the individual nodes. This is
899 // NOT the case for example when rotating or skewing. The bounding box itself cannot possibly rotate or skew,
900 // so it's corners have a different transformation. The snappers cannot handle this, therefore snapping
901 // of bounding boxes is not allowed here.
902 g_assert(!(point_type & Inkscape::SnapPreferences::SNAPPOINT_BBOX));
904 if (p.size() == 1) {
905 Geom::Point pt = _transformPoint(p.at(0), SKEW, s, o, d, false);
906 _displaySnapsource(point_type, Inkscape::SnapCandidatePoint(pt, p.at(0).getSourceType()));
907 }
909 return _snapTransformed(point_type, p, pointer, true, constraint, SKEW, s, o, d, false);
910 }
912 /**
913 * \brief Given a set of possible snap targets, find the best target (which is not necessarily
914 * also the nearest target), and show the snap indicator if requested
915 *
916 * \param p Source point to be snapped
917 * \param sc A structure holding all snap targets that have been found so far
918 * \param constrained True if the snap is constrained, e.g. for stretching or for purely horizontal translation.
919 * \param noCurves If true, then do consider snapping to intersections of curves, but not to the curves themselves
920 * \return An instance of the SnappedPoint class, which holds data on the snap source, snap target, and various metrics
921 */
923 Inkscape::SnappedPoint SnapManager::findBestSnap(Inkscape::SnapCandidatePoint const &p,
924 SnappedConstraints const &sc,
925 bool constrained,
926 bool noCurves) const
927 {
929 /*
930 std::cout << "Type and number of snapped constraints: " << std::endl;
931 std::cout << " Points : " << sc.points.size() << std::endl;
932 std::cout << " Lines : " << sc.lines.size() << std::endl;
933 std::cout << " Grid lines : " << sc.grid_lines.size()<< std::endl;
934 std::cout << " Guide lines : " << sc.guide_lines.size()<< std::endl;
935 std::cout << " Curves : " << sc.curves.size()<< std::endl;
936 */
938 // Store all snappoints
939 std::list<Inkscape::SnappedPoint> sp_list;
941 // search for the closest snapped point
942 Inkscape::SnappedPoint closestPoint;
943 if (getClosestSP(sc.points, closestPoint)) {
944 sp_list.push_back(closestPoint);
945 }
947 // search for the closest snapped curve
948 if (!noCurves) {
949 Inkscape::SnappedCurve closestCurve;
950 if (getClosestCurve(sc.curves, closestCurve)) {
951 sp_list.push_back(Inkscape::SnappedPoint(closestCurve));
952 }
953 }
955 if (snapprefs.getSnapIntersectionCS()) {
956 // search for the closest snapped intersection of curves
957 Inkscape::SnappedPoint closestCurvesIntersection;
958 if (getClosestIntersectionCS(sc.curves, p.getPoint(), closestCurvesIntersection, _desktop->dt2doc())) {
959 closestCurvesIntersection.setSource(p.getSourceType());
960 sp_list.push_back(closestCurvesIntersection);
961 }
962 }
964 // search for the closest snapped grid line
965 Inkscape::SnappedLine closestGridLine;
966 if (getClosestSL(sc.grid_lines, closestGridLine)) {
967 sp_list.push_back(Inkscape::SnappedPoint(closestGridLine));
968 }
970 // search for the closest snapped guide line
971 Inkscape::SnappedLine closestGuideLine;
972 if (getClosestSL(sc.guide_lines, closestGuideLine)) {
973 sp_list.push_back(Inkscape::SnappedPoint(closestGuideLine));
974 }
976 // When freely snapping to a grid/guide/path, only one degree of freedom is eliminated
977 // Therefore we will try get fully constrained by finding an intersection with another grid/guide/path
979 // When doing a constrained snap however, we're already at an intersection of the constrained line and
980 // the grid/guide/path we're snapping to. This snappoint is therefore fully constrained, so there's
981 // no need to look for additional intersections
982 if (!constrained) {
983 // search for the closest snapped intersection of grid lines
984 Inkscape::SnappedPoint closestGridPoint;
985 if (getClosestIntersectionSL(sc.grid_lines, closestGridPoint)) {
986 closestGridPoint.setSource(p.getSourceType());
987 closestGridPoint.setTarget(Inkscape::SNAPTARGET_GRID_INTERSECTION);
988 sp_list.push_back(closestGridPoint);
989 }
991 // search for the closest snapped intersection of guide lines
992 Inkscape::SnappedPoint closestGuidePoint;
993 if (getClosestIntersectionSL(sc.guide_lines, closestGuidePoint)) {
994 closestGuidePoint.setSource(p.getSourceType());
995 closestGuidePoint.setTarget(Inkscape::SNAPTARGET_GUIDE_INTERSECTION);
996 sp_list.push_back(closestGuidePoint);
997 }
999 // search for the closest snapped intersection of grid with guide lines
1000 if (snapprefs.getSnapIntersectionGG()) {
1001 Inkscape::SnappedPoint closestGridGuidePoint;
1002 if (getClosestIntersectionSL(sc.grid_lines, sc.guide_lines, closestGridGuidePoint)) {
1003 closestGridGuidePoint.setSource(p.getSourceType());
1004 closestGridGuidePoint.setTarget(Inkscape::SNAPTARGET_GRID_GUIDE_INTERSECTION);
1005 sp_list.push_back(closestGridGuidePoint);
1006 }
1007 }
1008 }
1010 // now let's see which snapped point gets a thumbs up
1011 Inkscape::SnappedPoint bestSnappedPoint(p.getPoint());
1012 // std::cout << "Finding the best snap..." << std::endl;
1013 for (std::list<Inkscape::SnappedPoint>::const_iterator i = sp_list.begin(); i != sp_list.end(); i++) {
1014 // first find out if this snapped point is within snapping range
1015 // std::cout << "sp = " << (*i).getPoint() << " | source = " << (*i).getSource() << " | target = " << (*i).getTarget();
1016 if ((*i).getSnapDistance() <= (*i).getTolerance()) {
1017 // if it's the first point, or if it is closer than the best snapped point so far
1018 if (i == sp_list.begin() || bestSnappedPoint.isOtherSnapBetter(*i, false)) {
1019 // then prefer this point over the previous one
1020 bestSnappedPoint = *i;
1021 }
1022 }
1023 // std::cout << std::endl;
1024 }
1026 // Update the snap indicator, if requested
1027 if (_snapindicator) {
1028 if (bestSnappedPoint.getSnapped()) {
1029 _desktop->snapindicator->set_new_snaptarget(bestSnappedPoint);
1030 } else {
1031 _desktop->snapindicator->remove_snaptarget();
1032 }
1033 }
1035 // std::cout << "findBestSnap = " << bestSnappedPoint.getPoint() << " | dist = " << bestSnappedPoint.getSnapDistance() << std::endl;
1036 return bestSnappedPoint;
1037 }
1039 /**
1040 * \brief Prepare the snap manager for the actual snapping, which includes building a list of snap targets
1041 * to ignore and toggling the snap indicator
1042 *
1043 * There are two overloaded setup() methods, of which this one only allows for a single item to be ignored
1044 * whereas the other one will take a list of items to ignore
1045 *
1046 * \param desktop Reference to the desktop to which this snap manager is attached
1047 * \param snapindicator If true then a snap indicator will be displayed automatically (when enabled in the preferences)
1048 * \param item_to_ignore This item will not be snapped to, e.g. the item that is currently being dragged. This avoids "self-snapping"
1049 * \param unselected_nodes Stationary nodes of the path that is currently being edited in the node tool and
1050 * that can be snapped too. Nodes not in this list will not be snapped to, to avoid "self-snapping". Of each
1051 * unselected node both the position (Geom::Point) and the type (Inkscape::SnapTargetType) will be stored
1052 * \param guide_to_ignore Guide that is currently being dragged and should not be snapped to
1053 */
1055 void SnapManager::setup(SPDesktop const *desktop,
1056 bool snapindicator,
1057 SPItem const *item_to_ignore,
1058 std::vector<Inkscape::SnapCandidatePoint> *unselected_nodes,
1059 SPGuide *guide_to_ignore)
1060 {
1061 g_assert(desktop != NULL);
1062 _item_to_ignore = item_to_ignore;
1063 _items_to_ignore = NULL;
1064 _desktop = desktop;
1065 _snapindicator = snapindicator;
1066 _unselected_nodes = unselected_nodes;
1067 _guide_to_ignore = guide_to_ignore;
1068 }
1070 /**
1071 * \brief Prepare the snap manager for the actual snapping, which includes building a list of snap targets
1072 * to ignore and toggling the snap indicator
1073 *
1074 * There are two overloaded setup() methods, of which the other one only allows for a single item to be ignored
1075 * whereas this one will take a list of items to ignore
1076 *
1077 * \param desktop Reference to the desktop to which this snap manager is attached
1078 * \param snapindicator If true then a snap indicator will be displayed automatically (when enabled in the preferences)
1079 * \param items_to_ignore These items will not be snapped to, e.g. the items that are currently being dragged. This avoids "self-snapping"
1080 * \param unselected_nodes Stationary nodes of the path that is currently being edited in the node tool and
1081 * that can be snapped too. Nodes not in this list will not be snapped to, to avoid "self-snapping". Of each
1082 * unselected node both the position (Geom::Point) and the type (Inkscape::SnapTargetType) will be stored
1083 * \param guide_to_ignore Guide that is currently being dragged and should not be snapped to
1084 */
1086 void SnapManager::setup(SPDesktop const *desktop,
1087 bool snapindicator,
1088 std::vector<SPItem const *> &items_to_ignore,
1089 std::vector<Inkscape::SnapCandidatePoint> *unselected_nodes,
1090 SPGuide *guide_to_ignore)
1091 {
1092 g_assert(desktop != NULL);
1093 _item_to_ignore = NULL;
1094 _items_to_ignore = &items_to_ignore;
1095 _desktop = desktop;
1096 _snapindicator = snapindicator;
1097 _unselected_nodes = unselected_nodes;
1098 _guide_to_ignore = guide_to_ignore;
1099 }
1101 SPDocument *SnapManager::getDocument() const
1102 {
1103 return _named_view->document;
1104 }
1106 /**
1107 * \brief Takes an untransformed point, applies the given transformation, and returns the transformed point. Eliminates lots of duplicated code
1108 *
1109 * \param p The untransformed position of the point, paired with an identifier of the type of the snap source.
1110 * \param transformation_type Type of transformation to apply.
1111 * \param transformation Mathematical description of the transformation; details depend on the type.
1112 * \param origin Origin of the transformation, if applicable.
1113 * \param dim Dimension to which the transformation applies, if applicable.
1114 * \param uniform true if the transformation should be uniform; only applicable for stretching and scaling.
1115 * \return The position of the point after transformation
1116 */
1118 Geom::Point SnapManager::_transformPoint(Inkscape::SnapCandidatePoint const &p,
1119 Transformation const transformation_type,
1120 Geom::Point const &transformation,
1121 Geom::Point const &origin,
1122 Geom::Dim2 const dim,
1123 bool const uniform) const
1124 {
1125 /* Work out the transformed version of this point */
1126 Geom::Point transformed;
1127 switch (transformation_type) {
1128 case TRANSLATION:
1129 transformed = p.getPoint() + transformation;
1130 break;
1131 case SCALE:
1132 transformed = (p.getPoint() - origin) * Geom::Scale(transformation[Geom::X], transformation[Geom::Y]) + origin;
1133 break;
1134 case STRETCH:
1135 {
1136 Geom::Scale s(1, 1);
1137 if (uniform)
1138 s[Geom::X] = s[Geom::Y] = transformation[dim];
1139 else {
1140 s[dim] = transformation[dim];
1141 s[1 - dim] = 1;
1142 }
1143 transformed = ((p.getPoint() - origin) * s) + origin;
1144 break;
1145 }
1146 case SKEW:
1147 // Apply the skew factor
1148 transformed[dim] = (p.getPoint())[dim] + transformation[0] * ((p.getPoint())[1 - dim] - origin[1 - dim]);
1149 // While skewing, mirroring and scaling (by integer multiples) in the opposite direction is also allowed.
1150 // Apply that scale factor here
1151 transformed[1-dim] = (p.getPoint() - origin)[1 - dim] * transformation[1] + origin[1 - dim];
1152 break;
1153 default:
1154 g_assert_not_reached();
1155 }
1157 return transformed;
1158 }
1160 /**
1161 * \brief Mark the location of the snap source (not the snap target!) on the canvas by drawing a symbol
1162 *
1163 * \param point_type Category of points to which the source point belongs: node, guide or bounding box
1164 * \param p The transformed position of the source point, paired with an identifier of the type of the snap source.
1165 */
1167 void SnapManager::_displaySnapsource(Inkscape::SnapPreferences::PointType point_type, Inkscape::SnapCandidatePoint const &p) const {
1169 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
1170 if (prefs->getBool("/options/snapclosestonly/value")) {
1171 bool p_is_a_node = point_type & Inkscape::SnapPreferences::SNAPPOINT_NODE;
1172 bool p_is_a_bbox = point_type & Inkscape::SnapPreferences::SNAPPOINT_BBOX;
1173 if (snapprefs.getSnapEnabledGlobally() && ((p_is_a_node && snapprefs.getSnapModeNode()) || (p_is_a_bbox && snapprefs.getSnapModeBBox()))) {
1174 _desktop->snapindicator->set_new_snapsource(p);
1175 } else {
1176 _desktop->snapindicator->remove_snapsource();
1177 }
1178 }
1179 }
1181 /*
1182 Local Variables:
1183 mode:c++
1184 c-file-style:"stroustrup"
1185 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1186 indent-tabs-mode:nil
1187 fill-column:99
1188 End:
1189 */
1190 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :