Code

angled guidelines: create angled line when dragging from edge of rulers
[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, false);
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, true);
280 Inkscape::SnappedPoint SnapManager::guideSnap(NR::Point const &p,
281                                              NR::Point const &guide_normal) const
283     
284     // This method is used to snap a guide to nodes, while dragging the guide around
285     Inkscape::ObjectSnapper::DimensionToSnap snap_dim;
286     if (guide_normal == component_vectors[NR::Y]) {
287         snap_dim = Inkscape::ObjectSnapper::SNAP_Y;
288     } else if (guide_normal == component_vectors[NR::X]) {
289         snap_dim = Inkscape::ObjectSnapper::SNAP_X;
290     } else {
291         g_warning("WARNING: snapping of angled guides is not supported yet!");
292         // this is because _snapnodes, called in object.guideSnap, cannot only handle
293         // vertical or horizontal lines for now....
294         // Rotating an agled guide will require some additional code, as it would be great to
295         // have it rotate around a snapped point
296         snap_dim = Inkscape::ObjectSnapper::SNAP_XY;
297     }
298     
299     SnappedConstraints sc;
300     object.guideSnap(sc, p, snap_dim);
301     
302     return findBestSnap(p, sc, false);    
306 /**
307  *  Main internal snapping method, which is called by the other, friendlier, public
308  *  methods.  It's a bit hairy as it has lots of parameters, but it saves on a lot
309  *  of duplicated code.
310  *
311  *  \param type Type of points being snapped.
312  *  \param points List of points to snap.
313  *  \param ignore List of items to ignore while snapping.
314  *  \param constrained true if the snap is constrained.
315  *  \param constraint Constraint line to use, if `constrained' is true, otherwise undefined.
316  *  \param transformation_type Type of transformation to apply to points before trying to snap them.
317  *  \param transformation Description of the transformation; details depend on the type.
318  *  \param origin Origin of the transformation, if applicable.
319  *  \param dim Dimension of the transformation, if applicable.
320  *  \param uniform true if the transformation should be uniform, if applicable.
321  */
323 std::pair<NR::Point, bool> SnapManager::_snapTransformed(
324     Inkscape::Snapper::PointType type,
325     std::vector<NR::Point> const &points,
326     std::list<SPItem const *> const &ignore,
327     bool constrained,
328     Inkscape::Snapper::ConstraintLine const &constraint,
329     Transformation transformation_type,
330     NR::Point const &transformation,
331     NR::Point const &origin,
332     NR::Dim2 dim,
333     bool uniform) const
335     /* We have a list of points, which we are proposing to transform in some way.  We need to see
336     ** if any of these points, when transformed, snap to anything.  If they do, we return the
337     ** appropriate transformation with `true'; otherwise we return the original scale with `false'.
338     */
340     /* Quick check to see if we have any snappers that are enabled
341     ** Also used to globally disable all snapping 
342     */
343     if (SomeSnapperMightSnap() == false) {
344         return std::make_pair(transformation, false);
345     }
346     
347     std::vector<NR::Point> transformed_points;
348     
349     for (std::vector<NR::Point>::const_iterator i = points.begin(); i != points.end(); i++) {
351         /* Work out the transformed version of this point */
352         NR::Point transformed;
353         switch (transformation_type) {
354             case TRANSLATION:
355                 transformed = *i + transformation;
356                 break;
357             case SCALE:
358                 transformed = ((*i - origin) * NR::scale(transformation[NR::X], transformation[NR::Y])) + origin;
359                 break;
360             case STRETCH:
361             {
362                 NR::scale s(1, 1);
363                 if (uniform)
364                     s[NR::X] = s[NR::Y] = transformation[dim];
365                 else {
366                     s[dim] = transformation[dim];
367                     s[1 - dim] = 1;
368                 }
369                 transformed = ((*i - origin) * s) + origin;
370                 break;
371             }
372             case SKEW:
373                 transformed = *i;
374                 transformed[dim] += transformation[dim] * ((*i)[1 - dim] - origin[1 - dim]);
375                 break;
376             default:
377                 g_assert_not_reached();
378         }
379         
380         // add the current transformed point to the box hulling all transformed points
381         transformed_points.push_back(transformed);
382     }    
383     
384     /* The current best transformation */
385     NR::Point best_transformation = transformation;
387     /* The current best metric for the best transformation; lower is better, NR_HUGE
388     ** means that we haven't snapped anything.
389     */
390     NR::Coord best_metric = NR_HUGE;
391     NR::Coord best_second_metric = NR_HUGE;
392     bool best_at_intersection = false;
394     std::vector<NR::Point>::const_iterator j = transformed_points.begin();
396     //std::cout << std::endl;
398     for (std::vector<NR::Point>::const_iterator i = points.begin(); i != points.end(); i++) {
399         
400         /* Snap it */
401         Inkscape::SnappedPoint const snapped = constrained ?
402             constrainedSnap(type, *j, i == points.begin(), transformed_points, constraint, ignore) : freeSnap(type, *j, i == points.begin(), transformed_points, ignore);
404         NR::Point result;
405         NR::Coord metric = NR_HUGE;
406         NR::Coord second_metric = NR_HUGE;
407         
408         if (snapped.getDistance() < NR_HUGE) {
409             /* We snapped.  Find the transformation that describes where the snapped point has
410             ** ended up, and also the metric for this transformation.
411             */
412             switch (transformation_type) {
413                 case TRANSLATION:
414                     result = snapped.getPoint() - *i;
415                     /* Consider the case in which a box is almost aligned with a grid in both 
416                      * horizontal and vertical directions. The distance to the intersection of
417                      * the grid lines will always be larger then the distance to a single grid
418                      * line. If we prefer snapping to an intersection instead of to a single 
419                      * grid line, then we cannot use "metric = NR::L2(result)". Therefore the
420                      * snapped distance will be used as a metric. Please note that the snapped
421                      * distance is defined as the distance to the nearest line of the intersection,
422                      * and not to the intersection itself! 
423                      */
424                     metric = snapped.getDistance(); //used to be: metric = NR::L2(result);
425                     second_metric = snapped.getSecondDistance();
426                     break;
427                 case SCALE:
428                 {
429                     NR::Point const a = (snapped.getPoint() - origin);
430                     NR::Point const b = (*i - origin);
431                     result = NR::Point(a[NR::X] / b[NR::X], a[NR::Y] / b[NR::Y]);
432                     metric = std::abs(NR::L2(result) - NR::L2(transformation));
433                     break;
434                 }
435                 case STRETCH:
436                 {
437                     for (int a = 0; a < 2; a++) {
438                         if (uniform || a == dim) {
439                             result[a] = (snapped.getPoint()[dim] - origin[dim]) / ((*i)[dim] - origin[dim]);
440                         } else {
441                             result[a] = 1;
442                         }
443                     }
444                     metric = std::abs(result[dim] - transformation[dim]);
445                     break;
446                 }
447                 case SKEW:
448                     result[dim] = (snapped.getPoint()[dim] - (*i)[dim]) / ((*i)[1 - dim] - origin[1 - dim]);
449                     metric = std::abs(result[dim] - transformation[dim]);
450                     break;
451                 default:
452                     g_assert_not_reached();
453             }
454             
455             /* Note it if it's the best so far */
456             bool const c1 = metric < best_metric;
457             bool const c2 = metric == best_metric && snapped.getAtIntersection() == true && best_at_intersection == false;
458                         bool const c3a = metric == best_metric && snapped.getAtIntersection() == true && best_at_intersection == true;
459             bool const c3b = second_metric < best_second_metric;
460             
461             if (c1 || c2 || c3a && c3b) {
462                 best_transformation = result;
463                 best_metric = metric;
464                 best_second_metric = second_metric;
465                 best_at_intersection = snapped.getAtIntersection(); 
466                 //std::cout << "SEL ";
467             } //else { std::cout << "    ";}
468         }
469         
470         //std::cout << "P_orig = " << (*i) << " | metric = " << metric << " | distance = " << snapped.getDistance() << " | second metric = " << second_metric << " | P_snap = " << snapped.getPoint() << std::endl;
471         j++;
472     }
473     
474     // Using " < 1e6" instead of " < NR_HUGE" for catching some rounding errors
475     // These rounding errors might be caused by NRRects, see bug #1584301
476     return std::make_pair(best_transformation, best_metric < 1e6);
480 /**
481  *  Try to snap a list of points to any interested snappers after they have undergone
482  *  a translation.
483  *
484  *  \param t Type of points.
485  *  \param p Points.
486  *  \param it List of items to ignore when snapping.
487  *  \param tr Proposed translation.
488  *  \return Snapped translation, if a snap occurred, and a flag indicating whether a snap occurred.
489  */
491 std::pair<NR::Point, bool> SnapManager::freeSnapTranslation(Inkscape::Snapper::PointType t,
492                                                             std::vector<NR::Point> const &p,
493                                                             std::list<SPItem const *> const &it,
494                                                             NR::Point const &tr) const
496     return _snapTransformed(
497         t, p, it, false, NR::Point(), TRANSLATION, tr, NR::Point(), NR::X, false
498         );
502 /**
503  *  Try to snap a list of points to any interested snappers after they have undergone a
504  *  translation.  A snap will only occur along a line described by a
505  *  Inkscape::Snapper::ConstraintLine.
506  *
507  *  \param t Type of points.
508  *  \param p Points.
509  *  \param it List of items to ignore when snapping.
510  *  \param c Constraint line.
511  *  \param tr Proposed translation.
512  *  \return Snapped translation, if a snap occurred, and a flag indicating whether a snap occurred.
513  */
515 std::pair<NR::Point, bool> SnapManager::constrainedSnapTranslation(Inkscape::Snapper::PointType t,
516                                                                    std::vector<NR::Point> const &p,
517                                                                    std::list<SPItem const *> const &it,
518                                                                    Inkscape::Snapper::ConstraintLine const &c,
519                                                                    NR::Point const &tr) const
521     return _snapTransformed(
522         t, p, it, true, c, TRANSLATION, tr, NR::Point(), NR::X, false
523         );
527 /**
528  *  Try to snap a list of points to any interested snappers after they have undergone
529  *  a scale.
530  *
531  *  \param t Type of points.
532  *  \param p Points.
533  *  \param it List of items to ignore when snapping.
534  *  \param s Proposed scale.
535  *  \param o Origin of proposed scale.
536  *  \return Snapped scale, if a snap occurred, and a flag indicating whether a snap occurred.
537  */
539 std::pair<NR::scale, bool> SnapManager::freeSnapScale(Inkscape::Snapper::PointType t,
540                                                       std::vector<NR::Point> const &p,
541                                                       std::list<SPItem const *> const &it,
542                                                       NR::scale const &s,
543                                                       NR::Point const &o) const
545     return _snapTransformed(
546         t, p, it, false, NR::Point(), SCALE, NR::Point(s[NR::X], s[NR::Y]), o, NR::X, false
547         );
551 /**
552  *  Try to snap a list of points to any interested snappers after they have undergone
553  *  a scale.  A snap will only occur along a line described by a
554  *  Inkscape::Snapper::ConstraintLine.
555  *
556  *  \param t Type of points.
557  *  \param p Points.
558  *  \param it List of items to ignore when snapping.
559  *  \param s Proposed scale.
560  *  \param o Origin of proposed scale.
561  *  \return Snapped scale, if a snap occurred, and a flag indicating whether a snap occurred.
562  */
564 std::pair<NR::scale, bool> SnapManager::constrainedSnapScale(Inkscape::Snapper::PointType t,
565                                                              std::vector<NR::Point> const &p,
566                                                              std::list<SPItem const *> const &it,
567                                                              Inkscape::Snapper::ConstraintLine const &c,
568                                                              NR::scale const &s,
569                                                              NR::Point const &o) const
571     return _snapTransformed(
572         t, p, it, true, c, SCALE, NR::Point(s[NR::X], s[NR::Y]), o, NR::X, false
573         );
577 /**
578  *  Try to snap a list of points to any interested snappers after they have undergone
579  *  a stretch.
580  *
581  *  \param t Type of points.
582  *  \param p Points.
583  *  \param it List of items to ignore when snapping.
584  *  \param s Proposed stretch.
585  *  \param o Origin of proposed stretch.
586  *  \param d Dimension in which to apply proposed stretch.
587  *  \param u true if the stretch should be uniform (ie to be applied equally in both dimensions)
588  *  \return Snapped stretch, if a snap occurred, and a flag indicating whether a snap occurred.
589  */
591 std::pair<NR::Coord, bool> SnapManager::freeSnapStretch(Inkscape::Snapper::PointType t,
592                                                         std::vector<NR::Point> const &p,
593                                                         std::list<SPItem const *> const &it,
594                                                         NR::Coord const &s,
595                                                         NR::Point const &o,
596                                                         NR::Dim2 d,
597                                                         bool u) const
599    std::pair<NR::Point, bool> const r = _snapTransformed(
600         t, p, it, false, NR::Point(), STRETCH, NR::Point(s, s), o, d, u
601         );
603    return std::make_pair(r.first[d], r.second);
607 /**
608  *  Try to snap a list of points to any interested snappers after they have undergone
609  *  a skew.
610  *
611  *  \param t Type of points.
612  *  \param p Points.
613  *  \param it List of items to ignore when snapping.
614  *  \param s Proposed skew.
615  *  \param o Origin of proposed skew.
616  *  \param d Dimension in which to apply proposed skew.
617  *  \return Snapped skew, if a snap occurred, and a flag indicating whether a snap occurred.
618  */
620 std::pair<NR::Coord, bool> SnapManager::freeSnapSkew(Inkscape::Snapper::PointType t,
621                                                      std::vector<NR::Point> const &p,
622                                                      std::list<SPItem const *> const &it,
623                                                      NR::Coord const &s,
624                                                      NR::Point const &o,
625                                                      NR::Dim2 d) const
627    std::pair<NR::Point, bool> const r = _snapTransformed(
628         t, p, it, false, NR::Point(), SKEW, NR::Point(s, s), o, d, false
629         );
631    return std::make_pair(r.first[d], r.second);
634 Inkscape::SnappedPoint SnapManager::findBestSnap(NR::Point const &p, SnappedConstraints &sc, bool constrained) const
636     NR::Coord const guide_sens = guide.getDistance();
637     NR::Coord grid_sens = 0;
638     
639     SnapManager::SnapperList const gs = getGridSnappers();
640     SnapperList::const_iterator i = gs.begin();
641     if (i != gs.end()) {        
642         grid_sens = (*i)->getDistance();
643     }
644     
645     // Store all snappoints, optionally together with their specific snapping range
646     std::list<std::pair<Inkscape::SnappedPoint, NR::Coord> > sp_list;
647     // Most of these snapped points are already within the snapping range, because
648     // they have already been filtered by their respective snappers. In that case
649     // we can set the snapping range to NR_HUGE here. If however we're looking at
650     // intersections of e.g. a grid and guide line, then we'll have to determine 
651     // once again whether we're within snapping range. In this case we will set
652     // the snapping range to e.g. min(guide_sens, grid_sens)
653     
654     // search for the closest snapped point
655     Inkscape::SnappedPoint closestPoint;
656     if (getClosestSP(sc.points, closestPoint)) {
657         sp_list.push_back(std::make_pair(closestPoint, NR_HUGE));
658     } 
659     
660     // search for the closest snapped line segment
661     Inkscape::SnappedLineSegment closestLineSegment;
662     if (getClosestSLS(sc.lines, closestLineSegment)) {    
663         sp_list.push_back(std::make_pair(Inkscape::SnappedPoint(closestLineSegment), NR_HUGE));
664     }
665     
666     if (_intersectionLS) {
667             // search for the closest snapped intersection of line segments
668             Inkscape::SnappedPoint closestLineSegmentIntersection;
669             if (getClosestIntersectionSLS(sc.lines, closestLineSegmentIntersection)) {
670                 sp_list.push_back(std::make_pair(closestLineSegmentIntersection, NR_HUGE));
671             }
672     }    
674     // search for the closest snapped grid line
675     Inkscape::SnappedLine closestGridLine;
676     if (getClosestSL(sc.grid_lines, closestGridLine)) {    
677         sp_list.push_back(std::make_pair(Inkscape::SnappedPoint(closestGridLine), NR_HUGE));
678     }
679     
680     // search for the closest snapped guide line
681     Inkscape::SnappedLine closestGuideLine;
682     if (getClosestSL(sc.guide_lines, closestGuideLine)) {
683         sp_list.push_back(std::make_pair(Inkscape::SnappedPoint(closestGuideLine), NR_HUGE));
684     }
685     
686     // When freely snapping to a grid/guide/path, only one degree of freedom is eliminated
687     // Therefore we will try get fully constrained by finding an intersection with another grid/guide/path 
688     
689     // When doing a constrained snap however, we're already at an intersection of the constrained line and
690     // the grid/guide/path we're snapping to. This snappoint is therefore fully constrained, so there's
691     // no need to look for additional intersections
692     if (!constrained) {
693         // search for the closest snapped intersection of grid lines
694         Inkscape::SnappedPoint closestGridPoint;
695         if (getClosestIntersectionSL(sc.grid_lines, closestGridPoint)) {
696             sp_list.push_back(std::make_pair(closestGridPoint, NR_HUGE));
697         }
698         
699         // search for the closest snapped intersection of guide lines
700         Inkscape::SnappedPoint closestGuidePoint;
701         if (getClosestIntersectionSL(sc.guide_lines, closestGuidePoint)) {
702             sp_list.push_back(std::make_pair(closestGuidePoint, NR_HUGE));
703         }
704         
705         // search for the closest snapped intersection of grid with guide lines
706         if (_intersectionGG) {
707             Inkscape::SnappedPoint closestGridGuidePoint;
708             if (getClosestIntersectionSL(sc.grid_lines, sc.guide_lines, closestGridGuidePoint)) {
709                 sp_list.push_back(std::make_pair(closestGridGuidePoint, std::min(guide_sens, grid_sens)));
710             }
711         }
712     }
713     
714     // now let's see which snapped point gets a thumbs up
715     Inkscape::SnappedPoint bestPoint(p, NR_HUGE);
716     for (std::list<std::pair<Inkscape::SnappedPoint, NR::Coord> >::const_iterator i = sp_list.begin(); i != sp_list.end(); i++) {
717                 // first find out if this snapped point is within snapping range
718             if ((*i).first.getDistance() <= (*i).second) {
719                 // if it's the first point
720                 bool c1 = (i == sp_list.begin());  
721                 // or, if it's closer
722                 bool c2 = (*i).first.getDistance() < bestPoint.getDistance(); 
723                 // or, if it's just as close then consider the second distance
724                 // (which is only relevant for points at an intersection)
725                 bool c3a = ((*i).first.getDistance() == bestPoint.getDistance()); 
726                 bool c3b = (*i).first.getSecondDistance() < bestPoint.getSecondDistance();
727                 // then prefer this point over the previous one
728                 if (c1 || c2 || c3a && c3b) {
729                 bestPoint = (*i).first;
730             }
731         }
732     }
733     return bestPoint;         
736 /*
737   Local Variables:
738   mode:c++
739   c-file-style:"stroustrup"
740   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
741   indent-tabs-mode:nil
742   fill-column:99
743   End:
744 */
745 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :