Inkscape
Vector Graphics Editor
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Modules Pages Concepts
line-geometry.cpp
Go to the documentation of this file.
1// SPDX-License-Identifier: GPL-2.0-or-later
2/*
3 * Routines for dealing with lines (intersections, etc.)
4 *
5 * Authors:
6 * Maximilian Albert <Anhalter42@gmx.de>
7 *
8 * Copyright (C) 2007 authors
9 *
10 * Released under GNU GPL v2+, read the file 'COPYING' for more information.
11 */
12
13#include "line-geometry.h"
14#include "desktop.h"
15
16namespace Box3D {
17
23Line::Line(Geom::Point const &start, Geom::Point const &vec, bool is_endpoint):
24 pt(start)
25{
26 if (is_endpoint)
27 v_dir = vec - start;
28 else
29 v_dir = vec;
30 normal = v_dir.ccw();
32}
33
34Line::Line(Line const &line)
35= default;
36
37Line &Line::operator=(Line const &line) = default;
38
39std::optional<Geom::Point> Line::intersect(Line const &line) {
40 Geom::Coord denom = Geom::dot(v_dir, line.normal);
41 std::optional<Geom::Point> no_point;
42 if (fabs(denom) < 1e-6)
43 return no_point;
44
45 Geom::Coord lambda = (line.d0 - Geom::dot(pt, line.normal)) / denom;
46 return pt + lambda * v_dir;
47}
48
50{
51 v_dir = dir;
52 normal = v_dir.ccw();
54}
55
57{
58 /* return the intersection of this line with a perpendicular line passing through pt */
59 std::optional<Geom::Point> result = this->intersect(Line(pt, (this->v_dir).ccw(), false));
60 g_return_val_if_fail (result, Geom::Point (0.0, 0.0));
61 return *result;
62}
63
64double Line::lambda (Geom::Point const pt)
65{
66 double sign = (Geom::dot (pt - this->pt, this->v_dir) > 0) ? 1.0 : -1.0;
67 double lambda = sign * Geom::L2 (pt - this->pt);
68 // FIXME: It may speed things up (but how much?) if we assume that
69 // pt lies on the line and thus skip the following test
71 if (!pts_coincide (pt, test)) {
72 g_warning ("Point does not lie on line.\n");
73 return 0;
74 }
75 return lambda;
76}
77
78/* The coordinates of w with respect to the basis {v1, v2} */
79std::pair<double, double> coordinates (Geom::Point const &v1, Geom::Point const &v2, Geom::Point const &w)
80{
81 double det = determinant (v1, v2);;
82 if (fabs (det) < epsilon) {
83 // vectors are not linearly independent; we indicate this in the return value(s)
84 return std::make_pair (HUGE_VAL, HUGE_VAL);
85 }
86
87 double lambda1 = determinant (w, v2) / det;
88 double lambda2 = determinant (v1, w) / det;
89 return std::make_pair (lambda1, lambda2);
90}
91
92/* whether w lies inside the sector spanned by v1 and v2 */
93bool lies_in_sector (Geom::Point const &v1, Geom::Point const &v2, Geom::Point const &w)
94{
95 std::pair<double, double> coords = coordinates (v1, v2, w);
96 if (coords.first == HUGE_VAL) {
97 // catch the case that the vectors are not linearly independent
98 // FIXME: Can we assume that it's safe to return true if the vectors point in different directions?
99 return (Geom::dot (v1, v2) < 0);
100 }
101 return (coords.first >= 0 && coords.second >= 0);
102}
103
104bool lies_in_quadrangle (Geom::Point const &A, Geom::Point const &B, Geom::Point const &C, Geom::Point const &D, Geom::Point const &pt)
105{
106 return (lies_in_sector (D - A, B - A, pt - A) && lies_in_sector (D - C, B - C, pt - C));
107}
108
110{
111 return fabs (Geom::atan2 (v) - Geom::atan2 (w));
112}
113
114/*
115 * Returns the two corners of the quadrangle A, B, C, D spanning the edge that is hit by a semiline
116 * starting at pt and going into direction dir.
117 * If none of the sides is hit, it returns a pair containing two identical points.
118 */
119std::pair<Geom::Point, Geom::Point>
121 Geom::Point const &pt, Geom::Point const &dir)
122{
123 Geom::Point dir_A (A - pt);
124 Geom::Point dir_B (B - pt);
125 Geom::Point dir_C (C - pt);
126 Geom::Point dir_D (D - pt);
127
128 std::pair<Geom::Point, Geom::Point> result;
129 double angle = -1;
130 double tmp_angle;
131
132 if (lies_in_sector (dir_A, dir_B, dir)) {
133 result = std::make_pair (A, B);
134 angle = pos_angle (dir_A, dir_B);
135 }
136 if (lies_in_sector (dir_B, dir_C, dir)) {
137 tmp_angle = pos_angle (dir_B, dir_C);
138 if (tmp_angle > angle) {
139 angle = tmp_angle;
140 result = std::make_pair (B, C);
141 }
142 }
143 if (lies_in_sector (dir_C, dir_D, dir)) {
144 tmp_angle = pos_angle (dir_C, dir_D);
145 if (tmp_angle > angle) {
146 angle = tmp_angle;
147 result = std::make_pair (C, D);
148 }
149 }
150 if (lies_in_sector (dir_D, dir_A, dir)) {
151 tmp_angle = pos_angle (dir_D, dir_A);
152 if (tmp_angle > angle) {
153 angle = tmp_angle;
154 result = std::make_pair (D, A);
155 }
156 }
157 if (angle == -1) {
158 // no intersection found; return a pair containing two identical points
159 return std::make_pair (A, A);
160 } else {
161 return result;
162 }
163}
164
166{
167 auto vb = desktop->get_display_area();
168
169 std::pair <Geom::Point, Geom::Point> e = side_of_intersection (vb.corner(0), vb.corner(1), vb.corner(2), vb.corner(3), this->pt, this->v_dir);
170 if (e.first == e.second) {
171 // perspective line lies outside the canvas
172 return std::optional<Geom::Point>();
173 }
174
175 Line line (e.first, e.second);
176 return this->intersect (line);
177}
178
179} // namespace Box3D
180
181/*
182 Local Variables:
183 mode:c++
184 c-file-style:"stroustrup"
185 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
186 indent-tabs-mode:nil
187 fill-column:99
188 End:
189*/
190// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :
int test()
Definition 2junctions.cpp:5
Geom::Coord d0
Line(Geom::Point const &start, Geom::Point const &vec, bool is_endpoint=true)
Draw a line beginning at 'start'.
Geom::Point normal
Geom::Point point_from_lambda(double const lambda)
Line & operator=(Line const &line)
static bool pts_coincide(Geom::Point const pt1, Geom::Point const pt2)
Geom::Point v_dir
Geom::Point closest_to(Geom::Point const &pt)
void set_direction(Geom::Point const &dir)
virtual std::optional< Geom::Point > intersect(Line const &line)
double lambda(Geom::Point const pt)
std::optional< Geom::Point > intersection_with_viewbox(SPDesktop *desktop)
Geom::Point pt
Two-dimensional point that doubles as a vector.
Definition point.h:66
constexpr Point ccw() const
Return a point like this point but rotated -90 degrees.
Definition point.h:130
To do: update description of desktop.
Definition desktop.h:149
Geom::Parallelogram get_display_area() const
Return canvas viewbox in desktop coordinates.
Definition desktop.cpp:521
const double w
Definition conic-4.cpp:19
static T det(T a, T b, T c, T d)
Definition conic-5.cpp:95
Css & result
Editable view implementation.
double Coord
Floating point type used to store coordinates.
Definition coord.h:76
Geom::Point start
Perspective line for 3D perspectives.
static double pos_angle(Geom::Point v, Geom::Point w)
std::pair< double, double > coordinates(Geom::Point const &v1, Geom::Point const &v2, Geom::Point const &w)
double determinant(Geom::Point const &a, Geom::Point const &b)
const double epsilon
Definition axis-manip.h:56
bool lies_in_sector(Geom::Point const &v1, Geom::Point const &v2, Geom::Point const &w)
std::pair< Geom::Point, Geom::Point > side_of_intersection(Geom::Point const &A, Geom::Point const &B, Geom::Point const &C, Geom::Point const &D, Geom::Point const &pt, Geom::Point const &dir)
bool lies_in_quadrangle(Geom::Point const &A, Geom::Point const &B, Geom::Point const &C, Geom::Point const &D, Geom::Point const &pt)
double atan2(Point const &p)
SBasis L2(D2< SBasis > const &a, unsigned k)
Definition d2-sbasis.cpp:42
T dot(D2< T > const &a, D2< T > const &b)
Definition d2.h:355
static double sign(double const x)
Returns -1 or 1 according to the sign of x.
SPDesktop * desktop