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> 34 template <
typename PointDataTreeT,
35 typename MaskTreeT =
typename PointDataTreeT::template ValueConverter<bool>::Type,
36 typename FilterT = NullFilter>
40 const FilterT& filter = NullFilter(),
41 bool threaded =
true);
48 template <
typename PointDataGridT,
49 typename MaskGridT =
typename PointDataGridT::template ValueConverter<bool>::Type,
50 typename FilterT = NullFilter>
54 const FilterT& filter = NullFilter(),
55 bool threaded =
true);
63 template <
typename PointDataGridT,
64 typename MaskT =
typename PointDataGridT::template ValueConverter<bool>::Type,
65 typename FilterT = NullFilter>
67 typename MaskT::Ptr>::type
69 const openvdb::math::Transform& transform,
70 const FilterT& filter = NullFilter(),
71 bool threaded =
true);
76 template <
typename LeafT>
77 void reset(LeafT&,
size_t = 0) { }
79 template <
typename IterT>
80 void apply(Vec3d&, IterT&)
const { }
85 template <
typename DeformerT>
88 static const bool IndexSpace =
false;
96 namespace point_mask_internal {
98 template <
typename LeafT>
99 void voxelSum(LeafT& leaf,
const Index offset,
const typename LeafT::ValueType&
value)
107 template <
typename T, Index Log2Dim>
117 template<
typename Gr
idT>
118 struct GridCombinerOp
120 using CombinableT =
typename tbb::combinable<GridT>;
122 using TreeT =
typename GridT::TreeType;
123 using LeafT =
typename TreeT::LeafNodeType;
124 using ValueType =
typename TreeT::ValueType;
127 GridCombinerOp(GridT& grid)
128 : mTree(grid.tree()) {}
130 void operator()(
const GridT& grid)
132 for (
auto leaf = grid.tree().beginLeaf(); leaf; ++leaf) {
133 auto* newLeaf = mTree.probeLeaf(leaf->
origin());
136 auto& tree =
const_cast<GridT&
>(grid).tree();
137 mTree.addLeaf(tree.template stealNode<LeafT>(leaf->
origin(),
138 zeroVal<ValueType>(),
false));
143 voxelSum(*newLeaf, iter.offset(), ValueType(*iter));
155 template <
typename TreeT,
typename Po
intDataTreeT,
typename FilterT>
156 struct PointsToScalarOp
158 using LeafT =
typename TreeT::LeafNodeType;
159 using ValueT =
typename LeafT::ValueType;
161 static constexpr
bool IsBool =
164 PointsToScalarOp(
const PointDataTreeT& tree,
165 const FilterT& filter)
166 : mPointDataAccessor(tree)
169 void operator()(LeafT& leaf,
size_t )
const 172 const auto*
const pointLeaf =
173 mPointDataAccessor.probeConstLeaf(leaf.origin());
177 const auto iter = pointLeaf->beginIndexVoxel(value.getCoord(), mFilter);
191 const FilterT& mFilter;
197 template <
typename Gr
idT,
typename Po
intDataGr
idT,
typename FilterT,
typename DeformerT>
198 struct PointsToTransformedScalarOp
200 using PointDataLeafT =
typename PointDataGridT::TreeType::LeafNodeType;
201 using ValueT =
typename GridT::TreeType::ValueType;
203 using CombinableT =
typename GridCombinerOp<GridT>::CombinableT;
207 const FilterT& filter,
208 const DeformerT& deformer,
209 CombinableT& combinable)
210 : mTargetTransform(targetTransform)
211 , mSourceTransform(sourceTransform)
213 , mDeformer(deformer)
214 , mCombinable(combinable) { }
216 void operator()(
const PointDataLeafT& leaf,
size_t idx)
const 218 DeformerT deformer(mDeformer);
220 auto& grid = mCombinable.local();
221 auto& countTree = grid.tree();
224 deformer.reset(leaf, idx);
226 auto handle = HandleT::create(leaf.constAttributeArray(
"P"));
228 for (
auto iter = leaf.beginIndexOn(mFilter); iter; iter++) {
232 Vec3d position = handle->get(*iter) + iter.getCoord().asVec3d();
238 deformer.template apply<decltype(iter)>(position, iter);
239 position = mSourceTransform.indexToWorld(position);
242 position = mSourceTransform.indexToWorld(position);
243 deformer.template apply<decltype(iter)>(position, iter);
248 const Coord ijk = mTargetTransform.worldToIndexCellCentered(position);
254 voxelSum(*newLeaf, newLeaf->coordToOffset(ijk), ValueT(1));
259 const openvdb::math::Transform& mTargetTransform;
260 const openvdb::math::Transform& mSourceTransform;
261 const FilterT& mFilter;
262 const DeformerT& mDeformer;
263 CombinableT& mCombinable;
267 template<
typename TreeT,
typename Po
intDataTreeT,
typename FilterT>
268 inline typename TreeT::Ptr convertPointsToScalar(
269 const PointDataTreeT& points,
270 const FilterT& filter,
271 bool threaded =
true)
273 using point_mask_internal::PointsToScalarOp;
275 using ValueT =
typename TreeT::ValueType;
279 typename TreeT::Ptr tree(
new TreeT(
false));
280 tree->topologyUnion(points);
284 if (points.leafCount() == 0)
return tree;
296 PointsToScalarOp<TreeT, PointDataTreeT, NullFilter> pointsToScalarOp(
298 leafManager.
foreach(pointsToScalarOp, threaded);
301 PointsToScalarOp<TreeT, PointDataTreeT, FilterT> pointsToScalarOp(
303 leafManager.
foreach(pointsToScalarOp, threaded);
310 template<
typename Gr
idT,
typename Po
intDataGr
idT,
typename FilterT,
typename DeformerT>
311 inline typename GridT::Ptr convertPointsToScalar(
312 PointDataGridT& points,
314 const FilterT& filter,
315 const DeformerT& deformer,
316 bool threaded =
true)
318 using point_mask_internal::PointsToTransformedScalarOp;
319 using point_mask_internal::GridCombinerOp;
321 using CombinerOpT = GridCombinerOp<GridT>;
322 using CombinableT =
typename GridCombinerOp<GridT>::CombinableT;
324 typename GridT::Ptr grid = GridT::create();
325 grid->setTransform(transform.
copy());
331 if (transform == pointsTransform && std::is_same<NullDeformer, DeformerT>()) {
332 using TreeT =
typename GridT::TreeType;
333 typename TreeT::Ptr tree =
334 convertPointsToScalar<TreeT>(points.tree(), filter, threaded);
341 if (points.constTree().leafCount() == 0)
return grid;
345 CombinableT combiner;
351 PointsToTransformedScalarOp<GridT, PointDataGridT, NullFilter, DeformerT> pointsToScalarOp(
352 transform, pointsTransform, nullFilter, deformer, combiner);
353 leafManager.foreach(pointsToScalarOp, threaded);
355 PointsToTransformedScalarOp<GridT, PointDataGridT, FilterT, DeformerT> pointsToScalarOp(
356 transform, pointsTransform, filter, deformer, combiner);
357 leafManager.foreach(pointsToScalarOp, threaded);
362 CombinerOpT combineOp(*grid);
363 combiner.combine_each(combineOp);
376 template <
typename Po
intDataTreeT,
typename MaskTreeT,
typename FilterT>
378 std::is_same<typename MaskTreeT::ValueType, bool>::value,
typename MaskTreeT::Ptr>::type
380 const FilterT& filter,
383 return point_mask_internal::convertPointsToScalar<MaskTreeT>(
384 tree, filter, threaded);
388 template<
typename Po
intDataGr
idT,
typename MaskGr
idT,
typename FilterT>
390 std::is_same<typename MaskGridT::ValueType, bool>::value,
typename MaskGridT::Ptr>::type
392 const PointDataGridT& points,
393 const FilterT& filter,
396 using PointDataTreeT =
typename PointDataGridT::TreeType;
397 using MaskTreeT =
typename MaskGridT::TreeType;
399 typename MaskTreeT::Ptr tree =
400 convertPointsToMask<PointDataTreeT, MaskTreeT, FilterT>
401 (points.tree(), filter, threaded);
403 typename MaskGridT::Ptr grid(
new MaskGridT(tree));
404 grid->setTransform(points.transform().copy());
409 template<
typename Po
intDataGr
idT,
typename MaskT,
typename FilterT>
410 inline typename std::enable_if<std::is_same<typename MaskT::ValueType, bool>::value,
411 typename MaskT::Ptr>::type
413 const PointDataGridT& points,
414 const openvdb::math::Transform& transform,
415 const FilterT& filter,
420 auto& nonConstPoints =
const_cast<typename AdapterT::NonConstGridType&
>(points);
423 return point_mask_internal::convertPointsToScalar<MaskT>(
424 nonConstPoints, transform, filter, deformer, threaded);
435 #endif // OPENVDB_POINTS_POINT_MASK_HAS_BEEN_INCLUDED
Index32 Index
Definition: Types.h:54
void setOffsetOn(Index offset, const ValueType &val)
Definition: PointDataGrid.h:1107
const Coord & origin() const
Return the grid index coordinates of this node's local origin.
Definition: LeafNode.h:173
ValueOnCIter beginValueOn() const
Definition: PointDataGrid.h:705
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
Definition: AttributeArray.h:817
Definition: PointDataGrid.h:181
A no-op filter that can be used when iterating over all indices.
Definition: IndexIterator.h:50
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:412
Index filters primarily designed to be used with a FilterIndexIter.
This class manages a linear array of pointers to a given tree's leaf nodes, as well as optional auxil...
Definition: LeafManager.h:84
Definition: ValueAccessor.h:182
Definition: Exceptions.h:13
void setValueOn(const Coord &xyz)
Definition: PointDataGrid.h:544
ValueT value
Definition: GridBuilder.h:1287
Definition: IndexIterator.h:43
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
Index64 iterCount(const IterT &iter)
Count up the number of times the iterator can iterate.
Definition: IndexIterator.h:314
Attribute-owned data structure for points. Point attributes are stored in leaf nodes and ordered by v...
#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:1064
void setValue(const Coord &, const ValueType &)
Definition: PointDataGrid.h:550
uint64_t Index64
Definition: Types.h:53
const ValueType & getValue(const Coord &xyz) const
Return the value of the voxel at the given coordinates.
Definition: LeafNode.h:1087
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h.in:202
ValueOnCIter cbeginValueOn() const
Definition: PointDataGrid.h:704
T ValueType
Definition: PointDataGrid.h:245