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 *
13 * Copyright (C) 2006-2007 Johan Engelen <johan@shouraizou.nl>
14 * Copyrigth (C) 2004 Nathan Hurst
15 * Copyright (C) 1999-2002 Authors
16 *
17 * Released under GNU GPL, read the file 'COPYING' for more information
18 */
20 #include "sp-namedview.h"
21 #include "snap.h"
23 #include <libnr/nr-point-fns.h>
24 #include <libnr/nr-scale-ops.h>
25 #include <libnr/nr-values.h>
27 #include "display/canvas-grid.h"
29 #include "inkscape.h"
30 #include "desktop.h"
31 #include "sp-guide.h"
32 using std::vector;
34 /**
35 * Construct a SnapManager for a SPNamedView.
36 *
37 * \param v `Owning' SPNamedView.
38 */
40 SnapManager::SnapManager(SPNamedView const *v) :
41 guide(v, 0),
42 object(v, 0),
43 _named_view(v),
44 _include_item_center(false)
45 {
47 }
50 /**
51 * \return List of snappers that we use.
52 */
53 SnapManager::SnapperList
54 SnapManager::getSnappers() const
55 {
56 SnapManager::SnapperList s;
57 s.push_back(&guide);
58 s.push_back(&object);
60 SnapManager::SnapperList gs = getGridSnappers();
61 s.splice(s.begin(), gs);
63 return s;
64 }
66 /**
67 * \return List of gridsnappers that we use.
68 */
69 SnapManager::SnapperList
70 SnapManager::getGridSnappers() const
71 {
72 SnapperList s;
74 //FIXME: this code should actually do this: add new grid snappers that are active for this desktop. now it just adds all gridsnappers
75 SPDesktop* desktop = SP_ACTIVE_DESKTOP;
76 if (desktop && desktop->gridsEnabled()) {
77 for ( GSList const *l = _named_view->grids; l != NULL; l = l->next) {
78 Inkscape::CanvasGrid *grid = (Inkscape::CanvasGrid*) l->data;
79 s.push_back(grid->snapper);
80 }
81 }
83 return s;
84 }
86 /**
87 * \return true if one of the snappers will try to snap something.
88 */
90 bool SnapManager::SomeSnapperMightSnap() const
91 {
92 SnapperList const s = getSnappers();
93 SnapperList::const_iterator i = s.begin();
94 while (i != s.end() && (*i)->ThisSnapperMightSnap() == false) {
95 i++;
96 }
98 return (i != s.end());
99 }
101 /*
102 * The snappers have too many parameters to adjust individually. Therefore only
103 * two snapping modes are presented to the user: snapping bounding box corners (to
104 * other bounding boxes, grids or guides), and/or snapping nodes (to other nodes,
105 * paths, grids or guides). To select either of these modes (or both), use the
106 * methods defined below: setSnapModeBBox() and setSnapModeNode().
107 *
108 * */
111 void SnapManager::setSnapModeBBox(bool enabled)
112 {
113 //The default values are being set in sp_namedview_set() (in sp-namedview.cpp)
114 guide.setSnapFrom(Inkscape::Snapper::SNAPPOINT_BBOX, enabled);
116 for ( GSList const *l = _named_view->grids; l != NULL; l = l->next) {
117 Inkscape::CanvasGrid *grid = (Inkscape::CanvasGrid*) l->data;
118 grid->snapper->setSnapFrom(Inkscape::Snapper::SNAPPOINT_BBOX, enabled);
119 }
121 object.setSnapFrom(Inkscape::Snapper::SNAPPOINT_BBOX, enabled);
122 object.setSnapToBBoxNode(enabled);
123 object.setSnapToBBoxPath(enabled);
124 object.setStrictSnapping(true); //don't snap bboxes to nodes/paths and vice versa
125 }
127 bool SnapManager::getSnapModeBBox() const
128 {
129 return guide.getSnapFrom(Inkscape::Snapper::SNAPPOINT_BBOX);
130 }
132 void SnapManager::setSnapModeNode(bool enabled)
133 {
134 guide.setSnapFrom(Inkscape::Snapper::SNAPPOINT_NODE, enabled);
136 for ( GSList const *l = _named_view->grids; l != NULL; l = l->next) {
137 Inkscape::CanvasGrid *grid = (Inkscape::CanvasGrid*) l->data;
138 grid->snapper->setSnapFrom(Inkscape::Snapper::SNAPPOINT_NODE, enabled);
139 }
141 object.setSnapFrom(Inkscape::Snapper::SNAPPOINT_NODE, enabled);
142 //object.setSnapToItemNode(enabled); // On second thought, these should be controlled
143 //object.setSnapToItemPath(enabled); // separately by the snapping prefs dialog
144 object.setStrictSnapping(true);
145 }
147 bool SnapManager::getSnapModeNode() const
148 {
149 return guide.getSnapFrom(Inkscape::Snapper::SNAPPOINT_NODE);
150 }
152 void SnapManager::setSnapModeGuide(bool enabled)
153 {
154 object.setSnapFrom(Inkscape::Snapper::SNAPPOINT_GUIDE, enabled);
155 }
157 bool SnapManager::getSnapModeGuide() const
158 {
159 return object.getSnapFrom(Inkscape::Snapper::SNAPPOINT_GUIDE);
160 }
162 /**
163 * Try to snap a point to any interested snappers.
164 *
165 * \param t Type of point.
166 * \param p Point.
167 * \param it Item to ignore when snapping.
168 * \return Snapped point.
169 */
171 Inkscape::SnappedPoint SnapManager::freeSnap(Inkscape::Snapper::PointType t,
172 NR::Point const &p,
173 SPItem const *it) const
175 {
176 std::list<SPItem const *> lit;
177 lit.push_back(it);
179 std::vector<NR::Point> points_to_snap;
180 points_to_snap.push_back(p);
182 return freeSnap(t, p, true, points_to_snap, lit);
183 }
186 /**
187 * Try to snap a point to any interested snappers.
188 *
189 * \param t Type of point.
190 * \param p Point.
191 * \param first_point If true then this point is the first one from a whole bunch of points
192 * \param points_to_snap The whole bunch of points, all from the same selection and having the same transformation
193 * \param it List of items to ignore when snapping.
194 * \return Snapped point.
195 */
197 Inkscape::SnappedPoint SnapManager::freeSnap(Inkscape::Snapper::PointType t,
198 NR::Point const &p,
199 bool const &first_point,
200 std::vector<NR::Point> &points_to_snap,
201 std::list<SPItem const *> const &it) const
202 {
203 SnapperList const snappers = getSnappers();
205 return freeSnap(t, p, first_point, points_to_snap, it, snappers);
206 }
208 /**
209 * Try to snap a point to any of the specified snappers.
210 *
211 * \param t Type of point.
212 * \param p Point.
213 * \param first_point If true then this point is the first one from a whole bunch of points
214 * \param points_to_snap The whole bunch of points, all from the same selection and having the same transformation
215 * \param it List of items to ignore when snapping.
216 * \param snappers List of snappers to try to snap to
217 * \return Snapped point.
218 */
220 Inkscape::SnappedPoint SnapManager::freeSnap(Inkscape::Snapper::PointType t,
221 NR::Point const &p,
222 bool const &first_point,
223 std::vector<NR::Point> &points_to_snap,
224 std::list<SPItem const *> const &it,
225 SnapperList const &snappers) const
226 {
227 Inkscape::SnappedPoint r(p, NR_HUGE);
229 for (SnapperList::const_iterator i = snappers.begin(); i != snappers.end(); i++) {
230 Inkscape::SnappedPoint const s = (*i)->freeSnap(t, p, first_point, points_to_snap, it);
231 if (s.getDistance() < r.getDistance()) {
232 r = s;
233 }
234 }
236 return r;
237 }
239 /**
240 * Try to snap a point to any of the specified snappers. Snap always, ignoring the snap-distance
241 *
242 * \param t Type of point.
243 * \param p Point.
244 * \param it Item to ignore when snapping.
245 * \param snappers List of snappers to try to snap to
246 * \return Snapped point.
247 */
249 Inkscape::SnappedPoint
250 SnapManager::freeSnapAlways( Inkscape::Snapper::PointType t,
251 NR::Point const &p,
252 SPItem const *it,
253 SnapperList &snappers )
254 {
255 std::list<SPItem const *> lit;
256 lit.push_back(it);
257 return freeSnapAlways(t, p, lit, snappers);
258 }
260 /**
261 * Try to snap a point to any of the specified snappers. Snap always, ignoring the snap-distance
262 *
263 * \param t Type of point.
264 * \param p Point.
265 * \param it List of items to ignore when snapping.
266 * \param snappers List of snappers to try to snap to
267 * \return Snapped point.
268 */
270 Inkscape::SnappedPoint
271 SnapManager::freeSnapAlways( Inkscape::Snapper::PointType t,
272 NR::Point const &p,
273 std::list<SPItem const *> const &it,
274 SnapperList &snappers )
275 {
276 Inkscape::SnappedPoint r(p, NR_HUGE);
278 for (SnapperList::iterator i = snappers.begin(); i != snappers.end(); i++) {
279 gdouble const curr_gridsnap = (*i)->getDistance();
280 const_cast<Inkscape::Snapper*> (*i)->setDistance(NR_HUGE);
281 std::vector<NR::Point> points_to_snap;
282 points_to_snap.push_back(p);
283 Inkscape::SnappedPoint const s = (*i)->freeSnap(t, p, true, points_to_snap, it);
284 const_cast<Inkscape::Snapper*> (*i)->setDistance(curr_gridsnap);
286 if (s.getDistance() < r.getDistance()) {
287 r = s;
288 }
289 }
291 return r;
292 }
296 /**
297 * Try to snap a point to any interested snappers. A snap will only occur along
298 * a line described by a Inkscape::Snapper::ConstraintLine.
299 *
300 * \param t Type of point.
301 * \param p Point.
302 * \param c Constraint line.
303 * \param it Item to ignore when snapping.
304 * \return Snapped point.
305 */
307 Inkscape::SnappedPoint SnapManager::constrainedSnap(Inkscape::Snapper::PointType t,
308 NR::Point const &p,
309 Inkscape::Snapper::ConstraintLine const &c,
310 SPItem const *it) const
311 {
312 std::list<SPItem const *> lit;
313 lit.push_back(it);
315 std::vector<NR::Point> points_to_snap;
316 points_to_snap.push_back(p);
318 return constrainedSnap(t, p, true, points_to_snap, c, lit);
319 }
323 /**
324 * Try to snap a point to any interested snappers. A snap will only occur along
325 * a line described by a Inkscape::Snapper::ConstraintLine.
326 *
327 * \param t Type of point.
328 * \param p Point.
329 * \param first_point If true then this point is the first one from a whole bunch of points
330 * \param points_to_snap The whole bunch of points, all from the same selection and having the same transformation
331 * \param c Constraint line.
332 * \param it List of items to ignore when snapping.
333 * \return Snapped point.
334 */
336 Inkscape::SnappedPoint SnapManager::constrainedSnap(Inkscape::Snapper::PointType t,
337 NR::Point const &p,
338 bool const &first_point,
339 std::vector<NR::Point> &points_to_snap,
340 Inkscape::Snapper::ConstraintLine const &c,
341 std::list<SPItem const *> const &it) const
342 {
343 Inkscape::SnappedPoint r(p, NR_HUGE);
345 SnapperList const snappers = getSnappers();
346 for (SnapperList::const_iterator i = snappers.begin(); i != snappers.end(); i++) {
347 Inkscape::SnappedPoint const s = (*i)->constrainedSnap(t, p, first_point, points_to_snap, c, it);
348 if (s.getDistance() < r.getDistance()) {
349 r = s;
350 }
351 }
353 return r;
354 }
356 Inkscape::SnappedPoint SnapManager::guideSnap(NR::Point const &p,
357 NR::Point const &guide_normal) const
358 {
359 Inkscape::ObjectSnapper::DimensionToSnap snap_dim;
360 if (guide_normal == component_vectors[NR::Y]) {
361 snap_dim = Inkscape::ObjectSnapper::SNAP_Y;
362 } else if (guide_normal == component_vectors[NR::X]) {
363 snap_dim = Inkscape::ObjectSnapper::SNAP_X;
364 } else {
365 g_warning("WARNING: snapping of angled guides is not supported yet!");
366 snap_dim = Inkscape::ObjectSnapper::SNAP_XY;
367 }
369 return object.guideSnap(p, snap_dim);
370 }
373 /**
374 * Main internal snapping method, which is called by the other, friendlier, public
375 * methods. It's a bit hairy as it has lots of parameters, but it saves on a lot
376 * of duplicated code.
377 *
378 * \param type Type of points being snapped.
379 * \param points List of points to snap.
380 * \param ignore List of items to ignore while snapping.
381 * \param constrained true if the snap is constrained.
382 * \param constraint Constraint line to use, if `constrained' is true, otherwise undefined.
383 * \param transformation_type Type of transformation to apply to points before trying to snap them.
384 * \param transformation Description of the transformation; details depend on the type.
385 * \param origin Origin of the transformation, if applicable.
386 * \param dim Dimension of the transformation, if applicable.
387 * \param uniform true if the transformation should be uniform, if applicable.
388 */
390 std::pair<NR::Point, bool> SnapManager::_snapTransformed(
391 Inkscape::Snapper::PointType type,
392 std::vector<NR::Point> const &points,
393 std::list<SPItem const *> const &ignore,
394 bool constrained,
395 Inkscape::Snapper::ConstraintLine const &constraint,
396 Transformation transformation_type,
397 NR::Point const &transformation,
398 NR::Point const &origin,
399 NR::Dim2 dim,
400 bool uniform) const
401 {
402 /* We have a list of points, which we are proposing to transform in some way. We need to see
403 ** if any of these points, when transformed, snap to anything. If they do, we return the
404 ** appropriate transformation with `true'; otherwise we return the original scale with `false'.
405 */
407 /* Quick check to see if we have any snappers that are enabled */
408 if (SomeSnapperMightSnap() == false) {
409 return std::make_pair(transformation, false);
410 }
412 std::vector<NR::Point> transformed_points;
414 for (std::vector<NR::Point>::const_iterator i = points.begin(); i != points.end(); i++) {
416 /* Work out the transformed version of this point */
417 NR::Point transformed;
418 switch (transformation_type) {
419 case TRANSLATION:
420 transformed = *i + transformation;
421 break;
422 case SCALE:
423 transformed = ((*i - origin) * NR::scale(transformation[NR::X], transformation[NR::Y])) + origin;
424 break;
425 case STRETCH:
426 {
427 NR::scale s(1, 1);
428 if (uniform)
429 s[NR::X] = s[NR::Y] = transformation[dim];
430 else {
431 s[dim] = transformation[dim];
432 s[1 - dim] = 1;
433 }
434 transformed = ((*i - origin) * s) + origin;
435 break;
436 }
437 case SKEW:
438 transformed = *i;
439 transformed[dim] += transformation[dim] * ((*i)[1 - dim] - origin[1 - dim]);
440 break;
441 default:
442 g_assert_not_reached();
443 }
445 // add the current transformed point to the box hulling all transformed points
446 transformed_points.push_back(transformed);
447 }
449 /* The current best transformation */
450 NR::Point best_transformation = transformation;
452 /* The current best metric for the best transformation; lower is better, NR_HUGE
453 ** means that we haven't snapped anything.
454 */
455 double best_metric = NR_HUGE;
457 std::vector<NR::Point>::const_iterator j = transformed_points.begin();
459 for (std::vector<NR::Point>::const_iterator i = points.begin(); i != points.end(); i++) {
461 /* Snap it */
462 Inkscape::SnappedPoint const snapped = constrained ?
463 constrainedSnap(type, *j, i == points.begin(), transformed_points, constraint, ignore) : freeSnap(type, *j, i == points.begin(), transformed_points, ignore);
465 if (snapped.getDistance() < NR_HUGE) {
466 /* We snapped. Find the transformation that describes where the snapped point has
467 ** ended up, and also the metric for this transformation.
468 */
469 NR::Point result;
470 NR::Coord metric;
471 switch (transformation_type) {
472 case TRANSLATION:
473 result = snapped.getPoint() - *i;
474 metric = NR::L2(result);
475 break;
476 case SCALE:
477 {
478 NR::Point const a = (snapped.getPoint() - origin);
479 NR::Point const b = (*i - origin);
480 result = NR::Point(a[NR::X] / b[NR::X], a[NR::Y] / b[NR::Y]);
481 metric = std::abs(NR::L2(result) - NR::L2(transformation));
482 break;
483 }
484 case STRETCH:
485 {
486 for (int j = 0; j < 2; j++) {
487 if (uniform || j == dim) {
488 result[j] = (snapped.getPoint()[dim] - origin[dim]) / ((*i)[dim] - origin[dim]);
489 } else {
490 result[j] = 1;
491 }
492 }
493 metric = std::abs(result[dim] - transformation[dim]);
494 break;
495 }
496 case SKEW:
497 result[dim] = (snapped.getPoint()[dim] - (*i)[dim]) / ((*i)[1 - dim] - origin[1 - dim]);
498 metric = std::abs(result[dim] - transformation[dim]);
499 break;
500 default:
501 g_assert_not_reached();
502 }
504 /* Note it if it's the best so far */
505 if (metric < best_metric) {
506 best_transformation = result;
507 best_metric = metric;
508 }
509 }
511 j++;
512 }
514 // Using " < 1e6" instead of " < NR::HUGE" for catching some rounding errors
515 // These rounding errors might be caused by NRRects, see bug #1584301
516 return std::make_pair(best_transformation, best_metric < 1e6);
517 }
520 /**
521 * Try to snap a list of points to any interested snappers after they have undergone
522 * a translation.
523 *
524 * \param t Type of points.
525 * \param p Points.
526 * \param it List of items to ignore when snapping.
527 * \param tr Proposed translation.
528 * \return Snapped translation, if a snap occurred, and a flag indicating whether a snap occurred.
529 */
531 std::pair<NR::Point, bool> SnapManager::freeSnapTranslation(Inkscape::Snapper::PointType t,
532 std::vector<NR::Point> const &p,
533 std::list<SPItem const *> const &it,
534 NR::Point const &tr) const
535 {
536 return _snapTransformed(
537 t, p, it, false, NR::Point(), TRANSLATION, tr, NR::Point(), NR::X, false
538 );
539 }
542 /**
543 * Try to snap a list of points to any interested snappers after they have undergone a
544 * translation. A snap will only occur along a line described by a
545 * Inkscape::Snapper::ConstraintLine.
546 *
547 * \param t Type of points.
548 * \param p Points.
549 * \param it List of items to ignore when snapping.
550 * \param c Constraint line.
551 * \param tr Proposed translation.
552 * \return Snapped translation, if a snap occurred, and a flag indicating whether a snap occurred.
553 */
555 std::pair<NR::Point, bool> SnapManager::constrainedSnapTranslation(Inkscape::Snapper::PointType t,
556 std::vector<NR::Point> const &p,
557 std::list<SPItem const *> const &it,
558 Inkscape::Snapper::ConstraintLine const &c,
559 NR::Point const &tr) const
560 {
561 return _snapTransformed(
562 t, p, it, true, c, TRANSLATION, tr, NR::Point(), NR::X, false
563 );
564 }
567 /**
568 * Try to snap a list of points to any interested snappers after they have undergone
569 * a scale.
570 *
571 * \param t Type of points.
572 * \param p Points.
573 * \param it List of items to ignore when snapping.
574 * \param s Proposed scale.
575 * \param o Origin of proposed scale.
576 * \return Snapped scale, if a snap occurred, and a flag indicating whether a snap occurred.
577 */
579 std::pair<NR::scale, bool> SnapManager::freeSnapScale(Inkscape::Snapper::PointType t,
580 std::vector<NR::Point> const &p,
581 std::list<SPItem const *> const &it,
582 NR::scale const &s,
583 NR::Point const &o) const
584 {
585 return _snapTransformed(
586 t, p, it, false, NR::Point(), SCALE, NR::Point(s[NR::X], s[NR::Y]), o, NR::X, false
587 );
588 }
591 /**
592 * Try to snap a list of points to any interested snappers after they have undergone
593 * a scale. A snap will only occur along a line described by a
594 * Inkscape::Snapper::ConstraintLine.
595 *
596 * \param t Type of points.
597 * \param p Points.
598 * \param it List of items to ignore when snapping.
599 * \param s Proposed scale.
600 * \param o Origin of proposed scale.
601 * \return Snapped scale, if a snap occurred, and a flag indicating whether a snap occurred.
602 */
604 std::pair<NR::scale, bool> SnapManager::constrainedSnapScale(Inkscape::Snapper::PointType t,
605 std::vector<NR::Point> const &p,
606 std::list<SPItem const *> const &it,
607 Inkscape::Snapper::ConstraintLine const &c,
608 NR::scale const &s,
609 NR::Point const &o) const
610 {
611 return _snapTransformed(
612 t, p, it, true, c, SCALE, NR::Point(s[NR::X], s[NR::Y]), o, NR::X, false
613 );
614 }
617 /**
618 * Try to snap a list of points to any interested snappers after they have undergone
619 * a stretch.
620 *
621 * \param t Type of points.
622 * \param p Points.
623 * \param it List of items to ignore when snapping.
624 * \param s Proposed stretch.
625 * \param o Origin of proposed stretch.
626 * \param d Dimension in which to apply proposed stretch.
627 * \param u true if the stretch should be uniform (ie to be applied equally in both dimensions)
628 * \return Snapped stretch, if a snap occurred, and a flag indicating whether a snap occurred.
629 */
631 std::pair<NR::Coord, bool> SnapManager::freeSnapStretch(Inkscape::Snapper::PointType t,
632 std::vector<NR::Point> const &p,
633 std::list<SPItem const *> const &it,
634 NR::Coord const &s,
635 NR::Point const &o,
636 NR::Dim2 d,
637 bool u) const
638 {
639 std::pair<NR::Point, bool> const r = _snapTransformed(
640 t, p, it, false, NR::Point(), STRETCH, NR::Point(s, s), o, d, u
641 );
643 return std::make_pair(r.first[d], r.second);
644 }
647 /**
648 * Try to snap a list of points to any interested snappers after they have undergone
649 * a skew.
650 *
651 * \param t Type of points.
652 * \param p Points.
653 * \param it List of items to ignore when snapping.
654 * \param s Proposed skew.
655 * \param o Origin of proposed skew.
656 * \param d Dimension in which to apply proposed skew.
657 * \return Snapped skew, if a snap occurred, and a flag indicating whether a snap occurred.
658 */
660 std::pair<NR::Coord, bool> SnapManager::freeSnapSkew(Inkscape::Snapper::PointType t,
661 std::vector<NR::Point> const &p,
662 std::list<SPItem const *> const &it,
663 NR::Coord const &s,
664 NR::Point const &o,
665 NR::Dim2 d) const
666 {
667 std::pair<NR::Point, bool> const r = _snapTransformed(
668 t, p, it, false, NR::Point(), SKEW, NR::Point(s, s), o, d, false
669 );
671 return std::make_pair(r.first[d], r.second);
672 }
674 /*
675 Local Variables:
676 mode:c++
677 c-file-style:"stroustrup"
678 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
679 indent-tabs-mode:nil
680 fill-column:99
681 End:
682 */
683 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :