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>
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>
70 void apply(Vec3d&, IterT&)
const { }
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>
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));
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());
166 pointLeaf->beginIndexVoxel(value.getCoord(), mFilter));
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;
190 using CombinableT =
typename GridCombinerOp<GridT>::CombinableT;
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();
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();
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);
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;
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;
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);
401 #endif // OPENVDB_POINTS_POINT_MASK_HAS_BEEN_INCLUDED
A no-op filter that can be used when iterating over all indices.
Definition: IndexIterator.h:50
void setValueOn(const Coord &xyz)
Definition: PointDataGrid.h:541
void foreach(const LeafOp &op, bool threaded=true, size_t grainSize=1)
Threaded method that applies a user-supplied functor to each leaf node in the LeafManager.
Definition: LeafManager.h:483
const ValueType & getValue(const Coord &xyz) const
Return the value of the voxel at the given coordinates.
Definition: LeafNode.h:1086
Tag dispatch class that distinguishes topology copy constructors from deep copy constructors.
Definition: Types.h:644
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
Index filters primarily designed to be used with a FilterIndexIter.
void setValue(const Coord &, const ValueType &)
Definition: PointDataGrid.h:547
ValueOnCIter cbeginValueOn() const
Definition: PointDataGrid.h:701
T ValueType
Definition: PointDataGrid.h:245
uint64_t Index64
Definition: Types.h:53
Definition: Exceptions.h:13
ValueT value
Definition: GridBuilder.h:1287
void setOffsetOn(Index offset, const ValueType &val)
Definition: PointDataGrid.h:1104
Definition: PointDataGrid.h:181
Definition: IndexIterator.h:43
This class manages a linear array of pointers to a given tree's leaf nodes, as well as optional auxil...
Definition: LeafManager.h:84
Index32 Index
Definition: Types.h:54
ValueOnCIter beginValueOn() const
Definition: PointDataGrid.h:702
LeafNodeT * touchLeaf(const Coord &xyz)
Return a pointer to the leaf node that contains voxel (x, y, z). If no such node exists, create one, but preserve the values and active states of all voxels.
Definition: ValueAccessor.h:348
Index64 iterCount(const IterT &iter)
Count up the number of times the iterator can iterate.
Definition: IndexIterator.h:314
Definition: AttributeArray.h:810
Attribute-owned data structure for points. Point attributes are stored in leaf nodes and ordered by v...
Definition: ValueAccessor.h:182
#define OPENVDB_VERSION_NAME
The version namespace name for this library version.
Definition: version.h.in:116
This adapter allows code that is templated on a Tree type to accept either a Tree type or a Grid type...
Definition: Grid.h:1070
const Coord & origin() const
Return the grid index coordinates of this node's local origin.
Definition: LeafNode.h:172
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h.in:202