Code

Major overhaul of the selector tool's internals to improve handling of transformation...
[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-2008 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,
182                                              NR::Maybe<NR::Point> point_not_to_snap_to) const
185     std::list<SPItem const *> lit;
186     lit.push_back(it);
187     
188     std::vector<NR::Point> points_to_snap;
189     points_to_snap.push_back(p);
190     
191     return freeSnap(t, p, true, points_to_snap, lit, NULL);
194 /**
195  *  Try to snap a point to any interested snappers.
196  *
197  *  \param t Type of point.
198  *  \param p Point.
199  *  \param it Item to ignore when snapping.
200  *  \return Snapped point.
201  */
203 Inkscape::SnappedPoint SnapManager::freeSnap(Inkscape::Snapper::PointType t,
204                                              NR::Point const &p,
205                                              SPItem const *it,
206                                              std::vector<NR::Point> *unselected_nodes) const
209     std::list<SPItem const *> lit;
210     lit.push_back(it);
211     
212     std::vector<NR::Point> points_to_snap;
213     points_to_snap.push_back(p);
214     
215     return freeSnap(t, p, true, points_to_snap, lit, unselected_nodes);
219 /**
220  *  Try to snap a point to any of the specified snappers.
221  *
222  *  \param t Type of point.
223  *  \param p Point.
224  *  \param first_point If true then this point is the first one from a whole bunch of points 
225  *  \param points_to_snap The whole bunch of points, all from the same selection and having the same transformation 
226  *  \param it List of items to ignore when snapping.
227  * \param snappers  List of snappers to try to snap to
228  *  \return Snapped point.
229  */
231 Inkscape::SnappedPoint SnapManager::freeSnap(Inkscape::Snapper::PointType t,
232                                              NR::Point const &p,
233                                              bool const &first_point,
234                                              std::vector<NR::Point> &points_to_snap,
235                                              std::list<SPItem const *> const &it,
236                                              std::vector<NR::Point> *unselected_nodes) const
238     if (!SomeSnapperMightSnap()) {
239         return Inkscape::SnappedPoint(p, NR_HUGE, 0, false);
240     }
241     
242     SnappedConstraints sc;        
243     
244     SnapperList const snappers = getSnappers();
246     for (SnapperList::const_iterator i = snappers.begin(); i != snappers.end(); i++) {
247         (*i)->freeSnap(sc, t, p, first_point, points_to_snap, it, unselected_nodes);
248     }
250     return findBestSnap(p, sc, false);
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 c Constraint line.
260  *  \param it Item to ignore when snapping.
261  *  \return Snapped point.
262  */
264 Inkscape::SnappedPoint SnapManager::constrainedSnap(Inkscape::Snapper::PointType t,
265                                                     NR::Point const &p,
266                                                     Inkscape::Snapper::ConstraintLine const &c,
267                                                     SPItem const *it) const
269     std::list<SPItem const *> lit;
270     lit.push_back(it);
271     
272     std::vector<NR::Point> points_to_snap;
273     points_to_snap.push_back(p);
274     
275     return constrainedSnap(t, p, true, points_to_snap, c, lit);
280 /**
281  *  Try to snap a point to any interested snappers.  A snap will only occur along
282  *  a line described by a Inkscape::Snapper::ConstraintLine.
283  *
284  *  \param t Type of point.
285  *  \param p Point.
286  *  \param first_point If true then this point is the first one from a whole bunch of points 
287  *  \param points_to_snap The whole bunch of points, all from the same selection and having the same transformation 
288  *  \param c Constraint line.
289  *  \param it List of items to ignore when snapping.
290  *  \return Snapped point.
291  */
293 Inkscape::SnappedPoint SnapManager::constrainedSnap(Inkscape::Snapper::PointType t,
294                                                     NR::Point const &p,
295                                                     bool const &first_point,
296                                                     std::vector<NR::Point> &points_to_snap,
297                                                     Inkscape::Snapper::ConstraintLine const &c,
298                                                     std::list<SPItem const *> const &it) const
300     if (!SomeSnapperMightSnap()) {
301         return Inkscape::SnappedPoint(p, NR_HUGE, 0, false);
302     }
303     
304     SnappedConstraints sc;
305         
306     SnapperList const snappers = getSnappers();
307     for (SnapperList::const_iterator i = snappers.begin(); i != snappers.end(); i++) {
308         (*i)->constrainedSnap(sc, t, p, first_point, points_to_snap, c, it);
309     }
311     return findBestSnap(p, sc, true);
314 Inkscape::SnappedPoint SnapManager::guideSnap(NR::Point const &p,
315                                               NR::Point const &guide_normal) const
317     // This method is used to snap a guide to nodes, while dragging the guide around
318     
319     if (!(object.GuidesMightSnap() && _snap_enabled_globally)) {
320         return Inkscape::SnappedPoint(p, NR_HUGE, 0, false);
321     }
322     
323     SnappedConstraints sc;
324     object.guideSnap(sc, p, guide_normal);
325     
326     return findBestSnap(p, sc, false);    
330 /**
331  *  Main internal snapping method, which is called by the other, friendlier, public
332  *  methods.  It's a bit hairy as it has lots of parameters, but it saves on a lot
333  *  of duplicated code.
334  *
335  *  \param type Type of points being snapped.
336  *  \param points List of points to snap.
337  *  \param ignore List of items to ignore while snapping.
338  *  \param constrained true if the snap is constrained.
339  *  \param constraint Constraint line to use, if `constrained' is true, otherwise undefined.
340  *  \param transformation_type Type of transformation to apply to points before trying to snap them.
341  *  \param transformation Description of the transformation; details depend on the type.
342  *  \param origin Origin of the transformation, if applicable.
343  *  \param dim Dimension of the transformation, if applicable.
344  *  \param uniform true if the transformation should be uniform; only applicable for stretching and scaling.
345  */
347 std::pair<NR::Point, bool> SnapManager::_snapTransformed(
348     Inkscape::Snapper::PointType type,
349     std::vector<NR::Point> const &points,
350     std::list<SPItem const *> const &ignore,
351     bool constrained,
352     Inkscape::Snapper::ConstraintLine const &constraint,
353     Transformation transformation_type,
354     NR::Point const &transformation,
355     NR::Point const &origin,
356     NR::Dim2 dim,
357     bool uniform) const
359     /* We have a list of points, which we are proposing to transform in some way.  We need to see
360     ** if any of these points, when transformed, snap to anything.  If they do, we return the
361     ** appropriate transformation with `true'; otherwise we return the original scale with `false'.
362     */
364     /* Quick check to see if we have any snappers that are enabled
365     ** Also used to globally disable all snapping 
366     */
367     if (SomeSnapperMightSnap() == false) {
368         return std::make_pair(transformation, false);
369     }
370     
371     std::vector<NR::Point> transformed_points;
372     
373     for (std::vector<NR::Point>::const_iterator i = points.begin(); i != points.end(); i++) {
375         /* Work out the transformed version of this point */
376         NR::Point transformed;
377         switch (transformation_type) {
378             case TRANSLATION:
379                 transformed = *i + transformation;
380                 break;
381             case SCALE:
382                 transformed = ((*i - origin) * NR::scale(transformation[NR::X], transformation[NR::Y])) + origin;
383                 break;
384             case STRETCH:
385             {
386                 NR::scale s(1, 1);
387                 if (uniform)
388                     s[NR::X] = s[NR::Y] = transformation[dim];
389                 else {
390                     s[dim] = transformation[dim];
391                     s[1 - dim] = 1;
392                 }
393                 transformed = ((*i - origin) * s) + origin;
394                 break;
395             }
396             case SKEW:
397                 transformed = *i;
398                 transformed[dim] += transformation[dim] * ((*i)[1 - dim] - origin[1 - dim]);
399                 break;
400             default:
401                 g_assert_not_reached();
402         }
403         
404         // add the current transformed point to the box hulling all transformed points
405         transformed_points.push_back(transformed);
406     }    
407     
408     /* The current best transformation */
409     NR::Point best_transformation = transformation;
411     /* The current best metric for the best transformation; lower is better, NR_HUGE
412     ** means that we haven't snapped anything.
413     */
414     NR::Coord best_metric = NR_HUGE;
415     NR::Coord best_second_metric = NR_HUGE;
416     NR::Point best_scale_metric(NR_HUGE, NR_HUGE);
417     bool best_at_intersection = false;
418     bool best_always_snap = false;
420     std::vector<NR::Point>::const_iterator j = transformed_points.begin();
422     //std::cout << std::endl;
423     
424     for (std::vector<NR::Point>::const_iterator i = points.begin(); i != points.end(); i++) {
425         
426         /* Snap it */        
427         Inkscape::SnappedPoint snapped;
428                 
429         if (constrained) {    
430             Inkscape::Snapper::ConstraintLine dedicated_constraint = constraint;
431             if ((transformation_type == SCALE || transformation_type == STRETCH) && uniform) {
432                 // When uniformly scaling, each point will have its own unique constraint line,
433                 // running from the scaling origin to the original untransformed point. We will
434                 // calculate that line here 
435                 dedicated_constraint = Inkscape::Snapper::ConstraintLine(origin, (*i) - origin);
436             } else if (transformation_type == STRETCH || transformation_type == SKEW) { // when skewing or non-uniform stretching {
437                 dedicated_constraint = Inkscape::Snapper::ConstraintLine((*i), component_vectors[dim]);
438             } // else: leave the original constraint, e.g. for constrained translation 
439             if (transformation_type == SCALE && !uniform) {
440                 g_warning("Non-uniform constrained scaling is not supported!");   
441             }
442             snapped = constrainedSnap(type, *j, i == points.begin(), transformed_points, dedicated_constraint, ignore);
443         } else {
444             snapped = freeSnap(type, *j, i == points.begin(), transformed_points, ignore, NULL);
445         }
447         NR::Point result;
448         NR::Coord metric = NR_HUGE;
449         NR::Coord second_metric = NR_HUGE;
450         NR::Point scale_metric(NR_HUGE, NR_HUGE);
451         
452         if (snapped.getDistance() < NR_HUGE) {
453             /* We snapped.  Find the transformation that describes where the snapped point has
454             ** ended up, and also the metric for this transformation.
455             */
456             NR::Point const a = (snapped.getPoint() - origin); // vector to snapped point
457             NR::Point const b = (*i - origin); // vector to original point
458             
459             switch (transformation_type) {
460                 case TRANSLATION:
461                     result = snapped.getPoint() - *i;
462                     /* Consider the case in which a box is almost aligned with a grid in both 
463                      * horizontal and vertical directions. The distance to the intersection of
464                      * the grid lines will always be larger then the distance to a single grid
465                      * line. If we prefer snapping to an intersection instead of to a single 
466                      * grid line, then we cannot use "metric = NR::L2(result)". Therefore the
467                      * snapped distance will be used as a metric. Please note that the snapped
468                      * distance is defined as the distance to the nearest line of the intersection,
469                      * and not to the intersection itself! 
470                      */
471                     metric = snapped.getDistance(); //used to be: metric = NR::L2(result);
472                     second_metric = snapped.getSecondDistance();
473                     break;
474                 case SCALE:
475                 {
476                     result = NR::Point(NR_HUGE, NR_HUGE);
477                     // If this point *i is horizontally or vertically aligned with
478                     // the origin of the scaling, then it will scale purely in X or Y 
479                     // We can therefore only calculate the scaling in this direction
480                     // and the scaling factor for the other direction should remain
481                     // untouched (unless scaling is uniform ofcourse)
482                     for (int index = 0; index < 2; index++) {
483                         if (fabs(b[index]) > 1e-6) { // if SCALING CAN occur in this direction
484                             if (fabs(fabs(a[index]/b[index]) - fabs(transformation[index])) > 1e-12) { // if SNAPPING DID occur in this direction
485                                 result[index] = a[index] / b[index]; // then calculate it!
486                             }
487                             // we might leave result[1-index] = NR_HUGE
488                             // if scaling didn't occur in the other direction
489                         }
490                     }
491                     // Compare the resulting scaling with the desired scaling
492                     scale_metric = result - transformation; // One or both of its components might be NR_HUGE
493                     break;
494                 }
495                 case STRETCH:
496                     result = NR::Point(NR_HUGE, NR_HUGE);
497                     if (fabs(b[dim]) > 1e-6) { // if STRETCHING will occur for this point
498                         result[dim] = a[dim] / b[dim];
499                         result[1-dim] = uniform ? result[dim] : 1;
500                     } else { // STRETCHING might occur for this point, but only when the stretching is uniform
501                         if (uniform && fabs(b[1-dim]) > 1e-6) {
502                            result[1-dim] = a[1-dim] / b[1-dim];
503                            result[dim] = result[1-dim];
504                         }
505                     }
506                     metric = std::abs(result[dim] - transformation[dim]);
507                     break;
508                 case SKEW:
509                     result[dim] = (snapped.getPoint()[dim] - (*i)[dim]) / ((*i)[1 - dim] - origin[1 - dim]);
510                     metric = std::abs(result[dim] - transformation[dim]);
511                     break;
512                 default:
513                     g_assert_not_reached();
514             }
515             
516             /* Note it if it's the best so far */
517             if (transformation_type == SCALE) {
518                 for (int index = 0; index < 2; index++) {
519                     if (fabs(scale_metric[index]) < fabs(best_scale_metric[index])) {
520                         best_transformation[index] = result[index];
521                         best_scale_metric[index] = fabs(scale_metric[index]);
522                         //std::cout << "SEL ";
523                     } //else { std::cout << "    ";}   
524                 }
525                 if (uniform) {
526                     if (best_scale_metric[0] < best_scale_metric[1]) {
527                         best_transformation[1] = best_transformation[0];
528                         best_scale_metric[1] = best_scale_metric[0]; 
529                     } else {
530                         best_transformation[0] = best_transformation[1];
531                         best_scale_metric[0] = best_scale_metric[1];
532                     }
533                 }
534                 best_metric = std::min(best_scale_metric[0], best_scale_metric[1]);
535                 //std::cout << "P_orig = " << (*i) << " | scale_metric = " << scale_metric << " | distance = " << snapped.getDistance() << " | P_snap = " << snapped.getPoint() << std::endl;
536             } else {
537                 bool const c1 = metric < best_metric;
538                 bool const c2 = metric == best_metric && snapped.getAtIntersection() == true && best_at_intersection == false;
539                         bool const c3a = metric == best_metric && snapped.getAtIntersection() == true && best_at_intersection == true;
540                 bool const c3b = second_metric < best_second_metric;
541                 bool const c4 = snapped.getAlwaysSnap() == true && best_always_snap == false;
542                 bool const c4n = snapped.getAlwaysSnap() == false && best_always_snap == true;
543                 
544                 if ((c1 || c2 || (c3a && c3b) || c4) && !c4n) {
545                     best_transformation = result;
546                     best_metric = metric;
547                     best_second_metric = second_metric;
548                     best_at_intersection = snapped.getAtIntersection();
549                     best_always_snap = snapped.getAlwaysSnap(); 
550                     //std::cout << "SEL ";
551                 } //else { std::cout << "    ";}
552                 //std::cout << "P_orig = " << (*i) << " | metric = " << metric << " | distance = " << snapped.getDistance() << " | second metric = " << second_metric << " | P_snap = " << snapped.getPoint() << std::endl;
553             }
554         }
555         
556         j++;
557     }
558     
559     if (transformation_type == SCALE) {
560         // When scaling, don't ever exit with one of scaling components set to NR_HUGE
561         for (int index = 0; index < 2; index++) {
562             if (best_transformation[index] == NR_HUGE) {
563                 if (uniform && best_transformation[1-index] < NR_HUGE) {
564                         best_transformation[index] = best_transformation[1-index];
565                 } else {
566                         best_transformation[index] = transformation[index];     
567                 }
568             }
569         }
570     }
571     
572     // Using " < 1e6" instead of " < NR_HUGE" for catching some rounding errors
573     // These rounding errors might be caused by NRRects, see bug #1584301
574     return std::make_pair(best_transformation, best_metric < 1e6);
578 /**
579  *  Try to snap a list of points to any interested snappers after they have undergone
580  *  a translation.
581  *
582  *  \param t Type of points.
583  *  \param p Points.
584  *  \param it List of items to ignore when snapping.
585  *  \param tr Proposed translation.
586  *  \return Snapped translation, if a snap occurred, and a flag indicating whether a snap occurred.
587  */
589 std::pair<NR::Point, bool> SnapManager::freeSnapTranslation(Inkscape::Snapper::PointType t,
590                                                             std::vector<NR::Point> const &p,
591                                                             std::list<SPItem const *> const &it,
592                                                             NR::Point const &tr) const
594     return _snapTransformed(
595         t, p, it, false, NR::Point(), TRANSLATION, tr, NR::Point(), NR::X, false
596         );
600 /**
601  *  Try to snap a list of points to any interested snappers after they have undergone a
602  *  translation.  A snap will only occur along a line described by a
603  *  Inkscape::Snapper::ConstraintLine.
604  *
605  *  \param t Type of points.
606  *  \param p Points.
607  *  \param it List of items to ignore when snapping.
608  *  \param c Constraint line.
609  *  \param tr Proposed translation.
610  *  \return Snapped translation, if a snap occurred, and a flag indicating whether a snap occurred.
611  */
613 std::pair<NR::Point, bool> SnapManager::constrainedSnapTranslation(Inkscape::Snapper::PointType t,
614                                                                    std::vector<NR::Point> const &p,
615                                                                    std::list<SPItem const *> const &it,
616                                                                    Inkscape::Snapper::ConstraintLine const &c,
617                                                                    NR::Point const &tr) const
619     return _snapTransformed(
620         t, p, it, true, c, TRANSLATION, tr, NR::Point(), NR::X, false
621         );
625 /**
626  *  Try to snap a list of points to any interested snappers after they have undergone
627  *  a scale.
628  *
629  *  \param t Type of points.
630  *  \param p Points.
631  *  \param it List of items to ignore when snapping.
632  *  \param s Proposed scale.
633  *  \param o Origin of proposed scale.
634  *  \return Snapped scale, if a snap occurred, and a flag indicating whether a snap occurred.
635  */
637 std::pair<NR::scale, bool> SnapManager::freeSnapScale(Inkscape::Snapper::PointType t,
638                                                       std::vector<NR::Point> const &p,
639                                                       std::list<SPItem const *> const &it,
640                                                       NR::scale const &s,
641                                                       NR::Point const &o) const
643     return _snapTransformed(
644         t, p, it, false, NR::Point(), SCALE, NR::Point(s[NR::X], s[NR::Y]), o, NR::X, false
645         );
649 /**
650  *  Try to snap a list of points to any interested snappers after they have undergone
651  *  a scale.  A snap will only occur along a line described by a
652  *  Inkscape::Snapper::ConstraintLine.
653  *
654  *  \param t Type of points.
655  *  \param p Points.
656  *  \param it List of items to ignore when snapping.
657  *  \param s Proposed scale.
658  *  \param o Origin of proposed scale.
659  *  \return Snapped scale, if a snap occurred, and a flag indicating whether a snap occurred.
660  */
662 std::pair<NR::scale, bool> SnapManager::constrainedSnapScale(Inkscape::Snapper::PointType t,
663                                                              std::vector<NR::Point> const &p,
664                                                              std::list<SPItem const *> const &it,
665                                                              NR::scale const &s,
666                                                              NR::Point const &o) const
668     // When constrained scaling, only uniform scaling is supported.
669     return _snapTransformed(
670         t, p, it, true, NR::Point(), SCALE, NR::Point(s[NR::X], s[NR::Y]), o, NR::X, true
671         );
675 /**
676  *  Try to snap a list of points to any interested snappers after they have undergone
677  *  a stretch.
678  *
679  *  \param t Type of points.
680  *  \param p Points.
681  *  \param it List of items to ignore when snapping.
682  *  \param s Proposed stretch.
683  *  \param o Origin of proposed stretch.
684  *  \param d Dimension in which to apply proposed stretch.
685  *  \param u true if the stretch should be uniform (ie to be applied equally in both dimensions)
686  *  \return Snapped stretch, if a snap occurred, and a flag indicating whether a snap occurred.
687  */
689 std::pair<NR::Coord, bool> SnapManager::constrainedSnapStretch(Inkscape::Snapper::PointType t,
690                                                         std::vector<NR::Point> const &p,
691                                                         std::list<SPItem const *> const &it,
692                                                         NR::Coord const &s,
693                                                         NR::Point const &o,
694                                                         NR::Dim2 d,
695                                                         bool u) const
697    std::pair<NR::Point, bool> const r = _snapTransformed(
698         t, p, it, true, NR::Point(), STRETCH, NR::Point(s, s), o, d, u
699         );
701    return std::make_pair(r.first[d], r.second);
705 /**
706  *  Try to snap a list of points to any interested snappers after they have undergone
707  *  a skew.
708  *
709  *  \param t Type of points.
710  *  \param p Points.
711  *  \param it List of items to ignore when snapping.
712  *  \param s Proposed skew.
713  *  \param o Origin of proposed skew.
714  *  \param d Dimension in which to apply proposed skew.
715  *  \return Snapped skew, if a snap occurred, and a flag indicating whether a snap occurred.
716  */
718 std::pair<NR::Coord, bool> SnapManager::freeSnapSkew(Inkscape::Snapper::PointType t,
719                                                      std::vector<NR::Point> const &p,
720                                                      std::list<SPItem const *> const &it,
721                                                      NR::Coord const &s,
722                                                      NR::Point const &o,
723                                                      NR::Dim2 d) const
725    std::pair<NR::Point, bool> const r = _snapTransformed(
726         t, p, it, false, NR::Point(), SKEW, NR::Point(s, s), o, d, false
727         );
729    return std::make_pair(r.first[d], r.second);
732 Inkscape::SnappedPoint SnapManager::findBestSnap(NR::Point const &p, SnappedConstraints &sc, bool constrained) const
734     /*
735     std::cout << "Type and number of snapped constraints: " << std::endl;
736     std::cout << "  Points      : " << sc.points.size() << std::endl;
737     std::cout << "  Lines       : " << sc.lines.size() << std::endl;
738     std::cout << "  Grid lines  : " << sc.grid_lines.size()<< std::endl;
739     std::cout << "  Guide lines : " << sc.guide_lines.size()<< std::endl;
740     */
741         
742     // Store all snappoints
743     std::list<Inkscape::SnappedPoint> sp_list;
744     
745     // search for the closest snapped point
746     Inkscape::SnappedPoint closestPoint;
747     if (getClosestSP(sc.points, closestPoint)) {
748         sp_list.push_back(closestPoint);
749     } 
750     
751     // search for the closest snapped line segment
752     Inkscape::SnappedLineSegment closestLineSegment;
753     if (getClosestSLS(sc.lines, closestLineSegment)) {    
754         sp_list.push_back(Inkscape::SnappedPoint(closestLineSegment));
755     }
756     
757     if (_intersectionLS) {
758             // search for the closest snapped intersection of line segments
759             Inkscape::SnappedPoint closestLineSegmentIntersection;
760             if (getClosestIntersectionSLS(sc.lines, closestLineSegmentIntersection)) {
761                 sp_list.push_back(closestLineSegmentIntersection);
762             }
763     }    
765     // search for the closest snapped grid line
766     Inkscape::SnappedLine closestGridLine;
767     if (getClosestSL(sc.grid_lines, closestGridLine)) {    
768         sp_list.push_back(Inkscape::SnappedPoint(closestGridLine));
769     }
770     
771     // search for the closest snapped guide line
772     Inkscape::SnappedLine closestGuideLine;
773     if (getClosestSL(sc.guide_lines, closestGuideLine)) {
774         sp_list.push_back(Inkscape::SnappedPoint(closestGuideLine));
775     }
776     
777     // When freely snapping to a grid/guide/path, only one degree of freedom is eliminated
778     // Therefore we will try get fully constrained by finding an intersection with another grid/guide/path 
779     
780     // When doing a constrained snap however, we're already at an intersection of the constrained line and
781     // the grid/guide/path we're snapping to. This snappoint is therefore fully constrained, so there's
782     // no need to look for additional intersections
783     if (!constrained) {
784         // search for the closest snapped intersection of grid lines
785         Inkscape::SnappedPoint closestGridPoint;
786         if (getClosestIntersectionSL(sc.grid_lines, closestGridPoint)) {
787             sp_list.push_back(closestGridPoint);
788         }
789         
790         // search for the closest snapped intersection of guide lines
791         Inkscape::SnappedPoint closestGuidePoint;
792         if (getClosestIntersectionSL(sc.guide_lines, closestGuidePoint)) {
793             sp_list.push_back(closestGuidePoint);
794         }
795         
796         // search for the closest snapped intersection of grid with guide lines
797         if (_intersectionGG) {
798             Inkscape::SnappedPoint closestGridGuidePoint;
799             if (getClosestIntersectionSL(sc.grid_lines, sc.guide_lines, closestGridGuidePoint)) {
800                 sp_list.push_back(closestGridGuidePoint);
801             }
802         }
803     }
804     
805     // now let's see which snapped point gets a thumbs up
806     Inkscape::SnappedPoint bestSnappedPoint = Inkscape::SnappedPoint(p, NR_HUGE, 0, false);
807     for (std::list<Inkscape::SnappedPoint>::const_iterator i = sp_list.begin(); i != sp_list.end(); i++) {
808                 // first find out if this snapped point is within snapping range
809         if ((*i).getDistance() <= (*i).getTolerance()) {
810                 // if it's the first point
811                 bool c1 = (i == sp_list.begin());  
812                 // or, if it's closer
813                 bool c2 = (*i).getDistance() < bestSnappedPoint.getDistance();
814             // or, if it's for a snapper with "always snap" turned on, and the previous wasn't
815             bool c3 = (*i).getAlwaysSnap() && !bestSnappedPoint.getAlwaysSnap();
816                 // But in no case fall back from a snapper with "always snap" on to one with "always snap" off
817             bool c3n = !(*i).getAlwaysSnap() && bestSnappedPoint.getAlwaysSnap();
818             // or, if it's just as close then consider the second distance
819                 // (which is only relevant for points at an intersection)
820                 bool c4a = ((*i).getDistance() == bestSnappedPoint.getDistance()); 
821                 bool c4b = (*i).getSecondDistance() < bestSnappedPoint.getSecondDistance();
822                 // then prefer this point over the previous one
823             if ((c1 || c2 || c3 || (c4a && c4b)) && !c3n) {
824                 bestSnappedPoint = *i;
825             }
826         }
827     }
828     
829     return bestSnappedPoint;         
832 /*
833   Local Variables:
834   mode:c++
835   c-file-style:"stroustrup"
836   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
837   indent-tabs-mode:nil
838   fill-column:99
839   End:
840 */
841 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :