Code

Groundwork to snap to intersections, e.g. intersections of gridlines with guidelines...
[inkscape.git] / src / line-geometry.h
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 "vanishing-point.h"
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; }
35     
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     friend NR::Point fourth_pt_with_given_cross_ratio (NR::Point const &A, NR::Point const &C, NR::Point const &D, double gamma);
41     double lambda (NR::Point const pt);
42     inline NR::Point point_from_lambda (double const lambda) { 
43         return (pt + lambda * NR::unit_vector (v_dir)); }
45 protected:
46     void set_direction(NR::Point const &dir);
47     inline static bool pts_coincide (NR::Point const pt1, NR::Point const pt2)
48     {
49         return (NR::L2 (pt2 - pt1) < epsilon);
50     }
52     NR::Point pt;
53     NR::Point v_dir;
54     NR::Point normal;
55     NR::Coord d0;
56 };
58 inline double determinant (NR::Point const &a, NR::Point const &b)
59 {
60     return (a[NR::X] * b[NR::Y] - a[NR::Y] * b[NR::X]);
61 }
62 std::pair<double, double> coordinates (NR::Point const &v1, NR::Point const &v2, NR::Point const &w);
63 bool lies_in_sector (NR::Point const &v1, NR::Point const &v2, NR::Point const &w);
64 bool lies_in_quadrangle (NR::Point const &A, NR::Point const &B, NR::Point const &C, NR::Point const &D, NR::Point const &pt);
65 std::pair<NR::Point, NR::Point> side_of_intersection (NR::Point const &A, NR::Point const &B,
66                                                       NR::Point const &C, NR::Point const &D,
67                                                       NR::Point const &pt, NR::Point const &dir);
69 double cross_ratio (NR::Point const &A, NR::Point const &B, NR::Point const &C, NR::Point const &D);
70 double cross_ratio (VanishingPoint const &V, NR::Point const &B, NR::Point const &C, NR::Point const &D);
71 NR::Point fourth_pt_with_given_cross_ratio (NR::Point const &A, NR::Point const &C, NR::Point const &D, double gamma);
73 /*** For testing purposes: Draw a knot/node of specified size and color at the given position ***/
74 void create_canvas_point(NR::Point const &pos, double size = 4.0, guint32 rgba = 0xff00007f);
76 /*** For testing purposes: Draw a line between the specified points ***/
77 void create_canvas_line(NR::Point const &p1, NR::Point const &p2, guint32 rgba = 0xff00007f);
80 /** A function to print out the Line.  It just prints out the coordinates of start point and
81     direction on the given output stream */
82 inline std::ostream &operator<< (std::ostream &out_file, const Line &in_line) {
83     out_file << "Start: " << in_line.pt << "  Direction: " << in_line.v_dir;
84     return out_file;
85 }
87 } // namespace Box3D
90 #endif /* !SEEN_LINE_GEOMETRY_H */
92 /*
93   Local Variables:
94   mode:c++
95   c-file-style:"stroustrup"
96   c-file-offsets:((innamespace . 0)(inline-open . 0))
97   indent-tabs-mode:nil
98   fill-column:99
99   End:
100 */
101 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :