-
Notifications
You must be signed in to change notification settings - Fork 4
/
mantis.cpp
1524 lines (1248 loc) · 49.7 KB
/
mantis.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
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
#include "mantis.h"
#include "Delaunay_psm.h"
#include <queue>
#include <unordered_set>
#include <algorithm>
#include <numeric>
#include <thread>
#if defined(__ARM_NEON) || defined(__ARM_NEON__)
#define MANTIS_HAS_NEON
#elif defined(__AVX__)
#define MANTIS_HAS_AVX
#ifdef __AVX512F__
#define MANTIS_HAS_AVX512
#endif
#else
#error "Mantis: No SIMD support detected, platform not supported."
#endif
#ifdef MANTIS_HAS_NEON
#include <arm_neon.h>
#endif
#ifdef MANTIS_HAS_AVX
#include <immintrin.h>
#endif
//#define DEBUG_MANTIS
#ifdef MANTIS_HAS_NEON
// float32x4_t and int32x4_t are already defined in arm_neon.h
using mask4_t = uint32x4_t;
#endif
#ifdef MANTIS_HAS_AVX
using float32x4_t = __m128;
using int32x4_t = __m128i;
using mask4_t = __m128i;
#endif
#ifdef MANTIS_HAS_AVX512
using float32x16_t = __m512;
using int32x16_t = __m512i;
using mask16_t = __mmask16;
#endif
#ifdef MANTIS_HAS_AVX512
constexpr size_t SimdWidth = 16;
using float32xN_t = float32x16_t;
using int32xN_t = int32x16_t;
using maskN_t = mask16_t;
#else
// For both SSE, AVX and NEON
constexpr size_t SimdWidth = 4;
using float32xN_t = float32x4_t;
using int32xN_t = int32x4_t;
using maskN_t = mask4_t;
#endif
namespace mantis {
using index_t = GEO::index_t;
// ============================= MISC STRUCTS ===============================
struct PackedEdge {
float32xN_t min_x;
float32xN_t start[3];
float32xN_t dir[3];
float32xN_t dir_len_squared;
int32xN_t primitive_idx;
};
struct PackedFace {
float32xN_t min_x;
float32xN_t face_plane[4];
float32xN_t edge_plane0[4];
float32xN_t edge_plane1[4];
float32xN_t edge_plane2[4];
int32xN_t primitive_idx;
};
struct FaceData {
// Plane coefficients of the face plane. Normal is of unit length.
GEO::vec4 face_plane;
// Edge plane at index i is the plane that contains the edge opposite to vertex i.
// Note that edge planes have to be oriented inwards, i.e. the normal is pointing to the
// inside of the triangle.
GEO::vec4 clipping_planes[3];
GEO::vec3 pt_on_plane;
};
struct EdgeData {
uint32_t start = uint32_t(-1), end = uint32_t(-1);
GEO::vec4 clipping_planes[4];
int num_planes = 0;
};
struct BoundingBox {
GEO::vec3 lower = GEO::vec3{std::numeric_limits<double>::max()};
GEO::vec3 upper = GEO::vec3{-std::numeric_limits<double>::max()};
void extend(const GEO::vec3 &pt) {
lower = {std::min(lower.x, pt.x), std::min(lower.y, pt.y), std::min(lower.z, pt.z)};
upper = {std::max(upper.x, pt.x), std::max(upper.y, pt.y), std::max(upper.z, pt.z)};
}
void extend(const BoundingBox &box) {
extend(box.lower);
extend(box.upper);
}
};
struct Node {
float32x4_t minCorners[3]; // x, y, z minimum corners for 4 boxes
float32x4_t maxCorners[3]; // x, y, z maximum corners for 4 boxes
int32x4_t children;
};
// ============================= SIMD ===============================
#ifdef MANTIS_HAS_NEON
// a*b + c
float32x4_t fma(float32x4_t a, float32x4_t b, float32x4_t c) {
return vmlaq_f32(c, a, b);
}
float32x4_t min(float32x4_t a, float32x4_t b) {
return vminq_f32(a, b);
}
float32x4_t max(float32x4_t a, float32x4_t b) {
return vmaxq_f32(a, b);
}
float32x4_t sub(float32x4_t a, float32x4_t b) {
return vsubq_f32(a, b);
}
float32x4_t add(float32x4_t a, float32x4_t b) {
return vaddq_f32(a, b);
}
float32x4_t mul(float32x4_t a, float32x4_t b) {
return vmulq_f32(a, b);
}
float32x4_t div(float32x4_t a, float32x4_t b) {
return vdivq_f32(a, b);
}
uint32x4_t leq(float32x4_t a, float32x4_t b) {
return vcleq_f32(a, b);
}
uint32x4_t geq(float32x4_t a, float32x4_t b) {
return vcgeq_f32(a, b);
}
uint32x4_t logical_and(uint32x4_t a, uint32x4_t b) {
return vandq_u32(a, b);
}
int32x4_t select_int(int32x4_t condition, int32x4_t trueValue, int32x4_t falseValue) {
return vbslq_s32(condition, trueValue, falseValue);
}
float32x4_t select_float(uint32x4_t condition, float32x4_t trueValue, float32x4_t falseValue) {
return vbslq_f32(condition, trueValue, falseValue);
}
template<int N = SimdWidth>
float32x4_t dupf32(float x) {
static_assert(N == 4);
return vdupq_n_f32(x);
}
template<int N = SimdWidth>
int32x4_t dupi32(int32_t x) {
static_assert(N == 4);
return vdupq_n_s32(x);
}
#endif
#ifdef MANTIS_HAS_AVX
float32x4_t fma(float32x4_t a, float32x4_t b, float32x4_t c) {
return _mm_add_ps(_mm_mul_ps(a, b), c); // TODO: check for fma
}
float32x4_t min(float32x4_t a, float32x4_t b) {
return _mm_min_ps(a, b);
}
float32x4_t max(float32x4_t a, float32x4_t b) {
return _mm_max_ps(a, b);
}
float32x4_t sub(float32x4_t a, float32x4_t b) {
return _mm_sub_ps(a, b);
}
float32x4_t add(float32x4_t a, float32x4_t b) {
return _mm_add_ps(a, b);
}
float32x4_t mul(float32x4_t a, float32x4_t b) {
return _mm_mul_ps(a, b);
}
float32x4_t div(float32x4_t a, float32x4_t b) {
return _mm_div_ps(a, b);
}
mask4_t leq(float32x4_t a, float32x4_t b) {
return _mm_castps_si128(_mm_cmple_ps(a, b)); // Cast result to integer type
}
mask4_t geq(float32x4_t a, float32x4_t b) {
return _mm_castps_si128(_mm_cmpge_ps(a, b)); // Cast result to integer type
}
mask4_t logical_and(mask4_t a, mask4_t b) {
return _mm_and_si128(a, b);
}
int32x4_t select_int(int32x4_t condition, int32x4_t trueValue, int32x4_t falseValue) {
return _mm_blendv_epi8(falseValue, trueValue, condition);
}
float32x4_t select_float(mask4_t condition, float32x4_t trueValue, float32x4_t falseValue) {
__m128 conditionAsFloat = _mm_castsi128_ps(condition);
return _mm_blendv_ps(falseValue, trueValue, conditionAsFloat);
}
#ifndef MANTIS_HAS_AVX512
template<int N = SimdWidth>
auto dupf32(float x) {
static_assert(N == 4);
return _mm_set1_ps(x);
}
template<int N = SimdWidth>
auto dupi32(int32_t x) {
static_assert(N == 4);
return _mm_set1_epi32(x);
}
#endif
#endif
#ifdef MANTIS_HAS_AVX512
float32x16_t fma(float32x16_t a, float32x16_t b, float32x16_t c) {
return _mm512_fmadd_ps(a, b, c);
}
float32x16_t min(float32x16_t a, float32x16_t b) {
return _mm512_min_ps(a, b);
}
float32x16_t max(float32x16_t a, float32x16_t b) {
return _mm512_max_ps(a, b);
}
float32x16_t sub(float32x16_t a, float32x16_t b) {
return _mm512_sub_ps(a, b);
}
float32x16_t add(float32x16_t a, float32x16_t b) {
return _mm512_add_ps(a, b);
}
float32x16_t mul(float32x16_t a, float32x16_t b) {
return _mm512_mul_ps(a, b);
}
float32x16_t div(float32x16_t a, float32x16_t b) {
return _mm512_div_ps(a, b);
}
mask16_t leq(float32x16_t a, float32x16_t b) {
return _mm512_cmp_ps_mask(a, b, _CMP_LE_OS);
}
mask16_t geq(float32x16_t a, float32x16_t b) {
return _mm512_cmp_ps_mask(a, b, _CMP_GE_OS);
}
mask16_t logical_and(mask16_t a, mask16_t b) {
return _mm512_kand(a, b);
}
int32x16_t select_int(mask16_t condition, int32x16_t trueValue, int32x16_t falseValue) {
return _mm512_mask_blend_epi32(condition, falseValue, trueValue);
}
float32x16_t select_float(mask16_t condition, float32x16_t trueValue, float32x16_t falseValue) {
return _mm512_mask_blend_ps(condition, falseValue, trueValue);
}
template<int N = SimdWidth>
auto dupf32(float x) {
if constexpr(N == 4) {
return _mm_set1_ps(x);
} else if constexpr(N == 16) {
return _mm512_set1_ps(x);
}
}
template<int N = SimdWidth>
auto dupi32(int32_t x) {
if constexpr(N == 4) {
return _mm_set1_epi32(x);
} else if constexpr(N == 16) {
return _mm512_set1_epi32(x);
}
}
void set(float32x16_t &v, size_t i, float x) {
assert(i < 16);
auto ptr = (float *) &v;
ptr[i] = x;
}
void set(int32x16_t &v, size_t i, int x) {
assert(i < 16);
auto ptr = (int *) &v;
ptr[i] = x;
}
float get(const float32x16_t &v, size_t i) {
assert(i < 16);
auto ptr = (const float *) &v;
return ptr[i];
}
int get(const int32x16_t &v, size_t i) {
assert(i < 16);
auto ptr = (const int *) &v;
return ptr[i];
}
#endif
// ============================= SIMD MATH UTILS ===============================
void set(float32x4_t &v, size_t i, float x) {
auto ptr = (float *) &v;
ptr[i] = x;
}
void set(int32x4_t &v, size_t i, int x) {
auto ptr = (int *) &v;
ptr[i] = x;
}
float get(const float32x4_t &v, size_t i) {
auto ptr = (const float *) &v;
return ptr[i];
}
int get(const int32x4_t &v, size_t i) {
auto ptr = (const int *) &v;
return ptr[i];
}
template<class T>
T dot(T ax, T ay, T az, T bx, T by, T bz) {
T result = mul(ax, bx);
result = fma(ay, by, result);
result = fma(az, bz, result);
return result;
}
template<class T>
T length_squared(T x, T y, T z) {
T result = mul(x, x);
result = fma(y, y, result);
result = fma(z, z, result);
return result;
}
template<class T>
T distance_squared(T ax, T ay, T az, T bx, T by, T bz) {
T dx = sub(ax, bx);
T dy = sub(ay, by);
T dz = sub(az, bz);
return length_squared(dx, dy, dz);
}
template<class T>
T eval_plane(T px, T py, T pz, T plane_x, T plane_y, T plane_z, T plane_w) {
T result = mul(px, plane_x);
result = fma(py, plane_y, result);
result = fma(pz, plane_z, result);
return add(result, plane_w);
}
inline float32x4_t p2bbox(const Node &node, const float32x4_t qx, const float32x4_t qy, const float32x4_t qz) {
// Compute distances in x, y, z directions and clamp them to zero if they are negative
float32x4_t dx = max(sub(node.minCorners[0], qx), sub(qx, node.maxCorners[0]));
dx = max(dx, dupf32<4>(0.0f));
float32x4_t dy = max(sub(node.minCorners[1], qy), sub(qy, node.maxCorners[1]));
dy = max(dy, dupf32<4>(0.0f));
float32x4_t dz = max(sub(node.minCorners[2], qz), sub(qz, node.maxCorners[2]));
dz = max(dz, dupf32<4>(0.0f));
// Compute squared distances for each box
float32x4_t squaredDist = length_squared(dx, dy, dz);
return squaredDist;
}
// ============================= BVH ===============================
struct LeafNode {
float32xN_t x_coords = dupf32(FLT_MAX);
float32xN_t y_coords = dupf32(FLT_MAX);
float32xN_t z_coords = dupf32(FLT_MAX);
int32xN_t indices = dupi32(-1);
};
#define cmin(a, b) get(distances,a) > get(distances,b) ? b : a
#define cmax(a, b) get(distances,a) > get(distances,b) ? a : b
#define cswap(a, b) \
{int tmp = a; \
a = cmax(a,b); \
b = cmin(tmp, b);}
#define nsort4(a, b, c, d) \
do \
{ \
cswap(a, b); \
cswap(c, d); \
cswap(a, c); \
cswap(b, d); \
cswap(b, c); \
} while (0)
constexpr static long long NUM_PACKETS = 8;
class Bvh {
public:
void updateClosestPoint(const float32xN_t &pt_x,
const float32xN_t &pt_y,
const float32xN_t &pt_z,
size_t firstPacket,
size_t numPackets,
float &bestDistSq,
int &bestIdx) const {
float32xN_t minDist = dupf32(bestDistSq);
int32xN_t minIdx = dupi32(bestIdx);
for (size_t i = firstPacket; i < firstPacket + numPackets; ++i) {
// Compute squared distances for a batch of SimdWidth points
const auto &leaf = m_leaves[i];
float32xN_t dx = sub(pt_x, leaf.x_coords);
float32xN_t dy = sub(pt_y, leaf.y_coords);
float32xN_t dz = sub(pt_z, leaf.z_coords);
float32xN_t distSq = length_squared(dx, dy, dz);
// Comparison mask for distances
// if distSq >= minDist => keep minDist
maskN_t keepMinDist = geq(distSq, minDist);
minDist = min(minDist, distSq);
// Update the indices
minIdx = select_int(keepMinDist, minIdx, leaf.indices);
}
// Find overall minimum distance and index
for (int j = 0; j < SimdWidth; ++j) {
if (get(minDist, j) < bestDistSq) {
bestDistSq = get(minDist, j);
bestIdx = get(minIdx, j);
}
}
}
explicit Bvh(const std::vector<GEO::vec3> &points) {
// Initialize the original_points vector
original_points.resize(points.size());
for (size_t i = 0; i < points.size(); ++i) {
original_points[i] = points[i];
}
// Create an index array for all points
std::vector<int> indices(points.size());
std::iota(indices.begin(), indices.end(), 0);
// Build the KD-tree
BoundingBox box;
int node_idx = constructTree(indices, 0, indices.size(), 0, box);
assert(node_idx == 0 || node_idx < 0);
}
std::pair<int, float> closestPoint(const GEO::vec3 &q) const {
constexpr int MAX_STACK_SIZE = 64;
struct StackNode {
int nodeIndex;
float minDistSq;
};
StackNode stack[MAX_STACK_SIZE];
int stackSize = 0;
float bestDistSq = std::numeric_limits<float>::max();
int bestIdx = -1;
// Broadcast query point coordinates to SIMD size
float32x4_t q_x4 = dupf32<4>(q.x);
float32x4_t q_y4 = dupf32<4>(q.y);
float32x4_t q_z4 = dupf32<4>(q.z);
float32xN_t q_xN = dupf32(q.x);
float32xN_t q_yN = dupf32(q.y);
float32xN_t q_zN = dupf32(q.z);
// Start with the root node
stack[stackSize++] = {0, 0.0f};
if (m_nodes.empty()) {
stack[0].nodeIndex = -1;
}
while (stackSize > 0) {
StackNode current = stack[--stackSize];
if (current.minDistSq >= bestDistSq) {
continue; // Skip nodes that can't possibly contain a closer point
}
if (current.nodeIndex < 0) {
auto [begin, numPackets] = m_leafRange[-(current.nodeIndex + 1)];
updateClosestPoint(q_xN, q_yN, q_zN, begin, numPackets, bestDistSq, bestIdx);
continue;
}
const Node &node = m_nodes[current.nodeIndex];
// Compute distances to each child
float32x4_t distances = p2bbox(node, q_x4, q_y4, q_z4);
int childIndices[4] = {0, 1, 2, 3};
// Sort children by distance
nsort4(childIndices[0], childIndices[1], childIndices[2], childIndices[3]);
// push children that are internal
for (int idx: childIndices) {
int childIdx = get(node.children, idx);
float childDist = get(distances, idx);
if (childDist < bestDistSq) {
assert(stackSize + 1 < MAX_STACK_SIZE);
stack[stackSize++] = {childIdx, childDist};
}
}
}
return {bestIdx, bestDistSq};
}
private:
std::vector<GEO::vec3> original_points;
std::vector<Node> m_nodes;
std::vector<LeafNode> m_leaves;
std::vector<std::pair<int, int>> m_leafRange;
int constructTree(std::vector<int> &indices, size_t begin, size_t end, size_t depth, BoundingBox &box) {
if (end - begin <= NUM_PACKETS * SimdWidth) {
// Update the bounding box for this leaf node
box = BoundingBox();
for (size_t i = begin; i < end; ++i) {
int idx = indices[i];
box.extend(original_points[idx]);
}
int leafIdx = int(m_leafRange.size());
auto firstLeaf = int(m_leaves.size());
auto numPackets = int((end - begin + SimdWidth - 1) / SimdWidth);
m_leafRange.emplace_back(firstLeaf, numPackets);
for (int i = 0; i < numPackets; ++i) {
LeafNode leaf{};
for (size_t j = 0; j < SimdWidth; ++j) {
size_t k = i * SimdWidth + j;
if (k < end - begin) {
set(leaf.x_coords, j, (float) original_points[indices[begin + k]].x);
set(leaf.y_coords, j, (float) original_points[indices[begin + k]].y);
set(leaf.z_coords, j, (float) original_points[indices[begin + k]].z);
set(leaf.indices, j, (int) indices[begin + k]);
}
}
m_leaves.push_back(leaf);
}
// Return negative index to indicate leaf node
return -(leafIdx + 1);
}
Node node{};
// Split dimensions: Choose different dimensions for each split
size_t primaryDim = depth % 3;
size_t secondaryDim = (primaryDim + 1) % 3; // Choose next dimension for secondary split
// Primary split
size_t primarySplit = (begin + end) / 2;
std::nth_element(indices.begin() + (long) begin, indices.begin() + (long) primarySplit,
indices.begin() + (long) end,
[primaryDim, this](int i1, int i2) {
return original_points[i1][primaryDim] < original_points[i2][primaryDim];
});
// Secondary splits
size_t secondarySplit1 = (begin + primarySplit) / 2;
size_t secondarySplit2 = (primarySplit + end) / 2;
std::nth_element(indices.begin() + (long) begin, indices.begin() + (long) secondarySplit1,
indices.begin() + (long) primarySplit,
[secondaryDim, this](int i1, int i2) {
return original_points[i1][secondaryDim] < original_points[i2][secondaryDim];
});
std::nth_element(indices.begin() + (long) primarySplit, indices.begin() + (long) secondarySplit2,
indices.begin() + (long) end,
[secondaryDim, this](int i1, int i2) {
return original_points[i1][secondaryDim] < original_points[i2][secondaryDim];
});
BoundingBox childBoxes[4] = {};
auto node_idx = int(m_nodes.size());
m_nodes.emplace_back();
set(node.children, 0, constructTree(indices, begin, secondarySplit1, depth + 2, childBoxes[0]));
set(node.children, 1, constructTree(indices, secondarySplit1, primarySplit, depth + 2, childBoxes[1]));
set(node.children, 2, constructTree(indices, primarySplit, secondarySplit2, depth + 2, childBoxes[2]));
set(node.children, 3, constructTree(indices, secondarySplit2, end, depth + 2, childBoxes[3]));
// set bounding boxes of node
for (int i = 0; i < 3; ++i) {
for (int j = 0; j < 4; ++j) {
set(node.minCorners[i], j, (float) childBoxes[j].lower[i]);
set(node.maxCorners[i], j, (float) childBoxes[j].upper[i]);
}
}
// Combine bounding boxes from children
box = childBoxes[0];
for (int i = 1; i < 4; ++i) {
box.extend(childBoxes[i]);
}
m_nodes[node_idx] = node;
return node_idx;
}
};
// ============================= UTILS ==================================
template<class F>
void parallel_for(size_t begin, size_t end, F f) {
// serial implementation
//for(size_t i = begin; i < end; ++i) {
// f(i);
//}
//return;
size_t num_threads = std::thread::hardware_concurrency();
std::vector<std::thread> threads;
threads.reserve(num_threads);
size_t chunk_size = (end - begin + num_threads - 1) / num_threads;
for (size_t i = 0; i < num_threads; ++i) {
size_t thread_begin = begin + i * chunk_size;
size_t thread_end = std::min(thread_begin + chunk_size, end);
threads.emplace_back([thread_begin, thread_end, &f]() {
for (size_t j = thread_begin; j < thread_end; ++j) {
f(j);
}
});
}
for (auto &thread: threads) {
thread.join();
}
}
// ============================= GEOMETRY UTILS ===============================
GEO::vec4 to_vec4(GEO::vec3 v, double w) {
return {v.x, v.y, v.z, w};
}
GEO::vec3 to_vec3(GEO::vec4 v) {
return {v.x, v.y, v.z};
}
double eval_plane(GEO::vec4 plane, GEO::vec3 p) {
return plane.x * p.x + plane.y * p.y + plane.z * p.z + plane.w;
}
double distance_to_line_squared(GEO::vec3 p, GEO::vec3 a, GEO::vec3 b) {
GEO::vec3 ab = b - a;
GEO::vec3 ap = p - a;
// Project ap onto ab to find the projected point p'
GEO::vec3 p_prime = a + ab * (GEO::dot(ap, ab) / GEO::dot(ab, ab));
return GEO::distance2(p, p_prime);
}
// assumes the plane normal is unit length
double distance_to_plane_squared(GEO::vec3 p, GEO::vec4 plane) {
assert(std::abs(to_vec3(plane).length() - 1.0) < 1e-8);
double d = eval_plane(plane, p);
return d * d;
}
GEO::vec3 project_plane(GEO::vec3 p, const FaceData &face) {
GEO::vec3 normal = to_vec3(face.face_plane);
GEO::vec3 pt_on_plane = face.pt_on_plane;
return p - GEO::dot(normal, p - pt_on_plane) * normal;
}
GEO::vec3 project_line(GEO::vec3 p, GEO::vec3 a, GEO::vec3 b) {
GEO::vec3 ab = b - a;
GEO::vec3 ap = p - a;
return a + ab * (GEO::dot(ap, ab) / GEO::dot(ab, ab));
}
template<class F>
inline GEO::vec3 intersect(GEO::vec3 A, GEO::vec3 B, GEO::vec3 p, F dist_to_element_squared) {
const double tol = 1e-5;
double l(0), r(1), m;
GEO::vec3 cur;
int T = (int) log2(GEO::length(A - B) / tol);
if (T <= 0) T = 1;
while (T--) {
m = (l + r) / 2;
cur = (1 - m) * B + m * A;
if (GEO::distance2(cur, p) > dist_to_element_squared(cur)) r = m;
else l = m;
}
return (1 - l) * B + l * A;
}
// squared distance between a point and the straight line of an edge
inline double dis2_p2e(const GEO::vec3 &p, const EdgeData &e, const std::vector<GEO::vec3> &points) {
GEO::vec3 dir = GEO::normalize(points[e.end] - points[e.start]);
return GEO::cross(points[e.start] - p, dir).length2();
}
// squared distance between a point and the plane of a face
inline double dis2_p2f(const GEO::vec3 &p, const FaceData &f) {
//double d = f.n.dot(points[f.verts.x()] - p);
double d = GEO::dot(to_vec3(f.face_plane), f.pt_on_plane - p);
return d * d;
}
// returns true if the vertex corresponding to site_point is intercepting the element,
// otherwise returns false. If an interception is detected, the bounding box of the
// element region clipped with the bisector of the element and the intercepted vertex
// is returned in box.
template<class F>
bool check_and_create_bounding_box(
const GEO::ConvexCell &C,
GEO::vec3 site_point,
F dist_to_element_squared,
BoundingBox &box) {
bool is_intercepting = false;
for (index_t v = 1; v < C.nb_v(); ++v) {
index_t t = C.vertex_triangle(v);
// Happens if a clipping plane did not clip anything.
if (t == VBW::END_OF_LIST) {
continue;
}
int last_region = 0;
int first_region = 0;
GEO::vec3 last_pt;
GEO::vec3 first_pt;
bool first_pt_set = false;
do {
GEO::vec3 pt = C.triangle_point(VBW::ushort(t));
int region = dist_to_element_squared(pt) < GEO::distance2(pt, site_point) ? -1 : 1;
if (!first_pt_set) {
first_pt_set = true;
first_pt = pt;
first_region = region;
}
if (region == -1) {
box.extend(pt);
is_intercepting = true;
}
// note that we traverse every edge twice, once from each side, but we only need to compute
// the intersection point once
if (last_region == -1 && region == 1) {
GEO::vec3 intersection = intersect(last_pt, pt, site_point, dist_to_element_squared);
box.extend(intersection);
}
last_pt = pt;
last_region = region;
index_t lv = C.triangle_find_vertex(t, v);
t = C.triangle_adjacent(t, (lv + 1) % 3);
} while (t != C.vertex_triangle(v));
// Process the edge connecting the last and first points
if (last_region == -1 && first_region == 1) {
GEO::vec3 intersection = intersect(last_pt, first_pt, site_point, dist_to_element_squared);
box.extend(intersection);
}
}
return is_intercepting;
}
// ============================= DISTANCE TO MESH ===============================
struct Impl {
Impl(const std::vector<GEO::vec3> &points, const std::vector<std::array<uint32_t, 3>> &triangles,
double limit_cube_len);
// for each voronoi cell, check every face of the mesh if the vertex corresponding to the cell
// "intercepts" the face. This means that after trimming the cell by the face's edge planes, it is
// contained in the convex region that is closer
void compute_interception_list();
Result calc_closest_point(GEO::vec3 q);
Bvh bvh;
std::vector<GEO::vec3> points;
std::vector<std::array<uint32_t, 3>> triangles;
double limit_cube_len = 0;
std::vector<EdgeData> edges;
std::vector<FaceData> faces;
std::vector<std::vector<PackedEdge>> intercepted_edges_packed;
std::vector<std::vector<PackedFace>> intercepted_faces_packed;
std::map<std::pair<index_t, index_t>, size_t> edge_index;
#ifdef DEBUG_MANTIS
std::map<std::pair<index_t, index_t>, GEO::ConvexCell> vertex_edge_cells;
std::map<std::pair<index_t, index_t>, GEO::ConvexCell> vertex_face_cells;
std::map<index_t, GEO::ConvexCell> vor_cells;
std::map<index_t, GEO::ConvexCell> edge_cells;
std::map<index_t, GEO::ConvexCell> face_cells;
#endif
};
struct PointEq {
bool operator()(const GEO::vec3 &a, const GEO::vec3 &b) const {
return a.x == b.x && a.y == b.y && a.z == b.z;
}
};
struct PointHash {
size_t operator()(const GEO::vec3 &p) const {
size_t h = 0;
for (size_t i = 0; i < 3; ++i) {
h ^= std::hash<double>{}(p[i]) + 0x9e3779b9 + (h << 6) + (h >> 2);
}
return h;
}
};
bool check_points(std::vector<GEO::vec3> points) {
for(auto p : points) {
if(!std::isfinite(p.x) || !std::isfinite(p.y) || !std::isfinite(p.z)) {
return false;
}
}
// check for duplicates
std::unordered_map<GEO::vec3, int, PointHash, PointEq> point_map;
for(auto p : points) {
point_map[p]++;
}
for(auto [p, count] : point_map) {
if(count > 1) {
return false;
}
}
return true;
}
void deduplicate_points(std::vector<GEO::vec3>& points, std::vector<std::array<uint32_t, 3>>& triangles) {
std::vector<int> vertices(points.size());
std::iota(vertices.begin(), vertices.end(), 0);
std::sort(vertices.begin(), vertices.end(), [&points](int a, int b) {
return std::tie(points[a].x, points[a].y, points[a].z) < std::tie(points[b].x, points[b].y, points[b].z);
});
std::vector<GEO::vec3> unique_points;
unique_points.reserve(points.size());
std::vector<uint32_t> index_map(points.size());
auto is_equal = [](const GEO::vec3& a, const GEO::vec3& b) {
return a.x == b.x && a.y == b.y && a.z == b.z;
};
for (size_t i = 0; i < vertices.size(); ++i) {
if (i == 0 || !is_equal(points[vertices[i]], points[vertices[i - 1]])) {
unique_points.push_back(points[vertices[i]]);
}
index_map[vertices[i]] = static_cast<uint32_t>(unique_points.size() - 1);
}
if(unique_points.size() == points.size()) {
return;
}
points.swap(unique_points);
for (auto& triangle : triangles) {
for (int i = 0; i < 3; ++i) {
triangle[i] = index_map[triangle[i]];
}
}
}
Impl::Impl(const std::vector<GEO::vec3> &points, const std::vector<std::array<index_t, 3>> &triangles,
double limit_cube_len)
: points(points), triangles(triangles), bvh(points), limit_cube_len(limit_cube_len) {
assert(check_points(points));
static int init_geogram = [] {
GEO::initialize();
return 0;
}();
(void) init_geogram;
std::map<std::pair<index_t, index_t>, EdgeData> edge_map;
for (auto t: triangles) {
for (int i = 0; i < 3; ++i) {
index_t v0 = t[i];
index_t v1 = t[(i + 1) % 3];
if (v0 > v1) {
std::swap(v0, v1);
}
auto [it, inserted] = edge_map.emplace(std::pair{v0, v1}, EdgeData{v0, v1});
if (inserted) {
// populate end planes of edge
GEO::vec3 start_pt = points[v0];
GEO::vec3 end_pt = points[v1];
GEO::vec3 n1 = GEO::normalize(end_pt - start_pt);
GEO::vec3 n2 = GEO::normalize(start_pt - end_pt);
auto &ed = it->second;
ed.clipping_planes[ed.num_planes++] = to_vec4(n1, -GEO::dot(n1, start_pt));
ed.clipping_planes[ed.num_planes++] = to_vec4(n2, -GEO::dot(n2, end_pt));
}
}
}
faces.resize(triangles.size());
for (index_t f = 0; f < faces.size(); ++f) {
auto [v0, v1, v2] = triangles[f];
GEO::vec3 p0 = points[v0];
GEO::vec3 p1 = points[v1];
GEO::vec3 p2 = points[v2];
GEO::vec3 n = GEO::normalize(GEO::cross(p1 - p0, p2 - p0));
GEO::vec3 n0 = GEO::normalize(GEO::cross(p2 - p1, n));
GEO::vec3 n1 = GEO::normalize(GEO::cross(p0 - p2, n));
GEO::vec3 n2 = GEO::normalize(GEO::cross(p1 - p0, n));
GEO::vec4 plane0 = to_vec4(-n0, GEO::dot(n0, p1));
GEO::vec4 plane1 = to_vec4(-n1, GEO::dot(n1, p2));
GEO::vec4 plane2 = to_vec4(-n2, GEO::dot(n2, p0));
faces[f].face_plane = to_vec4(n, -GEO::dot(n, p0));
faces[f].clipping_planes[0] = plane0;
faces[f].clipping_planes[1] = plane1;
faces[f].clipping_planes[2] = plane2;
faces[f].pt_on_plane = p0;