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);
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);
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.ThisSnapperMightSnap() && _snap_enabled_globally)) {
293 return Inkscape::SnappedPoint(p, NR_HUGE);
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, if applicable.
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 bool best_at_intersection = false;
391 std::vector<NR::Point>::const_iterator j = transformed_points.begin();
393 //std::cout << std::endl;
395 for (std::vector<NR::Point>::const_iterator i = points.begin(); i != points.end(); i++) {
397 /* Snap it */
398 Inkscape::SnappedPoint snapped;
400 if (constrained) {
401 Inkscape::Snapper::ConstraintLine dedicated_constraint = constraint;
402 if (transformation_type == SCALE) {
403 // When constrained scaling, each point will have its own unique constraint line,
404 // running from the scaling origin to the original untransformed point. We will
405 // calculate that line here
406 dedicated_constraint = Inkscape::Snapper::ConstraintLine(origin, (*i) - origin);
407 }
408 snapped = constrainedSnap(type, *j, i == points.begin(), transformed_points, dedicated_constraint, ignore);
409 } else {
410 snapped = freeSnap(type, *j, i == points.begin(), transformed_points, ignore);
411 }
413 NR::Point result;
414 NR::Coord metric = NR_HUGE;
415 NR::Coord second_metric = NR_HUGE;
417 if (snapped.getDistance() < NR_HUGE) {
418 /* We snapped. Find the transformation that describes where the snapped point has
419 ** ended up, and also the metric for this transformation.
420 */
421 switch (transformation_type) {
422 case TRANSLATION:
423 result = snapped.getPoint() - *i;
424 /* Consider the case in which a box is almost aligned with a grid in both
425 * horizontal and vertical directions. The distance to the intersection of
426 * the grid lines will always be larger then the distance to a single grid
427 * line. If we prefer snapping to an intersection instead of to a single
428 * grid line, then we cannot use "metric = NR::L2(result)". Therefore the
429 * snapped distance will be used as a metric. Please note that the snapped
430 * distance is defined as the distance to the nearest line of the intersection,
431 * and not to the intersection itself!
432 */
433 metric = snapped.getDistance(); //used to be: metric = NR::L2(result);
434 second_metric = snapped.getSecondDistance();
435 break;
436 case SCALE:
437 {
438 NR::Point const a = (snapped.getPoint() - origin);
439 NR::Point const b = (*i - origin);
440 // This is the scaling that results after snapping
441 result = NR::Point(a[NR::X] / b[NR::X], a[NR::Y] / b[NR::Y]);
442 // Compare the resulting scaling with the desired scaling
443 metric = std::abs(NR::L2(result) - NR::L2(transformation));
444 // TODO: (Diederik) This only works for snapping of the diagonals
445 // as the resulting scale cannot be calculated for points aligned
446 // vertically or horizontally to the origin, and therefore the the
447 // metric will also be useless. BTW, what about protection for 1/0?
448 break;
449 }
450 case STRETCH:
451 {
452 for (int a = 0; a < 2; a++) {
453 if (uniform || a == dim) {
454 result[a] = (snapped.getPoint()[dim] - origin[dim]) / ((*i)[dim] - origin[dim]);
455 } else {
456 result[a] = 1;
457 }
458 }
459 metric = std::abs(result[dim] - transformation[dim]);
460 break;
461 }
462 case SKEW:
463 result[dim] = (snapped.getPoint()[dim] - (*i)[dim]) / ((*i)[1 - dim] - origin[1 - dim]);
464 metric = std::abs(result[dim] - transformation[dim]);
465 break;
466 default:
467 g_assert_not_reached();
468 }
470 /* Note it if it's the best so far */
471 bool const c1 = metric < best_metric;
472 bool const c2 = metric == best_metric && snapped.getAtIntersection() == true && best_at_intersection == false;
473 bool const c3a = metric == best_metric && snapped.getAtIntersection() == true && best_at_intersection == true;
474 bool const c3b = second_metric < best_second_metric;
476 if (c1 || c2 || c3a && c3b) {
477 best_transformation = result;
478 best_metric = metric;
479 best_second_metric = second_metric;
480 best_at_intersection = snapped.getAtIntersection();
481 //std::cout << "SEL ";
482 } //else { std::cout << " ";}
483 }
485 //std::cout << "P_orig = " << (*i) << " | metric = " << metric << " | distance = " << snapped.getDistance() << " | second metric = " << second_metric << " | P_snap = " << snapped.getPoint() << std::endl;
486 j++;
487 }
489 // Using " < 1e6" instead of " < NR_HUGE" for catching some rounding errors
490 // These rounding errors might be caused by NRRects, see bug #1584301
491 return std::make_pair(best_transformation, best_metric < 1e6);
492 }
495 /**
496 * Try to snap a list of points to any interested snappers after they have undergone
497 * a translation.
498 *
499 * \param t Type of points.
500 * \param p Points.
501 * \param it List of items to ignore when snapping.
502 * \param tr Proposed translation.
503 * \return Snapped translation, if a snap occurred, and a flag indicating whether a snap occurred.
504 */
506 std::pair<NR::Point, bool> SnapManager::freeSnapTranslation(Inkscape::Snapper::PointType t,
507 std::vector<NR::Point> const &p,
508 std::list<SPItem const *> const &it,
509 NR::Point const &tr) const
510 {
511 return _snapTransformed(
512 t, p, it, false, NR::Point(), TRANSLATION, tr, NR::Point(), NR::X, false
513 );
514 }
517 /**
518 * Try to snap a list of points to any interested snappers after they have undergone a
519 * translation. A snap will only occur along a line described by a
520 * Inkscape::Snapper::ConstraintLine.
521 *
522 * \param t Type of points.
523 * \param p Points.
524 * \param it List of items to ignore when snapping.
525 * \param c Constraint line.
526 * \param tr Proposed translation.
527 * \return Snapped translation, if a snap occurred, and a flag indicating whether a snap occurred.
528 */
530 std::pair<NR::Point, bool> SnapManager::constrainedSnapTranslation(Inkscape::Snapper::PointType t,
531 std::vector<NR::Point> const &p,
532 std::list<SPItem const *> const &it,
533 Inkscape::Snapper::ConstraintLine const &c,
534 NR::Point const &tr) const
535 {
536 return _snapTransformed(
537 t, p, it, true, c, 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
544 * a scale.
545 *
546 * \param t Type of points.
547 * \param p Points.
548 * \param it List of items to ignore when snapping.
549 * \param s Proposed scale.
550 * \param o Origin of proposed scale.
551 * \return Snapped scale, if a snap occurred, and a flag indicating whether a snap occurred.
552 */
554 std::pair<NR::scale, bool> SnapManager::freeSnapScale(Inkscape::Snapper::PointType t,
555 std::vector<NR::Point> const &p,
556 std::list<SPItem const *> const &it,
557 NR::scale const &s,
558 NR::Point const &o) const
559 {
560 return _snapTransformed(
561 t, p, it, false, NR::Point(), SCALE, NR::Point(s[NR::X], s[NR::Y]), o, NR::X, false
562 );
563 }
566 /**
567 * Try to snap a list of points to any interested snappers after they have undergone
568 * a scale. A snap will only occur along a line described by a
569 * Inkscape::Snapper::ConstraintLine.
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::constrainedSnapScale(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, true, 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 stretch.
594 *
595 * \param t Type of points.
596 * \param p Points.
597 * \param it List of items to ignore when snapping.
598 * \param s Proposed stretch.
599 * \param o Origin of proposed stretch.
600 * \param d Dimension in which to apply proposed stretch.
601 * \param u true if the stretch should be uniform (ie to be applied equally in both dimensions)
602 * \return Snapped stretch, if a snap occurred, and a flag indicating whether a snap occurred.
603 */
605 std::pair<NR::Coord, bool> SnapManager::freeSnapStretch(Inkscape::Snapper::PointType t,
606 std::vector<NR::Point> const &p,
607 std::list<SPItem const *> const &it,
608 NR::Coord const &s,
609 NR::Point const &o,
610 NR::Dim2 d,
611 bool u) const
612 {
613 std::pair<NR::Point, bool> const r = _snapTransformed(
614 t, p, it, false, NR::Point(), STRETCH, NR::Point(s, s), o, d, u
615 );
617 return std::make_pair(r.first[d], r.second);
618 }
621 /**
622 * Try to snap a list of points to any interested snappers after they have undergone
623 * a skew.
624 *
625 * \param t Type of points.
626 * \param p Points.
627 * \param it List of items to ignore when snapping.
628 * \param s Proposed skew.
629 * \param o Origin of proposed skew.
630 * \param d Dimension in which to apply proposed skew.
631 * \return Snapped skew, if a snap occurred, and a flag indicating whether a snap occurred.
632 */
634 std::pair<NR::Coord, bool> SnapManager::freeSnapSkew(Inkscape::Snapper::PointType t,
635 std::vector<NR::Point> const &p,
636 std::list<SPItem const *> const &it,
637 NR::Coord const &s,
638 NR::Point const &o,
639 NR::Dim2 d) const
640 {
641 std::pair<NR::Point, bool> const r = _snapTransformed(
642 t, p, it, false, NR::Point(), SKEW, NR::Point(s, s), o, d, false
643 );
645 return std::make_pair(r.first[d], r.second);
646 }
648 Inkscape::SnappedPoint SnapManager::findBestSnap(NR::Point const &p, SnappedConstraints &sc, bool constrained) const
649 {
650 NR::Coord const guide_sens = guide.getDistance();
651 NR::Coord grid_sens = 0;
653 SnapManager::SnapperList const gs = getGridSnappers();
654 SnapperList::const_iterator i = gs.begin();
655 if (i != gs.end()) {
656 grid_sens = (*i)->getDistance();
657 }
659 // Store all snappoints, optionally together with their specific snapping range
660 std::list<std::pair<Inkscape::SnappedPoint, NR::Coord> > sp_list;
661 // Most of these snapped points are already within the snapping range, because
662 // they have already been filtered by their respective snappers. In that case
663 // we can set the snapping range to NR_HUGE here. If however we're looking at
664 // intersections of e.g. a grid and guide line, then we'll have to determine
665 // once again whether we're within snapping range. In this case we will set
666 // the snapping range to e.g. min(guide_sens, grid_sens)
668 // search for the closest snapped point
669 Inkscape::SnappedPoint closestPoint;
670 if (getClosestSP(sc.points, closestPoint)) {
671 sp_list.push_back(std::make_pair(closestPoint, NR_HUGE));
672 }
674 // search for the closest snapped line segment
675 Inkscape::SnappedLineSegment closestLineSegment;
676 if (getClosestSLS(sc.lines, closestLineSegment)) {
677 sp_list.push_back(std::make_pair(Inkscape::SnappedPoint(closestLineSegment), NR_HUGE));
678 }
680 if (_intersectionLS) {
681 // search for the closest snapped intersection of line segments
682 Inkscape::SnappedPoint closestLineSegmentIntersection;
683 if (getClosestIntersectionSLS(sc.lines, closestLineSegmentIntersection)) {
684 sp_list.push_back(std::make_pair(closestLineSegmentIntersection, NR_HUGE));
685 }
686 }
688 // search for the closest snapped grid line
689 Inkscape::SnappedLine closestGridLine;
690 if (getClosestSL(sc.grid_lines, closestGridLine)) {
691 sp_list.push_back(std::make_pair(Inkscape::SnappedPoint(closestGridLine), NR_HUGE));
692 }
694 // search for the closest snapped guide line
695 Inkscape::SnappedLine closestGuideLine;
696 if (getClosestSL(sc.guide_lines, closestGuideLine)) {
697 sp_list.push_back(std::make_pair(Inkscape::SnappedPoint(closestGuideLine), NR_HUGE));
698 }
700 // When freely snapping to a grid/guide/path, only one degree of freedom is eliminated
701 // Therefore we will try get fully constrained by finding an intersection with another grid/guide/path
703 // When doing a constrained snap however, we're already at an intersection of the constrained line and
704 // the grid/guide/path we're snapping to. This snappoint is therefore fully constrained, so there's
705 // no need to look for additional intersections
706 if (!constrained) {
707 // search for the closest snapped intersection of grid lines
708 Inkscape::SnappedPoint closestGridPoint;
709 if (getClosestIntersectionSL(sc.grid_lines, closestGridPoint)) {
710 sp_list.push_back(std::make_pair(closestGridPoint, NR_HUGE));
711 }
713 // search for the closest snapped intersection of guide lines
714 Inkscape::SnappedPoint closestGuidePoint;
715 if (getClosestIntersectionSL(sc.guide_lines, closestGuidePoint)) {
716 sp_list.push_back(std::make_pair(closestGuidePoint, NR_HUGE));
717 }
719 // search for the closest snapped intersection of grid with guide lines
720 if (_intersectionGG) {
721 Inkscape::SnappedPoint closestGridGuidePoint;
722 if (getClosestIntersectionSL(sc.grid_lines, sc.guide_lines, closestGridGuidePoint)) {
723 sp_list.push_back(std::make_pair(closestGridGuidePoint, std::min(guide_sens, grid_sens)));
724 }
725 }
726 }
728 // now let's see which snapped point gets a thumbs up
729 Inkscape::SnappedPoint bestPoint(p, NR_HUGE);
730 for (std::list<std::pair<Inkscape::SnappedPoint, NR::Coord> >::const_iterator i = sp_list.begin(); i != sp_list.end(); i++) {
731 // first find out if this snapped point is within snapping range
732 if ((*i).first.getDistance() <= (*i).second) {
733 // if it's the first point
734 bool c1 = (i == sp_list.begin());
735 // or, if it's closer
736 bool c2 = (*i).first.getDistance() < bestPoint.getDistance();
737 // or, if it's just as close then consider the second distance
738 // (which is only relevant for points at an intersection)
739 bool c3a = ((*i).first.getDistance() == bestPoint.getDistance());
740 bool c3b = (*i).first.getSecondDistance() < bestPoint.getSecondDistance();
741 // then prefer this point over the previous one
742 if (c1 || c2 || c3a && c3b) {
743 bestPoint = (*i).first;
744 }
745 }
746 }
747 return bestPoint;
748 }
750 /*
751 Local Variables:
752 mode:c++
753 c-file-style:"stroustrup"
754 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
755 indent-tabs-mode:nil
756 fill-column:99
757 End:
758 */
759 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :