Code

struct SPCurve => class SPCurve
[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 void Inkscape::ObjectSnapper::_collectPaths(Inkscape::Snapper::PointType const &t,
236                                          bool const &first_point,
237                                          NArtBpath *border_bpath) const
239     // Now, let's first collect all paths to snap to. If we have a whole bunch of points to snap,
240     // e.g. when translating an item using the selector tool, then we will only do this for the
241     // first point and store the collection for later use. This significantly improves the performance
242     if (first_point) {
243         _clear_paths();
244         
245         // Determine the type of bounding box we should snap to
246         SPItem::BBoxType bbox_type = SPItem::GEOMETRIC_BBOX;
247         
248         bool p_is_a_node = t & Inkscape::Snapper::SNAPPOINT_NODE;
249         
250         if (_snap_to_bboxpath) {
251             int prefs_bbox = prefs_get_int_attribute("tools", "bounding_box", 0);
252             bbox_type = (prefs_bbox ==0)? 
253                 SPItem::APPROXIMATE_BBOX : SPItem::GEOMETRIC_BBOX;
254         }
255         
256         // Consider the page border for snapping
257         if (border_bpath != NULL) { 
258             _bpaths_to_snap_to->push_back(border_bpath);    
259         }
260         
261         for (std::vector<SPItem*>::const_iterator i = _candidates->begin(); i != _candidates->end(); i++) {
263             /* Transform the requested snap point to this item's coordinates */
264             NR::Matrix i2doc(NR::identity());
265             SPItem *root_item = NULL;
266             /* We might have a clone at hand, so make sure we get the root item */
267             if (SP_IS_USE(*i)) {
268                 i2doc = sp_use_get_root_transform(SP_USE(*i));
269                 root_item = sp_use_root(SP_USE(*i));
270                 g_return_if_fail(root_item);
271             } else {
272                 i2doc = sp_item_i2doc_affine(*i);
273                 root_item = *i;
274             }
276             //Build a list of all paths considered for snapping to
278             //Add the item's path to snap to
279             if (_snap_to_itempath) {
280                 if (!(_strict_snapping && !p_is_a_node)) {
281                     // Snapping to the path of characters is very cool, but for a large
282                     // chunk of text this will take ages! So limit snapping to text paths
283                     // containing max. 240 characters. Snapping the bbox will not be affected
284                     bool very_lenghty_prose = false;
285                     if (SP_IS_TEXT(root_item) || SP_IS_FLOWTEXT(root_item)) {
286                         very_lenghty_prose =  sp_text_get_length(SP_TEXT(root_item)) > 240;
287                     }
288                     // On my AMD 3000+, the snapping lag becomes annoying at approx. 240 chars
289                     // which corresponds to a lag of 500 msec. This is for snapping a rect
290                     // to a single line of text.
292                     // Snapping for example to a traced bitmap is also very stressing for
293                     // the CPU, so we'll only snap to paths having no more than 500 nodes
294                     // This also leads to a lag of approx. 500 msec (in my lousy test set-up).
295                     bool very_complex_path = false;
296                     if (SP_IS_PATH(root_item)) {
297                         very_complex_path = sp_nodes_in_path(SP_PATH(root_item)) > 500;
298                     }
300                     if (!very_lenghty_prose && !very_complex_path) {
301                         SPCurve *curve = curve_for_item(root_item); 
302                         if (curve) {
303                             NArtBpath *bpath = bpath_for_curve(root_item, curve, true, true);
304                             _bpaths_to_snap_to->push_back(bpath);
305                             // Because we set doTransformation to true in bpath_for_curve, we
306                             // will get a dupe of the path, which must be freed at some point
307                             curve->unref();
308                         }
309                     }
310                 }
311             }
313             //Add the item's bounding box to snap to
314             if (_snap_to_bboxpath) {
315                 if (!(_strict_snapping && p_is_a_node)) {
316                     NRRect rect;
317                     sp_item_invoke_bbox(root_item, &rect, i2doc, TRUE, bbox_type);
318                     NArtBpath *bpath = nr_path_from_rect(rect);
319                     _bpaths_to_snap_to->push_back(bpath);
320                 }
321             }
322         }
323     }
325     
326 void Inkscape::ObjectSnapper::_snapPaths(SnappedConstraints &sc,
327                                      Inkscape::Snapper::PointType const &t,
328                                      NR::Point const &p,
329                                      bool const &first_point,
330                                      std::vector<NR::Point> *unselected_nodes,
331                                      SPPath const *selected_path,
332                                      NArtBpath *border_bpath) const
334     _collectPaths(t, first_point, border_bpath);
335     // Now we can finally do the real snapping, using the paths collected above
336     SnappedPoint s;
337     bool success = false;
338     
339     /* FIXME: this seems like a hack.  Perhaps Snappers should be
340     ** in SPDesktop rather than SPNamedView?
341     */
342     SPDesktop const *desktop = SP_ACTIVE_DESKTOP;    
343     NR::Point const p_doc = desktop->dt2doc(p);    
344     
345     bool const node_tool_active = _snap_to_itempath && selected_path != NULL;
346     
347     if (first_point) {
348         /* While editing a path in the node tool, findCandidates must ignore that path because 
349          * of the node snapping requirements (i.e. only unselected nodes must be snapable).
350          * This path must not be ignored however when snapping to the paths, so we add it here
351          * manually when applicable. 
352          * 
353          * Note that this path must be the last in line!
354          * */        
355         if (node_tool_active) {        
356             SPCurve *curve = curve_for_item(SP_ITEM(selected_path)); 
357             if (curve) {
358                 NArtBpath *bpath = bpath_for_curve(SP_ITEM(selected_path), curve, true, true);
359                 _bpaths_to_snap_to->push_back(bpath);
360                 // Because we set doTransformation to true in bpath_for_curve, we
361                 // will get a dupe of the path, which must be freed at some point
362                 curve->unref();
363             }
364         }   
365         // Convert all bpaths to Paths, because here we really must have Paths
366         // (whereas in _snapPathsConstrained we will use the original bpaths)
367         for (std::vector<NArtBpath*>::const_iterator k = _bpaths_to_snap_to->begin(); k != _bpaths_to_snap_to->end(); k++) {
368             Path *path = bpath_to_Path(*k);
369             if (path) {
370                 path->ConvertWithBackData(0.01); //This is extremely time consuming!
371                 _paths_to_snap_to->push_back(path);
372             }    
373         }
374     }
375     
376     for (std::vector<Path*>::const_iterator k = _paths_to_snap_to->begin(); k != _paths_to_snap_to->end(); k++) {
377         if (*k) {
378             bool const being_edited = (node_tool_active && (*k) == _paths_to_snap_to->back());            
379             //if true then this path k is currently being edited in the node tool
380             
381             for (unsigned i = 1 ; i < (*k)->pts.size() ; i++) { 
382                 //pts describes a polyline approximation, which might consist of 1000s of points!
383                 NR::Point start_point;
384                 NR::Point end_point;   
385                 NR::Maybe<Path::cut_position> o = NR::Nothing();                 
386                 if (being_edited) { 
387                     /* If the path is being edited, then we will try to snap to each piece of the 
388                      * path individually. We should only snap though to stationary pieces of the paths
389                      * and not to the pieces that are being dragged around. This way we avoid 
390                      * self-snapping. For this we check whether the nodes at both ends of the current
391                      * piece are unselected; if they are then this piece must be stationary 
392                      */                    
393                     unsigned piece = (*k)->pts[i].piece; 
394                     // Example: a cubic spline is a single piece within a path; it will be drawn using
395                     // a polyline, which is described by the collection of lines between the points pts[i] 
396                     (*k)->PointAt(piece, 0, start_point);
397                     (*k)->PointAt(piece, 1, end_point);
398                     start_point = desktop->doc2dt(start_point);
399                     end_point = desktop->doc2dt(end_point);
400                     g_assert(unselected_nodes != NULL);
401                     bool c1 = isUnselectedNode(start_point, unselected_nodes);
402                     bool c2 = isUnselectedNode(end_point, unselected_nodes);     
403                     if (c1 && c2) {
404                         o = get_nearest_position_on_Path(*k, p_doc, i);
405                     }
406                 } else {
407                     /* If the path is NOT being edited, then we will try to snap to the path as a
408                      * whole, so we need to do this only once and we will break out at the end of
409                      * this for-loop iteration */                    
410                     /* Look for the nearest position on this SPItem to our snap point */
411                     o = get_nearest_position_on_Path(*k, p_doc);
412                     (*k)->PointAt(o->piece, 0, start_point);
413                     (*k)->PointAt(o->piece, 1, end_point);
414                     start_point = desktop->doc2dt(start_point);
415                     end_point = desktop->doc2dt(end_point);
416                 }
417                 
418                 if (o && o->t >= 0 && o->t <= 1) {    
419                     /* Convert the nearest point back to desktop coordinates */
420                     NR::Point const o_it = get_point_on_Path(*k, o->piece, o->t);
421                     
422                     /* IF YOU'RE BUG HUNTING: IT LOOKS LIKE get_point_on_Path SOMETIMES
423                      * RETURNS THE WRONG POINT (WITH AN OFFSET, BUT STILL ON THE PATH.
424                      * THIS BUG WILL NO LONGER BE ENCOUNTERED ONCE WE'VE SWITCHED TO 
425                      * 2GEOM FOR THE OBJECT SNAPPER
426                      */
427                      
428                     NR::Point const o_dt = desktop->doc2dt(o_it);                
429                     NR::Coord const dist = NR::L2(o_dt - p);
430     
431                     if (dist < getSnapperTolerance()) {
432                         // if we snap to a straight line segment (within a path), then return this line segment
433                         if ((*k)->IsLineSegment(o->piece)) {
434                             sc.lines.push_back(Inkscape::SnappedLineSegment(o_dt, dist, getSnapperTolerance(), getSnapperAlwaysSnap(), start_point, end_point));    
435                         } else {                
436                             // for segments other than straight lines of a path, we'll return just the closest snapped point
437                             if (dist < s.getDistance()) {
438                                 s = SnappedPoint(o_dt, SNAPTARGET_PATH, dist, getSnapperTolerance(), getSnapperAlwaysSnap());
439                                 success = true;
440                             }
441                         }
442                     }
443                 }        
444                 
445                 // If the path is NOT being edited, then we will try to snap to the path as a whole
446                 // so we need to do this only once
447                 if (!being_edited) break;
448             }
449         }
450     }
452     if (success) {
453         sc.points.push_back(s); 
454     }
457 /* Returns true if point is coincident with one of the unselected nodes */ 
458 bool Inkscape::ObjectSnapper::isUnselectedNode(NR::Point const &point, std::vector<NR::Point> const *unselected_nodes) const
460     if (unselected_nodes == NULL) {
461         return false;
462     }
463     
464     if (unselected_nodes->size() == 0) {
465         return false;
466     }
467     
468     for (std::vector<NR::Point>::const_iterator i = unselected_nodes->begin(); i != unselected_nodes->end(); i++) {
469         if (NR::L2(point - *i) < 1e-4) {
470             return true;
471         }   
472     }  
473     
474     return false;    
477 void Inkscape::ObjectSnapper::_snapPathsConstrained(SnappedConstraints &sc,
478                                      Inkscape::Snapper::PointType const &t,
479                                      NR::Point const &p,
480                                      bool const &first_point,
481                                      ConstraintLine const &c) const
483     
484     // Consider the page's border for snapping to
485     NArtBpath *border_bpath = _snap_to_page_border ? _getBorderBPath() : NULL;
486     
487     _collectPaths(t, first_point, border_bpath);
488     
489     // Now we can finally do the real snapping, using the paths collected above
490     
491     /* FIXME: this seems like a hack.  Perhaps Snappers should be
492     ** in SPDesktop rather than SPNamedView?
493     */
494     SPDesktop const *desktop = SP_ACTIVE_DESKTOP;    
495     NR::Point const p_doc = desktop->dt2doc(p);    
496     
497     NR::Point direction_vector = c.getDirection();
498     if (!is_zero(direction_vector)) {
499         direction_vector = NR::unit_vector(direction_vector);
500     }
501     
502     NR::Point const p1_on_cl = c.hasPoint() ? c.getPoint() : p;
503     NR::Point const p2_on_cl = p1_on_cl + direction_vector;
504     
505     // The intersection point of the constraint line with any path, 
506     // must lie within two points on the constraintline: p_min_on_cl and p_max_on_cl
507     // The distance between those points is twice the snapping tolerance
508     NR::Point const p_proj_on_cl = project_on_linesegment(p, p1_on_cl, p2_on_cl);
509     NR::Point const p_min_on_cl = desktop->dt2doc(p_proj_on_cl - getSnapperTolerance() * direction_vector);    
510     NR::Point const p_max_on_cl = desktop->dt2doc(p_proj_on_cl + getSnapperTolerance() * direction_vector);
511     
512     Geom::Path cl;
513     cl.start(p_min_on_cl.to_2geom());
514     cl.appendNew<Geom::LineSegment>(p_max_on_cl.to_2geom());
515     
516     for (std::vector<NArtBpath*>::const_iterator k = _bpaths_to_snap_to->begin(); k != _bpaths_to_snap_to->end(); k++) {
517         if (*k) {                        
518             // convert a Path object (see src/livarot/Path.h) to a 2geom's path object (see 2geom/path.h)
519             // TODO: (Diederik) Only do this once for the first point, needs some storage of pointers in a member variable
520             std::vector<Geom::Path> path_2geom = BPath_to_2GeomPath(*k);  
521             
522             for (std::vector<Geom::Path>::const_iterator l = path_2geom.begin(); l != path_2geom.end(); l++) {
523                 Geom::SimpleCrosser sxr;
524                 Geom::Crossings crossings = sxr.crossings(*l, cl);
525                 for (std::vector<Geom::Crossing>::const_iterator m = crossings.begin(); m != crossings.end(); m++) {
526                     // Reconstruct the point of intersection
527                     NR::Point p_inters = p_min_on_cl + ((*m).tb) * (p_max_on_cl - p_min_on_cl);
528                     // When it's within snapping range, then return it
529                     // (within snapping range == between p_min_on_cl and p_max_on_cl == 0 < tb < 1)
530                     if ((*m).tb >= 0 && (*m).tb <= 1 ) {
531                         NR::Coord dist = NR::L2(desktop->dt2doc(p_proj_on_cl) - p_inters);
532                         SnappedPoint s(desktop->doc2dt(p_inters), SNAPTARGET_PATH, dist, getSnapperTolerance(), getSnapperAlwaysSnap());
533                         sc.points.push_back(s);    
534                     }  
535                 } 
536             }
537         }
538     }
542 void Inkscape::ObjectSnapper::freeSnap(SnappedConstraints &sc,
543                                             Inkscape::Snapper::PointType const &t,
544                                             NR::Point const &p,
545                                             bool const &first_point,
546                                             NR::Maybe<NR::Rect> const &bbox_to_snap,
547                                             std::vector<SPItem const *> const *it,
548                                             std::vector<NR::Point> *unselected_nodes) const
550     if (_snap_enabled == false || getSnapFrom(t) == false || _named_view == NULL) {
551         return;
552     }
554     /* Get a list of all the SPItems that we will try to snap to */
555     if (first_point) {
556         NR::Rect const local_bbox_to_snap = bbox_to_snap ? *bbox_to_snap : NR::Rect(p, p);
557         _findCandidates(sp_document_root(_named_view->document), it, first_point, local_bbox_to_snap, TRANSL_SNAP_XY);
558     }
559     
560     if (_snap_to_itemnode || _snap_to_bboxnode) {
561         _snapNodes(sc, t, p, first_point, unselected_nodes);
562     }
563     
564     // Consider the page's border for snapping to
565     NArtBpath *border_bpath = _snap_to_page_border ? _getBorderBPath() : NULL;   
566     
567     if (_snap_to_itempath || _snap_to_bboxpath || _snap_to_page_border) {
568         unsigned n = (unselected_nodes == NULL) ? 0 : unselected_nodes->size();
569         if (n > 0) {
570             /* While editing a path in the node tool, findCandidates must ignore that path because 
571              * of the node snapping requirements (i.e. only unselected nodes must be snapable).
572              * That path must not be ignored however when snapping to the paths, so we add it here
573              * manually when applicable
574              */            
575             SPPath *path = NULL;
576             if (it != NULL) {
577                 g_assert(SP_IS_PATH(*it->begin()));
578                 g_assert(it->size() == 1);
579                 path = SP_PATH(*it->begin());
580             }
581             _snapPaths(sc, t, p, first_point, unselected_nodes, path, border_bpath);                
582         } else {
583             _snapPaths(sc, t, p, first_point, NULL, NULL, border_bpath);   
584         }        
585     }
588 void Inkscape::ObjectSnapper::constrainedSnap( SnappedConstraints &sc,
589                                                   Inkscape::Snapper::PointType const &t,
590                                                   NR::Point const &p,
591                                                   bool const &first_point,
592                                                   NR::Maybe<NR::Rect> const &bbox_to_snap,
593                                                   ConstraintLine const &c,
594                                                   std::vector<SPItem const *> const *it) const
596     if (_snap_enabled == false || getSnapFrom(t) == false || _named_view == NULL) {
597         return;
598     }
600     /* Get a list of all the SPItems that we will try to snap to */
601     if (first_point) {
602         NR::Rect const local_bbox_to_snap = bbox_to_snap ? *bbox_to_snap : NR::Rect(p, p);
603         _findCandidates(sp_document_root(_named_view->document), it, first_point, local_bbox_to_snap, TRANSL_SNAP_XY);
604     }
605     
606     // A constrained snap, is a snap in only one degree of freedom (specified by the constraint line).
607     // This is usefull for example when scaling an object while maintaining a fixed aspect ratio. It's
608     // nodes are only allowed to move in one direction (i.e. in one degree of freedom).
609     
610     // When snapping to objects, we either snap to their nodes or their paths. It is however very
611     // unlikely that any node will be exactly at the constrained line, so for a constrained snap
612     // to objects we will only consider the object's paths. Beside, the nodes will be at these paths,
613     // so we will more or less snap to them anyhow.   
615     if (_snap_to_itempath || _snap_to_bboxpath || _snap_to_page_border) {
616         _snapPathsConstrained(sc, t, p, first_point, c);
617     }
621 // This method is used to snap a guide to nodes, while dragging the guide around
622 void Inkscape::ObjectSnapper::guideSnap(SnappedConstraints &sc,
623                                                                                 NR::Point const &p,
624                                             NR::Point const &guide_normal) const
626     if ( NULL == _named_view ) {
627         return;
628     }
629     
630     /* Get a list of all the SPItems that we will try to snap to */
631     std::vector<SPItem*> cand;
632     std::vector<SPItem const *> const it; //just an empty list
633     
634     DimensionToSnap snap_dim;
635     if (guide_normal == component_vectors[NR::Y]) {
636         snap_dim = GUIDE_TRANSL_SNAP_Y;
637     } else if (guide_normal == component_vectors[NR::X]) {
638         snap_dim = GUIDE_TRANSL_SNAP_X;
639     } else {
640         snap_dim = ANGLED_GUIDE_TRANSL_SNAP;
641     }
642     
643     // We don't support ANGLED_GUIDE_ROT_SNAP yet. 
644     
645     // It would be cool to allow the user to rotate a guide by dragging it, instead of
646     // only translating it. (For example when CTRL is pressed). We will need an UI part 
647     // for that first; and some important usability choices need to be made: 
648     // E.g. which point should be used for pivoting? A previously snapped point,
649     // or a transformation center (which can be moved after clicking for the
650     // second time on an object; but should this point then be constrained to the
651     // line, or can it be located anywhere?)
652     
653     _findCandidates(sp_document_root(_named_view->document), &it, true, NR::Rect(p, p), snap_dim);
654         _snapTranslatingGuideToNodes(sc, Inkscape::Snapper::SNAPPOINT_GUIDE, p, guide_normal);
655     
656     // _snapRotatingGuideToNodes has not been implemented yet. 
659 /**
660  *  \return true if this Snapper will snap at least one kind of point.
661  */
662 bool Inkscape::ObjectSnapper::ThisSnapperMightSnap() const
664     bool snap_to_something = _snap_to_itempath || _snap_to_itemnode || _snap_to_bboxpath || _snap_to_bboxnode || _snap_to_page_border;
665     return (_snap_enabled && _snap_from != 0 && snap_to_something);
668 bool Inkscape::ObjectSnapper::GuidesMightSnap() const
670     bool snap_to_something = _snap_to_itemnode || _snap_to_bboxnode;
671     return (_snap_enabled && (_snap_from & SNAPPOINT_GUIDE) && snap_to_something);
674 void Inkscape::ObjectSnapper::_clear_paths() const 
676     for (std::vector<NArtBpath*>::const_iterator k = _bpaths_to_snap_to->begin(); k != _bpaths_to_snap_to->end(); k++) {
677         g_free(*k);
678     }
679     _bpaths_to_snap_to->clear();
680     
681     for (std::vector<Path*>::const_iterator k = _paths_to_snap_to->begin(); k != _paths_to_snap_to->end(); k++) {
682         delete *k;    
683     }
684     _paths_to_snap_to->clear();
687 NArtBpath* Inkscape::ObjectSnapper::_getBorderBPath() const
689     NArtBpath *border_bpath = NULL;
690     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)));
691     SPCurve const *border_curve = SPCurve::new_from_rect(border_rect);
692     if (border_curve) {
693         border_bpath = SP_CURVE_BPATH(border_curve); 
694     }
695         
696     return border_bpath;
698 /*
699   Local Variables:
700   mode:c++
701   c-file-style:"stroustrup"
702   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
703   indent-tabs-mode:nil
704   fill-column:99
705   End:
706 */
707 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :