9 #include <boost/test/unit_test.hpp>
24 std::vector<std::pair<std::array<double, 3>,
int>> test_vector{
25 {{-8.5, 2.3, -1.6}, 84}, {{-9.7, -1.4, 7.6}, -56},
26 {{6.4, 2.7, -1.0}, -94}, {{-2.2, -9.3, 3.6}, 56},
27 {{-9.2, -2.8, -2.5}, -90}, {{3.8, 0.9, 7.2}, 43},
28 {{7.6, 1.9, -1.0}, 83}, {{2.1, -1.6, -1.7}, -15},
29 {{5.2, 3.2, -1.3}, 14}, {{-6.0, -7.5, 6.5}, 48},
30 {{3.5, -7.1, -4.4}, 83}, {{5.0, 6.9, -0.7}, 1},
31 {{8.0, 0.2, 3.8}, 69}, {{2.5, 0.8, 4.1}, -11},
32 {{-1.5, -6.6, 8.4}, -98}, {{0.8, 6.1, 2.7}, 14},
33 {{5.1, -5.1, 5.8}, 46}, {{-9.6, 0.7, 0.3}, 59},
34 {{-8.8, 1.3, -9.8}, -19}, {{-7.5, -9.3, -2.2}, 6},
35 {{-5.3, 1.9, -3.9}, 57}, {{-4.8, -9.8, -0.2}, 73},
36 {{-6.3, -7.4, 9.2}, -94}, {{-4.8, -9.8, -2.6}, 77},
37 {{2.4, 4.9, -8.4}, -72}, {{8.4, 6.1, -7.7}, -66},
38 {{6.1, -8.2, 9.3}, 67}, {{9.4, 7.4, 7.5}, -99},
39 {{9.4, 1.8, -4.4}, -87}, {{9.0, -3.7, 6.3}, -12},
40 {{-1.8, 2.8, 6.3}, 71}, {{-7.6, 7.6, -4.5}, -77},
41 {{-7.7, -1.2, 7.7}, -92}, {{-6.9, -0.5, -6.6}, -61},
42 {{0.9, 5.7, 0.5}, 60}, {{-0.3, 3.7, -7.0}, 15},
43 {{5.3, -0.6, -6.6}, 46}, {{-6.9, -3.0, 4.1}, -13},
44 {{1.8, -6.5, 1.8}, -29}, {{1.4, -7.8, -0.3}, 100},
45 {{8.2, 0.1, -7.6}, 69}, {{6.5, -9.1, 6.6}, -61},
46 {{4.2, 6.6, -0.7}, 56}, {{8.6, -3.1, -6.8}, -88},
47 {{9.7, -6.0, 2.9}, -44}, {{7.0, -3.2, 9.8}, 29},
48 {{-6.1, 3.3, -3.0}, 54}, {{-2.6, 7.6, -4.2}, -52},
49 {{-5.9, 7.8, 5.1}, -81}, {{0.6, 5.3, -2.5}, -69},
50 {{-8.5, 1.8, 6.0}, 56}, {{6.4, 2.0, -6.8}, -14},
51 {{-9.0, -2.1, 8.0}, 84}, {{7.0, 0.1, 6.3}, -17},
52 {{8.0, 4.5, -0.8}, -85}, {{-5.5, 9.2, 7.5}, 13},
53 {{-3.8, 2.1, 3.4}, 19}, {{-8.0, -5.2, 9.5}, -25},
54 {{6.2, 6.0, -2.2}, 81}, {{5.1, -5.1, -9.7}, 74},
55 {{-1.3, -9.0, 8.1}, -74}, {{5.4, -0.8, 2.4}, 32},
56 {{-5.6, 3.8, 6.2}, 73}, {{8.9, -3.8, -7.8}, 93},
57 {{7.7, 0.4, -2.9}, 78}, {{-6.9, -1.5, -3.3}, -75},
58 {{-5.3, 2.5, -3.6}, 48}, {{-6.6, -9.2, -2.1}, -7},
59 {{5.5, -7.7, 2.0}, 3}, {{4.1, -9.5, -6.9}, 82},
60 {{-2.3, -0.2, -0.7}, -3}, {{2.3, 0.4, -5.5}, 43},
61 {{5.0, 4.0, 9.0}, 59}, {{-8.7, -4.0, 1.0}, -17},
62 {{7.6, -7.6, -7.9}, 18}, {{4.2, 6.3, -4.4}, 2},
63 {{8.6, -6.3, 3.5}, -75}, {{9.8, 7.3, 0.1}, 8},
64 {{-2.2, 9.3, -6.2}, -54}, {{-0.9, -0.4, 1.4}, 45},
65 {{-5.3, 6.7, 7.6}, 20}, {{-2.7, 2.4, 8.8}, 42},
66 {{9.3, -0.0, 9.1}, -84}, {{0.5, 1.5, -2.3}, -47},
67 {{7.5, -5.7, 1.3}, 21}, {{0.4, 0.4, -2.0}, 50},
68 {{0.8, -6.2, -7.7}, 46}, {{5.1, -7.3, -0.7}, 26},
69 {{9.7, 2.8, 9.6}, 80}, {{7.3, -2.1, -6.7}, -91},
70 {{-9.4, -5.3, -3.1}, -24}, {{-2.4, -1.6, -0.2}, -88},
71 {{-9.9, -5.9, 0.0}, -90}, {{1.3, -3.0, 9.8}, 72},
72 {{0.9, -6.8, -2.7}, 92}, {{-1.7, -3.8, 2.8}, -78},
73 {{-6.4, -0.6, -0.6}, 95}, {{-4.7, 4.8, -8.0}, 95},
74 {{-6.0, 3.5, -7.4}, 7}, {{3.2, -6.2, 3.9}, -25}};
83 {{1.0}, 5}, {{2.0}, 6}, {{-1.2}, 2}, {{0.9}, 10}}) {}
105 {{1.0, 5.0}, 5}, {{2.0, -2.5}, 6}}) {}
113 {{-4.7, -2.0, -1.7}, 63}, {{8.2, -0.0, 9.5}, -82},
114 {{7.1, -3.6, -4.0}, -49}, {{-9.9, -4.6, 2.9}, 86},
115 {{5.0, -3.8, -2.8}, -12}, {{-4.8, -1.8, -1.6}, -60},
116 {{8.8, 9.6, -0.2}, 60}, {{7.6, 1.9, 8.8}, 74},
117 {{9.0, -6.9, -6.2}, 35}, {{4.3, 7.5, -2.0}, 19},
118 {{-7.7, 3.7, 7.1}, -54}, {{3.4, 7.4, -4.0}, -8},
119 {{8.5, 3.0, -3.2}, -48}, {{6.5, -0.3, -3.1}, -25},
120 {{6.9, -6.6, -8.0}, -4}, {{6.8, 5.5, -2.5}, 17},
121 {{-2.8, 8.8, -7.2}, 62}, {{-0.1, 3.5, 5.5}, -95},
122 {{-1.3, 6.9, 5.3}, -23}, {{6.2, 6.6, 7.1}, -84}}) {}
129 :
tree(std::vector<std::pair<std::
array<
double, 3>, int>>(test_vector)) {}
137 {{-100.0, -1.1, -1.7}, 0},
138 {{100.0, -0.0, 9.5}, 4},
139 {{-100.0, -0.6, -1.0}, 1},
140 {{100.0, -4.6, 2.9}, 5},
141 {{-100.0, -1.4, -2.8}, 2},
142 {{100.0, -1.8, -1.6}, 6},
143 {{-100.0, -1.0, -0.2}, 3},
144 {{100.0, 1.9, 8.8}, 7}}) {}
152 {{5.6, 7.5, -9.8, 9.6, 3.3, -7.3, 2.0, 4.7, -2.1, 5.9}, 32},
153 {{1.2, -1.5, -0.2, 0.9, 1.1, -1.4, -8.9, -1.2, -9.0, 7.4}, -66},
154 {{1.6, 6.2, 9.9, -2.5, 4.0, 8.9, 3.9, 8.4, -1.2, -4.1}, -51},
155 {{0.7, -5.7, -3.1, 5.4, 0.6, -8.7, 0.2, -0.2, 0.3, -2.7}, -19},
156 {{-5.0, -5.5, -7.1, 9.2, 5.5, 0.7, 9.9, -7.0, -6.8, 3.1}, 27},
157 {{0.4, 5.5, -5.8, -3.0, 6.0, -7.3, 2.1, 7.9, -8.0, -6.9}, -13},
158 {{-2.3, -5.9, 9.0, 1.4, 4.6, 3.4, -9.5, -6.3, 3.3, -7.0}, 97},
159 {{8.1, 3.6, -8.6, -0.4, 7.5, 10.0, -3.9, -5.8, -2.9, 9.8}, 15},
160 {{8.4, -4.0, 6.3, 1.1, -5.7, 8.1, -8.0, -2.5, -0.5, 3.2}, -56},
161 {{2.3, 5.8, 1.4, 4.0, 9.0, -6.4, 1.0, -7.8, 4.3, -5.3}, -83}}) {}
169 {{-0.2, -0.2, -3.8},
"string0"},
170 {{4.9, -7.8, -10.0},
"string1"},
171 {{3.5, -10.0, -8.1},
"string2"},
172 {{8.2, -6.1, 2.0},
"string3"},
173 {{-9.9, 7.7, -1.4},
"string4"},
174 {{-2.2, 2.1, 8.6},
"string5"},
175 {{-6.9, -5.8, 5.3},
"string6"},
176 {{-0.7, 0.9, -6.5},
"string7"},
177 {{-2.7, -5.9, -7.3},
"string8"},
178 {{3.1, -9.4, -2.5},
"string9"}}) {}
185 :
tree(std::vector<std::pair<std::
array<int, 1>, int>>{
186 {{1}, 5}, {{2}, 6}, {{-1}, 2}, {{5}, 10}}) {}
193 :
tree(std::vector<std::pair<std::
array<int, 2>, int>>{
194 {{1, 7}, 5}, {{2, 1}, 6}, {{-1, -11}, 2}, {{5, -2}, 10}}) {}
199 BOOST_AUTO_TEST_SUITE(Utilities)
201 BOOST_AUTO_TEST_SUITE(KDTree)
204 BOOST_CHECK_EQUAL(
tree.size(), 4);
208 BOOST_CHECK_EQUAL(
tree.size(), 7);
212 BOOST_CHECK_EQUAL(
tree.size(), 2);
216 BOOST_CHECK_EQUAL(
tree.size(), 20);
220 BOOST_CHECK_EQUAL(
tree.size(), 10);
224 BOOST_CHECK_EQUAL(
tree.size(), 10);
228 BOOST_CHECK_EQUAL(
tree.size(), 4);
232 BOOST_CHECK_EQUAL(
tree.size(), 100);
237 range[0].shrink(0.0, 2.5);
239 std::vector<int> result =
tree.rangeSearch(range);
240 BOOST_CHECK_EQUAL(result.size(), 3);
241 BOOST_CHECK((std::find(result.begin(), result.end(), 5) != result.end()));
242 BOOST_CHECK((std::find(result.begin(), result.end(), 6) != result.end()));
243 BOOST_CHECK((std::find(result.begin(), result.end(), 10) != result.end()));
244 BOOST_CHECK((std::find(result.begin(), result.end(), 7) == result.end()));
245 BOOST_CHECK((std::find(result.begin(), result.end(), 2) == result.end()));
246 BOOST_CHECK((std::find(result.begin(), result.end(), 9) == result.end()));
251 range[0].shrink(-10000.0, 10000.0);
253 std::vector<int> result =
tree.rangeSearch(range);
254 BOOST_CHECK_EQUAL(result.size(), 7);
255 BOOST_CHECK((std::find(result.begin(), result.end(), 1) != result.end()));
256 BOOST_CHECK((std::find(result.begin(), result.end(), 2) != result.end()));
257 BOOST_CHECK((std::find(result.begin(), result.end(), 3) != result.end()));
258 BOOST_CHECK((std::find(result.begin(), result.end(), 5) != result.end()));
259 BOOST_CHECK((std::find(result.begin(), result.end(), 6) != result.end()));
260 BOOST_CHECK((std::find(result.begin(), result.end(), 9) != result.end()));
261 BOOST_CHECK((std::find(result.begin(), result.end(), 10) != result.end()));
266 range[0].shrink(5000.0, 10000.0);
268 std::vector<int> result =
tree.rangeSearch(range);
269 BOOST_CHECK_EQUAL(result.size(), 0);
274 range[0].shrink(0.0, 10.0);
275 range[1].shrink(0.0, 10.0);
277 std::vector<int> result =
tree.rangeSearch(range);
278 BOOST_CHECK_EQUAL(result.size(), 1);
279 BOOST_CHECK((std::find(result.begin(), result.end(), 5) != result.end()));
284 range[0].shrink(0.0, 10.0);
285 range[1].shrink(-10.0, 10.0);
287 std::vector<int> result =
tree.rangeSearch(range);
288 BOOST_CHECK_EQUAL(result.size(), 2);
289 BOOST_CHECK((std::find(result.begin(), result.end(), 5) != result.end()));
290 BOOST_CHECK((std::find(result.begin(), result.end(), 6) != result.end()));
295 range[0].shrink(0.0, 5.0);
297 std::vector<int> result =
tree.rangeSearch(range);
298 BOOST_CHECK_EQUAL(result.size(), 5);
299 BOOST_CHECK((std::find(result.begin(), result.end(), -66) != result.end()));
300 BOOST_CHECK((std::find(result.begin(), result.end(), -51) != result.end()));
301 BOOST_CHECK((std::find(result.begin(), result.end(), -19) != result.end()));
302 BOOST_CHECK((std::find(result.begin(), result.end(), -13) != result.end()));
303 BOOST_CHECK((std::find(result.begin(), result.end(), -83) != result.end()));
308 range[9].shrink(0.0, 5.0);
310 std::vector<int> result =
tree.rangeSearch(range);
311 BOOST_CHECK_EQUAL(result.size(), 2);
312 BOOST_CHECK((std::find(result.begin(), result.end(), 27) != result.end()));
313 BOOST_CHECK((std::find(result.begin(), result.end(), -56) != result.end()));
318 range[0].shrink(-5.0, 5.0);
319 range[1].shrink(-5.0, 5.0);
320 range[2].shrink(-5.0, 5.0);
322 std::vector<std::string> result =
tree.rangeSearch(range);
323 BOOST_CHECK_EQUAL(result.size(), 1);
325 (std::find(result.begin(), result.end(),
"string0") != result.end()));
330 range[0].shrink(-10.0, 10.0);
331 range[1].shrink(-10.0, 10.0);
332 range[2].shrink(-5.0, 5.0);
334 std::vector<std::string> result =
tree.rangeSearch(range);
335 BOOST_CHECK_EQUAL(result.size(), 4);
337 (std::find(result.begin(), result.end(),
"string0") != result.end()));
339 (std::find(result.begin(), result.end(),
"string3") != result.end()));
341 (std::find(result.begin(), result.end(),
"string4") != result.end()));
343 (std::find(result.begin(), result.end(),
"string9") != result.end()));
348 range[0].shrink(0, 3);
350 std::vector<int> result =
tree.rangeSearch(range);
351 BOOST_CHECK_EQUAL(result.size(), 2);
352 BOOST_CHECK((std::find(result.begin(), result.end(), 5) != result.end()));
353 BOOST_CHECK((std::find(result.begin(), result.end(), 6) != result.end()));
358 range[1].shrink(0, 10);
360 std::vector<int> result =
tree.rangeSearch(range);
361 BOOST_CHECK_EQUAL(result.size(), 2);
362 BOOST_CHECK((std::find(result.begin(), result.end(), 5) != result.end()));
363 BOOST_CHECK((std::find(result.begin(), result.end(), 6) != result.end()));
368 range[0].shrink(0.0, 5.0);
370 std::vector<int> result;
371 tree.rangeSearch(range, result);
373 BOOST_CHECK_EQUAL(result.size(), 5);
374 BOOST_CHECK((std::find(result.begin(), result.end(), -66) != result.end()));
375 BOOST_CHECK((std::find(result.begin(), result.end(), -51) != result.end()));
376 BOOST_CHECK((std::find(result.begin(), result.end(), -19) != result.end()));
377 BOOST_CHECK((std::find(result.begin(), result.end(), -13) != result.end()));
378 BOOST_CHECK((std::find(result.begin(), result.end(), -83) != result.end()));
383 range[0].shrink(0.0, 5.0);
385 std::vector<int> result;
386 tree.rangeSearchInserter(range, std::back_inserter(result));
388 BOOST_CHECK_EQUAL(result.size(), 5);
389 BOOST_CHECK((std::find(result.begin(), result.end(), -66) != result.end()));
390 BOOST_CHECK((std::find(result.begin(), result.end(), -51) != result.end()));
391 BOOST_CHECK((std::find(result.begin(), result.end(), -19) != result.end()));
392 BOOST_CHECK((std::find(result.begin(), result.end(), -13) != result.end()));
393 BOOST_CHECK((std::find(result.begin(), result.end(), -83) != result.end()));
398 range[0].shrink(0.0, 5.0);
400 std::vector<int> result =
tree.rangeSearchMap<
int>(
402 [](
const std::array<double, 10>&,
const int&
i) ->
int {
return 2 *
i; });
404 BOOST_CHECK_EQUAL(result.size(), 5);
405 BOOST_CHECK((std::find(result.begin(), result.end(), -132) != result.end()));
406 BOOST_CHECK((std::find(result.begin(), result.end(), -102) != result.end()));
407 BOOST_CHECK((std::find(result.begin(), result.end(), -38) != result.end()));
408 BOOST_CHECK((std::find(result.begin(), result.end(), -26) != result.end()));
409 BOOST_CHECK((std::find(result.begin(), result.end(), -166) != result.end()));
414 range[0].shrink(0.0, 5.0);
416 std::vector<std::string> result;
420 [](
const std::array<double, 10>&,
const int&
i) ->
std::string {
423 std::back_inserter(result));
425 BOOST_CHECK_EQUAL(result.size(), 5);
426 BOOST_CHECK((std::find(result.begin(), result.end(),
"-66") != result.end()));
427 BOOST_CHECK((std::find(result.begin(), result.end(),
"-51") != result.end()));
428 BOOST_CHECK((std::find(result.begin(), result.end(),
"-19") != result.end()));
429 BOOST_CHECK((std::find(result.begin(), result.end(),
"-13") != result.end()));
430 BOOST_CHECK((std::find(result.begin(), result.end(),
"-83") != result.end()));
435 range[1].shrink(0, 10);
437 std::vector<int> result;
438 tree.rangeSearchMapInserter<
int>(
440 [](
const std::array<int, 2>&
c,
const int&
i) ->
int {
441 return i * (c[0] + c[1]);
443 std::back_inserter(result));
445 BOOST_CHECK_EQUAL(result.size(), 2);
446 BOOST_CHECK((std::find(result.begin(), result.end(), 40) != result.end()));
447 BOOST_CHECK((std::find(result.begin(), result.end(), 18) != result.end()));
452 range[1].shrink(-100, 5);
456 tree.rangeSearchMapDiscard(
457 range, [&result](
const std::array<int, 2>&
c,
const int&
i) {
458 result += i * (c[0] + c[1]);
461 BOOST_CHECK_EQUAL(result, 24);
466 range[0].shrinkMin(0.0);
470 tree.rangeSearchMapDiscard(range, [&result](
const std::array<double, 3>&,
471 const int&
i) { result +=
i; });
473 BOOST_CHECK_EQUAL(result, 555);
481 for (
double zmin = -10.0; zmin <= 10.0; zmin += 2.0) {
482 for (
double zmax = -10.0; zmax <= 10.0; zmax += 2.0) {
487 range[2].shrink(zmin, zmax);
489 std::vector<int>
valid;
491 for (
const std::pair<std::array<double, 3>,
int>&
i :
493 const std::array<double, 3>&
c =
i.first;
495 c[1] <
ymax && zmin <= c[2] && c[2] < zmax) {
496 valid.push_back(
i.second);
500 std::vector<int> result =
tree.rangeSearch(range);
502 BOOST_CHECK_EQUAL(result.size(), valid.size());
504 for (
int j : valid) {
505 BOOST_CHECK((std::find(result.begin(), result.end(),
j) !=
518 range1[0].shrink(-200, 0);
520 std::vector<int> result =
tree.rangeSearch(range1);
521 BOOST_CHECK_EQUAL(result.size(), 4);
522 BOOST_CHECK((std::find(result.begin(), result.end(), 0) != result.end()));
523 BOOST_CHECK((std::find(result.begin(), result.end(), 1) != result.end()));
524 BOOST_CHECK((std::find(result.begin(), result.end(), 2) != result.end()));
525 BOOST_CHECK((std::find(result.begin(), result.end(), 3) != result.end()));
530 range1[0].shrink(0, 200);
532 std::vector<int> result =
tree.rangeSearch(range1);
533 BOOST_CHECK_EQUAL(result.size(), 4);
534 BOOST_CHECK((std::find(result.begin(), result.end(), 4) != result.end()));
535 BOOST_CHECK((std::find(result.begin(), result.end(), 5) != result.end()));
536 BOOST_CHECK((std::find(result.begin(), result.end(), 6) != result.end()));
537 BOOST_CHECK((std::find(result.begin(), result.end(), 7) != result.end()));
543 std::vector<std::pair<std::array<double, 3>,
int>> points;
545 for (
double x = -10.0;
x < 10.0;
x += 0.5) {
546 for (
double y = -10.0;
y < 10.0;
y += 0.5) {
547 for (
double z = -10.0;
z < 10.0;
z += 0.5) {
548 points.push_back({{
x,
y,
z}, q++});
553 std::vector<std::pair<std::array<double, 3>,
int>> copy(points);
559 for (
double zmin = -10.0; zmin <= 10.0; zmin += 1.0) {
564 double zmax = zmin + 1.0;
566 range[0].shrink(
xmin, xmax);
567 range[1].shrink(
ymin, ymax);
568 range[2].shrink(zmin, zmax);
570 std::vector<int>
valid;
572 for (
const std::pair<std::array<double, 3>,
int>&
i : points) {
573 const std::array<double, 3>&
c =
i.first;
574 if (
xmin <= c[0] && c[0] < xmax &&
ymin <= c[1] && c[1] < ymax &&
575 zmin <= c[2] && c[2] < zmax) {
576 valid.push_back(
i.second);
582 BOOST_CHECK_EQUAL(result.size(), valid.size());
584 for (
int j : valid) {
586 (std::find(result.begin(), result.end(),
j) != result.end()));
596 std::vector<std::pair<std::array<double, 3>,
int>> points;
598 for (std::size_t
i = 0;
i < 50; ++
i) {
599 points.push_back({{64.0, 64.0, 64.0}, q++});
602 for (std::size_t
i = 0;
i < 50; ++
i) {
603 points.push_back({{-64.0, -64.0, -64.0}, q++});
609 range1[0].shrink(50.0, 70.0);
610 range1[1].shrink(50.0, 70.0);
611 range1[2].shrink(50.0, 70.0);
614 range2[0].shrink(-70.0, -50.0);
615 range2[1].shrink(-70.0, -50.0);
616 range2[2].shrink(-70.0, -50.0);
618 std::vector<int> result1 = tree.
rangeSearch(range1);
619 std::vector<int> result2 = tree.
rangeSearch(range2);
621 BOOST_CHECK_EQUAL(result1.size(), 50);
622 BOOST_CHECK_EQUAL(result2.size(), 50);
624 for (
int i = 0;
i < 50; ++
i) {
626 (std::find(result1.begin(), result1.end(),
i) != result1.end()));
629 for (
int i = 50;
i < 100; ++
i) {
631 (std::find(result2.begin(), result2.end(),
i) != result2.end()));
635 BOOST_AUTO_TEST_SUITE_END()
637 BOOST_AUTO_TEST_SUITE_END()