37 #ifndef OPENVDB_POINTS_INDEX_FILTER_HAS_BEEN_INCLUDED
38 #define OPENVDB_POINTS_INDEX_FILTER_HAS_BEEN_INCLUDED
53 #include <unordered_map>
56 class TestIndexFilter;
68 namespace index_filter_internal {
72 template <
typename RandGenT,
typename IntType>
74 generateRandomSubset(
const unsigned int seed,
const IntType
n,
const IntType m)
76 if (n <= 0)
return std::vector<IntType>();
79 std::vector<IntType>
values(m);
85 RandGenT randGen(seed);
110 template <
typename LeafT>
118 template <
typename LeafT>
121 template <
typename IterT>
124 const bool valueOn = iter.isValueOn();
125 return On ? valueOn : !valueOn;
141 using IndexVector = std::vector<AttributeSet::Descriptor::GroupIndex>;
147 for (
const auto&
name : names) {
150 }
catch (LookupError&) {
167 , mExclude(exclude) { }
170 : mInclude(filter.mInclude)
171 , mExclude(filter.mExclude)
172 , mIncludeHandles(filter.mIncludeHandles)
173 , mExcludeHandles(filter.mExcludeHandles)
174 , mInitialized(filter.mInitialized) { }
183 template <
typename LeafT>
186 template <
typename LeafT>
188 mIncludeHandles.clear();
189 mExcludeHandles.clear();
190 for (
const auto& i : mInclude) {
191 mIncludeHandles.emplace_back(leaf.groupHandle(i));
193 for (
const auto& i : mExclude) {
194 mExcludeHandles.emplace_back(leaf.groupHandle(i));
199 template <
typename IterT>
200 bool valid(
const IterT& iter)
const {
201 assert(mInitialized);
203 bool includeValid = mIncludeHandles.empty();
205 if (
handle.getUnsafe(*iter)) {
210 if (!includeValid)
return false;
212 if (
handle.getUnsafe(*iter))
return false;
222 bool mInitialized =
false;
227 template <
typename Po
intDataTreeT,
typename RandGenT>
232 using LeafMap = std::unordered_map<openvdb::Coord, SeedCountPair>;
236 const unsigned int seed = 0) {
238 for (
auto iter = tree.cbeginLeaf(); iter; ++iter) {
239 currentPoints += iter->pointCount();
242 const float factor = targetPoints > currentPoints ? 1.0f :
float(targetPoints) /
float(currentPoints);
244 std::mt19937 generator(seed);
248 float totalPointsFloat = 0.0f;
250 for (
auto iter = tree.cbeginLeaf(); iter; ++iter) {
252 if (leafCounter + 1 == tree.leafCount()) {
253 const int leafPoints =
static_cast<int>(targetPoints) - totalPoints;
257 totalPointsFloat += factor *
static_cast<float>(iter->pointCount());
258 const auto leafPoints =
static_cast<int>(
math::Floor(totalPointsFloat));
259 totalPointsFloat -=
static_cast<float>(leafPoints);
260 totalPoints += leafPoints;
271 template <
typename LeafT>
274 template <
typename LeafT>
276 using index_filter_internal::generateRandomSubset;
278 auto it = mLeafMap.find(leaf.origin());
279 if (it == mLeafMap.end()) {
281 "Cannot find leaf origin in map for random filter - " << leaf.origin());
285 const unsigned int seed =
static_cast<unsigned int>(value.first);
286 const auto total =
static_cast<Index>(leaf.pointCount());
287 mCount =
std::min(value.second, total);
289 mIndices = generateRandomSubset<RandGenT, int>(seed, mCount, total);
297 mNextIndex = mSubsetOffset >= mCount ?
299 mIndices[mSubsetOffset];
302 template <
typename IterT>
303 bool valid(
const IterT& iter)
const {
304 const int index = *iter;
305 while (mNextIndex < index) this->
next();
306 return mNextIndex == index;
310 friend class ::TestIndexFilter;
314 std::vector<int> mIndices;
316 mutable int mSubsetOffset = -1;
317 mutable int mNextIndex = -1;
322 template <
typename RandGenT,
typename IntType>
329 const double percentage,
330 const unsigned int seed = 0)
332 , mFactor(percentage / 100.0)
336 : mIndex(filter.mIndex)
337 , mFactor(filter.mFactor)
338 , mSeed(filter.mSeed)
340 if (filter.mIdHandle) mIdHandle.reset(
new Handle(*filter.mIdHandle));
346 template <
typename LeafT>
349 template <
typename LeafT>
351 assert(leaf.hasAttribute(mIndex));
352 mIdHandle.reset(
new Handle(leaf.constAttributeArray(mIndex)));
355 template <
typename IterT>
356 bool valid(
const IterT& iter)
const {
358 const IntType
id = mIdHandle->get(*iter);
359 const unsigned int seed = mSeed +
static_cast<unsigned int>(
id);
360 RandGenT generator(seed);
361 std::uniform_real_distribution<double>
dist(0.0, 1.0);
362 return dist(generator) < mFactor;
367 const double mFactor;
368 const unsigned int mSeed;
369 typename Handle::UniquePtr mIdHandle;
373 template <
typename LevelSetGr
idT>
384 : mAccessor(grid.getConstAccessor())
385 , mLevelSetTransform(grid.transform())
386 , mTransform(transform)
391 : mAccessor(filter.mAccessor)
392 , mLevelSetTransform(filter.mLevelSetTransform)
393 , mTransform(filter.mTransform)
397 if (filter.mPositionHandle) mPositionHandle.reset(
new Handle(*filter.mPositionHandle));
403 template <
typename LeafT>
406 template <
typename LeafT>
408 mPositionHandle.reset(
new Handle(leaf.constAttributeArray(
"P")));
411 template <
typename IterT>
412 bool valid(
const IterT& iter)
const {
413 assert(mPositionHandle);
416 const openvdb::Coord ijk = iter.getCoord();
417 const openvdb::Vec3f voxelIndexSpace = ijk.asVec3d();
420 const openvdb::Vec3f& pointVoxelSpace = mPositionHandle->get(*iter);
423 const openvdb::Vec3f pointWorldSpace = mTransform.
indexToWorld(pointVoxelSpace + voxelIndexSpace);
424 const openvdb::Vec3f pointIndexSpace = mLevelSetTransform.
worldToIndex(pointWorldSpace);
430 const bool invert = mMin > mMax;
437 const typename LevelSetGridT::ConstAccessor mAccessor;
442 Handle::UniquePtr mPositionHandle;
454 : mTransform(transform)
455 , mBbox(transform.worldToIndex(bboxWS)) { }
458 : mTransform(filter.mTransform)
459 , mBbox(filter.mBbox)
461 if (filter.mPositionHandle) mPositionHandle.reset(
new Handle(*filter.mPositionHandle));
470 template <
typename LeafT>
473 template <
typename LeafT>
475 mPositionHandle.reset(
new Handle(leaf.constAttributeArray(
"P")));
478 template <
typename IterT>
479 bool valid(
const IterT& iter)
const {
480 assert(mPositionHandle);
482 const openvdb::Coord ijk = iter.getCoord();
483 const openvdb::Vec3f voxelIndexSpace = ijk.asVec3d();
486 const openvdb::Vec3f& pointVoxelSpace = mPositionHandle->get(*iter);
489 const openvdb::Vec3f pointIndexSpace = pointVoxelSpace + voxelIndexSpace;
491 return mBbox.isInside(pointIndexSpace);
495 const openvdb::math::Transform& mTransform;
497 Handle::UniquePtr mPositionHandle;
502 template <
typename T1,
typename T2,
bool And = true>
509 , mFilter2(filter2) { }
511 inline bool initialized()
const {
return mFilter1.initialized() && mFilter2.initialized(); }
515 return this->computeState(mFilter1.state(), mFilter2.state());
517 template <
typename LeafT>
520 return this->computeState(mFilter1.state(leaf), mFilter2.state(leaf));
523 template <
typename LeafT>
525 mFilter1.reset(leaf);
526 mFilter2.reset(leaf);
529 template <
typename IterT>
530 bool valid(
const IterT& iter)
const {
531 if (And)
return mFilter1.valid(iter) && mFilter2.valid(iter);
532 return mFilter1.valid(iter) || mFilter2.valid(iter);
565 template <
typename T>
569 template <
typename T0,
typename T1,
bool And>
583 #endif // OPENVDB_POINTS_INDEX_FILTER_HAS_BEEN_INCLUDED
GA_API const UT_StringHolder dist
Util::GroupIndex groupIndex(const Name &groupName) const
Return the group index from the name of the group.
GLsizei GLenum const void * indices
static const bool RequiresCoord
OIIO_FORCEINLINE vbool4 shuffle(const vbool4 &a)
GLdouble GLdouble GLint GLint const GLdouble * points
void reset(const LeafT &leaf)
RandomLeafFilter(const PointDataTreeT &tree, const Index64 targetPoints, const unsigned int seed=0)
void reset(const LeafT &leaf)
index::State state() const
static index::State state()
#define OPENVDB_USE_VERSION_NAMESPACE
ImageBuf OIIO_API min(Image_or_Const A, Image_or_Const B, ROI roi={}, int nthreads=0)
bool valid(const IterT &iter) const
Index filtering on active / inactive state of host voxel.
void reset(const LeafT &leaf)
LevelSetFilter(const LevelSetFilter &filter)
std::vector< GroupHandle > HandleVector
MultiGroupFilter(const MultiGroupFilter &filter)
void reset(const LeafT &)
AttributeHandle< openvdb::Vec3f > Handle
static index::State state(const LeafT &)
bool valid(const IterT &iter) const
static index::State state()
std::vector< Name > NameVector
bool valid(const IterT &iter) const
static index::State state(const LeafT &)
IMATH_NAMESPACE::V2f float
bool valid(const IterT &iter) const
typename LevelSetGridT::ValueType ValueT
bool valid(const IterT &iter) const
void reset(const LeafT &leaf)
MultiGroupFilter(const NameVector &include, const NameVector &exclude, const AttributeSet &attributeSet)
index::State state() const
std::unordered_map< openvdb::Coord, SeedCountPair > LeafMap
GLuint const GLchar * name
GA_API const UT_StringHolder transform
bool valid(const IterT &iter) const
static index::State state()
AttributeHandle< IntType > Handle
static index::State state(const LeafT &leaf)
Library and file format version numbers.
AttributeHashFilter(const size_t index, const double percentage, const unsigned int seed=0)
static index::State state(const LeafT &)
GLenum GLsizei GLsizei GLint * values
BinaryFilter(const T1 &filter1, const T2 &filter2)
Set of Attribute Arrays which tracks metadata about each array.
index::State state() const
Attribute Group access and filtering for iteration.
AttributeHandle< openvdb::Vec3f > Handle
math::BBox< Vec3d > BBoxd
ImageBuf OIIO_API max(Image_or_Const A, Image_or_Const B, ROI roi={}, int nthreads=0)
Ordered collection of uniquely-named attribute arrays.
int Floor(float x)
Return the floor of x.
void reset(const LeafT &leaf)
static index::State state(const LeafT &)
index::State state(const LeafT &leaf) const
static index::State state()
LevelSetFilter(const LevelSetGridT &grid, const math::Transform &transform, const ValueT min, const ValueT max)
static index::State state(const LeafT &)
bool valid(const IterT &iter) const
BBoxFilter(const openvdb::math::Transform &transform, const openvdb::BBoxd &bboxWS)
Attribute Array storage templated on type and compression codec.
AttributeHashFilter(const AttributeHashFilter &filter)
std::pair< Index, Index > SeedCountPair
static bool initialized()
#define OPENVDB_VERSION_NAME
The version namespace name for this library version.
void sort(I begin, I end, const Pred &pred)
BBoxFilter(const BBoxFilter &filter)
void reset(const LeafT &leaf)
#define OPENVDB_THROW(exception, message)
MultiGroupFilter(const IndexVector &include, const IndexVector &exclude)
GLint GLint GLint GLint GLint GLint GLint GLbitfield GLenum filter
std::vector< AttributeSet::Descriptor::GroupIndex > IndexVector