1 #define __LINE_GEOMETRY_C__
3 /*
4 * Routines for dealing with lines (intersections, etc.)
5 *
6 * Authors:
7 * Maximilian Albert <Anhalter42@gmx.de>
8 *
9 * Copyright (C) 2007 authors
10 *
11 * Released under GNU GPL, read the file 'COPYING' for more information
12 */
14 #include "line-geometry.h"
15 #include "inkscape.h"
16 #include "desktop-style.h"
17 #include "desktop-handles.h"
18 #include "display/sp-canvas.h"
19 #include "display/sodipodi-ctrl.h"
20 //#include "display/curve.cpp"
22 namespace Box3D {
24 /**
25 * Draw a line beginning at 'start'. If is_endpoint is true, use 'vec' as the endpoint
26 * of the segment. Otherwise interpret it as the direction of the line.
27 * FIXME: Think of a better way to distinguish between the two constructors of lines.
28 */
29 Line::Line(NR::Point const &start, NR::Point const &vec, bool is_endpoint) {
30 pt = start;
31 if (is_endpoint)
32 v_dir = vec - start;
33 else
34 v_dir = vec;
35 normal = v_dir.ccw();
36 d0 = NR::dot(normal, pt);
37 }
39 Line::Line(Line const &line) {
40 pt = line.pt;
41 v_dir = line.v_dir;
42 normal = line.normal;
43 d0 = line.d0;
44 }
46 Line &Line::operator=(Line const &line) {
47 pt = line.pt;
48 v_dir = line.v_dir;
49 normal = line.normal;
50 d0 = line.d0;
52 return *this;
53 }
55 NR::Maybe<NR::Point> Line::intersect(Line const &line) {
56 NR::Coord denom = NR::dot(v_dir, line.normal);
57 g_return_val_if_fail(fabs(denom) > 1e-6, NR::Nothing());
59 NR::Coord lambda = (line.d0 - NR::dot(pt, line.normal)) / denom;
60 return pt + lambda * v_dir;
61 }
63 void Line::set_direction(NR::Point const &dir)
64 {
65 v_dir = dir;
66 normal = v_dir.ccw();
67 d0 = NR::dot(normal, pt);
68 }
70 NR::Point Line::closest_to(NR::Point const &pt)
71 {
72 /* return the intersection of this line with a perpendicular line passing through pt */
73 NR::Maybe<NR::Point> result = this->intersect(Line(pt, (this->v_dir).ccw(), false));
74 g_return_val_if_fail (result, NR::Point (0.0, 0.0));
75 return *result;
76 }
78 double Line::lambda (NR::Point const pt)
79 {
80 double sign = (NR::dot (pt - this->pt, this->v_dir) > 0) ? 1.0 : -1.0;
81 double lambda = sign * NR::L2 (pt - this->pt);
82 // FIXME: It may speed things up (but how much?) if we assume that
83 // pt lies on the line and thus skip the following test
84 NR::Point test = point_from_lambda (lambda);
85 if (!pts_coincide (pt, test)) {
86 g_warning ("Point does not lie on line.\n");
87 return 0;
88 }
89 return lambda;
90 }
92 inline static double determinant (NR::Point const &a, NR::Point const &b)
93 {
94 return (a[NR::X] * b[NR::Y] - a[NR::Y] * b[NR::X]);
95 }
97 /* The coordinates of w with respect to the basis {v1, v2} */
98 std::pair<double, double> coordinates (NR::Point const &v1, NR::Point const &v2, NR::Point const &w)
99 {
100 double det = determinant (v1, v2);;
101 if (fabs (det) < epsilon) {
102 g_warning ("Vectors do not form a basis.\n");
103 return std::make_pair (0.0, 0.0);
104 }
106 double lambda1 = determinant (w, v2) / det;
107 double lambda2 = determinant (v1, w) / det;
108 return std::make_pair (lambda1, lambda2);
109 }
111 /* whether w lies inside the sector spanned by v1 and v2 */
112 bool lies_in_sector (NR::Point const &v1, NR::Point const &v2, NR::Point const &w)
113 {
114 std::pair<double, double> coords = coordinates (v1, v2, w);
115 return (coords.first >= 0 and coords.second >= 0);
116 }
118 static double pos_angle (NR::Point A, NR::Point B)
119 {
120 return fabs (NR::atan2 (A) - NR::atan2 (B));
121 }
123 /*
124 * Returns the two corners of the quadrangle A, B, C, D spanning the edge that is hit by a semiline
125 * starting at pt and going into direction dir.
126 * If none of the sides is hit, it returns a pair containing two identical points.
127 */
128 std::pair<NR::Point, NR::Point>
129 side_of_intersection (NR::Point const &A, NR::Point const &B, NR::Point const &C, NR::Point const &D,
130 NR::Point const &pt, NR::Point const &dir)
131 {
132 NR::Point dir_A (A - pt);
133 NR::Point dir_B (B - pt);
134 NR::Point dir_C (C - pt);
135 NR::Point dir_D (D - pt);
137 std::pair<NR::Point, NR::Point> result;
138 double angle = -1;
139 double tmp_angle;
141 if (lies_in_sector (dir_A, dir_B, dir)) {
142 result = std::make_pair (A, B);
143 angle = pos_angle (dir_A, dir_B);
144 }
145 if (lies_in_sector (dir_B, dir_C, dir)) {
146 tmp_angle = pos_angle (dir_B, dir_C);
147 if (tmp_angle > angle) {
148 angle = tmp_angle;
149 result = std::make_pair (B, C);
150 }
151 }
152 if (lies_in_sector (dir_C, dir_D, dir)) {
153 tmp_angle = pos_angle (dir_C, dir_D);
154 if (tmp_angle > angle) {
155 angle = tmp_angle;
156 result = std::make_pair (C, D);
157 }
158 }
159 if (lies_in_sector (dir_D, dir_A, dir)) {
160 tmp_angle = pos_angle (dir_D, dir_A);
161 if (tmp_angle > angle) {
162 angle = tmp_angle;
163 result = std::make_pair (D, A);
164 }
165 }
166 if (angle == -1) {
167 // no intersection found; return a pair containing two identical points
168 return std::make_pair (A, A);
169 } else {
170 return result;
171 }
172 }
174 double cross_ratio (NR::Point const &A, NR::Point const &B, NR::Point const &C, NR::Point const &D)
175 {
176 Line line (A, D);
177 double lambda_A = line.lambda (A);
178 double lambda_B = line.lambda (B);
179 double lambda_C = line.lambda (C);
180 double lambda_D = line.lambda (D);
182 if (fabs (lambda_D - lambda_A) < epsilon || fabs (lambda_C - lambda_B) < epsilon) {
183 // FIXME: What should we return if the cross ratio can't be computed?
184 return 0;
185 //return NR_HUGE;
186 }
187 return (((lambda_C - lambda_A) / (lambda_D - lambda_A)) * ((lambda_D - lambda_B) / (lambda_C - lambda_B)));
188 }
190 double cross_ratio (VanishingPoint const &V, NR::Point const &B, NR::Point const &C, NR::Point const &D)
191 {
192 if (V.is_finite()) {
193 return cross_ratio (V.get_pos(), B, C, D);
194 } else {
195 Line line (B, D);
196 double lambda_B = line.lambda (B);
197 double lambda_C = line.lambda (C);
198 double lambda_D = line.lambda (D);
200 if (fabs (lambda_C - lambda_B) < epsilon) {
201 // FIXME: What should we return if the cross ratio can't be computed?
202 return 0;
203 //return NR_HUGE;
204 }
205 return (lambda_D - lambda_B) / (lambda_C - lambda_B);
206 }
207 }
209 NR::Point fourth_pt_with_given_cross_ratio (NR::Point const &A, NR::Point const &C, NR::Point const &D, double gamma)
210 {
211 Line line (A, D);
212 double lambda_A = line.lambda (A);
213 double lambda_C = line.lambda (C);
214 double lambda_D = line.lambda (D);
216 double beta = (lambda_C - lambda_A) / (lambda_D - lambda_A);
217 if (fabs (beta - gamma) < epsilon) {
218 // FIXME: How to handle the case when the point can't be computed?
219 // g_warning ("Cannot compute point with given cross ratio.\n");
220 return NR::Point (0.0, 0.0);
221 }
222 return line.point_from_lambda ((beta * lambda_D - gamma * lambda_C) / (beta - gamma));
223 }
225 void create_canvas_point(NR::Point const &pos, double size, guint32 rgba)
226 {
227 SPDesktop *desktop = inkscape_active_desktop();
228 SPCanvasItem * canvas_pt = sp_canvas_item_new(sp_desktop_controls(desktop), SP_TYPE_CTRL,
229 "size", size,
230 "filled", 1,
231 "fill_color", rgba,
232 "stroked", 1,
233 "stroke_color", 0x000000ff,
234 NULL);
235 SP_CTRL(canvas_pt)->moveto(pos);
236 }
238 void create_canvas_line(NR::Point const &p1, NR::Point const &p2, guint32 rgba)
239 {
240 SPDesktop *desktop = inkscape_active_desktop();
241 SPCanvasItem *line = sp_canvas_item_new(sp_desktop_controls(desktop),
242 SP_TYPE_CTRLLINE, NULL);
243 sp_ctrlline_set_coords(SP_CTRLLINE(line), p1, p2);
244 sp_ctrlline_set_rgba32 (SP_CTRLLINE(line), rgba);
245 sp_canvas_item_show (line);
246 }
248 } // namespace Box3D
250 /*
251 Local Variables:
252 mode:c++
253 c-file-style:"stroustrup"
254 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
255 indent-tabs-mode:nil
256 fill-column:99
257 End:
258 */
259 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :