Code

Merge further bbox work
[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 "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     SPDesktop const *desktop = SP_ACTIVE_DESKTOP;
42     for (SPObject* o = sp_object_first_child(r); o != NULL; o = SP_OBJECT_NEXT(o)) {
43         if (SP_IS_ITEM(o) && !SP_ITEM(o)->isLocked() && !desktop->itemIsHidden(SP_ITEM(o))) {
45             /* See if this item is on the ignore list */
46             std::list<SPItem const *>::const_iterator i = it.begin();
47             while (i != it.end() && *i != o) {
48                 i++;
49             }
51             if (i == it.end()) {
52                 /* See if the item is within range */
53                 if (SP_IS_GROUP(o)) {
54                     _findCandidates(c, o, it, p);
55                 } else {
56                     NR::Maybe<NR::Rect> b = sp_item_bbox_desktop(SP_ITEM(o));
57                     if ( b && NR::expand(*b, -getDistance()).contains(p) ) {
58                         c.push_back(SP_ITEM(o));
59                     }
60                 }
61             }
63         }
64     }
65 }
68 void Inkscape::ObjectSnapper::_snapNodes(Inkscape::SnappedPoint &s,
69                                          NR::Point const &p,
70                                          std::list<SPItem*> const &cand) const
71 {
72     /* FIXME: this seems like a hack.  Perhaps Snappers should be
73     ** in SPDesktop rather than SPNamedView?
74     */
75     SPDesktop const *desktop = SP_ACTIVE_DESKTOP;
77     for (std::list<SPItem*>::const_iterator i = cand.begin(); i != cand.end(); i++) {
78         if (SP_IS_SHAPE(*i)) {
80             SPShape const *sh = SP_SHAPE(*i);
81             if (sh->curve) {
83                 int j = 0;
84                 NR::Matrix const i2doc = sp_item_i2doc_affine(*i);
86                 while (SP_CURVE_BPATH(sh->curve)[j].code != NR_END) {
88                     /* Get this node in desktop coordinates */
89                     NArtBpath const &bp = SP_CURVE_BPATH(sh->curve)[j];
90                     NR::Point const n = desktop->doc2dt(bp.c(3) * i2doc);
92                     /* Try to snap to this node of the path */
93                     NR::Coord const dist = NR::L2(n - p);
94                     if (dist < getDistance() && dist < s.getDistance()) {
95                         s = SnappedPoint(n, dist);
96                     }
98                     j++;
99                 }
100             }
101         }
102     }
106 void Inkscape::ObjectSnapper::_snapPaths(Inkscape::SnappedPoint &s,
107                                          NR::Point const &p,
108                                          std::list<SPItem*> const &cand) const
110     /* FIXME: this seems like a hack.  Perhaps Snappers should be
111     ** in SPDesktop rather than SPNamedView?
112     */
113     SPDesktop const *desktop = SP_ACTIVE_DESKTOP;
115     NR::Point const p_doc = desktop->dt2doc(p);
117     for (std::list<SPItem*>::const_iterator i = cand.begin(); i != cand.end(); i++) {
119         /* Transform the requested snap point to this item's coordinates */
120         NR::Matrix const i2doc = sp_item_i2doc_affine(*i);
121         NR::Point const p_it = p_doc * i2doc.inverse();
123         Path *livarot_path = Path_for_item(*i, true, true);
124         if (!livarot_path)
125             continue;
127         livarot_path->ConvertWithBackData(0.01);
129         /* Look for the nearest position on this SPItem to our snap point */
130         NR::Maybe<Path::cut_position> const o = get_nearest_position_on_Path(livarot_path, p_it);
131         if (o && o->t >= 0 && o->t <= 1) {
133             /* Convert the nearest point back to desktop coordinates */
134             NR::Point const o_it = get_point_on_Path(livarot_path, o->piece, o->t);
135             NR::Point const o_dt = desktop->doc2dt(o_it * i2doc);
137             NR::Coord const dist = NR::L2(o_dt - p);
138             if (dist < getDistance() && dist < s.getDistance()) {
139                 s = SnappedPoint(o_dt, dist);
140             }
141         }
143         delete livarot_path;
144     }
149 Inkscape::SnappedPoint Inkscape::ObjectSnapper::_doFreeSnap(NR::Point const &p,
150                                                             std::list<SPItem const *> const &it) const
152     if ( NULL == _named_view ) {
153         return SnappedPoint(p, NR_HUGE);
154     }
156     /* Get a list of all the SPItems that we will try to snap to */
157     std::list<SPItem*> cand;
158     _findCandidates(cand, sp_document_root(_named_view->document), it, p);
160     SnappedPoint s(p, NR_HUGE);
162     if (_snap_to_nodes) {
163         _snapNodes(s, p, cand);
164     }
165     if (_snap_to_paths) {
166         _snapPaths(s, p, cand);
167     }
169     return s;
174 Inkscape::SnappedPoint Inkscape::ObjectSnapper::_doConstrainedSnap(NR::Point const &p,
175                                                                    ConstraintLine const &c,
176                                                                    std::list<SPItem const *> const &it) const
178     /* FIXME: this needs implementing properly; I think we have to do the
179     ** intersection of c with the objects.
180     */
181     return _doFreeSnap(p, it);
184 /*
185   Local Variables:
186   mode:c++
187   c-file-style:"stroustrup"
188   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
189   indent-tabs-mode:nil
190   fill-column:99
191   End:
192 */
193 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :