Repository navigation
Expand file tree
/
Copy pathcyl_sdf_volume_ext.cpp
More file actions
1418 lines (1358 loc) · 48.1 KB
/
Copy pathcyl_sdf_volume_ext.cpp
File metadata and controls
1418 lines (1358 loc) · 48.1 KB
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 <torch/extension.h>
#include <ATen/Parallel.h>
#include <Python.h>
#include <Eigen/Core>
#include <igl/AABB.h>
#include <igl/WindingNumberAABB.h>
#include <igl/signed_distance.h>
#include <algorithm>
#include <array>
#include <atomic>
#include <chrono>
#include <cmath>
#include <cstdint>
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <limits>
#include <string>
#include <thread>
#include <vector>
namespace {
using MatrixXdR = Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor>;
using MatrixXiR = Eigen::Matrix<int, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor>;
using RowVector3d = Eigen::Matrix<double, 1, 3>;
constexpr double kDefaultBarrierDepthMax = 2000.0;
enum class BarrierMode {
Inside,
Outside,
};
MatrixXdR tensor_to_vertices(torch::Tensor vertices) {
TORCH_CHECK(vertices.dim() == 2 && vertices.size(1) == 3, "vertices must be (N, 3)");
vertices = vertices.to(torch::kFloat64).cpu().contiguous();
MatrixXdR V(vertices.size(0), 3);
const double* src = vertices.data_ptr<double>();
std::copy(src, src + vertices.numel(), V.data());
return V;
}
MatrixXiR tensor_to_faces(torch::Tensor faces) {
TORCH_CHECK(faces.dim() == 2 && faces.size(1) == 3, "faces must be (M, 3)");
faces = faces.to(torch::kInt64).cpu().contiguous();
MatrixXiR F(faces.size(0), 3);
const int64_t* src = faces.data_ptr<int64_t>();
for (int64_t i = 0; i < faces.numel(); ++i) {
F.data()[i] = static_cast<int>(src[i]);
}
return F;
}
bool is_inside_at(
const igl::WindingNumberAABB<double, int>& hier,
const RowVector3d& q
) {
const double winding = hier.winding_number(q.transpose());
return winding > 0.5;
}
bool is_active_at(
const igl::WindingNumberAABB<double, int>& hier,
const RowVector3d& q,
BarrierMode mode
) {
const bool inside = is_inside_at(hier, q);
return mode == BarrierMode::Inside ? inside : !inside;
}
BarrierMode parse_barrier_mode(const std::string& mode) {
if (mode == "inside") {
return BarrierMode::Inside;
}
if (mode == "outside") {
return BarrierMode::Outside;
}
TORCH_CHECK(false, "cyl_outside mode must be 'inside' or 'outside', got '", mode, "'");
return BarrierMode::Inside;
}
const char* barrier_mode_name(BarrierMode mode) {
return mode == BarrierMode::Inside ? "inside" : "outside";
}
double surface_distance_at(
const igl::AABB<MatrixXdR, 3>& tree,
const MatrixXdR& V,
const MatrixXiR& F,
const RowVector3d& q
) {
double sqrd = 0.0;
int face_i = -1;
RowVector3d closest;
sqrd = tree.squared_distance(V, F, q, face_i, closest);
return std::sqrt(std::max(0.0, sqrd));
}
uint8_t encode_depth(double depth, double depth_max) {
if (!(depth > 0.0) || !(depth_max > 0.0) || !std::isfinite(depth_max)) {
return static_cast<uint8_t>(0);
}
const double normalized = std::min(1.0, std::max(0.0, depth / depth_max));
const long q = std::lround(255.0 * std::sqrt(normalized));
return static_cast<uint8_t>(std::min<long>(255, std::max<long>(0, q)));
}
int default_thread_count(int requested_threads) {
if (requested_threads > 0) {
return requested_threads;
}
const int torch_threads = at::get_num_threads();
if (torch_threads > 1) {
return torch_threads;
}
const unsigned int hw = std::thread::hardware_concurrency();
return static_cast<int>(std::max(1u, hw));
}
void print_progress_line(
const std::string& label,
const std::string& pass,
int64_t done,
int64_t total,
const std::chrono::steady_clock::time_point& start,
bool final,
bool final_complete,
const std::string& unit
) {
const double elapsed = std::chrono::duration<double>(std::chrono::steady_clock::now() - start).count();
const double rate = elapsed > 0.0 ? static_cast<double>(done) / elapsed : 0.0;
std::cout
<< "[cyl_outside] " << label << " " << pass << ": ";
if (total > 0) {
const double pct = 100.0 * static_cast<double>(done) / static_cast<double>(total);
const int bar_width = 28;
const int filled = std::min(
bar_width,
std::max(0, static_cast<int>(std::round((pct / 100.0) * static_cast<double>(bar_width))))
);
std::cout << "[";
for (int i = 0; i < bar_width; ++i) {
std::cout << (i < filled ? "#" : "-");
}
std::cout
<< "] "
<< std::fixed << std::setprecision(1) << pct << "% "
<< done << "/" << total << " " << unit;
} else {
std::cout
<< done << " " << unit;
}
std::cout
<< " elapsed=" << std::setprecision(1) << elapsed << "s"
<< " rate=" << std::setprecision(0) << rate << " " << unit << "/s"
<< (final ? (final_complete ? " done" : " stopped") : "")
<< std::endl;
}
class ProgressPrinter {
public:
ProgressPrinter(
const std::string& label,
const std::string& pass,
int64_t total,
const std::atomic<int64_t>& done,
bool enabled,
const std::string& unit = "vox",
bool partial_ok = false
):
label_(label),
pass_(pass),
total_(total),
done_(done),
enabled_(enabled),
unit_(unit),
partial_ok_(partial_ok),
stop_(false),
start_(std::chrono::steady_clock::now())
{
if (enabled_) {
print_progress_line(label_, pass_, 0, total_, start_, false, false, unit_);
thread_ = std::thread([this]() {
auto last = std::chrono::steady_clock::now();
while (!stop_.load()) {
std::this_thread::sleep_for(std::chrono::milliseconds(100));
if (stop_.load()) {
break;
}
const auto now = std::chrono::steady_clock::now();
if (std::chrono::duration<double>(now - last).count() < 2.0) {
continue;
}
last = now;
print_progress_line(label_, pass_, done_.load(), total_, start_, false, false, unit_);
}
});
}
}
~ProgressPrinter() {
if (!enabled_) {
return;
}
stop_.store(true);
if (thread_.joinable()) {
thread_.join();
}
const int64_t done = done_.load();
print_progress_line(label_, pass_, done, total_, start_, true, partial_ok_ || done >= total_, unit_);
}
private:
std::string label_;
std::string pass_;
int64_t total_;
const std::atomic<int64_t>& done_;
bool enabled_;
std::string unit_;
bool partial_ok_;
std::atomic<bool> stop_;
std::chrono::steady_clock::time_point start_;
std::thread thread_;
};
template <typename Fn>
bool parallel_chunks(int64_t total, int n_threads, int64_t grain, std::atomic<bool>& cancel, Fn fn) {
const int64_t chunk_count = std::max<int64_t>(1, (total + grain - 1) / grain);
n_threads = static_cast<int>(std::max<int64_t>(1, std::min<int64_t>(n_threads, chunk_count)));
std::atomic<int64_t> next(0);
std::atomic<int> active(n_threads);
std::vector<std::thread> workers;
workers.reserve(static_cast<size_t>(n_threads));
for (int ti = 0; ti < n_threads; ++ti) {
workers.emplace_back([&, ti]() {
while (true) {
if (cancel.load(std::memory_order_relaxed)) {
break;
}
const int64_t begin = next.fetch_add(grain);
if (begin >= total) {
break;
}
const int64_t end = std::min(total, begin + grain);
fn(begin, end, ti);
}
active.fetch_sub(1);
});
}
bool interrupted = false;
while (active.load() > 0) {
if (!interrupted && PyErr_CheckSignals() != 0) {
interrupted = true;
cancel.store(true);
}
std::this_thread::sleep_for(std::chrono::milliseconds(50));
}
for (std::thread& worker : workers) {
worker.join();
}
return interrupted;
}
void throw_if_cancelled(
std::atomic<bool>& cancel,
const std::string& label,
const std::string& phase,
bool progress_enabled
) {
if (!cancel.load(std::memory_order_relaxed) && PyErr_CheckSignals() == 0) {
return;
}
cancel.store(true);
if (progress_enabled) {
std::cout
<< "[cyl_outside] " << label
<< ": interrupted during " << phase << "; cancelling"
<< std::endl;
}
if (!PyErr_Occurred()) {
PyErr_SetNone(PyExc_KeyboardInterrupt);
}
throw pybind11::error_already_set();
}
struct DepthScanStats {
double depth_max = 0.0;
int64_t active_count = 0;
int64_t voxels_classified = 0;
int64_t voxels_skipped_by_chunk_mask = 0;
int64_t certified_active_chunks = 0;
int64_t winding_chunks = 0;
int64_t exact_distance_chunks = 0;
int64_t blended_chunks = 0;
int64_t coarse_chunks = 0;
int64_t exact_distance_voxels = 0;
int64_t coarse_voxels = 0;
int64_t total_chunks = 0;
int64_t center_active_chunks = 0;
int64_t surface_seed_chunks = 0;
int64_t initial_candidate_chunks = 0;
int64_t growth_iterations = 0;
int64_t processed_chunks = 0;
int64_t chunks_with_active = 0;
double center_prepass_elapsed = 0.0;
double growth_elapsed = 0.0;
double pass_cpu_seconds = 0.0;
double distance_cpu_seconds = 0.0;
double chunk_probe_distance_seconds = 0.0;
bool used_chunked = false;
bool used_fallback = false;
};
struct ChunkBounds {
int64_t x0 = 0;
int64_t x1 = 0;
int64_t y0 = 0;
int64_t y1 = 0;
int64_t z0 = 0;
int64_t z1 = 0;
};
int64_t flat_voxel_index(int64_t x, int64_t y, int64_t z, int64_t X, int64_t Y) {
return (z * Y + y) * X + x;
}
int64_t flat_chunk_index(int64_t cx, int64_t cy, int64_t cz, int64_t CX, int64_t CY) {
return (cz * CY + cy) * CX + cx;
}
ChunkBounds chunk_bounds_for_coords(
int64_t cx,
int64_t cy,
int64_t cz,
int64_t X,
int64_t Y,
int64_t Z,
int64_t cs
) {
ChunkBounds b;
b.x0 = cx * cs;
b.x1 = std::min<int64_t>(X, b.x0 + cs);
b.y0 = cy * cs;
b.y1 = std::min<int64_t>(Y, b.y0 + cs);
b.z0 = cz * cs;
b.z1 = std::min<int64_t>(Z, b.z0 + cs);
return b;
}
RowVector3d grid_point(
const double* origin,
const double* spacing,
double x,
double y,
double z
) {
return RowVector3d(
origin[0] + x * spacing[0],
origin[1] + y * spacing[1],
origin[2] + z * spacing[2]
);
}
RowVector3d voxel_point(
const double* origin,
const double* spacing,
int64_t x,
int64_t y,
int64_t z
) {
return grid_point(
origin,
spacing,
static_cast<double>(x),
static_cast<double>(y),
static_cast<double>(z)
);
}
RowVector3d chunk_domain_center(
const ChunkBounds& b,
const double* origin,
const double* spacing
) {
return grid_point(
origin,
spacing,
0.5 * static_cast<double>(b.x0 + b.x1),
0.5 * static_cast<double>(b.y0 + b.y1),
0.5 * static_cast<double>(b.z0 + b.z1)
);
}
double chunk_domain_half_diagonal(const ChunkBounds& b, const double* spacing) {
const double hx = 0.5 * static_cast<double>(b.x1 - b.x0) * spacing[0];
const double hy = 0.5 * static_cast<double>(b.y1 - b.y0) * spacing[1];
const double hz = 0.5 * static_cast<double>(b.z1 - b.z0) * spacing[2];
return std::sqrt(hx * hx + hy * hy + hz * hz);
}
int64_t chunk_voxel_count(const ChunkBounds& b) {
return (b.x1 - b.x0) * (b.y1 - b.y0) * (b.z1 - b.z0);
}
double smoothstep(double edge0, double edge1, double x) {
if (!(edge1 > edge0)) {
return x >= edge1 ? 1.0 : 0.0;
}
const double t = std::min(1.0, std::max(0.0, (x - edge0) / (edge1 - edge0)));
return t * t * (3.0 - 2.0 * t);
}
double trilinear_value(const std::array<double, 8>& values, double tx, double ty, double tz) {
tx = std::min(1.0, std::max(0.0, tx));
ty = std::min(1.0, std::max(0.0, ty));
tz = std::min(1.0, std::max(0.0, tz));
const double c00 = values[0] * (1.0 - tx) + values[1] * tx;
const double c10 = values[2] * (1.0 - tx) + values[3] * tx;
const double c01 = values[4] * (1.0 - tx) + values[5] * tx;
const double c11 = values[6] * (1.0 - tx) + values[7] * tx;
const double c0 = c00 * (1.0 - ty) + c10 * ty;
const double c1 = c01 * (1.0 - ty) + c11 * ty;
return c0 * (1.0 - tz) + c1 * tz;
}
double bubble_weight(double tx, double ty, double tz) {
auto axis = [](double t) {
t = std::min(1.0, std::max(0.0, t));
return 4.0 * t * (1.0 - t);
};
return axis(tx) * axis(ty) * axis(tz);
}
double positive_finite_depth(double depth) {
if (!(depth > 0.0) || !std::isfinite(depth)) {
return 0.0;
}
return depth;
}
double capped_positive_depth(double depth, double depth_max) {
const double positive = positive_finite_depth(depth);
if (!(depth_max > 0.0) || !std::isfinite(depth_max)) {
return positive;
}
return std::min(positive, depth_max);
}
int64_t count_marked(const std::vector<uint8_t>& values) {
int64_t count = 0;
for (uint8_t value : values) {
if (value != 0) {
++count;
}
}
return count;
}
void dilate_marked_chunks(
const std::vector<uint8_t>& src,
std::vector<uint8_t>& dst,
int64_t CZ,
int64_t CY,
int64_t CX
) {
for (int64_t cz = 0; cz < CZ; ++cz) {
for (int64_t cy = 0; cy < CY; ++cy) {
for (int64_t cx = 0; cx < CX; ++cx) {
const int64_t ci = flat_chunk_index(cx, cy, cz, CX, CY);
if (src[static_cast<size_t>(ci)] == 0) {
continue;
}
for (int dz = -1; dz <= 1; ++dz) {
const int64_t nz = cz + dz;
if (nz < 0 || nz >= CZ) {
continue;
}
for (int dy = -1; dy <= 1; ++dy) {
const int64_t ny = cy + dy;
if (ny < 0 || ny >= CY) {
continue;
}
for (int dx = -1; dx <= 1; ++dx) {
const int64_t nx = cx + dx;
if (nx < 0 || nx >= CX) {
continue;
}
dst[static_cast<size_t>(flat_chunk_index(nx, ny, nz, CX, CY))] = 1;
}
}
}
}
}
}
}
void dilate_one_chunk_into(
int64_t ci,
std::vector<uint8_t>& dst,
int64_t CZ,
int64_t CY,
int64_t CX
) {
const int64_t cx = ci % CX;
const int64_t yz = ci / CX;
const int64_t cy = yz % CY;
const int64_t cz = yz / CY;
for (int dz = -1; dz <= 1; ++dz) {
const int64_t nz = cz + dz;
if (nz < 0 || nz >= CZ) {
continue;
}
for (int dy = -1; dy <= 1; ++dy) {
const int64_t ny = cy + dy;
if (ny < 0 || ny >= CY) {
continue;
}
for (int dx = -1; dx <= 1; ++dx) {
const int64_t nx = cx + dx;
if (nx < 0 || nx >= CX) {
continue;
}
dst[static_cast<size_t>(flat_chunk_index(nx, ny, nz, CX, CY))] = 1;
}
}
}
}
bool voxel_range_for_coord_range(
double lo,
double hi,
double origin,
double spacing,
int64_t dim,
int64_t& out0,
int64_t& out1
) {
const double v0 = (lo - origin) / spacing;
const double v1 = (hi - origin) / spacing;
int64_t i0 = static_cast<int64_t>(std::floor(v0));
int64_t i1 = static_cast<int64_t>(std::ceil(v1));
if (i1 < 0 || i0 >= dim) {
return false;
}
i0 = std::max<int64_t>(0, std::min<int64_t>(dim - 1, i0));
i1 = std::max<int64_t>(0, std::min<int64_t>(dim - 1, i1));
if (i1 < i0) {
return false;
}
out0 = i0;
out1 = i1;
return true;
}
DepthScanStats scan_full_volume(
const igl::WindingNumberAABB<double, int>& hier,
const igl::AABB<MatrixXdR, 3>& tree,
const MatrixXdR& V,
const MatrixXiR& F,
const double* origin,
const double* spacing,
int64_t Z,
int64_t Y,
int64_t X,
int n_threads,
BarrierMode mode,
double depth_max,
float* depth_ptr,
std::atomic<bool>& cancel,
const std::string& progress_label,
const std::string& pass_label,
bool progress_enabled
) {
const int64_t total = Z * Y * X;
std::vector<double> thread_max(static_cast<size_t>(n_threads), 0.0);
std::vector<int64_t> thread_active(static_cast<size_t>(n_threads), 0);
std::vector<int64_t> thread_classified(static_cast<size_t>(n_threads), 0);
std::vector<double> thread_pass_seconds(static_cast<size_t>(n_threads), 0.0);
std::vector<double> thread_distance_seconds(static_cast<size_t>(n_threads), 0.0);
std::atomic<int64_t> max_done(0);
const auto pass_start = std::chrono::steady_clock::now();
{
ProgressPrinter progress(progress_label, pass_label, total, max_done, progress_enabled);
parallel_chunks(total, n_threads, 2048, cancel, [&](int64_t begin, int64_t end, int ti) {
const auto chunk_start = std::chrono::steady_clock::now();
double local_max = 0.0;
int64_t local_active = 0;
int64_t processed = 0;
double local_distance_seconds = 0.0;
for (int64_t n = begin; n < end; ++n) {
if ((processed & 63) == 0 && cancel.load(std::memory_order_relaxed)) {
break;
}
const int64_t x = n % X;
const int64_t yz = n / X;
const int64_t y = yz % Y;
const int64_t z = yz / Y;
const RowVector3d q(
origin[0] + static_cast<double>(x) * spacing[0],
origin[1] + static_cast<double>(y) * spacing[1],
origin[2] + static_cast<double>(z) * spacing[2]
);
double depth = 0.0;
if (is_active_at(hier, q, mode)) {
const auto distance_start = std::chrono::steady_clock::now();
depth = surface_distance_at(tree, V, F, q);
local_distance_seconds += std::chrono::duration<double>(
std::chrono::steady_clock::now() - distance_start
).count();
}
const float depth_f = static_cast<float>(capped_positive_depth(depth, depth_max));
depth_ptr[n] = depth_f;
local_max = std::max(local_max, static_cast<double>(depth_f));
if (depth_f > 0.0f) {
++local_active;
}
++processed;
}
thread_max[static_cast<size_t>(ti)] = std::max(thread_max[static_cast<size_t>(ti)], local_max);
thread_active[static_cast<size_t>(ti)] += local_active;
thread_classified[static_cast<size_t>(ti)] += processed;
thread_distance_seconds[static_cast<size_t>(ti)] += local_distance_seconds;
thread_pass_seconds[static_cast<size_t>(ti)] += std::chrono::duration<double>(
std::chrono::steady_clock::now() - chunk_start
).count();
max_done.fetch_add(processed);
});
}
DepthScanStats stats;
stats.growth_elapsed = std::chrono::duration<double>(std::chrono::steady_clock::now() - pass_start).count();
for (const double value : thread_max) {
stats.depth_max = std::max(stats.depth_max, value);
}
for (const int64_t value : thread_active) {
stats.active_count += value;
}
for (const int64_t value : thread_classified) {
stats.voxels_classified += value;
}
for (const double value : thread_pass_seconds) {
stats.pass_cpu_seconds += value;
}
for (const double value : thread_distance_seconds) {
stats.distance_cpu_seconds += value;
}
return stats;
}
DepthScanStats scan_chunked_volume(
const igl::WindingNumberAABB<double, int>& hier,
const igl::AABB<MatrixXdR, 3>& tree,
const MatrixXdR& V,
const MatrixXiR& F,
const double* origin,
const double* spacing,
int64_t Z,
int64_t Y,
int64_t X,
int n_threads,
int chunk_size,
double deep_interp_chunks,
double deep_blend_chunks,
BarrierMode mode,
double depth_max,
float* depth_ptr,
std::atomic<bool>& cancel,
const std::string& progress_label,
bool progress_enabled
) {
const int64_t total = Z * Y * X;
const int64_t cs = static_cast<int64_t>(std::max(1, chunk_size));
const int64_t CX = (X + cs - 1) / cs;
const int64_t CY = (Y + cs - 1) / cs;
const int64_t CZ = (Z + cs - 1) / cs;
const int64_t total_chunks = CX * CY * CZ;
const double max_spacing = std::max({spacing[0], spacing[1], spacing[2]});
const double deep_threshold = std::max(0.0, deep_interp_chunks) * static_cast<double>(cs) * max_spacing;
const double deep_blend_width = std::max(0.0, deep_blend_chunks) * static_cast<double>(cs) * max_spacing;
const bool coarse_enabled = deep_interp_chunks > 0.0;
DepthScanStats stats;
stats.used_chunked = true;
stats.total_chunks = total_chunks;
stats.voxels_skipped_by_chunk_mask = total;
std::fill(depth_ptr, depth_ptr + total, 0.0f);
std::vector<uint8_t> center_active(static_cast<size_t>(total_chunks), 0);
std::vector<int64_t> thread_center_active(static_cast<size_t>(n_threads), 0);
std::atomic<int64_t> center_done(0);
const auto center_start = std::chrono::steady_clock::now();
{
ProgressPrinter progress(
progress_label,
"chunk center winding prepass",
total_chunks,
center_done,
progress_enabled,
"chunks"
);
parallel_chunks(total_chunks, n_threads, 64, cancel, [&](int64_t begin, int64_t end, int ti) {
int64_t local_active = 0;
int64_t processed = 0;
for (int64_t ci = begin; ci < end; ++ci) {
if ((processed & 15) == 0 && cancel.load(std::memory_order_relaxed)) {
break;
}
const int64_t cx = ci % CX;
const int64_t yz = ci / CX;
const int64_t cy = yz % CY;
const int64_t cz = yz / CY;
const ChunkBounds b = chunk_bounds_for_coords(cx, cy, cz, X, Y, Z, cs);
const RowVector3d q = chunk_domain_center(b, origin, spacing);
if (is_active_at(hier, q, mode)) {
center_active[static_cast<size_t>(ci)] = 1;
++local_active;
}
++processed;
}
thread_center_active[static_cast<size_t>(ti)] += local_active;
center_done.fetch_add(processed);
});
}
stats.center_prepass_elapsed = std::chrono::duration<double>(
std::chrono::steady_clock::now() - center_start
).count();
for (const int64_t value : thread_center_active) {
stats.center_active_chunks += value;
}
throw_if_cancelled(cancel, progress_label, "chunk center prepass", progress_enabled);
std::vector<uint8_t> surface_seed(static_cast<size_t>(total_chunks), 0);
for (int64_t fi = 0; fi < F.rows(); ++fi) {
const int i0 = F(fi, 0);
const int i1 = F(fi, 1);
const int i2 = F(fi, 2);
const double xmin = std::min({V(i0, 0), V(i1, 0), V(i2, 0)});
const double xmax = std::max({V(i0, 0), V(i1, 0), V(i2, 0)});
const double ymin = std::min({V(i0, 1), V(i1, 1), V(i2, 1)});
const double ymax = std::max({V(i0, 1), V(i1, 1), V(i2, 1)});
const double zmin = std::min({V(i0, 2), V(i1, 2), V(i2, 2)});
const double zmax = std::max({V(i0, 2), V(i1, 2), V(i2, 2)});
int64_t vx0 = 0, vx1 = 0, vy0 = 0, vy1 = 0, vz0 = 0, vz1 = 0;
if (!voxel_range_for_coord_range(xmin, xmax, origin[0], spacing[0], X, vx0, vx1) ||
!voxel_range_for_coord_range(ymin, ymax, origin[1], spacing[1], Y, vy0, vy1) ||
!voxel_range_for_coord_range(zmin, zmax, origin[2], spacing[2], Z, vz0, vz1)) {
continue;
}
for (int64_t cz = vz0 / cs; cz <= vz1 / cs; ++cz) {
for (int64_t cy = vy0 / cs; cy <= vy1 / cs; ++cy) {
for (int64_t cx = vx0 / cs; cx <= vx1 / cs; ++cx) {
surface_seed[static_cast<size_t>(flat_chunk_index(cx, cy, cz, CX, CY))] = 1;
}
}
}
if ((fi & 255) == 0) {
throw_if_cancelled(cancel, progress_label, "surface chunk seeding", progress_enabled);
}
}
stats.surface_seed_chunks = count_marked(surface_seed);
std::vector<uint8_t> candidate(static_cast<size_t>(total_chunks), 0);
dilate_marked_chunks(center_active, candidate, CZ, CY, CX);
dilate_marked_chunks(surface_seed, candidate, CZ, CY, CX);
stats.initial_candidate_chunks = count_marked(candidate);
std::vector<uint8_t> processed(static_cast<size_t>(total_chunks), 0);
std::vector<double> thread_max(static_cast<size_t>(n_threads), 0.0);
std::vector<int64_t> thread_active(static_cast<size_t>(n_threads), 0);
std::vector<int64_t> thread_classified(static_cast<size_t>(n_threads), 0);
std::vector<int64_t> thread_processed_chunks(static_cast<size_t>(n_threads), 0);
std::vector<int64_t> thread_chunks_with_active(static_cast<size_t>(n_threads), 0);
std::vector<int64_t> thread_certified_active_chunks(static_cast<size_t>(n_threads), 0);
std::vector<int64_t> thread_winding_chunks(static_cast<size_t>(n_threads), 0);
std::vector<int64_t> thread_exact_distance_chunks(static_cast<size_t>(n_threads), 0);
std::vector<int64_t> thread_blended_chunks(static_cast<size_t>(n_threads), 0);
std::vector<int64_t> thread_coarse_chunks(static_cast<size_t>(n_threads), 0);
std::vector<int64_t> thread_exact_distance_voxels(static_cast<size_t>(n_threads), 0);
std::vector<int64_t> thread_coarse_voxels(static_cast<size_t>(n_threads), 0);
std::vector<double> thread_pass_seconds(static_cast<size_t>(n_threads), 0.0);
std::vector<double> thread_distance_seconds(static_cast<size_t>(n_threads), 0.0);
std::vector<double> thread_chunk_probe_distance_seconds(static_cast<size_t>(n_threads), 0.0);
std::vector<std::vector<int64_t>> thread_active_chunks(static_cast<size_t>(n_threads));
std::atomic<int64_t> growth_done(0);
const auto growth_start = std::chrono::steady_clock::now();
{
ProgressPrinter progress(
progress_label,
"candidate/growth winding+violation-distance",
0,
growth_done,
progress_enabled,
"vox classified",
true
);
while (true) {
throw_if_cancelled(cancel, progress_label, "candidate/growth scheduling", progress_enabled);
std::vector<int64_t> scan_list;
scan_list.reserve(static_cast<size_t>(std::min<int64_t>(total_chunks, 4096)));
for (int64_t ci = 0; ci < total_chunks; ++ci) {
if (candidate[static_cast<size_t>(ci)] != 0 && processed[static_cast<size_t>(ci)] == 0) {
processed[static_cast<size_t>(ci)] = 1;
scan_list.push_back(ci);
}
}
if (scan_list.empty()) {
break;
}
++stats.growth_iterations;
for (std::vector<int64_t>& chunks : thread_active_chunks) {
chunks.clear();
}
parallel_chunks(static_cast<int64_t>(scan_list.size()), n_threads, 1, cancel, [&](int64_t begin, int64_t end, int ti) {
const auto chunk_start = std::chrono::steady_clock::now();
double local_max = 0.0;
int64_t local_active = 0;
int64_t local_classified = 0;
int64_t local_processed_chunks = 0;
int64_t local_chunks_with_active = 0;
int64_t local_certified_active_chunks = 0;
int64_t local_winding_chunks = 0;
int64_t local_exact_distance_chunks = 0;
int64_t local_blended_chunks = 0;
int64_t local_coarse_chunks = 0;
int64_t local_exact_distance_voxels = 0;
int64_t local_coarse_voxels = 0;
double local_distance_seconds = 0.0;
double local_probe_distance_seconds = 0.0;
std::vector<int64_t>& local_active_chunks = thread_active_chunks[static_cast<size_t>(ti)];
auto query_distance = [&](const RowVector3d& q, bool probe) {
const auto distance_start = std::chrono::steady_clock::now();
const double depth = surface_distance_at(tree, V, F, q);
const double elapsed = std::chrono::duration<double>(
std::chrono::steady_clock::now() - distance_start
).count();
if (probe) {
local_probe_distance_seconds += elapsed;
} else {
local_distance_seconds += elapsed;
}
return depth;
};
auto write_depth = [&](int64_t n, double depth, int64_t& chunk_active) {
const float depth_f = static_cast<float>(capped_positive_depth(depth, depth_max));
depth_ptr[n] = depth_f;
local_max = std::max(local_max, static_cast<double>(depth_f));
if (depth_f > 0.0f) {
++local_active;
++chunk_active;
}
};
for (int64_t si = begin; si < end; ++si) {
if (cancel.load(std::memory_order_relaxed)) {
break;
}
const int64_t ci = scan_list[static_cast<size_t>(si)];
const int64_t cx = ci % CX;
const int64_t yz = ci / CX;
const int64_t cy = yz % CY;
const int64_t cz = yz / CY;
const ChunkBounds b = chunk_bounds_for_coords(cx, cy, cz, X, Y, Z, cs);
int64_t chunk_active = 0;
int64_t chunk_processed = 0;
bool used_certified = false;
bool used_blended = false;
bool used_coarse_only = false;
std::array<double, 8> corner_depths{};
double center_correction = 0.0;
const int64_t voxel_count = chunk_voxel_count(b);
if (center_active[static_cast<size_t>(ci)] != 0) {
const RowVector3d center_q = chunk_domain_center(b, origin, spacing);
const double center_distance = positive_finite_depth(query_distance(center_q, true));
const double half_diag = chunk_domain_half_diagonal(b, spacing);
if (center_distance > half_diag) {
used_certified = true;
++local_certified_active_chunks;
const double chunk_distance_lower = center_distance - half_diag;
const double chunk_distance_upper = center_distance + half_diag;
used_coarse_only = coarse_enabled &&
chunk_distance_lower >= deep_threshold + deep_blend_width;
used_blended = coarse_enabled && !used_coarse_only &&
chunk_distance_upper > deep_threshold;
if (used_coarse_only || used_blended) {
for (int zbit = 0; zbit <= 1; ++zbit) {
for (int ybit = 0; ybit <= 1; ++ybit) {
for (int xbit = 0; xbit <= 1; ++xbit) {
const int idx = xbit + 2 * ybit + 4 * zbit;
const RowVector3d q = grid_point(
origin,
spacing,
static_cast<double>(xbit ? b.x1 : b.x0),
static_cast<double>(ybit ? b.y1 : b.y0),
static_cast<double>(zbit ? b.z1 : b.z0)
);
corner_depths[static_cast<size_t>(idx)] =
positive_finite_depth(query_distance(q, true));
}
}
}
center_correction = center_distance -
trilinear_value(corner_depths, 0.5, 0.5, 0.5);
}
}
}
auto coarse_depth_at = [&](int64_t x, int64_t y, int64_t z) {
const double tx = static_cast<double>(x - b.x0) / static_cast<double>(b.x1 - b.x0);
const double ty = static_cast<double>(y - b.y0) / static_cast<double>(b.y1 - b.y0);
const double tz = static_cast<double>(z - b.z0) / static_cast<double>(b.z1 - b.z0);
const double depth = trilinear_value(corner_depths, tx, ty, tz) +
bubble_weight(tx, ty, tz) * center_correction;
return positive_finite_depth(depth);
};
if (used_certified && used_coarse_only) {
++local_coarse_chunks;
local_coarse_voxels += voxel_count;
for (int64_t z = b.z0; z < b.z1; ++z) {
for (int64_t y = b.y0; y < b.y1; ++y) {
for (int64_t x = b.x0; x < b.x1; ++x) {
if ((chunk_processed & 63) == 0 && cancel.load(std::memory_order_relaxed)) {
break;
}
const int64_t n = flat_voxel_index(x, y, z, X, Y);
write_depth(n, coarse_depth_at(x, y, z), chunk_active);
++chunk_processed;
}
if (cancel.load(std::memory_order_relaxed)) {
break;
}
}
if (cancel.load(std::memory_order_relaxed)) {
break;
}
}
} else if (used_certified && used_blended) {
++local_blended_chunks;
local_coarse_voxels += voxel_count;
for (int64_t z = b.z0; z < b.z1; ++z) {
for (int64_t y = b.y0; y < b.y1; ++y) {
for (int64_t x = b.x0; x < b.x1; ++x) {
if ((chunk_processed & 63) == 0 && cancel.load(std::memory_order_relaxed)) {
break;
}
const int64_t n = flat_voxel_index(x, y, z, X, Y);
const double exact_depth = positive_finite_depth(query_distance(
voxel_point(origin, spacing, x, y, z),
false
));
++local_exact_distance_voxels;
const double coarse_depth = coarse_depth_at(x, y, z);
const double w = smoothstep(
deep_threshold,
deep_threshold + deep_blend_width,
exact_depth
);
write_depth(n, exact_depth * (1.0 - w) + coarse_depth * w, chunk_active);
++chunk_processed;
}
if (cancel.load(std::memory_order_relaxed)) {
break;
}
}
if (cancel.load(std::memory_order_relaxed)) {
break;
}
}
} else if (used_certified) {
++local_exact_distance_chunks;
for (int64_t z = b.z0; z < b.z1; ++z) {
for (int64_t y = b.y0; y < b.y1; ++y) {
for (int64_t x = b.x0; x < b.x1; ++x) {
if ((chunk_processed & 63) == 0 && cancel.load(std::memory_order_relaxed)) {
break;
}
const int64_t n = flat_voxel_index(x, y, z, X, Y);
write_depth(n, query_distance(voxel_point(origin, spacing, x, y, z), false), chunk_active);
++local_exact_distance_voxels;
++chunk_processed;
}
if (cancel.load(std::memory_order_relaxed)) {
break;
}
}
if (cancel.load(std::memory_order_relaxed)) {
break;
}
}
} else {
++local_winding_chunks;
for (int64_t z = b.z0; z < b.z1; ++z) {
for (int64_t y = b.y0; y < b.y1; ++y) {
for (int64_t x = b.x0; x < b.x1; ++x) {
if ((chunk_processed & 63) == 0 && cancel.load(std::memory_order_relaxed)) {
break;
}