forked from attcs/Octree
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoctree.h
4340 lines (3608 loc) · 164 KB
/
octree.h
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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
MIT License
Copyright (c) 2021 Attila Csikós
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#ifndef ORTHOTREE_GUARD
#define ORTHOTREE_GUARD
#include <algorithm>
#include <concepts>
#include <execution>
#include <functional>
#include <iterator>
#include <numeric>
#include <optional>
#include <stdexcept>
#include <type_traits>
#include <array>
#include <bitset>
#include <map>
#include <queue>
#include <set>
#include <span>
#include <tuple>
#include <unordered_map>
#include <vector>
#include <assert.h>
#include <math.h>
#ifndef autoc
#define autoc auto const
#define undef_autoc
#endif
#ifndef autoce
#define autoce auto constexpr
#define undef_autoce
#endif
#if defined(__clang__)
#define LOOPIVDEP
#elif defined(__INTEL_COMPILER)
#define LOOPIVDEP _Pragma("ivdep")
#elif defined(__GNUC__)
#define LOOPIVDEP _Pragma("GCC ivdep")
#elif defined(_MSC_VER)
#define LOOPIVDEP _Pragma("loop(ivdep)")
#else
#define LOOPIVDEP
#endif
namespace OrthoTree
{
#ifdef __clang__
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wreturn-type"
#endif
#ifdef __GNUC__
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wreturn-type"
#endif
#ifdef _MSC_VER
#pragma warning(disable : 4715)
#endif
// Crash the program if out_of_range exception is raised
template<typename container_type>
inline auto& cont_at(container_type& container, typename std::remove_reference_t<container_type>::key_type const& id) noexcept
{
return container.at(id);
}
#ifdef _MSC_VER
#pragma warning(default : 4715)
#endif
#ifdef __clang__
#pragma clang diagnostic pop
#endif
#ifdef __GNUC__
#pragma GCC diagnostic pop
#endif
template<uint8_t e>
consteval std::size_t pow2_ce()
{
constexpr auto bitSize = sizeof(std::size_t) * CHAR_BIT;
static_assert(e >= 0 && e < bitSize);
return std::size_t{ 1 } << e;
}
constexpr int32_t pow2(int32_t e)
{
assert(e >= 0 && e < 31);
return int32_t(1) << e;
}
// Type of the dimension
using dim_t = int;
// Type of depth
using depth_t = int;
// Grid id
using GridID = uint32_t;
// Enum of relation with Planes
enum class PlaneRelation : char
{
Negative,
Hit,
Positive
};
// Adaptor concepts
template<class TAdapter, typename TVector, typename TBox, typename TRay, typename TPlane, typename TGeometry = double>
concept AdaptorBasicsConcept = requires(TVector& point, dim_t dimensionID, TGeometry value) {
{
TAdapter::SetPointC(point, dimensionID, value)
};
} && requires(TVector const& point, dim_t dimensionID) {
{
TAdapter::GetPointC(point, dimensionID)
} -> std::convertible_to<TGeometry>;
} && requires(TBox& box, dim_t dimensionID, TGeometry value) {
{
TAdapter::SetBoxMinC(box, dimensionID, value)
};
} && requires(TBox& box, dim_t dimensionID, TGeometry value) {
{
TAdapter::SetBoxMaxC(box, dimensionID, value)
};
} && requires(TBox const& box, dim_t dimensionID) {
{
TAdapter::GetBoxMinC(box, dimensionID)
} -> std::convertible_to<TGeometry>;
} && requires(TBox const& box, dim_t dimensionID) {
{
TAdapter::GetBoxMaxC(box, dimensionID)
} -> std::convertible_to<TGeometry>;
} && requires(TRay const& ray) {
{
TAdapter::GetRayOrigin(ray)
} -> std::convertible_to<TVector>;
} && requires(TRay const& ray) {
{
TAdapter::GetRayDirection(ray)
} -> std::convertible_to<TVector>;
} && requires(TPlane const& plane) {
{
TAdapter::GetPlaneNormal(plane)
} -> std::convertible_to<TVector>;
} && requires(TPlane const& plane) {
{
TAdapter::GetPlaneOrigoDistance(plane)
} -> std::convertible_to<TGeometry>;
};
template<class TAdapter, typename TVector, typename TBox, typename TRay, typename TPlane, typename TGeometry = double>
concept AdaptorConcept =
requires { AdaptorBasicsConcept<TAdapter, TVector, TBox, TRay, TPlane, TGeometry>; } && requires(TBox const& box, TVector const& point) {
{
TAdapter::DoesBoxContainPoint(box, point)
} -> std::convertible_to<bool>;
} && requires(TBox const& e1, TBox const& e2, bool e1_must_contain_e2) {
{
TAdapter::AreBoxesOverlapped(e1, e2, e1_must_contain_e2)
} -> std::convertible_to<bool>;
} && requires(TBox const& e1, TBox const& e2) {
{
TAdapter::AreBoxesOverlappedStrict(e1, e2)
} -> std::convertible_to<bool>;
} && requires(std::span<TVector const> const& points) {
{
TAdapter::GetBoxOfPoints(points)
} -> std::convertible_to<TBox>;
} && requires(std::span<TBox const> const& boxes) {
{
TAdapter::GetBoxOfBoxes(boxes)
} -> std::convertible_to<TBox>;
} && requires(TVector const& box, TGeometry distanceOfOrigo, TVector const& planeNormal, TGeometry tolerance) {
{
TAdapter::GetPointPlaneRelation(box, distanceOfOrigo, planeNormal, tolerance)
} -> std::convertible_to<PlaneRelation>;
} && requires(TBox const& box, TGeometry distanceOfOrigo, TVector const& planeNormal, TGeometry tolerance) {
{
TAdapter::GetBoxPlaneRelation(box, distanceOfOrigo, planeNormal, tolerance)
} -> std::convertible_to<PlaneRelation>;
} && requires(TBox const& box, TVector const& rayBasePoint, TVector const& rayHeading, TGeometry tolerance) {
{
TAdapter::IsRayHit(box, rayBasePoint, rayHeading, tolerance)
} -> std::convertible_to<std::optional<double>>;
} && requires(TBox const& box, TRay const& ray, TGeometry tolerance) {
{
TAdapter::IsRayHit(box, ray, tolerance)
} -> std::convertible_to<std::optional<double>>;
};
// Adaptors
template<dim_t DIMENSION_NO, typename TVector, typename TBox, typename TRay, typename TPlane, typename TGeometry = double>
struct AdaptorGeneralBasics
{
static constexpr TGeometry GetPointC(TVector const& point, dim_t dimensionID) noexcept { return point[dimensionID]; }
static constexpr void SetPointC(TVector& point, dim_t dimensionID, TGeometry value) noexcept { point[dimensionID] = value; }
static constexpr TGeometry GetBoxMinC(TBox const& box, dim_t dimensionID) noexcept { return box.Min[dimensionID]; }
static constexpr TGeometry GetBoxMaxC(TBox const& box, dim_t dimensionID) noexcept { return box.Max[dimensionID]; }
static constexpr void SetBoxMinC(TBox& box, dim_t dimensionID, TGeometry value) noexcept { box.Min[dimensionID] = value; }
static constexpr void SetBoxMaxC(TBox& box, dim_t dimensionID, TGeometry value) noexcept { box.Max[dimensionID] = value; }
static constexpr TVector const& GetRayDirection(TRay const& ray) noexcept { return ray.Direction; }
static constexpr TVector const& GetRayOrigin(TRay const& ray) noexcept { return ray.Origin; }
static constexpr TVector const& GetPlaneNormal(TPlane const& plane) noexcept { return plane.Normal; }
static constexpr TGeometry GetPlaneOrigoDistance(TPlane const& plane) noexcept { return plane.OrigoDistance; }
};
template<dim_t DIMENSION_NO, typename TVector, typename TBox, typename TRay, typename TPlane, typename TGeometry, typename TAdaptorBasics>
struct AdaptorGeneralBase : TAdaptorBasics
{
using Base = TAdaptorBasics;
static_assert(AdaptorBasicsConcept<Base, TVector, TBox, TRay, TPlane, TGeometry>);
static constexpr TGeometry Size2(TVector const& point) noexcept
{
auto d2 = TGeometry{ 0 };
for (dim_t dimensionID = 0; dimensionID < DIMENSION_NO; ++dimensionID)
{
autoc d = Base::GetPointC(point, dimensionID);
d2 += d * d;
}
return d2;
}
static constexpr TGeometry Size(TVector const& point) noexcept { return std::sqrt(Size2(point)); }
static constexpr TGeometry pose_size(TVector const& pt) noexcept
{
auto d2 = TGeometry{ 0 };
auto angle_dis = TGeometry{ 0 };
for (dim_t iDim = 0; iDim < DIMENSION_NO; ++iDim)
{
if (iDim < 3)
{
autoc d = Base::GetPointC(pt, iDim);
d2 += d * d;
}
else
{
angle_dis += Base::GetPointC(pt, iDim);
}
}
autoc translation_part = sqrt(d2);
autoc orientation_part = angle_dis / 3;
return translation_part + orientation_part;
}
static constexpr TGeometry translational_distance(TVector const& pt) noexcept
{
int trans_end = 0;
if (DIMENSION_NO == 3)
{
trans_end = 2;
}
else if (DIMENSION_NO == 6)
{
trans_end = 3;
}
else if (DIMENSION_NO == 7)
{
trans_end = 3;
}
else if (DIMENSION_NO == 2)
{
trans_end = 1;
}
if (trans_end == 0)
{
exit(0);
}
auto d2 = TGeometry{ 0 };
for (dim_t iDim = 0; iDim < trans_end; ++iDim)
{
autoc d = Base::GetPointC(pt, iDim);
d2 += d * d;
}
return sqrt(d2);
}
static constexpr TGeometry rotaional_distance_extra(TVector const& ptL, TVector const& ptR) noexcept
{
int rot_begin = 0;
int rot_end = 0;
if (DIMENSION_NO == 3)
{
rot_begin = 2;
rot_end = 3;
}
else if (DIMENSION_NO == 6)
{
rot_begin = 3;
rot_end = 6;
}
else if (DIMENSION_NO == 2)
{
rot_begin = 1;
rot_end = 2;
}
if (rot_begin == 0 || rot_end == 0)
{
exit(0);
}
auto diff_end = TGeometry{ 0 };
for (dim_t iDim = rot_begin; iDim < rot_end; ++iDim)
{
auto diff = abs(Base::GetPointC(ptL, iDim) - Base::GetPointC(ptR, iDim));
if (diff > std::numbers::pi)
{
if (Base::GetPointC(ptL, iDim) < Base::GetPointC(ptR, iDim))
{
diff_end += abs((Base::GetPointC(ptL, iDim) + (2 * std::numbers::pi)) - Base::GetPointC(ptR, iDim));
}
else
{
diff_end += abs(Base::GetPointC(ptL, iDim) - (Base::GetPointC(ptR, iDim) + (2 * std::numbers::pi)));
}
}
else
{
diff_end += diff;
}
}
return (diff_end / (rot_end - rot_begin));
}
static constexpr TGeometry pose_distance_extra(TVector const& ptL, TVector const& ptR) noexcept
{
autoc trans_distance = translational_distance(Subtract(ptL, ptR));
autoc rot_distance = rotaional_distance_extra(ptL, ptR);
return (trans_distance + rot_distance);
}
static constexpr TVector Add(TVector const& ptL, TVector const& ptR) noexcept
{
auto point = TVector{};
for (dim_t dimensionID = 0; dimensionID < DIMENSION_NO; ++dimensionID)
Base::SetPointC(point, dimensionID, Base::GetPointC(ptL, dimensionID) + Base::GetPointC(ptR, dimensionID));
return point;
}
static constexpr TVector Subtract(TVector const& ptL, TVector const& ptR) noexcept
{
auto point = TVector{};
for (dim_t dimensionID = 0; dimensionID < DIMENSION_NO; ++dimensionID)
Base::SetPointC(point, dimensionID, Base::GetPointC(ptL, dimensionID) - Base::GetPointC(ptR, dimensionID));
return point;
}
static constexpr TVector Multiply(TVector const& ptL, double rScalarR) noexcept
{
auto point = TVector{};
for (dim_t dimensionID = 0; dimensionID < DIMENSION_NO; ++dimensionID)
Base::SetPointC(point, dimensionID, TGeometry(Base::GetPointC(ptL, dimensionID) * rScalarR));
return point;
}
static constexpr TGeometry Dot(TVector const& ptL, TVector const& ptR) noexcept
{
auto value = TGeometry{};
for (dim_t dimensionID = 0; dimensionID < DIMENSION_NO; ++dimensionID)
value += Base::GetPointC(ptL, dimensionID) * Base::GetPointC(ptR, dimensionID);
return value;
}
static constexpr TGeometry Distance(TVector const& ptL, TVector const& ptR) noexcept { return Size(Subtract(ptL, ptR)); }
static constexpr TGeometry Distance2(TVector const& ptL, TVector const& ptR) noexcept { return Size2(Subtract(ptL, ptR)); }
static constexpr bool ArePointsEqual(TVector const& ptL, TVector const& ptR, TGeometry rAccuracy) noexcept
{
return Distance2(ptL, ptR) <= rAccuracy * rAccuracy;
}
static constexpr bool IsNormalizedVector(TVector const& normal) noexcept { return std::abs(Size2(normal) - 1.0) < 0.000001; }
static constexpr bool DoesBoxContainPoint(TBox const& box, TVector const& point, TGeometry tolerance = 0) noexcept
{
if (tolerance != 0.0)
{
assert(tolerance > 0);
for (dim_t dimensionID = 0; dimensionID < DIMENSION_NO; ++dimensionID)
if (!(Base::GetBoxMinC(box, dimensionID) - tolerance < Base::GetPointC(point, dimensionID) &&
Base::GetPointC(point, dimensionID) < Base::GetBoxMaxC(box, dimensionID) + tolerance))
return false;
}
else
{
for (dim_t dimensionID = 0; dimensionID < DIMENSION_NO; ++dimensionID)
if (!(Base::GetBoxMinC(box, dimensionID) <= Base::GetPointC(point, dimensionID) &&
Base::GetPointC(point, dimensionID) <= Base::GetBoxMaxC(box, dimensionID)))
return false;
}
return true;
}
static constexpr bool DoesBoxContainPointStrict(TBox const& box, TVector const& point) noexcept
{
for (dim_t dimensionID = 0; dimensionID < DIMENSION_NO; ++dimensionID)
if (!(Base::GetBoxMinC(box, dimensionID) < Base::GetPointC(point, dimensionID) &&
Base::GetPointC(point, dimensionID) < Base::GetBoxMaxC(box, dimensionID)))
return false;
return true;
}
enum class EBoxRelation
{
Overlapped = -1,
Adjecent = 0,
Separated = 1
};
static constexpr EBoxRelation GetBoxRelation(TBox const& e1, TBox const& e2) noexcept
{
enum EBoxRelationCandidate : uint8_t
{
OverlappedC = 0x1,
AdjecentC = 0x2,
SeparatedC = 0x4
};
uint8_t rel = 0;
for (dim_t dimensionID = 0; dimensionID < DIMENSION_NO; ++dimensionID)
{
if (Base::GetBoxMinC(e1, dimensionID) < Base::GetBoxMaxC(e2, dimensionID) && Base::GetBoxMaxC(e1, dimensionID) > Base::GetBoxMinC(e2, dimensionID))
rel |= EBoxRelationCandidate::OverlappedC;
else if (Base::GetBoxMinC(e1, dimensionID) == Base::GetBoxMaxC(e2, dimensionID) || Base::GetBoxMaxC(e1, dimensionID) == Base::GetBoxMinC(e2, dimensionID))
rel |= EBoxRelationCandidate::AdjecentC;
else if (Base::GetBoxMinC(e1, dimensionID) > Base::GetBoxMaxC(e2, dimensionID) || Base::GetBoxMaxC(e1, dimensionID) < Base::GetBoxMinC(e2, dimensionID))
return EBoxRelation::Separated;
}
return (rel & EBoxRelationCandidate::AdjecentC) == EBoxRelationCandidate::AdjecentC ? EBoxRelation::Adjecent : EBoxRelation::Overlapped;
}
static constexpr bool AreBoxesOverlappedStrict(TBox const& e1, TBox const& e2) noexcept
{
return GetBoxRelation(e1, e2) == EBoxRelation::Overlapped;
}
static constexpr bool AreBoxesOverlapped(TBox const& e1, TBox const& e2, bool e1_must_contain_e2 = true, bool fOverlapPtTouchAllowed = false) noexcept
{
if (e1_must_contain_e2)
{
for (dim_t dimensionID = 0; dimensionID < DIMENSION_NO; ++dimensionID)
{
if (Base::GetBoxMinC(e1, dimensionID) > Base::GetBoxMinC(e2, dimensionID) || Base::GetBoxMinC(e2, dimensionID) > Base::GetBoxMaxC(e1, dimensionID))
return false;
if (Base::GetBoxMinC(e1, dimensionID) > Base::GetBoxMaxC(e2, dimensionID) || Base::GetBoxMaxC(e2, dimensionID) > Base::GetBoxMaxC(e1, dimensionID))
return false;
}
return true;
}
else
{
autoc rel = GetBoxRelation(e1, e2);
if (fOverlapPtTouchAllowed)
return rel == EBoxRelation::Adjecent || rel == EBoxRelation::Overlapped;
else
return rel == EBoxRelation::Overlapped;
}
}
static inline TBox BoxInvertedInit() noexcept
{
auto ext = TBox{};
LOOPIVDEP
for (dim_t dimensionID = 0; dimensionID < DIMENSION_NO; ++dimensionID)
{
Base::SetBoxMinC(ext, dimensionID, std::numeric_limits<TGeometry>::max());
Base::SetBoxMaxC(ext, dimensionID, std::numeric_limits<TGeometry>::lowest());
}
return ext;
}
static TBox GetBoxOfPoints(std::span<TVector const> const& points) noexcept
{
auto ext = BoxInvertedInit();
for (autoc& point : points)
for (dim_t dimensionID = 0; dimensionID < DIMENSION_NO; ++dimensionID)
{
if (Base::GetBoxMinC(ext, dimensionID) > Base::GetPointC(point, dimensionID))
Base::SetBoxMinC(ext, dimensionID, Base::GetPointC(point, dimensionID));
if (Base::GetBoxMaxC(ext, dimensionID) < Base::GetPointC(point, dimensionID))
Base::SetBoxMaxC(ext, dimensionID, Base::GetPointC(point, dimensionID));
}
return ext;
}
static TBox GetBoxOfBoxes(std::span<TBox const> const& vExtent) noexcept
{
auto ext = BoxInvertedInit();
for (autoc& e : vExtent)
{
for (dim_t dimensionID = 0; dimensionID < DIMENSION_NO; ++dimensionID)
{
if (Base::GetBoxMinC(ext, dimensionID) > Base::GetBoxMinC(e, dimensionID))
Base::SetBoxMinC(ext, dimensionID, Base::GetBoxMinC(e, dimensionID));
if (Base::GetBoxMaxC(ext, dimensionID) < Base::GetBoxMaxC(e, dimensionID))
Base::SetBoxMaxC(ext, dimensionID, Base::GetBoxMaxC(e, dimensionID));
}
}
return ext;
}
static void MoveBox(TBox& box, TVector const& moveVector) noexcept
{
LOOPIVDEP
for (dim_t dimensionID = 0; dimensionID < DIMENSION_NO; ++dimensionID)
{
Base::SetBoxMinC(box, dimensionID, Base::GetBoxMinC(box, dimensionID) + Base::GetPointC(moveVector, dimensionID));
Base::SetBoxMaxC(box, dimensionID, Base::GetBoxMaxC(box, dimensionID) + Base::GetPointC(moveVector, dimensionID));
}
}
static constexpr std::optional<double> IsRayHit(TBox const& box, TVector const& rayBasePoint, TVector const& rayHeading, TGeometry tolerance) noexcept
{
if (DoesBoxContainPoint(box, rayBasePoint, tolerance))
return 0.0;
autoce inf = std::numeric_limits<double>::infinity();
auto minDistances = std::array<double, DIMENSION_NO>{};
auto maxDistances = std::array<double, DIMENSION_NO>{};
for (dim_t dimensionID = 0; dimensionID < DIMENSION_NO; ++dimensionID)
{
autoc hComp = Base::GetPointC(rayHeading, dimensionID);
if (hComp == 0)
{
if (tolerance != 0.0)
{
assert(tolerance > 0);
if (Base::GetBoxMaxC(box, dimensionID) + tolerance <= Base::GetPointC(rayBasePoint, dimensionID))
return std::nullopt;
if (Base::GetBoxMinC(box, dimensionID) - tolerance >= Base::GetPointC(rayBasePoint, dimensionID))
return std::nullopt;
}
else
{
if (Base::GetBoxMaxC(box, dimensionID) < Base::GetPointC(rayBasePoint, dimensionID))
return std::nullopt;
if (Base::GetBoxMinC(box, dimensionID) > Base::GetPointC(rayBasePoint, dimensionID))
return std::nullopt;
}
minDistances[dimensionID] = -inf;
maxDistances[dimensionID] = +inf;
continue;
}
minDistances[dimensionID] = ((hComp > 0.0 ? (Base::GetBoxMinC(box, dimensionID) - tolerance) : (Base::GetBoxMaxC(box, dimensionID) + tolerance)) -
Base::GetPointC(rayBasePoint, dimensionID)) /
hComp;
maxDistances[dimensionID] = ((hComp < 0.0 ? (Base::GetBoxMinC(box, dimensionID) - tolerance) : (Base::GetBoxMaxC(box, dimensionID) + tolerance)) -
Base::GetPointC(rayBasePoint, dimensionID)) /
hComp;
}
autoc rMin = *std::ranges::max_element(minDistances);
autoc rMax = *std::ranges::min_element(maxDistances);
if (rMin > rMax || rMax < 0.0)
return std::nullopt;
return rMin < 0 ? rMax : rMin;
}
static constexpr std::optional<double> IsRayHit(TBox const& box, TRay const& ray, TGeometry tolerance) noexcept
{
return IsRayHit(box, Base::GetRayOrigin(ray), Base::GetRayDirection(ray), tolerance);
}
// Get point-Hyperplane relation (Plane equation: dotProduct(planeNormal, point) = distanceOfOrigo)
static constexpr PlaneRelation GetPointPlaneRelation(TVector const& point, TGeometry distanceOfOrigo, TVector const& planeNormal, TGeometry tolerance) noexcept
{
assert(IsNormalizedVector(planeNormal));
autoc pointProjected = Dot(planeNormal, point);
if (pointProjected < distanceOfOrigo - tolerance)
return PlaneRelation::Negative;
if (pointProjected > distanceOfOrigo + tolerance)
return PlaneRelation::Positive;
return PlaneRelation::Hit;
}
// Get box-Hyperplane relation (Plane equation: dotProduct(planeNormal, point) = distanceOfOrigo)
static constexpr PlaneRelation GetBoxPlaneRelation(TBox const& box, TGeometry distanceOfOrigo, TVector const& planeNormal, TGeometry tolerance) noexcept
{
assert(IsNormalizedVector(planeNormal));
TVector center, radius;
for (dim_t dimensionID = 0; dimensionID < DIMENSION_NO; ++dimensionID)
{
autoc minComponent = Base::GetBoxMinC(box, dimensionID);
autoc maxComponent = Base::GetBoxMaxC(box, dimensionID);
autoc centerComponent = static_cast<TGeometry>((minComponent + maxComponent) * 0.5);
Base::SetPointC(center, dimensionID, centerComponent);
Base::SetPointC(radius, dimensionID, centerComponent - minComponent);
}
auto radiusProjected = double(tolerance);
for (dim_t dimensionID = 0; dimensionID < DIMENSION_NO; ++dimensionID)
radiusProjected += Base::GetPointC(radius, dimensionID) * std::abs(Base::GetPointC(planeNormal, dimensionID));
autoc centerProjected = Dot(planeNormal, center);
if (centerProjected + radiusProjected < distanceOfOrigo)
return PlaneRelation::Negative;
if (centerProjected - radiusProjected > distanceOfOrigo)
return PlaneRelation::Positive;
return PlaneRelation::Hit;
}
};
template<dim_t DIMENSION_NO, typename TVector, typename TBox, typename TRay, typename TPlane, typename TGeometry = double>
using AdaptorGeneral =
AdaptorGeneralBase<DIMENSION_NO, TVector, TBox, TRay, TPlane, TGeometry, AdaptorGeneralBasics<DIMENSION_NO, TVector, TBox, TRay, TPlane, TGeometry>>;
// Bitset helpers for higher dimensions
template<std::size_t N>
using bitset_arithmetic = std::bitset<N>;
template<std::size_t N>
bitset_arithmetic<N> operator+(bitset_arithmetic<N> const& lhs, bitset_arithmetic<N> const& rhs) noexcept
{
bool carry = false;
auto ans = bitset_arithmetic<N>();
for (std::size_t i = 0; i < N; ++i)
{
autoc sum = (lhs[i] ^ rhs[i]) ^ carry;
carry = (lhs[i] && rhs[i]) || (lhs[i] && carry) || (rhs[i] && carry);
ans[i] = sum;
}
assert(!carry); // unhandled overflow
return ans;
}
template<std::size_t N>
bitset_arithmetic<N> operator+(bitset_arithmetic<N> const& lhs, std::size_t rhs) noexcept
{
return lhs + bitset_arithmetic<N>(rhs);
}
template<std::size_t N>
bitset_arithmetic<N> operator-(bitset_arithmetic<N> const& lhs, bitset_arithmetic<N> const& rhs) noexcept
{
assert(lhs >= rhs);
auto ret = lhs;
bool borrow = false;
for (std::size_t i = 0; i < N; ++i)
{
if (borrow)
{
if (ret[i])
{
ret[i] = rhs[i];
borrow = rhs[i];
}
else
{
ret[i] = !rhs[i];
borrow = true;
}
}
else
{
if (ret[i])
{
ret[i] = !rhs[i];
borrow = false;
}
else
{
ret[i] = rhs[i];
borrow = rhs[i];
}
}
}
return ret;
}
template<std::size_t N>
bitset_arithmetic<N> operator-(bitset_arithmetic<N> const& lhs, std::size_t rhs) noexcept
{
return lhs - bitset_arithmetic<N>(rhs);
}
template<std::size_t N>
bitset_arithmetic<N> operator*(bitset_arithmetic<N> const& lhs, bitset_arithmetic<N> const& rhs) noexcept
{
auto ret = bitset_arithmetic<N>{};
if (lhs.count() < rhs.count())
{
for (std::size_t i = 0; i < N; ++i)
if (lhs[i])
ret = ret + (rhs << i);
}
else
{
for (std::size_t i = 0; i < N; ++i)
if (rhs[i])
ret = ret + (lhs << i);
}
return ret;
}
template<std::size_t N>
bitset_arithmetic<N> operator*(bitset_arithmetic<N> const& lhs, std::size_t rhs) noexcept
{
return lhs * bitset_arithmetic<N>(rhs);
}
template<std::size_t N>
bitset_arithmetic<N> operator*(std::size_t rhs, bitset_arithmetic<N> const& lhs) noexcept
{
return lhs * bitset_arithmetic<N>(rhs);
}
template<std::size_t N>
static std::tuple<bitset_arithmetic<N>, bitset_arithmetic<N>> gf2_div(bitset_arithmetic<N> const& dividend, bitset_arithmetic<N> divisor) noexcept
{
if (divisor.none())
{
assert(false);
return {};
}
if (dividend.none())
return {};
auto quotent = bitset_arithmetic<N>{ 0 };
if (dividend == divisor)
return { bitset_arithmetic<N>(1), quotent };
if (dividend < divisor)
return { quotent, dividend };
std::size_t sig_dividend = 0;
for (std::size_t i = 0, id = N - 1; i < N; ++i, --id)
if (dividend[id])
{
sig_dividend = id;
break;
}
std::size_t sig_divisor = 0;
for (std::size_t i = 0, id = N - 1; i < N; ++i, --id)
if (divisor[id])
{
sig_divisor = id;
break;
}
std::size_t nAlignment = (sig_dividend - sig_divisor);
divisor <<= nAlignment;
nAlignment += 1;
auto remainder = dividend;
while (nAlignment--)
{
if (divisor <= remainder)
{
quotent[nAlignment] = true;
remainder = remainder - divisor;
}
divisor >>= 1;
}
return { quotent, remainder };
}
template<std::size_t N>
bitset_arithmetic<N> operator/(bitset_arithmetic<N> const& dividend, bitset_arithmetic<N> const& divisor) noexcept
{
return std::get<0>(gf2_div(dividend, divisor));
}
template<std::size_t N>
auto operator<=>(bitset_arithmetic<N> const& lhs, bitset_arithmetic<N> const& rhs) noexcept
{
using R = std::strong_ordering;
for (std::size_t i = 0, id = N - 1; i < N; ++i, --id)
if (lhs[id] ^ rhs[id])
return lhs[id] ? R::greater : R::less;
return R::equal;
}
struct bitset_arithmetic_compare final
{
template<std::size_t N>
bool operator()(bitset_arithmetic<N> const& lhs, bitset_arithmetic<N> const& rhs) const noexcept
{
return lhs < rhs;
}
};
// NTrees
// NTreeLinear: Non-owning Base container which spatially organize data ids in N dimension space into a hash-table by Morton Z order.
template<
dim_t DIMENSION_NO,
typename TVector_,
typename TBox_,
typename TRay_,
typename TPlane_,
typename TGeometry_ = double,
typename TAdapter = AdaptorGeneral<DIMENSION_NO, TVector_, TBox_, TRay_, TPlane_, TGeometry_>>
class OrthoTreeBase
{
public:
struct UpdateID
{
enum : std::size_t
{
ERASE = std::numeric_limits<std::size_t>::max()
};
};
static autoce IS_LINEAR_TREE = DIMENSION_NO < 15;
// Max value: 2 ^ DIMENSION_NO
using ChildID = std::conditional < DIMENSION_NO<4, uint32_t, uint64_t>::type;
// Max value: 2 ^ nDepth ^ DIMENSION_NO * 2 (signal bit)
using LinearMortonGridID = ChildID;
using NonLinearMortonGridID = bitset_arithmetic<DIMENSION_NO * 4 + 1>;
using MortonGridID = typename std::conditional<IS_LINEAR_TREE, LinearMortonGridID, NonLinearMortonGridID>::type;
using MortonNodeID = MortonGridID; // same as the MortonGridID, but depth is signed by a sentinel bit.
using MortonGridIDCR = typename std::conditional<IS_LINEAR_TREE, MortonNodeID const, MortonNodeID const&>::type;
using MortonNodeIDCR = MortonGridIDCR;
using TGeometry = TGeometry_;
using TVector = TVector_;
using TBox = TBox_;
using TRay = TRay_;
using TPlane = TPlane_;
using AD = TAdapter;
static_assert(AdaptorConcept<AD, TVector, TBox, TRay, TPlane, TGeometry>);
static_assert(0 < DIMENSION_NO && DIMENSION_NO < 64);
protected:
// Max number of children
static autoce CHILD_NO = pow2_ce<DIMENSION_NO>();
// Type system determined maximal depth.
static autoce MAX_THEORETICAL_DEPTH = depth_t((CHAR_BIT * sizeof(MortonNodeID) - 1 /*sentinal bit*/) / DIMENSION_NO);
public:
class Node
{
private:
std::vector<MortonNodeID> m_children;
public: // Public members
std::vector<std::size_t> Entities = {};
TBox Box = {};
public:
constexpr void AddChild(MortonNodeIDCR childKey) noexcept { m_children.emplace_back(childKey); }
constexpr void AddChildInOrder(MortonNodeIDCR childKey) noexcept
{
auto it = std::end(m_children);
if constexpr (IS_LINEAR_TREE)
it = std::lower_bound(m_children.begin(), m_children.end(), childKey);
else
it = std::lower_bound(m_children.begin(), m_children.end(), childKey, bitset_arithmetic_compare{});
if (it != m_children.end() && *it == childKey)
return;
m_children.insert(it, childKey);
}
constexpr bool HasChild(MortonNodeIDCR childKey) const noexcept
{
if constexpr (IS_LINEAR_TREE)
return std::ranges::binary_search(m_children, childKey);
else
return std::ranges::binary_search(m_children, childKey, bitset_arithmetic_compare{});
}
constexpr bool IsChildNodeEnabled(ChildID childID) const noexcept
{
autoc childMortonID = MortonNodeID(childID);
return std::find_if(m_children.begin(), m_children.end(), [childMortonID](autoc& childKey) {
return (childKey & childMortonID) == childMortonID;
});
}
constexpr void RemoveChild(MortonNodeIDCR childKey) noexcept
{
auto it = std::end(m_children);
if constexpr (IS_LINEAR_TREE)
it = std::lower_bound(m_children.begin(), m_children.end(), childKey);
else
it = std::lower_bound(m_children.begin(), m_children.end(), childKey, bitset_arithmetic_compare{});
if (it == std::end(m_children))
return;
m_children.erase(it);
}
constexpr bool IsAnyChildExist() const noexcept { return !m_children.empty(); }
constexpr std::vector<MortonNodeID> const& GetChildren() const noexcept { return m_children; }
};
protected: // Aid struct to partitioning and distance ordering
struct ItemDistance
{
TGeometry Distance;
auto operator<=>(ItemDistance const& rhs) const = default;
};
struct EntityDistance : ItemDistance
{
std::size_t EntityID;
auto operator<=>(EntityDistance const& rhs) const = default;
};
struct BoxDistance : ItemDistance
{
MortonNodeID NodeKey;
Node const& NodeReference;
};
template<typename T>