NDEVR
API Documentation
Vertex.hpp
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: Vertex
29Included in API: True
30Author(s): Tyler Parke
31 *-----------------------------------------------------------------------------------------**/
32#pragma once
33#include <NDEVR/Vector.h>
34
35namespace NDEVR
36{
42 template<uint01 t_dims, class t_type, class t_vector_type = Vector<t_dims, t_type>>
43 class Vertex : public t_vector_type
44 {
45 public:
46 constexpr Vertex()
47 : t_vector_type()
48 {}
49 constexpr explicit Vertex(const t_type& scaler)
50 : t_vector_type(scaler)
51 {}
52 constexpr Vertex(const t_vector_type& vector)
53 : t_vector_type(vector)
54 {}
55 constexpr Vertex(const Vertex<t_dims, t_type, t_vector_type>& vector)
56 : t_vector_type(vector)
57 {}
58 constexpr Vertex(const t_type x, const t_type y)
59 : t_vector_type(x, y)
60 {}
61 constexpr Vertex(t_type x, t_type y, t_type z)
62 : t_vector_type(x, y, z)
63 {}
64 constexpr Vertex(const t_type x, const t_type y, const t_type z, const t_type w)
65 : t_vector_type(x, y, z, w)
66 {}
67 constexpr explicit Vertex(const t_type(&vector)[t_dims])
68 : t_vector_type(vector)
69 {}
70 constexpr Vertex(const Vector<getMax(t_dims - 1, 0), t_type>& vector, const t_type postfix)
71 : t_vector_type(vector, postfix)
72 {}
73 constexpr Vertex(const Vector<getMax(t_dims - 2, 0), t_type>& vector, const t_type postfix_a, const t_type postfix_b)
74 : t_vector_type(vector, postfix_a, postfix_b)
75 {}
76 constexpr Vertex(const t_type prefix, const Vector<t_dims - 1, t_type>& vector)
77 : t_vector_type(prefix, vector)
78 {}
79 template<uint01 t_new_dim, class t_new_type, class t_new_vector = Vector<t_new_dim, t_new_type>>
80 constexpr Vertex<t_new_dim, t_new_type, t_new_vector> as() const
81 {
82 return Vertex<t_new_dim, t_new_type, t_new_vector>(t_vector_type::template as<t_new_dim, t_new_type>());
83 }
84 template<class t_new_type, class t_new_vector = Vector<t_dims, t_new_type>>
85 constexpr Vertex<t_dims, t_new_type, t_new_vector> as() const
86 {
87 return Vertex<t_dims, t_new_type, t_new_vector>(t_vector_type::template as<t_dims, t_new_type>());
88 }
89 template<uint01 t_new_dim, class t_new_type, class t_new_vector = Vector<t_new_dim, t_new_type>>
90 constexpr Vertex<t_new_dim, t_new_type, t_new_vector> as(t_new_type extra_fill_value) const
91 {
92 return Vertex<t_new_dim, t_new_type, t_new_vector>(t_vector_type::template as<t_new_dim, t_new_type>(extra_fill_value));
93 }
94 constexpr Vertex scale(const t_vector_type& scale, const Vertex<t_dims, t_type>& center) const
95 {
96 return ((*this - center) * scale) + center;
97 }
98 constexpr Vertex scale(const t_type scale, const Vertex<t_dims, t_type>& center) const
99 {
100 return ((*this - center) * scale) + center;
101 }
102 constexpr const Vertex& center() const
103 {
104 return *this;
105 }
106 constexpr Vertex<t_dims, t_type, t_vector_type>& operator=(const t_type& scaler)
107 {
108 t_vector_type::operator=(scaler);
109 return *this;
110 }
111 constexpr Vertex<t_dims, t_type, t_vector_type>& operator=(const t_vector_type& vector)
112 {
113 t_vector_type::operator=(vector);
114 return *this;
115 }
116 constexpr Vertex<t_dims, t_type, t_vector_type>& operator=(const Vertex<t_dims, t_type, t_vector_type>& vector)
117 {
118 t_vector_type::operator=(vector);
119 return *this;
120 }
121 };
122 template<uint01 t_dims, class t_type, class t_base>
124 {
126 for (uint01 dim = 0; dim < t_dims; dim++)
127 {
128 vec[dim] = getMax(v1[dim], v2[dim]);
129 }
130 return vec;
131 }
132 template<uint01 t_dims, class t_type, class t_base>
134 {
136 for (uint01 dim = 0; dim < t_dims; dim++)
137 {
138 vec[dim] = getMin(v1[dim], v2[dim]);
139 }
140 return vec;
141 }
142 template<uint01 t_dims, class t_type, class t_vector_type>
143 struct Constant<Vertex<t_dims, t_type, t_vector_type>>
144 {
145 constexpr const static Vertex<t_dims, t_type, t_vector_type> Invalid{ Constant<t_type>::Invalid };
146 constexpr const static Vertex<t_dims, t_type, t_vector_type> Min{ Constant<t_type>::Min };
147 constexpr const static Vertex<t_dims, t_type, t_vector_type> Max{ Constant<t_type>::Max };
148 };
149 template<uint01 t_dims, class t_type, class t_vector_type>
150 static constexpr bool IsInvalid(const Vertex<t_dims, t_type, t_vector_type>& value)
151 {
152 for (uint01 dim = 0; dim < t_dims; ++dim)
153 {
154 if (IsInvalid(value[dim]))
155 return true;
156 }
157 return false;
158 }
159 template<uint01 t_dims, class t_type, class t_vector_type>
160 static constexpr bool IsValid(const Vertex<t_dims, t_type, t_vector_type>& value)
161 {
162 for (uint01 dim = 0; dim < t_dims; ++dim)
163 {
164 if (IsInvalid(value[dim]))
165 return false;
166 }
167 return true;
168 }
169 template<uint01 t_dims, class t_type, class t_vector_type = Vector<t_dims, t_type>>
170 class Ray : public t_vector_type
171 {
172 public:
173 constexpr Ray()
174 : t_vector_type()
175 {}
176 constexpr explicit Ray(const t_type& scaler)
177 : t_vector_type(scaler)
178 {}
179 constexpr Ray(const t_vector_type& vector)
180 : t_vector_type(vector)
181 {}
182 constexpr Ray(const Ray<t_dims, t_type, t_vector_type>& vector)
183 : t_vector_type(vector)
184 {}
185 constexpr Ray(const t_type x, const t_type y)
186 : t_vector_type(x, y)
187 {}
188 constexpr Ray(t_type x, t_type y, t_type z)
189 : t_vector_type(x, y, z)
190 {}
191 constexpr Ray(const t_type x, const t_type y, const t_type z, const t_type w)
192 : t_vector_type(x, y, z, w)
193 {}
194 constexpr explicit Ray(const t_type(&vector)[t_dims])
195 : t_vector_type(vector)
196 {}
197 constexpr Ray(const Vector<getMax(t_dims - 1, 0), t_type>& vector, const t_type postfix)
198 : t_vector_type(vector, postfix)
199 {}
200 constexpr Ray(const Vector<getMax(t_dims - 2, 0), t_type>& vector, const t_type postfix_a, const t_type postfix_b)
201 : t_vector_type(vector, postfix_a, postfix_b)
202 {}
203 constexpr Ray(const t_type prefix, const Vector<t_dims - 1, t_type>& vector)
204 : t_vector_type(prefix, vector)
205 {}
206 template<uint01 t_new_dim, class t_new_type, class t_new_vector = Vector<t_new_dim, t_new_type>>
207 constexpr Ray<t_new_dim, t_new_type, t_new_vector> as(t_new_type extra_fill_value = 0) const
208 {
209 return Ray<t_new_dim, t_new_type, t_new_vector>(t_vector_type::template as<t_new_dim, t_new_type>(extra_fill_value));
210 }
211 template<class t_new_type, class t_new_vector = Vector<t_dims, t_new_type>>
212 constexpr Ray<t_dims, t_new_type, t_new_vector> as() const
213 {
214 return Ray<t_dims, t_new_type, t_new_vector>(t_vector_type::template as<t_new_type>());
215 }
216 constexpr Ray scale(const t_vector_type& scale) const
217 {
218 return (*this * scale);
219 }
220 constexpr Ray scale(const t_type scale) const
221 {
222 return (*this * scale);
223 }
224 constexpr Ray<t_dims, t_type, t_vector_type>& operator=(const t_type& scaler)
225 {
226 t_vector_type::operator=(scaler);
227 return *this;
228 }
229 constexpr Ray<t_dims, t_type, t_vector_type>& operator=(const t_vector_type& vector)
230 {
231 t_vector_type::operator=(vector);
232 return *this;
233 }
234 constexpr Ray<t_dims, t_type, t_vector_type>& operator=(const Ray<t_dims, t_type, t_vector_type>& vector)
235 {
236 t_vector_type::operator=(vector);
237 return *this;
238 }
239 };
240 template<uint01 t_dims, class t_type, class t_base>
241 constexpr Ray<t_dims, t_type, t_base> getMax(const Ray<t_dims, t_type, t_base>& v1, const Ray<t_dims, t_type, t_base >& v2)
242 {
243 Ray<t_dims, t_type, t_base> vec;
244 for (uint01 dim = 0; dim < t_dims; dim++)
245 {
246 vec[dim] = getMax(abs(v1[dim]), abs(v2[dim]));
247 }
248 return vec;
249 }
250 template<uint01 t_dims, class t_type, class t_base>
251 constexpr Ray<t_dims, t_type, t_base> getMin(const Ray<t_dims, t_type, t_base>& v1, const Ray<t_dims, t_type, t_base>& v2)
252 {
253 Ray<t_dims, t_type, t_base> vec;
254 for (uint01 dim = 0; dim < t_dims; dim++)
255 {
256 vec[dim] = getMin(abs(v1[dim]), abs(v2[dim]));
257 }
258 return vec;
259 }
260 template<uint01 t_dims, class t_type, class t_vector_type>
261 struct Constant<Ray<t_dims, t_type, t_vector_type>>
262 {
263 constexpr const static Ray<t_dims, t_type, t_vector_type> Invalid{ Constant<t_vector_type>::Invalid };
264 constexpr const static Ray<t_dims, t_type, t_vector_type> Min{ 0 };
265 constexpr const static Ray<t_dims, t_type, t_vector_type> Max{ Constant<t_vector_type>::Max };
266 };
267 template<uint01 t_dims, class t_type, class t_vector_type>
268 static constexpr bool IsInvalid(const Ray<t_dims, t_type, t_vector_type>& value)
269 {
270 for (uint01 dim = 0; dim < t_dims; ++dim)
271 {
272 if (IsInvalid(value[dim]))
273 return true;
274 }
275 return false;
276 }
277 template<uint01 t_dims, class t_type, class t_vector_type>
278 static constexpr bool IsValid(const Ray<t_dims, t_type, t_vector_type>& value)
279 {
280 for (uint01 dim = 0; dim < t_dims; ++dim)
281 {
282 if (IsInvalid(value[dim]))
283 return false;
284 }
285 return true;
286 }
287}
288namespace std//Define things to allow use within std libs
289{
290 template <>
291 struct hash<Vertex<3, fltp08>>
292 {
293 std::size_t operator()(const Vertex<3, fltp08>& s) const noexcept
294 {
295 std::size_t h = 0, g = 0;
296 const uint01* bytes = reinterpret_cast<const uint01*>(&s[0]);
297 for (size_t i = 0; i < 3 * sizeof(fltp08); i++)
298 {
299 h = (h << 4) + bytes[i];
300 g = h & 0xF0000000L;
301 if (g)
302 h ^= g >> 24;
303 h &= ~g;
304 }
305 return h;
306 }
307 };
308 template <>
309 struct hash<Vertex<3, fltp04>>
310 {
311 std::size_t operator()(const Vertex<3, fltp04>& s) const noexcept
312 {
313 std::size_t h = 0, g = 0;
314 const uint01* bytes = reinterpret_cast<const uint01*>(&s[0]);
315 for (size_t i = 0; i < 3 * sizeof(fltp04); i++)
316 {
317 h = (h << 4) + bytes[i];
318 g = h & 0xF0000000L;
319 if (g)
320 h ^= g >> 24;
321 h &= ~g;
322 }
323 return h;
324 }
325 };
326};
constexpr decltype(auto) as() const
Returns the vector as a new time of vector.
Definition Vector.hpp:301
A point in N-dimensional space, used primarily for spatial location information.
Definition Vertex.hpp:44
The primary namespace for the NDEVR SDK.
constexpr t_type getMin(const t_type &left, const t_type &right)
Finds the minimum of the given arguments based on the < operator Author: Tyler Parke Date: 2017-11-05...
float fltp04
Defines an alias representing a 4 byte floating-point number Bit layout is as follows: -Sign: 1 bit a...
constexpr t_type getMax(const t_type &left, const t_type &right)
Finds the max of the given arguments using the > operator The only requirement is that t_type have > ...
static constexpr bool IsValid(const Angle< t_type > &value)
Checks whether the given Angle holds a valid value.
Definition Angle.h:398
constexpr Angle< t_angle_type > abs(const Angle< t_angle_type > &value)
Changes an input with a negative sign, to a positive sign.
double fltp08
Defines an alias representing an 8 byte floating-point number.
uint8_t uint01
-Defines an alias representing a 1 byte, unsigned integer -Can represent exact integer values 0 throu...
static constexpr bool IsInvalid(const Angle< t_type > &value)
Checks whether the given Angle holds an invalid value.
Definition Angle.h:388
STL namespace.
Defines for a given type (such as sint04, fltp08, UUID, etc) a maximum, minimum, and reserved 'invali...