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*/,
68 std::vector<SnapCandidatePoint> */*unselected_nodes*/) const
70 {
71 if (_snap_enabled == false || _snapmanager->snapprefs.getSnapFrom(p.getSourceType()) == false) {
72 return;
73 }
75 // project the mouse pointer onto the constraint. Only the projected point will be considered for snapping
76 Geom::Point pp = c.projection(p.getPoint());
78 /* Get the lines that we will try to snap to */
79 const LineList lines = _getSnapLines(pp);
81 for (LineList::const_iterator i = lines.begin(); i != lines.end(); i++) {
82 Geom::Point const point_on_line = c.hasPoint() ? c.getPoint() : pp;
83 Geom::Line gridguide_line(i->second, i->second + Geom::rot90(i->first));
85 if (c.isCircular()) {
86 // Find the intersections between the line and the circular constraint
87 // First, project the origin of the circle onto the line
88 Geom::Point const origin = c.getPoint();
89 Geom::Point const p_proj = Geom::projection(origin, gridguide_line);
90 Geom::Coord dist = Geom::L2(p_proj - origin); // distance from circle origin to constraint line
91 Geom::Coord radius = c.getRadius();
92 if (dist == radius) {
93 // Only one point of intersection;
94 _addSnappedPoint(sc, p_proj, Geom::L2(pp - p_proj), p.getSourceType(), p.getSourceNum(), true);
95 } else if (dist < radius) {
96 // Two points of intersection, symmetrical with respect to the projected point
97 // Calculate half the length of the linesegment between the two points of intersection
98 Geom::Coord l = sqrt(radius*radius - dist*dist);
99 Geom::Coord d = Geom::L2(gridguide_line.versor()); // length of versor, needed to normalize the versor
100 if (d > 0) {
101 Geom::Point v = l*gridguide_line.versor()/d;
102 _addSnappedPoint(sc, p_proj + v, Geom::L2(p.getPoint() - (p_proj + v)), p.getSourceType(), p.getSourceNum(), true);
103 _addSnappedPoint(sc, p_proj - v, Geom::L2(p.getPoint() - (p_proj - v)), p.getSourceType(), p.getSourceNum(), true);
104 }
105 }
106 } else {
107 // Find the intersections between the line and the linear constraint
108 Geom::Line constraint_line(point_on_line, point_on_line + c.getDirection());
109 Geom::OptCrossing inters = Geom::OptCrossing(); // empty by default
110 try
111 {
112 inters = Geom::intersection(constraint_line, gridguide_line);
113 }
114 catch (Geom::InfiniteSolutions e)
115 {
116 // We're probably dealing with parallel lines, so snapping doesn't make any sense here
117 continue; // jump to the next iterator in the for-loop
118 }
120 if (inters) {
121 Geom::Point t = constraint_line.pointAt((*inters).ta);
122 const Geom::Coord dist = Geom::L2(t - p.getPoint());
123 if (dist < getSnapperTolerance()) {
124 // When doing a constrained snap, we're already at an intersection.
125 // This snappoint is therefore fully constrained, so there's no need
126 // to look for additional intersections; just return the snapped point
127 // and forget about the line
128 _addSnappedPoint(sc, t, dist, p.getSourceType(), p.getSourceNum(), true);
129 }
130 }
131 }
132 }
133 }
135 // Will only be overridden in the guide-snapper class, because grid lines don't have an origin; the
136 // grid-snapper classes will use this default empty method
137 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
138 {
139 }
141 /*
142 Local Variables:
143 mode:c++
144 c-file-style:"stroustrup"
145 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
146 indent-tabs-mode:nil
147 fill-column:99
148 End:
149 */
150 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :