Code

Add 2 new object snapping modes: bbox to bbox, and nodes to bbox
[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"
26 #include "prefs-utils.h"
29 Inkscape::ObjectSnapper::ObjectSnapper(SPNamedView const *nv, NR::Coord const d)
30     : Snapper(nv, d), _snap_to_nodes(true), _snap_to_paths(true)
31 {
33 }
36 /**
37  *  \param p Point we are trying to snap (desktop coordinates)
38  */
40 void Inkscape::ObjectSnapper::_findCandidates(std::list<SPItem*>& c,
41                                               SPObject* r,
42                                               std::list<SPItem const *> const &it,
43                                               NR::Point const &p) const
44 {
45     if (ThisSnapperMightSnap()) {    
46         SPDesktop const *desktop = SP_ACTIVE_DESKTOP;
47         for (SPObject* o = sp_object_first_child(r); o != NULL; o = SP_OBJECT_NEXT(o)) {
48             if (SP_IS_ITEM(o) && !SP_ITEM(o)->isLocked() && !desktop->itemIsHidden(SP_ITEM(o))) {
49     
50                 /* See if this item is on the ignore list */
51                 std::list<SPItem const *>::const_iterator i = it.begin();
52                 while (i != it.end() && *i != o) {
53                     i++;
54                 }
55     
56                 if (i == it.end()) {
57                     /* See if the item is within range */
58                     if (SP_IS_GROUP(o)) {
59                         _findCandidates(c, o, it, p);
60                     } else {
61                         NR::Maybe<NR::Rect> b = sp_item_bbox_desktop(SP_ITEM(o));
62                         if ( b && NR::expand(*b, -getDistance()).contains(p) ) {
63                             c.push_back(SP_ITEM(o));
64                         }
65                     }
66                 }
67     
68             }
69         }
70     }
71 }
74 void Inkscape::ObjectSnapper::_snapNodes(Inkscape::SnappedPoint &s,
75                                          NR::Point const &p,
76                                          std::list<SPItem*> const &cand) const
77 {
78     /* FIXME: this seems like a hack.  Perhaps Snappers should be
79     ** in SPDesktop rather than SPNamedView?
80     */
81     SPDesktop const *desktop = SP_ACTIVE_DESKTOP;
82     
83     // Determine the type of bounding box we should snap to
84     //TODO if (_snap_to_bbox) ???? -> will save some cpu time 
85         gchar const *prefs_bbox = prefs_get_string_attribute("tools.select", "bounding_box");
86         SPItem::BBoxType bbox_type = (prefs_bbox != NULL && strcmp(prefs_bbox, "geometric")==0)? SPItem::GEOMETRIC_BBOX : SPItem::APPROXIMATE_BBOX;        
88     for (std::list<SPItem*>::const_iterator i = cand.begin(); i != cand.end(); i++) {
89         
90         NR::Matrix i2doc(NR::identity());
91         SPItem *root_item = NULL;
92         if (SP_IS_USE(*i)) {
93             i2doc = sp_use_get_root_transform(SP_USE(*i));
94             root_item = sp_use_root(SP_USE(*i));
95         } else { 
96             i2doc = sp_item_i2doc_affine(*i);
97             root_item = *i;
98         }
99         
100         SPCurve *curve = NULL;
101         
102         if (SP_IS_SHAPE(root_item)) {
103             SPShape const *sh = SP_SHAPE(root_item);
104             curve = sh->curve;
105         } else if (SP_IS_IMAGE(root_item)) {
106             SPImage const *im = SP_IMAGE(root_item);
107             curve = im->curve;
108         }
109             
110                 std::list<NR::Point> points_to_snap_to;
111         
112         //Collect all nodes so we can snap to them
113         if (curve) {
114             int j = 0;
115             while (SP_CURVE_BPATH(curve)[j].code != NR_END) {        
116                 /* Get this node in desktop coordinates */
117                 NArtBpath const &bp = SP_CURVE_BPATH(curve)[j];
118                 points_to_snap_to.push_back(desktop->doc2dt(bp.c(3) * i2doc));
119                 j++;
120             }
121         }
122         
123         //Collect the bounding box's corners so we can snap to them
124         NR::Maybe<NR::Rect> b = sp_item_bbox_desktop(root_item, bbox_type);
125         if (b) {
126                 for ( unsigned k = 0 ; k < 4 ; k++ ) {
127                     points_to_snap_to.push_back(b->corner(k));
128                 }
129         }        
130         
131         //Do the snapping, using all the nodes and corners collected above
132         for (std::list<NR::Point>::const_iterator k = points_to_snap_to.begin(); k != points_to_snap_to.end(); k++) {
133             /* Try to snap to this node of the path */
134             NR::Coord const dist = NR::L2(*k - p);
135             if (dist < getDistance() && dist < s.getDistance()) {
136                 s = SnappedPoint(*k, dist);
137             }       
138         }
139     }
143 void Inkscape::ObjectSnapper::_snapPaths(Inkscape::SnappedPoint &s,
144                                          NR::Point const &p,
145                                          std::list<SPItem*> const &cand) const
147     /* FIXME: this seems like a hack.  Perhaps Snappers should be
148     ** in SPDesktop rather than SPNamedView?
149     */
150     SPDesktop const *desktop = SP_ACTIVE_DESKTOP;
152     NR::Point const p_doc = desktop->dt2doc(p);
153     
154     // Determine the type of bounding box we should snap to
155     //TODO if (_snap_to_bbox) ???? -> will save some cpu time 
156         gchar const *prefs_bbox = prefs_get_string_attribute("tools.select", "bounding_box");
157         SPItem::BBoxType bbox_type = (prefs_bbox != NULL && strcmp(prefs_bbox, "geometric")==0)? SPItem::GEOMETRIC_BBOX : SPItem::APPROXIMATE_BBOX;        
158     
159     for (std::list<SPItem*>::const_iterator i = cand.begin(); i != cand.end(); i++) {
161         /* Transform the requested snap point to this item's coordinates */
162         NR::Matrix i2doc(NR::identity());
163         SPItem *root_item = NULL;
164         /* We might have a clone at hand, so make sure we get the root item */
165         if (SP_IS_USE(*i)) {
166             i2doc = sp_use_get_root_transform(SP_USE(*i));
167             root_item = sp_use_root(SP_USE(*i));
168         } else {
169             i2doc = sp_item_i2doc_affine(*i);
170             root_item = *i;
171         }
172         
173         //Build a list of all paths considered for snapping to
174         std::list<Path*> paths_to_snap_to;
175         
176         //Add the item's path to snap to
177         paths_to_snap_to.push_back(Path_for_item(root_item, true, true));
178                 
179         //Add the item's bounding box to snap to
180         //This will get ugly... rect -> curve -> bpath
181         NRRect rect;
182         sp_item_invoke_bbox(root_item, &rect, i2doc, TRUE, bbox_type);
183         NR::Maybe<NR::Rect> bbox = rect.upgrade();
184         SPCurve *curve = sp_curve_new_from_rect(bbox);
185         NArtBpath *bpath = SP_CURVE_BPATH(curve);
186         Path *path = bpath_to_Path(bpath);  
187         paths_to_snap_to.push_back(path);
188         delete curve;
189         delete bpath;
190         
191         //Now we can finally do the real snapping, using the paths collected above        
192         for (std::list<Path*>::const_iterator k = paths_to_snap_to.begin(); k != paths_to_snap_to.end(); k++) {
193                 if (*k) {
194                     (*k)->ConvertWithBackData(0.01);
195                 
196                         /* Look for the nearest position on this SPItem to our snap point */
197                         NR::Maybe<Path::cut_position> const o = get_nearest_position_on_Path(*k, p_doc);
198                         if (o && o->t >= 0 && o->t <= 1) {
199                 
200                             /* Convert the nearest point back to desktop coordinates */
201                             NR::Point const o_it = get_point_on_Path(*k, o->piece, o->t);                           
202                             NR::Point const o_dt = desktop->doc2dt(o_it);
203                                         
204                             NR::Coord const dist = NR::L2(o_dt - p);
205                             if (dist < getDistance() && dist < s.getDistance()) {
206                                 s = SnappedPoint(o_dt, dist);
207                             }
208                         }
209                 
210                         delete *k;
211                 }
212             }
213     }
217 Inkscape::SnappedPoint Inkscape::ObjectSnapper::_doFreeSnap(NR::Point const &p,
218                                                             std::list<SPItem const *> const &it) const
220     if ( NULL == _named_view ) {
221         return SnappedPoint(p, NR_HUGE);
222     }
224     /* Get a list of all the SPItems that we will try to snap to */
225     std::list<SPItem*> cand;
226     _findCandidates(cand, sp_document_root(_named_view->document), it, p);
228     SnappedPoint s(p, NR_HUGE);
230     if (_snap_to_nodes) {
231         _snapNodes(s, p, cand);
232     }
233     if (_snap_to_paths) {
234         _snapPaths(s, p, cand);
235     }
237     return s;
242 Inkscape::SnappedPoint Inkscape::ObjectSnapper::_doConstrainedSnap(NR::Point const &p,
243                                                                    ConstraintLine const &c,
244                                                                    std::list<SPItem const *> const &it) const
246     /* FIXME: this needs implementing properly; I think we have to do the
247     ** intersection of c with the objects.
248     */
249     return _doFreeSnap(p, it);
252 /**
253  *  \return true if this Snapper will snap at least one kind of point.
254  */
255 bool Inkscape::ObjectSnapper::ThisSnapperMightSnap() const
257     return (_enabled && _snap_to != 0 && (_snap_to_paths || _snap_to_nodes));
261 /*
262   Local Variables:
263   mode:c++
264   c-file-style:"stroustrup"
265   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
266   indent-tabs-mode:nil
267   fill-column:99
268   End:
269 */
270 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :