Code

9faa4fae6f22fcd4b7ba67fe89cad42444b1f8f5
[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     _snap_enabled_globally(true)
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     if (!_snap_enabled_globally) {
96         return false;
97     }
98     
99     SnapperList const s = getSnappers();
100     SnapperList::const_iterator i = s.begin();
101     while (i != s.end() && (*i)->ThisSnapperMightSnap() == false) {
102         i++;
103     }
104     
105     return (i != s.end());
108 /*
109  *  The snappers have too many parameters to adjust individually. Therefore only
110  *  two snapping modes are presented to the user: snapping bounding box corners (to 
111  *  other bounding boxes, grids or guides), and/or snapping nodes (to other nodes,
112  *  paths, grids or guides). To select either of these modes (or both), use the 
113  *  methods defined below: setSnapModeBBox() and setSnapModeNode().
114  * 
115  * */
118 void SnapManager::setSnapModeBBox(bool enabled)
120     //The default values are being set in sp_namedview_set() (in sp-namedview.cpp)
121     guide.setSnapFrom(Inkscape::Snapper::SNAPPOINT_BBOX, enabled);
122     
123     for ( GSList const *l = _named_view->grids; l != NULL; l = l->next) {
124         Inkscape::CanvasGrid *grid = (Inkscape::CanvasGrid*) l->data;
125         grid->snapper->setSnapFrom(Inkscape::Snapper::SNAPPOINT_BBOX, enabled);
126     }
127     
128     object.setSnapFrom(Inkscape::Snapper::SNAPPOINT_BBOX, enabled);
129     //object.setSnapToBBoxNode(enabled); // On second thought, these should be controlled
130     //object.setSnapToBBoxPath(enabled); // separately by the snapping prefs dialog
131     object.setStrictSnapping(true); //don't snap bboxes to nodes/paths and vice versa    
134 bool SnapManager::getSnapModeBBox() const
136     return guide.getSnapFrom(Inkscape::Snapper::SNAPPOINT_BBOX);
139 void SnapManager::setSnapModeNode(bool enabled)
141     guide.setSnapFrom(Inkscape::Snapper::SNAPPOINT_NODE, enabled);
142     
143     for ( GSList const *l = _named_view->grids; l != NULL; l = l->next) {
144         Inkscape::CanvasGrid *grid = (Inkscape::CanvasGrid*) l->data;
145         grid->snapper->setSnapFrom(Inkscape::Snapper::SNAPPOINT_NODE, enabled);
146     }
147         
148     object.setSnapFrom(Inkscape::Snapper::SNAPPOINT_NODE, enabled);
149     //object.setSnapToItemNode(enabled); // On second thought, these should be controlled
150     //object.setSnapToItemPath(enabled); // separately by the snapping prefs dialog 
151     object.setStrictSnapping(true);
154 bool SnapManager::getSnapModeNode() const
156     return guide.getSnapFrom(Inkscape::Snapper::SNAPPOINT_NODE);
159 void SnapManager::setSnapModeGuide(bool enabled)
161     object.setSnapFrom(Inkscape::Snapper::SNAPPOINT_GUIDE, enabled);
164 bool SnapManager::getSnapModeGuide() const
166     return object.getSnapFrom(Inkscape::Snapper::SNAPPOINT_GUIDE);
169 /**
170  *  Try to snap a point to any interested snappers.
171  *
172  *  \param t Type of point.
173  *  \param p Point.
174  *  \param it Item to ignore when snapping.
175  *  \return Snapped point.
176  */
178 Inkscape::SnappedPoint SnapManager::freeSnap(Inkscape::Snapper::PointType t,
179                                              NR::Point const &p,
180                                              SPItem const *it) const
183     std::list<SPItem const *> lit;
184     lit.push_back(it);
185     
186     std::vector<NR::Point> points_to_snap;
187     points_to_snap.push_back(p);
188     
189     return freeSnap(t, p, true, points_to_snap, lit);
192 /**
193  *  Try to snap a point to any of the specified snappers.
194  *
195  *  \param t Type of point.
196  *  \param p Point.
197  *  \param first_point If true then this point is the first one from a whole bunch of points 
198  *  \param points_to_snap The whole bunch of points, all from the same selection and having the same transformation 
199  *  \param it List of items to ignore when snapping.
200  * \param snappers  List of snappers to try to snap to
201  *  \return Snapped point.
202  */
204 Inkscape::SnappedPoint SnapManager::freeSnap(Inkscape::Snapper::PointType t,
205                                              NR::Point const &p,
206                                              bool const &first_point,
207                                              std::vector<NR::Point> &points_to_snap,
208                                              std::list<SPItem const *> const &it) const
210     
211     SnappedConstraints sc;        
212     
213     SnapperList const snappers = getSnappers();
215     for (SnapperList::const_iterator i = snappers.begin(); i != snappers.end(); i++) {
216         (*i)->freeSnap(sc, t, p, first_point, points_to_snap, it);
217     }
219     return findBestSnap(p, sc);
222 /**
223  *  Try to snap a point to any interested snappers.  A snap will only occur along
224  *  a line described by a Inkscape::Snapper::ConstraintLine.
225  *
226  *  \param t Type of point.
227  *  \param p Point.
228  *  \param c Constraint line.
229  *  \param it Item to ignore when snapping.
230  *  \return Snapped point.
231  */
233 Inkscape::SnappedPoint SnapManager::constrainedSnap(Inkscape::Snapper::PointType t,
234                                                     NR::Point const &p,
235                                                     Inkscape::Snapper::ConstraintLine const &c,
236                                                     SPItem const *it) const
238     std::list<SPItem const *> lit;
239     lit.push_back(it);
240     
241     std::vector<NR::Point> points_to_snap;
242     points_to_snap.push_back(p);
243     
244     return constrainedSnap(t, p, true, points_to_snap, c, lit);
249 /**
250  *  Try to snap a point to any interested snappers.  A snap will only occur along
251  *  a line described by a Inkscape::Snapper::ConstraintLine.
252  *
253  *  \param t Type of point.
254  *  \param p Point.
255  *  \param first_point If true then this point is the first one from a whole bunch of points 
256  *  \param points_to_snap The whole bunch of points, all from the same selection and having the same transformation 
257  *  \param c Constraint line.
258  *  \param it List of items to ignore when snapping.
259  *  \return Snapped point.
260  */
262 Inkscape::SnappedPoint SnapManager::constrainedSnap(Inkscape::Snapper::PointType t,
263                                                     NR::Point const &p,
264                                                     bool const &first_point,
265                                                     std::vector<NR::Point> &points_to_snap,
266                                                     Inkscape::Snapper::ConstraintLine const &c,
267                                                     std::list<SPItem const *> const &it) const
269     
270     SnappedConstraints sc;
271         
272     SnapperList const snappers = getSnappers();
273     for (SnapperList::const_iterator i = snappers.begin(); i != snappers.end(); i++) {
274         (*i)->constrainedSnap(sc, t, p, first_point, points_to_snap, c, it);
275     }
277     return findBestSnap(p, sc);
280 Inkscape::SnappedPoint SnapManager::guideSnap(NR::Point const &p,
281                                              NR::Point const &guide_normal) const
283     Inkscape::ObjectSnapper::DimensionToSnap snap_dim;
284     if (guide_normal == component_vectors[NR::Y]) {
285         snap_dim = Inkscape::ObjectSnapper::SNAP_Y;
286     } else if (guide_normal == component_vectors[NR::X]) {
287         snap_dim = Inkscape::ObjectSnapper::SNAP_X;
288     } else {
289         g_warning("WARNING: snapping of angled guides is not supported yet!");
290         snap_dim = Inkscape::ObjectSnapper::SNAP_XY;
291     }
292     
293     SnappedConstraints sc;
294     object.guideSnap(sc, p, snap_dim);
295     
296     return findBestSnap(p, sc);    
300 /**
301  *  Main internal snapping method, which is called by the other, friendlier, public
302  *  methods.  It's a bit hairy as it has lots of parameters, but it saves on a lot
303  *  of duplicated code.
304  *
305  *  \param type Type of points being snapped.
306  *  \param points List of points to snap.
307  *  \param ignore List of items to ignore while snapping.
308  *  \param constrained true if the snap is constrained.
309  *  \param constraint Constraint line to use, if `constrained' is true, otherwise undefined.
310  *  \param transformation_type Type of transformation to apply to points before trying to snap them.
311  *  \param transformation Description of the transformation; details depend on the type.
312  *  \param origin Origin of the transformation, if applicable.
313  *  \param dim Dimension of the transformation, if applicable.
314  *  \param uniform true if the transformation should be uniform, if applicable.
315  */
317 std::pair<NR::Point, bool> SnapManager::_snapTransformed(
318     Inkscape::Snapper::PointType type,
319     std::vector<NR::Point> const &points,
320     std::list<SPItem const *> const &ignore,
321     bool constrained,
322     Inkscape::Snapper::ConstraintLine const &constraint,
323     Transformation transformation_type,
324     NR::Point const &transformation,
325     NR::Point const &origin,
326     NR::Dim2 dim,
327     bool uniform) const
329     /* We have a list of points, which we are proposing to transform in some way.  We need to see
330     ** if any of these points, when transformed, snap to anything.  If they do, we return the
331     ** appropriate transformation with `true'; otherwise we return the original scale with `false'.
332     */
334     /* Quick check to see if we have any snappers that are enabled
335     ** Also used to globally disable all snapping 
336     */
337     if (SomeSnapperMightSnap() == false) {
338         return std::make_pair(transformation, false);
339     }
340     
341     std::vector<NR::Point> transformed_points;
342     
343     for (std::vector<NR::Point>::const_iterator i = points.begin(); i != points.end(); i++) {
345         /* Work out the transformed version of this point */
346         NR::Point transformed;
347         switch (transformation_type) {
348             case TRANSLATION:
349                 transformed = *i + transformation;
350                 break;
351             case SCALE:
352                 transformed = ((*i - origin) * NR::scale(transformation[NR::X], transformation[NR::Y])) + origin;
353                 break;
354             case STRETCH:
355             {
356                 NR::scale s(1, 1);
357                 if (uniform)
358                     s[NR::X] = s[NR::Y] = transformation[dim];
359                 else {
360                     s[dim] = transformation[dim];
361                     s[1 - dim] = 1;
362                 }
363                 transformed = ((*i - origin) * s) + origin;
364                 break;
365             }
366             case SKEW:
367                 transformed = *i;
368                 transformed[dim] += transformation[dim] * ((*i)[1 - dim] - origin[1 - dim]);
369                 break;
370             default:
371                 g_assert_not_reached();
372         }
373         
374         // add the current transformed point to the box hulling all transformed points
375         transformed_points.push_back(transformed);
376     }    
377     
378     /* The current best transformation */
379     NR::Point best_transformation = transformation;
381     /* The current best metric for the best transformation; lower is better, NR_HUGE
382     ** means that we haven't snapped anything.
383     */
384     NR::Coord best_metric = NR_HUGE;
385     NR::Coord best_second_metric = NR_HUGE;
386     bool best_at_intersection = false;
388     std::vector<NR::Point>::const_iterator j = transformed_points.begin();
390     //std::cout << std::endl;
392     for (std::vector<NR::Point>::const_iterator i = points.begin(); i != points.end(); i++) {
393         
394         /* Snap it */
395         Inkscape::SnappedPoint const snapped = constrained ?
396             constrainedSnap(type, *j, i == points.begin(), transformed_points, constraint, ignore) : freeSnap(type, *j, i == points.begin(), transformed_points, ignore);
398         NR::Point result;
399         NR::Coord metric = NR_HUGE;
400         NR::Coord second_metric = NR_HUGE;
401         
402         if (snapped.getDistance() < NR_HUGE) {
403             /* We snapped.  Find the transformation that describes where the snapped point has
404             ** ended up, and also the metric for this transformation.
405             */
406             switch (transformation_type) {
407                 case TRANSLATION:
408                     result = snapped.getPoint() - *i;
409                     /* Consider the case in which a box is almost aligned with a grid in both 
410                      * horizontal and vertical directions. The distance to the intersection of
411                      * the grid lines will always be larger then the distance to a single grid
412                      * line. If we prefer snapping to an intersection instead of to a single 
413                      * grid line, then we cannot use "metric = NR::L2(result)". Therefore the
414                      * snapped distance will be used as a metric. Please note that the snapped
415                      * distance is defined as the distance to the nearest line of the intersection,
416                      * and not to the intersection itself! 
417                      */
418                     metric = snapped.getDistance(); //used to be: metric = NR::L2(result);
419                     second_metric = snapped.getSecondDistance();
420                     break;
421                 case SCALE:
422                 {
423                     NR::Point const a = (snapped.getPoint() - origin);
424                     NR::Point const b = (*i - origin);
425                     result = NR::Point(a[NR::X] / b[NR::X], a[NR::Y] / b[NR::Y]);
426                     metric = std::abs(NR::L2(result) - NR::L2(transformation));
427                     break;
428                 }
429                 case STRETCH:
430                 {
431                     for (int a = 0; a < 2; a++) {
432                         if (uniform || a == dim) {
433                             result[a] = (snapped.getPoint()[dim] - origin[dim]) / ((*i)[dim] - origin[dim]);
434                         } else {
435                             result[a] = 1;
436                         }
437                     }
438                     metric = std::abs(result[dim] - transformation[dim]);
439                     break;
440                 }
441                 case SKEW:
442                     result[dim] = (snapped.getPoint()[dim] - (*i)[dim]) / ((*i)[1 - dim] - origin[1 - dim]);
443                     metric = std::abs(result[dim] - transformation[dim]);
444                     break;
445                 default:
446                     g_assert_not_reached();
447             }
448             
449             /* Note it if it's the best so far */
450             bool const c1 = metric < best_metric;
451             bool const c2 = metric == best_metric && snapped.getAtIntersection() == true && best_at_intersection == false;
452                         bool const c3a = metric == best_metric && snapped.getAtIntersection() == true && best_at_intersection == true;
453             bool const c3b = second_metric < best_second_metric;
454             
455             if (c1 || c2 || c3a && c3b) {
456                 best_transformation = result;
457                 best_metric = metric;
458                 best_second_metric = second_metric;
459                 best_at_intersection = snapped.getAtIntersection(); 
460                 //std::cout << "SEL ";
461             } //else { std::cout << "    ";}
462         }
463         
464         //std::cout << "P_orig = " << (*i) << " | metric = " << metric << " | distance = " << snapped.getDistance() << " | second metric = " << second_metric << " | P_snap = " << snapped.getPoint() << std::endl;
465         j++;
466     }
467     
468     // Using " < 1e6" instead of " < NR_HUGE" for catching some rounding errors
469     // These rounding errors might be caused by NRRects, see bug #1584301
470     return std::make_pair(best_transformation, best_metric < 1e6);
474 /**
475  *  Try to snap a list of points to any interested snappers after they have undergone
476  *  a translation.
477  *
478  *  \param t Type of points.
479  *  \param p Points.
480  *  \param it List of items to ignore when snapping.
481  *  \param tr Proposed translation.
482  *  \return Snapped translation, if a snap occurred, and a flag indicating whether a snap occurred.
483  */
485 std::pair<NR::Point, bool> SnapManager::freeSnapTranslation(Inkscape::Snapper::PointType t,
486                                                             std::vector<NR::Point> const &p,
487                                                             std::list<SPItem const *> const &it,
488                                                             NR::Point const &tr) const
490     return _snapTransformed(
491         t, p, it, false, NR::Point(), TRANSLATION, tr, NR::Point(), NR::X, false
492         );
496 /**
497  *  Try to snap a list of points to any interested snappers after they have undergone a
498  *  translation.  A snap will only occur along a line described by a
499  *  Inkscape::Snapper::ConstraintLine.
500  *
501  *  \param t Type of points.
502  *  \param p Points.
503  *  \param it List of items to ignore when snapping.
504  *  \param c Constraint line.
505  *  \param tr Proposed translation.
506  *  \return Snapped translation, if a snap occurred, and a flag indicating whether a snap occurred.
507  */
509 std::pair<NR::Point, bool> SnapManager::constrainedSnapTranslation(Inkscape::Snapper::PointType t,
510                                                                    std::vector<NR::Point> const &p,
511                                                                    std::list<SPItem const *> const &it,
512                                                                    Inkscape::Snapper::ConstraintLine const &c,
513                                                                    NR::Point const &tr) const
515     return _snapTransformed(
516         t, p, it, true, c, TRANSLATION, tr, NR::Point(), NR::X, false
517         );
521 /**
522  *  Try to snap a list of points to any interested snappers after they have undergone
523  *  a scale.
524  *
525  *  \param t Type of points.
526  *  \param p Points.
527  *  \param it List of items to ignore when snapping.
528  *  \param s Proposed scale.
529  *  \param o Origin of proposed scale.
530  *  \return Snapped scale, if a snap occurred, and a flag indicating whether a snap occurred.
531  */
533 std::pair<NR::scale, bool> SnapManager::freeSnapScale(Inkscape::Snapper::PointType t,
534                                                       std::vector<NR::Point> const &p,
535                                                       std::list<SPItem const *> const &it,
536                                                       NR::scale const &s,
537                                                       NR::Point const &o) const
539     return _snapTransformed(
540         t, p, it, false, NR::Point(), SCALE, NR::Point(s[NR::X], s[NR::Y]), o, NR::X, false
541         );
545 /**
546  *  Try to snap a list of points to any interested snappers after they have undergone
547  *  a scale.  A snap will only occur along a line described by a
548  *  Inkscape::Snapper::ConstraintLine.
549  *
550  *  \param t Type of points.
551  *  \param p Points.
552  *  \param it List of items to ignore when snapping.
553  *  \param s Proposed scale.
554  *  \param o Origin of proposed scale.
555  *  \return Snapped scale, if a snap occurred, and a flag indicating whether a snap occurred.
556  */
558 std::pair<NR::scale, bool> SnapManager::constrainedSnapScale(Inkscape::Snapper::PointType t,
559                                                              std::vector<NR::Point> const &p,
560                                                              std::list<SPItem const *> const &it,
561                                                              Inkscape::Snapper::ConstraintLine const &c,
562                                                              NR::scale const &s,
563                                                              NR::Point const &o) const
565     return _snapTransformed(
566         t, p, it, true, c, SCALE, NR::Point(s[NR::X], s[NR::Y]), o, NR::X, false
567         );
571 /**
572  *  Try to snap a list of points to any interested snappers after they have undergone
573  *  a stretch.
574  *
575  *  \param t Type of points.
576  *  \param p Points.
577  *  \param it List of items to ignore when snapping.
578  *  \param s Proposed stretch.
579  *  \param o Origin of proposed stretch.
580  *  \param d Dimension in which to apply proposed stretch.
581  *  \param u true if the stretch should be uniform (ie to be applied equally in both dimensions)
582  *  \return Snapped stretch, if a snap occurred, and a flag indicating whether a snap occurred.
583  */
585 std::pair<NR::Coord, bool> SnapManager::freeSnapStretch(Inkscape::Snapper::PointType t,
586                                                         std::vector<NR::Point> const &p,
587                                                         std::list<SPItem const *> const &it,
588                                                         NR::Coord const &s,
589                                                         NR::Point const &o,
590                                                         NR::Dim2 d,
591                                                         bool u) const
593    std::pair<NR::Point, bool> const r = _snapTransformed(
594         t, p, it, false, NR::Point(), STRETCH, NR::Point(s, s), o, d, u
595         );
597    return std::make_pair(r.first[d], r.second);
601 /**
602  *  Try to snap a list of points to any interested snappers after they have undergone
603  *  a skew.
604  *
605  *  \param t Type of points.
606  *  \param p Points.
607  *  \param it List of items to ignore when snapping.
608  *  \param s Proposed skew.
609  *  \param o Origin of proposed skew.
610  *  \param d Dimension in which to apply proposed skew.
611  *  \return Snapped skew, if a snap occurred, and a flag indicating whether a snap occurred.
612  */
614 std::pair<NR::Coord, bool> SnapManager::freeSnapSkew(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) const
621    std::pair<NR::Point, bool> const r = _snapTransformed(
622         t, p, it, false, NR::Point(), SKEW, NR::Point(s, s), o, d, false
623         );
625    return std::make_pair(r.first[d], r.second);
628 Inkscape::SnappedPoint SnapManager::findBestSnap(NR::Point const &p, SnappedConstraints &sc) const
630     NR::Coord const guide_sens = guide.getDistance();
631     NR::Coord grid_sens = 0;
632     
633     SnapManager::SnapperList const gs = getGridSnappers();
634     SnapperList::const_iterator i = gs.begin();
635     if (i != gs.end()) {        
636         grid_sens = (*i)->getDistance();
637     }
638     
639     // Store all snappoints, optionally together with their specific snapping range
640     std::list<std::pair<Inkscape::SnappedPoint, NR::Coord> > sp_list;
641     // Most of these snapped points are already within the snapping range, because
642     // they have already been filtered by their respective snappers. In that case
643     // we can set the snapping range to NR_HUGE here. If however we're looking at
644     // intersections of e.g. a grid and guide line, then we'll have to determine 
645     // once again whether we're within snapping range. In this case we will set
646     // the snapping range to e.g. min(guide_sens, grid_sens)
647     
648     // search for the closest snapped point
649     Inkscape::SnappedPoint closestPoint;
650     if (getClosestSP(sc.points, closestPoint)) {
651         sp_list.push_back(std::make_pair(closestPoint, NR_HUGE));
652     } 
653     
654     // search for the closest snapped line segment
655     Inkscape::SnappedLineSegment closestLineSegment;
656     if (getClosestSLS(sc.lines, closestLineSegment)) {    
657         sp_list.push_back(std::make_pair(Inkscape::SnappedPoint(closestLineSegment), NR_HUGE));
658     }
659     
660     if (_intersectionLS) {
661             // search for the closest snapped intersection of line segments
662             Inkscape::SnappedPoint closestLineSegmentIntersection;
663             if (getClosestIntersectionSLS(sc.lines, closestLineSegmentIntersection)) {
664                 sp_list.push_back(std::make_pair(closestLineSegmentIntersection, NR_HUGE));
665             }
666     }    
668     // search for the closest snapped grid line
669     Inkscape::SnappedLine closestGridLine;
670     if (getClosestSL(sc.grid_lines, closestGridLine)) {    
671         sp_list.push_back(std::make_pair(Inkscape::SnappedPoint(closestGridLine), NR_HUGE));
672     }
673     
674     // search for the closest snapped guide line
675     Inkscape::SnappedLine closestGuideLine;
676     if (getClosestSL(sc.guide_lines, closestGuideLine)) {
677         sp_list.push_back(std::make_pair(Inkscape::SnappedPoint(closestGuideLine), NR_HUGE));
678     }
679     
680     // search for the closest snapped intersection of grid lines
681     Inkscape::SnappedPoint closestGridPoint;
682     if (getClosestIntersectionSL(sc.grid_lines, closestGridPoint)) {
683         sp_list.push_back(std::make_pair(closestGridPoint, NR_HUGE));
684     }
685     
686     // search for the closest snapped intersection of guide lines
687     Inkscape::SnappedPoint closestGuidePoint;
688     if (getClosestIntersectionSL(sc.guide_lines, closestGuidePoint)) {
689         sp_list.push_back(std::make_pair(closestGuidePoint, NR_HUGE));
690     }
691     
692     // search for the closest snapped intersection of grid with guide lines
693     if (_intersectionGG) {
694             Inkscape::SnappedPoint closestGridGuidePoint;
695             if (getClosestIntersectionSL(sc.grid_lines, sc.guide_lines, closestGridGuidePoint)) {
696                 sp_list.push_back(std::make_pair(closestGridGuidePoint, std::min(guide_sens, grid_sens)));
697             }
698     }
699     
700     // now let's see which snapped point gets a thumbs up
701     Inkscape::SnappedPoint bestPoint(p, NR_HUGE);
702     for (std::list<std::pair<Inkscape::SnappedPoint, NR::Coord> >::const_iterator i = sp_list.begin(); i != sp_list.end(); i++) {
703                 // first find out if this snapped point is within snapping range
704             if ((*i).first.getDistance() <= (*i).second) {
705                 // if it's the first point
706                 bool c1 = (i == sp_list.begin());  
707                 // or, if it's closer
708                 bool c2 = (*i).first.getDistance() < bestPoint.getDistance(); 
709                 // or, if it's just as close then consider the second distance
710                 // (which is only relevant for points at an intersection)
711                 bool c3a = ((*i).first.getDistance() == bestPoint.getDistance()); 
712                 bool c3b = (*i).first.getSecondDistance() < bestPoint.getSecondDistance();
713                 // then prefer this point over the previous one
714                 if (c1 || c2 || c3a && c3b) {
715                 bestPoint = (*i).first;
716             }
717         }
718     }
719     return bestPoint;         
722 /*
723   Local Variables:
724   mode:c++
725   c-file-style:"stroustrup"
726   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
727   indent-tabs-mode:nil
728   fill-column:99
729   End:
730 */
731 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :