Code

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