Code

886a1216ff30d94340fa9ca06afc0a9dfb90d782
[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  *   Carl Hetherington <inkscape@carlh.net>
11  *
12  * Copyright (C) 2006-2007      Johan Engelen <johan@shouraizou.nl>
13  * Copyright (C) 1999-2002 Authors
14  *
15  * Released under GNU GPL, read the file 'COPYING' for more information
16  */
18 #include "sp-namedview.h"
19 #include "snap.h"
21 #include <libnr/nr-point-fns.h>
22 #include <libnr/nr-scale-ops.h>
23 #include <libnr/nr-values.h>
25 #include "display/canvas-grid.h"
27 #include "inkscape.h"
28 #include "desktop.h"
29 #include "sp-guide.h"
30 using std::vector;
32 /**
33  *  Construct a SnapManager for a SPNamedView.
34  *
35  *  \param v `Owning' SPNamedView.
36  */
38 SnapManager::SnapManager(SPNamedView const *v) :
39     guide(v, 0),
40     object(v, 0),
41     _named_view(v),
42     _include_item_center(false)
43 {
44         
45 }
48 /**
49  *  \return List of snappers that we use.
50  */
51 SnapManager::SnapperList 
52 SnapManager::getSnappers() const
53 {
54     SnapManager::SnapperList s;
55     s.push_back(&guide);
56     s.push_back(&object);
58     SnapManager::SnapperList gs = getGridSnappers();
59     s.splice(s.begin(), gs);
61     return s;
62 }
64 /**
65  *  \return List of gridsnappers that we use.
66  */
67 SnapManager::SnapperList 
68 SnapManager::getGridSnappers() const
69 {
70     SnapperList s;
72     //FIXME: this code should actually do this: add new grid snappers that are active for this desktop. now it just adds all gridsnappers
73     SPDesktop* desktop = SP_ACTIVE_DESKTOP;
74     if (desktop && desktop->gridsEnabled()) {
75         for ( GSList const *l = _named_view->grids; l != NULL; l = l->next) {
76             Inkscape::CanvasGrid *grid = (Inkscape::CanvasGrid*) l->data;
77             s.push_back(grid->snapper);
78         }
79     }
81     return s;
82 }
84 /**
85  * \return true if one of the snappers will try to snap something.
86  */
88 bool SnapManager::SomeSnapperMightSnap() const
89 {
90     SnapperList const s = getSnappers();
91     SnapperList::const_iterator i = s.begin();
92     while (i != s.end() && (*i)->ThisSnapperMightSnap() == false) {
93         i++;
94     }
95     
96     return (i != s.end());
97 }
99 /*
100  *  The snappers have too many parameters to adjust individually. Therefore only
101  *  two snapping modes are presented to the user: snapping bounding box corners (to 
102  *      other bounding boxes, grids or guides), and/or snapping nodes (to other nodes,
103  *  paths, grids or guides). To select either of these modes (or both), use the 
104  *  methods defined below: setSnapModeBBox() and setSnapModeNode().
105  * 
106  * */
109 void SnapManager::setSnapModeBBox(bool enabled)
111         //The default values are being set in sp_namedview_set() (in sp-namedview.cpp)
112         guide.setSnapFrom(Inkscape::Snapper::SNAPPOINT_BBOX, enabled);
113         
114         for ( GSList const *l = _named_view->grids; l != NULL; l = l->next) {
115         Inkscape::CanvasGrid *grid = (Inkscape::CanvasGrid*) l->data;
116         grid->snapper->setSnapFrom(Inkscape::Snapper::SNAPPOINT_BBOX, enabled);
117     }
118         
119         object.setSnapFrom(Inkscape::Snapper::SNAPPOINT_BBOX, enabled);
120         object.setSnapToBBoxNode(enabled);
121         object.setSnapToBBoxPath(enabled);
122         object.setStrictSnapping(true); //don't snap bboxes to nodes/paths and vice versa       
125 bool SnapManager::getSnapModeBBox() const
127         return guide.getSnapFrom(Inkscape::Snapper::SNAPPOINT_BBOX);
130 void SnapManager::setSnapModeNode(bool enabled)
132         guide.setSnapFrom(Inkscape::Snapper::SNAPPOINT_NODE, enabled);
133         
134         for ( GSList const *l = _named_view->grids; l != NULL; l = l->next) {
135         Inkscape::CanvasGrid *grid = (Inkscape::CanvasGrid*) l->data;
136         grid->snapper->setSnapFrom(Inkscape::Snapper::SNAPPOINT_NODE, enabled);
137     }
138         
139         object.setSnapFrom(Inkscape::Snapper::SNAPPOINT_NODE, enabled);
140         //object.setSnapToItemNode(enabled); // On second thought, these should be controlled
141         //object.setSnapToItemPath(enabled); // separately by the snapping prefs dialog 
142         object.setStrictSnapping(true);
145 bool SnapManager::getSnapModeNode() const
147         return guide.getSnapFrom(Inkscape::Snapper::SNAPPOINT_NODE);
150 void SnapManager::setSnapModeGuide(bool enabled)
152         object.setSnapFrom(Inkscape::Snapper::SNAPPOINT_GUIDE, enabled);
155 bool SnapManager::getSnapModeGuide() const
157         return object.getSnapFrom(Inkscape::Snapper::SNAPPOINT_GUIDE);
160 /**
161  *  Try to snap a point to any interested snappers.
162  *
163  *  \param t Type of point.
164  *  \param p Point.
165  *  \param it Item to ignore when snapping.
166  *  \return Snapped point.
167  */
169 Inkscape::SnappedPoint SnapManager::freeSnap(Inkscape::Snapper::PointType t,
170                                              NR::Point const &p,
171                                              SPItem const *it) const
174     std::list<SPItem const *> lit;
175     lit.push_back(it);
176     
177     std::vector<NR::Point> points_to_snap;
178     points_to_snap.push_back(p);
179     
180     return freeSnap(t, p, true, points_to_snap, lit);
184 /**
185  *  Try to snap a point to any interested snappers.
186  *
187  *  \param t Type of point.
188  *  \param p Point.
189  *  \param first_point If true then this point is the first one from a whole bunch of points 
190  *  \param points_to_snap The whole bunch of points, all from the same selection and having the same transformation 
191  *  \param it List of items to ignore when snapping.
192  *  \return Snapped point.
193  */
194  
195  Inkscape::SnappedPoint SnapManager::freeSnap(Inkscape::Snapper::PointType t,
196                                              NR::Point const &p,
197                                              bool const &first_point,
198                                              std::vector<NR::Point> &points_to_snap,
199                                              std::list<SPItem const *> const &it) const
201     SnapperList const snappers = getSnappers();
203         return freeSnap(t, p, first_point, points_to_snap, it, snappers);
206 /**
207  *  Try to snap a point to any of the specified snappers.
208  *
209  *  \param t Type of point.
210  *  \param p Point.
211  *  \param first_point If true then this point is the first one from a whole bunch of points 
212  *  \param points_to_snap The whole bunch of points, all from the same selection and having the same transformation 
213  *  \param it List of items to ignore when snapping.
214  * \param snappers  List of snappers to try to snap to
215  *  \return Snapped point.
216  */
218 Inkscape::SnappedPoint SnapManager::freeSnap(Inkscape::Snapper::PointType t,
219                                              NR::Point const &p,
220                                              bool const &first_point,
221                                              std::vector<NR::Point> &points_to_snap,
222                                              std::list<SPItem const *> const &it,
223                                              SnapperList const &snappers) const
225     Inkscape::SnappedPoint r(p, NR_HUGE);
227     for (SnapperList::const_iterator i = snappers.begin(); i != snappers.end(); i++) {
228         Inkscape::SnappedPoint const s = (*i)->freeSnap(t, p, first_point, points_to_snap, it);
229         if (s.getDistance() < r.getDistance()) {
230             r = s;
231         }
232     }
234     return r;
237 /**
238  *  Try to snap a point to any of the specified snappers. Snap always, ignoring the snap-distance
239  *
240  *  \param t Type of point.
241  *  \param p Point.
242  *  \param it Item to ignore when snapping.
243  *  \param snappers  List of snappers to try to snap to
244  *  \return Snapped point.
245  */
247 Inkscape::SnappedPoint
248 SnapManager::freeSnapAlways( Inkscape::Snapper::PointType t,
249                              NR::Point const &p,
250                              SPItem const *it,
251                              SnapperList &snappers )
253     std::list<SPItem const *> lit;
254     lit.push_back(it);
255     return freeSnapAlways(t, p, lit, snappers);
258 /**
259  *  Try to snap a point to any of the specified snappers. Snap always, ignoring the snap-distance
260  *
261  *  \param t Type of point.
262  *  \param p Point.
263  *  \param it List of items to ignore when snapping.
264  *  \param snappers  List of snappers to try to snap to
265  *  \return Snapped point.
266  */
268 Inkscape::SnappedPoint
269 SnapManager::freeSnapAlways( Inkscape::Snapper::PointType t,
270                              NR::Point const &p,
271                              std::list<SPItem const *> const &it,
272                              SnapperList &snappers )
274     Inkscape::SnappedPoint r(p, NR_HUGE);
276     for (SnapperList::iterator i = snappers.begin(); i != snappers.end(); i++) {
277         gdouble const curr_gridsnap = (*i)->getDistance();
278         const_cast<Inkscape::Snapper*> (*i)->setDistance(NR_HUGE);
279         std::vector<NR::Point> points_to_snap;
280         points_to_snap.push_back(p);    
281         Inkscape::SnappedPoint const s = (*i)->freeSnap(t, p, true, points_to_snap, it);
282         const_cast<Inkscape::Snapper*> (*i)->setDistance(curr_gridsnap);
284         if (s.getDistance() < r.getDistance()) {
285             r = s;
286         }
287     }
289     return r;
294 /**
295  *  Try to snap a point to any interested snappers.  A snap will only occur along
296  *  a line described by a Inkscape::Snapper::ConstraintLine.
297  *
298  *  \param t Type of point.
299  *  \param p Point.
300  *  \param c Constraint line.
301  *  \param it Item to ignore when snapping.
302  *  \return Snapped point.
303  */
305 Inkscape::SnappedPoint SnapManager::constrainedSnap(Inkscape::Snapper::PointType t,
306                                                     NR::Point const &p,
307                                                     Inkscape::Snapper::ConstraintLine const &c,
308                                                     SPItem const *it) const
310     std::list<SPItem const *> lit;
311     lit.push_back(it);
312     
313     std::vector<NR::Point> points_to_snap;
314     points_to_snap.push_back(p);
315     
316     return constrainedSnap(t, p, true, points_to_snap, c, lit);
321 /**
322  *  Try to snap a point to any interested snappers.  A snap will only occur along
323  *  a line described by a Inkscape::Snapper::ConstraintLine.
324  *
325  *  \param t Type of point.
326  *  \param p Point.
327  *  \param first_point If true then this point is the first one from a whole bunch of points 
328  *  \param points_to_snap The whole bunch of points, all from the same selection and having the same transformation 
329  *  \param c Constraint line.
330  *  \param it List of items to ignore when snapping.
331  *  \return Snapped point.
332  */
334 Inkscape::SnappedPoint SnapManager::constrainedSnap(Inkscape::Snapper::PointType t,
335                                                     NR::Point const &p,
336                                                     bool const &first_point,
337                                                         std::vector<NR::Point> &points_to_snap,
338                                                     Inkscape::Snapper::ConstraintLine const &c,
339                                                     std::list<SPItem const *> const &it) const
341     Inkscape::SnappedPoint r(p, NR_HUGE);
343     SnapperList const snappers = getSnappers();
344     for (SnapperList::const_iterator i = snappers.begin(); i != snappers.end(); i++) {
345         Inkscape::SnappedPoint const s = (*i)->constrainedSnap(t, p, first_point, points_to_snap, c, it);
346         if (s.getDistance() < r.getDistance()) {
347             r = s;
348         }
349     }
351     return r;
354 Inkscape::SnappedPoint SnapManager::guideSnap(NR::Point const &p,
355                                                         NR::Point const &guide_normal) const
357         Inkscape::ObjectSnapper::DimensionToSnap snap_dim;
358         if (guide_normal == component_vectors[NR::Y]) {
359                 snap_dim = Inkscape::ObjectSnapper::SNAP_Y;
360         } else if (guide_normal == component_vectors[NR::X]) {
361                 snap_dim = Inkscape::ObjectSnapper::SNAP_X;
362         } else {
363                 g_warning("WARNING: snapping of angled guides is not supported yet!");
364                 snap_dim = Inkscape::ObjectSnapper::SNAP_XY;
365         }
366         
367         return object.guideSnap(p, snap_dim);   
371 /**
372  *  Main internal snapping method, which is called by the other, friendlier, public
373  *  methods.  It's a bit hairy as it has lots of parameters, but it saves on a lot
374  *  of duplicated code.
375  *
376  *  \param type Type of points being snapped.
377  *  \param points List of points to snap.
378  *  \param ignore List of items to ignore while snapping.
379  *  \param constrained true if the snap is constrained.
380  *  \param constraint Constraint line to use, if `constrained' is true, otherwise undefined.
381  *  \param transformation_type Type of transformation to apply to points before trying to snap them.
382  *  \param transformation Description of the transformation; details depend on the type.
383  *  \param origin Origin of the transformation, if applicable.
384  *  \param dim Dimension of the transformation, if applicable.
385  *  \param uniform true if the transformation should be uniform, if applicable.
386  */
388 std::pair<NR::Point, bool> SnapManager::_snapTransformed(
389     Inkscape::Snapper::PointType type,
390     std::vector<NR::Point> const &points,
391     std::list<SPItem const *> const &ignore,
392     bool constrained,
393     Inkscape::Snapper::ConstraintLine const &constraint,
394     Transformation transformation_type,
395     NR::Point const &transformation,
396     NR::Point const &origin,
397     NR::Dim2 dim,
398     bool uniform) const
400     /* We have a list of points, which we are proposing to transform in some way.  We need to see
401     ** if any of these points, when transformed, snap to anything.  If they do, we return the
402     ** appropriate transformation with `true'; otherwise we return the original scale with `false'.
403     */
405     /* Quick check to see if we have any snappers that are enabled */
406     if (SomeSnapperMightSnap() == false) {
407         return std::make_pair(transformation, false);
408     }
409     
410     std::vector<NR::Point> transformed_points;
411     
412     for (std::vector<NR::Point>::const_iterator i = points.begin(); i != points.end(); i++) {
414         /* Work out the transformed version of this point */
415         NR::Point transformed;
416         switch (transformation_type) {
417             case TRANSLATION:
418                 transformed = *i + transformation;
419                 break;
420             case SCALE:
421                 transformed = ((*i - origin) * NR::scale(transformation[NR::X], transformation[NR::Y])) + origin;
422                 break;
423             case STRETCH:
424             {
425                 NR::scale s(1, 1);
426                 if (uniform)
427                     s[NR::X] = s[NR::Y] = transformation[dim];
428                 else {
429                     s[dim] = transformation[dim];
430                     s[1 - dim] = 1;
431                 }
432                 transformed = ((*i - origin) * s) + origin;
433                 break;
434             }
435             case SKEW:
436                 transformed = *i;
437                 transformed[dim] += transformation[dim] * ((*i)[1 - dim] - origin[1 - dim]);
438                 break;
439             default:
440                 g_assert_not_reached();
441         }
442         
443         // add the current transformed point to the box hulling all transformed points
444         transformed_points.push_back(transformed);
445     }    
446     
447     /* The current best transformation */
448     NR::Point best_transformation = transformation;
450     /* The current best metric for the best transformation; lower is better, NR_HUGE
451     ** means that we haven't snapped anything.
452     */
453     double best_metric = NR_HUGE;
455         std::vector<NR::Point>::const_iterator j = transformed_points.begin();
457     for (std::vector<NR::Point>::const_iterator i = points.begin(); i != points.end(); i++) {
458         
459         /* Snap it */
460         Inkscape::SnappedPoint const snapped = constrained ?
461             constrainedSnap(type, *j, i == points.begin(), transformed_points, constraint, ignore) : freeSnap(type, *j, i == points.begin(), transformed_points, ignore);
463         if (snapped.getDistance() < NR_HUGE) {
464             /* We snapped.  Find the transformation that describes where the snapped point has
465             ** ended up, and also the metric for this transformation.
466             */
467             NR::Point result;
468             NR::Coord metric;
469             switch (transformation_type) {
470                 case TRANSLATION:
471                     result = snapped.getPoint() - *i;
472                     metric = NR::L2(result);
473                     break;
474                 case SCALE:
475                 {
476                     NR::Point const a = (snapped.getPoint() - origin);
477                     NR::Point const b = (*i - origin);
478                     result = NR::Point(a[NR::X] / b[NR::X], a[NR::Y] / b[NR::Y]);
479                     metric = std::abs(NR::L2(result) - NR::L2(transformation));
480                     break;
481                 }
482                 case STRETCH:
483                 {
484                     for (int j = 0; j < 2; j++) {
485                         if (uniform || j == dim) {
486                             result[j] = (snapped.getPoint()[dim] - origin[dim]) / ((*i)[dim] - origin[dim]);
487                         } else {
488                             result[j] = 1;
489                         }
490                     }
491                     metric = std::abs(result[dim] - transformation[dim]);
492                     break;
493                 }
494                 case SKEW:
495                     result[dim] = (snapped.getPoint()[dim] - (*i)[dim]) / ((*i)[1 - dim] - origin[1 - dim]);
496                     metric = std::abs(result[dim] - transformation[dim]);
497                     break;
498                 default:
499                     g_assert_not_reached();
500             }
502             /* Note it if it's the best so far */
503             if (metric < best_metric) {
504                 best_transformation = result;
505                 best_metric = metric;
506             }
507         }
508         
509         j++;
510     }
512     // Using " < 1e6" instead of " < NR::HUGE" for catching some rounding errors
513     // These rounding errors might be caused by NRRects, see bug #1584301
514     return std::make_pair(best_transformation, best_metric < 1e6);
518 /**
519  *  Try to snap a list of points to any interested snappers after they have undergone
520  *  a translation.
521  *
522  *  \param t Type of points.
523  *  \param p Points.
524  *  \param it List of items to ignore when snapping.
525  *  \param tr Proposed translation.
526  *  \return Snapped translation, if a snap occurred, and a flag indicating whether a snap occurred.
527  */
529 std::pair<NR::Point, bool> SnapManager::freeSnapTranslation(Inkscape::Snapper::PointType t,
530                                                             std::vector<NR::Point> const &p,
531                                                             std::list<SPItem const *> const &it,
532                                                             NR::Point const &tr) const
534     return _snapTransformed(
535         t, p, it, false, NR::Point(), TRANSLATION, tr, NR::Point(), NR::X, false
536         );
540 /**
541  *  Try to snap a list of points to any interested snappers after they have undergone a
542  *  translation.  A snap will only occur along a line described by a
543  *  Inkscape::Snapper::ConstraintLine.
544  *
545  *  \param t Type of points.
546  *  \param p Points.
547  *  \param it List of items to ignore when snapping.
548  *  \param c Constraint line.
549  *  \param tr Proposed translation.
550  *  \return Snapped translation, if a snap occurred, and a flag indicating whether a snap occurred.
551  */
553 std::pair<NR::Point, bool> SnapManager::constrainedSnapTranslation(Inkscape::Snapper::PointType t,
554                                                                    std::vector<NR::Point> const &p,
555                                                                    std::list<SPItem const *> const &it,
556                                                                    Inkscape::Snapper::ConstraintLine const &c,
557                                                                    NR::Point const &tr) const
559     return _snapTransformed(
560         t, p, it, true, c, TRANSLATION, tr, NR::Point(), NR::X, false
561         );
565 /**
566  *  Try to snap a list of points to any interested snappers after they have undergone
567  *  a scale.
568  *
569  *  \param t Type of points.
570  *  \param p Points.
571  *  \param it List of items to ignore when snapping.
572  *  \param s Proposed scale.
573  *  \param o Origin of proposed scale.
574  *  \return Snapped scale, if a snap occurred, and a flag indicating whether a snap occurred.
575  */
577 std::pair<NR::scale, bool> SnapManager::freeSnapScale(Inkscape::Snapper::PointType t,
578                                                       std::vector<NR::Point> const &p,
579                                                       std::list<SPItem const *> const &it,
580                                                       NR::scale const &s,
581                                                       NR::Point const &o) const
583     return _snapTransformed(
584         t, p, it, false, NR::Point(), SCALE, NR::Point(s[NR::X], s[NR::Y]), o, NR::X, false
585         );
589 /**
590  *  Try to snap a list of points to any interested snappers after they have undergone
591  *  a scale.  A snap will only occur along a line described by a
592  *  Inkscape::Snapper::ConstraintLine.
593  *
594  *  \param t Type of points.
595  *  \param p Points.
596  *  \param it List of items to ignore when snapping.
597  *  \param s Proposed scale.
598  *  \param o Origin of proposed scale.
599  *  \return Snapped scale, if a snap occurred, and a flag indicating whether a snap occurred.
600  */
602 std::pair<NR::scale, bool> SnapManager::constrainedSnapScale(Inkscape::Snapper::PointType t,
603                                                              std::vector<NR::Point> const &p,
604                                                              std::list<SPItem const *> const &it,
605                                                              Inkscape::Snapper::ConstraintLine const &c,
606                                                              NR::scale const &s,
607                                                              NR::Point const &o) const
609     return _snapTransformed(
610         t, p, it, true, c, SCALE, NR::Point(s[NR::X], s[NR::Y]), o, NR::X, false
611         );
615 /**
616  *  Try to snap a list of points to any interested snappers after they have undergone
617  *  a stretch.
618  *
619  *  \param t Type of points.
620  *  \param p Points.
621  *  \param it List of items to ignore when snapping.
622  *  \param s Proposed stretch.
623  *  \param o Origin of proposed stretch.
624  *  \param d Dimension in which to apply proposed stretch.
625  *  \param u true if the stretch should be uniform (ie to be applied equally in both dimensions)
626  *  \return Snapped stretch, if a snap occurred, and a flag indicating whether a snap occurred.
627  */
629 std::pair<NR::Coord, bool> SnapManager::freeSnapStretch(Inkscape::Snapper::PointType t,
630                                                         std::vector<NR::Point> const &p,
631                                                         std::list<SPItem const *> const &it,
632                                                         NR::Coord const &s,
633                                                         NR::Point const &o,
634                                                         NR::Dim2 d,
635                                                         bool u) const
637    std::pair<NR::Point, bool> const r = _snapTransformed(
638         t, p, it, false, NR::Point(), STRETCH, NR::Point(s, s), o, d, u
639         );
641    return std::make_pair(r.first[d], r.second);
645 /**
646  *  Try to snap a list of points to any interested snappers after they have undergone
647  *  a skew.
648  *
649  *  \param t Type of points.
650  *  \param p Points.
651  *  \param it List of items to ignore when snapping.
652  *  \param s Proposed skew.
653  *  \param o Origin of proposed skew.
654  *  \param d Dimension in which to apply proposed skew.
655  *  \return Snapped skew, if a snap occurred, and a flag indicating whether a snap occurred.
656  */
658 std::pair<NR::Coord, bool> SnapManager::freeSnapSkew(Inkscape::Snapper::PointType t,
659                                                      std::vector<NR::Point> const &p,
660                                                      std::list<SPItem const *> const &it,
661                                                      NR::Coord const &s,
662                                                      NR::Point const &o,
663                                                      NR::Dim2 d) const
665    std::pair<NR::Point, bool> const r = _snapTransformed(
666         t, p, it, false, NR::Point(), SKEW, NR::Point(s, s), o, d, false
667         );
669    return std::make_pair(r.first[d], r.second);
672 /*
673   Local Variables:
674   mode:c++
675   c-file-style:"stroustrup"
676   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
677   indent-tabs-mode:nil
678   fill-column:99
679   End:
680 */
681 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :