2
* \file object-snapper.cpp
3
* \brief Snapping things to objects.
6
* Carl Hetherington <inkscape@carlh.net>
8
* Copyright (C) 2005 Authors
10
* Released under GNU GPL, read the file 'COPYING' for more information
13
#include "libnr/n-art-bpath.h"
14
#include "libnr/nr-rect-ops.h"
16
#include "sp-namedview.h"
18
#include "display/curve.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)
32
* \param p Point we are trying to snap (desktop coordinates)
35
void Inkscape::ObjectSnapper::_findCandidates(std::list<SPItem*>& c,
37
std::list<SPItem const *> const &it,
38
NR::Point const &p) const
40
for (SPObject* o = r->children; o != NULL; o = o->next) {
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) {
50
/* See if the item is within range */
51
NR::Rect const b = NR::expand(sp_item_bbox_desktop(SP_ITEM(o)), -getDistance());
53
c.push_back(SP_ITEM(o));
58
_findCandidates(c, o, it, p);
63
void Inkscape::ObjectSnapper::_snapNodes(Inkscape::SnappedPoint &s,
65
std::list<SPItem*> const &cand) const
67
/* FIXME: this seems like a hack. Perhaps Snappers should be
68
** in SPDesktop rather than SPNamedView?
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);
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);
101
void Inkscape::ObjectSnapper::_snapPaths(Inkscape::SnappedPoint &s,
103
std::list<SPItem*> const &cand) const
105
/* FIXME: this seems like a hack. Perhaps Snappers should be
106
** in SPDesktop rather than SPNamedView?
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);
136
Inkscape::SnappedPoint Inkscape::ObjectSnapper::_doFreeSnap(NR::Point const &p,
137
std::list<SPItem const *> const &it) const
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);
148
if (_snap_to_paths) {
149
_snapPaths(s, p, cand);
157
Inkscape::SnappedPoint Inkscape::ObjectSnapper::_doConstrainedSnap(NR::Point const &p,
159
std::list<SPItem const *> const &it) const
161
/* FIXME: this needs implementing properly; I think we have to do the
162
** intersection of c with the objects.
164
return _doFreeSnap(p, it);
170
c-file-style:"stroustrup"
171
c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
176
// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :