summaryrefslogtreecommitdiffstats
path: root/src/line-geometry.h
blob: 72e0ae794afcba9655ed633c761fcf6bae10cccc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/*
 * Routines for dealing with lines (intersections, etc.)
 *
 * Authors:
 *   Maximilian Albert <Anhalter42@gmx.de>
 *
 * Copyright (C) 2007 authors
 *
 * Released under GNU GPL, read the file 'COPYING' for more information
 */

#ifndef SEEN_LINE_GEOMETRY_H
#define SEEN_LINE_GEOMETRY_H

#include "libnr/nr-point.h"
#include "libnr/nr-point-fns.h"
#include "libnr/nr-maybe.h"
#include "glib.h"
#include "display/sp-ctrlline.h"
#include "vanishing-point.h"

#include "document.h"
#include "ui/view/view.h"

namespace Box3D {

class Line {
public:
    Line(NR::Point const &start, NR::Point const &vec, bool is_endpoint = true);
    Line(Line const &line);
    virtual ~Line() {}
    Line &operator=(Line const &line);
    virtual NR::Maybe<NR::Point> intersect(Line const &line);
    void set_direction(NR::Point const &dir); // FIXME: Can we avoid this explicit assignment?
    
    NR::Point closest_to(NR::Point const &pt); // returns the point on the line closest to pt 

    friend inline std::ostream &operator<< (std::ostream &out_file, const Line &in_line);
    friend NR::Point fourth_pt_with_given_cross_ratio (NR::Point const &A, NR::Point const &C, NR::Point const &D, double gamma);

    double lambda (NR::Point const pt);
    inline NR::Point point_from_lambda (double const lambda) { 
        return (pt + lambda * NR::unit_vector (v_dir)); }

protected:
    inline static bool pts_coincide (NR::Point const pt1, NR::Point const pt2)
    {
        return (NR::L2 (pt2 - pt1) < epsilon);
    }

    NR::Point pt;
    NR::Point v_dir;
    NR::Point normal;
    NR::Coord d0;
};

std::pair<double, double> coordinates (NR::Point const &v1, NR::Point const &v2, NR::Point const &w);
bool lies_in_sector (NR::Point const &v1, NR::Point const &v2, NR::Point const &w);
std::pair<NR::Point, NR::Point> side_of_intersection (NR::Point const &A, NR::Point const &B,
                                                      NR::Point const &C, NR::Point const &D,
                                                      NR::Point const &pt, NR::Point const &dir);

double cross_ratio (NR::Point const &A, NR::Point const &B, NR::Point const &C, NR::Point const &D);
double cross_ratio (VanishingPoint const &V, NR::Point const &B, NR::Point const &C, NR::Point const &D);
NR::Point fourth_pt_with_given_cross_ratio (NR::Point const &A, NR::Point const &C, NR::Point const &D, double gamma);

/*** For testing purposes: Draw a knot/node of specified size and color at the given position ***/
void create_canvas_point(NR::Point const &pos, double size = 4.0, guint32 rgba = 0xff00007f);

/*** For testing purposes: Draw a line between the specified points ***/
void create_canvas_line(NR::Point const &p1, NR::Point const &p2, guint32 rgba = 0xff00007f);


/** A function to print out the Line.  It just prints out the coordinates of start point and
    direction on the given output stream */
inline std::ostream &operator<< (std::ostream &out_file, const Line &in_line) {
    out_file << "Start: " << in_line.pt << "  Direction: " << in_line.v_dir;
    return out_file;
}

} // namespace Box3D


#endif /* !SEEN_LINE_GEOMETRY_H */

/*
  Local Variables:
  mode:c++
  c-file-style:"stroustrup"
  c-file-offsets:((innamespace . 0)(inline-open . 0))
  indent-tabs-mode:nil
  fill-column:99
  End:
*/
// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :