/
tstDetailsAlgorithms.cpp
358 lines (314 loc) · 14.4 KB
/
tstDetailsAlgorithms.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
/****************************************************************************
* Copyright (c) 2017-2022 by the ArborX authors *
* All rights reserved. *
* *
* This file is part of the ArborX library. ArborX is *
* distributed under a BSD 3-clause license. For the licensing terms see *
* the LICENSE file in the top-level directory. *
* *
* SPDX-License-Identifier: BSD-3-Clause *
****************************************************************************/
#include <ArborX_DetailsAlgorithms.hpp>
#include <ArborX_DetailsKokkosExtArithmeticTraits.hpp>
#include <ArborX_HyperBox.hpp>
#include <ArborX_HyperPoint.hpp>
#include <ArborX_HyperSphere.hpp>
#include <ArborX_HyperTriangle.hpp>
#include <boost/mpl/list.hpp>
#define BOOST_TEST_MODULE Geometry
#include <boost/test/unit_test.hpp>
using Point = ArborX::ExperimentalHyperGeometry::Point<3>;
using Box = ArborX::ExperimentalHyperGeometry::Box<3>;
using Sphere = ArborX::ExperimentalHyperGeometry::Sphere<3>;
using Triangle = ArborX::ExperimentalHyperGeometry::Triangle<3>;
BOOST_AUTO_TEST_CASE(distance_point_point)
{
using ArborX::Details::distance;
BOOST_TEST(distance(Point{{1.0, 2.0, 3.0}}, Point{{1.0, 1.0, 1.0}}) ==
std::sqrt(5.f));
}
BOOST_AUTO_TEST_CASE(distance_point_box)
{
using ArborX::Details::distance;
// box is unit cube
constexpr Box box{{{0.0, 0.0, 0.0}}, {{1.0, 1.0, 1.0}}};
// distance is zero if the point is inside the box
BOOST_TEST(distance(Point{{0.5, 0.5, 0.5}}, box) == 0.0);
// or anywhere on the boundary
BOOST_TEST(distance(Point{{0.0, 0.0, 0.5}}, box) == 0.0);
// normal projection onto center of one face
BOOST_TEST(distance(Point{{2.0, 0.5, 0.5}}, box) == 1.0);
// projection onto edge
BOOST_TEST(distance(Point{{2.0, 0.75, -1.0}}, box) == std::sqrt(2.f));
// projection onto corner node
BOOST_TEST(distance(Point{{-1.0, 2.0, 2.0}}, box) == std::sqrt(3.f));
}
BOOST_AUTO_TEST_CASE(distance_point_sphere)
{
using ArborX::Details::distance;
// unit sphere
constexpr Sphere sphere{{{0., 0., 0.}}, 1.};
BOOST_TEST(distance(Point{{.5, .5, .5}}, sphere) == 0.);
BOOST_TEST(distance(Point{{2., 0., 0.}}, sphere) == 1.);
BOOST_TEST(distance(Point{{1., 1., 1.}}, sphere) == std::sqrt(3.f) - 1.f);
}
BOOST_AUTO_TEST_CASE(distance_point_triangle)
{
using ArborX::Details::distance;
// Zones
// \ 2/
// \/
// 5 /\ 6
// / \
// / \
// \ / 0 \ /
// \/________\/
// 1 | 4 | 3
// | |
using Point2 = ArborX::ExperimentalHyperGeometry::Point<2>;
constexpr ArborX::ExperimentalHyperGeometry::Triangle<2> triangle2{
Point2{-1, 0}, Point2{1, 0}, Point2{0, 1}};
// vertices
BOOST_TEST(distance(Point2{-1, 0}, triangle2) == 0);
BOOST_TEST(distance(Point2{1, 0}, triangle2) == 0);
BOOST_TEST(distance(Point2{0, 1}, triangle2) == 0);
// mid edges
BOOST_TEST(distance(Point2{-0.5f, 0.5f}, triangle2) == 0);
BOOST_TEST(distance(Point2{0.5f, 0.5f}, triangle2) == 0);
BOOST_TEST(distance(Point2{0, 0}, triangle2) == 0);
// inside
BOOST_TEST(distance(Point2{0, 0.5f}, triangle2) == 0);
// outside zone 1
BOOST_TEST(distance(Point2{-2, 0}, triangle2) == 1);
// outside zone 2
BOOST_TEST(distance(Point2{0, 2}, triangle2) == 1);
// outside zone 3
BOOST_TEST(distance(Point2{2, 0}, triangle2) == 1);
// outside zone 4
BOOST_TEST(distance(Point2{0, -1}, triangle2) == 1);
// outside zone 5
BOOST_TEST(distance(Point2{-1, 1}, triangle2) == std::sqrt(2.f) / 2);
// outside zone 6
BOOST_TEST(distance(Point2{1, 1}, triangle2) == std::sqrt(2.f) / 2);
using Point3 = ArborX::ExperimentalHyperGeometry::Point<3>;
constexpr ArborX::ExperimentalHyperGeometry::Triangle<3> triangle3{
Point3{1, 0, 0}, Point3{0, 1, 0}, Point3{0, 0, 0}};
// same plane
BOOST_TEST(distance(Point3{2, 0, 0}, triangle3) == 1);
BOOST_TEST(distance(Point3{0.5f, -0.5f, 0}, triangle3) == 0.5f);
BOOST_TEST(distance(Point3{-0.5f, 0.5f, 0}, triangle3) == 0.5f);
// projected to inside
BOOST_TEST(distance(Point3{0, 0, 1}, triangle3) == 1);
BOOST_TEST(distance(Point3{1, 0, -1}, triangle3) == 1);
BOOST_TEST(distance(Point3{0, 1, 2}, triangle3) == 2);
BOOST_TEST(distance(Point3{0.25f, 0.25f, 2.f}, triangle3) == 2);
// projected outside
BOOST_TEST(distance(Point3{-1, 0, 1}, triangle3) == std::sqrt(2.f));
BOOST_TEST(distance(Point3{0, -1, -1}, triangle3) == std::sqrt(2.f));
BOOST_TEST(distance(Point3{2, -1, -1}, triangle3) == std::sqrt(3.f));
constexpr ArborX::ExperimentalHyperGeometry::Triangle<3> triangle3_2{
Point3{0, 0, 0}, Point3{0, 1, 0}, Point3{0, 0, 1}};
BOOST_TEST(distance(Point3{-1, 0, 1}, triangle3_2) == 1);
BOOST_TEST(distance(Point3{0, -1, -1}, triangle3_2) == std::sqrt(2.f));
BOOST_TEST(distance(Point3{1, -1, -1}, triangle3_2) == std::sqrt(3.f));
}
BOOST_AUTO_TEST_CASE(distance_box_box)
{
using ArborX::Details::distance;
namespace KokkosExt = ArborX::Details::KokkosExt;
constexpr Box unit_box{{{0.0, 0.0, 0.0}}, {{1.0, 1.0, 1.0}}};
// distance to self
BOOST_TEST(distance(unit_box, unit_box) == 0);
// distance to another unit box translated along one axis
BOOST_TEST(distance(unit_box, Box{{{2, 0, 0}}, {{3, 1, 1}}}) == 1);
BOOST_TEST(distance(unit_box, Box{{{0, -3, 0}}, {{1, -2, 1}}}) == 2);
BOOST_TEST(distance(unit_box, Box{{{0, 0, 4}}, {{1, 1, 5}}}) == 3);
// distance to another unit box translated along a plane
BOOST_TEST(distance(unit_box, Box{{{-4, -4, 0}}, {{-3, -3, 1}}}) ==
3 * std::sqrt(2.f));
BOOST_TEST(distance(unit_box, Box{{{0, -2, 3}}, {{1, -1, 4}}}) ==
std::sqrt(5.f));
BOOST_TEST(distance(unit_box, Box{{{5, 0, 7}}, {{6, 1, 8}}}) ==
2 * std::sqrt(13.f));
// distance to another box that contains the box
BOOST_TEST(distance(unit_box, Box{{{-1, -2, -3}}, {{4, 5, 6}}}) == 0);
// distance to another box within the unit box
BOOST_TEST(distance(unit_box, Box{{{.1, .2, .3}}, {{.4, .5, .6}}}) == 0);
// distance to another box that overlaps with the unit box
BOOST_TEST(distance(unit_box, Box{{{.1, .2, .3}}, {{4, 5, 6}}}) == 0);
// distance to empty boxes
auto infinity = KokkosExt::ArithmeticTraits::infinity<float>::value;
BOOST_TEST(distance(unit_box, Box{}) == infinity);
BOOST_TEST(distance(Box{}, unit_box) == infinity);
BOOST_TEST(distance(Box{}, Box{}) == infinity);
}
BOOST_AUTO_TEST_CASE(distance_sphere_box)
{
using ArborX::Details::distance;
namespace KokkosExt = ArborX::Details::KokkosExt;
auto infinity = KokkosExt::ArithmeticTraits::infinity<float>::value;
// unit sphere
constexpr Sphere sphere{{{0., 0., 0.}}, 1.};
// distance between a sphere and a box no intersection
BOOST_TEST(distance(sphere, Box{{2.0, 3.0, 4.0}, {2.5, 3.5, 4.5}}) ==
std::sqrt(29.f) - 1.f);
// distance between a sphere and a box with intersection
BOOST_TEST(distance(sphere, Box{{0.5, 0.5, 0.5}, {2.5, 3.5, 4.5}}) == 0.f);
// distance between a sphere included in a box and that box
BOOST_TEST(distance(sphere, Box{{-2., -2., -2.}, {2., 2., 2.}}) == 0.f);
// distance between a sphere and a box included in that sphere
BOOST_TEST(distance(sphere, Box{{0., 0., 0.}, {0.1, 0.2, 0.3}}) == 0.f);
// distance to empty box
BOOST_TEST(distance(sphere, Box{}) == infinity);
}
BOOST_AUTO_TEST_CASE(intersects)
{
using ArborX::Details::intersects;
// uninitialized box does not intersect with other boxes
static_assert(!intersects(Box{}, Box{{{1.0, 2.0, 3.0}}, {{4.0, 5.0, 6.0}}}));
// uninitialized box does not even intersect with itself
static_assert(!intersects(Box{}, Box{}));
// box with zero extent does
static_assert(intersects(Box{{{0.0, 0.0, 0.0}}, {{0.0, 0.0, 0.0}}},
Box{{{0.0, 0.0, 0.0}}, {{0.0, 0.0, 0.0}}}));
// point
constexpr Point point{{1.0, 1.0, 1.0}};
// point is contained in a box
static_assert(intersects(point, Box{{{0.0, 0.0, 0.0}}, {{2.0, 2.0, 2.0}}}));
static_assert(
!intersects(point, Box{{{-1.0, -1.0, -1.0}}, {{0.0, 0.0, 0.0}}}));
// point is on a side of a box
static_assert(intersects(point, Box{{{0.0, 0.0, 0.0}}, {{2.0, 2.0, 1.0}}}));
// point is a corner of a box
static_assert(intersects(point, Box{{{0.0, 0.0, 0.0}}, {{1.0, 1.0, 1.0}}}));
// unit cube
constexpr Box box{{{0.0, 0.0, 0.0}}, {{1.0, 1.0, 1.0}}};
static_assert(intersects(box, box));
static_assert(!intersects(box, Box{}));
// smaller box inside
static_assert(
intersects(box, Box{{{0.25, 0.25, 0.25}}, {{0.75, 0.75, 0.75}}}));
// bigger box that contains it
static_assert(intersects(box, Box{{{-1.0, -1.0, -1.0}}, {{2.0, 2.0, 2.0}}}));
// couple boxes that do intersect
static_assert(intersects(box, Box{{{0.5, 0.5, 0.5}}, {{1.5, 1.5, 1.5}}}));
static_assert(intersects(box, Box{{{-0.5, -0.5, -0.5}}, {{0.5, 0.5, 0.5}}}));
// couple boxes that do not
static_assert(
!intersects(box, Box{{{-2.0, -2.0, -2.0}}, {{-1.0, -1.0, -1.0}}}));
static_assert(!intersects(box, Box{{{0.0, 0.0, 2.0}}, {{1.0, 1.0, 3.0}}}));
// boxes intersect if faces touch
static_assert(intersects(box, Box{{{1.0, 0.0, 0.0}}, {{2.0, 1.0, 1.0}}}));
static_assert(intersects(box, Box{{{-0.5, -0.5, -0.5}}, {{0.5, 0.0, 0.5}}}));
// unit sphere
constexpr Sphere sphere{{{0., 0., 0.}}, 1.};
BOOST_TEST(intersects(sphere, Box{{{0., 0., 0.}}, {{1., 1., 1.}}}));
BOOST_TEST(!intersects(sphere, Box{{{1., 2., 3.}}, {{4., 5., 6.}}}));
BOOST_TEST(intersects(sphere, Point{0., 0.5, 0.5}));
BOOST_TEST(intersects(sphere, Point{0., 0., 1.0}));
BOOST_TEST(intersects(Point{-1., 0., 0.}, sphere));
BOOST_TEST(intersects(Point{-0.6, -0.8, 0.}, sphere));
BOOST_TEST(!intersects(Point{-0.7, -0.8, 0.}, sphere));
// triangle
using Point2 = ArborX::ExperimentalHyperGeometry::Point<2>;
constexpr ArborX::ExperimentalHyperGeometry::Triangle<2> triangle{
{{0, 0}}, {{1, 0}}, {{0, 2}}};
BOOST_TEST(intersects(Point2{{0, 0}}, triangle));
BOOST_TEST(intersects(Point2{{1, 0}}, triangle));
BOOST_TEST(intersects(Point2{{0, 2}}, triangle));
BOOST_TEST(intersects(Point2{{0.5, 0}}, triangle));
BOOST_TEST(intersects(Point2{{0.5, 1}}, triangle));
BOOST_TEST(intersects(Point2{{0, 1}}, triangle));
BOOST_TEST(intersects(Point2{{0.25, 0.5}}, triangle));
BOOST_TEST(!intersects(Point2{{1, 1}}, triangle));
BOOST_TEST(!intersects(Point2{{0.5, 1.1}}, triangle));
BOOST_TEST(!intersects(Point2{{1.1, 0}}, triangle));
BOOST_TEST(!intersects(Point2{{-0.1, 0}}, triangle));
}
BOOST_AUTO_TEST_CASE(equals)
{
using ArborX::Details::equals;
// points
static_assert(equals(Point{{0., 0., 0.}}, {{0., 0., 0.}}));
static_assert(!equals(Point{{0., 0., 0.}}, {{1., 1., 1.}}));
// boxes
static_assert(equals(Box{{{0.0, 0.0, 0.0}}, {{1.0, 1.0, 1.0}}},
{{{0.0, 0.0, 0.0}}, {{1.0, 1.0, 1.0}}}));
static_assert(!equals(Box{{{0.0, 0.0, 0.0}}, {{1.0, 0.0, 1.0}}},
{{{-1.0, -1.0, -1.0}}, {{1.0, 1.0, 1.0}}}));
// spheres
static_assert(equals(Sphere{{{0., 0., 0.}}, 1.}, {{{0., 0., 0.}}, 1.}));
static_assert(!equals(Sphere{{{0., 0., 0.}}, 1.}, {{{0., 1., 2.}}, 1.}));
static_assert(!equals(Sphere{{{0., 0., 0.}}, 1.}, {{{0., 0., 0.}}, 2.}));
}
BOOST_AUTO_TEST_CASE(expand)
{
using ArborX::Details::equals;
using ArborX::Details::expand;
Box box;
// expand box with points
expand(box, Point{{0.0, 0.0, 0.0}});
BOOST_TEST(equals(box, Box{{{0.0, 0.0, 0.0}}, {{0.0, 0.0, 0.0}}}));
expand(box, Point{{1.0, 1.0, 1.0}});
BOOST_TEST(equals(box, Box{{{0.0, 0.0, 0.0}}, {{1.0, 1.0, 1.0}}}));
expand(box, Point{{0.25, 0.75, 0.25}});
BOOST_TEST(equals(box, Box{{{0.0, 0.0, 0.0}}, {{1.0, 1.0, 1.0}}}));
expand(box, Point{{-1.0, -1.0, -1.0}});
BOOST_TEST(equals(box, Box{{{-1.0, -1.0, -1.0}}, {{1.0, 1.0, 1.0}}}));
// expand box with boxes
expand(box, Box{{{0.25, 0.25, 0.25}}, {{0.75, 0.75, 0.75}}});
BOOST_TEST(equals(box, {{{-1.0, -1.0, -1.0}}, {{1.0, 1.0, 1.0}}}));
expand(box, Box{{{10.0, 10.0, 10.0}}, {{11.0, 11.0, 11.0}}});
BOOST_TEST(equals(box, {{{-1.0, -1.0, -1.0}}, {{11.0, 11.0, 11.0}}}));
// expand box with spheres
expand(box, Sphere{{{0., 1., 2.}}, 3.});
BOOST_TEST(equals(box, Box{{{-3., -2., -1.}}, {{11., 11., 11.}}}));
expand(box, Sphere{{{0., 0., 0.}}, 1.});
BOOST_TEST(equals(box, Box{{{-3., -2., -1.}}, {{11., 11., 11.}}}));
expand(box, Sphere{{{0., 0., 0.}}, 24.});
BOOST_TEST(equals(box, Box{{{-24., -24., -24.}}, {{24., 24., 24.}}}));
// expand box with triangles
expand(box, Triangle{{{-1, -1, 0}}, {{2, 2, 2}}, {{1, 1, 0}}});
BOOST_TEST(equals(box, Box{{{-24., -24., -24.}}, {{24., 24., 24.}}}));
expand(box, Triangle{{{0, 0, 0}}, {{48, 0, 0}}, {{0, 48, 0}}});
BOOST_TEST(equals(box, Box{{{-24., -24., -24.}}, {{48., 48., 24.}}}));
}
BOOST_AUTO_TEST_CASE(centroid)
{
using ArborX::Details::returnCentroid;
Box box{{{-10.0, 0.0, 10.0}}, {{0.0, 10.0, 20.0}}};
auto center = returnCentroid(box);
BOOST_TEST(center[0] == -5.0);
BOOST_TEST(center[1] == 5.0);
BOOST_TEST(center[2] == 15.0);
Triangle tri2{{{-1, -0.5}}, {{1, -0.5}}, {{0, 1}}};
auto tri2_center = returnCentroid(tri2);
BOOST_TEST(tri2_center[0] == 0);
BOOST_TEST(tri2_center[1] == 0);
Triangle tri3{{{0, 0, -2}}, {{3, 0, 1}}, {{0, 3, 1}}};
auto tri3_center = returnCentroid(tri3);
BOOST_TEST(tri3_center[0] == 1);
BOOST_TEST(tri3_center[1] == 1);
BOOST_TEST(tri3_center[2] == 0);
}
BOOST_AUTO_TEST_CASE(is_valid)
{
using ArborX::Details::isValid;
auto infty = std::numeric_limits<float>::infinity();
BOOST_TEST(isValid(Point{{1., 2., 3.}}));
BOOST_TEST(!isValid(Point{{0., infty, 0.}}));
BOOST_TEST(isValid(Box{{{1., 2., 3.}}, {{4., 5., 6.}}}));
BOOST_TEST(!isValid(Box{{{0., 0., 0.}}, {{0., 0., 0.}}}));
BOOST_TEST(!isValid(Box{{{0., 0., -infty}}, {{0., 0., 0.}}}));
BOOST_TEST(!isValid(Box{{{0., 0., 0.}}, {{+infty, 0., 0.}}}));
BOOST_TEST(!isValid(Box{}));
BOOST_TEST(!isValid(Box{{{1., 1., 1.}}, {{0., 0., 0.}}}));
BOOST_TEST(!isValid(Box{{{0., 0., 1.}}, {{0., 0., 0.}}}));
BOOST_TEST(!isValid(Box{{{0., 0., 0.}}, {{-1, 0., 0.}}}));
BOOST_TEST(isValid(Sphere{{{1., 2., 3.}}, 4.}));
BOOST_TEST(isValid(Sphere{{{0., 0., 0.}}, 0.}));
BOOST_TEST(!isValid(Sphere{{{1., 2., 3.}}, -1.}));
BOOST_TEST(!isValid(Sphere{{{0., 0., 0.}}, +infty}));
BOOST_TEST(!isValid(Sphere{{{0., -infty, 0.}}, +1.}));
BOOST_TEST(isValid(Sphere{}));
}