37 #ifndef OPENVDB_POINTS_INDEX_FILTER_HAS_BEEN_INCLUDED
38 #define OPENVDB_POINTS_INDEX_FILTER_HAS_BEEN_INCLUDED
54 #include <unordered_map>
57 class TestIndexFilter;
69 namespace index_filter_internal {
73 template <
typename RandGenT,
typename IntType>
75 generateRandomSubset(
const unsigned int seed,
const IntType
n,
const IntType m)
77 if (n <= 0)
return std::vector<IntType>();
80 std::vector<IntType>
values(m);
86 RandGenT randGen(seed);
111 template <
typename LeafT>
119 template <
typename LeafT>
122 template <
typename IterT>
125 const bool valueOn = iter.isValueOn();
126 return On ? valueOn : !valueOn;
142 using IndexVector = std::vector<AttributeSet::Descriptor::GroupIndex>;
148 for (
const auto&
name : names) {
151 }
catch (LookupError&) {
168 , mExclude(exclude) { }
171 : mInclude(filter.mInclude)
172 , mExclude(filter.mExclude)
173 , mIncludeHandles(filter.mIncludeHandles)
174 , mExcludeHandles(filter.mExcludeHandles)
175 , mInitialized(filter.mInitialized) { }
184 template <
typename LeafT>
187 template <
typename LeafT>
189 mIncludeHandles.clear();
190 mExcludeHandles.clear();
191 for (
const auto& i : mInclude) {
192 mIncludeHandles.emplace_back(leaf.groupHandle(i));
194 for (
const auto& i : mExclude) {
195 mExcludeHandles.emplace_back(leaf.groupHandle(i));
200 template <
typename IterT>
201 bool valid(
const IterT& iter)
const {
204 bool includeValid = mIncludeHandles.empty();
205 for (
const GroupHandle& handle : mIncludeHandles) {
206 if (handle.getUnsafe(*iter)) {
211 if (!includeValid)
return false;
212 for (
const GroupHandle& handle : mExcludeHandles) {
213 if (handle.getUnsafe(*iter))
return false;
223 bool mInitialized =
false;
228 template <
typename Po
intDataTreeT,
typename RandGenT>
233 using LeafMap = std::unordered_map<openvdb::Coord, SeedCountPair>;
237 const unsigned int seed = 0) {
239 for (
auto iter = tree.cbeginLeaf(); iter; ++iter) {
240 currentPoints += iter->pointCount();
243 const float factor = targetPoints > currentPoints ? 1.0f :
float(targetPoints) /
float(currentPoints);
245 std::mt19937 generator(seed);
249 float totalPointsFloat = 0.0f;
251 for (
auto iter = tree.cbeginLeaf(); iter; ++iter) {
253 if (leafCounter + 1 == tree.leafCount()) {
254 const int leafPoints =
static_cast<int>(targetPoints) - totalPoints;
258 totalPointsFloat += factor *
static_cast<float>(iter->pointCount());
259 const auto leafPoints =
static_cast<int>(
math::Floor(totalPointsFloat));
260 totalPointsFloat -=
static_cast<float>(leafPoints);
261 totalPoints += leafPoints;
272 template <
typename LeafT>
275 template <
typename LeafT>
277 using index_filter_internal::generateRandomSubset;
279 auto it = mLeafMap.find(leaf.origin());
280 if (it == mLeafMap.end()) {
282 "Cannot find leaf origin in map for random filter - " << leaf.origin());
286 const unsigned int seed =
static_cast<unsigned int>(value.first);
287 const auto total =
static_cast<Index>(leaf.pointCount());
288 mCount =
std::min(value.second, total);
290 mIndices = generateRandomSubset<RandGenT, int>(seed, mCount, total);
298 mNextIndex = mSubsetOffset >= mCount ?
300 mIndices[mSubsetOffset];
303 template <
typename IterT>
304 bool valid(
const IterT& iter)
const {
305 const int index = *iter;
306 while (mNextIndex < index) this->
next();
307 return mNextIndex == index;
311 friend class ::TestIndexFilter;
315 std::vector<int> mIndices;
317 mutable int mSubsetOffset = -1;
318 mutable int mNextIndex = -1;
323 template <
typename RandGenT,
typename IntType>
330 const double percentage,
331 const unsigned int seed = 0)
333 , mFactor(percentage / 100.0)
337 : mIndex(filter.mIndex)
338 , mFactor(filter.mFactor)
339 , mSeed(filter.mSeed)
341 if (filter.mIdHandle) mIdHandle.reset(
new Handle(*filter.mIdHandle));
347 template <
typename LeafT>
350 template <
typename LeafT>
353 mIdHandle.reset(
new Handle(leaf.constAttributeArray(mIndex)));
356 template <
typename IterT>
357 bool valid(
const IterT& iter)
const {
359 const IntType
id = mIdHandle->get(*iter);
360 const unsigned int seed = mSeed +
static_cast<unsigned int>(
id);
361 RandGenT generator(seed);
362 std::uniform_real_distribution<double>
dist(0.0, 1.0);
363 return dist(generator) < mFactor;
368 const double mFactor;
369 const unsigned int mSeed;
370 typename Handle::UniquePtr mIdHandle;
374 template <
typename LevelSetGr
idT>
385 : mAccessor(grid.getConstAccessor())
386 , mLevelSetTransform(grid.transform())
387 , mTransform(transform)
392 : mAccessor(filter.mAccessor)
393 , mLevelSetTransform(filter.mLevelSetTransform)
394 , mTransform(filter.mTransform)
398 if (filter.mPositionHandle) mPositionHandle.reset(
new Handle(*filter.mPositionHandle));
404 template <
typename LeafT>
407 template <
typename LeafT>
409 mPositionHandle.reset(
new Handle(leaf.constAttributeArray(
"P")));
412 template <
typename IterT>
413 bool valid(
const IterT& iter)
const {
417 const openvdb::Coord ijk = iter.getCoord();
418 const openvdb::Vec3f voxelIndexSpace = ijk.asVec3d();
421 const openvdb::Vec3f& pointVoxelSpace = mPositionHandle->get(*iter);
424 const openvdb::Vec3f pointWorldSpace = mTransform.
indexToWorld(pointVoxelSpace + voxelIndexSpace);
425 const openvdb::Vec3f pointIndexSpace = mLevelSetTransform.
worldToIndex(pointWorldSpace);
431 const bool invert = mMin > mMax;
438 const typename LevelSetGridT::ConstAccessor mAccessor;
443 Handle::UniquePtr mPositionHandle;
455 : mTransform(transform)
456 , mBbox(transform.worldToIndex(bboxWS)) { }
459 : mTransform(filter.mTransform)
460 , mBbox(filter.mBbox)
462 if (filter.mPositionHandle) mPositionHandle.reset(
new Handle(*filter.mPositionHandle));
471 template <
typename LeafT>
474 template <
typename LeafT>
476 mPositionHandle.reset(
new Handle(leaf.constAttributeArray(
"P")));
479 template <
typename IterT>
480 bool valid(
const IterT& iter)
const {
483 const openvdb::Coord ijk = iter.getCoord();
484 const openvdb::Vec3f voxelIndexSpace = ijk.asVec3d();
487 const openvdb::Vec3f& pointVoxelSpace = mPositionHandle->get(*iter);
490 const openvdb::Vec3f pointIndexSpace = pointVoxelSpace + voxelIndexSpace;
492 return mBbox.isInside(pointIndexSpace);
496 const openvdb::math::Transform& mTransform;
498 Handle::UniquePtr mPositionHandle;
503 template <
typename T1,
typename T2,
bool And = true>
510 , mFilter2(filter2) { }
512 inline bool initialized()
const {
return mFilter1.initialized() && mFilter2.initialized(); }
516 return this->computeState(mFilter1.state(), mFilter2.state());
518 template <
typename LeafT>
521 return this->computeState(mFilter1.state(leaf), mFilter2.state(leaf));
524 template <
typename LeafT>
526 mFilter1.reset(leaf);
527 mFilter2.reset(leaf);
530 template <
typename IterT>
531 bool valid(
const IterT& iter)
const {
532 if (And)
return mFilter1.valid(iter) && mFilter2.valid(iter);
533 return mFilter1.valid(iter) || mFilter2.valid(iter);
566 template <
typename T>
570 template <
typename T0,
typename T1,
bool And>
584 #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)
GLsizei const GLfloat * value
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)
PUGI__FN void sort(I begin, I end, const Pred &pred)
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 &)
#define OPENVDB_ASSERT(X)
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 &)
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)
IMATH_NAMESPACE::V2f IMATH_NAMESPACE::Box2i std::string this attribute is obsolete as of OpenEXR v3 float
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.
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