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 enum class PlanePosition
39 {
43 };
44 template<uint01 t_dims, class t_type>
45 class Plane
46 {
47 public:
49 : normal()
50 , d(0)
51 {}
52 constexpr Plane(const Ray<t_dims, t_type>& _normal, const Vertex<t_dims, t_type>& _p)
53 : normal(_normal)
54 , d(dot(_p, _normal))
55 {}
57 : normal()
58 , d()
59 {
60 const Vector<t_dims, t_type> edge1 = b - a;
61 const Vector<t_dims, t_type> edge2 = c - a;
62 normal = (cross(edge1, edge2)).template normalized<t_type>(); // Cross product
63 d = -dot(c, normal);
64 }
65 constexpr Plane(const Ray<t_dims, t_type>& _normal, t_type _d)
66 : normal(_normal)
67 , d(_d)
68 {}
69 constexpr void negate()
70 {
71 normal = -normal;
72 d = -d;
73 }
74 template<uint01 t_new_dims, class t_new_type>
79
80 //untested
81 [[nodiscard]] PlanePosition planePosition(const Vector<t_dims, t_type>& pos) const
82 {
83 t_type dot_product = distanceTo(pos);
84 if (dot_product > 0)
86 else if (dot_product < 0)
88 else
90 }
91 [[nodiscard]] bool isAbovePlane(const Vector<t_dims, t_type>& pos) const
92 {
93 return distanceTo(pos) > 0;
94 }
95 [[nodiscard]] bool isBelowPlane(const Vector<t_dims, t_type>& pos) const
96 {
97 return distanceTo(pos) < 0;
98 }
99 [[nodiscard]] bool liesOnPlane(const Vector<t_dims, t_type>& pos) const
100 {
101 return distanceTo(pos) == 0;
102 }
103 [[nodiscard]] t_type distanceTo(const Vector<t_dims, t_type>& pos) const
104 {
105 return dot(normal, pos) - d;
106 }
107 //end untested
109 {
110 t_type V0 = dot(normal, pos) - d;
111 Vector<t_dims, t_type> v = pos - V0 * normal;
112 return v;
113 }
114 [[nodiscard]] t_type calculateIntersectionPos(const LineSegment<t_dims, t_type>& line, t_type epsilon) const
115 {
116 t_type Vd = dot(normal, line.ray());
117 t_type V0 = dot(normal, line[A]) - d;
118
119 if (abs(Vd) < epsilon)
120 {
121 return abs(V0) < epsilon ? Constant<t_type>::NaN : Constant<t_type>::Max;
122 }
123 t_type distance_along_line = -V0 / Vd;
124 return distance_along_line;
125 }
126
127 [[nodiscard]] Vertex<t_dims, t_type> calculateIntersection(const LineSegment<t_dims, t_type>& line, t_type epsilon) const
128 {
129 t_type distance_along_line = calculateIntersectionPos(line, epsilon);
130 Vertex<t_dims, t_type> v = line[A] + distance_along_line * line.ray();
131 return v;
132 }
133
134 [[nodiscard]] Matrix<t_type> projectionMatrix(const Vector<3, t_type>& up) const
135 {
137 if (right == 0.0)
138 return Matrix<fltp08>(1.0);
139 right = right.template normalized<t_type>();
140 Vector<t_dims, t_type> backward = cross(normal, right).template normalized<t_type>();
141 Vector<t_dims, t_type> offset = normal * -d;
142 return Matrix<t_type>(
143 right[X], backward[X], normal[X], 0
144 , right[Y], backward[Y], normal[Y], 0
145 , right[Z], backward[Z], normal[Z], 0
146 , offset[X], offset[Y], offset[Z], 1);
147 }
148
149 template<class t_buffer_type>
150 static Plane CreateBestFitPlane(const t_buffer_type& points, const Vector<t_dims, t_type>& direction_reference = Vector<t_dims, t_type>(0,0,1))
151 {
152 const uint04 vertex_size = points.size();
153 lib_assert(vertex_size >= 3, "Plane requires at least 3 points");
154 Vector<t_dims, t_type> centroid(0);
157 Vector<t_dims, t_type> p1, p2, p3, c1, c2;
158
159 p1 = c1 = points[0];
160 p2 = c2 = points[1];
161
162 for(uint04 i = 0; i < vertex_size; i++)
163 {
164 p3 = points[i];
165 v = cross((p3 - p2), (p1 - p2));
166 if(v[t_dims - 1] != 0)
167 v = -v;
168 n += v;
169 p1 = p2;
170 p2 = p3;
171 centroid += points[i];
172 }
173 centroid /= cast<t_type>(vertex_size);
174 p1 = p2;
175 p2 = p3;
176 p3 = c1;
177 v = cross((p3 - p2), (p1 - p2));
178 if(v[t_dims - 1] != 0)
179 v = -v;
180 n += v;
181 p1 = p2;
182 p2 = p3;
183 p3 = c2;
184 v = cross((p3 - p2), (p1 - p2));
185 if (v[t_dims - 1])
186 v = -v;
187 n += v;
188 n = n.template normalized<t_type>();
189 if (dot(direction_reference, n) < 0)
190 n = -n;
191 Plane plane(n, dot(n, centroid));
192 return plane;
193 };
194
195 bool contains(const Vector<3, t_type>& point, t_type epsilon) const
196 {
197 Vector<3, t_type> b = point - (normal * d);
198 return (abs(dot(b, normal)) < epsilon);
199 }
200 bool isSamePlane(const Plane& plane, t_type epsilon) const
201 {
202 return (equals(normal, plane.normal, epsilon) && abs(d - plane.d) < epsilon)
203 || (equals(-normal, plane.normal, epsilon) && abs(d + plane.d) < epsilon);
204 }
205
206 bool operator==(const Plane& plane) const
207 {
208 return normal == plane.normal && d == plane.d;
209 }
210 bool operator!=(const Plane& plane) const
211 {
212 return normal != plane.normal || d != plane.d;
213 }
214 public:
216 t_type d;
217
218 };
219
220 /**--------------------------------------------------------------------------------------------------
221 Struct: Constant<Plane<t_dims,t_type>>
222
223 A constant.
224
225 Author: Tyler Parke
226
227 Date: 2019-05-07
228 *-----------------------------------------------------------------------------------------------**/
229 template<uint01 t_dims, class t_type>
230 struct Constant<Plane<t_dims, t_type>>
231 {
233 };
234
235 /**--------------------------------------------------------------------------------------------------
236 Fn: template<uint01 t_dims, class t_type> static constexpr bool isNaN(const Plane<t_dims, t_type>& value)
237
238 Query if 'value' is NaN.
239
240 Author: Tyler Parke
241
242 Date: 2019-05-07
243
244 Typeparams:
245 t_dims - Type of the dims.
246 t_type - Type of the type.
247 Parameters:
248 value - The value.
249
250 Returns: True if nan, false if not.
251 *-----------------------------------------------------------------------------------------------**/
252
253 template<uint01 t_dims, class t_type>
254 static constexpr bool isNaN(const Plane<t_dims, t_type>& value)
255 {
256 return isNaN(value.normal) || isNaN(value.d);
257 }
258
259 template<class t_type, uint01 t_row_dims, uint01 t_col_dims>
261 {
262 Vertex<3, t_type> origin = plane.normal * plane.d;
263 return Plane<3, t_type>(matrix * plane.normal, origin);
264 }
265};
#define lib_assert(expression, message)
Asserts some logic in the code. Disabled in non debug mode by default. Can be re-enabled in release u...
Definition LibAssert.h:70
A line segment represented by two vertices, a start and end.
Definition Line.hpp:55
constexpr t_vertex ray() const
Definition Line.hpp:134
Definition Matrix.hpp:173
Definition Geometry.h:41
t_type d
Definition Plane.hpp:216
Matrix< t_type > projectionMatrix(const Vector< 3, t_type > &up) const
Definition Plane.hpp:134
constexpr void negate()
Definition Plane.hpp:69
constexpr Plane(const Ray< t_dims, t_type > &_normal, t_type _d)
Definition Plane.hpp:65
bool isAbovePlane(const Vector< t_dims, t_type > &pos) const
Definition Plane.hpp:91
bool operator==(const Plane &plane) const
Definition Plane.hpp:206
t_type calculateIntersectionPos(const LineSegment< t_dims, t_type > &line, t_type epsilon) const
Definition Plane.hpp:114
bool liesOnPlane(const Vector< t_dims, t_type > &pos) const
Definition Plane.hpp:99
Plane< t_new_dims, t_new_type > as() const
Definition Plane.hpp:75
t_type distanceTo(const Vector< t_dims, t_type > &pos) const
Definition Plane.hpp:103
constexpr Plane(const Ray< t_dims, t_type > &_normal, const Vertex< t_dims, t_type > &_p)
Definition Plane.hpp:52
Vertex< t_dims, t_type > nearestPosition(const Vector< 3, fltp08 > &pos) const
Definition Plane.hpp:108
PlanePosition planePosition(const Vector< t_dims, t_type > &pos) const
Definition Plane.hpp:81
Vertex< t_dims, t_type > calculateIntersection(const LineSegment< t_dims, t_type > &line, t_type epsilon) const
Definition Plane.hpp:127
bool operator!=(const Plane &plane) const
Definition Plane.hpp:210
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:56
bool isBelowPlane(const Vector< t_dims, t_type > &pos) const
Definition Plane.hpp:95
Ray< t_dims, t_type > normal
Definition Plane.hpp:215
bool contains(const Vector< 3, t_type > &point, t_type epsilon) const
Definition Plane.hpp:195
bool isSamePlane(const Plane &plane, t_type epsilon) const
Definition Plane.hpp:200
Plane()
Definition Plane.hpp:48
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:150
Definition Vertex.hpp:341
An element of a vector space. An element of the real coordinate space Rn Basis vector,...
Definition Vector.hpp:62
A vertex.
Definition Vertex.hpp:54
Definition ACIColor.h:37
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)
Definition AngleFunctions.h:403
t_type dot(const Vector< t_dims, t_type > &v1, const Vector< t_dims, t_type > &v2)
Definition VectorFunctions.hpp:1096
PlanePosition
Definition Plane.hpp:39
constexpr Vector< 1, t_type > cross(const Vector< 1, t_type > &, const Vector< 1, t_type > &)
Definition VectorFunctions.hpp:954
uint32_t uint04
-Defines an alias representing a 4 byte, unsigned integer -Can represent exact integer values 0 throu...
Definition BaseValues.hpp:120
constexpr t_to cast(const Angle< t_from > &value)
Definition Angle.h:514
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:819
constexpr Angle< t_angle_type > abs(const Angle< t_angle_type > &value)
Definition AngleFunctions.h:750
@ A
Definition BaseValues.hpp:201
@ Y
Definition BaseValues.hpp:202
@ X
Definition BaseValues.hpp:200
@ Z
Definition BaseValues.hpp:204
Definition BaseValues.hpp:272
static const t_type NaN
Definition BaseValues.hpp:274