58ef71595a289e0a1ebc51c0a904799e28371a88
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::vector<SPItem*>;
35 _points_to_snap_to = new std::vector<NR::Point>;
36 _paths_to_snap_to = new std::vector<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::vector<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(SnappedConstraints &sc,
117 Inkscape::Snapper::PointType const &t,
118 NR::Point const &p,
119 bool const &first_point,
120 DimensionToSnap const snap_dim) const
121 {
122 bool success = false;
124 // Determine the type of bounding box we should snap to
125 SPItem::BBoxType bbox_type = SPItem::GEOMETRIC_BBOX;
126 if (_snap_to_bboxnode) {
127 gchar const *prefs_bbox = prefs_get_string_attribute("tools.select", "bounding_box");
128 bbox_type = (prefs_bbox != NULL && strcmp(prefs_bbox, "geometric")==0)? SPItem::GEOMETRIC_BBOX : SPItem::APPROXIMATE_BBOX;
129 }
131 bool p_is_a_node = t & Inkscape::Snapper::SNAPPOINT_NODE;
132 bool p_is_a_bbox = t & Inkscape::Snapper::SNAPPOINT_BBOX;
133 bool p_is_a_guide = t & Inkscape::Snapper::SNAPPOINT_GUIDE;
135 // A point considered for snapping should be either a node, a bbox corner or a guide. Pick only ONE!
136 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));
138 // Now, let's first collect all points to snap to. If we have a whole bunch of points to snap,
139 // e.g. when translating an item using the selector tool, then we will only do this for the
140 // first point and store the collection for later use. This dramatically improves the performance
141 if (first_point) {
142 _points_to_snap_to->clear();
143 for (std::vector<SPItem*>::const_iterator i = _candidates->begin(); i != _candidates->end(); i++) {
144 //NR::Matrix i2doc(NR::identity());
145 SPItem *root_item = *i;
146 if (SP_IS_USE(*i)) {
147 root_item = sp_use_root(SP_USE(*i));
148 }
150 //Collect all nodes so we can snap to them
151 if (_snap_to_itemnode) {
152 if (!(_strict_snapping && !p_is_a_node) || p_is_a_guide) {
153 sp_item_snappoints(root_item, _include_item_center, SnapPointsIter(*_points_to_snap_to));
154 }
155 }
157 //Collect the bounding box's corners so we can snap to them
158 if (_snap_to_bboxnode) {
159 if (!(_strict_snapping && !p_is_a_bbox) || p_is_a_guide) {
160 NR::Maybe<NR::Rect> b = sp_item_bbox_desktop(root_item, bbox_type);
161 if (b) {
162 for ( unsigned k = 0 ; k < 4 ; k++ ) {
163 _points_to_snap_to->push_back(b->corner(k));
164 }
165 }
166 }
167 }
168 }
169 }
171 //Do the snapping, using all the nodes and corners collected above
172 NR::Point snapped_point;
173 SnappedPoint s;
174 for (std::vector<NR::Point>::const_iterator k = _points_to_snap_to->begin(); k != _points_to_snap_to->end(); k++) {
175 /* Try to snap to this node of the path */
176 NR::Coord dist = NR_HUGE;
177 switch (snap_dim) {
178 case SNAP_X:
179 dist = fabs((*k)[NR::X] - p[NR::X]);
180 snapped_point = NR::Point((*k)[NR::X], p[NR::Y]);
181 break;
182 case SNAP_Y:
183 dist = fabs((*k)[NR::Y] - p[NR::Y]);
184 snapped_point = NR::Point(p[NR::X], (*k)[NR::Y]);
185 break;
186 case SNAP_XY:
187 dist = NR::L2(*k - p);
188 snapped_point = *k;
189 break;
190 }
192 if (dist < getDistance() && dist < s.getDistance()) {
193 s = SnappedPoint(snapped_point, dist);
194 success = true;
195 }
196 }
198 if (success) {
199 sc.points.push_back(s);
200 }
201 }
204 void Inkscape::ObjectSnapper::_snapPaths(SnappedConstraints &sc,
205 Inkscape::Snapper::PointType const &t,
206 NR::Point const &p,
207 bool const &first_point) const
208 {
209 bool success = false;
210 /* FIXME: this seems like a hack. Perhaps Snappers should be
211 ** in SPDesktop rather than SPNamedView?
212 */
213 SPDesktop const *desktop = SP_ACTIVE_DESKTOP;
215 NR::Point const p_doc = desktop->dt2doc(p);
217 // Determine the type of bounding box we should snap to
218 SPItem::BBoxType bbox_type = SPItem::GEOMETRIC_BBOX;
219 if (_snap_to_bboxpath) {
220 gchar const *prefs_bbox = prefs_get_string_attribute("tools.select", "bounding_box");
221 bbox_type = (prefs_bbox != NULL && strcmp(prefs_bbox, "geometric")==0)? SPItem::GEOMETRIC_BBOX : SPItem::APPROXIMATE_BBOX;
222 }
224 bool p_is_a_node = t & Inkscape::Snapper::SNAPPOINT_NODE;
226 // Now, let's first collect all paths to snap to. If we have a whole bunch of points to snap,
227 // e.g. when translating an item using the selector tool, then we will only do this for the
228 // first point and store the collection for later use. This dramatically improves the performance
229 if (first_point) {
230 for (std::vector<Path*>::const_iterator k = _paths_to_snap_to->begin(); k != _paths_to_snap_to->end(); k++) {
231 delete *k;
232 }
233 _paths_to_snap_to->clear();
234 for (std::vector<SPItem*>::const_iterator i = _candidates->begin(); i != _candidates->end(); i++) {
236 /* Transform the requested snap point to this item's coordinates */
237 NR::Matrix i2doc(NR::identity());
238 SPItem *root_item = NULL;
239 /* We might have a clone at hand, so make sure we get the root item */
240 if (SP_IS_USE(*i)) {
241 i2doc = sp_use_get_root_transform(SP_USE(*i));
242 root_item = sp_use_root(SP_USE(*i));
243 } else {
244 i2doc = sp_item_i2doc_affine(*i);
245 root_item = *i;
246 }
248 //Build a list of all paths considered for snapping to
250 //Add the item's path to snap to
251 if (_snap_to_itempath) {
252 if (!(_strict_snapping && !p_is_a_node)) {
253 // Snapping to the path of characters is very cool, but for a large
254 // chunk of text this will take ages! So limit snapping to text paths
255 // containing max. 240 characters. Snapping the bbox will not be affected
256 bool very_lenghty_prose = false;
257 if (SP_IS_TEXT(root_item) || SP_IS_FLOWTEXT(root_item)) {
258 very_lenghty_prose = sp_text_get_length(SP_TEXT(root_item)) > 240;
259 }
260 // On my AMD 3000+, the snapping lag becomes annoying at approx. 240 chars
261 // which corresponds to a lag of 500 msec. This is for snapping a rect
262 // to a single line of text.
264 // Snapping for example to a traced bitmap is also very stressing for
265 // the CPU, so we'll only snap to paths having no more than 500 nodes
266 // This also leads to a lag of approx. 500 msec (in my lousy test set-up).
267 bool very_complex_path = false;
268 if (SP_IS_PATH(root_item)) {
269 very_complex_path = sp_nodes_in_path(SP_PATH(root_item)) > 500;
270 }
272 if (!very_lenghty_prose && !very_complex_path) {
273 _paths_to_snap_to->push_back(Path_for_item(root_item, true, true));
274 }
275 }
276 }
278 //Add the item's bounding box to snap to
279 if (_snap_to_bboxpath) {
280 if (!(_strict_snapping && p_is_a_node)) {
281 //This will get ugly... rect -> curve -> bpath
282 NRRect rect;
283 sp_item_invoke_bbox(root_item, &rect, i2doc, TRUE, bbox_type);
284 NR::Maybe<NR::Rect> bbox = rect.upgrade();
285 SPCurve *curve = sp_curve_new_from_rect(bbox);
286 if (curve) {
287 NArtBpath *bpath = SP_CURVE_BPATH(curve);
288 if (bpath) {
289 Path *path = bpath_to_Path(bpath);
290 if (path) {
291 _paths_to_snap_to->push_back(path);
292 }
293 delete bpath;
294 }
295 delete curve;
296 }
297 }
298 }
299 }
300 }
302 //Now we can finally do the real snapping, using the paths collected above
303 SnappedPoint s;
304 for (std::vector<Path*>::const_iterator k = _paths_to_snap_to->begin(); k != _paths_to_snap_to->end(); k++) {
305 if (*k) {
306 if (first_point) {
307 (*k)->ConvertWithBackData(0.01); //This is extremely time consuming!
308 }
310 /* Look for the nearest position on this SPItem to our snap point */
311 NR::Maybe<Path::cut_position> const o = get_nearest_position_on_Path(*k, p_doc);
312 if (o && o->t >= 0 && o->t <= 1) {
314 /* Convert the nearest point back to desktop coordinates */
315 NR::Point const o_it = get_point_on_Path(*k, o->piece, o->t);
316 NR::Point const o_dt = desktop->doc2dt(o_it);
317 NR::Coord const dist = NR::L2(o_dt - p);
319 if (dist < getDistance()) {
320 // if we snap to a straight line segment (within a path), then return this line segment
321 if ((*k)->IsLineSegment(o->piece)) {
322 NR::Point start_point;
323 NR::Point end_point;
324 (*k)->PointAt(o->piece, 0, start_point);
325 (*k)->PointAt(o->piece, 1, end_point);
326 start_point = desktop->doc2dt(start_point);
327 end_point = desktop->doc2dt(end_point);
328 sc.lines.push_back(Inkscape::SnappedLineSegment(o_dt, dist, start_point, end_point));
329 } else {
330 // for segments other than straight lines of a path, we'll return just the closest snapped point
331 if (dist < s.getDistance()) {
332 s = SnappedPoint(o_dt, dist);
333 success = true;
334 }
335 }
336 }
337 }
338 }
339 }
341 if (success) {
342 sc.points.push_back(s);
343 }
344 }
347 void Inkscape::ObjectSnapper::_doFreeSnap(SnappedConstraints &sc,
348 Inkscape::Snapper::PointType const &t,
349 NR::Point const &p,
350 bool const &first_point,
351 std::vector<NR::Point> &points_to_snap,
352 std::list<SPItem const *> const &it) const
353 {
354 if ( NULL == _named_view ) {
355 return;
356 }
358 /* Get a list of all the SPItems that we will try to snap to */
359 if (first_point) {
360 _findCandidates(sp_document_root(_named_view->document), it, first_point, points_to_snap, SNAP_XY);
361 }
363 if (_snap_to_itemnode || _snap_to_bboxnode) {
364 _snapNodes(sc, t, p, first_point, SNAP_XY);
365 }
366 if (_snap_to_itempath || _snap_to_bboxpath) {
367 _snapPaths(sc, t, p, first_point);
368 }
369 }
373 void Inkscape::ObjectSnapper::_doConstrainedSnap( SnappedConstraints &sc,
374 Inkscape::Snapper::PointType const &t,
375 NR::Point const &p,
376 bool const &first_point,
377 std::vector<NR::Point> &points_to_snap,
378 ConstraintLine const &/*c*/,
379 std::list<SPItem const *> const &it) const
380 {
381 /* FIXME: this needs implementing properly; I think we have to do the
382 ** intersection of c with the objects.
383 */
384 _doFreeSnap(sc, t, p, first_point, points_to_snap, it);
385 }
389 void Inkscape::ObjectSnapper::guideSnap(SnappedConstraints &sc,
390 NR::Point const &p,
391 DimensionToSnap const snap_dim) const
392 {
393 if ( NULL == _named_view ) {
394 return;
395 }
397 /* Get a list of all the SPItems that we will try to snap to */
398 std::vector<SPItem*> cand;
399 std::list<SPItem const *> const it; //just an empty list
401 std::vector<NR::Point> points_to_snap;
402 points_to_snap.push_back(p);
404 _findCandidates(sp_document_root(_named_view->document), it, true, points_to_snap, snap_dim);
405 _snapNodes(sc, Inkscape::Snapper::SNAPPOINT_GUIDE, p, true, snap_dim);
406 }
408 /**
409 * \return true if this Snapper will snap at least one kind of point.
410 */
411 bool Inkscape::ObjectSnapper::ThisSnapperMightSnap() const
412 {
413 bool snap_to_something = _snap_to_itempath || _snap_to_itemnode || _snap_to_bboxpath || _snap_to_bboxnode;
414 return (_enabled && _snap_from != 0 && snap_to_something);
415 }
418 /*
419 Local Variables:
420 mode:c++
421 c-file-style:"stroustrup"
422 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
423 indent-tabs-mode:nil
424 fill-column:99
425 End:
426 */
427 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :