37 #ifndef OPENVDB_POINTS_INDEX_FILTER_HAS_BEEN_INCLUDED 38 #define OPENVDB_POINTS_INDEX_FILTER_HAS_BEEN_INCLUDED 40 #include <openvdb/version.h> 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);
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());
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) {
149 indices.emplace_back(attributeSet.
groupIndex(name));
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();
204 for (
const GroupHandle& handle : mIncludeHandles) {
205 if (handle.getUnsafe(*iter)) {
210 if (!includeValid)
return false;
211 for (
const GroupHandle& handle : mExcludeHandles) {
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;
254 mLeafMap[iter->origin()] =
SeedCountPair(dist(generator), leafPoints);
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;
262 mLeafMap[iter->origin()] =
SeedCountPair(dist(generator), 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;
373 template <
typename LevelSetGr
idT>
377 using ValueT =
typename LevelSetGridT::ValueType;
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();
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);
427 const typename LevelSetGridT::ValueType
value = tools::BoxSampler::sample(mAccessor, pointIndexSpace);
430 const bool invert = mMin > mMax;
432 return invert ? (value < mMax || value > mMin) : (value < mMax && value > mMin);
437 const typename LevelSetGridT::ConstAccessor mAccessor;
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();
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;
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);
559 static const bool RequiresCoord =
false;
563 static const bool RequiresCoord =
true;
565 template <
typename T>
567 static const bool RequiresCoord =
true;
569 template <
typename T0,
typename T1,
bool And>
583 #endif // OPENVDB_POINTS_INDEX_FILTER_HAS_BEEN_INCLUDED std::unique_ptr< Handle > UniquePtr
Definition: AttributeArray.h:815
std::vector< AttributeSet::Descriptor::GroupIndex > IndexVector
Definition: IndexFilter.h:141
static index::State state(const LeafT &)
Definition: IndexFilter.h:471
bool initialized() const
Definition: IndexFilter.h:464
void reset(const LeafT &leaf)
Definition: IndexFilter.h:187
Definition: IndexFilter.h:558
Ordered collection of uniquely-named attribute arrays.
Definition: AttributeSet.h:38
Attribute Group access and filtering for iteration.
static index::State state()
Definition: IndexFilter.h:109
#define OPENVDB_THROW(exception, message)
Definition: Exceptions.h:74
LevelSetFilter(const LevelSetGridT &grid, const math::Transform &transform, const ValueT min, const ValueT max)
Definition: IndexFilter.h:380
bool initialized() const
Definition: IndexFilter.h:176
index::State state() const
Definition: IndexFilter.h:513
Util::GroupIndex groupIndex(const Name &groupName) const
Return the group index from the name of the group.
static index::State state()
Definition: IndexFilter.h:402
MultiGroupFilter(const IndexVector &include, const IndexVector &exclude)
Definition: IndexFilter.h:164
Definition: IndexIterator.h:41
void reset(const LeafT &leaf)
Definition: IndexFilter.h:407
Attribute Array storage templated on type and compression codec.
std::vector< Name > NameVector
Definition: IndexFilter.h:140
std::unordered_map< openvdb::Coord, SeedCountPair > LeafMap
Definition: IndexFilter.h:232
bool initialized() const
Definition: IndexFilter.h:268
static index::State state(const LeafT &)
Definition: IndexFilter.h:404
Definition: IndexFilter.h:323
static index::State state()
Definition: IndexFilter.h:345
Definition: Exceptions.h:60
bool valid(const IterT &iter) const
Definition: IndexFilter.h:530
void reset(const LeafT &leaf)
Definition: IndexFilter.h:474
int Floor(float x)
Return the floor of x.
Definition: Math.h:851
BBoxFilter(const BBoxFilter &filter)
Definition: IndexFilter.h:457
index::State state() const
Definition: IndexFilter.h:178
Definition: IndexFilter.h:374
Definition: Exceptions.h:59
std::vector< GroupHandle > HandleVector
Definition: IndexFilter.h:142
void reset(const LeafT &leaf)
Definition: IndexFilter.h:350
bool initialized() const
Definition: IndexFilter.h:400
bool valid(const IterT &iter) const
Definition: IndexFilter.h:122
void reset(const LeafT &)
Definition: IndexFilter.h:119
State
Definition: IndexIterator.h:39
bool initialized() const
Definition: IndexFilter.h:511
Definition: IndexFilter.h:503
static index::State state(const LeafT &)
Definition: IndexFilter.h:272
Definition: IndexFilter.h:228
MultiGroupFilter(const MultiGroupFilter &filter)
Definition: IndexFilter.h:169
AttributeHashFilter(const size_t index, const double percentage, const unsigned int seed=0)
Definition: IndexFilter.h:328
static bool initialized()
Definition: IndexFilter.h:108
index::State state() const
Definition: IndexFilter.h:466
uint64_t Index64
Definition: Types.h:53
Definition: Exceptions.h:13
bool valid(const IterT &iter) const
Definition: IndexFilter.h:356
ValueT value
Definition: GridBuilder.h:1287
Definition: IndexFilter.h:137
Definition: AttributeGroup.h:72
void reset(const LeafT &leaf)
Definition: IndexFilter.h:275
Definition: IndexIterator.h:43
Index32 Index
Definition: Types.h:54
Index filtering on active / inactive state of host voxel.
Definition: IndexFilter.h:105
static index::State state(const LeafT &)
Definition: IndexFilter.h:184
AttributeHashFilter(const AttributeHashFilter &filter)
Definition: IndexFilter.h:335
Definition: IndexFilter.h:447
typename LevelSetGridT::ValueType ValueT
Definition: IndexFilter.h:377
static index::State state(const LeafT &leaf)
Definition: IndexFilter.h:111
static index::State state()
Definition: IndexFilter.h:270
void reset(const LeafT &leaf)
Definition: IndexFilter.h:524
Definition: AttributeArray.h:810
static index::State state(const LeafT &)
Definition: IndexFilter.h:347
bool valid(const IterT &iter) const
Definition: IndexFilter.h:412
bool initialized() const
Definition: IndexFilter.h:343
void next() const
Definition: IndexFilter.h:295
uint32_t Index32
Definition: Types.h:52
LevelSetFilter(const LevelSetFilter &filter)
Definition: IndexFilter.h:390
MultiGroupFilter(const NameVector &include, const NameVector &exclude, const AttributeSet &attributeSet)
Definition: IndexFilter.h:158
index::State state(const LeafT &leaf) const
Definition: IndexFilter.h:518
bool valid(const IterT &iter) const
Definition: IndexFilter.h:200
BBoxFilter(const openvdb::math::Transform &transform, const openvdb::BBoxd &bboxWS)
Definition: IndexFilter.h:452
#define OPENVDB_VERSION_NAME
The version namespace name for this library version.
Definition: version.h.in:116
std::pair< Index, Index > SeedCountPair
Definition: IndexFilter.h:231
BinaryFilter(const T1 &filter1, const T2 &filter2)
Definition: IndexFilter.h:506
bool valid(const IterT &iter) const
Definition: IndexFilter.h:303
bool valid(const IterT &iter) const
Definition: IndexFilter.h:479
Set of Attribute Arrays which tracks metadata about each array.
RandomLeafFilter(const PointDataTreeT &tree, const Index64 targetPoints, const unsigned int seed=0)
Definition: IndexFilter.h:234
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h.in:202
Definition: IndexIterator.h:42