Code

Replace freeSnapSkew() by constrainedSnapSkew(). There is no such thing as freely...
[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"
32 #include "display/snap-indicator.h"
34 #include "inkscape.h"
35 #include "desktop.h"
36 #include "sp-guide.h"
37 using std::vector;
39 /**
40  *  Construct a SnapManager for a SPNamedView.
41  *
42  *  \param v `Owning' SPNamedView.
43  */
45 SnapManager::SnapManager(SPNamedView const *v) :
46     guide(v, 0),
47     object(v, 0),
48     _named_view(v),
49     _include_item_center(false),
50     _snap_enabled_globally(true)
51 {    
52 }
55 /**
56  *  \return List of snappers that we use.
57  */
58 SnapManager::SnapperList 
59 SnapManager::getSnappers() const
60 {
61     SnapManager::SnapperList s;
62     s.push_back(&guide);
63     s.push_back(&object);
65     SnapManager::SnapperList gs = getGridSnappers();
66     s.splice(s.begin(), gs);
68     return s;
69 }
71 /**
72  *  \return List of gridsnappers that we use.
73  */
74 SnapManager::SnapperList 
75 SnapManager::getGridSnappers() const
76 {
77     SnapperList s;
79     //FIXME: this code should actually do this: add new grid snappers that are active for this desktop. now it just adds all gridsnappers
80     SPDesktop* desktop = SP_ACTIVE_DESKTOP;
81     if (desktop && desktop->gridsEnabled()) {
82         for ( GSList const *l = _named_view->grids; l != NULL; l = l->next) {
83             Inkscape::CanvasGrid *grid = (Inkscape::CanvasGrid*) l->data;
84             s.push_back(grid->snapper);
85         }
86     }
88     return s;
89 }
91 /**
92  * \return true if one of the snappers will try to snap something.
93  */
95 bool SnapManager::SomeSnapperMightSnap() const
96 {
97     if (!_snap_enabled_globally) {
98         return false;
99     }
100     
101     SnapperList const s = getSnappers();
102     SnapperList::const_iterator i = s.begin();
103     while (i != s.end() && (*i)->ThisSnapperMightSnap() == false) {
104         i++;
105     }
106     
107     return (i != s.end());
110 /*
111  *  The snappers have too many parameters to adjust individually. Therefore only
112  *  two snapping modes are presented to the user: snapping bounding box corners (to 
113  *  other bounding boxes, grids or guides), and/or snapping nodes (to other nodes,
114  *  paths, grids or guides). To select either of these modes (or both), use the 
115  *  methods defined below: setSnapModeBBox() and setSnapModeNode().
116  * 
117  * */
120 void SnapManager::setSnapModeBBox(bool enabled)
122     //The default values are being set in sp_namedview_set() (in sp-namedview.cpp)
123     guide.setSnapFrom(Inkscape::Snapper::SNAPPOINT_BBOX, enabled);
124     
125     for ( GSList const *l = _named_view->grids; l != NULL; l = l->next) {
126         Inkscape::CanvasGrid *grid = (Inkscape::CanvasGrid*) l->data;
127         grid->snapper->setSnapFrom(Inkscape::Snapper::SNAPPOINT_BBOX, enabled);
128     }
129     
130     object.setSnapFrom(Inkscape::Snapper::SNAPPOINT_BBOX, enabled);
131     //object.setSnapToBBoxNode(enabled); // On second thought, these should be controlled
132     //object.setSnapToBBoxPath(enabled); // separately by the snapping prefs dialog
133     object.setStrictSnapping(true); //don't snap bboxes to nodes/paths and vice versa    
136 bool SnapManager::getSnapModeBBox() const
138     return guide.getSnapFrom(Inkscape::Snapper::SNAPPOINT_BBOX);
141 void SnapManager::setSnapModeNode(bool enabled)
143     guide.setSnapFrom(Inkscape::Snapper::SNAPPOINT_NODE, enabled);
144     
145     for ( GSList const *l = _named_view->grids; l != NULL; l = l->next) {
146         Inkscape::CanvasGrid *grid = (Inkscape::CanvasGrid*) l->data;
147         grid->snapper->setSnapFrom(Inkscape::Snapper::SNAPPOINT_NODE, enabled);
148     }
149         
150     object.setSnapFrom(Inkscape::Snapper::SNAPPOINT_NODE, enabled);
151     //object.setSnapToItemNode(enabled); // On second thought, these should be controlled
152     //object.setSnapToItemPath(enabled); // separately by the snapping prefs dialog 
153     object.setStrictSnapping(true);
156 bool SnapManager::getSnapModeNode() const
158     return guide.getSnapFrom(Inkscape::Snapper::SNAPPOINT_NODE);
161 void SnapManager::setSnapModeGuide(bool enabled)
163     object.setSnapFrom(Inkscape::Snapper::SNAPPOINT_GUIDE, enabled);
166 bool SnapManager::getSnapModeGuide() const
168     return object.getSnapFrom(Inkscape::Snapper::SNAPPOINT_GUIDE);
171 /**
172  *  Try to snap a point to any of the specified snappers.
173  *
174  *  \param point_type Type of point.
175  *  \param p Point.
176  *  \param first_point If true then this point is the first one from a whole bunch of points 
177  *  \param points_to_snap The whole bunch of points, all from the same selection and having the same transformation 
178  *  \param snappers List of snappers to try to snap to
179  *  \return Snapped point.
180  */
182 Inkscape::SnappedPoint SnapManager::freeSnap(Inkscape::Snapper::PointType point_type,
183                                              NR::Point const &p,
184                                              bool first_point,
185                                              NR::Maybe<NR::Rect> const &bbox_to_snap) const
187     if (!SomeSnapperMightSnap()) {
188         return Inkscape::SnappedPoint(p, NR_HUGE, 0, false);
189     }
190     
191     std::vector<SPItem const *> *items_to_ignore;
192     if (_item_to_ignore) { // If we have only a single item to ignore 
193         // then build a list containing this single item; 
194         // This single-item list will prevail over any other _items_to_ignore list, should that exist
195         items_to_ignore = new std::vector<SPItem const *>;
196         items_to_ignore->push_back(_item_to_ignore);
197     } else {
198         items_to_ignore = _items_to_ignore;
199     }
200     
201     SnappedConstraints sc;
202     SnapperList const snappers = getSnappers();
203     
204     for (SnapperList::const_iterator i = snappers.begin(); i != snappers.end(); i++) {
205         (*i)->freeSnap(sc, point_type, p, first_point, bbox_to_snap, items_to_ignore, _unselected_nodes);
206     }
207     
208     if (_item_to_ignore) {
209         delete items_to_ignore;   
210     }
211     
212     return findBestSnap(p, sc, false);
215 /**
216  *  Try to snap a point to any interested snappers.  A snap will only occur along
217  *  a line described by a Inkscape::Snapper::ConstraintLine.
218  *
219  *  \param point_type Type of point.
220  *  \param p Point.
221  *  \param first_point If true then this point is the first one from a whole bunch of points 
222  *  \param points_to_snap The whole bunch of points, all from the same selection and having the same transformation 
223  *  \param constraint Constraint line.
224  *  \return Snapped point.
225  */
227 Inkscape::SnappedPoint SnapManager::constrainedSnap(Inkscape::Snapper::PointType point_type,
228                                                     NR::Point const &p,
229                                                     Inkscape::Snapper::ConstraintLine const &constraint,
230                                                     bool first_point,
231                                                     NR::Maybe<NR::Rect> const &bbox_to_snap) const
233     if (!SomeSnapperMightSnap()) {
234         return Inkscape::SnappedPoint(p, NR_HUGE, 0, false);
235     }
236     
237     std::vector<SPItem const *> *items_to_ignore;
238     if (_item_to_ignore) { // If we have only a single item to ignore 
239         // then build a list containing this single item; 
240         // This single-item list will prevail over any other _items_to_ignore list, should that exist
241         items_to_ignore = new std::vector<SPItem const *>;
242         items_to_ignore->push_back(_item_to_ignore);
243     } else {
244         items_to_ignore = _items_to_ignore;
245     }
246     
247     SnappedConstraints sc;    
248     SnapperList const snappers = getSnappers();
249     for (SnapperList::const_iterator i = snappers.begin(); i != snappers.end(); i++) {
250         (*i)->constrainedSnap(sc, point_type, p, first_point, bbox_to_snap, constraint, items_to_ignore);
251     }
252     
253     if (_item_to_ignore) {
254         delete items_to_ignore;   
255     }
256     
257     return findBestSnap(p, sc, true);
260 Inkscape::SnappedPoint SnapManager::guideSnap(NR::Point const &p,
261                                               NR::Point const &guide_normal) const
263     // This method is used to snap a guide to nodes, while dragging the guide around
264     
265     if (!(object.GuidesMightSnap() && _snap_enabled_globally)) {
266         return Inkscape::SnappedPoint(p, NR_HUGE, 0, false);
267     }
268     
269     SnappedConstraints sc;
270     object.guideSnap(sc, p, guide_normal);
271     
272     return findBestSnap(p, sc, false);    
276 /**
277  *  Main internal snapping method, which is called by the other, friendlier, public
278  *  methods.  It's a bit hairy as it has lots of parameters, but it saves on a lot
279  *  of duplicated code.
280  *
281  *  \param type Type of points being snapped.
282  *  \param points List of points to snap.
283  *  \param constrained true if the snap is constrained.
284  *  \param constraint Constraint line to use, if `constrained' is true, otherwise undefined.
285  *  \param transformation_type Type of transformation to apply to points before trying to snap them.
286  *  \param transformation Description of the transformation; details depend on the type.
287  *  \param origin Origin of the transformation, if applicable.
288  *  \param dim Dimension of the transformation, if applicable.
289  *  \param uniform true if the transformation should be uniform; only applicable for stretching and scaling.
290  */
292 Inkscape::SnappedPoint SnapManager::_snapTransformed(
293     Inkscape::Snapper::PointType type,
294     std::vector<NR::Point> const &points,
295     bool constrained,
296     Inkscape::Snapper::ConstraintLine const &constraint,
297     Transformation transformation_type,
298     NR::Point const &transformation,
299     NR::Point const &origin,
300     NR::Dim2 dim,
301     bool uniform) const
303     /* We have a list of points, which we are proposing to transform in some way.  We need to see
304     ** if any of these points, when transformed, snap to anything.  If they do, we return the
305     ** appropriate transformation with `true'; otherwise we return the original scale with `false'.
306     */
308     /* Quick check to see if we have any snappers that are enabled
309     ** Also used to globally disable all snapping 
310     */
311     if (SomeSnapperMightSnap() == false) {
312         return Inkscape::SnappedPoint();
313     }
314     
315     std::vector<NR::Point> transformed_points;
316     NR::Rect bbox;
317     
318     for (std::vector<NR::Point>::const_iterator i = points.begin(); i != points.end(); i++) {
320         /* Work out the transformed version of this point */
321         NR::Point transformed;
322         switch (transformation_type) {
323             case TRANSLATION:
324                 transformed = *i + transformation;
325                 break;
326             case SCALE:
327                 transformed = (*i - origin) * NR::scale(transformation[NR::X], transformation[NR::Y]) + origin;
328                 break;
329             case STRETCH:
330             {
331                 NR::scale s(1, 1);
332                 if (uniform)
333                     s[NR::X] = s[NR::Y] = transformation[dim];
334                 else {
335                     s[dim] = transformation[dim];
336                     s[1 - dim] = 1;
337                 }
338                 transformed = ((*i - origin) * s) + origin;
339                 break;
340             }
341             case SKEW:
342                 // Apply the skew factor
343                 transformed[dim] = (*i)[dim] + transformation[0] * ((*i)[1 - dim] - origin[1 - dim]);
344                 // While skewing, mirroring and scaling (by integer multiples) in the opposite direction is also allowed.
345                 // Apply that scale factor here
346                 transformed[1-dim] = (*i - origin)[1 - dim] * transformation[1] + origin[1 - dim];
347                 break;
348             default:
349                 g_assert_not_reached();
350         }
351         
352         // add the current transformed point to the box hulling all transformed points
353         if (i == points.begin()) {
354             bbox = NR::Rect(transformed, transformed);    
355         } else {
356             bbox.expandTo(transformed);
357         }
358         
359         transformed_points.push_back(transformed);
360     }    
361     
362     /* The current best transformation */
363     NR::Point best_transformation = transformation;
365     /* The current best metric for the best transformation; lower is better, NR_HUGE
366     ** means that we haven't snapped anything.
367     */
368     NR::Coord best_metric = NR_HUGE;
369     NR::Coord best_second_metric = NR_HUGE;
370     NR::Point best_scale_metric(NR_HUGE, NR_HUGE);
371     Inkscape::SnappedPoint best_snapped_point;
372     g_assert(best_snapped_point.getAlwaysSnap() == false); // Check initialization of snapped point
373     g_assert(best_snapped_point.getAtIntersection() == false);
375     std::vector<NR::Point>::const_iterator j = transformed_points.begin();
377     // std::cout << std::endl;
378     for (std::vector<NR::Point>::const_iterator i = points.begin(); i != points.end(); i++) {
379         
380         /* Snap it */        
381         Inkscape::SnappedPoint snapped_point;
382                 
383         if (constrained) {    
384             Inkscape::Snapper::ConstraintLine dedicated_constraint = constraint;
385             if ((transformation_type == SCALE || transformation_type == STRETCH) && uniform) {
386                 // When uniformly scaling, each point will have its own unique constraint line,
387                 // running from the scaling origin to the original untransformed point. We will
388                 // calculate that line here 
389                 dedicated_constraint = Inkscape::Snapper::ConstraintLine(origin, (*i) - origin);
390             } else if (transformation_type == STRETCH) { // when non-uniform stretching {
391                 dedicated_constraint = Inkscape::Snapper::ConstraintLine((*i), component_vectors[dim]);
392             } // else: leave the original constraint, e.g. for constrained translation and skewing 
393             if (transformation_type == SCALE && !uniform) {
394                 g_warning("Non-uniform constrained scaling is not supported!");   
395             }
396             snapped_point = constrainedSnap(type, *j, dedicated_constraint, i == points.begin(), bbox);
397         } else {
398             snapped_point = freeSnap(type, *j, i == points.begin(), bbox);
399         }
401         NR::Point result;
402         NR::Coord metric = NR_HUGE;
403         NR::Coord second_metric = NR_HUGE;
404         NR::Point scale_metric(NR_HUGE, NR_HUGE);
405         
406         if (snapped_point.getSnapped()) {
407             /* We snapped.  Find the transformation that describes where the snapped point has
408             ** ended up, and also the metric for this transformation.
409             */
410             NR::Point const a = (snapped_point.getPoint() - origin); // vector to snapped point
411             NR::Point const b = (*i - origin); // vector to original point
412             
413             switch (transformation_type) {
414                 case TRANSLATION:
415                     result = snapped_point.getPoint() - *i;
416                     /* Consider the case in which a box is almost aligned with a grid in both 
417                      * horizontal and vertical directions. The distance to the intersection of
418                      * the grid lines will always be larger then the distance to a single grid
419                      * line. If we prefer snapping to an intersection instead of to a single 
420                      * grid line, then we cannot use "metric = NR::L2(result)". Therefore the
421                      * snapped distance will be used as a metric. Please note that the snapped
422                      * distance is defined as the distance to the nearest line of the intersection,
423                      * and not to the intersection itself! 
424                      */
425                     metric = snapped_point.getDistance(); //used to be: metric = NR::L2(result);
426                     second_metric = snapped_point.getSecondDistance();
427                     break;
428                 case SCALE:
429                 {
430                     result = NR::Point(NR_HUGE, NR_HUGE);
431                     // If this point *i is horizontally or vertically aligned with
432                     // the origin of the scaling, then it will scale purely in X or Y 
433                     // We can therefore only calculate the scaling in this direction
434                     // and the scaling factor for the other direction should remain
435                     // untouched (unless scaling is uniform ofcourse)
436                     for (int index = 0; index < 2; index++) {
437                         if (fabs(b[index]) > 1e-6) { // if SCALING CAN occur in this direction
438                             if (fabs(fabs(a[index]/b[index]) - fabs(transformation[index])) > 1e-12) { // if SNAPPING DID occur in this direction
439                                 result[index] = a[index] / b[index]; // then calculate it!
440                             }
441                             // we might leave result[1-index] = NR_HUGE
442                             // if scaling didn't occur in the other direction
443                         }
444                     }
445                     // Compare the resulting scaling with the desired scaling
446                     scale_metric = result - transformation; // One or both of its components might be NR_HUGE
447                     break;
448                 }
449                 case STRETCH:
450                     result = NR::Point(NR_HUGE, NR_HUGE);
451                     if (fabs(b[dim]) > 1e-6) { // if STRETCHING will occur for this point
452                         result[dim] = a[dim] / b[dim];
453                         result[1-dim] = uniform ? result[dim] : 1;
454                     } else { // STRETCHING might occur for this point, but only when the stretching is uniform
455                         if (uniform && fabs(b[1-dim]) > 1e-6) {
456                            result[1-dim] = a[1-dim] / b[1-dim];
457                            result[dim] = result[1-dim];
458                         }
459                     }
460                     metric = std::abs(result[dim] - transformation[dim]);
461                     break;
462                 case SKEW:
463                     result[0] = (snapped_point.getPoint()[dim] - (*i)[dim]) / ((*i)[1 - dim] - origin[1 - dim]); // skew factor
464                     result[1] = transformation[1]; // scale factor
465                     metric = std::abs(result[0] - transformation[0]);
466                     break;
467                 default:
468                     g_assert_not_reached();
469             }
470             
471             /* Note it if it's the best so far */
472             if (transformation_type == SCALE) {
473                 for (int index = 0; index < 2; index++) {
474                     if (fabs(scale_metric[index]) < fabs(best_scale_metric[index])) {
475                         best_transformation[index] = result[index];
476                         best_scale_metric[index] = fabs(scale_metric[index]);
477                         // When scaling, we're considering the best transformation in each direction separately
478                         // Therefore two different snapped points might together make a single best transformation
479                         // We will however return only a single snapped point (e.g. to display the snapping indicator)   
480                         best_snapped_point = snapped_point;
481                         // std::cout << "SEL ";
482                     } // else { std::cout << "    ";}
483                 }
484                 if (uniform) {
485                     if (best_scale_metric[0] < best_scale_metric[1]) {
486                         best_transformation[1] = best_transformation[0];
487                         best_scale_metric[1] = best_scale_metric[0]; 
488                     } else {
489                         best_transformation[0] = best_transformation[1];
490                         best_scale_metric[0] = best_scale_metric[1];
491                     }
492                 }
493                 best_metric = std::min(best_scale_metric[0], best_scale_metric[1]);
494                 // std::cout << "P_orig = " << (*i) << " | scale_metric = " << scale_metric << " | distance = " << snapped_point.getDistance() << " | P_snap = " << snapped_point.getPoint() << std::endl;
495             } else {
496                 bool const c1 = metric < best_metric;
497                 bool const c2 = metric == best_metric && snapped_point.getAtIntersection() == true && best_snapped_point.getAtIntersection() == false;
498                         bool const c3a = metric == best_metric && snapped_point.getAtIntersection() == true && best_snapped_point.getAtIntersection() == true;
499                 bool const c3b = second_metric < best_second_metric;
500                 bool const c4 = snapped_point.getAlwaysSnap() == true && best_snapped_point.getAlwaysSnap() == false;
501                 bool const c4n = snapped_point.getAlwaysSnap() == false && best_snapped_point.getAlwaysSnap() == true;
502                 
503                 if ((c1 || c2 || (c3a && c3b) || c4) && !c4n) {
504                     best_transformation = result;
505                     best_metric = metric;
506                     best_second_metric = second_metric;
507                     best_snapped_point = snapped_point; 
508                     // std::cout << "SEL ";
509                 } // else { std::cout << "    ";}
510                 // std::cout << "P_orig = " << (*i) << " | metric = " << metric << " | distance = " << snapped_point.getDistance() << " | second metric = " << second_metric << " | P_snap = " << snapped_point.getPoint() << std::endl;
511             }
512         }
513         
514         j++;
515     }
516     
517     if (transformation_type == SCALE) {
518         // When scaling, don't ever exit with one of scaling components set to NR_HUGE
519         for (int index = 0; index < 2; index++) {
520             if (best_transformation[index] == NR_HUGE) {
521                 if (uniform && best_transformation[1-index] < NR_HUGE) {
522                         best_transformation[index] = best_transformation[1-index];
523                 } else {
524                         best_transformation[index] = transformation[index];     
525                 }
526             }
527         }
528     }
529     
530     best_snapped_point.setTransformation(best_transformation);
531     // Using " < 1e6" instead of " < NR_HUGE" for catching some rounding errors
532     // These rounding errors might be caused by NRRects, see bug #1584301    
533     best_snapped_point.setDistance(best_metric < 1e6 ? best_metric : NR_HUGE);
534     return best_snapped_point;
538 /**
539  *  Try to snap a list of points to any interested snappers after they have undergone
540  *  a translation.
541  *
542  *  \param point_type Type of points.
543  *  \param p Points.
544  *  \param tr Proposed translation.
545  *  \return Snapped translation, if a snap occurred, and a flag indicating whether a snap occurred.
546  */
548 Inkscape::SnappedPoint SnapManager::freeSnapTranslation(Inkscape::Snapper::PointType point_type,
549                                                         std::vector<NR::Point> const &p,
550                                                         NR::Point const &tr) const
552     return _snapTransformed(point_type, p, false, NR::Point(), TRANSLATION, tr, NR::Point(), NR::X, false);
556 /**
557  *  Try to snap a list of points to any interested snappers after they have undergone a
558  *  translation.  A snap will only occur along a line described by a
559  *  Inkscape::Snapper::ConstraintLine.
560  *
561  *  \param point_type Type of points.
562  *  \param p Points.
563  *  \param constraint Constraint line.
564  *  \param tr Proposed translation.
565  *  \return Snapped translation, if a snap occurred, and a flag indicating whether a snap occurred.
566  */
568 Inkscape::SnappedPoint SnapManager::constrainedSnapTranslation(Inkscape::Snapper::PointType point_type,
569                                                                std::vector<NR::Point> const &p,
570                                                                Inkscape::Snapper::ConstraintLine const &constraint,
571                                                                NR::Point const &tr) const
573     return _snapTransformed(point_type, p, true, constraint, TRANSLATION, tr, NR::Point(), NR::X, false);
577 /**
578  *  Try to snap a list of points to any interested snappers after they have undergone
579  *  a scale.
580  *
581  *  \param point_type Type of points.
582  *  \param p Points.
583  *  \param s Proposed scale.
584  *  \param o Origin of proposed scale.
585  *  \return Snapped scale, if a snap occurred, and a flag indicating whether a snap occurred.
586  */
588 Inkscape::SnappedPoint SnapManager::freeSnapScale(Inkscape::Snapper::PointType point_type,
589                                                   std::vector<NR::Point> const &p,
590                                                   NR::scale const &s,
591                                                   NR::Point const &o) const
593     return _snapTransformed(point_type, p, false, NR::Point(), SCALE, NR::Point(s[NR::X], s[NR::Y]), o, NR::X, false);
597 /**
598  *  Try to snap a list of points to any interested snappers after they have undergone
599  *  a scale.  A snap will only occur along a line described by a
600  *  Inkscape::Snapper::ConstraintLine.
601  *
602  *  \param point_type Type of points.
603  *  \param p Points.
604  *  \param s Proposed scale.
605  *  \param o Origin of proposed scale.
606  *  \return Snapped scale, if a snap occurred, and a flag indicating whether a snap occurred.
607  */
609 Inkscape::SnappedPoint SnapManager::constrainedSnapScale(Inkscape::Snapper::PointType point_type,
610                                                          std::vector<NR::Point> const &p,
611                                                          NR::scale const &s,
612                                                          NR::Point const &o) const
614     // When constrained scaling, only uniform scaling is supported.
615     return _snapTransformed(point_type, p, true, NR::Point(), SCALE, NR::Point(s[NR::X], s[NR::Y]), o, NR::X, true);
619 /**
620  *  Try to snap a list of points to any interested snappers after they have undergone
621  *  a stretch.
622  *
623  *  \param point_type Type of points.
624  *  \param p Points.
625  *  \param s Proposed stretch.
626  *  \param o Origin of proposed stretch.
627  *  \param d Dimension in which to apply proposed stretch.
628  *  \param u true if the stretch should be uniform (ie to be applied equally in both dimensions)
629  *  \return Snapped stretch, if a snap occurred, and a flag indicating whether a snap occurred.
630  */
632 Inkscape::SnappedPoint SnapManager::constrainedSnapStretch(Inkscape::Snapper::PointType point_type,
633                                                             std::vector<NR::Point> const &p,
634                                                             NR::Coord const &s,
635                                                             NR::Point const &o,
636                                                             NR::Dim2 d,
637                                                             bool u) const
639    return _snapTransformed(point_type, p, true, NR::Point(), STRETCH, NR::Point(s, s), o, d, u);
643 /**
644  *  Try to snap a list of points to any interested snappers after they have undergone
645  *  a skew.
646  *
647  *  \param point_type Type of points.
648  *  \param p Points.
649  *  \param s Proposed skew.
650  *  \param o Origin of proposed skew.
651  *  \param d Dimension in which to apply proposed skew.
652  *  \return Snapped skew, if a snap occurred, and a flag indicating whether a snap occurred.
653  */
655 Inkscape::SnappedPoint SnapManager::constrainedSnapSkew(Inkscape::Snapper::PointType point_type,
656                                                  std::vector<NR::Point> const &p,
657                                                  Inkscape::Snapper::ConstraintLine const &constraint,
658                                                  NR::Point const &s,  
659                                                  NR::Point const &o,
660                                                  NR::Dim2 d) const
662    // "s" contains skew factor in s[0], and scale factor in s[1]
663    return _snapTransformed(point_type, p, true, constraint, SKEW, s, o, d, false);
666 Inkscape::SnappedPoint SnapManager::findBestSnap(NR::Point const &p, SnappedConstraints &sc, bool constrained) const
668     /*
669     std::cout << "Type and number of snapped constraints: " << std::endl;
670     std::cout << "  Points      : " << sc.points.size() << std::endl;
671     std::cout << "  Lines       : " << sc.lines.size() << std::endl;
672     std::cout << "  Grid lines  : " << sc.grid_lines.size()<< std::endl;
673     std::cout << "  Guide lines : " << sc.guide_lines.size()<< std::endl;
674     */
675         
676     // Store all snappoints
677     std::list<Inkscape::SnappedPoint> sp_list;
678     
679     // search for the closest snapped point
680     Inkscape::SnappedPoint closestPoint;
681     if (getClosestSP(sc.points, closestPoint)) {
682         sp_list.push_back(closestPoint);
683     } 
684     
685     // search for the closest snapped line segment
686     Inkscape::SnappedLineSegment closestLineSegment;
687     if (getClosestSLS(sc.lines, closestLineSegment)) {    
688         sp_list.push_back(Inkscape::SnappedPoint(closestLineSegment));
689     }
690     
691     if (_intersectionLS) {
692             // search for the closest snapped intersection of line segments
693             Inkscape::SnappedPoint closestLineSegmentIntersection;
694             if (getClosestIntersectionSLS(sc.lines, closestLineSegmentIntersection)) {
695                 sp_list.push_back(closestLineSegmentIntersection);
696             }
697     }    
699     // search for the closest snapped grid line
700     Inkscape::SnappedLine closestGridLine;
701     if (getClosestSL(sc.grid_lines, closestGridLine)) {    
702         sp_list.push_back(Inkscape::SnappedPoint(closestGridLine));
703     }
704     
705     // search for the closest snapped guide line
706     Inkscape::SnappedLine closestGuideLine;
707     if (getClosestSL(sc.guide_lines, closestGuideLine)) {
708         sp_list.push_back(Inkscape::SnappedPoint(closestGuideLine));
709     }
710     
711     // When freely snapping to a grid/guide/path, only one degree of freedom is eliminated
712     // Therefore we will try get fully constrained by finding an intersection with another grid/guide/path 
713     
714     // When doing a constrained snap however, we're already at an intersection of the constrained line and
715     // the grid/guide/path we're snapping to. This snappoint is therefore fully constrained, so there's
716     // no need to look for additional intersections
717     if (!constrained) {
718         // search for the closest snapped intersection of grid lines
719         Inkscape::SnappedPoint closestGridPoint;
720         if (getClosestIntersectionSL(sc.grid_lines, closestGridPoint)) {
721             sp_list.push_back(closestGridPoint);
722         }
723         
724         // search for the closest snapped intersection of guide lines
725         Inkscape::SnappedPoint closestGuidePoint;
726         if (getClosestIntersectionSL(sc.guide_lines, closestGuidePoint)) {
727             sp_list.push_back(closestGuidePoint);
728         }
729         
730         // search for the closest snapped intersection of grid with guide lines
731         if (_intersectionGG) {
732             Inkscape::SnappedPoint closestGridGuidePoint;
733             if (getClosestIntersectionSL(sc.grid_lines, sc.guide_lines, closestGridGuidePoint)) {
734                 sp_list.push_back(closestGridGuidePoint);
735             }
736         }
737     }
738     
739     // now let's see which snapped point gets a thumbs up
740     Inkscape::SnappedPoint bestSnappedPoint = Inkscape::SnappedPoint(p, NR_HUGE, 0, false);
741     for (std::list<Inkscape::SnappedPoint>::const_iterator i = sp_list.begin(); i != sp_list.end(); i++) {
742                 // first find out if this snapped point is within snapping range
743         if ((*i).getDistance() <= (*i).getTolerance()) {
744                 // if it's the first point
745                 bool c1 = (i == sp_list.begin());  
746                 // or, if it's closer
747                 bool c2 = (*i).getDistance() < bestSnappedPoint.getDistance();
748             // or, if it's for a snapper with "always snap" turned on, and the previous wasn't
749             bool c3 = (*i).getAlwaysSnap() && !bestSnappedPoint.getAlwaysSnap();
750                 // But in no case fall back from a snapper with "always snap" on to one with "always snap" off
751             bool c3n = !(*i).getAlwaysSnap() && bestSnappedPoint.getAlwaysSnap();
752             // or, if it's just as close then consider the second distance
753                 // (which is only relevant for points at an intersection)
754                 bool c4a = ((*i).getDistance() == bestSnappedPoint.getDistance()); 
755                 bool c4b = (*i).getSecondDistance() < bestSnappedPoint.getSecondDistance();
756                 // then prefer this point over the previous one
757             if ((c1 || c2 || c3 || (c4a && c4b)) && !c3n) {
758                 bestSnappedPoint = *i;
759             }
760         }
761     }
762     
763     
764     // Update the snap indicator, if requested
765     if (_desktop_for_snapindicator) {
766         if (bestSnappedPoint.getSnapped()) {
767             _desktop_for_snapindicator->snapindicator->set_new_snappoint(bestSnappedPoint);
768         } else {
769             _desktop_for_snapindicator->snapindicator->remove_snappoint();
770         }
771     }
772     
773     // std::cout << "findBestSnap = " << bestSnappedPoint.getPoint() << std::endl;
774     return bestSnappedPoint;         
777 void SnapManager::setup(SPDesktop const *desktop_for_snapindicator, SPItem const *item_to_ignore, std::vector<NR::Point> *unselected_nodes)
779     _item_to_ignore = item_to_ignore;
780     _items_to_ignore = NULL;
781     _desktop_for_snapindicator = desktop_for_snapindicator;
782     _unselected_nodes = unselected_nodes;
785 void SnapManager::setup(SPDesktop const *desktop_for_snapindicator, std::vector<SPItem const *> &items_to_ignore, std::vector<NR::Point> *unselected_nodes)
787     _item_to_ignore = NULL;
788     _items_to_ignore = &items_to_ignore;
789     _desktop_for_snapindicator = desktop_for_snapindicator;
790     _unselected_nodes = unselected_nodes;   
793 /*
794   Local Variables:
795   mode:c++
796   c-file-style:"stroustrup"
797   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
798   indent-tabs-mode:nil
799   fill-column:99
800   End:
801 */
802 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :