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-2002 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"
27 #include <libnr/nr-point-fns.h>
28 #include <libnr/nr-scale-ops.h>
29 #include <libnr/nr-values.h>
31 #include "display/canvas-grid.h"
33 #include "inkscape.h"
34 #include "desktop.h"
35 #include "sp-guide.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(v, 0),
46 object(v, 0),
47 _named_view(v),
48 _include_item_center(false),
49 _snap_enabled_globally(true)
50 {
51 }
54 /**
55 * \return List of snappers that we use.
56 */
57 SnapManager::SnapperList
58 SnapManager::getSnappers() const
59 {
60 SnapManager::SnapperList s;
61 s.push_back(&guide);
62 s.push_back(&object);
64 SnapManager::SnapperList gs = getGridSnappers();
65 s.splice(s.begin(), gs);
67 return s;
68 }
70 /**
71 * \return List of gridsnappers that we use.
72 */
73 SnapManager::SnapperList
74 SnapManager::getGridSnappers() const
75 {
76 SnapperList s;
78 //FIXME: this code should actually do this: add new grid snappers that are active for this desktop. now it just adds all gridsnappers
79 SPDesktop* desktop = SP_ACTIVE_DESKTOP;
80 if (desktop && desktop->gridsEnabled()) {
81 for ( GSList const *l = _named_view->grids; l != NULL; l = l->next) {
82 Inkscape::CanvasGrid *grid = (Inkscape::CanvasGrid*) l->data;
83 s.push_back(grid->snapper);
84 }
85 }
87 return s;
88 }
90 /**
91 * \return true if one of the snappers will try to snap something.
92 */
94 bool SnapManager::SomeSnapperMightSnap() const
95 {
96 if (!_snap_enabled_globally) {
97 return false;
98 }
100 SnapperList const s = getSnappers();
101 SnapperList::const_iterator i = s.begin();
102 while (i != s.end() && (*i)->ThisSnapperMightSnap() == false) {
103 i++;
104 }
106 return (i != s.end());
107 }
109 /*
110 * The snappers have too many parameters to adjust individually. Therefore only
111 * two snapping modes are presented to the user: snapping bounding box corners (to
112 * other bounding boxes, grids or guides), and/or snapping nodes (to other nodes,
113 * paths, grids or guides). To select either of these modes (or both), use the
114 * methods defined below: setSnapModeBBox() and setSnapModeNode().
115 *
116 * */
119 void SnapManager::setSnapModeBBox(bool enabled)
120 {
121 //The default values are being set in sp_namedview_set() (in sp-namedview.cpp)
122 guide.setSnapFrom(Inkscape::Snapper::SNAPPOINT_BBOX, enabled);
124 for ( GSList const *l = _named_view->grids; l != NULL; l = l->next) {
125 Inkscape::CanvasGrid *grid = (Inkscape::CanvasGrid*) l->data;
126 grid->snapper->setSnapFrom(Inkscape::Snapper::SNAPPOINT_BBOX, enabled);
127 }
129 object.setSnapFrom(Inkscape::Snapper::SNAPPOINT_BBOX, enabled);
130 //object.setSnapToBBoxNode(enabled); // On second thought, these should be controlled
131 //object.setSnapToBBoxPath(enabled); // separately by the snapping prefs dialog
132 object.setStrictSnapping(true); //don't snap bboxes to nodes/paths and vice versa
133 }
135 bool SnapManager::getSnapModeBBox() const
136 {
137 return guide.getSnapFrom(Inkscape::Snapper::SNAPPOINT_BBOX);
138 }
140 void SnapManager::setSnapModeNode(bool enabled)
141 {
142 guide.setSnapFrom(Inkscape::Snapper::SNAPPOINT_NODE, enabled);
144 for ( GSList const *l = _named_view->grids; l != NULL; l = l->next) {
145 Inkscape::CanvasGrid *grid = (Inkscape::CanvasGrid*) l->data;
146 grid->snapper->setSnapFrom(Inkscape::Snapper::SNAPPOINT_NODE, enabled);
147 }
149 object.setSnapFrom(Inkscape::Snapper::SNAPPOINT_NODE, enabled);
150 //object.setSnapToItemNode(enabled); // On second thought, these should be controlled
151 //object.setSnapToItemPath(enabled); // separately by the snapping prefs dialog
152 object.setStrictSnapping(true);
153 }
155 bool SnapManager::getSnapModeNode() const
156 {
157 return guide.getSnapFrom(Inkscape::Snapper::SNAPPOINT_NODE);
158 }
160 void SnapManager::setSnapModeGuide(bool enabled)
161 {
162 object.setSnapFrom(Inkscape::Snapper::SNAPPOINT_GUIDE, enabled);
163 }
165 bool SnapManager::getSnapModeGuide() const
166 {
167 return object.getSnapFrom(Inkscape::Snapper::SNAPPOINT_GUIDE);
168 }
170 /**
171 * Try to snap a point to any interested snappers.
172 *
173 * \param t Type of point.
174 * \param p Point.
175 * \param it Item to ignore when snapping.
176 * \return Snapped point.
177 */
179 Inkscape::SnappedPoint SnapManager::freeSnap(Inkscape::Snapper::PointType t,
180 NR::Point const &p,
181 SPItem const *it) const
183 {
184 std::list<SPItem const *> lit;
185 lit.push_back(it);
187 std::vector<NR::Point> points_to_snap;
188 points_to_snap.push_back(p);
190 return freeSnap(t, p, true, points_to_snap, lit);
191 }
193 /**
194 * Try to snap a point to any of the specified snappers.
195 *
196 * \param t Type of point.
197 * \param p Point.
198 * \param first_point If true then this point is the first one from a whole bunch of points
199 * \param points_to_snap The whole bunch of points, all from the same selection and having the same transformation
200 * \param it List of items to ignore when snapping.
201 * \param snappers List of snappers to try to snap to
202 * \return Snapped point.
203 */
205 Inkscape::SnappedPoint SnapManager::freeSnap(Inkscape::Snapper::PointType t,
206 NR::Point const &p,
207 bool const &first_point,
208 std::vector<NR::Point> &points_to_snap,
209 std::list<SPItem const *> const &it) const
210 {
211 if (!SomeSnapperMightSnap()) {
212 return Inkscape::SnappedPoint(p, NR_HUGE, 0, false);
213 }
215 SnappedConstraints sc;
217 SnapperList const snappers = getSnappers();
219 for (SnapperList::const_iterator i = snappers.begin(); i != snappers.end(); i++) {
220 (*i)->freeSnap(sc, t, p, first_point, points_to_snap, it);
221 }
223 return findBestSnap(p, sc, false);
224 }
226 /**
227 * Try to snap a point to any interested snappers. A snap will only occur along
228 * a line described by a Inkscape::Snapper::ConstraintLine.
229 *
230 * \param t Type of point.
231 * \param p Point.
232 * \param c Constraint line.
233 * \param it Item to ignore when snapping.
234 * \return Snapped point.
235 */
237 Inkscape::SnappedPoint SnapManager::constrainedSnap(Inkscape::Snapper::PointType t,
238 NR::Point const &p,
239 Inkscape::Snapper::ConstraintLine const &c,
240 SPItem const *it) const
241 {
242 std::list<SPItem const *> lit;
243 lit.push_back(it);
245 std::vector<NR::Point> points_to_snap;
246 points_to_snap.push_back(p);
248 return constrainedSnap(t, p, true, points_to_snap, c, lit);
249 }
253 /**
254 * Try to snap a point to any interested snappers. A snap will only occur along
255 * a line described by a Inkscape::Snapper::ConstraintLine.
256 *
257 * \param t Type of point.
258 * \param p Point.
259 * \param first_point If true then this point is the first one from a whole bunch of points
260 * \param points_to_snap The whole bunch of points, all from the same selection and having the same transformation
261 * \param c Constraint line.
262 * \param it List of items to ignore when snapping.
263 * \return Snapped point.
264 */
266 Inkscape::SnappedPoint SnapManager::constrainedSnap(Inkscape::Snapper::PointType t,
267 NR::Point const &p,
268 bool const &first_point,
269 std::vector<NR::Point> &points_to_snap,
270 Inkscape::Snapper::ConstraintLine const &c,
271 std::list<SPItem const *> const &it) const
272 {
273 if (!SomeSnapperMightSnap()) {
274 return Inkscape::SnappedPoint(p, NR_HUGE, 0, false);
275 }
277 SnappedConstraints sc;
279 SnapperList const snappers = getSnappers();
280 for (SnapperList::const_iterator i = snappers.begin(); i != snappers.end(); i++) {
281 (*i)->constrainedSnap(sc, t, p, first_point, points_to_snap, c, it);
282 }
284 return findBestSnap(p, sc, true);
285 }
287 Inkscape::SnappedPoint SnapManager::guideSnap(NR::Point const &p,
288 NR::Point const &guide_normal) const
289 {
290 // This method is used to snap a guide to nodes, while dragging the guide around
292 if (!(object.GuidesMightSnap() && _snap_enabled_globally)) {
293 return Inkscape::SnappedPoint(p, NR_HUGE, 0, false);
294 }
296 SnappedConstraints sc;
297 object.guideSnap(sc, p, guide_normal);
299 return findBestSnap(p, sc, false);
300 }
303 /**
304 * Main internal snapping method, which is called by the other, friendlier, public
305 * methods. It's a bit hairy as it has lots of parameters, but it saves on a lot
306 * of duplicated code.
307 *
308 * \param type Type of points being snapped.
309 * \param points List of points to snap.
310 * \param ignore List of items to ignore while snapping.
311 * \param constrained true if the snap is constrained.
312 * \param constraint Constraint line to use, if `constrained' is true, otherwise undefined.
313 * \param transformation_type Type of transformation to apply to points before trying to snap them.
314 * \param transformation Description of the transformation; details depend on the type.
315 * \param origin Origin of the transformation, if applicable.
316 * \param dim Dimension of the transformation, if applicable.
317 * \param uniform true if the transformation should be uniform; only applicable for stretching and scaling.
318 */
320 std::pair<NR::Point, bool> SnapManager::_snapTransformed(
321 Inkscape::Snapper::PointType type,
322 std::vector<NR::Point> const &points,
323 std::list<SPItem const *> const &ignore,
324 bool constrained,
325 Inkscape::Snapper::ConstraintLine const &constraint,
326 Transformation transformation_type,
327 NR::Point const &transformation,
328 NR::Point const &origin,
329 NR::Dim2 dim,
330 bool uniform) const
331 {
332 /* We have a list of points, which we are proposing to transform in some way. We need to see
333 ** if any of these points, when transformed, snap to anything. If they do, we return the
334 ** appropriate transformation with `true'; otherwise we return the original scale with `false'.
335 */
337 /* Quick check to see if we have any snappers that are enabled
338 ** Also used to globally disable all snapping
339 */
340 if (SomeSnapperMightSnap() == false) {
341 return std::make_pair(transformation, false);
342 }
344 std::vector<NR::Point> transformed_points;
346 for (std::vector<NR::Point>::const_iterator i = points.begin(); i != points.end(); i++) {
348 /* Work out the transformed version of this point */
349 NR::Point transformed;
350 switch (transformation_type) {
351 case TRANSLATION:
352 transformed = *i + transformation;
353 break;
354 case SCALE:
355 transformed = ((*i - origin) * NR::scale(transformation[NR::X], transformation[NR::Y])) + origin;
356 break;
357 case STRETCH:
358 {
359 NR::scale s(1, 1);
360 if (uniform)
361 s[NR::X] = s[NR::Y] = transformation[dim];
362 else {
363 s[dim] = transformation[dim];
364 s[1 - dim] = 1;
365 }
366 transformed = ((*i - origin) * s) + origin;
367 break;
368 }
369 case SKEW:
370 transformed = *i;
371 transformed[dim] += transformation[dim] * ((*i)[1 - dim] - origin[1 - dim]);
372 break;
373 default:
374 g_assert_not_reached();
375 }
377 // add the current transformed point to the box hulling all transformed points
378 transformed_points.push_back(transformed);
379 }
381 /* The current best transformation */
382 NR::Point best_transformation = transformation;
384 /* The current best metric for the best transformation; lower is better, NR_HUGE
385 ** means that we haven't snapped anything.
386 */
387 NR::Coord best_metric = NR_HUGE;
388 NR::Coord best_second_metric = NR_HUGE;
389 NR::Point best_scale_metric(NR_HUGE, NR_HUGE);
390 bool best_at_intersection = false;
391 bool best_always_snap = false;
393 std::vector<NR::Point>::const_iterator j = transformed_points.begin();
395 //std::cout << std::endl;
397 for (std::vector<NR::Point>::const_iterator i = points.begin(); i != points.end(); i++) {
399 /* Snap it */
400 Inkscape::SnappedPoint snapped;
402 if (constrained) {
403 Inkscape::Snapper::ConstraintLine dedicated_constraint = constraint;
404 if (transformation_type == SCALE && uniform) {
405 // When uniformly scaling, each point will have its own unique constraint line,
406 // running from the scaling origin to the original untransformed point. We will
407 // calculate that line here
408 dedicated_constraint = Inkscape::Snapper::ConstraintLine(origin, (*i) - origin);
409 } // else: leave the original constraint, e.g. for constrained translation
410 if (transformation_type == SCALE && !uniform) {
411 g_warning("Non-uniform constrained scaling is not supported!");
412 }
413 snapped = constrainedSnap(type, *j, i == points.begin(), transformed_points, dedicated_constraint, ignore);
414 } else {
415 snapped = freeSnap(type, *j, i == points.begin(), transformed_points, ignore);
416 }
418 NR::Point result;
419 NR::Coord metric = NR_HUGE;
420 NR::Coord second_metric = NR_HUGE;
421 NR::Point scale_metric(NR_HUGE, NR_HUGE);
423 if (snapped.getDistance() < NR_HUGE) {
424 /* We snapped. Find the transformation that describes where the snapped point has
425 ** ended up, and also the metric for this transformation.
426 */
427 switch (transformation_type) {
428 case TRANSLATION:
429 result = snapped.getPoint() - *i;
430 /* Consider the case in which a box is almost aligned with a grid in both
431 * horizontal and vertical directions. The distance to the intersection of
432 * the grid lines will always be larger then the distance to a single grid
433 * line. If we prefer snapping to an intersection instead of to a single
434 * grid line, then we cannot use "metric = NR::L2(result)". Therefore the
435 * snapped distance will be used as a metric. Please note that the snapped
436 * distance is defined as the distance to the nearest line of the intersection,
437 * and not to the intersection itself!
438 */
439 metric = snapped.getDistance(); //used to be: metric = NR::L2(result);
440 second_metric = snapped.getSecondDistance();
441 break;
442 case SCALE:
443 {
444 NR::Point const a = (snapped.getPoint() - origin); // vector to snapped point
445 NR::Point const b = (*i - origin); // vector to original point
446 result = NR::Point(NR_HUGE, NR_HUGE);
447 // If this point *i is horizontally or vertically aligned with
448 // the origin of the scaling, then it will scale purely in X or Y
449 // We can therefore only calculate the scaling in this direction
450 // and the scaling factor for the other direction should remain
451 // untouched (unless scaling is uniform ofcourse)
452 for (int index = 0; index < 2; index++) {
453 if (fabs(b[index]) > 1e-6) { // if SCALING CAN occur in this direction
454 if (fabs(fabs(a[index]/b[index]) - fabs(transformation[index])) > 1e-12) { // if SNAPPING DID occur in this direction
455 result[index] = a[index] / b[index]; // then calculate it!
456 }
457 // we might leave result[1-index] = NR_HUGE
458 // if scaling didn't occur in the other direction
459 }
460 }
462 // Compare the resulting scaling with the desired scaling
463 scale_metric = result - transformation; // One or both of its components might be NR_HUGE
464 break;
465 }
466 case STRETCH:
467 for (int index = 0; index < 2; index++) {
468 if (uniform || index == dim) {
469 result[index] = (snapped.getPoint()[dim] - origin[dim]) / ((*i)[dim] - origin[dim]);
470 } else {
471 result[index] = 1;
472 }
473 }
474 metric = std::abs(result[dim] - transformation[dim]);
475 break;
476 case SKEW:
477 result[dim] = (snapped.getPoint()[dim] - (*i)[dim]) / ((*i)[1 - dim] - origin[1 - dim]);
478 metric = std::abs(result[dim] - transformation[dim]);
479 break;
480 default:
481 g_assert_not_reached();
482 }
484 /* Note it if it's the best so far */
485 if (transformation_type == SCALE) {
486 for (int index = 0; index < 2; index++) {
487 if (fabs(scale_metric[index]) < fabs(best_scale_metric[index])) {
488 best_transformation[index] = result[index];
489 best_scale_metric[index] = fabs(scale_metric[index]);
490 //std::cout << "SEL ";
491 } //else { std::cout << " ";}
492 }
493 if (uniform) {
494 if (best_scale_metric[0] < best_scale_metric[1]) {
495 best_transformation[1] = best_transformation[0];
496 best_scale_metric[1] = best_scale_metric[0];
497 } else {
498 best_transformation[0] = best_transformation[1];
499 best_scale_metric[0] = best_scale_metric[1];
500 }
501 }
502 best_metric = std::min(best_scale_metric[0], best_scale_metric[1]);
503 //std::cout << "P_orig = " << (*i) << " | scale_metric = " << scale_metric << " | distance = " << snapped.getDistance() << " | P_snap = " << snapped.getPoint() << std::endl;
504 } else {
505 bool const c1 = metric < best_metric;
506 bool const c2 = metric == best_metric && snapped.getAtIntersection() == true && best_at_intersection == false;
507 bool const c3a = metric == best_metric && snapped.getAtIntersection() == true && best_at_intersection == true;
508 bool const c3b = second_metric < best_second_metric;
509 bool const c4 = snapped.getAlwaysSnap() == true && best_always_snap == false;
510 bool const c4n = snapped.getAlwaysSnap() == false && best_always_snap == true;
512 if ((c1 || c2 || (c3a && c3b) || c4) && !c4n) {
513 best_transformation = result;
514 best_metric = metric;
515 best_second_metric = second_metric;
516 best_at_intersection = snapped.getAtIntersection();
517 best_always_snap = snapped.getAlwaysSnap();
518 //std::cout << "SEL ";
519 } //else { std::cout << " ";}
520 //std::cout << "P_orig = " << (*i) << " | metric = " << metric << " | distance = " << snapped.getDistance() << " | second metric = " << second_metric << " | P_snap = " << snapped.getPoint() << std::endl;
521 }
522 }
525 j++;
526 }
528 if (transformation_type == SCALE) {
529 // When scaling, don't ever exit with one of scaling components set to NR_HUGE
530 if (best_transformation == NR::Point(NR_HUGE, NR_HUGE)) {
531 best_transformation == transformation; // return the original (i.e. un-snapped) transformation
532 } else {
533 // Still one of the transformation components could be NR_HUGE
534 for (int index = 0; index < 2; index++) {
535 if (best_transformation[index] == NR_HUGE) {
536 best_transformation[index] == uniform ? best_transformation[1-index] : transformation[index];
537 }
538 }
539 }
540 }
542 // Using " < 1e6" instead of " < NR_HUGE" for catching some rounding errors
543 // These rounding errors might be caused by NRRects, see bug #1584301
544 return std::make_pair(best_transformation, best_metric < 1e6);
545 }
548 /**
549 * Try to snap a list of points to any interested snappers after they have undergone
550 * a translation.
551 *
552 * \param t Type of points.
553 * \param p Points.
554 * \param it List of items to ignore when snapping.
555 * \param tr Proposed translation.
556 * \return Snapped translation, if a snap occurred, and a flag indicating whether a snap occurred.
557 */
559 std::pair<NR::Point, bool> SnapManager::freeSnapTranslation(Inkscape::Snapper::PointType t,
560 std::vector<NR::Point> const &p,
561 std::list<SPItem const *> const &it,
562 NR::Point const &tr) const
563 {
564 return _snapTransformed(
565 t, p, it, false, NR::Point(), TRANSLATION, tr, NR::Point(), NR::X, false
566 );
567 }
570 /**
571 * Try to snap a list of points to any interested snappers after they have undergone a
572 * translation. A snap will only occur along a line described by a
573 * Inkscape::Snapper::ConstraintLine.
574 *
575 * \param t Type of points.
576 * \param p Points.
577 * \param it List of items to ignore when snapping.
578 * \param c Constraint line.
579 * \param tr Proposed translation.
580 * \return Snapped translation, if a snap occurred, and a flag indicating whether a snap occurred.
581 */
583 std::pair<NR::Point, bool> SnapManager::constrainedSnapTranslation(Inkscape::Snapper::PointType t,
584 std::vector<NR::Point> const &p,
585 std::list<SPItem const *> const &it,
586 Inkscape::Snapper::ConstraintLine const &c,
587 NR::Point const &tr) const
588 {
589 return _snapTransformed(
590 t, p, it, true, c, TRANSLATION, tr, NR::Point(), NR::X, false
591 );
592 }
595 /**
596 * Try to snap a list of points to any interested snappers after they have undergone
597 * a scale.
598 *
599 * \param t Type of points.
600 * \param p Points.
601 * \param it List of items to ignore when snapping.
602 * \param s Proposed scale.
603 * \param o Origin of proposed scale.
604 * \return Snapped scale, if a snap occurred, and a flag indicating whether a snap occurred.
605 */
607 std::pair<NR::scale, bool> SnapManager::freeSnapScale(Inkscape::Snapper::PointType t,
608 std::vector<NR::Point> const &p,
609 std::list<SPItem const *> const &it,
610 NR::scale const &s,
611 NR::Point const &o) const
612 {
613 return _snapTransformed(
614 t, p, it, false, NR::Point(), SCALE, NR::Point(s[NR::X], s[NR::Y]), o, NR::X, false
615 );
616 }
619 /**
620 * Try to snap a list of points to any interested snappers after they have undergone
621 * a scale. A snap will only occur along a line described by a
622 * Inkscape::Snapper::ConstraintLine.
623 *
624 * \param t Type of points.
625 * \param p Points.
626 * \param it List of items to ignore when snapping.
627 * \param s Proposed scale.
628 * \param o Origin of proposed scale.
629 * \return Snapped scale, if a snap occurred, and a flag indicating whether a snap occurred.
630 */
632 std::pair<NR::scale, bool> SnapManager::constrainedSnapScale(Inkscape::Snapper::PointType t,
633 std::vector<NR::Point> const &p,
634 std::list<SPItem const *> const &it,
635 NR::scale const &s,
636 NR::Point const &o) const
637 {
638 // When constrained scaling, only uniform scaling is supported.
639 return _snapTransformed(
640 t, p, it, true, NR::Point(), SCALE, NR::Point(s[NR::X], s[NR::Y]), o, NR::X, true
641 );
642 }
645 /**
646 * Try to snap a list of points to any interested snappers after they have undergone
647 * a stretch.
648 *
649 * \param t Type of points.
650 * \param p Points.
651 * \param it List of items to ignore when snapping.
652 * \param s Proposed stretch.
653 * \param o Origin of proposed stretch.
654 * \param d Dimension in which to apply proposed stretch.
655 * \param u true if the stretch should be uniform (ie to be applied equally in both dimensions)
656 * \return Snapped stretch, if a snap occurred, and a flag indicating whether a snap occurred.
657 */
659 std::pair<NR::Coord, bool> SnapManager::freeSnapStretch(Inkscape::Snapper::PointType t,
660 std::vector<NR::Point> const &p,
661 std::list<SPItem const *> const &it,
662 NR::Coord const &s,
663 NR::Point const &o,
664 NR::Dim2 d,
665 bool u) const
666 {
667 std::pair<NR::Point, bool> const r = _snapTransformed(
668 t, p, it, false, NR::Point(), STRETCH, NR::Point(s, s), o, d, u
669 );
671 return std::make_pair(r.first[d], r.second);
672 }
675 /**
676 * Try to snap a list of points to any interested snappers after they have undergone
677 * a skew.
678 *
679 * \param t Type of points.
680 * \param p Points.
681 * \param it List of items to ignore when snapping.
682 * \param s Proposed skew.
683 * \param o Origin of proposed skew.
684 * \param d Dimension in which to apply proposed skew.
685 * \return Snapped skew, if a snap occurred, and a flag indicating whether a snap occurred.
686 */
688 std::pair<NR::Coord, bool> SnapManager::freeSnapSkew(Inkscape::Snapper::PointType t,
689 std::vector<NR::Point> const &p,
690 std::list<SPItem const *> const &it,
691 NR::Coord const &s,
692 NR::Point const &o,
693 NR::Dim2 d) const
694 {
695 std::pair<NR::Point, bool> const r = _snapTransformed(
696 t, p, it, false, NR::Point(), SKEW, NR::Point(s, s), o, d, false
697 );
699 return std::make_pair(r.first[d], r.second);
700 }
702 Inkscape::SnappedPoint SnapManager::findBestSnap(NR::Point const &p, SnappedConstraints &sc, bool constrained) const
703 {
704 NR::Coord const guide_tol = guide.getSnapperTolerance();
705 NR::Coord grid_tol = 0;
707 SnapManager::SnapperList const gs = getGridSnappers();
708 SnapperList::const_iterator i = gs.begin();
709 if (i != gs.end()) {
710 grid_tol = (*i)->getSnapperTolerance(); // there's only a single tolerance, equal for all grids
711 }
713 // Store all snappoints
714 std::list<Inkscape::SnappedPoint> sp_list;
715 // Most of these snapped points are already within the snapping range, because
716 // they have already been filtered by their respective snappers. In that case
717 // we can set the snapping range to NR_HUGE here. If however we're looking at
718 // intersections of e.g. a grid and guide line, then we'll have to determine
719 // once again whether we're within snapping range. In this case we will set
720 // the snapping range to e.g. min(guide_sens, grid_tol)
722 // search for the closest snapped point
723 Inkscape::SnappedPoint closestPoint;
724 if (getClosestSP(sc.points, closestPoint)) {
725 sp_list.push_back(closestPoint);
726 }
728 // search for the closest snapped line segment
729 Inkscape::SnappedLineSegment closestLineSegment;
730 if (getClosestSLS(sc.lines, closestLineSegment)) {
731 sp_list.push_back(Inkscape::SnappedPoint(closestLineSegment));
732 }
734 if (_intersectionLS) {
735 // search for the closest snapped intersection of line segments
736 Inkscape::SnappedPoint closestLineSegmentIntersection;
737 if (getClosestIntersectionSLS(sc.lines, closestLineSegmentIntersection)) {
738 sp_list.push_back(closestLineSegmentIntersection);
739 }
740 }
742 // search for the closest snapped grid line
743 Inkscape::SnappedLine closestGridLine;
744 if (getClosestSL(sc.grid_lines, closestGridLine)) {
745 sp_list.push_back(Inkscape::SnappedPoint(closestGridLine));
746 }
748 // search for the closest snapped guide line
749 Inkscape::SnappedLine closestGuideLine;
750 if (getClosestSL(sc.guide_lines, closestGuideLine)) {
751 sp_list.push_back(Inkscape::SnappedPoint(closestGuideLine));
752 }
754 // When freely snapping to a grid/guide/path, only one degree of freedom is eliminated
755 // Therefore we will try get fully constrained by finding an intersection with another grid/guide/path
757 // When doing a constrained snap however, we're already at an intersection of the constrained line and
758 // the grid/guide/path we're snapping to. This snappoint is therefore fully constrained, so there's
759 // no need to look for additional intersections
760 if (!constrained) {
761 // search for the closest snapped intersection of grid lines
762 Inkscape::SnappedPoint closestGridPoint;
763 if (getClosestIntersectionSL(sc.grid_lines, closestGridPoint)) {
764 sp_list.push_back(closestGridPoint);
765 }
767 // search for the closest snapped intersection of guide lines
768 Inkscape::SnappedPoint closestGuidePoint;
769 if (getClosestIntersectionSL(sc.guide_lines, closestGuidePoint)) {
770 sp_list.push_back(closestGuidePoint);
771 }
773 // search for the closest snapped intersection of grid with guide lines
774 if (_intersectionGG) {
775 Inkscape::SnappedPoint closestGridGuidePoint;
776 if (getClosestIntersectionSL(sc.grid_lines, sc.guide_lines, closestGridGuidePoint)) {
777 sp_list.push_back(closestGridGuidePoint);
778 }
779 }
780 }
782 // now let's see which snapped point gets a thumbs up
783 Inkscape::SnappedPoint bestSnappedPoint = Inkscape::SnappedPoint(p, NR_HUGE, 0, false);
784 for (std::list<Inkscape::SnappedPoint>::const_iterator i = sp_list.begin(); i != sp_list.end(); i++) {
785 // first find out if this snapped point is within snapping range
786 if ((*i).getDistance() <= (*i).getTolerance()) {
787 // if it's the first point
788 bool c1 = (i == sp_list.begin());
789 // or, if it's closer
790 bool c2 = (*i).getDistance() < bestSnappedPoint.getDistance();
791 // or, if it's for a snapper with "always snap" turned on, and the previous wasn't
792 bool c3 = (*i).getAlwaysSnap() && !bestSnappedPoint.getAlwaysSnap();
793 // But in no case fall back from a snapper with "always snap" on to one with "always snap" off
794 bool c3n = !(*i).getAlwaysSnap() && bestSnappedPoint.getAlwaysSnap();
795 // or, if it's just as close then consider the second distance
796 // (which is only relevant for points at an intersection)
797 bool c4a = ((*i).getDistance() == bestSnappedPoint.getDistance());
798 bool c4b = (*i).getSecondDistance() < bestSnappedPoint.getSecondDistance();
799 // then prefer this point over the previous one
800 if ((c1 || c2 || c3 || (c4a && c4b)) && !c3n) {
801 bestSnappedPoint = *i;
802 }
803 }
804 }
806 return bestSnappedPoint;
807 }
809 /*
810 Local Variables:
811 mode:c++
812 c-file-style:"stroustrup"
813 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
814 indent-tabs-mode:nil
815 fill-column:99
816 End:
817 */
818 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :