OpenVDB 5.1.0

MeshToVolume.h
Go to the documentation of this file.
1 //
3 // Copyright (c) 2012-2018 DreamWorks Animation LLC
4 //
5 // All rights reserved. This software is distributed under the
6 // Mozilla Public License 2.0 ( http://www.mozilla.org/MPL/2.0/ )
7 //
8 // Redistributions of source code must retain the above copyright
9 // and license notice and the following restrictions and disclaimer.
10 //
11 // * Neither the name of DreamWorks Animation nor the names of
12 // its contributors may be used to endorse or promote products derived
13 // from this software without specific prior written permission.
14 //
15 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
18 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
19 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY INDIRECT, INCIDENTAL,
20 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
21 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 // IN NO EVENT SHALL THE COPYRIGHT HOLDERS' AND CONTRIBUTORS' AGGREGATE
27 // LIABILITY FOR ALL CLAIMS REGARDLESS OF THEIR BASIS EXCEED US$250.00.
28 //
30
42
43 #ifndef OPENVDB_TOOLS_MESH_TO_VOLUME_HAS_BEEN_INCLUDED
44 #define OPENVDB_TOOLS_MESH_TO_VOLUME_HAS_BEEN_INCLUDED
45
46 #include <openvdb/Platform.h> // for OPENVDB_HAS_CXX11
47 #include <openvdb/Types.h>
48 #include <openvdb/math/FiniteDifference.h> // for GodunovsNormSqrd
49 #include <openvdb/math/Proximity.h> // for closestPointOnTriangleToPoint
50 #include <openvdb/util/NullInterrupter.h>
51 #include <openvdb/util/Util.h>
52
53 #include "ChangeBackground.h"
54 #include "Prune.h" // for pruneInactive and pruneLevelSet
55 #include "SignedFloodFill.h" // for signedFloodFillWithValues
56
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>
64
65 #include <boost/mpl/at.hpp>
66 #include <boost/mpl/int.hpp>
67 #include <boost/mpl/size.hpp>
68
69 #include <algorithm> // for std::sort()
70 #include <cmath> // for std::isfinite(), std::isnan()
71 #include <deque>
72 #include <limits>
73 #include <memory>
74 #include <sstream>
75 #include <type_traits>
76 #include <vector>
77
78 namespace openvdb {
79 OPENVDB_USE_VERSION_NAMESPACE
80 namespace OPENVDB_VERSION_NAME {
81 namespace tools {
82
83
85
86
88 enum MeshToVolumeFlags {
89
94 UNSIGNED_DISTANCE_FIELD = 0x1,
95
98 DISABLE_INTERSECTING_VOXEL_REMOVAL = 0x2,
99
102 DISABLE_RENORMALIZATION = 0x4,
103
106 DISABLE_NARROW_BAND_TRIMMING = 0x8
107 };
108
109
140 template <typename GridType, typename MeshDataAdapter>
141 inline typename GridType::Ptr
142 meshToVolume(
143 const MeshDataAdapter& mesh,
144 const math::Transform& transform,
145 float exteriorBandWidth = 3.0f,
146 float interiorBandWidth = 3.0f,
147 int flags = 0,
148 typename GridType::template ValueConverter<Int32>::Type * polygonIndexGrid = nullptr);
149
150
166 template <typename GridType, typename MeshDataAdapter, typename Interrupter>
167 inline typename GridType::Ptr
168 meshToVolume(
169 Interrupter& interrupter,
170 const MeshDataAdapter& mesh,
171 const math::Transform& transform,
172 float exteriorBandWidth = 3.0f,
173 float interiorBandWidth = 3.0f,
174 int flags = 0,
175 typename GridType::template ValueConverter<Int32>::Type * polygonIndexGrid = nullptr);
176
177
179
180
191 template<typename PointType, typename PolygonType>
192 struct QuadAndTriangleDataAdapter {
193
194 QuadAndTriangleDataAdapter(const std::vector<PointType>& points,
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())
200 {
201 }
202
203 QuadAndTriangleDataAdapter(const PointType * pointArray, size_t pointArraySize,
204 const PolygonType* polygonArray, size_t polygonArraySize)
205 : mPointArray(pointArray)
206 , mPointArraySize(pointArraySize)
207 , mPolygonArray(polygonArray)
208 , mPolygonArraySize(polygonArraySize)
209 {
210 }
211
212 size_t polygonCount() const { return mPolygonArraySize; }
213 size_t pointCount() const { return mPointArraySize; }
214
216 size_t vertexCount(size_t n) const {
217 return (PolygonType::size == 3 || mPolygonArray[n][3] == util::INVALID_IDX) ? 3 : 4;
218 }
219
222 void getIndexSpacePoint(size_t n, size_t v, Vec3d& pos) const {
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]);
227 }
228
229 private:
230 PointType const * const mPointArray;
231 size_t const mPointArraySize;
232 PolygonType const * const mPolygonArray;
233 size_t const mPolygonArraySize;
234 }; // struct QuadAndTriangleDataAdapter
235
236
238
239
240 // Convenience functions for the mesh to volume converter that wrap stl containers.
241 //
242 // Note the meshToVolume() method declared above is more flexible and better suited
243 // for arbitrary data structures.
244
245
261 template<typename GridType>
262 inline typename GridType::Ptr
263 meshToLevelSet(
264 const openvdb::math::Transform& xform,
265 const std::vector<Vec3s>& points,
266 const std::vector<Vec3I>& triangles,
267 float halfWidth = float(LEVEL_SET_HALF_WIDTH));
268
270 template<typename GridType, typename Interrupter>
271 inline typename GridType::Ptr
272 meshToLevelSet(
273 Interrupter& interrupter,
274 const openvdb::math::Transform& xform,
275 const std::vector<Vec3s>& points,
276 const std::vector<Vec3I>& triangles,
277 float halfWidth = float(LEVEL_SET_HALF_WIDTH));
278
279
295 template<typename GridType>
296 inline typename GridType::Ptr
297 meshToLevelSet(
298 const openvdb::math::Transform& xform,
299 const std::vector<Vec3s>& points,
300 const std::vector<Vec4I>& quads,
301 float halfWidth = float(LEVEL_SET_HALF_WIDTH));
302
304 template<typename GridType, typename Interrupter>
305 inline typename GridType::Ptr
306 meshToLevelSet(
307 Interrupter& interrupter,
308 const openvdb::math::Transform& xform,
309 const std::vector<Vec3s>& points,
310 const std::vector<Vec4I>& quads,
311 float halfWidth = float(LEVEL_SET_HALF_WIDTH));
312
313
330 template<typename GridType>
331 inline typename GridType::Ptr
332 meshToLevelSet(
333 const openvdb::math::Transform& xform,
334 const std::vector<Vec3s>& points,
335 const std::vector<Vec3I>& triangles,
336 const std::vector<Vec4I>& quads,
337 float halfWidth = float(LEVEL_SET_HALF_WIDTH));
338
340 template<typename GridType, typename Interrupter>
341 inline typename GridType::Ptr
342 meshToLevelSet(
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,
348 float halfWidth = float(LEVEL_SET_HALF_WIDTH));
349
350
369 template<typename GridType>
370 inline typename GridType::Ptr
371 meshToSignedDistanceField(
372 const openvdb::math::Transform& xform,
373 const std::vector<Vec3s>& points,
374 const std::vector<Vec3I>& triangles,
375 const std::vector<Vec4I>& quads,
376 float exBandWidth,
377 float inBandWidth);
378
380 template<typename GridType, typename Interrupter>
381 inline typename GridType::Ptr
382 meshToSignedDistanceField(
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,
388 float exBandWidth,
389 float inBandWidth);
390
391
406 template<typename GridType>
407 inline typename GridType::Ptr
408 meshToUnsignedDistanceField(
409 const openvdb::math::Transform& xform,
410 const std::vector<Vec3s>& points,
411 const std::vector<Vec3I>& triangles,
412 const std::vector<Vec4I>& quads,
413 float bandWidth);
414
416 template<typename GridType, typename Interrupter>
417 inline typename GridType::Ptr
418 meshToUnsignedDistanceField(
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,
424 float bandWidth);
425
426
428
429
436 template<typename GridType, typename VecType>
437 inline typename GridType::Ptr
438 createLevelSetBox(const math::BBox<VecType>& bbox,
439 const openvdb::math::Transform& xform,
440 typename VecType::ValueType halfWidth = LEVEL_SET_HALF_WIDTH);
441
442
444
445
452 template <typename FloatTreeT>
453 inline void
454 traceExteriorBoundaries(FloatTreeT& tree);
455
456
458
459
461 class MeshToVoxelEdgeData
462 {
463 public:
464
466
468 struct EdgeData {
469 EdgeData(float dist = 1.0)
470 : mXDist(dist), mYDist(dist), mZDist(dist)
471 , mXPrim(util::INVALID_IDX)
472 , mYPrim(util::INVALID_IDX)
473 , mZPrim(util::INVALID_IDX)
474 {
475 }
476
478 bool operator< (const EdgeData&) const { return false; }
481 bool operator> (const EdgeData&) const { return false; }
482 template<class T> EdgeData operator+(const T&) const { return *this; }
483 template<class T> EdgeData operator-(const T&) const { return *this; }
484 EdgeData operator-() const { return *this; }
486
487 bool operator==(const EdgeData& rhs) const
488 {
489 return mXPrim == rhs.mXPrim && mYPrim == rhs.mYPrim && mZPrim == rhs.mZPrim;
490 }
491
492 float mXDist, mYDist, mZDist;
493 Index32 mXPrim, mYPrim, mZPrim;
494 };
495
496 using TreeType = tree::Tree4<EdgeData, 5, 4, 3>::Type;
497 using Accessor = tree::ValueAccessor<TreeType>;
498
499
501
502
503 MeshToVoxelEdgeData();
504
505
513 void convert(const std::vector<Vec3s>& pointList, const std::vector<Vec4I>& polygonList);
514
515
518 void getEdgeData(Accessor& acc, const Coord& ijk,
519 std::vector<Vec3d>& points, std::vector<Index32>& primitives);
520
523 Accessor getAccessor() { return Accessor(mTree); }
524
525 private:
526 void operator=(const MeshToVoxelEdgeData&) {}
527 TreeType mTree;
528 class GenEdgeData;
529 };
530
531
534
535
536 // Internal utility objects and implementation details
537
538 namespace mesh_to_volume_internal {
539
540 template<typename PointType>
541 struct TransformPoints {
542
543 TransformPoints(const PointType* pointsIn, PointType* pointsOut,
544 const math::Transform& xform)
545 : mPointsIn(pointsIn), mPointsOut(pointsOut), mXform(&xform)
546 {
547 }
548
549 void operator()(const tbb::blocked_range<size_t>& range) const {
550
551 Vec3d pos;
552
553 for (size_t n = range.begin(), N = range.end(); n < N; ++n) {
554
555 const PointType& wsP = mPointsIn[n];
556 pos[0] = double(wsP[0]);
557 pos[1] = double(wsP[1]);
558 pos[2] = double(wsP[2]);
559
560 pos = mXform->worldToIndex(pos);
561
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]);
566 }
567 }
568
569 PointType const * const mPointsIn;
570 PointType * const mPointsOut;
571 math::Transform const * const mXform;
572 }; // TransformPoints
573
574
575 template<typename ValueType>
576 struct Tolerance
577 {
578 static ValueType epsilon() { return ValueType(1e-7); }
579 static ValueType minNarrowBandWidth() { return ValueType(1.0 + 1e-6); }
580 };
581
582
584
585
586 template<typename TreeType>
587 class CombineLeafNodes
588 {
589 public:
590
591 using Int32TreeType = typename TreeType::template ValueConverter<Int32>::Type;
592
593 using LeafNodeType = typename TreeType::LeafNodeType;
594 using Int32LeafNodeType = typename Int32TreeType::LeafNodeType;
595
596 CombineLeafNodes(TreeType& lhsDistTree, Int32TreeType& lhsIdxTree,
597 LeafNodeType ** rhsDistNodes, Int32LeafNodeType ** rhsIdxNodes)
598 : mDistTree(&lhsDistTree)
599 , mIdxTree(&lhsIdxTree)
600 , mRhsDistNodes(rhsDistNodes)
601 , mRhsIdxNodes(rhsIdxNodes)
602 {
603 }
604
605 void operator()(const tbb::blocked_range<size_t>& range) const {
606
607 tree::ValueAccessor<TreeType> distAcc(*mDistTree);
608 tree::ValueAccessor<Int32TreeType> idxAcc(*mIdxTree);
609
610 using DistValueType = typename LeafNodeType::ValueType;
611 using IndexValueType = typename Int32LeafNodeType::ValueType;
612
613 for (size_t n = range.begin(), N = range.end(); n < N; ++n) {
614
615 const Coord& origin = mRhsDistNodes[n]->origin();
616
617 LeafNodeType* lhsDistNode = distAcc.probeLeaf(origin);
618 Int32LeafNodeType* lhsIdxNode = idxAcc.probeLeaf(origin);
619
620 DistValueType* lhsDistData = lhsDistNode->buffer().data();
621 IndexValueType* lhsIdxData = lhsIdxNode->buffer().data();
622
623 const DistValueType* rhsDistData = mRhsDistNodes[n]->buffer().data();
624 const IndexValueType* rhsIdxData = mRhsIdxNodes[n]->buffer().data();
625
626
627 for (Index32 offset = 0; offset < LeafNodeType::SIZE; ++offset) {
628
629 if (rhsIdxData[offset] != Int32(util::INVALID_IDX)) {
630
631 const DistValueType& lhsValue = lhsDistData[offset];
632 const DistValueType& rhsValue = rhsDistData[offset];
633
634 if (rhsValue < lhsValue) {
635 lhsDistNode->setValueOn(offset, rhsValue);
636 lhsIdxNode->setValueOn(offset, rhsIdxData[offset]);
637 } else if (math::isExactlyEqual(rhsValue, lhsValue)) {
638 lhsIdxNode->setValueOn(offset,
639 std::min(lhsIdxData[offset], rhsIdxData[offset]));
640 }
641 }
642 }
643
644 delete mRhsDistNodes[n];
645 delete mRhsIdxNodes[n];
646 }
647 }
648
649 private:
650
651 TreeType * const mDistTree;
652 Int32TreeType * const mIdxTree;
653
654 LeafNodeType ** const mRhsDistNodes;
655 Int32LeafNodeType ** const mRhsIdxNodes;
656 }; // class CombineLeafNodes
657
658
660
661
662 template<typename TreeType>
663 struct StashOriginAndStoreOffset
664 {
665 using LeafNodeType = typename TreeType::LeafNodeType;
666
667 StashOriginAndStoreOffset(std::vector<LeafNodeType*>& nodes, Coord* coordinates)
668 : mNodes(nodes.empty() ? nullptr : &nodes[0]), mCoordinates(coordinates)
669 {
670 }
671
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);
677 }
678 }
679
680 LeafNodeType ** const mNodes;
681 Coord * const mCoordinates;
682 };
683
684
685 template<typename TreeType>
686 struct RestoreOrigin
687 {
688 using LeafNodeType = typename TreeType::LeafNodeType;
689
690 RestoreOrigin(std::vector<LeafNodeType*>& nodes, const Coord* coordinates)
691 : mNodes(nodes.empty() ? nullptr : &nodes[0]), mCoordinates(coordinates)
692 {
693 }
694
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];
699 }
700 }
701
702 LeafNodeType ** const mNodes;
703 Coord const * const mCoordinates;
704 };
705
706
707 template<typename TreeType>
708 class ComputeNodeConnectivity
709 {
710 public:
711 using LeafNodeType = typename TreeType::LeafNodeType;
712
713 ComputeNodeConnectivity(const TreeType& tree, const Coord* coordinates,
714 size_t* offsets, size_t numNodes, const CoordBBox& bbox)
715 : mTree(&tree)
716 , mCoordinates(coordinates)
717 , mOffsets(offsets)
718 , mNumNodes(numNodes)
719 , mBBox(bbox)
720 {
721 }
722
723 ComputeNodeConnectivity(const ComputeNodeConnectivity&) = default;
724
725 // Disallow assignment
726 ComputeNodeConnectivity& operator=(const ComputeNodeConnectivity&) = delete;
727
728 void operator()(const tbb::blocked_range<size_t>& range) const {
729
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;
736
737 tree::ValueAccessor<const TreeType> acc(*mTree);
738 Coord ijk;
739
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));
748 }
749 }
750
751 size_t findNeighbourNode(tree::ValueAccessor<const TreeType>& acc,
752 const Coord& start, const Coord& step) const
753 {
754 Coord ijk = start + step;
755 CoordBBox bbox(mBBox);
756
757 while (bbox.isInside(ijk)) {
758 const LeafNodeType* node = acc.probeConstLeaf(ijk);
759 if (node) return static_cast<size_t>(node->origin()[0]);
760 ijk += step;
761 }
762
763 return std::numeric_limits<size_t>::max();
764 }
765
766
767 private:
768 TreeType const * const mTree;
769 Coord const * const mCoordinates;
770 size_t * const mOffsets;
771
772 const size_t mNumNodes;
773 const CoordBBox mBBox;
774 }; // class ComputeNodeConnectivity
775
776
777 template<typename TreeType>
778 struct LeafNodeConnectivityTable
779 {
780 enum { INVALID_OFFSET = std::numeric_limits<size_t>::max() };
781
782 using LeafNodeType = typename TreeType::LeafNodeType;
783
784 LeafNodeConnectivityTable(TreeType& tree)
785 {
786 mLeafNodes.reserve(tree.leafCount());
787 tree.getNodes(mLeafNodes);
788
789 if (mLeafNodes.empty()) return;
790
791 CoordBBox bbox;
792 tree.evalLeafBoundingBox(bbox);
793
794 const tbb::blocked_range<size_t> range(0, mLeafNodes.size());
795
796 // stash the leafnode origin coordinate and temporarily store the
797 // linear offset in the origin.x variable.
798 std::unique_ptr<Coord[]> coordinates{new Coord[mLeafNodes.size()]};
799 tbb::parallel_for(range,
800 StashOriginAndStoreOffset<TreeType>(mLeafNodes, coordinates.get()));
801
802 // build the leafnode offset table
803 mOffsets.reset(new size_t[mLeafNodes.size() * 6]);
804
805
806 tbb::parallel_for(range, ComputeNodeConnectivity<TreeType>(
807 tree, coordinates.get(), mOffsets.get(), mLeafNodes.size(), bbox));
808
809 // restore the leafnode origin coordinate
810 tbb::parallel_for(range, RestoreOrigin<TreeType>(mLeafNodes, coordinates.get()));
811 }
812
813 size_t size() const { return mLeafNodes.size(); }
814
815 std::vector<LeafNodeType*>& nodes() { return mLeafNodes; }
816 const std::vector<LeafNodeType*>& nodes() const { return mLeafNodes; }
817
818
819 const size_t* offsetsNextX() const { return mOffsets.get(); }
820 const size_t* offsetsPrevX() const { return mOffsets.get() + mLeafNodes.size(); }
821
822 const size_t* offsetsNextY() const { return mOffsets.get() + mLeafNodes.size() * 2; }
823 const size_t* offsetsPrevY() const { return mOffsets.get() + mLeafNodes.size() * 3; }
824
825 const size_t* offsetsNextZ() const { return mOffsets.get() + mLeafNodes.size() * 4; }
826 const size_t* offsetsPrevZ() const { return mOffsets.get() + mLeafNodes.size() * 5; }
827
828 private:
829 std::vector<LeafNodeType*> mLeafNodes;
830 std::unique_ptr<size_t[]> mOffsets;
831 }; // struct LeafNodeConnectivityTable
832
833
834 template<typename TreeType>
835 class SweepExteriorSign
836 {
837 public:
838
839 enum Axis { X_AXIS = 0, Y_AXIS = 1, Z_AXIS = 2 };
840
841 using ValueType = typename TreeType::ValueType;
842 using LeafNodeType = typename TreeType::LeafNodeType;
843 using ConnectivityTable = LeafNodeConnectivityTable<TreeType>;
844
845 SweepExteriorSign(Axis axis, const std::vector<size_t>& startNodeIndices,
846 ConnectivityTable& connectivity)
847 : mStartNodeIndices(startNodeIndices.empty() ? nullptr : &startNodeIndices[0])
848 , mConnectivity(&connectivity)
849 , mAxis(axis)
850 {
851 }
852
853 void operator()(const tbb::blocked_range<size_t>& range) const {
854
855 std::vector<LeafNodeType*>& nodes = mConnectivity->nodes();
856
857 // Z Axis
858 size_t idxA = 0, idxB = 1;
859 Index step = 1;
860
861 const size_t* nextOffsets = mConnectivity->offsetsNextZ();
862 const size_t* prevOffsets = mConnectivity->offsetsPrevZ();
863
864 if (mAxis == Y_AXIS) {
865
866 idxA = 0;
867 idxB = 2;
868 step = LeafNodeType::DIM;
869
870 nextOffsets = mConnectivity->offsetsNextY();
871 prevOffsets = mConnectivity->offsetsPrevY();
872
873 } else if (mAxis == X_AXIS) {
874
875 idxA = 1;
876 idxB = 2;
877 step = LeafNodeType::DIM * LeafNodeType::DIM;
878
879 nextOffsets = mConnectivity->offsetsNextX();
880 prevOffsets = mConnectivity->offsetsPrevX();
881 }
882
883 Coord ijk(0, 0, 0);
884
885 int& a = ijk[idxA];
886 int& b = ijk[idxB];
887
888 for (size_t n = range.begin(), N = range.end(); n < N; ++n) {
889
890 size_t startOffset = mStartNodeIndices[n];
891 size_t lastOffset = startOffset;
892
893 Index pos(0);
894
895 for (a = 0; a < int(LeafNodeType::DIM); ++a) {
896 for (b = 0; b < int(LeafNodeType::DIM); ++b) {
897
898 pos = LeafNodeType::coordToOffset(ijk);
899 size_t offset = startOffset;
900
901 // sweep in +axis direction until a boundary voxel is hit.
902 while ( offset != ConnectivityTable::INVALID_OFFSET &&
903 traceVoxelLine(*nodes[offset], pos, step) ) {
904
905 lastOffset = offset;
906 offset = nextOffsets[offset];
907 }
908
909 // find last leafnode in +axis direction
910 offset = lastOffset;
911 while (offset != ConnectivityTable::INVALID_OFFSET) {
912 lastOffset = offset;
913 offset = nextOffsets[offset];
914 }
915
916 // sweep in -axis direction until a boundary voxel is hit.
917 offset = lastOffset;
918 pos += step * (LeafNodeType::DIM - 1);
919 while ( offset != ConnectivityTable::INVALID_OFFSET &&
920 traceVoxelLine(*nodes[offset], pos, -step)) {
921 offset = prevOffsets[offset];
922 }
923 }
924 }
925 }
926 }
927
928
929 bool traceVoxelLine(LeafNodeType& node, Index pos, Index step) const {
930
931 ValueType* data = node.buffer().data();
932
933 bool isOutside = true;
934
935 for (Index i = 0; i < LeafNodeType::DIM; ++i) {
936
937 ValueType& dist = data[pos];
938
939 if (dist < ValueType(0.0)) {
940 isOutside = true;
941 } else {
942 // Boundary voxel check. (Voxel that intersects the surface)
943 if (!(dist > ValueType(0.75))) isOutside = false;
944
945 if (isOutside) dist = ValueType(-dist);
946 }
947
948 pos += step;
949 }
950
951 return isOutside;
952 }
953
954
955 private:
956 size_t const * const mStartNodeIndices;
957 ConnectivityTable * const mConnectivity;
958
959 const Axis mAxis;
960 }; // class SweepExteriorSign
961
962
963 template<typename LeafNodeType>
964 inline void
965 seedFill(LeafNodeType& node)
966 {
967 using ValueType = typename LeafNodeType::ValueType;
968 using Queue = std::deque<Index>;
969
970
971 ValueType* data = node.buffer().data();
972
973 // find seed points
974 Queue seedPoints;
975 for (Index pos = 0; pos < LeafNodeType::SIZE; ++pos) {
976 if (data[pos] < 0.0) seedPoints.push_back(pos);
977 }
978
979 if (seedPoints.empty()) return;
980
981 // clear sign information
982 for (Queue::iterator it = seedPoints.begin(); it != seedPoints.end(); ++it) {
983 ValueType& dist = data[*it];
984 dist = -dist;
985 }
986
987 // flood fill
988
989 Coord ijk(0, 0, 0);
990 Index pos(0), nextPos(0);
991
992 while (!seedPoints.empty()) {
993
994 pos = seedPoints.back();
995 seedPoints.pop_back();
996
997 ValueType& dist = data[pos];
998
999 if (!(dist < ValueType(0.0))) {
1000
1001 dist = -dist; // flip sign
1002
1003 ijk = LeafNodeType::offsetToLocalCoord(pos);
1004
1005 if (ijk[0] != 0) { // i - 1, j, k
1006 nextPos = pos - LeafNodeType::DIM * LeafNodeType::DIM;
1007 if (data[nextPos] > ValueType(0.75)) seedPoints.push_back(nextPos);
1008 }
1009
1010 if (ijk[0] != (LeafNodeType::DIM - 1)) { // i + 1, j, k
1011 nextPos = pos + LeafNodeType::DIM * LeafNodeType::DIM;
1012 if (data[nextPos] > ValueType(0.75)) seedPoints.push_back(nextPos);
1013 }
1014
1015 if (ijk[1] != 0) { // i, j - 1, k
1016 nextPos = pos - LeafNodeType::DIM;
1017 if (data[nextPos] > ValueType(0.75)) seedPoints.push_back(nextPos);
1018 }
1019
1020 if (ijk[1] != (LeafNodeType::DIM - 1)) { // i, j + 1, k
1021 nextPos = pos + LeafNodeType::DIM;
1022 if (data[nextPos] > ValueType(0.75)) seedPoints.push_back(nextPos);
1023 }
1024
1025 if (ijk[2] != 0) { // i, j, k - 1
1026 nextPos = pos - 1;
1027 if (data[nextPos] > ValueType(0.75)) seedPoints.push_back(nextPos);
1028 }
1029
1030 if (ijk[2] != (LeafNodeType::DIM - 1)) { // i, j, k + 1
1031 nextPos = pos + 1;
1032 if (data[nextPos] > ValueType(0.75)) seedPoints.push_back(nextPos);
1033 }
1034 }
1035 }
1036 } // seedFill()
1037
1038
1039 template<typename LeafNodeType>
1040 inline bool
1041 scanFill(LeafNodeType& node)
1042 {
1043 bool updatedNode = false;
1044
1045 using ValueType = typename LeafNodeType::ValueType;
1046 ValueType* data = node.buffer().data();
1047
1048 Coord ijk(0, 0, 0);
1049
1050 bool updatedSign = true;
1051 while (updatedSign) {
1052
1053 updatedSign = false;
1054
1055 for (Index pos = 0; pos < LeafNodeType::SIZE; ++pos) {
1056
1057 ValueType& dist = data[pos];
1058
1059 if (!(dist < ValueType(0.0)) && dist > ValueType(0.75)) {
1060
1061 ijk = LeafNodeType::offsetToLocalCoord(pos);
1062
1063 // i, j, k - 1
1064 if (ijk[2] != 0 && data[pos - 1] < ValueType(0.0)) {
1065 updatedSign = true;
1066 dist = ValueType(-dist);
1067
1068 // i, j, k + 1
1069 } else if (ijk[2] != (LeafNodeType::DIM - 1) && data[pos + 1] < ValueType(0.0)) {
1070 updatedSign = true;
1071 dist = ValueType(-dist);
1072
1073 // i, j - 1, k
1074 } else if (ijk[1] != 0 && data[pos - LeafNodeType::DIM] < ValueType(0.0)) {
1075 updatedSign = true;
1076 dist = ValueType(-dist);
1077
1078 // i, j + 1, k
1079 } else if (ijk[1] != (LeafNodeType::DIM - 1)
1080 && data[pos + LeafNodeType::DIM] < ValueType(0.0))
1081 {
1082 updatedSign = true;
1083 dist = ValueType(-dist);
1084
1085 // i - 1, j, k
1086 } else if (ijk[0] != 0
1087 && data[pos - LeafNodeType::DIM * LeafNodeType::DIM] < ValueType(0.0))
1088 {
1089 updatedSign = true;
1090 dist = ValueType(-dist);
1091
1092 // i + 1, j, k
1093 } else if (ijk[0] != (LeafNodeType::DIM - 1)
1094 && data[pos + LeafNodeType::DIM * LeafNodeType::DIM] < ValueType(0.0))
1095 {
1096 updatedSign = true;
1097 dist = ValueType(-dist);
1098 }
1099 }
1100 } // end value loop
1101
1102 updatedNode |= updatedSign;
1103 } // end update loop
1104
1105 return updatedNode;
1106 } // scanFill()
1107
1108
1109 template<typename TreeType>
1110 class SeedFillExteriorSign
1111 {
1112 public:
1113 using ValueType = typename TreeType::ValueType;
1114 using LeafNodeType = typename TreeType::LeafNodeType;
1115
1116 SeedFillExteriorSign(std::vector<LeafNodeType*>& nodes, bool* changedNodeMask)
1117 : mNodes(nodes.empty() ? nullptr : &nodes[0])
1118 , mChangedNodeMask(changedNodeMask)
1119 {
1120 }
1121
1122 void operator()(const tbb::blocked_range<size_t>& range) const {
1123 for (size_t n = range.begin(), N = range.end(); n < N; ++n) {
1124 if (mChangedNodeMask[n]) {
1125 //seedFill(*mNodes[n]);
1126 mChangedNodeMask[n] = scanFill(*mNodes[n]);
1127 }
1128 }
1129 }
1130
1131 LeafNodeType ** const mNodes;
1132 bool * const mChangedNodeMask;
1133 };
1134
1135
1136 template<typename ValueType>
1137 struct FillArray
1138 {
1139 FillArray(ValueType* array, const ValueType v) : mArray(array), mValue(v) { }
1140
1141 void operator()(const tbb::blocked_range<size_t>& range) const {
1142 const ValueType v = mValue;
1143 for (size_t n = range.begin(), N = range.end(); n < N; ++n) {
1144 mArray[n] = v;
1145 }
1146 }
1147
1148 ValueType * const mArray;
1149 const ValueType mValue;
1150 };
1151
1152
1153 template<typename ValueType>
1154 inline void
1155 fillArray(ValueType* array, const ValueType val, const size_t length)
1156 {
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);
1160 tbb::parallel_for(range, FillArray<ValueType>(array, val), tbb::simple_partitioner());
1161 }
1162
1163
1164 template<typename TreeType>
1165 class SyncVoxelMask
1166 {
1167 public:
1168 using ValueType = typename TreeType::ValueType;
1169 using LeafNodeType = typename TreeType::LeafNodeType;
1170
1171 SyncVoxelMask(std::vector<LeafNodeType*>& nodes,
1172 const bool* changedNodeMask, bool* changedVoxelMask)
1173 : mNodes(nodes.empty() ? nullptr : &nodes[0])
1174 , mChangedNodeMask(changedNodeMask)
1175 , mChangedVoxelMask(changedVoxelMask)
1176 {
1177 }
1178
1179 void operator()(const tbb::blocked_range<size_t>& range) const {
1180 for (size_t n = range.begin(), N = range.end(); n < N; ++n) {
1181
1182 if (mChangedNodeMask[n]) {
1183 bool* mask = &mChangedVoxelMask[n * LeafNodeType::SIZE];
1184
1185 ValueType* data = mNodes[n]->buffer().data();
1186
1187 for (Index pos = 0; pos < LeafNodeType::SIZE; ++pos) {
1188 if (mask[pos]) {
1189 data[pos] = ValueType(-data[pos]);
1190 mask[pos] = false;
1191 }
1192 }
1193 }
1194 }
1195 }
1196
1197 LeafNodeType ** const mNodes;
1198 bool const * const mChangedNodeMask;
1199 bool * const mChangedVoxelMask;
1200 };
1201
1202
1203 template<typename TreeType>
1204 class SeedPoints
1205 {
1206 public:
1207 using ValueType = typename TreeType::ValueType;
1208 using LeafNodeType = typename TreeType::LeafNodeType;
1209 using ConnectivityTable = LeafNodeConnectivityTable<TreeType>;
1210
1211 SeedPoints(ConnectivityTable& connectivity,
1212 bool* changedNodeMask, bool* nodeMask, bool* changedVoxelMask)
1213 : mConnectivity(&connectivity)
1214 , mChangedNodeMask(changedNodeMask)
1215 , mNodeMask(nodeMask)
1216 , mChangedVoxelMask(changedVoxelMask)
1217 {
1218 }
1219
1220 void operator()(const tbb::blocked_range<size_t>& range) const {
1221
1222 for (size_t n = range.begin(), N = range.end(); n < N; ++n) {
1223
1224 if (!mChangedNodeMask[n]) {
1225
1226 bool changedValue = false;
1227
1228 changedValue |= processZ(n, /*firstFace=*/true);
1229 changedValue |= processZ(n, /*firstFace=*/false);
1230
1231 changedValue |= processY(n, /*firstFace=*/true);
1232 changedValue |= processY(n, /*firstFace=*/false);
1233
1234 changedValue |= processX(n, /*firstFace=*/true);
1235 changedValue |= processX(n, /*firstFace=*/false);
1236
1237 mNodeMask[n] = changedValue;
1238 }
1239 }
1240 }
1241
1242
1243 bool processZ(const size_t n, bool firstFace) const
1244 {
1245 const size_t offset =
1246 firstFace ? mConnectivity->offsetsPrevZ()[n] : mConnectivity->offsetsNextZ()[n];
1247 if (offset != ConnectivityTable::INVALID_OFFSET && mChangedNodeMask[offset]) {
1248
1249 bool* mask = &mChangedVoxelMask[n * LeafNodeType::SIZE];
1250
1251 const ValueType* lhsData = mConnectivity->nodes()[n]->buffer().data();
1252 const ValueType* rhsData = mConnectivity->nodes()[offset]->buffer().data();
1253
1254 const Index lastOffset = LeafNodeType::DIM - 1;
1255 const Index lhsOffset =
1256 firstFace ? 0 : lastOffset, rhsOffset = firstFace ? lastOffset : 0;
1257
1258 Index tmpPos(0), pos(0);
1259 bool changedValue = false;
1260
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);
1265
1266 if (lhsData[pos + lhsOffset] > ValueType(0.75)) {
1267 if (rhsData[pos + rhsOffset] < ValueType(0.0)) {
1268 changedValue = true;
1269 mask[pos + lhsOffset] = true;
1270 }
1271 }
1272 }
1273 }
1274
1275 return changedValue;
1276 }
1277
1278 return false;
1279 }
1280
1281 bool processY(const size_t n, bool firstFace) const
1282 {
1283 const size_t offset =
1284 firstFace ? mConnectivity->offsetsPrevY()[n] : mConnectivity->offsetsNextY()[n];
1285 if (offset != ConnectivityTable::INVALID_OFFSET && mChangedNodeMask[offset]) {
1286
1287 bool* mask = &mChangedVoxelMask[n * LeafNodeType::SIZE];
1288
1289 const ValueType* lhsData = mConnectivity->nodes()[n]->buffer().data();
1290 const ValueType* rhsData = mConnectivity->nodes()[offset]->buffer().data();
1291
1292 const Index lastOffset = LeafNodeType::DIM * (LeafNodeType::DIM - 1);
1293 const Index lhsOffset =
1294 firstFace ? 0 : lastOffset, rhsOffset = firstFace ? lastOffset : 0;
1295
1296 Index tmpPos(0), pos(0);
1297 bool changedValue = false;
1298
1299 for (Index x = 0; x < LeafNodeType::DIM; ++x) {
1300 tmpPos = x << (2 * LeafNodeType::LOG2DIM);
1301 for (Index z = 0; z < LeafNodeType::DIM; ++z) {
1302 pos = tmpPos + z;
1303
1304 if (lhsData[pos + lhsOffset] > ValueType(0.75)) {
1305 if (rhsData[pos + rhsOffset] < ValueType(0.0)) {
1306 changedValue = true;
1307 mask[pos + lhsOffset] = true;
1308 }
1309 }
1310 }
1311 }
1312
1313 return changedValue;
1314 }
1315
1316 return false;
1317 }
1318
1319 bool processX(const size_t n, bool firstFace) const
1320 {
1321 const size_t offset =
1322 firstFace ? mConnectivity->offsetsPrevX()[n] : mConnectivity->offsetsNextX()[n];
1323 if (offset != ConnectivityTable::INVALID_OFFSET && mChangedNodeMask[offset]) {
1324
1325 bool* mask = &mChangedVoxelMask[n * LeafNodeType::SIZE];
1326
1327 const ValueType* lhsData = mConnectivity->nodes()[n]->buffer().data();
1328 const ValueType* rhsData = mConnectivity->nodes()[offset]->buffer().data();
1329
1330 const Index lastOffset = LeafNodeType::DIM * LeafNodeType::DIM * (LeafNodeType::DIM-1);
1331 const Index lhsOffset =
1332 firstFace ? 0 : lastOffset, rhsOffset = firstFace ? lastOffset : 0;
1333
1334 Index tmpPos(0), pos(0);
1335 bool changedValue = false;
1336
1337 for (Index y = 0; y < LeafNodeType::DIM; ++y) {
1338 tmpPos = y << LeafNodeType::LOG2DIM;
1339 for (Index z = 0; z < LeafNodeType::DIM; ++z) {
1340 pos = tmpPos + z;
1341
1342 if (lhsData[pos + lhsOffset] > ValueType(0.75)) {
1343 if (rhsData[pos + rhsOffset] < ValueType(0.0)) {
1344 changedValue = true;
1345 mask[pos + lhsOffset] = true;
1346 }
1347 }
1348 }
1349 }
1350
1351 return changedValue;
1352 }
1353
1354 return false;
1355 }
1356
1357 ConnectivityTable * const mConnectivity;
1358 bool * const mChangedNodeMask;
1359 bool * const mNodeMask;
1360 bool * const mChangedVoxelMask;
1361 };
1362
1363
1365
1366 template<typename TreeType, typename MeshDataAdapter>
1367 struct ComputeIntersectingVoxelSign
1368 {
1369 using ValueType = typename TreeType::ValueType;
1370 using LeafNodeType = typename TreeType::LeafNodeType;
1371 using Int32TreeType = typename TreeType::template ValueConverter<Int32>::Type;
1372 using Int32LeafNodeType = typename Int32TreeType::LeafNodeType;
1373
1374 using PointArray = std::unique_ptr<Vec3d[]>;
1375 using MaskArray = std::unique_ptr<bool[]>;
1376 using LocalData = std::pair<PointArray, MaskArray>;
1377 using LocalDataTable = tbb::enumerable_thread_specific<LocalData>;
1378
1379 ComputeIntersectingVoxelSign(
1380 std::vector<LeafNodeType*>& distNodes,
1381 const TreeType& distTree,
1382 const Int32TreeType& indexTree,
1383 const MeshDataAdapter& mesh)
1384 : mDistNodes(distNodes.empty() ? nullptr : &distNodes[0])
1385 , mDistTree(&distTree)
1386 , mIndexTree(&indexTree)
1387 , mMesh(&mesh)
1388 , mLocalDataTable(new LocalDataTable())
1389 {
1390 }
1391
1392
1393 void operator()(const tbb::blocked_range<size_t>& range) const {
1394
1395 tree::ValueAccessor<const TreeType> distAcc(*mDistTree);
1396 tree::ValueAccessor<const Int32TreeType> idxAcc(*mIndexTree);
1397
1398 ValueType nval;
1399 CoordBBox bbox;
1400 Index xPos(0), yPos(0);
1401 Coord ijk, nijk, nodeMin, nodeMax;
1402 Vec3d cp, xyz, nxyz, dir1, dir2;
1403
1404 LocalData& localData = mLocalDataTable->local();
1405
1406 PointArray& points = localData.first;
1407 if (!points) points.reset(new Vec3d[LeafNodeType::SIZE * 2]);
1408
1409 MaskArray& mask = localData.second;
1410 if (!mask) mask.reset(new bool[LeafNodeType::SIZE]);
1411
1412
1413 typename LeafNodeType::ValueOnCIter it;
1414
1415 for (size_t n = range.begin(), N = range.end(); n < N; ++n) {
1416
1417 LeafNodeType& node = *mDistNodes[n];
1418 ValueType* data = node.buffer().data();
1419
1420 const Int32LeafNodeType* idxNode = idxAcc.probeConstLeaf(node.origin());
1421 const Int32* idxData = idxNode->buffer().data();
1422
1423 nodeMin = node.origin();
1424 nodeMax = nodeMin.offsetBy(LeafNodeType::DIM - 1);
1425
1426 // reset computed voxel mask.
1427 memset(mask.get(), 0, sizeof(bool) * LeafNodeType::SIZE);
1428
1429 for (it = node.cbeginValueOn(); it; ++it) {
1430 Index pos = it.pos();
1431
1432 ValueType& dist = data[pos];
1433 if (dist < 0.0 || dist > 0.75) continue;
1434
1435 ijk = node.offsetToGlobalCoord(pos);
1436
1437 xyz[0] = double(ijk[0]);
1438 xyz[1] = double(ijk[1]);
1439 xyz[2] = double(ijk[2]);
1440
1441
1442 bbox.min() = Coord::maxComponent(ijk.offsetBy(-1), nodeMin);
1443 bbox.max() = Coord::minComponent(ijk.offsetBy(1), nodeMax);
1444
1445 bool flipSign = false;
1446
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));
1453
1454 const Int32& polyIdx = idxData[pos];
1455
1456 if (polyIdx == Int32(util::INVALID_IDX) || !(data[pos] < -0.75))
1457 continue;
1458
1459 const Index pointIndex = pos * 2;
1460
1461 if (!mask[pos]) {
1462
1463 mask[pos] = true;
1464
1465 nxyz[0] = double(nijk[0]);
1466 nxyz[1] = double(nijk[1]);
1467 nxyz[2] = double(nijk[2]);
1468
1469 Vec3d& point = points[pointIndex];
1470
1471 point = closestPoint(nxyz, polyIdx);
1472
1473 Vec3d& direction = points[pointIndex + 1];
1474 direction = nxyz - point;
1475 direction.normalize();
1476 }
1477
1478 dir1 = xyz - points[pointIndex];
1479 dir1.normalize();
1480
1481 if (points[pointIndex + 1].dot(dir1) > 0.0) {
1482 flipSign = true;
1483 break;
1484 }
1485 }
1486 }
1487 }
1488
1489 if (flipSign) {
1490 dist = -dist;
1491 } else {
1492 for (Int32 m = 0; m < 26; ++m) {
1493 nijk = ijk + util::COORD_OFFSETS[m];
1494
1495 if (!bbox.isInside(nijk) && distAcc.probeValue(nijk, nval) && nval<-0.75) {
1496 nxyz[0] = double(nijk[0]);
1497 nxyz[1] = double(nijk[1]);
1498 nxyz[2] = double(nijk[2]);
1499
1500 cp = closestPoint(nxyz, idxAcc.getValue(nijk));
1501
1502 dir1 = xyz - cp;
1503 dir1.normalize();
1504
1505 dir2 = nxyz - cp;
1506 dir2.normalize();
1507
1508 if (dir2.dot(dir1) > 0.0) {
1509 dist = -dist;
1510 break;
1511 }
1512 }
1513 }
1514 }
1515
1516 } // active voxel loop
1517 } // leaf node loop
1518 }
1519
1520 private:
1521
1522 Vec3d closestPoint(const Vec3d& center, Int32 polyIdx) const
1523 {
1524 Vec3d a, b, c, cp, uvw;
1525
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);
1530
1531 cp = closestPointOnTriangleToPoint(a, c, b, center, uvw);
1532
1533 if (4 == mMesh->vertexCount(polygon)) {
1534
1535 mMesh->getIndexSpacePoint(polygon, 3, b);
1536
1537 c = closestPointOnTriangleToPoint(a, b, c, center, uvw);
1538
1539 if ((center - c).lengthSqr() < (center - cp).lengthSqr()) {
1540 cp = c;
1541 }
1542 }
1543
1544 return cp;
1545 }
1546
1547
1548 LeafNodeType ** const mDistNodes;
1549 TreeType const * const mDistTree;
1550 Int32TreeType const * const mIndexTree;
1551 MeshDataAdapter const * const mMesh;
1552
1553 SharedPtr<LocalDataTable> mLocalDataTable;
1554 }; // ComputeIntersectingVoxelSign
1555
1556
1558
1559
1560 template<typename LeafNodeType>
1561 inline void
1562 maskNodeInternalNeighbours(const Index pos, bool (&mask)[26])
1563 {
1564 using NodeT = LeafNodeType;
1565
1566 const Coord ijk = NodeT::offsetToLocalCoord(pos);
1567
1568 // Face adjacent neighbours
1569 // i+1, j, k
1570 mask[0] = ijk[0] != (NodeT::DIM - 1);
1571 // i-1, j, k
1572 mask[1] = ijk[0] != 0;
1573 // i, j+1, k
1574 mask[2] = ijk[1] != (NodeT::DIM - 1);
1575 // i, j-1, k
1576 mask[3] = ijk[1] != 0;
1577 // i, j, k+1
1578 mask[4] = ijk[2] != (NodeT::DIM - 1);
1579 // i, j, k-1
1580 mask[5] = ijk[2] != 0;
1581
1582 // Edge adjacent neighbour
1583 // i+1, j, k-1
1584 mask[6] = mask[0] && mask[5];
1585 // i-1, j, k-1
1586 mask[7] = mask[1] && mask[5];
1587 // i+1, j, k+1
1588 mask[8] = mask[0] && mask[4];
1589 // i-1, j, k+1
1590 mask[9] = mask[1] && mask[4];
1591 // i+1, j+1, k
1592 mask[10] = mask[0] && mask[2];
1593 // i-1, j+1, k
1594 mask[11] = mask[1] && mask[2];
1595 // i+1, j-1, k
1596 mask[12] = mask[0] && mask[3];
1597 // i-1, j-1, k
1598 mask[13] = mask[1] && mask[3];
1599 // i, j-1, k+1
1600 mask[14] = mask[3] && mask[4];
1601 // i, j-1, k-1
1602 mask[15] = mask[3] && mask[5];
1603 // i, j+1, k+1
1604 mask[16] = mask[2] && mask[4];
1605 // i, j+1, k-1
1606 mask[17] = mask[2] && mask[5];
1607
1608 // Corner adjacent neighbours
1609 // i-1, j-1, k-1
1610 mask[18] = mask[1] && mask[3] && mask[5];
1611 // i-1, j-1, k+1
1612 mask[19] = mask[1] && mask[3] && mask[4];
1613 // i+1, j-1, k+1
1614 mask[20] = mask[0] && mask[3] && mask[4];
1615 // i+1, j-1, k-1
1616 mask[21] = mask[0] && mask[3] && mask[5];
1617 // i-1, j+1, k-1
1618 mask[22] = mask[1] && mask[2] && mask[5];
1619 // i-1, j+1, k+1
1620 mask[23] = mask[1] && mask[2] && mask[4];
1621 // i+1, j+1, k+1
1622 mask[24] = mask[0] && mask[2] && mask[4];
1623 // i+1, j+1, k-1
1624 mask[25] = mask[0] && mask[2] && mask[5];
1625 }
1626
1627
1628 template<typename Compare, typename LeafNodeType>
1629 inline bool
1630 checkNeighbours(const Index pos, const typename LeafNodeType::ValueType * data, bool (&mask)[26])
1631 {
1632 using NodeT = LeafNodeType;
1633
1634 // i, j, k - 1
1635 if (mask[5] && Compare::check(data[pos - 1])) return true;
1636 // i, j, k + 1
1637 if (mask[4] && Compare::check(data[pos + 1])) return true;
1638 // i, j - 1, k
1639 if (mask[3] && Compare::check(data[pos - NodeT::DIM])) return true;
1640 // i, j + 1, k
1641 if (mask[2] && Compare::check(data[pos + NodeT::DIM])) return true;
1642 // i - 1, j, k
1643 if (mask[1] && Compare::check(data[pos - NodeT::DIM * NodeT::DIM])) return true;
1644 // i + 1, j, k
1645 if (mask[0] && Compare::check(data[pos + NodeT::DIM * NodeT::DIM])) return true;
1646 // i+1, j, k-1
1647 if (mask[6] && Compare::check(data[pos + NodeT::DIM * NodeT::DIM])) return true;
1648 // i-1, j, k-1
1649 if (mask[7] && Compare::check(data[pos - NodeT::DIM * NodeT::DIM - 1])) return true;
1650 // i+1, j, k+1
1651 if (mask[8] && Compare::check(data[pos + NodeT::DIM * NodeT::DIM + 1])) return true;
1652 // i-1, j, k+1
1653 if (mask[9] && Compare::check(data[pos - NodeT::DIM * NodeT::DIM + 1])) return true;
1654 // i+1, j+1, k
1655 if (mask[10] && Compare::check(data[pos + NodeT::DIM * NodeT::DIM + NodeT::DIM])) return true;
1656 // i-1, j+1, k
1657 if (mask[11] && Compare::check(data[pos - NodeT::DIM * NodeT::DIM + NodeT::DIM])) return true;
1658 // i+1, j-1, k
1659 if (mask[12] && Compare::check(data[pos + NodeT::DIM * NodeT::DIM - NodeT::DIM])) return true;
1660 // i-1, j-1, k
1661 if (mask[13] && Compare::check(data[pos - NodeT::DIM * NodeT::DIM - NodeT::DIM])) return true;
1662 // i, j-1, k+1
1663 if (mask[14] && Compare::check(data[pos - NodeT::DIM + 1])) return true;
1664 // i, j-1, k-1
1665 if (mask[15] && Compare::check(data[pos - NodeT::DIM - 1])) return true;
1666 // i, j+1, k+1
1667 if (mask[16] && Compare::check(data[pos + NodeT::DIM + 1])) return true;
1668 // i, j+1, k-1
1669 if (mask[17] && Compare::check(data[pos + NodeT::DIM - 1])) return true;
1670 // i-1, j-1, k-1
1671 if (mask[18] && Compare::check(data[pos - NodeT::DIM * NodeT::DIM - NodeT::DIM - 1])) return true;
1672 // i-1, j-1, k+1
1673 if (mask[19] && Compare::check(data[pos - NodeT::DIM * NodeT::DIM - NodeT::DIM + 1])) return true;
1674 // i+1, j-1, k+1
1675 if (mask[20] && Compare::check(data[pos + NodeT::DIM * NodeT::DIM - NodeT::DIM + 1])) return true;
1676 // i+1, j-1, k-1
1677 if (mask[21] && Compare::check(data[pos + NodeT::DIM * NodeT::DIM - NodeT::DIM - 1])) return true;
1678 // i-1, j+1, k-1
1679 if (mask[22] && Compare::check(data[pos - NodeT::DIM * NodeT::DIM + NodeT::DIM - 1])) return true;
1680 // i-1, j+1, k+1
1681 if (mask[23] && Compare::check(data[pos - NodeT::DIM * NodeT::DIM + NodeT::DIM + 1])) return true;
1682 // i+1, j+1, k+1
1683 if (mask[24] && Compare::check(data[pos + NodeT::DIM * NodeT::DIM + NodeT::DIM + 1])) return true;
1684 // i+1, j+1, k-1
1685 if (mask[25] && Compare::check(data[pos + NodeT::DIM * NodeT::DIM + NodeT::DIM - 1])) return true;
1686
1687 return false;
1688 }
1689
1690
1691 template<typename Compare, typename AccessorType>
1692 inline bool
1693 checkNeighbours(const Coord& ijk, AccessorType& acc, bool (&mask)[26])
1694 {
1695 for (Int32 m = 0; m < 26; ++m) {
1696 if (!mask[m] && Compare::check(acc.getValue(ijk + util::COORD_OFFSETS[m]))) {
1697 return true;
1698 }
1699 }
1700
1701 return false;
1702 }
1703
1704
1705 template<typename TreeType>
1706 struct ValidateIntersectingVoxels
1707 {
1708 using ValueType = typename TreeType::ValueType;
1709 using LeafNodeType = typename TreeType::LeafNodeType;
1710
1711 struct IsNegative { static bool check(const ValueType v) { return v < ValueType(0.0); } };
1712
1713 ValidateIntersectingVoxels(TreeType& tree, std::vector<LeafNodeType*>& nodes)
1714 : mTree(&tree)
1715 , mNodes(nodes.empty() ? nullptr : &nodes[0])
1716 {
1717 }
1718
1719 void operator()(const tbb::blocked_range<size_t>& range) const
1720 {
1721 tree::ValueAccessor<const TreeType> acc(*mTree);
1722 bool neighbourMask[26];
1723
1724 for (size_t n = range.begin(), N = range.end(); n < N; ++n) {
1725
1726 LeafNodeType& node = *mNodes[n];
1727 ValueType* data = node.buffer().data();
1728
1729 typename LeafNodeType::ValueOnCIter it;
1730 for (it = node.cbeginValueOn(); it; ++it) {
1731
1732 const Index pos = it.pos();
1733
1734 ValueType& dist = data[pos];
1735 if (dist < 0.0 || dist > 0.75) continue;
1736
1737 // Mask node internal neighbours
1738 maskNodeInternalNeighbours<LeafNodeType>(pos, neighbourMask);
1739
1740 const bool hasNegativeNeighbour =
1741 checkNeighbours<IsNegative, LeafNodeType>(pos, data, neighbourMask) ||
1742 checkNeighbours<IsNegative>(node.offsetToGlobalCoord(pos), acc, neighbourMask);
1743
1744 if (!hasNegativeNeighbour) {
1745 // push over boundary voxel distance
1746 dist = ValueType(0.75) + Tolerance<ValueType>::epsilon();
1747 }
1748 }
1749 }
1750 }
1751
1752 TreeType * const mTree;
1753 LeafNodeType ** const mNodes;
1754 }; // ValidateIntersectingVoxels
1755
1756
1757 template<typename TreeType>
1758 struct RemoveSelfIntersectingSurface
1759 {
1760 using ValueType = typename TreeType::ValueType;
1761 using LeafNodeType = typename TreeType::LeafNodeType;
1762 using Int32TreeType = typename TreeType::template ValueConverter<Int32>::Type;
1763
1764 struct Comp { static bool check(const ValueType v) { return !(v > ValueType(0.75)); } };
1765
1766 RemoveSelfIntersectingSurface(std::vector<LeafNodeType*>& nodes,
1767 TreeType& distTree, Int32TreeType& indexTree)
1768 : mNodes(nodes.empty() ? nullptr : &nodes[0])
1769 , mDistTree(&distTree)
1770 , mIndexTree(&indexTree)
1771 {
1772 }
1773
1774 void operator()(const tbb::blocked_range<size_t>& range) const
1775 {
1776 tree::ValueAccessor<const TreeType> distAcc(*mDistTree);
1777 tree::ValueAccessor<Int32TreeType> idxAcc(*mIndexTree);
1778 bool neighbourMask[26];
1779
1780 for (size_t n = range.begin(), N = range.end(); n < N; ++n) {
1781
1782 LeafNodeType& distNode = *mNodes[n];
1783 ValueType* data = distNode.buffer().data();
1784
1785 typename Int32TreeType::LeafNodeType* idxNode =
1786 idxAcc.probeLeaf(distNode.origin());
1787
1788 typename LeafNodeType::ValueOnCIter it;
1789 for (it = distNode.cbeginValueOn(); it; ++it) {
1790
1791 const Index pos = it.pos();
1792
1793 if (!(data[pos] > 0.75)) continue;
1794
1795 // Mask node internal neighbours
1796 maskNodeInternalNeighbours<LeafNodeType>(pos, neighbourMask);
1797
1798 const bool hasBoundaryNeighbour =
1799 checkNeighbours<Comp, LeafNodeType>(pos, data, neighbourMask) ||
1800 checkNeighbours<Comp>(distNode.offsetToGlobalCoord(pos),distAcc,neighbourMask);
1801
1802 if (!hasBoundaryNeighbour) {
1803 distNode.setValueOff(pos);
1804 idxNode->setValueOff(pos);
1805 }
1806 }
1807 }
1808 }
1809
1810 LeafNodeType * * const mNodes;
1811 TreeType * const mDistTree;
1812 Int32TreeType * const mIndexTree;
1813 }; // RemoveSelfIntersectingSurface
1814
1815
1817
1818
1819 template<typename NodeType>
1820 struct ReleaseChildNodes
1821 {
1822 ReleaseChildNodes(NodeType ** nodes) : mNodes(nodes) {}
1823
1824 void operator()(const tbb::blocked_range<size_t>& range) const {
1825
1826 using NodeMaskType = typename NodeType::NodeMaskType;
1827
1828 for (size_t n = range.begin(), N = range.end(); n < N; ++n) {
1829 const_cast<NodeMaskType&>(mNodes[n]->getChildMask()).setOff();
1830 }
1831 }
1832
1833 NodeType ** const mNodes;
1834 };
1835
1836
1837 template<typename TreeType>
1838 inline void
1839 releaseLeafNodes(TreeType& tree)
1840 {
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;
1844
1845 std::vector<InternalNodeType*> nodes;
1846 tree.getNodes(nodes);
1847
1848 tbb::parallel_for(tbb::blocked_range<size_t>(0, nodes.size()),
1849 ReleaseChildNodes<InternalNodeType>(nodes.empty() ? nullptr : &nodes[0]));
1850 }
1851
1852
1853 template<typename TreeType>
1854 struct StealUniqueLeafNodes
1855 {
1856 using LeafNodeType = typename TreeType::LeafNodeType;
1857
1858 StealUniqueLeafNodes(TreeType& lhsTree, TreeType& rhsTree,
1859 std::vector<LeafNodeType*>& overlappingNodes)
1860 : mLhsTree(&lhsTree)
1861 , mRhsTree(&rhsTree)
1862 , mNodes(&overlappingNodes)
1863 {
1864 }
1865
1866 void operator()() const {
1867
1868 std::vector<LeafNodeType*> rhsLeafNodes;
1869
1870 rhsLeafNodes.reserve(mRhsTree->leafCount());
1871 //mRhsTree->getNodes(rhsLeafNodes);
1872 //releaseLeafNodes(*mRhsTree);
1873 mRhsTree->stealNodes(rhsLeafNodes);
1874
1875 tree::ValueAccessor<TreeType> acc(*mLhsTree);
1876
1877 for (size_t n = 0, N = rhsLeafNodes.size(); n < N; ++n) {
1878 if (!acc.probeLeaf(rhsLeafNodes[n]->origin())) {
1879 acc.addLeaf(rhsLeafNodes[n]);
1880 } else {
1881 mNodes->push_back(rhsLeafNodes[n]);
1882 }
1883 }
1884 }
1885
1886 private:
1887 TreeType * const mLhsTree;
1888 TreeType * const mRhsTree;
1889 std::vector<LeafNodeType*> * const mNodes;
1890 };
1891
1892
1893 template<typename DistTreeType, typename IndexTreeType>
1894 inline void
1895 combineData(DistTreeType& lhsDist, IndexTreeType& lhsIdx,
1896 DistTreeType& rhsDist, IndexTreeType& rhsIdx)
1897 {
1898 using DistLeafNodeType = typename DistTreeType::LeafNodeType;
1899 using IndexLeafNodeType = typename IndexTreeType::LeafNodeType;
1900
1901 std::vector<DistLeafNodeType*> overlappingDistNodes;
1902 std::vector<IndexLeafNodeType*> overlappingIdxNodes;
1903
1904 // Steal unique leafnodes
1905 tbb::task_group tasks;
1906 tasks.run(StealUniqueLeafNodes<DistTreeType>(lhsDist, rhsDist, overlappingDistNodes));
1907 tasks.run(StealUniqueLeafNodes<IndexTreeType>(lhsIdx, rhsIdx, overlappingIdxNodes));
1908 tasks.wait();
1909
1910 // Combine overlapping leaf nodes
1911 if (!overlappingDistNodes.empty() && !overlappingIdxNodes.empty()) {
1912 tbb::parallel_for(tbb::blocked_range<size_t>(0, overlappingDistNodes.size()),
1913 CombineLeafNodes<DistTreeType>(lhsDist, lhsIdx,
1914 &overlappingDistNodes[0], &overlappingIdxNodes[0]));
1915 }
1916 }
1917
1924 template<typename TreeType>
1925 struct VoxelizationData {
1926
1927 using Ptr = std::unique_ptr<VoxelizationData>;
1928 using ValueType = typename TreeType::ValueType;
1929
1930 using Int32TreeType = typename TreeType::template ValueConverter<Int32>::Type;
1931 using UCharTreeType = typename TreeType::template ValueConverter<unsigned char>::Type;
1932
1933 using FloatTreeAcc = tree::ValueAccessor<TreeType>;
1934 using Int32TreeAcc = tree::ValueAccessor<Int32TreeType>;
1935 using UCharTreeAcc = tree::ValueAccessor<UCharTreeType>;
1936
1937
1938 VoxelizationData()
1939 : distTree(std::numeric_limits<ValueType>::max())
1940 , distAcc(distTree)
1941 , indexTree(Int32(util::INVALID_IDX))
1942 , indexAcc(indexTree)
1943 , primIdTree(MaxPrimId)
1944 , primIdAcc(primIdTree)
1945 , mPrimCount(0)
1946 {
1947 }
1948
1949 TreeType distTree;
1950 FloatTreeAcc distAcc;
1951
1952 Int32TreeType indexTree;
1953 Int32TreeAcc indexAcc;
1954
1955 UCharTreeType primIdTree;
1956 UCharTreeAcc primIdAcc;
1957
1958 unsigned char getNewPrimId() {
1959
1960 if (mPrimCount == MaxPrimId || primIdTree.leafCount() > 1000) {
1961 mPrimCount = 0;
1962 primIdTree.clear();
1963 }
1964
1965 return mPrimCount++;
1966 }
1967
1968 private:
1969
1970 enum { MaxPrimId = 100 };
1971
1972 unsigned char mPrimCount;
1973 };
1974
1975
1976 template<typename TreeType, typename MeshDataAdapter, typename Interrupter = util::NullInterrupter>
1977 class VoxelizePolygons
1978 {
1979 public:
1980
1981 using VoxelizationDataType = VoxelizationData<TreeType>;
1982 using DataTable = tbb::enumerable_thread_specific<typename VoxelizationDataType::Ptr>;
1983
1984 VoxelizePolygons(DataTable& dataTable,
1985 const MeshDataAdapter& mesh,
1986 Interrupter* interrupter = nullptr)
1987 : mDataTable(&dataTable)
1988 , mMesh(&mesh)
1989 , mInterrupter(interrupter)
1990 {
1991 }
1992
1993 void operator()(const tbb::blocked_range<size_t>& range) const {
1994
1995 typename VoxelizationDataType::Ptr& dataPtr = mDataTable->local();
1996 if (!dataPtr) dataPtr.reset(new VoxelizationDataType());
1997
1998 Triangle prim;
1999
2000 for (size_t n = range.begin(), N = range.end(); n < N; ++n) {
2001
2002 if (this->wasInterrupted()) {
2003 tbb::task::self().cancel_group_execution();
2004 break;
2005 }
2006
2007 const size_t numVerts = mMesh->vertexCount(n);
2008
2009 // rasterize triangles and quads.
2010 if (numVerts == 3 || numVerts == 4) {
2011
2012 prim.index = Int32(n);
2013
2014 mMesh->getIndexSpacePoint(n, 0, prim.a);
2015 mMesh->getIndexSpacePoint(n, 1, prim.b);
2016 mMesh->getIndexSpacePoint(n, 2, prim.c);
2017
2018 evalTriangle(prim, *dataPtr);
2019
2020 if (numVerts == 4) {
2021 mMesh->getIndexSpacePoint(n, 3, prim.b);
2022 evalTriangle(prim, *dataPtr);
2023 }
2024 }
2025 }
2026 }
2027
2028 private:
2029
2030 bool wasInterrupted() const { return mInterrupter && mInterrupter->wasInterrupted(); }
2031
2032 struct Triangle { Vec3d a, b, c; Int32 index; };
2033
2034 struct SubTask
2035 {
2036 enum { POLYGON_LIMIT = 1000 };
2037
2038 SubTask(const Triangle& prim, DataTable& dataTable,
2039 int subdivisionCount, size_t polygonCount)
2040 : mLocalDataTable(&dataTable)
2041 , mPrim(prim)
2042 , mSubdivisionCount(subdivisionCount)
2043 , mPolygonCount(polygonCount)
2044 {
2045 }
2046
2047 void operator()() const
2048 {
2049 if (mSubdivisionCount <= 0 || mPolygonCount >= POLYGON_LIMIT) {
2050
2051 typename VoxelizationDataType::Ptr& dataPtr = mLocalDataTable->local();
2052 if (!dataPtr) dataPtr.reset(new VoxelizationDataType());
2053
2054 voxelizeTriangle(mPrim, *dataPtr);
2055
2056 } else {
2057 spawnTasks(mPrim, *mLocalDataTable, mSubdivisionCount, mPolygonCount);
2058 }
2059 }
2060
2061 DataTable * const mLocalDataTable;
2062 Triangle const mPrim;
2063 int const mSubdivisionCount;
2064 size_t const mPolygonCount;
2065 }; // struct SubTask
2066
2067 inline static int evalSubdivisionCount(const Triangle& prim)
2068 {
2069 const double ax = prim.a[0], bx = prim.b[0], cx = prim.c[0];
2070 const double dx = std::max(ax, std::max(bx, cx)) - std::min(ax, std::min(bx, cx));
2071
2072 const double ay = prim.a[1], by = prim.b[1], cy = prim.c[1];
2073 const double dy = std::max(ay, std::max(by, cy)) - std::min(ay, std::min(by, cy));
2074
2075 const double az = prim.a[2], bz = prim.b[2], cz = prim.c[2];
2076 const double dz = std::max(az, std::max(bz, cz)) - std::min(az, std::min(bz, cz));
2077
2078 return int(std::max(dx, std::max(dy, dz)) / double(TreeType::LeafNodeType::DIM * 2));
2079 }
2080
2081 void evalTriangle(const Triangle& prim, VoxelizationDataType& data) const
2082 {
2083 const size_t polygonCount = mMesh->polygonCount();
2084 const int subdivisionCount =
2085 polygonCount < SubTask::POLYGON_LIMIT ? evalSubdivisionCount(prim) : 0;
2086
2087 if (subdivisionCount <= 0) {
2088 voxelizeTriangle(prim, data);
2089 } else {
2090 spawnTasks(prim, *mDataTable, subdivisionCount, polygonCount);
2091 }
2092 }
2093
2094 static void spawnTasks(
2095 const Triangle& mainPrim, DataTable& dataTable, int subdivisionCount, size_t polygonCount)
2096 {
2097 subdivisionCount -= 1;
2098 polygonCount *= 4;
2099
2100 tbb::task_group tasks;
2101
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;
2105
2106 Triangle prim;
2107 prim.index = mainPrim.index;
2108
2109 prim.a = mainPrim.a;
2110 prim.b = ab;
2111 prim.c = ac;
2112 tasks.run(SubTask(prim, dataTable, subdivisionCount, polygonCount));
2113
2114 prim.a = ab;
2115 prim.b = bc;
2116 prim.c = ac;
2117 tasks.run(SubTask(prim, dataTable, subdivisionCount, polygonCount));
2118
2119 prim.a = ab;
2120 prim.b = mainPrim.b;
2121 prim.c = bc;
2122 tasks.run(SubTask(prim, dataTable, subdivisionCount, polygonCount));
2123
2124 prim.a = ac;
2125 prim.b = bc;
2126 prim.c = mainPrim.c;
2127 tasks.run(SubTask(prim, dataTable, subdivisionCount, polygonCount));
2128
2129 tasks.wait();
2130 }
2131
2132 static void voxelizeTriangle(const Triangle& prim, VoxelizationDataType& data)
2133 {
2134 std::deque<Coord> coordList;
2135 Coord ijk, nijk;
2136
2137 ijk = Coord::floor(prim.a);
2138 coordList.push_back(ijk);
2139
2140 computeDistance(ijk, prim, data);
2141
2142 unsigned char primId = data.getNewPrimId();
2143 data.primIdAcc.setValueOnly(ijk, primId);
2144
2145 while (!coordList.empty()) {
2146 ijk = coordList.back();
2147 coordList.pop_back();
2148
2149 for (Int32 i = 0; i < 26; ++i) {
2150 nijk = ijk + util::COORD_OFFSETS[i];
2151 if (primId != data.primIdAcc.getValue(nijk)) {
2152 data.primIdAcc.setValueOnly(nijk, primId);
2153 if(computeDistance(nijk, prim, data)) coordList.push_back(nijk);
2154 }
2155 }
2156 }
2157 }
2158
2159 static bool computeDistance(const Coord& ijk, const Triangle& prim, VoxelizationDataType& data)
2160 {
2161 Vec3d uvw, voxelCenter(ijk[0], ijk[1], ijk[2]);
2162
2163 using ValueType = typename TreeType::ValueType;
2164
2165 const ValueType dist = ValueType((voxelCenter -
2166 closestPointOnTriangleToPoint(prim.a, prim.c, prim.b, voxelCenter, uvw)).lengthSqr());
2167
2168 const ValueType oldDist = data.distAcc.getValue(ijk);
2169
2170 if (dist < oldDist) {
2171 data.distAcc.setValue(ijk, dist);
2172 data.indexAcc.setValue(ijk, prim.index);
2173 } else if (math::isExactlyEqual(dist, oldDist)) {
2174 // makes reduction deterministic when different polygons
2175 // produce the same distance value.
2176 data.indexAcc.setValueOnly(ijk, std::min(prim.index, data.indexAcc.getValue(ijk)));
2177 }
2178
2179 return !(dist > 0.75); // true if the primitive intersects the voxel.
2180 }
2181
2182 DataTable * const mDataTable;
2183 MeshDataAdapter const * const mMesh;
2184 Interrupter * const mInterrupter;
2185 }; // VoxelizePolygons
2186
2187
2189
2190
2191 template<typename TreeType>
2192 struct DiffLeafNodeMask
2193 {
2194 using AccessorType = typename tree::ValueAccessor<TreeType>;
2195 using LeafNodeType = typename TreeType::LeafNodeType;
2196
2197 using BoolTreeType = typename TreeType::template ValueConverter<bool>::Type;
2198 using BoolLeafNodeType = typename BoolTreeType::LeafNodeType;
2199
2200 DiffLeafNodeMask(const TreeType& rhsTree,
2201 std::vector<BoolLeafNodeType*>& lhsNodes)
2202 : mRhsTree(&rhsTree), mLhsNodes(lhsNodes.empty() ? nullptr : &lhsNodes[0])
2203 {
2204 }
2205
2206 void operator()(const tbb::blocked_range<size_t>& range) const {
2207
2208 tree::ValueAccessor<const TreeType> acc(*mRhsTree);
2209
2210 for (size_t n = range.begin(), N = range.end(); n < N; ++n) {
2211
2212 BoolLeafNodeType* lhsNode = mLhsNodes[n];
2213 const LeafNodeType* rhsNode = acc.probeConstLeaf(lhsNode->origin());
2214
2215 if (rhsNode) lhsNode->topologyDifference(*rhsNode, false);
2216 }
2217 }
2218
2219 private:
2220 TreeType const * const mRhsTree;
2221 BoolLeafNodeType ** const mLhsNodes;
2222 };
2223
2224
2225 template<typename LeafNodeTypeA, typename LeafNodeTypeB>
2226 struct UnionValueMasks
2227 {
2228 UnionValueMasks(std::vector<LeafNodeTypeA*>& nodesA, std::vector<LeafNodeTypeB*>& nodesB)
2229 : mNodesA(nodesA.empty() ? nullptr : &nodesA[0])
2230 , mNodesB(nodesB.empty() ? nullptr : &nodesB[0])
2231 {
2232 }
2233
2234 void operator()(const tbb::blocked_range<size_t>& range) const {
2235 for (size_t n = range.begin(), N = range.end(); n < N; ++n) {
2236 mNodesA[n]->topologyUnion(*mNodesB[n]);
2237 }
2238 }
2239
2240 private:
2241 LeafNodeTypeA ** const mNodesA;
2242 LeafNodeTypeB ** const mNodesB;
2243 };
2244
2245
2246 template<typename TreeType>
2247 struct ConstructVoxelMask
2248 {
2249 using LeafNodeType = typename TreeType::LeafNodeType;
2250
2251 using BoolTreeType = typename TreeType::template ValueConverter<bool>::Type;
2252 using BoolLeafNodeType = typename BoolTreeType::LeafNodeType;
2253
2254 ConstructVoxelMask(BoolTreeType& maskTree, const TreeType& tree,
2255 std::vector<LeafNodeType*>& nodes)
2256 : mTree(&tree)
2257 , mNodes(nodes.empty() ? nullptr : &nodes[0])
2258 , mLocalMaskTree(false)
2259 , mMaskTree(&maskTree)
2260 {
2261 }
2262
2263 ConstructVoxelMask(ConstructVoxelMask& rhs, tbb::split)
2264 : mTree(rhs.mTree)
2265 , mNodes(rhs.mNodes)
2266 , mLocalMaskTree(false)
2267 , mMaskTree(&mLocalMaskTree)
2268 {
2269 }
2270
2271 void operator()(const tbb::blocked_range<size_t>& range)
2272 {
2273 using Iterator = typename LeafNodeType::ValueOnCIter;
2274
2275 tree::ValueAccessor<const TreeType> acc(*mTree);
2276 tree::ValueAccessor<BoolTreeType> maskAcc(*mMaskTree);
2277
2278 Coord ijk, nijk, localCorod;
2279 Index pos, npos;
2280
2281 for (size_t n = range.begin(); n != range.end(); ++n) {
2282
2283 LeafNodeType& node = *mNodes[n];
2284
2285 CoordBBox bbox = node.getNodeBoundingBox();
2286 bbox.expand(-1);
2287
2288 BoolLeafNodeType& maskNode = *maskAcc.touchLeaf(node.origin());
2289
2290 for (Iterator it = node.cbeginValueOn(); it; ++it) {
2291 ijk = it.getCoord();
2292 pos = it.pos();
2293
2294 localCorod = LeafNodeType::offsetToLocalCoord(pos);
2295
2296 if (localCorod[2] < int(LeafNodeType::DIM - 1)) {
2297 npos = pos + 1;
2298 if (!node.isValueOn(npos)) maskNode.setValueOn(npos);
2299 } else {
2300 nijk = ijk.offsetBy(0, 0, 1);
2301 if (!acc.isValueOn(nijk)) maskAcc.setValueOn(nijk);
2302 }
2303
2304 if (localCorod[2] > 0) {
2305 npos = pos - 1;
2306 if (!node.isValueOn(npos)) maskNode.setValueOn(npos);
2307 } else {
2308 nijk = ijk.offsetBy(0, 0, -1);
2309 if (!acc.isValueOn(nijk)) maskAcc.setValueOn(nijk);
2310 }
2311
2312 if (localCorod[1] < int(LeafNodeType::DIM - 1)) {
2313 npos = pos + LeafNodeType::DIM;
2314 if (!node.isValueOn(npos)) maskNode.setValueOn(npos);
2315 } else {
2316 nijk = ijk.offsetBy(0, 1, 0);
2317 if (!acc.isValueOn(nijk)) maskAcc.setValueOn(nijk);
2318 }
2319
2320 if (localCorod[1] > 0) {
2321 npos = pos - LeafNodeType::DIM;
2322 if (!node.isValueOn(npos)) maskNode.setValueOn(npos);
2323 } else {
2324 nijk = ijk.offsetBy(0, -1, 0);
2325 if (!acc.isValueOn(nijk)) maskAcc.setValueOn(nijk);
2326 }
2327
2328 if (localCorod[0] < int(LeafNodeType::DIM - 1)) {
2329 npos = pos + LeafNodeType::DIM * LeafNodeType::DIM;
2330 if (!node.isValueOn(npos)) maskNode.setValueOn(npos);
2331 } else {
2332 nijk = ijk.offsetBy(1, 0, 0);
2333 if (!acc.isValueOn(nijk)) maskAcc.setValueOn(nijk);
2334 }
2335
2336 if (localCorod[0] > 0) {
2337 npos = pos - LeafNodeType::DIM * LeafNodeType::DIM;
2338 if (!node.isValueOn(npos)) maskNode.setValueOn(npos);
2339 } else {
2340 nijk = ijk.offsetBy(-1, 0, 0);
2341 if (!acc.isValueOn(nijk)) maskAcc.setValueOn(nijk);
2342 }
2343 }
2344 }
2345 }
2346
2347 void join(ConstructVoxelMask& rhs) { mMaskTree->merge(*rhs.mMaskTree); }
2348
2349 private:
2350 TreeType const * const mTree;
2351 LeafNodeType ** const mNodes;
2352
2353 BoolTreeType mLocalMaskTree;
2354 BoolTreeType * const mMaskTree;
2355 };
2356
2357
2359 template<typename TreeType, typename MeshDataAdapter>
2360 struct ExpandNarrowband
2361 {
2362 using ValueType = typename TreeType::ValueType;
2363 using LeafNodeType = typename TreeType::LeafNodeType;
2364 using NodeMaskType = typename LeafNodeType::NodeMaskType;
2365 using Int32TreeType = typename TreeType::template ValueConverter<Int32>::Type;
2366 using Int32LeafNodeType = typename Int32TreeType::LeafNodeType;
2367 using BoolTreeType = typename TreeType::template ValueConverter<bool>::Type;
2368 using BoolLeafNodeType = typename BoolTreeType::LeafNodeType;
2369
2370 struct Fragment
2371 {
2372 Int32 idx, x, y, z;
2373 ValueType dist;
2374
2375 Fragment() : idx(0), x(0), y(0), z(0), dist(0.0) {}
2376
2377 Fragment(Int32 idx_, Int32 x_, Int32 y_, Int32 z_, ValueType dist_)
2378 : idx(idx_), x(x_), y(y_), z(z_), dist(dist_)
2379 {
2380 }
2381
2382 bool operator<(const Fragment& rhs) const { return idx < rhs.idx; }
2383 }; // struct Fragment
2384
2386
2387 ExpandNarrowband(
2388 std::vector<BoolLeafNodeType*>& maskNodes,
2389 BoolTreeType& maskTree,
2390 TreeType& distTree,
2391 Int32TreeType& indexTree,
2392 const MeshDataAdapter& mesh,
2393 ValueType exteriorBandWidth,
2394 ValueType interiorBandWidth,
2395 ValueType voxelSize)
2396 : mMaskNodes(maskNodes.empty() ? nullptr : &maskNodes[0])
2397 , mMaskTree(&maskTree)
2398 , mDistTree(&distTree)
2399 , mIndexTree(&indexTree)
2400 , mMesh(&mesh)
2401 , mNewMaskTree(false)
2402 , mDistNodes()
2403 , mUpdatedDistNodes()
2404 , mIndexNodes()
2405 , mUpdatedIndexNodes()
2406 , mExteriorBandWidth(exteriorBandWidth)
2407 , mInteriorBandWidth(interiorBandWidth)
2408 , mVoxelSize(voxelSize)
2409 {
2410 }
2411
2412 ExpandNarrowband(const ExpandNarrowband& rhs, tbb::split)
2413 : mMaskNodes(rhs.mMaskNodes)
2414 , mMaskTree(rhs.mMaskTree)
2415 , mDistTree(rhs.mDistTree)
2416 , mIndexTree(rhs.mIndexTree)
2417 , mMesh(rhs.mMesh)
2418 , mNewMaskTree(false)
2419 , mDistNodes()
2420 , mUpdatedDistNodes()
2421 , mIndexNodes()
2422 , mUpdatedIndexNodes()
2423 , mExteriorBandWidth(rhs.mExteriorBandWidth)
2424 , mInteriorBandWidth(rhs.mInteriorBandWidth)
2425 , mVoxelSize(rhs.mVoxelSize)
2426 {
2427 }
2428
2429 void join(ExpandNarrowband& rhs)
2430 {
2431 mDistNodes.insert(mDistNodes.end(), rhs.mDistNodes.begin(), rhs.mDistNodes.end());
2432 mIndexNodes.insert(mIndexNodes.end(), rhs.mIndexNodes.begin(), rhs.mIndexNodes.end());
2433
2434 mUpdatedDistNodes.insert(mUpdatedDistNodes.end(),
2435 rhs.mUpdatedDistNodes.begin(), rhs.mUpdatedDistNodes.end());
2436
2437 mUpdatedIndexNodes.insert(mUpdatedIndexNodes.end(),
2438 rhs.mUpdatedIndexNodes.begin(), rhs.mUpdatedIndexNodes.end());
2439
2440 mNewMaskTree.merge(rhs.mNewMaskTree);
2441 }
2442
2443 void operator()(const tbb::blocked_range<size_t>& range)
2444 {
2445 tree::ValueAccessor<BoolTreeType> newMaskAcc(mNewMaskTree);
2446 tree::ValueAccessor<TreeType> distAcc(*mDistTree);
2447 tree::ValueAccessor<Int32TreeType> indexAcc(*mIndexTree);
2448
2449 std::vector<Fragment> fragments;
2450 fragments.reserve(256);
2451
2452 std::unique_ptr<LeafNodeType> newDistNodePt;
2453 std::unique_ptr<Int32LeafNodeType> newIndexNodePt;
2454
2455 for (size_t n = range.begin(), N = range.end(); n < N; ++n) {
2456
2457 BoolLeafNodeType& maskNode = *mMaskNodes[n];
2458 if (maskNode.isEmpty()) continue;
2459
2460 // Setup local caches
2461
2462 const Coord& origin = maskNode.origin();
2463
2464 LeafNodeType * distNodePt = distAcc.probeLeaf(origin);
2465 Int32LeafNodeType * indexNodePt = indexAcc.probeLeaf(origin);
2466
2467 assert(!distNodePt == !indexNodePt);
2468
2469 bool usingNewNodes = false;
2470
2471 if (!distNodePt && !indexNodePt) {
2472
2473 const ValueType backgroundDist = distAcc.getValue(origin);
2474
2475 if (!newDistNodePt.get() && !newIndexNodePt.get()) {
2476 newDistNodePt.reset(new LeafNodeType(origin, backgroundDist));
2477 newIndexNodePt.reset(new Int32LeafNodeType(origin, indexAcc.getValue(origin)));
2478 } else {
2479
2480 if ((backgroundDist < ValueType(0.0)) !=
2481 (newDistNodePt->getValue(0) < ValueType(0.0))) {
2482 newDistNodePt->buffer().fill(backgroundDist);
2483 }
2484
2485 newDistNodePt->setOrigin(origin);
2486 newIndexNodePt->setOrigin(origin);
2487 }
2488
2489 distNodePt = newDistNodePt.get();
2490 indexNodePt = newIndexNodePt.get();
2491
2492 usingNewNodes = true;
2493 }
2494
2495
2496 // Gather neighbour information
2497
2498 CoordBBox bbox(Coord::max(), Coord::min());
2499 for (typename BoolLeafNodeType::ValueOnIter it = maskNode.beginValueOn(); it; ++it) {
2500 bbox.expand(it.getCoord());
2501 }
2502
2503 bbox.expand(1);
2504
2505 gatherFragments(fragments, bbox, distAcc, indexAcc);
2506
2507
2508 // Compute first voxel layer
2509
2510 bbox = maskNode.getNodeBoundingBox();
2511 NodeMaskType mask;
2512 bool updatedLeafNodes = false;
2513
2514 for (typename BoolLeafNodeType::ValueOnIter it = maskNode.beginValueOn(); it; ++it) {
2515
2516 const Coord ijk = it.getCoord();
2517
2518 if (updateVoxel(ijk, 5, fragments, *distNodePt, *indexNodePt, &updatedLeafNodes)) {
2519
2520 for (Int32 i = 0; i < 6; ++i) {
2521 const Coord nijk = ijk + util::COORD_OFFSETS[i];
2522 if (bbox.isInside(nijk)) {
2523 mask.setOn(BoolLeafNodeType::coordToOffset(nijk));
2524 } else {
2525 newMaskAcc.setValueOn(nijk);
2526 }
2527 }
2528
2529 for (Int32 i = 6; i < 26; ++i) {
2530 const Coord nijk = ijk + util::COORD_OFFSETS[i];
2531 if (bbox.isInside(nijk)) {
2532 mask.setOn(BoolLeafNodeType::coordToOffset(nijk));
2533 }
2534 }
2535 }
2536 }
2537
2538 if (updatedLeafNodes) {
2539
2540 // Compute second voxel layer
2541 mask -= indexNodePt->getValueMask();
2542
2543 for (typename NodeMaskType::OnIterator it = mask.beginOn(); it; ++it) {
2544
2545 const Index pos = it.pos();
2546 const Coord ijk = maskNode.origin() + LeafNodeType::offsetToLocalCoord(pos);
2547
2548 if (updateVoxel(ijk, 6, fragments, *distNodePt, *indexNodePt)) {
2549 for (Int32 i = 0; i < 6; ++i) {
2550 newMaskAcc.setValueOn(ijk + util::COORD_OFFSETS[i]);
2551 }
2552 }
2553 }
2554
2555 // Export new distance values
2556 if (usingNewNodes) {
2557 newDistNodePt->topologyUnion(*newIndexNodePt);
2558 mDistNodes.push_back(newDistNodePt.release());
2559 mIndexNodes.push_back(newIndexNodePt.release());
2560 } else {
2561 mUpdatedDistNodes.push_back(distNodePt);
2562 mUpdatedIndexNodes.push_back(indexNodePt);
2563 }
2564 }
2565 } // end leafnode loop
2566 }
2567
2569
2570 BoolTreeType& newMaskTree() { return mNewMaskTree; }
2571
2572 std::vector<LeafNodeType*>& newDistNodes() { return mDistNodes; }
2573 std::vector<LeafNodeType*>& updatedDistNodes() { return mUpdatedDistNodes; }
2574
2575 std::vector<Int32LeafNodeType*>& newIndexNodes() { return mIndexNodes; }
2576 std::vector<Int32LeafNodeType*>& updatedIndexNodes() { return mUpdatedIndexNodes; }
2577
2578 private:
2579
2581 void
2582 gatherFragments(std::vector<Fragment>& fragments, const CoordBBox& bbox,
2583 tree::ValueAccessor<TreeType>& distAcc, tree::ValueAccessor<Int32TreeType>& indexAcc)
2584 {
2585 fragments.clear();
2586 const Coord nodeMin = bbox.min() & ~(LeafNodeType::DIM - 1);
2587 const Coord nodeMax = bbox.max() & ~(LeafNodeType::DIM - 1);
2588
2589 CoordBBox region;
2590 Coord ijk;
2591
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) {
2595 if (LeafNodeType* distleaf = distAcc.probeLeaf(ijk)) {
2596 region.min() = Coord::maxComponent(bbox.min(), ijk);
2597 region.max() = Coord::minComponent(bbox.max(),
2598 ijk.offsetBy(LeafNodeType::DIM - 1));
2599 gatherFragments(fragments, region, *distleaf, *indexAcc.probeLeaf(ijk));
2600 }
2601 }
2602 }
2603 }
2604
2605 std::sort(fragments.begin(), fragments.end());
2606 }
2607
2608 void
2609 gatherFragments(std::vector<Fragment>& fragments, const CoordBBox& bbox,
2610 const LeafNodeType& distLeaf, const Int32LeafNodeType& idxLeaf) const
2611 {
2612 const typename LeafNodeType::NodeMaskType& mask = distLeaf.getValueMask();
2613 const ValueType* distData = distLeaf.buffer().data();
2614 const Int32* idxData = idxLeaf.buffer().data();
2615
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])));
2624 }
2625 }
2626 }
2627 }
2628 }
2629
2632 ValueType
2633 computeDistance(const Coord& ijk, const Int32 manhattanLimit,
2634 const std::vector<Fragment>& fragments, Int32& closestPrimIdx) const
2635 {
2636 Vec3d a, b, c, uvw, voxelCenter(ijk[0], ijk[1], ijk[2]);
2637 double primDist, tmpDist, dist = std::numeric_limits<double>::max();
2638 Int32 lastIdx = Int32(util::INVALID_IDX);
2639
2640 for (size_t n = 0, N = fragments.size(); n < N; ++n) {
2641
2642 const Fragment& fragment = fragments[n];
2643 if (lastIdx == fragment.idx) continue;
2644
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]);
2648
2649 const Int32 manhattan = dx + dy + dz;
2650 if (manhattan > manhattanLimit) continue;
2651
2652 lastIdx = fragment.idx;
2653
2654 const size_t polygon = size_t(lastIdx);
2655
2656 mMesh->getIndexSpacePoint(polygon, 0, a);
2657 mMesh->getIndexSpacePoint(polygon, 1, b);
2658 mMesh->getIndexSpacePoint(polygon, 2, c);
2659
2660 primDist = (voxelCenter -
2661 closestPointOnTriangleToPoint(a, c, b, voxelCenter, uvw)).lengthSqr();
2662
2663 // Split quad into a second triangle
2664 if (4 == mMesh->vertexCount(polygon)) {
2665
2666 mMesh->getIndexSpacePoint(polygon, 3, b);
2667
2668 tmpDist = (voxelCenter - closestPointOnTriangleToPoint(
2669 a, b, c, voxelCenter, uvw)).lengthSqr();
2670
2671 if (tmpDist < primDist) primDist = tmpDist;
2672 }
2673
2674 if (primDist < dist) {
2675 dist = primDist;
2676 closestPrimIdx = lastIdx;
2677 }
2678 }
2679
2680 return ValueType(std::sqrt(dist)) * mVoxelSize;
2681 }
2682
2685 bool
2686 updateVoxel(const Coord& ijk, const Int32 manhattanLimit,
2687 const std::vector<Fragment>& fragments,
2688 LeafNodeType& distLeaf, Int32LeafNodeType& idxLeaf, bool* updatedLeafNodes = nullptr)
2689 {
2690 Int32 closestPrimIdx = 0;
2691 const ValueType distance = computeDistance(ijk, manhattanLimit, fragments, closestPrimIdx);
2692
2693 const Index pos = LeafNodeType::coordToOffset(ijk);
2694 const bool inside = distLeaf.getValue(pos) < ValueType(0.0);
2695
2696 bool activateNeighbourVoxels = false;
2697
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);
2708 }
2709
2710 return activateNeighbourVoxels;
2711 }
2712
2714
2715 BoolLeafNodeType ** const mMaskNodes;
2716 BoolTreeType * const mMaskTree;
2717 TreeType * const mDistTree;
2718 Int32TreeType * const mIndexTree;
2719
2720 MeshDataAdapter const * const mMesh;
2721
2722 BoolTreeType mNewMaskTree;
2723
2724 std::vector<LeafNodeType*> mDistNodes, mUpdatedDistNodes;
2725 std::vector<Int32LeafNodeType*> mIndexNodes, mUpdatedIndexNodes;
2726
2727 const ValueType mExteriorBandWidth, mInteriorBandWidth, mVoxelSize;
2728 }; // struct ExpandNarrowband
2729
2730
2731 template<typename TreeType>
2732 struct AddNodes {
2733 using LeafNodeType = typename TreeType::LeafNodeType;
2734
2735 AddNodes(TreeType& tree, std::vector<LeafNodeType*>& nodes)
2736 : mTree(&tree) , mNodes(&nodes)
2737 {
2738 }
2739
2740 void operator()() const {
2741 tree::ValueAccessor<TreeType> acc(*mTree);
2742 std::vector<LeafNodeType*>& nodes = *mNodes;
2743 for (size_t n = 0, N = nodes.size(); n < N; ++n) {
2744 acc.addLeaf(nodes[n]);
2745 }
2746 }
2747
2748 TreeType * const mTree;
2749 std::vector<LeafNodeType*> * const mNodes;
2750 }; // AddNodes
2751
2752
2753 template<typename TreeType, typename Int32TreeType, typename BoolTreeType, typename MeshDataAdapter>
2754 inline void
2755 expandNarrowband(
2756 TreeType& distTree,
2757 Int32TreeType& indexTree,
2758 BoolTreeType& maskTree,
2759 std::vector<typename BoolTreeType::LeafNodeType*>& maskNodes,
2760 const MeshDataAdapter& mesh,
2761 typename TreeType::ValueType exteriorBandWidth,
2762 typename TreeType::ValueType interiorBandWidth,
2763 typename TreeType::ValueType voxelSize)
2764 {
2765 ExpandNarrowband<TreeType, MeshDataAdapter> expandOp(maskNodes, maskTree,
2766 distTree, indexTree, mesh, exteriorBandWidth, interiorBandWidth, voxelSize);
2767
2768 tbb::parallel_reduce(tbb::blocked_range<size_t>(0, maskNodes.size()), expandOp);
2769
2770 tbb::parallel_for(tbb::blocked_range<size_t>(0, expandOp.updatedIndexNodes().size()),
2771 UnionValueMasks<typename TreeType::LeafNodeType, typename Int32TreeType::LeafNodeType>(
2772 expandOp.updatedDistNodes(), expandOp.updatedIndexNodes()));
2773
2774 tbb::task_group tasks;
2775 tasks.run(AddNodes<TreeType>(distTree, expandOp.newDistNodes()));
2776 tasks.run(AddNodes<Int32TreeType>(indexTree, expandOp.newIndexNodes()));
2777 tasks.wait();
2778
2779 maskTree.clear();
2780 maskTree.merge(expandOp.newMaskTree());
2781 }
2782
2783
2785
2786
2787 // Transform values (sqrt, world space scaling and sign flip if sdf)
2788 template<typename TreeType>
2789 struct TransformValues
2790 {
2791 using LeafNodeType = typename TreeType::LeafNodeType;
2792 using ValueType = typename TreeType::ValueType;
2793
2794 TransformValues(std::vector<LeafNodeType*>& nodes,
2795 ValueType voxelSize, bool unsignedDist)
2796 : mNodes(&nodes[0])
2797 , mVoxelSize(voxelSize)
2798 , mUnsigned(unsignedDist)
2799 {
2800 }
2801
2802 void operator()(const tbb::blocked_range<size_t>& range) const {
2803
2804 typename LeafNodeType::ValueOnIter iter;
2805
2806 const bool udf = mUnsigned;
2807 const ValueType w[2] = { -mVoxelSize, mVoxelSize };
2808
2809 for (size_t n = range.begin(), N = range.end(); n < N; ++n) {
2810
2811 for (iter = mNodes[n]->beginValueOn(); iter; ++iter) {
2812 ValueType& val = const_cast<ValueType&>(iter.getValue());
2813 val = w[udf || (val < ValueType(0.0))] * std::sqrt(std::abs(val));
2814 }
2815 }
2816 }
2817
2818 private:
2819 LeafNodeType * * const mNodes;
2820 const ValueType mVoxelSize;
2821 const bool mUnsigned;
2822 };
2823
2824
2825 // Inactivate values outside the (exBandWidth, inBandWidth) range.
2826 template<typename TreeType>
2827 struct InactivateValues
2828 {
2829 using LeafNodeType = typename TreeType::LeafNodeType;
2830 using ValueType = typename TreeType::ValueType;
2831
2832 InactivateValues(std::vector<LeafNodeType*>& nodes,
2833 ValueType exBandWidth, ValueType inBandWidth)
2834 : mNodes(nodes.empty() ? nullptr : &nodes[0])
2835 , mExBandWidth(exBandWidth)
2836 , mInBandWidth(inBandWidth)
2837 {
2838 }
2839
2840 void operator()(const tbb::blocked_range<size_t>& range) const {
2841
2842 typename LeafNodeType::ValueOnIter iter;
2843 const ValueType exVal = mExBandWidth;
2844 const ValueType inVal = -mInBandWidth;
2845
2846 for (size_t n = range.begin(), N = range.end(); n < N; ++n) {
2847
2848 for (iter = mNodes[n]->beginValueOn(); iter; ++iter) {
2849
2850 ValueType& val = const_cast<ValueType&>(iter.getValue());
2851
2852 const bool inside = val < ValueType(0.0);
2853
2854 if (inside && !(val > inVal)) {
2855 val = inVal;
2856 iter.setValueOff();
2857 } else if (!inside && !(val < exVal)) {
2858 val = exVal;
2859 iter.setValueOff();
2860 }
2861 }
2862 }
2863 }
2864
2865 private:
2866 LeafNodeType * * const mNodes;
2867 const ValueType mExBandWidth, mInBandWidth;
2868 };
2869
2870
2871 template<typename TreeType>
2872 struct OffsetValues
2873 {
2874 using LeafNodeType = typename TreeType::LeafNodeType;
2875 using ValueType = typename TreeType::ValueType;
2876
2877 OffsetValues(std::vector<LeafNodeType*>& nodes, ValueType offset)
2878 : mNodes(nodes.empty() ? nullptr : &nodes[0]), mOffset(offset)
2879 {
2880 }
2881
2882 void operator()(const tbb::blocked_range<size_t>& range) const {
2883
2884 const ValueType offset = mOffset;
2885
2886 for (size_t n = range.begin(), N = range.end(); n < N; ++n) {
2887
2888 typename LeafNodeType::ValueOnIter iter = mNodes[n]->beginValueOn();
2889
2890 for (; iter; ++iter) {
2891 ValueType& val = const_cast<ValueType&>(iter.getValue());
2892 val += offset;
2893 }
2894 }
2895 }
2896
2897 private:
2898 LeafNodeType * * const mNodes;
2899 const ValueType mOffset;
2900 };
2901
2902
2903 template<typename TreeType>
2904 struct Renormalize
2905 {
2906 using LeafNodeType = typename TreeType::LeafNodeType;
2907 using ValueType = typename TreeType::ValueType;
2908
2909 Renormalize(const TreeType& tree, const std::vector<LeafNodeType*>& nodes,
2910 ValueType* buffer, ValueType voxelSize)
2911 : mTree(&tree)
2912 , mNodes(nodes.empty() ? nullptr : &nodes[0])
2913 , mBuffer(buffer)
2914 , mVoxelSize(voxelSize)
2915 {
2916 }
2917
2918 void operator()(const tbb::blocked_range<size_t>& range) const
2919 {
2920 using Vec3Type = math::Vec3<ValueType>;
2921
2922 tree::ValueAccessor<const TreeType> acc(*mTree);
2923
2924 Coord ijk;
2925 Vec3Type up, down;
2926
2927 const ValueType dx = mVoxelSize, invDx = ValueType(1.0) / mVoxelSize;
2928
2929 for (size_t n = range.begin(), N = range.end(); n < N; ++n) {
2930
2931 ValueType* bufferData = &mBuffer[n * LeafNodeType::SIZE];
2932
2933 typename LeafNodeType::ValueOnCIter iter = mNodes[n]->cbeginValueOn();
2934 for (; iter; ++iter) {
2935
2936 const ValueType phi0 = *iter;
2937
2938 ijk = iter.getCoord();
2939
2940 up[0] = acc.getValue(ijk.offsetBy(1, 0, 0)) - phi0;
2941 up[1] = acc.getValue(ijk.offsetBy(0, 1, 0)) - phi0;
2942 up[2] = acc.getValue(ijk.offsetBy(0, 0, 1)) - phi0;
2943
2944 down[0] = phi0 - acc.getValue(ijk.offsetBy(-1, 0, 0));
2945 down[1] = phi0 - acc.getValue(ijk.offsetBy(0, -1, 0));
2946 down[2] = phi0 - acc.getValue(ijk.offsetBy(0, 0, -1));
2947
2948 const ValueType normSqGradPhi = math::GodunovsNormSqrd(phi0 > 0.0, down, up);
2949
2950 const ValueType diff = math::Sqrt(normSqGradPhi) * invDx - ValueType(1.0);
2951 const ValueType S = phi0 / (math::Sqrt(math::Pow2(phi0) + normSqGradPhi));
2952
2953 bufferData[iter.pos()] = phi0 - dx * S * diff;
2954 }
2955 }
2956 }
2957
2958 private:
2959 TreeType const * const mTree;
2960 LeafNodeType const * const * const mNodes;
2961 ValueType * const mBuffer;
2962
2963 const ValueType mVoxelSize;
2964 };
2965
2966
2967 template<typename TreeType>
2968 struct MinCombine
2969 {
2970 using LeafNodeType = typename TreeType::LeafNodeType;
2971 using ValueType = typename TreeType::ValueType;
2972
2973 MinCombine(std::vector<LeafNodeType*>& nodes, const ValueType* buffer)
2974 : mNodes(nodes.empty() ? nullptr : &nodes[0]), mBuffer(buffer)
2975 {
2976 }
2977
2978 void operator()(const tbb::blocked_range<size_t>& range) const {
2979
2980 for (size_t n = range.begin(), N = range.end(); n < N; ++n) {
2981
2982 const ValueType* bufferData = &mBuffer[n * LeafNodeType::SIZE];
2983
2984 typename LeafNodeType::ValueOnIter iter = mNodes[n]->beginValueOn();
2985
2986 for (; iter; ++iter) {
2987 ValueType& val = const_cast<ValueType&>(iter.getValue());
2988 val = std::min(val, bufferData[iter.pos()]);
2989 }
2990 }
2991 }
2992
2993 private:
2994 LeafNodeType * * const mNodes;
2995 ValueType const * const mBuffer;
2996 };
2997
2998
2999 } // mesh_to_volume_internal namespace
3000
3001
3003
3004 // Utility method implementation
3005
3006
3007 template <typename FloatTreeT>
3008 inline void
3009 traceExteriorBoundaries(FloatTreeT& tree)
3010 {
3011 using ConnectivityTable = mesh_to_volume_internal::LeafNodeConnectivityTable<FloatTreeT>;
3012
3013 ConnectivityTable nodeConnectivity(tree);
3014
3015 std::vector<size_t> zStartNodes, yStartNodes, xStartNodes;
3016
3017 for (size_t n = 0; n < nodeConnectivity.size(); ++n) {
3018 if (ConnectivityTable::INVALID_OFFSET == nodeConnectivity.offsetsPrevX()[n]) {
3019 xStartNodes.push_back(n);
3020 }
3021
3022 if (ConnectivityTable::INVALID_OFFSET == nodeConnectivity.offsetsPrevY()[n]) {
3023 yStartNodes.push_back(n);
3024 }
3025
3026 if (ConnectivityTable::INVALID_OFFSET == nodeConnectivity.offsetsPrevZ()[n]) {
3027 zStartNodes.push_back(n);
3028 }
3029 }
3030
3031 using SweepingOp = mesh_to_volume_internal::SweepExteriorSign<FloatTreeT>;
3032
3033 tbb::parallel_for(tbb::blocked_range<size_t>(0, zStartNodes.size()),
3034 SweepingOp(SweepingOp::Z_AXIS, zStartNodes, nodeConnectivity));
3035
3036 tbb::parallel_for(tbb::blocked_range<size_t>(0, yStartNodes.size()),
3037 SweepingOp(SweepingOp::Y_AXIS, yStartNodes, nodeConnectivity));
3038
3039 tbb::parallel_for(tbb::blocked_range<size_t>(0, xStartNodes.size()),
3040 SweepingOp(SweepingOp::X_AXIS, xStartNodes, nodeConnectivity));
3041
3042 const size_t numLeafNodes = nodeConnectivity.size();
3043 const size_t numVoxels = numLeafNodes * FloatTreeT::LeafNodeType::SIZE;
3044
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]};
3048
3049 mesh_to_volume_internal::fillArray(changedNodeMaskA.get(), true, numLeafNodes);
3050 mesh_to_volume_internal::fillArray(changedNodeMaskB.get(), false, numLeafNodes);
3051 mesh_to_volume_internal::fillArray(changedVoxelMask.get(), false, numVoxels);
3052
3053 const tbb::blocked_range<size_t> nodeRange(0, numLeafNodes);
3054
3055 bool nodesUpdated = false;
3056 do {
3057 tbb::parallel_for(nodeRange, mesh_to_volume_internal::SeedFillExteriorSign<FloatTreeT>(
3058 nodeConnectivity.nodes(), changedNodeMaskA.get()));
3059
3060 tbb::parallel_for(nodeRange, mesh_to_volume_internal::SeedPoints<FloatTreeT>(
3061 nodeConnectivity, changedNodeMaskA.get(), changedNodeMaskB.get(),
3062 changedVoxelMask.get()));
3063
3064 changedNodeMaskA.swap(changedNodeMaskB);
3065
3066 nodesUpdated = false;
3067 for (size_t n = 0; n < numLeafNodes; ++n) {
3068 nodesUpdated |= changedNodeMaskA[n];
3069 if (nodesUpdated) break;
3070 }
3071
3072 if (nodesUpdated) {
3073 tbb::parallel_for(nodeRange, mesh_to_volume_internal::SyncVoxelMask<FloatTreeT>(
3074 nodeConnectivity.nodes(), changedNodeMaskA.get(), changedVoxelMask.get()));
3075 }
3076 } while (nodesUpdated);
3077
3078 } // void traceExteriorBoundaries()
3079
3080
3082
3083
3084 template <typename GridType, typename MeshDataAdapter, typename Interrupter>
3085 inline typename GridType::Ptr
3086 meshToVolume(
3087 Interrupter& interrupter,
3088 const MeshDataAdapter& mesh,
3089 const math::Transform& transform,
3090 float exteriorBandWidth,
3091 float interiorBandWidth,
3092 int flags,
3093 typename GridType::template ValueConverter<Int32>::Type * polygonIndexGrid)
3094 {
3095 using GridTypePtr = typename GridType::Ptr;
3096 using TreeType = typename GridType::TreeType;
3097 using LeafNodeType = typename TreeType::LeafNodeType;
3098 using ValueType = typename GridType::ValueType;
3099
3100 using Int32GridType = typename GridType::template ValueConverter<Int32>::Type;
3101 using Int32TreeType = typename Int32GridType::TreeType;
3102
3103 using BoolTreeType = typename TreeType::template ValueConverter<bool>::Type;
3104
3106
3107 // Setup
3108
3109 GridTypePtr distGrid(new GridType(std::numeric_limits<ValueType>::max()));
3110 distGrid->setTransform(transform.copy());
3111
3112 ValueType exteriorWidth = ValueType(exteriorBandWidth);
3113 ValueType interiorWidth = ValueType(interiorBandWidth);
3114
3115 // Note: inf interior width is all right, this value makes the converter fill
3116 // interior regions with distance values.
3117 if (!std::isfinite(exteriorWidth) || std::isnan(interiorWidth)) {
3118 std::stringstream msg;
3119 msg << "Illegal narrow band width: exterior = " << exteriorWidth
3120 << ", interior = " << interiorWidth;
3121 OPENVDB_LOG_DEBUG(msg.str());
3122 return distGrid;
3123 }
3124
3125 const ValueType voxelSize = ValueType(transform.voxelSize()[0]);
3126
3127 if (!std::isfinite(voxelSize) || math::isZero(voxelSize)) {
3128 std::stringstream msg;
3129 msg << "Illegal transform, voxel size = " << voxelSize;
3130 OPENVDB_LOG_DEBUG(msg.str());
3131 return distGrid;
3132 }
3133
3134 // Convert narrow band width from voxel units to world space units.
3135 exteriorWidth *= voxelSize;
3136 // Avoid the unit conversion if the interior band width is set to
3137 // inf or std::numeric_limits<float>::max().
3138 if (interiorWidth < std::numeric_limits<ValueType>::max()) {
3139 interiorWidth *= voxelSize;
3140 }
3141
3142 const bool computeSignedDistanceField = (flags & UNSIGNED_DISTANCE_FIELD) == 0;
3143 const bool removeIntersectingVoxels = (flags & DISABLE_INTERSECTING_VOXEL_REMOVAL) == 0;
3144 const bool renormalizeValues = (flags & DISABLE_RENORMALIZATION) == 0;
3145 const bool trimNarrowBand = (flags & DISABLE_NARROW_BAND_TRIMMING) == 0;
3146
3147 Int32GridType* indexGrid = nullptr;
3148
3149 typename Int32GridType::Ptr temporaryIndexGrid;
3150
3151 if (polygonIndexGrid) {
3152 indexGrid = polygonIndexGrid;
3153 } else {
3154 temporaryIndexGrid.reset(new Int32GridType(Int32(util::INVALID_IDX)));
3155 indexGrid = temporaryIndexGrid.get();
3156 }
3157
3158 indexGrid->newTree();
3159 indexGrid->setTransform(transform.copy());
3160
3161 if (computeSignedDistanceField) {
3162 distGrid->setGridClass(GRID_LEVEL_SET);
3163 } else {
3164 distGrid->setGridClass(GRID_UNKNOWN);
3165 interiorWidth = ValueType(0.0);
3166 }
3167
3168 TreeType& distTree = distGrid->tree();
3169 Int32TreeType& indexTree = indexGrid->tree();
3170
3171
3173
3174 // Voxelize mesh
3175
3176 {
3177 using VoxelizationDataType = mesh_to_volume_internal::VoxelizationData<TreeType>;
3178 using DataTable = tbb::enumerable_thread_specific<typename VoxelizationDataType::Ptr>;
3179
3180 DataTable data;
3181 using Voxelizer =
3182 mesh_to_volume_internal::VoxelizePolygons<TreeType, MeshDataAdapter, Interrupter>;
3183
3184 const tbb::blocked_range<size_t> polygonRange(0, mesh.polygonCount());
3185
3186 tbb::parallel_for(polygonRange, Voxelizer(data, mesh, &interrupter));
3187
3188 for (typename DataTable::iterator i = data.begin(); i != data.end(); ++i) {
3189 VoxelizationDataType& dataItem = **i;
3190 mesh_to_volume_internal::combineData(
3191 distTree, indexTree, dataItem.distTree, dataItem.indexTree);
3192 }
3193 }
3194
3195 // The progress estimates are based on the observed average time for a few different
3196 // test cases and is only intended to provide some rough progression feedback to the user.
3197 if (interrupter.wasInterrupted(30)) return distGrid;
3198
3199
3201
3202 // Classify interior and exterior regions
3203
3204 if (computeSignedDistanceField) {
3205
3206 // Determines the inside/outside state for the narrow band of voxels.
3207 traceExteriorBoundaries(distTree);
3208
3209 std::vector<LeafNodeType*> nodes;
3210 nodes.reserve(distTree.leafCount());
3211 distTree.getNodes(nodes);
3212
3213 const tbb::blocked_range<size_t> nodeRange(0, nodes.size());
3214
3215 using SignOp =
3216 mesh_to_volume_internal::ComputeIntersectingVoxelSign<TreeType, MeshDataAdapter>;
3217
3218 tbb::parallel_for(nodeRange, SignOp(nodes, distTree, indexTree, mesh));
3219
3220 if (interrupter.wasInterrupted(45)) return distGrid;
3221
3222 // Remove voxels created by self intersecting portions of the mesh.
3223 if (removeIntersectingVoxels) {
3224
3225 tbb::parallel_for(nodeRange,
3226 mesh_to_volume_internal::ValidateIntersectingVoxels<TreeType>(distTree, nodes));
3227
3228 tbb::parallel_for(nodeRange,
3229 mesh_to_volume_internal::RemoveSelfIntersectingSurface<TreeType>(
3230 nodes, distTree, indexTree));
3231
3232 tools::pruneInactive(distTree, /*threading=*/true);
3233 tools::pruneInactive(indexTree, /*threading=*/true);
3234 }
3235 }
3236
3237 if (interrupter.wasInterrupted(50)) return distGrid;
3238
3239 if (distTree.activeVoxelCount() == 0) {
3240 distTree.clear();
3241 distTree.root().setBackground(exteriorWidth, /*updateChildNodes=*/false);
3242 return distGrid;
3243 }
3244
3245 // Transform values (world space scaling etc.).
3246 {
3247 std::vector<LeafNodeType*> nodes;
3248 nodes.reserve(distTree.leafCount());
3249 distTree.getNodes(nodes);
3250
3251 tbb::parallel_for(tbb::blocked_range<size_t>(0, nodes.size()),
3252 mesh_to_volume_internal::TransformValues<TreeType>(
3253 nodes, voxelSize, !computeSignedDistanceField));
3254 }
3255
3256 // Propagate sign information into tile regions.
3257 if (computeSignedDistanceField) {
3258 distTree.root().setBackground(exteriorWidth, /*updateChildNodes=*/false);
3259 tools::signedFloodFillWithValues(distTree, exteriorWidth, -interiorWidth);
3260 } else {
3261 tools::changeBackground(distTree, exteriorWidth);
3262 }
3263
3264 if (interrupter.wasInterrupted(54)) return distGrid;
3265
3266
3268
3269 // Expand the narrow band region
3270
3271 const ValueType minBandWidth = voxelSize * ValueType(2.0);
3272
3273 if (interiorWidth > minBandWidth || exteriorWidth > minBandWidth) {
3274
3275 // Create the initial voxel mask.
3276 BoolTreeType maskTree(false);
3277
3278 {
3279 std::vector<LeafNodeType*> nodes;
3280 nodes.reserve(distTree.leafCount());
3281 distTree.getNodes(nodes);
3282
3283 mesh_to_volume_internal::ConstructVoxelMask<TreeType> op(maskTree, distTree, nodes);
3284 tbb::parallel_reduce(tbb::blocked_range<size_t>(0, nodes.size()), op);
3285 }
3286
3287 // Progress estimation
3288 unsigned maxIterations = std::numeric_limits<unsigned>::max();
3289
3290 float progress = 54.0f, step = 0.0f;
3291 double estimated =
3292 2.0 * std::ceil((std::max(interiorWidth, exteriorWidth) - minBandWidth) / voxelSize);
3293
3294 if (estimated < double(maxIterations)) {
3295 maxIterations = unsigned(estimated);
3296 step = 40.0f / float(maxIterations);
3297 }
3298
3299 std::vector<typename BoolTreeType::LeafNodeType*> maskNodes;
3300
3301 unsigned count = 0;
3302 while (true) {
3303
3304 if (interrupter.wasInterrupted(int(progress))) return distGrid;
3305
3306 const size_t maskNodeCount = maskTree.leafCount();
3307 if (maskNodeCount == 0) break;
3308
3309 maskNodes.clear();
3310 maskNodes.reserve(maskNodeCount);
3311 maskTree.getNodes(maskNodes);
3312
3313 const tbb::blocked_range<size_t> range(0, maskNodes.size());
3314
3315 tbb::parallel_for(range,
3316 mesh_to_volume_internal::DiffLeafNodeMask<TreeType>(distTree, maskNodes));
3317
3318 mesh_to_volume_internal::expandNarrowband(distTree, indexTree, maskTree, maskNodes,
3319 mesh, exteriorWidth, interiorWidth, voxelSize);
3320
3321 if ((++count) >= maxIterations) break;
3322 progress += step;
3323 }
3324 }
3325
3326 if (interrupter.wasInterrupted(94)) return distGrid;
3327
3328 if (!polygonIndexGrid) indexGrid->clear();
3329
3330
3332
3333 // Renormalize distances to smooth out bumps caused by self intersecting
3334 // and overlapping portions of the mesh and renormalize the level set.
3335
3336 if (computeSignedDistanceField && renormalizeValues) {
3337
3338 std::vector<LeafNodeType*> nodes;
3339 nodes.reserve(distTree.leafCount());
3340 distTree.getNodes(nodes);
3341
3342 std::unique_ptr<ValueType[]> buffer{new ValueType[LeafNodeType::SIZE * nodes.size()]};
3343
3344 const ValueType offset = ValueType(0.8 * voxelSize);
3345
3346 tbb::parallel_for(tbb::blocked_range<size_t>(0, nodes.size()),
3347 mesh_to_volume_internal::OffsetValues<TreeType>(nodes, -offset));
3348
3349 tbb::parallel_for(tbb::blocked_range<size_t>(0, nodes.size()),
3350 mesh_to_volume_internal::Renormalize<TreeType>(
3351 distTree, nodes, buffer.get(), voxelSize));
3352
3353 tbb::parallel_for(tbb::blocked_range<size_t>(0, nodes.size()),
3354 mesh_to_volume_internal::MinCombine<TreeType>(nodes, buffer.get()));
3355
3356 tbb::parallel_for(tbb::blocked_range<size_t>(0, nodes.size()),
3357 mesh_to_volume_internal::OffsetValues<TreeType>(
3358 nodes, offset - mesh_to_volume_internal::Tolerance<ValueType>::epsilon()));
3359 }
3360
3361 if (interrupter.wasInterrupted(99)) return distGrid;
3362
3363
3365
3366 // Remove active voxels that exceed the narrow band limits
3367
3368 if (trimNarrowBand && std::min(interiorWidth, exteriorWidth) < voxelSize * ValueType(4.0)) {
3369
3370 std::vector<LeafNodeType*> nodes;
3371 nodes.reserve(distTree.leafCount());
3372 distTree.getNodes(nodes);
3373
3374 tbb::parallel_for(tbb::blocked_range<size_t>(0, nodes.size()),
3375 mesh_to_volume_internal::InactivateValues<TreeType>(
3376 nodes, exteriorWidth, computeSignedDistanceField ? interiorWidth : exteriorWidth));
3377
3378 tools::pruneLevelSet(
3379 distTree, exteriorWidth, computeSignedDistanceField ? -interiorWidth : -exteriorWidth);
3380 }
3381
3382 return distGrid;
3383 }
3384
3385
3386 template <typename GridType, typename MeshDataAdapter>
3387 inline typename GridType::Ptr
3388 meshToVolume(
3389 const MeshDataAdapter& mesh,
3390 const math::Transform& transform,
3391 float exteriorBandWidth,
3392 float interiorBandWidth,
3393 int flags,
3394 typename GridType::template ValueConverter<Int32>::Type * polygonIndexGrid)
3395 {
3396 util::NullInterrupter nullInterrupter;
3397 return meshToVolume<GridType>(nullInterrupter, mesh, transform,
3398 exteriorBandWidth, interiorBandWidth, flags, polygonIndexGrid);
3399 }
3400
3401
3403
3404
3405 //{
3407
3409 template<typename GridType, typename Interrupter>
3410 inline typename std::enable_if<std::is_floating_point<typename GridType::ValueType>::value,
3411 typename GridType::Ptr>::type
3412 doMeshConversion(
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,
3418 float exBandWidth,
3419 float inBandWidth,
3420 bool unsignedDistanceField = false)
3421 {
3422 if (points.empty()) {
3423 return typename GridType::Ptr(new GridType(typename GridType::ValueType(exBandWidth)));
3424 }
3425
3426 const size_t numPoints = points.size();
3427 std::unique_ptr<Vec3s[]> indexSpacePoints{new Vec3s[numPoints]};
3428
3429 // transform points to local grid index space
3430 tbb::parallel_for(tbb::blocked_range<size_t>(0, numPoints),
3431 mesh_to_volume_internal::TransformPoints<Vec3s>(
3432 &points[0], indexSpacePoints.get(), xform));
3433
3434 const int conversionFlags = unsignedDistanceField ? UNSIGNED_DISTANCE_FIELD : 0;
3435
3436 if (quads.empty()) {
3437
3438 QuadAndTriangleDataAdapter<Vec3s, Vec3I>
3439 mesh(indexSpacePoints.get(), numPoints, &triangles[0], triangles.size());
3440
3441 return meshToVolume<GridType>(mesh, xform, exBandWidth, inBandWidth, conversionFlags);
3442
3443 } else if (triangles.empty()) {
3444
3445 QuadAndTriangleDataAdapter<Vec3s, Vec4I>
3446 mesh(indexSpacePoints.get(), numPoints, &quads[0], quads.size());
3447
3448 return meshToVolume<GridType>(mesh, xform, exBandWidth, inBandWidth, conversionFlags);
3449 }
3450
3451 // pack primitives
3452
3453 const size_t numPrimitives = triangles.size() + quads.size();
3454 std::unique_ptr<Vec4I[]> prims{new Vec4I[numPrimitives]};
3455
3456 for (size_t n = 0, N = triangles.size(); n < N; ++n) {
3457 const Vec3I& triangle = triangles[n];
3458 Vec4I& prim = prims[n];
3459 prim[0] = triangle[0];
3460 prim[1] = triangle[1];
3461 prim[2] = triangle[2];
3462 prim[3] = util::INVALID_IDX;
3463 }
3464
3465 const size_t offset = triangles.size();
3466 for (size_t n = 0, N = quads.size(); n < N; ++n) {
3467 prims[offset + n] = quads[n];
3468 }
3469
3470 QuadAndTriangleDataAdapter<Vec3s, Vec4I>
3471 mesh(indexSpacePoints.get(), numPoints, prims.get(), numPrimitives);
3472
3473 return meshToVolume<GridType>(interrupter, mesh, xform,
3474 exBandWidth, inBandWidth, conversionFlags);
3475 }
3476
3477
3480 template<typename GridType, typename Interrupter>
3481 inline typename std::enable_if<!std::is_floating_point<typename GridType::ValueType>::value,
3482 typename GridType::Ptr>::type
3483 doMeshConversion(
3484 Interrupter&,
3485 const math::Transform& /*xform*/,
3486 const std::vector<Vec3s>& /*points*/,
3487 const std::vector<Vec3I>& /*triangles*/,
3488 const std::vector<Vec4I>& /*quads*/,
3489 float /*exBandWidth*/,
3490 float /*inBandWidth*/,
3491 bool /*unsignedDistanceField*/ = false)
3492 {
3493 OPENVDB_THROW(TypeError,
3494 "mesh to volume conversion is supported only for scalar floating-point grids");
3495 }
3496
3498 //}
3499
3500
3502
3503
3504 template<typename GridType>
3505 inline typename GridType::Ptr
3506 meshToLevelSet(
3507 const openvdb::math::Transform& xform,
3508 const std::vector<Vec3s>& points,
3509 const std::vector<Vec3I>& triangles,
3510 float halfWidth)
3511 {
3512 util::NullInterrupter nullInterrupter;
3513 std::vector<Vec4I> quads(0);
3514 return doMeshConversion<GridType>(nullInterrupter, xform, points, triangles, quads,
3515 halfWidth, halfWidth);
3516 }
3517
3518
3519 template<typename GridType, typename Interrupter>
3520 inline typename GridType::Ptr
3521 meshToLevelSet(
3522 Interrupter& interrupter,
3523 const openvdb::math::Transform& xform,
3524 const std::vector<Vec3s>& points,
3525 const std::vector<Vec3I>& triangles,
3526 float halfWidth)
3527 {
3528 std::vector<Vec4I> quads(0);
3529 return doMeshConversion<GridType>(interrupter, xform, points, triangles, quads,
3530 halfWidth, halfWidth);
3531 }
3532
3533
3534 template<typename GridType>
3535 inline typename GridType::Ptr
3536 meshToLevelSet(
3537 const openvdb::math::Transform& xform,
3538 const std::vector<Vec3s>& points,
3539 const std::vector<Vec4I>& quads,
3540 float halfWidth)
3541 {
3542 util::NullInterrupter nullInterrupter;
3543 std::vector<Vec3I> triangles(0);
3544 return doMeshConversion<GridType>(nullInterrupter, xform, points, triangles, quads,
3545 halfWidth, halfWidth);
3546 }
3547
3548
3549 template<typename GridType, typename Interrupter>
3550 inline typename GridType::Ptr
3551 meshToLevelSet(
3552 Interrupter& interrupter,
3553 const openvdb::math::Transform& xform,
3554 const std::vector<Vec3s>& points,
3555 const std::vector<Vec4I>& quads,
3556 float halfWidth)
3557 {
3558 std::vector<Vec3I> triangles(0);
3559 return doMeshConversion<GridType>(interrupter, xform, points, triangles, quads,
3560 halfWidth, halfWidth);
3561 }
3562
3563
3564 template<typename GridType>
3565 inline typename GridType::Ptr
3566 meshToLevelSet(
3567 const openvdb::math::Transform& xform,
3568 const std::vector<Vec3s>& points,
3569 const std::vector<Vec3I>& triangles,
3570 const std::vector<Vec4I>& quads,
3571 float halfWidth)
3572 {
3573 util::NullInterrupter nullInterrupter;
3574 return doMeshConversion<GridType>(nullInterrupter, xform, points, triangles, quads,
3575 halfWidth, halfWidth);
3576 }
3577
3578
3579 template<typename GridType, typename Interrupter>
3580 inline typename GridType::Ptr
3581 meshToLevelSet(
3582 Interrupter& interrupter,
3583 const openvdb::math::Transform& xform,
3584 const std::vector<Vec3s>& points,
3585 const std::vector<Vec3I>& triangles,
3586 const std::vector<Vec4I>& quads,
3587 float halfWidth)
3588 {
3589 return doMeshConversion<GridType>(interrupter, xform, points, triangles, quads,
3590 halfWidth, halfWidth);
3591 }
3592
3593
3594 template<typename GridType>
3595 inline typename GridType::Ptr
3596 meshToSignedDistanceField(
3597 const openvdb::math::Transform& xform,
3598 const std::vector<Vec3s>& points,
3599 const std::vector<Vec3I>& triangles,
3600 const std::vector<Vec4I>& quads,
3601 float exBandWidth,
3602 float inBandWidth)
3603 {
3604 util::NullInterrupter nullInterrupter;
3605 return doMeshConversion<GridType>(nullInterrupter, xform, points, triangles,
3606 quads, exBandWidth, inBandWidth);
3607 }
3608
3609
3610 template<typename GridType, typename Interrupter>
3611 inline typename GridType::Ptr
3612 meshToSignedDistanceField(
3613 Interrupter& interrupter,
3614 const openvdb::math::Transform& xform,
3615 const std::vector<Vec3s>& points,
3616 const std::vector<Vec3I>& triangles,
3617 const std::vector<Vec4I>& quads,
3618 float exBandWidth,
3619 float inBandWidth)
3620 {
3621 return doMeshConversion<GridType>(interrupter, xform, points, triangles,
3622 quads, exBandWidth, inBandWidth);
3623 }
3624
3625
3626 template<typename GridType>
3627 inline typename GridType::Ptr
3628 meshToUnsignedDistanceField(
3629 const openvdb::math::Transform& xform,
3630 const std::vector<Vec3s>& points,
3631 const std::vector<Vec3I>& triangles,
3632 const std::vector<Vec4I>& quads,
3633 float bandWidth)
3634 {
3635 util::NullInterrupter nullInterrupter;
3636 return doMeshConversion<GridType>(nullInterrupter, xform, points, triangles, quads,
3637 bandWidth, bandWidth, true);
3638 }
3639
3640
3641 template<typename GridType, typename Interrupter>
3642 inline typename GridType::Ptr
3643 meshToUnsignedDistanceField(
3644 Interrupter& interrupter,
3645 const openvdb::math::Transform& xform,
3646 const std::vector<Vec3s>& points,
3647 const std::vector<Vec3I>& triangles,
3648 const std::vector<Vec4I>& quads,
3649 float bandWidth)
3650 {
3651 return doMeshConversion<GridType>(interrupter, xform, points, triangles, quads,
3652 bandWidth, bandWidth, true);
3653 }
3654
3655
3657
3658
3659 // Required by several of the tree nodes
3660 inline std::ostream&
3661 operator<<(std::ostream& ostr, const MeshToVoxelEdgeData::EdgeData& rhs)
3662 {
3663 ostr << "{[ " << rhs.mXPrim << ", " << rhs.mXDist << "]";
3664 ostr << " [ " << rhs.mYPrim << ", " << rhs.mYDist << "]";
3665 ostr << " [ " << rhs.mZPrim << ", " << rhs.mZDist << "]}";
3666 return ostr;
3667 }
3668
3669 // Required by math::Abs
3670 inline MeshToVoxelEdgeData::EdgeData
3671 Abs(const MeshToVoxelEdgeData::EdgeData& x)
3672 {
3673 return x;
3674 }
3675
3676
3678
3679
3680 class MeshToVoxelEdgeData::GenEdgeData
3681 {
3682 public:
3683
3684 GenEdgeData(
3685 const std::vector<Vec3s>& pointList,
3686 const std::vector<Vec4I>& polygonList);
3687
3688 void run(bool threaded = true);
3689
3690 GenEdgeData(GenEdgeData& rhs, tbb::split);
3691 inline void operator() (const tbb::blocked_range<size_t> &range);
3692 inline void join(GenEdgeData& rhs);
3693
3694 inline TreeType& tree() { return mTree; }
3695
3696 private:
3697 void operator=(const GenEdgeData&) {}
3698
3699 struct Primitive { Vec3d a, b, c, d; Int32 index; };
3700
3701 template<bool IsQuad>
3702 inline void voxelize(const Primitive&);
3703
3704 template<bool IsQuad>
3705 inline bool evalPrimitive(const Coord&, const Primitive&);
3706
3707 inline bool rayTriangleIntersection( const Vec3d& origin, const Vec3d& dir,
3708 const Vec3d& a, const Vec3d& b, const Vec3d& c, double& t);
3709
3710
3711 TreeType mTree;
3712 Accessor mAccessor;
3713
3714 const std::vector<Vec3s>& mPointList;
3715 const std::vector<Vec4I>& mPolygonList;
3716
3717 // Used internally for acceleration
3718 using IntTreeT = TreeType::ValueConverter<Int32>::Type;
3719 IntTreeT mLastPrimTree;
3720 tree::ValueAccessor<IntTreeT> mLastPrimAccessor;
3721 }; // class MeshToVoxelEdgeData::GenEdgeData
3722
3723
3724 inline
3725 MeshToVoxelEdgeData::GenEdgeData::GenEdgeData(
3726 const std::vector<Vec3s>& pointList,
3727 const std::vector<Vec4I>& polygonList)
3728 : mTree(EdgeData())
3729 , mAccessor(mTree)
3730 , mPointList(pointList)
3731 , mPolygonList(polygonList)
3732 , mLastPrimTree(Int32(util::INVALID_IDX))
3733 , mLastPrimAccessor(mLastPrimTree)
3734 {
3735 }
3736
3737
3738 inline
3739 MeshToVoxelEdgeData::GenEdgeData::GenEdgeData(GenEdgeData& rhs, tbb::split)
3740 : mTree(EdgeData())
3741 , mAccessor(mTree)
3742 , mPointList(rhs.mPointList)
3743 , mPolygonList(rhs.mPolygonList)
3744 , mLastPrimTree(Int32(util::INVALID_IDX))
3745 , mLastPrimAccessor(mLastPrimTree)
3746 {
3747 }
3748
3749
3750 inline void
3751 MeshToVoxelEdgeData::GenEdgeData::run(bool threaded)
3752 {
3753 if (threaded) {
3754 tbb::parallel_reduce(tbb::blocked_range<size_t>(0, mPolygonList.size()), *this);
3755 } else {
3756 (*this)(tbb::blocked_range<size_t>(0, mPolygonList.size()));
3757 }
3758 }
3759
3760
3761 inline void
3762 MeshToVoxelEdgeData::GenEdgeData::join(GenEdgeData& rhs)
3763 {
3764 using RootNodeType = TreeType::RootNodeType;
3765 using NodeChainType = RootNodeType::NodeChainType;
3766 static_assert(boost::mpl::size<NodeChainType>::value > 1, "expected tree height > 1");
3767 using InternalNodeType = boost::mpl::at<NodeChainType, boost::mpl::int_<1> >::type;
3768
3769 Coord ijk;
3770 Index offset;
3771
3772 rhs.mTree.clearAllAccessors();
3773
3774 TreeType::LeafIter leafIt = rhs.mTree.beginLeaf();
3775 for ( ; leafIt; ++leafIt) {
3776 ijk = leafIt->origin();
3777
3778 TreeType::LeafNodeType* lhsLeafPt = mTree.probeLeaf(ijk);
3779
3780 if (!lhsLeafPt) {
3781
3782 mAccessor.addLeaf(rhs.mAccessor.probeLeaf(ijk));
3783 InternalNodeType* node = rhs.mAccessor.getNode<InternalNodeType>();
3784 node->stealNode<TreeType::LeafNodeType>(ijk, EdgeData(), false);
3785 rhs.mAccessor.clear();
3786
3787 } else {
3788
3789 TreeType::LeafNodeType::ValueOnCIter it = leafIt->cbeginValueOn();
3790 for ( ; it; ++it) {
3791
3792 offset = it.pos();
3793 const EdgeData& rhsValue = it.getValue();
3794
3795 if (!lhsLeafPt->isValueOn(offset)) {
3796 lhsLeafPt->setValueOn(offset, rhsValue);
3797 } else {
3798
3799 EdgeData& lhsValue = const_cast<EdgeData&>(lhsLeafPt->getValue(offset));
3800
3801 if (rhsValue.mXDist < lhsValue.mXDist) {
3802 lhsValue.mXDist = rhsValue.mXDist;
3803 lhsValue.mXPrim = rhsValue.mXPrim;
3804 }
3805
3806 if (rhsValue.mYDist < lhsValue.mYDist) {
3807 lhsValue.mYDist = rhsValue.mYDist;
3808 lhsValue.mYPrim = rhsValue.mYPrim;
3809 }
3810
3811 if (rhsValue.mZDist < lhsValue.mZDist) {
3812 lhsValue.mZDist = rhsValue.mZDist;
3813 lhsValue.mZPrim = rhsValue.mZPrim;
3814 }
3815
3816 }
3817 } // end value iteration
3818 }
3819 } // end leaf iteration
3820 }
3821
3822
3823 inline void
3824 MeshToVoxelEdgeData::GenEdgeData::operator()(const tbb::blocked_range<size_t> &range)
3825 {
3826 Primitive prim;
3827
3828 for (size_t n = range.begin(); n < range.end(); ++n) {
3829
3830 const Vec4I& verts = mPolygonList[n];
3831
3832 prim.index = Int32(n);
3833 prim.a = Vec3d(mPointList[verts[0]]);
3834 prim.b = Vec3d(mPointList[verts[1]]);
3835 prim.c = Vec3d(mPointList[verts[2]]);
3836
3837 if (util::INVALID_IDX != verts[3]) {
3838 prim.d = Vec3d(mPointList[verts[3]]);
3839 voxelize<true>(prim);
3840 } else {
3841 voxelize<false>(prim);
3842 }
3843 }
3844 }
3845
3846
3847 template<bool IsQuad>
3848 inline void
3849 MeshToVoxelEdgeData::GenEdgeData::voxelize(const Primitive& prim)
3850 {
3851 std::deque<Coord> coordList;
3852 Coord ijk, nijk;
3853
3854 ijk = Coord::floor(prim.a);
3855 coordList.push_back(ijk);
3856
3857 evalPrimitive<IsQuad>(ijk, prim);
3858
3859 while (!coordList.empty()) {
3860
3861 ijk = coordList.back();
3862 coordList.pop_back();
3863
3864 for (Int32 i = 0; i < 26; ++i) {
3865 nijk = ijk + util::COORD_OFFSETS[i];
3866
3867 if (prim.index != mLastPrimAccessor.getValue(nijk)) {
3868 mLastPrimAccessor.setValue(nijk, prim.index);
3869 if(evalPrimitive<IsQuad>(nijk, prim)) coordList.push_back(nijk);
3870 }
3871 }
3872 }
3873 }
3874
3875
3876 template<bool IsQuad>
3877 inline bool
3878 MeshToVoxelEdgeData::GenEdgeData::evalPrimitive(const Coord& ijk, const Primitive& prim)
3879 {
3880 Vec3d uvw, org(ijk[0], ijk[1], ijk[2]);
3881 bool intersecting = false;
3882 double t;
3883
3884 EdgeData edgeData;
3885 mAccessor.probeValue(ijk, edgeData);
3886
3887 // Evaluate first triangle
3888 double dist = (org -
3889 closestPointOnTriangleToPoint(prim.a, prim.c, prim.b, org, uvw)).lengthSqr();
3890
3891 if (rayTriangleIntersection(org, Vec3d(1.0, 0.0, 0.0), prim.a, prim.c, prim.b, t)) {
3892 if (t < edgeData.mXDist) {
3893 edgeData.mXDist = float(t);
3894 edgeData.mXPrim = prim.index;
3895 intersecting = true;
3896 }
3897 }
3898
3899 if (rayTriangleIntersection(org, Vec3d(0.0, 1.0, 0.0), prim.a, prim.c, prim.b, t)) {
3900 if (t < edgeData.mYDist) {
3901 edgeData.mYDist = float(t);
3902 edgeData.mYPrim = prim.index;
3903 intersecting = true;
3904 }
3905 }
3906
3907 if (rayTriangleIntersection(org, Vec3d(0.0, 0.0, 1.0), prim.a, prim.c, prim.b, t)) {
3908 if (t < edgeData.mZDist) {
3909 edgeData.mZDist = float(t);
3910 edgeData.mZPrim = prim.index;
3911 intersecting = true;
3912 }
3913 }
3914
3915 if (IsQuad) {
3916 // Split quad into a second triangle and calculate distance.
3917 double secondDist = (org -
3918 closestPointOnTriangleToPoint(prim.a, prim.d, prim.c, org, uvw)).lengthSqr();
3919
3920 if (secondDist < dist) dist = secondDist;
3921
3922 if (rayTriangleIntersection(org, Vec3d(1.0, 0.0, 0.0), prim.a, prim.d, prim.c, t)) {
3923 if (t < edgeData.mXDist) {
3924 edgeData.mXDist = float(t);
3925 edgeData.mXPrim = prim.index;
3926 intersecting = true;
3927 }
3928 }
3929
3930 if (rayTriangleIntersection(org, Vec3d(0.0, 1.0, 0.0), prim.a, prim.d, prim.c, t)) {
3931 if (t < edgeData.mYDist) {
3932 edgeData.mYDist = float(t);
3933 edgeData.mYPrim = prim.index;
3934 intersecting = true;
3935 }
3936 }
3937
3938 if (rayTriangleIntersection(org, Vec3d(0.0, 0.0, 1.0), prim.a, prim.d, prim.c, t)) {
3939 if (t < edgeData.mZDist) {
3940 edgeData.mZDist = float(t);
3941 edgeData.mZPrim = prim.index;
3942 intersecting = true;
3943 }
3944 }
3945 }
3946
3947 if (intersecting) mAccessor.setValue(ijk, edgeData);
3948
3949 return (dist < 0.86602540378443861);
3950 }
3951
3952
3953 inline bool
3954 MeshToVoxelEdgeData::GenEdgeData::rayTriangleIntersection(
3955 const Vec3d& origin, const Vec3d& dir,
3956 const Vec3d& a, const Vec3d& b, const Vec3d& c,
3957 double& t)
3958 {
3959 // Check if ray is parallel with triangle
3960
3961 Vec3d e1 = b - a;
3962 Vec3d e2 = c - a;
3963 Vec3d s1 = dir.cross(e2);
3964
3965 double divisor = s1.dot(e1);
3966 if (!(std::abs(divisor) > 0.0)) return false;
3967
3968 // Compute barycentric coordinates
3969
3970 double inv_divisor = 1.0 / divisor;
3971 Vec3d d = origin - a;
3972 double b1 = d.dot(s1) * inv_divisor;
3973
3974 if (b1 < 0.0 || b1 > 1.0) return false;
3975
3976 Vec3d s2 = d.cross(e1);
3977 double b2 = dir.dot(s2) * inv_divisor;
3978
3979 if (b2 < 0.0 || (b1 + b2) > 1.0) return false;
3980
3981 // Compute distance to intersection point
3982
3983 t = e2.dot(s2) * inv_divisor;
3984 return (t < 0.0) ? false : true;
3985 }
3986
3987
3989
3990
3991 inline
3992 MeshToVoxelEdgeData::MeshToVoxelEdgeData()
3993 : mTree(EdgeData())
3994 {
3995 }
3996
3997
3998 inline void
3999 MeshToVoxelEdgeData::convert(
4000 const std::vector<Vec3s>& pointList,
4001 const std::vector<Vec4I>& polygonList)
4002 {
4003 GenEdgeData converter(pointList, polygonList);
4004 converter.run();
4005
4006 mTree.clear();
4007 mTree.merge(converter.tree());
4008 }
4009
4010
4011 inline void
4012 MeshToVoxelEdgeData::getEdgeData(
4013 Accessor& acc,
4014 const Coord& ijk,
4015 std::vector<Vec3d>& points,
4016 std::vector<Index32>& primitives)
4017 {
4018 EdgeData data;
4019 Vec3d point;
4020
4021 Coord coord = ijk;
4022
4023 if (acc.probeValue(coord, data)) {
4024
4025 if (data.mXPrim != util::INVALID_IDX) {
4026 point[0] = double(coord[0]) + data.mXDist;
4027 point[1] = double(coord[1]);
4028 point[2] = double(coord[2]);
4029
4030 points.push_back(point);
4031 primitives.push_back(data.mXPrim);
4032 }
4033
4034 if (data.mYPrim != util::INVALID_IDX) {
4035 point[0] = double(coord[0]);
4036 point[1] = double(coord[1]) + data.mYDist;
4037 point[2] = double(coord[2]);
4038
4039 points.push_back(point);
4040 primitives.push_back(data.mYPrim);
4041 }
4042
4043 if (data.mZPrim != util::INVALID_IDX) {
4044 point[0] = double(coord[0]);
4045 point[1] = double(coord[1]);
4046 point[2] = double(coord[2]) + data.mZDist;
4047
4048 points.push_back(point);
4049 primitives.push_back(data.mZPrim);
4050 }
4051
4052 }
4053
4054 coord[0] += 1;
4055
4056 if (acc.probeValue(coord, data)) {
4057
4058 if (data.mYPrim != util::INVALID_IDX) {
4059 point[0] = double(coord[0]);
4060 point[1] = double(coord[1]) + data.mYDist;
4061 point[2] = double(coord[2]);
4062
4063 points.push_back(point);
4064 primitives.push_back(data.mYPrim);
4065 }
4066
4067 if (data.mZPrim != util::INVALID_IDX) {
4068 point[0] = double(coord[0]);
4069 point[1] = double(coord[1]);
4070 point[2] = double(coord[2]) + data.mZDist;
4071
4072 points.push_back(point);
4073 primitives.push_back(data.mZPrim);
4074 }
4075 }
4076
4077 coord[2] += 1;
4078
4079 if (acc.probeValue(coord, data)) {
4080 if (data.mYPrim != util::INVALID_IDX) {
4081 point[0] = double(coord[0]);
4082 point[1] = double(coord[1]) + data.mYDist;
4083 point[2] = double(coord[2]);
4084
4085 points.push_back(point);
4086 primitives.push_back(data.mYPrim);
4087 }
4088 }
4089
4090 coord[0] -= 1;
4091
4092 if (acc.probeValue(coord, data)) {
4093
4094 if (data.mXPrim != util::INVALID_IDX) {
4095 point[0] = double(coord[0]) + data.mXDist;
4096 point[1] = double(coord[1]);
4097 point[2] = double(coord[2]);
4098
4099 points.push_back(point);
4100 primitives.push_back(data.mXPrim);
4101 }
4102
4103 if (data.mYPrim != util::INVALID_IDX) {
4104 point[0] = double(coord[0]);
4105 point[1] = double(coord[1]) + data.mYDist;
4106 point[2] = double(coord[2]);
4107
4108 points.push_back(point);
4109 primitives.push_back(data.mYPrim);
4110 }
4111 }
4112
4113
4114 coord[1] += 1;
4115
4116 if (acc.probeValue(coord, data)) {
4117
4118 if (data.mXPrim != util::INVALID_IDX) {
4119 point[0] = double(coord[0]) + data.mXDist;
4120 point[1] = double(coord[1]);
4121 point[2] = double(coord[2]);
4122
4123 points.push_back(point);
4124 primitives.push_back(data.mXPrim);
4125 }
4126 }
4127
4128 coord[2] -= 1;
4129
4130 if (acc.probeValue(coord, data)) {
4131
4132 if (data.mXPrim != util::INVALID_IDX) {
4133 point[0] = double(coord[0]) + data.mXDist;
4134 point[1] = double(coord[1]);
4135 point[2] = double(coord[2]);
4136
4137 points.push_back(point);
4138 primitives.push_back(data.mXPrim);
4139 }
4140
4141 if (data.mZPrim != util::INVALID_IDX) {
4142 point[0] = double(coord[0]);
4143 point[1] = double(coord[1]);
4144 point[2] = double(coord[2]) + data.mZDist;
4145
4146 points.push_back(point);
4147 primitives.push_back(data.mZPrim);
4148 }
4149 }
4150
4151 coord[0] += 1;
4152
4153 if (acc.probeValue(coord, data)) {
4154
4155 if (data.mZPrim != util::INVALID_IDX) {
4156 point[0] = double(coord[0]);
4157 point[1] = double(coord[1]);
4158 point[2] = double(coord[2]) + data.mZDist;
4159
4160 points.push_back(point);
4161 primitives.push_back(data.mZPrim);
4162 }
4163 }
4164 }
4165
4166
4167 template<typename GridType, typename VecType>
4168 inline typename GridType::Ptr
4169 createLevelSetBox(const math::BBox<VecType>& bbox,
4170 const openvdb::math::Transform& xform,
4171 typename VecType::ValueType halfWidth)
4172 {
4173 const Vec3s pmin = Vec3s(xform.worldToIndex(bbox.min()));
4174 const Vec3s pmax = Vec3s(xform.worldToIndex(bbox.max()));
4175
4176 Vec3s points[8];
4177 points[0] = Vec3s(pmin[0], pmin[1], pmin[2]);
4178 points[1] = Vec3s(pmin[0], pmin[1], pmax[2]);
4179 points[2] = Vec3s(pmax[0], pmin[1], pmax[2]);
4180 points[3] = Vec3s(pmax[0], pmin[1], pmin[2]);
4181 points[4] = Vec3s(pmin[0], pmax[1], pmin[2]);
4182 points[5] = Vec3s(pmin[0], pmax[1], pmax[2]);
4183 points[6] = Vec3s(pmax[0], pmax[1], pmax[2]);
4184 points[7] = Vec3s(pmax[0], pmax[1], pmin[2]);
4185
4186 Vec4I faces[6];
4187 faces[0] = Vec4I(0, 1, 2, 3); // bottom
4188 faces[1] = Vec4I(7, 6, 5, 4); // top
4189 faces[2] = Vec4I(4, 5, 1, 0); // front
4190 faces[3] = Vec4I(6, 7, 3, 2); // back
4191 faces[4] = Vec4I(0, 3, 7, 4); // left
4192 faces[5] = Vec4I(1, 5, 6, 2); // right
4193
4194 QuadAndTriangleDataAdapter<Vec3s, Vec4I> mesh(points, 8, faces, 6);
4195
4196 return meshToVolume<GridType>(mesh, xform, halfWidth, halfWidth);
4197 }
4198
4199
4200 } // namespace tools
4201 } // namespace OPENVDB_VERSION_NAME
4202 } // namespace openvdb
4203
4204 #endif // OPENVDB_TOOLS_MESH_TO_VOLUME_HAS_BEEN_INCLUDED
4205
4206 // Copyright (c) 2012-2018 DreamWorks Animation LLC
4207 // All rights reserved. This software is distributed under the
4208 // Mozilla Public License 2.0 ( http://www.mozilla.org/MPL/2.0/ )
openvdb::v5_1::tools::signedFloodFillWithValues
void signedFloodFillWithValues(TreeOrLeafManagerT &tree, const typename TreeOrLeafManagerT::ValueType &outsideWidth, const typename TreeOrLeafManagerT::ValueType &insideWidth, bool threaded=true, size_t grainSize=1, Index minLevel=0)
Set the values of all inactive voxels and tiles of a narrow-band level set from the signs of the acti...
Definition: SignedFloodFill.h:280
openvdb::v5_1::math::isExactlyEqual
bool isExactlyEqual(const T0 &a, const T1 &b)
Return true if a is exactly equal to b.
Definition: Math.h:395
openvdb::v5_1::tree::ValueAccessor::getValue
const ValueType & getValue(const Coord &xyz) const
Return the value of the voxel at the given coordinates.
Definition: ValueAccessor.h:257
openvdb::v5_1::tools::meshToVolume
GridType::Ptr meshToVolume(Interrupter &interrupter, const MeshDataAdapter &mesh, const math::Transform &transform, float exteriorBandWidth=3.0f, float interiorBandWidth=3.0f, int flags=0, typename GridType::template ValueConverter< Int32 >::Type *polygonIndexGrid=nullptr)
Convert polygonal meshes that consist of quads and/or triangles into signed or unsigned distance fiel...
Definition: MeshToVolume.h:3086
openvdb::v5_1::tools::mesh_to_volume_internal::MinCombine::operator()
void operator()(const tbb::blocked_range< size_t > &range) const
Definition: MeshToVolume.h:2978
openvdb::v5_1::tools::mesh_to_volume_internal::OffsetValues::LeafNodeType
typename TreeType::LeafNodeType LeafNodeType
Definition: MeshToVolume.h:2874
openvdb::v5_1::tools::MeshToVoxelEdgeData::MeshToVoxelEdgeData
MeshToVoxelEdgeData()
Definition: MeshToVolume.h:3992
openvdb::v5_1::tools::mesh_to_volume_internal::LeafNodeConnectivityTable::LeafNodeConnectivityTable
LeafNodeConnectivityTable(TreeType &tree)
Definition: MeshToVolume.h:784
FiniteDifference.h
openvdb::v5_1::tree::Tree::LeafNodeType
typename RootNodeType::LeafNodeType LeafNodeType
Definition: Tree.h:212
openvdb::v5_1::tools::mesh_to_volume_internal::ComputeIntersectingVoxelSign::operator()
void operator()(const tbb::blocked_range< size_t > &range) const
Definition: MeshToVolume.h:1393
openvdb::v5_1::Index32
uint32_t Index32
Definition: Types.h:59
openvdb::v5_1::tools::mesh_to_volume_internal::CombineLeafNodes::LeafNodeType
typename TreeType::LeafNodeType LeafNodeType
Definition: MeshToVolume.h:593
openvdb::v5_1::tools::mesh_to_volume_internal::ValidateIntersectingVoxels::LeafNodeType
typename TreeType::LeafNodeType LeafNodeType
Definition: MeshToVolume.h:1709
openvdb::v5_1::tree::Tree::beginLeaf
LeafIter beginLeaf()
Return an iterator over all leaf nodes in this tree.
Definition: Tree.h:1179
openvdb::v5_1::tools::mesh_to_volume_internal::VoxelizationData::indexTree
Int32TreeType indexTree
Definition: MeshToVolume.h:1952
openvdb::v5_1::tools::mesh_to_volume_internal::SweepExteriorSign::ValueType
typename TreeType::ValueType ValueType
Definition: MeshToVolume.h:841
openvdb::v5_1::tools::mesh_to_volume_internal::RemoveSelfIntersectingSurface::mNodes
LeafNodeType **const mNodes
Definition: MeshToVolume.h:1810
openvdb::v5_1::tools::MeshToVoxelEdgeData::EdgeData::mXPrim
Index32 mXPrim
Definition: MeshToVolume.h:493
openvdb::v5_1::tools::mesh_to_volume_internal::ExpandNarrowband::ExpandNarrowband
ExpandNarrowband(std::vector< BoolLeafNodeType *> &maskNodes, BoolTreeType &maskTree, TreeType &distTree, Int32TreeType &indexTree, const MeshDataAdapter &mesh, ValueType exteriorBandWidth, ValueType interiorBandWidth, ValueType voxelSize)
Definition: MeshToVolume.h:2387
openvdb::v5_1::tools::MeshToVoxelEdgeData::EdgeData
Internal edge data type.
Definition: MeshToVolume.h:468
openvdb::v5_1::tools::mesh_to_volume_internal::Tolerance::minNarrowBandWidth
static ValueType minNarrowBandWidth()
Definition: MeshToVolume.h:579
openvdb::v5_1::tools::mesh_to_volume_internal::ComputeIntersectingVoxelSign::LocalData
std::pair< PointArray, MaskArray > LocalData
Definition: MeshToVolume.h:1376
openvdb::v5_1::math::Vec4
Definition: Mat4.h:51
openvdb::v5_1::math::Coord::min
static Coord min()
Return the smallest possible coordinate.
Definition: Coord.h:70
openvdb::v5_1::tools::mesh_to_volume_internal::ValidateIntersectingVoxels::ValidateIntersectingVoxels
ValidateIntersectingVoxels(TreeType &tree, std::vector< LeafNodeType *> &nodes)
Definition: MeshToVolume.h:1713
openvdb::v5_1::tools::mesh_to_volume_internal::SeedFillExteriorSign::mNodes
LeafNodeType **const mNodes
Definition: MeshToVolume.h:1131
openvdb::v5_1::tools::mesh_to_volume_internal::SeedPoints::LeafNodeType
typename TreeType::LeafNodeType LeafNodeType
Definition: MeshToVolume.h:1208
openvdb::v5_1::tools::mesh_to_volume_internal::OffsetValues::operator()
void operator()(const tbb::blocked_range< size_t > &range) const
Definition: MeshToVolume.h:2882
Types.h
openvdb::v5_1::math::CoordBBox::max
const Coord & max() const
Definition: Coord.h:338
openvdb::v5_1::tools::mesh_to_volume_internal::TransformPoints::TransformPoints
TransformPoints(const PointType *pointsIn, PointType *pointsOut, const math::Transform &xform)
Definition: MeshToVolume.h:543
openvdb::v5_1::tools::mesh_to_volume_internal::ConstructVoxelMask::ConstructVoxelMask
ConstructVoxelMask(ConstructVoxelMask &rhs, tbb::split)
Definition: MeshToVolume.h:2263
openvdb::v5_1::tools::mesh_to_volume_internal::LeafNodeConnectivityTable::offsetsPrevY
const size_t * offsetsPrevY() const
Definition: MeshToVolume.h:823
openvdb::v5_1::tree::ValueAccessor::touchLeaf
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
openvdb::v5_1::tools::mesh_to_volume_internal::expandNarrowband
void expandNarrowband(TreeType &distTree, Int32TreeType &indexTree, BoolTreeType &maskTree, std::vector< typename BoolTreeType::LeafNodeType *> &maskNodes, const MeshDataAdapter &mesh, typename TreeType::ValueType exteriorBandWidth, typename TreeType::ValueType interiorBandWidth, typename TreeType::ValueType voxelSize)
Definition: MeshToVolume.h:2755
NullInterrupter.h
openvdb::v5_1::tools::mesh_to_volume_internal::VoxelizationData::distTree
TreeType distTree
Definition: MeshToVolume.h:1949
openvdb::v5_1::math::CoordBBox
Axis-aligned bounding box of signed integer coordinates.
Definition: Coord.h:264
openvdb::v5_1::tools::MeshToVoxelEdgeData::EdgeData::mZDist
float mZDist
Definition: MeshToVolume.h:492
openvdb::v5_1::tools::mesh_to_volume_internal::Tolerance
Definition: MeshToVolume.h:576
openvdb::v5_1::math::Coord::max
static Coord max()
Return the largest possible coordinate.
Definition: Coord.h:73
openvdb::v5_1::tools::mesh_to_volume_internal::DiffLeafNodeMask::operator()
void operator()(const tbb::blocked_range< size_t > &range) const
Definition: MeshToVolume.h:2206
openvdb::v5_1::tools::mesh_to_volume_internal::SeedFillExteriorSign
Definition: MeshToVolume.h:1110
openvdb::v5_1::tools::mesh_to_volume_internal::ExpandNarrowband::ValueType
typename TreeType::ValueType ValueType
Definition: MeshToVolume.h:2362
openvdb::v5_1::tools::mesh_to_volume_internal::ComputeNodeConnectivity::LeafNodeType
typename TreeType::LeafNodeType LeafNodeType
Definition: MeshToVolume.h:711
openvdb::v5_1::math::Axis
Axis
Definition: Math.h:856
openvdb::v5_1::tree::TreeValueIteratorBase::getValue
const ValueT & getValue() const
Return the tile or voxel value to which this iterator is currently pointing.
Definition: TreeIterator.h:741
openvdb::v5_1::tools::mesh_to_volume_internal::OffsetValues::ValueType
typename TreeType::ValueType ValueType
Definition: MeshToVolume.h:2875
openvdb::v5_1::tools::mesh_to_volume_internal::AddNodes::mTree
TreeType *const mTree
Definition: MeshToVolume.h:2748
openvdb::v5_1::math::isZero
bool isZero(const Type &x)
Return true if x is exactly equal to zero.
Definition: Math.h:308
OPENVDB_LOG_DEBUG
#define OPENVDB_LOG_DEBUG(message)
In debug builds only, log a debugging message of the form &#39;someVar << "text" << ...&#39;.
Definition: logging.h:290
openvdb::v5_1::tools::mesh_to_volume_internal::ValidateIntersectingVoxels::mTree
TreeType *const mTree
Definition: MeshToVolume.h:1752
openvdb::v5_1::tools::mesh_to_volume_internal::RemoveSelfIntersectingSurface::mIndexTree
Int32TreeType *const mIndexTree
Definition: MeshToVolume.h:1812
openvdb::v5_1::tools::MeshToVoxelEdgeData::convert
void convert(const std::vector< Vec3s > &pointList, const std::vector< Vec4I > &polygonList)
Threaded method to extract voxel edge data, the closest intersection point and corresponding primitiv...
Definition: MeshToVolume.h:3999
openvdb::v5_1::tools::mesh_to_volume_internal::ExpandNarrowband::join
void join(ExpandNarrowband &rhs)
Definition: MeshToVolume.h:2429
openvdb::v5_1::tools::mesh_to_volume_internal::StashOriginAndStoreOffset::LeafNodeType
typename TreeType::LeafNodeType LeafNodeType
Definition: MeshToVolume.h:665
openvdb::v5_1::math::Transform::copy
Ptr copy() const
Definition: Transform.h:77
openvdb::v5_1::tools::mesh_to_volume_internal::Renormalize::LeafNodeType
typename TreeType::LeafNodeType LeafNodeType
Definition: MeshToVolume.h:2906
openvdb::v5_1::tools::mesh_to_volume_internal::ConstructVoxelMask::join
void join(ConstructVoxelMask &rhs)
Definition: MeshToVolume.h:2347
openvdb::v5_1::tools::mesh_to_volume_internal::ExpandNarrowband::LeafNodeType
typename TreeType::LeafNodeType LeafNodeType
Definition: MeshToVolume.h:2363
openvdb::v5_1::tools::mesh_to_volume_internal::ReleaseChildNodes::ReleaseChildNodes
ReleaseChildNodes(NodeType **nodes)
Definition: MeshToVolume.h:1822
openvdb::v5_1::tools::mesh_to_volume_internal::combineData
void combineData(DistTreeType &lhsDist, IndexTreeType &lhsIdx, DistTreeType &rhsDist, IndexTreeType &rhsIdx)
Definition: MeshToVolume.h:1895
openvdb::v5_1::tools::mesh_to_volume_internal::ComputeIntersectingVoxelSign::ComputeIntersectingVoxelSign
ComputeIntersectingVoxelSign(std::vector< LeafNodeType *> &distNodes, const TreeType &distTree, const Int32TreeType &indexTree, const MeshDataAdapter &mesh)
Definition: MeshToVolume.h:1379
openvdb::v5_1::tools::mesh_to_volume_internal::ExpandNarrowband::Fragment::operator<
bool operator<(const Fragment &rhs) const
Definition: MeshToVolume.h:2382
openvdb::v5_1::math::CoordBBox::expand
void expand(ValueType padding)
Pad this bounding box with the specified padding.
Definition: Coord.h:422
MeshDataAdapter
Convert polygonal meshes that consist of quads and/or triangles into signed or unsigned distance fiel...
openvdb::v5_1::tools::mesh_to_volume_internal::LeafNodeConnectivityTable::offsetsNextY
const size_t * offsetsNextY() const
Definition: MeshToVolume.h:822
openvdb::v5_1::tools::mesh_to_volume_internal::RemoveSelfIntersectingSurface::Comp
Definition: MeshToVolume.h:1764
openvdb::v5_1::tools::mesh_to_volume_internal::CombineLeafNodes::operator()
void operator()(const tbb::blocked_range< size_t > &range) const
Definition: MeshToVolume.h:605
openvdb::v5_1::tools::mesh_to_volume_internal::DiffLeafNodeMask::BoolTreeType
typename TreeType::template ValueConverter< bool >::Type BoolTreeType
Definition: MeshToVolume.h:2197
openvdb::v5_1::tools::mesh_to_volume_internal::SweepExteriorSign::SweepExteriorSign
SweepExteriorSign(Axis axis, const std::vector< size_t > &startNodeIndices, ConnectivityTable &connectivity)
Definition: MeshToVolume.h:845
openvdb::v5_1::tools::createLevelSetBox
GridType::Ptr createLevelSetBox(const math::BBox< VecType > &bbox, const openvdb::math::Transform &xform, typename VecType::ValueType halfWidth=LEVEL_SET_HALF_WIDTH)
Return a grid of type GridType containing a narrow-band level set representation of a box...
Definition: MeshToVolume.h:4169
openvdb::v5_1::tools::mesh_to_volume_internal::VoxelizePolygons::VoxelizePolygons
VoxelizePolygons(DataTable &dataTable, const MeshDataAdapter &mesh, Interrupter *interrupter=nullptr)
Definition: MeshToVolume.h:1984
openvdb::v5_1::tools::mesh_to_volume_internal::ExpandNarrowband::newDistNodes
std::vector< LeafNodeType * > & newDistNodes()
Definition: MeshToVolume.h:2572
openvdb::v5_1::math::BBox::min
const Vec3T & min() const
Return a const reference to the minimum point of this bounding box.
Definition: BBox.h:89
openvdb::v5_1::tools::MeshToVoxelEdgeData::EdgeData::EdgeData
EdgeData(float dist=1.0)
Definition: MeshToVolume.h:469
OPENVDB_THROW
#define OPENVDB_THROW(exception, message)
Definition: Exceptions.h:109
openvdb::v5_1::tools::mesh_to_volume_internal::ConstructVoxelMask::BoolTreeType
typename TreeType::template ValueConverter< bool >::Type BoolTreeType
Definition: MeshToVolume.h:2251
openvdb::v5_1::tools::mesh_to_volume_internal::FillArray::mValue
const ValueType mValue
Definition: MeshToVolume.h:1149
openvdb::v5_1::math::Coord::floor
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
openvdb::v5_1::tools::mesh_to_volume_internal::VoxelizationData::getNewPrimId
unsigned char getNewPrimId()
Definition: MeshToVolume.h:1958
openvdb::v5_1::tools::mesh_to_volume_internal::TransformValues::LeafNodeType
typename TreeType::LeafNodeType LeafNodeType
Definition: MeshToVolume.h:2791
ChangeBackground.h
Efficient multi-threaded replacement of the background values in tree.
openvdb::v5_1::tools::mesh_to_volume_internal::LeafNodeConnectivityTable::offsetsNextX
const size_t * offsetsNextX() const
Definition: MeshToVolume.h:819
openvdb::v5_1::tools::QuadAndTriangleDataAdapter::vertexCount
size_t vertexCount(size_t n) const
Vertex count for polygon n.
Definition: MeshToVolume.h:216
openvdb::v5_1::tools::mesh_to_volume_internal::StashOriginAndStoreOffset::mNodes
LeafNodeType **const mNodes
Definition: MeshToVolume.h:680
openvdb::v5_1::tools::mesh_to_volume_internal::AddNodes
Definition: MeshToVolume.h:2732
openvdb::v5_1::tools::mesh_to_volume_internal::Renormalize::Renormalize
Renormalize(const TreeType &tree, const std::vector< LeafNodeType *> &nodes, ValueType *buffer, ValueType voxelSize)
Definition: MeshToVolume.h:2909
openvdb::v5_1::tools::mesh_to_volume_internal::ExpandNarrowband::NodeMaskType
typename LeafNodeType::NodeMaskType NodeMaskType
Definition: MeshToVolume.h:2364
openvdb::v5_1::tools::mesh_to_volume_internal::TransformPoints::mPointsIn
PointType const *const mPointsIn
Definition: MeshToVolume.h:569
openvdb::v5_1::Vec3I
math::Vec3< Index32 > Vec3I
Definition: Types.h:80
openvdb::v5_1::tree::Tree::merge
void merge(Tree &other, MergePolicy=MERGE_ACTIVE_STATES)
Efficiently merge another tree into this tree using one of several schemes.
Definition: Tree.h:1864
openvdb::v5_1::tools::mesh_to_volume_internal::ConstructVoxelMask::LeafNodeType
typename TreeType::LeafNodeType LeafNodeType
Definition: MeshToVolume.h:2249
openvdb::v5_1::tools::mesh_to_volume_internal::VoxelizationData::distAcc
FloatTreeAcc distAcc
Definition: MeshToVolume.h:1950
openvdb::v5_1::tools::mesh_to_volume_internal::RestoreOrigin::mNodes
LeafNodeType **const mNodes
Definition: MeshToVolume.h:702
openvdb::v5_1::tools::mesh_to_volume_internal::ReleaseChildNodes::operator()
void operator()(const tbb::blocked_range< size_t > &range) const
Definition: MeshToVolume.h:1824
openvdb::v5_1::tools::mesh_to_volume_internal::ValidateIntersectingVoxels::ValueType
typename TreeType::ValueType ValueType
Definition: MeshToVolume.h:1708
openvdb::v5_1::tools::operator<<
std::ostream & operator<<(std::ostream &ostr, const MeshToVoxelEdgeData::EdgeData &rhs)
Definition: MeshToVolume.h:3661
openvdb::v5_1::math::Vec3
Definition: Mat.h:197
openvdb::v5_1::tools::QuadAndTriangleDataAdapter::getIndexSpacePoint
void getIndexSpacePoint(size_t n, size_t v, Vec3d &pos) const
Returns position pos in local grid index space for polygon n and vertex v.
Definition: MeshToVolume.h:222
openvdb::v5_1::tools::mesh_to_volume_internal::VoxelizationData::indexAcc
Int32TreeAcc indexAcc
Definition: MeshToVolume.h:1953
openvdb::v5_1::tools::mesh_to_volume_internal::ExpandNarrowband::newIndexNodes
std::vector< Int32LeafNodeType * > & newIndexNodes()
Definition: MeshToVolume.h:2575
openvdb::v5_1::tools::mesh_to_volume_internal::SeedFillExteriorSign::mChangedNodeMask
bool *const mChangedNodeMask
Definition: MeshToVolume.h:1132
openvdb::v5_1::tools::QuadAndTriangleDataAdapter::QuadAndTriangleDataAdapter
QuadAndTriangleDataAdapter(const PointType *pointArray, size_t pointArraySize, const PolygonType *polygonArray, size_t polygonArraySize)
Definition: MeshToVolume.h:203
openvdb::v5_1::tools::mesh_to_volume_internal::StealUniqueLeafNodes::LeafNodeType
typename TreeType::LeafNodeType LeafNodeType
Definition: MeshToVolume.h:1856
openvdb::v5_1::tools::mesh_to_volume_internal::VoxelizePolygons::DataTable
tbb::enumerable_thread_specific< typename VoxelizationDataType::Ptr > DataTable
Definition: MeshToVolume.h:1982
openvdb::v5_1::math::Pow2
Type Pow2(Type x)
Return x2.
Definition: Math.h:502
openvdb::v5_1::SharedPtr
std::shared_ptr< T > SharedPtr
Definition: Types.h:139
openvdb::v5_1::tree::TreeValueIteratorBase
Base class for tree-traversal iterators over tile and voxel values.
Definition: TreeIterator.h:665
openvdb::v5_1::tools::mesh_to_volume_internal::ConstructVoxelMask::ConstructVoxelMask
ConstructVoxelMask(BoolTreeType &maskTree, const TreeType &tree, std::vector< LeafNodeType *> &nodes)
Definition: MeshToVolume.h:2254
openvdb::v5_1::tools::MeshToVoxelEdgeData::GenEdgeData::GenEdgeData
GenEdgeData(const std::vector< Vec3s > &pointList, const std::vector< Vec4I > &polygonList)
Definition: MeshToVolume.h:3725
openvdb::v5_1::tools::mesh_to_volume_internal::LeafNodeConnectivityTable::nodes
const std::vector< LeafNodeType * > & nodes() const
Definition: MeshToVolume.h:816
openvdb::v5_1::tools::mesh_to_volume_internal::UnionValueMasks
Definition: MeshToVolume.h:2226
openvdb::v5_1::tools::mesh_to_volume_internal::InactivateValues::operator()
void operator()(const tbb::blocked_range< size_t > &range) const
Definition: MeshToVolume.h:2840
openvdb::v5_1::tools::mesh_to_volume_internal::StashOriginAndStoreOffset
Definition: MeshToVolume.h:663
openvdb::v5_1::tree::Tree::ValueConverter< Int32 >::Type
Tree< typename RootNodeType::template ValueConverter< Int32 >::Type > Type
Definition: Tree.h:224
openvdb::v5_1::tree::ValueAccessor::getNode
NodeType * getNode()
Return the cached node of type NodeType. [Mainly for internal use].
Definition: ValueAccessor.h:350
openvdb::v5_1::tools::mesh_to_volume_internal::SweepExteriorSign::Axis
Axis
Definition: MeshToVolume.h:839
openvdb::v5_1::tools::mesh_to_volume_internal::TransformPoints::mXform
math::Transform const *const mXform
Definition: MeshToVolume.h:571
openvdb::v5_1::tools::mesh_to_volume_internal::CombineLeafNodes::Int32TreeType
typename TreeType::template ValueConverter< Int32 >::Type Int32TreeType
Definition: MeshToVolume.h:591
openvdb::v5_1::tools::mesh_to_volume_internal::ComputeIntersectingVoxelSign::LeafNodeType
typename TreeType::LeafNodeType LeafNodeType
Definition: MeshToVolume.h:1370
openvdb::v5_1::tools::pruneLevelSet
void pruneLevelSet(TreeT &tree, bool threaded=true, size_t grainSize=1)
Reduce the memory footprint of a tree by replacing nodes whose values are all inactive with inactive ...
Definition: Prune.h:416
openvdb::v5_1::tools::mesh_to_volume_internal::DiffLeafNodeMask::AccessorType
typename tree::ValueAccessor< TreeType > AccessorType
Definition: MeshToVolume.h:2194
openvdb::v5_1::tools::mesh_to_volume_internal::ComputeIntersectingVoxelSign::LocalDataTable
tbb::enumerable_thread_specific< LocalData > LocalDataTable
Definition: MeshToVolume.h:1377
Prune.h
Defined various multi-threaded utility functions for trees.
openvdb::v5_1::tools::mesh_to_volume_internal::SeedPoints::mConnectivity
ConnectivityTable *const mConnectivity
Definition: MeshToVolume.h:1357
openvdb::v5_1::tools::mesh_to_volume_internal::AddNodes::mNodes
std::vector< LeafNodeType * > *const mNodes
Definition: MeshToVolume.h:2749
openvdb::v5_1::tools::mesh_to_volume_internal::VoxelizationData::primIdTree
UCharTreeType primIdTree
Definition: MeshToVolume.h:1955
openvdb::v5_1::tools::mesh_to_volume_internal::RemoveSelfIntersectingSurface::RemoveSelfIntersectingSurface
RemoveSelfIntersectingSurface(std::vector< LeafNodeType *> &nodes, TreeType &distTree, Int32TreeType &indexTree)
Definition: MeshToVolume.h:1766
openvdb::v5_1::tools::mesh_to_volume_internal::AddNodes::AddNodes
AddNodes(TreeType &tree, std::vector< LeafNodeType *> &nodes)
Definition: MeshToVolume.h:2735
openvdb::v5_1::tools::mesh_to_volume_internal::Tolerance::epsilon
static ValueType epsilon()
Definition: MeshToVolume.h:578
openvdb::v5_1::tools::mesh_to_volume_internal::MinCombine
Definition: MeshToVolume.h:2968
openvdb::v5_1::tools::mesh_to_volume_internal::RestoreOrigin
Definition: MeshToVolume.h:686
openvdb::v5_1::tools::mesh_to_volume_internal::FillArray::mArray
ValueType *const mArray
Definition: MeshToVolume.h:1148
openvdb::v5_1::tools::MeshToVoxelEdgeData::GenEdgeData::join
void join(GenEdgeData &rhs)
Definition: MeshToVolume.h:3762
openvdb::v5_1::tools::meshToLevelSet
GridType::Ptr meshToLevelSet(Interrupter &interrupter, const openvdb::math::Transform &xform, const std::vector< Vec3s > &points, const std::vector< Vec3I > &triangles, const std::vector< Vec4I > &quads, float halfWidth=float(LEVEL_SET_HALF_WIDTH))
Adds support for a interrupter callback used to cancel the conversion.
Definition: MeshToVolume.h:3581
openvdb::v5_1::tools::mesh_to_volume_internal::VoxelizationData::Int32TreeType
typename TreeType::template ValueConverter< Int32 >::Type Int32TreeType
Definition: MeshToVolume.h:1930
openvdb::v5_1::tools::QuadAndTriangleDataAdapter::pointCount
size_t pointCount() const
Definition: MeshToVolume.h:213
openvdb::v5_1::tree::Tree::clearAllAccessors
void clearAllAccessors()
Clear all registered accessors.
Definition: Tree.h:1547
openvdb::v5_1::tools::mesh_to_volume_internal::SyncVoxelMask::operator()
void operator()(const tbb::blocked_range< size_t > &range) const
Definition: MeshToVolume.h:1179
openvdb::v5_1::tree::ValueAccessor::addLeaf
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
openvdb::v5_1::tools::mesh_to_volume_internal::OffsetValues::OffsetValues
OffsetValues(std::vector< LeafNodeType *> &nodes, ValueType offset)
Definition: MeshToVolume.h:2877
openvdb::v5_1::tools::mesh_to_volume_internal::Renormalize::operator()
void operator()(const tbb::blocked_range< size_t > &range) const
Definition: MeshToVolume.h:2918
openvdb::v5_1::tools::mesh_to_volume_internal::ValidateIntersectingVoxels
Definition: MeshToVolume.h:1706
openvdb::v5_1::tools::mesh_to_volume_internal::SeedPoints::processZ
bool processZ(const size_t n, bool firstFace) const
Definition: MeshToVolume.h:1243
openvdb::v5_1::tools::MeshToVoxelEdgeData::GenEdgeData::run
void run(bool threaded=true)
Definition: MeshToVolume.h:3751
openvdb::v5_1::tools::mesh_to_volume_internal::ComputeIntersectingVoxelSign::MaskArray
std::unique_ptr< bool[]> MaskArray
Definition: MeshToVolume.h:1375
openvdb::v5_1::tools::mesh_to_volume_internal::ComputeNodeConnectivity::findNeighbourNode
size_t findNeighbourNode(tree::ValueAccessor< const TreeType > &acc, const Coord &start, const Coord &step) const
Definition: MeshToVolume.h:751
openvdb::v5_1::tools::mesh_to_volume_internal::ComputeIntersectingVoxelSign::Int32TreeType
typename TreeType::template ValueConverter< Int32 >::Type Int32TreeType
Definition: MeshToVolume.h:1371
openvdb::v5_1::tools::mesh_to_volume_internal::SeedFillExteriorSign::operator()
void operator()(const tbb::blocked_range< size_t > &range) const
Definition: MeshToVolume.h:1122
openvdb::v5_1::tools::mesh_to_volume_internal::ComputeNodeConnectivity::operator()
void operator()(const tbb::blocked_range< size_t > &range) const
Definition: MeshToVolume.h:728
OPENVDB_VERSION_NAME
#define OPENVDB_VERSION_NAME
The version namespace name for this library version.
Definition: version.h:136
openvdb::v5_1::tools::mesh_to_volume_internal::SeedPoints
Definition: MeshToVolume.h:1204
openvdb::v5_1::math::CoordBBox::min
const Coord & min() const
Definition: Coord.h:337
openvdb::v5_1::tools::QuadAndTriangleDataAdapter::polygonCount
size_t polygonCount() const
Definition: MeshToVolume.h:212
openvdb::v5_1::tree::ValueAccessor::probeConstLeaf
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
openvdb::v5_1::tools::mesh_to_volume_internal::SweepExteriorSign
Definition: MeshToVolume.h:835
openvdb::v5_1::math::Vec3::dot
T dot(const Vec3< T > &v) const
Dot product.
Definition: Vec3.h:216
openvdb::v5_1::tools::mesh_to_volume_internal::ComputeNodeConnectivity::ComputeNodeConnectivity
ComputeNodeConnectivity(const TreeType &tree, const Coord *coordinates, size_t *offsets, size_t numNodes, const CoordBBox &bbox)
Definition: MeshToVolume.h:713
openvdb::v5_1::tools::mesh_to_volume_internal::SyncVoxelMask::mChangedVoxelMask
bool *const mChangedVoxelMask
Definition: MeshToVolume.h:1199
openvdb::v5_1::math::Transform
Definition: Transform.h:66
openvdb::v5_1::tools::mesh_to_volume_internal::ValidateIntersectingVoxels::IsNegative::check
static bool check(const ValueType v)
Definition: MeshToVolume.h:1711
openvdb::v5_1::tools::DISABLE_NARROW_BAND_TRIMMING
Definition: MeshToVolume.h:106
openvdb::v5_1::tools::mesh_to_volume_internal::SyncVoxelMask
Definition: MeshToVolume.h:1165
openvdb::v5_1::tools::mesh_to_volume_internal::MinCombine::MinCombine
MinCombine(std::vector< LeafNodeType *> &nodes, const ValueType *buffer)
Definition: MeshToVolume.h:2973
openvdb::v5_1::math::Vec3d
Vec3< double > Vec3d
Definition: Vec3.h:679
openvdb::v5_1::tools::mesh_to_volume_internal::StashOriginAndStoreOffset::operator()
void operator()(const tbb::blocked_range< size_t > &range) const
Definition: MeshToVolume.h:672
SignedFloodFill.h
Propagate the signs of distance values from the active voxels in the narrow band to the inactive valu...
openvdb::v5_1::tools::MeshToVoxelEdgeData
Extracts and stores voxel edge intersection data from a mesh.
Definition: MeshToVolume.h:461
openvdb::v5_1::tools::mesh_to_volume_internal::OffsetValues
Definition: MeshToVolume.h:2872
openvdb::v5_1::math::CoordBBox::isInside
bool isInside(const Coord &xyz) const
Return true if point (x, y, z) is inside this bounding box.
Definition: Coord.h:404
openvdb::v5_1::tools::mesh_to_volume_internal::CombineLeafNodes::CombineLeafNodes
CombineLeafNodes(TreeType &lhsDistTree, Int32TreeType &lhsIdxTree, LeafNodeType **rhsDistNodes, Int32LeafNodeType **rhsIdxNodes)
Definition: MeshToVolume.h:596
openvdb::v5_1::tools::mesh_to_volume_internal::TransformValues::TransformValues
TransformValues(std::vector< LeafNodeType *> &nodes, ValueType voxelSize, bool unsignedDist)
Definition: MeshToVolume.h:2794
openvdb::v5_1::tools::mesh_to_volume_internal::RemoveSelfIntersectingSurface::Comp::check
static bool check(const ValueType v)
Definition: MeshToVolume.h:1764
openvdb::v5_1::tools::MeshToVoxelEdgeData::getEdgeData
void getEdgeData(Accessor &acc, const Coord &ijk, std::vector< Vec3d > &points, std::vector< Index32 > &primitives)
Returns intersection points with corresponding primitive indices for the given ijk voxel...
Definition: MeshToVolume.h:4012
openvdb::v5_1::tools::meshToSignedDistanceField
GridType::Ptr meshToSignedDistanceField(Interrupter &interrupter, const openvdb::math::Transform &xform, const std::vector< Vec3s > &points, const std::vector< Vec3I > &triangles, const std::vector< Vec4I > &quads, float exBandWidth, float inBandWidth)
Adds support for a interrupter callback used to cancel the conversion.
Definition: MeshToVolume.h:3612
openvdb::v5_1::tools::mesh_to_volume_internal::VoxelizationData
TBB body object to voxelize a mesh of triangles and/or quads into a collection of VDB grids...
Definition: MeshToVolume.h:1925
openvdb::v5_1::math::operator<
bool operator<(const Tuple< SIZE, T0 > &t0, const Tuple< SIZE, T1 > &t1)
Definition: Tuple.h:207
openvdb::v5_1::util::wasInterrupted
bool wasInterrupted(T *i, int percent=-1)
Definition: NullInterrupter.h:76
openvdb::v5_1::tools::mesh_to_volume_internal::ExpandNarrowband::Fragment
Definition: MeshToVolume.h:2370
openvdb::v5_1::tools::MeshToVoxelEdgeData::EdgeData::operator-
EdgeData operator-() const
Definition: MeshToVolume.h:484
openvdb::v5_1::Int32
int32_t Int32
Definition: Types.h:63
openvdb::v5_1::tools::traceExteriorBoundaries
void traceExteriorBoundaries(FloatTreeT &tree)
Traces the exterior voxel boundary of closed objects in the input volume tree. Exterior voxels are ma...
Definition: MeshToVolume.h:3009
openvdb::v5_1::tools::mesh_to_volume_internal::UnionValueMasks::UnionValueMasks
UnionValueMasks(std::vector< LeafNodeTypeA *> &nodesA, std::vector< LeafNodeTypeB *> &nodesB)
Definition: MeshToVolume.h:2228
openvdb::v5_1::tools::mesh_to_volume_internal::VoxelizePolygons::operator()
void operator()(const tbb::blocked_range< size_t > &range) const
Definition: MeshToVolume.h:1993
openvdb::v5_1::tools::mesh_to_volume_internal::TransformValues::operator()
void operator()(const tbb::blocked_range< size_t > &range) const
Definition: MeshToVolume.h:2802
openvdb::v5_1::math::Coord::minComponent
void minComponent(const Coord &other)
Perform a component-wise minimum with the other Coord.
Definition: Coord.h:202
openvdb::v5_1::tree::Tree::RootNodeType
_RootNodeType RootNodeType
Definition: Tree.h:209
openvdb::v5_1::tools::mesh_to_volume_internal::TransformValues::ValueType
typename TreeType::ValueType ValueType
Definition: MeshToVolume.h:2792
openvdb::v5_1::tree::ValueAccessor::probeValue
bool probeValue(const Coord &xyz, ValueType &value) const
Return the active state of the voxel as well as its value.
Definition: ValueAccessor.h:267
openvdb::v5_1::tools::mesh_to_volume_internal::AddNodes::LeafNodeType
typename TreeType::LeafNodeType LeafNodeType
Definition: MeshToVolume.h:2733
openvdb::v5_1::tools::mesh_to_volume_internal::ExpandNarrowband::Fragment::Fragment
Fragment(Int32 idx_, Int32 x_, Int32 y_, Int32 z_, ValueType dist_)
Definition: MeshToVolume.h:2377
openvdb
Definition: Exceptions.h:40
openvdb::v5_1::math::Z_AXIS
Definition: Math.h:859
openvdb::v5_1::tools::mesh_to_volume_internal::ExpandNarrowband::updatedIndexNodes
std::vector< Int32LeafNodeType * > & updatedIndexNodes()
Definition: MeshToVolume.h:2576
openvdb::v5_1::tools::mesh_to_volume_internal::DiffLeafNodeMask
Definition: MeshToVolume.h:2192
openvdb::v5_1::tools::mesh_to_volume_internal::seedFill
void seedFill(LeafNodeType &node)
Definition: MeshToVolume.h:965
openvdb::v5_1::tools::mesh_to_volume_internal::SweepExteriorSign::LeafNodeType
typename TreeType::LeafNodeType LeafNodeType
Definition: MeshToVolume.h:842
openvdb::v5_1::tools::mesh_to_volume_internal::SeedPoints::mChangedNodeMask
bool *const mChangedNodeMask
Definition: MeshToVolume.h:1358
openvdb::v5_1::tools::mesh_to_volume_internal::DiffLeafNodeMask::LeafNodeType
typename TreeType::LeafNodeType LeafNodeType
Definition: MeshToVolume.h:2195
openvdb::v5_1::tools::UNSIGNED_DISTANCE_FIELD
Definition: MeshToVolume.h:94
openvdb::v5_1::tree::ValueAccessor::setValueOn
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
openvdb::v5_1::tools::mesh_to_volume_internal::RestoreOrigin::mCoordinates
Coord const *const mCoordinates
Definition: MeshToVolume.h:703
openvdb::v5_1::tools::MeshToVoxelEdgeData::GenEdgeData::tree
TreeType & tree()
Definition: MeshToVolume.h:3694
openvdb::v5_1::tools::mesh_to_volume_internal::SeedFillExteriorSign::ValueType
typename TreeType::ValueType ValueType
Definition: MeshToVolume.h:1113
openvdb::v5_1::tools::QuadAndTriangleDataAdapter::QuadAndTriangleDataAdapter
QuadAndTriangleDataAdapter(const std::vector< PointType > &points, const std::vector< PolygonType > &polygons)
Definition: MeshToVolume.h:194
openvdb::v5_1::tools::mesh_to_volume_internal::DiffLeafNodeMask::BoolLeafNodeType
typename BoolTreeType::LeafNodeType BoolLeafNodeType
Definition: MeshToVolume.h:2198
openvdb::v5_1::tools::mesh_to_volume_internal::StealUniqueLeafNodes::StealUniqueLeafNodes
StealUniqueLeafNodes(TreeType &lhsTree, TreeType &rhsTree, std::vector< LeafNodeType *> &overlappingNodes)
Definition: MeshToVolume.h:1858
openvdb::v5_1::GRID_LEVEL_SET
Definition: Types.h:274
openvdb::v5_1::tools::mesh_to_volume_internal::MinCombine::ValueType
typename TreeType::ValueType ValueType
Definition: MeshToVolume.h:2971
openvdb::v5_1::tools::mesh_to_volume_internal::SyncVoxelMask::mNodes
LeafNodeType **const mNodes
Definition: MeshToVolume.h:1197
openvdb::v5_1::tools::mesh_to_volume_internal::SeedFillExteriorSign::SeedFillExteriorSign
SeedFillExteriorSign(std::vector< LeafNodeType *> &nodes, bool *changedNodeMask)
Definition: MeshToVolume.h:1116
openvdb::v5_1::math::Coord::offsetBy
Coord offsetBy(Int32 dx, Int32 dy, Int32 dz) const
Definition: Coord.h:118
openvdb::v5_1::tools::mesh_to_volume_internal::LeafNodeConnectivityTable::offsetsPrevZ
const size_t * offsetsPrevZ() const
Definition: MeshToVolume.h:826
openvdb::v5_1::tools::mesh_to_volume_internal::SeedPoints::processX
bool processX(const size_t n, bool firstFace) const
Definition: MeshToVolume.h:1319
openvdb::v5_1::tree::LeafIteratorBase
Base class for tree-traversal iterators over all leaf nodes (but not leaf voxels) ...
Definition: TreeIterator.h:1235
openvdb::v5_1::tools::mesh_to_volume_internal::SyncVoxelMask::SyncVoxelMask
SyncVoxelMask(std::vector< LeafNodeType *> &nodes, const bool *changedNodeMask, bool *changedVoxelMask)
Definition: MeshToVolume.h:1171
openvdb::v5_1::Vec4I
math::Vec4< Index32 > Vec4I
Definition: Types.h:95
openvdb::v5_1::tools::Abs
MeshToVoxelEdgeData::EdgeData Abs(const MeshToVoxelEdgeData::EdgeData &x)
Definition: MeshToVolume.h:3671
openvdb::v5_1::util::INVALID_IDX
OPENVDB_API const Index32 INVALID_IDX
openvdb::v5_1::tools::mesh_to_volume_internal::VoxelizePolygons
Definition: MeshToVolume.h:1977
openvdb::v5_1::math::Y_AXIS
Definition: Math.h:858
openvdb::v5_1::tools::mesh_to_volume_internal::ExpandNarrowband::operator()
void operator()(const tbb::blocked_range< size_t > &range)
Definition: MeshToVolume.h:2443
openvdb::v5_1::tools::mesh_to_volume_internal::SeedPoints::ValueType
typename TreeType::ValueType ValueType
Definition: MeshToVolume.h:1207
openvdb::v5_1::tools::mesh_to_volume_internal::SeedFillExteriorSign::LeafNodeType
typename TreeType::LeafNodeType LeafNodeType
Definition: MeshToVolume.h:1114
openvdb::v5_1::tools::mesh_to_volume_internal::VoxelizationData::UCharTreeType
typename TreeType::template ValueConverter< unsigned char >::Type UCharTreeType
Definition: MeshToVolume.h:1931
openvdb::v5_1::tree::ValueAccessor::probeLeaf
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
Util.h
openvdb::v5_1::tools::mesh_to_volume_internal::ExpandNarrowband::Int32LeafNodeType
typename Int32TreeType::LeafNodeType Int32LeafNodeType
Definition: MeshToVolume.h:2366
openvdb::v5_1::math::Coord::maxComponent
void maxComponent(const Coord &other)
Perform a component-wise maximum with the other Coord.
Definition: Coord.h:210
openvdb::v5_1::tools::mesh_to_volume_internal::RemoveSelfIntersectingSurface::operator()
void operator()(const tbb::blocked_range< size_t > &range) const
Definition: MeshToVolume.h:1774
openvdb::v5_1::tools::pruneInactive
void pruneInactive(TreeT &tree, bool threaded=true, size_t grainSize=1)
Reduce the memory footprint of a tree by replacing with background tiles any nodes whose values are a...
Definition: Prune.h:381
openvdb::v5_1::tools::mesh_to_volume_internal::DiffLeafNodeMask::DiffLeafNodeMask
DiffLeafNodeMask(const TreeType &rhsTree, std::vector< BoolLeafNodeType *> &lhsNodes)
Definition: MeshToVolume.h:2200
openvdb::v5_1::tools::mesh_to_volume_internal::StealUniqueLeafNodes
Definition: MeshToVolume.h:1854
openvdb::v5_1::tools::MeshToVoxelEdgeData::EdgeData::operator-
EdgeData operator-(const T &) const
Definition: MeshToVolume.h:483
openvdb::v5_1::tools::mesh_to_volume_internal::FillArray
Definition: MeshToVolume.h:1137
openvdb::v5_1::math::Sqrt
float Sqrt(float x)
Return the square root of a floating-point value.
Definition: Math.h:715
openvdb::v5_1::tools::mesh_to_volume_internal::RemoveSelfIntersectingSurface
Definition: MeshToVolume.h:1758
openvdb::v5_1::tools::mesh_to_volume_internal::RemoveSelfIntersectingSurface::ValueType
typename TreeType::ValueType ValueType
Definition: MeshToVolume.h:1760
openvdb::v5_1::tools::mesh_to_volume_internal::VoxelizationData::Ptr
std::unique_ptr< VoxelizationData > Ptr
Definition: MeshToVolume.h:1927
openvdb::v5_1::tools::mesh_to_volume_internal::UnionValueMasks::operator()
void operator()(const tbb::blocked_range< size_t > &range) const
Definition: MeshToVolume.h:2234
openvdb::v5_1::tools::mesh_to_volume_internal::SyncVoxelMask::ValueType
typename TreeType::ValueType ValueType
Definition: MeshToVolume.h:1168
openvdb::v5_1::tools::mesh_to_volume_internal::LeafNodeConnectivityTable
Definition: MeshToVolume.h:778
openvdb::v5_1::math::Vec3s
Vec3< float > Vec3s
Definition: Vec3.h:678
openvdb::v5_1::tools::MeshToVoxelEdgeData::EdgeData::mYDist
float mYDist
Definition: MeshToVolume.h:492
openvdb::v5_1::tools::mesh_to_volume_internal::ComputeIntersectingVoxelSign::Int32LeafNodeType
typename Int32TreeType::LeafNodeType Int32LeafNodeType
Definition: MeshToVolume.h:1372
openvdb::v5_1::tools::mesh_to_volume_internal::ReleaseChildNodes
Definition: MeshToVolume.h:1820
openvdb::v5_1::util::COORD_OFFSETS
OPENVDB_API const Coord COORD_OFFSETS[26]
coordinate offset table for neighboring voxels
openvdb::v5_1::tools::mesh_to_volume_internal::ExpandNarrowband
Definition: MeshToVolume.h:2360
openvdb::v5_1::tools::mesh_to_volume_internal::checkNeighbours
bool checkNeighbours(const Coord &ijk, AccessorType &acc, bool(&mask)[26])
Definition: MeshToVolume.h:1693
openvdb::v5_1::tools::mesh_to_volume_internal::InactivateValues::InactivateValues
InactivateValues(std::vector< LeafNodeType *> &nodes, ValueType exBandWidth, ValueType inBandWidth)
Definition: MeshToVolume.h:2832
openvdb::v5_1::tools::MeshToVoxelEdgeData::GenEdgeData::operator()
void operator()(const tbb::blocked_range< size_t > &range)
Definition: MeshToVolume.h:3824
openvdb::v5_1::tools::MeshToVoxelEdgeData::GenEdgeData
Definition: MeshToVolume.h:3680
openvdb::v5_1::tools::mesh_to_volume_internal::Renormalize
Definition: MeshToVolume.h:2904
openvdb::v5_1::tools::mesh_to_volume_internal::ConstructVoxelMask
Definition: MeshToVolume.h:2247
openvdb::v5_1::tools::mesh_to_volume_internal::InactivateValues
Definition: MeshToVolume.h:2827
openvdb::v5_1::tools::mesh_to_volume_internal::CombineLeafNodes::Int32LeafNodeType
typename Int32TreeType::LeafNodeType Int32LeafNodeType
Definition: MeshToVolume.h:594
openvdb::v5_1::tools::mesh_to_volume_internal::VoxelizationData::VoxelizationData
VoxelizationData()
Definition: MeshToVolume.h:1938
openvdb::v5_1::tools::mesh_to_volume_internal::RemoveSelfIntersectingSurface::LeafNodeType
typename TreeType::LeafNodeType LeafNodeType
Definition: MeshToVolume.h:1761
openvdb::v5_1::tools::mesh_to_volume_internal::ExpandNarrowband::BoolTreeType
typename TreeType::template ValueConverter< bool >::Type BoolTreeType
Definition: MeshToVolume.h:2367
openvdb::v5_1::tree::Tree::clear
void clear()
Remove all tiles from this tree and all nodes other than the root node.
Definition: Tree.h:1488
openvdb::v5_1::math::Vec3::cross
Vec3< T > cross(const Vec3< T > &v) const
Return the cross product of "this" vector and v;.
Definition: Vec3.h:245
openvdb::v5_1::tools::mesh_to_volume_internal::ExpandNarrowband::Fragment::idx
Int32 idx
Definition: MeshToVolume.h:2372
openvdb::v5_1::math::GodunovsNormSqrd
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
openvdb::v5_1::tools::mesh_to_volume_internal::maskNodeInternalNeighbours
void maskNodeInternalNeighbours(const Index pos, bool(&mask)[26])
Definition: MeshToVolume.h:1562
openvdb::v5_1::tools::mesh_to_volume_internal::ConstructVoxelMask::BoolLeafNodeType
typename BoolTreeType::LeafNodeType BoolLeafNodeType
Definition: MeshToVolume.h:2252
openvdb::v5_1::tools::mesh_to_volume_internal::TransformPoints::operator()
void operator()(const tbb::blocked_range< size_t > &range) const
Definition: MeshToVolume.h:549
openvdb::v5_1::math::BBox::max
const Vec3T & max() const
Return a const reference to the maximum point of this bounding box.
Definition: BBox.h:91
openvdb::v5_1::tools::composite::min
const std::enable_if<!VecTraits< T >::IsVec, T >::type & min(const T &a, const T &b)
Definition: Composite.h:129
openvdb::v5_1::tools::mesh_to_volume_internal::ComputeIntersectingVoxelSign
Definition: MeshToVolume.h:1367
openvdb::v5_1::tools::mesh_to_volume_internal::SyncVoxelMask::mChangedNodeMask
bool const *const mChangedNodeMask
Definition: MeshToVolume.h:1198
openvdb::v5_1::tools::mesh_to_volume_internal::InactivateValues::ValueType
typename TreeType::ValueType ValueType
Definition: MeshToVolume.h:2830
openvdb::v5_1::GRID_UNKNOWN
Definition: Types.h:273
openvdb::v5_1::tools::MeshToVoxelEdgeData::EdgeData::operator==
bool operator==(const EdgeData &rhs) const
Definition: MeshToVolume.h:487
openvdb::v5_1::tools::mesh_to_volume_internal::ExpandNarrowband::Fragment::Fragment
Fragment()
Definition: MeshToVolume.h:2375
openvdb::v5_1::math::Coord
Signed (x, y, z) 32-bit integer coordinates.
Definition: Coord.h:51
openvdb::v5_1::tools::mesh_to_volume_internal::RemoveSelfIntersectingSurface::mDistTree
TreeType *const mDistTree
Definition: MeshToVolume.h:1811
openvdb::v5_1::tools::mesh_to_volume_internal::releaseLeafNodes
void releaseLeafNodes(TreeType &tree)
Definition: MeshToVolume.h:1839
openvdb::v5_1::tools::DISABLE_RENORMALIZATION
Definition: MeshToVolume.h:102
openvdb::v5_1::tools::mesh_to_volume_internal::ComputeIntersectingVoxelSign::ValueType
typename TreeType::ValueType ValueType
Definition: MeshToVolume.h:1369
openvdb::v5_1::tools::mesh_to_volume_internal::ExpandNarrowband::newMaskTree
BoolTreeType & newMaskTree()
Definition: MeshToVolume.h:2570
openvdb::v5_1::tools::mesh_to_volume_internal::CombineLeafNodes
Definition: MeshToVolume.h:587
openvdb::v5_1::math::operator>
bool operator>(const Tuple< SIZE, T0 > &t0, const Tuple< SIZE, T1 > &t1)
Definition: Tuple.h:219
openvdb::v5_1::tools::mesh_to_volume_internal::SeedPoints::processY
bool processY(const size_t n, bool firstFace) const
Definition: MeshToVolume.h:1281
openvdb::v5_1::tools::mesh_to_volume_internal::RestoreOrigin::LeafNodeType
typename TreeType::LeafNodeType LeafNodeType
Definition: MeshToVolume.h:688
openvdb::v5_1::tools::mesh_to_volume_internal::SeedPoints::mNodeMask
bool *const mNodeMask
Definition: MeshToVolume.h:1359
openvdb::v5_1::tools::mesh_to_volume_internal::TransformPoints::mPointsOut
PointType *const mPointsOut
Definition: MeshToVolume.h:570
openvdb::v5_1::tools::mesh_to_volume_internal::ValidateIntersectingVoxels::mNodes
LeafNodeType **const mNodes
Definition: MeshToVolume.h:1753
openvdb::v5_1::tree::ValueAccessor< TreeType >
openvdb::v5_1::tools::mesh_to_volume_internal::ValidateIntersectingVoxels::operator()
void operator()(const tbb::blocked_range< size_t > &range) const
Definition: MeshToVolume.h:1719
openvdb::v5_1::tools::QuadAndTriangleDataAdapter
Contiguous quad and triangle data adapter class.
Definition: MeshToVolume.h:192
openvdb::v5_1::tools::mesh_to_volume_internal::InactivateValues::LeafNodeType
typename TreeType::LeafNodeType LeafNodeType
Definition: MeshToVolume.h:2829
openvdb::v5_1::tools::mesh_to_volume_internal::Renormalize::ValueType
typename TreeType::ValueType ValueType
Definition: MeshToVolume.h:2907
openvdb::v5_1::tools::MeshToVoxelEdgeData::EdgeData::mYPrim
Index32 mYPrim
Definition: MeshToVolume.h:493
openvdb::v5_1::tree::ValueAccessor::isValueOn
bool isValueOn(const Coord &xyz) const
Return the active state of the voxel at the given coordinates.
Definition: ValueAccessor.h:264
openvdb::v5_1::tools::mesh_to_volume_internal::fillArray
void fillArray(ValueType *array, const ValueType val, const size_t length)
Definition: MeshToVolume.h:1155
openvdb::v5_1::tools::mesh_to_volume_internal::LeafNodeConnectivityTable::offsetsNextZ
const size_t * offsetsNextZ() const
Definition: MeshToVolume.h:825
Proximity.h
openvdb::v5_1::math::X_AXIS
Definition: Math.h:857
openvdb::v5_1::tools::mesh_to_volume_internal::RemoveSelfIntersectingSurface::Int32TreeType
typename TreeType::template ValueConverter< Int32 >::Type Int32TreeType
Definition: MeshToVolume.h:1762
openvdb::v5_1::tree::Tree::probeLeaf
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
openvdb::v5_1::tools::mesh_to_volume_internal::ExpandNarrowband::Int32TreeType
typename TreeType::template ValueConverter< Int32 >::Type Int32TreeType
Definition: MeshToVolume.h:2365
openvdb::v5_1::Index
Index32 Index
Definition: Types.h:61
openvdb::v5_1::tools::mesh_to_volume_internal::ExpandNarrowband::Fragment::z
Int32 z
Definition: MeshToVolume.h:2372
openvdb::v5_1::tools::MeshToVoxelEdgeData::EdgeData::mZPrim
Index32 mZPrim
Definition: MeshToVolume.h:493
OPENVDB_USE_VERSION_NAMESPACE
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h:188
openvdb::v5_1::tools::mesh_to_volume_internal::RestoreOrigin::RestoreOrigin
RestoreOrigin(std::vector< LeafNodeType *> &nodes, const Coord *coordinates)
Definition: MeshToVolume.h:690
openvdb::v5_1::math::closestPointOnTriangleToPoint
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...
openvdb::v5_1::tools::mesh_to_volume_internal::LeafNodeConnectivityTable::offsetsPrevX
const size_t * offsetsPrevX() const
Definition: MeshToVolume.h:820
openvdb::v5_1::tools::DISABLE_INTERSECTING_VOXEL_REMOVAL
Definition: MeshToVolume.h:98
openvdb::v5_1::tools::mesh_to_volume_internal::SeedPoints::SeedPoints
SeedPoints(ConnectivityTable &connectivity, bool *changedNodeMask, bool *nodeMask, bool *changedVoxelMask)
Definition: MeshToVolume.h:1211
openvdb::v5_1::tools::mesh_to_volume_internal::VoxelizationData::ValueType
typename TreeType::ValueType ValueType
Definition: MeshToVolume.h:1928
openvdb::v5_1::tools::mesh_to_volume_internal::LeafNodeConnectivityTable::LeafNodeType
typename TreeType::LeafNodeType LeafNodeType
Definition: MeshToVolume.h:782
openvdb::v5_1::math::Transform::voxelSize
Vec3d voxelSize() const
Return the size of a voxel using the linear component of the map.
Definition: Transform.h:120
openvdb::v5_1::tools::mesh_to_volume_internal::FillArray::FillArray
FillArray(ValueType *array, const ValueType v)
Definition: MeshToVolume.h:1139
openvdb::v5_1::tools::mesh_to_volume_internal::StashOriginAndStoreOffset::mCoordinates
Coord *const mCoordinates
Definition: MeshToVolume.h:681
openvdb::v5_1::tools::mesh_to_volume_internal::ReleaseChildNodes::mNodes
NodeType **const mNodes
Definition: MeshToVolume.h:1833
openvdb::v5_1::tools::composite::max
const std::enable_if<!VecTraits< T >::IsVec, T >::type & max(const T &a, const T &b)
Definition: Composite.h:133
openvdb::v5_1::tools::mesh_to_volume_internal::SweepExteriorSign::traceVoxelLine
bool traceVoxelLine(LeafNodeType &node, Index pos, Index step) const
Definition: MeshToVolume.h:929
openvdb::v5_1::tools::MeshToVoxelEdgeData::getAccessor
Accessor getAccessor()
Definition: MeshToVolume.h:523
openvdb::v5_1::tools::mesh_to_volume_internal::SeedPoints::mChangedVoxelMask
bool *const mChangedVoxelMask
Definition: MeshToVolume.h:1360
openvdb::v5_1::tools::mesh_to_volume_internal::SweepExteriorSign::operator()
void operator()(const tbb::blocked_range< size_t > &range) const
Definition: MeshToVolume.h:853
openvdb::v5_1::tools::mesh_to_volume_internal::ExpandNarrowband::Fragment::dist
ValueType dist
Definition: MeshToVolume.h:2373
openvdb::v5_1::tools::mesh_to_volume_internal::StealUniqueLeafNodes::operator()
void operator()() const
Definition: MeshToVolume.h:1866
openvdb::v5_1::tools::mesh_to_volume_internal::StashOriginAndStoreOffset::StashOriginAndStoreOffset
StashOriginAndStoreOffset(std::vector< LeafNodeType *> &nodes, Coord *coordinates)
Definition: MeshToVolume.h:667
openvdb::v5_1::tools::changeBackground
void changeBackground(TreeOrLeafManagerT &tree, const typename TreeOrLeafManagerT::ValueType &background, bool threaded=true, size_t grainSize=32)
Replace the background value in all the nodes of a tree.
Definition: ChangeBackground.h:230
openvdb::v5_1::tools::mesh_to_volume_internal::SeedPoints::operator()
void operator()(const tbb::blocked_range< size_t > &range) const
Definition: MeshToVolume.h:1220
openvdb::v5_1::tools::mesh_to_volume_internal::ConstructVoxelMask::operator()
void operator()(const tbb::blocked_range< size_t > &range)
Definition: MeshToVolume.h:2271
openvdb::v5_1::tools::mesh_to_volume_internal::VoxelizationData::primIdAcc
UCharTreeAcc primIdAcc
Definition: MeshToVolume.h:1956
openvdb::v5_1::tools::MeshToVolumeFlags
MeshToVolumeFlags
Mesh to volume conversion flags.
Definition: MeshToVolume.h:88
openvdb::v5_1::tree::Tree
Definition: Tree.h:203
openvdb::v5_1::tools::mesh_to_volume_internal::AddNodes::operator()
void operator()() const
Definition: MeshToVolume.h:2740
openvdb::v5_1::math::BBox
Axis-aligned bounding box.
Definition: BBox.h:50
openvdb::v5_1::LEVEL_SET_HALF_WIDTH
static const Real LEVEL_SET_HALF_WIDTH
Definition: Types.h:280
openvdb::v5_1::tools::mesh_to_volume_internal::scanFill
bool scanFill(LeafNodeType &node)
Definition: MeshToVolume.h:1041
openvdb::v5_1::tools::mesh_to_volume_internal::SyncVoxelMask::LeafNodeType
typename TreeType::LeafNodeType LeafNodeType
Definition: MeshToVolume.h:1169
openvdb::v5_1::tools::mesh_to_volume_internal::LeafNodeConnectivityTable::size
size_t size() const
Definition: MeshToVolume.h:813
openvdb::v5_1::tools::mesh_to_volume_internal::TransformValues
Definition: MeshToVolume.h:2789
openvdb::v5_1::tools::mesh_to_volume_internal::TransformPoints
Definition: MeshToVolume.h:541
openvdb::v5_1::tools::mesh_to_volume_internal::ExpandNarrowband::BoolLeafNodeType
typename BoolTreeType::LeafNodeType BoolLeafNodeType
Definition: MeshToVolume.h:2368
openvdb::v5_1::tools::MeshToVoxelEdgeData::EdgeData::mXDist
float mXDist
Definition: MeshToVolume.h:492
openvdb::v5_1::tools::mesh_to_volume_internal::FillArray::operator()
void operator()(const tbb::blocked_range< size_t > &range) const
Definition: MeshToVolume.h:1141
openvdb::v5_1::tools::mesh_to_volume_internal::MinCombine::LeafNodeType
typename TreeType::LeafNodeType LeafNodeType
Definition: MeshToVolume.h:2970
PointArray
Partitions points into BucketLog2Dim aligned buckets using a parallel radix-based sorting algorithm...
openvdb::v5_1::tools::mesh_to_volume_internal::ExpandNarrowband::updatedDistNodes
std::vector< LeafNodeType * > & updatedDistNodes()
Definition: MeshToVolume.h:2573
openvdb::v5_1::tools::MeshToVoxelEdgeData::EdgeData::operator+
EdgeData operator+(const T &) const
Definition: MeshToVolume.h:482
openvdb::v5_1::tools::meshToUnsignedDistanceField
GridType::Ptr meshToUnsignedDistanceField(Interrupter &interrupter, const openvdb::math::Transform &xform, const std::vector< Vec3s > &points, const std::vector< Vec3I > &triangles, const std::vector< Vec4I > &quads, float bandWidth)
Adds support for a interrupter callback used to cancel the conversion.
Definition: MeshToVolume.h:3643
openvdb::v5_1::tools::mesh_to_volume_internal::ExpandNarrowband::ExpandNarrowband
ExpandNarrowband(const ExpandNarrowband &rhs, tbb::split)
Definition: MeshToVolume.h:2412
openvdb::v5_1::tree::ValueAccessor::clear
void clear() override
Remove all nodes from this cache, then reinsert the root node.
Definition: ValueAccessor.h:439
openvdb::v5_1::tools::mesh_to_volume_internal::ValidateIntersectingVoxels::IsNegative
Definition: MeshToVolume.h:1711
Platform.h
openvdb::v5_1::tools::mesh_to_volume_internal::ComputeNodeConnectivity
Definition: MeshToVolume.h:708
openvdb::v5_1::util::NullInterrupter
Dummy NOOP interrupter class defining interface.
Definition: NullInterrupter.h:52
openvdb::v5_1::tools::mesh_to_volume_internal::LeafNodeConnectivityTable::nodes
std::vector< LeafNodeType * > & nodes()
Definition: MeshToVolume.h:815
openvdb::v5_1::tools::mesh_to_volume_internal::RestoreOrigin::operator()
void operator()(const tbb::blocked_range< size_t > &range) const
Definition: MeshToVolume.h:695