Code

- try to use more forward declarations for less dependencies on display/curve.h
[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  *   Diederik van Lierop <mail@diedenrezi.nl>
8  *
9  * Copyright (C) 2005 - 2008 Authors
10  *
11  * Released under GNU GPL, read the file 'COPYING' for more information
12  */
14 #include "libnr/n-art-bpath.h"
15 #include "libnr/nr-path.h"
16 #include "libnr/nr-rect-ops.h"
17 #include "libnr/nr-point-fns.h"
18 #include "libnr/n-art-bpath-2geom.h"
19 #include "2geom/path-intersection.h"
20 #include "document.h"
21 #include "sp-namedview.h"
22 #include "sp-image.h"
23 #include "sp-item-group.h"
24 #include "sp-item.h"
25 #include "sp-use.h"
26 #include "display/curve.h"
27 #include "desktop.h"
28 #include "inkscape.h"
29 #include "prefs-utils.h"
30 #include "sp-text.h"
31 #include "sp-flowtext.h"
32 #include "text-editing.h"
34 Inkscape::ObjectSnapper::ObjectSnapper(SPNamedView const *nv, NR::Coord const d)
35     : Snapper(nv, d), _snap_to_itemnode(true), _snap_to_itempath(true),
36       _snap_to_bboxnode(true), _snap_to_bboxpath(true), _snap_to_page_border(false),
37       _strict_snapping(true), _include_item_center(false)
38 {
39     _candidates = new std::vector<SPItem*>;
40     _points_to_snap_to = new std::vector<NR::Point>;
41     _bpaths_to_snap_to = new std::vector<NArtBpath*>;
42     _paths_to_snap_to = new std::vector<Path*>;
43 }
45 Inkscape::ObjectSnapper::~ObjectSnapper()
46 {
47     _candidates->clear(); //Don't delete the candidates themselves, as these are not ours!
48     delete _candidates;
50     _points_to_snap_to->clear();
51     delete _points_to_snap_to;
53     _clear_paths();
54     delete _paths_to_snap_to;
55     delete _bpaths_to_snap_to;
56 }
58 /**
59  *  Find all items within snapping range.
60  *  \param r Pointer to the current document
61  *  \param it List of items to ignore
62  *  \param first_point If true then this point is the first one from a whole bunch of points
63  *  \param bbox_to_snap Bounding box hulling the whole bunch of points, all from the same selection and having the same transformation
64  *  \param DimensionToSnap Snap in X, Y, or both directions.
65  */
67 void Inkscape::ObjectSnapper::_findCandidates(SPObject* r,
68                                               std::vector<SPItem const *> const *it,
69                                               bool const &first_point,
70                                               NR::Rect const &bbox_to_snap,
71                                               DimensionToSnap const snap_dim) const
72 {
73     bool const c1 = (snap_dim == TRANSL_SNAP_XY) && ThisSnapperMightSnap();
74     bool const c2 = (snap_dim != TRANSL_SNAP_XY) && GuidesMightSnap();
75     
76     if (!(c1 || c2)) {
77         return;        
78     }
79     
80     SPDesktop const *desktop = SP_ACTIVE_DESKTOP;
82     if (first_point) {
83         _candidates->clear();
84     }
85     
86     NR::Maybe<NR::Rect> bbox_of_item = NR::Rect(); // a default NR::Rect is infinitely large
87     NR::Rect bbox_to_snap_incl = bbox_to_snap; // _incl means: will include the snapper tolerance
88     bbox_to_snap_incl.growBy(getSnapperTolerance()); // see?
89     
90     for (SPObject* o = sp_object_first_child(r); o != NULL; o = SP_OBJECT_NEXT(o)) {
91         if (SP_IS_ITEM(o) && !SP_ITEM(o)->isLocked() && !desktop->itemIsHidden(SP_ITEM(o))) {
93             /* See if this item is on the ignore list */
94             std::vector<SPItem const *>::const_iterator i;
95             if (it != NULL) {
96                 i = it->begin();
97                 while (i != it->end() && *i != o) {
98                     i++;
99                 }
100             }
102             if (it == NULL || i == it->end()) {
103                 /* See if the item is within range */
104                 if (SP_IS_GROUP(o)) {
105                     _findCandidates(o, it, false, bbox_to_snap, snap_dim);
106                 } else {
107                     bbox_of_item = sp_item_bbox_desktop(SP_ITEM(o));
108                     if (bbox_of_item) {
109                         if (bbox_to_snap_incl.intersects(*bbox_of_item)) {
110                             //This item is within snapping range, so record it as a candidate
111                             _candidates->push_back(SP_ITEM(o));
112                         }
113                     }
114                 }
115             }
116         }
117     }
121 void Inkscape::ObjectSnapper::_collectNodes(Inkscape::Snapper::PointType const &t,
122                                          bool const &first_point) const
124     // Now, let's first collect all points to snap to. If we have a whole bunch of points to snap,
125     // e.g. when translating an item using the selector tool, then we will only do this for the
126     // first point and store the collection for later use. This significantly improves the performance
127     if (first_point) {
128         _points_to_snap_to->clear();
129         
130          // Determine the type of bounding box we should snap to
131         SPItem::BBoxType bbox_type = SPItem::GEOMETRIC_BBOX;
132         
133         bool p_is_a_node = t & Inkscape::Snapper::SNAPPOINT_NODE;
134         bool p_is_a_bbox = t & Inkscape::Snapper::SNAPPOINT_BBOX;
135         bool p_is_a_guide = t & Inkscape::Snapper::SNAPPOINT_GUIDE;
136         
137         // A point considered for snapping should be either a node, a bbox corner or a guide. Pick only ONE!
138         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));        
139         
140         if (_snap_to_bboxnode) {
141             int prefs_bbox = prefs_get_int_attribute("tools", "bounding_box", 0);
142             bbox_type = (prefs_bbox ==0)? 
143                 SPItem::APPROXIMATE_BBOX : SPItem::GEOMETRIC_BBOX;
144         }
146         for (std::vector<SPItem*>::const_iterator i = _candidates->begin(); i != _candidates->end(); i++) {
147             //NR::Matrix i2doc(NR::identity());
148             SPItem *root_item = *i;
149             if (SP_IS_USE(*i)) {
150                 root_item = sp_use_root(SP_USE(*i));
151             }
152             g_return_if_fail(root_item);
154             //Collect all nodes so we can snap to them
155             if (_snap_to_itemnode) {
156                 if (!(_strict_snapping && !p_is_a_node) || p_is_a_guide) {
157                     sp_item_snappoints(root_item, _include_item_center, SnapPointsIter(*_points_to_snap_to));
158                 }
159             }
161             //Collect the bounding box's corners so we can snap to them
162             if (_snap_to_bboxnode) {
163                 if (!(_strict_snapping && !p_is_a_bbox) || p_is_a_guide) {
164                     NR::Maybe<NR::Rect> b = sp_item_bbox_desktop(root_item, bbox_type);
165                     if (b) {
166                         for ( unsigned k = 0 ; k < 4 ; k++ ) {
167                             _points_to_snap_to->push_back(b->corner(k));
168                         }
169                     }
170                 }
171             }
172         }
173     }
176 void Inkscape::ObjectSnapper::_snapNodes(SnappedConstraints &sc,
177                                          Inkscape::Snapper::PointType const &t,
178                                          NR::Point const &p,
179                                          bool const &first_point,
180                                          std::vector<NR::Point> *unselected_nodes) const
182     // Iterate through all nodes, find out which one is the closest to p, and snap to it!
183     
184     _collectNodes(t, first_point);
185     
186     if (unselected_nodes != NULL) {
187         _points_to_snap_to->insert(_points_to_snap_to->end(), unselected_nodes->begin(), unselected_nodes->end());
188     }   
189     
190     SnappedPoint s;
191     bool success = false;
192     
193     for (std::vector<NR::Point>::const_iterator k = _points_to_snap_to->begin(); k != _points_to_snap_to->end(); k++) {
194         NR::Coord dist = NR::L2(*k - p);        
195         if (dist < getSnapperTolerance() && dist < s.getDistance()) {
196             s = SnappedPoint(*k, SNAPTARGET_NODE, dist, getSnapperTolerance(), getSnapperAlwaysSnap());
197             success = true;
198         }
199     }
201     if (success) {
202         sc.points.push_back(s); 
203     }
206 void Inkscape::ObjectSnapper::_snapTranslatingGuideToNodes(SnappedConstraints &sc,
207                                          Inkscape::Snapper::PointType const &t,
208                                          NR::Point const &p,
209                                          NR::Point const &guide_normal) const
211     // Iterate through all nodes, find out which one is the closest to this guide, and snap to it!
212     _collectNodes(t, true);
213     
214     SnappedPoint s;
215     bool success = false;
216     
217     NR::Coord tol = getSnapperTolerance();
218     
219     for (std::vector<NR::Point>::const_iterator k = _points_to_snap_to->begin(); k != _points_to_snap_to->end(); k++) {
220         // Project each node (*k) on the guide line (running through point p)
221         NR::Point p_proj = project_on_linesegment(*k, p, p + NR::rot90(guide_normal));
222         NR::Coord dist = NR::L2(*k - p_proj); // distance from node to the guide         
223         NR::Coord dist2 = NR::L2(p - p_proj); // distance from projection of node on the guide, to the mouse location
224         if ((dist < tol && dist2 < tol || getSnapperAlwaysSnap()) && dist < s.getDistance()) {
225             s = SnappedPoint(*k, SNAPTARGET_NODE, dist, tol, getSnapperAlwaysSnap());
226             success = true;
227         }
228     }
230     if (success) {
231         sc.points.push_back(s); 
232     }
235 /**
236  * Returns index of first NR_END bpath in array.
237  */
238 static unsigned sp_bpath_length(NArtBpath const bpath[])
240     g_return_val_if_fail(bpath != NULL, FALSE);
241     unsigned ret = 0;
242     while ( bpath[ret].code != NR_END ) {
243         ++ret;
244     }
245     ++ret;
246     return ret;
249 void Inkscape::ObjectSnapper::_collectPaths(Inkscape::Snapper::PointType const &t,
250                                          bool const &first_point,
251                                          NArtBpath const *border_bpath) const
253     // Now, let's first collect all paths to snap to. If we have a whole bunch of points to snap,
254     // e.g. when translating an item using the selector tool, then we will only do this for the
255     // first point and store the collection for later use. This significantly improves the performance
256     if (first_point) {
257         _clear_paths();
258         
259         // Determine the type of bounding box we should snap to
260         SPItem::BBoxType bbox_type = SPItem::GEOMETRIC_BBOX;
261         
262         bool p_is_a_node = t & Inkscape::Snapper::SNAPPOINT_NODE;
263         
264         if (_snap_to_bboxpath) {
265             int prefs_bbox = prefs_get_int_attribute("tools", "bounding_box", 0);
266             bbox_type = (prefs_bbox ==0)? 
267                 SPItem::APPROXIMATE_BBOX : SPItem::GEOMETRIC_BBOX;
268         }
269         
270         // Consider the page border for snapping
271         if (border_bpath != NULL) {
272             // make our own copy of the const*
273             NArtBpath *new_bpath;
274             unsigned const len = sp_bpath_length(border_bpath);
275             new_bpath = g_new(NArtBpath, len);
276             memcpy(new_bpath, border_bpath, len * sizeof(NArtBpath));
278             _bpaths_to_snap_to->push_back(new_bpath);    
279         }
280         
281         for (std::vector<SPItem*>::const_iterator i = _candidates->begin(); i != _candidates->end(); i++) {
283             /* Transform the requested snap point to this item's coordinates */
284             NR::Matrix i2doc(NR::identity());
285             SPItem *root_item = NULL;
286             /* We might have a clone at hand, so make sure we get the root item */
287             if (SP_IS_USE(*i)) {
288                 i2doc = sp_use_get_root_transform(SP_USE(*i));
289                 root_item = sp_use_root(SP_USE(*i));
290                 g_return_if_fail(root_item);
291             } else {
292                 i2doc = sp_item_i2doc_affine(*i);
293                 root_item = *i;
294             }
296             //Build a list of all paths considered for snapping to
298             //Add the item's path to snap to
299             if (_snap_to_itempath) {
300                 if (!(_strict_snapping && !p_is_a_node)) {
301                     // Snapping to the path of characters is very cool, but for a large
302                     // chunk of text this will take ages! So limit snapping to text paths
303                     // containing max. 240 characters. Snapping the bbox will not be affected
304                     bool very_lenghty_prose = false;
305                     if (SP_IS_TEXT(root_item) || SP_IS_FLOWTEXT(root_item)) {
306                         very_lenghty_prose =  sp_text_get_length(SP_TEXT(root_item)) > 240;
307                     }
308                     // On my AMD 3000+, the snapping lag becomes annoying at approx. 240 chars
309                     // which corresponds to a lag of 500 msec. This is for snapping a rect
310                     // to a single line of text.
312                     // Snapping for example to a traced bitmap is also very stressing for
313                     // the CPU, so we'll only snap to paths having no more than 500 nodes
314                     // This also leads to a lag of approx. 500 msec (in my lousy test set-up).
315                     bool very_complex_path = false;
316                     if (SP_IS_PATH(root_item)) {
317                         very_complex_path = sp_nodes_in_path(SP_PATH(root_item)) > 500;
318                     }
320                     if (!very_lenghty_prose && !very_complex_path) {
321                         SPCurve *curve = curve_for_item(root_item); 
322                         if (curve) {
323                             NArtBpath *bpath = bpath_for_curve(root_item, curve, true, true);
324                             _bpaths_to_snap_to->push_back(bpath);
325                             // Because we set doTransformation to true in bpath_for_curve, we
326                             // will get a dupe of the path, which must be freed at some point
327                             curve->unref();
328                         }
329                     }
330                 }
331             }
333             //Add the item's bounding box to snap to
334             if (_snap_to_bboxpath) {
335                 if (!(_strict_snapping && p_is_a_node)) {
336                     NRRect rect;
337                     sp_item_invoke_bbox(root_item, &rect, i2doc, TRUE, bbox_type);
338                     NArtBpath *bpath = nr_path_from_rect(rect);
339                     _bpaths_to_snap_to->push_back(bpath);
340                 }
341             }
342         }
343     }
345     
346 void Inkscape::ObjectSnapper::_snapPaths(SnappedConstraints &sc,
347                                      Inkscape::Snapper::PointType const &t,
348                                      NR::Point const &p,
349                                      bool const &first_point,
350                                      std::vector<NR::Point> *unselected_nodes,
351                                      SPPath const *selected_path,
352                                      NArtBpath const *border_bpath) const
354     _collectPaths(t, first_point, border_bpath);
355     // Now we can finally do the real snapping, using the paths collected above
356     SnappedPoint s;
357     bool success = false;
358     
359     /* FIXME: this seems like a hack.  Perhaps Snappers should be
360     ** in SPDesktop rather than SPNamedView?
361     */
362     SPDesktop const *desktop = SP_ACTIVE_DESKTOP;    
363     NR::Point const p_doc = desktop->dt2doc(p);    
364     
365     bool const node_tool_active = _snap_to_itempath && selected_path != NULL;
366     
367     if (first_point) {
368         /* While editing a path in the node tool, findCandidates must ignore that path because 
369          * of the node snapping requirements (i.e. only unselected nodes must be snapable).
370          * This path must not be ignored however when snapping to the paths, so we add it here
371          * manually when applicable. 
372          * 
373          * Note that this path must be the last in line!
374          * */        
375         if (node_tool_active) {        
376             SPCurve *curve = curve_for_item(SP_ITEM(selected_path)); 
377             if (curve) {
378                 NArtBpath *bpath = bpath_for_curve(SP_ITEM(selected_path), curve, true, true);
379                 _bpaths_to_snap_to->push_back(bpath);
380                 // Because we set doTransformation to true in bpath_for_curve, we
381                 // will get a dupe of the path, which must be freed at some point
382                 curve->unref();
383             }
384         }   
385         // Convert all bpaths to Paths, because here we really must have Paths
386         // (whereas in _snapPathsConstrained we will use the original bpaths)
387         for (std::vector<NArtBpath*>::const_iterator k = _bpaths_to_snap_to->begin(); k != _bpaths_to_snap_to->end(); k++) {
388             Path *path = bpath_to_Path(*k);
389             if (path) {
390                 path->ConvertWithBackData(0.01); //This is extremely time consuming!
391                 _paths_to_snap_to->push_back(path);
392             }    
393         }
394     }
395     
396     for (std::vector<Path*>::const_iterator k = _paths_to_snap_to->begin(); k != _paths_to_snap_to->end(); k++) {
397         if (*k) {
398             bool const being_edited = (node_tool_active && (*k) == _paths_to_snap_to->back());            
399             //if true then this path k is currently being edited in the node tool
400             
401             for (unsigned i = 1 ; i < (*k)->pts.size() ; i++) { 
402                 //pts describes a polyline approximation, which might consist of 1000s of points!
403                 NR::Point start_point;
404                 NR::Point end_point;   
405                 NR::Maybe<Path::cut_position> o = NR::Nothing();                 
406                 if (being_edited) { 
407                     /* If the path is being edited, then we will try to snap to each piece of the 
408                      * path individually. We should only snap though to stationary pieces of the paths
409                      * and not to the pieces that are being dragged around. This way we avoid 
410                      * self-snapping. For this we check whether the nodes at both ends of the current
411                      * piece are unselected; if they are then this piece must be stationary 
412                      */                    
413                     unsigned piece = (*k)->pts[i].piece; 
414                     // Example: a cubic spline is a single piece within a path; it will be drawn using
415                     // a polyline, which is described by the collection of lines between the points pts[i] 
416                     (*k)->PointAt(piece, 0, start_point);
417                     (*k)->PointAt(piece, 1, end_point);
418                     start_point = desktop->doc2dt(start_point);
419                     end_point = desktop->doc2dt(end_point);
420                     g_assert(unselected_nodes != NULL);
421                     bool c1 = isUnselectedNode(start_point, unselected_nodes);
422                     bool c2 = isUnselectedNode(end_point, unselected_nodes);     
423                     if (c1 && c2) {
424                         o = get_nearest_position_on_Path(*k, p_doc, i);
425                     }
426                 } else {
427                     /* If the path is NOT being edited, then we will try to snap to the path as a
428                      * whole, so we need to do this only once and we will break out at the end of
429                      * this for-loop iteration */                    
430                     /* Look for the nearest position on this SPItem to our snap point */
431                     o = get_nearest_position_on_Path(*k, p_doc);
432                     (*k)->PointAt(o->piece, 0, start_point);
433                     (*k)->PointAt(o->piece, 1, end_point);
434                     start_point = desktop->doc2dt(start_point);
435                     end_point = desktop->doc2dt(end_point);
436                 }
437                 
438                 if (o && o->t >= 0 && o->t <= 1) {    
439                     /* Convert the nearest point back to desktop coordinates */
440                     NR::Point const o_it = get_point_on_Path(*k, o->piece, o->t);
441                     
442                     /* IF YOU'RE BUG HUNTING: IT LOOKS LIKE get_point_on_Path SOMETIMES
443                      * RETURNS THE WRONG POINT (WITH AN OFFSET, BUT STILL ON THE PATH.
444                      * THIS BUG WILL NO LONGER BE ENCOUNTERED ONCE WE'VE SWITCHED TO 
445                      * 2GEOM FOR THE OBJECT SNAPPER
446                      */
447                      
448                     NR::Point const o_dt = desktop->doc2dt(o_it);                
449                     NR::Coord const dist = NR::L2(o_dt - p);
450     
451                     if (dist < getSnapperTolerance()) {
452                         // if we snap to a straight line segment (within a path), then return this line segment
453                         if ((*k)->IsLineSegment(o->piece)) {
454                             sc.lines.push_back(Inkscape::SnappedLineSegment(o_dt, dist, getSnapperTolerance(), getSnapperAlwaysSnap(), start_point, end_point));    
455                         } else {                
456                             // for segments other than straight lines of a path, we'll return just the closest snapped point
457                             if (dist < s.getDistance()) {
458                                 s = SnappedPoint(o_dt, SNAPTARGET_PATH, dist, getSnapperTolerance(), getSnapperAlwaysSnap());
459                                 success = true;
460                             }
461                         }
462                     }
463                 }        
464                 
465                 // If the path is NOT being edited, then we will try to snap to the path as a whole
466                 // so we need to do this only once
467                 if (!being_edited) break;
468             }
469         }
470     }
472     if (success) {
473         sc.points.push_back(s); 
474     }
477 /* Returns true if point is coincident with one of the unselected nodes */ 
478 bool Inkscape::ObjectSnapper::isUnselectedNode(NR::Point const &point, std::vector<NR::Point> const *unselected_nodes) const
480     if (unselected_nodes == NULL) {
481         return false;
482     }
483     
484     if (unselected_nodes->size() == 0) {
485         return false;
486     }
487     
488     for (std::vector<NR::Point>::const_iterator i = unselected_nodes->begin(); i != unselected_nodes->end(); i++) {
489         if (NR::L2(point - *i) < 1e-4) {
490             return true;
491         }   
492     }  
493     
494     return false;    
497 void Inkscape::ObjectSnapper::_snapPathsConstrained(SnappedConstraints &sc,
498                                      Inkscape::Snapper::PointType const &t,
499                                      NR::Point const &p,
500                                      bool const &first_point,
501                                      ConstraintLine const &c) const
503     
504     // Consider the page's border for snapping to
505     NArtBpath const *border_bpath = _snap_to_page_border ? _getBorderBPath() : NULL;
506     
507     _collectPaths(t, first_point, border_bpath);
508     
509     // Now we can finally do the real snapping, using the paths collected above
510     
511     /* FIXME: this seems like a hack.  Perhaps Snappers should be
512     ** in SPDesktop rather than SPNamedView?
513     */
514     SPDesktop const *desktop = SP_ACTIVE_DESKTOP;    
515     NR::Point const p_doc = desktop->dt2doc(p);    
516     
517     NR::Point direction_vector = c.getDirection();
518     if (!is_zero(direction_vector)) {
519         direction_vector = NR::unit_vector(direction_vector);
520     }
521     
522     NR::Point const p1_on_cl = c.hasPoint() ? c.getPoint() : p;
523     NR::Point const p2_on_cl = p1_on_cl + direction_vector;
524     
525     // The intersection point of the constraint line with any path, 
526     // must lie within two points on the constraintline: p_min_on_cl and p_max_on_cl
527     // The distance between those points is twice the snapping tolerance
528     NR::Point const p_proj_on_cl = project_on_linesegment(p, p1_on_cl, p2_on_cl);
529     NR::Point const p_min_on_cl = desktop->dt2doc(p_proj_on_cl - getSnapperTolerance() * direction_vector);    
530     NR::Point const p_max_on_cl = desktop->dt2doc(p_proj_on_cl + getSnapperTolerance() * direction_vector);
531     
532     Geom::Path cl;
533     std::vector<Geom::Path> clv;    
534     cl.start(p_min_on_cl.to_2geom());
535     cl.appendNew<Geom::LineSegment>(p_max_on_cl.to_2geom());
536     clv.push_back(cl);
537     
538     for (std::vector<NArtBpath*>::const_iterator k = _bpaths_to_snap_to->begin(); k != _bpaths_to_snap_to->end(); k++) {
539         if (*k) {                        
540             // convert a Path object (see src/livarot/Path.h) to a 2geom's path object (see 2geom/path.h)
541             // TODO: (Diederik) Only do this once for the first point, needs some storage of pointers in a member variable
542             std::vector<Geom::Path> path_2geom = BPath_to_2GeomPath(*k);  
543             
544             Geom::CrossingSet cs = Geom::crossings(clv, path_2geom);
545             if (cs.size() > 0) {
546                 // We need only the first element of cs, because cl is only a single straight linesegment
547                 // This first element contains a vector filled with crossings of cl with path_2geom
548                 for (std::vector<Geom::Crossing>::const_iterator m = cs[0].begin(); m != cs[0].end(); m++) {                    
549                     if ((*m).ta >= 0 && (*m).ta <= 1 ) {
550                         // Reconstruct the point of intersection
551                         NR::Point p_inters = p_min_on_cl + ((*m).ta) * (p_max_on_cl - p_min_on_cl);
552                         // When it's within snapping range, then return it
553                         // (within snapping range == between p_min_on_cl and p_max_on_cl == 0 < ta < 1)                        
554                         NR::Coord dist = NR::L2(desktop->dt2doc(p_proj_on_cl) - p_inters);
555                         SnappedPoint s(desktop->doc2dt(p_inters), SNAPTARGET_PATH, dist, getSnapperTolerance(), getSnapperAlwaysSnap());
556                         sc.points.push_back(s);    
557                     }  
558                 } 
559             }
560         }
561     }
565 void Inkscape::ObjectSnapper::freeSnap(SnappedConstraints &sc,
566                                             Inkscape::Snapper::PointType const &t,
567                                             NR::Point const &p,
568                                             bool const &first_point,
569                                             NR::Maybe<NR::Rect> const &bbox_to_snap,
570                                             std::vector<SPItem const *> const *it,
571                                             std::vector<NR::Point> *unselected_nodes) const
573     if (_snap_enabled == false || getSnapFrom(t) == false || _named_view == NULL) {
574         return;
575     }
577     /* Get a list of all the SPItems that we will try to snap to */
578     if (first_point) {
579         NR::Rect const local_bbox_to_snap = bbox_to_snap ? *bbox_to_snap : NR::Rect(p, p);
580         _findCandidates(sp_document_root(_named_view->document), it, first_point, local_bbox_to_snap, TRANSL_SNAP_XY);
581     }
582     
583     if (_snap_to_itemnode || _snap_to_bboxnode) {
584         _snapNodes(sc, t, p, first_point, unselected_nodes);
585     }
586     
587     // Consider the page's border for snapping to
588     NArtBpath const *border_bpath = _snap_to_page_border ? _getBorderBPath() : NULL;   
589     
590     if (_snap_to_itempath || _snap_to_bboxpath || _snap_to_page_border) {
591         unsigned n = (unselected_nodes == NULL) ? 0 : unselected_nodes->size();
592         if (n > 0) {
593             /* While editing a path in the node tool, findCandidates must ignore that path because 
594              * of the node snapping requirements (i.e. only unselected nodes must be snapable).
595              * That path must not be ignored however when snapping to the paths, so we add it here
596              * manually when applicable
597              */            
598             SPPath *path = NULL;
599             if (it != NULL) {
600                 g_assert(SP_IS_PATH(*it->begin()));
601                 g_assert(it->size() == 1);
602                 path = SP_PATH(*it->begin());
603             }
604             _snapPaths(sc, t, p, first_point, unselected_nodes, path, border_bpath);                
605         } else {
606             _snapPaths(sc, t, p, first_point, NULL, NULL, border_bpath);   
607         }        
608     }
611 void Inkscape::ObjectSnapper::constrainedSnap( SnappedConstraints &sc,
612                                                   Inkscape::Snapper::PointType const &t,
613                                                   NR::Point const &p,
614                                                   bool const &first_point,
615                                                   NR::Maybe<NR::Rect> const &bbox_to_snap,
616                                                   ConstraintLine const &c,
617                                                   std::vector<SPItem const *> const *it) const
619     if (_snap_enabled == false || getSnapFrom(t) == false || _named_view == NULL) {
620         return;
621     }
623     /* Get a list of all the SPItems that we will try to snap to */
624     if (first_point) {
625         NR::Rect const local_bbox_to_snap = bbox_to_snap ? *bbox_to_snap : NR::Rect(p, p);
626         _findCandidates(sp_document_root(_named_view->document), it, first_point, local_bbox_to_snap, TRANSL_SNAP_XY);
627     }
628     
629     // A constrained snap, is a snap in only one degree of freedom (specified by the constraint line).
630     // This is usefull for example when scaling an object while maintaining a fixed aspect ratio. It's
631     // nodes are only allowed to move in one direction (i.e. in one degree of freedom).
632     
633     // When snapping to objects, we either snap to their nodes or their paths. It is however very
634     // unlikely that any node will be exactly at the constrained line, so for a constrained snap
635     // to objects we will only consider the object's paths. Beside, the nodes will be at these paths,
636     // so we will more or less snap to them anyhow.   
638     if (_snap_to_itempath || _snap_to_bboxpath || _snap_to_page_border) {
639         _snapPathsConstrained(sc, t, p, first_point, c);
640     }
644 // This method is used to snap a guide to nodes, while dragging the guide around
645 void Inkscape::ObjectSnapper::guideSnap(SnappedConstraints &sc,
646                                                                                 NR::Point const &p,
647                                             NR::Point const &guide_normal) const
649     if ( NULL == _named_view ) {
650         return;
651     }
652     
653     /* Get a list of all the SPItems that we will try to snap to */
654     std::vector<SPItem*> cand;
655     std::vector<SPItem const *> const it; //just an empty list
656     
657     DimensionToSnap snap_dim;
658     if (guide_normal == component_vectors[NR::Y]) {
659         snap_dim = GUIDE_TRANSL_SNAP_Y;
660     } else if (guide_normal == component_vectors[NR::X]) {
661         snap_dim = GUIDE_TRANSL_SNAP_X;
662     } else {
663         snap_dim = ANGLED_GUIDE_TRANSL_SNAP;
664     }
665     
666     // We don't support ANGLED_GUIDE_ROT_SNAP yet. 
667     
668     // It would be cool to allow the user to rotate a guide by dragging it, instead of
669     // only translating it. (For example when CTRL is pressed). We will need an UI part 
670     // for that first; and some important usability choices need to be made: 
671     // E.g. which point should be used for pivoting? A previously snapped point,
672     // or a transformation center (which can be moved after clicking for the
673     // second time on an object; but should this point then be constrained to the
674     // line, or can it be located anywhere?)
675     
676     _findCandidates(sp_document_root(_named_view->document), &it, true, NR::Rect(p, p), snap_dim);
677         _snapTranslatingGuideToNodes(sc, Inkscape::Snapper::SNAPPOINT_GUIDE, p, guide_normal);
678     
679     // _snapRotatingGuideToNodes has not been implemented yet. 
682 /**
683  *  \return true if this Snapper will snap at least one kind of point.
684  */
685 bool Inkscape::ObjectSnapper::ThisSnapperMightSnap() const
687     bool snap_to_something = _snap_to_itempath || _snap_to_itemnode || _snap_to_bboxpath || _snap_to_bboxnode || _snap_to_page_border;
688     return (_snap_enabled && _snap_from != 0 && snap_to_something);
691 bool Inkscape::ObjectSnapper::GuidesMightSnap() const
693     bool snap_to_something = _snap_to_itemnode || _snap_to_bboxnode;
694     return (_snap_enabled && (_snap_from & SNAPPOINT_GUIDE) && snap_to_something);
697 void Inkscape::ObjectSnapper::_clear_paths() const 
699     for (std::vector<NArtBpath*>::const_iterator k = _bpaths_to_snap_to->begin(); k != _bpaths_to_snap_to->end(); k++) {
700         g_free(*k);
701     }
702     _bpaths_to_snap_to->clear();
703     
704     for (std::vector<Path*>::const_iterator k = _paths_to_snap_to->begin(); k != _paths_to_snap_to->end(); k++) {
705         delete *k;    
706     }
707     _paths_to_snap_to->clear();
710 NArtBpath const* Inkscape::ObjectSnapper::_getBorderBPath() const
712     NArtBpath const *border_bpath = NULL;
713     NR::Rect const border_rect = NR::Rect(NR::Point(0,0), NR::Point(sp_document_width(_named_view->document),sp_document_height(_named_view->document)));
714     SPCurve const *border_curve = SPCurve::new_from_rect(border_rect);
715     if (border_curve) {
716         border_bpath = SP_CURVE_BPATH(border_curve); 
717     }
718         
719     return border_bpath;
721 /*
722   Local Variables:
723   mode:c++
724   c-file-style:"stroustrup"
725   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
726   indent-tabs-mode:nil
727   fill-column:99
728   End:
729 */
730 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :