10 #ifndef OPENVDB_POINTS_POINT_MASK_HAS_BEEN_INCLUDED
11 #define OPENVDB_POINTS_POINT_MASK_HAS_BEEN_INCLUDED
19 #include <tbb/combinable.h>
21 #include <type_traits>
36 template <
typename PointDataGridT,
37 typename MaskT =
typename PointDataGridT::template ValueConverter<bool>::Type,
38 typename FilterT = NullFilter>
39 inline typename std::enable_if<std::is_same<typename MaskT::ValueType, bool>::value,
40 typename MaskT::Ptr>::type
42 const FilterT& filter = NullFilter(),
43 bool threaded =
true);
52 template <
typename PointDataGridT,
53 typename MaskT =
typename PointDataGridT::template ValueConverter<bool>::Type,
54 typename FilterT = NullFilter>
55 inline typename std::enable_if<std::is_same<typename MaskT::ValueType, bool>::value,
56 typename MaskT::Ptr>::type
58 const openvdb::math::Transform& transform,
59 const FilterT& filter = NullFilter(),
60 bool threaded =
true);
66 template <
typename LeafT>
67 void reset(LeafT&,
size_t = 0) { }
69 template <
typename IterT>
75 template <
typename DeformerT>
78 static const bool IndexSpace =
false;
85 namespace point_mask_internal {
87 template <
typename LeafT>
88 void voxelSum(LeafT& leaf,
const Index offset,
const typename LeafT::ValueType& value)
96 template <
typename T, Index Log2Dim>
100 leaf.
setOffsetOn(offset, leaf.getValue(offset) + value);
106 template<
typename Gr
idT>
111 using TreeT =
typename GridT::TreeType;
112 using LeafT =
typename TreeT::LeafNodeType;
117 : mTree(grid.tree()) {}
121 for (
auto leaf = grid.tree().beginLeaf(); leaf; ++leaf) {
122 auto* newLeaf = mTree.probeLeaf(leaf->origin());
125 auto& tree =
const_cast<GridT&
>(grid).tree();
126 mTree.addLeaf(tree.template stealNode<LeafT>(leaf->origin(),
127 zeroVal<ValueType>(),
false));
131 for (
auto iter = leaf->cbeginValueOn(); iter; ++iter) {
144 template <
typename Gr
idT,
typename Po
intDataGr
idT,
typename FilterT>
147 using LeafT =
typename GridT::TreeType::LeafNodeType;
148 using ValueT =
typename LeafT::ValueType;
151 const FilterT& filter)
152 : mPointDataAccessor(grid.getConstAccessor())
153 , mFilter(filter) { }
157 const auto*
const pointLeaf =
158 mPointDataAccessor.probeConstLeaf(leaf.origin());
163 for (
auto value = leaf.beginValueOn(); value; ++value) {
165 pointLeaf->beginIndexVoxel(value.getCoord(), mFilter));
167 value.setValue(
ValueT(count));
170 value.setValueOn(
false);
176 const typename PointDataGridT::ConstAccessor mPointDataAccessor;
177 const FilterT& mFilter;
183 template <
typename Gr
idT,
typename Po
intDataGr
idT,
typename FilterT,
typename DeformerT>
187 using ValueT =
typename GridT::TreeType::ValueType;
193 const FilterT& filter,
194 const DeformerT& deformer,
196 : mTargetTransform(targetTransform)
197 , mSourceTransform(sourceTransform)
199 , mDeformer(deformer)
200 , mCombinable(combinable) { }
204 DeformerT deformer(mDeformer);
206 auto& grid = mCombinable.local();
207 auto& countTree = grid.tree();
210 deformer.reset(leaf, idx);
212 auto handle = HandleT::create(leaf.constAttributeArray(
"P"));
214 for (
auto iter = leaf.beginIndexOn(mFilter); iter; iter++) {
218 Vec3d position = handle->get(*iter) + iter.getCoord().asVec3d();
224 deformer.template apply<decltype(iter)>(position, iter);
225 position = mSourceTransform.indexToWorld(position);
228 position = mSourceTransform.indexToWorld(position);
229 deformer.template apply<decltype(iter)>(position, iter);
234 const Coord ijk = mTargetTransform.worldToIndexCellCentered(position);
245 const openvdb::math::Transform& mTargetTransform;
246 const openvdb::math::Transform& mSourceTransform;
247 const FilterT& mFilter;
248 const DeformerT& mDeformer;
249 CombinableT& mCombinable;
253 template<
typename Gr
idT,
typename Po
intDataGr
idT,
typename FilterT>
255 const PointDataGridT& points,
256 const FilterT& filter,
257 bool threaded =
true)
261 using GridTreeT =
typename GridT::TreeType;
262 using ValueT =
typename GridTreeT::ValueType;
266 typename GridTreeT::Ptr tree(
new GridTreeT(points.constTree(),
267 false, openvdb::TopologyCopy()));
268 typename GridT::Ptr grid = GridT::create(tree);
269 grid->setTransform(points.transform().copy());
273 if (points.constTree().leafCount() == 0)
return grid;
277 if (std::is_same<ValueT, bool>::value && filter.state() ==
index::ALL)
return grid;
287 leafManager.
foreach(pointsToScalarOp, threaded);
292 leafManager.
foreach(pointsToScalarOp, threaded);
299 template<
typename Gr
idT,
typename Po
intDataGr
idT,
typename FilterT,
typename DeformerT>
301 PointDataGridT& points,
302 const openvdb::math::Transform& transform,
303 const FilterT& filter,
304 const DeformerT& deformer,
305 bool threaded =
true)
315 const openvdb::math::Transform& pointsTransform = points.constTransform();
317 if (transform == pointsTransform && std::is_same<NullDeformer, DeformerT>()) {
318 return convertPointsToScalar<GridT>(points, filter, threaded);
321 typename GridT::Ptr grid = GridT::create();
322 grid->setTransform(transform.copy());
326 if (points.constTree().leafCount() == 0)
return grid;
330 CombinableT combiner;
337 transform, pointsTransform, nullFilter, deformer, combiner);
338 leafManager.
foreach(pointsToScalarOp, threaded);
341 transform, pointsTransform, filter, deformer, combiner);
342 leafManager.
foreach(pointsToScalarOp, threaded);
347 CombinerOpT combineOp(*grid);
348 combiner.combine_each(combineOp);
360 template<
typename Po
intDataGr
idT,
typename MaskT,
typename FilterT>
361 inline typename std::enable_if<std::is_same<typename MaskT::ValueType, bool>::value,
362 typename MaskT::Ptr>::type
364 const PointDataGridT& points,
365 const FilterT& filter,
368 return point_mask_internal::convertPointsToScalar<MaskT>(
369 points, filter, threaded);
373 template<
typename Po
intDataGr
idT,
typename MaskT,
typename FilterT>
374 inline typename std::enable_if<std::is_same<typename MaskT::ValueType, bool>::value,
375 typename MaskT::Ptr>::type
377 const PointDataGridT& points,
378 const openvdb::math::Transform& transform,
379 const FilterT& filter,
384 auto& nonConstPoints =
const_cast<typename AdapterT::NonConstGridType&
>(points);
387 return point_mask_internal::convertPointsToScalar<MaskT>(
388 nonConstPoints, transform, filter, deformer, threaded);
398 template <
typename PointDataGridT,
399 typename MaskT =
typename PointDataGridT::template ValueConverter<bool>::Type>
401 inline typename std::enable_if<std::is_same<typename MaskT::ValueType, bool>::value,
402 typename MaskT::Ptr>::type
404 const std::vector<Name>& includeGroups,
405 const std::vector<Name>& excludeGroups)
407 auto leaf = grid.tree().cbeginLeaf();
408 if (!leaf)
return MaskT::create();
409 MultiGroupFilter filter(includeGroups, excludeGroups, leaf->attributeSet());
414 template <
typename PointDataGridT,
415 typename MaskT =
typename PointDataGridT::template ValueConverter<bool>::Type>
417 inline typename std::enable_if<std::is_same<typename MaskT::ValueType, bool>::value,
418 typename MaskT::Ptr>::type
420 const openvdb::math::Transform& transform,
421 const std::vector<Name>& includeGroups,
422 const std::vector<Name>& excludeGroups)
424 auto leaf = grid.tree().cbeginLeaf();
425 if (!leaf)
return MaskT::create();
426 MultiGroupFilter filter(includeGroups, excludeGroups, leaf->attributeSet());
435 #endif // OPENVDB_POINTS_POINT_MASK_HAS_BEEN_INCLUDED