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{
39 /**-----------------------------------------------------------------------------------------
40 \brief Dummy class for including distance functions
41 Distance functions help determine the distance between two objects in N-dimensional space.
42 *-----------------------------------------------------------------------------------------*/
44 {};
45 template<uint01 t_dims, class t_type, class t_vertex>
47 {
48 t_type distance = 0;
49 for (uint01 dim = 0; dim < t_dims; ++dim)
50 {
51 if (vertex[dim] < bounds[MIN][dim])
52 {
53 t_type val = bounds[MIN][dim] - vertex[dim];
54 distance += val * val;
55 }
56 else if (vertex[dim] > bounds[MAX][dim] )
57 {
58 t_type val = vertex[dim] - bounds[MAX][dim];
59 distance += val * val;
60 }
61 }
62 return distance;
63 }
64 template<uint01 t_dims, class t_type, class t_vertex>
66 {
67 return distanceSquared(bounds, vertex);
68 }
69
70
71 template<uint01 t_dims, class t_type, class t_vertex_a, class t_vertex_b>
73 {
74 if (bounds.contains(line))
75 return cast<t_type>(0);
77 Vector<t_dims, t_type> v = line.ray();
78 for (uint01 i = 0; i < t_dims; ++i)
79 {
80 if ( bounds.doesIntersect(line.vertex(A)[i] - bounds[MIN][i], line.vertex(B)[i] - bounds[MIN][i], line.vertex(A), v, i)
81 || bounds.doesIntersect(line.vertex(A)[i] - bounds[MAX][i], line.vertex(B)[i] - bounds[MAX][i], line.vertex(A), v, i))
82 return cast<t_type>(0);
83 }
84
85 Vector<t_dims, t_type> t1 = (bounds[MIN] - p) / v;
86 Vector<t_dims, t_type> t2 = (bounds[MAX] - p) / v;
87 t_type p1 = getMax(t_type(0), t1.template dimensionalValue<MIN>(), t2.template dimensionalValue<MIN>());
88 t_type p2 = getMax(t_type(0), getMin(t1.template dimensionalValue<MAX>(), t2.template dimensionalValue<MAX>()));
89 Vector<t_dims, t_type> val = getMin(getMax((p + v * p1 + p + v * p2) / t_type(2), bounds[MIN]), bounds[MAX]);
90 t_type t = getMax(t_type(0), ((val - p) * v).sum() / v.magnitudeSquared());
91 val = p + v * t - val;
92 return val.magnitudeSquared();
93 }
94
95 template<uint01 t_dims, class t_type, class t_vertex_a, class t_vertex_b>
97 {
98 return distanceSquared(line, bounds);
99 }
100
101 template<uint01 t_dims, class t_type, class t_vertex>
103 {
104 t_type distance = 0;
105 for (uint01 dim = 0; dim < t_dims; ++dim)
106 {
107 if (a[MAX][dim] < b[MIN][dim])
108 {
109 t_type val = b[MIN][dim] - a[MAX][dim];
110 distance += val * val;
111 }
112 else if(a[MIN][dim] < b[MAX][dim])
113 {
114 t_type val = a[MIN][dim] - b[MAX][dim];
115 distance += val * val;
116 }
117 }
118 return distance;
119 }
120 template<uint01 t_dims, class t_type, class t_vertex>
122 {
123 return left.template closestPoints<t_type>(right, epsilon).lengthSquared();
124 }
125
126 /**
127 \returns The squared distance between the triangle and the vertex.
128 **/
129 template<uint01 t_dims, class t_type, class t_vertex>
130 constexpr t_type distanceSquared(const Triangle<t_dims, t_type, t_vertex>& tri, const t_vertex& vertex)
131 {
132 return distanceSquared(vertex, ClosestPoint(tri, vertex));
133 }
134 template<uint01 t_dims, class t_type, class t_vertex>
135 constexpr t_type distanceSquared(const t_vertex& vertex, const Triangle<t_dims, t_type, t_vertex>& tri)
136 {
137 return distanceSquared(vertex, ClosestPoint(tri, vertex));
138 }
139 template<uint01 t_dims, class t_type, class t_vertex>
141 {
142 if (classify(line, tri) != IntersectionTypes::outside)
143 {
144 return cast<t_type>(0);
145 }
146 t_type dist_a = distanceSquared(line, tri.edge(A, B));
147 t_type dist_b = distanceSquared(line, tri.edge(B, C));
148 t_type dist_c = distanceSquared(line, tri.edge(C, A));
149 return getMin(dist_a, dist_b, dist_c);
150 }
151 template<uint01 t_dims, class t_type, class t_vertex>
153 {
154 return distanceSquared(line, tri);
155 }
156
157
158 template<uint01 t_dims, class t_type, class t_vertex>
160 {
161 lib_assert(false, "Not yet finished");
162 return Constant<t_type>::Invalid;// distanceSquared(line, closestPoint(tri, line));
163 }
164 template<uint01 t_dims, class t_type, class t_vertex>
166 {
167 lib_assert(false, "Not yet finished");
168 return Constant<t_type>::Invalid;// distanceSquared(line, closestPoint(tri, line));
169 }
170 template<class epsilon_type, uint01 t_dims, class t_type, class t_vertex>
171 constexpr t_type distance(const t_vertex& vertex, const LineSegment<t_dims, t_type, t_vertex>& line)
172 {
173 return sqrt(cast<epsilon_type>(distanceSquared(line, vertex)));
174 }
175 template<class t_type, class t_other_type>
176 t_type distanceSquared(const t_other_type& original_object, const Polygon<t_type>& poly)
177 {
178 auto object = original_object.template as<2, t_type>();
179 if (classify(poly, object) != IntersectionTypes::outside)
180 {
181 return cast<t_type>(0);
182 }
183 else
184 {
185 t_type min = Constant<t_type>::Max;
186 for (uint04 i = 0; i < poly.vertexCount(); i++)
187 {
188 const t_type current = distanceSquared(poly.edge(i), object);
189 if (current < min)
190 {
191 min = current;
192 }
193 }
194 return min;
195 }
196 }
197 template<class t_type, class t_other_type>
198 t_type distanceSquared(const Polygon<t_type>& poly, const t_other_type& original_object)
199 {
200 return distanceSquared(original_object, poly);
201 }
202
203 template<uint01 t_dims, class t_type, class t_other_type>
204 t_type distanceSquared(const t_other_type& original_object, const Polyline<t_dims, t_type>& poly)
205 {
206 t_type min = Constant<t_type>::Max;
207 if (poly.vertexCount() == 0)
209 if (poly.vertexCount() == 1)
210 return distanceSquared(poly.vertex(0), original_object);;
211 for (uint04 i = 0; i < poly.vertexCount() - 1; i++)
212 {
213 const t_type current = distanceSquared(poly.segment(i), original_object);
214 if (current < min)
215 {
216 min = current;
217 }
218 }
219 return min;
220 }
221 template<uint01 t_dims, class t_type, class t_other_type>
222 t_type distanceSquared(const Polyline<t_dims, t_type>& poly, const t_other_type& original_object)
223 {
224 return distanceSquared(original_object, poly);
225 }
226 template<class t_precision, uint01 t_dims, class t_type, class t_vertex>
227 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))
228 {
229 return sqrt(cast<t_precision>(distanceSquared(left, right, epsilon)));
230 }
231
232 template<uint01 t_dims, class t_type, class t_vertex>
233 constexpr t_type distance(const Plane<t_dims, t_type>& plane, const Vertex<t_dims, t_vertex>& vertex)
234 {
235 return abs(dot(plane.normal, vertex) + plane.d);
236 }
237 template<class t_precision, uint01 t_dims, class t_type, class t_vertex>
238 constexpr t_type distance(const LineSegment<t_dims, t_type>& line, const Vector<t_dims, t_type>& vertex)
239 {
240 return sqrt(cast<t_precision>(distanceSquared(line, vertex)));
241 }
242}
243
#define lib_assert(expression, message)
Definition LibAssert.h:61
A specification of upper and lower bounds in N-dimensions.
Definition Bounds.hpp:52
constexpr bool contains(const t_type &value) const
Query if this object contains the given value. t_allow_bounds - whether or not to allow boundary case...
Definition Bounds.hpp:237
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:577
Dummy class for including distance functions.
Definition Distance.hpp:44
A line segment represented by two vertices, a start and end.
Definition Line.hpp:49
constexpr t_vertex ray() const
Definition Line.hpp:120
constexpr const t_vertex & vertex(uint01 index) const
Definition Line.hpp:152
constexpr t_type lengthSquared() const
Definition Line.hpp:463
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
Ray< t_dims, t_type > normal
Definition Plane.hpp:222
An N-sided polygon.
Definition Polygon.hpp:53
LineSegment< 2, t_type, t_vertex > edge(uint04 index) const
Definition Polygon.hpp:174
uint04 vertexCount() const
Definition Polygon.hpp:204
A polyline which stores vertex information for many points along a given path.
Definition CoordinateProjectionManager.h:44
uint04 vertexCount() const
Definition PolyLine.hpp:191
LineSegment< t_dims, t_type, t_vertex > segment(uint04 index) const
Definition PolyLine.hpp:161
const t_vertex & vertex(uint04 index) const
Definition PolyLine.hpp:143
A triangle is a polygon with three edges and three vertices. It is one of the basic shapes in geometr...
Definition Triangle.hpp:138
constexpr LineSegment< t_dims, t_type, t_vertex > edge(uint01 triangle_node_a, uint01 triangle_node_b) const
Definition Triangle.hpp:256
A fixed-size array with better performance compared to dynamic containers.
Definition Vector.hpp:60
constexpr t_type magnitudeSquared() const
Definition Vector.hpp:426
A vertex or point. A specific type of Vector used primarily for spacial location information.
Definition Vertex.hpp:48
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 The only requirement is that t_type have > ...
Definition BaseFunctions.hpp:94
t_type dot(const Vector< t_dims, t_type > &v1, const Vector< t_dims, t_type > &v2)
Definition VectorFunctions.hpp:1030
@ MIN
Definition BaseValues.hpp:196
@ MAX
Definition BaseValues.hpp:197
uint8_t uint01
-Defines an alias representing a 1 byte, unsigned integer -Can represent exact integer values 0 throu...
Definition BaseValues.hpp:80
t_type distanceSquared(const Bounds< t_dims, t_type, t_vertex > &bounds, const Vector< t_dims, t_type > &vertex)
Definition Distance.hpp:46
@ outside
Definition BaseValues.hpp:211
IntersectionTypes classify(const Vector< t_dims, t_type > &v1, const Vector< t_dims, t_type > &v2)
Definition Intersection.hpp:329
uint32_t uint04
-Defines an alias representing a 4 byte, unsigned integer -Can represent exact integer values 0 throu...
Definition BaseValues.hpp:96
t_type sqrt(const t_type &value)
Definition VectorFunctions.hpp:1225
constexpr t_to cast(const Angle< t_from > &value)
Definition Angle.h:375
constexpr t_type distance(const t_vertex &vertex, const LineSegment< t_dims, t_type, t_vertex > &line)
Definition Distance.hpp:171
constexpr Vector< t_dims, t_type > ClosestPoint(const Triangle< t_dims, t_type, t_vertex > &tri, const t_vertex &point)
Definition Triangle.hpp:857
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
@ B
Definition BaseValues.hpp:170
@ A
Definition BaseValues.hpp:168
@ C
Definition BaseValues.hpp:172
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...
Definition BaseFunctions.hpp:56
Defines for a given type (such as sint04, fltp08, UUID, etc) a maximum, minimum, and reserved 'invali...
Definition BaseValues.hpp:233