37 #ifndef OPENVDB_POINTS_INDEX_FILTER_HAS_BEEN_INCLUDED 38 #define OPENVDB_POINTS_INDEX_FILTER_HAS_BEEN_INCLUDED 51 #include <boost/ptr_container/ptr_vector.hpp> 57 class TestIndexFilter;
68 namespace index_filter_internal {
72 template <
typename RandGenT,
typename IntType>
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());
108 using IndexVector = std::vector<AttributeSet::Descriptor::GroupIndex>;
114 for (
const auto& name : names) {
116 indices.emplace_back(attributeSet.
groupIndex(name));
134 , mExclude(exclude) { }
137 : mInclude(filter.mInclude)
138 , mExclude(filter.mExclude)
139 , mIncludeHandles(filter.mIncludeHandles)
140 , mExcludeHandles(filter.mExcludeHandles)
141 , mInitialized(filter.mInitialized) { }
145 template <
typename LeafT>
147 mIncludeHandles.clear();
148 mExcludeHandles.clear();
149 for (
const AttributeSet::Descriptor::GroupIndex& index : mInclude) {
150 mIncludeHandles.emplace_back(leaf.groupHandle(index));
152 for (
const AttributeSet::Descriptor::GroupIndex& index : mExclude) {
153 mExcludeHandles.emplace_back(leaf.groupHandle(index));
158 template <
typename IterT>
159 bool valid(
const IterT& iter)
const {
160 assert(mInitialized);
162 bool includeValid = mIncludeHandles.empty();
163 for (
const GroupHandle& handle : mIncludeHandles) {
164 if (handle.getUnsafe(*iter)) {
169 if (!includeValid)
return false;
170 for (
const GroupHandle& handle : mExcludeHandles) {
171 if (handle.getUnsafe(*iter))
return false;
177 IndexVector mInclude;
178 IndexVector mExclude;
179 HandleVector mIncludeHandles;
180 HandleVector mExcludeHandles;
181 bool mInitialized =
false;
186 template <
typename Po
intDataTreeT,
typename RandGenT>
191 using LeafMap = std::map<openvdb::Coord, SeedCountPair>;
195 const unsigned int seed = 0) {
197 for (
auto iter = tree.cbeginLeaf(); iter; ++iter) {
198 currentPoints += iter->pointCount();
201 const float factor = targetPoints > currentPoints ? 1.0f : float(targetPoints) / float(currentPoints);
203 std::mt19937 generator(seed);
207 float totalPointsFloat = 0.0f;
209 for (
auto iter = tree.cbeginLeaf(); iter; ++iter) {
211 if (leafCounter + 1 == tree.leafCount()) {
212 const int leafPoints =
static_cast<int>(targetPoints) - totalPoints;
213 mLeafMap[iter->origin()] =
SeedCountPair(dist(generator), leafPoints);
216 totalPointsFloat += factor * iter->pointCount();
217 const auto leafPoints =
static_cast<int>(
math::Floor(totalPointsFloat));
218 totalPointsFloat -=
static_cast<float>(leafPoints);
219 totalPoints += leafPoints;
221 mLeafMap[iter->origin()] =
SeedCountPair(dist(generator), leafPoints);
229 template <
typename LeafT>
233 auto it = mLeafMap.find(leaf.origin());
234 if (it == mLeafMap.end()) {
236 "Cannot find leaf origin in map for random filter - " << leaf.origin());
240 const unsigned int seed =
static_cast<unsigned int>(value.first);
241 const auto total =
static_cast<Index>(leaf.pointCount());
242 mCount =
std::min(value.second, total);
244 mIndices = generateRandomSubset<RandGenT, int>(seed, mCount, total);
252 mNextIndex = mSubsetOffset >= mCount ?
254 mIndices[mSubsetOffset];
257 template <
typename IterT>
258 bool valid(
const IterT& iter)
const {
259 const int index = *iter;
260 while (mNextIndex < index) this->next();
261 return mNextIndex == index;
265 friend class ::TestIndexFilter;
269 std::vector<int> mIndices;
271 mutable int mSubsetOffset = -1;
272 mutable int mNextIndex = -1;
277 template <
typename RandGenT,
typename IntType>
284 const double percentage,
285 const unsigned int seed = 0)
287 , mFactor(percentage / 100.0)
291 : mIndex(filter.mIndex)
292 , mFactor(filter.mFactor)
293 , mSeed(filter.mSeed)
295 if (filter.mIdHandle) mIdHandle.reset(
new Handle(*filter.mIdHandle));
300 template <
typename LeafT>
302 assert(leaf.hasAttribute(mIndex));
303 mIdHandle.reset(
new Handle(leaf.constAttributeArray(mIndex)));
306 template <
typename IterT>
307 bool valid(
const IterT& iter)
const {
309 const IntType
id = mIdHandle->get(*iter);
310 const unsigned int seed = mSeed +
static_cast<unsigned int>(id);
311 RandGenT generator(seed);
312 std::uniform_real_distribution<double> dist(0.0, 1.0);
313 return dist(generator) < mFactor;
318 const double mFactor;
319 const unsigned int mSeed;
320 typename Handle::UniquePtr mIdHandle;
324 template <
typename LevelSetGr
idT>
328 using ValueT =
typename LevelSetGridT::ValueType;
335 : mAccessor(grid.getConstAccessor())
336 , mLevelSetTransform(grid.transform())
337 , mTransform(transform)
342 : mAccessor(filter.mAccessor)
343 , mLevelSetTransform(filter.mLevelSetTransform)
344 , mTransform(filter.mTransform)
348 if (filter.mPositionHandle) mPositionHandle.reset(
new Handle(*filter.mPositionHandle));
353 template <
typename LeafT>
355 mPositionHandle.reset(
new Handle(leaf.constAttributeArray(
"P")));
358 template <
typename IterT>
359 bool valid(
const IterT& iter)
const {
360 assert(mPositionHandle);
367 const openvdb::Vec3f& pointVoxelSpace = mPositionHandle->get(*iter);
370 const openvdb::Vec3f pointWorldSpace = mTransform.indexToWorld(pointVoxelSpace + voxelIndexSpace);
371 const openvdb::Vec3f pointIndexSpace = mLevelSetTransform.worldToIndex(pointWorldSpace);
374 const typename LevelSetGridT::ValueType value = tools::BoxSampler::sample(mAccessor, pointIndexSpace);
377 const bool invert = mMin > mMax;
379 return invert ? (value < mMax || value > mMin) : (value < mMax && value > mMin);
384 const typename LevelSetGridT::ConstAccessor mAccessor;
389 Handle::UniquePtr mPositionHandle;
401 : mTransform(transform)
402 , mBbox(transform.worldToIndex(bboxWS)) { }
405 : mTransform(filter.mTransform)
406 , mBbox(filter.mBbox)
408 if (filter.mPositionHandle) mPositionHandle.reset(
new Handle(*filter.mPositionHandle));
413 template <
typename LeafT>
415 mPositionHandle.reset(
new Handle(leaf.constAttributeArray(
"P")));
418 template <
typename IterT>
419 bool valid(
const IterT& iter)
const {
420 assert(mPositionHandle);
426 const openvdb::Vec3f& pointVoxelSpace = mPositionHandle->get(*iter);
429 const openvdb::Vec3f pointIndexSpace = pointVoxelSpace + voxelIndexSpace;
431 return mBbox.isInside(pointIndexSpace);
435 const openvdb::math::Transform& mTransform;
437 Handle::UniquePtr mPositionHandle;
442 template <
typename T1,
typename T2,
bool And = true>
449 , mFilter2(filter2) { }
451 inline bool initialized()
const {
return mFilter1.initialized() && mFilter2.initialized(); }
453 template <
typename LeafT>
455 mFilter1.reset(leaf);
456 mFilter2.reset(leaf);
459 template <
typename IterT>
460 bool valid(
const IterT& iter)
const {
461 if (And)
return mFilter1.valid(iter) && mFilter2.valid(iter);
462 return mFilter1.valid(iter) || mFilter2.valid(iter);
476 static const bool RequiresCoord =
false;
480 static const bool RequiresCoord =
true;
482 template <
typename T>
484 static const bool RequiresCoord =
true;
486 template <
typename T0,
typename T1,
bool And>
500 #endif // OPENVDB_POINTS_INDEX_FILTER_HAS_BEEN_INCLUDED BinaryFilter(const T1 &filter1, const T2 &filter2)
Definition: IndexFilter.h:446
void next() const
Definition: IndexFilter.h:250
Attribute Group access and filtering for iteration.
BBoxFilter(const openvdb::math::Transform &transform, const openvdb::BBoxd &bboxWS)
Definition: IndexFilter.h:399
AttributeHashFilter(const size_t index, const double percentage, const unsigned int seed=0)
Definition: IndexFilter.h:283
uint64_t Index64
Definition: Types.h:59
void reset(const LeafT &leaf)
Definition: IndexFilter.h:414
BBoxFilter(const BBoxFilter &filter)
Definition: IndexFilter.h:404
Definition: IndexFilter.h:325
Definition: Exceptions.h:86
std::map< openvdb::Coord, SeedCountPair > LeafMap
Definition: IndexFilter.h:191
std::vector< GroupHandle > HandleVector
Definition: IndexFilter.h:109
RandomLeafFilter(const PointDataTreeT &tree, const Index64 targetPoints, const unsigned int seed=0)
Definition: IndexFilter.h:193
bool initialized() const
Definition: IndexFilter.h:351
#define OPENVDB_THROW(exception, message)
Definition: Exceptions.h:108
Attribute Array storage templated on type and compression codec.
bool initialized() const
Definition: IndexFilter.h:451
std::vector< AttributeSet::Descriptor::GroupIndex > IndexVector
Definition: IndexFilter.h:108
Definition: Exceptions.h:85
Definition: AttributeGroup.h:102
Definition: IndexFilter.h:475
Definition: IndexFilter.h:443
Signed (x, y, z) 32-bit integer coordinates.
Definition: Coord.h:51
AttributeHashFilter(const AttributeHashFilter &filter)
Definition: IndexFilter.h:290
Definition: IndexFilter.h:187
Definition: IndexFilter.h:278
bool initialized() const
Definition: IndexFilter.h:227
uint32_t Index32
Definition: Types.h:58
Index32 Index
Definition: Types.h:60
#define OPENVDB_VERSION_NAME
The version namespace name for this library version.
Definition: version.h:136
bool initialized() const
Definition: IndexFilter.h:298
bool valid(const IterT &iter) const
Definition: IndexFilter.h:159
Definition: Exceptions.h:39
bool valid(const IterT &iter) const
Definition: IndexFilter.h:359
void reset(const LeafT &leaf)
Definition: IndexFilter.h:454
bool valid(const IterT &iter) const
Definition: IndexFilter.h:307
Library and file format version numbers.
LevelSetFilter(const LevelSetFilter &filter)
Definition: IndexFilter.h:341
MultiGroupFilter(const MultiGroupFilter &filter)
Definition: IndexFilter.h:136
MultiGroupFilter(const NameVector &include, const NameVector &exclude, const AttributeSet &attributeSet)
Definition: IndexFilter.h:125
Definition: IndexFilter.h:104
bool initialized() const
Definition: IndexFilter.h:411
std::pair< Index, Index > SeedCountPair
Definition: IndexFilter.h:190
void reset(const LeafT &leaf)
Definition: IndexFilter.h:301
Definition: AttributeArray.h:644
bool valid(const IterT &iter) const
Definition: IndexFilter.h:258
bool valid(const IterT &iter) const
Definition: IndexFilter.h:460
Definition: IndexFilter.h:394
void reset(const LeafT &leaf)
Definition: IndexFilter.h:230
bool valid(const IterT &iter) const
Definition: IndexFilter.h:419
int Floor(float x)
Return the floor of x.
Definition: Math.h:798
void reset(const LeafT &leaf)
Definition: IndexFilter.h:146
Ordered collection of uniquely-named attribute arrays.
Definition: AttributeSet.h:62
Vec3d asVec3d() const
Definition: Coord.h:170
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h:188
Util::GroupIndex groupIndex(const Name &groupName) const
Return the group index from the name of the group.
bool initialized() const
Definition: IndexFilter.h:143
LevelSetFilter(const LevelSetGridT &grid, const math::Transform &transform, const ValueT min, const ValueT max)
Definition: IndexFilter.h:331
typename LevelSetGridT::ValueType ValueT
Definition: IndexFilter.h:328
std::vector< IntType > generateRandomSubset(const unsigned int seed, const IntType n, const IntType m)
Definition: IndexFilter.h:74
Set of Attribute Arrays which tracks metadata about each array.
MultiGroupFilter(const IndexVector &include, const IndexVector &exclude)
Definition: IndexFilter.h:131
void reset(const LeafT &leaf)
Definition: IndexFilter.h:354
std::vector< Name > NameVector
Definition: IndexFilter.h:107