Code

Rewrite of z-order code for 3D boxes, first stage (hopefully this is finally the...
[inkscape.git] / src / line-geometry.cpp
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     NR::Maybe<NR::Point> no_point = NR::Nothing();
58     g_return_val_if_fail(fabs(denom) > 1e-6, no_point );
60     NR::Coord lambda = (line.d0 - NR::dot(pt, line.normal)) / denom;
61     return pt + lambda * v_dir;
62 }
64 void Line::set_direction(NR::Point const &dir)
65 {
66     v_dir = dir;
67     normal = v_dir.ccw();
68     d0 = NR::dot(normal, pt);
69 }
71 NR::Point Line::closest_to(NR::Point const &pt)
72 {
73         /* return the intersection of this line with a perpendicular line passing through pt */ 
74     NR::Maybe<NR::Point> result = this->intersect(Line(pt, (this->v_dir).ccw(), false));
75     g_return_val_if_fail (result, NR::Point (0.0, 0.0));
76     return *result;
77 }
79 double Line::lambda (NR::Point const pt)
80 {
81     double sign = (NR::dot (pt - this->pt, this->v_dir) > 0) ? 1.0 : -1.0;
82     double lambda = sign * NR::L2 (pt - this->pt);
83     // FIXME: It may speed things up (but how much?) if we assume that
84     //        pt lies on the line and thus skip the following test
85     NR::Point test = point_from_lambda (lambda);
86     if (!pts_coincide (pt, test)) {
87         g_warning ("Point does not lie on line.\n");
88         return 0;
89     }
90     return lambda;
91 }
93 inline static double determinant (NR::Point const &a, NR::Point const &b)
94 {
95     return (a[NR::X] * b[NR::Y] - a[NR::Y] * b[NR::X]);
96 }
98 /* The coordinates of w with respect to the basis {v1, v2} */
99 std::pair<double, double> coordinates (NR::Point const &v1, NR::Point const &v2, NR::Point const &w)
101     double det = determinant (v1, v2);;
102     if (fabs (det) < epsilon) {
103         // vectors are not linearly independent; we indicate this in the return value(s)
104         return std::make_pair (HUGE_VAL, HUGE_VAL);
105     }
107     double lambda1 = determinant (w, v2) / det;
108     double lambda2 = determinant (v1, w) / det;
109     return std::make_pair (lambda1, lambda2);
112 /* whether w lies inside the sector spanned by v1 and v2 */
113 bool lies_in_sector (NR::Point const &v1, NR::Point const &v2, NR::Point const &w)
115     std::pair<double, double> coords = coordinates (v1, v2, w);
116     if (coords.first == HUGE_VAL) {
117         // catch the case that the vectors are not linearly independent
118         // FIXME: Can we assume that it's safe to return true if the vectors point in different directions?
119         return (NR::dot (v1, v2) < 0);
120     }
121     return (coords.first >= 0 and coords.second >= 0);
124 static double pos_angle (NR::Point A, NR::Point B)
126     return fabs (NR::atan2 (A) - NR::atan2 (B));
129 /*
130  * Returns the two corners of the quadrangle A, B, C, D spanning the edge that is hit by a semiline
131  * starting at pt and going into direction dir.
132  * If none of the sides is hit, it returns a pair containing two identical points.
133  */
134 std::pair<NR::Point, NR::Point>
135 side_of_intersection (NR::Point const &A, NR::Point const &B, NR::Point const &C, NR::Point const &D,
136                       NR::Point const &pt, NR::Point const &dir)
138     NR::Point dir_A (A - pt);
139     NR::Point dir_B (B - pt);
140     NR::Point dir_C (C - pt);
141     NR::Point dir_D (D - pt);
143     std::pair<NR::Point, NR::Point> result;
144     double angle = -1;
145     double tmp_angle;
147     if (lies_in_sector (dir_A, dir_B, dir)) {
148         result = std::make_pair (A, B);
149         angle = pos_angle (dir_A, dir_B);
150     }
151     if (lies_in_sector (dir_B, dir_C, dir)) {
152         tmp_angle = pos_angle (dir_B, dir_C);
153         if (tmp_angle > angle) {
154             angle = tmp_angle;
155             result = std::make_pair (B, C);
156         }
157     }
158     if (lies_in_sector (dir_C, dir_D, dir)) {
159         tmp_angle = pos_angle (dir_C, dir_D);
160         if (tmp_angle > angle) {
161             angle = tmp_angle;
162             result = std::make_pair (C, D);
163         }
164     }
165     if (lies_in_sector (dir_D, dir_A, dir)) {
166         tmp_angle = pos_angle (dir_D, dir_A);
167         if (tmp_angle > angle) {
168             angle = tmp_angle;
169             result = std::make_pair (D, A);
170         }
171     }
172     if (angle == -1) {
173         // no intersection found; return a pair containing two identical points
174         return std::make_pair (A, A);
175     } else {
176         return result;
177     }
180 double cross_ratio (NR::Point const &A, NR::Point const &B, NR::Point const &C, NR::Point const &D)
182     Line line (A, D);
183     double lambda_A = line.lambda (A);
184     double lambda_B = line.lambda (B);
185     double lambda_C = line.lambda (C);
186     double lambda_D = line.lambda (D);
188     if (fabs (lambda_D - lambda_A) < epsilon || fabs (lambda_C - lambda_B) < epsilon) {
189         // FIXME: What should we return if the cross ratio can't be computed?
190         return 0;
191         //return NR_HUGE;
192     }
193     return (((lambda_C - lambda_A) / (lambda_D - lambda_A)) * ((lambda_D - lambda_B) / (lambda_C - lambda_B)));
196 double cross_ratio (VanishingPoint const &V, NR::Point const &B, NR::Point const &C, NR::Point const &D)
198     if (V.is_finite()) {
199         return cross_ratio (V.get_pos(), B, C, D);
200     } else {
201         Line line (B, D);
202         double lambda_B = line.lambda (B);
203         double lambda_C = line.lambda (C);
204         double lambda_D = line.lambda (D);
206         if (fabs (lambda_C - lambda_B) < epsilon) {
207             // FIXME: What should we return if the cross ratio can't be computed?
208             return 0;
209             //return NR_HUGE;
210         }
211         return (lambda_D - lambda_B) / (lambda_C - lambda_B);
212     }
215 NR::Point fourth_pt_with_given_cross_ratio (NR::Point const &A, NR::Point const &C, NR::Point const &D, double gamma)
217     Line line (A, D);
218     double lambda_A = line.lambda (A);
219     double lambda_C = line.lambda (C);
220     double lambda_D = line.lambda (D);
222     double beta = (lambda_C - lambda_A) / (lambda_D - lambda_A);
223     if (fabs (beta - gamma) < epsilon) {
224         // FIXME: How to handle the case when the point can't be computed?
225         // g_warning ("Cannot compute point with given cross ratio.\n");
226         return NR::Point (0.0, 0.0);
227     }
228     return line.point_from_lambda ((beta * lambda_D - gamma * lambda_C) / (beta - gamma));
231 void create_canvas_point(NR::Point const &pos, double size, guint32 rgba)
233     SPDesktop *desktop = inkscape_active_desktop();
234     SPCanvasItem * canvas_pt = sp_canvas_item_new(sp_desktop_controls(desktop), SP_TYPE_CTRL,
235                           "size", size,
236                           "filled", 1,
237                           "fill_color", rgba,
238                           "stroked", 1,
239                           "stroke_color", 0x000000ff,
240                           NULL);
241     SP_CTRL(canvas_pt)->moveto(pos);
244 void create_canvas_line(NR::Point const &p1, NR::Point const &p2, guint32 rgba)
246     SPDesktop *desktop = inkscape_active_desktop();
247     SPCanvasItem *line = sp_canvas_item_new(sp_desktop_controls(desktop),
248                                                             SP_TYPE_CTRLLINE, NULL);
249     sp_ctrlline_set_coords(SP_CTRLLINE(line), p1, p2);
250     sp_ctrlline_set_rgba32 (SP_CTRLLINE(line), rgba);
251     sp_canvas_item_show (line);
254 } // namespace Box3D 
255  
256 /*
257   Local Variables:
258   mode:c++
259   c-file-style:"stroustrup"
260   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
261   indent-tabs-mode:nil
262   fill-column:99
263   End:
264 */
265 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :