Code

Various snapping cleanups and bug fixes.
[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  *
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
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         Path *livarot_path = Path_for_item(*i, true, true);
119         if (!livarot_path)
120             continue;
122         livarot_path->ConvertWithBackData(0.01);
124         /* Look for the nearest position on this SPItem to our snap point */
125         NR::Maybe<Path::cut_position> const o = get_nearest_position_on_Path(livarot_path, p_it);
126         if (o != NR::Nothing() && o.assume().t >= 0 && o.assume().t <= 1) {
128             /* Convert the nearest point back to desktop coordinates */
129             NR::Point const o_it = get_point_on_Path(livarot_path, o.assume().piece, o.assume().t);
130             NR::Point const o_dt = desktop->doc2dt(o_it * i2doc);
132             NR::Coord const dist = NR::L2(o_dt - p);
133             if (dist < getDistance() && dist < s.getDistance()) {
134                 s = SnappedPoint(o_dt, dist);
135             }
136         }
138         delete livarot_path;
139     }
144 Inkscape::SnappedPoint Inkscape::ObjectSnapper::_doFreeSnap(NR::Point const &p,
145                                                             std::list<SPItem const *> const &it) const
147     if ( NULL == _named_view ) {
148         return SnappedPoint(p, NR_HUGE);
149     }
151     /* Get a list of all the SPItems that we will try to snap to */
152     std::list<SPItem*> cand;
153     _findCandidates(cand, sp_document_root(_named_view->document), it, p);
155     SnappedPoint s(p, NR_HUGE);
157     if (_snap_to_nodes) {
158         _snapNodes(s, p, cand);
159     }
160     if (_snap_to_paths) {
161         _snapPaths(s, p, cand);
162     }
164     return s;
169 Inkscape::SnappedPoint Inkscape::ObjectSnapper::_doConstrainedSnap(NR::Point const &p,
170                                                                    ConstraintLine const &c,
171                                                                    std::list<SPItem const *> const &it) const
173     /* FIXME: this needs implementing properly; I think we have to do the
174     ** intersection of c with the objects.
175     */
176     return _doFreeSnap(p, it);
179 /*
180   Local Variables:
181   mode:c++
182   c-file-style:"stroustrup"
183   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
184   indent-tabs-mode:nil
185   fill-column:99
186   End:
187 */
188 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :