OpenVDB  6.1.0
PointsToMask.h
Go to the documentation of this file.
1 //
3 // Copyright (c) 2012-2019 DreamWorks Animation LLC
4 //
5 // All rights reserved. This software is distributed under the
6 // Mozilla Public License 2.0 ( http://www.mozilla.org/MPL/2.0/ )
7 //
8 // Redistributions of source code must retain the above copyright
9 // and license notice and the following restrictions and disclaimer.
10 //
11 // * Neither the name of DreamWorks Animation nor the names of
12 // its contributors may be used to endorse or promote products derived
13 // from this software without specific prior written permission.
14 //
15 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
18 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
19 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY INDIRECT, INCIDENTAL,
20 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
21 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 // IN NO EVENT SHALL THE COPYRIGHT HOLDERS' AND CONTRIBUTORS' AGGREGATE
27 // LIABILITY FOR ALL CLAIMS REGARDLESS OF THEIR BASIS EXCEED US$250.00.
28 //
30 
72 
73 #ifndef OPENVDB_TOOLS_POINTSTOMASK_HAS_BEEN_INCLUDED
74 #define OPENVDB_TOOLS_POINTSTOMASK_HAS_BEEN_INCLUDED
75 
76 #include <tbb/enumerable_thread_specific.h>
77 #include <tbb/parallel_for.h>
78 #include <tbb/parallel_reduce.h>
79 #include <tbb/blocked_range.h>
80 #include <openvdb/openvdb.h> // for MaskGrid
81 #include <openvdb/Grid.h>
82 #include <openvdb/Types.h>
84 #include <vector>
85 
86 
87 namespace openvdb {
89 namespace OPENVDB_VERSION_NAME {
90 namespace tools {
91 
92 // Forward declaration of main class
93 template<typename GridT = MaskGrid, typename InterrupterT = util::NullInterrupter>
95 
100 template<typename PointListT, typename GridT>
101 inline void
102 maskPoints(const PointListT& points, GridT& grid)
103 {
105  tmp.addPoints(points);
106 }
107 
115 template<typename PointListT>
116 inline MaskGrid::Ptr
117 createPointMask(const PointListT& points, const math::Transform& xform)
118 {
119  MaskGrid::Ptr grid = createGrid<MaskGrid>( false );
120  grid->setTransform( xform.copy() );
121  maskPoints( points, *grid );
122  return grid;
123 }
124 
126 
128 template<typename GridT, typename InterrupterT>
129 class PointsToMask
130 {
131 public:
132  using ValueT = typename GridT::ValueType;
133 
138  explicit PointsToMask(GridT& grid, InterrupterT* interrupter = nullptr)
139  : mGrid(&grid)
140  , mInterrupter(interrupter)
141  {
142  }
143 
149  template<typename PointListT>
150  void addPoints(const PointListT& points, size_t grainSize = 1024)
151  {
152  if (mInterrupter) mInterrupter->start("PointsToMask: adding points");
153  if (grainSize > 0) {
154 #if OPENVDB_ABI_VERSION_NUMBER <= 3
155  typename GridT::Ptr examplar = mGrid->copy(CP_NEW);
156 #else
157  typename GridT::Ptr examplar = mGrid->copyWithNewTree();
158 #endif
159  PoolType pool( *examplar );//thread local storage pool of grids
160  AddPoints<PointListT> tmp(points, pool, grainSize, *this );
161  if ( this->interrupt() ) return;
162  ReducePool reducePool(pool, mGrid, size_t(0));
163  } else {
164  const math::Transform& xform = mGrid->transform();
165  typename GridT::Accessor acc = mGrid->getAccessor();
166  Vec3R wPos;
167  for (size_t i = 0, n = points.size(); i < n; ++i) {
168  if ( this->interrupt() ) break;
169  points.getPos(i, wPos);
170  acc.setValueOn( xform.worldToIndexCellCentered( wPos ) );
171  }
172  }
173  if (mInterrupter) mInterrupter->end();
174  }
175 
176 private:
177  // Disallow copy construction and copy by assignment!
178  PointsToMask(const PointsToMask&);// not implemented
179  PointsToMask& operator=(const PointsToMask&);// not implemented
180 
181  bool interrupt() const
182  {
183  if (mInterrupter && util::wasInterrupted(mInterrupter)) {
184  tbb::task::self().cancel_group_execution();
185  return true;
186  }
187  return false;
188  }
189 
190  // Private struct that implements concurrent thread-local
191  // insersion of points into a grid
192  using PoolType = tbb::enumerable_thread_specific<GridT>;
193  template<typename PointListT> struct AddPoints;
194 
195  // Private class that implements concurrent reduction of a thread-local pool
196  struct ReducePool;
197 
198  GridT* mGrid;
199  InterrupterT* mInterrupter;
200 };// PointsToMask
201 
202 // Private member class that implements concurrent thread-local
203 // insersion of points into a grid
204 template<typename GridT, typename InterrupterT>
205 template<typename PointListT>
206 struct PointsToMask<GridT, InterrupterT>::AddPoints
207 {
208  AddPoints(const PointListT& points,
209  PoolType& pool,
210  size_t grainSize,
211  const PointsToMask& parent)
212  : mPoints(&points)
213  , mParent(&parent)
214  , mPool(&pool)
215  {
216  tbb::parallel_for(tbb::blocked_range<size_t>(0, mPoints->size(), grainSize), *this);
217  }
218  void operator()(const tbb::blocked_range<size_t>& range) const
219  {
220  if (mParent->interrupt()) return;
221  GridT& grid = mPool->local();
222  const math::Transform& xform = grid.transform();
223  typename GridT::Accessor acc = grid.getAccessor();
224  Vec3R wPos;
225  for (size_t i=range.begin(), n=range.end(); i!=n; ++i) {
226  mPoints->getPos(i, wPos);
227  acc.setValueOn( xform.worldToIndexCellCentered( wPos ) );
228  }
229  }
230  const PointListT* mPoints;
231  const PointsToMask* mParent;
232  PoolType* mPool;
233 
234 };// end of private member class AddPoints
235 
236 // Private member class that implements concurrent reduction of a thread-local pool
237 template<typename GridT, typename InterrupterT>
238 struct PointsToMask<GridT, InterrupterT>::ReducePool
239 {
240  using VecT = std::vector<GridT*>;
241  using IterT = typename VecT::iterator;
242  using RangeT = tbb::blocked_range<IterT>;
243 
244  ReducePool(PoolType& pool, GridT* grid, size_t grainSize = 1)
245  : mOwnsGrid(false)
246  , mGrid(grid)
247  {
248  if (grainSize == 0) {
249  for (typename PoolType::const_iterator i = pool.begin(); i != pool.end(); ++i) {
250  mGrid->topologyUnion(*i);
251  }
252  } else {
253  VecT grids( pool.size() );
254  typename PoolType::iterator i = pool.begin();
255  for (size_t j=0; j != pool.size(); ++i, ++j) grids[j] = &(*i);
256  tbb::parallel_reduce( RangeT( grids.begin(), grids.end(), grainSize ), *this );
257  }
258  }
259 
260  ReducePool(const ReducePool&, tbb::split)
261  : mOwnsGrid(true)
262  , mGrid(new GridT())
263  {
264  }
265 
266  ~ReducePool() { if (mOwnsGrid) delete mGrid; }
267 
268  void operator()(const RangeT& r)
269  {
270  for (IterT i=r.begin(); i!=r.end(); ++i) mGrid->topologyUnion( *(*i) );
271  }
272 
273  void join(ReducePool& other) { mGrid->topologyUnion(*other.mGrid); }
274 
275  const bool mOwnsGrid;
276  GridT* mGrid;
277 };// end of private member class ReducePool
278 
279 } // namespace tools
280 } // namespace OPENVDB_VERSION_NAME
281 } // namespace openvdb
282 
283 #endif // OPENVDB_TOOLS_POINTSTOMASK_HAS_BEEN_INCLUDED
284 
285 // Copyright (c) 2012-2019 DreamWorks Animation LLC
286 // All rights reserved. This software is distributed under the
287 // Mozilla Public License 2.0 ( http://www.mozilla.org/MPL/2.0/ )
void addPoints(const PointListT &points, size_t grainSize=1024)
Activates the state of any voxel in the input grid that contains a point.
Definition: PointsToMask.h:150
typename VecT::iterator IterT
Definition: PointsToMask.h:241
Coord worldToIndexCellCentered(const Vec3d &xyz) const
Apply this transformation to the given coordinates.
Definition: Transform.h:138
ReducePool(PoolType &pool, GridT *grid, size_t grainSize=1)
Definition: PointsToMask.h:244
MaskGrid::Ptr createPointMask(const PointListT &points, const math::Transform &xform)
Return a MaskGrid where each binary voxel value is on if the voxel contains one (or more) points (i...
Definition: PointsToMask.h:117
void maskPoints(const PointListT &points, GridT &grid)
Makes every voxel of the grid active if it contains a point.
Definition: PointsToMask.h:102
tbb::blocked_range< IterT > RangeT
Definition: PointsToMask.h:242
bool wasInterrupted(T *i, int percent=-1)
Definition: NullInterrupter.h:76
ReducePool(const ReducePool &, tbb::split)
Definition: PointsToMask.h:260
std::vector< GridT * > VecT
Definition: PointsToMask.h:240
#define OPENVDB_VERSION_NAME
The version namespace name for this library version.
Definition: version.h:125
const bool mOwnsGrid
Definition: PointsToMask.h:275
Makes every voxel of a grid active if it contains a point.
Definition: PointsToMask.h:94
PointsToMask(GridT &grid, InterrupterT *interrupter=nullptr)
Constructor from a grid and optional interrupter.
Definition: PointsToMask.h:138
void join(ReducePool &other)
Definition: PointsToMask.h:273
Definition: Exceptions.h:40
~ReducePool()
Definition: PointsToMask.h:266
Ptr copy() const
Definition: Transform.h:77
void operator()(const RangeT &r)
Definition: PointsToMask.h:268
Definition: Transform.h:66
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h:177
SharedPtr< Grid > Ptr
Definition: Grid.h:592
typename GridT::ValueType ValueT
Definition: PointsToMask.h:132
GridT * mGrid
Definition: PointsToMask.h:276