f2d2f2ff624b0b46099fd802e324cf5cdd579a91
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 const *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 bool const &/*f*/,
30 Geom::OptRect const &/*bbox_to_snap*/,
31 std::vector<SPItem const *> const */*it*/,
32 std::vector<Geom::Point> */*unselected_nodes*/) const
33 {
34 if (_snap_enabled == false || _snapmanager->snapprefs.getSnapFrom(t) == false) {
35 return;
36 }
38 /* Get the lines that we will try to snap to */
39 const LineList lines = _getSnapLines(p);
41 // std::cout << "snap point " << p << " to: " << std::endl;
43 for (LineList::const_iterator i = lines.begin(); i != lines.end(); i++) {
44 Geom::Point const p1 = i->second; // point at guide/grid line
45 Geom::Point const p2 = p1 + Geom::rot90(i->first); // 2nd point at guide/grid line
46 // std::cout << " line through " << i->second << " with normal " << i->first;
47 g_assert(i->first != Geom::Point(0,0)); // we cannot project on an linesegment of zero length
49 Geom::Point const p_proj = project_on_linesegment(p, p1, p2);
50 Geom::Coord const dist = Geom::L2(p_proj - p);
51 //Store any line that's within snapping range
52 if (dist < getSnapperTolerance()) {
53 _addSnappedLine(sc, p_proj, dist, i->first, i->second);
54 // std::cout << " -> distance = " << dist;
55 }
56 // std::cout << std::endl;
57 }
58 }
60 void Inkscape::LineSnapper::constrainedSnap(SnappedConstraints &sc,
61 Inkscape::SnapPreferences::PointType const &t,
62 Geom::Point const &p,
63 bool const &/*f*/,
64 Geom::OptRect const &/*bbox_to_snap*/,
65 ConstraintLine const &c,
66 std::vector<SPItem const *> const */*it*/) const
68 {
69 if (_snap_enabled == false || _snapmanager->snapprefs.getSnapFrom(t) == false) {
70 return;
71 }
73 /* Get the lines that we will try to snap to */
74 const LineList lines = _getSnapLines(p);
76 for (LineList::const_iterator i = lines.begin(); i != lines.end(); i++) {
77 if (Geom::L2(c.getDirection()) > 0) { // Can't do a constrained snap without a constraint
78 /* Normal to the line we're trying to snap along */
79 Geom::Point const n(Geom::rot90(Geom::unit_vector(c.getDirection())));
81 Geom::Point const point_on_line = c.hasPoint() ? c.getPoint() : p;
83 /* Constant term of the line we're trying to snap along */
84 Geom::Coord const q0 = dot(n, point_on_line);
85 /* Constant term of the grid or guide line */
86 Geom::Coord const q1 = dot(i->first, i->second);
88 /* Try to intersect this line with the target line */
89 Geom::Point t_2geom(NR_HUGE, NR_HUGE);
90 Geom::IntersectorKind const k = Geom::line_intersection(n, q0, i->first, q1, t_2geom);
91 Geom::Point t(t_2geom);
93 if (k == Geom::intersects) {
94 const Geom::Coord dist = L2(t - p);
95 if (dist < getSnapperTolerance()) {
96 // When doing a constrained snap, we're already at an intersection.
97 // This snappoint is therefore fully constrained, so there's no need
98 // to look for additional intersections; just return the snapped point
99 // and forget about the line
100 sc.points.push_back(SnappedPoint(t, Inkscape::SNAPTARGET_UNDEFINED, dist, getSnapperTolerance(), getSnapperAlwaysSnap(), true));
101 // The type of the snap target is yet undefined, as we cannot tell whether
102 // we're snapping to grid or the guide lines; must be set by on a higher level
103 }
104 }
105 }
106 }
107 }
109 /*
110 Local Variables:
111 mode:c++
112 c-file-style:"stroustrup"
113 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
114 indent-tabs-mode:nil
115 fill-column:99
116 End:
117 */
118 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :