10#ifndef OPENVDB_TOOLS_VOLUME_TO_MESH_HAS_BEEN_INCLUDED
11#define OPENVDB_TOOLS_VOLUME_TO_MESH_HAS_BEEN_INCLUDED
19#include <tbb/blocked_range.h>
20#include <tbb/parallel_for.h>
21#include <tbb/parallel_reduce.h>
22#include <tbb/task_arena.h>
52template<
typename Gr
idType>
56 std::vector<Vec3s>& points,
57 std::vector<Vec4I>& quads,
58 double isovalue = 0.0);
81template<
typename Gr
idType>
85 std::vector<Vec3s>& points,
86 std::vector<Vec3I>& triangles,
87 std::vector<Vec4I>& quads,
88 double isovalue = 0.0,
89 double adaptivity = 0.0,
90 bool relaxDisorientedTriangles =
true);
106 inline PolygonPool(
const size_t numQuads,
const size_t numTriangles);
110 inline void resetQuads(
size_t size);
111 inline void clearQuads();
113 inline void resetTriangles(
size_t size);
114 inline void clearTriangles();
119 const size_t&
numQuads()
const {
return mNumQuads; }
134 const char&
quadFlags(
size_t n)
const {
return mQuadFlags[n]; }
143 inline bool trimQuads(
const size_t n,
bool reallocate =
false);
144 inline bool trimTrinagles(
const size_t n,
bool reallocate =
false);
150 size_t mNumQuads, mNumTriangles;
151 std::unique_ptr<openvdb::Vec4I[]> mQuads;
152 std::unique_ptr<openvdb::Vec3I[]> mTriangles;
153 std::unique_ptr<char[]> mQuadFlags, mTriangleFlags;
175 VolumeToMesh(
double isovalue = 0,
double adaptivity = 0,
bool relaxDisorientedTriangles =
true);
191 const std::vector<uint8_t>&
pointFlags()
const {
return mPointFlags; }
200 template<
typename InputGr
idType>
201 void operator()(
const InputGridType&);
254 size_t mPointListSize, mSeamPointListSize, mPolygonPoolListSize;
255 double mIsovalue, mPrimAdaptivity, mSecAdaptivity;
262 bool mInvertSurfaceMask, mRelaxDisorientedTriangles;
264 std::unique_ptr<uint32_t[]> mQuantizedSeamPoints;
265 std::vector<uint8_t> mPointFlags;
279 const std::vector<Vec3d>& points,
280 const std::vector<Vec3d>& normals)
286 if (points.empty())
return avgPos;
288 for (
size_t n = 0, N = points.size(); n < N; ++n) {
292 avgPos /= double(points.size());
296 double m00=0,m01=0,m02=0,
303 for (
size_t n = 0, N = points.size(); n < N; ++n) {
305 const Vec3d& n_ref = normals[n];
308 m00 += n_ref[0] * n_ref[0];
309 m11 += n_ref[1] * n_ref[1];
310 m22 += n_ref[2] * n_ref[2];
312 m01 += n_ref[0] * n_ref[1];
313 m02 += n_ref[0] * n_ref[2];
314 m12 += n_ref[1] * n_ref[2];
317 rhs += n_ref * n_ref.
dot(points[n] - avgPos);
340 diagonalizeSymmetricMatrix(A, eigenVectors, eigenValues, 300);
342 Mat3d D = Mat3d::identity();
345 double tolerance = std::max(std::abs(eigenValues[0]), std::abs(eigenValues[1]));
346 tolerance = std::max(tolerance, std::abs(eigenValues[2]));
350 for (
int i = 0; i < 3; ++i ) {
351 if (std::abs(eigenValues[i]) < tolerance) {
355 D[i][i] = 1.0 / eigenValues[i];
361 Mat3d pseudoInv = eigenVectors * D * eigenVectors.
transpose();
362 return avgPos + pseudoInv * rhs;
377namespace volume_to_mesh_internal {
379template<
typename ValueType>
382 FillArray(ValueType* array,
const ValueType& v) : mArray(array), mValue(v) { }
384 void operator()(
const tbb::blocked_range<size_t>& range)
const {
385 const ValueType v = mValue;
386 for (
size_t n = range.begin(), N = range.end(); n < N; ++n) {
391 ValueType *
const mArray;
392 const ValueType mValue;
396template<
typename ValueType>
398fillArray(ValueType* array,
const ValueType& val,
const size_t length)
400 const auto grainSize = std::max<size_t>(
401 length / tbb::this_task_arena::max_concurrency(), 1024);
402 const tbb::blocked_range<size_t> range(0, length, grainSize);
403 tbb::parallel_for(range, FillArray<ValueType>(array, val), tbb::simple_partitioner());
408enum { SIGNS = 0xFF, EDGES = 0xE00, INSIDE = 0x100,
409 XEDGE = 0x200, YEDGE = 0x400, ZEDGE = 0x800, SEAM = 0x1000};
413const bool sAdaptable[256] = {
414 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,
415 1,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,0,0,0,0,0,0,0,0,0,0,0,0}};
532 const Vec3d& p0,
const Vec3d& p1,
533 const Vec3d& p2,
const Vec3d& p3,
534 double epsilon = 0.001)
537 Vec3d normal = (p2-p0).cross(p1-p3);
539 const Vec3d centroid = (p0 + p1 + p2 + p3);
540 const double d = centroid.
dot(normal) * 0.25;
544 double absDist = std::abs(p0.dot(normal) - d);
545 if (absDist > epsilon)
return false;
547 absDist = std::abs(p1.dot(normal) - d);
548 if (absDist > epsilon)
return false;
550 absDist = std::abs(p2.dot(normal) - d);
551 if (absDist > epsilon)
return false;
553 absDist = std::abs(p3.dot(normal) - d);
554 if (absDist > epsilon)
return false;
567 MASK_FIRST_10_BITS = 0x000003FF,
568 MASK_DIRTY_BIT = 0x80000000,
569 MASK_INVALID_BIT = 0x40000000
573packPoint(
const Vec3d& v)
578 assert(!(v.x() > 1.0) && !(v.y() > 1.0) && !(v.z() > 1.0));
579 assert(!(v.x() < 0.0) && !(v.y() < 0.0) && !(v.z() < 0.0));
581 data |= (uint32_t(v.x() * 1023.0) & MASK_FIRST_10_BITS) << 20;
582 data |= (uint32_t(v.y() * 1023.0) & MASK_FIRST_10_BITS) << 10;
583 data |= (uint32_t(v.z() * 1023.0) & MASK_FIRST_10_BITS);
589unpackPoint(uint32_t data)
592 v.z() = double(data & MASK_FIRST_10_BITS) * 0.0009775171;
594 v.y() = double(data & MASK_FIRST_10_BITS) * 0.0009775171;
596 v.x() = double(data & MASK_FIRST_10_BITS) * 0.0009775171;
606inline bool isBoolValue() {
return false; }
609inline bool isBoolValue<bool>() {
return true; }
614inline bool isInsideValue(T
value, T isovalue) {
return value < isovalue; }
617inline bool isInsideValue<bool>(
bool value,
bool ) {
return value; }
620template<
typename AccessorT>
622getCellVertexValues(
const AccessorT& accessor, Coord ijk,
623 math::Tuple<8, typename AccessorT::ValueType>& values)
625 values[0] = accessor.getValue(ijk);
627 values[1] = accessor.getValue(ijk);
629 values[2] = accessor.getValue(ijk);
631 values[3] = accessor.getValue(ijk);
633 values[4] = accessor.getValue(ijk);
635 values[5] = accessor.getValue(ijk);
637 values[6] = accessor.getValue(ijk);
639 values[7] = accessor.getValue(ijk);
643template<
typename LeafT>
645getCellVertexValues(
const LeafT& leaf,
const Index offset,
646 math::Tuple<8, typename LeafT::ValueType>& values)
648 values[0] = leaf.getValue(offset);
649 values[3] = leaf.getValue(offset + 1);
650 values[4] = leaf.getValue(offset + LeafT::DIM);
651 values[7] = leaf.getValue(offset + LeafT::DIM + 1);
652 values[1] = leaf.getValue(offset + (LeafT::DIM * LeafT::DIM));
653 values[2] = leaf.getValue(offset + (LeafT::DIM * LeafT::DIM) + 1);
654 values[5] = leaf.getValue(offset + (LeafT::DIM * LeafT::DIM) + LeafT::DIM);
655 values[6] = leaf.getValue(offset + (LeafT::DIM * LeafT::DIM) + LeafT::DIM + 1);
659template<
typename ValueType>
661computeSignFlags(
const math::Tuple<8, ValueType>& values,
const ValueType iso)
664 signs |= isInsideValue(values[0], iso) ? 1u : 0u;
665 signs |= isInsideValue(values[1], iso) ? 2u : 0u;
666 signs |= isInsideValue(values[2], iso) ? 4u : 0u;
667 signs |= isInsideValue(values[3], iso) ? 8u : 0u;
668 signs |= isInsideValue(values[4], iso) ? 16u : 0u;
669 signs |= isInsideValue(values[5], iso) ? 32u : 0u;
670 signs |= isInsideValue(values[6], iso) ? 64u : 0u;
671 signs |= isInsideValue(values[7], iso) ? 128u : 0u;
672 return uint8_t(signs);
678template<
typename AccessorT>
680evalCellSigns(
const AccessorT& accessor,
const Coord& ijk,
typename AccessorT::ValueType iso)
684 if (isInsideValue(accessor.getValue(coord), iso)) signs |= 1u;
686 if (isInsideValue(accessor.getValue(coord), iso)) signs |= 2u;
688 if (isInsideValue(accessor.getValue(coord), iso)) signs |= 4u;
690 if (isInsideValue(accessor.getValue(coord), iso)) signs |= 8u;
691 coord[1] += 1; coord[2] = ijk[2];
692 if (isInsideValue(accessor.getValue(coord), iso)) signs |= 16u;
694 if (isInsideValue(accessor.getValue(coord), iso)) signs |= 32u;
696 if (isInsideValue(accessor.getValue(coord), iso)) signs |= 64u;
698 if (isInsideValue(accessor.getValue(coord), iso)) signs |= 128u;
699 return uint8_t(signs);
705template<
typename LeafT>
707evalCellSigns(
const LeafT& leaf,
const Index offset,
typename LeafT::ValueType iso)
712 if (isInsideValue(leaf.getValue(offset), iso)) signs |= 1u;
715 if (isInsideValue(leaf.getValue(offset + 1), iso)) signs |= 8u;
718 if (isInsideValue(leaf.getValue(offset + LeafT::DIM), iso)) signs |= 16u;
721 if (isInsideValue(leaf.getValue(offset + LeafT::DIM + 1), iso)) signs |= 128u;
724 if (isInsideValue(leaf.getValue(offset + (LeafT::DIM * LeafT::DIM) ), iso)) signs |= 2u;
727 if (isInsideValue(leaf.getValue(offset + (LeafT::DIM * LeafT::DIM) + 1), iso)) signs |= 4u;
730 if (isInsideValue(leaf.getValue(offset + (LeafT::DIM * LeafT::DIM) + LeafT::DIM), iso)) signs |= 32u;
733 if (isInsideValue(leaf.getValue(offset + (LeafT::DIM * LeafT::DIM) + LeafT::DIM + 1), iso)) signs |= 64u;
735 return uint8_t(signs);
741template<
class AccessorT>
743correctCellSigns(uint8_t& signs, uint8_t face,
744 const AccessorT& acc, Coord ijk,
typename AccessorT::ValueType iso)
749 if (sAmbiguousFace[evalCellSigns(acc, ijk, iso)] == 3) signs = uint8_t(~signs);
753 if (sAmbiguousFace[evalCellSigns(acc, ijk, iso)] == 4) signs = uint8_t(~signs);
757 if (sAmbiguousFace[evalCellSigns(acc, ijk, iso)] == 1) signs = uint8_t(~signs);
761 if (sAmbiguousFace[evalCellSigns(acc, ijk, iso)] == 2) signs = uint8_t(~signs);
765 if (sAmbiguousFace[evalCellSigns(acc, ijk, iso)] == 6) signs = uint8_t(~signs);
769 if (sAmbiguousFace[evalCellSigns(acc, ijk, iso)] == 5) signs = uint8_t(~signs);
777template<
class AccessorT>
779isNonManifold(
const AccessorT& accessor,
const Coord& ijk,
780 typename AccessorT::ValueType isovalue,
const int dim)
786 p[0] = isInsideValue(accessor.getValue(coord), isovalue);
788 p[1] = isInsideValue(accessor.getValue(coord), isovalue);
790 p[2] = isInsideValue(accessor.getValue(coord), isovalue);
792 p[3] = isInsideValue(accessor.getValue(coord), isovalue);
793 coord[1] += dim; coord[2] = ijk[2];
794 p[4] = isInsideValue(accessor.getValue(coord), isovalue);
796 p[5] = isInsideValue(accessor.getValue(coord), isovalue);
798 p[6] = isInsideValue(accessor.getValue(coord), isovalue);
800 p[7] = isInsideValue(accessor.getValue(coord), isovalue);
804 if (p[0]) signs |= 1u;
805 if (p[1]) signs |= 2u;
806 if (p[2]) signs |= 4u;
807 if (p[3]) signs |= 8u;
808 if (p[4]) signs |= 16u;
809 if (p[5]) signs |= 32u;
810 if (p[6]) signs |= 64u;
811 if (p[7]) signs |= 128u;
812 if (!sAdaptable[signs])
return true;
817 int i = ijk[0], ip = ijk[0] + hDim, ipp = ijk[0] + dim;
818 int j = ijk[1], jp = ijk[1] + hDim, jpp = ijk[1] + dim;
819 int k = ijk[2], kp = ijk[2] + hDim, kpp = ijk[2] + dim;
822 coord.reset(ip, j, k);
823 m = isInsideValue(accessor.getValue(coord), isovalue);
824 if (p[0] != m && p[1] != m)
return true;
827 coord.reset(ipp, j, kp);
828 m = isInsideValue(accessor.getValue(coord), isovalue);
829 if (p[1] != m && p[2] != m)
return true;
832 coord.reset(ip, j, kpp);
833 m = isInsideValue(accessor.getValue(coord), isovalue);
834 if (p[2] != m && p[3] != m)
return true;
837 coord.reset(i, j, kp);
838 m = isInsideValue(accessor.getValue(coord), isovalue);
839 if (p[0] != m && p[3] != m)
return true;
842 coord.reset(ip, jpp, k);
843 m = isInsideValue(accessor.getValue(coord), isovalue);
844 if (p[4] != m && p[5] != m)
return true;
847 coord.reset(ipp, jpp, kp);
848 m = isInsideValue(accessor.getValue(coord), isovalue);
849 if (p[5] != m && p[6] != m)
return true;
852 coord.reset(ip, jpp, kpp);
853 m = isInsideValue(accessor.getValue(coord), isovalue);
854 if (p[6] != m && p[7] != m)
return true;
857 coord.reset(i, jpp, kp);
858 m = isInsideValue(accessor.getValue(coord), isovalue);
859 if (p[7] != m && p[4] != m)
return true;
862 coord.reset(i, jp, k);
863 m = isInsideValue(accessor.getValue(coord), isovalue);
864 if (p[0] != m && p[4] != m)
return true;
867 coord.reset(ipp, jp, k);
868 m = isInsideValue(accessor.getValue(coord), isovalue);
869 if (p[1] != m && p[5] != m)
return true;
872 coord.reset(ipp, jp, kpp);
873 m = isInsideValue(accessor.getValue(coord), isovalue);
874 if (p[2] != m && p[6] != m)
return true;
878 coord.reset(i, jp, kpp);
879 m = isInsideValue(accessor.getValue(coord), isovalue);
880 if (p[3] != m && p[7] != m)
return true;
886 coord.reset(ip, jp, k);
887 m = isInsideValue(accessor.getValue(coord), isovalue);
888 if (p[0] != m && p[1] != m && p[4] != m && p[5] != m)
return true;
891 coord.reset(ipp, jp, kp);
892 m = isInsideValue(accessor.getValue(coord), isovalue);
893 if (p[1] != m && p[2] != m && p[5] != m && p[6] != m)
return true;
896 coord.reset(ip, jp, kpp);
897 m = isInsideValue(accessor.getValue(coord), isovalue);
898 if (p[2] != m && p[3] != m && p[6] != m && p[7] != m)
return true;
901 coord.reset(i, jp, kp);
902 m = isInsideValue(accessor.getValue(coord), isovalue);
903 if (p[0] != m && p[3] != m && p[4] != m && p[7] != m)
return true;
906 coord.reset(ip, j, kp);
907 m = isInsideValue(accessor.getValue(coord), isovalue);
908 if (p[0] != m && p[1] != m && p[2] != m && p[3] != m)
return true;
911 coord.reset(ip, jpp, kp);
912 m = isInsideValue(accessor.getValue(coord), isovalue);
913 if (p[4] != m && p[5] != m && p[6] != m && p[7] != m)
return true;
916 coord.reset(ip, jp, kp);
917 m = isInsideValue(accessor.getValue(coord), isovalue);
918 if (p[0] != m && p[1] != m && p[2] != m && p[3] != m &&
919 p[4] != m && p[5] != m && p[6] != m && p[7] != m)
return true;
928template <
class LeafType>
930mergeVoxels(LeafType& leaf,
const Coord& start,
int dim,
int regionId)
932 Coord ijk, end = start;
937 for (ijk[0] = start[0]; ijk[0] < end[0]; ++ijk[0]) {
938 for (ijk[1] = start[1]; ijk[1] < end[1]; ++ijk[1]) {
939 for (ijk[2] = start[2]; ijk[2] < end[2]; ++ijk[2]) {
940 leaf.setValueOnly(ijk, regionId);
949template <
class LeafType>
951isMergable(LeafType& leaf,
const Coord& start,
int dim,
952 typename LeafType::ValueType::value_type adaptivity)
954 if (adaptivity < 1e-6)
return false;
956 using VecT =
typename LeafType::ValueType;
957 Coord ijk, end = start;
962 std::vector<VecT> norms;
963 for (ijk[0] = start[0]; ijk[0] < end[0]; ++ijk[0]) {
964 for (ijk[1] = start[1]; ijk[1] < end[1]; ++ijk[1]) {
965 for (ijk[2] = start[2]; ijk[2] < end[2]; ++ijk[2]) {
967 if(!leaf.isValueOn(ijk))
continue;
968 norms.push_back(leaf.getValue(ijk));
973 size_t N = norms.size();
974 for (
size_t ni = 0; ni < N; ++ni) {
975 VecT n_i = norms[ni];
976 for (
size_t nj = 0; nj < N; ++nj) {
977 VecT n_j = norms[nj];
978 if ((1.0 - n_i.dot(n_j)) > adaptivity)
return false;
989inline double evalZeroCrossing(
double v0,
double v1,
double iso) {
return (iso - v0) / (v1 - v0); }
993template<
typename LeafT>
995collectCornerValues(
const LeafT& leaf,
const Index offset, std::vector<double>& values)
997 values[0] = double(leaf.getValue(offset));
998 values[3] = double(leaf.getValue(offset + 1));
999 values[4] = double(leaf.getValue(offset + LeafT::DIM));
1000 values[7] = double(leaf.getValue(offset + LeafT::DIM + 1));
1001 values[1] = double(leaf.getValue(offset + (LeafT::DIM * LeafT::DIM)));
1002 values[2] = double(leaf.getValue(offset + (LeafT::DIM * LeafT::DIM) + 1));
1003 values[5] = double(leaf.getValue(offset + (LeafT::DIM * LeafT::DIM) + LeafT::DIM));
1004 values[6] = double(leaf.getValue(offset + (LeafT::DIM * LeafT::DIM) + LeafT::DIM + 1));
1009template<
typename AccessorT>
1011collectCornerValues(
const AccessorT& acc,
const Coord& ijk, std::vector<double>& values)
1014 values[0] = double(acc.getValue(coord));
1017 values[1] = double(acc.getValue(coord));
1020 values[2] = double(acc.getValue(coord));
1023 values[3] = double(acc.getValue(coord));
1025 coord[1] += 1; coord[2] = ijk[2];
1026 values[4] = double(acc.getValue(coord));
1029 values[5] = double(acc.getValue(coord));
1032 values[6] = double(acc.getValue(coord));
1035 values[7] = double(acc.getValue(coord));
1041computePoint(
const std::vector<double>& values,
unsigned char signs,
1042 unsigned char edgeGroup,
double iso)
1044 Vec3d avg(0.0, 0.0, 0.0);
1047 if (sEdgeGroupTable[signs][1] == edgeGroup) {
1048 avg[0] += evalZeroCrossing(values[0], values[1], iso);
1052 if (sEdgeGroupTable[signs][2] == edgeGroup) {
1054 avg[2] += evalZeroCrossing(values[1], values[2], iso);
1058 if (sEdgeGroupTable[signs][3] == edgeGroup) {
1059 avg[0] += evalZeroCrossing(values[3], values[2], iso);
1064 if (sEdgeGroupTable[signs][4] == edgeGroup) {
1065 avg[2] += evalZeroCrossing(values[0], values[3], iso);
1069 if (sEdgeGroupTable[signs][5] == edgeGroup) {
1070 avg[0] += evalZeroCrossing(values[4], values[5], iso);
1075 if (sEdgeGroupTable[signs][6] == edgeGroup) {
1078 avg[2] += evalZeroCrossing(values[5], values[6], iso);
1082 if (sEdgeGroupTable[signs][7] == edgeGroup) {
1083 avg[0] += evalZeroCrossing(values[7], values[6], iso);
1089 if (sEdgeGroupTable[signs][8] == edgeGroup) {
1091 avg[2] += evalZeroCrossing(values[4], values[7], iso);
1095 if (sEdgeGroupTable[signs][9] == edgeGroup) {
1096 avg[1] += evalZeroCrossing(values[0], values[4], iso);
1100 if (sEdgeGroupTable[signs][10] == edgeGroup) {
1102 avg[1] += evalZeroCrossing(values[1], values[5], iso);
1106 if (sEdgeGroupTable[signs][11] == edgeGroup) {
1108 avg[1] += evalZeroCrossing(values[2], values[6], iso);
1113 if (sEdgeGroupTable[signs][12] == edgeGroup) {
1114 avg[1] += evalZeroCrossing(values[3], values[7], iso);
1120 double w = 1.0 / double(samples);
1133computeMaskedPoint(Vec3d& avg,
const std::vector<double>& values,
unsigned char signs,
1134 unsigned char signsMask,
unsigned char edgeGroup,
double iso)
1136 avg =
Vec3d(0.0, 0.0, 0.0);
1139 if (sEdgeGroupTable[signs][1] == edgeGroup
1140 && sEdgeGroupTable[signsMask][1] == 0) {
1141 avg[0] += evalZeroCrossing(values[0], values[1], iso);
1145 if (sEdgeGroupTable[signs][2] == edgeGroup
1146 && sEdgeGroupTable[signsMask][2] == 0) {
1148 avg[2] += evalZeroCrossing(values[1], values[2], iso);
1152 if (sEdgeGroupTable[signs][3] == edgeGroup
1153 && sEdgeGroupTable[signsMask][3] == 0) {
1154 avg[0] += evalZeroCrossing(values[3], values[2], iso);
1159 if (sEdgeGroupTable[signs][4] == edgeGroup
1160 && sEdgeGroupTable[signsMask][4] == 0) {
1161 avg[2] += evalZeroCrossing(values[0], values[3], iso);
1165 if (sEdgeGroupTable[signs][5] == edgeGroup
1166 && sEdgeGroupTable[signsMask][5] == 0) {
1167 avg[0] += evalZeroCrossing(values[4], values[5], iso);
1172 if (sEdgeGroupTable[signs][6] == edgeGroup
1173 && sEdgeGroupTable[signsMask][6] == 0) {
1176 avg[2] += evalZeroCrossing(values[5], values[6], iso);
1180 if (sEdgeGroupTable[signs][7] == edgeGroup
1181 && sEdgeGroupTable[signsMask][7] == 0) {
1182 avg[0] += evalZeroCrossing(values[7], values[6], iso);
1188 if (sEdgeGroupTable[signs][8] == edgeGroup
1189 && sEdgeGroupTable[signsMask][8] == 0) {
1191 avg[2] += evalZeroCrossing(values[4], values[7], iso);
1195 if (sEdgeGroupTable[signs][9] == edgeGroup
1196 && sEdgeGroupTable[signsMask][9] == 0) {
1197 avg[1] += evalZeroCrossing(values[0], values[4], iso);
1201 if (sEdgeGroupTable[signs][10] == edgeGroup
1202 && sEdgeGroupTable[signsMask][10] == 0) {
1204 avg[1] += evalZeroCrossing(values[1], values[5], iso);
1208 if (sEdgeGroupTable[signs][11] == edgeGroup
1209 && sEdgeGroupTable[signsMask][11] == 0) {
1211 avg[1] += evalZeroCrossing(values[2], values[6], iso);
1216 if (sEdgeGroupTable[signs][12] == edgeGroup
1217 && sEdgeGroupTable[signsMask][12] == 0) {
1218 avg[1] += evalZeroCrossing(values[3], values[7], iso);
1224 double w = 1.0 / double(samples);
1237computeWeightedPoint(
const Vec3d& p,
const std::vector<double>& values,
1238 unsigned char signs,
unsigned char edgeGroup,
double iso)
1240 std::vector<Vec3d> samples;
1243 std::vector<double> weights;
1246 Vec3d avg(0.0, 0.0, 0.0);
1248 if (sEdgeGroupTable[signs][1] == edgeGroup) {
1249 avg[0] = evalZeroCrossing(values[0], values[1], iso);
1253 samples.push_back(avg);
1254 weights.push_back((avg-p).lengthSqr());
1257 if (sEdgeGroupTable[signs][2] == edgeGroup) {
1260 avg[2] = evalZeroCrossing(values[1], values[2], iso);
1262 samples.push_back(avg);
1263 weights.push_back((avg-p).lengthSqr());
1266 if (sEdgeGroupTable[signs][3] == edgeGroup) {
1267 avg[0] = evalZeroCrossing(values[3], values[2], iso);
1271 samples.push_back(avg);
1272 weights.push_back((avg-p).lengthSqr());
1275 if (sEdgeGroupTable[signs][4] == edgeGroup) {
1278 avg[2] = evalZeroCrossing(values[0], values[3], iso);
1280 samples.push_back(avg);
1281 weights.push_back((avg-p).lengthSqr());
1284 if (sEdgeGroupTable[signs][5] == edgeGroup) {
1285 avg[0] = evalZeroCrossing(values[4], values[5], iso);
1289 samples.push_back(avg);
1290 weights.push_back((avg-p).lengthSqr());
1293 if (sEdgeGroupTable[signs][6] == edgeGroup) {
1296 avg[2] = evalZeroCrossing(values[5], values[6], iso);
1298 samples.push_back(avg);
1299 weights.push_back((avg-p).lengthSqr());
1302 if (sEdgeGroupTable[signs][7] == edgeGroup) {
1303 avg[0] = evalZeroCrossing(values[7], values[6], iso);
1307 samples.push_back(avg);
1308 weights.push_back((avg-p).lengthSqr());
1311 if (sEdgeGroupTable[signs][8] == edgeGroup) {
1314 avg[2] = evalZeroCrossing(values[4], values[7], iso);
1316 samples.push_back(avg);
1317 weights.push_back((avg-p).lengthSqr());
1320 if (sEdgeGroupTable[signs][9] == edgeGroup) {
1322 avg[1] = evalZeroCrossing(values[0], values[4], iso);
1325 samples.push_back(avg);
1326 weights.push_back((avg-p).lengthSqr());
1329 if (sEdgeGroupTable[signs][10] == edgeGroup) {
1331 avg[1] = evalZeroCrossing(values[1], values[5], iso);
1334 samples.push_back(avg);
1335 weights.push_back((avg-p).lengthSqr());
1338 if (sEdgeGroupTable[signs][11] == edgeGroup) {
1340 avg[1] = evalZeroCrossing(values[2], values[6], iso);
1343 samples.push_back(avg);
1344 weights.push_back((avg-p).lengthSqr());
1347 if (sEdgeGroupTable[signs][12] == edgeGroup) {
1349 avg[1] = evalZeroCrossing(values[3], values[7], iso);
1352 samples.push_back(avg);
1353 weights.push_back((avg-p).lengthSqr());
1357 double minWeight = std::numeric_limits<double>::max();
1358 double maxWeight = -std::numeric_limits<double>::max();
1360 for (
size_t i = 0, I = weights.size(); i < I; ++i) {
1361 minWeight = std::min(minWeight, weights[i]);
1362 maxWeight = std::max(maxWeight, weights[i]);
1365 const double offset = maxWeight + minWeight * 0.1;
1366 for (
size_t i = 0, I = weights.size(); i < I; ++i) {
1367 weights[i] = offset - weights[i];
1371 double weightSum = 0.0;
1372 for (
size_t i = 0, I = weights.size(); i < I; ++i) {
1373 weightSum += weights[i];
1380 if (samples.size() > 1) {
1381 for (
size_t i = 0, I = samples.size(); i < I; ++i) {
1382 avg += samples[i] * (weights[i] / weightSum);
1385 avg = samples.front();
1395computeCellPoints(std::vector<Vec3d>& points,
1396 const std::vector<double>& values,
unsigned char signs,
double iso)
1398 for (
size_t n = 1, N = sEdgeGroupTable[signs][0] + 1; n < N; ++n) {
1399 points.push_back(computePoint(values, signs, uint8_t(n), iso));
1408matchEdgeGroup(
unsigned char groupId,
unsigned char lhsSigns,
unsigned char rhsSigns)
1411 for (
size_t i = 1; i <= 12; ++i) {
1412 if (sEdgeGroupTable[lhsSigns][i] == groupId && sEdgeGroupTable[rhsSigns][i] != 0) {
1413 id = sEdgeGroupTable[rhsSigns][i];
1426computeCellPoints(std::vector<Vec3d>& points, std::vector<bool>& weightedPointMask,
1427 const std::vector<double>& lhsValues,
const std::vector<double>& rhsValues,
1428 unsigned char lhsSigns,
unsigned char rhsSigns,
1429 double iso,
size_t pointIdx,
const uint32_t * seamPointArray)
1431 for (
size_t n = 1, N = sEdgeGroupTable[lhsSigns][0] + 1; n < N; ++n) {
1433 int id = matchEdgeGroup(uint8_t(n), lhsSigns, rhsSigns);
1437 const unsigned char e = uint8_t(
id);
1438 const uint32_t& quantizedPoint = seamPointArray[pointIdx + (
id - 1)];
1440 if ((quantizedPoint & MASK_DIRTY_BIT) && !(quantizedPoint & MASK_INVALID_BIT)) {
1441 Vec3d p = unpackPoint(quantizedPoint);
1442 points.push_back(computeWeightedPoint(p, rhsValues, rhsSigns, e, iso));
1443 weightedPointMask.push_back(
true);
1445 points.push_back(computePoint(rhsValues, rhsSigns, e, iso));
1446 weightedPointMask.push_back(
false);
1450 points.push_back(computePoint(lhsValues, lhsSigns, uint8_t(n), iso));
1451 weightedPointMask.push_back(
false);
1457template <
typename InputTreeType>
1460 using InputLeafNodeType =
typename InputTreeType::LeafNodeType;
1461 using InputValueType =
typename InputLeafNodeType::ValueType;
1463 using Int16TreeType =
typename InputTreeType::template ValueConverter<Int16>::Type;
1464 using Int16LeafNodeType =
typename Int16TreeType::LeafNodeType;
1466 using Index32TreeType =
typename InputTreeType::template ValueConverter<Index32>::Type;
1467 using Index32LeafNodeType =
typename Index32TreeType::LeafNodeType;
1469 ComputePoints(Vec3s * pointArray,
1470 const InputTreeType& inputTree,
1471 const std::vector<Index32LeafNodeType*>& pointIndexLeafNodes,
1472 const std::vector<Int16LeafNodeType*>& signFlagsLeafNodes,
1473 const std::unique_ptr<Index32[]>& leafNodeOffsets,
1474 const math::Transform& xform,
1477 void setRefData(
const InputTreeType& refInputTree,
1478 const Index32TreeType& refPointIndexTree,
1479 const Int16TreeType& refSignFlagsTree,
1480 const uint32_t * quantizedSeamLinePoints,
1481 uint8_t * seamLinePointsFlags);
1483 void operator()(
const tbb::blocked_range<size_t>&)
const;
1486 Vec3s *
const mPoints;
1487 InputTreeType
const *
const mInputTree;
1488 Index32LeafNodeType *
const *
const mPointIndexNodes;
1489 Int16LeafNodeType
const *
const *
const mSignFlagsNodes;
1490 Index32 const *
const mNodeOffsets;
1491 math::Transform
const mTransform;
1492 double const mIsovalue;
1494 InputTreeType
const * mRefInputTree;
1495 Index32TreeType
const * mRefPointIndexTree;
1496 Int16TreeType
const * mRefSignFlagsTree;
1497 uint32_t
const * mQuantizedSeamLinePoints;
1498 uint8_t * mSeamLinePointsFlags;
1502template <
typename InputTreeType>
1503ComputePoints<InputTreeType>::ComputePoints(
1505 const InputTreeType& inputTree,
1506 const std::vector<Index32LeafNodeType*>& pointIndexLeafNodes,
1507 const std::vector<Int16LeafNodeType*>& signFlagsLeafNodes,
1508 const std::unique_ptr<Index32[]>& leafNodeOffsets,
1509 const math::Transform& xform,
1511 : mPoints(pointArray)
1512 , mInputTree(&inputTree)
1513 , mPointIndexNodes(pointIndexLeafNodes.data())
1514 , mSignFlagsNodes(signFlagsLeafNodes.data())
1515 , mNodeOffsets(leafNodeOffsets.get())
1518 , mRefInputTree(nullptr)
1519 , mRefPointIndexTree(nullptr)
1520 , mRefSignFlagsTree(nullptr)
1521 , mQuantizedSeamLinePoints(nullptr)
1522 , mSeamLinePointsFlags(nullptr)
1526template <
typename InputTreeType>
1528ComputePoints<InputTreeType>::setRefData(
1529 const InputTreeType& refInputTree,
1530 const Index32TreeType& refPointIndexTree,
1531 const Int16TreeType& refSignFlagsTree,
1532 const uint32_t * quantizedSeamLinePoints,
1533 uint8_t * seamLinePointsFlags)
1535 mRefInputTree = &refInputTree;
1536 mRefPointIndexTree = &refPointIndexTree;
1537 mRefSignFlagsTree = &refSignFlagsTree;
1538 mQuantizedSeamLinePoints = quantizedSeamLinePoints;
1539 mSeamLinePointsFlags = seamLinePointsFlags;
1542template <
typename InputTreeType>
1544ComputePoints<InputTreeType>::operator()(
const tbb::blocked_range<size_t>& range)
const
1546 using InputTreeAccessor = tree::ValueAccessor<const InputTreeType>;
1547 using Index32TreeAccessor = tree::ValueAccessor<const Index32TreeType>;
1548 using Int16TreeAccessor = tree::ValueAccessor<const Int16TreeType>;
1550 using IndexType =
typename Index32TreeType::ValueType;
1553 using IndexArrayMap = std::map<IndexType, IndexArray>;
1555 InputTreeAccessor inputAcc(*mInputTree);
1559 std::vector<Vec3d> points(4);
1560 std::vector<bool> weightedPointMask(4);
1561 std::vector<double> values(8), refValues(8);
1562 const double iso = mIsovalue;
1566 std::unique_ptr<InputTreeAccessor> refInputAcc;
1567 std::unique_ptr<Index32TreeAccessor> refPointIndexAcc;
1568 std::unique_ptr<Int16TreeAccessor> refSignFlagsAcc;
1570 const bool hasReferenceData = mRefInputTree && mRefPointIndexTree && mRefSignFlagsTree;
1572 if (hasReferenceData) {
1573 refInputAcc.reset(
new InputTreeAccessor(*mRefInputTree));
1574 refPointIndexAcc.reset(
new Index32TreeAccessor(*mRefPointIndexTree));
1575 refSignFlagsAcc.reset(
new Int16TreeAccessor(*mRefSignFlagsTree));
1578 for (
size_t n = range.begin(), N = range.end(); n != N; ++n) {
1580 Index32LeafNodeType& pointIndexNode = *mPointIndexNodes[n];
1581 const Coord& origin = pointIndexNode.origin();
1583 const Int16LeafNodeType& signFlagsNode = *mSignFlagsNodes[n];
1584 const InputLeafNodeType * inputNode = inputAcc.probeConstLeaf(origin);
1587 const InputLeafNodeType * refInputNode =
nullptr;
1588 const Index32LeafNodeType * refPointIndexNode =
nullptr;
1589 const Int16LeafNodeType * refSignFlagsNode =
nullptr;
1591 if (hasReferenceData) {
1592 refInputNode = refInputAcc->probeConstLeaf(origin);
1593 refPointIndexNode = refPointIndexAcc->probeConstLeaf(origin);
1594 refSignFlagsNode = refSignFlagsAcc->probeConstLeaf(origin);
1597 IndexType pointOffset = IndexType(mNodeOffsets[n]);
1598 IndexArrayMap regions;
1600 for (
auto it = pointIndexNode.beginValueOn(); it; ++it) {
1601 const Index offset = it.pos();
1603 const IndexType
id = it.getValue();
1606 regions[id].push_back(offset);
1611 pointIndexNode.setValueOnly(offset, pointOffset);
1613 const Int16 flags = signFlagsNode.getValue(offset);
1614 uint8_t signs = uint8_t(SIGNS & flags);
1615 uint8_t refSigns = 0;
1617 if ((flags & SEAM) && refPointIndexNode && refSignFlagsNode) {
1618 if (refSignFlagsNode->isValueOn(offset)) {
1619 refSigns = uint8_t(SIGNS & refSignFlagsNode->getValue(offset));
1623 ijk = Index32LeafNodeType::offsetToLocalCoord(offset);
1625 const bool inclusiveCell = inputNode &&
1626 ijk[0] < int(Index32LeafNodeType::DIM - 1) &&
1627 ijk[1] < int(Index32LeafNodeType::DIM - 1) &&
1628 ijk[2] < int(Index32LeafNodeType::DIM - 1);
1632 if (inclusiveCell) collectCornerValues(*inputNode, offset, values);
1633 else collectCornerValues(inputAcc, ijk, values);
1636 weightedPointMask.clear();
1638 if (refSigns == 0) {
1640 computeCellPoints(points, values, signs, iso);
1643 if (inclusiveCell && refInputNode) {
1644 collectCornerValues(*refInputNode, offset, refValues);
1646 collectCornerValues(*refInputAcc, ijk, refValues);
1648 computeCellPoints(points, weightedPointMask, values, refValues, signs, refSigns,
1649 iso, refPointIndexNode->getValue(offset), mQuantizedSeamLinePoints);
1652 xyz[0] = double(ijk[0]);
1653 xyz[1] = double(ijk[1]);
1654 xyz[2] = double(ijk[2]);
1656 for (
size_t i = 0, I = points.size(); i < I; ++i) {
1658 Vec3d& point = points[i];
1661 if (!std::isfinite(point[0]) ||
1662 !std::isfinite(point[1]) ||
1663 !std::isfinite(point[2]))
1666 "VolumeToMesh encountered NaNs or infs in the input VDB!"
1667 " Hint: Check the input and consider using the \"Diagnostics\" tool "
1668 "to detect and resolve the NaNs.");
1672 point = mTransform.indexToWorld(point);
1674 Vec3s& pos = mPoints[pointOffset];
1675 pos[0] = float(point[0]);
1676 pos[1] = float(point[1]);
1677 pos[2] = float(point[2]);
1679 if (mSeamLinePointsFlags && !weightedPointMask.empty() && weightedPointMask[i]) {
1680 mSeamLinePointsFlags[pointOffset] = uint8_t(1);
1688 for (
typename IndexArrayMap::iterator it = regions.begin(); it != regions.end(); ++it) {
1690 Vec3d avg(0.0), point(0.0);
1694 for (
size_t i = 0, I = voxels.size(); i < I; ++i) {
1696 const Index offset = voxels[i];
1697 ijk = Index32LeafNodeType::offsetToLocalCoord(offset);
1699 const bool inclusiveCell = inputNode &&
1700 ijk[0] < int(Index32LeafNodeType::DIM - 1) &&
1701 ijk[1] < int(Index32LeafNodeType::DIM - 1) &&
1702 ijk[2] < int(Index32LeafNodeType::DIM - 1);
1706 pointIndexNode.setValueOnly(offset, pointOffset);
1708 uint8_t signs = uint8_t(SIGNS & signFlagsNode.getValue(offset));
1710 if (inclusiveCell) collectCornerValues(*inputNode, offset, values);
1711 else collectCornerValues(inputAcc, ijk, values);
1714 computeCellPoints(points, values, signs, iso);
1716 avg[0] += double(ijk[0]) + points[0][0];
1717 avg[1] += double(ijk[1]) + points[0][1];
1718 avg[2] += double(ijk[2]) + points[0][2];
1724 double w = 1.0 / double(count);
1730 avg = mTransform.indexToWorld(avg);
1732 Vec3s& pos = mPoints[pointOffset];
1733 pos[0] = float(avg[0]);
1734 pos[1] = float(avg[1]);
1735 pos[2] = float(avg[2]);
1746template <
typename InputTreeType>
1747struct SeamLineWeights
1749 using InputLeafNodeType =
typename InputTreeType::LeafNodeType;
1750 using InputValueType =
typename InputLeafNodeType::ValueType;
1752 using Int16TreeType =
typename InputTreeType::template ValueConverter<Int16>::Type;
1753 using Int16LeafNodeType =
typename Int16TreeType::LeafNodeType;
1755 using Index32TreeType =
typename InputTreeType::template ValueConverter<Index32>::Type;
1756 using Index32LeafNodeType =
typename Index32TreeType::LeafNodeType;
1758 SeamLineWeights(
const std::vector<Int16LeafNodeType*>& signFlagsLeafNodes,
1759 const InputTreeType& inputTree,
1760 const Index32TreeType& refPointIndexTree,
1761 const Int16TreeType& refSignFlagsTree,
1762 uint32_t * quantizedPoints,
1764 : mSignFlagsNodes(signFlagsLeafNodes.data())
1765 , mInputTree(&inputTree)
1766 , mRefPointIndexTree(&refPointIndexTree)
1767 , mRefSignFlagsTree(&refSignFlagsTree)
1768 , mQuantizedPoints(quantizedPoints)
1773 void operator()(
const tbb::blocked_range<size_t>& range)
const
1775 tree::ValueAccessor<const InputTreeType> inputTreeAcc(*mInputTree);
1776 tree::ValueAccessor<const Index32TreeType> pointIndexTreeAcc(*mRefPointIndexTree);
1777 tree::ValueAccessor<const Int16TreeType> signFlagsTreeAcc(*mRefSignFlagsTree);
1779 std::vector<double> values(8);
1780 const double iso = double(mIsovalue);
1784 for (
size_t n = range.begin(), N = range.end(); n != N; ++n) {
1786 const Int16LeafNodeType& signFlagsNode = *mSignFlagsNodes[n];
1787 const Coord& origin = signFlagsNode.origin();
1789 const Int16LeafNodeType * refSignNode = signFlagsTreeAcc.probeConstLeaf(origin);
1790 if (!refSignNode)
continue;
1792 const Index32LeafNodeType* refPointIndexNode =
1793 pointIndexTreeAcc.probeConstLeaf(origin);
1794 if (!refPointIndexNode)
continue;
1796 const InputLeafNodeType * inputNode = inputTreeAcc.probeConstLeaf(origin);
1798 for (
typename Int16LeafNodeType::ValueOnCIter it = signFlagsNode.cbeginValueOn();
1801 const Index offset = it.pos();
1803 ijk = Index32LeafNodeType::offsetToLocalCoord(offset);
1805 const bool inclusiveCell = inputNode &&
1806 ijk[0] < int(Index32LeafNodeType::DIM - 1) &&
1807 ijk[1] < int(Index32LeafNodeType::DIM - 1) &&
1808 ijk[2] < int(Index32LeafNodeType::DIM - 1);
1812 if ((it.getValue() & SEAM) && refSignNode->isValueOn(offset)) {
1814 uint8_t lhsSigns = uint8_t(SIGNS & it.getValue());
1815 uint8_t rhsSigns = uint8_t(SIGNS & refSignNode->getValue(offset));
1818 if (inclusiveCell) {
1819 collectCornerValues(*inputNode, offset, values);
1821 collectCornerValues(inputTreeAcc, ijk, values);
1825 for (
unsigned i = 1, I = sEdgeGroupTable[lhsSigns][0] + 1; i < I; ++i) {
1827 int id = matchEdgeGroup(uint8_t(i), lhsSigns, rhsSigns);
1831 uint32_t& data = mQuantizedPoints[
1832 refPointIndexNode->getValue(offset) + (
id - 1)];
1834 if (!(data & MASK_DIRTY_BIT)) {
1836 int smaples = computeMaskedPoint(
1837 pos, values, lhsSigns, rhsSigns, uint8_t(i), iso);
1839 if (smaples > 0) data = packPoint(pos);
1840 else data = MASK_INVALID_BIT;
1842 data |= MASK_DIRTY_BIT;
1854 Int16LeafNodeType
const *
const *
const mSignFlagsNodes;
1855 InputTreeType
const *
const mInputTree;
1856 Index32TreeType
const *
const mRefPointIndexTree;
1857 Int16TreeType
const *
const mRefSignFlagsTree;
1858 uint32_t *
const mQuantizedPoints;
1859 InputValueType
const mIsovalue;
1863template <
typename TreeType>
1864struct SetSeamLineFlags
1866 using LeafNodeType =
typename TreeType::LeafNodeType;
1868 SetSeamLineFlags(
const std::vector<LeafNodeType*>& signFlagsLeafNodes,
1869 const TreeType& refSignFlagsTree)
1870 : mSignFlagsNodes(signFlagsLeafNodes.data())
1871 , mRefSignFlagsTree(&refSignFlagsTree)
1875 void operator()(
const tbb::blocked_range<size_t>& range)
const
1877 tree::ValueAccessor<const TreeType> refSignFlagsTreeAcc(*mRefSignFlagsTree);
1879 for (
size_t n = range.begin(), N = range.end(); n != N; ++n) {
1881 LeafNodeType& signFlagsNode = *mSignFlagsNodes[n];
1882 const Coord& origin = signFlagsNode.origin();
1884 const LeafNodeType * refSignNode = refSignFlagsTreeAcc.probeConstLeaf(origin);
1885 if (!refSignNode)
continue;
1887 for (
auto it = signFlagsNode.cbeginValueOn(); it; ++it) {
1888 const Index offset = it.pos();
1890 uint8_t rhsSigns = uint8_t(refSignNode->getValue(offset) & SIGNS);
1892 if (sEdgeGroupTable[rhsSigns][0] > 0) {
1894 const typename LeafNodeType::ValueType
value = it.getValue();
1895 uint8_t lhsSigns = uint8_t(
value & SIGNS);
1897 if (rhsSigns != lhsSigns) {
1898 signFlagsNode.setValueOnly(offset,
value | SEAM);
1908 LeafNodeType *
const *
const mSignFlagsNodes;
1909 TreeType
const *
const mRefSignFlagsTree;
1913template <
typename BoolTreeType,
typename SignDataType>
1914struct TransferSeamLineFlags
1916 using BoolLeafNodeType =
typename BoolTreeType::LeafNodeType;
1918 using SignDataTreeType =
typename BoolTreeType::template ValueConverter<SignDataType>::Type;
1919 using SignDataLeafNodeType =
typename SignDataTreeType::LeafNodeType;
1921 TransferSeamLineFlags(
const std::vector<SignDataLeafNodeType*>& signFlagsLeafNodes,
1922 const BoolTreeType& maskTree)
1923 : mSignFlagsNodes(signFlagsLeafNodes.data())
1924 , mMaskTree(&maskTree)
1928 void operator()(
const tbb::blocked_range<size_t>& range)
const
1930 tree::ValueAccessor<const BoolTreeType> maskAcc(*mMaskTree);
1932 for (
size_t n = range.begin(), N = range.end(); n != N; ++n) {
1934 SignDataLeafNodeType& signFlagsNode = *mSignFlagsNodes[n];
1935 const Coord& origin = signFlagsNode.origin();
1937 const BoolLeafNodeType * maskNode = maskAcc.probeConstLeaf(origin);
1938 if (!maskNode)
continue;
1940 using ValueOnCIter =
typename SignDataLeafNodeType::ValueOnCIter;
1942 for (ValueOnCIter it = signFlagsNode.cbeginValueOn(); it; ++it) {
1943 const Index offset = it.pos();
1945 if (maskNode->isValueOn(offset)) {
1946 signFlagsNode.setValueOnly(offset, it.getValue() | SEAM);
1955 SignDataLeafNodeType *
const *
const mSignFlagsNodes;
1956 BoolTreeType
const *
const mMaskTree;
1960template <
typename TreeType>
1961struct MaskSeamLineVoxels
1963 using LeafNodeType =
typename TreeType::LeafNodeType;
1964 using BoolTreeType =
typename TreeType::template ValueConverter<bool>::Type;
1966 MaskSeamLineVoxels(
const std::vector<LeafNodeType*>& signFlagsLeafNodes,
1967 const TreeType& signFlagsTree,
1969 : mSignFlagsNodes(signFlagsLeafNodes.data())
1970 , mSignFlagsTree(&signFlagsTree)
1976 MaskSeamLineVoxels(MaskSeamLineVoxels& rhs, tbb::split)
1977 : mSignFlagsNodes(rhs.mSignFlagsNodes)
1978 , mSignFlagsTree(rhs.mSignFlagsTree)
1984 void join(MaskSeamLineVoxels& rhs) { mMask->merge(*rhs.mMask); }
1986 void operator()(
const tbb::blocked_range<size_t>& range)
1988 using ValueOnCIter =
typename LeafNodeType::ValueOnCIter;
1989 using ValueType =
typename LeafNodeType::ValueType;
1991 tree::ValueAccessor<const TreeType> signFlagsAcc(*mSignFlagsTree);
1992 tree::ValueAccessor<BoolTreeType> maskAcc(*mMask);
1995 for (
size_t n = range.begin(), N = range.end(); n != N; ++n) {
1997 LeafNodeType& signFlagsNode = *mSignFlagsNodes[n];
2000 for (ValueOnCIter it = signFlagsNode.cbeginValueOn(); it; ++it) {
2002 const ValueType flags = it.getValue();
2004 if (!(flags & SEAM) && (flags & EDGES)) {
2006 ijk = it.getCoord();
2008 bool isSeamLineVoxel =
false;
2010 if (flags & XEDGE) {
2012 isSeamLineVoxel = (signFlagsAcc.getValue(ijk) & SEAM);
2014 isSeamLineVoxel = isSeamLineVoxel || (signFlagsAcc.getValue(ijk) & SEAM);
2016 isSeamLineVoxel = isSeamLineVoxel || (signFlagsAcc.getValue(ijk) & SEAM);
2020 if (!isSeamLineVoxel && flags & YEDGE) {
2022 isSeamLineVoxel = isSeamLineVoxel || (signFlagsAcc.getValue(ijk) & SEAM);
2024 isSeamLineVoxel = isSeamLineVoxel || (signFlagsAcc.getValue(ijk) & SEAM);
2026 isSeamLineVoxel = isSeamLineVoxel || (signFlagsAcc.getValue(ijk) & SEAM);
2030 if (!isSeamLineVoxel && flags & ZEDGE) {
2032 isSeamLineVoxel = isSeamLineVoxel || (signFlagsAcc.getValue(ijk) & SEAM);
2034 isSeamLineVoxel = isSeamLineVoxel || (signFlagsAcc.getValue(ijk) & SEAM);
2036 isSeamLineVoxel = isSeamLineVoxel || (signFlagsAcc.getValue(ijk) & SEAM);
2040 if (isSeamLineVoxel) {
2041 maskAcc.setValue(it.getCoord(),
true);
2050 LeafNodeType *
const *
const mSignFlagsNodes;
2051 TreeType
const *
const mSignFlagsTree;
2052 BoolTreeType mTempMask;
2053 BoolTreeType *
const mMask;
2057template<
typename SignDataTreeType>
2059markSeamLineData(SignDataTreeType& signFlagsTree,
const SignDataTreeType& refSignFlagsTree)
2061 using SignDataType =
typename SignDataTreeType::ValueType;
2062 using SignDataLeafNodeType =
typename SignDataTreeType::LeafNodeType;
2063 using BoolTreeType =
typename SignDataTreeType::template ValueConverter<bool>::Type;
2065 std::vector<SignDataLeafNodeType*> signFlagsLeafNodes;
2066 signFlagsTree.getNodes(signFlagsLeafNodes);
2068 const tbb::blocked_range<size_t> nodeRange(0, signFlagsLeafNodes.size());
2070 tbb::parallel_for(nodeRange,
2071 SetSeamLineFlags<SignDataTreeType>(signFlagsLeafNodes, refSignFlagsTree));
2073 BoolTreeType seamLineMaskTree(
false);
2075 MaskSeamLineVoxels<SignDataTreeType>
2076 maskSeamLine(signFlagsLeafNodes, signFlagsTree, seamLineMaskTree);
2078 tbb::parallel_reduce(nodeRange, maskSeamLine);
2080 tbb::parallel_for(nodeRange,
2081 TransferSeamLineFlags<BoolTreeType, SignDataType>(signFlagsLeafNodes, seamLineMaskTree));
2088template <
typename InputGr
idType>
2089struct MergeVoxelRegions
2091 using InputTreeType =
typename InputGridType::TreeType;
2092 using InputLeafNodeType =
typename InputTreeType::LeafNodeType;
2093 using InputValueType =
typename InputLeafNodeType::ValueType;
2095 using FloatTreeType =
typename InputTreeType::template ValueConverter<float>::Type;
2096 using FloatLeafNodeType =
typename FloatTreeType::LeafNodeType;
2097 using FloatGridType = Grid<FloatTreeType>;
2099 using Int16TreeType =
typename InputTreeType::template ValueConverter<Int16>::Type;
2100 using Int16LeafNodeType =
typename Int16TreeType::LeafNodeType;
2102 using Index32TreeType =
typename InputTreeType::template ValueConverter<Index32>::Type;
2103 using Index32LeafNodeType =
typename Index32TreeType::LeafNodeType;
2105 using BoolTreeType =
typename InputTreeType::template ValueConverter<bool>::Type;
2106 using BoolLeafNodeType =
typename BoolTreeType::LeafNodeType;
2108 MergeVoxelRegions(
const InputGridType& inputGrid,
2109 const Index32TreeType& pointIndexTree,
2110 const std::vector<Index32LeafNodeType*>& pointIndexLeafNodes,
2111 const std::vector<Int16LeafNodeType*>& signFlagsLeafNodes,
2114 bool invertSurfaceOrientation);
2116 void setSpatialAdaptivity(
const FloatGridType& grid)
2118 mSpatialAdaptivityTree = &grid.tree();
2119 mSpatialAdaptivityTransform = &grid.transform();
2122 void setAdaptivityMask(
const BoolTreeType& mask)
2127 void setRefSignFlagsData(
const Int16TreeType& signFlagsData,
float internalAdaptivity)
2129 mRefSignFlagsTree = &signFlagsData;
2130 mInternalAdaptivity = internalAdaptivity;
2133 void operator()(
const tbb::blocked_range<size_t>&)
const;
2136 InputTreeType
const *
const mInputTree;
2137 math::Transform
const *
const mInputTransform;
2139 Index32TreeType
const *
const mPointIndexTree;
2140 Index32LeafNodeType *
const *
const mPointIndexNodes;
2141 Int16LeafNodeType
const *
const *
const mSignFlagsNodes;
2143 InputValueType mIsovalue;
2144 float mSurfaceAdaptivity, mInternalAdaptivity;
2145 bool mInvertSurfaceOrientation;
2147 FloatTreeType
const * mSpatialAdaptivityTree;
2148 BoolTreeType
const * mMaskTree;
2149 Int16TreeType
const * mRefSignFlagsTree;
2150 math::Transform
const * mSpatialAdaptivityTransform;
2154template <
typename InputGr
idType>
2155MergeVoxelRegions<InputGridType>::MergeVoxelRegions(
2156 const InputGridType& inputGrid,
2157 const Index32TreeType& pointIndexTree,
2158 const std::vector<Index32LeafNodeType*>& pointIndexLeafNodes,
2159 const std::vector<Int16LeafNodeType*>& signFlagsLeafNodes,
2162 bool invertSurfaceOrientation)
2163 : mInputTree(&inputGrid.tree())
2164 , mInputTransform(&inputGrid.transform())
2165 , mPointIndexTree(&pointIndexTree)
2166 , mPointIndexNodes(pointIndexLeafNodes.data())
2167 , mSignFlagsNodes(signFlagsLeafNodes.data())
2169 , mSurfaceAdaptivity(adaptivity)
2170 , mInternalAdaptivity(adaptivity)
2171 , mInvertSurfaceOrientation(invertSurfaceOrientation)
2172 , mSpatialAdaptivityTree(nullptr)
2173 , mMaskTree(nullptr)
2174 , mRefSignFlagsTree(nullptr)
2175 , mSpatialAdaptivityTransform(nullptr)
2180template <
typename InputGr
idType>
2182MergeVoxelRegions<InputGridType>::operator()(
const tbb::blocked_range<size_t>& range)
const
2184 using Vec3sType = math::Vec3<float>;
2185 using Vec3sLeafNodeType =
typename InputLeafNodeType::template ValueConverter<Vec3sType>::Type;
2187 using InputTreeAccessor = tree::ValueAccessor<const InputTreeType>;
2188 using FloatTreeAccessor = tree::ValueAccessor<const FloatTreeType>;
2189 using Index32TreeAccessor = tree::ValueAccessor<const Index32TreeType>;
2190 using Int16TreeAccessor = tree::ValueAccessor<const Int16TreeType>;
2191 using BoolTreeAccessor = tree::ValueAccessor<const BoolTreeType>;
2193 std::unique_ptr<FloatTreeAccessor> spatialAdaptivityAcc;
2194 if (mSpatialAdaptivityTree && mSpatialAdaptivityTransform) {
2195 spatialAdaptivityAcc.reset(
new FloatTreeAccessor(*mSpatialAdaptivityTree));
2198 std::unique_ptr<BoolTreeAccessor> maskAcc;
2200 maskAcc.reset(
new BoolTreeAccessor(*mMaskTree));
2203 std::unique_ptr<Int16TreeAccessor> refSignFlagsAcc;
2204 if (mRefSignFlagsTree) {
2205 refSignFlagsAcc.reset(
new Int16TreeAccessor(*mRefSignFlagsTree));
2208 InputTreeAccessor inputAcc(*mInputTree);
2209 Index32TreeAccessor pointIndexAcc(*mPointIndexTree);
2211 BoolLeafNodeType mask;
2213 const bool invertGradientDir = mInvertSurfaceOrientation || isBoolValue<InputValueType>();
2214 std::unique_ptr<Vec3sLeafNodeType> gradientNode;
2217 const int LeafDim = InputLeafNodeType::DIM;
2219 for (
size_t n = range.begin(), N = range.end(); n != N; ++n) {
2221 mask.setValuesOff();
2223 const Int16LeafNodeType& signFlagsNode = *mSignFlagsNodes[n];
2224 Index32LeafNodeType& pointIndexNode = *mPointIndexNodes[n];
2226 const Coord& origin = pointIndexNode.origin();
2228 end[0] = origin[0] + LeafDim;
2229 end[1] = origin[1] + LeafDim;
2230 end[2] = origin[2] + LeafDim;
2234 const BoolLeafNodeType* maskLeaf = maskAcc->probeConstLeaf(origin);
2235 if (maskLeaf !=
nullptr) {
2236 for (
typename BoolLeafNodeType::ValueOnCIter it = maskLeaf->cbeginValueOn();
2239 mask.setActiveState(it.getCoord() & ~1u,
true);
2244 float adaptivity = (refSignFlagsAcc && !refSignFlagsAcc->probeConstLeaf(origin)) ?
2245 mInternalAdaptivity : mSurfaceAdaptivity;
2247 bool useGradients = adaptivity < 1.0f;
2250 FloatLeafNodeType adaptivityLeaf(origin, adaptivity);
2252 if (spatialAdaptivityAcc) {
2253 useGradients =
false;
2254 for (
Index offset = 0; offset < FloatLeafNodeType::NUM_VALUES; ++offset) {
2255 ijk = adaptivityLeaf.offsetToGlobalCoord(offset);
2256 ijk = mSpatialAdaptivityTransform->worldToIndexCellCentered(
2257 mInputTransform->indexToWorld(ijk));
2258 float weight = spatialAdaptivityAcc->getValue(ijk);
2259 float adaptivityValue = weight * adaptivity;
2260 if (adaptivityValue < 1.0f) useGradients =
true;
2261 adaptivityLeaf.setValueOnly(offset, adaptivityValue);
2266 for (
auto it = signFlagsNode.cbeginValueOn(); it; ++it) {
2267 const Int16 flags = it.getValue();
2268 const unsigned char signs =
static_cast<unsigned char>(SIGNS & int(flags));
2270 if ((flags & SEAM) || !sAdaptable[signs] || sEdgeGroupTable[signs][0] > 1) {
2272 mask.setActiveState(it.getCoord() & ~1u,
true);
2274 }
else if (flags & EDGES) {
2276 bool maskRegion =
false;
2278 ijk = it.getCoord();
2279 if (!pointIndexAcc.isValueOn(ijk)) maskRegion =
true;
2281 if (!maskRegion && flags & XEDGE) {
2283 if (!maskRegion && !pointIndexAcc.isValueOn(ijk)) maskRegion =
true;
2285 if (!maskRegion && !pointIndexAcc.isValueOn(ijk)) maskRegion =
true;
2287 if (!maskRegion && !pointIndexAcc.isValueOn(ijk)) maskRegion =
true;
2291 if (!maskRegion && flags & YEDGE) {
2293 if (!maskRegion && !pointIndexAcc.isValueOn(ijk)) maskRegion =
true;
2295 if (!maskRegion && !pointIndexAcc.isValueOn(ijk)) maskRegion =
true;
2297 if (!maskRegion && !pointIndexAcc.isValueOn(ijk)) maskRegion =
true;
2301 if (!maskRegion && flags & ZEDGE) {
2303 if (!maskRegion && !pointIndexAcc.isValueOn(ijk)) maskRegion =
true;
2305 if (!maskRegion && !pointIndexAcc.isValueOn(ijk)) maskRegion =
true;
2307 if (!maskRegion && !pointIndexAcc.isValueOn(ijk)) maskRegion =
true;
2312 mask.setActiveState(it.getCoord() & ~1u,
true);
2319 for (ijk[0] = origin[0]; ijk[0] < end[0]; ijk[0] += dim) {
2320 for (ijk[1] = origin[1]; ijk[1] < end[1]; ijk[1] += dim) {
2321 for (ijk[2] = origin[2]; ijk[2] < end[2]; ijk[2] += dim) {
2322 if (!mask.isValueOn(ijk) && isNonManifold(inputAcc, ijk, mIsovalue, dim)) {
2323 mask.setActiveState(ijk,
true);
2334 gradientNode->setValuesOff();
2336 gradientNode.reset(
new Vec3sLeafNodeType());
2339 for (
auto it = signFlagsNode.cbeginValueOn(); it; ++it) {
2340 ijk = it.getCoord();
2341 if (!mask.isValueOn(ijk & ~1u)) {
2345 if (invertGradientDir) {
2349 gradientNode->setValueOn(it.pos(), dir);
2356 for ( ; dim <= LeafDim; dim = dim << 1) {
2357 const unsigned coordMask = ~((dim << 1) - 1);
2358 for (ijk[0] = origin[0]; ijk[0] < end[0]; ijk[0] += dim) {
2359 for (ijk[1] = origin[1]; ijk[1] < end[1]; ijk[1] += dim) {
2360 for (ijk[2] = origin[2]; ijk[2] < end[2]; ijk[2] += dim) {
2362 adaptivity = adaptivityLeaf.getValue(ijk);
2364 if (mask.isValueOn(ijk)
2365 || isNonManifold(inputAcc, ijk, mIsovalue, dim)
2366 || (useGradients && !isMergable(*gradientNode, ijk, dim, adaptivity)))
2368 mask.setActiveState(ijk & coordMask,
true);
2370 mergeVoxels(pointIndexNode, ijk, dim, regionId++);
2384struct UniformPrimBuilder
2386 UniformPrimBuilder(): mIdx(0), mPolygonPool(nullptr) {}
2388 void init(
const size_t upperBound, PolygonPool& quadPool)
2390 mPolygonPool = &quadPool;
2391 mPolygonPool->resetQuads(upperBound);
2395 template<
typename IndexType>
2396 void addPrim(
const math::Vec4<IndexType>& verts,
bool reverse,
char flags = 0)
2399 mPolygonPool->quad(mIdx) = verts;
2401 Vec4I& quad = mPolygonPool->quad(mIdx);
2407 mPolygonPool->quadFlags(mIdx) = flags;
2413 mPolygonPool->trimQuads(mIdx);
2418 PolygonPool* mPolygonPool;
2423struct AdaptivePrimBuilder
2425 AdaptivePrimBuilder() : mQuadIdx(0), mTriangleIdx(0), mPolygonPool(nullptr) {}
2427 void init(
const size_t upperBound, PolygonPool& polygonPool)
2429 mPolygonPool = &polygonPool;
2430 mPolygonPool->resetQuads(upperBound);
2431 mPolygonPool->resetTriangles(upperBound);
2437 template<
typename IndexType>
2438 void addPrim(
const math::Vec4<IndexType>& verts,
bool reverse,
char flags = 0)
2440 if (verts[0] != verts[1] && verts[0] != verts[2] && verts[0] != verts[3]
2441 && verts[1] != verts[2] && verts[1] != verts[3] && verts[2] != verts[3]) {
2442 mPolygonPool->quadFlags(mQuadIdx) = flags;
2443 addQuad(verts, reverse);
2445 verts[0] == verts[3] &&
2446 verts[1] != verts[2] &&
2447 verts[1] != verts[0] &&
2448 verts[2] != verts[0]) {
2449 mPolygonPool->triangleFlags(mTriangleIdx) = flags;
2450 addTriangle(verts[0], verts[1], verts[2], reverse);
2452 verts[1] == verts[2] &&
2453 verts[0] != verts[3] &&
2454 verts[0] != verts[1] &&
2455 verts[3] != verts[1]) {
2456 mPolygonPool->triangleFlags(mTriangleIdx) = flags;
2457 addTriangle(verts[0], verts[1], verts[3], reverse);
2459 verts[0] == verts[1] &&
2460 verts[2] != verts[3] &&
2461 verts[2] != verts[0] &&
2462 verts[3] != verts[0]) {
2463 mPolygonPool->triangleFlags(mTriangleIdx) = flags;
2464 addTriangle(verts[0], verts[2], verts[3], reverse);
2466 verts[2] == verts[3] &&
2467 verts[0] != verts[1] &&
2468 verts[0] != verts[2] &&
2469 verts[1] != verts[2]) {
2470 mPolygonPool->triangleFlags(mTriangleIdx) = flags;
2471 addTriangle(verts[0], verts[1], verts[2], reverse);
2478 mPolygonPool->trimQuads(mQuadIdx,
true);
2479 mPolygonPool->trimTrinagles(mTriangleIdx,
true);
2484 template<
typename IndexType>
2485 void addQuad(
const math::Vec4<IndexType>& verts,
bool reverse)
2488 mPolygonPool->quad(mQuadIdx) = verts;
2490 Vec4I& quad = mPolygonPool->quad(mQuadIdx);
2499 void addTriangle(
unsigned v0,
unsigned v1,
unsigned v2,
bool reverse)
2501 Vec3I& prim = mPolygonPool->triangle(mTriangleIdx);
2515 size_t mQuadIdx, mTriangleIdx;
2516 PolygonPool *mPolygonPool;
2520template<
typename SignAccT,
typename IdxAccT,
typename PrimBuilder>
2523 bool invertSurfaceOrientation,
2526 const Vec3i& offsets,
2528 const SignAccT& signAcc,
2529 const IdxAccT& idxAcc,
2530 PrimBuilder& mesher)
2532 using IndexType =
typename IdxAccT::ValueType;
2535 const bool isActive = idxAcc.probeValue(ijk, v0);
2539 tag[0] = (flags & SEAM) ? POLYFLAG_FRACTURE_SEAM : 0;
2540 tag[1] = tag[0] | char(POLYFLAG_EXTERIOR);
2542 bool isInside = flags & INSIDE;
2544 isInside = invertSurfaceOrientation ? !isInside : isInside;
2547 math::Vec4<IndexType> quad(0,0,0,0);
2549 if (flags & XEDGE) {
2551 quad[0] = v0 + offsets[0];
2555 bool activeValues = idxAcc.probeValue(coord, quad[1]);
2556 uint8_t cell = uint8_t(SIGNS & signAcc.getValue(coord));
2557 quad[1] += sEdgeGroupTable[cell][0] > 1 ? sEdgeGroupTable[cell][5] - 1 : 0;
2561 activeValues = activeValues && idxAcc.probeValue(coord, quad[2]);
2562 cell = uint8_t(SIGNS & signAcc.getValue(coord));
2563 quad[2] += sEdgeGroupTable[cell][0] > 1 ? sEdgeGroupTable[cell][7] - 1 : 0;
2567 activeValues = activeValues && idxAcc.probeValue(coord, quad[3]);
2568 cell = uint8_t(SIGNS & signAcc.getValue(coord));
2569 quad[3] += sEdgeGroupTable[cell][0] > 1 ? sEdgeGroupTable[cell][3] - 1 : 0;
2572 mesher.addPrim(quad, isInside, tag[
bool(refFlags & XEDGE)]);
2579 if (flags & YEDGE) {
2581 quad[0] = v0 + offsets[1];
2585 bool activeValues = idxAcc.probeValue(coord, quad[1]);
2586 uint8_t cell = uint8_t(SIGNS & signAcc.getValue(coord));
2587 quad[1] += sEdgeGroupTable[cell][0] > 1 ? sEdgeGroupTable[cell][12] - 1 : 0;
2591 activeValues = activeValues && idxAcc.probeValue(coord, quad[2]);
2592 cell = uint8_t(SIGNS & signAcc.getValue(coord));
2593 quad[2] += sEdgeGroupTable[cell][0] > 1 ? sEdgeGroupTable[cell][11] - 1 : 0;
2597 activeValues = activeValues && idxAcc.probeValue(coord, quad[3]);
2598 cell = uint8_t(SIGNS & signAcc.getValue(coord));
2599 quad[3] += sEdgeGroupTable[cell][0] > 1 ? sEdgeGroupTable[cell][10] - 1 : 0;
2602 mesher.addPrim(quad, isInside, tag[
bool(refFlags & YEDGE)]);
2609 if (flags & ZEDGE) {
2611 quad[0] = v0 + offsets[2];
2615 bool activeValues = idxAcc.probeValue(coord, quad[1]);
2616 uint8_t cell = uint8_t(SIGNS & signAcc.getValue(coord));
2617 quad[1] += sEdgeGroupTable[cell][0] > 1 ? sEdgeGroupTable[cell][8] - 1 : 0;
2621 activeValues = activeValues && idxAcc.probeValue(coord, quad[2]);
2622 cell = uint8_t(SIGNS & signAcc.getValue(coord));
2623 quad[2] += sEdgeGroupTable[cell][0] > 1 ? sEdgeGroupTable[cell][6] - 1 : 0;
2627 activeValues = activeValues && idxAcc.probeValue(coord, quad[3]);
2628 cell = uint8_t(SIGNS & signAcc.getValue(coord));
2629 quad[3] += sEdgeGroupTable[cell][0] > 1 ? sEdgeGroupTable[cell][2] - 1 : 0;
2632 mesher.addPrim(quad, !isInside, tag[
bool(refFlags & ZEDGE)]);
2641template<
typename InputTreeType>
2642struct MaskTileBorders
2644 using InputValueType =
typename InputTreeType::ValueType;
2645 using BoolTreeType =
typename InputTreeType::template ValueConverter<bool>::Type;
2648 MaskTileBorders(
const InputTreeType& inputTree, InputValueType iso,
2649 BoolTreeType& mask,
const Vec4i* tileArray)
2650 : mInputTree(&inputTree)
2654 , mTileArray(tileArray)
2658 MaskTileBorders(MaskTileBorders& rhs, tbb::split)
2659 : mInputTree(rhs.mInputTree)
2660 , mIsovalue(rhs.mIsovalue)
2663 , mTileArray(rhs.mTileArray)
2667 void join(MaskTileBorders& rhs) { mMask->merge(*rhs.mMask); }
2669 void operator()(
const tbb::blocked_range<size_t>&);
2672 InputTreeType
const *
const mInputTree;
2673 InputValueType
const mIsovalue;
2674 BoolTreeType mTempMask;
2675 BoolTreeType *
const mMask;
2676 Vec4i const *
const mTileArray;
2680template<
typename InputTreeType>
2682MaskTileBorders<InputTreeType>::operator()(
const tbb::blocked_range<size_t>& range)
2684 tree::ValueAccessor<const InputTreeType> inputTreeAcc(*mInputTree);
2689 for (
size_t n = range.begin(), N = range.end(); n != N; ++n) {
2691 const Vec4i& tile = mTileArray[n];
2693 bbox.min()[0] = tile[0];
2694 bbox.min()[1] = tile[1];
2695 bbox.min()[2] = tile[2];
2696 bbox.max() = bbox.min();
2697 bbox.max().offset(tile[3]);
2699 InputValueType
value = mInputTree->background();
2701 const bool isInside = isInsideValue(inputTreeAcc.getValue(bbox.min()), mIsovalue);
2702 const int valueDepth = inputTreeAcc.getValueDepth(bbox.min());
2710 bool processRegion =
true;
2711 if (valueDepth >= inputTreeAcc.getValueDepth(nijk)) {
2712 processRegion = isInside != isInsideValue(inputTreeAcc.getValue(nijk), mIsovalue);
2715 if (processRegion) {
2718 region.min()[0] = region.max()[0] = ijk[0];
2719 mMask->fill(region,
false);
2726 processRegion =
true;
2727 if (valueDepth >= inputTreeAcc.getValueDepth(ijk)) {
2728 processRegion = (!inputTreeAcc.probeValue(ijk,
value)
2729 && isInside != isInsideValue(
value, mIsovalue));
2732 if (processRegion) {
2735 region.min()[0] = region.max()[0] = ijk[0];
2736 mMask->fill(region,
false);
2746 processRegion =
true;
2747 if (valueDepth >= inputTreeAcc.getValueDepth(nijk)) {
2748 processRegion = isInside != isInsideValue(inputTreeAcc.getValue(nijk), mIsovalue);
2751 if (processRegion) {
2754 region.min()[1] = region.max()[1] = ijk[1];
2755 mMask->fill(region,
false);
2762 processRegion =
true;
2763 if (valueDepth >= inputTreeAcc.getValueDepth(ijk)) {
2764 processRegion = (!inputTreeAcc.probeValue(ijk,
value)
2765 && isInside != isInsideValue(
value, mIsovalue));
2768 if (processRegion) {
2771 region.min()[1] = region.max()[1] = ijk[1];
2772 mMask->fill(region,
false);
2782 processRegion =
true;
2783 if (valueDepth >= inputTreeAcc.getValueDepth(nijk)) {
2784 processRegion = isInside != isInsideValue(inputTreeAcc.getValue(nijk), mIsovalue);
2787 if (processRegion) {
2790 region.min()[2] = region.max()[2] = ijk[2];
2791 mMask->fill(region,
false);
2797 processRegion =
true;
2798 if (valueDepth >= inputTreeAcc.getValueDepth(ijk)) {
2799 processRegion = (!inputTreeAcc.probeValue(ijk,
value)
2800 && isInside != isInsideValue(
value, mIsovalue));
2803 if (processRegion) {
2806 region.min()[2] = region.max()[2] = ijk[2];
2807 mMask->fill(region,
false);
2813template<
typename InputTreeType>
2815maskActiveTileBorders(
const InputTreeType& inputTree,
typename InputTreeType::ValueType iso,
2816 typename InputTreeType::template ValueConverter<bool>::Type& mask)
2818 typename InputTreeType::ValueOnCIter tileIter(inputTree);
2819 tileIter.setMaxDepth(InputTreeType::ValueOnCIter::LEAF_DEPTH - 1);
2821 size_t tileCount = 0;
2822 for ( ; tileIter; ++tileIter) {
2826 if (tileCount > 0) {
2827 std::unique_ptr<Vec4i[]> tiles(
new Vec4i[tileCount]);
2832 tileIter = inputTree.cbeginValueOn();
2833 tileIter.setMaxDepth(InputTreeType::ValueOnCIter::LEAF_DEPTH - 1);
2835 for (; tileIter; ++tileIter) {
2836 Vec4i& tile = tiles[index++];
2837 tileIter.getBoundingBox(bbox);
2838 tile[0] = bbox.min()[0];
2839 tile[1] = bbox.min()[1];
2840 tile[2] = bbox.min()[2];
2841 tile[3] = bbox.max()[0] - bbox.min()[0];
2844 MaskTileBorders<InputTreeType> op(inputTree, iso, mask, tiles.get());
2845 tbb::parallel_reduce(tbb::blocked_range<size_t>(0, tileCount), op);
2857 PointListCopy(
const PointList& pointsIn, std::vector<Vec3s>& pointsOut)
2858 : mPointsIn(pointsIn) , mPointsOut(pointsOut)
2862 void operator()(
const tbb::blocked_range<size_t>& range)
const
2864 for (
size_t n = range.begin(); n < range.end(); ++n) {
2865 mPointsOut[n] = mPointsIn[n];
2871 std::vector<Vec3s>& mPointsOut;
2879struct LeafNodeVoxelOffsets
2881 using IndexVector = std::vector<Index>;
2883 template<
typename LeafNodeType>
2884 void constructOffsetList();
2887 const IndexVector& core()
const {
return mCore; }
2891 const IndexVector& minX()
const {
return mMinX; }
2894 const IndexVector& maxX()
const {
return mMaxX; }
2898 const IndexVector& minY()
const {
return mMinY; }
2901 const IndexVector& maxY()
const {
return mMaxY; }
2905 const IndexVector& minZ()
const {
return mMinZ; }
2908 const IndexVector& maxZ()
const {
return mMaxZ; }
2912 const IndexVector& internalNeighborsX()
const {
return mInternalNeighborsX; }
2915 const IndexVector& internalNeighborsY()
const {
return mInternalNeighborsY; }
2918 const IndexVector& internalNeighborsZ()
const {
return mInternalNeighborsZ; }
2922 IndexVector mCore, mMinX, mMaxX, mMinY, mMaxY, mMinZ, mMaxZ,
2923 mInternalNeighborsX, mInternalNeighborsY, mInternalNeighborsZ;
2927template<
typename LeafNodeType>
2929LeafNodeVoxelOffsets::constructOffsetList()
2933 mCore.reserve((LeafNodeType::DIM - 2) * (LeafNodeType::DIM - 2));
2935 for (
Index x = 1; x < (LeafNodeType::DIM - 1); ++x) {
2936 const Index offsetX = x << (2 * LeafNodeType::LOG2DIM);
2937 for (
Index y = 1; y < (LeafNodeType::DIM - 1); ++y) {
2938 const Index offsetXY = offsetX + (y << LeafNodeType::LOG2DIM);
2939 for (
Index z = 1; z < (LeafNodeType::DIM - 1); ++z) {
2940 mCore.push_back(offsetXY + z);
2946 mInternalNeighborsX.clear();
2947 mInternalNeighborsX.reserve(LeafNodeType::SIZE - (LeafNodeType::DIM * LeafNodeType::DIM));
2949 for (
Index x = 0; x < (LeafNodeType::DIM - 1); ++x) {
2950 const Index offsetX = x << (2 * LeafNodeType::LOG2DIM);
2951 for (
Index y = 0; y < LeafNodeType::DIM; ++y) {
2952 const Index offsetXY = offsetX + (y << LeafNodeType::LOG2DIM);
2953 for (
Index z = 0; z < LeafNodeType::DIM; ++z) {
2954 mInternalNeighborsX.push_back(offsetXY + z);
2960 mInternalNeighborsY.clear();
2961 mInternalNeighborsY.reserve(LeafNodeType::SIZE - (LeafNodeType::DIM * LeafNodeType::DIM));
2963 for (
Index x = 0; x < LeafNodeType::DIM; ++x) {
2964 const Index offsetX = x << (2 * LeafNodeType::LOG2DIM);
2965 for (
Index y = 0; y < (LeafNodeType::DIM - 1); ++y) {
2966 const Index offsetXY = offsetX + (y << LeafNodeType::LOG2DIM);
2967 for (
Index z = 0; z < LeafNodeType::DIM; ++z) {
2968 mInternalNeighborsY.push_back(offsetXY + z);
2974 mInternalNeighborsZ.clear();
2975 mInternalNeighborsZ.reserve(LeafNodeType::SIZE - (LeafNodeType::DIM * LeafNodeType::DIM));
2977 for (
Index x = 0; x < LeafNodeType::DIM; ++x) {
2978 const Index offsetX = x << (2 * LeafNodeType::LOG2DIM);
2979 for (
Index y = 0; y < LeafNodeType::DIM; ++y) {
2980 const Index offsetXY = offsetX + (y << LeafNodeType::LOG2DIM);
2981 for (
Index z = 0; z < (LeafNodeType::DIM - 1); ++z) {
2982 mInternalNeighborsZ.push_back(offsetXY + z);
2989 mMinX.reserve(LeafNodeType::DIM * LeafNodeType::DIM);
2991 for (
Index y = 0; y < LeafNodeType::DIM; ++y) {
2992 const Index offsetXY = (y << LeafNodeType::LOG2DIM);
2993 for (
Index z = 0; z < LeafNodeType::DIM; ++z) {
2994 mMinX.push_back(offsetXY + z);
3001 mMaxX.reserve(LeafNodeType::DIM * LeafNodeType::DIM);
3003 const Index offsetX = (LeafNodeType::DIM - 1) << (2 * LeafNodeType::LOG2DIM);
3004 for (
Index y = 0; y < LeafNodeType::DIM; ++y) {
3005 const Index offsetXY = offsetX + (y << LeafNodeType::LOG2DIM);
3006 for (
Index z = 0; z < LeafNodeType::DIM; ++z) {
3007 mMaxX.push_back(offsetXY + z);
3014 mMinY.reserve(LeafNodeType::DIM * LeafNodeType::DIM);
3016 for (
Index x = 0; x < LeafNodeType::DIM; ++x) {
3017 const Index offsetX = x << (2 * LeafNodeType::LOG2DIM);
3018 for (
Index z = 0; z < (LeafNodeType::DIM - 1); ++z) {
3019 mMinY.push_back(offsetX + z);
3026 mMaxY.reserve(LeafNodeType::DIM * LeafNodeType::DIM);
3028 const Index offsetY = (LeafNodeType::DIM - 1) << LeafNodeType::LOG2DIM;
3029 for (
Index x = 0; x < LeafNodeType::DIM; ++x) {
3030 const Index offsetX = x << (2 * LeafNodeType::LOG2DIM);
3031 for (
Index z = 0; z < (LeafNodeType::DIM - 1); ++z) {
3032 mMaxY.push_back(offsetX + offsetY + z);
3039 mMinZ.reserve(LeafNodeType::DIM * LeafNodeType::DIM);
3041 for (
Index x = 0; x < LeafNodeType::DIM; ++x) {
3042 const Index offsetX = x << (2 * LeafNodeType::LOG2DIM);
3043 for (
Index y = 0; y < LeafNodeType::DIM; ++y) {
3044 const Index offsetXY = offsetX + (y << LeafNodeType::LOG2DIM);
3045 mMinZ.push_back(offsetXY);
3052 mMaxZ.reserve(LeafNodeType::DIM * LeafNodeType::DIM);
3054 for (
Index x = 0; x < LeafNodeType::DIM; ++x) {
3055 const Index offsetX = x << (2 * LeafNodeType::LOG2DIM);
3056 for (
Index y = 0; y < LeafNodeType::DIM; ++y) {
3057 const Index offsetXY = offsetX + (y << LeafNodeType::LOG2DIM);
3058 mMaxZ.push_back(offsetXY + (LeafNodeType::DIM - 1));
3069template<
typename AccessorT,
int _AXIS>
3070struct VoxelEdgeAccessor {
3072 enum { AXIS = _AXIS };
3075 VoxelEdgeAccessor(AccessorT& _acc) : acc(_acc) {}
3077 void set(Coord ijk) {
3079 acc.setActiveState(ijk);
3081 acc.setActiveState(ijk);
3083 acc.setActiveState(ijk);
3085 acc.setActiveState(ijk);
3086 }
else if (_AXIS == 1) {
3087 acc.setActiveState(ijk);
3089 acc.setActiveState(ijk);
3091 acc.setActiveState(ijk);
3093 acc.setActiveState(ijk);
3095 acc.setActiveState(ijk);
3097 acc.setActiveState(ijk);
3099 acc.setActiveState(ijk);
3101 acc.setActiveState(ijk);
3110template<
typename VoxelEdgeAcc,
typename LeafNode>
3112evalInternalVoxelEdges(VoxelEdgeAcc& edgeAcc,
const LeafNode& leafnode,
3113 const LeafNodeVoxelOffsets& voxels,
const typename LeafNode::ValueType iso)
3116 const std::vector<Index>* offsets = &voxels.internalNeighborsZ();
3118 if (VoxelEdgeAcc::AXIS == 0) {
3119 nvo = LeafNode::DIM * LeafNode::DIM;
3120 offsets = &voxels.internalNeighborsX();
3121 }
else if (VoxelEdgeAcc::AXIS == 1) {
3122 nvo = LeafNode::DIM;
3123 offsets = &voxels.internalNeighborsY();
3126 for (
size_t n = 0, N = offsets->size(); n < N; ++n) {
3127 const Index& pos = (*offsets)[n];
3128 bool isActive = leafnode.isValueOn(pos) || leafnode.isValueOn(pos + nvo);
3129 if (isActive && (isInsideValue(leafnode.getValue(pos), iso) !=
3130 isInsideValue(leafnode.getValue(pos + nvo), iso))) {
3131 edgeAcc.set(leafnode.offsetToGlobalCoord(pos));
3140template<
typename LeafNode,
typename TreeAcc,
typename VoxelEdgeAcc>
3142evalExtrenalVoxelEdges(VoxelEdgeAcc& edgeAcc, TreeAcc& acc,
const LeafNode& lhsNode,
3143 const LeafNodeVoxelOffsets& voxels,
const typename LeafNode::ValueType iso)
3145 const std::vector<Index>* lhsOffsets = &voxels.maxX();
3146 const std::vector<Index>* rhsOffsets = &voxels.minX();
3147 Coord ijk = lhsNode.origin();
3149 if (VoxelEdgeAcc::AXIS == 0) {
3150 ijk[0] += LeafNode::DIM;
3151 }
else if (VoxelEdgeAcc::AXIS == 1) {
3152 ijk[1] += LeafNode::DIM;
3153 lhsOffsets = &voxels.maxY();
3154 rhsOffsets = &voxels.minY();
3155 }
else if (VoxelEdgeAcc::AXIS == 2) {
3156 ijk[2] += LeafNode::DIM;
3157 lhsOffsets = &voxels.maxZ();
3158 rhsOffsets = &voxels.minZ();
3161 typename LeafNode::ValueType
value;
3162 const LeafNode* rhsNodePt = acc.probeConstLeaf(ijk);
3165 for (
size_t n = 0, N = lhsOffsets->size(); n < N; ++n) {
3166 const Index& pos = (*lhsOffsets)[n];
3167 bool isActive = lhsNode.isValueOn(pos) || rhsNodePt->isValueOn((*rhsOffsets)[n]);
3168 if (isActive && (isInsideValue(lhsNode.getValue(pos), iso) !=
3169 isInsideValue(rhsNodePt->getValue((*rhsOffsets)[n]), iso))) {
3170 edgeAcc.set(lhsNode.offsetToGlobalCoord(pos));
3173 }
else if (!acc.probeValue(ijk,
value)) {
3174 const bool inside = isInsideValue(
value, iso);
3175 for (
size_t n = 0, N = lhsOffsets->size(); n < N; ++n) {
3176 const Index& pos = (*lhsOffsets)[n];
3177 if (lhsNode.isValueOn(pos) && (inside != isInsideValue(lhsNode.getValue(pos), iso))) {
3178 edgeAcc.set(lhsNode.offsetToGlobalCoord(pos));
3188template<
typename LeafNode,
typename TreeAcc,
typename VoxelEdgeAcc>
3190evalExtrenalVoxelEdgesInv(VoxelEdgeAcc& edgeAcc, TreeAcc& acc,
const LeafNode& leafnode,
3191 const LeafNodeVoxelOffsets& voxels,
const typename LeafNode::ValueType iso)
3193 Coord ijk = leafnode.origin();
3194 if (VoxelEdgeAcc::AXIS == 0) --ijk[0];
3195 else if (VoxelEdgeAcc::AXIS == 1) --ijk[1];
3196 else if (VoxelEdgeAcc::AXIS == 2) --ijk[2];
3198 typename LeafNode::ValueType
value;
3199 if (!acc.probeConstLeaf(ijk) && !acc.probeValue(ijk,
value)) {
3201 const std::vector<Index>* offsets = &voxels.internalNeighborsX();
3202 if (VoxelEdgeAcc::AXIS == 1) offsets = &voxels.internalNeighborsY();
3203 else if (VoxelEdgeAcc::AXIS == 2) offsets = &voxels.internalNeighborsZ();
3205 const bool inside = isInsideValue(
value, iso);
3206 for (
size_t n = 0, N = offsets->size(); n < N; ++n) {
3208 const Index& pos = (*offsets)[n];
3209 if (leafnode.isValueOn(pos)
3210 && (inside != isInsideValue(leafnode.getValue(pos), iso)))
3212 ijk = leafnode.offsetToGlobalCoord(pos);
3213 if (VoxelEdgeAcc::AXIS == 0) --ijk[0];
3214 else if (VoxelEdgeAcc::AXIS == 1) --ijk[1];
3215 else if (VoxelEdgeAcc::AXIS == 2) --ijk[2];
3225template<
typename InputTreeType>
3226struct IdentifyIntersectingVoxels
3228 using InputLeafNodeType =
typename InputTreeType::LeafNodeType;
3229 using InputValueType =
typename InputLeafNodeType::ValueType;
3231 using BoolTreeType =
typename InputTreeType::template ValueConverter<bool>::Type;
3233 IdentifyIntersectingVoxels(
3234 const InputTreeType& inputTree,
3235 const std::vector<const InputLeafNodeType*>& inputLeafNodes,
3236 BoolTreeType& intersectionTree,
3237 InputValueType iso);
3239 IdentifyIntersectingVoxels(IdentifyIntersectingVoxels&, tbb::split);
3240 void operator()(
const tbb::blocked_range<size_t>&);
3241 void join(
const IdentifyIntersectingVoxels& rhs) {
3242 mIntersectionAccessor.tree().merge(rhs.mIntersectionAccessor.tree());
3246 tree::ValueAccessor<const InputTreeType> mInputAccessor;
3247 InputLeafNodeType
const *
const *
const mInputNodes;
3249 BoolTreeType mIntersectionTree;
3250 tree::ValueAccessor<BoolTreeType> mIntersectionAccessor;
3252 LeafNodeVoxelOffsets mOffsetData;
3253 const LeafNodeVoxelOffsets* mOffsets;
3255 InputValueType mIsovalue;
3259template<
typename InputTreeType>
3260IdentifyIntersectingVoxels<InputTreeType>::IdentifyIntersectingVoxels(
3261 const InputTreeType& inputTree,
3262 const std::vector<const InputLeafNodeType*>& inputLeafNodes,
3263 BoolTreeType& intersectionTree,
3265 : mInputAccessor(inputTree)
3266 , mInputNodes(inputLeafNodes.data())
3267 , mIntersectionTree(false)
3268 , mIntersectionAccessor(intersectionTree)
3270 , mOffsets(&mOffsetData)
3273 mOffsetData.constructOffsetList<InputLeafNodeType>();
3277template<
typename InputTreeType>
3278IdentifyIntersectingVoxels<InputTreeType>::IdentifyIntersectingVoxels(
3279 IdentifyIntersectingVoxels& rhs, tbb::split)
3280 : mInputAccessor(rhs.mInputAccessor.tree())
3281 , mInputNodes(rhs.mInputNodes)
3282 , mIntersectionTree(false)
3283 , mIntersectionAccessor(mIntersectionTree)
3285 , mOffsets(rhs.mOffsets)
3286 , mIsovalue(rhs.mIsovalue)
3291template<
typename InputTreeType>
3293IdentifyIntersectingVoxels<InputTreeType>::operator()(
const tbb::blocked_range<size_t>& range)
3295 VoxelEdgeAccessor<tree::ValueAccessor<BoolTreeType>, 0> xEdgeAcc(mIntersectionAccessor);
3296 VoxelEdgeAccessor<tree::ValueAccessor<BoolTreeType>, 1> yEdgeAcc(mIntersectionAccessor);
3297 VoxelEdgeAccessor<tree::ValueAccessor<BoolTreeType>, 2> zEdgeAcc(mIntersectionAccessor);
3299 for (
size_t n = range.begin(); n != range.end(); ++n) {
3301 const InputLeafNodeType& node = *mInputNodes[n];
3304 evalInternalVoxelEdges(xEdgeAcc, node, *mOffsets, mIsovalue);
3306 evalInternalVoxelEdges(yEdgeAcc, node, *mOffsets, mIsovalue);
3308 evalInternalVoxelEdges(zEdgeAcc, node, *mOffsets, mIsovalue);
3311 evalExtrenalVoxelEdges(xEdgeAcc, mInputAccessor, node, *mOffsets, mIsovalue);
3313 evalExtrenalVoxelEdges(yEdgeAcc, mInputAccessor, node, *mOffsets, mIsovalue);
3315 evalExtrenalVoxelEdges(zEdgeAcc, mInputAccessor, node, *mOffsets, mIsovalue);
3321 evalExtrenalVoxelEdgesInv(xEdgeAcc, mInputAccessor, node, *mOffsets, mIsovalue);
3323 evalExtrenalVoxelEdgesInv(yEdgeAcc, mInputAccessor, node, *mOffsets, mIsovalue);
3325 evalExtrenalVoxelEdgesInv(zEdgeAcc, mInputAccessor, node, *mOffsets, mIsovalue);
3330template<
typename InputTreeType>
3332identifySurfaceIntersectingVoxels(
3333 typename InputTreeType::template ValueConverter<bool>::Type& intersectionTree,
3334 const InputTreeType& inputTree,
3335 typename InputTreeType::ValueType isovalue)
3337 using InputLeafNodeType =
typename InputTreeType::LeafNodeType;
3339 std::vector<const InputLeafNodeType*> inputLeafNodes;
3340 inputTree.getNodes(inputLeafNodes);
3342 IdentifyIntersectingVoxels<InputTreeType> op(
3343 inputTree, inputLeafNodes, intersectionTree, isovalue);
3345 tbb::parallel_reduce(tbb::blocked_range<size_t>(0, inputLeafNodes.size()), op);
3347 maskActiveTileBorders(inputTree, isovalue, intersectionTree);
3354template<
typename InputTreeType>
3355struct MaskIntersectingVoxels
3357 using InputLeafNodeType =
typename InputTreeType::LeafNodeType;
3358 using InputValueType =
typename InputLeafNodeType::ValueType;
3360 using BoolTreeType =
typename InputTreeType::template ValueConverter<bool>::Type;
3361 using BoolLeafNodeType =
typename BoolTreeType::LeafNodeType;
3363 MaskIntersectingVoxels(
3364 const InputTreeType& inputTree,
3365 const std::vector<BoolLeafNodeType*>& nodes,
3366 BoolTreeType& intersectionTree,
3367 InputValueType iso);
3369 MaskIntersectingVoxels(MaskIntersectingVoxels&, tbb::split);
3370 void operator()(
const tbb::blocked_range<size_t>&);
3371 void join(
const MaskIntersectingVoxels& rhs) {
3372 mIntersectionAccessor.tree().merge(rhs.mIntersectionAccessor.tree());
3376 tree::ValueAccessor<const InputTreeType> mInputAccessor;
3377 BoolLeafNodeType
const *
const *
const mNodes;
3379 BoolTreeType mIntersectionTree;
3380 tree::ValueAccessor<BoolTreeType> mIntersectionAccessor;
3382 InputValueType mIsovalue;
3386template<
typename InputTreeType>
3387MaskIntersectingVoxels<InputTreeType>::MaskIntersectingVoxels(
3388 const InputTreeType& inputTree,
3389 const std::vector<BoolLeafNodeType*>& nodes,
3390 BoolTreeType& intersectionTree,
3392 : mInputAccessor(inputTree)
3393 , mNodes(nodes.data())
3394 , mIntersectionTree(false)
3395 , mIntersectionAccessor(intersectionTree)
3401template<
typename InputTreeType>
3402MaskIntersectingVoxels<InputTreeType>::MaskIntersectingVoxels(
3403 MaskIntersectingVoxels& rhs, tbb::split)
3404 : mInputAccessor(rhs.mInputAccessor.tree())
3405 , mNodes(rhs.mNodes)
3406 , mIntersectionTree(false)
3407 , mIntersectionAccessor(mIntersectionTree)
3408 , mIsovalue(rhs.mIsovalue)
3413template<
typename InputTreeType>
3415MaskIntersectingVoxels<InputTreeType>::operator()(
const tbb::blocked_range<size_t>& range)
3417 VoxelEdgeAccessor<tree::ValueAccessor<BoolTreeType>, 0> xEdgeAcc(mIntersectionAccessor);
3418 VoxelEdgeAccessor<tree::ValueAccessor<BoolTreeType>, 1> yEdgeAcc(mIntersectionAccessor);
3419 VoxelEdgeAccessor<tree::ValueAccessor<BoolTreeType>, 2> zEdgeAcc(mIntersectionAccessor);
3422 InputValueType iso(mIsovalue);
3424 for (
size_t n = range.begin(); n != range.end(); ++n) {
3426 const BoolLeafNodeType& node = *mNodes[n];
3428 for (
typename BoolLeafNodeType::ValueOnCIter it = node.cbeginValueOn(); it; ++it) {
3430 if (!it.getValue()) {
3432 ijk = it.getCoord();
3434 const bool inside = isInsideValue(mInputAccessor.getValue(ijk), iso);
3436 if (inside != isInsideValue(mInputAccessor.getValue(ijk.offsetBy(1, 0, 0)), iso)) {
3440 if (inside != isInsideValue(mInputAccessor.getValue(ijk.offsetBy(0, 1, 0)), iso)) {
3444 if (inside != isInsideValue(mInputAccessor.getValue(ijk.offsetBy(0, 0, 1)), iso)) {
3453template<
typename BoolTreeType>
3454struct MaskBorderVoxels
3456 using BoolLeafNodeType =
typename BoolTreeType::LeafNodeType;
3458 MaskBorderVoxels(
const BoolTreeType& maskTree,
3459 const std::vector<BoolLeafNodeType*>& maskNodes,
3460 BoolTreeType& borderTree)
3461 : mMaskTree(&maskTree)
3462 , mMaskNodes(maskNodes.data())
3463 , mTmpBorderTree(false)
3464 , mBorderTree(&borderTree)
3468 MaskBorderVoxels(MaskBorderVoxels& rhs, tbb::split)
3469 : mMaskTree(rhs.mMaskTree)
3470 , mMaskNodes(rhs.mMaskNodes)
3471 , mTmpBorderTree(false)
3472 , mBorderTree(&mTmpBorderTree)
3476 void join(MaskBorderVoxels& rhs) { mBorderTree->merge(*rhs.mBorderTree); }
3478 void operator()(
const tbb::blocked_range<size_t>& range)
3480 tree::ValueAccessor<const BoolTreeType> maskAcc(*mMaskTree);
3481 tree::ValueAccessor<BoolTreeType> borderAcc(*mBorderTree);
3484 for (
size_t n = range.begin(); n != range.end(); ++n) {
3486 const BoolLeafNodeType& node = *mMaskNodes[n];
3488 for (
typename BoolLeafNodeType::ValueOnCIter it = node.cbeginValueOn(); it; ++it) {
3490 ijk = it.getCoord();
3492 const bool lhs = it.getValue();
3495 bool isEdgeVoxel =
false;
3498 isEdgeVoxel = (maskAcc.probeValue(ijk, rhs) && lhs != rhs);
3501 isEdgeVoxel = isEdgeVoxel || (maskAcc.probeValue(ijk, rhs) && lhs != rhs);
3504 isEdgeVoxel = isEdgeVoxel || (maskAcc.probeValue(ijk, rhs) && lhs != rhs);
3507 isEdgeVoxel = isEdgeVoxel || (maskAcc.probeValue(ijk, rhs) && lhs != rhs);
3511 isEdgeVoxel = isEdgeVoxel || (maskAcc.probeValue(ijk, rhs) && lhs != rhs);
3514 isEdgeVoxel = isEdgeVoxel || (maskAcc.probeValue(ijk, rhs) && lhs != rhs);
3517 isEdgeVoxel = isEdgeVoxel || (maskAcc.probeValue(ijk, rhs) && lhs != rhs);
3521 borderAcc.setValue(ijk,
true);
3528 BoolTreeType
const *
const mMaskTree;
3529 BoolLeafNodeType
const *
const *
const mMaskNodes;
3531 BoolTreeType mTmpBorderTree;
3532 BoolTreeType *
const mBorderTree;
3536template<
typename BoolTreeType>
3537struct SyncMaskValues
3539 using BoolLeafNodeType =
typename BoolTreeType::LeafNodeType;
3541 SyncMaskValues(
const std::vector<BoolLeafNodeType*>& nodes,
const BoolTreeType& mask)
3542 : mNodes(nodes.data())
3547 void operator()(
const tbb::blocked_range<size_t>& range)
const
3549 using ValueOnIter =
typename BoolLeafNodeType::ValueOnIter;
3551 tree::ValueAccessor<const BoolTreeType> maskTreeAcc(*mMaskTree);
3553 for (
size_t n = range.begin(), N = range.end(); n != N; ++n) {
3555 BoolLeafNodeType& node = *mNodes[n];
3557 const BoolLeafNodeType * maskNode = maskTreeAcc.probeConstLeaf(node.origin());
3560 for (ValueOnIter it = node.beginValueOn(); it; ++it) {
3561 const Index pos = it.pos();
3562 if (maskNode->getValue(pos)) {
3563 node.setValueOnly(pos,
true);
3571 BoolLeafNodeType *
const *
const mNodes;
3572 BoolTreeType
const *
const mMaskTree;
3579template<
typename BoolTreeType>
3582 using BoolLeafNodeType =
typename BoolTreeType::LeafNodeType;
3584 MaskSurface(
const std::vector<BoolLeafNodeType*>& nodes,
const BoolTreeType& mask,
3585 const math::Transform& inputTransform,
const math::Transform& maskTransform,
bool invert)
3586 : mNodes(nodes.data())
3588 , mInputTransform(inputTransform)
3589 , mMaskTransform(maskTransform)
3590 , mInvertMask(invert)
3594 void operator()(
const tbb::blocked_range<size_t>& range)
const
3596 using ValueOnIter =
typename BoolLeafNodeType::ValueOnIter;
3598 tree::ValueAccessor<const BoolTreeType> maskTreeAcc(*mMaskTree);
3600 const bool matchingTransforms = mInputTransform == mMaskTransform;
3601 const bool maskState = mInvertMask;
3603 for (
size_t n = range.begin(), N = range.end(); n != N; ++n) {
3605 BoolLeafNodeType& node = *mNodes[n];
3607 if (matchingTransforms) {
3609 const BoolLeafNodeType * maskNode = maskTreeAcc.probeConstLeaf(node.origin());
3613 for (ValueOnIter it = node.beginValueOn(); it; ++it) {
3614 const Index pos = it.pos();
3615 if (maskNode->isValueOn(pos) == maskState) {
3616 node.setValueOnly(pos,
true);
3622 if (maskTreeAcc.isValueOn(node.origin()) == maskState) {
3623 for (ValueOnIter it = node.beginValueOn(); it; ++it) {
3624 node.setValueOnly(it.pos(),
true);
3634 for (ValueOnIter it = node.beginValueOn(); it; ++it) {
3636 ijk = mMaskTransform.worldToIndexCellCentered(
3637 mInputTransform.indexToWorld(it.getCoord()));
3639 if (maskTreeAcc.isValueOn(ijk) == maskState) {
3640 node.setValueOnly(it.pos(),
true);
3649 BoolLeafNodeType *
const *
const mNodes;
3650 BoolTreeType
const *
const mMaskTree;
3651 math::Transform
const mInputTransform;
3652 math::Transform
const mMaskTransform;
3653 bool const mInvertMask;
3657template<
typename InputGr
idType>
3660 typename InputGridType::TreeType::template ValueConverter<bool>::Type& intersectionTree,
3661 typename InputGridType::TreeType::template ValueConverter<bool>::Type& borderTree,
3662 const InputGridType& inputGrid,
3665 typename InputGridType::ValueType isovalue)
3667 using InputTreeType =
typename InputGridType::TreeType;
3668 using BoolTreeType =
typename InputTreeType::template ValueConverter<bool>::Type;
3669 using BoolLeafNodeType =
typename BoolTreeType::LeafNodeType;
3670 using BoolGridType = Grid<BoolTreeType>;
3672 if (maskGrid && maskGrid->type() == BoolGridType::gridType()) {
3674 const math::Transform& transform = inputGrid.transform();
3675 const InputTreeType& inputTree = inputGrid.tree();
3677 const BoolGridType * surfaceMask =
static_cast<const BoolGridType*
>(maskGrid.get());
3679 const BoolTreeType& maskTree = surfaceMask->tree();
3680 const math::Transform& maskTransform = surfaceMask->transform();
3685 std::vector<BoolLeafNodeType*> intersectionLeafNodes;
3686 intersectionTree.getNodes(intersectionLeafNodes);
3688 tbb::parallel_for(tbb::blocked_range<size_t>(0, intersectionLeafNodes.size()),
3689 MaskSurface<BoolTreeType>(
3690 intersectionLeafNodes, maskTree, transform, maskTransform, invertMask));
3695 MaskBorderVoxels<BoolTreeType> borderOp(
3696 intersectionTree, intersectionLeafNodes, borderTree);
3697 tbb::parallel_reduce(tbb::blocked_range<size_t>(0, intersectionLeafNodes.size()), borderOp);
3702 BoolTreeType tmpIntersectionTree(
false);
3704 MaskIntersectingVoxels<InputTreeType> op(
3705 inputTree, intersectionLeafNodes, tmpIntersectionTree, isovalue);
3707 tbb::parallel_reduce(tbb::blocked_range<size_t>(0, intersectionLeafNodes.size()), op);
3709 std::vector<BoolLeafNodeType*> tmpIntersectionLeafNodes;
3710 tmpIntersectionTree.getNodes(tmpIntersectionLeafNodes);
3712 tbb::parallel_for(tbb::blocked_range<size_t>(0, tmpIntersectionLeafNodes.size()),
3713 SyncMaskValues<BoolTreeType>(tmpIntersectionLeafNodes, intersectionTree));
3715 intersectionTree.clear();
3716 intersectionTree.merge(tmpIntersectionTree);
3724template<
typename InputTreeType>
3725struct ComputeAuxiliaryData
3727 using InputLeafNodeType =
typename InputTreeType::LeafNodeType;
3728 using InputValueType =
typename InputLeafNodeType::ValueType;
3730 using BoolLeafNodeType = tree::LeafNode<bool, InputLeafNodeType::LOG2DIM>;
3732 using Int16TreeType =
typename InputTreeType::template ValueConverter<Int16>::Type;
3733 using Index32TreeType =
typename InputTreeType::template ValueConverter<Index32>::Type;
3736 ComputeAuxiliaryData(
const InputTreeType& inputTree,
3737 const std::vector<const BoolLeafNodeType*>& intersectionLeafNodes,
3738 Int16TreeType& signFlagsTree,
3739 Index32TreeType& pointIndexTree,
3740 InputValueType iso);
3742 ComputeAuxiliaryData(ComputeAuxiliaryData&, tbb::split);
3743 void operator()(
const tbb::blocked_range<size_t>&);
3744 void join(
const ComputeAuxiliaryData& rhs) {
3745 mSignFlagsAccessor.tree().merge(rhs.mSignFlagsAccessor.tree());
3746 mPointIndexAccessor.tree().merge(rhs.mPointIndexAccessor.tree());
3750 tree::ValueAccessor<const InputTreeType> mInputAccessor;
3751 BoolLeafNodeType
const *
const *
const mIntersectionNodes;
3753 Int16TreeType mSignFlagsTree;
3754 tree::ValueAccessor<Int16TreeType> mSignFlagsAccessor;
3755 Index32TreeType mPointIndexTree;
3756 tree::ValueAccessor<Index32TreeType> mPointIndexAccessor;
3758 const InputValueType mIsovalue;
3762template<
typename InputTreeType>
3763ComputeAuxiliaryData<InputTreeType>::ComputeAuxiliaryData(
3764 const InputTreeType& inputTree,
3765 const std::vector<const BoolLeafNodeType*>& intersectionLeafNodes,
3766 Int16TreeType& signFlagsTree,
3767 Index32TreeType& pointIndexTree,
3769 : mInputAccessor(inputTree)
3770 , mIntersectionNodes(intersectionLeafNodes.data())
3772 , mSignFlagsAccessor(signFlagsTree)
3774 , mPointIndexAccessor(pointIndexTree)
3777 pointIndexTree.root().setBackground(std::numeric_limits<Index32>::max(),
false);
3781template<
typename InputTreeType>
3782ComputeAuxiliaryData<InputTreeType>::ComputeAuxiliaryData(ComputeAuxiliaryData& rhs, tbb::split)
3783 : mInputAccessor(rhs.mInputAccessor.tree())
3784 , mIntersectionNodes(rhs.mIntersectionNodes)
3786 , mSignFlagsAccessor(mSignFlagsTree)
3788 , mPointIndexAccessor(mPointIndexTree)
3789 , mIsovalue(rhs.mIsovalue)
3794template<
typename InputTreeType>
3796ComputeAuxiliaryData<InputTreeType>::operator()(
const tbb::blocked_range<size_t>& range)
3798 using Int16LeafNodeType =
typename Int16TreeType::LeafNodeType;
3801 math::Tuple<8, InputValueType> cellVertexValues;
3802 typename std::unique_ptr<Int16LeafNodeType> signsNodePt(
new Int16LeafNodeType(ijk, 0));
3804 for (
size_t n = range.begin(), N = range.end(); n != N; ++n) {
3806 const BoolLeafNodeType& maskNode = *mIntersectionNodes[n];
3807 const Coord& origin = maskNode.origin();
3809 const InputLeafNodeType *leafPt = mInputAccessor.probeConstLeaf(origin);
3811 if (!signsNodePt.get()) signsNodePt.reset(
new Int16LeafNodeType(origin, 0));
3812 else signsNodePt->setOrigin(origin);
3814 bool updatedNode =
false;
3816 for (
typename BoolLeafNodeType::ValueOnCIter it = maskNode.cbeginValueOn(); it; ++it) {
3818 const Index pos = it.pos();
3819 ijk = BoolLeafNodeType::offsetToLocalCoord(pos);
3822 ijk[0] <
int(BoolLeafNodeType::DIM - 1) &&
3823 ijk[1] <
int(BoolLeafNodeType::DIM - 1) &&
3824 ijk[2] <
int(BoolLeafNodeType::DIM - 1) ) {
3825 getCellVertexValues(*leafPt, pos, cellVertexValues);
3827 getCellVertexValues(mInputAccessor, origin + ijk, cellVertexValues);
3830 uint8_t signFlags = computeSignFlags(cellVertexValues, mIsovalue);
3832 if (signFlags != 0 && signFlags != 0xFF) {
3834 const bool inside = signFlags & 0x1;
3836 int edgeFlags = inside ? INSIDE : 0;
3838 if (!it.getValue()) {
3839 edgeFlags |= inside != ((signFlags & 0x02) != 0) ? XEDGE : 0;
3840 edgeFlags |= inside != ((signFlags & 0x10) != 0) ? YEDGE : 0;
3841 edgeFlags |= inside != ((signFlags & 0x08) != 0) ? ZEDGE : 0;
3844 const uint8_t ambiguousCellFlags = sAmbiguousFace[signFlags];
3845 if (ambiguousCellFlags != 0) {
3846 correctCellSigns(signFlags, ambiguousCellFlags, mInputAccessor,
3847 origin + ijk, mIsovalue);
3850 edgeFlags |= int(signFlags);
3852 signsNodePt->setValueOn(pos,
Int16(edgeFlags));
3858 typename Index32TreeType::LeafNodeType* idxNode = mPointIndexAccessor.touchLeaf(origin);
3859 idxNode->topologyUnion(*signsNodePt);
3862 for (
auto it = idxNode->beginValueOn(); it; ++it) {
3863 idxNode->setValueOnly(it.pos(), 0);
3866 mSignFlagsAccessor.addLeaf(signsNodePt.release());
3872template<
typename InputTreeType>
3874computeAuxiliaryData(
3875 typename InputTreeType::template ValueConverter<Int16>::Type& signFlagsTree,
3876 typename InputTreeType::template ValueConverter<Index32>::Type& pointIndexTree,
3877 const typename InputTreeType::template ValueConverter<bool>::Type& intersectionTree,
3878 const InputTreeType& inputTree,
3879 typename InputTreeType::ValueType isovalue)
3881 using BoolTreeType =
typename InputTreeType::template ValueConverter<bool>::Type;
3882 using BoolLeafNodeType =
typename BoolTreeType::LeafNodeType;
3884 std::vector<const BoolLeafNodeType*> intersectionLeafNodes;
3885 intersectionTree.getNodes(intersectionLeafNodes);
3887 ComputeAuxiliaryData<InputTreeType> op(
3888 inputTree, intersectionLeafNodes, signFlagsTree, pointIndexTree, isovalue);
3890 tbb::parallel_reduce(tbb::blocked_range<size_t>(0, intersectionLeafNodes.size()), op);
3897template<Index32 LeafNodeLog2Dim>
3898struct LeafNodePointCount
3900 using Int16LeafNodeType = tree::LeafNode<Int16, LeafNodeLog2Dim>;
3902 LeafNodePointCount(
const std::vector<Int16LeafNodeType*>& leafNodes,
3903 std::unique_ptr<Index32[]>& leafNodeCount)
3904 : mLeafNodes(leafNodes.data())
3905 , mData(leafNodeCount.get())
3909 void operator()(
const tbb::blocked_range<size_t>& range)
const {
3911 for (
size_t n = range.begin(), N = range.end(); n != N; ++n) {
3915 Int16 const * p = mLeafNodes[n]->buffer().data();
3916 Int16 const *
const endP = p + Int16LeafNodeType::SIZE;
3919 count +=
Index32(sEdgeGroupTable[(SIGNS & *p)][0]);
3928 Int16LeafNodeType *
const *
const mLeafNodes;
3933template<
typename Po
intIndexLeafNode>
3934struct AdaptiveLeafNodePointCount
3936 using Int16LeafNodeType = tree::LeafNode<Int16, PointIndexLeafNode::LOG2DIM>;
3938 AdaptiveLeafNodePointCount(
const std::vector<PointIndexLeafNode*>& pointIndexNodes,
3939 const std::vector<Int16LeafNodeType*>& signDataNodes,
3940 std::unique_ptr<Index32[]>& leafNodeCount)
3941 : mPointIndexNodes(pointIndexNodes.data())
3942 , mSignDataNodes(signDataNodes.data())
3943 , mData(leafNodeCount.get())
3947 void operator()(
const tbb::blocked_range<size_t>& range)
const
3949 using IndexType =
typename PointIndexLeafNode::ValueType;
3951 for (
size_t n = range.begin(), N = range.end(); n != N; ++n) {
3953 const PointIndexLeafNode& node = *mPointIndexNodes[n];
3954 const Int16LeafNodeType& signNode = *mSignDataNodes[n];
3958 std::set<IndexType> uniqueRegions;
3960 for (
typename PointIndexLeafNode::ValueOnCIter it = node.cbeginValueOn(); it; ++it) {
3962 IndexType
id = it.getValue();
3965 count += size_t(sEdgeGroupTable[(SIGNS & signNode.getValue(it.pos()))][0]);
3966 }
else if (
id != IndexType(util::INVALID_IDX)) {
3967 uniqueRegions.insert(
id);
3971 mData[n] =
Index32(count + uniqueRegions.size());
3976 PointIndexLeafNode
const *
const *
const mPointIndexNodes;
3977 Int16LeafNodeType
const *
const *
const mSignDataNodes;
3982template<
typename Po
intIndexLeafNode>
3985 using Int16LeafNodeType = tree::LeafNode<Int16, PointIndexLeafNode::LOG2DIM>;
3987 MapPoints(
const std::vector<PointIndexLeafNode*>& pointIndexNodes,
3988 const std::vector<Int16LeafNodeType*>& signDataNodes,
3989 std::unique_ptr<Index32[]>& leafNodeCount)
3990 : mPointIndexNodes(pointIndexNodes.data())
3991 , mSignDataNodes(signDataNodes.data())
3992 , mData(leafNodeCount.get())
3996 void operator()(
const tbb::blocked_range<size_t>& range)
const {
3998 for (
size_t n = range.begin(), N = range.end(); n != N; ++n) {
4000 const Int16LeafNodeType& signNode = *mSignDataNodes[n];
4001 PointIndexLeafNode& indexNode = *mPointIndexNodes[n];
4003 Index32 pointOffset = mData[n];
4005 for (
auto it = indexNode.beginValueOn(); it; ++it) {
4006 const Index pos = it.pos();
4007 indexNode.setValueOnly(pos, pointOffset);
4008 const int signs = SIGNS & int(signNode.getValue(pos));
4009 pointOffset +=
Index32(sEdgeGroupTable[signs][0]);
4015 PointIndexLeafNode *
const *
const mPointIndexNodes;
4016 Int16LeafNodeType
const *
const *
const mSignDataNodes;
4023template<
typename TreeType,
typename PrimBuilder>
4024struct ComputePolygons
4026 using Int16TreeType =
typename TreeType::template ValueConverter<Int16>::Type;
4027 using Int16LeafNodeType =
typename Int16TreeType::LeafNodeType;
4029 using Index32TreeType =
typename TreeType::template ValueConverter<Index32>::Type;
4030 using Index32LeafNodeType =
typename Index32TreeType::LeafNodeType;
4034 const std::vector<Int16LeafNodeType*>& signFlagsLeafNodes,
4035 const Int16TreeType& signFlagsTree,
4036 const Index32TreeType& idxTree,
4037 PolygonPoolList& polygons,
4038 bool invertSurfaceOrientation);
4040 void setRefSignTree(
const Int16TreeType * r) { mRefSignFlagsTree = r; }
4042 void operator()(
const tbb::blocked_range<size_t>&)
const;
4045 Int16LeafNodeType *
const *
const mSignFlagsLeafNodes;
4046 Int16TreeType
const *
const mSignFlagsTree;
4047 Int16TreeType
const * mRefSignFlagsTree;
4048 Index32TreeType
const *
const mIndexTree;
4050 bool const mInvertSurfaceOrientation;
4054template<
typename TreeType,
typename PrimBuilder>
4055ComputePolygons<TreeType, PrimBuilder>::ComputePolygons(
4056 const std::vector<Int16LeafNodeType*>& signFlagsLeafNodes,
4057 const Int16TreeType& signFlagsTree,
4058 const Index32TreeType& idxTree,
4059 PolygonPoolList& polygons,
4060 bool invertSurfaceOrientation)
4061 : mSignFlagsLeafNodes(signFlagsLeafNodes.data())
4062 , mSignFlagsTree(&signFlagsTree)
4063 , mRefSignFlagsTree(nullptr)
4064 , mIndexTree(&idxTree)
4065 , mPolygonPoolList(&polygons)
4066 , mInvertSurfaceOrientation(invertSurfaceOrientation)
4070template<
typename InputTreeType,
typename PrimBuilder>
4072ComputePolygons<InputTreeType, PrimBuilder>::operator()(
const tbb::blocked_range<size_t>& range)
const
4074 using Int16ValueAccessor = tree::ValueAccessor<const Int16TreeType>;
4075 Int16ValueAccessor signAcc(*mSignFlagsTree);
4077 tree::ValueAccessor<const Index32TreeType> idxAcc(*mIndexTree);
4079 const bool invertSurfaceOrientation = mInvertSurfaceOrientation;
4086 std::unique_ptr<Int16ValueAccessor> refSignAcc;
4087 if (mRefSignFlagsTree) refSignAcc.reset(
new Int16ValueAccessor(*mRefSignFlagsTree));
4089 for (
size_t n = range.begin(); n != range.end(); ++n) {
4091 const Int16LeafNodeType& node = *mSignFlagsLeafNodes[n];
4092 origin = node.origin();
4096 typename Int16LeafNodeType::ValueOnCIter iter = node.cbeginValueOn();
4097 for (; iter; ++iter) {
4098 if (iter.getValue() & XEDGE) ++edgeCount;
4099 if (iter.getValue() & YEDGE) ++edgeCount;
4100 if (iter.getValue() & ZEDGE) ++edgeCount;
4103 if(edgeCount == 0)
continue;
4105 mesher.init(edgeCount, (*mPolygonPoolList)[n]);
4107 const Int16LeafNodeType *signleafPt = signAcc.probeConstLeaf(origin);
4108 const Index32LeafNodeType *idxLeafPt = idxAcc.probeConstLeaf(origin);
4110 if (!signleafPt || !idxLeafPt)
continue;
4113 const Int16LeafNodeType *refSignLeafPt =
nullptr;
4114 if (refSignAcc) refSignLeafPt = refSignAcc->probeConstLeaf(origin);
4118 for (iter = node.cbeginValueOn(); iter; ++iter) {
4119 ijk = iter.getCoord();
4121 Int16 flags = iter.getValue();
4123 if (!(flags & 0xE00))
continue;
4126 if (refSignLeafPt) {
4127 refFlags = refSignLeafPt->getValue(iter.pos());
4134 const uint8_t cell = uint8_t(SIGNS & flags);
4136 if (sEdgeGroupTable[cell][0] > 1) {
4137 offsets[0] = (sEdgeGroupTable[cell][1] - 1);
4138 offsets[1] = (sEdgeGroupTable[cell][9] - 1);
4139 offsets[2] = (sEdgeGroupTable[cell][4] - 1);
4142 if (ijk[0] > origin[0] && ijk[1] > origin[1] && ijk[2] > origin[2]) {
4143 constructPolygons(invertSurfaceOrientation,
4144 flags, refFlags, offsets, ijk, *signleafPt, *idxLeafPt, mesher);
4146 constructPolygons(invertSurfaceOrientation,
4147 flags, refFlags, offsets, ijk, signAcc, idxAcc, mesher);
4163 CopyArray(T * outputArray,
const T * inputArray,
size_t outputOffset = 0)
4164 : mOutputArray(outputArray), mInputArray(inputArray), mOutputOffset(outputOffset)
4168 void operator()(
const tbb::blocked_range<size_t>& inputArrayRange)
const
4170 const size_t offset = mOutputOffset;
4171 for (
size_t n = inputArrayRange.begin(), N = inputArrayRange.end(); n < N; ++n) {
4172 mOutputArray[offset + n] = mInputArray[n];
4177 T *
const mOutputArray;
4178 T
const *
const mInputArray;
4179 size_t const mOutputOffset;
4183struct FlagAndCountQuadsToSubdivide
4185 FlagAndCountQuadsToSubdivide(PolygonPoolList& polygons,
4186 const std::vector<uint8_t>& pointFlags,
4187 std::unique_ptr<openvdb::Vec3s[]>& points,
4188 std::unique_ptr<
unsigned[]>& numQuadsToDivide)
4189 : mPolygonPoolList(&polygons)
4190 , mPointFlags(pointFlags.data())
4191 , mPoints(points.get())
4192 , mNumQuadsToDivide(numQuadsToDivide.get())
4196 void operator()(
const tbb::blocked_range<size_t>& range)
const
4198 for (
size_t n = range.begin(), N = range.end(); n < N; ++n) {
4200 PolygonPool& polygons = (*mPolygonPoolList)[n];
4205 for (
size_t i = 0, I = polygons.numQuads(); i < I; ++i) {
4207 char& flags = polygons.quadFlags(i);
4209 if ((flags & POLYFLAG_FRACTURE_SEAM) && !(flags & POLYFLAG_EXTERIOR)) {
4211 Vec4I& quad = polygons.quad(i);
4213 const bool edgePoly = mPointFlags[quad[0]] || mPointFlags[quad[1]]
4214 || mPointFlags[quad[2]] || mPointFlags[quad[3]];
4216 if (!edgePoly)
continue;
4218 const Vec3s& p0 = mPoints[quad[0]];
4219 const Vec3s& p1 = mPoints[quad[1]];
4220 const Vec3s& p2 = mPoints[quad[2]];
4221 const Vec3s& p3 = mPoints[quad[3]];
4223 if (!isPlanarQuad(p0, p1, p2, p3, 1e-6f)) {
4230 mNumQuadsToDivide[n] = count;
4236 uint8_t
const *
const mPointFlags;
4237 Vec3s const *
const mPoints;
4238 unsigned *
const mNumQuadsToDivide;
4242struct SubdivideQuads
4244 SubdivideQuads(PolygonPoolList& polygons,
4245 const std::unique_ptr<openvdb::Vec3s[]>& points,
4247 std::unique_ptr<openvdb::Vec3s[]>& centroids,
4248 std::unique_ptr<
unsigned[]>& numQuadsToDivide,
4249 std::unique_ptr<
unsigned[]>& centroidOffsets)
4250 : mPolygonPoolList(&polygons)
4251 , mPoints(points.get())
4252 , mCentroids(centroids.get())
4253 , mNumQuadsToDivide(numQuadsToDivide.get())
4254 , mCentroidOffsets(centroidOffsets.get())
4259 void operator()(
const tbb::blocked_range<size_t>& range)
const
4261 for (
size_t n = range.begin(), N = range.end(); n < N; ++n) {
4263 PolygonPool& polygons = (*mPolygonPoolList)[n];
4265 const size_t nonplanarCount = size_t(mNumQuadsToDivide[n]);
4267 if (nonplanarCount > 0) {
4269 PolygonPool tmpPolygons;
4270 tmpPolygons.resetQuads(polygons.numQuads() - nonplanarCount);
4271 tmpPolygons.resetTriangles(polygons.numTriangles() +
size_t(4) * nonplanarCount);
4273 size_t offset = mCentroidOffsets[n];
4275 size_t triangleIdx = 0;
4277 for (
size_t i = 0, I = polygons.numQuads(); i < I; ++i) {
4279 const char quadFlags = polygons.quadFlags(i);
4280 if (!(quadFlags & POLYFLAG_SUBDIVIDED))
continue;
4282 unsigned newPointIdx = unsigned(offset + mPointCount);
4286 mCentroids[offset] = (mPoints[quad[0]] + mPoints[quad[1]] +
4287 mPoints[quad[2]] + mPoints[quad[3]]) * 0.25f;
4292 Vec3I& triangle = tmpPolygons.triangle(triangleIdx);
4294 triangle[0] = quad[0];
4295 triangle[1] = newPointIdx;
4296 triangle[2] = quad[3];
4298 tmpPolygons.triangleFlags(triangleIdx) = quadFlags;
4304 Vec3I& triangle = tmpPolygons.triangle(triangleIdx);
4306 triangle[0] = quad[0];
4307 triangle[1] = quad[1];
4308 triangle[2] = newPointIdx;
4310 tmpPolygons.triangleFlags(triangleIdx) = quadFlags;
4316 Vec3I& triangle = tmpPolygons.triangle(triangleIdx);
4318 triangle[0] = quad[1];
4319 triangle[1] = quad[2];
4320 triangle[2] = newPointIdx;
4322 tmpPolygons.triangleFlags(triangleIdx) = quadFlags;
4329 Vec3I& triangle = tmpPolygons.triangle(triangleIdx);
4331 triangle[0] = quad[2];
4332 triangle[1] = quad[3];
4333 triangle[2] = newPointIdx;
4335 tmpPolygons.triangleFlags(triangleIdx) = quadFlags;
4340 quad[0] = util::INVALID_IDX;
4344 for (
size_t i = 0, I = polygons.numTriangles(); i < I; ++i) {
4345 tmpPolygons.triangle(triangleIdx) = polygons.triangle(i);
4346 tmpPolygons.triangleFlags(triangleIdx) = polygons.triangleFlags(i);
4351 for (
size_t i = 0, I = polygons.numQuads(); i < I; ++i) {
4354 if (quad[0] != util::INVALID_IDX) {
4355 tmpPolygons.quad(quadIdx) = quad;
4356 tmpPolygons.quadFlags(quadIdx) = polygons.quadFlags(i);
4361 polygons.copy(tmpPolygons);
4368 Vec3s const *
const mPoints;
4369 Vec3s *
const mCentroids;
4370 unsigned *
const mNumQuadsToDivide;
4371 unsigned *
const mCentroidOffsets;
4372 size_t const mPointCount;
4377subdivideNonplanarSeamLineQuads(
4378 PolygonPoolList& polygonPoolList,
4379 size_t polygonPoolListSize,
4380 PointList& pointList,
4381 size_t& pointListSize,
4382 std::vector<uint8_t>& pointFlags)
4384 const tbb::blocked_range<size_t> polygonPoolListRange(0, polygonPoolListSize);
4386 std::unique_ptr<unsigned[]> numQuadsToDivide(
new unsigned[polygonPoolListSize]);
4388 tbb::parallel_for(polygonPoolListRange,
4389 FlagAndCountQuadsToSubdivide(polygonPoolList, pointFlags, pointList, numQuadsToDivide));
4391 std::unique_ptr<unsigned[]> centroidOffsets(
new unsigned[polygonPoolListSize]);
4393 size_t centroidCount = 0;
4397 for (
size_t n = 0, N = polygonPoolListSize; n < N; ++n) {
4398 centroidOffsets[n] = sum;
4399 sum += numQuadsToDivide[n];
4401 centroidCount = size_t(sum);
4404 std::unique_ptr<Vec3s[]> centroidList(
new Vec3s[centroidCount]);
4406 tbb::parallel_for(polygonPoolListRange,
4407 SubdivideQuads(polygonPoolList, pointList, pointListSize,
4408 centroidList, numQuadsToDivide, centroidOffsets));
4410 if (centroidCount > 0) {
4412 const size_t newPointListSize = centroidCount + pointListSize;
4414 std::unique_ptr<openvdb::Vec3s[]> newPointList(
new openvdb::Vec3s[newPointListSize]);
4416 tbb::parallel_for(tbb::blocked_range<size_t>(0, pointListSize),
4417 CopyArray<Vec3s>(newPointList.get(), pointList.get()));
4419 tbb::parallel_for(tbb::blocked_range<size_t>(0, newPointListSize - pointListSize),
4420 CopyArray<Vec3s>(newPointList.get(), centroidList.get(), pointListSize));
4422 pointListSize = newPointListSize;
4423 pointList.swap(newPointList);
4424 pointFlags.resize(pointListSize, 0);
4429struct ReviseSeamLineFlags
4431 ReviseSeamLineFlags(PolygonPoolList& polygons,
4432 const std::vector<uint8_t>& pointFlags)
4433 : mPolygonPoolList(&polygons)
4434 , mPointFlags(pointFlags.data())
4438 void operator()(
const tbb::blocked_range<size_t>& range)
const
4440 for (
size_t n = range.begin(), N = range.end(); n < N; ++n) {
4442 PolygonPool& polygons = (*mPolygonPoolList)[n];
4444 for (
size_t i = 0, I = polygons.numQuads(); i < I; ++i) {
4446 char& flags = polygons.quadFlags(i);
4448 if (flags & POLYFLAG_FRACTURE_SEAM) {
4452 const bool hasSeamLinePoint =
4453 mPointFlags[verts[0]] || mPointFlags[verts[1]] ||
4454 mPointFlags[verts[2]] || mPointFlags[verts[3]];
4456 if (!hasSeamLinePoint) {
4457 flags &= ~POLYFLAG_FRACTURE_SEAM;
4462 for (
size_t i = 0, I = polygons.numTriangles(); i < I; ++i) {
4464 char& flags = polygons.triangleFlags(i);
4466 if (flags & POLYFLAG_FRACTURE_SEAM) {
4470 const bool hasSeamLinePoint =
4471 mPointFlags[verts[0]] || mPointFlags[verts[1]] || mPointFlags[verts[2]];
4473 if (!hasSeamLinePoint) {
4474 flags &= ~POLYFLAG_FRACTURE_SEAM;
4485 uint8_t
const *
const mPointFlags;
4490reviseSeamLineFlags(PolygonPoolList& polygonPoolList,
size_t polygonPoolListSize,
4491 std::vector<uint8_t>& pointFlags)
4493 tbb::parallel_for(tbb::blocked_range<size_t>(0, polygonPoolListSize),
4494 ReviseSeamLineFlags(polygonPoolList, pointFlags));
4501template<
typename InputTreeType>
4502struct MaskDisorientedTrianglePoints
4504 MaskDisorientedTrianglePoints(
const InputTreeType& inputTree,
const PolygonPoolList& polygons,
4505 const PointList& pointList, std::unique_ptr<uint8_t[]>& pointMask,
4506 const math::Transform& transform,
bool invertSurfaceOrientation)
4507 : mInputTree(&inputTree)
4508 , mPolygonPoolList(&polygons)
4509 , mPointList(&pointList)
4510 , mPointMask(pointMask.get())
4511 , mTransform(transform)
4512 , mInvertSurfaceOrientation(invertSurfaceOrientation)
4516 void operator()(
const tbb::blocked_range<size_t>& range)
const
4518 using ValueType =
typename InputTreeType::LeafNodeType::ValueType;
4520 tree::ValueAccessor<const InputTreeType> inputAcc(*mInputTree);
4521 Vec3s centroid, normal;
4524 const bool invertGradientDir = mInvertSurfaceOrientation || isBoolValue<ValueType>();
4526 for (
size_t n = range.begin(), N = range.end(); n < N; ++n) {
4528 const PolygonPool& polygons = (*mPolygonPoolList)[n];
4530 for (
size_t i = 0, I = polygons.numTriangles(); i < I; ++i) {
4532 const Vec3I& verts = polygons.triangle(i);
4534 const Vec3s& v0 = (*mPointList)[verts[0]];
4535 const Vec3s& v1 = (*mPointList)[verts[1]];
4536 const Vec3s& v2 = (*mPointList)[verts[2]];
4538 normal = (v2 - v0).cross((v1 - v0));
4541 centroid = (v0 + v1 + v2) * (1.0f / 3.0f);
4542 ijk = mTransform.worldToIndexCellCentered(centroid);
4544 Vec3s dir( math::ISGradient<math::CD_2ND>::result(inputAcc, ijk) );
4547 if (invertGradientDir) {
4552 if (dir.dot(normal) < -0.5f) {
4557 mPointMask[verts[0]] = 1;
4558 mPointMask[verts[1]] = 1;
4559 mPointMask[verts[2]] = 1;
4568 InputTreeType
const *
const mInputTree;
4571 uint8_t *
const mPointMask;
4572 math::Transform
const mTransform;
4573 bool const mInvertSurfaceOrientation;
4577template<
typename InputTree>
4579relaxDisorientedTriangles(
4580 bool invertSurfaceOrientation,
4581 const InputTree& inputTree,
4582 const math::Transform& transform,
4583 PolygonPoolList& polygonPoolList,
4584 size_t polygonPoolListSize,
4585 PointList& pointList,
4586 const size_t pointListSize)
4588 const tbb::blocked_range<size_t> polygonPoolListRange(0, polygonPoolListSize);
4590 std::unique_ptr<uint8_t[]> pointMask(
new uint8_t[pointListSize]);
4591 fillArray(pointMask.get(), uint8_t(0), pointListSize);
4593 tbb::parallel_for(polygonPoolListRange,
4594 MaskDisorientedTrianglePoints<InputTree>(
4595 inputTree, polygonPoolList, pointList, pointMask, transform, invertSurfaceOrientation));
4597 std::unique_ptr<uint8_t[]> pointUpdates(
new uint8_t[pointListSize]);
4598 fillArray(pointUpdates.get(), uint8_t(0), pointListSize);
4600 std::unique_ptr<Vec3s[]> newPoints(
new Vec3s[pointListSize]);
4601 fillArray(newPoints.get(),
Vec3s(0.0f, 0.0f, 0.0f), pointListSize);
4603 for (
size_t n = 0, N = polygonPoolListSize; n < N; ++n) {
4605 PolygonPool& polygons = polygonPoolList[n];
4607 for (
size_t i = 0, I = polygons.numQuads(); i < I; ++i) {
4610 for (
int v = 0; v < 4; ++v) {
4612 const unsigned pointIdx = verts[v];
4614 if (pointMask[pointIdx] == 1) {
4616 newPoints[pointIdx] +=
4617 pointList[verts[0]] + pointList[verts[1]] +
4618 pointList[verts[2]] + pointList[verts[3]];
4620 pointUpdates[pointIdx] = uint8_t(pointUpdates[pointIdx] + 4);
4625 for (
size_t i = 0, I = polygons.numTriangles(); i < I; ++i) {
4628 for (
int v = 0; v < 3; ++v) {
4630 const unsigned pointIdx = verts[v];
4632 if (pointMask[pointIdx] == 1) {
4633 newPoints[pointIdx] +=
4634 pointList[verts[0]] + pointList[verts[1]] + pointList[verts[2]];
4636 pointUpdates[pointIdx] = uint8_t(pointUpdates[pointIdx] + 3);
4642 for (
size_t n = 0, N = pointListSize; n < N; ++n) {
4643 if (pointUpdates[n] > 0) {
4644 const double weight = 1.0 / double(pointUpdates[n]);
4645 pointList[n] = newPoints[n] * float(weight);
4659PolygonPool::PolygonPool()
4663 , mTriangles(nullptr)
4664 , mQuadFlags(nullptr)
4665 , mTriangleFlags(nullptr)
4672 : mNumQuads(numQuads)
4673 , mNumTriangles(numTriangles)
4676 , mQuadFlags(new char[mNumQuads])
4677 , mTriangleFlags(new char[mNumTriangles])
4688 for (
size_t i = 0; i < mNumQuads; ++i) {
4689 mQuads[i] = rhs.mQuads[i];
4690 mQuadFlags[i] = rhs.mQuadFlags[i];
4693 for (
size_t i = 0; i < mNumTriangles; ++i) {
4694 mTriangles[i] = rhs.mTriangles[i];
4695 mTriangleFlags[i] = rhs.mTriangleFlags[i];
4705 mQuadFlags.reset(
new char[mNumQuads]);
4713 mQuads.reset(
nullptr);
4714 mQuadFlags.reset(
nullptr);
4721 mNumTriangles = size;
4723 mTriangleFlags.reset(
new char[mNumTriangles]);
4731 mTriangles.reset(
nullptr);
4732 mTriangleFlags.reset(
nullptr);
4739 if (!(n < mNumQuads))
return false;
4744 mQuads.reset(
nullptr);
4748 std::unique_ptr<char[]> flags(
new char[n]);
4750 for (
size_t i = 0; i < n; ++i) {
4751 quads[i] = mQuads[i];
4752 flags[i] = mQuadFlags[i];
4756 mQuadFlags.swap(flags);
4768 if (!(n < mNumTriangles))
return false;
4773 mTriangles.reset(
nullptr);
4776 std::unique_ptr<openvdb::Vec3I[]> triangles(
new openvdb::Vec3I[n]);
4777 std::unique_ptr<char[]> flags(
new char[n]);
4779 for (
size_t i = 0; i < n; ++i) {
4780 triangles[i] = mTriangles[i];
4781 flags[i] = mTriangleFlags[i];
4784 mTriangles.swap(triangles);
4785 mTriangleFlags.swap(flags);
4802 , mSeamPointListSize(0)
4803 , mPolygonPoolListSize(0)
4804 , mIsovalue(isovalue)
4805 , mPrimAdaptivity(adaptivity)
4806 , mSecAdaptivity(0.0)
4808 , mSurfaceMaskGrid(
GridBase::ConstPtr())
4809 , mAdaptivityGrid(
GridBase::ConstPtr())
4810 , mAdaptivityMaskTree(
TreeBase::ConstPtr())
4813 , mInvertSurfaceMask(false)
4814 , mRelaxDisorientedTriangles(relaxDisorientedTriangles)
4815 , mQuantizedSeamPoints(nullptr)
4825 mSecAdaptivity = secAdaptivity;
4830 mSeamPointListSize = 0;
4831 mQuantizedSeamPoints.reset(
nullptr);
4838 mSurfaceMaskGrid = mask;
4839 mInvertSurfaceMask = invertMask;
4846 mAdaptivityGrid = grid;
4853 mAdaptivityMaskTree = tree;
4857template<
typename InputGr
idType>
4863 using InputTreeType =
typename InputGridType::TreeType;
4864 using InputLeafNodeType =
typename InputTreeType::LeafNodeType;
4865 using InputValueType =
typename InputLeafNodeType::ValueType;
4869 using FloatTreeType =
typename InputTreeType::template ValueConverter<float>::Type;
4871 using BoolTreeType =
typename InputTreeType::template ValueConverter<bool>::Type;
4872 using Int16TreeType =
typename InputTreeType::template ValueConverter<Int16>::Type;
4873 using Int16LeafNodeType =
typename Int16TreeType::LeafNodeType;
4874 using Index32TreeType =
typename InputTreeType::template ValueConverter<Index32>::Type;
4875 using Index32LeafNodeType =
typename Index32TreeType::LeafNodeType;
4880 mPolygonPoolListSize = 0;
4882 mPointFlags.clear();
4887 const InputValueType isovalue = InputValueType(mIsovalue);
4888 const float adaptivityThreshold = float(mPrimAdaptivity);
4889 const bool adaptive = mPrimAdaptivity > 1e-7 || mSecAdaptivity > 1e-7;
4895 const bool invertSurfaceOrientation = (!volume_to_mesh_internal::isBoolValue<InputValueType>()
4900 const InputTreeType& inputTree = inputGrid.tree();
4902 BoolTreeType intersectionTree(
false), adaptivityMask(
false);
4904 if (mAdaptivityMaskTree && mAdaptivityMaskTree->type() == BoolTreeType::treeType()) {
4905 const BoolTreeType *refAdaptivityMask=
4906 static_cast<const BoolTreeType*
>(mAdaptivityMaskTree.get());
4907 adaptivityMask.topologyUnion(*refAdaptivityMask);
4910 Int16TreeType signFlagsTree(0);
4911 Index32TreeType pointIndexTree(std::numeric_limits<Index32>::max());
4916 volume_to_mesh_internal::identifySurfaceIntersectingVoxels(
4917 intersectionTree, inputTree, isovalue);
4919 volume_to_mesh_internal::applySurfaceMask(intersectionTree, adaptivityMask,
4920 inputGrid, mSurfaceMaskGrid, mInvertSurfaceMask, isovalue);
4922 if (intersectionTree.empty())
return;
4924 volume_to_mesh_internal::computeAuxiliaryData(
4925 signFlagsTree, pointIndexTree, intersectionTree, inputTree, isovalue);
4927 intersectionTree.clear();
4929 std::vector<Index32LeafNodeType*> pointIndexLeafNodes;
4930 pointIndexTree.getNodes(pointIndexLeafNodes);
4932 std::vector<Int16LeafNodeType*> signFlagsLeafNodes;
4933 signFlagsTree.getNodes(signFlagsLeafNodes);
4935 const tbb::blocked_range<size_t> auxiliaryLeafNodeRange(0, signFlagsLeafNodes.size());
4940 Int16TreeType* refSignFlagsTree =
nullptr;
4941 Index32TreeType* refPointIndexTree =
nullptr;
4942 InputTreeType
const* refInputTree =
nullptr;
4944 if (mRefGrid && mRefGrid->type() == InputGridType::gridType()) {
4946 const InputGridType* refGrid =
static_cast<const InputGridType*
>(mRefGrid.get());
4947 refInputTree = &refGrid->tree();
4949 if (!mRefSignTree && !mRefIdxTree) {
4953 typename Int16TreeType::Ptr refSignFlagsTreePt(
new Int16TreeType(0));
4954 typename Index32TreeType::Ptr refPointIndexTreePt(
4955 new Index32TreeType(std::numeric_limits<Index32>::max()));
4957 BoolTreeType refIntersectionTree(
false);
4959 volume_to_mesh_internal::identifySurfaceIntersectingVoxels(
4960 refIntersectionTree, *refInputTree, isovalue);
4962 volume_to_mesh_internal::computeAuxiliaryData(*refSignFlagsTreePt,
4963 *refPointIndexTreePt, refIntersectionTree, *refInputTree, isovalue);
4965 mRefSignTree = refSignFlagsTreePt;
4966 mRefIdxTree = refPointIndexTreePt;
4969 if (mRefSignTree && mRefIdxTree) {
4973 refSignFlagsTree =
static_cast<Int16TreeType*
>(mRefSignTree.get());
4974 refPointIndexTree =
static_cast<Index32TreeType*
>(mRefIdxTree.get());
4978 if (refSignFlagsTree && refPointIndexTree) {
4982 volume_to_mesh_internal::markSeamLineData(signFlagsTree, *refSignFlagsTree);
4984 if (mSeamPointListSize == 0) {
4988 std::vector<Int16LeafNodeType*> refSignFlagsLeafNodes;
4989 refSignFlagsTree->getNodes(refSignFlagsLeafNodes);
4991 std::unique_ptr<Index32[]> leafNodeOffsets(
4992 new Index32[refSignFlagsLeafNodes.size()]);
4994 tbb::parallel_for(tbb::blocked_range<size_t>(0, refSignFlagsLeafNodes.size()),
4995 volume_to_mesh_internal::LeafNodePointCount<Int16LeafNodeType::LOG2DIM>(
4996 refSignFlagsLeafNodes, leafNodeOffsets));
5000 for (
size_t n = 0, N = refSignFlagsLeafNodes.size(); n < N; ++n) {
5001 const Index32 tmp = leafNodeOffsets[n];
5002 leafNodeOffsets[n] = count;
5005 mSeamPointListSize = size_t(count);
5008 if (mSeamPointListSize != 0) {
5010 mQuantizedSeamPoints.reset(
new uint32_t[mSeamPointListSize]);
5012 memset(mQuantizedSeamPoints.get(), 0,
sizeof(uint32_t) * mSeamPointListSize);
5014 std::vector<Index32LeafNodeType*> refPointIndexLeafNodes;
5015 refPointIndexTree->getNodes(refPointIndexLeafNodes);
5017 tbb::parallel_for(tbb::blocked_range<size_t>(0, refPointIndexLeafNodes.size()),
5018 volume_to_mesh_internal::MapPoints<Index32LeafNodeType>(
5019 refPointIndexLeafNodes, refSignFlagsLeafNodes, leafNodeOffsets));
5023 if (mSeamPointListSize != 0) {
5025 tbb::parallel_for(auxiliaryLeafNodeRange,
5026 volume_to_mesh_internal::SeamLineWeights<InputTreeType>(
5027 signFlagsLeafNodes, inputTree, *refPointIndexTree, *refSignFlagsTree,
5028 mQuantizedSeamPoints.get(), isovalue));
5033 const bool referenceMeshing = refSignFlagsTree && refPointIndexTree && refInputTree;
5038 std::unique_ptr<Index32[]> leafNodeOffsets(
new Index32[signFlagsLeafNodes.size()]);
5041 volume_to_mesh_internal::MergeVoxelRegions<InputGridType> mergeOp(
5042 inputGrid, pointIndexTree, pointIndexLeafNodes, signFlagsLeafNodes,
5043 isovalue, adaptivityThreshold, invertSurfaceOrientation);
5045 if (mAdaptivityGrid && mAdaptivityGrid->type() == FloatGridType::gridType()) {
5046 const FloatGridType* adaptivityGrid =
5047 static_cast<const FloatGridType*
>(mAdaptivityGrid.get());
5048 mergeOp.setSpatialAdaptivity(*adaptivityGrid);
5051 if (!adaptivityMask.empty()) {
5052 mergeOp.setAdaptivityMask(adaptivityMask);
5055 if (referenceMeshing) {
5056 mergeOp.setRefSignFlagsData(*refSignFlagsTree,
float(mSecAdaptivity));
5059 tbb::parallel_for(auxiliaryLeafNodeRange, mergeOp);
5061 volume_to_mesh_internal::AdaptiveLeafNodePointCount<Index32LeafNodeType>
5062 op(pointIndexLeafNodes, signFlagsLeafNodes, leafNodeOffsets);
5064 tbb::parallel_for(auxiliaryLeafNodeRange, op);
5068 volume_to_mesh_internal::LeafNodePointCount<Int16LeafNodeType::LOG2DIM>
5069 op(signFlagsLeafNodes, leafNodeOffsets);
5071 tbb::parallel_for(auxiliaryLeafNodeRange, op);
5077 for (
size_t n = 0, N = signFlagsLeafNodes.size(); n < N; ++n) {
5078 const Index32 tmp = leafNodeOffsets[n];
5079 leafNodeOffsets[n] = pointCount;
5083 mPointListSize = size_t(pointCount);
5084 mPoints.reset(
new openvdb::Vec3s[mPointListSize]);
5085 mPointFlags.clear();
5092 volume_to_mesh_internal::ComputePoints<InputTreeType>
5093 op(mPoints.get(), inputTree, pointIndexLeafNodes,
5094 signFlagsLeafNodes, leafNodeOffsets, transform, mIsovalue);
5096 if (referenceMeshing) {
5097 mPointFlags.resize(mPointListSize);
5098 op.setRefData(*refInputTree, *refPointIndexTree, *refSignFlagsTree,
5099 mQuantizedSeamPoints.get(), mPointFlags.data());
5102 tbb::parallel_for(auxiliaryLeafNodeRange, op);
5108 mPolygonPoolListSize = signFlagsLeafNodes.size();
5109 mPolygons.reset(
new PolygonPool[mPolygonPoolListSize]);
5113 using PrimBuilder = volume_to_mesh_internal::AdaptivePrimBuilder;
5115 volume_to_mesh_internal::ComputePolygons<Int16TreeType, PrimBuilder>
5116 op(signFlagsLeafNodes, signFlagsTree, pointIndexTree,
5117 mPolygons, invertSurfaceOrientation);
5119 if (referenceMeshing) {
5120 op.setRefSignTree(refSignFlagsTree);
5123 tbb::parallel_for(auxiliaryLeafNodeRange, op);
5127 using PrimBuilder = volume_to_mesh_internal::UniformPrimBuilder;
5129 volume_to_mesh_internal::ComputePolygons<Int16TreeType, PrimBuilder>
5130 op(signFlagsLeafNodes, signFlagsTree, pointIndexTree,
5131 mPolygons, invertSurfaceOrientation);
5133 if (referenceMeshing) {
5134 op.setRefSignTree(refSignFlagsTree);
5137 tbb::parallel_for(auxiliaryLeafNodeRange, op);
5141 signFlagsTree.clear();
5142 pointIndexTree.clear();
5145 if (adaptive && mRelaxDisorientedTriangles) {
5146 volume_to_mesh_internal::relaxDisorientedTriangles(invertSurfaceOrientation,
5147 inputTree, transform, mPolygons, mPolygonPoolListSize, mPoints, mPointListSize);
5151 if (referenceMeshing) {
5152 volume_to_mesh_internal::subdivideNonplanarSeamLineQuads(
5153 mPolygons, mPolygonPoolListSize, mPoints, mPointListSize, mPointFlags);
5155 volume_to_mesh_internal::reviseSeamLineFlags(mPolygons, mPolygonPoolListSize, mPointFlags);
5168template<
typename Gr
idType>
5169inline typename std::enable_if<std::is_scalar<typename GridType::ValueType>::value,
void>::type
5171 const GridType& grid,
5172 std::vector<Vec3s>& points,
5173 std::vector<Vec3I>& triangles,
5174 std::vector<Vec4I>& quads,
5177 bool relaxDisorientedTriangles)
5179 VolumeToMesh mesher(isovalue, adaptivity, relaxDisorientedTriangles);
5184 points.resize(mesher.pointListSize());
5187 volume_to_mesh_internal::PointListCopy ptnCpy(mesher.pointList(), points);
5188 tbb::parallel_for(tbb::blocked_range<size_t>(0, points.size()), ptnCpy);
5189 mesher.pointList().reset(
nullptr);
5195 size_t numQuads = 0, numTriangles = 0;
5196 for (
size_t n = 0, N = mesher.polygonPoolListSize(); n < N; ++n) {
5197 openvdb::tools::PolygonPool& polygons = polygonPoolList[n];
5198 numTriangles += polygons.numTriangles();
5199 numQuads += polygons.numQuads();
5203 triangles.resize(numTriangles);
5205 quads.resize(numQuads);
5209 size_t qIdx = 0, tIdx = 0;
5210 for (
size_t n = 0, N = mesher.polygonPoolListSize(); n < N; ++n) {
5211 openvdb::tools::PolygonPool& polygons = polygonPoolList[n];
5213 for (
size_t i = 0, I = polygons.numQuads(); i < I; ++i) {
5214 quads[qIdx++] = polygons.quad(i);
5217 for (
size_t i = 0, I = polygons.numTriangles(); i < I; ++i) {
5218 triangles[tIdx++] = polygons.triangle(i);
5224template<
typename Gr
idType>
5225inline typename std::enable_if<!std::is_scalar<typename GridType::ValueType>::value,
void>::type
5228 std::vector<Vec3s>&,
5229 std::vector<Vec3I>&,
5230 std::vector<Vec4I>&,
5235 OPENVDB_THROW(TypeError,
"volume to mesh conversion is supported only for scalar grids");
5242template<
typename Gr
idType>
5245 const GridType& grid,
5246 std::vector<Vec3s>& points,
5247 std::vector<Vec3I>& triangles,
5248 std::vector<Vec4I>& quads,
5251 bool relaxDisorientedTriangles)
5253 doVolumeToMesh(grid, points, triangles, quads, isovalue, adaptivity, relaxDisorientedTriangles);
5257template<
typename Gr
idType>
5260 const GridType& grid,
5261 std::vector<Vec3s>& points,
5262 std::vector<Vec4I>& quads,
5265 std::vector<Vec3I> triangles;
5266 doVolumeToMesh(grid, points, triangles, quads, isovalue, 0.0,
true);
5275#ifdef OPENVDB_USE_EXPLICIT_INSTANTIATION
5277#ifdef OPENVDB_INSTANTIATE_VOLUMETOMESH
5281#define _FUNCTION(TreeT) \
5282 void volumeToMesh(const Grid<TreeT>&, std::vector<Vec3s>&, std::vector<Vec4I>&, double)
5286#define _FUNCTION(TreeT) \
5287 void volumeToMesh(const Grid<TreeT>&, std::vector<Vec3s>&, std::vector<Vec3I>&, std::vector<Vec4I>&, double, double, bool)
ValueT value
Definition: GridBuilder.h:1287
T dot(const Vec3T &v) const
Definition: NanoVDB.h:1081
Vec3 & normalize()
Definition: NanoVDB.h:1123
Abstract base class for typed grids.
Definition: Grid.h:78
SharedPtr< const GridBase > ConstPtr
Definition: Grid.h:81
Container class that associates a tree with a transform and metadata.
Definition: Grid.h:573
Mat3 transpose() const
returns transpose of this
Definition: Mat3.h:468
Base class for typed trees.
Definition: Tree.h:37
SharedPtr< TreeBase > Ptr
Definition: Tree.h:39
SharedPtr< const TreeBase > ConstPtr
Definition: Tree.h:40
BBox< Coord > CoordBBox
Definition: NanoVDB.h:1658
Vec3< double > Vec3d
Definition: Vec3.h:668
Vec4< int32_t > Vec4i
Definition: Vec4.h:563
Vec3< int32_t > Vec3i
Definition: Vec3.h:665
Vec3< float > Vec3s
Definition: Vec3.h:667
std::vector< Index > IndexArray
Definition: PointMove.h:161
Index64 pointCount(const PointDataTreeT &tree, const FilterT &filter=NullFilter(), const bool inCoreOnly=false, const bool threaded=true)
Count the total number of points in a PointDataTree.
Definition: PointCount.h:88
OPENVDB_API const Index32 INVALID_IDX
Index32 Index
Definition: Types.h:54
math::Vec4< Index32 > Vec4I
Definition: Types.h:88
int16_t Int16
Definition: Types.h:55
@ GRID_LEVEL_SET
Definition: Types.h:416
uint32_t Index32
Definition: Types.h:52
math::Vec3< Index32 > Vec3I
Definition: Types.h:73
Definition: Exceptions.h:13
#define OPENVDB_THROW(exception, message)
Definition: Exceptions.h:74
static Vec3< typename Accessor::ValueType > result(const Accessor &grid, const Coord &ijk)
Definition: Operators.h:103
#define OPENVDB_VERSION_NAME
The version namespace name for this library version.
Definition: version.h.in:116
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h.in:202
#define OPENVDB_NUMERIC_TREE_INSTANTIATE(Function)
Definition: version.h.in:148