OpenVDB: PointsToMask.h Source File

OpenVDB 4.0.2

PointsToMask.h
Go to the documentation of this file.
1 //
3 // Copyright (c) 2012-2017 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>
83 #include <openvdb/util/NullInterrupter.h>
84
85
86 namespace openvdb {
87 OPENVDB_USE_VERSION_NAMESPACE
88 namespace OPENVDB_VERSION_NAME {
89 namespace tools {
90
91 // Forward declaration of main class
92 template<typename GridT = MaskGrid, typename InterrupterT = util::NullInterrupter>
93 class PointsToMask;
94
99 template<typename PointListT, typename GridT>
100 inline void
101 maskPoints(const PointListT& points, GridT& grid)
102 {
103 PointsToMask<GridT, util::NullInterrupter> tmp(grid, nullptr);
104 tmp.addPoints(points);
105 }
106
114 template<typename PointListT>
115 inline MaskGrid::Ptr
116 createPointMask(const PointListT& points, const math::Transform& xform)
117 {
118 MaskGrid::Ptr grid = createGrid<MaskGrid>( false );
119 grid->setTransform( xform.copy() );
120 maskPoints( points, *grid );
121 return grid;
122 }
123
125
127 template<typename GridT, typename InterrupterT>
128 class PointsToMask
129 {
130 public:
131 typedef typename GridT::ValueType ValueT;
132
137 explicit PointsToMask(GridT& grid, InterrupterT* interrupter = nullptr)
138 : mGrid(&grid)
139 , mInterrupter(interrupter)
140 {
141 }
142
148 template<typename PointListT>
149 void addPoints(const PointListT& points, size_t grainSize = 1024)
150 {
151 if (mInterrupter) mInterrupter->start("PointsToMask: adding points");
152 if (grainSize > 0) {
153 #ifdef OPENVDB_3_ABI_COMPATIBLE
154 typename GridT::Ptr examplar = mGrid->copy(CP_NEW);
155 #else
156 typename GridT::Ptr examplar = mGrid->copyWithNewTree();
157 #endif
158 PoolType pool( *examplar );//thread local storage pool of grids
159 AddPoints<PointListT> tmp(points, pool, grainSize, *this );
160 if ( this->interrupt() ) return;
161 ReducePool reducePool(pool, mGrid, size_t(0));
162 } else {
163 const math::Transform& xform = mGrid->transform();
164 typename GridT::Accessor acc = mGrid->getAccessor();
165 Vec3R wPos;
166 for (size_t i = 0, n = points.size(); i < n; ++i) {
167 if ( this->interrupt() ) break;
168 points.getPos(i, wPos);
169 acc.setValueOn( xform.worldToIndexCellCentered( wPos ) );
170 }
171 }
172 if (mInterrupter) mInterrupter->end();
173 }
174
175 private:
176 // Disallow copy construction and copy by assignment!
177 PointsToMask(const PointsToMask&);// not implemented
178 PointsToMask& operator=(const PointsToMask&);// not implemented
179
180 bool interrupt() const
181 {
182 if (mInterrupter && util::wasInterrupted(mInterrupter)) {
183 tbb::task::self().cancel_group_execution();
184 return true;
185 }
186 return false;
187 }
188
189 // Private struct that implements concurrent thread-local
190 // insersion of points into a grid
191 typedef tbb::enumerable_thread_specific<GridT> PoolType;
192 template<typename PointListT> struct AddPoints;
193
194 // Private class that implements concurrent reduction of a thread-local pool
195 struct ReducePool;
196
197 GridT* mGrid;
198 InterrupterT* mInterrupter;
199 };// PointsToMask
200
201 // Private member class that implements concurrent thread-local
202 // insersion of points into a grid
203 template<typename GridT, typename InterrupterT>
204 template<typename PointListT>
205 struct PointsToMask<GridT, InterrupterT>::AddPoints
206 {
207 AddPoints(const PointListT& points,
208 PoolType& pool,
209 size_t grainSize,
210 const PointsToMask& parent)
211 : mPoints(&points)
212 , mParent(&parent)
213 , mPool(&pool)
214 {
215 tbb::parallel_for(tbb::blocked_range<size_t>(0, mPoints->size(), grainSize), *this);
216 }
217 void operator()(const tbb::blocked_range<size_t>& range) const
218 {
219 if (mParent->interrupt()) return;
220 GridT& grid = mPool->local();
221 const math::Transform& xform = grid.transform();
222 typename GridT::Accessor acc = grid.getAccessor();
223 Vec3R wPos;
224 for (size_t i=range.begin(), n=range.end(); i!=n; ++i) {
225 mPoints->getPos(i, wPos);
226 acc.setValueOn( xform.worldToIndexCellCentered( wPos ) );
227 }
228 }
229 const PointListT* mPoints;
230 const PointsToMask* mParent;
231 PoolType* mPool;
232
233 };// end of private member class AddPoints
234
235 // Private member class that implements concurrent reduction of a thread-local pool
236 template<typename GridT, typename InterrupterT>
237 struct PointsToMask<GridT, InterrupterT>::ReducePool
238 {
239 typedef std::vector<GridT*> VecT;
240 typedef typename VecT::iterator IterT;
241 typedef tbb::blocked_range<IterT> RangeT;
242
243 ReducePool(PoolType& pool, GridT* grid, size_t grainSize = 1)
244 : mOwnsGrid(false)
245 , mGrid(grid)
246 {
247 if ( grainSize == 0 ) {
248 typedef typename PoolType::const_iterator IterT;
249 for (IterT i=pool.begin(); i!=pool.end(); ++i) mGrid->topologyUnion( *i );
250 } else {
251 VecT grids( pool.size() );
252 typename PoolType::iterator i = pool.begin();
253 for (size_t j=0; j != pool.size(); ++i, ++j) grids[j] = &(*i);
254 tbb::parallel_reduce( RangeT( grids.begin(), grids.end(), grainSize ), *this );
255 }
256 }
257
258 ReducePool(const ReducePool&, tbb::split)
259 : mOwnsGrid(true)
260 , mGrid(new GridT())
261 {
262 }
263
264 ~ReducePool() { if (mOwnsGrid) delete mGrid; }
265
266 void operator()(const RangeT& r)
267 {
268 for (IterT i=r.begin(); i!=r.end(); ++i) mGrid->topologyUnion( *(*i) );
269 }
270
271 void join(ReducePool& other) { mGrid->topologyUnion(*other.mGrid); }
272
273 const bool mOwnsGrid;
274 GridT* mGrid;
275 };// end of private member class ReducePool
276
277 } // namespace tools
278 } // namespace OPENVDB_VERSION_NAME
279 } // namespace openvdb
280
281 #endif // OPENVDB_TOOLS_POINTSTOMASK_HAS_BEEN_INCLUDED
282
283 // Copyright (c) 2012-2017 DreamWorks Animation LLC
284 // All rights reserved. This software is distributed under the
285 // Mozilla Public License 2.0 ( http://www.mozilla.org/MPL/2.0/ )
openvdb::v4_0_2::tools::PointsToMask::ReducePool::IterT
VecT::iterator IterT
Definition: PointsToMask.h:240
openvdb::v4_0_2::tools::PointsToMask::ValueT
GridT::ValueType ValueT
Definition: PointsToMask.h:131
openvdb::v4_0_2::tools::PointsToMask::addPoints
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:149
Types.h
openvdb::v4_0_2::tools::PointsToMask::ReducePool::ReducePool
ReducePool(const ReducePool &, tbb::split)
Definition: PointsToMask.h:258
NullInterrupter.h
openvdb::v4_0_2::tools::PointsToMask::ReducePool::RangeT
tbb::blocked_range< IterT > RangeT
Definition: PointsToMask.h:241
openvdb.h
openvdb::v4_0_2::Grid::Ptr
SharedPtr< Grid > Ptr
Definition: Grid.h:502
openvdb::v4_0_2::math::Transform::worldToIndexCellCentered
Coord worldToIndexCellCentered(const Vec3d &xyz) const
Apply this transformation to the given coordinates.
Definition: Transform.h:138
openvdb::v4_0_2::math::Transform::copy
Ptr copy() const
Definition: Transform.h:77
openvdb::v4_0_2::tools::PointsToMask::ReducePool::operator()
void operator()(const RangeT &r)
Definition: PointsToMask.h:266
i
tbb::atomic< Index32 > i
Definition: LeafBuffer.h:71
openvdb::v4_0_2::math::Vec3< Real >
openvdb::v4_0_2::tools::maskPoints
void maskPoints(const PointListT &points, GridT &grid)
Makes every voxel of the grid active if it contains a point.
Definition: PointsToMask.h:101
openvdb::v4_0_2::util::wasInterrupted
bool wasInterrupted(T *i, int percent=-1)
Definition: NullInterrupter.h:76
openvdb::v4_0_2::math::Transform
Calculate an axis-aligned bounding box in index space from a bounding sphere in world space...
Definition: Transform.h:66
OPENVDB_VERSION_NAME
#define OPENVDB_VERSION_NAME
Definition: version.h:43
openvdb::v4_0_2::tools::PointsToMask::ReducePool::join
void join(ReducePool &other)
Definition: PointsToMask.h:271
openvdb::v4_0_2::tools::PointsToMask::PointsToMask
PointsToMask(GridT &grid, InterrupterT *interrupter=nullptr)
Constructor from a grid and optional interrupter.
Definition: PointsToMask.h:137
openvdb::v4_0_2::tools::PointsToMask::ReducePool::mOwnsGrid
const bool mOwnsGrid
Definition: PointsToMask.h:273
openvdb
Definition: Exceptions.h:39
openvdb::v4_0_2::tools::createPointMask
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:116
openvdb::v4_0_2::tools::PointsToMask::ReducePool::ReducePool
ReducePool(PoolType &pool, GridT *grid, size_t grainSize=1)
Definition: PointsToMask.h:243
openvdb::v4_0_2::tools::PointsToMask
Makes every voxel of a grid active if it contains a point.
Definition: PointsToMask.h:93
openvdb::v4_0_2::tools::PointsToMask::ReducePool::VecT
std::vector< GridT * > VecT
Definition: PointsToMask.h:239
openvdb::v4_0_2::tools::PointsToMask::ReducePool
Definition: PointsToMask.h:237
Grid.h
openvdb::v4_0_2::tools::PointsToMask::ReducePool::mGrid
GridT * mGrid
Definition: PointsToMask.h:274
OPENVDB_USE_VERSION_NAMESPACE
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h:71
openvdb::v4_0_2::tools::PointsToMask::ReducePool::~ReducePool
~ReducePool()
Definition: PointsToMask.h:264
openvdb::v4_0_2::Vec3R
math::Vec3< Real > Vec3R
Definition: Types.h:75