446ec73e73deab0c57eafa6545f2b9533597f350
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-2007 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.cpp"
19 #include <gtk/gtk.h>
21 Inkscape::LineSnapper::LineSnapper(SPNamedView const *nv, NR::Coord const d) : Snapper(nv, d)
22 {
24 }
26 void Inkscape::LineSnapper::_doFreeSnap(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 std::list<SPItem const *> const &it) const
32 {
33 Inkscape::SnappedPoint s = SnappedPoint(p, NR_HUGE);
35 /* Get the lines that we will try to snap to */
36 const LineList lines = _getSnapLines(p);
38 // std::cout << "snap point " << p << " to: " << std::endl;
40 for (LineList::const_iterator i = lines.begin(); i != lines.end(); i++) {
41 NR::Point const p1 = i->second; // point at guide/grid line
42 NR::Point const p2 = p1 + NR::rot90(i->first); // 2nd point at guide/grid line
44 // std::cout << " line through " << i->second << " with normal " << i->first;
46 g_assert(i->first != NR::Point(0,0)); // otherwise we'll have div. by zero because NR::L2(d2) = 0
48 // p_proj = projection of p on the grid/guide line running from p1 to p2
49 // p_proj = p1 + u (p2 - p1)
50 // calculate u according to "Minimum Distance between a Point and a Line"
51 // see http://local.wasp.uwa.edu.au/~pbourke/geometry/pointline/
52 NR::Point const d1(p-p1); // delta 1
53 NR::Point const d2(p2-p1); // delta 1
54 double const u = (d1[NR::X] * d2[NR::X] + d1[NR::Y] * d2[NR::Y]) / (NR::L2(d2) * NR::L2(d2));
56 NR::Point const p_proj(p1 + u*(p2-p1));
57 NR::Coord const dist = NR::L2(p_proj - p);
58 //Store any line that's within snapping range
59 if (dist < getDistance()) {
60 _addSnappedLine(sc, p_proj, dist, i->first, i->second);
61 // std::cout << " -> distance = " << dist;
62 }
63 // std::cout << std::endl;
64 }
65 }
67 void Inkscape::LineSnapper::_doConstrainedSnap(SnappedConstraints &sc,
68 Inkscape::Snapper::PointType const &/*t*/,
69 NR::Point const &p,
70 bool const &/*f*/,
71 std::vector<NR::Point> &/*points_to_snap*/,
72 ConstraintLine const &c,
73 std::list<SPItem const *> const &/*it*/) const
75 {
76 Inkscape::SnappedPoint s = SnappedPoint(p, NR_HUGE);
78 /* Get the lines that we will try to snap to */
79 const LineList lines = _getSnapLines(p);
81 for (LineList::const_iterator i = lines.begin(); i != lines.end(); i++) {
83 /* Normal to the line we're trying to snap along */
84 NR::Point const n(NR::rot90(NR::unit_vector(c.getDirection())));
86 NR::Point const point_on_line = c.hasPoint() ? c.getPoint() : p;
88 /* Constant term of the line we're trying to snap along */
89 NR::Coord const q0 = dot(n, point_on_line);
90 /* Constant term of the grid or guide line */
91 NR::Coord const q1 = dot(i->first, i->second);
93 /* Try to intersect this line with the target line */
94 Geom::Point t_2geom(NR_HUGE, NR_HUGE);
95 Geom::IntersectorKind const k = Geom::line_intersection(n.to_2geom(), q0, i->first.to_2geom(), q1, t_2geom);
96 NR::Point t(t_2geom);
98 if (k == Geom::intersects) {
99 const NR::Coord dist = L2(t - p);
100 //Store any line that's within snapping range
101 if (dist < getDistance()) {
102 _addSnappedLine(sc, t, dist, c.getDirection(), t);
103 }
104 }
105 }
106 }
108 /*
109 Local Variables:
110 mode:c++
111 c-file-style:"stroustrup"
112 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
113 indent-tabs-mode:nil
114 fill-column:99
115 End:
116 */
117 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :