Code

a6b91e07447a70ae60b306574516f22729e933b9
[inkscape.git] / src / snap.cpp
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 {
49     
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     }
100     
101     return (i != s.end());
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)
116     //The default values are being set in sp_namedview_set() (in sp-namedview.cpp)
117     guide.setSnapFrom(Inkscape::Snapper::SNAPPOINT_BBOX, enabled);
118     
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     }
123     
124     object.setSnapFrom(Inkscape::Snapper::SNAPPOINT_BBOX, enabled);
125     //object.setSnapToBBoxNode(enabled); // On second thought, these should be controlled
126     //object.setSnapToBBoxPath(enabled); // separately by the snapping prefs dialog
127     object.setStrictSnapping(true); //don't snap bboxes to nodes/paths and vice versa    
130 bool SnapManager::getSnapModeBBox() const
132     return guide.getSnapFrom(Inkscape::Snapper::SNAPPOINT_BBOX);
135 void SnapManager::setSnapModeNode(bool enabled)
137     guide.setSnapFrom(Inkscape::Snapper::SNAPPOINT_NODE, enabled);
138     
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     }
143         
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);
150 bool SnapManager::getSnapModeNode() const
152     return guide.getSnapFrom(Inkscape::Snapper::SNAPPOINT_NODE);
155 void SnapManager::setSnapModeGuide(bool enabled)
157     object.setSnapFrom(Inkscape::Snapper::SNAPPOINT_GUIDE, enabled);
160 bool SnapManager::getSnapModeGuide() const
162     return object.getSnapFrom(Inkscape::Snapper::SNAPPOINT_GUIDE);
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
179     std::list<SPItem const *> lit;
180     lit.push_back(it);
181     
182     std::vector<NR::Point> points_to_snap;
183     points_to_snap.push_back(p);
184     
185     return freeSnap(t, p, true, points_to_snap, lit);
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
206     
207     SnappedConstraints sc;        
208     
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);
218 /**
219  *  Try to snap a point to any interested snappers.  A snap will only occur along
220  *  a line described by a Inkscape::Snapper::ConstraintLine.
221  *
222  *  \param t Type of point.
223  *  \param p Point.
224  *  \param c Constraint line.
225  *  \param it Item to ignore when snapping.
226  *  \return Snapped point.
227  */
229 Inkscape::SnappedPoint SnapManager::constrainedSnap(Inkscape::Snapper::PointType t,
230                                                     NR::Point const &p,
231                                                     Inkscape::Snapper::ConstraintLine const &c,
232                                                     SPItem const *it) const
234     std::list<SPItem const *> lit;
235     lit.push_back(it);
236     
237     std::vector<NR::Point> points_to_snap;
238     points_to_snap.push_back(p);
239     
240     return constrainedSnap(t, p, true, points_to_snap, c, lit);
245 /**
246  *  Try to snap a point to any interested snappers.  A snap will only occur along
247  *  a line described by a Inkscape::Snapper::ConstraintLine.
248  *
249  *  \param t Type of point.
250  *  \param p Point.
251  *  \param first_point If true then this point is the first one from a whole bunch of points 
252  *  \param points_to_snap The whole bunch of points, all from the same selection and having the same transformation 
253  *  \param c Constraint line.
254  *  \param it List of items to ignore when snapping.
255  *  \return Snapped point.
256  */
258 Inkscape::SnappedPoint SnapManager::constrainedSnap(Inkscape::Snapper::PointType t,
259                                                     NR::Point const &p,
260                                                     bool const &first_point,
261                                                      std::vector<NR::Point> &points_to_snap,
262                                                     Inkscape::Snapper::ConstraintLine const &c,
263                                                     std::list<SPItem const *> const &it) const
265     
266     SnappedConstraints sc;
267         
268     SnapperList const snappers = getSnappers();
269     for (SnapperList::const_iterator i = snappers.begin(); i != snappers.end(); i++) {
270         (*i)->constrainedSnap(sc, t, p, first_point, points_to_snap, c, it);
271     }
273     return findBestSnap(p, sc);
276 Inkscape::SnappedPoint SnapManager::guideSnap(NR::Point const &p,
277                                              NR::Point const &guide_normal) const
279     Inkscape::ObjectSnapper::DimensionToSnap snap_dim;
280     if (guide_normal == component_vectors[NR::Y]) {
281         snap_dim = Inkscape::ObjectSnapper::SNAP_Y;
282     } else if (guide_normal == component_vectors[NR::X]) {
283         snap_dim = Inkscape::ObjectSnapper::SNAP_X;
284     } else {
285         g_warning("WARNING: snapping of angled guides is not supported yet!");
286         snap_dim = Inkscape::ObjectSnapper::SNAP_XY;
287     }
288     
289     SnappedConstraints sc;
290     object.guideSnap(sc, p, snap_dim);
291     
292     return findBestSnap(p, sc);    
296 /**
297  *  Main internal snapping method, which is called by the other, friendlier, public
298  *  methods.  It's a bit hairy as it has lots of parameters, but it saves on a lot
299  *  of duplicated code.
300  *
301  *  \param type Type of points being snapped.
302  *  \param points List of points to snap.
303  *  \param ignore List of items to ignore while snapping.
304  *  \param constrained true if the snap is constrained.
305  *  \param constraint Constraint line to use, if `constrained' is true, otherwise undefined.
306  *  \param transformation_type Type of transformation to apply to points before trying to snap them.
307  *  \param transformation Description of the transformation; details depend on the type.
308  *  \param origin Origin of the transformation, if applicable.
309  *  \param dim Dimension of the transformation, if applicable.
310  *  \param uniform true if the transformation should be uniform, if applicable.
311  */
313 std::pair<NR::Point, bool> SnapManager::_snapTransformed(
314     Inkscape::Snapper::PointType type,
315     std::vector<NR::Point> const &points,
316     std::list<SPItem const *> const &ignore,
317     bool constrained,
318     Inkscape::Snapper::ConstraintLine const &constraint,
319     Transformation transformation_type,
320     NR::Point const &transformation,
321     NR::Point const &origin,
322     NR::Dim2 dim,
323     bool uniform) const
325     /* We have a list of points, which we are proposing to transform in some way.  We need to see
326     ** if any of these points, when transformed, snap to anything.  If they do, we return the
327     ** appropriate transformation with `true'; otherwise we return the original scale with `false'.
328     */
330     /* Quick check to see if we have any snappers that are enabled */
331     if (SomeSnapperMightSnap() == false) {
332         return std::make_pair(transformation, false);
333     }
334     
335     std::vector<NR::Point> transformed_points;
336     
337     for (std::vector<NR::Point>::const_iterator i = points.begin(); i != points.end(); i++) {
339         /* Work out the transformed version of this point */
340         NR::Point transformed;
341         switch (transformation_type) {
342             case TRANSLATION:
343                 transformed = *i + transformation;
344                 break;
345             case SCALE:
346                 transformed = ((*i - origin) * NR::scale(transformation[NR::X], transformation[NR::Y])) + origin;
347                 break;
348             case STRETCH:
349             {
350                 NR::scale s(1, 1);
351                 if (uniform)
352                     s[NR::X] = s[NR::Y] = transformation[dim];
353                 else {
354                     s[dim] = transformation[dim];
355                     s[1 - dim] = 1;
356                 }
357                 transformed = ((*i - origin) * s) + origin;
358                 break;
359             }
360             case SKEW:
361                 transformed = *i;
362                 transformed[dim] += transformation[dim] * ((*i)[1 - dim] - origin[1 - dim]);
363                 break;
364             default:
365                 g_assert_not_reached();
366         }
367         
368         // add the current transformed point to the box hulling all transformed points
369         transformed_points.push_back(transformed);
370     }    
371     
372     /* The current best transformation */
373     NR::Point best_transformation = transformation;
375     /* The current best metric for the best transformation; lower is better, NR_HUGE
376     ** means that we haven't snapped anything.
377     */
378     double best_metric = NR_HUGE;
380     std::vector<NR::Point>::const_iterator j = transformed_points.begin();
382     // std::cout << std::endl;
384     for (std::vector<NR::Point>::const_iterator i = points.begin(); i != points.end(); i++) {
385         
386         /* Snap it */
387         Inkscape::SnappedPoint const snapped = constrained ?
388             constrainedSnap(type, *j, i == points.begin(), transformed_points, constraint, ignore) : freeSnap(type, *j, i == points.begin(), transformed_points, ignore);
390         NR::Point result;
391         NR::Coord metric;
392         
393         if (snapped.getDistance() < NR_HUGE) {
394             /* We snapped.  Find the transformation that describes where the snapped point has
395             ** ended up, and also the metric for this transformation.
396             */
397             switch (transformation_type) {
398                 case TRANSLATION:
399                     result = snapped.getPoint() - *i;
400                     /* Consider the case in which a box is almost aligned with a grid in both 
401                      * horizontal and vertical directions. The distance to the intersection of
402                      * the grid lines will always be larger then the distance to a single grid
403                      * line. If we prefer snapping to an intersection instead of to a single 
404                      * grid line, then we cannot use "metric = NR::L2(result)". Therefore the
405                      * snapped distance will be used as a metric. Please note that the snapped
406                      * distance is defined as the distance to the nearest line of the intersection,
407                      * and not to the intersection itself! 
408                      */
409                     metric = snapped.getDistance(); //used to be: metric = NR::L2(result);
410                     break;
411                 case SCALE:
412                 {
413                     NR::Point const a = (snapped.getPoint() - origin);
414                     NR::Point const b = (*i - origin);
415                     result = NR::Point(a[NR::X] / b[NR::X], a[NR::Y] / b[NR::Y]);
416                     metric = std::abs(NR::L2(result) - NR::L2(transformation));
417                     break;
418                 }
419                 case STRETCH:
420                 {
421                     for (int a = 0; a < 2; a++) {
422                         if (uniform || a == dim) {
423                             result[a] = (snapped.getPoint()[dim] - origin[dim]) / ((*i)[dim] - origin[dim]);
424                         } else {
425                             result[a] = 1;
426                         }
427                     }
428                     metric = std::abs(result[dim] - transformation[dim]);
429                     break;
430                 }
431                 case SKEW:
432                     result[dim] = (snapped.getPoint()[dim] - (*i)[dim]) / ((*i)[1 - dim] - origin[1 - dim]);
433                     metric = std::abs(result[dim] - transformation[dim]);
434                     break;
435                 default:
436                     g_assert_not_reached();
437             }
439             /* Note it if it's the best so far */
440             if ((metric < best_metric) || ((metric == best_metric) && snapped.getAtIntersection() == true)) {
441                 best_transformation = result;
442                 best_metric = metric;
443                 // std::cout << "SEL ";;
444             } //else { std::cout << "    ";}
445         }
446         
447         // std::cout << "P_orig = " << (*i) << " | metric = " << metric << " | distance = " << snapped.getDistance() << " | P_snap = " << snapped.getPoint() << std::endl;
448         j++;
449     }
450     
451     // Using " < 1e6" instead of " < NR::HUGE" for catching some rounding errors
452     // These rounding errors might be caused by NRRects, see bug #1584301
453     return std::make_pair(best_transformation, best_metric < 1e6);
457 /**
458  *  Try to snap a list of points to any interested snappers after they have undergone
459  *  a translation.
460  *
461  *  \param t Type of points.
462  *  \param p Points.
463  *  \param it List of items to ignore when snapping.
464  *  \param tr Proposed translation.
465  *  \return Snapped translation, if a snap occurred, and a flag indicating whether a snap occurred.
466  */
468 std::pair<NR::Point, bool> SnapManager::freeSnapTranslation(Inkscape::Snapper::PointType t,
469                                                             std::vector<NR::Point> const &p,
470                                                             std::list<SPItem const *> const &it,
471                                                             NR::Point const &tr) const
473     return _snapTransformed(
474         t, p, it, false, NR::Point(), TRANSLATION, tr, NR::Point(), NR::X, false
475         );
479 /**
480  *  Try to snap a list of points to any interested snappers after they have undergone a
481  *  translation.  A snap will only occur along a line described by a
482  *  Inkscape::Snapper::ConstraintLine.
483  *
484  *  \param t Type of points.
485  *  \param p Points.
486  *  \param it List of items to ignore when snapping.
487  *  \param c Constraint line.
488  *  \param tr Proposed translation.
489  *  \return Snapped translation, if a snap occurred, and a flag indicating whether a snap occurred.
490  */
492 std::pair<NR::Point, bool> SnapManager::constrainedSnapTranslation(Inkscape::Snapper::PointType t,
493                                                                    std::vector<NR::Point> const &p,
494                                                                    std::list<SPItem const *> const &it,
495                                                                    Inkscape::Snapper::ConstraintLine const &c,
496                                                                    NR::Point const &tr) const
498     return _snapTransformed(
499         t, p, it, true, c, TRANSLATION, tr, NR::Point(), NR::X, false
500         );
504 /**
505  *  Try to snap a list of points to any interested snappers after they have undergone
506  *  a scale.
507  *
508  *  \param t Type of points.
509  *  \param p Points.
510  *  \param it List of items to ignore when snapping.
511  *  \param s Proposed scale.
512  *  \param o Origin of proposed scale.
513  *  \return Snapped scale, if a snap occurred, and a flag indicating whether a snap occurred.
514  */
516 std::pair<NR::scale, bool> SnapManager::freeSnapScale(Inkscape::Snapper::PointType t,
517                                                       std::vector<NR::Point> const &p,
518                                                       std::list<SPItem const *> const &it,
519                                                       NR::scale const &s,
520                                                       NR::Point const &o) const
522     return _snapTransformed(
523         t, p, it, false, NR::Point(), SCALE, NR::Point(s[NR::X], s[NR::Y]), o, NR::X, false
524         );
528 /**
529  *  Try to snap a list of points to any interested snappers after they have undergone
530  *  a scale.  A snap will only occur along a line described by a
531  *  Inkscape::Snapper::ConstraintLine.
532  *
533  *  \param t Type of points.
534  *  \param p Points.
535  *  \param it List of items to ignore when snapping.
536  *  \param s Proposed scale.
537  *  \param o Origin of proposed scale.
538  *  \return Snapped scale, if a snap occurred, and a flag indicating whether a snap occurred.
539  */
541 std::pair<NR::scale, bool> SnapManager::constrainedSnapScale(Inkscape::Snapper::PointType t,
542                                                              std::vector<NR::Point> const &p,
543                                                              std::list<SPItem const *> const &it,
544                                                              Inkscape::Snapper::ConstraintLine const &c,
545                                                              NR::scale const &s,
546                                                              NR::Point const &o) const
548     return _snapTransformed(
549         t, p, it, true, c, SCALE, NR::Point(s[NR::X], s[NR::Y]), o, NR::X, false
550         );
554 /**
555  *  Try to snap a list of points to any interested snappers after they have undergone
556  *  a stretch.
557  *
558  *  \param t Type of points.
559  *  \param p Points.
560  *  \param it List of items to ignore when snapping.
561  *  \param s Proposed stretch.
562  *  \param o Origin of proposed stretch.
563  *  \param d Dimension in which to apply proposed stretch.
564  *  \param u true if the stretch should be uniform (ie to be applied equally in both dimensions)
565  *  \return Snapped stretch, if a snap occurred, and a flag indicating whether a snap occurred.
566  */
568 std::pair<NR::Coord, bool> SnapManager::freeSnapStretch(Inkscape::Snapper::PointType t,
569                                                         std::vector<NR::Point> const &p,
570                                                         std::list<SPItem const *> const &it,
571                                                         NR::Coord const &s,
572                                                         NR::Point const &o,
573                                                         NR::Dim2 d,
574                                                         bool u) const
576    std::pair<NR::Point, bool> const r = _snapTransformed(
577         t, p, it, false, NR::Point(), STRETCH, NR::Point(s, s), o, d, u
578         );
580    return std::make_pair(r.first[d], r.second);
584 /**
585  *  Try to snap a list of points to any interested snappers after they have undergone
586  *  a skew.
587  *
588  *  \param t Type of points.
589  *  \param p Points.
590  *  \param it List of items to ignore when snapping.
591  *  \param s Proposed skew.
592  *  \param o Origin of proposed skew.
593  *  \param d Dimension in which to apply proposed skew.
594  *  \return Snapped skew, if a snap occurred, and a flag indicating whether a snap occurred.
595  */
597 std::pair<NR::Coord, bool> SnapManager::freeSnapSkew(Inkscape::Snapper::PointType t,
598                                                      std::vector<NR::Point> const &p,
599                                                      std::list<SPItem const *> const &it,
600                                                      NR::Coord const &s,
601                                                      NR::Point const &o,
602                                                      NR::Dim2 d) const
604    std::pair<NR::Point, bool> const r = _snapTransformed(
605         t, p, it, false, NR::Point(), SKEW, NR::Point(s, s), o, d, false
606         );
608    return std::make_pair(r.first[d], r.second);
611 Inkscape::SnappedPoint SnapManager::findBestSnap(NR::Point const &p, SnappedConstraints &sc) const
613     NR::Coord const guide_sens = guide.getDistance();
614     NR::Coord grid_sens = 0;
615     
616     SnapManager::SnapperList const gs = getGridSnappers();
617     SnapperList::const_iterator i = gs.begin();
618     if (i != gs.end()) {        
619         grid_sens = (*i)->getDistance();
620     }
621     
622     // Store all snappoints, optionally together with their specific snapping range
623     std::list<std::pair<Inkscape::SnappedPoint, NR::Coord> > sp_list;
624     // Most of these snapped points are already within the snapping range, because
625     // they have already been filtered by their respective snappers. In that case
626     // we can set the snapping range to NR_HUGE here. If however we're looking at
627     // intersections of e.g. a grid and guide line, then we'll have to determine 
628     // once again whether we're within snapping range. In this case we will set
629     // the snapping range to e.g. min(guide_sens, grid_sens)
630     
631     // search for the closest snapped point
632     Inkscape::SnappedPoint closestPoint;
633     if (getClosestSP(sc.points, closestPoint)) {
634         sp_list.push_back(std::make_pair(closestPoint, NR_HUGE));
635     } 
636     
637     // search for the closest snapped line segment
638     Inkscape::SnappedLineSegment closestLineSegment;
639     if (getClosestSLS(sc.lines, closestLineSegment)) {    
640         sp_list.push_back(std::make_pair(Inkscape::SnappedPoint(closestLineSegment), NR_HUGE));
641     }
642     
643     if (_intersectionLS) {
644             // search for the closest snapped intersection of line segments
645             Inkscape::SnappedPoint closestLineSegmentIntersection;
646             if (getClosestIntersectionSLS(sc.lines, closestLineSegmentIntersection)) {
647                 sp_list.push_back(std::make_pair(closestLineSegmentIntersection, NR_HUGE));
648             }
649     }    
651     // search for the closest snapped grid line
652     Inkscape::SnappedLine closestGridLine;
653     if (getClosestSL(sc.grid_lines, closestGridLine)) {    
654         sp_list.push_back(std::make_pair(Inkscape::SnappedPoint(closestGridLine), NR_HUGE));
655     }
656     
657     // search for the closest snapped guide line
658     Inkscape::SnappedLine closestGuideLine;
659     if (getClosestSL(sc.guide_lines, closestGuideLine)) {
660         sp_list.push_back(std::make_pair(Inkscape::SnappedPoint(closestGuideLine), NR_HUGE));
661     }
662     
663     // search for the closest snapped intersection of grid lines
664     Inkscape::SnappedPoint closestGridPoint;
665     if (getClosestIntersectionSL(sc.grid_lines, closestGridPoint)) {
666         sp_list.push_back(std::make_pair(closestGridPoint, NR_HUGE));
667     }
668     
669     // search for the closest snapped intersection of guide lines
670     Inkscape::SnappedPoint closestGuidePoint;
671     if (getClosestIntersectionSL(sc.guide_lines, closestGuidePoint)) {
672         sp_list.push_back(std::make_pair(closestGuidePoint, NR_HUGE));
673     }
674     
675     // search for the closest snapped intersection of grid with guide lines
676     if (_intersectionGG) {
677             Inkscape::SnappedPoint closestGridGuidePoint;
678             if (getClosestIntersectionSL(sc.grid_lines, sc.guide_lines, closestGridGuidePoint)) {
679                 sp_list.push_back(std::make_pair(closestGridGuidePoint, std::min(guide_sens, grid_sens)));
680             }
681     }
682     
683     // now let's see which snapped point gets a thumbs up
684     Inkscape::SnappedPoint bestPoint(p, NR_HUGE);
685     for (std::list<std::pair<Inkscape::SnappedPoint, NR::Coord> >::const_iterator i = sp_list.begin(); i != sp_list.end(); i++) {
686          // first find out if this snapped point is within snapping range
687          if ((*i).first.getDistance() <= (*i).second) {
688              // if it's the first point
689              bool c1 = (i == sp_list.begin());  
690              // or, if it's closer
691              bool c2 = (*i).first.getDistance() < bestPoint.getDistance(); 
692              // or, if it's just as close but at an intersection
693              bool c3 = ((*i).first.getDistance() == bestPoint.getDistance()) && (*i).first.getAtIntersection(); 
694              // then prefer this point over the previous one
695              if (c1 || c2 || c3) {
696                  bestPoint = (*i).first;
697              }
698          }
699      }
700      return bestPoint;         
703 /*
704   Local Variables:
705   mode:c++
706   c-file-style:"stroustrup"
707   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
708   indent-tabs-mode:nil
709   fill-column:99
710   End:
711 */
712 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :