-
Notifications
You must be signed in to change notification settings - Fork 628
/
VolumeToMesh.h
5262 lines (4026 loc) · 175 KB
/
VolumeToMesh.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright Contributors to the OpenVDB Project
// SPDX-License-Identifier: MPL-2.0
/// @file VolumeToMesh.h
///
/// @brief Extract polygonal surfaces from scalar volumes.
///
/// @author Mihai Alden
#ifndef OPENVDB_TOOLS_VOLUME_TO_MESH_HAS_BEEN_INCLUDED
#define OPENVDB_TOOLS_VOLUME_TO_MESH_HAS_BEEN_INCLUDED
#include <openvdb/Platform.h>
#include <openvdb/math/Operators.h> // for ISGradient
#include <openvdb/tree/ValueAccessor.h>
#include <openvdb/util/Util.h> // for INVALID_IDX
#include <tbb/blocked_range.h>
#include <tbb/parallel_for.h>
#include <tbb/parallel_reduce.h>
#include <tbb/task_scheduler_init.h>
#include <cmath> // for std::isfinite()
#include <map>
#include <memory>
#include <set>
#include <type_traits>
#include <vector>
namespace openvdb {
OPENVDB_USE_VERSION_NAMESPACE
namespace OPENVDB_VERSION_NAME {
namespace tools {
////////////////////////////////////////
// Wrapper functions for the VolumeToMesh converter
/// @brief Uniformly mesh any scalar grid that has a continuous isosurface.
///
/// @param grid a scalar grid to mesh
/// @param points output list of world space points
/// @param quads output quad index list
/// @param isovalue determines which isosurface to mesh
///
/// @throw TypeError if @a grid does not have a scalar value type
template<typename GridType>
inline void
volumeToMesh(
const GridType& grid,
std::vector<Vec3s>& points,
std::vector<Vec4I>& quads,
double isovalue = 0.0);
/// @brief Adaptively mesh any scalar grid that has a continuous isosurface.
///
/// @param grid a scalar grid to mesh
/// @param points output list of world space points
/// @param triangles output triangle index list
/// @param quads output quad index list
/// @param isovalue determines which isosurface to mesh
/// @param adaptivity surface adaptivity threshold [0 to 1]
/// @param relaxDisorientedTriangles toggle relaxing disoriented triangles during
/// adaptive meshing.
///
/// @throw TypeError if @a grid does not have a scalar value type
template<typename GridType>
inline void
volumeToMesh(
const GridType& grid,
std::vector<Vec3s>& points,
std::vector<Vec3I>& triangles,
std::vector<Vec4I>& quads,
double isovalue = 0.0,
double adaptivity = 0.0,
bool relaxDisorientedTriangles = true);
////////////////////////////////////////
/// @brief Polygon flags, used for reference based meshing.
enum { POLYFLAG_EXTERIOR = 0x1, POLYFLAG_FRACTURE_SEAM = 0x2, POLYFLAG_SUBDIVIDED = 0x4 };
/// @brief Collection of quads and triangles
class PolygonPool
{
public:
inline PolygonPool();
inline PolygonPool(const size_t numQuads, const size_t numTriangles);
inline void copy(const PolygonPool& rhs);
inline void resetQuads(size_t size);
inline void clearQuads();
inline void resetTriangles(size_t size);
inline void clearTriangles();
// polygon accessor methods
const size_t& numQuads() const { return mNumQuads; }
openvdb::Vec4I& quad(size_t n) { return mQuads[n]; }
const openvdb::Vec4I& quad(size_t n) const { return mQuads[n]; }
const size_t& numTriangles() const { return mNumTriangles; }
openvdb::Vec3I& triangle(size_t n) { return mTriangles[n]; }
const openvdb::Vec3I& triangle(size_t n) const { return mTriangles[n]; }
// polygon flags accessor methods
char& quadFlags(size_t n) { return mQuadFlags[n]; }
const char& quadFlags(size_t n) const { return mQuadFlags[n]; }
char& triangleFlags(size_t n) { return mTriangleFlags[n]; }
const char& triangleFlags(size_t n) const { return mTriangleFlags[n]; }
// reduce the polygon containers, n has to
// be smaller than the current container size.
inline bool trimQuads(const size_t n, bool reallocate = false);
inline bool trimTrinagles(const size_t n, bool reallocate = false);
private:
// disallow copy by assignment
void operator=(const PolygonPool&) {}
size_t mNumQuads, mNumTriangles;
std::unique_ptr<openvdb::Vec4I[]> mQuads;
std::unique_ptr<openvdb::Vec3I[]> mTriangles;
std::unique_ptr<char[]> mQuadFlags, mTriangleFlags;
};
/// @{
/// @brief Point and primitive list types.
using PointList = std::unique_ptr<openvdb::Vec3s[]>;
using PolygonPoolList = std::unique_ptr<PolygonPool[]>;
/// @}
////////////////////////////////////////
/// @brief Mesh any scalar grid that has a continuous isosurface.
struct VolumeToMesh
{
/// @param isovalue Determines which isosurface to mesh.
/// @param adaptivity Adaptivity threshold [0 to 1]
/// @param relaxDisorientedTriangles Toggle relaxing disoriented triangles during
/// adaptive meshing.
VolumeToMesh(double isovalue = 0, double adaptivity = 0, bool relaxDisorientedTriangles = true);
//////////
/// @{
// Mesh data accessors
size_t pointListSize() const { return mPointListSize; }
PointList& pointList() { return mPoints; }
const PointList& pointList() const { return mPoints; }
size_t polygonPoolListSize() const { return mPolygonPoolListSize; }
PolygonPoolList& polygonPoolList() { return mPolygons; }
const PolygonPoolList& polygonPoolList() const { return mPolygons; }
std::vector<uint8_t>& pointFlags() { return mPointFlags; }
const std::vector<uint8_t>& pointFlags() const { return mPointFlags; }
/// @}
//////////
/// @brief Main call
/// @note Call with scalar typed grid.
template<typename InputGridType>
void operator()(const InputGridType&);
//////////
/// @brief When surfacing fractured SDF fragments, the original unfractured
/// SDF grid can be used to eliminate seam lines and tag polygons that are
/// coincident with the reference surface with the @c POLYFLAG_EXTERIOR
/// flag and polygons that are in proximity to the seam lines with the
/// @c POLYFLAG_FRACTURE_SEAM flag. (The performance cost for using this
/// reference based scheme compared to the regular meshing scheme is
/// approximately 15% for the first fragment and neglect-able for
/// subsequent fragments.)
///
/// @note Attributes from the original asset such as uv coordinates, normals etc.
/// are typically transfered to polygons that are marked with the
/// @c POLYFLAG_EXTERIOR flag. Polygons that are not marked with this flag
/// are interior to reference surface and might need projected UV coordinates
/// or a different material. Polygons marked as @c POLYFLAG_FRACTURE_SEAM can
/// be used to drive secondary elements such as debris and dust in a FX pipeline.
///
/// @param grid reference surface grid of @c GridT type.
/// @param secAdaptivity Secondary adaptivity threshold [0 to 1]. Used in regions
/// that do not exist in the reference grid. (Parts of the
/// fragment surface that are not coincident with the
/// reference surface.)
void setRefGrid(const GridBase::ConstPtr& grid, double secAdaptivity = 0);
/// @param mask A boolean grid whose active topology defines the region to mesh.
/// @param invertMask Toggle to mesh the complement of the mask.
/// @note The mask's tree configuration has to match @c GridT's tree configuration.
void setSurfaceMask(const GridBase::ConstPtr& mask, bool invertMask = false);
/// @param grid A scalar grid used as a spatial multiplier for the adaptivity threshold.
/// @note The grid's tree configuration has to match @c GridT's tree configuration.
void setSpatialAdaptivity(const GridBase::ConstPtr& grid);
/// @param tree A boolean tree whose active topology defines the adaptivity mask.
/// @note The tree configuration has to match @c GridT's tree configuration.
void setAdaptivityMask(const TreeBase::ConstPtr& tree);
private:
// Disallow copying
VolumeToMesh(const VolumeToMesh&);
VolumeToMesh& operator=(const VolumeToMesh&);
PointList mPoints;
PolygonPoolList mPolygons;
size_t mPointListSize, mSeamPointListSize, mPolygonPoolListSize;
double mIsovalue, mPrimAdaptivity, mSecAdaptivity;
GridBase::ConstPtr mRefGrid, mSurfaceMaskGrid, mAdaptivityGrid;
TreeBase::ConstPtr mAdaptivityMaskTree;
TreeBase::Ptr mRefSignTree, mRefIdxTree;
bool mInvertSurfaceMask, mRelaxDisorientedTriangles;
std::unique_ptr<uint32_t[]> mQuantizedSeamPoints;
std::vector<uint8_t> mPointFlags;
}; // struct VolumeToMesh
////////////////////////////////////////
/// @brief Given a set of tangent elements, @c points with corresponding @c normals,
/// this method returns the intersection point of all tangent elements.
///
/// @note Used to extract surfaces with sharp edges and corners from volume data,
/// see the following paper for details: "Feature Sensitive Surface
/// Extraction from Volume Data, Kobbelt et al. 2001".
inline Vec3d findFeaturePoint(
const std::vector<Vec3d>& points,
const std::vector<Vec3d>& normals)
{
using Mat3d = math::Mat3d;
Vec3d avgPos(0.0);
if (points.empty()) return avgPos;
for (size_t n = 0, N = points.size(); n < N; ++n) {
avgPos += points[n];
}
avgPos /= double(points.size());
// Unique components of the 3x3 A^TA matrix, where A is
// the matrix of normals.
double m00=0,m01=0,m02=0,
m11=0,m12=0,
m22=0;
// The rhs vector, A^Tb, where b = n dot p
Vec3d rhs(0.0);
for (size_t n = 0, N = points.size(); n < N; ++n) {
const Vec3d& n_ref = normals[n];
// A^TA
m00 += n_ref[0] * n_ref[0]; // diagonal
m11 += n_ref[1] * n_ref[1];
m22 += n_ref[2] * n_ref[2];
m01 += n_ref[0] * n_ref[1]; // Upper-tri
m02 += n_ref[0] * n_ref[2];
m12 += n_ref[1] * n_ref[2];
// A^Tb (centered around the origin)
rhs += n_ref * n_ref.dot(points[n] - avgPos);
}
Mat3d A(m00,m01,m02,
m01,m11,m12,
m02,m12,m22);
/*
// Inverse
const double det = A.det();
if (det > 0.01) {
Mat3d A_inv = A.adjoint();
A_inv *= (1.0 / det);
return avgPos + A_inv * rhs;
}
*/
// Compute the pseudo inverse
math::Mat3d eigenVectors;
Vec3d eigenValues;
diagonalizeSymmetricMatrix(A, eigenVectors, eigenValues, 300);
Mat3d D = Mat3d::identity();
double tolerance = std::max(std::abs(eigenValues[0]), std::abs(eigenValues[1]));
tolerance = std::max(tolerance, std::abs(eigenValues[2]));
tolerance *= 0.01;
int clamped = 0;
for (int i = 0; i < 3; ++i ) {
if (std::abs(eigenValues[i]) < tolerance) {
D[i][i] = 0.0;
++clamped;
} else {
D[i][i] = 1.0 / eigenValues[i];
}
}
// Assemble the pseudo inverse and calc. the intersection point
if (clamped < 3) {
Mat3d pseudoInv = eigenVectors * D * eigenVectors.transpose();
return avgPos + pseudoInv * rhs;
}
return avgPos;
}
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
// Internal utility objects and implementation details
namespace volume_to_mesh_internal {
template<typename ValueType>
struct FillArray
{
FillArray(ValueType* array, const ValueType& v) : mArray(array), mValue(v) { }
void operator()(const tbb::blocked_range<size_t>& range) const {
const ValueType v = mValue;
for (size_t n = range.begin(), N = range.end(); n < N; ++n) {
mArray[n] = v;
}
}
ValueType * const mArray;
const ValueType mValue;
};
template<typename ValueType>
inline void
fillArray(ValueType* array, const ValueType& val, const size_t length)
{
const auto grainSize = std::max<size_t>(
length / tbb::task_scheduler_init::default_num_threads(), 1024);
const tbb::blocked_range<size_t> range(0, length, grainSize);
tbb::parallel_for(range, FillArray<ValueType>(array, val), tbb::simple_partitioner());
}
/// @brief Bit-flags used to classify cells.
enum { SIGNS = 0xFF, EDGES = 0xE00, INSIDE = 0x100,
XEDGE = 0x200, YEDGE = 0x400, ZEDGE = 0x800, SEAM = 0x1000};
/// @brief Used to quickly determine if a given cell is adaptable.
const bool sAdaptable[256] = {
1,1,1,1,1,0,1,1,1,1,0,1,1,1,1,1,1,1,0,1,0,0,0,1,0,1,0,1,0,1,0,1,
1,0,1,1,0,0,1,1,0,0,0,1,0,0,1,1,1,1,1,1,0,0,1,1,0,1,0,1,0,0,0,1,
1,0,0,0,1,0,1,1,0,0,0,0,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
1,0,1,1,1,0,1,1,0,0,0,0,1,0,1,1,1,1,1,1,1,0,1,1,0,0,0,0,0,0,0,1,
1,0,0,0,0,0,0,0,1,1,0,1,1,1,1,1,1,1,0,1,0,0,0,0,1,1,0,1,1,1,0,1,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,0,0,0,0,1,1,0,1,0,0,0,1,
1,0,0,0,1,0,1,0,1,1,0,0,1,1,1,1,1,1,0,0,1,0,0,0,1,1,0,0,1,1,0,1,
1,0,1,0,1,0,1,0,1,0,0,0,1,0,1,1,1,1,1,1,1,0,1,1,1,1,0,1,1,1,1,1};
/// @brief Contains the ambiguous face index for certain cell configuration.
const unsigned char sAmbiguousFace[256] = {
0,0,0,0,0,5,0,0,0,0,5,0,0,0,0,0,0,0,1,0,0,5,1,0,4,0,0,0,4,0,0,0,
0,1,0,0,2,0,0,0,0,1,5,0,2,0,0,0,0,0,0,0,2,0,0,0,4,0,0,0,0,0,0,0,
0,0,2,2,0,5,0,0,3,3,0,0,0,0,0,0,6,6,0,0,6,0,0,0,0,0,0,0,0,0,0,0,
0,1,0,0,0,0,0,0,3,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,4,0,4,3,0,3,0,0,0,5,0,0,0,0,0,0,0,1,0,3,0,0,0,0,0,0,0,0,0,0,0,
6,0,6,0,0,0,0,0,6,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,4,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
/// @brief Lookup table for different cell sign configurations. The first entry specifies
/// the total number of points that need to be generated inside a cell and the
/// remaining 12 entries indicate different edge groups.
const unsigned char sEdgeGroupTable[256][13] = {
{0,0,0,0,0,0,0,0,0,0,0,0,0},{1,1,0,0,1,0,0,0,0,1,0,0,0},{1,1,1,0,0,0,0,0,0,0,1,0,0},
{1,0,1,0,1,0,0,0,0,1,1,0,0},{1,0,1,1,0,0,0,0,0,0,0,1,0},{1,1,1,1,1,0,0,0,0,1,0,1,0},
{1,1,0,1,0,0,0,0,0,0,1,1,0},{1,0,0,1,1,0,0,0,0,1,1,1,0},{1,0,0,1,1,0,0,0,0,0,0,0,1},
{1,1,0,1,0,0,0,0,0,1,0,0,1},{1,1,1,1,1,0,0,0,0,0,1,0,1},{1,0,1,1,0,0,0,0,0,1,1,0,1},
{1,0,1,0,1,0,0,0,0,0,0,1,1},{1,1,1,0,0,0,0,0,0,1,0,1,1},{1,1,0,0,1,0,0,0,0,0,1,1,1},
{1,0,0,0,0,0,0,0,0,1,1,1,1},{1,0,0,0,0,1,0,0,1,1,0,0,0},{1,1,0,0,1,1,0,0,1,0,0,0,0},
{1,1,1,0,0,1,0,0,1,1,1,0,0},{1,0,1,0,1,1,0,0,1,0,1,0,0},{2,0,1,1,0,2,0,0,2,2,0,1,0},
{1,1,1,1,1,1,0,0,1,0,0,1,0},{1,1,0,1,0,1,0,0,1,1,1,1,0},{1,0,0,1,1,1,0,0,1,0,1,1,0},
{1,0,0,1,1,1,0,0,1,1,0,0,1},{1,1,0,1,0,1,0,0,1,0,0,0,1},{2,2,1,1,2,1,0,0,1,2,1,0,1},
{1,0,1,1,0,1,0,0,1,0,1,0,1},{1,0,1,0,1,1,0,0,1,1,0,1,1},{1,1,1,0,0,1,0,0,1,0,0,1,1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,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0}};
////////////////////////////////////////
inline bool
isPlanarQuad(
const Vec3d& p0, const Vec3d& p1,
const Vec3d& p2, const Vec3d& p3,
double epsilon = 0.001)
{
// compute representative plane
Vec3d normal = (p2-p0).cross(p1-p3);
normal.normalize();
const Vec3d centroid = (p0 + p1 + p2 + p3);
const double d = centroid.dot(normal) * 0.25;
// test vertice distance to plane
double absDist = std::abs(p0.dot(normal) - d);
if (absDist > epsilon) return false;
absDist = std::abs(p1.dot(normal) - d);
if (absDist > epsilon) return false;
absDist = std::abs(p2.dot(normal) - d);
if (absDist > epsilon) return false;
absDist = std::abs(p3.dot(normal) - d);
if (absDist > epsilon) return false;
return true;
}
////////////////////////////////////////
/// @{
/// @brief Utility methods for point quantization.
enum {
MASK_FIRST_10_BITS = 0x000003FF,
MASK_DIRTY_BIT = 0x80000000,
MASK_INVALID_BIT = 0x40000000
};
inline uint32_t
packPoint(const Vec3d& v)
{
uint32_t data = 0;
// values are expected to be in the [0.0 to 1.0] range.
assert(!(v.x() > 1.0) && !(v.y() > 1.0) && !(v.z() > 1.0));
assert(!(v.x() < 0.0) && !(v.y() < 0.0) && !(v.z() < 0.0));
data |= (uint32_t(v.x() * 1023.0) & MASK_FIRST_10_BITS) << 20;
data |= (uint32_t(v.y() * 1023.0) & MASK_FIRST_10_BITS) << 10;
data |= (uint32_t(v.z() * 1023.0) & MASK_FIRST_10_BITS);
return data;
}
inline Vec3d
unpackPoint(uint32_t data)
{
Vec3d v;
v.z() = double(data & MASK_FIRST_10_BITS) * 0.0009775171;
data = data >> 10;
v.y() = double(data & MASK_FIRST_10_BITS) * 0.0009775171;
data = data >> 10;
v.x() = double(data & MASK_FIRST_10_BITS) * 0.0009775171;
return v;
}
/// @}
////////////////////////////////////////
template<typename T>
inline bool isBoolValue() { return false; }
template<>
inline bool isBoolValue<bool>() { return true; }
template<typename T>
inline bool isInsideValue(T value, T isovalue) { return value < isovalue; }
template<>
inline bool isInsideValue<bool>(bool value, bool /*isovalue*/) { return value; }
template<typename AccessorT>
inline void
getCellVertexValues(const AccessorT& accessor, Coord ijk,
math::Tuple<8, typename AccessorT::ValueType>& values)
{
values[0] = accessor.getValue(ijk); // i, j, k
++ijk[0];
values[1] = accessor.getValue(ijk); // i+1, j, k
++ijk[2];
values[2] = accessor.getValue(ijk); // i+1, j, k+1
--ijk[0];
values[3] = accessor.getValue(ijk); // i, j, k+1
--ijk[2]; ++ijk[1];
values[4] = accessor.getValue(ijk); // i, j+1, k
++ijk[0];
values[5] = accessor.getValue(ijk); // i+1, j+1, k
++ijk[2];
values[6] = accessor.getValue(ijk); // i+1, j+1, k+1
--ijk[0];
values[7] = accessor.getValue(ijk); // i, j+1, k+1
}
template<typename LeafT>
inline void
getCellVertexValues(const LeafT& leaf, const Index offset,
math::Tuple<8, typename LeafT::ValueType>& values)
{
values[0] = leaf.getValue(offset); // i, j, k
values[3] = leaf.getValue(offset + 1); // i, j, k+1
values[4] = leaf.getValue(offset + LeafT::DIM); // i, j+1, k
values[7] = leaf.getValue(offset + LeafT::DIM + 1); // i, j+1, k+1
values[1] = leaf.getValue(offset + (LeafT::DIM * LeafT::DIM)); // i+1, j, k
values[2] = leaf.getValue(offset + (LeafT::DIM * LeafT::DIM) + 1); // i+1, j, k+1
values[5] = leaf.getValue(offset + (LeafT::DIM * LeafT::DIM) + LeafT::DIM); // i+1, j+1, k
values[6] = leaf.getValue(offset + (LeafT::DIM * LeafT::DIM) + LeafT::DIM + 1); // i+1, j+1, k+1
}
template<typename ValueType>
inline uint8_t
computeSignFlags(const math::Tuple<8, ValueType>& values, const ValueType iso)
{
unsigned signs = 0;
signs |= isInsideValue(values[0], iso) ? 1u : 0u;
signs |= isInsideValue(values[1], iso) ? 2u : 0u;
signs |= isInsideValue(values[2], iso) ? 4u : 0u;
signs |= isInsideValue(values[3], iso) ? 8u : 0u;
signs |= isInsideValue(values[4], iso) ? 16u : 0u;
signs |= isInsideValue(values[5], iso) ? 32u : 0u;
signs |= isInsideValue(values[6], iso) ? 64u : 0u;
signs |= isInsideValue(values[7], iso) ? 128u : 0u;
return uint8_t(signs);
}
/// @brief General method that computes the cell-sign configuration at the given
/// @c ijk coordinate.
template<typename AccessorT>
inline uint8_t
evalCellSigns(const AccessorT& accessor, const Coord& ijk, typename AccessorT::ValueType iso)
{
unsigned signs = 0;
Coord coord = ijk; // i, j, k
if (isInsideValue(accessor.getValue(coord), iso)) signs |= 1u;
coord[0] += 1; // i+1, j, k
if (isInsideValue(accessor.getValue(coord), iso)) signs |= 2u;
coord[2] += 1; // i+1, j, k+1
if (isInsideValue(accessor.getValue(coord), iso)) signs |= 4u;
coord[0] = ijk[0]; // i, j, k+1
if (isInsideValue(accessor.getValue(coord), iso)) signs |= 8u;
coord[1] += 1; coord[2] = ijk[2]; // i, j+1, k
if (isInsideValue(accessor.getValue(coord), iso)) signs |= 16u;
coord[0] += 1; // i+1, j+1, k
if (isInsideValue(accessor.getValue(coord), iso)) signs |= 32u;
coord[2] += 1; // i+1, j+1, k+1
if (isInsideValue(accessor.getValue(coord), iso)) signs |= 64u;
coord[0] = ijk[0]; // i, j+1, k+1
if (isInsideValue(accessor.getValue(coord), iso)) signs |= 128u;
return uint8_t(signs);
}
/// @brief Leaf node optimized method that computes the cell-sign configuration
/// at the given local @c offset
template<typename LeafT>
inline uint8_t
evalCellSigns(const LeafT& leaf, const Index offset, typename LeafT::ValueType iso)
{
unsigned signs = 0;
// i, j, k
if (isInsideValue(leaf.getValue(offset), iso)) signs |= 1u;
// i, j, k+1
if (isInsideValue(leaf.getValue(offset + 1), iso)) signs |= 8u;
// i, j+1, k
if (isInsideValue(leaf.getValue(offset + LeafT::DIM), iso)) signs |= 16u;
// i, j+1, k+1
if (isInsideValue(leaf.getValue(offset + LeafT::DIM + 1), iso)) signs |= 128u;
// i+1, j, k
if (isInsideValue(leaf.getValue(offset + (LeafT::DIM * LeafT::DIM) ), iso)) signs |= 2u;
// i+1, j, k+1
if (isInsideValue(leaf.getValue(offset + (LeafT::DIM * LeafT::DIM) + 1), iso)) signs |= 4u;
// i+1, j+1, k
if (isInsideValue(leaf.getValue(offset + (LeafT::DIM * LeafT::DIM) + LeafT::DIM), iso)) signs |= 32u;
// i+1, j+1, k+1
if (isInsideValue(leaf.getValue(offset + (LeafT::DIM * LeafT::DIM) + LeafT::DIM + 1), iso)) signs |= 64u;
return uint8_t(signs);
}
/// @brief Used to correct topological ambiguities related to two adjacent cells
/// that share an ambiguous face.
template<class AccessorT>
inline void
correctCellSigns(uint8_t& signs, uint8_t face,
const AccessorT& acc, Coord ijk, typename AccessorT::ValueType iso)
{
switch (int(face)) {
case 1:
ijk[2] -= 1;
if (sAmbiguousFace[evalCellSigns(acc, ijk, iso)] == 3) signs = uint8_t(~signs);
break;
case 2:
ijk[0] += 1;
if (sAmbiguousFace[evalCellSigns(acc, ijk, iso)] == 4) signs = uint8_t(~signs);
break;
case 3:
ijk[2] += 1;
if (sAmbiguousFace[evalCellSigns(acc, ijk, iso)] == 1) signs = uint8_t(~signs);
break;
case 4:
ijk[0] -= 1;
if (sAmbiguousFace[evalCellSigns(acc, ijk, iso)] == 2) signs = uint8_t(~signs);
break;
case 5:
ijk[1] -= 1;
if (sAmbiguousFace[evalCellSigns(acc, ijk, iso)] == 6) signs = uint8_t(~signs);
break;
case 6:
ijk[1] += 1;
if (sAmbiguousFace[evalCellSigns(acc, ijk, iso)] == 5) signs = uint8_t(~signs);
break;
default:
break;
}
}
template<class AccessorT>
inline bool
isNonManifold(const AccessorT& accessor, const Coord& ijk,
typename AccessorT::ValueType isovalue, const int dim)
{
int hDim = dim >> 1;
bool m, p[8]; // Corner signs
Coord coord = ijk; // i, j, k
p[0] = isInsideValue(accessor.getValue(coord), isovalue);
coord[0] += dim; // i+dim, j, k
p[1] = isInsideValue(accessor.getValue(coord), isovalue);
coord[2] += dim; // i+dim, j, k+dim
p[2] = isInsideValue(accessor.getValue(coord), isovalue);
coord[0] = ijk[0]; // i, j, k+dim
p[3] = isInsideValue(accessor.getValue(coord), isovalue);
coord[1] += dim; coord[2] = ijk[2]; // i, j+dim, k
p[4] = isInsideValue(accessor.getValue(coord), isovalue);
coord[0] += dim; // i+dim, j+dim, k
p[5] = isInsideValue(accessor.getValue(coord), isovalue);
coord[2] += dim; // i+dim, j+dim, k+dim
p[6] = isInsideValue(accessor.getValue(coord), isovalue);
coord[0] = ijk[0]; // i, j+dim, k+dim
p[7] = isInsideValue(accessor.getValue(coord), isovalue);
// Check if the corner sign configuration is ambiguous
unsigned signs = 0;
if (p[0]) signs |= 1u;
if (p[1]) signs |= 2u;
if (p[2]) signs |= 4u;
if (p[3]) signs |= 8u;
if (p[4]) signs |= 16u;
if (p[5]) signs |= 32u;
if (p[6]) signs |= 64u;
if (p[7]) signs |= 128u;
if (!sAdaptable[signs]) return true;
// Manifold check
// Evaluate edges
int i = ijk[0], ip = ijk[0] + hDim, ipp = ijk[0] + dim;
int j = ijk[1], jp = ijk[1] + hDim, jpp = ijk[1] + dim;
int k = ijk[2], kp = ijk[2] + hDim, kpp = ijk[2] + dim;
// edge 1
coord.reset(ip, j, k);
m = isInsideValue(accessor.getValue(coord), isovalue);
if (p[0] != m && p[1] != m) return true;
// edge 2
coord.reset(ipp, j, kp);
m = isInsideValue(accessor.getValue(coord), isovalue);
if (p[1] != m && p[2] != m) return true;
// edge 3
coord.reset(ip, j, kpp);
m = isInsideValue(accessor.getValue(coord), isovalue);
if (p[2] != m && p[3] != m) return true;
// edge 4
coord.reset(i, j, kp);
m = isInsideValue(accessor.getValue(coord), isovalue);
if (p[0] != m && p[3] != m) return true;
// edge 5
coord.reset(ip, jpp, k);
m = isInsideValue(accessor.getValue(coord), isovalue);
if (p[4] != m && p[5] != m) return true;
// edge 6
coord.reset(ipp, jpp, kp);
m = isInsideValue(accessor.getValue(coord), isovalue);
if (p[5] != m && p[6] != m) return true;
// edge 7
coord.reset(ip, jpp, kpp);
m = isInsideValue(accessor.getValue(coord), isovalue);
if (p[6] != m && p[7] != m) return true;
// edge 8
coord.reset(i, jpp, kp);
m = isInsideValue(accessor.getValue(coord), isovalue);
if (p[7] != m && p[4] != m) return true;
// edge 9
coord.reset(i, jp, k);
m = isInsideValue(accessor.getValue(coord), isovalue);
if (p[0] != m && p[4] != m) return true;
// edge 10
coord.reset(ipp, jp, k);
m = isInsideValue(accessor.getValue(coord), isovalue);
if (p[1] != m && p[5] != m) return true;
// edge 11
coord.reset(ipp, jp, kpp);
m = isInsideValue(accessor.getValue(coord), isovalue);
if (p[2] != m && p[6] != m) return true;
// edge 12
coord.reset(i, jp, kpp);
m = isInsideValue(accessor.getValue(coord), isovalue);
if (p[3] != m && p[7] != m) return true;
// Evaluate faces
// face 1
coord.reset(ip, jp, k);
m = isInsideValue(accessor.getValue(coord), isovalue);
if (p[0] != m && p[1] != m && p[4] != m && p[5] != m) return true;
// face 2
coord.reset(ipp, jp, kp);
m = isInsideValue(accessor.getValue(coord), isovalue);
if (p[1] != m && p[2] != m && p[5] != m && p[6] != m) return true;
// face 3
coord.reset(ip, jp, kpp);
m = isInsideValue(accessor.getValue(coord), isovalue);
if (p[2] != m && p[3] != m && p[6] != m && p[7] != m) return true;
// face 4
coord.reset(i, jp, kp);
m = isInsideValue(accessor.getValue(coord), isovalue);
if (p[0] != m && p[3] != m && p[4] != m && p[7] != m) return true;
// face 5
coord.reset(ip, j, kp);
m = isInsideValue(accessor.getValue(coord), isovalue);
if (p[0] != m && p[1] != m && p[2] != m && p[3] != m) return true;
// face 6
coord.reset(ip, jpp, kp);
m = isInsideValue(accessor.getValue(coord), isovalue);
if (p[4] != m && p[5] != m && p[6] != m && p[7] != m) return true;
// test cube center
coord.reset(ip, jp, kp);
m = isInsideValue(accessor.getValue(coord), isovalue);
if (p[0] != m && p[1] != m && p[2] != m && p[3] != m &&
p[4] != m && p[5] != m && p[6] != m && p[7] != m) return true;
return false;
}
////////////////////////////////////////
template <class LeafType>
inline void
mergeVoxels(LeafType& leaf, const Coord& start, int dim, int regionId)
{
Coord ijk, end = start;
end[0] += dim;
end[1] += dim;
end[2] += dim;
for (ijk[0] = start[0]; ijk[0] < end[0]; ++ijk[0]) {
for (ijk[1] = start[1]; ijk[1] < end[1]; ++ijk[1]) {
for (ijk[2] = start[2]; ijk[2] < end[2]; ++ijk[2]) {
leaf.setValueOnly(ijk, regionId);
}
}
}
}
// Note that we must use ValueType::value_type or else Visual C++ gets confused
// thinking that it is a constructor.
template <class LeafType>
inline bool
isMergable(LeafType& leaf, const Coord& start, int dim,
typename LeafType::ValueType::value_type adaptivity)
{
if (adaptivity < 1e-6) return false;
using VecT = typename LeafType::ValueType;
Coord ijk, end = start;
end[0] += dim;
end[1] += dim;
end[2] += dim;
std::vector<VecT> norms;
for (ijk[0] = start[0]; ijk[0] < end[0]; ++ijk[0]) {
for (ijk[1] = start[1]; ijk[1] < end[1]; ++ijk[1]) {
for (ijk[2] = start[2]; ijk[2] < end[2]; ++ijk[2]) {
if(!leaf.isValueOn(ijk)) continue;
norms.push_back(leaf.getValue(ijk));
}
}
}
size_t N = norms.size();
for (size_t ni = 0; ni < N; ++ni) {
VecT n_i = norms[ni];
for (size_t nj = 0; nj < N; ++nj) {
VecT n_j = norms[nj];
if ((1.0 - n_i.dot(n_j)) > adaptivity) return false;
}
}
return true;
}
////////////////////////////////////////
/// linear interpolation.
inline double evalZeroCrossing(double v0, double v1, double iso) { return (iso - v0) / (v1 - v0); }
/// @brief Extracts the eight corner values for leaf inclusive cells.
template<typename LeafT>
inline void
collectCornerValues(const LeafT& leaf, const Index offset, std::vector<double>& values)
{
values[0] = double(leaf.getValue(offset)); // i, j, k
values[3] = double(leaf.getValue(offset + 1)); // i, j, k+1
values[4] = double(leaf.getValue(offset + LeafT::DIM)); // i, j+1, k
values[7] = double(leaf.getValue(offset + LeafT::DIM + 1)); // i, j+1, k+1
values[1] = double(leaf.getValue(offset + (LeafT::DIM * LeafT::DIM))); // i+1, j, k
values[2] = double(leaf.getValue(offset + (LeafT::DIM * LeafT::DIM) + 1)); // i+1, j, k+1
values[5] = double(leaf.getValue(offset + (LeafT::DIM * LeafT::DIM) + LeafT::DIM)); // i+1, j+1, k
values[6] = double(leaf.getValue(offset + (LeafT::DIM * LeafT::DIM) + LeafT::DIM + 1)); // i+1, j+1, k+1
}
/// @brief Extracts the eight corner values for a cell starting at the given @ijk coordinate.
template<typename AccessorT>
inline void