Code

The snap indicator's tooltip now displays "A to B", whereas before it only displayed...
[inkscape.git] / src / line-snapper.cpp
1 /**
2  * \file line-snapper.cpp
3  * \brief LineSnapper class.
4  *
5  * Authors:
6  *   Diederik van Lierop <mail@diedenrezi.nl>
7  *   And others...
8  *
9  * Copyright (C) 1999-2008 Authors
10  *
11  * Released under GNU GPL, read the file 'COPYING' for more information
12  */
14 #include "libnr/nr-values.h"
15 #include "libnr/nr-point-fns.h"
16 #include <2geom/geom.h>
17 #include "line-snapper.h"
18 #include "snapped-line.h"
19 #include <gtk/gtk.h>
20 #include "snap.h"
22 Inkscape::LineSnapper::LineSnapper(SnapManager *sm, Geom::Coord const d) : Snapper(sm, d)
23 {
24 }
26 void Inkscape::LineSnapper::freeSnap(SnappedConstraints &sc,
27                                                     Inkscape::SnapPreferences::PointType const &t,
28                                                     Geom::Point const &p,
29                                                     SnapSourceType const &source_type,
30                                                     bool const &/*f*/,
31                                                     Geom::OptRect const &/*bbox_to_snap*/,
32                                                     std::vector<SPItem const *> const */*it*/,
33                                                     std::vector<std::pair<Geom::Point, int> > */*unselected_nodes*/) const
34 {
35         if (!(_snap_enabled && _snapmanager->snapprefs.getSnapFrom(t)) ) {
36         return;
37     }
39     /* Get the lines that we will try to snap to */
40     const LineList lines = _getSnapLines(p);
42     // std::cout << "snap point " << p << " to: " << std::endl;
44     for (LineList::const_iterator i = lines.begin(); i != lines.end(); i++) {
45         Geom::Point const p1 = i->second; // point at guide/grid line
46         Geom::Point const p2 = p1 + Geom::rot90(i->first); // 2nd point at guide/grid line
47         // std::cout << "  line through " << i->second << " with normal " << i->first;
48         g_assert(i->first != Geom::Point(0,0)); // we cannot project on an linesegment of zero length
50         Geom::Point const p_proj = project_on_linesegment(p, p1, p2);
51         Geom::Coord const dist = Geom::L2(p_proj - p);
52         //Store any line that's within snapping range
53         if (dist < getSnapperTolerance()) {
54             _addSnappedLine(sc, p_proj, dist, source_type, Inkscape::SNAPTARGET_UNDEFINED, i->first, i->second);
55             // We don't know if we're snapping to grids or guides here; therefore the snap target type will be set in findBestSnap()
56             // std::cout << " -> distance = " << dist;
57         }
58         // std::cout << std::endl;
59     }
60 }
62 void Inkscape::LineSnapper::constrainedSnap(SnappedConstraints &sc,
63                                                Inkscape::SnapPreferences::PointType const &t,
64                                                Geom::Point const &p,
65                                                SnapSourceType const &source_type,
66                                                bool const &/*f*/,
67                                                Geom::OptRect const &/*bbox_to_snap*/,
68                                                ConstraintLine const &c,
69                                                std::vector<SPItem const *> const */*it*/) const
71 {
72     if (_snap_enabled == false || _snapmanager->snapprefs.getSnapFrom(t) == false) {
73         return;
74     }
76     /* Get the lines that we will try to snap to */
77     const LineList lines = _getSnapLines(p);
79     for (LineList::const_iterator i = lines.begin(); i != lines.end(); i++) {
80         if (Geom::L2(c.getDirection()) > 0) { // Can't do a constrained snap without a constraint
81             /* Normal to the line we're trying to snap along */
82             Geom::Point const n(Geom::rot90(Geom::unit_vector(c.getDirection())));
84             Geom::Point const point_on_line = c.hasPoint() ? c.getPoint() : p;
86             /* Constant term of the line we're trying to snap along */
87             Geom::Coord const q0 = dot(n, point_on_line);
88             /* Constant term of the grid or guide line */
89             Geom::Coord const q1 = dot(i->first, i->second);
91             /* Try to intersect this line with the target line */
92             Geom::Point t_2geom(NR_HUGE, NR_HUGE);
93             Geom::IntersectorKind const k = Geom::line_intersection(n, q0, i->first, q1, t_2geom);
94             Geom::Point t(t_2geom);
96             if (k == Geom::intersects) {
97                 const Geom::Coord dist = L2(t - p);
98                 if (dist < getSnapperTolerance()) {
99                                 // When doing a constrained snap, we're already at an intersection.
100                     // This snappoint is therefore fully constrained, so there's no need
101                     // to look for additional intersections; just return the snapped point
102                     // and forget about the line
103                     sc.points.push_back(SnappedPoint(t, source_type, Inkscape::SNAPTARGET_UNDEFINED, dist, getSnapperTolerance(), getSnapperAlwaysSnap(), true));
104                 }
105             }
106         }
107     }
110 /*
111   Local Variables:
112   mode:c++
113   c-file-style:"stroustrup"
114   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
115   indent-tabs-mode:nil
116   fill-column:99
117   End:
118 */
119 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :