f2f02533246a8d6b90a20fb73db4694db4ffc678
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());
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 ConstraintLine 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 if (Geom::L2(c.getDirection()) > 0) { // Can't do a constrained snap without a constraint
79 // constraint line
80 Geom::Point const point_on_line = c.hasPoint() ? c.getPoint() : p.getPoint();
81 Geom::Line line1(point_on_line, point_on_line + c.getDirection());
83 // grid/guide line
84 Geom::Point const p1 = i->second; // point at guide/grid line
85 Geom::Point const p2 = p1 + Geom::rot90(i->first); // 2nd point at guide/grid line
86 Geom::Line line2(p1, p2);
88 Geom::OptCrossing inters = Geom::OptCrossing(); // empty by default
89 try
90 {
91 inters = Geom::intersection(line1, line2);
92 }
93 catch (Geom::InfiniteSolutions e)
94 {
95 // We're probably dealing with parallel lines, so snapping doesn't make any sense here
96 continue; // jump to the next iterator in the for-loop
97 }
99 if (inters) {
100 Geom::Point t = line1.pointAt((*inters).ta);
101 const Geom::Coord dist = Geom::L2(t - p.getPoint());
102 if (dist < getSnapperTolerance()) {
103 // When doing a constrained snap, we're already at an intersection.
104 // This snappoint is therefore fully constrained, so there's no need
105 // to look for additional intersections; just return the snapped point
106 // and forget about the line
107 _addSnappedPoint(sc, t, dist, p.getSourceType(), p.getSourceNum());
108 // For any line that's within range, we will also look at it's "point on line" p1. For guides
109 // this point coincides with its origin; for grids this is of no use, but we cannot
110 // discern between grids and guides here
111 Geom::Coord const dist_p1 = Geom::L2(p1 - p.getPoint());
112 if (dist_p1 < getSnapperTolerance()) {
113 _addSnappedLinesOrigin(sc, p1, dist_p1, p.getSourceType(), p.getSourceNum());
114 // Only relevant for guides; grids don't have an origin per line
115 // Therefore _addSnappedLinesOrigin() will only be implemented for guides
116 }
117 }
118 }
119 }
120 }
121 }
123 // Will only be overridden in the guide-snapper class, because grid lines don't have an origin; the
124 // grid-snapper classes will use this default empty method
125 void Inkscape::LineSnapper::_addSnappedLinesOrigin(SnappedConstraints &/*sc*/, Geom::Point const /*origin*/, Geom::Coord const /*snapped_distance*/, SnapSourceType const &/*source_type*/, long /*source_num*/) const
126 {
127 }
129 /*
130 Local Variables:
131 mode:c++
132 c-file-style:"stroustrup"
133 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
134 indent-tabs-mode:nil
135 fill-column:99
136 End:
137 */
138 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :