1a05bdaacf6234079fb148059900136c80e08c0c
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 <utility>
22 #include "sp-namedview.h"
23 #include "snap.h"
24 #include "snapped-line.h"
26 #include <libnr/nr-point-fns.h>
27 #include <libnr/nr-scale-ops.h>
28 #include <libnr/nr-values.h>
30 #include "display/canvas-grid.h"
32 #include "inkscape.h"
33 #include "desktop.h"
34 #include "sp-guide.h"
35 using std::vector;
37 /**
38 * Construct a SnapManager for a SPNamedView.
39 *
40 * \param v `Owning' SPNamedView.
41 */
43 SnapManager::SnapManager(SPNamedView const *v) :
44 guide(v, 0),
45 object(v, 0),
46 _named_view(v),
47 _include_item_center(false)
48 {
50 }
53 /**
54 * \return List of snappers that we use.
55 */
56 SnapManager::SnapperList
57 SnapManager::getSnappers() const
58 {
59 SnapManager::SnapperList s;
60 s.push_back(&guide);
61 s.push_back(&object);
63 SnapManager::SnapperList gs = getGridSnappers();
64 s.splice(s.begin(), gs);
66 return s;
67 }
69 /**
70 * \return List of gridsnappers that we use.
71 */
72 SnapManager::SnapperList
73 SnapManager::getGridSnappers() const
74 {
75 SnapperList s;
77 //FIXME: this code should actually do this: add new grid snappers that are active for this desktop. now it just adds all gridsnappers
78 SPDesktop* desktop = SP_ACTIVE_DESKTOP;
79 if (desktop && desktop->gridsEnabled()) {
80 for ( GSList const *l = _named_view->grids; l != NULL; l = l->next) {
81 Inkscape::CanvasGrid *grid = (Inkscape::CanvasGrid*) l->data;
82 s.push_back(grid->snapper);
83 }
84 }
86 return s;
87 }
89 /**
90 * \return true if one of the snappers will try to snap something.
91 */
93 bool SnapManager::SomeSnapperMightSnap() const
94 {
95 SnapperList const s = getSnappers();
96 SnapperList::const_iterator i = s.begin();
97 while (i != s.end() && (*i)->ThisSnapperMightSnap() == false) {
98 i++;
99 }
101 return (i != s.end());
102 }
104 /*
105 * The snappers have too many parameters to adjust individually. Therefore only
106 * two snapping modes are presented to the user: snapping bounding box corners (to
107 * other bounding boxes, grids or guides), and/or snapping nodes (to other nodes,
108 * paths, grids or guides). To select either of these modes (or both), use the
109 * methods defined below: setSnapModeBBox() and setSnapModeNode().
110 *
111 * */
114 void SnapManager::setSnapModeBBox(bool enabled)
115 {
116 //The default values are being set in sp_namedview_set() (in sp-namedview.cpp)
117 guide.setSnapFrom(Inkscape::Snapper::SNAPPOINT_BBOX, enabled);
119 for ( GSList const *l = _named_view->grids; l != NULL; l = l->next) {
120 Inkscape::CanvasGrid *grid = (Inkscape::CanvasGrid*) l->data;
121 grid->snapper->setSnapFrom(Inkscape::Snapper::SNAPPOINT_BBOX, enabled);
122 }
124 object.setSnapFrom(Inkscape::Snapper::SNAPPOINT_BBOX, enabled);
125 object.setSnapToBBoxNode(enabled);
126 object.setSnapToBBoxPath(enabled);
127 object.setStrictSnapping(true); //don't snap bboxes to nodes/paths and vice versa
128 }
130 bool SnapManager::getSnapModeBBox() const
131 {
132 return guide.getSnapFrom(Inkscape::Snapper::SNAPPOINT_BBOX);
133 }
135 void SnapManager::setSnapModeNode(bool enabled)
136 {
137 guide.setSnapFrom(Inkscape::Snapper::SNAPPOINT_NODE, enabled);
139 for ( GSList const *l = _named_view->grids; l != NULL; l = l->next) {
140 Inkscape::CanvasGrid *grid = (Inkscape::CanvasGrid*) l->data;
141 grid->snapper->setSnapFrom(Inkscape::Snapper::SNAPPOINT_NODE, enabled);
142 }
144 object.setSnapFrom(Inkscape::Snapper::SNAPPOINT_NODE, enabled);
145 //object.setSnapToItemNode(enabled); // On second thought, these should be controlled
146 //object.setSnapToItemPath(enabled); // separately by the snapping prefs dialog
147 object.setStrictSnapping(true);
148 }
150 bool SnapManager::getSnapModeNode() const
151 {
152 return guide.getSnapFrom(Inkscape::Snapper::SNAPPOINT_NODE);
153 }
155 void SnapManager::setSnapModeGuide(bool enabled)
156 {
157 object.setSnapFrom(Inkscape::Snapper::SNAPPOINT_GUIDE, enabled);
158 }
160 bool SnapManager::getSnapModeGuide() const
161 {
162 return object.getSnapFrom(Inkscape::Snapper::SNAPPOINT_GUIDE);
163 }
165 /**
166 * Try to snap a point to any interested snappers.
167 *
168 * \param t Type of point.
169 * \param p Point.
170 * \param it Item to ignore when snapping.
171 * \return Snapped point.
172 */
174 Inkscape::SnappedPoint SnapManager::freeSnap(Inkscape::Snapper::PointType t,
175 NR::Point const &p,
176 SPItem const *it) const
178 {
179 std::list<SPItem const *> lit;
180 lit.push_back(it);
182 std::vector<NR::Point> points_to_snap;
183 points_to_snap.push_back(p);
185 return freeSnap(t, p, true, points_to_snap, lit);
186 }
188 /**
189 * Try to snap a point to any of the specified snappers.
190 *
191 * \param t Type of point.
192 * \param p Point.
193 * \param first_point If true then this point is the first one from a whole bunch of points
194 * \param points_to_snap The whole bunch of points, all from the same selection and having the same transformation
195 * \param it List of items to ignore when snapping.
196 * \param snappers List of snappers to try to snap to
197 * \return Snapped point.
198 */
200 Inkscape::SnappedPoint SnapManager::freeSnap(Inkscape::Snapper::PointType t,
201 NR::Point const &p,
202 bool const &first_point,
203 std::vector<NR::Point> &points_to_snap,
204 std::list<SPItem const *> const &it) const
205 {
207 SnappedConstraints sc;
209 SnapperList const snappers = getSnappers();
211 for (SnapperList::const_iterator i = snappers.begin(); i != snappers.end(); i++) {
212 (*i)->freeSnap(sc, t, p, first_point, points_to_snap, it);
213 }
215 return findBestSnap(p, sc);
216 }
218 /**
219 * Try to snap a point to any of the specified snappers. Snap always, ignoring the snap-distance
220 *
221 * \param t Type of point.
222 * \param p Point.
223 * \param it Item to ignore when snapping.
224 * \param snappers List of snappers to try to snap to
225 * \return Snapped point.
226 */
228 Inkscape::SnappedPoint
229 SnapManager::freeSnapAlways( Inkscape::Snapper::PointType t,
230 NR::Point const &p,
231 SPItem const *it,
232 SnapperList &snappers )
233 {
234 std::list<SPItem const *> lit;
235 lit.push_back(it);
236 return freeSnapAlways(t, p, lit, snappers);
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 List of items 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 std::list<SPItem const *> const &it,
253 SnapperList &snappers )
254 {
256 SnappedConstraints sc;
258 for (SnapperList::iterator i = snappers.begin(); i != snappers.end(); i++) {
259 gdouble const curr_gridsnap = (*i)->getDistance();
260 const_cast<Inkscape::Snapper*> (*i)->setDistance(NR_HUGE);
261 std::vector<NR::Point> points_to_snap;
262 points_to_snap.push_back(p);
263 (*i)->freeSnap(sc, t, p, true, points_to_snap, it);
264 const_cast<Inkscape::Snapper*> (*i)->setDistance(curr_gridsnap);
265 }
267 return findBestSnap(p, sc);
268 }
272 /**
273 * Try to snap a point to any interested snappers. A snap will only occur along
274 * a line described by a Inkscape::Snapper::ConstraintLine.
275 *
276 * \param t Type of point.
277 * \param p Point.
278 * \param c Constraint line.
279 * \param it Item to ignore when snapping.
280 * \return Snapped point.
281 */
283 Inkscape::SnappedPoint SnapManager::constrainedSnap(Inkscape::Snapper::PointType t,
284 NR::Point const &p,
285 Inkscape::Snapper::ConstraintLine const &c,
286 SPItem const *it) const
287 {
288 std::list<SPItem const *> lit;
289 lit.push_back(it);
291 std::vector<NR::Point> points_to_snap;
292 points_to_snap.push_back(p);
294 return constrainedSnap(t, p, true, points_to_snap, c, lit);
295 }
299 /**
300 * Try to snap a point to any interested snappers. A snap will only occur along
301 * a line described by a Inkscape::Snapper::ConstraintLine.
302 *
303 * \param t Type of point.
304 * \param p Point.
305 * \param first_point If true then this point is the first one from a whole bunch of points
306 * \param points_to_snap The whole bunch of points, all from the same selection and having the same transformation
307 * \param c Constraint line.
308 * \param it List of items to ignore when snapping.
309 * \return Snapped point.
310 */
312 Inkscape::SnappedPoint SnapManager::constrainedSnap(Inkscape::Snapper::PointType t,
313 NR::Point const &p,
314 bool const &first_point,
315 std::vector<NR::Point> &points_to_snap,
316 Inkscape::Snapper::ConstraintLine const &c,
317 std::list<SPItem const *> const &it) const
318 {
320 SnappedConstraints sc;
322 SnapperList const snappers = getSnappers();
323 for (SnapperList::const_iterator i = snappers.begin(); i != snappers.end(); i++) {
324 (*i)->constrainedSnap(sc, t, p, first_point, points_to_snap, c, it);
325 }
327 return findBestSnap(p, sc);
328 }
330 Inkscape::SnappedPoint SnapManager::guideSnap(NR::Point const &p,
331 NR::Point const &guide_normal) const
332 {
333 Inkscape::ObjectSnapper::DimensionToSnap snap_dim;
334 if (guide_normal == component_vectors[NR::Y]) {
335 snap_dim = Inkscape::ObjectSnapper::SNAP_Y;
336 } else if (guide_normal == component_vectors[NR::X]) {
337 snap_dim = Inkscape::ObjectSnapper::SNAP_X;
338 } else {
339 g_warning("WARNING: snapping of angled guides is not supported yet!");
340 snap_dim = Inkscape::ObjectSnapper::SNAP_XY;
341 }
343 return object.guideSnap(p, snap_dim);
344 }
347 /**
348 * Main internal snapping method, which is called by the other, friendlier, public
349 * methods. It's a bit hairy as it has lots of parameters, but it saves on a lot
350 * of duplicated code.
351 *
352 * \param type Type of points being snapped.
353 * \param points List of points to snap.
354 * \param ignore List of items to ignore while snapping.
355 * \param constrained true if the snap is constrained.
356 * \param constraint Constraint line to use, if `constrained' is true, otherwise undefined.
357 * \param transformation_type Type of transformation to apply to points before trying to snap them.
358 * \param transformation Description of the transformation; details depend on the type.
359 * \param origin Origin of the transformation, if applicable.
360 * \param dim Dimension of the transformation, if applicable.
361 * \param uniform true if the transformation should be uniform, if applicable.
362 */
364 std::pair<NR::Point, bool> SnapManager::_snapTransformed(
365 Inkscape::Snapper::PointType type,
366 std::vector<NR::Point> const &points,
367 std::list<SPItem const *> const &ignore,
368 bool constrained,
369 Inkscape::Snapper::ConstraintLine const &constraint,
370 Transformation transformation_type,
371 NR::Point const &transformation,
372 NR::Point const &origin,
373 NR::Dim2 dim,
374 bool uniform) const
375 {
376 /* We have a list of points, which we are proposing to transform in some way. We need to see
377 ** if any of these points, when transformed, snap to anything. If they do, we return the
378 ** appropriate transformation with `true'; otherwise we return the original scale with `false'.
379 */
381 /* Quick check to see if we have any snappers that are enabled */
382 if (SomeSnapperMightSnap() == false) {
383 return std::make_pair(transformation, false);
384 }
386 std::vector<NR::Point> transformed_points;
388 for (std::vector<NR::Point>::const_iterator i = points.begin(); i != points.end(); i++) {
390 /* Work out the transformed version of this point */
391 NR::Point transformed;
392 switch (transformation_type) {
393 case TRANSLATION:
394 transformed = *i + transformation;
395 break;
396 case SCALE:
397 transformed = ((*i - origin) * NR::scale(transformation[NR::X], transformation[NR::Y])) + origin;
398 break;
399 case STRETCH:
400 {
401 NR::scale s(1, 1);
402 if (uniform)
403 s[NR::X] = s[NR::Y] = transformation[dim];
404 else {
405 s[dim] = transformation[dim];
406 s[1 - dim] = 1;
407 }
408 transformed = ((*i - origin) * s) + origin;
409 break;
410 }
411 case SKEW:
412 transformed = *i;
413 transformed[dim] += transformation[dim] * ((*i)[1 - dim] - origin[1 - dim]);
414 break;
415 default:
416 g_assert_not_reached();
417 }
419 // add the current transformed point to the box hulling all transformed points
420 transformed_points.push_back(transformed);
421 }
423 /* The current best transformation */
424 NR::Point best_transformation = transformation;
426 /* The current best metric for the best transformation; lower is better, NR_HUGE
427 ** means that we haven't snapped anything.
428 */
429 double best_metric = NR_HUGE;
431 std::vector<NR::Point>::const_iterator j = transformed_points.begin();
433 for (std::vector<NR::Point>::const_iterator i = points.begin(); i != points.end(); i++) {
435 /* Snap it */
436 Inkscape::SnappedPoint const snapped = constrained ?
437 constrainedSnap(type, *j, i == points.begin(), transformed_points, constraint, ignore) : freeSnap(type, *j, i == points.begin(), transformed_points, ignore);
439 if (snapped.getDistance() < NR_HUGE) {
440 /* We snapped. Find the transformation that describes where the snapped point has
441 ** ended up, and also the metric for this transformation.
442 */
443 NR::Point result;
444 NR::Coord metric;
445 switch (transformation_type) {
446 case TRANSLATION:
447 result = snapped.getPoint() - *i;
448 /* Consider the case in which a box is almost aligned with a grid in both
449 * horizontal and vertical directions. The distance to the intersection of
450 * the grid lines will always be larger then the distance to a single grid
451 * line. If we prefer snapping to an intersection instead of to a single
452 * grid line, then we cannot use "metric = NR::L2(result)". Therefore the
453 * snapped distance will be used as a metric. Please note that the snapped
454 * distance is defined as the distance to the nearest line of the intersection,
455 * and not to the intersection itself!
456 */
457 metric = snapped.getDistance(); //used to be: metric = NR::L2(result);
458 break;
459 case SCALE:
460 {
461 NR::Point const a = (snapped.getPoint() - origin);
462 NR::Point const b = (*i - origin);
463 result = NR::Point(a[NR::X] / b[NR::X], a[NR::Y] / b[NR::Y]);
464 metric = std::abs(NR::L2(result) - NR::L2(transformation));
465 break;
466 }
467 case STRETCH:
468 {
469 for (int a = 0; a < 2; a++) {
470 if (uniform || a == dim) {
471 result[a] = (snapped.getPoint()[dim] - origin[dim]) / ((*i)[dim] - origin[dim]);
472 } else {
473 result[a] = 1;
474 }
475 }
476 metric = std::abs(result[dim] - transformation[dim]);
477 break;
478 }
479 case SKEW:
480 result[dim] = (snapped.getPoint()[dim] - (*i)[dim]) / ((*i)[1 - dim] - origin[1 - dim]);
481 metric = std::abs(result[dim] - transformation[dim]);
482 break;
483 default:
484 g_assert_not_reached();
485 }
487 /* Note it if it's the best so far */
488 if ((metric < best_metric) || ((metric == best_metric) && snapped.getAtIntersection() == true)) {
489 best_transformation = result;
490 best_metric = metric;
491 }
492 }
494 j++;
495 }
497 // Using " < 1e6" instead of " < NR::HUGE" for catching some rounding errors
498 // These rounding errors might be caused by NRRects, see bug #1584301
499 return std::make_pair(best_transformation, best_metric < 1e6);
500 }
503 /**
504 * Try to snap a list of points to any interested snappers after they have undergone
505 * a translation.
506 *
507 * \param t Type of points.
508 * \param p Points.
509 * \param it List of items to ignore when snapping.
510 * \param tr Proposed translation.
511 * \return Snapped translation, if a snap occurred, and a flag indicating whether a snap occurred.
512 */
514 std::pair<NR::Point, bool> SnapManager::freeSnapTranslation(Inkscape::Snapper::PointType t,
515 std::vector<NR::Point> const &p,
516 std::list<SPItem const *> const &it,
517 NR::Point const &tr) const
518 {
519 return _snapTransformed(
520 t, p, it, false, NR::Point(), TRANSLATION, tr, NR::Point(), NR::X, false
521 );
522 }
525 /**
526 * Try to snap a list of points to any interested snappers after they have undergone a
527 * translation. A snap will only occur along a line described by a
528 * Inkscape::Snapper::ConstraintLine.
529 *
530 * \param t Type of points.
531 * \param p Points.
532 * \param it List of items to ignore when snapping.
533 * \param c Constraint line.
534 * \param tr Proposed translation.
535 * \return Snapped translation, if a snap occurred, and a flag indicating whether a snap occurred.
536 */
538 std::pair<NR::Point, bool> SnapManager::constrainedSnapTranslation(Inkscape::Snapper::PointType t,
539 std::vector<NR::Point> const &p,
540 std::list<SPItem const *> const &it,
541 Inkscape::Snapper::ConstraintLine const &c,
542 NR::Point const &tr) const
543 {
544 return _snapTransformed(
545 t, p, it, true, c, TRANSLATION, tr, NR::Point(), NR::X, false
546 );
547 }
550 /**
551 * Try to snap a list of points to any interested snappers after they have undergone
552 * a scale.
553 *
554 * \param t Type of points.
555 * \param p Points.
556 * \param it List of items to ignore when snapping.
557 * \param s Proposed scale.
558 * \param o Origin of proposed scale.
559 * \return Snapped scale, if a snap occurred, and a flag indicating whether a snap occurred.
560 */
562 std::pair<NR::scale, bool> SnapManager::freeSnapScale(Inkscape::Snapper::PointType t,
563 std::vector<NR::Point> const &p,
564 std::list<SPItem const *> const &it,
565 NR::scale const &s,
566 NR::Point const &o) const
567 {
568 return _snapTransformed(
569 t, p, it, false, NR::Point(), SCALE, NR::Point(s[NR::X], s[NR::Y]), o, NR::X, false
570 );
571 }
574 /**
575 * Try to snap a list of points to any interested snappers after they have undergone
576 * a scale. A snap will only occur along a line described by a
577 * Inkscape::Snapper::ConstraintLine.
578 *
579 * \param t Type of points.
580 * \param p Points.
581 * \param it List of items to ignore when snapping.
582 * \param s Proposed scale.
583 * \param o Origin of proposed scale.
584 * \return Snapped scale, if a snap occurred, and a flag indicating whether a snap occurred.
585 */
587 std::pair<NR::scale, bool> SnapManager::constrainedSnapScale(Inkscape::Snapper::PointType t,
588 std::vector<NR::Point> const &p,
589 std::list<SPItem const *> const &it,
590 Inkscape::Snapper::ConstraintLine const &c,
591 NR::scale const &s,
592 NR::Point const &o) const
593 {
594 return _snapTransformed(
595 t, p, it, true, c, SCALE, NR::Point(s[NR::X], s[NR::Y]), o, NR::X, false
596 );
597 }
600 /**
601 * Try to snap a list of points to any interested snappers after they have undergone
602 * a stretch.
603 *
604 * \param t Type of points.
605 * \param p Points.
606 * \param it List of items to ignore when snapping.
607 * \param s Proposed stretch.
608 * \param o Origin of proposed stretch.
609 * \param d Dimension in which to apply proposed stretch.
610 * \param u true if the stretch should be uniform (ie to be applied equally in both dimensions)
611 * \return Snapped stretch, if a snap occurred, and a flag indicating whether a snap occurred.
612 */
614 std::pair<NR::Coord, bool> SnapManager::freeSnapStretch(Inkscape::Snapper::PointType t,
615 std::vector<NR::Point> const &p,
616 std::list<SPItem const *> const &it,
617 NR::Coord const &s,
618 NR::Point const &o,
619 NR::Dim2 d,
620 bool u) const
621 {
622 std::pair<NR::Point, bool> const r = _snapTransformed(
623 t, p, it, false, NR::Point(), STRETCH, NR::Point(s, s), o, d, u
624 );
626 return std::make_pair(r.first[d], r.second);
627 }
630 /**
631 * Try to snap a list of points to any interested snappers after they have undergone
632 * a skew.
633 *
634 * \param t Type of points.
635 * \param p Points.
636 * \param it List of items to ignore when snapping.
637 * \param s Proposed skew.
638 * \param o Origin of proposed skew.
639 * \param d Dimension in which to apply proposed skew.
640 * \return Snapped skew, if a snap occurred, and a flag indicating whether a snap occurred.
641 */
643 std::pair<NR::Coord, bool> SnapManager::freeSnapSkew(Inkscape::Snapper::PointType t,
644 std::vector<NR::Point> const &p,
645 std::list<SPItem const *> const &it,
646 NR::Coord const &s,
647 NR::Point const &o,
648 NR::Dim2 d) const
649 {
650 std::pair<NR::Point, bool> const r = _snapTransformed(
651 t, p, it, false, NR::Point(), SKEW, NR::Point(s, s), o, d, false
652 );
654 return std::make_pair(r.first[d], r.second);
655 }
657 Inkscape::SnappedPoint SnapManager::findBestSnap(NR::Point const &p, SnappedConstraints &sc) const
658 {
659 NR::Coord const guide_sens = guide.getDistance();
660 NR::Coord grid_sens = 0;
662 SnapManager::SnapperList const gs = getGridSnappers();
663 SnapperList::const_iterator i = gs.begin();
664 if (i != gs.end()) {
665 grid_sens = (*i)->getDistance();
666 }
668 // Store all snappoints, optionally together with their specific snapping range
669 std::list<std::pair<Inkscape::SnappedPoint, NR::Coord> > sp_list;
670 // Most of these snapped points are already within the snapping range, because
671 // they have already been filtered by their respective snappers. In that case
672 // we can set the snapping range to NR_HUGE here. If however we're looking at
673 // intersections of e.g. a grid and guide line, then we'll have to determine
674 // once again whether we're within snapping range. In this case we will set
675 // the snapping range to e.g. min(guide_sens, grid_sens)
677 // search for the closest snapped point
678 Inkscape::SnappedPoint closestPoint;
679 if (getClosestSP(sc.points, closestPoint)) {
680 sp_list.push_back(std::make_pair(closestPoint, NR_HUGE));
681 }
683 // search for the closest snapped grid line
684 Inkscape::SnappedInfiniteLine closestGridLine;
685 if (getClosestSIL(sc.grid_lines, closestGridLine)) {
686 sp_list.push_back(std::make_pair(Inkscape::SnappedPoint(closestGridLine), NR_HUGE));
687 }
689 // search for the closest snapped guide line
690 Inkscape::SnappedInfiniteLine closestGuideLine;
691 if (getClosestSIL(sc.guide_lines, closestGuideLine)) {
692 sp_list.push_back(std::make_pair(Inkscape::SnappedPoint(closestGuideLine), NR_HUGE));
693 }
695 // search for the closest snapped intersection of grid lines
696 Inkscape::SnappedPoint closestGridPoint;
697 if (getClosestIntersectionSIL(sc.grid_lines, closestGridPoint)) {
698 sp_list.push_back(std::make_pair(closestGridPoint, NR_HUGE));
699 }
701 // search for the closest snapped intersection of guide lines
702 Inkscape::SnappedPoint closestGuidePoint;
703 if (getClosestIntersectionSIL(sc.guide_lines, closestGuidePoint)) {
704 sp_list.push_back(std::make_pair(closestGuidePoint, NR_HUGE));
705 }
707 // search for the closest snapped intersection of grid with guide lines
708 Inkscape::SnappedPoint closestGridGuidePoint;
709 if (getClosestIntersectionSIL(sc.grid_lines, sc.guide_lines, closestGridGuidePoint)) {
710 sp_list.push_back(std::make_pair(closestGridGuidePoint, std::min(guide_sens, grid_sens)));
711 }
713 // now let's see which snapped point gets a thumbs up
714 Inkscape::SnappedPoint bestPoint(p, NR_HUGE);
715 for (std::list<std::pair<Inkscape::SnappedPoint, NR::Coord> >::const_iterator i = sp_list.begin(); i != sp_list.end(); i++) {
716 // first find out if this snapped point is within snapping range
717 if ((*i).first.getDistance() <= (*i).second) {
718 // if it's the first point
719 bool c1 = (i == sp_list.begin());
720 // or, if it's closer
721 bool c2 = (*i).first.getDistance() < bestPoint.getDistance();
722 // or, if it's just as close but at an intersection
723 bool c3 = ((*i).first.getDistance() == bestPoint.getDistance()) && (*i).first.getAtIntersection();
724 // then prefer this point over the previous one
725 if (c1 || c2 || c3) {
726 bestPoint = (*i).first;
727 }
728 }
729 }
730 return bestPoint;
731 }
732 /*
733 Local Variables:
734 mode:c++
735 c-file-style:"stroustrup"
736 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
737 indent-tabs-mode:nil
738 fill-column:99
739 End:
740 */
741 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :