Code

Remove pointer to SPDesktop from ObjectSnapper
[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 "svg/svg.h"
15 #include "libnr/nr-path.h"
16 #include "libnr/nr-rect-ops.h"
17 #include "libnr/nr-point-fns.h"
18 #include <2geom/path-intersection.h>
19 #include <2geom/point.h>
20 #include <2geom/rect.h>
21 #include "document.h"
22 #include "sp-namedview.h"
23 #include "sp-image.h"
24 #include "sp-item-group.h"
25 #include "sp-item.h"
26 #include "sp-use.h"
27 #include "display/curve.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"
33 #include "sp-clippath.h"
34 #include "sp-mask.h"
35 #include "helper/geom-curves.h"
36 #include "desktop.h"
38 Inkscape::SnapCandidate::SnapCandidate(SPItem* item, bool clip_or_mask, Geom::Matrix additional_affine)
39     : item(item), clip_or_mask(clip_or_mask), additional_affine(additional_affine)
40 {    
41 }
43 Inkscape::SnapCandidate::~SnapCandidate()
44 {    
45 }
47 Inkscape::ObjectSnapper::ObjectSnapper(SPNamedView const *nv, Geom::Coord const d)
48     : Snapper(nv, d), _snap_to_itemnode(true), _snap_to_itempath(true),
49       _snap_to_bboxnode(true), _snap_to_bboxpath(true), _snap_to_page_border(false),
50       _strict_snapping(true), _include_item_center(false)
51 {
52     _candidates = new std::vector<SnapCandidate>;
53     _points_to_snap_to = new std::vector<Geom::Point>;
54     _paths_to_snap_to = new std::vector<Geom::PathVector*>;
55 }
57 Inkscape::ObjectSnapper::~ObjectSnapper()
58 {
59     _candidates->clear();
60     delete _candidates;
62     _points_to_snap_to->clear();
63     delete _points_to_snap_to;
65     _clear_paths();
66     delete _paths_to_snap_to;
67 }
69 /**
70  *  Find all items within snapping range.
71  *  \param parent Pointer to the document's root, or to a clipped path or mask object
72  *  \param it List of items to ignore
73  *  \param first_point If true then this point is the first one from a whole bunch of points
74  *  \param bbox_to_snap Bounding box hulling the whole bunch of points, all from the same selection and having the same transformation
75  *  \param DimensionToSnap Snap in X, Y, or both directions.
76  */
78 void Inkscape::ObjectSnapper::_findCandidates(SPObject* parent,
79                                               std::vector<SPItem const *> const *it,
80                                               bool const &first_point,
81                                               Geom::Rect const &bbox_to_snap,
82                                               DimensionToSnap const snap_dim,
83                                               bool const clip_or_mask,
84                                               Geom::Matrix const additional_affine) const // transformation of the item being clipped / masked
85 {
86     bool const c1 = (snap_dim == TRANSL_SNAP_XY) && ThisSnapperMightSnap();
87     bool const c2 = (snap_dim != TRANSL_SNAP_XY) && GuidesMightSnap();
88     
89     if (!(c1 || c2)) {
90         return;        
91     }
92     
93     if (first_point) {
94         _candidates->clear();
95     }
96     
97     Geom::Rect bbox_to_snap_incl = bbox_to_snap; // _incl means: will include the snapper tolerance
98     bbox_to_snap_incl.expandBy(getSnapperTolerance()); // see?
99     
100     for (SPObject* o = sp_object_first_child(parent); o != NULL; o = SP_OBJECT_NEXT(o)) {
101         g_assert(_named_view->snap_manager.getDesktop() != NULL);
102         if (SP_IS_ITEM(o) && !SP_ITEM(o)->isLocked() && !(_named_view->snap_manager.getDesktop()->itemIsHidden(SP_ITEM(o)) && !clip_or_mask)) {
103             // Don't snap to locked items, and
104             // don't snap to hidden objects, unless they're a clipped path or a mask
105             /* See if this item is on the ignore list */
106             std::vector<SPItem const *>::const_iterator i;
107             if (it != NULL) {
108                 i = it->begin();
109                 while (i != it->end() && *i != o) {
110                     i++;
111                 }
112             }
113             
114             if (it == NULL || i == it->end()) {
115                 SPItem *item = SP_ITEM(o); 
116                 Geom::Matrix transform = Geom::identity();
117                 if (item) {
118                     SPObject *obj = NULL;
119                     if (clip_or_mask) { // If the current item is a clipping path or a mask
120                         // then store the transformation of the clipped path or mask itself
121                         // but also take into account the additional affine of the object 
122                         // being clipped / masked
123                         transform = to_2geom(item->transform) * additional_affine;
124                     } else { // cannot clip or mask more than once                     
125                         // The current item is not a clipping path or a mask, but might
126                         // still be the subject of clipping or masking itself ; if so, then
127                         // we should also consider that path or mask for snapping to
128                         obj = SP_OBJECT(item->clip_ref->getObject());
129                         if (obj) {
130                             _findCandidates(obj, it, false, bbox_to_snap, snap_dim, true, item->transform);
131                         } 
132                         obj = SP_OBJECT(item->mask_ref->getObject());
133                         if (obj) {
134                             _findCandidates(obj, it, false, bbox_to_snap, snap_dim, true, item->transform);
135                         }
136                     }
137                 }            
138                 
139                 if (SP_IS_GROUP(o)) {
140                     _findCandidates(o, it, false, bbox_to_snap, snap_dim, false, Geom::identity());
141                 } else {
142                     boost::optional<NR::Rect> bbox_of_item = NR::Rect();
143                     if (clip_or_mask) {
144                         // Oh oh, this will get ugly. We cannot use sp_item_i2d_affine directly because we need to
145                         // insert an additional transformation in document coordinates (code copied from sp_item_i2d_affine)
146                         sp_item_invoke_bbox(item, 
147                             bbox_of_item,
148                             from_2geom(to_2geom(sp_item_i2doc_affine(item)) * matrix_to_desktop(additional_affine, item)),
149                             true);
150                     } else {
151                         sp_item_invoke_bbox(item, bbox_of_item, sp_item_i2d_affine(item), true);
152                     }
153                     if (bbox_of_item) {
154                         // See if the item is within range                                    
155                         if (bbox_to_snap_incl.intersects(to_2geom(*bbox_of_item))) {
156                             // This item is within snapping range, so record it as a candidate
157                             _candidates->push_back(SnapCandidate(item, clip_or_mask, additional_affine));
158                         }                        
159                     }
160                 }
161             }
162         }
163     }
167 void Inkscape::ObjectSnapper::_collectNodes(Inkscape::Snapper::PointType const &t,
168                                          bool const &first_point) const
170     // Now, let's first collect all points to snap to. If we have a whole bunch of points to snap,
171     // e.g. when translating an item using the selector tool, then we will only do this for the
172     // first point and store the collection for later use. This significantly improves the performance
173     if (first_point) {
174         _points_to_snap_to->clear();
175         
176          // Determine the type of bounding box we should snap to
177         SPItem::BBoxType bbox_type = SPItem::GEOMETRIC_BBOX;
178         
179         bool p_is_a_node = t & Inkscape::Snapper::SNAPPOINT_NODE;
180         bool p_is_a_bbox = t & Inkscape::Snapper::SNAPPOINT_BBOX;
181         bool p_is_a_guide = t & Inkscape::Snapper::SNAPPOINT_GUIDE;
182         
183         // A point considered for snapping should be either a node, a bbox corner or a guide. Pick only ONE!
184         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));        
185         
186         if (_snap_to_bboxnode) {
187             int prefs_bbox = prefs_get_int_attribute("tools", "bounding_box", 0);
188             bbox_type = (prefs_bbox == 0)? 
189                 SPItem::APPROXIMATE_BBOX : SPItem::GEOMETRIC_BBOX;
190         }
191         
192         // Consider the page border for snapping
193         if (_snap_to_page_border) {
194             _getBorderNodes(_points_to_snap_to);            
195         }
197         for (std::vector<SnapCandidate>::const_iterator i = _candidates->begin(); i != _candidates->end(); i++) {
198             //Geom::Matrix i2doc(Geom::identity());
199             SPItem *root_item = (*i).item;
200             if (SP_IS_USE((*i).item)) {
201                 root_item = sp_use_root(SP_USE((*i).item));
202             }
203             g_return_if_fail(root_item);
205             //Collect all nodes so we can snap to them
206             if (_snap_to_itemnode) {
207                 if (!(_strict_snapping && !p_is_a_node) || p_is_a_guide) {
208                     std::vector<NR::Point> dummy_vctr;
209                     sp_item_snappoints(root_item, _include_item_center, SnapPointsIter(dummy_vctr));
210                     to_2geom(dummy_vctr, *_points_to_snap_to);
211                 }
212             }
214             //Collect the bounding box's corners so we can snap to them
215             if (_snap_to_bboxnode) {
216                 if (!(_strict_snapping && !p_is_a_bbox) || p_is_a_guide) {
217                     // Discard the bbox of a clipped path / mask, because we don't want to snap to both the bbox
218                     // of the item AND the bbox of the clipping path at the same time
219                     if (!(*i).clip_or_mask) {  
220                         boost::optional<NR::Rect> b = sp_item_bbox_desktop(root_item, bbox_type);
221                         if (b) {
222                             for ( unsigned k = 0 ; k < 4 ; k++ ) {
223                                 _points_to_snap_to->push_back(to_2geom(b->corner(k)));
224                             }
225                         }
226                     }
227                 }
228             }
229         }
230     }
233 void Inkscape::ObjectSnapper::_snapNodes(SnappedConstraints &sc,
234                                          Inkscape::Snapper::PointType const &t,
235                                          Geom::Point const &p,
236                                          bool const &first_point,
237                                          std::vector<Geom::Point> *unselected_nodes) const
239     // Iterate through all nodes, find out which one is the closest to p, and snap to it!
240     
241     _collectNodes(t, first_point);
242     
243     if (unselected_nodes != NULL) {
244         _points_to_snap_to->insert(_points_to_snap_to->end(), unselected_nodes->begin(), unselected_nodes->end());
245     }   
246     
247     SnappedPoint s;
248     bool success = false;
249     
250     for (std::vector<Geom::Point>::const_iterator k = _points_to_snap_to->begin(); k != _points_to_snap_to->end(); k++) {
251         Geom::Coord dist = Geom::L2(*k - p);        
252         if (dist < getSnapperTolerance() && dist < s.getDistance()) {
253             s = SnappedPoint(*k, SNAPTARGET_NODE, dist, getSnapperTolerance(), getSnapperAlwaysSnap());
254             success = true;
255         }
256     }
258     if (success) {
259         sc.points.push_back(s);    
260     }
263 void Inkscape::ObjectSnapper::_snapTranslatingGuideToNodes(SnappedConstraints &sc,
264                                          Inkscape::Snapper::PointType const &t,
265                                          Geom::Point const &p,
266                                          Geom::Point const &guide_normal) const
268     // Iterate through all nodes, find out which one is the closest to this guide, and snap to it!
269     _collectNodes(t, true);
270     
271     SnappedPoint s;
272     bool success = false;
273     
274     Geom::Coord tol = getSnapperTolerance();
275     
276     for (std::vector<Geom::Point>::const_iterator k = _points_to_snap_to->begin(); k != _points_to_snap_to->end(); k++) {
277         // Project each node (*k) on the guide line (running through point p)
278         Geom::Point p_proj = project_on_linesegment(*k, p, p + Geom::rot90(guide_normal));
279         Geom::Coord dist = Geom::L2(*k - p_proj); // distance from node to the guide         
280         Geom::Coord dist2 = Geom::L2(p - p_proj); // distance from projection of node on the guide, to the mouse location
281         if ((dist < tol && dist2 < tol || getSnapperAlwaysSnap()) && dist < s.getDistance()) {
282             s = SnappedPoint(*k, SNAPTARGET_NODE, dist, tol, getSnapperAlwaysSnap());
283             success = true;
284         }
285     }
287     if (success) {
288         sc.points.push_back(s); 
289     }
293 /**
294  * Returns index of first NR_END bpath in array.
295  */
297 /* Obsolete
298 static unsigned sp_bpath_length(NArtBpath const bpath[])
300     g_return_val_if_fail(bpath != NULL, FALSE);
301     unsigned ret = 0;
302     while ( bpath[ret].code != NR_END ) {
303         ++ret;
304     }
305     ++ret;
306     return ret;
307 }*/
309 void Inkscape::ObjectSnapper::_collectPaths(Inkscape::Snapper::PointType const &t,
310                                          bool const &first_point) const
312     // Now, let's first collect all paths to snap to. If we have a whole bunch of points to snap,
313     // e.g. when translating an item using the selector tool, then we will only do this for the
314     // first point and store the collection for later use. This significantly improves the performance
315     if (first_point) {
316         _clear_paths();
317         
318         // Determine the type of bounding box we should snap to
319         SPItem::BBoxType bbox_type = SPItem::GEOMETRIC_BBOX;
320         
321         bool p_is_a_node = t & Inkscape::Snapper::SNAPPOINT_NODE;
322         
323         if (_snap_to_bboxpath) {
324             int prefs_bbox = prefs_get_int_attribute("tools", "bounding_box", 0);
325             bbox_type = (prefs_bbox ==0)? 
326                 SPItem::APPROXIMATE_BBOX : SPItem::GEOMETRIC_BBOX;
327         }
328         
329         // Consider the page border for snapping
330         if (_snap_to_page_border) {
331             Geom::PathVector *border_path = _getBorderPathv();
332             if (border_path != NULL) {
333                 _paths_to_snap_to->push_back(border_path);
334             }
335         }
336         
337         for (std::vector<SnapCandidate>::const_iterator i = _candidates->begin(); i != _candidates->end(); i++) {
339             /* Transform the requested snap point to this item's coordinates */
340             Geom::Matrix i2doc(Geom::identity());
341             SPItem *root_item = NULL;
342             /* We might have a clone at hand, so make sure we get the root item */
343             if (SP_IS_USE((*i).item)) {
344                 i2doc = sp_use_get_root_transform(SP_USE((*i).item));
345                 root_item = sp_use_root(SP_USE((*i).item));
346                 g_return_if_fail(root_item);
347             } else {
348                 i2doc = sp_item_i2doc_affine((*i).item);
349                 root_item = (*i).item;
350             }
351             
352             //Build a list of all paths considered for snapping to
354             //Add the item's path to snap to
355             if (_snap_to_itempath) {
356                 if (!(_strict_snapping && !p_is_a_node)) {
357                     // Snapping to the path of characters is very cool, but for a large
358                     // chunk of text this will take ages! So limit snapping to text paths
359                     // containing max. 240 characters. Snapping the bbox will not be affected
360                     bool very_lenghty_prose = false;
361                     if (SP_IS_TEXT(root_item) || SP_IS_FLOWTEXT(root_item)) {
362                         very_lenghty_prose =  sp_text_get_length(SP_TEXT(root_item)) > 240;
363                     }
364                     // On my AMD 3000+, the snapping lag becomes annoying at approx. 240 chars
365                     // which corresponds to a lag of 500 msec. This is for snapping a rect
366                     // to a single line of text.
368                     // Snapping for example to a traced bitmap is also very stressing for
369                     // the CPU, so we'll only snap to paths having no more than 500 nodes
370                     // This also leads to a lag of approx. 500 msec (in my lousy test set-up).
371                     bool very_complex_path = false;
372                     if (SP_IS_PATH(root_item)) {
373                         very_complex_path = sp_nodes_in_path(SP_PATH(root_item)) > 500;
374                     }
376                     if (!very_lenghty_prose && !very_complex_path) {
377                         SPCurve *curve = curve_for_item(root_item); 
378                         if (curve) {
379                             // We will get our own copy of the path, which must be freed at some point
380                             Geom::PathVector *borderpathv = pathvector_for_curve(root_item, curve, true, true, Geom::identity(), (*i).additional_affine); 
381                             _paths_to_snap_to->push_back(borderpathv); // Perhaps for speed, get a reference to the Geom::pathvector, and store the transformation besides it.
382                             curve->unref();
383                         }
384                     }
385                 }
386             }
388             //Add the item's bounding box to snap to
389             if (_snap_to_bboxpath) {
390                 if (!(_strict_snapping && p_is_a_node)) {
391                     // Discard the bbox of a clipped path / mask, because we don't want to snap to both the bbox
392                     // of the item AND the bbox of the clipping path at the same time
393                     if (!(*i).clip_or_mask) { 
394                         NRRect rect;
395                         sp_item_invoke_bbox(root_item, &rect, i2doc, TRUE, bbox_type);
396                         Geom::Rect const rect2 = to_2geom(*rect.upgrade());
397                         Geom::PathVector *path = _getPathvFromRect(rect2);
398                         _paths_to_snap_to->push_back(path);                        
399                     }
400                 }
401             }
402         }
403     }
405     
406 void Inkscape::ObjectSnapper::_snapPaths(SnappedConstraints &sc,
407                                      Inkscape::Snapper::PointType const &t,
408                                      Geom::Point const &p,
409                                      bool const &first_point,
410                                      std::vector<Geom::Point> *unselected_nodes,
411                                      SPPath const *selected_path) const
413     _collectPaths(t, first_point);
414     // Now we can finally do the real snapping, using the paths collected above
415     
416     g_assert(_named_view->snap_manager.getDesktop() != NULL);    
417     Geom::Point const p_doc = _named_view->snap_manager.getDesktop()->dt2doc(p);
418     
419     bool const node_tool_active = _snap_to_itempath && selected_path != NULL;
420     
421     if (first_point) {
422         /* While editing a path in the node tool, findCandidates must ignore that path because 
423          * of the node snapping requirements (i.e. only unselected nodes must be snapable).
424          * This path must not be ignored however when snapping to the paths, so we add it here
425          * manually when applicable. 
426          * 
427          * Note that this path must be the last in line!
428          * */        
429         if (node_tool_active) {        
430             SPCurve *curve = curve_for_item(SP_ITEM(selected_path)); 
431             if (curve) {
432                 Geom::PathVector *pathv = pathvector_for_curve(SP_ITEM(selected_path), curve, true, true, Geom::identity(), Geom::identity()); // We will get our own copy of the path, which must be freed at some point
433                 _paths_to_snap_to->push_back(pathv);
434                 curve->unref();
435             }
436         }
437     }
438     
439     for (std::vector<Geom::PathVector*>::const_iterator it_p = _paths_to_snap_to->begin(); it_p != _paths_to_snap_to->end(); it_p++) {
440         bool const being_edited = (node_tool_active && (*it_p) == _paths_to_snap_to->back());            
441         //if true then this pathvector it_pv is currently being edited in the node tool
442         
443         // char * svgd = sp_svg_write_path(**it_p);
444         // std::cout << "Dumping the pathvector: " << svgd << std::endl;        
445         
446         for(Geom::PathVector::iterator it_pv = (*it_p)->begin(); it_pv != (*it_p)->end(); ++it_pv) {
447             // Find a nearest point for each curve within this path
448             // n curves will return n time values with 0 <= t <= 1
449             std::vector<double> anp = (*it_pv).nearestPointPerCurve(p_doc);
450             
451             std::vector<double>::const_iterator np = anp.begin();
452             unsigned int index = 0;
453             for (; np != anp.end(); np++, index++) {
454                 Geom::Curve const *curve = &((*it_pv).at_index(index));
455                 Geom::Point const sp_doc = curve->pointAt(*np);
456                 
457                 bool c1 = true;
458                 bool c2 = true;                                
459                 if (being_edited) {
460                     /* If the path is being edited, then we should only snap though to stationary pieces of the path
461                      * and not to the pieces that are being dragged around. This way we avoid 
462                      * self-snapping. For this we check whether the nodes at both ends of the current
463                      * piece are unselected; if they are then this piece must be stationary 
464                      */                    
465                     g_assert(unselected_nodes != NULL);
466                     Geom::Point start_pt = _named_view->snap_manager.getDesktop()->doc2dt(curve->pointAt(0)); 
467                     Geom::Point end_pt = _named_view->snap_manager.getDesktop()->doc2dt(curve->pointAt(1));                                    
468                     c1 = isUnselectedNode(start_pt, unselected_nodes);
469                     c2 = isUnselectedNode(end_pt, unselected_nodes);
470                 }
471                 
472                 Geom::Point const sp_dt = _named_view->snap_manager.getDesktop()->doc2dt(sp_doc);                
473                 if (!being_edited || (c1 && c2)) {
474                     Geom::Coord const dist = Geom::distance(sp_doc, p_doc);
475                     if (dist < getSnapperTolerance()) {
476                         sc.curves.push_back(Inkscape::SnappedCurve(from_2geom(sp_dt), dist, getSnapperTolerance(), getSnapperAlwaysSnap(), curve));   
477                     }
478                 }
479             }        
480         } // End of: for (Geom::PathVector::iterator ....)        
481     }    
484 /* Returns true if point is coincident with one of the unselected nodes */ 
485 bool Inkscape::ObjectSnapper::isUnselectedNode(Geom::Point const &point, std::vector<Geom::Point> const *unselected_nodes) const
487     if (unselected_nodes == NULL) {
488         return false;
489     }
490     
491     if (unselected_nodes->size() == 0) {
492         return false;
493     }
494     
495     for (std::vector<Geom::Point>::const_iterator i = unselected_nodes->begin(); i != unselected_nodes->end(); i++) {
496         if (Geom::L2(point - *i) < 1e-4) {
497             return true;
498         }   
499     }  
500     
501     return false;    
504 void Inkscape::ObjectSnapper::_snapPathsConstrained(SnappedConstraints &sc,
505                                      Inkscape::Snapper::PointType const &t,
506                                      Geom::Point const &p,
507                                      bool const &first_point,
508                                      ConstraintLine const &c) const
510     
511     _collectPaths(t, first_point);
512     
513     // Now we can finally do the real snapping, using the paths collected above
514     
515     g_assert(_named_view->snap_manager.getDesktop() != NULL);
516     Geom::Point const p_doc = _named_view->snap_manager.getDesktop()->dt2doc(p);    
517     
518     Geom::Point direction_vector = c.getDirection();
519     if (!is_zero(direction_vector)) {
520         direction_vector = Geom::unit_vector(direction_vector);
521     }
522     
523     Geom::Point const p1_on_cl = c.hasPoint() ? c.getPoint() : p;
524     Geom::Point const p2_on_cl = p1_on_cl + direction_vector;
525     
526     // The intersection point of the constraint line with any path, 
527     // must lie within two points on the constraintline: p_min_on_cl and p_max_on_cl
528     // The distance between those points is twice the snapping tolerance
529     Geom::Point const p_proj_on_cl = project_on_linesegment(p, p1_on_cl, p2_on_cl);
530     Geom::Point const p_min_on_cl = _named_view->snap_manager.getDesktop()->dt2doc(p_proj_on_cl - getSnapperTolerance() * direction_vector);    
531     Geom::Point const p_max_on_cl = _named_view->snap_manager.getDesktop()->dt2doc(p_proj_on_cl + getSnapperTolerance() * direction_vector);
532     
533     Geom::Path cl;
534     std::vector<Geom::Path> clv;    
535     cl.start(p_min_on_cl);
536     cl.appendNew<Geom::LineSegment>(p_max_on_cl);
537     clv.push_back(cl);
538     
539     for (std::vector<Geom::PathVector*>::const_iterator k = _paths_to_snap_to->begin(); k != _paths_to_snap_to->end(); k++) {
540         if (*k) {                        
541             Geom::CrossingSet cs = Geom::crossings(clv, *(*k));
542             if (cs.size() > 0) {
543                 // We need only the first element of cs, because cl is only a single straight linesegment
544                 // This first element contains a vector filled with crossings of cl with *k
545                 for (std::vector<Geom::Crossing>::const_iterator m = cs[0].begin(); m != cs[0].end(); m++) {                    
546                     if ((*m).ta >= 0 && (*m).ta <= 1 ) {
547                         // Reconstruct the point of intersection
548                         Geom::Point p_inters = p_min_on_cl + ((*m).ta) * (p_max_on_cl - p_min_on_cl);
549                         // When it's within snapping range, then return it
550                         // (within snapping range == between p_min_on_cl and p_max_on_cl == 0 < ta < 1)                        
551                         Geom::Coord dist = Geom::L2(_named_view->snap_manager.getDesktop()->dt2doc(p_proj_on_cl) - p_inters);
552                         SnappedPoint s(_named_view->snap_manager.getDesktop()->doc2dt(p_inters), SNAPTARGET_PATH, dist, getSnapperTolerance(), getSnapperAlwaysSnap());
553                         sc.points.push_back(s);
554                     }  
555                 } 
556             }
557         }
558     }
562 void Inkscape::ObjectSnapper::freeSnap(SnappedConstraints &sc,
563                                             Inkscape::Snapper::PointType const &t,
564                                             Geom::Point const &p,
565                                             bool const &first_point,
566                                             boost::optional<Geom::Rect> const &bbox_to_snap,
567                                             std::vector<SPItem const *> const *it,
568                                             std::vector<Geom::Point> *unselected_nodes) const
570     if (_snap_enabled == false || getSnapFrom(t) == false || _named_view == NULL) {
571         return;
572     }
574     /* Get a list of all the SPItems that we will try to snap to */
575     if (first_point) {
576         Geom::Rect const local_bbox_to_snap = bbox_to_snap ? *bbox_to_snap : Geom::Rect(p, p);
577         _findCandidates(sp_document_root(_named_view->document), it, first_point, local_bbox_to_snap, TRANSL_SNAP_XY, false, Geom::identity());
578     }
579     
580     if (_snap_to_itemnode || _snap_to_bboxnode || _snap_to_page_border) {
581         _snapNodes(sc, t, p, first_point, unselected_nodes);
582     }
583     
584     if (_snap_to_itempath || _snap_to_bboxpath || _snap_to_page_border) {
585         unsigned n = (unselected_nodes == NULL) ? 0 : unselected_nodes->size();
586         if (n > 0) {
587             /* While editing a path in the node tool, findCandidates must ignore that path because 
588              * of the node snapping requirements (i.e. only unselected nodes must be snapable).
589              * That path must not be ignored however when snapping to the paths, so we add it here
590              * manually when applicable
591              */            
592             SPPath *path = NULL;
593             if (it != NULL) {
594                 g_assert(SP_IS_PATH(*it->begin()));
595                 g_assert(it->size() == 1);
596                 path = SP_PATH(*it->begin());
597             }
598             _snapPaths(sc, t, p, first_point, unselected_nodes, path);                
599         } else {
600             _snapPaths(sc, t, p, first_point, NULL, NULL);   
601         }        
602     }
605 void Inkscape::ObjectSnapper::constrainedSnap( SnappedConstraints &sc,
606                                                   Inkscape::Snapper::PointType const &t,
607                                                   Geom::Point const &p,
608                                                   bool const &first_point,
609                                                   boost::optional<Geom::Rect> const &bbox_to_snap,
610                                                   ConstraintLine const &c,
611                                                   std::vector<SPItem const *> const *it) const
613     if (_snap_enabled == false || getSnapFrom(t) == false || _named_view == NULL) {
614         return;
615     }
617     /* Get a list of all the SPItems that we will try to snap to */
618     if (first_point) {
619         Geom::Rect const local_bbox_to_snap = bbox_to_snap ? *bbox_to_snap : Geom::Rect(p, p);
620         _findCandidates(sp_document_root(_named_view->document), it, first_point, local_bbox_to_snap, TRANSL_SNAP_XY, false, Geom::identity());
621     }
622     
623     // A constrained snap, is a snap in only one degree of freedom (specified by the constraint line).
624     // This is usefull for example when scaling an object while maintaining a fixed aspect ratio. It's
625     // nodes are only allowed to move in one direction (i.e. in one degree of freedom).
626     
627     // When snapping to objects, we either snap to their nodes or their paths. It is however very
628     // unlikely that any node will be exactly at the constrained line, so for a constrained snap
629     // to objects we will only consider the object's paths. Beside, the nodes will be at these paths,
630     // so we will more or less snap to them anyhow.   
632     if (_snap_to_itempath || _snap_to_bboxpath || _snap_to_page_border) {
633         _snapPathsConstrained(sc, t, p, first_point, c);
634     }
638 // This method is used to snap a guide to nodes, while dragging the guide around
639 void Inkscape::ObjectSnapper::guideSnap(SnappedConstraints &sc,
640                                         Geom::Point const &p,
641                                         Geom::Point const &guide_normal) const
643     if ( NULL == _named_view ) {
644         return;
645     }
646     
647     /* Get a list of all the SPItems that we will try to snap to */
648     std::vector<SPItem*> cand;
649     std::vector<SPItem const *> const it; //just an empty list
650     
651     DimensionToSnap snap_dim;
652     if (guide_normal == to_2geom(component_vectors[Geom::Y])) {
653         snap_dim = GUIDE_TRANSL_SNAP_Y;
654     } else if (guide_normal == to_2geom(component_vectors[Geom::X])) {
655         snap_dim = GUIDE_TRANSL_SNAP_X;
656     } else {
657         snap_dim = ANGLED_GUIDE_TRANSL_SNAP;
658     }
659     
660     // We don't support ANGLED_GUIDE_ROT_SNAP yet. 
661     
662     // It would be cool to allow the user to rotate a guide by dragging it, instead of
663     // only translating it. (For example when CTRL is pressed). We will need an UI part 
664     // for that first; and some important usability choices need to be made: 
665     // E.g. which point should be used for pivoting? A previously snapped point,
666     // or a transformation center (which can be moved after clicking for the
667     // second time on an object; but should this point then be constrained to the
668     // line, or can it be located anywhere?)
669     
670     _findCandidates(sp_document_root(_named_view->document), &it, true, Geom::Rect(p, p), snap_dim, false, Geom::identity());
671     _snapTranslatingGuideToNodes(sc, Inkscape::Snapper::SNAPPOINT_GUIDE, p, guide_normal);
672     // _snapRotatingGuideToNodes has not been implemented yet. 
675 /**
676  *  \return true if this Snapper will snap at least one kind of point.
677  */
678 bool Inkscape::ObjectSnapper::ThisSnapperMightSnap() const
680     bool snap_to_something = _snap_to_itempath || _snap_to_itemnode || _snap_to_bboxpath || _snap_to_bboxnode || _snap_to_page_border;
681     return (_snap_enabled && _snap_from != 0 && snap_to_something);
684 bool Inkscape::ObjectSnapper::GuidesMightSnap() const
686     bool snap_to_something = _snap_to_itemnode || _snap_to_bboxnode;
687     return (_snap_enabled && (_snap_from & SNAPPOINT_GUIDE) && snap_to_something);
690 void Inkscape::ObjectSnapper::_clear_paths() const 
692     for (std::vector<Geom::PathVector*>::const_iterator k = _paths_to_snap_to->begin(); k != _paths_to_snap_to->end(); k++) {
693         g_free(*k);
694     }
695     _paths_to_snap_to->clear();
698 Geom::PathVector* Inkscape::ObjectSnapper::_getBorderPathv() const
700     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)));
701     return _getPathvFromRect(border_rect);        
704 Geom::PathVector* Inkscape::ObjectSnapper::_getPathvFromRect(Geom::Rect const rect) const
706     SPCurve const *border_curve = SPCurve::new_from_rect(rect);
707     if (border_curve) {
708         Geom::PathVector *dummy = new Geom::PathVector(border_curve->get_pathvector());
709         return dummy;
710     } else {
711         return NULL;
712     }     
715 void Inkscape::ObjectSnapper::_getBorderNodes(std::vector<Geom::Point> *points) const
717     Geom::Coord w = sp_document_width(_named_view->document);
718     Geom::Coord h = sp_document_height(_named_view->document);
719     points->push_back(Geom::Point(0,0));
720     points->push_back(Geom::Point(0,h));
721     points->push_back(Geom::Point(w,h));
722     points->push_back(Geom::Point(w,0));
725 /*
726   Local Variables:
727   mode:c++
728   c-file-style:"stroustrup"
729   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
730   indent-tabs-mode:nil
731   fill-column:99
732   End:
733 */
734 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :