Hermes
line.h
1 /*
2  * Copyright (c) 2017 FilipeCN
3  *
4  * The MIT License (MIT)
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to deal
8  * in the Software without restriction, including without limitation the rights
9  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10  * copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21  * THE SOFTWARE.
22  *
23  */
24 
25 #ifndef HERMES_GEOMETRY_LINE_H
26 #define HERMES_GEOMETRY_LINE_H
27 
28 #include <hermes/geometry/point.h>
29 #include <hermes/geometry/vector.h>
30 
31 namespace hermes {
32 
33 // *********************************************************************************************************************
34 // Line
35 // *********************************************************************************************************************
37 class Line2 {
38 public:
39  // *******************************************************************************************************************
40  // CONSTRUCTORS
41  // *******************************************************************************************************************
42  Line2() = default;
45  Line2(point2 _a, vec2 _d) {
46  a = _a;
47  d = normalize(_d);
48  }
49  // *******************************************************************************************************************
50  // OPERATORS
51  // *******************************************************************************************************************
54  point2 operator()(float t) const { return a + d * t; }
55  // *******************************************************************************************************************
56  // METHODS
57  // *******************************************************************************************************************
58  // access
60  [[nodiscard]] vec2 direction() const { return normalize(d); }
61  // queries
64  [[nodiscard]] float projection(point2 p) const { return dot((p - a), d); }
67  [[nodiscard]] point2 closestPoint(point2 p) const { return (*this)(projection(p)); }
68  // *******************************************************************************************************************
69  // DEBUG
70  // *******************************************************************************************************************
71  friend std::ostream &operator<<(std::ostream &os, const Line2 &p) {
72  os << "[Line]\n";
73  os << p.a << p.d;
74  return os;
75  }
76  // *******************************************************************************************************************
77  // PUBLIC FIELDS
78  // *******************************************************************************************************************
80  vec2 d;
81 };
82 
83 // *********************************************************************************************************************
84 // Line
85 // *********************************************************************************************************************
87 class Line {
88 public:
89  // *******************************************************************************************************************
90  // CONSTRUCTORS
91  // *******************************************************************************************************************
92  Line() = default;
95  Line(point3 _a, vec3 _d) {
96  a = _a;
97  d = normalize(_d);
98  }
99  // *******************************************************************************************************************
100  // OPERATORS
101  // *******************************************************************************************************************
104  point3 operator()(float t) const { return a + d * t; }
105  // *******************************************************************************************************************
106  // METHODS
107  // *******************************************************************************************************************
108  // access
110  [[nodiscard]] vec3 direction() const { return normalize(d); }
111  // queries
114  [[nodiscard]] float projection(point3 p) const { return dot((p - a), d); }
117  [[nodiscard]] point3 closestPoint(point3 p) const { return (*this)(projection(p)); }
118  // *******************************************************************************************************************
119  // DEBUG
120  // *******************************************************************************************************************
121  friend std::ostream &operator<<(std::ostream &os, const Line &p) {
122  os << "[Line]\n";
123  os << p.a << p.d;
124  return os;
125  }
126  // *******************************************************************************************************************
127  // PUBLIC FIELDS
128  // *******************************************************************************************************************
130  vec3 d;
131 };
132 
133 } // hermes namespace
134 
135 #endif
Represents a 2D line by a point and a vector.
Definition: line.h:37
point2 a
line point
Definition: line.h:79
float projection(point2 p) const
Definition: line.h:64
point2 operator()(float t) const
Definition: line.h:54
vec2 d
line direction
Definition: line.h:80
vec2 direction() const
Definition: line.h:60
Line2(point2 _a, vec2 _d)
Definition: line.h:45
point2 closestPoint(point2 p) const
Definition: line.h:67
Represents a line by a point and a vector.
Definition: line.h:87
point3 operator()(float t) const
Definition: line.h:104
vec3 d
line direction
Definition: line.h:130
point3 a
line point
Definition: line.h:129
Line(point3 _a, vec3 _d)
Definition: line.h:95
point3 closestPoint(point3 p) const
Definition: line.h:117
vec3 direction() const
Definition: line.h:110
float projection(point3 p) const
Definition: line.h:114
HERMES_DEVICE_CALLABLE Normal3< T > normalize(const Normal3< T > &normal)
Computes normalized copy.
Definition: normal.h:201
HERMES_DEVICE_CALLABLE T dot(const Normal3< T > &n, const Vector3< T > &v)
Computes dot product with vector.
Definition: normal.h:237
Geometric point classes.
Geometric vector classes.