Code

1) Improving snapping logic 2) When skewing, don't snap to selection itself
[inkscape.git] / src / line-snapper.cpp
1 #include "libnr/nr-values.h"
2 #include "libnr/nr-point-fns.h"
3 #include <2geom/geom.h>
4 #include "line-snapper.h"
5 #include "snapped-line.cpp"
7 Inkscape::LineSnapper::LineSnapper(SPNamedView const *nv, NR::Coord const d) : Snapper(nv, d)
8 {
10 }
12 void Inkscape::LineSnapper::_doFreeSnap(SnappedConstraints &sc,
13                                                     Inkscape::Snapper::PointType const &t,
14                                                     NR::Point const &p,
15                                                     bool const &f,
16                                                      std::vector<NR::Point> &points_to_snap,
17                                                     std::list<SPItem const *> const &it) const
18 {
19     /* Snap along x (i.e. to vertical lines) */
20     _doConstrainedSnap(sc, t, p, f, points_to_snap, component_vectors[NR::X], it);
21     /* Snap along y (i.e. to horizontal lines) */
22     _doConstrainedSnap(sc, t, p, f, points_to_snap, component_vectors[NR::Y], it);
24 }
26 void Inkscape::LineSnapper::_doConstrainedSnap(SnappedConstraints &sc,
27                                                Inkscape::Snapper::PointType const &/*t*/,
28                                                NR::Point const &p,
29                                                bool const &/*f*/,
30                                                std::vector<NR::Point> &/*points_to_snap*/,
31                                                ConstraintLine const &c,
32                                                std::list<SPItem const *> const &/*it*/) const
34 {
35     Inkscape::SnappedPoint s = SnappedPoint(p, NR_HUGE);
37     /* Get the lines that we will try to snap to */
38     const LineList lines = _getSnapLines(p);
40     for (LineList::const_iterator i = lines.begin(); i != lines.end(); i++) {
42         /* Normal to the line we're trying to snap along */
43         NR::Point const n(NR::rot90(NR::unit_vector(c.getDirection())));
45         NR::Point const point_on_line = c.hasPoint() ? c.getPoint() : p;
47         /* Constant term of the line we're trying to snap along */
48         NR::Coord const q = dot(n, point_on_line);
50         /* Try to intersect this line with the target line */
51         Geom::Point t_2geom(NR_HUGE, NR_HUGE);
52         Geom::IntersectorKind const k = Geom::line_intersection(n.to_2geom(), q, component_vectors[i->first].to_2geom(), i->second, t_2geom);
53         NR::Point t(t_2geom);
55         if (k == Geom::intersects) {
56             const NR::Coord dist = L2(t - p);
57             //Store any line that's within snapping range
58             if (dist < getDistance()) {
59                                 _addSnappedLine(sc, t, dist, c.getDirection(), t);
60                 //SnappedLine dummy = SnappedLine(t, dist, c.getDirection(), t);
61                 //sc.infinite_lines.push_back(dummy);
62             }
63         }
64     }
65 }
67 /*
68   Local Variables:
69   mode:c++
70   c-file-style:"stroustrup"
71   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
72   indent-tabs-mode:nil
73   fill-column:99
74   End:
75 */
76 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :