Go to the documentation of this file.
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) {
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
Definition: AttributeGroup.h:73
Set of Attribute Arrays which tracks metadata about each array.
typename LevelSetGridT::ValueType ValueT
Definition: IndexFilter.h:375
Definition: AttributeArray.h:849
bool initialized() const
Definition: IndexFilter.h:174
static index::State state()
Definition: IndexFilter.h:107
LevelSetFilter(const LevelSetGridT &grid, const math::Transform &transform, const ValueT min, const ValueT max)
Definition: IndexFilter.h:378
std::vector< AttributeSet::Descriptor::GroupIndex > IndexVector
Definition: IndexFilter.h:139
MultiGroupFilter(const IndexVector &include, const IndexVector &exclude)
Definition: IndexFilter.h:162
Definition: Exceptions.h:59
Index filtering on active / inactive state of host voxel.
Definition: IndexFilter.h:104
Definition: IndexFilter.h:556
void reset(const LeafT &leaf)
Definition: IndexFilter.h:405
Definition: IndexFilter.h:322
Index32 Index
Definition: Types.h:31
bool initialized() const
Definition: IndexFilter.h:509
static bool initialized()
Definition: IndexFilter.h:106
index::State state() const
Definition: IndexFilter.h:464
bool initialized() const
Definition: IndexFilter.h:341
Definition: IndexFilter.h:373
static index::State state(const LeafT &)
Definition: IndexFilter.h:469
int Floor(float x)
Return the floor of x.
Definition: Math.h:815
AttributeHashFilter(const AttributeHashFilter &filter)
Definition: IndexFilter.h:333
@ NONE
Definition: IndexIterator.h:42
Library and file format version numbers.
BinaryFilter(const T1 &filter1, const T2 &filter2)
Definition: IndexFilter.h:504
Definition: IndexFilter.h:136
std::unordered_map< openvdb::Coord, SeedCountPair > LeafMap
Definition: IndexFilter.h:230
Attribute Array storage templated on type and compression codec.
static index::State state(const LeafT &)
Definition: IndexFilter.h:270
uint64_t Index64
Definition: Types.h:30
@ PARTIAL
Definition: IndexIterator.h:41
bool valid(const IterT &iter) const
Definition: IndexFilter.h:120
bool initialized() const
Definition: IndexFilter.h:462
State
Definition: IndexIterator.h:40
Definition: IndexFilter.h:446
LevelSetFilter(const LevelSetFilter &filter)
Definition: IndexFilter.h:388
index::State state() const
Definition: IndexFilter.h:511
void reset(const LeafT &)
Definition: IndexFilter.h:117
std::vector< GroupHandle > HandleVector
Definition: IndexFilter.h:140
uint32_t Index32
Definition: Types.h:29
void reset(const LeafT &leaf)
Definition: IndexFilter.h:522
Attribute Group access and filtering for iteration.
Definition: IndexFilter.h:227
bool initialized() const
Definition: IndexFilter.h:398
std::pair< Index, Index > SeedCountPair
Definition: IndexFilter.h:229
std::vector< IntType > generateRandomSubset(const unsigned int seed, const IntType n, const IntType m)
Definition: IndexFilter.h:74
static index::State state(const LeafT &)
Definition: IndexFilter.h:182
Definition: Exceptions.h:60
std::vector< Name > NameVector
Definition: IndexFilter.h:138
bool valid(const IterT &iter) const
Definition: IndexFilter.h:198
Vec3d asVec3d() const
Definition: Coord.h:144
void reset(const LeafT &leaf)
Definition: IndexFilter.h:273
bool valid(const IterT &iter) const
Definition: IndexFilter.h:477
BBoxFilter(const openvdb::math::Transform &transform, const openvdb::BBoxd &bboxWS)
Definition: IndexFilter.h:450
void reset(const LeafT &leaf)
Definition: IndexFilter.h:185
Signed (x, y, z) 32-bit integer coordinates.
Definition: Coord.h:26
bool initialized() const
Definition: IndexFilter.h:266
MultiGroupFilter(const NameVector &include, const NameVector &exclude, const AttributeSet &attributeSet)
Definition: IndexFilter.h:156
void reset(const LeafT &leaf)
Definition: IndexFilter.h:472
MultiGroupFilter(const MultiGroupFilter &filter)
Definition: IndexFilter.h:167
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h:146
Ordered collection of uniquely-named attribute arrays.
Definition: AttributeSet.h:39
void next() const
Definition: IndexFilter.h:293
static index::State state(const LeafT &leaf)
Definition: IndexFilter.h:109
static index::State state()
Definition: IndexFilter.h:400
index::State state(const LeafT &leaf) const
Definition: IndexFilter.h:516
bool valid(const IterT &iter) const
Definition: IndexFilter.h:528
static index::State state(const LeafT &)
Definition: IndexFilter.h:345
RandomLeafFilter(const PointDataTreeT &tree, const Index64 targetPoints, const unsigned int seed=0)
Definition: IndexFilter.h:232
static index::State state()
Definition: IndexFilter.h:343
const Name & name
Definition: PointAttribute.h:544
BBoxFilter(const BBoxFilter &filter)
Definition: IndexFilter.h:455
@ ALL
Definition: IndexIterator.h:43
#define OPENVDB_VERSION_NAME
The version namespace name for this library version.
Definition: version.h:94
Util::GroupIndex groupIndex(const Name &groupName) const
Return the group index from the name of the group.
static index::State state()
Definition: IndexFilter.h:268
static index::State state(const LeafT &)
Definition: IndexFilter.h:402
AttributeHashFilter(const size_t index, const double percentage, const unsigned int seed=0)
Definition: IndexFilter.h:326
void reset(const LeafT &leaf)
Definition: IndexFilter.h:348
bool valid(const IterT &iter) const
Definition: IndexFilter.h:354
Definition: Exceptions.h:13
bool valid(const IterT &iter) const
Definition: IndexFilter.h:301
index::State state() const
Definition: IndexFilter.h:176
bool valid(const IterT &iter) const
Definition: IndexFilter.h:410
#define OPENVDB_THROW(exception, message)
Definition: Exceptions.h:82
Definition: IndexFilter.h:502