1 /*
2 * Routines for dealing with lines (intersections, etc.)
3 *
4 * Authors:
5 * Maximilian Albert <Anhalter42@gmx.de>
6 *
7 * Copyright (C) 2007 authors
8 *
9 * Released under GNU GPL, read the file 'COPYING' for more information
10 */
12 #ifndef SEEN_LINE_GEOMETRY_H
13 #define SEEN_LINE_GEOMETRY_H
15 #include "libnr/nr-point.h"
16 #include "libnr/nr-point-fns.h"
17 #include "libnr/nr-maybe.h"
18 #include "glib.h"
19 #include "display/sp-ctrlline.h"
20 #include "axis-manip.h" // FIXME: This is only for Box3D::epsilon; move that to a better location
22 #include "document.h"
23 #include "ui/view/view.h"
25 namespace Box3D {
27 class Line {
28 public:
29 Line(NR::Point const &start, NR::Point const &vec, bool is_endpoint = true);
30 Line(Line const &line);
31 virtual ~Line() {}
32 Line &operator=(Line const &line);
33 virtual NR::Maybe<NR::Point> intersect(Line const &line);
34 inline NR::Point direction () { return v_dir; }
36 NR::Point closest_to(NR::Point const &pt); // returns the point on the line closest to pt
38 friend inline std::ostream &operator<< (std::ostream &out_file, const Line &in_line);
39 NR::Maybe<NR::Point> intersection_with_viewbox (SPDesktop *desktop);
40 inline bool lie_on_same_side (NR::Point const &A, NR::Point const &B) {
41 /* If A is a point in the plane and n is the normal vector of the line then
42 the sign of dot(A, n) specifies the half-plane in which A lies.
43 Thus A and B lie on the same side if the dot products have equal sign. */
44 return ((NR::dot(A, normal) - d0) * (NR::dot(B, normal) - d0)) > 0;
45 }
47 double lambda (NR::Point const pt);
48 inline NR::Point point_from_lambda (double const lambda) {
49 return (pt + lambda * NR::unit_vector (v_dir)); }
51 protected:
52 void set_direction(NR::Point const &dir);
53 inline static bool pts_coincide (NR::Point const pt1, NR::Point const pt2)
54 {
55 return (NR::L2 (pt2 - pt1) < epsilon);
56 }
58 NR::Point pt;
59 NR::Point v_dir;
60 NR::Point normal;
61 NR::Coord d0;
62 };
64 inline double determinant (NR::Point const &a, NR::Point const &b)
65 {
66 return (a[NR::X] * b[NR::Y] - a[NR::Y] * b[NR::X]);
67 }
68 std::pair<double, double> coordinates (NR::Point const &v1, NR::Point const &v2, NR::Point const &w);
69 bool lies_in_sector (NR::Point const &v1, NR::Point const &v2, NR::Point const &w);
70 bool lies_in_quadrangle (NR::Point const &A, NR::Point const &B, NR::Point const &C, NR::Point const &D, NR::Point const &pt);
71 std::pair<NR::Point, NR::Point> side_of_intersection (NR::Point const &A, NR::Point const &B,
72 NR::Point const &C, NR::Point const &D,
73 NR::Point const &pt, NR::Point const &dir);
75 /*** For debugging purposes: Draw a knot/node of specified size and color at the given position ***/
76 void create_canvas_point(NR::Point const &pos, double size = 4.0, guint32 rgba = 0xff00007f);
78 /*** For debugging purposes: Draw a line between the specified points ***/
79 void create_canvas_line(NR::Point const &p1, NR::Point const &p2, guint32 rgba = 0xff00007f);
82 /** A function to print out the Line. It just prints out the coordinates of start point and
83 direction on the given output stream */
84 inline std::ostream &operator<< (std::ostream &out_file, const Line &in_line) {
85 out_file << "Start: " << in_line.pt << " Direction: " << in_line.v_dir;
86 return out_file;
87 }
89 } // namespace Box3D
92 #endif /* !SEEN_LINE_GEOMETRY_H */
94 /*
95 Local Variables:
96 mode:c++
97 c-file-style:"stroustrup"
98 c-file-offsets:((innamespace . 0)(inline-open . 0))
99 indent-tabs-mode:nil
100 fill-column:99
101 End:
102 */
103 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :