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> 84 class TestIndexFilter;
96 namespace index_filter_internal {
100 template <
typename RandGenT,
typename IntType>
104 if (n <= 0)
return std::vector<IntType>();
107 std::vector<IntType> values(m);
108 std::iota(values.begin(), values.end(), 0);
109 if (n >= m)
return values;
113 RandGenT randGen(seed);
114 std::shuffle(values.begin(), values.end(), randGen);
120 std::sort(values.begin(), values.end());
136 template <
typename LeafT>
144 template <
typename LeafT>
147 template <
typename IterT>
150 const bool valueOn = iter.isValueOn();
151 return On ? valueOn : !valueOn;
167 using IndexVector = std::vector<AttributeSet::Descriptor::GroupIndex>;
173 for (
const auto& name : names) {
175 indices.emplace_back(attributeSet.
groupIndex(name));
193 , mExclude(exclude) { }
196 : mInclude(filter.mInclude)
197 , mExclude(filter.mExclude)
198 , mIncludeHandles(filter.mIncludeHandles)
199 , mExcludeHandles(filter.mExcludeHandles)
200 , mInitialized(filter.mInitialized) { }
209 template <
typename LeafT>
212 template <
typename LeafT>
214 mIncludeHandles.clear();
215 mExcludeHandles.clear();
216 for (
const auto& i : mInclude) {
217 mIncludeHandles.emplace_back(leaf.groupHandle(i));
219 for (
const auto& i : mExclude) {
220 mExcludeHandles.emplace_back(leaf.groupHandle(i));
225 template <
typename IterT>
226 bool valid(
const IterT& iter)
const {
227 assert(mInitialized);
229 bool includeValid = mIncludeHandles.empty();
230 for (
const GroupHandle& handle : mIncludeHandles) {
231 if (handle.getUnsafe(*iter)) {
236 if (!includeValid)
return false;
237 for (
const GroupHandle& handle : mExcludeHandles) {
238 if (handle.getUnsafe(*iter))
return false;
244 IndexVector mInclude;
245 IndexVector mExclude;
246 HandleVector mIncludeHandles;
247 HandleVector mExcludeHandles;
248 bool mInitialized =
false;
253 template <
typename Po
intDataTreeT,
typename RandGenT>
258 using LeafMap = std::map<openvdb::Coord, SeedCountPair>;
262 const unsigned int seed = 0) {
264 for (
auto iter = tree.cbeginLeaf(); iter; ++iter) {
265 currentPoints += iter->pointCount();
268 const float factor = targetPoints > currentPoints ? 1.0f : float(targetPoints) / float(currentPoints);
270 std::mt19937 generator(seed);
274 float totalPointsFloat = 0.0f;
276 for (
auto iter = tree.cbeginLeaf(); iter; ++iter) {
278 if (leafCounter + 1 == tree.leafCount()) {
279 const int leafPoints = static_cast<int>(targetPoints) - totalPoints;
280 mLeafMap[iter->origin()] =
SeedCountPair(dist(generator), leafPoints);
283 totalPointsFloat += factor * iter->pointCount();
284 const auto leafPoints = static_cast<int>(
math::Floor(totalPointsFloat));
285 totalPointsFloat -= static_cast<float>(leafPoints);
286 totalPoints += leafPoints;
288 mLeafMap[iter->origin()] =
SeedCountPair(dist(generator), leafPoints);
297 template <
typename LeafT>
300 template <
typename LeafT>
304 auto it = mLeafMap.find(leaf.origin());
305 if (it == mLeafMap.end()) {
307 "Cannot find leaf origin in map for random filter - " << leaf.origin());
311 const unsigned int seed = static_cast<unsigned int>(value.first);
312 const auto total = static_cast<Index>(leaf.pointCount());
313 mCount =
std::min(value.second, total);
315 mIndices = generateRandomSubset<RandGenT, int>(seed, mCount, total);
323 mNextIndex = mSubsetOffset >= mCount ?
325 mIndices[mSubsetOffset];
328 template <
typename IterT>
329 bool valid(
const IterT& iter)
const {
330 const int index = *iter;
331 while (mNextIndex < index) this->next();
332 return mNextIndex == index;
336 friend class ::TestIndexFilter;
340 std::vector<int> mIndices;
342 mutable int mSubsetOffset = -1;
343 mutable int mNextIndex = -1;
348 template <
typename RandGenT,
typename IntType>
355 const double percentage,
356 const unsigned int seed = 0)
358 , mFactor(percentage / 100.0)
362 : mIndex(filter.mIndex)
363 , mFactor(filter.mFactor)
364 , mSeed(filter.mSeed)
366 if (filter.mIdHandle) mIdHandle.reset(
new Handle(*filter.mIdHandle));
372 template <
typename LeafT>
375 template <
typename LeafT>
377 assert(leaf.hasAttribute(mIndex));
378 mIdHandle.reset(
new Handle(leaf.constAttributeArray(mIndex)));
381 template <
typename IterT>
382 bool valid(
const IterT& iter)
const {
384 const IntType
id = mIdHandle->get(*iter);
385 const unsigned int seed = mSeed + static_cast<unsigned int>(
id);
386 RandGenT generator(seed);
387 std::uniform_real_distribution<double> dist(0.0, 1.0);
388 return dist(generator) < mFactor;
393 const double mFactor;
394 const unsigned int mSeed;
395 typename Handle::UniquePtr mIdHandle;
399 template <
typename LevelSetGr
idT>
403 using ValueT =
typename LevelSetGridT::ValueType;
410 : mAccessor(grid.getConstAccessor())
411 , mLevelSetTransform(grid.transform())
412 , mTransform(transform)
417 : mAccessor(filter.mAccessor)
418 , mLevelSetTransform(filter.mLevelSetTransform)
419 , mTransform(filter.mTransform)
423 if (filter.mPositionHandle) mPositionHandle.reset(
new Handle(*filter.mPositionHandle));
429 template <
typename LeafT>
432 template <
typename LeafT>
434 mPositionHandle.reset(
new Handle(leaf.constAttributeArray(
"P")));
437 template <
typename IterT>
438 bool valid(
const IterT& iter)
const {
439 assert(mPositionHandle);
446 const openvdb::Vec3f& pointVoxelSpace = mPositionHandle->get(*iter);
449 const openvdb::Vec3f pointWorldSpace = mTransform.indexToWorld(pointVoxelSpace + voxelIndexSpace);
450 const openvdb::Vec3f pointIndexSpace = mLevelSetTransform.worldToIndex(pointWorldSpace);
453 const typename LevelSetGridT::ValueType value = tools::BoxSampler::sample(mAccessor, pointIndexSpace);
456 const bool invert = mMin > mMax;
458 return invert ? (value < mMax || value > mMin) : (value < mMax && value > mMin);
463 const typename LevelSetGridT::ConstAccessor mAccessor;
468 Handle::UniquePtr mPositionHandle;
480 : mTransform(transform)
481 , mBbox(transform.worldToIndex(bboxWS)) { }
484 : mTransform(filter.mTransform)
485 , mBbox(filter.mBbox)
487 if (filter.mPositionHandle) mPositionHandle.reset(
new Handle(*filter.mPositionHandle));
496 template <
typename LeafT>
499 template <
typename LeafT>
501 mPositionHandle.reset(
new Handle(leaf.constAttributeArray(
"P")));
504 template <
typename IterT>
505 bool valid(
const IterT& iter)
const {
506 assert(mPositionHandle);
512 const openvdb::Vec3f& pointVoxelSpace = mPositionHandle->get(*iter);
515 const openvdb::Vec3f pointIndexSpace = pointVoxelSpace + voxelIndexSpace;
517 return mBbox.isInside(pointIndexSpace);
521 const openvdb::math::Transform& mTransform;
523 Handle::UniquePtr mPositionHandle;
528 template <
typename T1,
typename T2,
bool And = true>
535 , mFilter2(filter2) { }
537 inline bool initialized()
const {
return mFilter1.initialized() && mFilter2.initialized(); }
541 return this->computeState(mFilter1.state(), mFilter2.state());
543 template <
typename LeafT>
546 return this->computeState(mFilter1.state(leaf), mFilter2.state(leaf));
549 template <
typename LeafT>
551 mFilter1.reset(leaf);
552 mFilter2.reset(leaf);
555 template <
typename IterT>
556 bool valid(
const IterT& iter)
const {
557 if (And)
return mFilter1.valid(iter) && mFilter2.valid(iter);
558 return mFilter1.valid(iter) || mFilter2.valid(iter);
585 static const bool RequiresCoord =
false;
589 static const bool RequiresCoord =
true;
591 template <
typename T>
593 static const bool RequiresCoord =
true;
595 template <
typename T0,
typename T1,
bool And>
609 #endif // OPENVDB_POINTS_INDEX_FILTER_HAS_BEEN_INCLUDED Definition: IndexIterator.h:68
Definition: IndexFilter.h:349
Definition: IndexIterator.h:69
Attribute Group access and filtering for iteration.
AttributeHashFilter(const size_t index, const double percentage, const unsigned int seed=0)
Definition: IndexFilter.h:354
AttributeHashFilter(const AttributeHashFilter &filter)
Definition: IndexFilter.h:361
uint64_t Index64
Definition: Types.h:60
static index::State state(const LeafT &)
Definition: IndexFilter.h:210
bool valid(const IterT &iter) const
Definition: IndexFilter.h:226
index::State state() const
Definition: IndexFilter.h:492
bool valid(const IterT &iter) const
Definition: IndexFilter.h:329
void reset(const LeafT &leaf)
Definition: IndexFilter.h:301
static index::State state()
Definition: IndexFilter.h:296
#define OPENVDB_THROW(exception, message)
Definition: Exceptions.h:109
Attribute Array storage templated on type and compression codec.
Definition: IndexFilter.h:400
void reset(const LeafT &leaf)
Definition: IndexFilter.h:213
MultiGroupFilter(const NameVector &include, const NameVector &exclude, const AttributeSet &attributeSet)
Definition: IndexFilter.h:184
bool initialized() const
Definition: IndexFilter.h:202
bool initialized() const
Definition: IndexFilter.h:537
void reset(const LeafT &leaf)
Definition: IndexFilter.h:433
LevelSetFilter(const LevelSetFilter &filter)
Definition: IndexFilter.h:416
bool initialized() const
Definition: IndexFilter.h:369
Signed (x, y, z) 32-bit integer coordinates.
Definition: Coord.h:51
int Floor(float x)
Return the floor of x.
Definition: Math.h:802
MultiGroupFilter(const MultiGroupFilter &filter)
Definition: IndexFilter.h:195
Definition: IndexFilter.h:584
index::State state() const
Definition: IndexFilter.h:539
Definition: Exceptions.h:86
index::State state(const LeafT &leaf) const
Definition: IndexFilter.h:544
std::vector< Name > NameVector
Definition: IndexFilter.h:166
static index::State state(const LeafT &)
Definition: IndexFilter.h:373
void next() const
Definition: IndexFilter.h:321
static bool initialized()
Definition: IndexFilter.h:134
uint32_t Index32
Definition: Types.h:59
static index::State state()
Definition: IndexFilter.h:428
Ordered collection of uniquely-named attribute arrays.
Definition: AttributeSet.h:62
bool initialized() const
Definition: IndexFilter.h:490
std::vector< IntType > generateRandomSubset(const unsigned int seed, const IntType n, const IntType m)
Definition: IndexFilter.h:102
BBoxFilter(const openvdb::math::Transform &transform, const openvdb::BBoxd &bboxWS)
Definition: IndexFilter.h:478
State
Definition: IndexIterator.h:66
#define OPENVDB_VERSION_NAME
The version namespace name for this library version.
Definition: version.h:136
std::pair< Index, Index > SeedCountPair
Definition: IndexFilter.h:257
static index::State state(const LeafT &)
Definition: IndexFilter.h:497
Definition: IndexFilter.h:254
index::State state() const
Definition: IndexFilter.h:204
MultiGroupFilter(const IndexVector &include, const IndexVector &exclude)
Definition: IndexFilter.h:190
Vec3d asVec3d() const
Definition: Coord.h:170
Definition: Exceptions.h:87
std::map< openvdb::Coord, SeedCountPair > LeafMap
Definition: IndexFilter.h:258
RandomLeafFilter(const PointDataTreeT &tree, const Index64 targetPoints, const unsigned int seed=0)
Definition: IndexFilter.h:260
Definition: Exceptions.h:40
bool valid(const IterT &iter) const
Definition: IndexFilter.h:505
typename LevelSetGridT::ValueType ValueT
Definition: IndexFilter.h:403
Definition: IndexFilter.h:163
bool valid(const IterT &iter) const
Definition: IndexFilter.h:148
bool valid(const IterT &iter) const
Definition: IndexFilter.h:382
Index filtering on active / inactive state of host voxel.
Definition: IndexFilter.h:131
Library and file format version numbers.
bool valid(const IterT &iter) const
Definition: IndexFilter.h:556
void reset(const LeafT &leaf)
Definition: IndexFilter.h:550
static index::State state()
Definition: IndexFilter.h:135
Definition: IndexFilter.h:473
static index::State state(const LeafT &)
Definition: IndexFilter.h:430
Definition: AttributeArray.h:832
std::vector< AttributeSet::Descriptor::GroupIndex > IndexVector
Definition: IndexFilter.h:167
Definition: IndexIterator.h:70
bool initialized() const
Definition: IndexFilter.h:294
void reset(const LeafT &)
Definition: IndexFilter.h:145
static index::State state(const LeafT &leaf)
Definition: IndexFilter.h:137
Definition: IndexFilter.h:529
bool initialized() const
Definition: IndexFilter.h:426
std::vector< GroupHandle > HandleVector
Definition: IndexFilter.h:168
static index::State state()
Definition: IndexFilter.h:371
Definition: AttributeGroup.h:102
bool valid(const IterT &iter) const
Definition: IndexFilter.h:438
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h:188
void reset(const LeafT &leaf)
Definition: IndexFilter.h:500
BBoxFilter(const BBoxFilter &filter)
Definition: IndexFilter.h:483
BinaryFilter(const T1 &filter1, const T2 &filter2)
Definition: IndexFilter.h:532
static index::State state(const LeafT &)
Definition: IndexFilter.h:298
Set of Attribute Arrays which tracks metadata about each array.
Util::GroupIndex groupIndex(const Name &groupName) const
Return the group index from the name of the group.
void reset(const LeafT &leaf)
Definition: IndexFilter.h:376
LevelSetFilter(const LevelSetGridT &grid, const math::Transform &transform, const ValueT min, const ValueT max)
Definition: IndexFilter.h:406