1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * LineSnapper class.
4  *
5  * Authors:
6  *   Diederik van Lierop <mail@diedenrezi.nl>
7  *   And others...
8  *
9  * Copyright (C) 1999-2012 Authors
10  *
11  * Released under GNU GPL v2+, read the file 'COPYING' for more information.
12  */
13 
14 #include <2geom/line.h>
15 
16 #include "line-snapper.h"
17 #include "snap.h"
18 
LineSnapper(SnapManager * sm,Geom::Coord const d)19 Inkscape::LineSnapper::LineSnapper(SnapManager *sm, Geom::Coord const d) : Snapper(sm, d)
20 {
21 }
22 
freeSnap(IntermSnapResults & isr,Inkscape::SnapCandidatePoint const & p,Geom::OptRect const &,std::vector<SPItem const * > const *,std::vector<Inkscape::SnapCandidatePoint> *) const23 void Inkscape::LineSnapper::freeSnap(IntermSnapResults &isr,
24                                                     Inkscape::SnapCandidatePoint const &p,
25                                                     Geom::OptRect const &/*bbox_to_snap*/,
26                                                     std::vector<SPItem const *> const */*it*/,
27                                                     std::vector<Inkscape::SnapCandidatePoint> */*unselected_nodes*/) const
28 {
29     if (!(_snap_enabled && _snapmanager->snapprefs.isSourceSnappable(p.getSourceType())) ) {
30         return;
31     }
32 
33     /* Get the lines that we will try to snap to */
34     const LineList lines = _getSnapLines(p.getPoint());
35 
36     for (const auto & line : lines) {
37         Geom::Point const p1 = line.second; // point at guide/grid line
38         Geom::Point const p2 = p1 + Geom::rot90(line.first); // 2nd point at guide/grid line
39         assert(line.first != Geom::Point(0,0)); // we cannot project on an linesegment of zero length
40 
41         Geom::Point const p_proj = Geom::projection(p.getPoint(), Geom::Line(p1, p2));
42         Geom::Coord const dist = Geom::L2(p_proj - p.getPoint());
43         //Store any line that's within snapping range
44         if (dist < getSnapperTolerance()) {
45             _addSnappedLine(isr, p_proj, dist, p.getSourceType(), p.getSourceNum(), line.first, line.second);
46             // For any line that's within range, we will also look at it's "point on line" p1. For guides
47             // this point coincides with its origin; for grids this is of no use, but we cannot
48             // discern between grids and guides here
49             Geom::Coord const dist_p1 = Geom::L2(p1 - p.getPoint());
50             if (dist_p1 < getSnapperTolerance()) {
51                 _addSnappedLinesOrigin(isr, p1, dist_p1, p.getSourceType(), p.getSourceNum(), false);
52                 // Only relevant for guides; grids don't have an origin per line
53                 // Therefore _addSnappedLinesOrigin() will only be implemented for guides
54             }
55 
56             // Here we will try to snap either tangentially or perpendicularly to a grid/guide line
57             // For this we need to know where the origin is located of the line that is currently being rotated,
58             std::vector<std::pair<Geom::Point, bool> > const origins_and_vectors = p.getOriginsAndVectors();
59             // Now we will iterate over all the origins and vectors and see which of these will get use a tangential or perpendicular snap
60             for (const auto & origins_and_vector : origins_and_vectors) {
61                 if (origins_and_vector.second) { // if "second" is true then "first" is a vector, otherwise it's a point
62                     // When snapping a line with a constant vector (constant direction) to a guide or grid line,
63                     // then either all points will be perpendicular/tangential or none at all. This is not very useful
64                     continue;
65                 }
66 
67                 //Geom::Point origin_doc = _snapmanager->getDesktop()->dt2doc((*it_origin_or_vector).first); // "first" contains a Geom::Point, denoting either a point
68                 Geom::Point origin = origins_and_vector.first; // "first" contains a Geom::Point, denoting either a point
69 
70                 // We won't try to snap tangentially; a line being tangential to another line can be achieved by snapping both its endpoints
71                 // individually to the other line. There's no need to have an explicit tangential snap here, that would be redundant
72 
73                 if (_snapmanager->snapprefs.getSnapPerp()) { // Find the point that leads to a perpendicular snap
74                     Geom::Point const origin_proj = Geom::projection(origin, Geom::Line(p1, p2));
75                     Geom::Coord dist = Geom::L2(origin_proj - p.getPoint());
76                     if (dist < getSnapperTolerance()) {
77                         _addSnappedLinePerpendicularly(isr, origin_proj, dist, p.getSourceType(), p.getSourceNum(), false);
78                     }
79                 }
80             }
81         }
82     }
83 }
84 
constrainedSnap(IntermSnapResults & isr,Inkscape::SnapCandidatePoint const & p,Geom::OptRect const &,SnapConstraint const & c,std::vector<SPItem const * > const *,std::vector<SnapCandidatePoint> *) const85 void Inkscape::LineSnapper::constrainedSnap(IntermSnapResults &isr,
86                                                Inkscape::SnapCandidatePoint const &p,
87                                                Geom::OptRect const &/*bbox_to_snap*/,
88                                                SnapConstraint const &c,
89                                                std::vector<SPItem const *> const */*it*/,
90                                                std::vector<SnapCandidatePoint> */*unselected_nodes*/) const
91 
92 {
93     if (_snap_enabled == false || _snapmanager->snapprefs.isSourceSnappable(p.getSourceType()) == false) {
94         return;
95     }
96 
97     // project the mouse pointer onto the constraint. Only the projected point will be considered for snapping
98     Geom::Point pp = c.projection(p.getPoint());
99 
100     /* Get the lines that we will try to snap to */
101     const LineList lines = _getSnapLines(pp);
102 
103     for (const auto & line : lines) {
104         Geom::Point const point_on_line = c.hasPoint() ? c.getPoint() : pp;
105         Geom::Line gridguide_line(line.second, line.second + Geom::rot90(line.first));
106 
107         if (c.isCircular()) {
108             // Find the intersections between the line and the circular constraint
109             // First, project the origin of the circle onto the line
110             Geom::Point const origin = c.getPoint();
111             Geom::Point const p_proj = Geom::projection(origin, gridguide_line);
112             Geom::Coord dist = Geom::L2(p_proj - origin); // distance from circle origin to constraint line
113             Geom::Coord radius = c.getRadius();
114             if (dist == radius) {
115                 // Only one point of intersection;
116                 _addSnappedPoint(isr, p_proj, Geom::L2(pp - p_proj), p.getSourceType(), p.getSourceNum(), true);
117             } else if (dist < radius) {
118                 // Two points of intersection, symmetrical with respect to the projected point
119                 // Calculate half the length of the linesegment between the two points of intersection
120                 Geom::Coord l = sqrt(radius*radius - dist*dist);
121                 Geom::Coord d = Geom::L2(gridguide_line.versor()); // length of versor, needed to normalize the versor
122                 if (d > 0) {
123                     Geom::Point v = l*gridguide_line.versor()/d;
124                     _addSnappedPoint(isr, p_proj + v, Geom::L2(p.getPoint() - (p_proj + v)), p.getSourceType(), p.getSourceNum(), true);
125                     _addSnappedPoint(isr, p_proj - v, Geom::L2(p.getPoint() - (p_proj - v)), p.getSourceType(), p.getSourceNum(), true);
126                 }
127             }
128         } else {
129             // Find the intersections between the line and the linear constraint
130             Geom::Line constraint_line(point_on_line, point_on_line + c.getDirection());
131             Geom::OptCrossing inters = Geom::OptCrossing(); // empty by default
132             try
133             {
134                 inters = Geom::intersection(constraint_line, gridguide_line);
135             }
136             catch (Geom::InfiniteSolutions &e)
137             {
138                 // We're probably dealing with parallel lines, so snapping doesn't make any sense here
139                 continue; // jump to the next iterator in the for-loop
140             }
141 
142             if (inters) {
143                 Geom::Point t = constraint_line.pointAt((*inters).ta);
144                 const Geom::Coord dist = Geom::L2(t - p.getPoint());
145                 if (dist < getSnapperTolerance()) {
146                     // When doing a constrained snap, we're already at an intersection.
147                     // This snappoint is therefore fully constrained, so there's no need
148                     // to look for additional intersections; just return the snapped point
149                     // and forget about the line
150                     _addSnappedPoint(isr, t, dist, p.getSourceType(), p.getSourceNum(), true);
151                 }
152             }
153         }
154     }
155 }
156 
157 // Will only be overridden in the guide-snapper class, because grid lines don't have an origin; the
158 // grid-snapper classes will use this default empty method
_addSnappedLinesOrigin(IntermSnapResults &,Geom::Point const &,Geom::Coord const &,SnapSourceType const &,long,bool) const159 void Inkscape::LineSnapper::_addSnappedLinesOrigin(IntermSnapResults &/*isr*/, Geom::Point const &/*origin*/, Geom::Coord const &/*snapped_distance*/, SnapSourceType const &/*source_type*/, long /*source_num*/, bool /*constrained_snap*/) const
160 {
161 }
162 
163 /*
164   Local Variables:
165   mode:c++
166   c-file-style:"stroustrup"
167   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
168   indent-tabs-mode:nil
169   fill-column:99
170   End:
171 */
172 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :
173