43 #ifndef OPENVDB_TOOLS_MESH_TO_VOLUME_HAS_BEEN_INCLUDED 44 #define OPENVDB_TOOLS_MESH_TO_VOLUME_HAS_BEEN_INCLUDED 46 #include <openvdb/Platform.h> 47 #include <openvdb/Types.h> 48 #include <openvdb/math/FiniteDifference.h> 49 #include <openvdb/math/Proximity.h> 50 #include <openvdb/util/NullInterrupter.h> 51 #include <openvdb/util/Util.h> 57 #include <tbb/blocked_range.h> 58 #include <tbb/enumerable_thread_specific.h> 59 #include <tbb/parallel_for.h> 60 #include <tbb/parallel_reduce.h> 61 #include <tbb/partitioner.h> 62 #include <tbb/task_group.h> 63 #include <tbb/task_scheduler_init.h> 65 #include <boost/mpl/at.hpp> 66 #include <boost/mpl/int.hpp> 67 #include <boost/mpl/size.hpp> 75 #include <type_traits> 140 template <
typename Gr
idType,
typename MeshDataAdapter>
141 inline typename GridType::Ptr
145 float exteriorBandWidth = 3.0f,
146 float interiorBandWidth = 3.0f,
166 template <
typename Gr
idType,
typename MeshDataAdapter,
typename Interrupter>
167 inline typename GridType::Ptr
169 Interrupter& interrupter,
172 float exteriorBandWidth = 3.0f,
173 float interiorBandWidth = 3.0f,
191 template<
typename Po
intType,
typename PolygonType>
195 const std::vector<PolygonType>& polygons)
196 : mPointArray(points.empty() ? nullptr : &points[0])
197 , mPointArraySize(points.size())
198 , mPolygonArray(polygons.empty() ? nullptr : &polygons[0])
199 , mPolygonArraySize(polygons.size())
204 const PolygonType* polygonArray,
size_t polygonArraySize)
205 : mPointArray(pointArray)
206 , mPointArraySize(pointArraySize)
207 , mPolygonArray(polygonArray)
208 , mPolygonArraySize(polygonArraySize)
217 return (PolygonType::size == 3 || mPolygonArray[n][3] ==
util::INVALID_IDX) ? 3 : 4;
223 const PointType& p = mPointArray[mPolygonArray[n][int(v)]];
224 pos[0] = double(p[0]);
225 pos[1] = double(p[1]);
226 pos[2] = double(p[2]);
230 PointType
const *
const mPointArray;
231 size_t const mPointArraySize;
232 PolygonType
const *
const mPolygonArray;
233 size_t const mPolygonArraySize;
261 template<
typename Gr
idType>
262 inline typename GridType::Ptr
264 const openvdb::math::Transform& xform,
265 const std::vector<Vec3s>& points,
266 const std::vector<Vec3I>& triangles,
270 template<
typename Gr
idType,
typename Interrupter>
271 inline typename GridType::Ptr
273 Interrupter& interrupter,
274 const openvdb::math::Transform& xform,
275 const std::vector<Vec3s>& points,
276 const std::vector<Vec3I>& triangles,
295 template<
typename Gr
idType>
296 inline typename GridType::Ptr
298 const openvdb::math::Transform& xform,
299 const std::vector<Vec3s>& points,
300 const std::vector<Vec4I>& quads,
304 template<
typename Gr
idType,
typename Interrupter>
305 inline typename GridType::Ptr
307 Interrupter& interrupter,
308 const openvdb::math::Transform& xform,
309 const std::vector<Vec3s>& points,
310 const std::vector<Vec4I>& quads,
330 template<
typename Gr
idType>
331 inline typename GridType::Ptr
333 const openvdb::math::Transform& xform,
334 const std::vector<Vec3s>& points,
335 const std::vector<Vec3I>& triangles,
336 const std::vector<Vec4I>& quads,
340 template<
typename Gr
idType,
typename Interrupter>
341 inline typename GridType::Ptr
343 Interrupter& interrupter,
344 const openvdb::math::Transform& xform,
345 const std::vector<Vec3s>& points,
346 const std::vector<Vec3I>& triangles,
347 const std::vector<Vec4I>& quads,
369 template<
typename Gr
idType>
370 inline typename GridType::Ptr
372 const openvdb::math::Transform& xform,
373 const std::vector<Vec3s>& points,
374 const std::vector<Vec3I>& triangles,
375 const std::vector<Vec4I>& quads,
380 template<
typename Gr
idType,
typename Interrupter>
381 inline typename GridType::Ptr
383 Interrupter& interrupter,
384 const openvdb::math::Transform& xform,
385 const std::vector<Vec3s>& points,
386 const std::vector<Vec3I>& triangles,
387 const std::vector<Vec4I>& quads,
406 template<
typename Gr
idType>
407 inline typename GridType::Ptr
409 const openvdb::math::Transform& xform,
410 const std::vector<Vec3s>& points,
411 const std::vector<Vec3I>& triangles,
412 const std::vector<Vec4I>& quads,
416 template<
typename Gr
idType,
typename Interrupter>
417 inline typename GridType::Ptr
419 Interrupter& interrupter,
420 const openvdb::math::Transform& xform,
421 const std::vector<Vec3s>& points,
422 const std::vector<Vec3I>& triangles,
423 const std::vector<Vec4I>& quads,
436 template<
typename Gr
idType,
typename VecType>
437 inline typename GridType::Ptr
439 const openvdb::math::Transform& xform,
452 template <
typename FloatTreeT>
470 : mXDist(dist), mYDist(dist), mZDist(dist)
513 void convert(
const std::vector<Vec3s>& pointList,
const std::vector<Vec4I>& polygonList);
519 std::vector<Vec3d>& points, std::vector<Index32>& primitives);
538 namespace mesh_to_volume_internal {
540 template<
typename Po
intType>
545 : mPointsIn(pointsIn), mPointsOut(pointsOut), mXform(&xform)
549 void operator()(
const tbb::blocked_range<size_t>& range)
const {
553 for (
size_t n = range.begin(), N = range.end(); n < N; ++n) {
555 const PointType& wsP = mPointsIn[n];
556 pos[0] = double(wsP[0]);
557 pos[1] = double(wsP[1]);
558 pos[2] = double(wsP[2]);
560 pos = mXform->worldToIndex(pos);
562 PointType& isP = mPointsOut[n];
563 isP[0] =
typename PointType::value_type(pos[0]);
564 isP[1] =
typename PointType::value_type(pos[1]);
565 isP[2] =
typename PointType::value_type(pos[2]);
575 template<
typename ValueType>
578 static ValueType
epsilon() {
return ValueType(1e-7); }
586 template<
typename TreeType>
598 : mDistTree(&lhsDistTree)
599 , mIdxTree(&lhsIdxTree)
600 , mRhsDistNodes(rhsDistNodes)
601 , mRhsIdxNodes(rhsIdxNodes)
605 void operator()(
const tbb::blocked_range<size_t>& range)
const {
610 using DistValueType =
typename LeafNodeType::ValueType;
611 using IndexValueType =
typename Int32LeafNodeType::ValueType;
613 for (
size_t n = range.begin(), N = range.end(); n < N; ++n) {
615 const Coord& origin = mRhsDistNodes[n]->origin();
620 DistValueType* lhsDistData = lhsDistNode->buffer().data();
621 IndexValueType* lhsIdxData = lhsIdxNode->buffer().data();
623 const DistValueType* rhsDistData = mRhsDistNodes[n]->buffer().data();
624 const IndexValueType* rhsIdxData = mRhsIdxNodes[n]->buffer().data();
627 for (
Index32 offset = 0; offset < LeafNodeType::SIZE; ++offset) {
631 const DistValueType& lhsValue = lhsDistData[offset];
632 const DistValueType& rhsValue = rhsDistData[offset];
634 if (rhsValue < lhsValue) {
635 lhsDistNode->setValueOn(offset, rhsValue);
636 lhsIdxNode->setValueOn(offset, rhsIdxData[offset]);
638 lhsIdxNode->setValueOn(offset,
639 std::min(lhsIdxData[offset], rhsIdxData[offset]));
644 delete mRhsDistNodes[n];
645 delete mRhsIdxNodes[n];
651 TreeType *
const mDistTree;
662 template<
typename TreeType>
668 : mNodes(nodes.empty() ? nullptr : &nodes[0]), mCoordinates(coordinates)
672 void operator()(
const tbb::blocked_range<size_t>& range)
const {
673 for (
size_t n = range.begin(), N = range.end(); n < N; ++n) {
674 Coord& origin =
const_cast<Coord&
>(mNodes[n]->origin());
675 mCoordinates[n] = origin;
676 origin[0] =
static_cast<int>(n);
685 template<
typename TreeType>
691 : mNodes(nodes.empty() ? nullptr : &nodes[0]), mCoordinates(coordinates)
695 void operator()(
const tbb::blocked_range<size_t>& range)
const {
696 for (
size_t n = range.begin(), N = range.end(); n < N; ++n) {
697 Coord& origin =
const_cast<Coord&
>(mNodes[n]->origin());
698 origin[0] = mCoordinates[n][0];
707 template<
typename TreeType>
714 size_t* offsets,
size_t numNodes,
const CoordBBox& bbox)
716 , mCoordinates(coordinates)
718 , mNumNodes(numNodes)
728 void operator()(
const tbb::blocked_range<size_t>& range)
const {
730 size_t* offsetsNextX = mOffsets;
731 size_t* offsetsPrevX = mOffsets + mNumNodes;
732 size_t* offsetsNextY = mOffsets + mNumNodes * 2;
733 size_t* offsetsPrevY = mOffsets + mNumNodes * 3;
734 size_t* offsetsNextZ = mOffsets + mNumNodes * 4;
735 size_t* offsetsPrevZ = mOffsets + mNumNodes * 5;
740 for (
size_t n = range.begin(), N = range.end(); n < N; ++n) {
741 const Coord& origin = mCoordinates[n];
742 offsetsNextX[n] = findNeighbourNode(acc, origin,
Coord(LeafNodeType::DIM, 0, 0));
743 offsetsPrevX[n] = findNeighbourNode(acc, origin,
Coord(-LeafNodeType::DIM, 0, 0));
744 offsetsNextY[n] = findNeighbourNode(acc, origin,
Coord(0, LeafNodeType::DIM, 0));
745 offsetsPrevY[n] = findNeighbourNode(acc, origin,
Coord(0, -LeafNodeType::DIM, 0));
746 offsetsNextZ[n] = findNeighbourNode(acc, origin,
Coord(0, 0, LeafNodeType::DIM));
747 offsetsPrevZ[n] = findNeighbourNode(acc, origin,
Coord(0, 0, -LeafNodeType::DIM));
754 Coord ijk = start + step;
759 if (node)
return static_cast<size_t>(node->origin()[0]);
768 TreeType
const *
const mTree;
769 Coord const *
const mCoordinates;
770 size_t *
const mOffsets;
772 const size_t mNumNodes;
777 template<
typename TreeType>
786 mLeafNodes.reserve(tree.leafCount());
787 tree.getNodes(mLeafNodes);
789 if (mLeafNodes.empty())
return;
792 tree.evalLeafBoundingBox(bbox);
794 const tbb::blocked_range<size_t> range(0, mLeafNodes.size());
798 std::unique_ptr<Coord[]> coordinates{
new Coord[mLeafNodes.size()]};
799 tbb::parallel_for(range,
803 mOffsets.reset(
new size_t[mLeafNodes.size() * 6]);
807 tree, coordinates.get(), mOffsets.get(), mLeafNodes.size(), bbox));
813 size_t size()
const {
return mLeafNodes.size(); }
815 std::vector<LeafNodeType*>&
nodes() {
return mLeafNodes; }
816 const std::vector<LeafNodeType*>&
nodes()
const {
return mLeafNodes; }
820 const size_t*
offsetsPrevX()
const {
return mOffsets.get() + mLeafNodes.size(); }
822 const size_t*
offsetsNextY()
const {
return mOffsets.get() + mLeafNodes.size() * 2; }
823 const size_t*
offsetsPrevY()
const {
return mOffsets.get() + mLeafNodes.size() * 3; }
825 const size_t*
offsetsNextZ()
const {
return mOffsets.get() + mLeafNodes.size() * 4; }
826 const size_t*
offsetsPrevZ()
const {
return mOffsets.get() + mLeafNodes.size() * 5; }
829 std::vector<LeafNodeType*> mLeafNodes;
830 std::unique_ptr<size_t[]> mOffsets;
834 template<
typename TreeType>
847 : mStartNodeIndices(startNodeIndices.empty() ? nullptr : &startNodeIndices[0])
848 , mConnectivity(&connectivity)
853 void operator()(
const tbb::blocked_range<size_t>& range)
const {
855 std::vector<LeafNodeType*>& nodes = mConnectivity->nodes();
858 size_t idxA = 0, idxB = 1;
861 const size_t* nextOffsets = mConnectivity->offsetsNextZ();
862 const size_t* prevOffsets = mConnectivity->offsetsPrevZ();
868 step = LeafNodeType::DIM;
870 nextOffsets = mConnectivity->offsetsNextY();
871 prevOffsets = mConnectivity->offsetsPrevY();
873 }
else if (mAxis ==
X_AXIS) {
877 step = LeafNodeType::DIM * LeafNodeType::DIM;
879 nextOffsets = mConnectivity->offsetsNextX();
880 prevOffsets = mConnectivity->offsetsPrevX();
888 for (
size_t n = range.begin(), N = range.end(); n < N; ++n) {
890 size_t startOffset = mStartNodeIndices[n];
891 size_t lastOffset = startOffset;
895 for (a = 0; a < int(LeafNodeType::DIM); ++a) {
896 for (b = 0; b < int(LeafNodeType::DIM); ++b) {
898 pos = LeafNodeType::coordToOffset(ijk);
899 size_t offset = startOffset;
902 while ( offset != ConnectivityTable::INVALID_OFFSET &&
903 traceVoxelLine(*nodes[offset], pos, step) ) {
906 offset = nextOffsets[offset];
911 while (offset != ConnectivityTable::INVALID_OFFSET) {
913 offset = nextOffsets[offset];
918 pos += step * (LeafNodeType::DIM - 1);
919 while ( offset != ConnectivityTable::INVALID_OFFSET &&
920 traceVoxelLine(*nodes[offset], pos, -step)) {
921 offset = prevOffsets[offset];
933 bool isOutside =
true;
935 for (
Index i = 0; i < LeafNodeType::DIM; ++i) {
943 if (!(dist >
ValueType(0.75))) isOutside =
false;
956 size_t const *
const mStartNodeIndices;
963 template<
typename LeafNodeType>
967 using ValueType =
typename LeafNodeType::ValueType;
968 using Queue = std::deque<Index>;
971 ValueType* data = node.buffer().data();
975 for (
Index pos = 0; pos < LeafNodeType::SIZE; ++pos) {
976 if (data[pos] < 0.0) seedPoints.push_back(pos);
979 if (seedPoints.empty())
return;
982 for (Queue::iterator it = seedPoints.begin(); it != seedPoints.end(); ++it) {
983 ValueType& dist = data[*it];
990 Index pos(0), nextPos(0);
992 while (!seedPoints.empty()) {
994 pos = seedPoints.back();
995 seedPoints.pop_back();
997 ValueType& dist = data[pos];
999 if (!(dist < ValueType(0.0))) {
1003 ijk = LeafNodeType::offsetToLocalCoord(pos);
1006 nextPos = pos - LeafNodeType::DIM * LeafNodeType::DIM;
1007 if (data[nextPos] > ValueType(0.75)) seedPoints.push_back(nextPos);
1010 if (ijk[0] != (LeafNodeType::DIM - 1)) {
1011 nextPos = pos + LeafNodeType::DIM * LeafNodeType::DIM;
1012 if (data[nextPos] > ValueType(0.75)) seedPoints.push_back(nextPos);
1016 nextPos = pos - LeafNodeType::DIM;
1017 if (data[nextPos] > ValueType(0.75)) seedPoints.push_back(nextPos);
1020 if (ijk[1] != (LeafNodeType::DIM - 1)) {
1021 nextPos = pos + LeafNodeType::DIM;
1022 if (data[nextPos] > ValueType(0.75)) seedPoints.push_back(nextPos);
1027 if (data[nextPos] > ValueType(0.75)) seedPoints.push_back(nextPos);
1030 if (ijk[2] != (LeafNodeType::DIM - 1)) {
1032 if (data[nextPos] > ValueType(0.75)) seedPoints.push_back(nextPos);
1039 template<
typename LeafNodeType>
1043 bool updatedNode =
false;
1045 using ValueType =
typename LeafNodeType::ValueType;
1046 ValueType* data = node.buffer().data();
1050 bool updatedSign =
true;
1051 while (updatedSign) {
1053 updatedSign =
false;
1055 for (
Index pos = 0; pos < LeafNodeType::SIZE; ++pos) {
1057 ValueType& dist = data[pos];
1059 if (!(dist < ValueType(0.0)) && dist > ValueType(0.75)) {
1061 ijk = LeafNodeType::offsetToLocalCoord(pos);
1064 if (ijk[2] != 0 && data[pos - 1] < ValueType(0.0)) {
1066 dist = ValueType(-dist);
1069 }
else if (ijk[2] != (LeafNodeType::DIM - 1) && data[pos + 1] < ValueType(0.0)) {
1071 dist = ValueType(-dist);
1074 }
else if (ijk[1] != 0 && data[pos - LeafNodeType::DIM] < ValueType(0.0)) {
1076 dist = ValueType(-dist);
1079 }
else if (ijk[1] != (LeafNodeType::DIM - 1)
1080 && data[pos + LeafNodeType::DIM] < ValueType(0.0))
1083 dist = ValueType(-dist);
1086 }
else if (ijk[0] != 0
1087 && data[pos - LeafNodeType::DIM * LeafNodeType::DIM] < ValueType(0.0))
1090 dist = ValueType(-dist);
1093 }
else if (ijk[0] != (LeafNodeType::DIM - 1)
1094 && data[pos + LeafNodeType::DIM * LeafNodeType::DIM] < ValueType(0.0))
1097 dist = ValueType(-dist);
1102 updatedNode |= updatedSign;
1109 template<
typename TreeType>
1117 : mNodes(nodes.empty() ? nullptr : &nodes[0])
1118 , mChangedNodeMask(changedNodeMask)
1123 for (
size_t n = range.begin(), N = range.end(); n < N; ++n) {
1124 if (mChangedNodeMask[n]) {
1126 mChangedNodeMask[n] =
scanFill(*mNodes[n]);
1136 template<
typename ValueType>
1139 FillArray(ValueType* array,
const ValueType v) : mArray(array), mValue(v) { }
1142 const ValueType v = mValue;
1143 for (
size_t n = range.begin(), N = range.end(); n < N; ++n) {
1153 template<
typename ValueType>
1155 fillArray(ValueType* array,
const ValueType val,
const size_t length)
1157 const auto grainSize = std::max<size_t>(
1158 length / tbb::task_scheduler_init::default_num_threads(), 1024);
1159 const tbb::blocked_range<size_t> range(0, length, grainSize);
1164 template<
typename TreeType>
1172 const bool* changedNodeMask,
bool* changedVoxelMask)
1173 : mNodes(nodes.empty() ? nullptr : &nodes[0])
1174 , mChangedNodeMask(changedNodeMask)
1175 , mChangedVoxelMask(changedVoxelMask)
1180 for (
size_t n = range.begin(), N = range.end(); n < N; ++n) {
1182 if (mChangedNodeMask[n]) {
1183 bool* mask = &mChangedVoxelMask[n * LeafNodeType::SIZE];
1185 ValueType* data = mNodes[n]->buffer().data();
1187 for (
Index pos = 0; pos < LeafNodeType::SIZE; ++pos) {
1203 template<
typename TreeType>
1212 bool* changedNodeMask,
bool* nodeMask,
bool* changedVoxelMask)
1213 : mConnectivity(&connectivity)
1214 , mChangedNodeMask(changedNodeMask)
1215 , mNodeMask(nodeMask)
1216 , mChangedVoxelMask(changedVoxelMask)
1222 for (
size_t n = range.begin(), N = range.end(); n < N; ++n) {
1224 if (!mChangedNodeMask[n]) {
1226 bool changedValue =
false;
1228 changedValue |= processZ(n,
true);
1229 changedValue |= processZ(n,
false);
1231 changedValue |= processY(n,
true);
1232 changedValue |= processY(n,
false);
1234 changedValue |= processX(n,
true);
1235 changedValue |= processX(n,
false);
1237 mNodeMask[n] = changedValue;
1245 const size_t offset =
1246 firstFace ? mConnectivity->offsetsPrevZ()[n] : mConnectivity->offsetsNextZ()[n];
1247 if (offset != ConnectivityTable::INVALID_OFFSET && mChangedNodeMask[offset]) {
1249 bool* mask = &mChangedVoxelMask[n * LeafNodeType::SIZE];
1251 const ValueType* lhsData = mConnectivity->nodes()[n]->buffer().data();
1252 const ValueType* rhsData = mConnectivity->nodes()[offset]->buffer().data();
1254 const Index lastOffset = LeafNodeType::DIM - 1;
1255 const Index lhsOffset =
1256 firstFace ? 0 : lastOffset, rhsOffset = firstFace ? lastOffset : 0;
1258 Index tmpPos(0), pos(0);
1259 bool changedValue =
false;
1261 for (
Index x = 0; x < LeafNodeType::DIM; ++x) {
1262 tmpPos = x << (2 * LeafNodeType::LOG2DIM);
1263 for (
Index y = 0; y < LeafNodeType::DIM; ++y) {
1264 pos = tmpPos + (y << LeafNodeType::LOG2DIM);
1266 if (lhsData[pos + lhsOffset] >
ValueType(0.75)) {
1267 if (rhsData[pos + rhsOffset] <
ValueType(0.0)) {
1268 changedValue =
true;
1269 mask[pos + lhsOffset] =
true;
1275 return changedValue;
1283 const size_t offset =
1284 firstFace ? mConnectivity->offsetsPrevY()[n] : mConnectivity->offsetsNextY()[n];
1285 if (offset != ConnectivityTable::INVALID_OFFSET && mChangedNodeMask[offset]) {
1287 bool* mask = &mChangedVoxelMask[n * LeafNodeType::SIZE];
1289 const ValueType* lhsData = mConnectivity->nodes()[n]->buffer().data();
1290 const ValueType* rhsData = mConnectivity->nodes()[offset]->buffer().data();
1292 const Index lastOffset = LeafNodeType::DIM * (LeafNodeType::DIM - 1);
1293 const Index lhsOffset =
1294 firstFace ? 0 : lastOffset, rhsOffset = firstFace ? lastOffset : 0;
1296 Index tmpPos(0), pos(0);
1297 bool changedValue =
false;
1299 for (
Index x = 0; x < LeafNodeType::DIM; ++x) {
1300 tmpPos = x << (2 * LeafNodeType::LOG2DIM);
1301 for (
Index z = 0; z < LeafNodeType::DIM; ++z) {
1304 if (lhsData[pos + lhsOffset] >
ValueType(0.75)) {
1305 if (rhsData[pos + rhsOffset] <
ValueType(0.0)) {
1306 changedValue =
true;
1307 mask[pos + lhsOffset] =
true;
1313 return changedValue;
1321 const size_t offset =
1322 firstFace ? mConnectivity->offsetsPrevX()[n] : mConnectivity->offsetsNextX()[n];
1323 if (offset != ConnectivityTable::INVALID_OFFSET && mChangedNodeMask[offset]) {
1325 bool* mask = &mChangedVoxelMask[n * LeafNodeType::SIZE];
1327 const ValueType* lhsData = mConnectivity->nodes()[n]->buffer().data();
1328 const ValueType* rhsData = mConnectivity->nodes()[offset]->buffer().data();
1330 const Index lastOffset = LeafNodeType::DIM * LeafNodeType::DIM * (LeafNodeType::DIM-1);
1331 const Index lhsOffset =
1332 firstFace ? 0 : lastOffset, rhsOffset = firstFace ? lastOffset : 0;
1334 Index tmpPos(0), pos(0);
1335 bool changedValue =
false;
1337 for (
Index y = 0; y < LeafNodeType::DIM; ++y) {
1338 tmpPos = y << LeafNodeType::LOG2DIM;
1339 for (
Index z = 0; z < LeafNodeType::DIM; ++z) {
1342 if (lhsData[pos + lhsOffset] >
ValueType(0.75)) {
1343 if (rhsData[pos + rhsOffset] <
ValueType(0.0)) {
1344 changedValue =
true;
1345 mask[pos + lhsOffset] =
true;
1351 return changedValue;
1366 template<
typename TreeType,
typename MeshDataAdapter>
1380 std::vector<LeafNodeType*>& distNodes,
1381 const TreeType& distTree,
1384 : mDistNodes(distNodes.empty() ? nullptr : &distNodes[0])
1385 , mDistTree(&distTree)
1386 , mIndexTree(&indexTree)
1400 Index xPos(0), yPos(0);
1401 Coord ijk, nijk, nodeMin, nodeMax;
1402 Vec3d cp, xyz, nxyz, dir1, dir2;
1404 LocalData& localData = mLocalDataTable->local();
1407 if (!points) points.reset(
new Vec3d[LeafNodeType::SIZE * 2]);
1410 if (!mask) mask.reset(
new bool[LeafNodeType::SIZE]);
1413 typename LeafNodeType::ValueOnCIter it;
1415 for (
size_t n = range.begin(), N = range.end(); n < N; ++n) {
1421 const Int32* idxData = idxNode->buffer().data();
1423 nodeMin = node.origin();
1424 nodeMax = nodeMin.
offsetBy(LeafNodeType::DIM - 1);
1427 memset(mask.get(), 0,
sizeof(bool) * LeafNodeType::SIZE);
1429 for (it = node.cbeginValueOn(); it; ++it) {
1430 Index pos = it.pos();
1433 if (dist < 0.0 || dist > 0.75)
continue;
1435 ijk = node.offsetToGlobalCoord(pos);
1437 xyz[0] = double(ijk[0]);
1438 xyz[1] = double(ijk[1]);
1439 xyz[2] = double(ijk[2]);
1445 bool flipSign =
false;
1447 for (nijk[0] = bbox.
min()[0]; nijk[0] <= bbox.
max()[0] && !flipSign; ++nijk[0]) {
1448 xPos = (nijk[0] & (LeafNodeType::DIM - 1u)) << (2 * LeafNodeType::LOG2DIM);
1449 for (nijk[1]=bbox.
min()[1]; nijk[1] <= bbox.
max()[1] && !flipSign; ++nijk[1]) {
1450 yPos = xPos + ((nijk[1] & (LeafNodeType::DIM-1u)) << LeafNodeType::LOG2DIM);
1451 for (nijk[2] = bbox.
min()[2]; nijk[2] <= bbox.
max()[2]; ++nijk[2]) {
1452 pos = yPos + (nijk[2] & (LeafNodeType::DIM - 1u));
1454 const Int32& polyIdx = idxData[pos];
1459 const Index pointIndex = pos * 2;
1465 nxyz[0] = double(nijk[0]);
1466 nxyz[1] = double(nijk[1]);
1467 nxyz[2] = double(nijk[2]);
1469 Vec3d& point = points[pointIndex];
1471 point = closestPoint(nxyz, polyIdx);
1473 Vec3d& direction = points[pointIndex + 1];
1474 direction = nxyz - point;
1475 direction.normalize();
1478 dir1 = xyz - points[pointIndex];
1481 if (points[pointIndex + 1].dot(dir1) > 0.0) {
1492 for (
Int32 m = 0; m < 26; ++m) {
1496 nxyz[0] = double(nijk[0]);
1497 nxyz[1] = double(nijk[1]);
1498 nxyz[2] = double(nijk[2]);
1500 cp = closestPoint(nxyz, idxAcc.
getValue(nijk));
1508 if (dir2.dot(dir1) > 0.0) {
1524 Vec3d a, b, c, cp, uvw;
1526 const size_t polygon = size_t(polyIdx);
1527 mMesh->getIndexSpacePoint(polygon, 0, a);
1528 mMesh->getIndexSpacePoint(polygon, 1, b);
1529 mMesh->getIndexSpacePoint(polygon, 2, c);
1533 if (4 == mMesh->vertexCount(polygon)) {
1535 mMesh->getIndexSpacePoint(polygon, 3, b);
1539 if ((center - c).lengthSqr() < (center - cp).lengthSqr()) {
1549 TreeType
const *
const mDistTree;
1560 template<
typename LeafNodeType>
1564 using NodeT = LeafNodeType;
1566 const Coord ijk = NodeT::offsetToLocalCoord(pos);
1570 mask[0] = ijk[0] != (NodeT::DIM - 1);
1572 mask[1] = ijk[0] != 0;
1574 mask[2] = ijk[1] != (NodeT::DIM - 1);
1576 mask[3] = ijk[1] != 0;
1578 mask[4] = ijk[2] != (NodeT::DIM - 1);
1580 mask[5] = ijk[2] != 0;
1584 mask[6] = mask[0] && mask[5];
1586 mask[7] = mask[1] && mask[5];
1588 mask[8] = mask[0] && mask[4];
1590 mask[9] = mask[1] && mask[4];
1592 mask[10] = mask[0] && mask[2];
1594 mask[11] = mask[1] && mask[2];
1596 mask[12] = mask[0] && mask[3];
1598 mask[13] = mask[1] && mask[3];
1600 mask[14] = mask[3] && mask[4];
1602 mask[15] = mask[3] && mask[5];
1604 mask[16] = mask[2] && mask[4];
1606 mask[17] = mask[2] && mask[5];
1610 mask[18] = mask[1] && mask[3] && mask[5];
1612 mask[19] = mask[1] && mask[3] && mask[4];
1614 mask[20] = mask[0] && mask[3] && mask[4];
1616 mask[21] = mask[0] && mask[3] && mask[5];
1618 mask[22] = mask[1] && mask[2] && mask[5];
1620 mask[23] = mask[1] && mask[2] && mask[4];
1622 mask[24] = mask[0] && mask[2] && mask[4];
1624 mask[25] = mask[0] && mask[2] && mask[5];
1628 template<
typename Compare,
typename LeafNodeType>
1632 using NodeT = LeafNodeType;
1635 if (mask[5] && Compare::check(data[pos - 1]))
return true;
1637 if (mask[4] && Compare::check(data[pos + 1]))
return true;
1639 if (mask[3] && Compare::check(data[pos - NodeT::DIM]))
return true;
1641 if (mask[2] && Compare::check(data[pos + NodeT::DIM]))
return true;
1643 if (mask[1] && Compare::check(data[pos - NodeT::DIM * NodeT::DIM]))
return true;
1645 if (mask[0] && Compare::check(data[pos + NodeT::DIM * NodeT::DIM]))
return true;
1647 if (mask[6] && Compare::check(data[pos + NodeT::DIM * NodeT::DIM]))
return true;
1649 if (mask[7] && Compare::check(data[pos - NodeT::DIM * NodeT::DIM - 1]))
return true;
1651 if (mask[8] && Compare::check(data[pos + NodeT::DIM * NodeT::DIM + 1]))
return true;
1653 if (mask[9] && Compare::check(data[pos - NodeT::DIM * NodeT::DIM + 1]))
return true;
1655 if (mask[10] && Compare::check(data[pos + NodeT::DIM * NodeT::DIM + NodeT::DIM]))
return true;
1657 if (mask[11] && Compare::check(data[pos - NodeT::DIM * NodeT::DIM + NodeT::DIM]))
return true;
1659 if (mask[12] && Compare::check(data[pos + NodeT::DIM * NodeT::DIM - NodeT::DIM]))
return true;
1661 if (mask[13] && Compare::check(data[pos - NodeT::DIM * NodeT::DIM - NodeT::DIM]))
return true;
1663 if (mask[14] && Compare::check(data[pos - NodeT::DIM + 1]))
return true;
1665 if (mask[15] && Compare::check(data[pos - NodeT::DIM - 1]))
return true;
1667 if (mask[16] && Compare::check(data[pos + NodeT::DIM + 1]))
return true;
1669 if (mask[17] && Compare::check(data[pos + NodeT::DIM - 1]))
return true;
1671 if (mask[18] && Compare::check(data[pos - NodeT::DIM * NodeT::DIM - NodeT::DIM - 1]))
return true;
1673 if (mask[19] && Compare::check(data[pos - NodeT::DIM * NodeT::DIM - NodeT::DIM + 1]))
return true;
1675 if (mask[20] && Compare::check(data[pos + NodeT::DIM * NodeT::DIM - NodeT::DIM + 1]))
return true;
1677 if (mask[21] && Compare::check(data[pos + NodeT::DIM * NodeT::DIM - NodeT::DIM - 1]))
return true;
1679 if (mask[22] && Compare::check(data[pos - NodeT::DIM * NodeT::DIM + NodeT::DIM - 1]))
return true;
1681 if (mask[23] && Compare::check(data[pos - NodeT::DIM * NodeT::DIM + NodeT::DIM + 1]))
return true;
1683 if (mask[24] && Compare::check(data[pos + NodeT::DIM * NodeT::DIM + NodeT::DIM + 1]))
return true;
1685 if (mask[25] && Compare::check(data[pos + NodeT::DIM * NodeT::DIM + NodeT::DIM - 1]))
return true;
1691 template<
typename Compare,
typename AccessorType>
1695 for (
Int32 m = 0; m < 26; ++m) {
1705 template<
typename TreeType>
1715 , mNodes(nodes.empty() ? nullptr : &nodes[0])
1722 bool neighbourMask[26];
1724 for (
size_t n = range.begin(), N = range.end(); n < N; ++n) {
1729 typename LeafNodeType::ValueOnCIter it;
1730 for (it = node.cbeginValueOn(); it; ++it) {
1732 const Index pos = it.pos();
1735 if (dist < 0.0 || dist > 0.75)
continue;
1738 maskNodeInternalNeighbours<LeafNodeType>(pos, neighbourMask);
1740 const bool hasNegativeNeighbour =
1741 checkNeighbours<IsNegative, LeafNodeType>(pos, data, neighbourMask) ||
1742 checkNeighbours<IsNegative>(node.offsetToGlobalCoord(pos), acc, neighbourMask);
1744 if (!hasNegativeNeighbour) {
1757 template<
typename TreeType>
1768 : mNodes(nodes.empty() ? nullptr : &nodes[0])
1769 , mDistTree(&distTree)
1770 , mIndexTree(&indexTree)
1778 bool neighbourMask[26];
1780 for (
size_t n = range.begin(), N = range.end(); n < N; ++n) {
1783 ValueType* data = distNode.buffer().data();
1785 typename Int32TreeType::LeafNodeType* idxNode =
1788 typename LeafNodeType::ValueOnCIter it;
1789 for (it = distNode.cbeginValueOn(); it; ++it) {
1791 const Index pos = it.pos();
1793 if (!(data[pos] > 0.75))
continue;
1796 maskNodeInternalNeighbours<LeafNodeType>(pos, neighbourMask);
1798 const bool hasBoundaryNeighbour =
1799 checkNeighbours<Comp, LeafNodeType>(pos, data, neighbourMask) ||
1800 checkNeighbours<Comp>(distNode.offsetToGlobalCoord(pos),distAcc,neighbourMask);
1802 if (!hasBoundaryNeighbour) {
1803 distNode.setValueOff(pos);
1804 idxNode->setValueOff(pos);
1819 template<
typename NodeType>
1826 using NodeMaskType =
typename NodeType::NodeMaskType;
1828 for (
size_t n = range.begin(), N = range.end(); n < N; ++n) {
1829 const_cast<NodeMaskType&
>(mNodes[n]->getChildMask()).setOff();
1837 template<
typename TreeType>
1841 using RootNodeType =
typename TreeType::RootNodeType;
1842 using NodeChainType =
typename RootNodeType::NodeChainType;
1843 using InternalNodeType =
typename boost::mpl::at<NodeChainType, boost::mpl::int_<1> >::type;
1845 std::vector<InternalNodeType*> nodes;
1846 tree.getNodes(nodes);
1848 tbb::parallel_for(tbb::blocked_range<size_t>(0, nodes.size()),
1853 template<
typename TreeType>
1859 std::vector<LeafNodeType*>& overlappingNodes)
1860 : mLhsTree(&lhsTree)
1861 , mRhsTree(&rhsTree)
1862 , mNodes(&overlappingNodes)
1868 std::vector<LeafNodeType*> rhsLeafNodes;
1870 rhsLeafNodes.reserve(mRhsTree->leafCount());
1873 mRhsTree->stealNodes(rhsLeafNodes);
1877 for (
size_t n = 0, N = rhsLeafNodes.size(); n < N; ++n) {
1878 if (!acc.
probeLeaf(rhsLeafNodes[n]->origin())) {
1881 mNodes->push_back(rhsLeafNodes[n]);
1887 TreeType *
const mLhsTree;
1888 TreeType *
const mRhsTree;
1889 std::vector<LeafNodeType*> *
const mNodes;
1893 template<
typename DistTreeType,
typename IndexTreeType>
1896 DistTreeType& rhsDist, IndexTreeType& rhsIdx)
1898 using DistLeafNodeType =
typename DistTreeType::LeafNodeType;
1899 using IndexLeafNodeType =
typename IndexTreeType::LeafNodeType;
1901 std::vector<DistLeafNodeType*> overlappingDistNodes;
1902 std::vector<IndexLeafNodeType*> overlappingIdxNodes;
1905 tbb::task_group tasks;
1911 if (!overlappingDistNodes.empty() && !overlappingIdxNodes.empty()) {
1912 tbb::parallel_for(tbb::blocked_range<size_t>(0, overlappingDistNodes.size()),
1914 &overlappingDistNodes[0], &overlappingIdxNodes[0]));
1924 template<
typename TreeType>
1927 using Ptr = std::unique_ptr<VoxelizationData>;
1931 using UCharTreeType =
typename TreeType::template ValueConverter<unsigned char>::Type;
1942 , indexAcc(indexTree)
1943 , primIdTree(MaxPrimId)
1944 , primIdAcc(primIdTree)
1960 if (mPrimCount == MaxPrimId || primIdTree.leafCount() > 1000) {
1965 return mPrimCount++;
1970 enum { MaxPrimId = 100 };
1972 unsigned char mPrimCount;
1976 template<
typename TreeType,
typename MeshDataAdapter,
typename Interrupter = util::NullInterrupter>
1982 using DataTable = tbb::enumerable_thread_specific<typename VoxelizationDataType::Ptr>;
1986 Interrupter* interrupter =
nullptr)
1987 : mDataTable(&dataTable)
1989 , mInterrupter(interrupter)
2000 for (
size_t n = range.begin(), N = range.end(); n < N; ++n) {
2003 tbb::task::self().cancel_group_execution();
2007 const size_t numVerts = mMesh->vertexCount(n);
2010 if (numVerts == 3 || numVerts == 4) {
2012 prim.index =
Int32(n);
2014 mMesh->getIndexSpacePoint(n, 0, prim.a);
2015 mMesh->getIndexSpacePoint(n, 1, prim.b);
2016 mMesh->getIndexSpacePoint(n, 2, prim.c);
2018 evalTriangle(prim, *dataPtr);
2020 if (numVerts == 4) {
2021 mMesh->getIndexSpacePoint(n, 3, prim.b);
2022 evalTriangle(prim, *dataPtr);
2030 bool wasInterrupted()
const {
return mInterrupter && mInterrupter->wasInterrupted(); }
2032 struct Triangle {
Vec3d a, b, c;
Int32 index; };
2036 enum { POLYGON_LIMIT = 1000 };
2038 SubTask(
const Triangle& prim,
DataTable& dataTable,
2039 int subdivisionCount,
size_t polygonCount)
2040 : mLocalDataTable(&dataTable)
2042 , mSubdivisionCount(subdivisionCount)
2043 , mPolygonCount(polygonCount)
2047 void operator()()
const 2049 if (mSubdivisionCount <= 0 || mPolygonCount >= POLYGON_LIMIT) {
2054 voxelizeTriangle(mPrim, *dataPtr);
2057 spawnTasks(mPrim, *mLocalDataTable, mSubdivisionCount, mPolygonCount);
2062 Triangle
const mPrim;
2063 int const mSubdivisionCount;
2064 size_t const mPolygonCount;
2067 inline static int evalSubdivisionCount(
const Triangle& prim)
2069 const double ax = prim.a[0], bx = prim.b[0], cx = prim.c[0];
2072 const double ay = prim.a[1], by = prim.b[1], cy = prim.c[1];
2075 const double az = prim.a[2], bz = prim.b[2], cz = prim.c[2];
2078 return int(
std::max(dx,
std::max(dy, dz)) /
double(TreeType::LeafNodeType::DIM * 2));
2083 const size_t polygonCount = mMesh->polygonCount();
2084 const int subdivisionCount =
2085 polygonCount < SubTask::POLYGON_LIMIT ? evalSubdivisionCount(prim) : 0;
2087 if (subdivisionCount <= 0) {
2088 voxelizeTriangle(prim, data);
2090 spawnTasks(prim, *mDataTable, subdivisionCount, polygonCount);
2094 static void spawnTasks(
2095 const Triangle& mainPrim,
DataTable& dataTable,
int subdivisionCount,
size_t polygonCount)
2097 subdivisionCount -= 1;
2100 tbb::task_group tasks;
2102 const Vec3d ac = (mainPrim.a + mainPrim.c) * 0.5;
2103 const Vec3d bc = (mainPrim.b + mainPrim.c) * 0.5;
2104 const Vec3d ab = (mainPrim.a + mainPrim.b) * 0.5;
2107 prim.index = mainPrim.index;
2109 prim.a = mainPrim.a;
2112 tasks.run(SubTask(prim, dataTable, subdivisionCount, polygonCount));
2117 tasks.run(SubTask(prim, dataTable, subdivisionCount, polygonCount));
2120 prim.b = mainPrim.b;
2122 tasks.run(SubTask(prim, dataTable, subdivisionCount, polygonCount));
2126 prim.c = mainPrim.c;
2127 tasks.run(SubTask(prim, dataTable, subdivisionCount, polygonCount));
2134 std::deque<Coord> coordList;
2138 coordList.push_back(ijk);
2140 computeDistance(ijk, prim, data);
2145 while (!coordList.empty()) {
2146 ijk = coordList.back();
2147 coordList.pop_back();
2149 for (
Int32 i = 0; i < 26; ++i) {
2153 if(computeDistance(nijk, prim, data)) coordList.push_back(nijk);
2161 Vec3d uvw, voxelCenter(ijk[0], ijk[1], ijk[2]);
2163 using ValueType =
typename TreeType::ValueType;
2165 const ValueType dist = ValueType((voxelCenter -
2170 if (dist < oldDist) {
2179 return !(dist > 0.75);
2184 Interrupter *
const mInterrupter;
2191 template<
typename TreeType>
2197 using BoolTreeType =
typename TreeType::template ValueConverter<bool>::Type;
2201 std::vector<BoolLeafNodeType*>& lhsNodes)
2202 : mRhsTree(&rhsTree), mLhsNodes(lhsNodes.empty() ? nullptr : &lhsNodes[0])
2210 for (
size_t n = range.begin(), N = range.end(); n < N; ++n) {
2215 if (rhsNode) lhsNode->topologyDifference(*rhsNode,
false);
2220 TreeType
const *
const mRhsTree;
2225 template<
typename LeafNodeTypeA,
typename LeafNodeTypeB>
2229 : mNodesA(nodesA.empty() ? nullptr : &nodesA[0])
2230 , mNodesB(nodesB.empty() ? nullptr : &nodesB[0])
2235 for (
size_t n = range.begin(), N = range.end(); n < N; ++n) {
2236 mNodesA[n]->topologyUnion(*mNodesB[n]);
2241 LeafNodeTypeA **
const mNodesA;
2242 LeafNodeTypeB **
const mNodesB;
2246 template<
typename TreeType>
2251 using BoolTreeType =
typename TreeType::template ValueConverter<bool>::Type;
2255 std::vector<LeafNodeType*>& nodes)
2257 , mNodes(nodes.empty() ? nullptr : &nodes[0])
2258 , mLocalMaskTree(false)
2259 , mMaskTree(&maskTree)
2265 , mNodes(rhs.mNodes)
2266 , mLocalMaskTree(false)
2267 , mMaskTree(&mLocalMaskTree)
2273 using Iterator =
typename LeafNodeType::ValueOnCIter;
2278 Coord ijk, nijk, localCorod;
2281 for (
size_t n = range.begin(); n != range.end(); ++n) {
2285 CoordBBox bbox = node.getNodeBoundingBox();
2290 for (Iterator it = node.cbeginValueOn(); it; ++it) {
2291 ijk = it.getCoord();
2294 localCorod = LeafNodeType::offsetToLocalCoord(pos);
2296 if (localCorod[2] <
int(LeafNodeType::DIM - 1)) {
2298 if (!node.isValueOn(npos)) maskNode.setValueOn(npos);
2304 if (localCorod[2] > 0) {
2306 if (!node.isValueOn(npos)) maskNode.setValueOn(npos);
2312 if (localCorod[1] <
int(LeafNodeType::DIM - 1)) {
2313 npos = pos + LeafNodeType::DIM;
2314 if (!node.isValueOn(npos)) maskNode.setValueOn(npos);
2320 if (localCorod[1] > 0) {
2321 npos = pos - LeafNodeType::DIM;
2322 if (!node.isValueOn(npos)) maskNode.setValueOn(npos);
2328 if (localCorod[0] <
int(LeafNodeType::DIM - 1)) {
2329 npos = pos + LeafNodeType::DIM * LeafNodeType::DIM;
2330 if (!node.isValueOn(npos)) maskNode.setValueOn(npos);
2336 if (localCorod[0] > 0) {
2337 npos = pos - LeafNodeType::DIM * LeafNodeType::DIM;
2338 if (!node.isValueOn(npos)) maskNode.setValueOn(npos);
2350 TreeType
const *
const mTree;
2359 template<
typename TreeType,
typename MeshDataAdapter>
2367 using BoolTreeType =
typename TreeType::template ValueConverter<bool>::Type;
2378 : idx(idx_), x(x_), y(y_), z(z_), dist(dist_)
2388 std::vector<BoolLeafNodeType*>& maskNodes,
2396 : mMaskNodes(maskNodes.empty() ? nullptr : &maskNodes[0])
2397 , mMaskTree(&maskTree)
2398 , mDistTree(&distTree)
2399 , mIndexTree(&indexTree)
2401 , mNewMaskTree(false)
2403 , mUpdatedDistNodes()
2405 , mUpdatedIndexNodes()
2406 , mExteriorBandWidth(exteriorBandWidth)
2407 , mInteriorBandWidth(interiorBandWidth)
2408 , mVoxelSize(voxelSize)
2413 : mMaskNodes(rhs.mMaskNodes)
2414 , mMaskTree(rhs.mMaskTree)
2415 , mDistTree(rhs.mDistTree)
2416 , mIndexTree(rhs.mIndexTree)
2418 , mNewMaskTree(false)
2420 , mUpdatedDistNodes()
2422 , mUpdatedIndexNodes()
2423 , mExteriorBandWidth(rhs.mExteriorBandWidth)
2424 , mInteriorBandWidth(rhs.mInteriorBandWidth)
2425 , mVoxelSize(rhs.mVoxelSize)
2431 mDistNodes.insert(mDistNodes.end(), rhs.mDistNodes.begin(), rhs.mDistNodes.end());
2432 mIndexNodes.insert(mIndexNodes.end(), rhs.mIndexNodes.begin(), rhs.mIndexNodes.end());
2434 mUpdatedDistNodes.insert(mUpdatedDistNodes.end(),
2435 rhs.mUpdatedDistNodes.begin(), rhs.mUpdatedDistNodes.end());
2437 mUpdatedIndexNodes.insert(mUpdatedIndexNodes.end(),
2438 rhs.mUpdatedIndexNodes.begin(), rhs.mUpdatedIndexNodes.end());
2440 mNewMaskTree.merge(rhs.mNewMaskTree);
2449 std::vector<Fragment> fragments;
2450 fragments.reserve(256);
2452 std::unique_ptr<LeafNodeType> newDistNodePt;
2453 std::unique_ptr<Int32LeafNodeType> newIndexNodePt;
2455 for (
size_t n = range.begin(), N = range.end(); n < N; ++n) {
2458 if (maskNode.isEmpty())
continue;
2462 const Coord& origin = maskNode.origin();
2467 assert(!distNodePt == !indexNodePt);
2469 bool usingNewNodes =
false;
2471 if (!distNodePt && !indexNodePt) {
2475 if (!newDistNodePt.get() && !newIndexNodePt.get()) {
2476 newDistNodePt.reset(
new LeafNodeType(origin, backgroundDist));
2480 if ((backgroundDist <
ValueType(0.0)) !=
2481 (newDistNodePt->getValue(0) <
ValueType(0.0))) {
2482 newDistNodePt->buffer().fill(backgroundDist);
2485 newDistNodePt->setOrigin(origin);
2486 newIndexNodePt->setOrigin(origin);
2489 distNodePt = newDistNodePt.get();
2490 indexNodePt = newIndexNodePt.get();
2492 usingNewNodes =
true;
2499 for (
typename BoolLeafNodeType::ValueOnIter it = maskNode.beginValueOn(); it; ++it) {
2500 bbox.
expand(it.getCoord());
2505 gatherFragments(fragments, bbox, distAcc, indexAcc);
2510 bbox = maskNode.getNodeBoundingBox();
2512 bool updatedLeafNodes =
false;
2514 for (
typename BoolLeafNodeType::ValueOnIter it = maskNode.beginValueOn(); it; ++it) {
2516 const Coord ijk = it.getCoord();
2518 if (updateVoxel(ijk, 5, fragments, *distNodePt, *indexNodePt, &updatedLeafNodes)) {
2520 for (
Int32 i = 0; i < 6; ++i) {
2523 mask.setOn(BoolLeafNodeType::coordToOffset(nijk));
2529 for (
Int32 i = 6; i < 26; ++i) {
2532 mask.setOn(BoolLeafNodeType::coordToOffset(nijk));
2538 if (updatedLeafNodes) {
2541 mask -= indexNodePt->getValueMask();
2543 for (
typename NodeMaskType::OnIterator it = mask.beginOn(); it; ++it) {
2545 const Index pos = it.pos();
2546 const Coord ijk = maskNode.origin() + LeafNodeType::offsetToLocalCoord(pos);
2548 if (updateVoxel(ijk, 6, fragments, *distNodePt, *indexNodePt)) {
2549 for (
Int32 i = 0; i < 6; ++i) {
2556 if (usingNewNodes) {
2557 newDistNodePt->topologyUnion(*newIndexNodePt);
2558 mDistNodes.push_back(newDistNodePt.release());
2559 mIndexNodes.push_back(newIndexNodePt.release());
2561 mUpdatedDistNodes.push_back(distNodePt);
2562 mUpdatedIndexNodes.push_back(indexNodePt);
2582 gatherFragments(std::vector<Fragment>& fragments,
const CoordBBox& bbox,
2586 const Coord nodeMin = bbox.
min() & ~(LeafNodeType::DIM - 1);
2587 const Coord nodeMax = bbox.
max() & ~(LeafNodeType::DIM - 1);
2592 for (ijk[0] = nodeMin[0]; ijk[0] <= nodeMax[0]; ijk[0] += LeafNodeType::DIM) {
2593 for (ijk[1] = nodeMin[1]; ijk[1] <= nodeMax[1]; ijk[1] += LeafNodeType::DIM) {
2594 for (ijk[2] = nodeMin[2]; ijk[2] <= nodeMax[2]; ijk[2] += LeafNodeType::DIM) {
2598 ijk.
offsetBy(LeafNodeType::DIM - 1));
2599 gatherFragments(fragments, region, *distleaf, *indexAcc.
probeLeaf(ijk));
2605 std::sort(fragments.begin(), fragments.end());
2609 gatherFragments(std::vector<Fragment>& fragments,
const CoordBBox& bbox,
2612 const typename LeafNodeType::NodeMaskType& mask = distLeaf.getValueMask();
2613 const ValueType* distData = distLeaf.buffer().data();
2614 const Int32* idxData = idxLeaf.buffer().data();
2616 for (
int x = bbox.
min()[0]; x <= bbox.
max()[0]; ++x) {
2617 const Index xPos = (x & (LeafNodeType::DIM - 1u)) << (2 * LeafNodeType::LOG2DIM);
2618 for (
int y = bbox.
min()[1]; y <= bbox.
max()[1]; ++y) {
2619 const Index yPos = xPos + ((y & (LeafNodeType::DIM - 1u)) << LeafNodeType::LOG2DIM);
2620 for (
int z = bbox.
min()[2]; z <= bbox.
max()[2]; ++z) {
2621 const Index pos = yPos + (z & (LeafNodeType::DIM - 1u));
2622 if (mask.isOn(pos)) {
2623 fragments.push_back(
Fragment(idxData[pos],x,y,z, std::abs(distData[pos])));
2633 computeDistance(
const Coord& ijk,
const Int32 manhattanLimit,
2634 const std::vector<Fragment>& fragments,
Int32& closestPrimIdx)
const 2636 Vec3d a, b, c, uvw, voxelCenter(ijk[0], ijk[1], ijk[2]);
2640 for (
size_t n = 0, N = fragments.size(); n < N; ++n) {
2642 const Fragment& fragment = fragments[n];
2643 if (lastIdx == fragment.
idx)
continue;
2645 const Int32 dx = std::abs(fragment.
x - ijk[0]);
2646 const Int32 dy = std::abs(fragment.
y - ijk[1]);
2647 const Int32 dz = std::abs(fragment.
z - ijk[2]);
2649 const Int32 manhattan = dx + dy + dz;
2650 if (manhattan > manhattanLimit)
continue;
2652 lastIdx = fragment.
idx;
2654 const size_t polygon = size_t(lastIdx);
2656 mMesh->getIndexSpacePoint(polygon, 0, a);
2657 mMesh->getIndexSpacePoint(polygon, 1, b);
2658 mMesh->getIndexSpacePoint(polygon, 2, c);
2660 primDist = (voxelCenter -
2664 if (4 == mMesh->vertexCount(polygon)) {
2666 mMesh->getIndexSpacePoint(polygon, 3, b);
2669 a, b, c, voxelCenter, uvw)).lengthSqr();
2671 if (tmpDist < primDist) primDist = tmpDist;
2674 if (primDist < dist) {
2676 closestPrimIdx = lastIdx;
2680 return ValueType(std::sqrt(dist)) * mVoxelSize;
2686 updateVoxel(
const Coord& ijk,
const Int32 manhattanLimit,
2687 const std::vector<Fragment>& fragments,
2690 Int32 closestPrimIdx = 0;
2691 const ValueType distance = computeDistance(ijk, manhattanLimit, fragments, closestPrimIdx);
2693 const Index pos = LeafNodeType::coordToOffset(ijk);
2694 const bool inside = distLeaf.getValue(pos) <
ValueType(0.0);
2696 bool activateNeighbourVoxels =
false;
2698 if (!inside && distance < mExteriorBandWidth) {
2699 if (updatedLeafNodes) *updatedLeafNodes =
true;
2700 activateNeighbourVoxels = (distance + mVoxelSize) < mExteriorBandWidth;
2701 distLeaf.setValueOnly(pos, distance);
2702 idxLeaf.setValueOn(pos, closestPrimIdx);
2703 }
else if (inside && distance < mInteriorBandWidth) {
2704 if (updatedLeafNodes) *updatedLeafNodes =
true;
2705 activateNeighbourVoxels = (distance + mVoxelSize) < mInteriorBandWidth;
2706 distLeaf.setValueOnly(pos, -distance);
2707 idxLeaf.setValueOn(pos, closestPrimIdx);
2710 return activateNeighbourVoxels;
2717 TreeType *
const mDistTree;
2724 std::vector<LeafNodeType*> mDistNodes, mUpdatedDistNodes;
2725 std::vector<Int32LeafNodeType*> mIndexNodes, mUpdatedIndexNodes;
2727 const ValueType mExteriorBandWidth, mInteriorBandWidth, mVoxelSize;
2731 template<
typename TreeType>
2735 AddNodes(TreeType& tree, std::vector<LeafNodeType*>& nodes)
2736 : mTree(&tree) , mNodes(&nodes)
2742 std::vector<LeafNodeType*>& nodes = *mNodes;
2743 for (
size_t n = 0, N = nodes.size(); n < N; ++n) {
2753 template<
typename TreeType,
typename Int32TreeType,
typename BoolTreeType,
typename MeshDataAdapter>
2757 Int32TreeType& indexTree,
2758 BoolTreeType& maskTree,
2759 std::vector<typename BoolTreeType::LeafNodeType*>& maskNodes,
2761 typename TreeType::ValueType exteriorBandWidth,
2762 typename TreeType::ValueType interiorBandWidth,
2763 typename TreeType::ValueType voxelSize)
2766 distTree, indexTree, mesh, exteriorBandWidth, interiorBandWidth, voxelSize);
2768 tbb::parallel_reduce(tbb::blocked_range<size_t>(0, maskNodes.size()), expandOp);
2770 tbb::parallel_for(tbb::blocked_range<size_t>(0, expandOp.
updatedIndexNodes().size()),
2774 tbb::task_group tasks;
2788 template<
typename TreeType>
2797 , mVoxelSize(voxelSize)
2798 , mUnsigned(unsignedDist)
2804 typename LeafNodeType::ValueOnIter iter;
2806 const bool udf = mUnsigned;
2807 const ValueType w[2] = { -mVoxelSize, mVoxelSize };
2809 for (
size_t n = range.begin(), N = range.end(); n < N; ++n) {
2811 for (iter = mNodes[n]->beginValueOn(); iter; ++iter) {
2813 val = w[udf || (val <
ValueType(0.0))] * std::sqrt(std::abs(val));
2821 const bool mUnsigned;
2826 template<
typename TreeType>
2834 : mNodes(nodes.empty() ? nullptr : &nodes[0])
2835 , mExBandWidth(exBandWidth)
2836 , mInBandWidth(inBandWidth)
2842 typename LeafNodeType::ValueOnIter iter;
2846 for (
size_t n = range.begin(), N = range.end(); n < N; ++n) {
2848 for (iter = mNodes[n]->beginValueOn(); iter; ++iter) {
2852 const bool inside = val <
ValueType(0.0);
2854 if (inside && !(val > inVal)) {
2857 }
else if (!inside && !(val < exVal)) {
2867 const ValueType mExBandWidth, mInBandWidth;
2871 template<
typename TreeType>
2878 : mNodes(nodes.empty() ? nullptr : &nodes[0]), mOffset(offset)
2886 for (
size_t n = range.begin(), N = range.end(); n < N; ++n) {
2888 typename LeafNodeType::ValueOnIter iter = mNodes[n]->beginValueOn();
2890 for (; iter; ++iter) {
2903 template<
typename TreeType>
2909 Renormalize(
const TreeType& tree,
const std::vector<LeafNodeType*>& nodes,
2912 , mNodes(nodes.empty() ? nullptr : &nodes[0])
2914 , mVoxelSize(voxelSize)
2929 for (
size_t n = range.begin(), N = range.end(); n < N; ++n) {
2931 ValueType* bufferData = &mBuffer[n * LeafNodeType::SIZE];
2933 typename LeafNodeType::ValueOnCIter iter = mNodes[n]->cbeginValueOn();
2934 for (; iter; ++iter) {
2938 ijk = iter.getCoord();
2953 bufferData[iter.pos()] = phi0 - dx * S * diff;
2959 TreeType
const *
const mTree;
2967 template<
typename TreeType>
2974 : mNodes(nodes.empty() ? nullptr : &nodes[0]), mBuffer(buffer)
2980 for (
size_t n = range.begin(), N = range.end(); n < N; ++n) {
2982 const ValueType* bufferData = &mBuffer[n * LeafNodeType::SIZE];
2984 typename LeafNodeType::ValueOnIter iter = mNodes[n]->beginValueOn();
2986 for (; iter; ++iter) {
2988 val =
std::min(val, bufferData[iter.pos()]);
3007 template <
typename FloatTreeT>
3013 ConnectivityTable nodeConnectivity(tree);
3015 std::vector<size_t> zStartNodes, yStartNodes, xStartNodes;
3017 for (
size_t n = 0; n < nodeConnectivity.size(); ++n) {
3018 if (ConnectivityTable::INVALID_OFFSET == nodeConnectivity.offsetsPrevX()[n]) {
3019 xStartNodes.push_back(n);
3022 if (ConnectivityTable::INVALID_OFFSET == nodeConnectivity.offsetsPrevY()[n]) {
3023 yStartNodes.push_back(n);
3026 if (ConnectivityTable::INVALID_OFFSET == nodeConnectivity.offsetsPrevZ()[n]) {
3027 zStartNodes.push_back(n);
3033 tbb::parallel_for(tbb::blocked_range<size_t>(0, zStartNodes.size()),
3036 tbb::parallel_for(tbb::blocked_range<size_t>(0, yStartNodes.size()),
3039 tbb::parallel_for(tbb::blocked_range<size_t>(0, xStartNodes.size()),
3042 const size_t numLeafNodes = nodeConnectivity.size();
3043 const size_t numVoxels = numLeafNodes * FloatTreeT::LeafNodeType::SIZE;
3045 std::unique_ptr<bool[]> changedNodeMaskA{
new bool[numLeafNodes]};
3046 std::unique_ptr<bool[]> changedNodeMaskB{
new bool[numLeafNodes]};
3047 std::unique_ptr<bool[]> changedVoxelMask{
new bool[numVoxels]};
3053 const tbb::blocked_range<size_t> nodeRange(0, numLeafNodes);
3055 bool nodesUpdated =
false;
3058 nodeConnectivity.nodes(), changedNodeMaskA.get()));
3061 nodeConnectivity, changedNodeMaskA.get(), changedNodeMaskB.get(),
3062 changedVoxelMask.get()));
3064 changedNodeMaskA.swap(changedNodeMaskB);
3066 nodesUpdated =
false;
3067 for (
size_t n = 0; n < numLeafNodes; ++n) {
3068 nodesUpdated |= changedNodeMaskA[n];
3069 if (nodesUpdated)
break;
3074 nodeConnectivity.nodes(), changedNodeMaskA.get(), changedVoxelMask.get()));
3076 }
while (nodesUpdated);
3084 template <
typename Gr
idType,
typename MeshDataAdapter,
typename Interrupter>
3085 inline typename GridType::Ptr
3087 Interrupter& interrupter,
3090 float exteriorBandWidth,
3091 float interiorBandWidth,
3095 using GridTypePtr =
typename GridType::Ptr;
3096 using TreeType =
typename GridType::TreeType;
3097 using LeafNodeType =
typename TreeType::LeafNodeType;
3098 using ValueType =
typename GridType::ValueType;
3101 using Int32TreeType =
typename Int32GridType::TreeType;
3103 using BoolTreeType =
typename TreeType::template ValueConverter<bool>::Type;
3110 distGrid->setTransform(transform.
copy());
3112 ValueType exteriorWidth = ValueType(exteriorBandWidth);
3113 ValueType interiorWidth = ValueType(interiorBandWidth);
3117 if (!std::isfinite(exteriorWidth) || std::isnan(interiorWidth)) {
3118 std::stringstream msg;
3119 msg <<
"Illegal narrow band width: exterior = " << exteriorWidth
3120 <<
", interior = " << interiorWidth;
3125 const ValueType voxelSize = ValueType(transform.
voxelSize()[0]);
3127 if (!std::isfinite(voxelSize) ||
math::isZero(voxelSize)) {
3128 std::stringstream msg;
3129 msg <<
"Illegal transform, voxel size = " << voxelSize;
3135 exteriorWidth *= voxelSize;
3139 interiorWidth *= voxelSize;
3147 Int32GridType* indexGrid =
nullptr;
3149 typename Int32GridType::Ptr temporaryIndexGrid;
3151 if (polygonIndexGrid) {
3152 indexGrid = polygonIndexGrid;
3155 indexGrid = temporaryIndexGrid.get();
3158 indexGrid->newTree();
3159 indexGrid->setTransform(transform.
copy());
3161 if (computeSignedDistanceField) {
3165 interiorWidth = ValueType(0.0);
3168 TreeType& distTree = distGrid->tree();
3169 Int32TreeType& indexTree = indexGrid->tree();
3178 using DataTable = tbb::enumerable_thread_specific<typename VoxelizationDataType::Ptr>;
3184 const tbb::blocked_range<size_t> polygonRange(0, mesh.polygonCount());
3186 tbb::parallel_for(polygonRange, Voxelizer(data, mesh, &interrupter));
3188 for (
typename DataTable::iterator i = data.begin(); i != data.end(); ++i) {
3189 VoxelizationDataType& dataItem = **i;
3191 distTree, indexTree, dataItem.distTree, dataItem.indexTree);
3197 if (interrupter.wasInterrupted(30))
return distGrid;
3204 if (computeSignedDistanceField) {
3209 std::vector<LeafNodeType*> nodes;
3210 nodes.reserve(distTree.leafCount());
3211 distTree.getNodes(nodes);
3213 const tbb::blocked_range<size_t> nodeRange(0, nodes.size());
3218 tbb::parallel_for(nodeRange, SignOp(nodes, distTree, indexTree, mesh));
3220 if (interrupter.wasInterrupted(45))
return distGrid;
3223 if (removeIntersectingVoxels) {
3225 tbb::parallel_for(nodeRange,
3228 tbb::parallel_for(nodeRange,
3230 nodes, distTree, indexTree));
3237 if (interrupter.wasInterrupted(50))
return distGrid;
3239 if (distTree.activeVoxelCount() == 0) {
3241 distTree.root().setBackground(exteriorWidth,
false);
3247 std::vector<LeafNodeType*> nodes;
3248 nodes.reserve(distTree.leafCount());
3249 distTree.getNodes(nodes);
3251 tbb::parallel_for(tbb::blocked_range<size_t>(0, nodes.size()),
3253 nodes, voxelSize, !computeSignedDistanceField));
3257 if (computeSignedDistanceField) {
3258 distTree.root().setBackground(exteriorWidth,
false);
3264 if (interrupter.wasInterrupted(54))
return distGrid;
3271 const ValueType minBandWidth = voxelSize * ValueType(2.0);
3273 if (interiorWidth > minBandWidth || exteriorWidth > minBandWidth) {
3276 BoolTreeType maskTree(
false);
3279 std::vector<LeafNodeType*> nodes;
3280 nodes.reserve(distTree.leafCount());
3281 distTree.getNodes(nodes);
3284 tbb::parallel_reduce(tbb::blocked_range<size_t>(0, nodes.size()), op);
3290 float progress = 54.0f, step = 0.0f;
3292 2.0 * std::ceil((
std::max(interiorWidth, exteriorWidth) - minBandWidth) / voxelSize);
3294 if (estimated <
double(maxIterations)) {
3295 maxIterations = unsigned(estimated);
3296 step = 40.0f / float(maxIterations);
3299 std::vector<typename BoolTreeType::LeafNodeType*> maskNodes;
3304 if (interrupter.wasInterrupted(
int(progress)))
return distGrid;
3306 const size_t maskNodeCount = maskTree.leafCount();
3307 if (maskNodeCount == 0)
break;
3310 maskNodes.reserve(maskNodeCount);
3311 maskTree.getNodes(maskNodes);
3313 const tbb::blocked_range<size_t> range(0, maskNodes.size());
3315 tbb::parallel_for(range,
3319 mesh, exteriorWidth, interiorWidth, voxelSize);
3321 if ((++count) >= maxIterations)
break;
3326 if (interrupter.wasInterrupted(94))
return distGrid;
3328 if (!polygonIndexGrid) indexGrid->clear();
3336 if (computeSignedDistanceField && renormalizeValues) {
3338 std::vector<LeafNodeType*> nodes;
3339 nodes.reserve(distTree.leafCount());
3340 distTree.getNodes(nodes);
3342 std::unique_ptr<ValueType[]> buffer{
new ValueType[LeafNodeType::SIZE * nodes.size()]};
3344 const ValueType offset = ValueType(0.8 * voxelSize);
3346 tbb::parallel_for(tbb::blocked_range<size_t>(0, nodes.size()),
3349 tbb::parallel_for(tbb::blocked_range<size_t>(0, nodes.size()),
3351 distTree, nodes, buffer.get(), voxelSize));
3353 tbb::parallel_for(tbb::blocked_range<size_t>(0, nodes.size()),
3356 tbb::parallel_for(tbb::blocked_range<size_t>(0, nodes.size()),
3361 if (interrupter.wasInterrupted(99))
return distGrid;
3368 if (trimNarrowBand &&
std::min(interiorWidth, exteriorWidth) < voxelSize * ValueType(4.0)) {
3370 std::vector<LeafNodeType*> nodes;
3371 nodes.reserve(distTree.leafCount());
3372 distTree.getNodes(nodes);
3374 tbb::parallel_for(tbb::blocked_range<size_t>(0, nodes.size()),
3376 nodes, exteriorWidth, computeSignedDistanceField ? interiorWidth : exteriorWidth));
3379 distTree, exteriorWidth, computeSignedDistanceField ? -interiorWidth : -exteriorWidth);
3386 template <
typename Gr
idType,
typename MeshDataAdapter>
3387 inline typename GridType::Ptr
3391 float exteriorBandWidth,
3392 float interiorBandWidth,
3397 return meshToVolume<GridType>(nullInterrupter, mesh, transform,
3398 exteriorBandWidth, interiorBandWidth, flags, polygonIndexGrid);
3409 template<
typename Gr
idType,
typename Interrupter>
3410 inline typename std::enable_if<std::is_floating_point<typename GridType::ValueType>::value,
3411 typename GridType::Ptr>::type
3413 Interrupter& interrupter,
3414 const openvdb::math::Transform& xform,
3415 const std::vector<Vec3s>& points,
3416 const std::vector<Vec3I>& triangles,
3417 const std::vector<Vec4I>& quads,
3420 bool unsignedDistanceField =
false)
3422 if (points.empty()) {
3423 return typename GridType::Ptr(
new GridType(
typename GridType::ValueType(exBandWidth)));
3426 const size_t numPoints = points.size();
3427 std::unique_ptr<Vec3s[]> indexSpacePoints{
new Vec3s[numPoints]};
3430 tbb::parallel_for(tbb::blocked_range<size_t>(0, numPoints),
3432 &points[0], indexSpacePoints.get(), xform));
3436 if (quads.empty()) {
3439 mesh(indexSpacePoints.get(), numPoints, &triangles[0], triangles.size());
3441 return meshToVolume<GridType>(
3442 interrupter, mesh, xform, exBandWidth, inBandWidth, conversionFlags);
3444 }
else if (triangles.empty()) {
3447 mesh(indexSpacePoints.get(), numPoints, &quads[0], quads.size());
3449 return meshToVolume<GridType>(
3450 interrupter, mesh, xform, exBandWidth, inBandWidth, conversionFlags);
3455 const size_t numPrimitives = triangles.size() + quads.size();
3456 std::unique_ptr<Vec4I[]> prims{
new Vec4I[numPrimitives]};
3458 for (
size_t n = 0, N = triangles.size(); n < N; ++n) {
3459 const Vec3I& triangle = triangles[n];
3460 Vec4I& prim = prims[n];
3461 prim[0] = triangle[0];
3462 prim[1] = triangle[1];
3463 prim[2] = triangle[2];
3467 const size_t offset = triangles.size();
3468 for (
size_t n = 0, N = quads.size(); n < N; ++n) {
3469 prims[offset + n] = quads[n];
3473 mesh(indexSpacePoints.get(), numPoints, prims.get(), numPrimitives);
3475 return meshToVolume<GridType>(interrupter, mesh, xform,
3476 exBandWidth, inBandWidth, conversionFlags);
3482 template<
typename Gr
idType,
typename Interrupter>
3483 inline typename std::enable_if<!std::is_floating_point<typename GridType::ValueType>::value,
3484 typename GridType::Ptr>::type
3488 const std::vector<Vec3s>& ,
3489 const std::vector<Vec3I>& ,
3490 const std::vector<Vec4I>& ,
3496 "mesh to volume conversion is supported only for scalar floating-point grids");
3506 template<
typename Gr
idType>
3507 inline typename GridType::Ptr
3509 const openvdb::math::Transform& xform,
3510 const std::vector<Vec3s>& points,
3511 const std::vector<Vec3I>& triangles,
3515 std::vector<Vec4I> quads(0);
3516 return doMeshConversion<GridType>(nullInterrupter, xform, points, triangles, quads,
3517 halfWidth, halfWidth);
3521 template<
typename Gr
idType,
typename Interrupter>
3522 inline typename GridType::Ptr
3524 Interrupter& interrupter,
3525 const openvdb::math::Transform& xform,
3526 const std::vector<Vec3s>& points,
3527 const std::vector<Vec3I>& triangles,
3530 std::vector<Vec4I> quads(0);
3531 return doMeshConversion<GridType>(interrupter, xform, points, triangles, quads,
3532 halfWidth, halfWidth);
3536 template<
typename Gr
idType>
3537 inline typename GridType::Ptr
3539 const openvdb::math::Transform& xform,
3540 const std::vector<Vec3s>& points,
3541 const std::vector<Vec4I>& quads,
3545 std::vector<Vec3I> triangles(0);
3546 return doMeshConversion<GridType>(nullInterrupter, xform, points, triangles, quads,
3547 halfWidth, halfWidth);
3551 template<
typename Gr
idType,
typename Interrupter>
3552 inline typename GridType::Ptr
3554 Interrupter& interrupter,
3555 const openvdb::math::Transform& xform,
3556 const std::vector<Vec3s>& points,
3557 const std::vector<Vec4I>& quads,
3560 std::vector<Vec3I> triangles(0);
3561 return doMeshConversion<GridType>(interrupter, xform, points, triangles, quads,
3562 halfWidth, halfWidth);
3566 template<
typename Gr
idType>
3567 inline typename GridType::Ptr
3569 const openvdb::math::Transform& xform,
3570 const std::vector<Vec3s>& points,
3571 const std::vector<Vec3I>& triangles,
3572 const std::vector<Vec4I>& quads,
3576 return doMeshConversion<GridType>(nullInterrupter, xform, points, triangles, quads,
3577 halfWidth, halfWidth);
3581 template<
typename Gr
idType,
typename Interrupter>
3582 inline typename GridType::Ptr
3584 Interrupter& interrupter,
3585 const openvdb::math::Transform& xform,
3586 const std::vector<Vec3s>& points,
3587 const std::vector<Vec3I>& triangles,
3588 const std::vector<Vec4I>& quads,
3591 return doMeshConversion<GridType>(interrupter, xform, points, triangles, quads,
3592 halfWidth, halfWidth);
3596 template<
typename Gr
idType>
3597 inline typename GridType::Ptr
3599 const openvdb::math::Transform& xform,
3600 const std::vector<Vec3s>& points,
3601 const std::vector<Vec3I>& triangles,
3602 const std::vector<Vec4I>& quads,
3607 return doMeshConversion<GridType>(nullInterrupter, xform, points, triangles,
3608 quads, exBandWidth, inBandWidth);
3612 template<
typename Gr
idType,
typename Interrupter>
3613 inline typename GridType::Ptr
3615 Interrupter& interrupter,
3616 const openvdb::math::Transform& xform,
3617 const std::vector<Vec3s>& points,
3618 const std::vector<Vec3I>& triangles,
3619 const std::vector<Vec4I>& quads,
3623 return doMeshConversion<GridType>(interrupter, xform, points, triangles,
3624 quads, exBandWidth, inBandWidth);
3628 template<
typename Gr
idType>
3629 inline typename GridType::Ptr
3631 const openvdb::math::Transform& xform,
3632 const std::vector<Vec3s>& points,
3633 const std::vector<Vec3I>& triangles,
3634 const std::vector<Vec4I>& quads,
3638 return doMeshConversion<GridType>(nullInterrupter, xform, points, triangles, quads,
3639 bandWidth, bandWidth,
true);
3643 template<
typename Gr
idType,
typename Interrupter>
3644 inline typename GridType::Ptr
3646 Interrupter& interrupter,
3647 const openvdb::math::Transform& xform,
3648 const std::vector<Vec3s>& points,
3649 const std::vector<Vec3I>& triangles,
3650 const std::vector<Vec4I>& quads,
3653 return doMeshConversion<GridType>(interrupter, xform, points, triangles, quads,
3654 bandWidth, bandWidth,
true);
3662 inline std::ostream&
3665 ostr <<
"{[ " << rhs.
mXPrim <<
", " << rhs.
mXDist <<
"]";
3666 ostr <<
" [ " << rhs.
mYPrim <<
", " << rhs.
mYDist <<
"]";
3667 ostr <<
" [ " << rhs.
mZPrim <<
", " << rhs.
mZDist <<
"]}";
3687 const std::vector<Vec3s>& pointList,
3688 const std::vector<Vec4I>& polygonList);
3690 void run(
bool threaded =
true);
3693 inline void operator() (
const tbb::blocked_range<size_t> &range);
3701 struct Primitive {
Vec3d a, b, c, d;
Int32 index; };
3703 template<
bool IsQuad>
3704 inline void voxelize(
const Primitive&);
3706 template<
bool IsQuad>
3707 inline bool evalPrimitive(
const Coord&,
const Primitive&);
3709 inline bool rayTriangleIntersection(
const Vec3d& origin,
const Vec3d& dir,
3716 const std::vector<Vec3s>& mPointList;
3717 const std::vector<Vec4I>& mPolygonList;
3721 IntTreeT mLastPrimTree;
3727 MeshToVoxelEdgeData::GenEdgeData::GenEdgeData(
3728 const std::vector<Vec3s>& pointList,
3729 const std::vector<Vec4I>& polygonList)
3732 , mPointList(pointList)
3733 , mPolygonList(polygonList)
3735 , mLastPrimAccessor(mLastPrimTree)
3744 , mPointList(rhs.mPointList)
3745 , mPolygonList(rhs.mPolygonList)
3747 , mLastPrimAccessor(mLastPrimTree)
3756 tbb::parallel_reduce(tbb::blocked_range<size_t>(0, mPolygonList.size()), *
this);
3758 (*this)(tbb::blocked_range<size_t>(0, mPolygonList.size()));
3767 using NodeChainType = RootNodeType::NodeChainType;
3768 static_assert(boost::mpl::size<NodeChainType>::value > 1,
"expected tree height > 1");
3769 using InternalNodeType = boost::mpl::at<NodeChainType, boost::mpl::int_<1> >::type;
3777 for ( ; leafIt; ++leafIt) {
3778 ijk = leafIt->origin();
3785 InternalNodeType* node = rhs.mAccessor.
getNode<InternalNodeType>();
3787 rhs.mAccessor.
clear();
3797 if (!lhsLeafPt->isValueOn(offset)) {
3798 lhsLeafPt->setValueOn(offset, rhsValue);
3830 for (
size_t n = range.begin(); n < range.end(); ++n) {
3832 const Vec4I& verts = mPolygonList[n];
3834 prim.index =
Int32(n);
3835 prim.a =
Vec3d(mPointList[verts[0]]);
3836 prim.b =
Vec3d(mPointList[verts[1]]);
3837 prim.c =
Vec3d(mPointList[verts[2]]);
3840 prim.d =
Vec3d(mPointList[verts[3]]);
3841 voxelize<true>(prim);
3843 voxelize<false>(prim);
3849 template<
bool IsQuad>
3851 MeshToVoxelEdgeData::GenEdgeData::voxelize(
const Primitive& prim)
3853 std::deque<Coord> coordList;
3857 coordList.push_back(ijk);
3859 evalPrimitive<IsQuad>(ijk, prim);
3861 while (!coordList.empty()) {
3863 ijk = coordList.back();
3864 coordList.pop_back();
3866 for (
Int32 i = 0; i < 26; ++i) {
3869 if (prim.index != mLastPrimAccessor.
getValue(nijk)) {
3870 mLastPrimAccessor.
setValue(nijk, prim.index);
3871 if(evalPrimitive<IsQuad>(nijk, prim)) coordList.push_back(nijk);
3878 template<
bool IsQuad>
3880 MeshToVoxelEdgeData::GenEdgeData::evalPrimitive(
const Coord& ijk,
const Primitive& prim)
3882 Vec3d uvw, org(ijk[0], ijk[1], ijk[2]);
3883 bool intersecting =
false;
3890 double dist = (org -
3893 if (rayTriangleIntersection(org,
Vec3d(1.0, 0.0, 0.0), prim.a, prim.c, prim.b, t)) {
3894 if (t < edgeData.
mXDist) {
3895 edgeData.
mXDist = float(t);
3896 edgeData.
mXPrim = prim.index;
3897 intersecting =
true;
3901 if (rayTriangleIntersection(org,
Vec3d(0.0, 1.0, 0.0), prim.a, prim.c, prim.b, t)) {
3902 if (t < edgeData.
mYDist) {
3903 edgeData.
mYDist = float(t);
3904 edgeData.
mYPrim = prim.index;
3905 intersecting =
true;
3909 if (rayTriangleIntersection(org,
Vec3d(0.0, 0.0, 1.0), prim.a, prim.c, prim.b, t)) {
3910 if (t < edgeData.
mZDist) {
3911 edgeData.
mZDist = float(t);
3912 edgeData.
mZPrim = prim.index;
3913 intersecting =
true;
3919 double secondDist = (org -
3922 if (secondDist < dist) dist = secondDist;
3924 if (rayTriangleIntersection(org,
Vec3d(1.0, 0.0, 0.0), prim.a, prim.d, prim.c, t)) {
3925 if (t < edgeData.
mXDist) {
3926 edgeData.
mXDist = float(t);
3927 edgeData.
mXPrim = prim.index;
3928 intersecting =
true;
3932 if (rayTriangleIntersection(org,
Vec3d(0.0, 1.0, 0.0), prim.a, prim.d, prim.c, t)) {
3933 if (t < edgeData.
mYDist) {
3934 edgeData.
mYDist = float(t);
3935 edgeData.
mYPrim = prim.index;
3936 intersecting =
true;
3940 if (rayTriangleIntersection(org,
Vec3d(0.0, 0.0, 1.0), prim.a, prim.d, prim.c, t)) {
3941 if (t < edgeData.
mZDist) {
3942 edgeData.
mZDist = float(t);
3943 edgeData.
mZPrim = prim.index;
3944 intersecting =
true;
3949 if (intersecting) mAccessor.
setValue(ijk, edgeData);
3951 return (dist < 0.86602540378443861);
3956 MeshToVoxelEdgeData::GenEdgeData::rayTriangleIntersection(
3967 double divisor = s1.
dot(e1);
3968 if (!(std::abs(divisor) > 0.0))
return false;
3972 double inv_divisor = 1.0 / divisor;
3973 Vec3d d = origin - a;
3974 double b1 = d.
dot(s1) * inv_divisor;
3976 if (b1 < 0.0 || b1 > 1.0)
return false;
3979 double b2 = dir.
dot(s2) * inv_divisor;
3981 if (b2 < 0.0 || (b1 + b2) > 1.0)
return false;
3985 t = e2.dot(s2) * inv_divisor;
3986 return (t < 0.0) ? false :
true;
4002 const std::vector<Vec3s>& pointList,
4003 const std::vector<Vec4I>& polygonList)
4017 std::vector<Vec3d>& points,
4018 std::vector<Index32>& primitives)
4028 point[0] = double(coord[0]) + data.
mXDist;
4029 point[1] = double(coord[1]);
4030 point[2] = double(coord[2]);
4032 points.push_back(point);
4033 primitives.push_back(data.
mXPrim);
4037 point[0] = double(coord[0]);
4038 point[1] = double(coord[1]) + data.
mYDist;
4039 point[2] = double(coord[2]);
4041 points.push_back(point);
4042 primitives.push_back(data.
mYPrim);
4046 point[0] = double(coord[0]);
4047 point[1] = double(coord[1]);
4048 point[2] = double(coord[2]) + data.
mZDist;
4050 points.push_back(point);
4051 primitives.push_back(data.
mZPrim);
4061 point[0] = double(coord[0]);
4062 point[1] = double(coord[1]) + data.
mYDist;
4063 point[2] = double(coord[2]);
4065 points.push_back(point);
4066 primitives.push_back(data.
mYPrim);
4070 point[0] = double(coord[0]);
4071 point[1] = double(coord[1]);
4072 point[2] = double(coord[2]) + data.
mZDist;
4074 points.push_back(point);
4075 primitives.push_back(data.
mZPrim);
4083 point[0] = double(coord[0]);
4084 point[1] = double(coord[1]) + data.
mYDist;
4085 point[2] = double(coord[2]);
4087 points.push_back(point);
4088 primitives.push_back(data.
mYPrim);
4097 point[0] = double(coord[0]) + data.
mXDist;
4098 point[1] = double(coord[1]);
4099 point[2] = double(coord[2]);
4101 points.push_back(point);
4102 primitives.push_back(data.
mXPrim);
4106 point[0] = double(coord[0]);
4107 point[1] = double(coord[1]) + data.
mYDist;
4108 point[2] = double(coord[2]);
4110 points.push_back(point);
4111 primitives.push_back(data.
mYPrim);
4121 point[0] = double(coord[0]) + data.
mXDist;
4122 point[1] = double(coord[1]);
4123 point[2] = double(coord[2]);
4125 points.push_back(point);
4126 primitives.push_back(data.
mXPrim);
4135 point[0] = double(coord[0]) + data.
mXDist;
4136 point[1] = double(coord[1]);
4137 point[2] = double(coord[2]);
4139 points.push_back(point);
4140 primitives.push_back(data.
mXPrim);
4144 point[0] = double(coord[0]);
4145 point[1] = double(coord[1]);
4146 point[2] = double(coord[2]) + data.
mZDist;
4148 points.push_back(point);
4149 primitives.push_back(data.
mZPrim);
4158 point[0] = double(coord[0]);
4159 point[1] = double(coord[1]);
4160 point[2] = double(coord[2]) + data.
mZDist;
4162 points.push_back(point);
4163 primitives.push_back(data.
mZPrim);
4169 template<
typename Gr
idType,
typename VecType>
4170 inline typename GridType::Ptr
4172 const openvdb::math::Transform& xform,
4173 typename VecType::ValueType halfWidth)
4179 points[0] =
Vec3s(pmin[0], pmin[1], pmin[2]);
4180 points[1] =
Vec3s(pmin[0], pmin[1], pmax[2]);
4181 points[2] =
Vec3s(pmax[0], pmin[1], pmax[2]);
4182 points[3] =
Vec3s(pmax[0], pmin[1], pmin[2]);
4183 points[4] =
Vec3s(pmin[0], pmax[1], pmin[2]);
4184 points[5] =
Vec3s(pmin[0], pmax[1], pmax[2]);
4185 points[6] =
Vec3s(pmax[0], pmax[1], pmax[2]);
4186 points[7] =
Vec3s(pmax[0], pmax[1], pmin[2]);
4189 faces[0] =
Vec4I(0, 1, 2, 3);
4190 faces[1] =
Vec4I(7, 6, 5, 4);
4191 faces[2] =
Vec4I(4, 5, 1, 0);
4192 faces[3] =
Vec4I(6, 7, 3, 2);
4193 faces[4] =
Vec4I(0, 3, 7, 4);
4194 faces[5] =
Vec4I(1, 5, 6, 2);
4198 return meshToVolume<GridType>(mesh, xform, halfWidth, halfWidth);
4206 #endif // OPENVDB_TOOLS_MESH_TO_VOLUME_HAS_BEEN_INCLUDED
typename RootNodeType::LeafNodeType LeafNodeType
Definition: Tree.h:212
LeafIter beginLeaf()
Return an iterator over all leaf nodes in this tree.
Definition: Tree.h:1179
Axis-aligned bounding box of signed integer coordinates.
Definition: Coord.h:264
static Coord min()
Return the smallest possible coordinate.
Definition: Coord.h:70
bool operator>(const Tuple< SIZE, T0 > &t0, const Tuple< SIZE, T1 > &t1)
Definition: Tuple.h:219
OPENVDB_API const Coord COORD_OFFSETS[26]
coordinate offset table for neighboring voxels
OPENVDB_API Vec3d closestPointOnTriangleToPoint(const Vec3d &a, const Vec3d &b, const Vec3d &c, const Vec3d &p, Vec3d &uvw)
Closest Point on Triangle to Point. Given a triangle abc and a point p, return the point on abc close...
bool isExactlyEqual(const T0 &a, const T1 &b)
Return true if a is exactly equal to b.
Definition: Math.h:395
const LeafNodeT * probeConstLeaf(const Coord &xyz) const
Return a pointer to the leaf node that contains voxel (x, y, z), or nullptr if no such node exists...
Definition: ValueAccessor.h:430
#define OPENVDB_LOG_DEBUG(message)
In debug builds only, log a debugging message of the form 'someVar << "text" << ...'.
Definition: logging.h:290
const ValueT & getValue() const
Return the tile or voxel value to which this iterator is currently pointing.
Definition: TreeIterator.h:741
void addLeaf(LeafNodeT *leaf)
Add the specified leaf to this tree, possibly creating a child branch in the process. If the leaf node already exists, replace it.
Definition: ValueAccessor.h:375
Convert polygonal meshes that consist of quads and/or triangles into signed or unsigned distance fiel...
Axis-aligned bounding box.
Definition: BBox.h:50
const Vec3T & min() const
Return a const reference to the minimum point of this bounding box.
Definition: BBox.h:89
#define OPENVDB_THROW(exception, message)
Definition: Exceptions.h:109
static Coord max()
Return the largest possible coordinate.
Definition: Coord.h:73
Efficient multi-threaded replacement of the background values in tree.
bool wasInterrupted(T *i, int percent=-1)
Definition: NullInterrupter.h:76
const Coord & min() const
Definition: Coord.h:337
Signed (x, y, z) 32-bit integer coordinates.
Definition: Coord.h:51
LeafNodeT * touchLeaf(const Coord &xyz)
Return a pointer to the leaf node that contains voxel (x, y, z). If no such node exists, create one, but preserve the values and active states of all voxels.
Definition: ValueAccessor.h:394
void merge(Tree &other, MergePolicy=MERGE_ACTIVE_STATES)
Efficiently merge another tree into this tree using one of several schemes.
Definition: Tree.h:1864
Definition: Exceptions.h:91
void setValueOn(const Coord &xyz, const ValueType &value)
Set the value of the voxel at the given coordinates and mark the voxel as active. ...
Definition: ValueAccessor.h:293
std::shared_ptr< T > SharedPtr
Definition: Types.h:139
static Coord floor(const Vec3< T > &xyz)
Return the largest integer coordinates that are not greater than xyz (node centered conversion)...
Definition: Coord.h:83
Tree< typename RootNodeType::template ValueConverter< Int32 >::Type > Type
Definition: Tree.h:224
static const Real LEVEL_SET_HALF_WIDTH
Definition: Types.h:283
Defined various multi-threaded utility functions for trees.
void clearAllAccessors()
Clear all registered accessors.
Definition: Tree.h:1547
void expand(ValueType padding)
Pad this bounding box with the specified padding.
Definition: Coord.h:422
Base class for tree-traversal iterators over tile and voxel values.
Definition: TreeIterator.h:665
Vec3< double > Vec3d
Definition: Vec3.h:679
#define OPENVDB_VERSION_NAME
The version namespace name for this library version.
Definition: version.h:136
void minComponent(const Coord &other)
Perform a component-wise minimum with the other Coord.
Definition: Coord.h:202
bool isValueOn(const Coord &xyz) const
Return the active state of the voxel at the given coordinates.
Definition: ValueAccessor.h:264
T dot(const Vec3< T > &v) const
Dot product.
Definition: Vec3.h:216
int32_t Int32
Definition: Types.h:63
Propagate the signs of distance values from the active voxels in the narrow band to the inactive valu...
LeafNodeT * probeLeaf(const Coord &xyz)
Return a pointer to the leaf node that contains voxel (x, y, z), or nullptr if no such node exists...
Definition: ValueAccessor.h:425
_RootNodeType RootNodeType
Definition: Tree.h:209
void setValue(const Coord &xyz, const ValueType &value)
Set the value of the voxel at the given coordinates and mark the voxel as active. ...
Definition: ValueAccessor.h:288
Definition: Exceptions.h:40
void setValueOnly(const Coord &xyz, const ValueType &value)
Set the value of the voxel at the given coordinate but don't change its active state.
Definition: ValueAccessor.h:297
OPENVDB_API const Index32 INVALID_IDX
Base class for tree-traversal iterators over all leaf nodes (but not leaf voxels) ...
Definition: TreeIterator.h:1235
Coord offsetBy(Int32 dx, Int32 dy, Int32 dz) const
Definition: Coord.h:118
Index32 Index
Definition: Types.h:61
float Sqrt(float x)
Return the square root of a floating-point value.
Definition: Math.h:715
void maxComponent(const Coord &other)
Perform a component-wise maximum with the other Coord.
Definition: Coord.h:210
const ValueType & getValue(const Coord &xyz) const
Return the value of the voxel at the given coordinates.
Definition: ValueAccessor.h:257
Vec3< float > Vec3s
Definition: Vec3.h:678
NodeType * getNode()
Return the cached node of type NodeType. [Mainly for internal use].
Definition: ValueAccessor.h:350
bool probeValue(const Coord &xyz, ValueType &value) const
Return the active state of the voxel as well as its value.
Definition: ValueAccessor.h:267
Vec3< T > cross(const Vec3< T > &v) const
Return the cross product of "this" vector and v;.
Definition: Vec3.h:245
void clear()
Remove all tiles from this tree and all nodes other than the root node.
Definition: Tree.h:1488
const Vec3T & max() const
Return a const reference to the maximum point of this bounding box.
Definition: BBox.h:91
math::Vec4< Index32 > Vec4I
Definition: Types.h:95
Type Pow2(Type x)
Return x2.
Definition: Math.h:502
bool operator<(const Tuple< SIZE, T0 > &t0, const Tuple< SIZE, T1 > &t1)
Definition: Tuple.h:207
const Coord & max() const
Definition: Coord.h:338
LeafNodeType * probeLeaf(const Coord &xyz)
Return a pointer to the leaf node that contains voxel (x, y, z). If no such node exists, return nullptr.
Definition: Tree.h:1734
bool isInside(const Coord &xyz) const
Return true if point (x, y, z) is inside this bounding box.
Definition: Coord.h:404
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h:188
bool isZero(const Type &x)
Return true if x is exactly equal to zero.
Definition: Math.h:308
ValueConverter<T>::Type is the type of a tree having the same hierarchy as this tree but a different ...
Definition: Tree.h:223
Real GodunovsNormSqrd(bool isOutside, Real dP_xm, Real dP_xp, Real dP_ym, Real dP_yp, Real dP_zm, Real dP_zp)
Definition: FiniteDifference.h:353
void clear() override
Remove all nodes from this cache, then reinsert the root node.
Definition: ValueAccessor.h:439
Dummy NOOP interrupter class defining interface.
Definition: NullInterrupter.h:52
uint32_t Index32
Definition: Types.h:59
Partitions points into BucketLog2Dim aligned buckets using a parallel radix-based sorting algorithm...