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>
69namespace index_filter_internal {
73template <
typename RandGenT,
typename IntType>
75generateRandomSubset(
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;
219 IndexVector mInclude;
220 IndexVector mExclude;
221 HandleVector mIncludeHandles;
222 HandleVector mExcludeHandles;
223 bool mInitialized =
false;
228template <
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);
246 std::uniform_int_distribution<unsigned int> dist(0, std::numeric_limits<unsigned int>::max() - 1);
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 ?
299 std::numeric_limits<int>::max() :
300 mIndices[mSubsetOffset];
303 template <
typename IterT>
304 bool valid(
const IterT& iter)
const {
305 const int index = *iter;
307 return mNextIndex ==
index;
311 friend class ::TestIndexFilter;
315 std::vector<int> mIndices;
317 mutable int mSubsetOffset = -1;
318 mutable int mNextIndex = -1;
323template <
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;
370 typename Handle::UniquePtr mIdHandle;
374template <
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 {
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);
431 const bool invert = mMin > mMax;
433 return invert ? (value < mMax || value > mMin) : (value < mMax && value > mMin);
438 const typename LevelSetGridT::ConstAccessor mAccessor;
443 Handle::UniquePtr mPositionHandle;
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 {
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;
498 Handle::UniquePtr mPositionHandle;
503template <
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);
544 if (state1 == index::NONE && state2 == index::NONE)
return index::NONE;
545 else if (state1 == index::ALL && state2 == index::ALL)
return index::ALL;
547 return index::PARTIAL;
570template <
typename T0,
typename T1,
bool And>
#define OPENVDB_ASSERT(X)
Definition Assert.h:41
Attribute Array storage templated on type and compression codec.
Attribute Group access and filtering for iteration.
Set of Attribute Arrays which tracks metadata about each array.
Definition Exceptions.h:59
Definition Exceptions.h:60
Signed (x, y, z) 32-bit integer coordinates.
Definition Coord.h:26
Vec3d asVec3d() const
Definition Coord.h:144
Definition AttributeArray.h:764
void reset(const LeafT &leaf)
Definition IndexFilter.h:351
static index::State state()
Definition IndexFilter.h:346
static index::State state(const LeafT &)
Definition IndexFilter.h:348
AttributeHandle< IntType > Handle
Definition IndexFilter.h:327
AttributeHashFilter(const AttributeHashFilter &filter)
Definition IndexFilter.h:336
AttributeHashFilter(const size_t index, const double percentage, const unsigned int seed=0)
Definition IndexFilter.h:329
bool valid(const IterT &iter) const
Definition IndexFilter.h:357
bool initialized() const
Definition IndexFilter.h:344
Ordered collection of uniquely-named attribute arrays.
Definition AttributeSet.h:40
Util::GroupIndex groupIndex(const Name &groupName) const
Return the group index from the name of the group.
Definition IndexFilter.h:449
void reset(const LeafT &leaf)
Definition IndexFilter.h:475
index::State state() const
Definition IndexFilter.h:467
static index::State state(const LeafT &)
Definition IndexFilter.h:472
BBoxFilter(const BBoxFilter &filter)
Definition IndexFilter.h:458
AttributeHandle< openvdb::Vec3f > Handle
Definition IndexFilter.h:451
BBoxFilter(const openvdb::math::Transform &transform, const openvdb::BBoxd &bboxWS)
Definition IndexFilter.h:453
bool valid(const IterT &iter) const
Definition IndexFilter.h:480
bool initialized() const
Definition IndexFilter.h:465
Definition IndexFilter.h:505
BinaryFilter(const T1 &filter1, const T2 &filter2)
Definition IndexFilter.h:507
void reset(const LeafT &leaf)
Definition IndexFilter.h:525
index::State state() const
Definition IndexFilter.h:514
index::State state(const LeafT &leaf) const
Definition IndexFilter.h:519
bool valid(const IterT &iter) const
Definition IndexFilter.h:531
bool initialized() const
Definition IndexFilter.h:512
Definition AttributeGroup.h:74
Definition IndexFilter.h:376
void reset(const LeafT &leaf)
Definition IndexFilter.h:408
static index::State state()
Definition IndexFilter.h:403
static index::State state(const LeafT &)
Definition IndexFilter.h:405
typename LevelSetGridT::ValueType ValueT
Definition IndexFilter.h:378
LevelSetFilter(const LevelSetGridT &grid, const math::Transform &transform, const ValueT min, const ValueT max)
Definition IndexFilter.h:381
LevelSetFilter(const LevelSetFilter &filter)
Definition IndexFilter.h:391
AttributeHandle< openvdb::Vec3f > Handle
Definition IndexFilter.h:379
bool valid(const IterT &iter) const
Definition IndexFilter.h:413
bool initialized() const
Definition IndexFilter.h:401
std::vector< GroupHandle > HandleVector
Definition IndexFilter.h:143
MultiGroupFilter(const IndexVector &include, const IndexVector &exclude)
Definition IndexFilter.h:165
std::vector< AttributeSet::Descriptor::GroupIndex > IndexVector
Definition IndexFilter.h:142
void reset(const LeafT &leaf)
Definition IndexFilter.h:188
index::State state() const
Definition IndexFilter.h:179
static index::State state(const LeafT &)
Definition IndexFilter.h:185
MultiGroupFilter(const NameVector &include, const NameVector &exclude, const AttributeSet &attributeSet)
Definition IndexFilter.h:159
std::vector< Name > NameVector
Definition IndexFilter.h:141
MultiGroupFilter(const MultiGroupFilter &filter)
Definition IndexFilter.h:170
bool valid(const IterT &iter) const
Definition IndexFilter.h:201
bool initialized() const
Definition IndexFilter.h:177
void reset(const LeafT &leaf)
Definition IndexFilter.h:276
void next() const
Definition IndexFilter.h:296
std::pair< Index, Index > SeedCountPair
Definition IndexFilter.h:232
RandomLeafFilter(const PointDataTreeT &tree, const Index64 targetPoints, const unsigned int seed=0)
Definition IndexFilter.h:235
static index::State state()
Definition IndexFilter.h:271
static index::State state(const LeafT &)
Definition IndexFilter.h:273
std::unordered_map< openvdb::Coord, SeedCountPair > LeafMap
Definition IndexFilter.h:233
bool valid(const IterT &iter) const
Definition IndexFilter.h:304
bool initialized() const
Definition IndexFilter.h:269
Index filtering on active / inactive state of host voxel.
Definition IndexFilter.h:107
static index::State state()
Definition IndexFilter.h:110
static bool initialized()
Definition IndexFilter.h:109
void reset(const LeafT &)
Definition IndexFilter.h:120
static index::State state(const LeafT &leaf)
Definition IndexFilter.h:112
bool valid(const IterT &iter) const
Definition IndexFilter.h:123
int Floor(float x)
Return the floor of x.
Definition Math.h:848
Definition IndexIterator.h:35
State
Definition IndexIterator.h:41
@ PARTIAL
Definition IndexIterator.h:42
@ ALL
Definition IndexIterator.h:44
@ NONE
Definition IndexIterator.h:43
ValueMaskFilter< false > InactiveFilter
Definition IndexFilter.h:132
ValueMaskFilter< true > ActiveFilter
Definition IndexFilter.h:131
Definition PointDataGrid.h:170
Index32 Index
Definition Types.h:54
math::Vec3< float > Vec3f
Definition Types.h:74
math::BBox< Vec3d > BBoxd
Definition Types.h:84
uint64_t Index64
Definition Types.h:53
Definition Exceptions.h:13
#define OPENVDB_THROW(exception, message)
Definition Exceptions.h:74
static const bool RequiresCoord
Definition IndexFilter.h:564
static const bool RequiresCoord
Definition IndexFilter.h:572
static const bool RequiresCoord
Definition IndexFilter.h:568
Definition IndexFilter.h:559
static const bool RequiresCoord
Definition IndexFilter.h:560
#define OPENVDB_VERSION_NAME
The version namespace name for this library version.
Definition version.h.in:121
#define OPENVDB_USE_VERSION_NAMESPACE
Definition version.h.in:218