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 <2geom/line.h>
15 #include "line-snapper.h"
16 #include "snapped-line.h"
17 //#include <gtk/gtk.h>
18 #include "snap.h"
20 Inkscape::LineSnapper::LineSnapper(SnapManager *sm, Geom::Coord const d) : Snapper(sm, d)
21 {
22 }
24 void Inkscape::LineSnapper::freeSnap(SnappedConstraints &sc,
25 Inkscape::SnapCandidatePoint const &p,
26 Geom::OptRect const &/*bbox_to_snap*/,
27 std::vector<SPItem const *> const */*it*/,
28 std::vector<Inkscape::SnapCandidatePoint> */*unselected_nodes*/) const
29 {
30 if (!(_snap_enabled && _snapmanager->snapprefs.getSnapFrom(p.getSourceType())) ) {
31 return;
32 }
34 /* Get the lines that we will try to snap to */
35 const LineList lines = _getSnapLines(p.getPoint());
37 for (LineList::const_iterator i = lines.begin(); i != lines.end(); i++) {
38 Geom::Point const p1 = i->second; // point at guide/grid line
39 Geom::Point const p2 = p1 + Geom::rot90(i->first); // 2nd point at guide/grid line
40 // std::cout << " line through " << i->second << " with normal " << i->first;
41 g_assert(i->first != Geom::Point(0,0)); // we cannot project on an linesegment of zero length
43 Geom::Point const p_proj = Geom::projection(p.getPoint(), Geom::Line(p1, p2));
44 Geom::Coord const dist = Geom::L2(p_proj - p.getPoint());
45 //Store any line that's within snapping range
46 if (dist < getSnapperTolerance()) {
47 _addSnappedLine(sc, p_proj, dist, p.getSourceType(), p.getSourceNum(), i->first, i->second);
48 // For any line that's within range, we will also look at it's "point on line" p1. For guides
49 // this point coincides with its origin; for grids this is of no use, but we cannot
50 // discern between grids and guides here
51 Geom::Coord const dist_p1 = Geom::L2(p1 - p.getPoint());
52 if (dist_p1 < getSnapperTolerance()) {
53 _addSnappedLinesOrigin(sc, p1, dist_p1, p.getSourceType(), p.getSourceNum(), false);
54 // Only relevant for guides; grids don't have an origin per line
55 // Therefore _addSnappedLinesOrigin() will only be implemented for guides
56 }
57 // std::cout << " -> distance = " << dist;
58 }
59 // std::cout << std::endl;
60 }
61 }
63 void Inkscape::LineSnapper::constrainedSnap(SnappedConstraints &sc,
64 Inkscape::SnapCandidatePoint const &p,
65 Geom::OptRect const &/*bbox_to_snap*/,
66 SnapConstraint const &c,
67 std::vector<SPItem const *> const */*it*/) const
69 {
70 if (_snap_enabled == false || _snapmanager->snapprefs.getSnapFrom(p.getSourceType()) == false) {
71 return;
72 }
74 /* Get the lines that we will try to snap to */
75 const LineList lines = _getSnapLines(p.getPoint());
77 for (LineList::const_iterator i = lines.begin(); i != lines.end(); i++) {
78 Geom::Point const point_on_line = c.hasPoint() ? c.getPoint() : p.getPoint();
79 Geom::Line gridguide_line(i->second, i->second + Geom::rot90(i->first));
81 if (c.isCircular()) {
82 // Find the intersections between the line and the circular constraint
83 // First, project the origin of the circle onto the line
84 Geom::Point const origin = c.getPoint();
85 Geom::Point const p_proj = Geom::projection(origin, gridguide_line);
86 Geom::Point v_orig = c.getDirection(); // vector from the origin to the original (untransformed) point
87 Geom::Point v_proj = p_proj - origin;
88 Geom::Coord dist = Geom::L2(v_proj); // distance from circle origin to constraint line
89 Geom::Coord radius = c.getRadius();
90 Geom::Coord radians = NR_HUGE;
91 if (dist == radius) {
92 // Only one point of intersection;
93 // Calculate the rotation in radians...
94 radians = atan2(Geom::dot(Geom::rot90(v_orig), v_proj), Geom::dot(v_orig, v_proj));
95 _addSnappedPoint(sc, p_proj, Geom::L2(p.getPoint() - p_proj), p.getSourceType(), p.getSourceNum(), true, radians);
96 } else if (dist < radius) {
97 // Two points of intersection, symmetrical with respect to the projected point
98 // Calculate half the length of the linesegment between the two points of intersection
99 Geom::Coord l = sqrt(radius*radius - dist*dist);
100 Geom::Coord d = Geom::L2(gridguide_line.versor()); // length of versor, needed to normalize the versor
101 if (d > 0) {
102 Geom::Point v = l*gridguide_line.versor()/d;
103 v_proj = p_proj + v - origin;
104 radians = atan2(Geom::dot(Geom::rot90(v_orig), v_proj), Geom::dot(v_orig, v_proj));
105 _addSnappedPoint(sc, p_proj + v, Geom::L2(p.getPoint() - (p_proj + v)), p.getSourceType(), p.getSourceNum(), true, radians);
106 v_proj = p_proj - v - origin;
107 radians = atan2(Geom::dot(Geom::rot90(v_orig), v_proj), Geom::dot(v_orig, v_proj));
108 _addSnappedPoint(sc, p_proj - v, Geom::L2(p.getPoint() - (p_proj - v)), p.getSourceType(), p.getSourceNum(), true, radians);
109 }
110 }
111 } else {
112 // Find the intersections between the line and the linear constraint
113 Geom::Line constraint_line(point_on_line, point_on_line + c.getDirection());
114 Geom::OptCrossing inters = Geom::OptCrossing(); // empty by default
115 try
116 {
117 inters = Geom::intersection(constraint_line, gridguide_line);
118 }
119 catch (Geom::InfiniteSolutions e)
120 {
121 // We're probably dealing with parallel lines, so snapping doesn't make any sense here
122 continue; // jump to the next iterator in the for-loop
123 }
125 if (inters) {
126 Geom::Point t = constraint_line.pointAt((*inters).ta);
127 const Geom::Coord dist = Geom::L2(t - p.getPoint());
128 if (dist < getSnapperTolerance()) {
129 // When doing a constrained snap, we're already at an intersection.
130 // This snappoint is therefore fully constrained, so there's no need
131 // to look for additional intersections; just return the snapped point
132 // and forget about the line
133 _addSnappedPoint(sc, t, dist, p.getSourceType(), p.getSourceNum(), true, 1);
134 }
135 }
136 }
137 }
138 }
140 // Will only be overridden in the guide-snapper class, because grid lines don't have an origin; the
141 // grid-snapper classes will use this default empty method
142 void Inkscape::LineSnapper::_addSnappedLinesOrigin(SnappedConstraints &/*sc*/, Geom::Point const /*origin*/, Geom::Coord const /*snapped_distance*/, SnapSourceType const &/*source_type*/, long /*source_num*/, bool /*constrained_snap*/) const
143 {
144 }
146 /*
147 Local Variables:
148 mode:c++
149 c-file-style:"stroustrup"
150 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
151 indent-tabs-mode:nil
152 fill-column:99
153 End:
154 */
155 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :