Code

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