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-image.h"
18 #include "sp-item-group.h"
19 #include "sp-item.h"
20 #include "sp-use.h"
21 #include "display/curve.h"
22 #include "desktop.h"
23 #include "inkscape.h"
24 #include "prefs-utils.h"
25 #include "sp-text.h"
26 #include "sp-flowtext.h"
27 #include "text-editing.h"
29 Inkscape::ObjectSnapper::ObjectSnapper(SPNamedView const *nv, NR::Coord const d)
30 : Snapper(nv, d), _snap_to_itemnode(true), _snap_to_itempath(true),
31 _snap_to_bboxnode(true), _snap_to_bboxpath(true), _strict_snapping(true),
32 _include_item_center(false)
33 {
34 _candidates = new std::list<SPItem*>;
35 _points_to_snap_to = new std::list<NR::Point>;
36 _paths_to_snap_to = new std::list<Path*>;
37 }
39 Inkscape::ObjectSnapper::~ObjectSnapper()
40 {
41 _candidates->clear(); //Don't delete the candidates themselves, as these are not ours!
42 delete _candidates;
44 _points_to_snap_to->clear();
45 delete _points_to_snap_to;
47 for (std::list<Path*>::const_iterator k = _paths_to_snap_to->begin(); k != _paths_to_snap_to->end(); k++) {
48 delete *k;
49 }
50 _paths_to_snap_to->clear();
51 delete _paths_to_snap_to;
52 }
54 /**
55 * Find all items within snapping range.
56 * \param r Pointer to the current document
57 * \param it List of items to ignore
58 * \param first_point If true then this point is the first one from a whole bunch of points
59 * \param points_to_snap The whole bunch of points, all from the same selection and having the same transformation
60 * \param DimensionToSnap Snap in X, Y, or both directions.
61 */
63 void Inkscape::ObjectSnapper::_findCandidates(SPObject* r,
64 std::list<SPItem const *> const &it,
65 bool const &first_point,
66 std::vector<NR::Point> &points_to_snap,
67 DimensionToSnap const snap_dim) const
68 {
69 if (ThisSnapperMightSnap()) {
70 SPDesktop const *desktop = SP_ACTIVE_DESKTOP;
72 if (first_point) {
73 _candidates->clear();
74 }
76 for (SPObject* o = sp_object_first_child(r); o != NULL; o = SP_OBJECT_NEXT(o)) {
77 if (SP_IS_ITEM(o) && !SP_ITEM(o)->isLocked() && !desktop->itemIsHidden(SP_ITEM(o))) {
79 /* See if this item is on the ignore list */
80 std::list<SPItem const *>::const_iterator i = it.begin();
81 while (i != it.end() && *i != o) {
82 i++;
83 }
85 if (i == it.end()) {
86 /* See if the item is within range */
87 if (SP_IS_GROUP(o)) {
88 _findCandidates(o, it, false, points_to_snap, snap_dim);
89 } else {
90 // Now let's see if any of the snapping points is within
91 // snapping range of this object
92 NR::Maybe<NR::Rect> b = sp_item_bbox_desktop(SP_ITEM(o));
93 if (b) {
94 for (std::vector<NR::Point>::const_iterator i = points_to_snap.begin(); i != points_to_snap.end(); i++) {
95 NR::Point b_min = b->min();
96 NR::Point b_max = b->max();
97 double d = getDistance();
98 bool withinX = ((*i)[NR::X] >= b_min[NR::X] - d) && ((*i)[NR::X] <= b_max[NR::X] + d);
99 bool withinY = ((*i)[NR::Y] >= b_min[NR::Y] - d) && ((*i)[NR::Y] <= b_max[NR::Y] + d);
100 if (snap_dim == SNAP_X && withinX || snap_dim == SNAP_Y && withinY || snap_dim == SNAP_XY && withinX && withinY) {
101 //We've found a point that is within snapping range
102 //of this object, so record it as a candidate
103 _candidates->push_back(SP_ITEM(o));
104 break;
105 }
106 }
107 }
108 }
109 }
110 }
111 }
112 }
113 }
116 void Inkscape::ObjectSnapper::_snapNodes(Inkscape::Snapper::PointType const &t,
117 Inkscape::SnappedPoint &s,
118 NR::Point const &p,
119 bool const &first_point,
120 DimensionToSnap const snap_dim) const
121 {
122 /* FIXME: this seems like a hack. Perhaps Snappers should be
123 ** in SPDesktop rather than SPNamedView?
124 */
125 SPDesktop const *desktop = SP_ACTIVE_DESKTOP;
127 // Determine the type of bounding box we should snap to
128 SPItem::BBoxType bbox_type = SPItem::GEOMETRIC_BBOX;
129 if (_snap_to_bboxnode) {
130 gchar const *prefs_bbox = prefs_get_string_attribute("tools.select", "bounding_box");
131 bbox_type = (prefs_bbox != NULL && strcmp(prefs_bbox, "geometric")==0)? SPItem::GEOMETRIC_BBOX : SPItem::APPROXIMATE_BBOX;
132 }
134 bool p_is_a_node = t & Inkscape::Snapper::SNAPPOINT_NODE;
135 bool p_is_a_bbox = t & Inkscape::Snapper::SNAPPOINT_BBOX;
136 bool p_is_a_guide = t & Inkscape::Snapper::SNAPPOINT_GUIDE;
138 // A point considered for snapping should be either a node, a bbox corner or a guide. Pick only ONE!
139 g_assert(!(p_is_a_node && p_is_a_bbox || p_is_a_bbox && p_is_a_guide || p_is_a_node && p_is_a_guide));
141 // Now, let's first collect all points to snap to. If we have a whole bunch of points to snap,
142 // e.g. when translating an item using the selector tool, then we will only do this for the
143 // first point and store the collection for later use. This dramatically improves the performance
144 if (first_point) {
145 _points_to_snap_to->clear();
146 for (std::list<SPItem*>::const_iterator i = _candidates->begin(); i != _candidates->end(); i++) {
148 NR::Matrix i2doc(NR::identity());
149 SPItem *root_item = NULL;
150 if (SP_IS_USE(*i)) {
151 i2doc = sp_use_get_root_transform(SP_USE(*i));
152 root_item = sp_use_root(SP_USE(*i));
153 } else {
154 i2doc = sp_item_i2doc_affine(*i);
155 root_item = *i;
156 }
158 SPCurve *curve = NULL;
160 if (SP_IS_SHAPE(root_item)) {
161 SPShape const *sh = SP_SHAPE(root_item);
162 curve = sh->curve;
163 } else if (SP_IS_IMAGE(root_item)) {
164 SPImage const *im = SP_IMAGE(root_item);
165 curve = im->curve;
166 }
168 //Collect all nodes so we can snap to them
169 if (_snap_to_itemnode) {
170 if (!(_strict_snapping && !p_is_a_node) || p_is_a_guide) {
171 if (curve) {
172 int j = 0;
173 while (SP_CURVE_BPATH(curve)[j].code != NR_END) {
174 /* Get this node in desktop coordinates */
175 NArtBpath const &bp = SP_CURVE_BPATH(curve)[j];
176 _points_to_snap_to->push_back(desktop->doc2dt(bp.c(3) * i2doc));
177 j++;
178 }
179 if (_include_item_center) {
180 _points_to_snap_to->push_back(root_item->getCenter());
181 }
182 }
183 }
184 }
186 //Collect the bounding box's corners so we can snap to them
187 if (_snap_to_bboxnode) {
188 if (!(_strict_snapping && !p_is_a_bbox) || p_is_a_guide) {
189 NR::Maybe<NR::Rect> b = sp_item_bbox_desktop(root_item, bbox_type);
190 if (b) {
191 for ( unsigned k = 0 ; k < 4 ; k++ ) {
192 _points_to_snap_to->push_back(b->corner(k));
193 }
194 }
195 }
196 }
197 }
198 }
200 //Do the snapping, using all the nodes and corners collected above
201 for (std::list<NR::Point>::const_iterator k = _points_to_snap_to->begin(); k != _points_to_snap_to->end(); k++) {
202 /* Try to snap to this node of the path */
203 NR::Coord dist = NR_HUGE;
204 NR::Point snapped_point;
205 switch (snap_dim) {
206 case SNAP_X:
207 dist = fabs((*k)[NR::X] - p[NR::X]);
208 snapped_point = NR::Point((*k)[NR::X], p[NR::Y]);
209 break;
210 case SNAP_Y:
211 dist = fabs((*k)[NR::Y] - p[NR::Y]);
212 snapped_point = NR::Point(p[NR::X], (*k)[NR::Y]);
213 break;
214 case SNAP_XY:
215 dist = NR::L2(*k - p);
216 snapped_point = *k;
217 break;
218 }
219 if (dist < getDistance() && dist < s.getDistance()) {
220 s = SnappedPoint(snapped_point, dist);
221 }
222 }
223 }
226 void Inkscape::ObjectSnapper::_snapPaths(Inkscape::Snapper::PointType const &t,
227 Inkscape::SnappedPoint &s,
228 NR::Point const &p,
229 bool const &first_point) const
230 {
231 /* FIXME: this seems like a hack. Perhaps Snappers should be
232 ** in SPDesktop rather than SPNamedView?
233 */
234 SPDesktop const *desktop = SP_ACTIVE_DESKTOP;
236 NR::Point const p_doc = desktop->dt2doc(p);
238 // Determine the type of bounding box we should snap to
239 SPItem::BBoxType bbox_type = SPItem::GEOMETRIC_BBOX;
240 if (_snap_to_bboxpath) {
241 gchar const *prefs_bbox = prefs_get_string_attribute("tools.select", "bounding_box");
242 bbox_type = (prefs_bbox != NULL && strcmp(prefs_bbox, "geometric")==0)? SPItem::GEOMETRIC_BBOX : SPItem::APPROXIMATE_BBOX;
243 }
245 bool p_is_a_node = t & Inkscape::Snapper::SNAPPOINT_NODE;
247 // Now, let's first collect all paths to snap to. If we have a whole bunch of points to snap,
248 // e.g. when translating an item using the selector tool, then we will only do this for the
249 // first point and store the collection for later use. This dramatically improves the performance
250 if (first_point) {
251 for (std::list<Path*>::const_iterator k = _paths_to_snap_to->begin(); k != _paths_to_snap_to->end(); k++) {
252 delete *k;
253 }
254 _paths_to_snap_to->clear();
255 for (std::list<SPItem*>::const_iterator i = _candidates->begin(); i != _candidates->end(); i++) {
257 /* Transform the requested snap point to this item's coordinates */
258 NR::Matrix i2doc(NR::identity());
259 SPItem *root_item = NULL;
260 /* We might have a clone at hand, so make sure we get the root item */
261 if (SP_IS_USE(*i)) {
262 i2doc = sp_use_get_root_transform(SP_USE(*i));
263 root_item = sp_use_root(SP_USE(*i));
264 } else {
265 i2doc = sp_item_i2doc_affine(*i);
266 root_item = *i;
267 }
269 //Build a list of all paths considered for snapping to
271 //Add the item's path to snap to
272 if (_snap_to_itempath) {
273 if (!(_strict_snapping && !p_is_a_node)) {
274 // Snapping to the path of characters is very cool, but for a large
275 // chunk of text this will take ages! So limit snapping to text paths
276 // containing max. 240 characters. Snapping the bbox will not be affected
277 bool very_lenghty_prose = false;
278 if (SP_IS_TEXT(root_item) || SP_IS_FLOWTEXT(root_item)) {
279 very_lenghty_prose = sp_text_get_length(SP_TEXT(root_item)) > 240;
280 }
282 // On my AMD 3000+, the snapping lag becomes annoying at approx. 240 chars
283 // which corresponds to a lag of 500 msec. This is for snapping a rect
284 // to a single line of text.
286 // Snapping for example to a traced bitmap is also very stressing for
287 // the CPU, so we'll only snap to paths having no more than 500 nodes
288 // This also leads to a lag of approx. 500 msec (in my lousy test set-up).
289 bool very_complex_path = false;
290 if (SP_IS_PATH(root_item)) {
291 very_complex_path = sp_nodes_in_path(SP_PATH(root_item)) > 500;
292 }
294 if (!very_lenghty_prose && !very_complex_path) {
295 _paths_to_snap_to->push_back(Path_for_item(root_item, true, true));
296 }
297 }
298 }
300 //Add the item's bounding box to snap to
301 if (_snap_to_bboxpath) {
302 if (!(_strict_snapping && p_is_a_node)) {
303 //This will get ugly... rect -> curve -> bpath
304 NRRect rect;
305 sp_item_invoke_bbox(root_item, &rect, i2doc, TRUE, bbox_type);
306 NR::Maybe<NR::Rect> bbox = rect.upgrade();
307 SPCurve *curve = sp_curve_new_from_rect(bbox);
308 if (curve) {
309 NArtBpath *bpath = SP_CURVE_BPATH(curve);
310 if (bpath) {
311 Path *path = bpath_to_Path(bpath);
312 if (path) {
313 _paths_to_snap_to->push_back(path);
314 }
315 delete bpath;
316 }
317 delete curve;
318 }
319 }
320 }
321 }
322 }
324 //Now we can finally do the real snapping, using the paths collected above
325 for (std::list<Path*>::const_iterator k = _paths_to_snap_to->begin(); k != _paths_to_snap_to->end(); k++) {
326 if (*k) {
327 if (first_point) {
328 (*k)->ConvertWithBackData(0.01); //This is extremely time consuming!
329 }
331 /* Look for the nearest position on this SPItem to our snap point */
332 NR::Maybe<Path::cut_position> const o = get_nearest_position_on_Path(*k, p_doc);
333 if (o && o->t >= 0 && o->t <= 1) {
335 /* Convert the nearest point back to desktop coordinates */
336 NR::Point const o_it = get_point_on_Path(*k, o->piece, o->t);
337 NR::Point const o_dt = desktop->doc2dt(o_it);
339 NR::Coord const dist = NR::L2(o_dt - p);
340 if (dist < getDistance() && dist < s.getDistance()) {
341 s = SnappedPoint(o_dt, dist);
342 }
343 }
344 }
345 }
346 }
349 Inkscape::SnappedPoint Inkscape::ObjectSnapper::_doFreeSnap(Inkscape::Snapper::PointType const &t,
350 NR::Point const &p,
351 bool const &first_point,
352 std::vector<NR::Point> &points_to_snap,
353 std::list<SPItem const *> const &it) const
354 {
355 if ( NULL == _named_view ) {
356 return SnappedPoint(p, NR_HUGE);
357 }
359 /* Get a list of all the SPItems that we will try to snap to */
360 if (first_point) {
361 _findCandidates(sp_document_root(_named_view->document), it, first_point, points_to_snap, SNAP_XY);
362 }
364 SnappedPoint s(p, NR_HUGE);
366 if (_snap_to_itemnode || _snap_to_bboxnode) {
367 _snapNodes(t, s, p, first_point, SNAP_XY);
368 }
369 if (_snap_to_itempath || _snap_to_bboxpath) {
370 _snapPaths(t, s, p, first_point);
371 }
373 return s;
374 }
378 Inkscape::SnappedPoint Inkscape::ObjectSnapper::_doConstrainedSnap(Inkscape::Snapper::PointType const &t,
379 NR::Point const &p,
380 bool const &first_point,
381 std::vector<NR::Point> &points_to_snap,
382 ConstraintLine const &c,
383 std::list<SPItem const *> const &it) const
384 {
385 /* FIXME: this needs implementing properly; I think we have to do the
386 ** intersection of c with the objects.
387 */
388 return _doFreeSnap(t, p, first_point, points_to_snap, it);
389 }
393 Inkscape::SnappedPoint Inkscape::ObjectSnapper::guideSnap(NR::Point const &p,
394 DimensionToSnap const snap_dim) const
395 {
396 if ( NULL == _named_view ) {
397 return SnappedPoint(p, NR_HUGE);
398 }
400 /* Get a list of all the SPItems that we will try to snap to */
401 std::list<SPItem*> cand;
402 std::list<SPItem const *> const it; //just an empty list
404 std::vector<NR::Point> points_to_snap;
405 points_to_snap.push_back(p);
407 _findCandidates(sp_document_root(_named_view->document), it, true, points_to_snap, snap_dim);
409 SnappedPoint s(p, NR_HUGE);
410 _snapNodes(Inkscape::Snapper::SNAPPOINT_GUIDE, s, p, true, snap_dim);
412 return s;
413 }
415 /**
416 * \return true if this Snapper will snap at least one kind of point.
417 */
418 bool Inkscape::ObjectSnapper::ThisSnapperMightSnap() const
419 {
420 bool snap_to_something = _snap_to_itempath || _snap_to_itemnode || _snap_to_bboxpath || _snap_to_bboxnode;
421 return (_enabled && _snap_from != 0 && snap_to_something);
422 }
425 /*
426 Local Variables:
427 mode:c++
428 c-file-style:"stroustrup"
429 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
430 indent-tabs-mode:nil
431 fill-column:99
432 End:
433 */
434 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :