API Documentation
Loading...
Searching...
No Matches
Plane.hpp
Go to the documentation of this file.
1/*--------------------------------------------------------------------------------------------
2Copyright (c) 2019, NDEVR LLC
3tyler.parke@ndevr.org
4 __ __ ____ _____ __ __ _______
5 | \ | | | __ \ | ___|\ \ / / | __ \
6 | \ | | | | \ \ | |___ \ \ / / | |__) |
7 | . \| | | |__/ / | |___ \ V / | _ /
8 | |\ |_|_____/__|_____|___\_/____| | \ \
9 |__| \__________________________________| \__\
10
11Subject to the terms of the Enterprise+ Agreement, NDEVR hereby grants
12Licensee a limited, non-exclusive, non-transferable, royalty-free license
13(without the right to sublicense) to use the API solely for the purpose of
14Licensee's internal development efforts to develop applications for which
15the API was provided.
16
17The above copyright notice and this permission notice shall be included in all
18copies or substantial portions of the Software.
19
20THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
21INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
22PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
23FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
24OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
25DEALINGS IN THE SOFTWARE.
26
27Library: Base
28File: Plane
29Included in API: True
30Author(s): Tyler Parke
31 *-----------------------------------------------------------------------------------------**/
32#pragma once
33#include <NDEVR/Vertex.h>
34#include <NDEVR/LineSegment.h>
35#include <NDEVR/Matrix.h>
36namespace NDEVR
37{
38 /**--------------------------------------------------------------------------------------------------
39 \brief The location of an object either below, above, or on a given N-dimensional plane
40 **/
41 enum class PlanePosition
42 {
46 };
47 /**--------------------------------------------------------------------------------------------------
48 \brief Logic for a given plane or N-dimensions. Planes are coordinate systems of one less dimension
49 than the dimension they occupy.
50 **/
51 template<uint01 t_dims, class t_type>
52 class Plane
53 {
54 public:
56 : normal()
57 , d(0)
58 {}
59 constexpr Plane(const Ray<t_dims, t_type>& _normal, const Vertex<t_dims, t_type>& _p)
60 : normal(_normal)
61 , d(dot(_p, _normal))
62 {}
64 : normal()
65 , d()
66 {
67 const Vector<t_dims, t_type> edge1 = b - a;
68 const Vector<t_dims, t_type> edge2 = c - a;
69 normal = (cross(edge1, edge2)).template normalized<t_type>(); // Cross product
70 d = -dot(c, normal);
71 }
72 constexpr Plane(const Ray<t_dims, t_type>& _normal, t_type _d)
73 : normal(_normal)
74 , d(_d)
75 {}
76 constexpr void negate()
77 {
78 normal = -normal;
79 d = -d;
80 }
81 template<uint01 t_new_dims, class t_new_type>
86
87 //untested
88 [[nodiscard]] PlanePosition planePosition(const Vector<t_dims, t_type>& pos) const
89 {
90 t_type dot_product = distanceTo(pos);
91 if (dot_product > 0)
93 else if (dot_product < 0)
95 else
97 }
98 [[nodiscard]] bool isAbovePlane(const Vector<t_dims, t_type>& pos) const
99 {
100 return distanceTo(pos) > 0;
101 }
102 [[nodiscard]] bool isBelowPlane(const Vector<t_dims, t_type>& pos) const
103 {
104 return distanceTo(pos) < 0;
105 }
106 [[nodiscard]] bool liesOnPlane(const Vector<t_dims, t_type>& pos) const
107 {
108 return distanceTo(pos) == 0;
109 }
110 [[nodiscard]] t_type distanceTo(const Vector<t_dims, t_type>& pos) const
111 {
112 return dot(normal, pos) - d;
113 }
114 //end untested
116 {
117 t_type V0 = dot(normal, pos) - d;
118 Vector<t_dims, t_type> v = pos - V0 * normal;
119 return v;
120 }
121 [[nodiscard]] t_type calculateIntersectionPos(const LineSegment<t_dims, t_type>& line, t_type epsilon) const
122 {
123 t_type Vd = dot(normal, line.ray());
124 t_type V0 = dot(normal, line[A]) - d;
125
126 if (abs(Vd) < epsilon)
127 {
128 return abs(V0) < epsilon ? Constant<t_type>::Invalid : Constant<t_type>::Max;
129 }
130 t_type distance_along_line = -V0 / Vd;
131 return distance_along_line;
132 }
133
134 [[nodiscard]] Vertex<t_dims, t_type> calculateIntersection(const LineSegment<t_dims, t_type>& line, t_type epsilon) const
135 {
136 t_type distance_along_line = calculateIntersectionPos(line, epsilon);
137 Vertex<t_dims, t_type> v = line[A] + distance_along_line * line.ray();
138 return v;
139 }
140
141 [[nodiscard]] Matrix<t_type> projectionMatrix(const Vector<3, t_type>& up) const
142 {
144 if (right == 0.0)
145 return Matrix<fltp08>(1.0);
146 right = right.template normalized<t_type>();
147 Vector<t_dims, t_type> backward = cross(normal, right).template normalized<t_type>();
148 Vector<t_dims, t_type> offset = normal * -d;
149 return Matrix<t_type>(
150 right[X], backward[X], normal[X], 0
151 , right[Y], backward[Y], normal[Y], 0
152 , right[Z], backward[Z], normal[Z], 0
153 , offset[X], offset[Y], offset[Z], 1);
154 }
155
156 template<class t_buffer_type>
157 static Plane CreateBestFitPlane(const t_buffer_type& points, const Vector<t_dims, t_type>& direction_reference = Vector<t_dims, t_type>(0,0,1))
158 {
159 const uint04 vertex_size = points.size();
160 lib_assert(vertex_size >= 3, "Plane requires at least 3 points");
161 Vector<t_dims, t_type> centroid(0);
164 Vector<t_dims, t_type> p1, p2, p3, c1, c2;
165
166 p1 = c1 = points[0];
167 p2 = c2 = points[1];
168
169 for(uint04 i = 0; i < vertex_size; i++)
170 {
171 p3 = points[i];
172 v = cross((p3 - p2), (p1 - p2));
173 if(v[t_dims - 1] != 0)
174 v = -v;
175 n += v;
176 p1 = p2;
177 p2 = p3;
178 centroid += points[i];
179 }
180 centroid /= cast<t_type>(vertex_size);
181 p1 = p2;
182 p2 = p3;
183 p3 = c1;
184 v = cross((p3 - p2), (p1 - p2));
185 if(v[t_dims - 1] != 0)
186 v = -v;
187 n += v;
188 p1 = p2;
189 p2 = p3;
190 p3 = c2;
191 v = cross((p3 - p2), (p1 - p2));
192 if (v[t_dims - 1])
193 v = -v;
194 n += v;
195 n = n.template normalized<t_type>();
196 if (dot(direction_reference, n) < 0)
197 n = -n;
198 Plane plane(n, dot(n, centroid));
199 return plane;
200 };
201
202 bool contains(const Vector<3, t_type>& point, t_type epsilon) const
203 {
204 Vector<3, t_type> b = point - (normal * d);
205 return (abs(dot(b, normal)) < epsilon);
206 }
207 bool isSamePlane(const Plane& plane, t_type epsilon) const
208 {
209 return (equals(normal, plane.normal, epsilon) && abs(d - plane.d) < epsilon)
210 || (equals(-normal, plane.normal, epsilon) && abs(d + plane.d) < epsilon);
211 }
212
213 bool operator==(const Plane& plane) const
214 {
215 return normal == plane.normal && d == plane.d;
216 }
217 bool operator!=(const Plane& plane) const
218 {
219 return normal != plane.normal || d != plane.d;
220 }
221 public:
223 t_type d;
224
225 };
226
227 /**--------------------------------------------------------------------------------------------------
228 Struct: Constant<Plane<t_dims,t_type>>
229
230 A constant.
231
232 Author: Tyler Parke
233
234 Date: 2019-05-07
235 **/
236 template<uint01 t_dims, class t_type>
237 struct Constant<Plane<t_dims, t_type>>
238 {
240 };
241
242 /**--------------------------------------------------------------------------------------------------
243 Query if 'value' is Invalid.
244
245 Author: Tyler Parke
246
247 Date: 2019-05-07
248
249 Typeparams:
250 t_dims - Type of the dims.
251 t_type - Type of the type.
252 Parameters:
253 value - The value.
254
255 \returns True if nan, false if not.
256 **/
257
258 template<uint01 t_dims, class t_type>
259 static constexpr bool IsInvalid(const Plane<t_dims, t_type>& value)
260 {
261 return IsInvalid(value.normal) || IsInvalid(value.d);
262 }
263
264 template<class t_type, uint01 t_row_dims, uint01 t_col_dims>
266 {
267 Vertex<3, t_type> origin = plane.normal * plane.d;
268 return Plane<3, t_type>(matrix * plane.normal, origin);
269 }
270};
#define lib_assert(expression, message)
Definition LibAssert.h:61
A line segment represented by two vertices, a start and end.
Definition Line.hpp:49
constexpr t_vertex ray() const
Definition Line.hpp:120
Definition Matrix.hpp:176
Logic for a given plane or N-dimensions. Planes are coordinate systems of one less dimension than the...
Definition Geometry.h:41
t_type d
Definition Plane.hpp:223
Matrix< t_type > projectionMatrix(const Vector< 3, t_type > &up) const
Definition Plane.hpp:141
constexpr void negate()
Definition Plane.hpp:76
constexpr Plane(const Ray< t_dims, t_type > &_normal, t_type _d)
Definition Plane.hpp:72
bool isAbovePlane(const Vector< t_dims, t_type > &pos) const
Definition Plane.hpp:98
bool operator==(const Plane &plane) const
Definition Plane.hpp:213
t_type calculateIntersectionPos(const LineSegment< t_dims, t_type > &line, t_type epsilon) const
Definition Plane.hpp:121
bool liesOnPlane(const Vector< t_dims, t_type > &pos) const
Definition Plane.hpp:106
Plane< t_new_dims, t_new_type > as() const
Definition Plane.hpp:82
t_type distanceTo(const Vector< t_dims, t_type > &pos) const
Definition Plane.hpp:110
constexpr Plane(const Ray< t_dims, t_type > &_normal, const Vertex< t_dims, t_type > &_p)
Definition Plane.hpp:59
Vertex< t_dims, t_type > nearestPosition(const Vector< 3, fltp08 > &pos) const
Definition Plane.hpp:115
PlanePosition planePosition(const Vector< t_dims, t_type > &pos) const
Definition Plane.hpp:88
Vertex< t_dims, t_type > calculateIntersection(const LineSegment< t_dims, t_type > &line, t_type epsilon) const
Definition Plane.hpp:134
bool operator!=(const Plane &plane) const
Definition Plane.hpp:217
constexpr Plane(const Vertex< t_dims, t_type > &a, const Vertex< t_dims, t_type > &b, const Vertex< t_dims, t_type > &c)
Definition Plane.hpp:63
bool isBelowPlane(const Vector< t_dims, t_type > &pos) const
Definition Plane.hpp:102
Ray< t_dims, t_type > normal
Definition Plane.hpp:222
bool contains(const Vector< 3, t_type > &point, t_type epsilon) const
Definition Plane.hpp:202
bool isSamePlane(const Plane &plane, t_type epsilon) const
Definition Plane.hpp:207
Plane()
Definition Plane.hpp:55
static Plane CreateBestFitPlane(const t_buffer_type &points, const Vector< t_dims, t_type > &direction_reference=Vector< t_dims, t_type >(0, 0, 1))
Definition Plane.hpp:157
Definition Vertex.hpp:317
A fixed-size array with better performance compared to dynamic containers.
Definition Vector.hpp:60
A vertex or point. A specific type of Vector used primarily for spacial location information.
Definition Vertex.hpp:48
Definition ACIColor.h:37
constexpr bool IsInvalid(const t_type &value)
Query if 'value' is valid or invalid. Invalid values should return invalid if used for calculations o...
Definition BaseFunctions.hpp:170
constexpr std::enable_if< IsVecType< t_vector_type, Angle< fltp08 > >::value, t_vector_type >::type operator*(const t_vector_type &angle, const t_type &mult)
Multiplication operator for a Vector of Angles.
Definition AngleFunctions.h:326
t_type dot(const Vector< t_dims, t_type > &v1, const Vector< t_dims, t_type > &v2)
Definition VectorFunctions.hpp:1030
PlanePosition
The location of an object either below, above, or on a given N-dimensional plane.
Definition Plane.hpp:42
constexpr Vector< 1, t_type > cross(const Vector< 1, t_type > &, const Vector< 1, t_type > &)
Definition VectorFunctions.hpp:898
uint32_t uint04
-Defines an alias representing a 4 byte, unsigned integer -Can represent exact integer values 0 throu...
Definition BaseValues.hpp:96
constexpr t_to cast(const Angle< t_from > &value)
Definition Angle.h:375
constexpr bool equals(const LineSegment< t_dims, t_type, t_vertex > &left, const LineSegment< t_dims, t_type, t_vertex > &right, const t_type &epsilon=cast< t_type >(0))
Definition Line.hpp:757
constexpr Angle< t_angle_type > abs(const Angle< t_angle_type > &value)
Changes an input with a negative sign, to a positive sign.
Definition AngleFunctions.h:645
@ A
Definition BaseValues.hpp:168
@ Y
Definition BaseValues.hpp:169
@ X
Definition BaseValues.hpp:167
@ Z
Definition BaseValues.hpp:171
Defines for a given type (such as sint04, fltp08, UUID, etc) a maximum, minimum, and reserved 'invali...
Definition BaseValues.hpp:233
static const t_type Invalid
Definition BaseValues.hpp:234