9 #ifndef OPENVDB_POINTS_POINT_MASK_IMPL_HAS_BEEN_INCLUDED
10 #define OPENVDB_POINTS_POINT_MASK_IMPL_HAS_BEEN_INCLUDED
19 namespace point_mask_internal {
21 template <
typename LeafT>
24 leaf.modifyValue(offset, tools::valxform::SumOp<typename LeafT::ValueType>(value));
30 template <
typename T, Index Log2Dim>
31 void voxelSum(PointDataLeafNode<T, Log2Dim>& leaf,
const Index offset,
34 leaf.setOffsetOn(offset, leaf.getValue(offset) +
value);
40 template<
typename Gr
idT>
43 using CombinableT =
typename tbb::combinable<GridT>;
45 using TreeT =
typename GridT::TreeType;
46 using LeafT =
typename TreeT::LeafNodeType;
48 using SumOp = tools::valxform::SumOp<typename TreeT::ValueType>;
50 GridCombinerOp(GridT& grid)
51 : mTree(grid.tree()) {}
53 void operator()(
const GridT& grid)
55 for (
auto leaf = grid.tree().beginLeaf(); leaf; ++leaf) {
56 auto* newLeaf = mTree.probeLeaf(leaf->origin());
59 auto& tree =
const_cast<GridT&
>(grid).tree();
60 mTree.addLeaf(tree.template stealNode<LeafT>(leaf->origin(),
61 zeroVal<ValueType>(),
false));
65 for (
auto iter = leaf->cbeginValueOn(); iter; ++iter) {
66 voxelSum(*newLeaf, iter.offset(),
ValueType(*iter));
78 template <
typename TreeT,
typename Po
intDataTreeT,
typename FilterT>
79 struct PointsToScalarOp
81 using LeafT =
typename TreeT::LeafNodeType;
84 static constexpr
bool IsBool =
87 PointsToScalarOp(
const PointDataTreeT& tree,
89 : mPointDataAccessor(tree)
92 void operator()(LeafT& leaf,
size_t )
const
95 const auto*
const pointLeaf =
96 mPointDataAccessor.probeConstLeaf(leaf.origin());
99 for (
auto value = leaf.beginValueOn();
value; ++
value) {
100 const auto iter = pointLeaf->beginIndexVoxel(value.getCoord(), mFilter);
102 if (!iter) value.setValueOn(
false);
106 if (count >
Index64(0)) value.setValue(ValueT(count));
107 else value.setValueOn(
false);
113 const tree::ValueAccessor<const PointDataTreeT> mPointDataAccessor;
114 const FilterT& mFilter;
120 template <
typename Gr
idT,
typename Po
intDataGr
idT,
typename FilterT,
typename DeformerT>
121 struct PointsToTransformedScalarOp
123 using PointDataLeafT =
typename PointDataGridT::TreeType::LeafNodeType;
125 using HandleT = AttributeHandle<Vec3f>;
126 using CombinableT =
typename GridCombinerOp<GridT>::CombinableT;
128 PointsToTransformedScalarOp(
const math::Transform& targetTransform,
129 const math::Transform& sourceTransform,
131 const DeformerT& deformer,
132 CombinableT& combinable)
133 : mTargetTransform(targetTransform)
134 , mSourceTransform(sourceTransform)
136 , mDeformer(deformer)
137 , mCombinable(combinable) { }
139 void operator()(
const PointDataLeafT& leaf,
size_t idx)
const
141 DeformerT deformer(mDeformer);
143 auto& grid = mCombinable.local();
144 auto& countTree = grid.tree();
145 tree::ValueAccessor<typename GridT::TreeType> accessor(countTree);
147 deformer.reset(leaf, idx);
149 auto handle = HandleT::create(leaf.constAttributeArray(
"P"));
151 for (
auto iter = leaf.beginIndexOn(mFilter); iter; iter++) {
160 if (DeformerTraits<DeformerT>::IndexSpace) {
161 deformer.template apply<decltype(iter)>(
position, iter);
162 position = mSourceTransform.indexToWorld(position);
165 position = mSourceTransform.indexToWorld(position);
166 deformer.template apply<decltype(iter)>(
position, iter);
171 const Coord ijk = mTargetTransform.worldToIndexCellCentered(position);
175 auto* newLeaf = accessor.touchLeaf(ijk);
177 voxelSum(*newLeaf, newLeaf->coordToOffset(ijk), ValueT(1));
182 const openvdb::math::Transform& mTargetTransform;
183 const openvdb::math::Transform& mSourceTransform;
184 const FilterT& mFilter;
185 const DeformerT& mDeformer;
186 CombinableT& mCombinable;
190 template<
typename TreeT,
typename Po
intDataTreeT,
typename FilterT>
191 inline typename TreeT::Ptr convertPointsToScalar(
192 const PointDataTreeT&
points,
194 bool threaded =
true)
196 using point_mask_internal::PointsToScalarOp;
202 typename TreeT::Ptr tree(
new TreeT(
false));
203 tree->topologyUnion(points);
207 if (points.leafCount() == 0)
return tree;
215 tree::LeafManager<TreeT> leafManager(*tree);
218 NullFilter nullFilter;
219 PointsToScalarOp<TreeT, PointDataTreeT, NullFilter> pointsToScalarOp(
221 leafManager.foreach(pointsToScalarOp, threaded);
224 PointsToScalarOp<TreeT, PointDataTreeT, FilterT> pointsToScalarOp(
226 leafManager.foreach(pointsToScalarOp, threaded);
233 template<
typename Gr
idT,
typename Po
intDataGr
idT,
typename FilterT,
typename DeformerT>
234 inline typename GridT::Ptr convertPointsToScalar(
235 PointDataGridT& points,
237 const FilterT& filter,
238 const DeformerT& deformer,
239 bool threaded =
true)
241 using point_mask_internal::PointsToTransformedScalarOp;
242 using point_mask_internal::GridCombinerOp;
244 using CombinerOpT = GridCombinerOp<GridT>;
245 using CombinableT =
typename GridCombinerOp<GridT>::CombinableT;
247 typename GridT::Ptr grid = GridT::create();
248 grid->setTransform(transform.copy());
252 const math::Transform& pointsTransform = points.constTransform();
254 if (transform == pointsTransform && std::is_same<NullDeformer, DeformerT>()) {
255 using TreeT =
typename GridT::TreeType;
256 typename TreeT::Ptr tree =
257 convertPointsToScalar<TreeT>(points.tree(),
filter, threaded);
264 if (points.constTree().leafCount() == 0)
return grid;
268 CombinableT combiner;
270 tree::LeafManager<typename PointDataGridT::TreeType> leafManager(points.tree());
273 NullFilter nullFilter;
274 PointsToTransformedScalarOp<GridT, PointDataGridT, NullFilter, DeformerT> pointsToScalarOp(
275 transform, pointsTransform, nullFilter, deformer, combiner);
276 leafManager.foreach(pointsToScalarOp, threaded);
278 PointsToTransformedScalarOp<GridT, PointDataGridT, FilterT, DeformerT> pointsToScalarOp(
279 transform, pointsTransform, filter, deformer, combiner);
280 leafManager.foreach(pointsToScalarOp, threaded);
285 CombinerOpT combineOp(*grid);
286 combiner.combine_each(combineOp);
299 template <
typename Po
intDataTreeT,
typename MaskTreeT,
typename FilterT>
303 const FilterT& filter,
306 return point_mask_internal::convertPointsToScalar<MaskTreeT>(
311 template<
typename Po
intDataGr
idT,
typename MaskGr
idT,
typename FilterT>
315 const PointDataGridT& points,
316 const FilterT& filter,
319 using PointDataTreeT =
typename PointDataGridT::TreeType;
320 using MaskTreeT =
typename MaskGridT::TreeType;
322 typename MaskTreeT::Ptr tree =
323 convertPointsToMask<PointDataTreeT, MaskTreeT, FilterT>
324 (points.tree(),
filter, threaded);
326 typename MaskGridT::Ptr grid(
new MaskGridT(tree));
327 grid->setTransform(points.transform().copy());
332 template<
typename Po
intDataGr
idT,
typename MaskT,
typename FilterT>
334 typename MaskT::Ptr>
::type
336 const PointDataGridT& points,
337 const openvdb::math::Transform& transform,
338 const FilterT& filter,
343 auto& nonConstPoints =
const_cast<typename AdapterT::NonConstGridType&
>(
points);
346 return point_mask_internal::convertPointsToScalar<MaskT>(
358 #endif // OPENVDB_POINTS_POINT_MASK_IMPL_HAS_BEEN_INCLUDED
GLdouble GLdouble GLint GLint const GLdouble * points
GLsizei const GLfloat * value
#define OPENVDB_USE_VERSION_NAMESPACE
This adapter allows code that is templated on a Tree type to accept either a Tree type or a Grid type...
GA_API const UT_StringHolder transform
std::enable_if< std::is_base_of< TreeBase, PointDataTreeT >::value &&std::is_same< typename MaskTreeT::ValueType, bool >::value, typename MaskTreeT::Ptr >::type convertPointsToMask(const PointDataTreeT &tree, const FilterT &filter, bool threaded)
Extract a Mask Tree from a Point Data Tree.
SIM_API const UT_StringHolder position
Index64 iterCount(const IterT &iter)
Count up the number of times the iterator can iterate.
#define OPENVDB_VERSION_NAME
The version namespace name for this library version.
GLint GLint GLint GLint GLint GLint GLint GLbitfield GLenum filter