Code

implementing snapping to images
[inkscape.git] / src / object-snapper.cpp
1 /**
2  *  \file object-snapper.cpp
3  *  \brief Snapping things to objects.
4  *
5  * Authors:
6  *   Carl Hetherington <inkscape@carlh.net>
7  *
8  * Copyright (C) 2005 Authors
9  *
10  * Released under GNU GPL, read the file 'COPYING' for more information
11  */
13 #include "libnr/n-art-bpath.h"
14 #include "libnr/nr-rect-ops.h"
15 #include "document.h"
16 #include "sp-namedview.h"
17 #include "sp-path.h"
18 #include "sp-image.h"
19 #include "sp-item-group.h"
20 #include "sp-item.h"
21 #include "sp-use.h"
22 #include "display/curve.h"
23 #include "desktop.h"
24 #include "inkscape.h"
25 #include "splivarot.h"
28 Inkscape::ObjectSnapper::ObjectSnapper(SPNamedView const *nv, NR::Coord const d)
29     : Snapper(nv, d), _snap_to_nodes(true), _snap_to_paths(true)
30 {
32 }
35 /**
36  *  \param p Point we are trying to snap (desktop coordinates)
37  */
39 void Inkscape::ObjectSnapper::_findCandidates(std::list<SPItem*>& c,
40                                               SPObject* r,
41                                               std::list<SPItem const *> const &it,
42                                               NR::Point const &p) const
43 {
44     if (ThisSnapperMightSnap()) {    
45         SPDesktop const *desktop = SP_ACTIVE_DESKTOP;
46         for (SPObject* o = sp_object_first_child(r); o != NULL; o = SP_OBJECT_NEXT(o)) {
47             if (SP_IS_ITEM(o) && !SP_ITEM(o)->isLocked() && !desktop->itemIsHidden(SP_ITEM(o))) {
48     
49                 /* See if this item is on the ignore list */
50                 std::list<SPItem const *>::const_iterator i = it.begin();
51                 while (i != it.end() && *i != o) {
52                     i++;
53                 }
54     
55                 if (i == it.end()) {
56                     /* See if the item is within range */
57                     if (SP_IS_GROUP(o)) {
58                         _findCandidates(c, o, it, p);
59                     } else {
60                         NR::Maybe<NR::Rect> b = sp_item_bbox_desktop(SP_ITEM(o));
61                         if ( b && NR::expand(*b, -getDistance()).contains(p) ) {
62                             c.push_back(SP_ITEM(o));
63                         }
64                     }
65                 }
66     
67             }
68         }
69     }
70 }
73 void Inkscape::ObjectSnapper::_snapNodes(Inkscape::SnappedPoint &s,
74                                          NR::Point const &p,
75                                          std::list<SPItem*> const &cand) const
76 {
77     /* FIXME: this seems like a hack.  Perhaps Snappers should be
78     ** in SPDesktop rather than SPNamedView?
79     */
80     SPDesktop const *desktop = SP_ACTIVE_DESKTOP;
82     for (std::list<SPItem*>::const_iterator i = cand.begin(); i != cand.end(); i++) {
83         
84         NR::Matrix i2doc(NR::identity());
85         SPItem *root_item = NULL;
86         if (SP_IS_USE(*i)) {
87             i2doc = sp_use_get_root_transform(SP_USE(*i));
88             root_item = sp_use_root(SP_USE(*i));
89         } else { 
90             i2doc = sp_item_i2doc_affine(*i);
91             root_item = *i;
92         }
93         
94         SPCurve *curve = NULL;
95         
96         if (SP_IS_SHAPE(root_item)) {
97             SPShape const *sh = SP_SHAPE(root_item);
98             curve = sh->curve;
99         } else if (SP_IS_IMAGE(root_item)) {
100             SPImage const *im = SP_IMAGE(root_item);
101             curve = im->curve;
102         }
103             
104         if (curve) {
106             int j = 0;
108             while (SP_CURVE_BPATH(curve)[j].code != NR_END) {
109         
110                 /* Get this node in desktop coordinates */
111                 NArtBpath const &bp = SP_CURVE_BPATH(curve)[j];
112                 NR::Point const n = desktop->doc2dt(bp.c(3) * i2doc);
113         
114                 /* Try to snap to this node of the path */
115                 NR::Coord const dist = NR::L2(n - p);
116                 if (dist < getDistance() && dist < s.getDistance()) {
117                     s = SnappedPoint(n, dist);
118                 }
120                 j++;
121             }
122         }
123     }
127 void Inkscape::ObjectSnapper::_snapPaths(Inkscape::SnappedPoint &s,
128                                          NR::Point const &p,
129                                          std::list<SPItem*> const &cand) const
131     /* FIXME: this seems like a hack.  Perhaps Snappers should be
132     ** in SPDesktop rather than SPNamedView?
133     */
134     SPDesktop const *desktop = SP_ACTIVE_DESKTOP;
136     NR::Point const p_doc = desktop->dt2doc(p);
137     
138     for (std::list<SPItem*>::const_iterator i = cand.begin(); i != cand.end(); i++) {
140         /* Transform the requested snap point to this item's coordinates */
141         NR::Matrix i2doc(NR::identity());
142         SPItem *root_item = NULL;
143         /* We might have a clone at hand, so make sure we get the root item */
144         if (SP_IS_USE(*i)) {
145             i2doc = sp_use_get_root_transform(SP_USE(*i));
146             root_item = sp_use_root(SP_USE(*i));
147         } else {
148             i2doc = sp_item_i2doc_affine(*i);
149             root_item = *i;
150         }
151         
152         NR::Point const p_it = p_doc * i2doc.inverse();
154         Path *livarot_path = Path_for_item(root_item, false, false);
155         if (!livarot_path)
156             continue;
158         livarot_path->ConvertWithBackData(0.01);
160         /* Look for the nearest position on this SPItem to our snap point */
161         NR::Maybe<Path::cut_position> const o = get_nearest_position_on_Path(livarot_path, p_it);
162         if (o && o->t >= 0 && o->t <= 1) {
164             /* Convert the nearest point back to desktop coordinates */
165             NR::Point const o_it = get_point_on_Path(livarot_path, o->piece, o->t);
166             NR::Point const o_dt = desktop->doc2dt(o_it * i2doc);
168             NR::Coord const dist = NR::L2(o_dt - p);
169             if (dist < getDistance() && dist < s.getDistance()) {
170                 s = SnappedPoint(o_dt, dist);
171             }
172         }
174         delete livarot_path;
175     }
179 Inkscape::SnappedPoint Inkscape::ObjectSnapper::_doFreeSnap(NR::Point const &p,
180                                                             std::list<SPItem const *> const &it) const
182     if ( NULL == _named_view ) {
183         return SnappedPoint(p, NR_HUGE);
184     }
186     /* Get a list of all the SPItems that we will try to snap to */
187     std::list<SPItem*> cand;
188     _findCandidates(cand, sp_document_root(_named_view->document), it, p);
190     SnappedPoint s(p, NR_HUGE);
192     if (_snap_to_nodes) {
193         _snapNodes(s, p, cand);
194     }
195     if (_snap_to_paths) {
196         _snapPaths(s, p, cand);
197     }
199     return s;
204 Inkscape::SnappedPoint Inkscape::ObjectSnapper::_doConstrainedSnap(NR::Point const &p,
205                                                                    ConstraintLine const &c,
206                                                                    std::list<SPItem const *> const &it) const
208     /* FIXME: this needs implementing properly; I think we have to do the
209     ** intersection of c with the objects.
210     */
211     return _doFreeSnap(p, it);
214 /**
215  *  \return true if this Snapper will snap at least one kind of point.
216  */
217 bool Inkscape::ObjectSnapper::ThisSnapperMightSnap() const
219     return (_enabled && _snap_to != 0 && (_snap_to_paths || _snap_to_nodes));
223 /*
224   Local Variables:
225   mode:c++
226   c-file-style:"stroustrup"
227   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
228   indent-tabs-mode:nil
229   fill-column:99
230   End:
231 */
232 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :