31 #ifndef OPENVDB_TOOLS_VOLUME_TO_SPHERES_HAS_BEEN_INCLUDED
32 #define OPENVDB_TOOLS_VOLUME_TO_SPHERES_HAS_BEEN_INCLUDED
34 #include <openvdb/tree/ValueAccessor.h>
35 #include <openvdb/tree/LeafManager.h>
36 #include <openvdb/tools/Morphology.h>
38 #include <openvdb/tools/PointScatter.h>
39 #include <openvdb/tools/LevelSetUtil.h>
40 #include <openvdb/tools/VolumeToMesh.h>
42 #include <boost/scoped_array.hpp>
43 #include <boost/scoped_ptr.hpp>
44 #include <tbb/blocked_range.h>
45 #include <tbb/parallel_for.h>
46 #include <tbb/parallel_reduce.h>
89 template<
typename Gr
idT,
typename InterrupterT>
93 std::vector<openvdb::Vec4s>& spheres,
95 bool overlapping =
false,
96 float minRadius = 1.0,
99 int instanceCount = 10000,
100 InterrupterT* interrupter = NULL);
105 template<
typename Gr
idT>
109 std::vector<openvdb::Vec4s>& spheres,
111 bool overlapping =
false,
112 float minRadius = 1.0,
114 float isovalue = 0.0,
115 int instanceCount = 10000)
117 fillWithSpheres<GridT, util::NullInterrupter>(grid, spheres,
118 maxSphereCount, overlapping, minRadius, maxRadius, isovalue, instanceCount);
128 template<
typename Gr
idT>
132 typedef typename GridT::TreeType
TreeT;
133 typedef typename TreeT::template ValueConverter<int>::Type
IntTreeT;
134 typedef typename TreeT::template ValueConverter<Int16>::Type
Int16TreeT;
151 template<
typename InterrupterT>
152 void initialize(
const GridT& grid,
float isovalue = 0.0, InterrupterT* interrupter = NULL);
157 void initialize(
const GridT& grid,
float isovalue = 0.0);
167 bool search(
const std::vector<Vec3R>& points, std::vector<float>& distances);
177 bool searchAndReplace(std::vector<Vec3R>& points, std::vector<float>& distances);
187 typedef typename IntTreeT::LeafNodeType IntLeafT;
188 typedef std::pair<size_t, size_t> IndexRange;
191 std::vector<Vec4R> mLeafBoundingSpheres, mNodeBoundingSpheres;
192 std::vector<IndexRange> mLeafRanges;
193 std::vector<const IntLeafT*> mLeafNodes;
195 size_t mPointListSize, mMaxNodeLeafs;
197 typename IntTreeT::Ptr mIdxTreePt;
198 typename Int16TreeT::Ptr mSignTreePt;
200 bool search(std::vector<Vec3R>&, std::vector<float>&,
bool transformPoints);
223 mPoints.push_back(pos);
226 std::vector<Vec3R>& mPoints;
230 template<
typename IntLeafT>
235 LeafBS(std::vector<Vec4R>& leafBoundingSpheres,
236 const std::vector<const IntLeafT*>& leafNodes,
240 void run(
bool threaded =
true);
243 void operator()(
const tbb::blocked_range<size_t>&)
const;
246 std::vector<Vec4R>& mLeafBoundingSpheres;
247 const std::vector<const IntLeafT*>& mLeafNodes;
252 template<
typename IntLeafT>
254 std::vector<Vec4R>& leafBoundingSpheres,
255 const std::vector<const IntLeafT*>& leafNodes,
258 : mLeafBoundingSpheres(leafBoundingSpheres)
259 , mLeafNodes(leafNodes)
260 , mTransform(transform)
261 , mSurfacePointList(surfacePointList)
265 template<
typename IntLeafT>
270 tbb::parallel_for(tbb::blocked_range<size_t>(0, mLeafNodes.size()), *
this);
272 (*this)(tbb::blocked_range<size_t>(0, mLeafNodes.size()));
276 template<
typename IntLeafT>
280 typename IntLeafT::ValueOnCIter iter;
283 for (
size_t n = range.begin(); n != range.end(); ++n) {
290 for (iter = mLeafNodes[n]->cbeginValueOn(); iter; ++iter) {
291 avg += mSurfacePointList[iter.getValue()];
295 if (count > 1) avg *= float(1.0 /
double(count));
299 for (iter = mLeafNodes[n]->cbeginValueOn(); iter; ++iter) {
300 float tmpDist = (mSurfacePointList[iter.getValue()] - avg).lengthSqr();
301 if (tmpDist > maxDist) maxDist = tmpDist;
304 Vec4R& sphere = mLeafBoundingSpheres[n];
309 sphere[3] = maxDist * 2.0;
319 NodeBS(std::vector<Vec4R>& nodeBoundingSpheres,
320 const std::vector<IndexRange>& leafRanges,
321 const std::vector<Vec4R>& leafBoundingSpheres);
323 void run(
bool threaded =
true);
326 void operator()(
const tbb::blocked_range<size_t>&)
const;
329 std::vector<Vec4R>& mNodeBoundingSpheres;
330 const std::vector<IndexRange>& mLeafRanges;
331 const std::vector<Vec4R>& mLeafBoundingSpheres;
336 const std::vector<IndexRange>& leafRanges,
337 const std::vector<Vec4R>& leafBoundingSpheres)
338 : mNodeBoundingSpheres(nodeBoundingSpheres)
339 , mLeafRanges(leafRanges)
340 , mLeafBoundingSpheres(leafBoundingSpheres)
348 tbb::parallel_for(tbb::blocked_range<size_t>(0, mLeafRanges.size()), *
this);
350 (*this)(tbb::blocked_range<size_t>(0, mLeafRanges.size()));
359 for (
size_t n = range.begin(); n != range.end(); ++n) {
365 int count = mLeafRanges[n].second - mLeafRanges[n].first;
367 for (
size_t i = mLeafRanges[n].first; i < mLeafRanges[n].second; ++i) {
368 avg[0] += mLeafBoundingSpheres[i][0];
369 avg[1] += mLeafBoundingSpheres[i][1];
370 avg[2] += mLeafBoundingSpheres[i][2];
373 if (count > 1) avg *= float(1.0 /
double(count));
378 for (
size_t i = mLeafRanges[n].first; i < mLeafRanges[n].second; ++i) {
379 pos[0] = mLeafBoundingSpheres[i][0];
380 pos[1] = mLeafBoundingSpheres[i][1];
381 pos[2] = mLeafBoundingSpheres[i][2];
383 float tmpDist = (pos - avg).lengthSqr() + mLeafBoundingSpheres[i][3];
384 if (tmpDist > maxDist) maxDist = tmpDist;
387 Vec4R& sphere = mNodeBoundingSpheres[n];
392 sphere[3] = maxDist * 2.0;
401 template<
typename IntLeafT>
408 std::vector<Vec3R>& instancePoints,
409 std::vector<float>& instanceDistances,
411 const std::vector<const IntLeafT*>& leafNodes,
412 const std::vector<IndexRange>& leafRanges,
413 const std::vector<Vec4R>& leafBoundingSpheres,
414 const std::vector<Vec4R>& nodeBoundingSpheres,
416 bool transformPoints =
false);
419 void run(
bool threaded =
true);
422 void operator()(
const tbb::blocked_range<size_t>&)
const;
426 void evalLeaf(
size_t index,
const IntLeafT& leaf)
const;
427 void evalNode(
size_t pointIndex,
size_t nodeIndex)
const;
430 std::vector<Vec3R>& mInstancePoints;
431 std::vector<float>& mInstanceDistances;
435 const std::vector<const IntLeafT*>& mLeafNodes;
436 const std::vector<IndexRange>& mLeafRanges;
437 const std::vector<Vec4R>& mLeafBoundingSpheres;
438 const std::vector<Vec4R>& mNodeBoundingSpheres;
440 std::vector<float> mLeafDistances, mNodeDistances;
442 const bool mTransformPoints;
443 size_t mClosestPointIndex;
447 template<
typename IntLeafT>
449 std::vector<Vec3R>& instancePoints,
450 std::vector<float>& instanceDistances,
452 const std::vector<const IntLeafT*>& leafNodes,
453 const std::vector<IndexRange>& leafRanges,
454 const std::vector<Vec4R>& leafBoundingSpheres,
455 const std::vector<Vec4R>& nodeBoundingSpheres,
457 bool transformPoints)
458 : mInstancePoints(instancePoints)
459 , mInstanceDistances(instanceDistances)
460 , mSurfacePointList(surfacePointList)
461 , mLeafNodes(leafNodes)
462 , mLeafRanges(leafRanges)
463 , mLeafBoundingSpheres(leafBoundingSpheres)
464 , mNodeBoundingSpheres(nodeBoundingSpheres)
465 , mLeafDistances(maxNodeLeafs, 0.0)
466 , mNodeDistances(leafRanges.size(), 0.0)
467 , mTransformPoints(transformPoints)
468 , mClosestPointIndex(0)
473 template<
typename IntLeafT>
478 tbb::parallel_for(tbb::blocked_range<size_t>(0, mInstancePoints.size()), *
this);
480 (*this)(tbb::blocked_range<size_t>(0, mInstancePoints.size()));
484 template<
typename IntLeafT>
488 typename IntLeafT::ValueOnCIter iter;
489 const Vec3s center = mInstancePoints[index];
490 size_t& closestPointIndex =
const_cast<size_t&
>(mClosestPointIndex);
492 for (iter = leaf.cbeginValueOn(); iter; ++iter) {
494 const Vec3s& point = mSurfacePointList[iter.getValue()];
495 float tmpDist = (point - center).lengthSqr();
497 if (tmpDist < mInstanceDistances[index]) {
498 mInstanceDistances[index] = tmpDist;
499 closestPointIndex = iter.getValue();
505 template<
typename IntLeafT>
507 ClosestPointDist<IntLeafT>::evalNode(
size_t pointIndex,
size_t nodeIndex)
const
509 const Vec3R& pos = mInstancePoints[pointIndex];
510 float minDist = mInstanceDistances[pointIndex];
511 size_t minDistIdx = 0;
513 bool updatedDist =
false;
515 for (
size_t i = mLeafRanges[nodeIndex].first, n = 0; i < mLeafRanges[nodeIndex].second; ++i, ++n) {
517 float& distToLeaf =
const_cast<float&
>(mLeafDistances[n]);
519 center[0] = mLeafBoundingSpheres[i][0];
520 center[1] = mLeafBoundingSpheres[i][1];
521 center[2] = mLeafBoundingSpheres[i][2];
523 distToLeaf = (pos - center).lengthSqr() - mLeafBoundingSpheres[i][3];
525 if (distToLeaf < minDist) {
526 minDist = distToLeaf;
532 if (!updatedDist)
return;
534 evalLeaf(pointIndex, *mLeafNodes[minDistIdx]);
536 for (
size_t i = mLeafRanges[nodeIndex].first, n = 0; i < mLeafRanges[nodeIndex].second; ++i, ++n) {
537 if (mLeafDistances[n] < mInstanceDistances[pointIndex] && i != minDistIdx) {
538 evalLeaf(pointIndex, *mLeafNodes[i]);
544 template<
typename IntLeafT>
549 for (
size_t n = range.begin(); n != range.end(); ++n) {
551 const Vec3R& pos = mInstancePoints[n];
552 float minDist = mInstanceDistances[n];
553 size_t minDistIdx = 0;
555 for (
size_t i = 0, I = mNodeDistances.size(); i < I; ++i) {
556 float& distToNode =
const_cast<float&
>(mNodeDistances[i]);
558 center[0] = mNodeBoundingSpheres[i][0];
559 center[1] = mNodeBoundingSpheres[i][1];
560 center[2] = mNodeBoundingSpheres[i][2];
562 distToNode = (pos - center).lengthSqr() - mNodeBoundingSpheres[i][3];
564 if (distToNode < minDist) {
565 minDist = distToNode;
570 evalNode(n, minDistIdx);
572 for (
size_t i = 0, I = mNodeDistances.size(); i < I; ++i) {
573 if (mNodeDistances[i] < mInstanceDistances[n] && i != minDistIdx) {
578 mInstanceDistances[n] = std::sqrt(mInstanceDistances[n]);
580 if (mTransformPoints) mInstancePoints[n] = mSurfacePointList[mClosestPointIndex];
590 const std::vector<Vec3R>& points,
591 std::vector<float>& distances,
592 std::vector<unsigned char>& mask,
596 int index()
const {
return mIndex; };
598 void run(
bool threaded =
true);
602 void operator()(
const tbb::blocked_range<size_t>& range);
605 if (rhs.mRadius > mRadius) {
606 mRadius = rhs.mRadius;
613 const Vec4s& mSphere;
614 const std::vector<Vec3R>& mPoints;
616 std::vector<float>& mDistances;
617 std::vector<unsigned char>& mMask;
627 const std::vector<Vec3R>& points,
628 std::vector<float>& distances,
629 std::vector<unsigned char>& mask,
633 , mDistances(distances)
635 , mOverlapping(overlapping)
643 : mSphere(rhs.mSphere)
644 , mPoints(rhs.mPoints)
645 , mDistances(rhs.mDistances)
647 , mOverlapping(rhs.mOverlapping)
648 , mRadius(rhs.mRadius)
657 tbb::parallel_reduce(tbb::blocked_range<size_t>(0, mPoints.size()), *
this);
659 (*this)(tbb::blocked_range<size_t>(0, mPoints.size()));
667 for (
size_t n = range.begin(); n != range.end(); ++n) {
668 if (mMask[n])
continue;
670 pos.
x() = float(mPoints[n].x()) - mSphere[0];
671 pos.y() = float(mPoints[n].y()) - mSphere[1];
672 pos.z() = float(mPoints[n].z()) - mSphere[2];
674 float dist = pos.length();
676 if (dist < mSphere[3]) {
682 mDistances[n] =
std::min(mDistances[n], (dist - mSphere[3]));
685 if (mDistances[n] > mRadius) {
686 mRadius = mDistances[n];
699 template<
typename Gr
idT,
typename InterrupterT>
703 std::vector<openvdb::Vec4s>& spheres,
710 InterrupterT* interrupter)
713 spheres.reserve(maxSphereCount);
715 const bool addNBPoints = grid.activeVoxelCount() < 10000;
716 int instances =
std::max(instanceCount, maxSphereCount);
718 typedef typename GridT::TreeType TreeT;
719 typedef typename GridT::ValueType ValueT;
721 typedef typename TreeT::template ValueConverter<bool>::Type BoolTreeT;
722 typedef typename TreeT::template ValueConverter<int>::Type IntTreeT;
723 typedef typename TreeT::template ValueConverter<Int16>::Type Int16TreeT;
730 typedef boost::mt11213b RandGen;
733 const TreeT& tree = grid.tree();
736 std::vector<Vec3R> instancePoints;
746 interiorMaskPtr->
tree().topologyUnion(tree);
749 if (interrupter && interrupter->wasInterrupted())
return;
753 instancePoints.reserve(instances);
757 scatter(ptnAcc, (addNBPoints ? (instances / 2) : instances), mtRand, interrupter);
759 scatter(*interiorMaskPtr);
762 if (interrupter && interrupter->wasInterrupted())
return;
764 std::vector<float> instanceRadius;
770 if (instancePoints.size() < instances) {
771 const Int16TreeT& signTree = csp.
signTree();
772 typename Int16TreeT::LeafNodeType::ValueOnCIter it;
773 typename Int16TreeT::LeafCIter leafIt = signTree.cbeginLeaf();
775 for (; leafIt; ++leafIt) {
776 for (it = leafIt->cbeginValueOn(); it; ++it) {
777 const int flags = it.getValue();
778 if (!(0xE00 & flags) && (flags & 0x100)) {
779 instancePoints.push_back(transform.
indexToWorld(it.getCoord()));
782 if (instancePoints.size() == instances)
break;
784 if (instancePoints.size() == instances)
break;
789 if (interrupter && interrupter->wasInterrupted())
return;
791 if (!csp.
search(instancePoints, instanceRadius))
return;
793 std::vector<unsigned char> instanceMask(instancePoints.size(), 0);
794 float largestRadius = 0.0;
795 int largestRadiusIdx = 0;
797 for (
size_t n = 0, N = instancePoints.size(); n < N; ++n) {
798 if (instanceRadius[n] > largestRadius) {
799 largestRadius = instanceRadius[n];
800 largestRadiusIdx = n;
809 for (
size_t s = 0, S =
std::min(
size_t(maxSphereCount), instancePoints.size()); s < S; ++s) {
811 if (interrupter && interrupter->wasInterrupted())
return;
813 largestRadius =
std::min(maxRadius, largestRadius);
815 if (s != 0 && largestRadius < minRadius)
break;
817 sphere[0] = float(instancePoints[largestRadiusIdx].x());
818 sphere[1] = float(instancePoints[largestRadiusIdx].y());
819 sphere[2] = float(instancePoints[largestRadiusIdx].z());
820 sphere[3] = largestRadius;
822 spheres.push_back(sphere);
823 instanceMask[largestRadiusIdx] = 1;
828 largestRadius = op.
radius();
829 largestRadiusIdx = op.
index();
836 template<
typename Gr
idT>
838 : mIsInitialized(false)
839 , mLeafBoundingSpheres(0)
840 , mNodeBoundingSpheres(0)
843 , mSurfacePointList()
851 template<
typename Gr
idT>
855 initialize<GridT, util::NullInterrupter>(grid, isovalue, NULL);
859 template<
typename Gr
idT>
860 template<
typename InterrupterT>
863 const GridT& grid,
float isovalue, InterrupterT* interrupter)
865 mIsInitialized =
false;
869 typedef typename GridT::ValueType ValueT;
877 LeafManagerT leafs(tree);
879 signDataOp(tree, leafs, ValueT(isovalue));
883 mSignTreePt = signDataOp.
signTree();
884 mIdxTreePt = signDataOp.
idxTree();
887 if (interrupter && interrupter->wasInterrupted())
return;
889 Int16LeafManagerT signLeafs(*mSignTreePt);
891 std::vector<size_t> regions(signLeafs.leafCount(), 0);
895 for (
size_t tmp = 0, n = 0, N = regions.size(); n < N; ++n) {
897 regions[n] = mPointListSize;
898 mPointListSize += tmp;
901 if (mPointListSize == 0)
return;
903 mSurfacePointList.reset(
new Vec3s[mPointListSize]);
906 pointOp(signLeafs, tree, *mIdxTreePt, mSurfacePointList, regions, transform, isovalue);
910 mIdxTreePt->topologyUnion(*mSignTreePt);
913 if (interrupter && interrupter->wasInterrupted())
return;
916 CoordBBox bbox = grid.evalActiveVoxelBoundingBox();
921 dim[0] = std::abs(dim[0]);
922 dim[1] = std::abs(dim[1]);
923 dim[2] = std::abs(dim[2]);
926 mMaxRadiusSqr *= 0.51;
927 mMaxRadiusSqr *= mMaxRadiusSqr;
930 IntLeafManagerT idxLeafs(*mIdxTreePt);
933 typedef typename IntTreeT::RootNodeType IntRootNodeT;
934 typedef typename IntRootNodeT::NodeChainType IntNodeChainT;
935 BOOST_STATIC_ASSERT(boost::mpl::size<IntNodeChainT>::value > 1);
936 typedef typename boost::mpl::at<IntNodeChainT, boost::mpl::int_<1> >::type IntInternalNodeT;
939 typename IntTreeT::NodeCIter nIt = mIdxTreePt->cbeginNode();
940 nIt.setMinDepth(IntTreeT::NodeCIter::LEAF_DEPTH - 1);
941 nIt.setMaxDepth(IntTreeT::NodeCIter::LEAF_DEPTH - 1);
943 std::vector<const IntInternalNodeT*> internalNodes;
945 const IntInternalNodeT* node = NULL;
948 if (node) internalNodes.push_back(node);
951 std::vector<IndexRange>().swap(mLeafRanges);
952 mLeafRanges.resize(internalNodes.size());
954 std::vector<const IntLeafT*>().swap(mLeafNodes);
955 mLeafNodes.reserve(idxLeafs.leafCount());
957 typename IntInternalNodeT::ChildOnCIter leafIt;
959 for (
size_t n = 0, N = internalNodes.size(); n < N; ++n) {
961 mLeafRanges[n].first = mLeafNodes.size();
963 size_t leafCount = 0;
964 for (leafIt = internalNodes[n]->cbeginChildOn(); leafIt; ++leafIt) {
965 mLeafNodes.push_back(&(*leafIt));
969 mMaxNodeLeafs =
std::max(leafCount, mMaxNodeLeafs);
971 mLeafRanges[n].second = mLeafNodes.size();
974 std::vector<Vec4R>().swap(mLeafBoundingSpheres);
975 mLeafBoundingSpheres.resize(mLeafNodes.size());
981 std::vector<Vec4R>().swap(mNodeBoundingSpheres);
982 mNodeBoundingSpheres.resize(internalNodes.size());
984 internal::NodeBS nodeBS(mNodeBoundingSpheres, mLeafRanges, mLeafBoundingSpheres);
986 mIsInitialized =
true;
990 template<
typename Gr
idT>
993 std::vector<float>& distances,
bool transformPoints)
995 if (!mIsInitialized)
return false;
998 distances.resize(points.size(), mMaxRadiusSqr);
1001 mLeafNodes, mLeafRanges, mLeafBoundingSpheres, mNodeBoundingSpheres,
1002 mMaxNodeLeafs, transformPoints);
1010 template<
typename Gr
idT>
1014 return search(
const_cast<std::vector<Vec3R>&
>(points), distances,
false);
1018 template<
typename Gr
idT>
1022 return search(points, distances,
true);
1030 #endif // OPENVDB_TOOLS_VOLUME_TO_MESH_HAS_BEEN_INCLUDED
OPENVDB_API Hermite min(const Hermite &, const Hermite &)
min and max operations done directly on the compressed data.
OPENVDB_IMPORT void initialize()
Global registration of basic types.
void setTransform(math::Transform::Ptr)
Associate the given transform with this grid, in place of its existing transform. ...
Definition: Grid.h:978
TreeType & tree()
Return a reference to this grid's tree, which might be shared with other grids.
Definition: Grid.h:710
boost::shared_ptr< Grid > Ptr
Definition: Grid.h:461
Container class that associates a tree with a transform and metadata.
Definition: Grid.h:54
#define OPENVDB_VERSION_NAME
Definition: version.h:45
OPENVDB_API Hermite max(const Hermite &, const Hermite &)
min and max operations done directly on the compressed data.
Vec3< float > Vec3s
Definition: Vec3.h:624
TreeType & tree()
Return the tree associated with this manager.
Definition: LeafManager.h:298
Vec4< float > Vec4s
Definition: Vec4.h:545
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h:67
This class manages a linear array of pointers to a given tree's leaf nodes, as well as optional auxil...
Definition: LeafManager.h:109
T & x()
Reference to the component, e.g. v.x() = 4.5f;.
Definition: Vec3.h:94