37 #ifndef OPENVDB_POINTS_INDEX_FILTER_HAS_BEEN_INCLUDED 38 #define OPENVDB_POINTS_INDEX_FILTER_HAS_BEEN_INCLUDED 40 #include <openvdb/version.h> 54 #include <unordered_map> 57 class TestIndexFilter;
69 namespace index_filter_internal {
73 template <
typename RandGenT,
typename IntType>
75 generateRandomSubset(
const unsigned int seed,
const IntType n,
const IntType m)
77 if (n <= 0)
return std::vector<IntType>();
80 std::vector<IntType> values(m);
81 std::iota(values.begin(), values.end(), 0);
82 if (n >= m)
return values;
86 RandGenT randGen(seed);
87 std::shuffle(values.begin(), values.end(), randGen);
93 std::sort(values.begin(), values.end());
111 template <
typename LeafT>
119 template <
typename LeafT>
122 template <
typename IterT>
125 const bool valueOn = iter.isValueOn();
126 return On ? valueOn : !valueOn;
142 using IndexVector = std::vector<AttributeSet::Descriptor::GroupIndex>;
148 for (
const auto& name : names) {
150 indices.emplace_back(attributeSet.
groupIndex(name));
168 , mExclude(exclude) { }
171 : mInclude(filter.mInclude)
172 , mExclude(filter.mExclude)
173 , mIncludeHandles(filter.mIncludeHandles)
174 , mExcludeHandles(filter.mExcludeHandles)
175 , mInitialized(filter.mInitialized) { }
184 template <
typename LeafT>
187 template <
typename LeafT>
189 mIncludeHandles.clear();
190 mExcludeHandles.clear();
191 for (
const auto& i : mInclude) {
192 mIncludeHandles.emplace_back(leaf.groupHandle(i));
194 for (
const auto& i : mExclude) {
195 mExcludeHandles.emplace_back(leaf.groupHandle(i));
200 template <
typename IterT>
201 bool valid(
const IterT& iter)
const {
204 bool includeValid = mIncludeHandles.empty();
205 for (
const GroupHandle& handle : mIncludeHandles) {
206 if (handle.getUnsafe(*iter)) {
211 if (!includeValid)
return false;
212 for (
const GroupHandle& handle : mExcludeHandles) {
213 if (handle.getUnsafe(*iter))
return false;
223 bool mInitialized =
false;
228 template <
typename Po
intDataTreeT,
typename RandGenT>
233 using LeafMap = std::unordered_map<openvdb::Coord, SeedCountPair>;
237 const unsigned int seed = 0) {
239 for (
auto iter = tree.cbeginLeaf(); iter; ++iter) {
240 currentPoints += iter->pointCount();
243 const float factor = targetPoints > currentPoints ? 1.0f : float(targetPoints) / float(currentPoints);
245 std::mt19937 generator(seed);
249 float totalPointsFloat = 0.0f;
251 for (
auto iter = tree.cbeginLeaf(); iter; ++iter) {
253 if (leafCounter + 1 == tree.leafCount()) {
254 const int leafPoints =
static_cast<int>(targetPoints) - totalPoints;
255 mLeafMap[iter->origin()] =
SeedCountPair(dist(generator), leafPoints);
258 totalPointsFloat += factor *
static_cast<float>(iter->pointCount());
259 const auto leafPoints =
static_cast<int>(
math::Floor(totalPointsFloat));
260 totalPointsFloat -=
static_cast<float>(leafPoints);
261 totalPoints += leafPoints;
263 mLeafMap[iter->origin()] =
SeedCountPair(dist(generator), leafPoints);
272 template <
typename LeafT>
275 template <
typename LeafT>
277 using index_filter_internal::generateRandomSubset;
279 auto it = mLeafMap.find(leaf.origin());
280 if (it == mLeafMap.end()) {
282 "Cannot find leaf origin in map for random filter - " << leaf.origin());
286 const unsigned int seed =
static_cast<unsigned int>(value.first);
287 const auto total =
static_cast<Index>(leaf.pointCount());
288 mCount =
std::min(value.second, total);
290 mIndices = generateRandomSubset<RandGenT, int>(seed, mCount, total);
298 mNextIndex = mSubsetOffset >= mCount ?
300 mIndices[mSubsetOffset];
303 template <
typename IterT>
304 bool valid(
const IterT& iter)
const {
305 const int index = *iter;
306 while (mNextIndex < index) this->next();
307 return mNextIndex == index;
311 friend class ::TestIndexFilter;
315 std::vector<int> mIndices;
317 mutable int mSubsetOffset = -1;
318 mutable int mNextIndex = -1;
323 template <
typename RandGenT,
typename IntType>
330 const double percentage,
331 const unsigned int seed = 0)
333 , mFactor(percentage / 100.0)
337 : mIndex(filter.mIndex)
338 , mFactor(filter.mFactor)
339 , mSeed(filter.mSeed)
341 if (filter.mIdHandle) mIdHandle.reset(
new Handle(*filter.mIdHandle));
347 template <
typename LeafT>
350 template <
typename LeafT>
353 mIdHandle.reset(
new Handle(leaf.constAttributeArray(mIndex)));
356 template <
typename IterT>
357 bool valid(
const IterT& iter)
const {
359 const IntType
id = mIdHandle->get(*iter);
360 const unsigned int seed = mSeed +
static_cast<unsigned int>(id);
361 RandGenT generator(seed);
362 std::uniform_real_distribution<double> dist(0.0, 1.0);
363 return dist(generator) < mFactor;
368 const double mFactor;
369 const unsigned int mSeed;
374 template <
typename LevelSetGr
idT>
378 using ValueT =
typename LevelSetGridT::ValueType;
385 : mAccessor(grid.getConstAccessor())
386 , mLevelSetTransform(grid.transform())
387 , mTransform(transform)
392 : mAccessor(filter.mAccessor)
393 , mLevelSetTransform(filter.mLevelSetTransform)
394 , mTransform(filter.mTransform)
398 if (filter.mPositionHandle) mPositionHandle.reset(
new Handle(*filter.mPositionHandle));
404 template <
typename LeafT>
407 template <
typename LeafT>
409 mPositionHandle.reset(
new Handle(leaf.constAttributeArray(
"P")));
412 template <
typename IterT>
413 bool valid(
const IterT& iter)
const {
417 const openvdb::Coord ijk = iter.getCoord();
421 const openvdb::Vec3f& pointVoxelSpace = mPositionHandle->get(*iter);
424 const openvdb::Vec3f pointWorldSpace = mTransform.indexToWorld(pointVoxelSpace + voxelIndexSpace);
425 const openvdb::Vec3f pointIndexSpace = mLevelSetTransform.worldToIndex(pointWorldSpace);
428 const typename LevelSetGridT::ValueType value = tools::BoxSampler::sample(mAccessor, pointIndexSpace);
431 const bool invert = mMin > mMax;
433 return invert ? (value < mMax || value > mMin) : (value < mMax && value > mMin);
438 const typename LevelSetGridT::ConstAccessor mAccessor;
455 : mTransform(transform)
456 , mBbox(transform.worldToIndex(bboxWS)) { }
459 : mTransform(filter.mTransform)
460 , mBbox(filter.mBbox)
462 if (filter.mPositionHandle) mPositionHandle.reset(
new Handle(*filter.mPositionHandle));
471 template <
typename LeafT>
474 template <
typename LeafT>
476 mPositionHandle.reset(
new Handle(leaf.constAttributeArray(
"P")));
479 template <
typename IterT>
480 bool valid(
const IterT& iter)
const {
483 const openvdb::Coord ijk = iter.getCoord();
487 const openvdb::Vec3f& pointVoxelSpace = mPositionHandle->get(*iter);
490 const openvdb::Vec3f pointIndexSpace = pointVoxelSpace + voxelIndexSpace;
492 return mBbox.isInside(pointIndexSpace);
496 const openvdb::math::Transform& mTransform;
503 template <
typename T1,
typename T2,
bool And = true>
510 , mFilter2(filter2) { }
512 inline bool initialized()
const {
return mFilter1.initialized() && mFilter2.initialized(); }
516 return this->computeState(mFilter1.state(), mFilter2.state());
518 template <
typename LeafT>
521 return this->computeState(mFilter1.state(leaf), mFilter2.state(leaf));
524 template <
typename LeafT>
526 mFilter1.reset(leaf);
527 mFilter2.reset(leaf);
530 template <
typename IterT>
531 bool valid(
const IterT& iter)
const {
532 if (And)
return mFilter1.valid(iter) && mFilter2.valid(iter);
533 return mFilter1.valid(iter) || mFilter2.valid(iter);
560 static const bool RequiresCoord =
false;
564 static const bool RequiresCoord =
true;
566 template <
typename T>
568 static const bool RequiresCoord =
true;
570 template <
typename T0,
typename T1,
bool And>
584 #endif // OPENVDB_POINTS_INDEX_FILTER_HAS_BEEN_INCLUDED Definition: IndexFilter.h:324
Definition: IndexFilter.h:375
std::unordered_map< openvdb::Coord, SeedCountPair > LeafMap
Definition: IndexFilter.h:233
static bool initialized()
Definition: IndexFilter.h:109
bool valid(const IterT &iter) const
Definition: IndexFilter.h:201
Attribute Group access and filtering for iteration.
Definition: Exceptions.h:60
LevelSetFilter(const LevelSetGridT &grid, const math::Transform &transform, const ValueT min, const ValueT max)
Definition: IndexFilter.h:381
#define OPENVDB_THROW(exception, message)
Definition: Exceptions.h:74
static index::State state()
Definition: IndexFilter.h:403
bool valid(const IterT &iter) const
Definition: IndexFilter.h:357
uint64_t Index64
Definition: Types.h:53
index::State state() const
Definition: IndexFilter.h:179
void reset(const LeafT &leaf)
Definition: IndexFilter.h:408
bool valid(const IterT &iter) const
Definition: IndexFilter.h:531
static index::State state(const LeafT &leaf)
Definition: IndexFilter.h:112
Definition: IndexFilter.h:138
AttributeHashFilter(const size_t index, const double percentage, const unsigned int seed=0)
Definition: IndexFilter.h:329
static index::State state(const LeafT &)
Definition: IndexFilter.h:273
Attribute Array storage templated on type and compression codec.
BBoxFilter(const BBoxFilter &filter)
Definition: IndexFilter.h:458
bool valid(const IterT &iter) const
Definition: IndexFilter.h:480
AttributeHashFilter(const AttributeHashFilter &filter)
Definition: IndexFilter.h:336
void reset(const LeafT &leaf)
Definition: IndexFilter.h:276
std::unique_ptr< Handle > UniquePtr
Definition: AttributeArray.h:768
Index filtering on active / inactive state of host voxel.
Definition: IndexFilter.h:106
static index::State state()
Definition: IndexFilter.h:110
Definition: AttributeArray.h:763
MultiGroupFilter(const MultiGroupFilter &filter)
Definition: IndexFilter.h:170
Definition: IndexFilter.h:448
void reset(const LeafT &leaf)
Definition: IndexFilter.h:351
Index32 Index
Definition: Types.h:54
index::State state() const
Definition: IndexFilter.h:514
bool initialized() const
Definition: IndexFilter.h:177
Definition: IndexIterator.h:44
bool initialized() const
Definition: IndexFilter.h:465
BBoxFilter(const openvdb::math::Transform &transform, const openvdb::BBoxd &bboxWS)
Definition: IndexFilter.h:453
Definition: IndexIterator.h:43
Definition: AttributeGroup.h:73
LevelSetFilter(const LevelSetFilter &filter)
Definition: IndexFilter.h:391
#define OPENVDB_ASSERT(X)
Definition: Assert.h:41
Definition: IndexFilter.h:504
void reset(const LeafT &)
Definition: IndexFilter.h:120
bool valid(const IterT &iter) const
Definition: IndexFilter.h:413
static index::State state()
Definition: IndexFilter.h:346
std::pair< Index, Index > SeedCountPair
Definition: IndexFilter.h:232
int Floor(float x)
Return the floor of x.
Definition: Math.h:848
void next() const
Definition: IndexFilter.h:296
static index::State state()
Definition: IndexFilter.h:271
Definition: Exceptions.h:13
std::vector< Name > NameVector
Definition: IndexFilter.h:141
MultiGroupFilter(const NameVector &include, const NameVector &exclude, const AttributeSet &attributeSet)
Definition: IndexFilter.h:159
bool valid(const IterT &iter) const
Definition: IndexFilter.h:304
void reset(const LeafT &leaf)
Definition: IndexFilter.h:475
RandomLeafFilter(const PointDataTreeT &tree, const Index64 targetPoints, const unsigned int seed=0)
Definition: IndexFilter.h:235
bool initialized() const
Definition: IndexFilter.h:344
Definition: IndexFilter.h:559
State
Definition: IndexIterator.h:40
Definition: IndexFilter.h:229
bool initialized() const
Definition: IndexFilter.h:401
MultiGroupFilter(const IndexVector &include, const IndexVector &exclude)
Definition: IndexFilter.h:165
void reset(const LeafT &leaf)
Definition: IndexFilter.h:525
bool valid(const IterT &iter) const
Definition: IndexFilter.h:123
void reset(const LeafT &leaf)
Definition: IndexFilter.h:188
index::State state() const
Definition: IndexFilter.h:467
std::vector< GroupHandle > HandleVector
Definition: IndexFilter.h:143
std::vector< AttributeSet::Descriptor::GroupIndex > IndexVector
Definition: IndexFilter.h:142
Util::GroupIndex groupIndex(const Name &groupName) const
Return the group index from the name of the group.
Ordered collection of uniquely-named attribute arrays.
Definition: AttributeSet.h:39
bool initialized() const
Definition: IndexFilter.h:512
static index::State state(const LeafT &)
Definition: IndexFilter.h:185
#define OPENVDB_VERSION_NAME
The version namespace name for this library version.
Definition: version.h.in:121
Definition: IndexIterator.h:42
bool initialized() const
Definition: IndexFilter.h:269
static index::State state(const LeafT &)
Definition: IndexFilter.h:472
Set of Attribute Arrays which tracks metadata about each array.
static index::State state(const LeafT &)
Definition: IndexFilter.h:348
index::State state(const LeafT &leaf) const
Definition: IndexFilter.h:519
typename LevelSetGridT::ValueType ValueT
Definition: IndexFilter.h:378
Definition: Exceptions.h:59
BinaryFilter(const T1 &filter1, const T2 &filter2)
Definition: IndexFilter.h:507
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h.in:218
static index::State state(const LeafT &)
Definition: IndexFilter.h:405