d63376e14bffc9840d475ce77dcb47eb70da0048
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 "display/curve.h"
20 #include "desktop.h"
21 #include "inkscape.h"
22 #include "splivarot.h"
25 Inkscape::ObjectSnapper::ObjectSnapper(SPNamedView const *nv, NR::Coord const d)
26 : Snapper(nv, d), _snap_to_nodes(true), _snap_to_paths(true)
27 {
29 }
32 /**
33 * \param p Point we are trying to snap (desktop coordinates)
34 */
36 void Inkscape::ObjectSnapper::_findCandidates(std::list<SPItem*>& c,
37 SPObject* r,
38 std::list<SPItem const *> const &it,
39 NR::Point const &p) const
40 {
41 if (ThisSnapperMightSnap()) {
42 SPDesktop const *desktop = SP_ACTIVE_DESKTOP;
43 for (SPObject* o = sp_object_first_child(r); o != NULL; o = SP_OBJECT_NEXT(o)) {
44 if (SP_IS_ITEM(o) && !SP_ITEM(o)->isLocked() && !desktop->itemIsHidden(SP_ITEM(o))) {
46 /* See if this item is on the ignore list */
47 std::list<SPItem const *>::const_iterator i = it.begin();
48 while (i != it.end() && *i != o) {
49 i++;
50 }
52 if (i == it.end()) {
53 /* See if the item is within range */
54 if (SP_IS_GROUP(o)) {
55 _findCandidates(c, o, it, p);
56 } else {
57 NR::Maybe<NR::Rect> b = sp_item_bbox_desktop(SP_ITEM(o));
58 if ( b && NR::expand(*b, -getDistance()).contains(p) ) {
59 c.push_back(SP_ITEM(o));
60 }
61 }
62 }
64 }
65 }
66 }
67 }
70 void Inkscape::ObjectSnapper::_snapNodes(Inkscape::SnappedPoint &s,
71 NR::Point const &p,
72 std::list<SPItem*> const &cand) const
73 {
74 /* FIXME: this seems like a hack. Perhaps Snappers should be
75 ** in SPDesktop rather than SPNamedView?
76 */
77 SPDesktop const *desktop = SP_ACTIVE_DESKTOP;
79 for (std::list<SPItem*>::const_iterator i = cand.begin(); i != cand.end(); i++) {
80 if (SP_IS_SHAPE(*i)) {
82 SPShape const *sh = SP_SHAPE(*i);
83 if (sh->curve) {
85 int j = 0;
86 NR::Matrix const i2doc = sp_item_i2doc_affine(*i);
88 while (SP_CURVE_BPATH(sh->curve)[j].code != NR_END) {
90 /* Get this node in desktop coordinates */
91 NArtBpath const &bp = SP_CURVE_BPATH(sh->curve)[j];
92 NR::Point const n = desktop->doc2dt(bp.c(3) * i2doc);
94 /* Try to snap to this node of the path */
95 NR::Coord const dist = NR::L2(n - p);
96 if (dist < getDistance() && dist < s.getDistance()) {
97 s = SnappedPoint(n, dist);
98 }
100 j++;
101 }
102 }
103 }
104 }
105 }
108 void Inkscape::ObjectSnapper::_snapPaths(Inkscape::SnappedPoint &s,
109 NR::Point const &p,
110 std::list<SPItem*> const &cand) const
111 {
112 /* FIXME: this seems like a hack. Perhaps Snappers should be
113 ** in SPDesktop rather than SPNamedView?
114 */
115 SPDesktop const *desktop = SP_ACTIVE_DESKTOP;
117 NR::Point const p_doc = desktop->dt2doc(p);
119 for (std::list<SPItem*>::const_iterator i = cand.begin(); i != cand.end(); i++) {
121 /* Transform the requested snap point to this item's coordinates */
122 NR::Matrix const i2doc = sp_item_i2doc_affine(*i);
123 NR::Point const p_it = p_doc * i2doc.inverse();
125 Path *livarot_path = Path_for_item(*i, true, true);
126 if (!livarot_path)
127 continue;
129 livarot_path->ConvertWithBackData(0.01);
131 /* Look for the nearest position on this SPItem to our snap point */
132 NR::Maybe<Path::cut_position> const o = get_nearest_position_on_Path(livarot_path, p_it);
133 if (o && o->t >= 0 && o->t <= 1) {
135 /* Convert the nearest point back to desktop coordinates */
136 NR::Point const o_it = get_point_on_Path(livarot_path, o->piece, o->t);
137 NR::Point const o_dt = desktop->doc2dt(o_it * i2doc);
139 NR::Coord const dist = NR::L2(o_dt - p);
140 if (dist < getDistance() && dist < s.getDistance()) {
141 s = SnappedPoint(o_dt, dist);
142 }
143 }
145 delete livarot_path;
146 }
148 }
151 Inkscape::SnappedPoint Inkscape::ObjectSnapper::_doFreeSnap(NR::Point const &p,
152 std::list<SPItem const *> const &it) const
153 {
154 if ( NULL == _named_view ) {
155 return SnappedPoint(p, NR_HUGE);
156 }
158 /* Get a list of all the SPItems that we will try to snap to */
159 std::list<SPItem*> cand;
160 _findCandidates(cand, sp_document_root(_named_view->document), it, p);
162 SnappedPoint s(p, NR_HUGE);
164 if (_snap_to_nodes) {
165 _snapNodes(s, p, cand);
166 }
167 if (_snap_to_paths) {
168 _snapPaths(s, p, cand);
169 }
171 return s;
172 }
176 Inkscape::SnappedPoint Inkscape::ObjectSnapper::_doConstrainedSnap(NR::Point const &p,
177 ConstraintLine const &c,
178 std::list<SPItem const *> const &it) const
179 {
180 /* FIXME: this needs implementing properly; I think we have to do the
181 ** intersection of c with the objects.
182 */
183 return _doFreeSnap(p, it);
184 }
186 /**
187 * \return true if this Snapper will snap at least one kind of point.
188 */
189 bool Inkscape::ObjectSnapper::ThisSnapperMightSnap() const
190 {
191 return (_enabled && _snap_to != 0 && (_snap_to_paths || _snap_to_nodes));
192 }
195 /*
196 Local Variables:
197 mode:c++
198 c-file-style:"stroustrup"
199 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
200 indent-tabs-mode:nil
201 fill-column:99
202 End:
203 */
204 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :