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>
76 if (n <= 0)
return std::vector<IntType>();
79 std::vector<IntType> values(m);
80 std::iota(values.begin(), values.end(), 0);
81 if (n >= m)
return values;
85 RandGenT randGen(seed);
86 std::shuffle(values.begin(), values.end(), randGen);
92 std::sort(values.begin(), values.end());
108 template <
typename LeafT>
116 template <
typename LeafT>
119 template <
typename IterT>
122 const bool valueOn = iter.isValueOn();
123 return On ? valueOn : !valueOn;
139 using IndexVector = std::vector<AttributeSet::Descriptor::GroupIndex>;
145 for (
const auto& name : names) {
147 indices.emplace_back(attributeSet.
groupIndex(name));
165 , mExclude(exclude) { }
168 : mInclude(filter.mInclude)
169 , mExclude(filter.mExclude)
170 , mIncludeHandles(filter.mIncludeHandles)
171 , mExcludeHandles(filter.mExcludeHandles)
172 , mInitialized(filter.mInitialized) { }
181 template <
typename LeafT>
184 template <
typename LeafT>
186 mIncludeHandles.clear();
187 mExcludeHandles.clear();
188 for (
const auto& i : mInclude) {
189 mIncludeHandles.emplace_back(leaf.groupHandle(i));
191 for (
const auto& i : mExclude) {
192 mExcludeHandles.emplace_back(leaf.groupHandle(i));
197 template <
typename IterT>
198 bool valid(
const IterT& iter)
const {
199 assert(mInitialized);
201 bool includeValid = mIncludeHandles.empty();
202 for (
const GroupHandle& handle : mIncludeHandles) {
203 if (handle.getUnsafe(*iter)) {
208 if (!includeValid)
return false;
209 for (
const GroupHandle& handle : mExcludeHandles) {
210 if (handle.getUnsafe(*iter))
return false;
216 IndexVector mInclude;
217 IndexVector mExclude;
218 HandleVector mIncludeHandles;
219 HandleVector mExcludeHandles;
220 bool mInitialized =
false;
225 template <
typename Po
intDataTreeT,
typename RandGenT>
230 using LeafMap = std::unordered_map<openvdb::Coord, SeedCountPair>;
234 const unsigned int seed = 0) {
236 for (
auto iter = tree.cbeginLeaf(); iter; ++iter) {
237 currentPoints += iter->pointCount();
240 const float factor = targetPoints > currentPoints ? 1.0f : float(targetPoints) / float(currentPoints);
242 std::mt19937 generator(seed);
246 float totalPointsFloat = 0.0f;
248 for (
auto iter = tree.cbeginLeaf(); iter; ++iter) {
250 if (leafCounter + 1 == tree.leafCount()) {
251 const int leafPoints =
static_cast<int>(targetPoints) - totalPoints;
252 mLeafMap[iter->origin()] =
SeedCountPair(dist(generator), leafPoints);
255 totalPointsFloat += factor *
static_cast<float>(iter->pointCount());
256 const auto leafPoints =
static_cast<int>(
math::Floor(totalPointsFloat));
257 totalPointsFloat -=
static_cast<float>(leafPoints);
258 totalPoints += leafPoints;
260 mLeafMap[iter->origin()] =
SeedCountPair(dist(generator), leafPoints);
269 template <
typename LeafT>
272 template <
typename LeafT>
276 auto it = mLeafMap.find(leaf.origin());
277 if (it == mLeafMap.end()) {
279 "Cannot find leaf origin in map for random filter - " << leaf.origin());
283 const unsigned int seed =
static_cast<unsigned int>(value.first);
284 const auto total =
static_cast<Index>(leaf.pointCount());
285 mCount =
std::min(value.second, total);
287 mIndices = generateRandomSubset<RandGenT, int>(seed, mCount, total);
295 mNextIndex = mSubsetOffset >= mCount ?
297 mIndices[mSubsetOffset];
300 template <
typename IterT>
301 bool valid(
const IterT& iter)
const {
302 const int index = *iter;
303 while (mNextIndex < index) this->next();
304 return mNextIndex == index;
308 friend class ::TestIndexFilter;
312 std::vector<int> mIndices;
314 mutable int mSubsetOffset = -1;
315 mutable int mNextIndex = -1;
320 template <
typename RandGenT,
typename IntType>
327 const double percentage,
328 const unsigned int seed = 0)
330 , mFactor(percentage / 100.0)
334 : mIndex(filter.mIndex)
335 , mFactor(filter.mFactor)
336 , mSeed(filter.mSeed)
338 if (filter.mIdHandle) mIdHandle.reset(
new Handle(*filter.mIdHandle));
344 template <
typename LeafT>
347 template <
typename LeafT>
349 assert(leaf.hasAttribute(mIndex));
350 mIdHandle.reset(
new Handle(leaf.constAttributeArray(mIndex)));
353 template <
typename IterT>
354 bool valid(
const IterT& iter)
const {
356 const IntType
id = mIdHandle->get(*iter);
357 const unsigned int seed = mSeed +
static_cast<unsigned int>(id);
358 RandGenT generator(seed);
359 std::uniform_real_distribution<double> dist(0.0, 1.0);
360 return dist(generator) < mFactor;
365 const double mFactor;
366 const unsigned int mSeed;
367 typename Handle::UniquePtr mIdHandle;
371 template <
typename LevelSetGr
idT>
375 using ValueT =
typename LevelSetGridT::ValueType;
382 : mAccessor(grid.getConstAccessor())
383 , mLevelSetTransform(grid.transform())
384 , mTransform(transform)
389 : mAccessor(filter.mAccessor)
390 , mLevelSetTransform(filter.mLevelSetTransform)
391 , mTransform(filter.mTransform)
395 if (filter.mPositionHandle) mPositionHandle.reset(
new Handle(*filter.mPositionHandle));
401 template <
typename LeafT>
404 template <
typename LeafT>
406 mPositionHandle.reset(
new Handle(leaf.constAttributeArray(
"P")));
409 template <
typename IterT>
410 bool valid(
const IterT& iter)
const {
411 assert(mPositionHandle);
418 const openvdb::Vec3f& pointVoxelSpace = mPositionHandle->get(*iter);
421 const openvdb::Vec3f pointWorldSpace = mTransform.indexToWorld(pointVoxelSpace + voxelIndexSpace);
422 const openvdb::Vec3f pointIndexSpace = mLevelSetTransform.worldToIndex(pointWorldSpace);
425 const typename LevelSetGridT::ValueType value = tools::BoxSampler::sample(mAccessor, pointIndexSpace);
428 const bool invert = mMin > mMax;
430 return invert ? (value < mMax || value > mMin) : (value < mMax && value > mMin);
435 const typename LevelSetGridT::ConstAccessor mAccessor;
440 Handle::UniquePtr mPositionHandle;
452 : mTransform(transform)
453 , mBbox(transform.worldToIndex(bboxWS)) { }
456 : mTransform(filter.mTransform)
457 , mBbox(filter.mBbox)
459 if (filter.mPositionHandle) mPositionHandle.reset(
new Handle(*filter.mPositionHandle));
468 template <
typename LeafT>
471 template <
typename LeafT>
473 mPositionHandle.reset(
new Handle(leaf.constAttributeArray(
"P")));
476 template <
typename IterT>
477 bool valid(
const IterT& iter)
const {
478 assert(mPositionHandle);
484 const openvdb::Vec3f& pointVoxelSpace = mPositionHandle->get(*iter);
487 const openvdb::Vec3f pointIndexSpace = pointVoxelSpace + voxelIndexSpace;
489 return mBbox.isInside(pointIndexSpace);
493 const openvdb::math::Transform& mTransform;
495 Handle::UniquePtr mPositionHandle;
500 template <
typename T1,
typename T2,
bool And = true>
507 , mFilter2(filter2) { }
509 inline bool initialized()
const {
return mFilter1.initialized() && mFilter2.initialized(); }
513 return this->computeState(mFilter1.state(), mFilter2.state());
515 template <
typename LeafT>
518 return this->computeState(mFilter1.state(leaf), mFilter2.state(leaf));
521 template <
typename LeafT>
523 mFilter1.reset(leaf);
524 mFilter2.reset(leaf);
527 template <
typename IterT>
528 bool valid(
const IterT& iter)
const {
529 if (And)
return mFilter1.valid(iter) && mFilter2.valid(iter);
530 return mFilter1.valid(iter) || mFilter2.valid(iter);
557 static const bool RequiresCoord =
false;
561 static const bool RequiresCoord =
true;
563 template <
typename T>
565 static const bool RequiresCoord =
true;
567 template <
typename T0,
typename T1,
bool And>
581 #endif // OPENVDB_POINTS_INDEX_FILTER_HAS_BEEN_INCLUDED static index::State state(const LeafT &leaf)
Definition: IndexFilter.h:109
void reset(const LeafT &leaf)
Definition: IndexFilter.h:472
BBoxFilter(const BBoxFilter &filter)
Definition: IndexFilter.h:455
Attribute Group access and filtering for iteration.
void reset(const LeafT &leaf)
Definition: IndexFilter.h:405
std::vector< Name > NameVector
Definition: IndexFilter.h:138
std::unordered_map< openvdb::Coord, SeedCountPair > LeafMap
Definition: IndexFilter.h:230
std::vector< AttributeSet::Descriptor::GroupIndex > IndexVector
Definition: IndexFilter.h:139
uint32_t Index32
Definition: Types.h:29
Definition: IndexFilter.h:556
#define OPENVDB_THROW(exception, message)
Definition: Exceptions.h:82
static bool initialized()
Definition: IndexFilter.h:106
Attribute Array storage templated on type and compression codec.
std::pair< Index, Index > SeedCountPair
Definition: IndexFilter.h:229
static index::State state()
Definition: IndexFilter.h:400
MultiGroupFilter(const IndexVector &include, const IndexVector &exclude)
Definition: IndexFilter.h:162
bool valid(const IterT &iter) const
Definition: IndexFilter.h:301
Definition: IndexIterator.h:41
bool initialized() const
Definition: IndexFilter.h:509
Definition: Exceptions.h:59
Signed (x, y, z) 32-bit integer coordinates.
Definition: Coord.h:25
Vec3d asVec3d() const
Definition: Coord.h:144
Definition: IndexIterator.h:42
static index::State state(const LeafT &)
Definition: IndexFilter.h:402
Definition: IndexIterator.h:43
void reset(const LeafT &leaf)
Definition: IndexFilter.h:273
index::State state() const
Definition: IndexFilter.h:511
Definition: IndexFilter.h:501
index::State state(const LeafT &leaf) const
Definition: IndexFilter.h:516
static index::State state(const LeafT &)
Definition: IndexFilter.h:270
bool valid(const IterT &iter) const
Definition: IndexFilter.h:120
Definition: AttributeGroup.h:75
Definition: IndexFilter.h:372
State
Definition: IndexIterator.h:39
#define OPENVDB_VERSION_NAME
The version namespace name for this library version.
Definition: version.h:102
std::vector< GroupHandle > HandleVector
Definition: IndexFilter.h:140
AttributeHashFilter(const AttributeHashFilter &filter)
Definition: IndexFilter.h:333
std::vector< IntType > generateRandomSubset(const unsigned int seed, const IntType n, const IntType m)
Definition: IndexFilter.h:74
static index::State state()
Definition: IndexFilter.h:107
Definition: IndexFilter.h:226
void next() const
Definition: IndexFilter.h:293
Definition: IndexFilter.h:135
Definition: Exceptions.h:13
MultiGroupFilter(const MultiGroupFilter &filter)
Definition: IndexFilter.h:167
void reset(const LeafT &leaf)
Definition: IndexFilter.h:348
int Floor(float x)
Return the floor of x.
Definition: Math.h:795
bool initialized() const
Definition: IndexFilter.h:462
typename LevelSetGridT::ValueType ValueT
Definition: IndexFilter.h:375
static index::State state()
Definition: IndexFilter.h:268
Index filtering on active / inactive state of host voxel.
Definition: IndexFilter.h:103
bool valid(const IterT &iter) const
Definition: IndexFilter.h:528
RandomLeafFilter(const PointDataTreeT &tree, const Index64 targetPoints, const unsigned int seed=0)
Definition: IndexFilter.h:232
Library and file format version numbers.
void reset(const LeafT &leaf)
Definition: IndexFilter.h:522
static index::State state()
Definition: IndexFilter.h:343
bool initialized() const
Definition: IndexFilter.h:341
Index32 Index
Definition: Types.h:31
Definition: Exceptions.h:60
static index::State state(const LeafT &)
Definition: IndexFilter.h:182
Definition: AttributeArray.h:849
Definition: IndexFilter.h:445
LevelSetFilter(const LevelSetFilter &filter)
Definition: IndexFilter.h:388
index::State state() const
Definition: IndexFilter.h:176
Definition: IndexFilter.h:321
uint64_t Index64
Definition: Types.h:30
bool valid(const IterT &iter) const
Definition: IndexFilter.h:354
bool valid(const IterT &iter) const
Definition: IndexFilter.h:198
BinaryFilter(const T1 &filter1, const T2 &filter2)
Definition: IndexFilter.h:504
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h:154
bool initialized() const
Definition: IndexFilter.h:398
bool valid(const IterT &iter) const
Definition: IndexFilter.h:410
Util::GroupIndex groupIndex(const Name &groupName) const
Return the group index from the name of the group.
AttributeHashFilter(const size_t index, const double percentage, const unsigned int seed=0)
Definition: IndexFilter.h:326
static index::State state(const LeafT &)
Definition: IndexFilter.h:469
bool valid(const IterT &iter) const
Definition: IndexFilter.h:477
bool initialized() const
Definition: IndexFilter.h:266
Set of Attribute Arrays which tracks metadata about each array.
void reset(const LeafT &leaf)
Definition: IndexFilter.h:185
static index::State state(const LeafT &)
Definition: IndexFilter.h:345
Ordered collection of uniquely-named attribute arrays.
Definition: AttributeSet.h:35
index::State state() const
Definition: IndexFilter.h:464
MultiGroupFilter(const NameVector &include, const NameVector &exclude, const AttributeSet &attributeSet)
Definition: IndexFilter.h:156
bool initialized() const
Definition: IndexFilter.h:174
BBoxFilter(const openvdb::math::Transform &transform, const openvdb::BBoxd &bboxWS)
Definition: IndexFilter.h:450
LevelSetFilter(const LevelSetGridT &grid, const math::Transform &transform, const ValueT min, const ValueT max)
Definition: IndexFilter.h:378
void reset(const LeafT &)
Definition: IndexFilter.h:117