1
+ /* *
2
+ * @file DistanceQueries.h
3
+ * @author Quoc-Minh Ton-That ([email protected] )
4
+ * @brief This file contains functions to answer distance queries.
5
+ * @date 2025-02-12
6
+ *
7
+ * @copyright Copyright (c) 2025
8
+ *
9
+ */
10
+
1
11
#ifndef PBAT_GEOMETRY_DISTANCEQUERIES_H
2
12
#define PBAT_GEOMETRY_DISTANCEQUERIES_H
3
13
10
20
11
21
namespace pbat {
12
22
namespace geometry {
23
+ /* *
24
+ * @brief This namespace contains functions to answer distance queries.
25
+ */
13
26
namespace DistanceQueries {
14
27
15
28
namespace mini = math::linalg::mini;
16
29
17
30
/* *
18
31
* @brief Obtain squared distance between 2 axis-aligned bounding boxes
32
+ * @tparam TMatrixL1 1st AABB's lower corner matrix type
33
+ * @tparam TMatrixU1 1st AABB's upper corner matrix type
34
+ * @tparam TMatrixL2 2nd AABB's lower corner matrix type
35
+ * @tparam TMatrixU2 2nd AABB's upper corner matrix type
19
36
* @param L1 1st AABB's lower corner
20
37
* @param U1 1st AABB's upper corner
21
38
* @param L2 2nd AABB's lower corner
22
39
* @param U2 2nd AABB's upper corner
23
- * @return
40
+ * @return Squared distance between the 2 AABBs
24
41
*/
25
42
template <
26
43
mini::CMatrix TMatrixL1,
27
44
mini::CMatrix TMatrixU1,
28
45
mini::CMatrix TMatrixL2,
29
46
mini::CMatrix TMatrixU2>
30
- PBAT_HOST_DEVICE typename TMatrixL1::ScalarType AxisAlignedBoundingBoxes (
47
+ PBAT_HOST_DEVICE auto AxisAlignedBoundingBoxes (
31
48
TMatrixL1 const & L1,
32
49
TMatrixU1 const & U1,
33
50
TMatrixL2 const & L2,
34
- TMatrixU2 const & U2);
51
+ TMatrixU2 const & U2) -> typename TMatrixL1::ScalarType ;
35
52
36
53
/* *
37
54
* @brief Obtain squared distance between point P and axis-aligned box (L,U)
38
- * @tparam TMatrixP
39
- * @tparam TMatrixL
40
- * @tparam TMatrixU
41
- * @param P
42
- * @param L
43
- * @param U
44
- * @return
55
+ * @tparam TMatrixP Point matrix type
56
+ * @tparam TMatrixL Lower corner matrix type
57
+ * @tparam TMatrixU Upper corner matrix type
58
+ * @param P Point
59
+ * @param L Lower corner of the box
60
+ * @param U Upper corner of the box
61
+ * @return Squared distance between point and box
45
62
*/
46
63
template <mini::CMatrix TMatrixP, mini::CMatrix TMatrixL, mini::CMatrix TMatrixU>
47
- PBAT_HOST_DEVICE typename TMatrixP::ScalarType
48
- PointAxisAlignedBoundingBox (TMatrixP const & P, TMatrixL const & L, TMatrixU const & U);
64
+ PBAT_HOST_DEVICE auto
65
+ PointAxisAlignedBoundingBox (TMatrixP const & P, TMatrixL const & L, TMatrixU const & U) ->
66
+ typename TMatrixP::ScalarType;
49
67
50
68
/* *
51
69
* @brief Obtain squared distance between point P and triangle ABC
52
- * @param P
53
- * @param A
54
- * @param B
55
- * @param C
56
- * @return
70
+ * @tparam TMatrixP Point matrix type
71
+ * @tparam TMatrixA Vertex A matrix type
72
+ * @tparam TMatrixB Vertex B matrix type
73
+ * @tparam TMatrixC Vertex C matrix type
74
+ * @param P Point
75
+ * @param A Vertex A of the triangle
76
+ * @param B Vertex B of the triangle
77
+ * @param C Vertex C of the triangle
78
+ * @return Squared distance between point and triangle
57
79
*/
58
80
template <
59
81
mini::CMatrix TMatrixP,
60
82
mini::CMatrix TMatrixA,
61
83
mini::CMatrix TMatrixB,
62
84
mini::CMatrix TMatrixC>
63
- PBAT_HOST_DEVICE typename TMatrixP::ScalarType
64
- PointTriangle (TMatrixP const & P, TMatrixA const & A, TMatrixB const & B, TMatrixC const & C);
85
+ PBAT_HOST_DEVICE auto
86
+ PointTriangle (TMatrixP const & P, TMatrixA const & A, TMatrixB const & B, TMatrixC const & C) ->
87
+ typename TMatrixP::ScalarType;
65
88
66
89
/* *
67
90
* @brief Obtain squared distance between point P and tetrahedron ABCD
68
- * @tparam TMatrixP
69
- * @tparam TMatrixA
70
- * @tparam TMatrixB
71
- * @tparam TMatrixC
72
- * @tparam TMatrixD
73
- * @param P
74
- * @param A
75
- * @param B
76
- * @param C
77
- * @param D
78
- * @return
91
+ * @tparam TMatrixP Point matrix type
92
+ * @tparam TMatrixA Vertex A matrix type
93
+ * @tparam TMatrixB Vertex B matrix type
94
+ * @tparam TMatrixC Vertex C matrix type
95
+ * @tparam TMatrixD Vertex D matrix type
96
+ * @param P Point
97
+ * @param A Vertex A of the tetrahedron
98
+ * @param B Vertex B of the tetrahedron
99
+ * @param C Vertex C of the tetrahedron
100
+ * @param D Vertex D of the tetrahedron
101
+ * @return Squared distance between point and tetrahedron
79
102
*/
80
103
template <
81
104
mini::CMatrix TMatrixP,
82
105
mini::CMatrix TMatrixA,
83
106
mini::CMatrix TMatrixB,
84
107
mini::CMatrix TMatrixC,
85
108
mini::CMatrix TMatrixD>
86
- PBAT_HOST_DEVICE typename TMatrixP::ScalarType PointTetrahedron (
109
+ PBAT_HOST_DEVICE auto PointTetrahedron (
87
110
TMatrixP const & P,
88
111
TMatrixA const & A,
89
112
TMatrixB const & B,
90
113
TMatrixC const & C,
91
- TMatrixD const & D);
114
+ TMatrixD const & D) -> typename TMatrixP::ScalarType ;
92
115
93
116
/* *
94
117
* @brief Obtains the signed distance of X w.r.t. plane (P,n)
95
- * @param X
96
- * @param P
97
- * @param n
98
- * @return
118
+ * @tparam TMatrixX Query point matrix type
119
+ * @tparam TMatrixP Point on the plane matrix type
120
+ * @tparam TMatrixN Normal of the plane matrix type
121
+ * @param X Query point
122
+ * @param P Point on the plane
123
+ * @param n Normal of the plane
124
+ * @return Signed distance of X w.r.t. plane (P,n)
99
125
*/
100
126
template <mini::CMatrix TMatrixX, mini::CMatrix TMatrixP, mini::CMatrix TMatrixN>
101
- PBAT_HOST_DEVICE typename TMatrixX::ScalarType
102
- PointPlane (TMatrixX const & X, TMatrixP const & P, TMatrixN const & n) ;
127
+ PBAT_HOST_DEVICE auto PointPlane ( TMatrixX const & X, TMatrixP const & P, TMatrixN const & n) ->
128
+ typename TMatrixX::ScalarType ;
103
129
104
130
/* *
105
131
* @brief Obtains the signed distance of X w.r.t. plane spanned by ABC
106
- * @param X
107
- * @param P
108
- * @param n
109
- * @return
132
+ * @tparam TMatrixX Query point matrix type
133
+ * @tparam TMatrixA Vertex A of the triangle in plane matrix type
134
+ * @tparam TMatrixB Vertex B of the triangle in plane matrix type
135
+ * @tparam TMatrixC Vertex C of the triangle in plane matrix type
136
+ * @param X Query point
137
+ * @param A Vertex A of the triangle in plane
138
+ * @param B Vertex B of the triangle in plane
139
+ * @param C Vertex C of the triangle in plane
140
+ * @return Signed distance of X w.r.t. plane spanned by ABC
110
141
*/
111
142
template <
112
143
mini::CMatrix TMatrixX,
113
144
mini::CMatrix TMatrixA,
114
145
mini::CMatrix TMatrixB,
115
146
mini::CMatrix TMatrixC>
116
- PBAT_HOST_DEVICE typename TMatrixX::ScalarType
117
- PointPlane (TMatrixX const & X, TMatrixA const & A, TMatrixB const & B, TMatrixC const & C);
147
+ PBAT_HOST_DEVICE auto
148
+ PointPlane (TMatrixX const & X, TMatrixA const & A, TMatrixB const & B, TMatrixC const & C) ->
149
+ typename TMatrixX::ScalarType;
118
150
119
151
/* *
120
152
* @brief Obtains the squared distance between sphere (X,R) and triangle ABC.
121
- * @param X
122
- * @param R
123
- * @param A
124
- * @param B
125
- * @param C
126
- * @return
153
+ * @tparam TMatrixX Sphere center matrix type
154
+ * @tparam TMatrixA Triangle vertex A matrix type
155
+ * @tparam TMatrixB Triangle vertex B matrix type
156
+ * @tparam TMatrixC Triangle vertex C matrix type
157
+ * @param X Sphere center
158
+ * @param R Sphere radius
159
+ * @param A Vertex A of the triangle
160
+ * @param B Vertex B of the triangle
161
+ * @param C Vertex C of the triangle
162
+ * @return Squared distance between sphere and triangle
127
163
*/
128
164
template <
129
165
mini::CMatrix TMatrixX,
130
166
mini::CMatrix TMatrixA,
131
167
mini::CMatrix TMatrixB,
132
168
mini::CMatrix TMatrixC>
133
- PBAT_HOST_DEVICE typename TMatrixX::ScalarType SphereTriangle (
169
+ PBAT_HOST_DEVICE auto SphereTriangle (
134
170
TMatrixX const & X,
135
171
typename TMatrixX::ScalarType R,
136
172
TMatrixA const & A,
137
173
TMatrixB const & B,
138
- TMatrixC const & C);
174
+ TMatrixC const & C) -> typename TMatrixX::ScalarType ;
139
175
140
176
template <
141
177
mini::CMatrix TMatrixL1,
142
178
mini::CMatrix TMatrixU1,
143
179
mini::CMatrix TMatrixL2,
144
180
mini::CMatrix TMatrixU2>
145
- PBAT_HOST_DEVICE typename TMatrixL1::ScalarType AxisAlignedBoundingBoxes (
181
+ PBAT_HOST_DEVICE auto AxisAlignedBoundingBoxes (
146
182
TMatrixL1 const & L1,
147
183
TMatrixU1 const & U1,
148
184
TMatrixL2 const & L2,
149
- TMatrixU2 const & U2)
185
+ TMatrixU2 const & U2) -> typename TMatrixL1::ScalarType
150
186
{
151
187
using ScalarType = typename TMatrixL1::ScalarType;
152
188
auto constexpr kDims = TMatrixL1::kRows ;
@@ -159,8 +195,9 @@ PBAT_HOST_DEVICE typename TMatrixL1::ScalarType AxisAlignedBoundingBoxes(
159
195
}
160
196
161
197
template <mini::CMatrix TMatrixP, mini::CMatrix TMatrixL, mini::CMatrix TMatrixU>
162
- PBAT_HOST_DEVICE typename TMatrixP::ScalarType
163
- PointAxisAlignedBoundingBox (TMatrixP const & P, TMatrixL const & L, TMatrixU const & U)
198
+ PBAT_HOST_DEVICE auto
199
+ PointAxisAlignedBoundingBox (TMatrixP const & P, TMatrixL const & L, TMatrixU const & U) ->
200
+ typename TMatrixP::ScalarType
164
201
{
165
202
// If point is inside AABB, then distance is 0.
166
203
bool const bIsInsideBox = OverlapQueries::PointAxisAlignedBoundingBox (P, L, U);
@@ -176,8 +213,9 @@ template <
176
213
mini::CMatrix TMatrixA,
177
214
mini::CMatrix TMatrixB,
178
215
mini::CMatrix TMatrixC>
179
- PBAT_HOST_DEVICE typename TMatrixP::ScalarType
180
- PointTriangle (TMatrixP const & P, TMatrixA const & A, TMatrixB const & B, TMatrixC const & C)
216
+ PBAT_HOST_DEVICE auto
217
+ PointTriangle (TMatrixP const & P, TMatrixA const & A, TMatrixB const & B, TMatrixC const & C) ->
218
+ typename TMatrixP::ScalarType
181
219
{
182
220
auto const PP = ClosestPointQueries::PointInTriangle (P, A, B, C);
183
221
return SquaredNorm (P - PP);
@@ -189,12 +227,12 @@ template <
189
227
mini::CMatrix TMatrixB,
190
228
mini::CMatrix TMatrixC,
191
229
mini::CMatrix TMatrixD>
192
- PBAT_HOST_DEVICE typename TMatrixP::ScalarType PointTetrahedron (
230
+ PBAT_HOST_DEVICE auto PointTetrahedron (
193
231
TMatrixP const & P,
194
232
TMatrixA const & A,
195
233
TMatrixB const & B,
196
234
TMatrixC const & C,
197
- TMatrixD const & D)
235
+ TMatrixD const & D) -> typename TMatrixP::ScalarType
198
236
{
199
237
bool const bPointInTetrahedron = OverlapQueries::PointTetrahedron3D (P, A, B, C, D);
200
238
if (bPointInTetrahedron)
@@ -211,8 +249,8 @@ PBAT_HOST_DEVICE typename TMatrixP::ScalarType PointTetrahedron(
211
249
}
212
250
213
251
template <mini::CMatrix TMatrixX, mini::CMatrix TMatrixP, mini::CMatrix TMatrixN>
214
- PBAT_HOST_DEVICE typename TMatrixX::ScalarType
215
- PointPlane (TMatrixX const & X, TMatrixP const & P, TMatrixN const & n)
252
+ PBAT_HOST_DEVICE auto PointPlane ( TMatrixX const & X, TMatrixP const & P, TMatrixN const & n) ->
253
+ typename TMatrixX::ScalarType
216
254
{
217
255
return Dot (X - P, n);
218
256
}
@@ -222,8 +260,9 @@ template <
222
260
mini::CMatrix TMatrixA,
223
261
mini::CMatrix TMatrixB,
224
262
mini::CMatrix TMatrixC>
225
- PBAT_HOST_DEVICE typename TMatrixX::ScalarType
226
- PointPlane (TMatrixX const & X, TMatrixA const & A, TMatrixB const & B, TMatrixC const & C)
263
+ PBAT_HOST_DEVICE auto
264
+ PointPlane (TMatrixX const & X, TMatrixA const & A, TMatrixB const & B, TMatrixC const & C) ->
265
+ typename TMatrixX::ScalarType
227
266
{
228
267
auto const n = Cross (B - A, C - A);
229
268
return PointPlane (X, A, n);
@@ -234,12 +273,12 @@ template <
234
273
mini::CMatrix TMatrixA,
235
274
mini::CMatrix TMatrixB,
236
275
mini::CMatrix TMatrixC>
237
- PBAT_HOST_DEVICE typename TMatrixX::ScalarType SphereTriangle (
276
+ PBAT_HOST_DEVICE auto SphereTriangle (
238
277
TMatrixX const & X,
239
278
typename TMatrixX::ScalarType R,
240
279
TMatrixA const & A,
241
280
TMatrixB const & B,
242
- TMatrixC const & C)
281
+ TMatrixC const & C) -> typename TMatrixX::ScalarType
243
282
{
244
283
auto const sd2c = PointTriangle (X, A, B, C);
245
284
return sd2c - R * R;
0 commit comments