Code

Make snapping to the item's transformation center optional, but not yet available...
[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"
30 /**
31  *  Construct a SnapManager for a SPNamedView.
32  *
33  *  \param v `Owning' SPNamedView.
34  */
36 SnapManager::SnapManager(SPNamedView const *v) :
37     guide(v, 0),
38     object(v, 0),
39     _named_view(v),
40     _include_item_center(false)
41 {
42         
43 }
46 /**
47  *  \return List of snappers that we use.
48  */
49 SnapManager::SnapperList 
50 SnapManager::getSnappers() const
51 {
52     SnapManager::SnapperList s;
53     s.push_back(&guide);
54     s.push_back(&object);
56     SnapManager::SnapperList gs = getGridSnappers();
57     s.splice(s.begin(), gs);
59     return s;
60 }
62 /**
63  *  \return List of gridsnappers that we use.
64  */
65 SnapManager::SnapperList 
66 SnapManager::getGridSnappers() const
67 {
68     SnapperList s;
70     //FIXME: this code should actually do this: add new grid snappers that are active for this desktop. now it just adds all gridsnappers
71     SPDesktop* desktop = SP_ACTIVE_DESKTOP;
72     if (desktop && desktop->gridsEnabled()) {
73         for ( GSList const *l = _named_view->grids; l != NULL; l = l->next) {
74             Inkscape::CanvasGrid *grid = (Inkscape::CanvasGrid*) l->data;
75             s.push_back(grid->snapper);
76         }
77     }
79     return s;
80 }
82 /**
83  * \return true if one of the snappers will try to snap something.
84  */
86 bool SnapManager::SomeSnapperMightSnap() const
87 {
88     SnapperList const s = getSnappers();
89     SnapperList::const_iterator i = s.begin();
90     while (i != s.end() && (*i)->ThisSnapperMightSnap() == false) {
91         i++;
92     }
93     
94     return (i != s.end());
95 }
97 /*
98  *  The snappers have too many parameters to adjust individually. Therefore only
99  *  two snapping modes are presented to the user: snapping bounding box corners (to 
100  *      other bounding boxes, grids or guides), and/or snapping nodes (to other nodes,
101  *  paths, grids or guides). To select either of these modes (or both), use the 
102  *  methods defined below: setSnapModeBBox() and setSnapModeNode().
103  * 
104  * */
107 void SnapManager::setSnapModeBBox(bool enabled)
109         //The default values are being set in sp_namedview_set() (in sp-namedview.cpp)
110         guide.setSnapFrom(Inkscape::Snapper::SNAPPOINT_BBOX, enabled);
111         
112         for ( GSList const *l = _named_view->grids; l != NULL; l = l->next) {
113         Inkscape::CanvasGrid *grid = (Inkscape::CanvasGrid*) l->data;
114         grid->snapper->setSnapFrom(Inkscape::Snapper::SNAPPOINT_BBOX, enabled);
115     }
116         
117         object.setSnapFrom(Inkscape::Snapper::SNAPPOINT_BBOX, enabled);
118         object.setSnapToBBoxNode(enabled);
119         object.setSnapToBBoxPath(enabled);
120         object.setStrictSnapping(true); //don't snap bboxes to nodes/paths and vice versa       
123 bool SnapManager::getSnapModeBBox() const
125         return guide.getSnapFrom(Inkscape::Snapper::SNAPPOINT_BBOX);
128 void SnapManager::setSnapModeNode(bool enabled)
130         guide.setSnapFrom(Inkscape::Snapper::SNAPPOINT_NODE, enabled);
131         
132         for ( GSList const *l = _named_view->grids; l != NULL; l = l->next) {
133         Inkscape::CanvasGrid *grid = (Inkscape::CanvasGrid*) l->data;
134         grid->snapper->setSnapFrom(Inkscape::Snapper::SNAPPOINT_NODE, enabled);
135     }
136         
137         object.setSnapFrom(Inkscape::Snapper::SNAPPOINT_NODE, enabled);
138         //object.setSnapToItemNode(enabled); // On second thought, these should be controlled
139         //object.setSnapToItemPath(enabled); // separately by the snapping prefs dialog 
140         object.setStrictSnapping(true); 
143 bool SnapManager::getSnapModeNode() const
145         return guide.getSnapFrom(Inkscape::Snapper::SNAPPOINT_NODE);
148 /**
149  *  Try to snap a point to any interested snappers.
150  *
151  *  \param t Type of point.
152  *  \param p Point.
153  *  \param it Item to ignore when snapping.
154  *  \return Snapped point.
155  */
157 Inkscape::SnappedPoint SnapManager::freeSnap(Inkscape::Snapper::PointType t,
158                                              NR::Point const &p,
159                                              SPItem const *it) const
162     std::list<SPItem const *> lit;
163     lit.push_back(it);
164     return freeSnap(t, p, lit);
168 /**
169  *  Try to snap a point to any interested snappers.
170  *
171  *  \param t Type of point.
172  *  \param p Point.
173  *  \param it List of items to ignore when snapping.
174  *  \return Snapped point.
175  */
177 Inkscape::SnappedPoint SnapManager::freeSnap(Inkscape::Snapper::PointType t,
178                                              NR::Point const &p,
179                                              std::list<SPItem const *> const &it) const
181     SnapperList const snappers = getSnappers();
183     return freeSnap(t, p, it, snappers);
186 /**
187  *  Try to snap a point to any of the specified snappers.
188  *
189  *  \param t Type of point.
190  *  \param p Point.
191  *  \param it List of items to ignore when snapping.
192  * \param snappers  List of snappers to try to snap to
193  *  \return Snapped point.
194  */
196 Inkscape::SnappedPoint SnapManager::freeSnap(Inkscape::Snapper::PointType t,
197                                              NR::Point const &p,
198                                              std::list<SPItem const *> const &it,
199                                              SnapperList const &snappers) const
201     Inkscape::SnappedPoint r(p, NR_HUGE);
203     for (SnapperList::const_iterator i = snappers.begin(); i != snappers.end(); i++) {
204         Inkscape::SnappedPoint const s = (*i)->freeSnap(t, p, it);
205         if (s.getDistance() < r.getDistance()) {
206             r = s;
207         }
208     }
210     return r;
213 /**
214  *  Try to snap a point to any of the specified snappers. Snap always, ignoring the snap-distance
215  *
216  *  \param t Type of point.
217  *  \param p Point.
218  *  \param it Item to ignore when snapping.
219  * \param snappers  List of snappers to try to snap to
220  *  \return Snapped point.
221  */
223 Inkscape::SnappedPoint
224 SnapManager::freeSnapAlways( Inkscape::Snapper::PointType t,
225                              NR::Point const &p,
226                              SPItem const *it,
227                              SnapperList &snappers )
229     std::list<SPItem const *> lit;
230     lit.push_back(it);
231     return freeSnapAlways(t, p, lit, snappers);
234 /**
235  *  Try to snap a point to any of the specified snappers. Snap always, ignoring the snap-distance
236  *
237  *  \param t Type of point.
238  *  \param p Point.
239  *  \param it List of items to ignore when snapping.
240  * \param snappers  List of snappers to try to snap to
241  *  \return Snapped point.
242  */
244 Inkscape::SnappedPoint
245 SnapManager::freeSnapAlways( Inkscape::Snapper::PointType t,
246                              NR::Point const &p,
247                              std::list<SPItem const *> const &it,
248                              SnapperList &snappers )
250     Inkscape::SnappedPoint r(p, NR_HUGE);
252     for (SnapperList::iterator i = snappers.begin(); i != snappers.end(); i++) {
253         gdouble const curr_gridsnap = (*i)->getDistance();
254         const_cast<Inkscape::Snapper*> (*i)->setDistance(NR_HUGE);
255         Inkscape::SnappedPoint const s = (*i)->freeSnap(t, p, it);
256         const_cast<Inkscape::Snapper*> (*i)->setDistance(curr_gridsnap);
258         if (s.getDistance() < r.getDistance()) {
259             r = s;
260         }
261     }
263     return r;
268 /**
269  *  Try to snap a point to any interested snappers.  A snap will only occur along
270  *  a line described by a Inkscape::Snapper::ConstraintLine.
271  *
272  *  \param t Type of point.
273  *  \param p Point.
274  *  \param c Constraint line.
275  *  \param it Item to ignore when snapping.
276  *  \return Snapped point.
277  */
279 Inkscape::SnappedPoint SnapManager::constrainedSnap(Inkscape::Snapper::PointType t,
280                                                     NR::Point const &p,
281                                                     Inkscape::Snapper::ConstraintLine const &c,
282                                                     SPItem const *it) const
284     std::list<SPItem const *> lit;
285     lit.push_back(it);
286     return constrainedSnap(t, p, c, lit);
291 /**
292  *  Try to snap a point to any interested snappers.  A snap will only occur along
293  *  a line described by a Inkscape::Snapper::ConstraintLine.
294  *
295  *  \param t Type of point.
296  *  \param p Point.
297  *  \param c Constraint line.
298  *  \param it List of items to ignore when snapping.
299  *  \return Snapped point.
300  */
302 Inkscape::SnappedPoint SnapManager::constrainedSnap(Inkscape::Snapper::PointType t,
303                                                     NR::Point const &p,
304                                                     Inkscape::Snapper::ConstraintLine const &c,
305                                                     std::list<SPItem const *> const &it) const
307     Inkscape::SnappedPoint r(p, NR_HUGE);
309     SnapperList const snappers = getSnappers();
310     for (SnapperList::const_iterator i = snappers.begin(); i != snappers.end(); i++) {
311         Inkscape::SnappedPoint const s = (*i)->constrainedSnap(t, p, c, it);
312         if (s.getDistance() < r.getDistance()) {
313             r = s;
314         }
315     }
317     return r;
322 /**
323  *  Main internal snapping method, which is called by the other, friendlier, public
324  *  methods.  It's a bit hairy as it has lots of parameters, but it saves on a lot
325  *  of duplicated code.
326  *
327  *  \param type Type of points being snapped.
328  *  \param points List of points to snap.
329  *  \param ignore List of items to ignore while snapping.
330  *  \param constrained true if the snap is constrained.
331  *  \param constraint Constraint line to use, if `constrained' is true, otherwise undefined.
332  *  \param transformation_type Type of transformation to apply to points before trying to snap them.
333  *  \param transformation Description of the transformation; details depend on the type.
334  *  \param origin Origin of the transformation, if applicable.
335  *  \param dim Dimension of the transformation, if applicable.
336  *  \param uniform true if the transformation should be uniform, if applicable.
337  */
339 std::pair<NR::Point, bool> SnapManager::_snapTransformed(
340     Inkscape::Snapper::PointType type,
341     std::vector<NR::Point> const &points,
342     std::list<SPItem const *> const &ignore,
343     bool constrained,
344     Inkscape::Snapper::ConstraintLine const &constraint,
345     Transformation transformation_type,
346     NR::Point const &transformation,
347     NR::Point const &origin,
348     NR::Dim2 dim,
349     bool uniform) const
351     /* We have a list of points, which we are proposing to transform in some way.  We need to see
352     ** if any of these points, when transformed, snap to anything.  If they do, we return the
353     ** appropriate transformation with `true'; otherwise we return the original scale with `false'.
354     */
356     /* Quick check to see if we have any snappers that are enabled */
357     if (SomeSnapperMightSnap() == false) {
358         return std::make_pair(transformation, false);
359     }
361     /* The current best transformation */
362     NR::Point best_transformation = transformation;
364     /* The current best metric for the best transformation; lower is better, NR_HUGE
365     ** means that we haven't snapped anything.
366     */
367     double best_metric = NR_HUGE;
369     for (std::vector<NR::Point>::const_iterator i = points.begin(); i != points.end(); i++) {
371         /* Work out the transformed version of this point */
372         NR::Point transformed;
373         switch (transformation_type) {
374             case TRANSLATION:
375                 transformed = *i + transformation;
376                 break;
377             case SCALE:
378                 transformed = ((*i - origin) * NR::scale(transformation[NR::X], transformation[NR::Y])) + origin;
379                 break;
380             case STRETCH:
381             {
382                 NR::scale s(1, 1);
383                 if (uniform)
384                     s[NR::X] = s[NR::Y] = transformation[dim];
385                 else {
386                     s[dim] = transformation[dim];
387                     s[1 - dim] = 1;
388                 }
389                 transformed = ((*i - origin) * s) + origin;
390                 break;
391             }
392             case SKEW:
393                 transformed = *i;
394                 transformed[dim] += transformation[dim] * ((*i)[1 - dim] - origin[1 - dim]);
395                 break;
396             default:
397                 g_assert_not_reached();
398         }
400         /* Snap it */
401         Inkscape::SnappedPoint const snapped = constrained ?
402             constrainedSnap(type, transformed, constraint, ignore) : freeSnap(type, transformed, ignore);
404         if (snapped.getDistance() < NR_HUGE) {
405             /* We snapped.  Find the transformation that describes where the snapped point has
406             ** ended up, and also the metric for this transformation.
407             */
408             NR::Point result;
409             NR::Coord metric;
410             switch (transformation_type) {
411                 case TRANSLATION:
412                     result = snapped.getPoint() - *i;
413                     metric = NR::L2(result);
414                     break;
415                 case SCALE:
416                 {
417                     NR::Point const a = (snapped.getPoint() - origin);
418                     NR::Point const b = (*i - origin);
419                     result = NR::Point(a[NR::X] / b[NR::X], a[NR::Y] / b[NR::Y]);
420                     metric = std::abs(NR::L2(result) - NR::L2(transformation));
421                     break;
422                 }
423                 case STRETCH:
424                 {
425                     for (int j = 0; j < 2; j++) {
426                         if (uniform || j == dim) {
427                             result[j] = (snapped.getPoint()[dim] - origin[dim]) / ((*i)[dim] - origin[dim]);
428                         } else {
429                             result[j] = 1;
430                         }
431                     }
432                     metric = std::abs(result[dim] - transformation[dim]);
433                     break;
434                 }
435                 case SKEW:
436                     result[dim] = (snapped.getPoint()[dim] - (*i)[dim]) / ((*i)[1 - dim] - origin[1 - dim]);
437                     metric = std::abs(result[dim] - transformation[dim]);
438                     break;
439                 default:
440                     g_assert_not_reached();
441             }
443             /* Note it if it's the best so far */
444             if (metric < best_metric && metric != 0) {
445                 best_transformation = result;
446                 best_metric = metric;
447             }
448         }
449     }
451     // Using " < 1e6" instead of " < NR::HUGE" for catching some rounding errors
452     // These rounding errors might be caused by NRRects, see bug #1584301
453     return std::make_pair(best_transformation, best_metric < 1e6);
457 /**
458  *  Try to snap a list of points to any interested snappers after they have undergone
459  *  a translation.
460  *
461  *  \param t Type of points.
462  *  \param p Points.
463  *  \param it List of items to ignore when snapping.
464  *  \param tr Proposed translation.
465  *  \return Snapped translation, if a snap occurred, and a flag indicating whether a snap occurred.
466  */
468 std::pair<NR::Point, bool> SnapManager::freeSnapTranslation(Inkscape::Snapper::PointType t,
469                                                             std::vector<NR::Point> const &p,
470                                                             std::list<SPItem const *> const &it,
471                                                             NR::Point const &tr) const
473     return _snapTransformed(
474         t, p, it, false, NR::Point(), TRANSLATION, tr, NR::Point(), NR::X, false
475         );
479 /**
480  *  Try to snap a list of points to any interested snappers after they have undergone a
481  *  translation.  A snap will only occur along a line described by a
482  *  Inkscape::Snapper::ConstraintLine.
483  *
484  *  \param t Type of points.
485  *  \param p Points.
486  *  \param it List of items to ignore when snapping.
487  *  \param c Constraint line.
488  *  \param tr Proposed translation.
489  *  \return Snapped translation, if a snap occurred, and a flag indicating whether a snap occurred.
490  */
492 std::pair<NR::Point, bool> SnapManager::constrainedSnapTranslation(Inkscape::Snapper::PointType t,
493                                                                    std::vector<NR::Point> const &p,
494                                                                    std::list<SPItem const *> const &it,
495                                                                    Inkscape::Snapper::ConstraintLine const &c,
496                                                                    NR::Point const &tr) const
498     return _snapTransformed(
499         t, p, it, true, c, TRANSLATION, tr, NR::Point(), NR::X, false
500         );
504 /**
505  *  Try to snap a list of points to any interested snappers after they have undergone
506  *  a scale.
507  *
508  *  \param t Type of points.
509  *  \param p Points.
510  *  \param it List of items to ignore when snapping.
511  *  \param s Proposed scale.
512  *  \param o Origin of proposed scale.
513  *  \return Snapped scale, if a snap occurred, and a flag indicating whether a snap occurred.
514  */
516 std::pair<NR::scale, bool> SnapManager::freeSnapScale(Inkscape::Snapper::PointType t,
517                                                       std::vector<NR::Point> const &p,
518                                                       std::list<SPItem const *> const &it,
519                                                       NR::scale const &s,
520                                                       NR::Point const &o) const
522     return _snapTransformed(
523         t, p, it, false, NR::Point(), SCALE, NR::Point(s[NR::X], s[NR::Y]), o, NR::X, false
524         );
528 /**
529  *  Try to snap a list of points to any interested snappers after they have undergone
530  *  a scale.  A snap will only occur along a line described by a
531  *  Inkscape::Snapper::ConstraintLine.
532  *
533  *  \param t Type of points.
534  *  \param p Points.
535  *  \param it List of items to ignore when snapping.
536  *  \param s Proposed scale.
537  *  \param o Origin of proposed scale.
538  *  \return Snapped scale, if a snap occurred, and a flag indicating whether a snap occurred.
539  */
541 std::pair<NR::scale, bool> SnapManager::constrainedSnapScale(Inkscape::Snapper::PointType t,
542                                                              std::vector<NR::Point> const &p,
543                                                              std::list<SPItem const *> const &it,
544                                                              Inkscape::Snapper::ConstraintLine const &c,
545                                                              NR::scale const &s,
546                                                              NR::Point const &o) const
548     return _snapTransformed(
549         t, p, it, true, c, SCALE, NR::Point(s[NR::X], s[NR::Y]), o, NR::X, false
550         );
554 /**
555  *  Try to snap a list of points to any interested snappers after they have undergone
556  *  a stretch.
557  *
558  *  \param t Type of points.
559  *  \param p Points.
560  *  \param it List of items to ignore when snapping.
561  *  \param s Proposed stretch.
562  *  \param o Origin of proposed stretch.
563  *  \param d Dimension in which to apply proposed stretch.
564  *  \param u true if the stretch should be uniform (ie to be applied equally in both dimensions)
565  *  \return Snapped stretch, if a snap occurred, and a flag indicating whether a snap occurred.
566  */
568 std::pair<NR::Coord, bool> SnapManager::freeSnapStretch(Inkscape::Snapper::PointType t,
569                                                         std::vector<NR::Point> const &p,
570                                                         std::list<SPItem const *> const &it,
571                                                         NR::Coord const &s,
572                                                         NR::Point const &o,
573                                                         NR::Dim2 d,
574                                                         bool u) const
576    std::pair<NR::Point, bool> const r = _snapTransformed(
577         t, p, it, false, NR::Point(), STRETCH, NR::Point(s, s), o, d, u
578         );
580    return std::make_pair(r.first[d], r.second);
584 /**
585  *  Try to snap a list of points to any interested snappers after they have undergone
586  *  a skew.
587  *
588  *  \param t Type of points.
589  *  \param p Points.
590  *  \param it List of items to ignore when snapping.
591  *  \param s Proposed skew.
592  *  \param o Origin of proposed skew.
593  *  \param d Dimension in which to apply proposed skew.
594  *  \return Snapped skew, if a snap occurred, and a flag indicating whether a snap occurred.
595  */
597 std::pair<NR::Coord, bool> SnapManager::freeSnapSkew(Inkscape::Snapper::PointType t,
598                                                      std::vector<NR::Point> const &p,
599                                                      std::list<SPItem const *> const &it,
600                                                      NR::Coord const &s,
601                                                      NR::Point const &o,
602                                                      NR::Dim2 d) const
604    std::pair<NR::Point, bool> const r = _snapTransformed(
605         t, p, it, false, NR::Point(), SKEW, NR::Point(s, s), o, d, false
606         );
608    return std::make_pair(r.first[d], r.second);
611 /*
612   Local Variables:
613   mode:c++
614   c-file-style:"stroustrup"
615   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
616   indent-tabs-mode:nil
617   fill-column:99
618   End:
619 */
620 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :