46 #ifndef OPENVDB_TOOLS_POINTSTOMASK_HAS_BEEN_INCLUDED
47 #define OPENVDB_TOOLS_POINTSTOMASK_HAS_BEEN_INCLUDED
49 #include <tbb/enumerable_thread_specific.h>
50 #include <tbb/parallel_for.h>
51 #include <tbb/parallel_reduce.h>
52 #include <tbb/blocked_range.h>
66 template<
typename Gr
idT = MaskGr
id,
typename InterrupterT = util::NullInterrupter>
73 template<
typename Po
intListT,
typename Gr
idT>
88 template<
typename Po
intListT>
93 grid->setTransform( xform.
copy() );
101 template<
typename Gr
idT,
typename InterrupterT>
105 using ValueT =
typename GridT::ValueType;
122 template<
typename Po
intListT>
123 void addPoints(
const PointListT& points,
size_t grainSize = 1024)
125 if (mInterrupter) mInterrupter->start(
"PointsToMask: adding points");
127 typename GridT::Ptr examplar = mGrid->copyWithNewTree();
128 PoolType pool( *examplar );
129 AddPoints<PointListT> tmp(points, pool, grainSize, *
this );
130 if ( this->interrupt() )
return;
131 ReducePool reducePool(pool, mGrid,
size_t(0));
134 typename GridT::Accessor acc = mGrid->getAccessor();
136 for (
size_t i = 0, n = points.size(); i < n; ++i) {
137 if ( this->interrupt() )
break;
138 points.getPos(i, wPos);
142 if (mInterrupter) mInterrupter->end();
150 bool interrupt()
const
153 tbb::task::self().cancel_group_execution();
161 using PoolType = tbb::enumerable_thread_specific<GridT>;
162 template<
typename Po
intListT>
struct AddPoints;
168 InterrupterT* mInterrupter;
173 template<
typename Gr
idT,
typename InterrupterT>
174 template<
typename Po
intListT>
175 struct PointsToMask<GridT, InterrupterT>::AddPoints
177 AddPoints(
const PointListT& points,
180 const PointsToMask& parent)
185 tbb::parallel_for(tbb::blocked_range<size_t>(0, mPoints->size(), grainSize), *
this);
187 void operator()(
const tbb::blocked_range<size_t>& range)
const
189 if (mParent->interrupt())
return;
190 GridT& grid = mPool->local();
191 const math::Transform& xform = grid.transform();
192 typename GridT::Accessor acc = grid.getAccessor();
194 for (
size_t i=range.begin(), n=range.end(); i!=n; ++i) {
195 mPoints->getPos(i, wPos);
196 acc.setValueOn( xform.worldToIndexCellCentered( wPos ) );
199 const PointListT* mPoints;
200 const PointsToMask* mParent;
206 template<
typename Gr
idT,
typename InterrupterT>
209 using VecT = std::vector<GridT*>;
210 using IterT =
typename VecT::iterator;
211 using RangeT = tbb::blocked_range<IterT>;
213 ReducePool(PoolType& pool, GridT* grid,
size_t grainSize = 1)
217 if (grainSize == 0) {
218 for (
typename PoolType::const_iterator i = pool.begin(); i != pool.end(); ++i) {
219 mGrid->topologyUnion(*i);
222 VecT grids( pool.size() );
223 typename PoolType::iterator i = pool.begin();
224 for (
size_t j=0; j != pool.size(); ++i, ++j) grids[j] = &(*i);
225 tbb::parallel_reduce(
RangeT( grids.begin(), grids.end(), grainSize ), *
this );
239 for (
IterT i=r.begin(); i!=r.end(); ++i) mGrid->topologyUnion( *(*i) );
252 #endif // OPENVDB_TOOLS_POINTSTOMASK_HAS_BEEN_INCLUDED