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 "display/curve.h"
19 #include "desktop.h"
20 #include "inkscape.h"
21 #include "splivarot.h"
24 Inkscape::ObjectSnapper::ObjectSnapper(SPNamedView const *nv, NR::Coord const d)
25 : Snapper(nv, d), _snap_to_nodes(true), _snap_to_paths(true)
26 {
28 }
31 /**
32 * \param p Point we are trying to snap (desktop coordinates)
33 */
35 void Inkscape::ObjectSnapper::_findCandidates(std::list<SPItem*>& c,
36 SPObject* r,
37 std::list<SPItem const *> const &it,
38 NR::Point const &p) const
39 {
40 for (SPObject* o = r->children; o != NULL; o = o->next) {
41 if (SP_IS_ITEM(o)) {
43 /* See if this item is on the ignore list */
44 std::list<SPItem const *>::const_iterator i = it.begin();
45 while (i != it.end() && *i != o) {
46 i++;
47 }
49 if (i == it.end()) {
50 /* See if the item is within range */
51 NR::Rect const b = NR::expand(sp_item_bbox_desktop(SP_ITEM(o)), -getDistance());
52 if (b.contains(p)) {
53 c.push_back(SP_ITEM(o));
54 }
55 }
56 }
58 _findCandidates(c, o, it, p);
59 }
60 }
63 void Inkscape::ObjectSnapper::_snapNodes(Inkscape::SnappedPoint &s,
64 NR::Point const &p,
65 std::list<SPItem*> const &cand) const
66 {
67 /* FIXME: this seems like a hack. Perhaps Snappers should be
68 ** in SPDesktop rather than SPNamedView?
69 */
70 SPDesktop const *desktop = SP_ACTIVE_DESKTOP;
72 for (std::list<SPItem*>::const_iterator i = cand.begin(); i != cand.end(); i++) {
73 if (SP_IS_SHAPE(*i)) {
75 SPShape const *sh = SP_SHAPE(*i);
76 if (sh->curve) {
78 int j = 0;
79 NR::Matrix const i2doc = sp_item_i2doc_affine(*i);
81 while (sh->curve->bpath[j].code != NR_END) {
83 /* Get this node in desktop coordinates */
84 NArtBpath const &bp = sh->curve->bpath[j];
85 NR::Point const n = desktop->doc2dt(bp.c(3) * i2doc);
87 /* Try to snap to this node of the path */
88 NR::Coord const dist = NR::L2(n - p);
89 if (dist < getDistance() && dist < s.getDistance()) {
90 s = SnappedPoint(n, dist);
91 }
93 j++;
94 }
95 }
96 }
97 }
98 }
101 void Inkscape::ObjectSnapper::_snapPaths(Inkscape::SnappedPoint &s,
102 NR::Point const &p,
103 std::list<SPItem*> const &cand) const
104 {
105 /* FIXME: this seems like a hack. Perhaps Snappers should be
106 ** in SPDesktop rather than SPNamedView?
107 */
108 SPDesktop const *desktop = SP_ACTIVE_DESKTOP;
110 NR::Point const p_doc = desktop->dt2doc(p);
112 for (std::list<SPItem*>::const_iterator i = cand.begin(); i != cand.end(); i++) {
114 /* Transform the requested snap point to this item's coordinates */
115 NR::Matrix const i2doc = sp_item_i2doc_affine(*i);
116 NR::Point const p_it = p_doc * i2doc.inverse();
118 /* Look for the nearest position on this SPItem to our snap point */
119 NR::Maybe<Path::cut_position> const o = get_nearest_position_on_Path(*i, p_it);
120 if (o != NR::Nothing() && o.assume().t >= 0 && o.assume().t <= 1) {
122 /* Convert the nearest point back to desktop coordinates */
123 NR::Point const o_it = get_point_on_Path(*i, o.assume().piece, o.assume().t);
124 NR::Point const o_dt = desktop->doc2dt(o_it * i2doc);
126 NR::Coord const dist = NR::L2(o_dt - p);
127 if (dist < getDistance() && dist < s.getDistance()) {
128 s = SnappedPoint(o_dt, dist);
129 }
130 }
131 }
133 }
136 Inkscape::SnappedPoint Inkscape::ObjectSnapper::_doFreeSnap(NR::Point const &p,
137 std::list<SPItem const *> const &it) const
138 {
139 /* Get a list of all the SPItems that we will try to snap to */
140 std::list<SPItem*> cand;
141 _findCandidates(cand, sp_document_root(_named_view->document), it, p);
143 SnappedPoint s(p, NR_HUGE);
145 if (_snap_to_nodes) {
146 _snapNodes(s, p, cand);
147 }
148 if (_snap_to_paths) {
149 _snapPaths(s, p, cand);
150 }
152 return s;
153 }
157 Inkscape::SnappedPoint Inkscape::ObjectSnapper::_doConstrainedSnap(NR::Point const &p,
158 NR::Point const &c,
159 std::list<SPItem const *> const &it) const
160 {
161 /* FIXME: this needs implementing properly; I think we have to do the
162 ** intersection of c with the objects.
163 */
164 return _doFreeSnap(p, it);
165 }
167 /*
168 Local Variables:
169 mode:c++
170 c-file-style:"stroustrup"
171 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
172 indent-tabs-mode:nil
173 fill-column:99
174 End:
175 */
176 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :