dcd4e85f74ee90df59f0e99b00e61e116ede0e00
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 snap_projection Currently unused
334 * \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
335 * \param bbox_to_snap Bounding box hulling the set of points, all from the same selection and having the same transformation
336 */
338 void SnapManager::constrainedSnapReturnByRef(Inkscape::SnapPreferences::PointType point_type,
339 Geom::Point &p,
340 Inkscape::SnapSourceType const source_type,
341 Inkscape::Snapper::ConstraintLine const &constraint,
342 bool const snap_projection,
343 bool first_point,
344 Geom::OptRect const &bbox_to_snap) const
345 {
346 Inkscape::SnappedPoint const s = constrainedSnap(point_type, p, source_type, constraint, snap_projection, first_point, bbox_to_snap);
347 s.getPoint(p);
348 }
350 /**
351 * \brief Try to snap a point along a constraint line to grids, guides or objects.
352 *
353 * Try to snap a point to grids, guides or objects, in only one degree-of-freedom,
354 * i.e. snap in a specific direction on the two dimensional canvas to the nearest
355 * snap target. constrainedSnap is equal in snapping behavior to
356 * constrainedSnapReturnByRef(). Please read the comments of the latter for more details.
357 *
358 * PS: SnapManager::setup() must have been called before calling this method,
359 * but only once for a set of points
360 *
361 * \param point_type Category of points to which the source point belongs: node, guide or bounding box
362 * \param p Current position of the snap source
363 * \param source_type Detailed description of the source type, will be used by the snap indicator
364 * \param constraint The direction or line along which snapping must occur
365 * \param snap_projection Currently unused
366 * \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
367 * \param bbox_to_snap Bounding box hulling the set of points, all from the same selection and having the same transformation
368 */
370 Inkscape::SnappedPoint SnapManager::constrainedSnap(Inkscape::SnapPreferences::PointType point_type,
371 Geom::Point const &p,
372 Inkscape::SnapSourceType const &source_type,
373 Inkscape::Snapper::ConstraintLine const &constraint,
374 bool /*snap_projection*/,
375 bool first_point,
376 Geom::OptRect const &bbox_to_snap) const
377 {
378 //TODO: Get rid of the snap_projection parameter (if it is really not used)
380 if (_desktop->event_context && _desktop->event_context->_snap_window_open == false) {
381 g_warning("The current tool tries to snap, but it hasn't yet opened the snap window. Please report this!");
382 // When the context goes into dragging-mode, then Inkscape should call this: sp_event_context_snap_window_open(event_context);
383 }
385 if (!someSnapperMightSnap()) {
386 return Inkscape::SnappedPoint(p, source_type, Inkscape::SNAPTARGET_UNDEFINED, NR_HUGE, 0, false, false);
387 }
389 std::vector<SPItem const *> *items_to_ignore;
390 if (_item_to_ignore) { // If we have only a single item to ignore
391 // then build a list containing this single item;
392 // This single-item list will prevail over any other _items_to_ignore list, should that exist
393 items_to_ignore = new std::vector<SPItem const *>;
394 items_to_ignore->push_back(_item_to_ignore);
395 } else {
396 items_to_ignore = _items_to_ignore;
397 }
399 Geom::Point pp = constraint.projection(p);
401 SnappedConstraints sc;
402 SnapperList const snappers = getSnappers();
403 for (SnapperList::const_iterator i = snappers.begin(); i != snappers.end(); i++) {
404 (*i)->constrainedSnap(sc, point_type, pp, source_type, first_point, bbox_to_snap, constraint, items_to_ignore);
405 }
407 if (_item_to_ignore) {
408 delete items_to_ignore;
409 }
411 return findBestSnap(p, source_type, sc, true);
412 }
414 /**
415 * \brief Try to snap a point of a guide to another guide or to a node
416 *
417 * Try to snap a point of a guide to another guide or to a node in two degrees-
418 * of-freedom, i.e. snap in any direction on the two dimensional canvas to the
419 * nearest snap target. This method is used when dragging or rotating a guide
420 *
421 * PS: SnapManager::setup() must have been called before calling this method,
422 *
423 * \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
424 * \param guide_normal Vector normal to the guide line
425 */
426 void SnapManager::guideFreeSnap(Geom::Point &p, Geom::Point const &guide_normal) const
427 {
428 if (_desktop->event_context && _desktop->event_context->_snap_window_open == false) {
429 g_warning("The current tool tries to snap, but it hasn't yet opened the snap window. Please report this!");
430 // When the context goes into dragging-mode, then Inkscape should call this: sp_event_context_snap_window_open(event_context);
431 }
433 if (!snapprefs.getSnapEnabledGlobally() || snapprefs.getSnapPostponedGlobally()) {
434 return;
435 }
437 if (!(object.GuidesMightSnap() || snapprefs.getSnapToGuides())) {
438 return;
439 }
441 // Snap to nodes
442 SnappedConstraints sc;
443 if (object.GuidesMightSnap()) {
444 object.guideFreeSnap(sc, p, guide_normal);
445 }
447 // Snap to guides
448 if (snapprefs.getSnapToGuides()) {
449 guide.freeSnap(sc, Inkscape::SnapPreferences::SNAPPOINT_GUIDE, p, Inkscape::SNAPSOURCE_GUIDE, true, Geom::OptRect(), NULL, NULL);
450 }
452 // We won't snap to grids, what's the use?
454 Inkscape::SnappedPoint const s = findBestSnap(p, Inkscape::SNAPSOURCE_GUIDE, sc, false);
455 s.getPoint(p);
456 }
458 /**
459 * \brief Try to snap a point on a guide to the intersection with another guide or a path
460 *
461 * Try to snap a point on a guide to the intersection of that guide with another
462 * guide or with a path. The snapped point will lie somewhere on the guide-line,
463 * making this is a constrained snap, i.e. in only one degree-of-freedom.
464 * This method is used when dragging the origin of the guide along the guide itself.
465 *
466 * PS: SnapManager::setup() must have been called before calling this method,
467 *
468 * \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
469 * \param guide_normal Vector normal to the guide line
470 */
472 void SnapManager::guideConstrainedSnap(Geom::Point &p, SPGuide const &guideline) const
473 {
474 if (_desktop->event_context && _desktop->event_context->_snap_window_open == false) {
475 g_warning("The current tool tries to snap, but it hasn't yet opened the snap window. Please report this!");
476 // When the context goes into dragging-mode, then Inkscape should call this: sp_event_context_snap_window_open(event_context);
477 }
479 if (!snapprefs.getSnapEnabledGlobally() || snapprefs.getSnapPostponedGlobally()) {
480 return;
481 }
483 if (!(object.ThisSnapperMightSnap() || snapprefs.getSnapToGuides())) {
484 return;
485 }
487 // Snap to nodes or paths
488 SnappedConstraints sc;
489 Inkscape::Snapper::ConstraintLine cl(guideline.point_on_line, Geom::rot90(guideline.normal_to_line));
490 if (object.ThisSnapperMightSnap()) {
491 object.constrainedSnap(sc, Inkscape::SnapPreferences::SNAPPOINT_GUIDE, p, Inkscape::SNAPSOURCE_GUIDE_ORIGIN, true, Geom::OptRect(), cl, NULL);
492 }
494 // Snap to guides
495 if (snapprefs.getSnapToGuides()) {
496 guide.constrainedSnap(sc, Inkscape::SnapPreferences::SNAPPOINT_GUIDE, p, Inkscape::SNAPSOURCE_GUIDE_ORIGIN, true, Geom::OptRect(), cl, NULL);
497 }
499 // We won't snap to grids, what's the use?
501 Inkscape::SnappedPoint const s = findBestSnap(p, Inkscape::SNAPSOURCE_GUIDE, sc, false);
502 s.getPoint(p);
503 }
505 /**
506 * \brief Method for snapping sets of points while they are being transformed
507 *
508 * Method for snapping sets of points while they are being transformed, when using
509 * for example the selector tool. This method is for internal use only, and should
510 * not have to be called directly. Use freeSnapTransalation(), constrainedSnapScale(),
511 * etc. instead.
512 *
513 * This is what is being done in this method: transform each point, find out whether
514 * a free snap or constrained snap is more appropriate, do the snapping, calculate
515 * some metrics to quantify the snap "distance", and see if it's better than the
516 * previous snap. Finally, the best ("nearest") snap from all these points is returned.
517 *
518 * \param type Category of points to which the source point belongs: node or bounding box.
519 * \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.
520 * \param pointer Location of the mouse pointer at the time dragging started (i.e. when the selection was still untransformed).
521 * \param constrained true if the snap is constrained, e.g. for stretching or for purely horizontal translation.
522 * \param constraint The direction or line along which snapping must occur, if 'constrained' is true; otherwise undefined.
523 * \param transformation_type Type of transformation to apply to points before trying to snap them.
524 * \param transformation Description of the transformation; details depend on the type.
525 * \param origin Origin of the transformation, if applicable.
526 * \param dim Dimension to which the transformation applies, if applicable.
527 * \param uniform true if the transformation should be uniform; only applicable for stretching and scaling.
528 * \return An instance of the SnappedPoint class, which holds data on the snap source, snap target, and various metrics.
529 */
531 Inkscape::SnappedPoint SnapManager::_snapTransformed(
532 Inkscape::SnapPreferences::PointType type,
533 std::vector<std::pair<Geom::Point, int> > const &points,
534 Geom::Point const &pointer,
535 bool constrained,
536 Inkscape::Snapper::ConstraintLine const &constraint,
537 Transformation transformation_type,
538 Geom::Point const &transformation,
539 Geom::Point const &origin,
540 Geom::Dim2 dim,
541 bool uniform) const
542 {
543 /* We have a list of points, which we are proposing to transform in some way. We need to see
544 ** if any of these points, when transformed, snap to anything. If they do, we return the
545 ** appropriate transformation with `true'; otherwise we return the original scale with `false'.
546 */
548 /* Quick check to see if we have any snappers that are enabled
549 ** Also used to globally disable all snapping
550 */
551 if (someSnapperMightSnap() == false) {
552 return Inkscape::SnappedPoint();
553 }
555 std::vector<std::pair<Geom::Point, int> > transformed_points;
556 Geom::Rect bbox;
558 for (std::vector<std::pair<Geom::Point, int> >::const_iterator i = points.begin(); i != points.end(); i++) {
560 /* Work out the transformed version of this point */
561 Geom::Point transformed = _transformPoint(*i, transformation_type, transformation, origin, dim, uniform);
563 // add the current transformed point to the box hulling all transformed points
564 if (i == points.begin()) {
565 bbox = Geom::Rect(transformed, transformed);
566 } else {
567 bbox.expandTo(transformed);
568 }
570 transformed_points.push_back(std::make_pair(transformed, (*i).second));
571 }
573 /* The current best transformation */
574 Geom::Point best_transformation = transformation;
576 /* The current best metric for the best transformation; lower is better, NR_HUGE
577 ** means that we haven't snapped anything.
578 */
579 Geom::Point best_scale_metric(NR_HUGE, NR_HUGE);
580 Inkscape::SnappedPoint best_snapped_point;
581 g_assert(best_snapped_point.getAlwaysSnap() == false); // Check initialization of snapped point
582 g_assert(best_snapped_point.getAtIntersection() == false);
584 std::vector<std::pair<Geom::Point, int> >::const_iterator j = transformed_points.begin();
586 // std::cout << std::endl;
587 for (std::vector<std::pair<Geom::Point, int> >::const_iterator i = points.begin(); i != points.end(); i++) {
589 /* Snap it */
590 Inkscape::SnappedPoint snapped_point;
591 Inkscape::Snapper::ConstraintLine dedicated_constraint = constraint;
592 Geom::Point const b = ((*i).first - origin); // vector to original point
594 if (constrained) {
595 if ((transformation_type == SCALE || transformation_type == STRETCH) && uniform) {
596 // When uniformly scaling, each point will have its own unique constraint line,
597 // running from the scaling origin to the original untransformed point. We will
598 // calculate that line here
599 dedicated_constraint = Inkscape::Snapper::ConstraintLine(origin, b);
600 } else if (transformation_type == STRETCH) { // when non-uniform stretching {
601 dedicated_constraint = Inkscape::Snapper::ConstraintLine((*i).first, component_vectors[dim]);
602 } else if (transformation_type == TRANSLATION) {
603 // When doing a constrained translation, all points will move in the same direction, i.e.
604 // either horizontally or vertically. The lines along which they move are therefore all
605 // parallel, but might not be colinear. Therefore we will have to set the point through
606 // which the constraint-line runs here, for each point individually.
607 dedicated_constraint.setPoint((*i).first);
608 } // else: leave the original constraint, e.g. for skewing
609 if (transformation_type == SCALE && !uniform) {
610 g_warning("Non-uniform constrained scaling is not supported!");
611 }
612 snapped_point = constrainedSnap(type, (*j).first, static_cast<Inkscape::SnapSourceType>((*j).second), dedicated_constraint, false, i == points.begin(), bbox);
613 } else {
614 bool const c1 = fabs(b[Geom::X]) < 1e-6;
615 bool const c2 = fabs(b[Geom::Y]) < 1e-6;
616 if (transformation_type == SCALE && (c1 || c2) && !(c1 && c2)) {
617 // When scaling, a point aligned either horizontally or vertically with the origin can only
618 // move in that specific direction; therefore it should only snap in that direction, otherwise
619 // we will get snapped points with an invalid transformation
620 dedicated_constraint = Inkscape::Snapper::ConstraintLine(origin, component_vectors[c1]);
621 snapped_point = constrainedSnap(type, (*j).first, static_cast<Inkscape::SnapSourceType>((*j).second), dedicated_constraint, false, i == points.begin(), bbox);
622 } else {
623 snapped_point = freeSnap(type, (*j).first, static_cast<Inkscape::SnapSourceType>((*j).second), i == points.begin(), bbox);
624 }
625 }
626 // std::cout << "dist = " << snapped_point.getSnapDistance() << std::endl;
627 snapped_point.setPointerDistance(Geom::L2(pointer - (*i).first));
629 Geom::Point result;
630 Geom::Point scale_metric(NR_HUGE, NR_HUGE);
632 if (snapped_point.getSnapped()) {
633 /* We snapped. Find the transformation that describes where the snapped point has
634 ** ended up, and also the metric for this transformation.
635 */
636 Geom::Point const a = (snapped_point.getPoint() - origin); // vector to snapped point
637 //Geom::Point const b = (*i - origin); // vector to original point
639 switch (transformation_type) {
640 case TRANSLATION:
641 result = snapped_point.getPoint() - (*i).first;
642 /* Consider the case in which a box is almost aligned with a grid in both
643 * horizontal and vertical directions. The distance to the intersection of
644 * the grid lines will always be larger then the distance to a single grid
645 * line. If we prefer snapping to an intersection instead of to a single
646 * grid line, then we cannot use "metric = Geom::L2(result)". Therefore the
647 * snapped distance will be used as a metric. Please note that the snapped
648 * distance is defined as the distance to the nearest line of the intersection,
649 * and not to the intersection itself!
650 */
651 // Only for translations, the relevant metric will be the real snapped distance,
652 // so we don't have to do anything special here
653 break;
654 case SCALE:
655 {
656 result = Geom::Point(NR_HUGE, NR_HUGE);
657 // If this point *i is horizontally or vertically aligned with
658 // the origin of the scaling, then it will scale purely in X or Y
659 // We can therefore only calculate the scaling in this direction
660 // and the scaling factor for the other direction should remain
661 // untouched (unless scaling is uniform ofcourse)
662 for (int index = 0; index < 2; index++) {
663 if (fabs(b[index]) > 1e-6) { // if SCALING CAN occur in this direction
664 if (fabs(fabs(a[index]/b[index]) - fabs(transformation[index])) > 1e-12) { // if SNAPPING DID occur in this direction
665 result[index] = a[index] / b[index]; // then calculate it!
666 }
667 // we might leave result[1-index] = NR_HUGE
668 // if scaling didn't occur in the other direction
669 }
670 }
671 // Compare the resulting scaling with the desired scaling
672 scale_metric = result - transformation; // One or both of its components might be NR_HUGE
673 break;
674 }
675 case STRETCH:
676 result = Geom::Point(NR_HUGE, NR_HUGE);
677 if (fabs(b[dim]) > 1e-6) { // if STRETCHING will occur for this point
678 result[dim] = a[dim] / b[dim];
679 result[1-dim] = uniform ? result[dim] : 1;
680 } else { // STRETCHING might occur for this point, but only when the stretching is uniform
681 if (uniform && fabs(b[1-dim]) > 1e-6) {
682 result[1-dim] = a[1-dim] / b[1-dim];
683 result[dim] = result[1-dim];
684 }
685 }
686 // Store the metric for this transformation as a virtual distance
687 snapped_point.setSnapDistance(std::abs(result[dim] - transformation[dim]));
688 snapped_point.setSecondSnapDistance(NR_HUGE);
689 break;
690 case SKEW:
691 result[0] = (snapped_point.getPoint()[dim] - ((*i).first)[dim]) / (((*i).first)[1 - dim] - origin[1 - dim]); // skew factor
692 result[1] = transformation[1]; // scale factor
693 // Store the metric for this transformation as a virtual distance
694 snapped_point.setSnapDistance(std::abs(result[0] - transformation[0]));
695 snapped_point.setSecondSnapDistance(NR_HUGE);
696 break;
697 default:
698 g_assert_not_reached();
699 }
701 // When scaling, we're considering the best transformation in each direction separately. We will have a metric in each
702 // direction, whereas for all other transformation we only a single one-dimensional metric. That's why we need to handle
703 // the scaling metric differently
704 if (transformation_type == SCALE) {
705 for (int index = 0; index < 2; index++) {
706 if (fabs(scale_metric[index]) < fabs(best_scale_metric[index])) {
707 best_transformation[index] = result[index];
708 best_scale_metric[index] = fabs(scale_metric[index]);
709 // When scaling, we're considering the best transformation in each direction separately
710 // Therefore two different snapped points might together make a single best transformation
711 // We will however return only a single snapped point (e.g. to display the snapping indicator)
712 best_snapped_point = snapped_point;
713 // std::cout << "SEL ";
714 } // else { std::cout << " ";}
715 }
716 if (uniform) {
717 if (best_scale_metric[0] < best_scale_metric[1]) {
718 best_transformation[1] = best_transformation[0];
719 best_scale_metric[1] = best_scale_metric[0];
720 } else {
721 best_transformation[0] = best_transformation[1];
722 best_scale_metric[0] = best_scale_metric[1];
723 }
724 }
725 } else { // For all transformations other than scaling
726 if (best_snapped_point.isOtherSnapBetter(snapped_point, true)) {
727 best_transformation = result;
728 best_snapped_point = snapped_point;
729 }
730 }
731 }
733 j++;
734 }
736 Geom::Coord best_metric;
737 if (transformation_type == SCALE) {
738 // When scaling, don't ever exit with one of scaling components set to NR_HUGE
739 for (int index = 0; index < 2; index++) {
740 if (best_transformation[index] == NR_HUGE) {
741 if (uniform && best_transformation[1-index] < NR_HUGE) {
742 best_transformation[index] = best_transformation[1-index];
743 } else {
744 best_transformation[index] = transformation[index];
745 }
746 }
747 }
748 best_metric = std::min(best_scale_metric[0], best_scale_metric[1]);
749 } else { // For all transformations other than scaling
750 best_metric = best_snapped_point.getSnapDistance();
751 }
753 best_snapped_point.setTransformation(best_transformation);
754 // Using " < 1e6" instead of " < NR_HUGE" for catching some rounding errors
755 // These rounding errors might be caused by NRRects, see bug #1584301
756 best_snapped_point.setSnapDistance(best_metric < 1e6 ? best_metric : NR_HUGE);
757 return best_snapped_point;
758 }
761 /**
762 * \brief Apply a translation to a set of points and try to snap freely in 2 degrees-of-freedom
763 *
764 * \param point_type Category of points to which the source point belongs: node or bounding box.
765 * \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.
766 * \param pointer Location of the mouse pointer at the time dragging started (i.e. when the selection was still untransformed).
767 * \param tr Proposed translation; the final translation can only be calculated after snapping has occurred
768 * \return An instance of the SnappedPoint class, which holds data on the snap source, snap target, and various metrics.
769 */
771 Inkscape::SnappedPoint SnapManager::freeSnapTranslation(Inkscape::SnapPreferences::PointType point_type,
772 std::vector<std::pair<Geom::Point, int> > const &p,
773 Geom::Point const &pointer,
774 Geom::Point const &tr) const
775 {
776 if (p.size() == 1) {
777 _displaySnapsource(point_type, std::make_pair(_transformPoint(p.at(0), TRANSLATION, tr, Geom::Point(0,0), Geom::X, false), (p.at(0)).second));
778 }
780 return _snapTransformed(point_type, p, pointer, false, Geom::Point(0,0), TRANSLATION, tr, Geom::Point(0,0), Geom::X, false);
781 }
783 /**
784 * \brief Apply a translation to a set of points and try to snap along a constraint
785 *
786 * \param point_type Category of points to which the source point belongs: node or bounding box.
787 * \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.
788 * \param pointer Location of the mouse pointer at the time dragging started (i.e. when the selection was still untransformed).
789 * \param constraint The direction or line along which snapping must occur.
790 * \param tr Proposed translation; the final translation can only be calculated after snapping has occurred.
791 * \return An instance of the SnappedPoint class, which holds data on the snap source, snap target, and various metrics.
792 */
794 Inkscape::SnappedPoint SnapManager::constrainedSnapTranslation(Inkscape::SnapPreferences::PointType point_type,
795 std::vector<std::pair<Geom::Point, int> > const &p,
796 Geom::Point const &pointer,
797 Inkscape::Snapper::ConstraintLine const &constraint,
798 Geom::Point const &tr) const
799 {
800 if (p.size() == 1) {
801 _displaySnapsource(point_type, std::make_pair(_transformPoint(p.at(0), TRANSLATION, tr, Geom::Point(0,0), Geom::X, false), (p.at(0)).second));
802 }
804 return _snapTransformed(point_type, p, pointer, true, constraint, TRANSLATION, tr, Geom::Point(0,0), Geom::X, false);
805 }
808 /**
809 * \brief Apply a scaling to a set of points and try to snap freely in 2 degrees-of-freedom
810 *
811 * \param point_type Category of points to which the source point belongs: node or bounding box.
812 * \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.
813 * \param pointer Location of the mouse pointer at the time dragging started (i.e. when the selection was still untransformed).
814 * \param s Proposed scaling; the final scaling can only be calculated after snapping has occurred
815 * \param o Origin of the scaling
816 * \return An instance of the SnappedPoint class, which holds data on the snap source, snap target, and various metrics.
817 */
819 Inkscape::SnappedPoint SnapManager::freeSnapScale(Inkscape::SnapPreferences::PointType point_type,
820 std::vector<std::pair<Geom::Point, int> > const &p,
821 Geom::Point const &pointer,
822 Geom::Scale const &s,
823 Geom::Point const &o) const
824 {
825 if (p.size() == 1) {
826 _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));
827 }
829 return _snapTransformed(point_type, p, pointer, false, Geom::Point(0,0), SCALE, Geom::Point(s[Geom::X], s[Geom::Y]), o, Geom::X, false);
830 }
833 /**
834 * \brief Apply a scaling to a set of points and snap such that the aspect ratio of the selection is preserved
835 *
836 * \param point_type Category of points to which the source point belongs: node or bounding box.
837 * \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.
838 * \param pointer Location of the mouse pointer at the time dragging started (i.e. when the selection was still untransformed).
839 * \param s Proposed scaling; the final scaling can only be calculated after snapping has occurred
840 * \param o Origin of the scaling
841 * \return An instance of the SnappedPoint class, which holds data on the snap source, snap target, and various metrics.
842 */
844 Inkscape::SnappedPoint SnapManager::constrainedSnapScale(Inkscape::SnapPreferences::PointType point_type,
845 std::vector<std::pair<Geom::Point, int> > const &p,
846 Geom::Point const &pointer,
847 Geom::Scale const &s,
848 Geom::Point const &o) const
849 {
850 // When constrained scaling, only uniform scaling is supported.
851 if (p.size() == 1) {
852 _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));
853 }
855 return _snapTransformed(point_type, p, pointer, true, Geom::Point(0,0), SCALE, Geom::Point(s[Geom::X], s[Geom::Y]), o, Geom::X, true);
856 }
858 /**
859 * \brief Apply a stretch to a set of points and snap such that the direction of the stretch is preserved
860 *
861 * \param point_type Category of points to which the source point belongs: node or bounding box.
862 * \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.
863 * \param pointer Location of the mouse pointer at the time dragging started (i.e. when the selection was still untransformed).
864 * \param s Proposed stretch; the final stretch can only be calculated after snapping has occurred
865 * \param o Origin of the stretching
866 * \param d Dimension in which to apply proposed stretch.
867 * \param u true if the stretch should be uniform (i.e. to be applied equally in both dimensions)
868 * \return An instance of the SnappedPoint class, which holds data on the snap source, snap target, and various metrics.
869 */
871 Inkscape::SnappedPoint SnapManager::constrainedSnapStretch(Inkscape::SnapPreferences::PointType point_type,
872 std::vector<std::pair<Geom::Point, int> > const &p,
873 Geom::Point const &pointer,
874 Geom::Coord const &s,
875 Geom::Point const &o,
876 Geom::Dim2 d,
877 bool u) const
878 {
879 if (p.size() == 1) {
880 _displaySnapsource(point_type, std::make_pair(_transformPoint(p.at(0), STRETCH, Geom::Point(s, s), o, d, u), (p.at(0)).second));
881 }
883 return _snapTransformed(point_type, p, pointer, true, Geom::Point(0,0), STRETCH, Geom::Point(s, s), o, d, u);
884 }
886 /**
887 * \brief Apply a skew to a set of points and snap such that the direction of the skew is preserved
888 *
889 * \param point_type Category of points to which the source point belongs: node or bounding box.
890 * \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.
891 * \param pointer Location of the mouse pointer at the time dragging started (i.e. when the selection was still untransformed).
892 * \param constraint The direction or line along which snapping must occur.
893 * \param s Proposed skew; the final skew can only be calculated after snapping has occurred
894 * \param o Origin of the proposed skew
895 * \param d Dimension in which to apply proposed skew.
896 * \return An instance of the SnappedPoint class, which holds data on the snap source, snap target, and various metrics.
897 */
899 Inkscape::SnappedPoint SnapManager::constrainedSnapSkew(Inkscape::SnapPreferences::PointType point_type,
900 std::vector<std::pair<Geom::Point, int> > const &p,
901 Geom::Point const &pointer,
902 Inkscape::Snapper::ConstraintLine const &constraint,
903 Geom::Point const &s,
904 Geom::Point const &o,
905 Geom::Dim2 d) const
906 {
907 // "s" contains skew factor in s[0], and scale factor in s[1]
909 // Snapping the nodes of the bounding box of a selection that is being transformed, will only work if
910 // the transformation of the bounding box is equal to the transformation of the individual nodes. This is
911 // NOT the case for example when rotating or skewing. The bounding box itself cannot possibly rotate or skew,
912 // so it's corners have a different transformation. The snappers cannot handle this, therefore snapping
913 // of bounding boxes is not allowed here.
914 g_assert(!(point_type & Inkscape::SnapPreferences::SNAPPOINT_BBOX));
916 if (p.size() == 1) {
917 _displaySnapsource(point_type, std::make_pair(_transformPoint(p.at(0), SKEW, s, o, d, false), (p.at(0)).second));
918 }
920 return _snapTransformed(point_type, p, pointer, true, constraint, SKEW, s, o, d, false);
921 }
923 /**
924 * \brief Given a set of possible snap targets, find the best target (which is not necessarily
925 * also the nearest target), and show the snap indicator if requested
926 *
927 * \param p Current position of the snap source
928 * \param source_type Detailed description of the source type, will be used by the snap indicator
929 * \param sc A structure holding all snap targets that have been found so far
930 * \param constrained True if the snap is constrained, e.g. for stretching or for purely horizontal translation.
931 * \return An instance of the SnappedPoint class, which holds data on the snap source, snap target, and various metrics
932 */
934 Inkscape::SnappedPoint SnapManager::findBestSnap(Geom::Point const &p,
935 Inkscape::SnapSourceType const source_type,
936 SnappedConstraints &sc,
937 bool constrained) const
938 {
940 /*
941 std::cout << "Type and number of snapped constraints: " << std::endl;
942 std::cout << " Points : " << sc.points.size() << std::endl;
943 std::cout << " Lines : " << sc.lines.size() << std::endl;
944 std::cout << " Grid lines : " << sc.grid_lines.size()<< std::endl;
945 std::cout << " Guide lines : " << sc.guide_lines.size()<< std::endl;
946 std::cout << " Curves : " << sc.curves.size()<< std::endl;
947 */
949 // Store all snappoints
950 std::list<Inkscape::SnappedPoint> sp_list;
952 // search for the closest snapped point
953 Inkscape::SnappedPoint closestPoint;
954 if (getClosestSP(sc.points, closestPoint)) {
955 sp_list.push_back(closestPoint);
956 }
958 // search for the closest snapped curve
959 Inkscape::SnappedCurve closestCurve;
960 if (getClosestCurve(sc.curves, closestCurve)) {
961 sp_list.push_back(Inkscape::SnappedPoint(closestCurve));
962 }
964 if (snapprefs.getSnapIntersectionCS()) {
965 // search for the closest snapped intersection of curves
966 Inkscape::SnappedPoint closestCurvesIntersection;
967 if (getClosestIntersectionCS(sc.curves, p, closestCurvesIntersection, _desktop->dt2doc())) {
968 closestCurvesIntersection.setSource(source_type);
969 sp_list.push_back(closestCurvesIntersection);
970 }
971 }
973 // search for the closest snapped grid line
974 Inkscape::SnappedLine closestGridLine;
975 if (getClosestSL(sc.grid_lines, closestGridLine)) {
976 sp_list.push_back(Inkscape::SnappedPoint(closestGridLine));
977 }
979 // search for the closest snapped guide line
980 Inkscape::SnappedLine closestGuideLine;
981 if (getClosestSL(sc.guide_lines, closestGuideLine)) {
982 sp_list.push_back(Inkscape::SnappedPoint(closestGuideLine));
983 }
985 // When freely snapping to a grid/guide/path, only one degree of freedom is eliminated
986 // Therefore we will try get fully constrained by finding an intersection with another grid/guide/path
988 // When doing a constrained snap however, we're already at an intersection of the constrained line and
989 // the grid/guide/path we're snapping to. This snappoint is therefore fully constrained, so there's
990 // no need to look for additional intersections
991 if (!constrained) {
992 // search for the closest snapped intersection of grid lines
993 Inkscape::SnappedPoint closestGridPoint;
994 if (getClosestIntersectionSL(sc.grid_lines, closestGridPoint)) {
995 closestGridPoint.setSource(source_type);
996 closestGridPoint.setTarget(Inkscape::SNAPTARGET_GRID_INTERSECTION);
997 sp_list.push_back(closestGridPoint);
998 }
1000 // search for the closest snapped intersection of guide lines
1001 Inkscape::SnappedPoint closestGuidePoint;
1002 if (getClosestIntersectionSL(sc.guide_lines, closestGuidePoint)) {
1003 closestGuidePoint.setSource(source_type);
1004 closestGuidePoint.setTarget(Inkscape::SNAPTARGET_GUIDE_INTERSECTION);
1005 sp_list.push_back(closestGuidePoint);
1006 }
1008 // search for the closest snapped intersection of grid with guide lines
1009 if (snapprefs.getSnapIntersectionGG()) {
1010 Inkscape::SnappedPoint closestGridGuidePoint;
1011 if (getClosestIntersectionSL(sc.grid_lines, sc.guide_lines, closestGridGuidePoint)) {
1012 closestGridGuidePoint.setSource(source_type);
1013 closestGridGuidePoint.setTarget(Inkscape::SNAPTARGET_GRID_GUIDE_INTERSECTION);
1014 sp_list.push_back(closestGridGuidePoint);
1015 }
1016 }
1017 }
1019 // now let's see which snapped point gets a thumbs up
1020 Inkscape::SnappedPoint bestSnappedPoint = Inkscape::SnappedPoint(p, Inkscape::SNAPSOURCE_UNDEFINED, Inkscape::SNAPTARGET_UNDEFINED, NR_HUGE, 0, false, false);
1021 // std::cout << "Finding the best snap..." << std::endl;
1022 for (std::list<Inkscape::SnappedPoint>::const_iterator i = sp_list.begin(); i != sp_list.end(); i++) {
1023 // first find out if this snapped point is within snapping range
1024 // std::cout << "sp = " << from_2geom((*i).getPoint());
1025 if ((*i).getSnapDistance() <= (*i).getTolerance()) {
1026 // if it's the first point, or if it is closer than the best snapped point so far
1027 if (i == sp_list.begin() || bestSnappedPoint.isOtherSnapBetter(*i, false)) {
1028 // then prefer this point over the previous one
1029 bestSnappedPoint = *i;
1030 }
1031 }
1032 // std::cout << std::endl;
1033 }
1035 // Update the snap indicator, if requested
1036 if (_snapindicator) {
1037 if (bestSnappedPoint.getSnapped()) {
1038 _desktop->snapindicator->set_new_snaptarget(bestSnappedPoint);
1039 } else {
1040 _desktop->snapindicator->remove_snaptarget();
1041 }
1042 }
1044 // std::cout << "findBestSnap = " << bestSnappedPoint.getPoint() << " | dist = " << bestSnappedPoint.getSnapDistance() << std::endl;
1045 return bestSnappedPoint;
1046 }
1048 /**
1049 * \brief Prepare the snap manager for the actual snapping, which includes building a list of snap targets
1050 * to ignore and toggling the snap indicator
1051 *
1052 * There are two overloaded setup() methods, of which this one only allows for a single item to be ignored
1053 * whereas the other one will take a list of items to ignore
1054 *
1055 * \param desktop Reference to the desktop to which this snap manager is attached
1056 * \param snapindicator If true then a snap indicator will be displayed automatically (when enabled in the preferences)
1057 * \param item_to_ignore This item will not be snapped to, e.g. the item that is currently being dragged. This avoids "self-snapping"
1058 * \param unselected_nodes Stationary nodes of the path that is currently being edited in the node tool and
1059 * that can be snapped too. Nodes not in this list will not be snapped to, to avoid "self-snapping". Of each
1060 * unselected node both the position (Geom::Point) and the type (Inkscape::SnapTargetType) will be stored
1061 * \param guide_to_ignore Guide that is currently being dragged and should not be snapped to
1062 */
1064 void SnapManager::setup(SPDesktop const *desktop,
1065 bool snapindicator,
1066 SPItem const *item_to_ignore,
1067 std::vector<std::pair<Geom::Point, int> > *unselected_nodes,
1068 SPGuide *guide_to_ignore)
1069 {
1070 g_assert(desktop != NULL);
1071 _item_to_ignore = item_to_ignore;
1072 _items_to_ignore = NULL;
1073 _desktop = desktop;
1074 _snapindicator = snapindicator;
1075 _unselected_nodes = unselected_nodes;
1076 _guide_to_ignore = guide_to_ignore;
1077 }
1079 /**
1080 * \brief Prepare the snap manager for the actual snapping, which includes building a list of snap targets
1081 * to ignore and toggling the snap indicator
1082 *
1083 * There are two overloaded setup() methods, of which the other one only allows for a single item to be ignored
1084 * whereas this one will take a list of items to ignore
1085 *
1086 * \param desktop Reference to the desktop to which this snap manager is attached
1087 * \param snapindicator If true then a snap indicator will be displayed automatically (when enabled in the preferences)
1088 * \param items_to_ignore These items will not be snapped to, e.g. the items that are currently being dragged. This avoids "self-snapping"
1089 * \param unselected_nodes Stationary nodes of the path that is currently being edited in the node tool and
1090 * that can be snapped too. Nodes not in this list will not be snapped to, to avoid "self-snapping". Of each
1091 * unselected node both the position (Geom::Point) and the type (Inkscape::SnapTargetType) will be stored
1092 * \param guide_to_ignore Guide that is currently being dragged and should not be snapped to
1093 */
1095 void SnapManager::setup(SPDesktop const *desktop,
1096 bool snapindicator,
1097 std::vector<SPItem const *> &items_to_ignore,
1098 std::vector<std::pair<Geom::Point, int> > *unselected_nodes,
1099 SPGuide *guide_to_ignore)
1100 {
1101 g_assert(desktop != NULL);
1102 _item_to_ignore = NULL;
1103 _items_to_ignore = &items_to_ignore;
1104 _desktop = desktop;
1105 _snapindicator = snapindicator;
1106 _unselected_nodes = unselected_nodes;
1107 _guide_to_ignore = guide_to_ignore;
1108 }
1110 SPDocument *SnapManager::getDocument() const
1111 {
1112 return _named_view->document;
1113 }
1115 /**
1116 * \brief Takes an untransformed point, applies the given transformation, and returns the transformed point. Eliminates lots of duplicated code
1117 *
1118 * \param p The untransformed position of the point, paired with an identifier of the type of the snap source.
1119 * \param transformation_type Type of transformation to apply.
1120 * \param transformation Mathematical description of the transformation; details depend on the type.
1121 * \param origin Origin of the transformation, if applicable.
1122 * \param dim Dimension to which the transformation applies, if applicable.
1123 * \param uniform true if the transformation should be uniform; only applicable for stretching and scaling.
1124 * \return The position of the point after transformation
1125 */
1127 Geom::Point SnapManager::_transformPoint(std::pair<Geom::Point, int> const &p,
1128 Transformation const transformation_type,
1129 Geom::Point const &transformation,
1130 Geom::Point const &origin,
1131 Geom::Dim2 const dim,
1132 bool const uniform) const
1133 {
1134 /* Work out the transformed version of this point */
1135 Geom::Point transformed;
1136 switch (transformation_type) {
1137 case TRANSLATION:
1138 transformed = p.first + transformation;
1139 break;
1140 case SCALE:
1141 transformed = (p.first - origin) * Geom::Scale(transformation[Geom::X], transformation[Geom::Y]) + origin;
1142 break;
1143 case STRETCH:
1144 {
1145 Geom::Scale s(1, 1);
1146 if (uniform)
1147 s[Geom::X] = s[Geom::Y] = transformation[dim];
1148 else {
1149 s[dim] = transformation[dim];
1150 s[1 - dim] = 1;
1151 }
1152 transformed = ((p.first - origin) * s) + origin;
1153 break;
1154 }
1155 case SKEW:
1156 // Apply the skew factor
1157 transformed[dim] = (p.first)[dim] + transformation[0] * ((p.first)[1 - dim] - origin[1 - dim]);
1158 // While skewing, mirroring and scaling (by integer multiples) in the opposite direction is also allowed.
1159 // Apply that scale factor here
1160 transformed[1-dim] = (p.first - origin)[1 - dim] * transformation[1] + origin[1 - dim];
1161 break;
1162 default:
1163 g_assert_not_reached();
1164 }
1166 return transformed;
1167 }
1169 /**
1170 * \brief Mark the location of the snap source (not the snap target!) on the canvas by drawing a symbol
1171 *
1172 * \param point_type Category of points to which the source point belongs: node, guide or bounding box
1173 * \param p The transformed position of the source point, paired with an identifier of the type of the snap source.
1174 */
1176 void SnapManager::_displaySnapsource(Inkscape::SnapPreferences::PointType point_type, std::pair<Geom::Point, int> const &p) const {
1178 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
1179 if (prefs->getBool("/options/snapclosestonly/value")) {
1180 bool p_is_a_node = point_type & Inkscape::SnapPreferences::SNAPPOINT_NODE;
1181 bool p_is_a_bbox = point_type & Inkscape::SnapPreferences::SNAPPOINT_BBOX;
1182 if (snapprefs.getSnapEnabledGlobally() && ((p_is_a_node && snapprefs.getSnapModeNode()) || (p_is_a_bbox && snapprefs.getSnapModeBBox()))) {
1183 _desktop->snapindicator->set_new_snapsource(p);
1184 } else {
1185 _desktop->snapindicator->remove_snapsource();
1186 }
1187 }
1188 }
1190 /*
1191 Local Variables:
1192 mode:c++
1193 c-file-style:"stroustrup"
1194 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1195 indent-tabs-mode:nil
1196 fill-column:99
1197 End:
1198 */
1199 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :