API Documentation
Loading...
Searching...
No Matches
Distance.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: Distance
29Included in API: True
30Author(s): Tyler Parke
31 *-----------------------------------------------------------------------------------------**/
32#pragma once
33#include <NDEVR/Polygon.h>
34#include <NDEVR/PolyLine.h>
35#include <NDEVR/Plane.h>
36#include <NDEVR/Intersection.h>
37namespace NDEVR
38{
40 {};
41 template<uint01 t_dims, class t_type, class t_vertex>
43 {
44 t_type distance = 0;
45 for (uint01 dim = 0; dim < t_dims; ++dim)
46 {
47 if (vertex[dim] < bounds[MIN][dim])
48 {
49 t_type val = bounds[MIN][dim] - vertex[dim];
50 distance += val * val;
51 }
52 else if (vertex[dim] > bounds[MAX][dim] )
53 {
54 t_type val = vertex[dim] - bounds[MAX][dim];
55 distance += val * val;
56 }
57 }
58 return distance;
59 }
60 template<uint01 t_dims, class t_type, class t_vertex>
62 {
63 return distanceSquared(bounds, vertex);
64 }
65
66
67 template<uint01 t_dims, class t_type, class t_vertex_a, class t_vertex_b>
69 {
70 if (bounds.contains(line))
71 return cast<t_type>(0);
73 Vector<t_dims, t_type> v = line.ray();
74 for (uint01 i = 0; i < t_dims; ++i)
75 {
76 if ( bounds.doesIntersect(line.vertex(A)[i] - bounds[MIN][i], line.vertex(B)[i] - bounds[MIN][i], line.vertex(A), v, i)
77 || bounds.doesIntersect(line.vertex(A)[i] - bounds[MAX][i], line.vertex(B)[i] - bounds[MAX][i], line.vertex(A), v, i))
78 return cast<t_type>(0);
79 }
80
81 Vector<t_dims, t_type> t1 = (bounds[MIN] - p) / v;
82 Vector<t_dims, t_type> t2 = (bounds[MAX] - p) / v;
83 t_type p1 = getMax(t_type(0), t1.template dimensionalValue<MIN>(), t2.template dimensionalValue<MIN>());
84 t_type p2 = getMax(t_type(0), getMin(t1.template dimensionalValue<MAX>(), t2.template dimensionalValue<MAX>()));
85 Vector<t_dims, t_type> val = getMin(getMax((p + v * p1 + p + v * p2) / t_type(2), bounds[MIN]), bounds[MAX]);
86 t_type t = getMax(t_type(0), ((val - p) * v).sum() / v.magnitudeSquared());
87 val = p + v * t - val;
88 return val.magnitudeSquared();
89 }
90
91 template<uint01 t_dims, class t_type, class t_vertex_a, class t_vertex_b>
93 {
94 return distanceSquared(line, bounds);
95 }
96
97 template<uint01 t_dims, class t_type, class t_vertex>
99 {
100 t_type distance = 0;
101 for (uint01 dim = 0; dim < t_dims; ++dim)
102 {
103 if (a[MAX][dim] < b[MIN][dim])
104 {
105 t_type val = b[MIN][dim] - a[MAX][dim];
106 distance += val * val;
107 }
108 else if(a[MIN][dim] < b[MAX][dim])
109 {
110 t_type val = a[MIN][dim] - b[MAX][dim];
111 distance += val * val;
112 }
113 }
114 return distance;
115 }
116 /**--------------------------------------------------------------------------------------------------
117 Fn: t_type distanceSquared(const LineSegment<t_dims, t_type, t_vertex>& left,
118 const LineSegment<t_dims, t_type, t_vertex>& right, const t_type& epsilon = cast<t_type>(0))
119
120 Distance squared.
121
122 Author: Tyler Parke
123
124 Date: 2017-11-19
125
126 Parameters:
127 left - The left.
128 right - The right.
129 epsilon - (Optional) The epsilon.
130
131 Returns: A t_type.
132 *-----------------------------------------------------------------------------------------------**/
133 template<uint01 t_dims, class t_type, class t_vertex>
135 {
136 return left.template closestPoints<t_type>(right, epsilon).lengthSquared();
137 }
138
139 /**--------------------------------------------------------------------------------------------------
140 Fn: constexpr t_type distanceSquared(const Triangle<t_dims, t_type>& tri,
141 const Vector<t_dims, t_type>& point)
142
143 Distance squared.
144
145 Author: Tyler Parke
146
147 Date: 2017-11-17
148
149 Parameters:
150 tri - The triangle.
151 point - The point.
152
153 Returns: A t_type.
154 *-----------------------------------------------------------------------------------------------**/
155 template<uint01 t_dims, class t_type, class t_vertex>
156 constexpr t_type distanceSquared(const Triangle<t_dims, t_type, t_vertex>& tri, const t_vertex& vertex)
157 {
158 return distanceSquared(vertex, closestPoint(tri, vertex));
159 }
160
161 /**--------------------------------------------------------------------------------------------------
162 Fn: constexpr t_type distanceSquared(const Vector<t_dims, t_type>& point,
163 const Triangle<t_dims, t_type>& tri)
164
165 Distance squared.
166
167 Author: Tyler Parke
168
169 Date: 2017-11-17
170
171 Parameters:
172 point - The point.
173 tri - The triangle.
174
175 Returns: A t_type.
176 *-----------------------------------------------------------------------------------------------**/
177 template<uint01 t_dims, class t_type, class t_vertex>
178 constexpr t_type distanceSquared(const t_vertex& vertex, const Triangle<t_dims, t_type, t_vertex>& tri)
179 {
180 return distanceSquared(vertex, closestPoint(tri, vertex));
181 }
182
183 /**--------------------------------------------------------------------------------------------------
184 Fn: constexpr t_type distanceSquared(const LineSegment<t_dims, t_type>& line,
185 const Triangle<t_dims, t_type>& tri)
186
187 Distance squared.
188
189 Author: Tyler Parke
190
191 Date: 2017-11-17
192
193 Parameters:
194 line - The line.
195 tri - The triangle.
196
197 Returns: A t_type.
198 *-----------------------------------------------------------------------------------------------**/
199 template<uint01 t_dims, class t_type, class t_vertex>
201 {
202 if (classify(line, tri) != IntersectionTypes::outside)
203 {
204 return cast<t_type>(0);
205 }
206 t_type dist_a = distanceSquared(line, tri.edge(A, B));
207 t_type dist_b = distanceSquared(line, tri.edge(B, C));
208 t_type dist_c = distanceSquared(line, tri.edge(C, A));
209 return getMin(dist_a, dist_b, dist_c);
210 }
211 template<uint01 t_dims, class t_type, class t_vertex>
213 {
214 return distanceSquared(line, tri);
215 }
216
217
218 template<uint01 t_dims, class t_type, class t_vertex>
220 {
221 lib_assert(false, "Not yet finished");
222 return Constant<t_type>::NaN;// distanceSquared(line, closestPoint(tri, line));
223 }
224 template<uint01 t_dims, class t_type, class t_vertex>
226 {
227 lib_assert(false, "Not yet finished");
228 return Constant<t_type>::NaN;// distanceSquared(line, closestPoint(tri, line));
229 }
230
231
232
233 /**--------------------------------------------------------------------------------------------------
234 Fn: constexpr t_type distance(const t_vertex& vertex,
235 const LineSegment<t_dims, t_type, t_vertex>& line)
236
237 Distances.
238
239 Author: Tyler Parke
240
241 Date: 2017-11-19
242
243 Parameters:
244 vertex - The vertex.
245 line - The line.
246
247 Returns: A t_type.
248 *-----------------------------------------------------------------------------------------------**/
249 template<class epsilon_type, uint01 t_dims, class t_type, class t_vertex>
250 constexpr t_type distance(const t_vertex& vertex, const LineSegment<t_dims, t_type, t_vertex>& line)
251 {
252 return sqrt(cast<epsilon_type>(distanceSquared(line, vertex)));
253 }
254 template<class t_type, class t_other_type>
255 t_type distanceSquared(const t_other_type& original_object, const Polygon<t_type>& poly)
256 {
257 auto object = original_object.template as<2, t_type>();
258 if (classify(poly, object) != IntersectionTypes::outside)
259 {
260 return cast<t_type>(0);
261 }
262 else
263 {
264 t_type min = Constant<t_type>::Max;
265 for (uint04 i = 0; i < poly.vertexCount(); i++)
266 {
267 const t_type current = distanceSquared(poly.edge(i), object);
268 if (current < min)
269 {
270 min = current;
271 }
272 }
273 return min;
274 }
275 }
276 template<class t_type, class t_other_type>
277 t_type distanceSquared(const Polygon<t_type>& poly, const t_other_type& original_object)
278 {
279 return distanceSquared(original_object, poly);
280 }
281
282 template<uint01 t_dims, class t_type, class t_other_type>
283 t_type distanceSquared(const t_other_type& original_object, const Polyline<t_dims, t_type>& poly)
284 {
285 t_type min = Constant<t_type>::Max;
286 if (poly.vertexCount() == 0)
288 if (poly.vertexCount() == 1)
289 return distanceSquared(poly.vertex(0), original_object);;
290 for (uint04 i = 0; i < poly.vertexCount() - 1; i++)
291 {
292 const t_type current = distanceSquared(poly.segment(i), original_object);
293 if (current < min)
294 {
295 min = current;
296 }
297 }
298 return min;
299 }
300 template<uint01 t_dims, class t_type, class t_other_type>
301 t_type distanceSquared(const Polyline<t_dims, t_type>& poly, const t_other_type& original_object)
302 {
303 return distanceSquared(original_object, poly);
304 }
305
306 /**--------------------------------------------------------------------------------------------------
307 Fn: t_precision distance(const LineSegment<t_dims, t_type, t_vertex>& left,
308 const LineSegment<t_dims, t_type, t_vertex>& right, t_precision epsilon = cast<t_precision>(0))
309
310 Distances.
311
312 Author: Tyler Parke
313
314 Date: 2017-11-19
315
316 Parameters:
317 left - The left.
318 right - The right.
319 epsilon - (Optional) The epsilon.
320
321 Returns: A t_precision.
322 *-----------------------------------------------------------------------------------------------**/
323 template<class t_precision, uint01 t_dims, class t_type, class t_vertex>
324 t_precision distance(const LineSegment<t_dims, t_type, t_vertex>& left, const LineSegment<t_dims, t_type, t_vertex>& right, t_precision epsilon = cast<t_precision>(0))
325 {
326 return sqrt(cast<t_precision>(distanceSquared(left, right, epsilon)));
327 }
328
329 template<uint01 t_dims, class t_type, class t_vertex>
330 constexpr t_type distance(const Plane<t_dims, t_type>& plane, const Vertex<t_dims, t_vertex>& vertex)
331 {
332 return abs(dot(plane.normal, vertex) + plane.d);
333 }
334
335
336
337
338
339
340
341
342 /**--------------------------------------------------------------------------------------------------
343 Fn: constexpr t_type distance(const LineSegment<t_dims, t_type>& line,
344 const Vector<t_dims, t_type>& vertex)
345
346 Distances.
347
348 Author: Tyler Parke
349
350 Date: 2017-11-19
351
352 Parameters:
353 line - The line.
354 vertex - The vertex.
355
356 Returns: A t_type.
357 *-----------------------------------------------------------------------------------------------**/
358 template<class t_precision, uint01 t_dims, class t_type, class t_vertex>
359 constexpr t_type distance(const LineSegment<t_dims, t_type>& line, const Vector<t_dims, t_type>& vertex)
360 {
361 return sqrt(cast<t_precision>(distanceSquared(line, vertex)));
362 }
363}
364
#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 specification of upper and lower bounds in N-dimensions.
Definition Bounds.hpp:57
constexpr bool contains(const t_type &value) const
Query if this object contains the given value.
Definition Bounds.hpp:329
constexpr bool doesIntersect(t_type distance_a, t_type distance_b, const t_vertex &origin, const Vector< t_dims, t_type > &ray, uint01 exclusion_axis) const
Checks for intersection of the ray from a given distance, excluding one axis.
Definition Bounds.hpp:701
Definition Distance.hpp:40
A line segment represented by two vertices, a start and end.
Definition Line.hpp:55
constexpr t_vertex ray() const
Definition Line.hpp:134
constexpr const t_vertex & vertex(uint01 index) const
Definition Line.hpp:171
constexpr t_type lengthSquared() const
Definition Line.hpp:505
Definition Geometry.h:41
t_type d
Definition Plane.hpp:216
Ray< t_dims, t_type > normal
Definition Plane.hpp:215
An N-sided polygon.
Definition Polygon.hpp:58
LineSegment< 2, t_type, t_vertex > edge(uint04 index) const
Definition Polygon.hpp:185
uint04 vertexCount() const
Definition Polygon.hpp:219
A polyline which stores vertex information for many points along a given path.
Definition CoordinateProjectionManager.h:44
uint04 vertexCount() const
Definition PolyLine.hpp:209
LineSegment< t_dims, t_type, t_vertex > segment(uint04 index) const
Definition PolyLine.hpp:175
const t_vertex & vertex(uint04 index) const
Definition PolyLine.hpp:155
Definition Triangle.hpp:143
constexpr LineSegment< t_dims, t_type, t_vertex > edge(uint01 triangle_node_a, uint01 triangle_node_b) const
Definition Triangle.hpp:276
An element of a vector space. An element of the real coordinate space Rn Basis vector,...
Definition Vector.hpp:62
constexpr t_type magnitudeSquared() const
Definition Vector.hpp:458
A vertex.
Definition Vertex.hpp:54
Definition ACIColor.h:37
constexpr t_type getMax(const t_type &left, const t_type &right)
Finds the max of the given arguments using the > operator.
Definition BaseFunctions.hpp:116
t_type dot(const Vector< t_dims, t_type > &v1, const Vector< t_dims, t_type > &v2)
Definition VectorFunctions.hpp:1096
@ MIN
Definition BaseValues.hpp:226
@ MAX
Definition BaseValues.hpp:227
uint8_t uint01
-Defines an alias representing a 1 byte, unsigned integer -Can represent exact integer values 0 throu...
Definition BaseValues.hpp:98
t_type distanceSquared(const Bounds< t_dims, t_type, t_vertex > &bounds, const Vector< t_dims, t_type > &vertex)
Definition Distance.hpp:42
@ outside
Definition BaseValues.hpp:242
constexpr Vector< t_dims, t_type > closestPoint(const Triangle< t_dims, t_type, t_vertex > &tri, const t_vertex &point)
Definition Triangle.hpp:909
IntersectionTypes classify(const Vector< t_dims, t_type > &v1, const Vector< t_dims, t_type > &v2)
Definition Intersection.hpp:338
uint32_t uint04
-Defines an alias representing a 4 byte, unsigned integer -Can represent exact integer values 0 throu...
Definition BaseValues.hpp:120
t_type sqrt(const t_type &value)
Definition VectorFunctions.hpp:1309
constexpr t_to cast(const Angle< t_from > &value)
Definition Angle.h:514
constexpr t_type distance(const t_vertex &vertex, const LineSegment< t_dims, t_type, t_vertex > &line)
Definition Distance.hpp:250
constexpr Angle< t_angle_type > abs(const Angle< t_angle_type > &value)
Definition AngleFunctions.h:750
@ B
Definition BaseValues.hpp:203
@ A
Definition BaseValues.hpp:201
@ C
Definition BaseValues.hpp:205
constexpr t_type getMin(const t_type &left, const t_type &right)
Finds the minimum of the given arguments based on the < operator.
Definition BaseFunctions.hpp:67
Definition BaseValues.hpp:272