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;
86 namespace point_mask_internal {
88 template <
typename LeafT>
89 void voxelSum(LeafT& leaf,
const Index offset,
const typename LeafT::ValueType& value)
97 template <
typename T, Index Log2Dim>
98 void voxelSum(PointDataLeafNode<T, Log2Dim>& leaf,
const Index offset,
99 const typename PointDataLeafNode<T, Log2Dim>::ValueType& value)
101 leaf.setOffsetOn(offset, leaf.getValue(offset) + value);
107 template<
typename Gr
idT>
108 struct GridCombinerOp
110 using CombinableT =
typename tbb::combinable<GridT>;
112 using TreeT =
typename GridT::TreeType;
113 using LeafT =
typename TreeT::LeafNodeType;
114 using ValueType =
typename TreeT::ValueType;
117 GridCombinerOp(GridT& grid)
118 : mTree(grid.tree()) {}
120 void operator()(
const GridT& grid)
122 for (
auto leaf = grid.tree().beginLeaf(); leaf; ++leaf) {
123 auto* newLeaf = mTree.probeLeaf(leaf->origin());
126 auto& tree =
const_cast<GridT&
>(grid).tree();
127 mTree.addLeaf(tree.template stealNode<LeafT>(leaf->origin(),
128 zeroVal<ValueType>(),
false));
132 for (
auto iter = leaf->cbeginValueOn(); iter; ++iter) {
133 voxelSum(*newLeaf, iter.offset(), ValueType(*iter));
145 template <
typename Gr
idT,
typename Po
intDataGr
idT,
typename FilterT>
146 struct PointsToScalarOp
148 using LeafT =
typename GridT::TreeType::LeafNodeType;
149 using ValueT =
typename LeafT::ValueType;
151 PointsToScalarOp(
const PointDataGridT& grid,
152 const FilterT& filter)
153 : mPointDataAccessor(grid.getConstAccessor())
154 , mFilter(filter) { }
156 void operator()(LeafT& leaf,
size_t )
const {
158 const auto*
const pointLeaf =
159 mPointDataAccessor.probeConstLeaf(leaf.origin());
164 for (
auto value = leaf.beginValueOn(); value; ++value) {
166 pointLeaf->beginIndexVoxel(value.getCoord(), mFilter));
168 value.setValue(ValueT(count));
171 value.setValueOn(
false);
177 const typename PointDataGridT::ConstAccessor mPointDataAccessor;
178 const FilterT& mFilter;
184 template <
typename Gr
idT,
typename Po
intDataGr
idT,
typename FilterT,
typename DeformerT>
185 struct PointsToTransformedScalarOp
187 using PointDataLeafT =
typename PointDataGridT::TreeType::LeafNodeType;
188 using ValueT =
typename GridT::TreeType::ValueType;
189 using HandleT = AttributeHandle<Vec3f>;
190 using CombinableT =
typename GridCombinerOp<GridT>::CombinableT;
192 PointsToTransformedScalarOp(
const math::Transform& targetTransform,
193 const math::Transform& sourceTransform,
194 const FilterT& filter,
195 const DeformerT& deformer,
196 CombinableT& combinable)
197 : mTargetTransform(targetTransform)
198 , mSourceTransform(sourceTransform)
200 , mDeformer(deformer)
201 , mCombinable(combinable) { }
203 void operator()(
const PointDataLeafT& leaf,
size_t idx)
const
205 DeformerT deformer(mDeformer);
207 auto& grid = mCombinable.local();
208 auto& countTree = grid.tree();
209 tree::ValueAccessor<typename GridT::TreeType> accessor(countTree);
211 deformer.reset(leaf, idx);
213 auto handle = HandleT::create(leaf.constAttributeArray(
"P"));
215 for (
auto iter = leaf.beginIndexOn(mFilter); iter; iter++) {
219 Vec3d position = handle->get(*iter) + iter.getCoord().asVec3d();
224 if (DeformerTraits<DeformerT>::IndexSpace) {
225 deformer.template apply<decltype(iter)>(position, iter);
226 position = mSourceTransform.indexToWorld(position);
229 position = mSourceTransform.indexToWorld(position);
230 deformer.template apply<decltype(iter)>(position, iter);
235 const Coord ijk = mTargetTransform.worldToIndexCellCentered(position);
239 auto* newLeaf = accessor.touchLeaf(ijk);
241 voxelSum(*newLeaf, newLeaf->coordToOffset(ijk), ValueT(1));
246 const openvdb::math::Transform& mTargetTransform;
247 const openvdb::math::Transform& mSourceTransform;
248 const FilterT& mFilter;
249 const DeformerT& mDeformer;
250 CombinableT& mCombinable;
254 template<
typename Gr
idT,
typename Po
intDataGr
idT,
typename FilterT>
255 inline typename GridT::Ptr convertPointsToScalar(
256 const PointDataGridT& points,
257 const FilterT& filter,
258 bool threaded =
true)
260 using point_mask_internal::PointsToScalarOp;
262 using GridTreeT =
typename GridT::TreeType;
263 using ValueT =
typename GridTreeT::ValueType;
267 typename GridTreeT::Ptr tree(
new GridTreeT(points.constTree(),
269 typename GridT::Ptr grid = GridT::create(tree);
270 grid->setTransform(points.transform().copy());
274 if (points.constTree().leafCount() == 0)
return grid;
278 if (std::is_same<ValueT, bool>::value && filter.state() ==
index::ALL)
return grid;
282 tree::LeafManager<GridTreeT> leafManager(*tree);
285 NullFilter nullFilter;
286 PointsToScalarOp<GridT, PointDataGridT, NullFilter> pointsToScalarOp(
288 leafManager.foreach(pointsToScalarOp, threaded);
291 PointsToScalarOp<GridT, PointDataGridT, FilterT> pointsToScalarOp(
293 leafManager.foreach(pointsToScalarOp, threaded);
300 template<
typename Gr
idT,
typename Po
intDataGr
idT,
typename FilterT,
typename DeformerT>
301 inline typename GridT::Ptr convertPointsToScalar(
302 PointDataGridT& points,
303 const openvdb::math::Transform& transform,
304 const FilterT& filter,
305 const DeformerT& deformer,
306 bool threaded =
true)
308 using point_mask_internal::PointsToTransformedScalarOp;
309 using point_mask_internal::GridCombinerOp;
311 using CombinerOpT = GridCombinerOp<GridT>;
312 using CombinableT =
typename GridCombinerOp<GridT>::CombinableT;
316 const openvdb::math::Transform& pointsTransform = points.constTransform();
318 if (transform == pointsTransform && std::is_same<NullDeformer, DeformerT>()) {
319 return convertPointsToScalar<GridT>(points, filter, threaded);
322 typename GridT::Ptr grid = GridT::create();
323 grid->setTransform(transform.copy());
327 if (points.constTree().leafCount() == 0)
return grid;
331 CombinableT combiner;
333 tree::LeafManager<typename PointDataGridT::TreeType> leafManager(points.tree());
336 NullFilter nullFilter;
337 PointsToTransformedScalarOp<GridT, PointDataGridT, NullFilter, DeformerT> pointsToScalarOp(
338 transform, pointsTransform, nullFilter, deformer, combiner);
339 leafManager.foreach(pointsToScalarOp, threaded);
341 PointsToTransformedScalarOp<GridT, PointDataGridT, FilterT, DeformerT> pointsToScalarOp(
342 transform, pointsTransform, filter, deformer, combiner);
343 leafManager.foreach(pointsToScalarOp, threaded);
348 CombinerOpT combineOp(*grid);
349 combiner.combine_each(combineOp);
362 template<
typename Po
intDataGr
idT,
typename MaskT,
typename FilterT>
363 inline typename std::enable_if<std::is_same<typename MaskT::ValueType, bool>::value,
364 typename MaskT::Ptr>::type
366 const PointDataGridT& points,
367 const FilterT& filter,
370 return point_mask_internal::convertPointsToScalar<MaskT>(
371 points, filter, threaded);
375 template<
typename Po
intDataGr
idT,
typename MaskT,
typename FilterT>
376 inline typename std::enable_if<std::is_same<typename MaskT::ValueType, bool>::value,
377 typename MaskT::Ptr>::type
379 const PointDataGridT& points,
380 const openvdb::math::Transform& transform,
381 const FilterT& filter,
386 auto& nonConstPoints =
const_cast<typename AdapterT::NonConstGridType&
>(points);
389 return point_mask_internal::convertPointsToScalar<MaskT>(
390 nonConstPoints, transform, filter, deformer, threaded);
Index filters primarily designed to be used with a FilterIndexIter.
Attribute-owned data structure for points. Point attributes are stored in leaf nodes and ordered by v...
Tag dispatch class that distinguishes topology copy constructors from deep copy constructors.
Definition: Types.h:564
Vec3< double > Vec3d
Definition: Vec3.h:668
@ ALL
Definition: IndexIterator.h:43
Index64 iterCount(const IterT &iter)
Count up the number of times the iterator can iterate.
Definition: IndexIterator.h:314
std::enable_if< std::is_same< typename MaskT::ValueType, bool >::value, typename MaskT::Ptr >::type convertPointsToMask(const PointDataGridT &grid, const openvdb::math::Transform &transform, const FilterT &filter=NullFilter(), bool threaded=true)
Extract a Mask Grid from a Point Data Grid using a new transform.
Definition: PointMask.h:378
Index32 Index
Definition: Types.h:54
uint64_t Index64
Definition: Types.h:53
Definition: Exceptions.h:13
This adapter allows code that is templated on a Tree type to accept either a Tree type or a Grid type...
Definition: Grid.h:1071
#define OPENVDB_VERSION_NAME
The version namespace name for this library version.
Definition: version.h.in:116
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h.in:180