Code

Fix #181795
[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  *   Diederik van Lierop <mail@diedenrezi.nl>
13  *
14  * Copyright (C) 2006-2007 Johan Engelen <johan@shouraizou.nl>
15  * Copyrigth (C) 2004      Nathan Hurst
16  * Copyright (C) 1999-2002 Authors
17  *
18  * Released under GNU GPL, read the file 'COPYING' for more information
19  */
21 #include <utility>
23 #include "sp-namedview.h"
24 #include "snap.h"
25 #include "snapped-line.h"
27 #include <libnr/nr-point-fns.h>
28 #include <libnr/nr-scale-ops.h>
29 #include <libnr/nr-values.h>
31 #include "display/canvas-grid.h"
33 #include "inkscape.h"
34 #include "desktop.h"
35 #include "sp-guide.h"
36 using std::vector;
38 /**
39  *  Construct a SnapManager for a SPNamedView.
40  *
41  *  \param v `Owning' SPNamedView.
42  */
44 SnapManager::SnapManager(SPNamedView const *v) :
45     guide(v, 0),
46     object(v, 0),
47     _named_view(v),
48     _include_item_center(false),
49     _snap_enabled_globally(true)
50 {    
51 }
54 /**
55  *  \return List of snappers that we use.
56  */
57 SnapManager::SnapperList 
58 SnapManager::getSnappers() const
59 {
60     SnapManager::SnapperList s;
61     s.push_back(&guide);
62     s.push_back(&object);
64     SnapManager::SnapperList gs = getGridSnappers();
65     s.splice(s.begin(), gs);
67     return s;
68 }
70 /**
71  *  \return List of gridsnappers that we use.
72  */
73 SnapManager::SnapperList 
74 SnapManager::getGridSnappers() const
75 {
76     SnapperList s;
78     //FIXME: this code should actually do this: add new grid snappers that are active for this desktop. now it just adds all gridsnappers
79     SPDesktop* desktop = SP_ACTIVE_DESKTOP;
80     if (desktop && desktop->gridsEnabled()) {
81         for ( GSList const *l = _named_view->grids; l != NULL; l = l->next) {
82             Inkscape::CanvasGrid *grid = (Inkscape::CanvasGrid*) l->data;
83             s.push_back(grid->snapper);
84         }
85     }
87     return s;
88 }
90 /**
91  * \return true if one of the snappers will try to snap something.
92  */
94 bool SnapManager::SomeSnapperMightSnap() const
95 {
96     if (!_snap_enabled_globally) {
97         return false;
98     }
99     
100     SnapperList const s = getSnappers();
101     SnapperList::const_iterator i = s.begin();
102     while (i != s.end() && (*i)->ThisSnapperMightSnap() == false) {
103         i++;
104     }
105     
106     return (i != s.end());
109 /*
110  *  The snappers have too many parameters to adjust individually. Therefore only
111  *  two snapping modes are presented to the user: snapping bounding box corners (to 
112  *  other bounding boxes, grids or guides), and/or snapping nodes (to other nodes,
113  *  paths, grids or guides). To select either of these modes (or both), use the 
114  *  methods defined below: setSnapModeBBox() and setSnapModeNode().
115  * 
116  * */
119 void SnapManager::setSnapModeBBox(bool enabled)
121     //The default values are being set in sp_namedview_set() (in sp-namedview.cpp)
122     guide.setSnapFrom(Inkscape::Snapper::SNAPPOINT_BBOX, enabled);
123     
124     for ( GSList const *l = _named_view->grids; l != NULL; l = l->next) {
125         Inkscape::CanvasGrid *grid = (Inkscape::CanvasGrid*) l->data;
126         grid->snapper->setSnapFrom(Inkscape::Snapper::SNAPPOINT_BBOX, enabled);
127     }
128     
129     object.setSnapFrom(Inkscape::Snapper::SNAPPOINT_BBOX, enabled);
130     //object.setSnapToBBoxNode(enabled); // On second thought, these should be controlled
131     //object.setSnapToBBoxPath(enabled); // separately by the snapping prefs dialog
132     object.setStrictSnapping(true); //don't snap bboxes to nodes/paths and vice versa    
135 bool SnapManager::getSnapModeBBox() const
137     return guide.getSnapFrom(Inkscape::Snapper::SNAPPOINT_BBOX);
140 void SnapManager::setSnapModeNode(bool enabled)
142     guide.setSnapFrom(Inkscape::Snapper::SNAPPOINT_NODE, enabled);
143     
144     for ( GSList const *l = _named_view->grids; l != NULL; l = l->next) {
145         Inkscape::CanvasGrid *grid = (Inkscape::CanvasGrid*) l->data;
146         grid->snapper->setSnapFrom(Inkscape::Snapper::SNAPPOINT_NODE, enabled);
147     }
148         
149     object.setSnapFrom(Inkscape::Snapper::SNAPPOINT_NODE, enabled);
150     //object.setSnapToItemNode(enabled); // On second thought, these should be controlled
151     //object.setSnapToItemPath(enabled); // separately by the snapping prefs dialog 
152     object.setStrictSnapping(true);
155 bool SnapManager::getSnapModeNode() const
157     return guide.getSnapFrom(Inkscape::Snapper::SNAPPOINT_NODE);
160 void SnapManager::setSnapModeGuide(bool enabled)
162     object.setSnapFrom(Inkscape::Snapper::SNAPPOINT_GUIDE, enabled);
165 bool SnapManager::getSnapModeGuide() const
167     return object.getSnapFrom(Inkscape::Snapper::SNAPPOINT_GUIDE);
170 /**
171  *  Try to snap a point to any interested snappers.
172  *
173  *  \param t Type of point.
174  *  \param p Point.
175  *  \param it Item to ignore when snapping.
176  *  \return Snapped point.
177  */
179 Inkscape::SnappedPoint SnapManager::freeSnap(Inkscape::Snapper::PointType t,
180                                              NR::Point const &p,
181                                              SPItem const *it) const
184     std::list<SPItem const *> lit;
185     lit.push_back(it);
186     
187     std::vector<NR::Point> points_to_snap;
188     points_to_snap.push_back(p);
189     
190     return freeSnap(t, p, true, points_to_snap, lit);
193 /**
194  *  Try to snap a point to any of the specified snappers.
195  *
196  *  \param t Type of point.
197  *  \param p Point.
198  *  \param first_point If true then this point is the first one from a whole bunch of points 
199  *  \param points_to_snap The whole bunch of points, all from the same selection and having the same transformation 
200  *  \param it List of items to ignore when snapping.
201  * \param snappers  List of snappers to try to snap to
202  *  \return Snapped point.
203  */
205 Inkscape::SnappedPoint SnapManager::freeSnap(Inkscape::Snapper::PointType t,
206                                              NR::Point const &p,
207                                              bool const &first_point,
208                                              std::vector<NR::Point> &points_to_snap,
209                                              std::list<SPItem const *> const &it) const
211     if (!SomeSnapperMightSnap()) {
212         return Inkscape::SnappedPoint(p, NR_HUGE, 0, false);
213     }
214     
215     SnappedConstraints sc;        
216     
217     SnapperList const snappers = getSnappers();
219     for (SnapperList::const_iterator i = snappers.begin(); i != snappers.end(); i++) {
220         (*i)->freeSnap(sc, t, p, first_point, points_to_snap, it);
221     }
223     return findBestSnap(p, sc, false);
226 /**
227  *  Try to snap a point to any interested snappers.  A snap will only occur along
228  *  a line described by a Inkscape::Snapper::ConstraintLine.
229  *
230  *  \param t Type of point.
231  *  \param p Point.
232  *  \param c Constraint line.
233  *  \param it Item to ignore when snapping.
234  *  \return Snapped point.
235  */
237 Inkscape::SnappedPoint SnapManager::constrainedSnap(Inkscape::Snapper::PointType t,
238                                                     NR::Point const &p,
239                                                     Inkscape::Snapper::ConstraintLine const &c,
240                                                     SPItem const *it) const
242     std::list<SPItem const *> lit;
243     lit.push_back(it);
244     
245     std::vector<NR::Point> points_to_snap;
246     points_to_snap.push_back(p);
247     
248     return constrainedSnap(t, p, true, points_to_snap, c, lit);
253 /**
254  *  Try to snap a point to any interested snappers.  A snap will only occur along
255  *  a line described by a Inkscape::Snapper::ConstraintLine.
256  *
257  *  \param t Type of point.
258  *  \param p Point.
259  *  \param first_point If true then this point is the first one from a whole bunch of points 
260  *  \param points_to_snap The whole bunch of points, all from the same selection and having the same transformation 
261  *  \param c Constraint line.
262  *  \param it List of items to ignore when snapping.
263  *  \return Snapped point.
264  */
266 Inkscape::SnappedPoint SnapManager::constrainedSnap(Inkscape::Snapper::PointType t,
267                                                     NR::Point const &p,
268                                                     bool const &first_point,
269                                                     std::vector<NR::Point> &points_to_snap,
270                                                     Inkscape::Snapper::ConstraintLine const &c,
271                                                     std::list<SPItem const *> const &it) const
273     if (!SomeSnapperMightSnap()) {
274         return Inkscape::SnappedPoint(p, NR_HUGE, 0, false);
275     }
276     
277     SnappedConstraints sc;
278         
279     SnapperList const snappers = getSnappers();
280     for (SnapperList::const_iterator i = snappers.begin(); i != snappers.end(); i++) {
281         (*i)->constrainedSnap(sc, t, p, first_point, points_to_snap, c, it);
282     }
284     return findBestSnap(p, sc, true);
287 Inkscape::SnappedPoint SnapManager::guideSnap(NR::Point const &p,
288                                               NR::Point const &guide_normal) const
290     // This method is used to snap a guide to nodes, while dragging the guide around
291     
292     if (!(object.GuidesMightSnap() && _snap_enabled_globally)) {
293         return Inkscape::SnappedPoint(p, NR_HUGE, 0, false);
294     }
295     
296     SnappedConstraints sc;
297     object.guideSnap(sc, p, guide_normal);
298     
299     return findBestSnap(p, sc, false);    
303 /**
304  *  Main internal snapping method, which is called by the other, friendlier, public
305  *  methods.  It's a bit hairy as it has lots of parameters, but it saves on a lot
306  *  of duplicated code.
307  *
308  *  \param type Type of points being snapped.
309  *  \param points List of points to snap.
310  *  \param ignore List of items to ignore while snapping.
311  *  \param constrained true if the snap is constrained.
312  *  \param constraint Constraint line to use, if `constrained' is true, otherwise undefined.
313  *  \param transformation_type Type of transformation to apply to points before trying to snap them.
314  *  \param transformation Description of the transformation; details depend on the type.
315  *  \param origin Origin of the transformation, if applicable.
316  *  \param dim Dimension of the transformation, if applicable.
317  *  \param uniform true if the transformation should be uniform; only applicable for stretching and scaling.
318  */
320 std::pair<NR::Point, bool> SnapManager::_snapTransformed(
321     Inkscape::Snapper::PointType type,
322     std::vector<NR::Point> const &points,
323     std::list<SPItem const *> const &ignore,
324     bool constrained,
325     Inkscape::Snapper::ConstraintLine const &constraint,
326     Transformation transformation_type,
327     NR::Point const &transformation,
328     NR::Point const &origin,
329     NR::Dim2 dim,
330     bool uniform) const
332     /* We have a list of points, which we are proposing to transform in some way.  We need to see
333     ** if any of these points, when transformed, snap to anything.  If they do, we return the
334     ** appropriate transformation with `true'; otherwise we return the original scale with `false'.
335     */
337     /* Quick check to see if we have any snappers that are enabled
338     ** Also used to globally disable all snapping 
339     */
340     if (SomeSnapperMightSnap() == false) {
341         return std::make_pair(transformation, false);
342     }
343     
344     std::vector<NR::Point> transformed_points;
345     
346     for (std::vector<NR::Point>::const_iterator i = points.begin(); i != points.end(); i++) {
348         /* Work out the transformed version of this point */
349         NR::Point transformed;
350         switch (transformation_type) {
351             case TRANSLATION:
352                 transformed = *i + transformation;
353                 break;
354             case SCALE:
355                 transformed = ((*i - origin) * NR::scale(transformation[NR::X], transformation[NR::Y])) + origin;
356                 break;
357             case STRETCH:
358             {
359                 NR::scale s(1, 1);
360                 if (uniform)
361                     s[NR::X] = s[NR::Y] = transformation[dim];
362                 else {
363                     s[dim] = transformation[dim];
364                     s[1 - dim] = 1;
365                 }
366                 transformed = ((*i - origin) * s) + origin;
367                 break;
368             }
369             case SKEW:
370                 transformed = *i;
371                 transformed[dim] += transformation[dim] * ((*i)[1 - dim] - origin[1 - dim]);
372                 break;
373             default:
374                 g_assert_not_reached();
375         }
376         
377         // add the current transformed point to the box hulling all transformed points
378         transformed_points.push_back(transformed);
379     }    
380     
381     /* The current best transformation */
382     NR::Point best_transformation = transformation;
384     /* The current best metric for the best transformation; lower is better, NR_HUGE
385     ** means that we haven't snapped anything.
386     */
387     NR::Coord best_metric = NR_HUGE;
388     NR::Coord best_second_metric = NR_HUGE;
389     NR::Point best_scale_metric(NR_HUGE, NR_HUGE);
390     bool best_at_intersection = false;
391     bool best_always_snap = false;
393     std::vector<NR::Point>::const_iterator j = transformed_points.begin();
395     //std::cout << std::endl;
396     
397     for (std::vector<NR::Point>::const_iterator i = points.begin(); i != points.end(); i++) {
398         
399         /* Snap it */        
400         Inkscape::SnappedPoint snapped;
401                 
402         if (constrained) {    
403             Inkscape::Snapper::ConstraintLine dedicated_constraint = constraint;
404             if ((transformation_type == SCALE || transformation_type == STRETCH) && uniform) {
405                 // When uniformly scaling, each point will have its own unique constraint line,
406                 // running from the scaling origin to the original untransformed point. We will
407                 // calculate that line here 
408                 dedicated_constraint = Inkscape::Snapper::ConstraintLine(origin, (*i) - origin);
409             } else if (transformation_type == STRETCH || transformation_type == SKEW) { // when skewing or non-uniform stretching {
410                 dedicated_constraint = Inkscape::Snapper::ConstraintLine((*i), component_vectors[dim]);
411             } // else: leave the original constraint, e.g. for constrained translation 
412             if (transformation_type == SCALE && !uniform) {
413                 g_warning("Non-uniform constrained scaling is not supported!");   
414             }
415             snapped = constrainedSnap(type, *j, i == points.begin(), transformed_points, dedicated_constraint, ignore);
416         } else {
417             snapped = freeSnap(type, *j, i == points.begin(), transformed_points, ignore);
418         }
420         NR::Point result;
421         NR::Coord metric = NR_HUGE;
422         NR::Coord second_metric = NR_HUGE;
423         NR::Point scale_metric(NR_HUGE, NR_HUGE);
424         
425         if (snapped.getDistance() < NR_HUGE) {
426             /* We snapped.  Find the transformation that describes where the snapped point has
427             ** ended up, and also the metric for this transformation.
428             */
429             NR::Point const a = (snapped.getPoint() - origin); // vector to snapped point
430             NR::Point const b = (*i - origin); // vector to original point
431             
432             switch (transformation_type) {
433                 case TRANSLATION:
434                     result = snapped.getPoint() - *i;
435                     /* Consider the case in which a box is almost aligned with a grid in both 
436                      * horizontal and vertical directions. The distance to the intersection of
437                      * the grid lines will always be larger then the distance to a single grid
438                      * line. If we prefer snapping to an intersection instead of to a single 
439                      * grid line, then we cannot use "metric = NR::L2(result)". Therefore the
440                      * snapped distance will be used as a metric. Please note that the snapped
441                      * distance is defined as the distance to the nearest line of the intersection,
442                      * and not to the intersection itself! 
443                      */
444                     metric = snapped.getDistance(); //used to be: metric = NR::L2(result);
445                     second_metric = snapped.getSecondDistance();
446                     break;
447                 case SCALE:
448                 {
449                     result = NR::Point(NR_HUGE, NR_HUGE);
450                     // If this point *i is horizontally or vertically aligned with
451                     // the origin of the scaling, then it will scale purely in X or Y 
452                     // We can therefore only calculate the scaling in this direction
453                     // and the scaling factor for the other direction should remain
454                     // untouched (unless scaling is uniform ofcourse)
455                     for (int index = 0; index < 2; index++) {
456                         if (fabs(b[index]) > 1e-6) { // if SCALING CAN occur in this direction
457                             if (fabs(fabs(a[index]/b[index]) - fabs(transformation[index])) > 1e-12) { // if SNAPPING DID occur in this direction
458                                 result[index] = a[index] / b[index]; // then calculate it!
459                             }
460                             // we might leave result[1-index] = NR_HUGE
461                             // if scaling didn't occur in the other direction
462                         }
463                     }
464                     // Compare the resulting scaling with the desired scaling
465                     scale_metric = result - transformation; // One or both of its components might be NR_HUGE
466                     break;
467                 }
468                 case STRETCH:
469                     result = NR::Point(NR_HUGE, NR_HUGE);
470                     if (fabs(b[dim]) > 1e-6) { // if STRETCHING will occur for this point
471                         result[dim] = a[dim] / b[dim];
472                         result[1-dim] = uniform ? result[dim] : 1;
473                     } else { // STRETCHING might occur for this point, but only when the stretching is uniform
474                         if (uniform && fabs(b[1-dim]) > 1e-6) {
475                            result[1-dim] = a[1-dim] / b[1-dim];
476                            result[dim] = result[1-dim];
477                         }
478                     }
479                     metric = std::abs(result[dim] - transformation[dim]);
480                     break;
481                 case SKEW:
482                     result[dim] = (snapped.getPoint()[dim] - (*i)[dim]) / ((*i)[1 - dim] - origin[1 - dim]);
483                     metric = std::abs(result[dim] - transformation[dim]);
484                     break;
485                 default:
486                     g_assert_not_reached();
487             }
488             
489             /* Note it if it's the best so far */
490             if (transformation_type == SCALE) {
491                 for (int index = 0; index < 2; index++) {
492                     if (fabs(scale_metric[index]) < fabs(best_scale_metric[index])) {
493                         best_transformation[index] = result[index];
494                         best_scale_metric[index] = fabs(scale_metric[index]);
495                         //std::cout << "SEL ";
496                     } //else { std::cout << "    ";}   
497                 }
498                 if (uniform) {
499                     if (best_scale_metric[0] < best_scale_metric[1]) {
500                         best_transformation[1] = best_transformation[0];
501                         best_scale_metric[1] = best_scale_metric[0]; 
502                     } else {
503                         best_transformation[0] = best_transformation[1];
504                         best_scale_metric[0] = best_scale_metric[1];
505                     }
506                 }
507                 best_metric = std::min(best_scale_metric[0], best_scale_metric[1]);
508                 //std::cout << "P_orig = " << (*i) << " | scale_metric = " << scale_metric << " | distance = " << snapped.getDistance() << " | P_snap = " << snapped.getPoint() << std::endl;
509             } else {
510                 bool const c1 = metric < best_metric;
511                 bool const c2 = metric == best_metric && snapped.getAtIntersection() == true && best_at_intersection == false;
512                         bool const c3a = metric == best_metric && snapped.getAtIntersection() == true && best_at_intersection == true;
513                 bool const c3b = second_metric < best_second_metric;
514                 bool const c4 = snapped.getAlwaysSnap() == true && best_always_snap == false;
515                 bool const c4n = snapped.getAlwaysSnap() == false && best_always_snap == true;
516                 
517                 if ((c1 || c2 || (c3a && c3b) || c4) && !c4n) {
518                     best_transformation = result;
519                     best_metric = metric;
520                     best_second_metric = second_metric;
521                     best_at_intersection = snapped.getAtIntersection();
522                     best_always_snap = snapped.getAlwaysSnap(); 
523                     //std::cout << "SEL ";
524                 } //else { std::cout << "    ";}
525                 //std::cout << "P_orig = " << (*i) << " | metric = " << metric << " | distance = " << snapped.getDistance() << " | second metric = " << second_metric << " | P_snap = " << snapped.getPoint() << std::endl;
526             }
527         }
528         
529         j++;
530     }
531     
532     if (transformation_type == SCALE) {
533         // When scaling, don't ever exit with one of scaling components set to NR_HUGE
534         for (int index = 0; index < 2; index++) {
535             if (best_transformation[index] == NR_HUGE) {
536                 if (uniform && best_transformation[1-index] < NR_HUGE) {
537                         best_transformation[index] = best_transformation[1-index];
538                 } else {
539                         best_transformation[index] = transformation[index];     
540                 }
541             }
542         }
543     }
544     
545     // Using " < 1e6" instead of " < NR_HUGE" for catching some rounding errors
546     // These rounding errors might be caused by NRRects, see bug #1584301
547     return std::make_pair(best_transformation, best_metric < 1e6);
551 /**
552  *  Try to snap a list of points to any interested snappers after they have undergone
553  *  a translation.
554  *
555  *  \param t Type of points.
556  *  \param p Points.
557  *  \param it List of items to ignore when snapping.
558  *  \param tr Proposed translation.
559  *  \return Snapped translation, if a snap occurred, and a flag indicating whether a snap occurred.
560  */
562 std::pair<NR::Point, bool> SnapManager::freeSnapTranslation(Inkscape::Snapper::PointType t,
563                                                             std::vector<NR::Point> const &p,
564                                                             std::list<SPItem const *> const &it,
565                                                             NR::Point const &tr) const
567     return _snapTransformed(
568         t, p, it, false, NR::Point(), TRANSLATION, tr, NR::Point(), NR::X, false
569         );
573 /**
574  *  Try to snap a list of points to any interested snappers after they have undergone a
575  *  translation.  A snap will only occur along a line described by a
576  *  Inkscape::Snapper::ConstraintLine.
577  *
578  *  \param t Type of points.
579  *  \param p Points.
580  *  \param it List of items to ignore when snapping.
581  *  \param c Constraint line.
582  *  \param tr Proposed translation.
583  *  \return Snapped translation, if a snap occurred, and a flag indicating whether a snap occurred.
584  */
586 std::pair<NR::Point, bool> SnapManager::constrainedSnapTranslation(Inkscape::Snapper::PointType t,
587                                                                    std::vector<NR::Point> const &p,
588                                                                    std::list<SPItem const *> const &it,
589                                                                    Inkscape::Snapper::ConstraintLine const &c,
590                                                                    NR::Point const &tr) const
592     return _snapTransformed(
593         t, p, it, true, c, TRANSLATION, tr, NR::Point(), NR::X, false
594         );
598 /**
599  *  Try to snap a list of points to any interested snappers after they have undergone
600  *  a scale.
601  *
602  *  \param t Type of points.
603  *  \param p Points.
604  *  \param it List of items to ignore when snapping.
605  *  \param s Proposed scale.
606  *  \param o Origin of proposed scale.
607  *  \return Snapped scale, if a snap occurred, and a flag indicating whether a snap occurred.
608  */
610 std::pair<NR::scale, bool> SnapManager::freeSnapScale(Inkscape::Snapper::PointType t,
611                                                       std::vector<NR::Point> const &p,
612                                                       std::list<SPItem const *> const &it,
613                                                       NR::scale const &s,
614                                                       NR::Point const &o) const
616     return _snapTransformed(
617         t, p, it, false, NR::Point(), SCALE, NR::Point(s[NR::X], s[NR::Y]), o, NR::X, false
618         );
622 /**
623  *  Try to snap a list of points to any interested snappers after they have undergone
624  *  a scale.  A snap will only occur along a line described by a
625  *  Inkscape::Snapper::ConstraintLine.
626  *
627  *  \param t Type of points.
628  *  \param p Points.
629  *  \param it List of items to ignore when snapping.
630  *  \param s Proposed scale.
631  *  \param o Origin of proposed scale.
632  *  \return Snapped scale, if a snap occurred, and a flag indicating whether a snap occurred.
633  */
635 std::pair<NR::scale, bool> SnapManager::constrainedSnapScale(Inkscape::Snapper::PointType t,
636                                                              std::vector<NR::Point> const &p,
637                                                              std::list<SPItem const *> const &it,
638                                                              NR::scale const &s,
639                                                              NR::Point const &o) const
641     // When constrained scaling, only uniform scaling is supported.
642     return _snapTransformed(
643         t, p, it, true, NR::Point(), SCALE, NR::Point(s[NR::X], s[NR::Y]), o, NR::X, true
644         );
648 /**
649  *  Try to snap a list of points to any interested snappers after they have undergone
650  *  a stretch.
651  *
652  *  \param t Type of points.
653  *  \param p Points.
654  *  \param it List of items to ignore when snapping.
655  *  \param s Proposed stretch.
656  *  \param o Origin of proposed stretch.
657  *  \param d Dimension in which to apply proposed stretch.
658  *  \param u true if the stretch should be uniform (ie to be applied equally in both dimensions)
659  *  \return Snapped stretch, if a snap occurred, and a flag indicating whether a snap occurred.
660  */
662 std::pair<NR::Coord, bool> SnapManager::constrainedSnapStretch(Inkscape::Snapper::PointType t,
663                                                         std::vector<NR::Point> const &p,
664                                                         std::list<SPItem const *> const &it,
665                                                         NR::Coord const &s,
666                                                         NR::Point const &o,
667                                                         NR::Dim2 d,
668                                                         bool u) const
670    std::pair<NR::Point, bool> const r = _snapTransformed(
671         t, p, it, true, NR::Point(), STRETCH, NR::Point(s, s), o, d, u
672         );
674    return std::make_pair(r.first[d], r.second);
678 /**
679  *  Try to snap a list of points to any interested snappers after they have undergone
680  *  a skew.
681  *
682  *  \param t Type of points.
683  *  \param p Points.
684  *  \param it List of items to ignore when snapping.
685  *  \param s Proposed skew.
686  *  \param o Origin of proposed skew.
687  *  \param d Dimension in which to apply proposed skew.
688  *  \return Snapped skew, if a snap occurred, and a flag indicating whether a snap occurred.
689  */
691 std::pair<NR::Coord, bool> SnapManager::freeSnapSkew(Inkscape::Snapper::PointType t,
692                                                      std::vector<NR::Point> const &p,
693                                                      std::list<SPItem const *> const &it,
694                                                      NR::Coord const &s,
695                                                      NR::Point const &o,
696                                                      NR::Dim2 d) const
698    std::pair<NR::Point, bool> const r = _snapTransformed(
699         t, p, it, false, NR::Point(), SKEW, NR::Point(s, s), o, d, false
700         );
702    return std::make_pair(r.first[d], r.second);
705 Inkscape::SnappedPoint SnapManager::findBestSnap(NR::Point const &p, SnappedConstraints &sc, bool constrained) const
707     // Store all snappoints
708     std::list<Inkscape::SnappedPoint> sp_list;
709     
710     // search for the closest snapped point
711     Inkscape::SnappedPoint closestPoint;
712     if (getClosestSP(sc.points, closestPoint)) {
713         sp_list.push_back(closestPoint);
714     } 
715     
716     // search for the closest snapped line segment
717     Inkscape::SnappedLineSegment closestLineSegment;
718     if (getClosestSLS(sc.lines, closestLineSegment)) {    
719         sp_list.push_back(Inkscape::SnappedPoint(closestLineSegment));
720     }
721     
722     if (_intersectionLS) {
723             // search for the closest snapped intersection of line segments
724             Inkscape::SnappedPoint closestLineSegmentIntersection;
725             if (getClosestIntersectionSLS(sc.lines, closestLineSegmentIntersection)) {
726                 sp_list.push_back(closestLineSegmentIntersection);
727             }
728     }    
730     // search for the closest snapped grid line
731     Inkscape::SnappedLine closestGridLine;
732     if (getClosestSL(sc.grid_lines, closestGridLine)) {    
733         sp_list.push_back(Inkscape::SnappedPoint(closestGridLine));
734     }
735     
736     // search for the closest snapped guide line
737     Inkscape::SnappedLine closestGuideLine;
738     if (getClosestSL(sc.guide_lines, closestGuideLine)) {
739         sp_list.push_back(Inkscape::SnappedPoint(closestGuideLine));
740     }
741     
742     // When freely snapping to a grid/guide/path, only one degree of freedom is eliminated
743     // Therefore we will try get fully constrained by finding an intersection with another grid/guide/path 
744     
745     // When doing a constrained snap however, we're already at an intersection of the constrained line and
746     // the grid/guide/path we're snapping to. This snappoint is therefore fully constrained, so there's
747     // no need to look for additional intersections
748     if (!constrained) {
749         // search for the closest snapped intersection of grid lines
750         Inkscape::SnappedPoint closestGridPoint;
751         if (getClosestIntersectionSL(sc.grid_lines, closestGridPoint)) {
752             sp_list.push_back(closestGridPoint);
753         }
754         
755         // search for the closest snapped intersection of guide lines
756         Inkscape::SnappedPoint closestGuidePoint;
757         if (getClosestIntersectionSL(sc.guide_lines, closestGuidePoint)) {
758             sp_list.push_back(closestGuidePoint);
759         }
760         
761         // search for the closest snapped intersection of grid with guide lines
762         if (_intersectionGG) {
763             Inkscape::SnappedPoint closestGridGuidePoint;
764             if (getClosestIntersectionSL(sc.grid_lines, sc.guide_lines, closestGridGuidePoint)) {
765                 sp_list.push_back(closestGridGuidePoint);
766             }
767         }
768     }
769     
770     // now let's see which snapped point gets a thumbs up
771     Inkscape::SnappedPoint bestSnappedPoint = Inkscape::SnappedPoint(p, NR_HUGE, 0, false);
772     for (std::list<Inkscape::SnappedPoint>::const_iterator i = sp_list.begin(); i != sp_list.end(); i++) {
773                 // first find out if this snapped point is within snapping range
774         if ((*i).getDistance() <= (*i).getTolerance()) {
775                 // if it's the first point
776                 bool c1 = (i == sp_list.begin());  
777                 // or, if it's closer
778                 bool c2 = (*i).getDistance() < bestSnappedPoint.getDistance();
779             // or, if it's for a snapper with "always snap" turned on, and the previous wasn't
780             bool c3 = (*i).getAlwaysSnap() && !bestSnappedPoint.getAlwaysSnap();
781                 // But in no case fall back from a snapper with "always snap" on to one with "always snap" off
782             bool c3n = !(*i).getAlwaysSnap() && bestSnappedPoint.getAlwaysSnap();
783             // or, if it's just as close then consider the second distance
784                 // (which is only relevant for points at an intersection)
785                 bool c4a = ((*i).getDistance() == bestSnappedPoint.getDistance()); 
786                 bool c4b = (*i).getSecondDistance() < bestSnappedPoint.getSecondDistance();
787                 // then prefer this point over the previous one
788             if ((c1 || c2 || c3 || (c4a && c4b)) && !c3n) {
789                 bestSnappedPoint = *i;
790             }
791         }
792     }
793     
794     return bestSnappedPoint;         
797 /*
798   Local Variables:
799   mode:c++
800   c-file-style:"stroustrup"
801   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
802   indent-tabs-mode:nil
803   fill-column:99
804   End:
805 */
806 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :