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