-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.cpp
More file actions
1503 lines (1117 loc) · 58.2 KB
/
main.cpp
File metadata and controls
1503 lines (1117 loc) · 58.2 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 <openvdb/openvdb.h>
#include <openvdb/io/File.h>
#include <openvdb/tools/GridTransformer.h>
#include <iostream>
#include <tira/volume.h>
#include "tira/image/colormap.h"
#include "tira/image.h"
#include <openvdb/tools/VolumeToMesh.h>
#include <openvdb/tools/MeshToVolume.h>
#include <openvdb/tools/Fastsweeping.h>
#include <openvdb/tools/GridOperators.h>
#include <openvdb/tools/LevelsetFilter.h>
#include <openvdb/tools/Filter.h>
#include <openvdb/tools/LevelSetSphere.h>
#include <openvdb/tools/Composite.h>
#include <openvdb/tools/ChangeBackground.h>
#include <limits>
#include <openvdb/tools/Morphology.h>
#include <openvdb/tools/GridTransformer.h>
#include <openvdb/tools/LevelSetUtil.h>
#include <openvdb/tools/ValueTransformer.h>
#include <cmath>
#include <openvdb/tools/Interpolation.h>
#include <openvdb/tools/Morphology.h>
#include <mutex>
#include <tbb/enumerable_thread_specific.h>
#include <queue>
#include <tbb/tbb.h>
#include <unordered_set>
using namespace openvdb;
//vdb to image conversion for 3D
template<class GridType> void vdb2img3D(GridType& grid, tira::volume<float>& img) {
using ValueT = typename GridType::ValueType;
typename GridType::Accessor accessor = grid.getAccessor();
//openvdb::Coord dim = grid.evalActiveVoxelDim();
openvdb::Coord ijk;
int& i = ijk[0], & j = ijk[1], & k = ijk[2];
for (i = 0; i < img.X(); i++) {
for (j = 0; j < img.Y(); j++) {
for (k = 0; k < img.Z(); k++) {
float pixel = (float)accessor.getValue(ijk);
img(i, j, k) = pixel;
}
}
}
}
openvdb::FloatGrid::Ptr heaviside_return(openvdb::FloatGrid::Ptr& grid, float& backgroundvalue)
{
float epsilon = 0.2;
// Get accessor for the grid
openvdb::FloatGrid::Ptr resultGrid = openvdb::FloatGrid::create(backgroundvalue);
resultGrid->setTransform(grid->transform().copy());
openvdb::FloatGrid::Accessor resultAccessor = resultGrid->getAccessor();
// Iterate over the grid's active values
for (openvdb::FloatGrid::ValueOnIter iter = grid->beginValueOn(); iter; ++iter) {
float value = *iter;
// Calculate the Heaviside function value
float heaviside = (0.5 * (1 + (2 / 3.14159265358979323846) * atan(value / epsilon)));
//resultGrid->tree().setValueOn(iter.getCoord(), heaviside);
resultAccessor.setValueOn(iter.getCoord(), heaviside);
}
return resultGrid;
}
// Heaviside function
template <class GridType>
void heaviside(GridType& grid)
{
float epsilon = 0.2;
// Get accessor for the grid
typename GridType::Accessor accessor = grid.getAccessor();
// Iterate over the grid's active values
for (typename GridType::ValueOnIter iter = grid.beginValueOn(); iter; ++iter) {
// Get the current value
const typename GridType::ValueType value = iter.getValue();
// Calculate the Heaviside function value
const typename GridType::ValueType heaviside = (0.5 * (1 + (2 / 3.14159265358979323846) * atan(value / epsilon)));
accessor.setValue(iter.getCoord(), heaviside);
}
}
// derivative of Heaviside function
template <class GridType>
void deri_heaviside(GridType& grid)
{
float epsilon = 0.2;
typename GridType::Accessor accessor = grid.getAccessor();
for (typename GridType::ValueOnIter iter = grid.beginValueOn(); iter; ++iter) {
const typename GridType::ValueType value = iter.getValue();
const typename GridType::ValueType deri_heaviside = (1 / 3.14159265358979323846) * (epsilon / ((epsilon * epsilon) + (value * value)));
accessor.setValue(iter.getCoord(), deri_heaviside);
}
}
// inverse a vdb file
template <class GridType>
void vdb_inverse(GridType& grid)
{
typename GridType::Accessor accessor = grid.getAccessor();
for (typename GridType::ValueOnIter iter = grid.beginValueOn(); iter; ++iter) {
const typename GridType::ValueType value = iter.getValue();
const typename GridType::ValueType inverse_vdb = (1 - value);
accessor.setValue(iter.getCoord(), inverse_vdb);
}
}
// Function to multiply two VDB grids
openvdb::FloatGrid::Ptr multiplyGrids(openvdb::FloatGrid::Ptr& grid1, openvdb::FloatGrid::Ptr& grid2, float& backgroundvalue) {
openvdb::FloatGrid::Ptr resultGrid = openvdb::FloatGrid::create(backgroundvalue);
// Get accessors for the grids
openvdb::FloatGrid::Accessor resultAccessor = resultGrid->getAccessor();
openvdb::FloatGrid::Accessor grid1Accessor = grid1->getAccessor();
openvdb::FloatGrid::Accessor grid2Accessor = grid2->getAccessor();
for (openvdb::FloatGrid::ValueOnIter iter = grid1->beginValueOn(); iter; ++iter) {
const openvdb::Coord& coord = iter.getCoord();
// Multiply the values from grid1 and grid2
resultAccessor.setValue(coord, grid1Accessor.getValue(coord) * grid2Accessor.getValue(coord));
}
return resultGrid;
}
// Function to divide two VDB grids
openvdb::FloatGrid::Ptr divideGrids(openvdb::FloatGrid::Ptr& grid1, openvdb::FloatGrid::Ptr& grid2) {
openvdb::FloatGrid::Ptr resultGrid = openvdb::FloatGrid::create();
resultGrid = grid1->deepCopy();
// Get accessors for the grids
openvdb::FloatGrid::Accessor resultAccessor = resultGrid->getAccessor();
openvdb::FloatGrid::Accessor grid1Accessor = grid1->getAccessor();
openvdb::FloatGrid::Accessor grid2Accessor = grid2->getAccessor();
for (openvdb::FloatGrid::ValueOnIter iter = grid1->beginValueOn(); iter; ++iter) {
const openvdb::Coord& coord = iter.getCoord();
// value from the second grid
float grid2Value = grid2Accessor.getValue(coord);
// if the divisor is not zero to avoid division by zero
if (grid2Value != 0.0f) {
// Divide the values from grid1 by grid2, and set in resultGrid
resultAccessor.setValue(coord, grid1Accessor.getValue(coord) / grid2Value);
}
else {
// Handle division by zero
resultAccessor.setValue(coord, 0.0f);
}
}
return resultGrid;
}
openvdb::FloatGrid::Ptr calculateGradientX(const openvdb::FloatGrid::Ptr& grid) {
/* openvdb::FloatGrid::Ptr gradXGrid = openvdb::FloatGrid::create(0.0);
gradXGrid->setTransform(grid->transform().copy());*/
openvdb::FloatGrid::Ptr gradXGrid = openvdb::FloatGrid::create();
gradXGrid = grid->deepCopy();
openvdb::FloatGrid::ConstAccessor accessor = grid->getConstAccessor();
for (openvdb::FloatGrid::ValueOnCIter iter = grid->cbeginValueOn(); iter; ++iter) {
const openvdb::Coord& xyz = iter.getCoord();
float valueLeft = accessor.isValueOn(xyz.offsetBy(-1, 0, 0)) ? accessor.getValue(xyz.offsetBy(-1, 0, 0)) : iter.getValue();
float valueRight = accessor.isValueOn(xyz.offsetBy(1, 0, 0)) ? accessor.getValue(xyz.offsetBy(1, 0, 0)) : iter.getValue();
float gradX = (valueRight - valueLeft) / 2.0f;
gradXGrid->tree().setValueOn(xyz, gradX);
}
return gradXGrid;
}
openvdb::FloatGrid::Ptr calculateGradientY(const openvdb::FloatGrid::Ptr& grid) {
/*openvdb::FloatGrid::Ptr gradYGrid = openvdb::FloatGrid::create(0.0);
gradYGrid->setTransform(grid->transform().copy());*/
openvdb::FloatGrid::Ptr gradYGrid = openvdb::FloatGrid::create();
gradYGrid = grid->deepCopy();
openvdb::FloatGrid::ConstAccessor accessor = grid->getConstAccessor();
for (openvdb::FloatGrid::ValueOnCIter iter = grid->cbeginValueOn(); iter; ++iter) {
const openvdb::Coord& xyz = iter.getCoord();
float valueDown = accessor.isValueOn(xyz.offsetBy(0, -1, 0)) ? accessor.getValue(xyz.offsetBy(0, -1, 0)) : iter.getValue();
float valueUp = accessor.isValueOn(xyz.offsetBy(0, 1, 0)) ? accessor.getValue(xyz.offsetBy(0, 1, 0)) : iter.getValue();
float gradY = (valueUp - valueDown) / 2.0f;
gradYGrid->tree().setValueOn(xyz, gradY);
}
return gradYGrid;
}
openvdb::FloatGrid::Ptr calculateGradientZ(const openvdb::FloatGrid::Ptr& grid) {
/*openvdb::FloatGrid::Ptr gradZGrid = openvdb::FloatGrid::create(0.0);
gradZGrid->setTransform(grid->transform().copy());*/
openvdb::FloatGrid::Ptr gradZGrid = openvdb::FloatGrid::create();
gradZGrid = grid->deepCopy();
openvdb::FloatGrid::ConstAccessor accessor = grid->getConstAccessor();
for (openvdb::FloatGrid::ValueOnCIter iter = grid->cbeginValueOn(); iter; ++iter) {
const openvdb::Coord& xyz = iter.getCoord();
float valueBelow = accessor.isValueOn(xyz.offsetBy(0, 0, -1)) ? accessor.getValue(xyz.offsetBy(0, 0, -1)) : iter.getValue();
float valueAbove = accessor.isValueOn(xyz.offsetBy(0, 0, 1)) ? accessor.getValue(xyz.offsetBy(0, 0, 1)) : iter.getValue();
float gradZ = (valueAbove - valueBelow) / 2.0f;
gradZGrid->tree().setValueOn(xyz, gradZ);
}
return gradZGrid;
}
openvdb::FloatGrid::Ptr calculateGradientMagnitude(
const openvdb::FloatGrid::Ptr& gradXGrid,
const openvdb::FloatGrid::Ptr& gradYGrid,
const openvdb::FloatGrid::Ptr& gradZGrid) {
/*openvdb::FloatGrid::Ptr gradMagGrid = openvdb::FloatGrid::create(0.0);
gradMagGrid->setTransform(gradXGrid->transform().copy());*/
openvdb::FloatGrid::Ptr gradMagGrid = openvdb::FloatGrid::create();
gradMagGrid = gradXGrid->deepCopy();
// gradient grids
openvdb::FloatGrid::ConstAccessor accessorX = gradXGrid->getConstAccessor();
openvdb::FloatGrid::ConstAccessor accessorY = gradYGrid->getConstAccessor();
openvdb::FloatGrid::ConstAccessor accessorZ = gradZGrid->getConstAccessor();
for (openvdb::FloatGrid::ValueOnCIter iter = gradXGrid->cbeginValueOn(); iter; ++iter) {
const openvdb::Coord& xyz = iter.getCoord();
// for the current voxel
float gradX = accessorX.getValue(xyz);
float gradY = accessorY.getValue(xyz);
float gradZ = accessorZ.getValue(xyz);
// magnitude
float magnitude = openvdb::math::Sqrt(gradX * gradX + gradY * gradY + gradZ * gradZ);
// computed magnitude in the output grid
gradMagGrid->tree().setValueOn(xyz, magnitude);
}
return gradMagGrid;
}
////////////////////////////////////////TBB_PARALLEL_FOR()///////////////////////////////////////////////////////
// Functor for use with tbb::parallel_for() that operates on a grid's leaf nodes
//TBB_HEAVISIDE
struct Heaviside_Processor {
using GridType = openvdb::FloatGrid;
using TreeType = GridType::TreeType;
using LeafNode = TreeType::LeafNodeType;
// splits the iteration space of a leaf iterator
using IterRange = openvdb::tree::IteratorRange<TreeType::LeafCIter>;
float epsilon;
Heaviside_Processor(float eps) : epsilon(eps) {}
void operator()(IterRange& range) const {
for (; range; ++range) {
auto& leaf = const_cast<LeafNode&>(*range.iterator()); // modifiable leaf node
for (auto iter = leaf.beginValueOn(); iter; ++iter) {
const auto value = iter.getValue();
const auto heaviside = static_cast<float>(
0.5 * (1 + (2 / 3.14159265358979323846) * atan(value / epsilon)));
leaf.setValueOn(iter.offset(), heaviside);
}
}
}
};
void heaviside_tbb(openvdb::FloatGrid::Ptr grid) {
Heaviside_Processor proc(0.2); // Epsilon value
// Wrap a leaf iterator in an IteratorRange and run in parallel
Heaviside_Processor::IterRange range(grid->tree().cbeginLeaf());
tbb::parallel_for(range, proc);
}
//Derivative Heavisisde_TBB
struct deriv_Heaviside_Processor {
using GridType = openvdb::FloatGrid;
using TreeType = GridType::TreeType;
using LeafNode = TreeType::LeafNodeType;
// Isplits the iteration space of a leaf iterator
using IterRange = openvdb::tree::IteratorRange<TreeType::LeafCIter>;
float epsilon;
deriv_Heaviside_Processor(float eps) : epsilon(eps) {}
void operator()(IterRange& range) const {
for (; range; ++range) {
auto& leaf = const_cast<LeafNode&>(*range.iterator()); // modifiable leaf node
for (auto iter = leaf.beginValueOn(); iter; ++iter) {
const auto value = iter.getValue();
const auto deri_heaviside = static_cast<float>(
(1 / 3.14159265358979323846) * (epsilon / ((epsilon * epsilon) + (value * value))));
leaf.setValueOn(iter.offset(), deri_heaviside);
}
}
}
};
void deri_heaviside_tbb(openvdb::FloatGrid::Ptr grid) {
deriv_Heaviside_Processor proc(0.2); // Epsilon value
// Wrap a leaf iterator in an IteratorRange and run in parallel
deriv_Heaviside_Processor::IterRange range(grid->tree().cbeginLeaf());
tbb::parallel_for(range, proc);
}
// INVERSE VDB
template <class GridType>
struct InverseProcessor {
using ValueType = typename GridType::ValueType;
using TreeType = typename GridType::TreeType;
using LeafNode = typename TreeType::LeafNodeType;
// splits the iteration space of a leaf iterator
using IterRange = openvdb::tree::IteratorRange<typename TreeType::LeafCIter>;
void operator()(IterRange& range) const {
for (; range; ++range) {
auto& leaf = const_cast<LeafNode&>(*range.iterator()); // modifiable leaf node
for (auto iter = leaf.beginValueOn(); iter; ++iter) {
ValueType value = iter.getValue();
ValueType inverse_value = 1 - value; // inverse of the current value
leaf.setValueOn(iter.offset(), inverse_value);
}
}
}
};
template <class GridType>
void vdb_inverse_tbb(GridType& grid) {
InverseProcessor<GridType> proc;
// Wrap a leaf iterator in an IteratorRange and run in parallel
typename InverseProcessor<GridType>::IterRange range(grid.tree().cbeginLeaf());
tbb::parallel_for(range, proc);
}
struct GridMultiplier {
using GridType = openvdb::FloatGrid;
using TreeType = GridType::TreeType;
using LeafNode = TreeType::LeafNodeType;
using IterRange = openvdb::tree::IteratorRange<typename TreeType::LeafCIter>;
GridType::ConstAccessor acc1;
GridType::ConstAccessor acc2;
GridMultiplier(GridType::ConstAccessor accessor1, GridType::ConstAccessor accessor2)
: acc1(accessor1), acc2(accessor2) {}
void operator()(IterRange& range) const {
for (; range; ++range) {
auto& leaf1 = const_cast<LeafNode&>(*range.iterator());
if (const LeafNode* leaf2 = acc2.probeConstLeaf(leaf1.origin())) { // Match leaf in grid2
for (auto iter = leaf1.beginValueOn(); iter; ++iter) {
const openvdb::Coord coord = iter.getCoord();
if (leaf2->isValueOn(coord)) { // if the value is active in grid2
float value1 = iter.getValue();
float value2 = leaf2->getValue(coord);
leaf1.setValueOn(iter.offset(), value1 * value2); // Multiply values
}
else {
leaf1.setValueOff(iter.offset()); // Optionally turn off if no corresponding value in grid2
}
}
}
else {
// no corresponding leaf in grid2
for (auto iter = leaf1.beginValueOn(); iter; ++iter) {
leaf1.setValueOff(iter.offset());
}
}
}
}
};
openvdb::FloatGrid::Ptr multiplyGrids_tbb(openvdb::FloatGrid::Ptr& grid1, openvdb::FloatGrid::Ptr& grid2, float backgroundValue) {
openvdb::FloatGrid::Ptr resultGrid = openvdb::FloatGrid::create(backgroundValue);
resultGrid = grid1->deepCopy();
openvdb::FloatGrid::Accessor resultAccessor = resultGrid->getAccessor();
GridMultiplier proc(resultGrid->getConstAccessor(), grid2->getConstAccessor());
GridMultiplier::IterRange range(resultGrid->tree().cbeginLeaf());
tbb::parallel_for(range, proc);
return resultGrid;
}
struct VoxelProcessor {
using GridType = openvdb::FloatGrid;
using TreeType = GridType::TreeType;
using LeafNode = TreeType::LeafNodeType;
using IterRange = openvdb::tree::IteratorRange<typename TreeType::LeafCIter>;
GridType::ConstPtr input_grid, fout, fin;
tbb::enumerable_thread_specific<GridType::Ptr>& threadLocalEout;
tbb::enumerable_thread_specific<GridType::Ptr>& threadLocalEin;
float factor;
VoxelProcessor(GridType::ConstPtr ig, GridType::ConstPtr fout, GridType::ConstPtr fin, tbb::enumerable_thread_specific<GridType::Ptr>& eo, tbb::enumerable_thread_specific<GridType::Ptr>& ei, float f)
: input_grid(ig), fout(fout), fin(fin), threadLocalEout(eo), threadLocalEin(ei), factor(f) {}
void operator()(IterRange& range) const {
for (; range; ++range) {
const LeafNode& leaf = *range.iterator();
openvdb::Coord xyz;
auto& EoutGrid = threadLocalEout.local();
auto& EinGrid = threadLocalEin.local();
auto EoutAccessor = EoutGrid->getAccessor();
auto EinAccessor = EinGrid->getAccessor();
for (auto voxelIter = leaf.beginValueOn(); voxelIter; ++voxelIter) {
xyz = voxelIter.getCoord();
float s1 = 0.0f, s2 = 0.0f;
int sigma1 = 0;
for (int u = -sigma1; u <= sigma1; ++u) {
for (int v = -sigma1; v <= sigma1; ++v) {
for (int w = -sigma1; w <= sigma1; ++w) {
openvdb::Coord neighbor = xyz.offsetBy(u, v, w);
if (input_grid->tree().isValueOn(neighbor)) {
float inputValue = input_grid->tree().getValue(neighbor);
float foutValue = 0.0f, finValue = 0.0f;
if (fout->tree().isValueOn(neighbor)) {
foutValue = fout->tree().getValue(neighbor);
}
if (fin->tree().isValueOn(neighbor)) {
finValue = fin->tree().getValue(neighbor);
}
s1 += factor * std::pow(inputValue - foutValue, 2);
s2 += factor * std::pow(inputValue - finValue, 2);
}
}
}
}
EoutAccessor.setValueOn(xyz, s1);
EinAccessor.setValueOn(xyz, s2);
}
}
}
};
void parallelVoxelComputation(openvdb::FloatGrid::ConstPtr input_grid, openvdb::FloatGrid::ConstPtr fout, openvdb::FloatGrid::ConstPtr fin, openvdb::FloatGrid::Ptr Eout, openvdb::FloatGrid::Ptr Ein, float sigma1) {
float kernelSize = (2 * sigma1) + 1;
float factor = 1 / std::pow(kernelSize, 3);
// Create thread-local grids
tbb::enumerable_thread_specific<openvdb::FloatGrid::Ptr> threadLocalEout([&]() {
auto localGrid = openvdb::FloatGrid::create();
localGrid->setTransform(input_grid->transform().copy());
return localGrid;
});
tbb::enumerable_thread_specific<openvdb::FloatGrid::Ptr> threadLocalEin([&]() {
auto localGrid = openvdb::FloatGrid::create();
localGrid->setTransform(input_grid->transform().copy());
return localGrid;
});
// iterator range over leaf nodes
VoxelProcessor::IterRange range(input_grid->tree().cbeginLeaf());
// tbb
tbb::parallel_for(range, VoxelProcessor(input_grid, fout, fin, threadLocalEout, threadLocalEin, factor));
// merging the thread-local grids
for (auto& localGrid : threadLocalEout) {
Eout->tree().merge(localGrid->tree());
}
for (auto& localGrid : threadLocalEin) {
Ein->tree().merge(localGrid->tree());
}
}
struct GridDivider {
using GridType = openvdb::FloatGrid;
using TreeType = GridType::TreeType;
using LeafNode = TreeType::LeafNodeType;
using IterRange = openvdb::tree::IteratorRange<typename TreeType::LeafCIter>;
GridType::ConstAccessor acc1;
GridType::ConstAccessor acc2;
GridDivider(GridType::ConstAccessor accessor1, GridType::ConstAccessor accessor2)
: acc1(accessor1), acc2(accessor2) {}
void operator()(IterRange& range) const {
for (; range; ++range) {
auto& leaf1 = const_cast<LeafNode&>(*range.iterator()); // modifiable leaf node from grid1
if (const LeafNode* leaf2 = acc2.probeConstLeaf(leaf1.origin())) { // matching --- leaf in grid2
for (auto iter = leaf1.beginValueOn(); iter; ++iter) {
const openvdb::Coord coord = iter.getCoord();
if (leaf2->isValueOn(coord)) { // if the value is active in grid2
float value1 = iter.getValue();
float value2 = leaf2->getValue(coord);
if (value2 != 0.0f) { // no division by zero
leaf1.setValueOn(iter.offset(), value1 / value2); // Divide values
}
else {
leaf1.setValueOff(iter.offset()); // Optionally turn off if divisor is zero
}
}
else {
leaf1.setValueOff(iter.offset()); // turn off if no corresponding value in grid2
}
}
}
else {
// if no corresponding leaf in grid2
for (auto iter = leaf1.beginValueOn(); iter; ++iter) {
leaf1.setValueOff(iter.offset());
}
}
}
}
};
openvdb::FloatGrid::Ptr divideGrids_tbb(openvdb::FloatGrid::Ptr& grid1, openvdb::FloatGrid::Ptr& grid2) {
openvdb::FloatGrid::Ptr resultGrid = openvdb::FloatGrid::create();
resultGrid = grid1->deepCopy();
openvdb::FloatGrid::Accessor resultAccessor = resultGrid->getAccessor();
GridDivider proc(resultGrid->getConstAccessor(), grid2->getConstAccessor());
GridDivider::IterRange range(resultGrid->tree().cbeginLeaf());
tbb::parallel_for(range, proc);
return resultGrid;
}
struct GradientXComputer {
using GridType = openvdb::FloatGrid;
using TreeType = GridType::TreeType;
using LeafNode = TreeType::LeafNodeType;
using IterRange = openvdb::tree::IteratorRange<typename TreeType::LeafCIter>;
GridType::ConstPtr inputGrid; // Using ConstPtr for--- input grid
tbb::enumerable_thread_specific<GridType::Ptr>& threadLocalGrids; // Thread-local grids
//constructor
GradientXComputer(GridType::ConstPtr ig, tbb::enumerable_thread_specific<GridType::Ptr>& tlg)
: inputGrid(ig), threadLocalGrids(tlg) {}
void operator()(IterRange& range) const {
auto& gradXGrid = threadLocalGrids.local();
GridType::Accessor accessor = gradXGrid->getAccessor(); // local accessor __for each thread__ to write data
for (; range; ++range) {
const LeafNode& leaf = *range.iterator();
for (auto iter = leaf.beginValueOn(); iter; ++iter) {
const openvdb::Coord xyz = iter.getCoord();
float valueLeft = inputGrid->tree().isValueOn(xyz.offsetBy(-1, 0, 0)) ?
inputGrid->tree().getValue(xyz.offsetBy(-1, 0, 0)) : iter.getValue();
float valueRight = inputGrid->tree().isValueOn(xyz.offsetBy(1, 0, 0)) ?
inputGrid->tree().getValue(xyz.offsetBy(1, 0, 0)) : iter.getValue();
float gradX = (valueRight - valueLeft) / 2.0f;
accessor.setValueOn(xyz, gradX); // using local accessor--write
}
}
}
};
openvdb::FloatGrid::Ptr calculateGradientX_tbb(const openvdb::FloatGrid::Ptr& grid) {
openvdb::FloatGrid::Ptr gradXGrid = openvdb::FloatGrid::create();
gradXGrid->setTransform(grid->transform().copy());
// create thread-local grids
tbb::enumerable_thread_specific<openvdb::FloatGrid::Ptr> threadLocalGrids([&]() {
auto localGrid = openvdb::FloatGrid::create();
localGrid->setTransform(grid->transform().copy());
return localGrid;
});
GradientXComputer::IterRange range(grid->tree().cbeginLeaf());
GradientXComputer proc(grid, threadLocalGrids);
tbb::parallel_for(range, proc);
// merge the thread-local grids into the final output grid
for (auto& localGrid : threadLocalGrids) {
gradXGrid->tree().merge(localGrid->tree());
}
return gradXGrid;
}
struct GradientYProcessor {
using GridType = openvdb::FloatGrid;
using TreeType = GridType::TreeType;
using LeafNode = TreeType::LeafNodeType;
using IterRange = openvdb::tree::IteratorRange<typename TreeType::LeafCIter>;
GridType::ConstPtr inputGrid; // Using ConstPtr for input grid [using ConstPtr for the input grid to ensure it remains read-only]
tbb::enumerable_thread_specific<GridType::Ptr>& threadLocalGrids; // Thread-local grids
GradientYProcessor(GridType::ConstPtr ig, tbb::enumerable_thread_specific<GridType::Ptr>& tlg)
: inputGrid(ig), threadLocalGrids(tlg) {}
void operator()(IterRange& range) const {
auto& gradYGrid = threadLocalGrids.local();
GridType::Accessor accessor = gradYGrid->getAccessor(); // Local accessor for each thread to write data
for (; range; ++range) {
const LeafNode& leaf = *range.iterator();
for (auto iter = leaf.beginValueOn(); iter; ++iter) {
const openvdb::Coord xyz = iter.getCoord();
float valueDown = inputGrid->tree().isValueOn(xyz.offsetBy(0, -1, 0)) ?
inputGrid->tree().getValue(xyz.offsetBy(0, -1, 0)) : iter.getValue();
float valueUp = inputGrid->tree().isValueOn(xyz.offsetBy(0, 1, 0)) ?
inputGrid->tree().getValue(xyz.offsetBy(0, 1, 0)) : iter.getValue();
float gradY = (valueUp - valueDown) / 2.0f;
accessor.setValueOn(xyz, gradY); // Write using local accessor
}
}
}
};
openvdb::FloatGrid::Ptr calculateGradientY_tbb(const openvdb::FloatGrid::Ptr& grid) {
openvdb::FloatGrid::Ptr gradYGrid = openvdb::FloatGrid::create();
gradYGrid->setTransform(grid->transform().copy());
// Create thread-local grids
tbb::enumerable_thread_specific<openvdb::FloatGrid::Ptr> threadLocalGrids([&]() {
auto localGrid = openvdb::FloatGrid::create();
localGrid->setTransform(grid->transform().copy());
return localGrid;
});
GradientYProcessor::IterRange range(grid->tree().cbeginLeaf());
GradientYProcessor proc(grid, threadLocalGrids);
tbb::parallel_for(range, proc);
// Merge thread-local grids ------- final output grid
for (auto& localGrid : threadLocalGrids) {
gradYGrid->tree().merge(localGrid->tree());
}
return gradYGrid;
}
struct GradientZProcessor {
using GridType = openvdb::FloatGrid;
using TreeType = GridType::TreeType;
using LeafNode = TreeType::LeafNodeType;
using IterRange = openvdb::tree::IteratorRange<typename TreeType::LeafCIter>;
GridType::ConstPtr inputGrid; // Using ConstPtr for input grid
tbb::enumerable_thread_specific<GridType::Ptr>& threadLocalGrids; // Thread-local grids
GradientZProcessor(GridType::ConstPtr ig, tbb::enumerable_thread_specific<GridType::Ptr>& tlg)
: inputGrid(ig), threadLocalGrids(tlg) {}
void operator()(IterRange& range) const {
auto& gradZGrid = threadLocalGrids.local();
GridType::Accessor accessor = gradZGrid->getAccessor(); // Local accessor for each thread to write data
for (; range; ++range) {
const LeafNode& leaf = *range.iterator();
for (auto iter = leaf.beginValueOn(); iter; ++iter) {
const openvdb::Coord xyz = iter.getCoord();
float valueBelow = inputGrid->tree().isValueOn(xyz.offsetBy(0, 0, -1)) ?
inputGrid->tree().getValue(xyz.offsetBy(0, 0, -1)) : iter.getValue();
float valueAbove = inputGrid->tree().isValueOn(xyz.offsetBy(0, 0, 1)) ?
inputGrid->tree().getValue(xyz.offsetBy(0, 0, 1)) : iter.getValue();
float gradZ = (valueAbove - valueBelow) / 2.0f;
accessor.setValueOn(xyz, gradZ); // Write using local accessor
}
}
}
};
openvdb::FloatGrid::Ptr calculateGradientZ_tbb(const openvdb::FloatGrid::Ptr& grid) {
openvdb::FloatGrid::Ptr gradZGrid = openvdb::FloatGrid::create();
gradZGrid->setTransform(grid->transform().copy());
// Create thread-local grids
tbb::enumerable_thread_specific<openvdb::FloatGrid::Ptr> threadLocalGrids([&]() {
auto localGrid = openvdb::FloatGrid::create();
localGrid->setTransform(grid->transform().copy());
return localGrid;
});
GradientZProcessor::IterRange range(grid->tree().cbeginLeaf());
GradientZProcessor proc(grid, threadLocalGrids);
tbb::parallel_for(range, proc);
// Merge
for (auto& localGrid : threadLocalGrids) {
gradZGrid->tree().merge(localGrid->tree());
}
return gradZGrid;
}
struct GradientMagnitudeComputer {
using GridType = openvdb::FloatGrid;
using TreeType = GridType::TreeType;
using LeafNode = TreeType::LeafNodeType;
using IterRange = openvdb::tree::IteratorRange<typename TreeType::LeafCIter>;
GridType::ConstPtr gradXGrid;
GridType::ConstPtr gradYGrid;
GridType::ConstPtr gradZGrid;
tbb::enumerable_thread_specific<GridType::Ptr>& threadLocalGrids;
GradientMagnitudeComputer(GridType::ConstPtr gx, GridType::ConstPtr gy, GridType::ConstPtr gz, tbb::enumerable_thread_specific<GridType::Ptr>& tlg)
: gradXGrid(gx), gradYGrid(gy), gradZGrid(gz), threadLocalGrids(tlg) {}
void operator()(IterRange& range) const {
auto& gradMagGrid = threadLocalGrids.local();
GridType::Accessor accessor = gradMagGrid->getAccessor(); // Local accessor
auto accessorX = gradXGrid->getConstAccessor();
auto accessorY = gradYGrid->getConstAccessor();
auto accessorZ = gradZGrid->getConstAccessor();
for (; range; ++range) {
const LeafNode& leaf = *range.iterator();
for (auto iter = leaf.beginValueOn(); iter; ++iter) {
const openvdb::Coord& xyz = iter.getCoord();
float gradX = accessorX.getValue(xyz);
float gradY = accessorY.getValue(xyz);
float gradZ = accessorZ.getValue(xyz);
float magnitude = openvdb::math::Sqrt(gradX * gradX + gradY * gradY + gradZ * gradZ);
accessor.setValueOn(xyz, magnitude);
}
}
}
};
openvdb::FloatGrid::Ptr calculateGradientMagnitude_tbb(
const openvdb::FloatGrid::Ptr& gradXGrid,
const openvdb::FloatGrid::Ptr& gradYGrid,
const openvdb::FloatGrid::Ptr& gradZGrid) {
openvdb::FloatGrid::Ptr gradMagGrid = openvdb::FloatGrid::create();
gradMagGrid->setTransform(gradXGrid->transform().copy());
gradMagGrid->setGridClass(openvdb::GRID_LEVEL_SET);
// Create thread-local grids
tbb::enumerable_thread_specific<openvdb::FloatGrid::Ptr> threadLocalGrids([&]() {
auto localGrid = openvdb::FloatGrid::create();
localGrid->setTransform(gradXGrid->transform().copy());
return localGrid;
});
GradientMagnitudeComputer::IterRange range(gradXGrid->tree().cbeginLeaf());
GradientMagnitudeComputer proc(gradXGrid, gradYGrid, gradZGrid, threadLocalGrids);
tbb::parallel_for(range, proc);
// Merge
for (auto& localGrid : threadLocalGrids) {
gradMagGrid->tree().merge(localGrid->tree());
}
return gradMagGrid;
}
openvdb::FloatGrid::Ptr dist3D(openvdb::FloatGrid::Ptr phi, openvdb::FloatGrid::Ptr binary_boundary) {
// Initialize grid dimensions
const openvdb::CoordBBox bbox = phi->evalActiveVoxelBoundingBox();
int width = bbox.dim().x();
int height = bbox.dim().y();
int length = bbox.dim().z();
// Create binary boundary grid
binary_boundary->setTransform(phi->transform().copy());
// Neighbor offsets
std::vector<openvdb::Coord> neighbors = {
openvdb::Coord(0, 0, 1),
openvdb::Coord(0, 1, 0),
openvdb::Coord(1, 0, 0),
openvdb::Coord(-1, 0, 0),
openvdb::Coord(0, -1, 0),
openvdb::Coord(0, 0, -1)
};
openvdb::FloatGrid::Accessor phiAccessor = phi->getAccessor();
openvdb::FloatGrid::Accessor binaryAccessor = binary_boundary->getAccessor();
// Identifying boundary cells using tiles
for (auto leafIter = phi->tree().cbeginLeaf(); leafIter; ++leafIter) {
for (auto iter = leafIter->cbeginValueOn(); iter; ++iter) {
const openvdb::Coord& xyz = iter.getCoord();
for (const auto& neighbor : neighbors) {
openvdb::Coord nxyz = xyz + neighbor;
if (phiAccessor.isValueOn(nxyz) && (iter.getValue() * phiAccessor.getValue(nxyz) <= 0)) {
binaryAccessor.setValueOn(xyz);
binaryAccessor.setValueOn(nxyz);
}
}
}
}
// Create distance grid and initialize with large values
openvdb::FloatGrid::Ptr dist = openvdb::FloatGrid::create(35.0f);
dist->setTransform(phi->transform().copy());
openvdb::FloatGrid::Accessor distAccessor = dist->getAccessor();
// Calculate distance for all boundary cells to the contour using tiles
for (auto leafIter = binary_boundary->tree().cbeginLeaf(); leafIter; ++leafIter) {
for (auto iter = leafIter->cbeginValueOn(); iter; ++iter) {
const openvdb::Coord& xyz = iter.getCoord();
for (const auto& neighbor : neighbors) {
openvdb::Coord nxyz = neighbor + xyz;
if (binaryAccessor.isValueOn(nxyz)) {
float da = std::abs(phiAccessor.getValue(xyz)) / std::abs(phiAccessor.getValue(nxyz) - phiAccessor.getValue(xyz));
float db = std::abs(phiAccessor.getValue(nxyz)) / std::abs(phiAccessor.getValue(nxyz) - phiAccessor.getValue(xyz));
distAccessor.setValue(xyz, std::min(distAccessor.getValue(xyz), da));
distAccessor.setValue(nxyz, std::min(distAccessor.getValue(nxyz), db));
}
}
}
}
// Fast sweeping algorithm
const int NSweeps = 4;
const int dirX[NSweeps][3] = { {0, width - 1, 1}, {width - 1, 0, -1}, {width - 1, 0, -1}, {0, width - 1, 1} };
const int dirY[NSweeps][3] = { {0, height - 1, 1}, {0, height - 1, 1}, {height - 1, 0, -1}, {height - 1, 0, -1} };
const int dirZ[NSweeps][3] = { {0, length - 1, 1}, {0, length - 1, 1}, {0, length - 1, 1}, {0, length - 1, 1} };
const double h = 1.0, f = 1.0, eps = 1e-6;
for (int s = 0; s < NSweeps; s++) {
for (int iy = dirY[s][0]; dirY[s][2] * iy <= dirY[s][1]; iy += dirY[s][2]) {
for (int ix = dirX[s][0]; dirX[s][2] * ix <= dirX[s][1]; ix += dirX[s][2]) {
for (int iz = dirZ[s][0]; dirZ[s][2] * iz <= dirZ[s][1]; iz += dirZ[s][2]) {
openvdb::Coord xyz(ix, iy, iz);
if (phi->tree().isValueOn(xyz)) {
if (!binaryAccessor.isValueOn(xyz)) {
double aa[3], tmp;
if (iy == 0) aa[1] = distAccessor.getValue(openvdb::Coord(ix, iy + 1, iz));
else if (iy == height - 1) aa[1] = distAccessor.getValue(openvdb::Coord(ix, iy - 1, iz));
else aa[1] = std::min(distAccessor.getValue(openvdb::Coord(ix, iy - 1, iz)),
distAccessor.getValue(openvdb::Coord(ix, iy + 1, iz)));
if (ix == 0) aa[0] = distAccessor.getValue(openvdb::Coord(ix + 1, iy, iz));
else if (ix == width - 1) aa[0] = distAccessor.getValue(openvdb::Coord(ix - 1, iy, iz));
else aa[0] = std::min(distAccessor.getValue(openvdb::Coord(ix - 1, iy, iz)),
distAccessor.getValue(openvdb::Coord(ix + 1, iy, iz)));
if (iz == 0) aa[2] = distAccessor.getValue(openvdb::Coord(ix, iy, iz + 1));
else if (iz == length - 1) aa[2] = distAccessor.getValue(openvdb::Coord(ix, iy, iz - 1));
else aa[2] = std::min(distAccessor.getValue(openvdb::Coord(ix, iy, iz - 1)),
distAccessor.getValue(openvdb::Coord(ix, iy, iz + 1)));
// Simple bubble sort
if (aa[0] > aa[1]) { tmp = aa[0]; aa[0] = aa[1]; aa[1] = tmp; }
if (aa[1] > aa[2]) { tmp = aa[1]; aa[1] = aa[2]; aa[2] = tmp; }
if (aa[0] > aa[1]) { tmp = aa[0]; aa[0] = aa[1]; aa[1] = tmp; }
double d_curr = aa[0] + h * f;
double d_new;
if (d_curr <= (aa[1] + eps)) {
d_new = d_curr;
}
else {
double a = 2.0, b = -2.0 * (aa[0] + aa[1]), c = aa[0] * aa[0] + aa[1] * aa[1] - h * h * f * f;
double D = sqrt(b * b - 4.0 * a * c);
d_curr = ((-b + D) > (-b - D) ? (-b + D) : (-b - D)) / (2.0 * a);
if (d_curr <= (aa[2] + eps)) {
d_new = d_curr;
}
else {
a = 3.0;
b = -2.0 * (aa[0] + aa[1] + aa[2]);
c = aa[0] * aa[0] + aa[1] * aa[1] + aa[2] * aa[2] - h * h * f * f;
D = sqrt(b * b - 4.0 * a * c);
d_new = ((-b + D) > (-b - D) ? (-b + D) : (-b - D)) / (2.0 * a);
}
}
distAccessor.setValue(xyz, std::min(distAccessor.getValue(xyz), float(d_new)));
}
}
}
}
}
}
return dist;
}
openvdb::FloatGrid::Ptr dist2sdf3D(openvdb::FloatGrid::Ptr distance, openvdb::FloatGrid::Ptr binary_boundary) {
// Initialize grid dimensions
const openvdb::CoordBBox bbox = distance->evalActiveVoxelBoundingBox();
int width = bbox.dim().x();
int height = bbox.dim().y();
int length = bbox.dim().z();
std::cout << "start_sdf" << "\n";
// Initialize SDF and frozen cells grids
auto SDF = openvdb::FloatGrid::create(std::numeric_limits<float>::max());
auto frozenCells = openvdb::Int32Grid::create(0);
SDF->setTransform(distance->transform().copy());
frozenCells->setTransform(distance->transform().copy());
// Copy distance values to SDF grid
for (auto iter = distance->cbeginValueOn(); iter; ++iter) {
const openvdb::Coord& xyz = iter.getCoord();
SDF->tree().setValue(xyz, iter.getValue());
}
std::cout << "start_sdf(2)" << "\n";
// Fill the frozenCells grid for active voxels in binary_boundary
for (auto iter = binary_boundary->cbeginValueOn(); iter; ++iter) {
const openvdb::Coord& xyz = iter.getCoord();
if (distance->tree().isValueOn(xyz)) { // Check if the voxel is active in the distance grid
frozenCells->tree().setValue(xyz, 1);
}
}
// Turn the whole input distance field to negative
for (auto iter = distance->cbeginValueOn(); iter; ++iter) {
const openvdb::Coord& xyz = iter.getCoord();
float value = iter.getValue();
SDF->tree().setValue(xyz, -value);
}
std::cout << "start_sdf(3)" << "\n";
std::queue<openvdb::Coord> queue;
// Initialize the queue with all active voxels in distance that are not frozen
for (auto iter = distance->cbeginValueOn(); iter; ++iter) {
const openvdb::Coord& xyz = iter.getCoord();
if (frozenCells->tree().getValue(xyz) == 0) {
queue.push(xyz);
}
}