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::SnapPreferences::PointType const &t,
26 Inkscape::SnapCandidatePoint const &p,
27 Geom::OptRect const &/*bbox_to_snap*/,
28 std::vector<SPItem const *> const */*it*/,
29 std::vector<Inkscape::SnapCandidatePoint> */*unselected_nodes*/) const
30 {
31 if (!(_snap_enabled && _snapmanager->snapprefs.getSnapFrom(t)) ) {
32 return;
33 }
35 /* Get the lines that we will try to snap to */
36 const LineList lines = _getSnapLines(p.getPoint());
38 for (LineList::const_iterator i = lines.begin(); i != lines.end(); i++) {
39 Geom::Point const p1 = i->second; // point at guide/grid line
40 Geom::Point const p2 = p1 + Geom::rot90(i->first); // 2nd point at guide/grid line
41 // std::cout << " line through " << i->second << " with normal " << i->first;
42 g_assert(i->first != Geom::Point(0,0)); // we cannot project on an linesegment of zero length
44 Geom::Point const p_proj = Geom::projection(p.getPoint(), Geom::Line(p1, p2));
45 Geom::Coord const dist = Geom::L2(p_proj - p.getPoint());
46 //Store any line that's within snapping range
47 if (dist < getSnapperTolerance()) {
48 _addSnappedLine(sc, p_proj, dist, p.getSourceType(), p.getSourceNum(), i->first, i->second);
49 // For any line that's within range, we will also look at it's "point on line" p1. For guides
50 // this point coincides with its origin; for grids this is of no use, but we cannot
51 // discern between grids and guides here
52 Geom::Coord const dist_p1 = Geom::L2(p1 - p.getPoint());
53 if (dist_p1 < getSnapperTolerance()) {
54 _addSnappedLinesOrigin(sc, p1, dist_p1, p.getSourceType(), p.getSourceNum());
55 // Only relevant for guides; grids don't have an origin per line
56 // Therefore _addSnappedLinesOrigin() will only be implemented for guides
57 }
58 // std::cout << " -> distance = " << dist;
59 }
60 // std::cout << std::endl;
61 }
62 }
64 void Inkscape::LineSnapper::constrainedSnap(SnappedConstraints &sc,
65 Inkscape::SnapPreferences::PointType const &t,
66 Inkscape::SnapCandidatePoint const &p,
67 Geom::OptRect const &/*bbox_to_snap*/,
68 ConstraintLine const &c,
69 std::vector<SPItem const *> const */*it*/) const
71 {
72 if (_snap_enabled == false || _snapmanager->snapprefs.getSnapFrom(t) == false) {
73 return;
74 }
76 /* Get the lines that we will try to snap to */
77 const LineList lines = _getSnapLines(p.getPoint());
79 for (LineList::const_iterator i = lines.begin(); i != lines.end(); i++) {
80 if (Geom::L2(c.getDirection()) > 0) { // Can't do a constrained snap without a constraint
81 // constraint line
82 Geom::Point const point_on_line = c.hasPoint() ? c.getPoint() : p.getPoint();
83 Geom::Line line1(point_on_line, point_on_line + c.getDirection());
85 // grid/guide line
86 Geom::Point const p1 = i->second; // point at guide/grid line
87 Geom::Point const p2 = p1 + Geom::rot90(i->first); // 2nd point at guide/grid line
88 Geom::Line line2(p1, p2);
90 Geom::OptCrossing inters = Geom::OptCrossing(); // empty by default
91 try
92 {
93 inters = Geom::intersection(line1, line2);
94 }
95 catch (Geom::InfiniteSolutions e)
96 {
97 // We're probably dealing with parallel lines, so snapping doesn't make any sense here
98 continue; // jump to the next iterator in the for-loop
99 }
101 if (inters) {
102 Geom::Point t = line1.pointAt((*inters).ta);
103 const Geom::Coord dist = Geom::L2(t - p.getPoint());
104 if (dist < getSnapperTolerance()) {
105 // When doing a constrained snap, we're already at an intersection.
106 // This snappoint is therefore fully constrained, so there's no need
107 // to look for additional intersections; just return the snapped point
108 // and forget about the line
109 _addSnappedPoint(sc, t, dist, p.getSourceType(), p.getSourceNum());
110 // For any line that's within range, we will also look at it's "point on line" p1. For guides
111 // this point coincides with its origin; for grids this is of no use, but we cannot
112 // discern between grids and guides here
113 Geom::Coord const dist_p1 = Geom::L2(p1 - p.getPoint());
114 if (dist_p1 < getSnapperTolerance()) {
115 _addSnappedLinesOrigin(sc, p1, dist_p1, p.getSourceType(), p.getSourceNum());
116 // Only relevant for guides; grids don't have an origin per line
117 // Therefore _addSnappedLinesOrigin() will only be implemented for guides
118 }
119 }
120 }
121 }
122 }
123 }
125 // Will only be overridden in the guide-snapper class, because grid lines don't have an origin; the
126 // grid-snapper classes will use this default empty method
127 void Inkscape::LineSnapper::_addSnappedLinesOrigin(SnappedConstraints &/*sc*/, Geom::Point const /*origin*/, Geom::Coord const /*snapped_distance*/, SnapSourceType const &/*source_type*/, long /*source_num*/) const
128 {
129 }
131 /*
132 Local Variables:
133 mode:c++
134 c-file-style:"stroustrup"
135 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
136 indent-tabs-mode:nil
137 fill-column:99
138 End:
139 */
140 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :