64 #ifndef OPENVDB_POINTS_INDEX_FILTER_HAS_BEEN_INCLUDED 65 #define OPENVDB_POINTS_INDEX_FILTER_HAS_BEEN_INCLUDED 78 #include <boost/ptr_container/ptr_vector.hpp> 82 #include <unordered_map> 85 class TestIndexFilter;
97 namespace index_filter_internal {
101 template <
typename RandGenT,
typename IntType>
105 if (n <= 0)
return std::vector<IntType>();
108 std::vector<IntType> values(m);
109 std::iota(values.begin(), values.end(), 0);
110 if (n >= m)
return values;
114 RandGenT randGen(seed);
115 std::shuffle(values.begin(), values.end(), randGen);
121 std::sort(values.begin(), values.end());
137 template <
typename LeafT>
145 template <
typename LeafT>
148 template <
typename IterT>
151 const bool valueOn = iter.isValueOn();
152 return On ? valueOn : !valueOn;
168 using IndexVector = std::vector<AttributeSet::Descriptor::GroupIndex>;
174 for (
const auto& name : names) {
176 indices.emplace_back(attributeSet.
groupIndex(name));
194 , mExclude(exclude) { }
197 : mInclude(filter.mInclude)
198 , mExclude(filter.mExclude)
199 , mIncludeHandles(filter.mIncludeHandles)
200 , mExcludeHandles(filter.mExcludeHandles)
201 , mInitialized(filter.mInitialized) { }
210 template <
typename LeafT>
213 template <
typename LeafT>
215 mIncludeHandles.clear();
216 mExcludeHandles.clear();
217 for (
const auto& i : mInclude) {
218 mIncludeHandles.emplace_back(leaf.groupHandle(i));
220 for (
const auto& i : mExclude) {
221 mExcludeHandles.emplace_back(leaf.groupHandle(i));
226 template <
typename IterT>
227 bool valid(
const IterT& iter)
const {
228 assert(mInitialized);
230 bool includeValid = mIncludeHandles.empty();
231 for (
const GroupHandle& handle : mIncludeHandles) {
232 if (handle.getUnsafe(*iter)) {
237 if (!includeValid)
return false;
238 for (
const GroupHandle& handle : mExcludeHandles) {
239 if (handle.getUnsafe(*iter))
return false;
245 IndexVector mInclude;
246 IndexVector mExclude;
247 HandleVector mIncludeHandles;
248 HandleVector mExcludeHandles;
249 bool mInitialized =
false;
254 template <
typename Po
intDataTreeT,
typename RandGenT>
259 using LeafMap = std::unordered_map<openvdb::Coord, SeedCountPair>;
263 const unsigned int seed = 0) {
265 for (
auto iter = tree.cbeginLeaf(); iter; ++iter) {
266 currentPoints += iter->pointCount();
269 const float factor = targetPoints > currentPoints ? 1.0f : float(targetPoints) / float(currentPoints);
271 std::mt19937 generator(seed);
275 float totalPointsFloat = 0.0f;
277 for (
auto iter = tree.cbeginLeaf(); iter; ++iter) {
279 if (leafCounter + 1 == tree.leafCount()) {
280 const int leafPoints = static_cast<int>(targetPoints) - totalPoints;
281 mLeafMap[iter->origin()] =
SeedCountPair(dist(generator), leafPoints);
284 totalPointsFloat += factor * static_cast<float>(iter->pointCount());
285 const auto leafPoints = static_cast<int>(
math::Floor(totalPointsFloat));
286 totalPointsFloat -= static_cast<float>(leafPoints);
287 totalPoints += leafPoints;
289 mLeafMap[iter->origin()] =
SeedCountPair(dist(generator), leafPoints);
298 template <
typename LeafT>
301 template <
typename LeafT>
305 auto it = mLeafMap.find(leaf.origin());
306 if (it == mLeafMap.end()) {
308 "Cannot find leaf origin in map for random filter - " << leaf.origin());
312 const unsigned int seed = static_cast<unsigned int>(value.first);
313 const auto total = static_cast<Index>(leaf.pointCount());
314 mCount =
std::min(value.second, total);
316 mIndices = generateRandomSubset<RandGenT, int>(seed, mCount, total);
324 mNextIndex = mSubsetOffset >= mCount ?
326 mIndices[mSubsetOffset];
329 template <
typename IterT>
330 bool valid(
const IterT& iter)
const {
331 const int index = *iter;
332 while (mNextIndex < index) this->next();
333 return mNextIndex == index;
337 friend class ::TestIndexFilter;
341 std::vector<int> mIndices;
343 mutable int mSubsetOffset = -1;
344 mutable int mNextIndex = -1;
349 template <
typename RandGenT,
typename IntType>
356 const double percentage,
357 const unsigned int seed = 0)
359 , mFactor(percentage / 100.0)
363 : mIndex(filter.mIndex)
364 , mFactor(filter.mFactor)
365 , mSeed(filter.mSeed)
367 if (filter.mIdHandle) mIdHandle.reset(
new Handle(*filter.mIdHandle));
373 template <
typename LeafT>
376 template <
typename LeafT>
378 assert(leaf.hasAttribute(mIndex));
379 mIdHandle.reset(
new Handle(leaf.constAttributeArray(mIndex)));
382 template <
typename IterT>
383 bool valid(
const IterT& iter)
const {
385 const IntType
id = mIdHandle->get(*iter);
386 const unsigned int seed = mSeed + static_cast<unsigned int>(
id);
387 RandGenT generator(seed);
388 std::uniform_real_distribution<double> dist(0.0, 1.0);
389 return dist(generator) < mFactor;
394 const double mFactor;
395 const unsigned int mSeed;
396 typename Handle::UniquePtr mIdHandle;
400 template <
typename LevelSetGr
idT>
404 using ValueT =
typename LevelSetGridT::ValueType;
411 : mAccessor(grid.getConstAccessor())
412 , mLevelSetTransform(grid.transform())
413 , mTransform(transform)
418 : mAccessor(filter.mAccessor)
419 , mLevelSetTransform(filter.mLevelSetTransform)
420 , mTransform(filter.mTransform)
424 if (filter.mPositionHandle) mPositionHandle.reset(
new Handle(*filter.mPositionHandle));
430 template <
typename LeafT>
433 template <
typename LeafT>
435 mPositionHandle.reset(
new Handle(leaf.constAttributeArray(
"P")));
438 template <
typename IterT>
439 bool valid(
const IterT& iter)
const {
440 assert(mPositionHandle);
447 const openvdb::Vec3f& pointVoxelSpace = mPositionHandle->get(*iter);
450 const openvdb::Vec3f pointWorldSpace = mTransform.indexToWorld(pointVoxelSpace + voxelIndexSpace);
451 const openvdb::Vec3f pointIndexSpace = mLevelSetTransform.worldToIndex(pointWorldSpace);
454 const typename LevelSetGridT::ValueType value = tools::BoxSampler::sample(mAccessor, pointIndexSpace);
457 const bool invert = mMin > mMax;
459 return invert ? (value < mMax || value > mMin) : (value < mMax && value > mMin);
464 const typename LevelSetGridT::ConstAccessor mAccessor;
469 Handle::UniquePtr mPositionHandle;
481 : mTransform(transform)
482 , mBbox(transform.worldToIndex(bboxWS)) { }
485 : mTransform(filter.mTransform)
486 , mBbox(filter.mBbox)
488 if (filter.mPositionHandle) mPositionHandle.reset(
new Handle(*filter.mPositionHandle));
497 template <
typename LeafT>
500 template <
typename LeafT>
502 mPositionHandle.reset(
new Handle(leaf.constAttributeArray(
"P")));
505 template <
typename IterT>
506 bool valid(
const IterT& iter)
const {
507 assert(mPositionHandle);
513 const openvdb::Vec3f& pointVoxelSpace = mPositionHandle->get(*iter);
516 const openvdb::Vec3f pointIndexSpace = pointVoxelSpace + voxelIndexSpace;
518 return mBbox.isInside(pointIndexSpace);
522 const openvdb::math::Transform& mTransform;
524 Handle::UniquePtr mPositionHandle;
529 template <
typename T1,
typename T2,
bool And = true>
536 , mFilter2(filter2) { }
538 inline bool initialized()
const {
return mFilter1.initialized() && mFilter2.initialized(); }
542 return this->computeState(mFilter1.state(), mFilter2.state());
544 template <
typename LeafT>
547 return this->computeState(mFilter1.state(leaf), mFilter2.state(leaf));
550 template <
typename LeafT>
552 mFilter1.reset(leaf);
553 mFilter2.reset(leaf);
556 template <
typename IterT>
557 bool valid(
const IterT& iter)
const {
558 if (And)
return mFilter1.valid(iter) && mFilter2.valid(iter);
559 return mFilter1.valid(iter) || mFilter2.valid(iter);
586 static const bool RequiresCoord =
false;
590 static const bool RequiresCoord =
true;
592 template <
typename T>
594 static const bool RequiresCoord =
true;
596 template <
typename T0,
typename T1,
bool And>
610 #endif // OPENVDB_POINTS_INDEX_FILTER_HAS_BEEN_INCLUDED bool initialized() const
Definition: IndexFilter.h:203
BBoxFilter(const BBoxFilter &filter)
Definition: IndexFilter.h:484
Attribute Group access and filtering for iteration.
MultiGroupFilter(const MultiGroupFilter &filter)
Definition: IndexFilter.h:196
void reset(const LeafT &leaf)
Definition: IndexFilter.h:551
Definition: IndexFilter.h:474
static index::State state()
Definition: IndexFilter.h:372
std::vector< Name > NameVector
Definition: IndexFilter.h:167
Definition: IndexFilter.h:164
#define OPENVDB_THROW(exception, message)
Definition: Exceptions.h:109
Attribute Array storage templated on type and compression codec.
Signed (x, y, z) 32-bit integer coordinates.
Definition: Coord.h:52
Definition: IndexFilter.h:255
bool valid(const IterT &iter) const
Definition: IndexFilter.h:557
static index::State state(const LeafT &)
Definition: IndexFilter.h:211
void reset(const LeafT &leaf)
Definition: IndexFilter.h:434
LevelSetFilter(const LevelSetFilter &filter)
Definition: IndexFilter.h:417
static index::State state(const LeafT &)
Definition: IndexFilter.h:498
index::State state() const
Definition: IndexFilter.h:493
BinaryFilter(const T1 &filter1, const T2 &filter2)
Definition: IndexFilter.h:533
index::State state() const
Definition: IndexFilter.h:205
MultiGroupFilter(const IndexVector &include, const IndexVector &exclude)
Definition: IndexFilter.h:191
Definition: IndexFilter.h:585
std::unordered_map< openvdb::Coord, SeedCountPair > LeafMap
Definition: IndexFilter.h:259
Definition: IndexFilter.h:401
void reset(const LeafT &leaf)
Definition: IndexFilter.h:214
static index::State state()
Definition: IndexFilter.h:429
Ordered collection of uniquely-named attribute arrays.
Definition: AttributeSet.h:62
MultiGroupFilter(const NameVector &include, const NameVector &exclude, const AttributeSet &attributeSet)
Definition: IndexFilter.h:185
#define OPENVDB_VERSION_NAME
The version namespace name for this library version.
Definition: version.h:128
static bool initialized()
Definition: IndexFilter.h:135
static index::State state()
Definition: IndexFilter.h:297
std::pair< Index, Index > SeedCountPair
Definition: IndexFilter.h:258
void reset(const LeafT &leaf)
Definition: IndexFilter.h:377
Index filtering on active / inactive state of host voxel.
Definition: IndexFilter.h:132
bool valid(const IterT &iter) const
Definition: IndexFilter.h:330
bool valid(const IterT &iter) const
Definition: IndexFilter.h:149
Definition: Exceptions.h:40
Vec3d asVec3d() const
Definition: Coord.h:171
std::vector< IntType > generateRandomSubset(const unsigned int seed, const IntType n, const IntType m)
Definition: IndexFilter.h:103
Definition: Exceptions.h:87
AttributeHashFilter(const size_t index, const double percentage, const unsigned int seed=0)
Definition: IndexFilter.h:355
bool initialized() const
Definition: IndexFilter.h:295
AttributeHashFilter(const AttributeHashFilter &filter)
Definition: IndexFilter.h:362
typename LevelSetGridT::ValueType ValueT
Definition: IndexFilter.h:404
bool initialized() const
Definition: IndexFilter.h:491
BBoxFilter(const openvdb::math::Transform &transform, const openvdb::BBoxd &bboxWS)
Definition: IndexFilter.h:479
uint64_t Index64
Definition: Types.h:60
Util::GroupIndex groupIndex(const Name &groupName) const
Return the group index from the name of the group.
Definition: AttributeArray.h:873
Library and file format version numbers.
RandomLeafFilter(const PointDataTreeT &tree, const Index64 targetPoints, const unsigned int seed=0)
Definition: IndexFilter.h:261
bool initialized() const
Definition: IndexFilter.h:538
static index::State state()
Definition: IndexFilter.h:136
void reset(const LeafT &leaf)
Definition: IndexFilter.h:501
static index::State state(const LeafT &)
Definition: IndexFilter.h:374
bool valid(const IterT &iter) const
Definition: IndexFilter.h:383
void next() const
Definition: IndexFilter.h:322
Definition: AttributeGroup.h:102
static index::State state(const LeafT &)
Definition: IndexFilter.h:431
Definition: IndexIterator.h:70
static index::State state(const LeafT &leaf)
Definition: IndexFilter.h:138
bool valid(const IterT &iter) const
Definition: IndexFilter.h:506
index::State state(const LeafT &leaf) const
Definition: IndexFilter.h:545
bool initialized() const
Definition: IndexFilter.h:427
Definition: IndexFilter.h:530
bool initialized() const
Definition: IndexFilter.h:370
void reset(const LeafT &)
Definition: IndexFilter.h:146
bool valid(const IterT &iter) const
Definition: IndexFilter.h:439
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h:180
static index::State state(const LeafT &)
Definition: IndexFilter.h:299
index::State state() const
Definition: IndexFilter.h:540
Definition: Exceptions.h:86
void reset(const LeafT &leaf)
Definition: IndexFilter.h:302
bool valid(const IterT &iter) const
Definition: IndexFilter.h:227
Definition: IndexIterator.h:68
Set of Attribute Arrays which tracks metadata about each array.
uint32_t Index32
Definition: Types.h:59
std::vector< AttributeSet::Descriptor::GroupIndex > IndexVector
Definition: IndexFilter.h:168
State
Definition: IndexIterator.h:66
Definition: IndexFilter.h:350
int Floor(float x)
Return the floor of x.
Definition: Math.h:822
Definition: IndexIterator.h:69
LevelSetFilter(const LevelSetGridT &grid, const math::Transform &transform, const ValueT min, const ValueT max)
Definition: IndexFilter.h:407
std::vector< GroupHandle > HandleVector
Definition: IndexFilter.h:169