8d5b30dffea2c58482a25fc4517d7be63af1eb9a
1 /**
2 * \file object-snapper.cpp
3 * \brief Snapping things to objects.
4 *
5 * Authors:
6 * Carl Hetherington <inkscape@carlh.net>
7 *
8 * Copyright (C) 2005 Authors
9 *
10 * Released under GNU GPL, read the file 'COPYING' for more information
11 */
13 #include "libnr/n-art-bpath.h"
14 #include "libnr/nr-rect-ops.h"
15 #include "document.h"
16 #include "sp-namedview.h"
17 #include "sp-path.h"
18 #include "sp-item-group.h"
19 #include "sp-use.h"
20 #include "display/curve.h"
21 #include "desktop.h"
22 #include "inkscape.h"
23 #include "splivarot.h"
26 Inkscape::ObjectSnapper::ObjectSnapper(SPNamedView const *nv, NR::Coord const d)
27 : Snapper(nv, d), _snap_to_nodes(true), _snap_to_paths(true)
28 {
30 }
33 /**
34 * \param p Point we are trying to snap (desktop coordinates)
35 */
37 void Inkscape::ObjectSnapper::_findCandidates(std::list<SPItem*>& c,
38 SPObject* r,
39 std::list<SPItem const *> const &it,
40 NR::Point const &p) const
41 {
42 if (ThisSnapperMightSnap()) {
43 SPDesktop const *desktop = SP_ACTIVE_DESKTOP;
44 for (SPObject* o = sp_object_first_child(r); o != NULL; o = SP_OBJECT_NEXT(o)) {
45 if (SP_IS_ITEM(o) && !SP_ITEM(o)->isLocked() && !desktop->itemIsHidden(SP_ITEM(o))) {
47 /* See if this item is on the ignore list */
48 std::list<SPItem const *>::const_iterator i = it.begin();
49 while (i != it.end() && *i != o) {
50 i++;
51 }
53 if (i == it.end()) {
54 /* See if the item is within range */
55 if (SP_IS_GROUP(o)) {
56 _findCandidates(c, o, it, p);
57 } else {
58 NR::Maybe<NR::Rect> b = sp_item_bbox_desktop(SP_ITEM(o));
59 if ( b && NR::expand(*b, -getDistance()).contains(p) ) {
60 c.push_back(SP_ITEM(o));
61 }
62 }
63 }
65 }
66 }
67 }
68 }
71 void Inkscape::ObjectSnapper::_snapNodes(Inkscape::SnappedPoint &s,
72 NR::Point const &p,
73 std::list<SPItem*> const &cand) const
74 {
75 /* FIXME: this seems like a hack. Perhaps Snappers should be
76 ** in SPDesktop rather than SPNamedView?
77 */
78 SPDesktop const *desktop = SP_ACTIVE_DESKTOP;
80 for (std::list<SPItem*>::const_iterator i = cand.begin(); i != cand.end(); i++) {
82 NR::Matrix i2doc(NR::identity());
83 SPItem *root_item = NULL;
84 if (SP_IS_USE(*i)) {
85 i2doc = sp_use_get_root_transform(SP_USE(*i));
86 root_item = sp_use_root(SP_USE(*i));
87 } else {
88 i2doc = sp_item_i2doc_affine(*i);
89 root_item = *i;
90 }
92 if (SP_IS_SHAPE(root_item)) {
94 SPShape const *sh = SP_SHAPE(root_item);
95 if (sh->curve) {
97 int j = 0;
99 while (SP_CURVE_BPATH(sh->curve)[j].code != NR_END) {
101 /* Get this node in desktop coordinates */
102 NArtBpath const &bp = SP_CURVE_BPATH(sh->curve)[j];
103 NR::Point const n = desktop->doc2dt(bp.c(3) * i2doc);
105 /* Try to snap to this node of the path */
106 NR::Coord const dist = NR::L2(n - p);
107 if (dist < getDistance() && dist < s.getDistance()) {
108 s = SnappedPoint(n, dist);
109 }
111 j++;
112 }
113 }
114 }
115 }
116 }
119 void Inkscape::ObjectSnapper::_snapPaths(Inkscape::SnappedPoint &s,
120 NR::Point const &p,
121 std::list<SPItem*> const &cand) const
122 {
123 /* FIXME: this seems like a hack. Perhaps Snappers should be
124 ** in SPDesktop rather than SPNamedView?
125 */
126 SPDesktop const *desktop = SP_ACTIVE_DESKTOP;
128 NR::Point const p_doc = desktop->dt2doc(p);
130 for (std::list<SPItem*>::const_iterator i = cand.begin(); i != cand.end(); i++) {
132 /* Transform the requested snap point to this item's coordinates */
133 NR::Matrix i2doc(NR::identity());
134 SPItem *root_item = NULL;
135 /* We might have a clone at hand, so make sure we get the root item */
136 if (SP_IS_USE(*i)) {
137 i2doc = sp_use_get_root_transform(SP_USE(*i));
138 root_item = sp_use_root(SP_USE(*i));
139 } else {
140 i2doc = sp_item_i2doc_affine(*i);
141 root_item = *i;
142 }
144 NR::Point const p_it = p_doc * i2doc.inverse();
146 Path *livarot_path = Path_for_item(root_item, false, false);
147 if (!livarot_path)
148 continue;
150 livarot_path->ConvertWithBackData(0.01);
152 /* Look for the nearest position on this SPItem to our snap point */
153 NR::Maybe<Path::cut_position> const o = get_nearest_position_on_Path(livarot_path, p_it);
154 if (o && o->t >= 0 && o->t <= 1) {
156 /* Convert the nearest point back to desktop coordinates */
157 NR::Point const o_it = get_point_on_Path(livarot_path, o->piece, o->t);
158 NR::Point const o_dt = desktop->doc2dt(o_it * i2doc);
160 NR::Coord const dist = NR::L2(o_dt - p);
161 if (dist < getDistance() && dist < s.getDistance()) {
162 s = SnappedPoint(o_dt, dist);
163 }
164 }
166 delete livarot_path;
167 }
168 }
171 Inkscape::SnappedPoint Inkscape::ObjectSnapper::_doFreeSnap(NR::Point const &p,
172 std::list<SPItem const *> const &it) const
173 {
174 if ( NULL == _named_view ) {
175 return SnappedPoint(p, NR_HUGE);
176 }
178 /* Get a list of all the SPItems that we will try to snap to */
179 std::list<SPItem*> cand;
180 _findCandidates(cand, sp_document_root(_named_view->document), it, p);
182 SnappedPoint s(p, NR_HUGE);
184 if (_snap_to_nodes) {
185 _snapNodes(s, p, cand);
186 }
187 if (_snap_to_paths) {
188 _snapPaths(s, p, cand);
189 }
191 return s;
192 }
196 Inkscape::SnappedPoint Inkscape::ObjectSnapper::_doConstrainedSnap(NR::Point const &p,
197 ConstraintLine const &c,
198 std::list<SPItem const *> const &it) const
199 {
200 /* FIXME: this needs implementing properly; I think we have to do the
201 ** intersection of c with the objects.
202 */
203 return _doFreeSnap(p, it);
204 }
206 /**
207 * \return true if this Snapper will snap at least one kind of point.
208 */
209 bool Inkscape::ObjectSnapper::ThisSnapperMightSnap() const
210 {
211 return (_enabled && _snap_to != 0 && (_snap_to_paths || _snap_to_nodes));
212 }
215 /*
216 Local Variables:
217 mode:c++
218 c-file-style:"stroustrup"
219 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
220 indent-tabs-mode:nil
221 fill-column:99
222 End:
223 */
224 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :