OpenVDB  3.1.0
Clip.h
Go to the documentation of this file.
1 //
3 // Copyright (c) 2012-2015 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 //
35 
36 #ifndef OPENVDB_TOOLS_CLIP_HAS_BEEN_INCLUDED
37 #define OPENVDB_TOOLS_CLIP_HAS_BEEN_INCLUDED
38 
39 #include <openvdb/Grid.h>
40 #include <openvdb/math/Math.h>// for isNegative
41 #include <openvdb/tree/LeafManager.h>
42 #include "GridTransformer.h" // for resampleToMatch()
43 #include <boost/type_traits/is_same.hpp>
44 #include <boost/type_traits/is_signed.hpp>
45 #include <boost/utility/enable_if.hpp>
46 #include <tbb/blocked_range.h>
47 #include <tbb/parallel_reduce.h>
48 #include "Prune.h"
49 
50 
51 namespace openvdb {
53 namespace OPENVDB_VERSION_NAME {
54 namespace tools {
55 
60 template<typename GridType> OPENVDB_STATIC_SPECIALIZATION
61 inline typename GridType::Ptr clip(const GridType& grid, const BBoxd&);
62 
73 template<typename GridType, typename MaskTreeType> OPENVDB_STATIC_SPECIALIZATION
74 inline typename GridType::Ptr clip(const GridType& grid, const Grid<MaskTreeType>& mask);
75 
76 
78 
79 
80 namespace clip_internal {
81 
82 
84 
85 
86 template<typename TreeT>
88 {
89 public:
90  typedef typename TreeT::ValueType ValueT;
91  typedef typename TreeT::LeafNodeType LeafNodeT;
92 
93  MaskInteriorVoxels(const TreeT& tree): mAcc(tree) {}
94 
95  template <typename LeafNodeType>
96  void operator()(LeafNodeType &leaf, size_t /*leafIndex*/) const
97  {
98  const LeafNodeT *refLeaf = mAcc.probeConstLeaf(leaf.origin());
99  if (refLeaf) {
100  typename LeafNodeType::ValueOffIter iter = leaf.beginValueOff();
101  for ( ; iter; ++iter) {
102  const Index pos = iter.pos();
103  leaf.setActiveState(pos, math::isNegative(refLeaf->getValue(pos)));
104  }
105  }
106  }
107 
108 private:
110 };
111 
112 
114 
115 
116 template<typename TreeT>
118 {
119 public:
120  typedef typename TreeT::template ValueConverter<bool>::Type BoolTreeT;
122 
123  CopyLeafNodes(const TreeT& tree, const BoolLeafManagerT& leafNodes);
124 
125  void run(bool threaded = true);
126 
127  typename TreeT::Ptr tree() const { return mNewTree; }
128 
129  CopyLeafNodes(CopyLeafNodes&, tbb::split);
130  void operator()(const tbb::blocked_range<size_t>&);
131  void join(const CopyLeafNodes& rhs) { mNewTree->merge(*rhs.mNewTree); }
132 
133 private:
134  const BoolTreeT* mClipMask;
135  const TreeT* mTree;
136  const BoolLeafManagerT* mLeafNodes;
137  typename TreeT::Ptr mNewTree;
138 };
139 
140 
141 template<typename TreeT>
142 CopyLeafNodes<TreeT>::CopyLeafNodes(const TreeT& tree, const BoolLeafManagerT& leafNodes)
143  : mTree(&tree)
144  , mLeafNodes(&leafNodes)
145  , mNewTree(new TreeT(mTree->background()))
146 {
147 }
148 
149 
150 template<typename TreeT>
152  : mTree(rhs.mTree)
153  , mLeafNodes(rhs.mLeafNodes)
154  , mNewTree(new TreeT(mTree->background()))
155 {
156 }
157 
158 
159 template<typename TreeT>
160 void
162 {
163  if (threaded) tbb::parallel_reduce(mLeafNodes->getRange(), *this);
164  else (*this)(mLeafNodes->getRange());
165 }
166 
167 
168 template<typename TreeT>
169 void
170 CopyLeafNodes<TreeT>::operator()(const tbb::blocked_range<size_t>& range)
171 {
172  typedef typename TreeT::LeafNodeType LeafT;
173  typedef typename BoolTree::LeafNodeType BoolLeafT;
174  typename BoolLeafT::ValueOnCIter it;
175 
176  tree::ValueAccessor<TreeT> acc(*mNewTree);
177  tree::ValueAccessor<const TreeT> refAcc(*mTree);
178 
179  for (size_t n = range.begin(); n != range.end(); ++n) {
180  const BoolLeafT& maskLeaf = mLeafNodes->leaf(n);
181  const Coord& ijk = maskLeaf.origin();
182  const LeafT* refLeaf = refAcc.probeConstLeaf(ijk);
183 
184  LeafT* newLeaf = acc.touchLeaf(ijk);
185 
186  if (refLeaf) {
187  for (it = maskLeaf.cbeginValueOn(); it; ++it) {
188  const Index pos = it.pos();
189  newLeaf->setValueOnly(pos, refLeaf->getValue(pos));
190  newLeaf->setActiveState(pos, refLeaf->isValueOn(pos));
191  }
192  } else {
193  typename TreeT::ValueType value;
194  bool isActive = refAcc.probeValue(ijk, value);
195 
196  for (it = maskLeaf.cbeginValueOn(); it; ++it) {
197  const Index pos = it.pos();
198  newLeaf->setValueOnly(pos, value);
199  newLeaf->setActiveState(pos, isActive);
200  }
201  }
202  }
203 }
204 
205 
207 
208 
210 {
211  static const char* name() { return "bin"; }
212  static int radius() { return 2; }
213  static bool mipmap() { return false; }
214  static bool consistent() { return true; }
215 
216  template<class TreeT>
217  static bool sample(const TreeT& inTree,
218  const Vec3R& inCoord, typename TreeT::ValueType& result)
219  {
220  Coord ijk;
221  ijk[0] = int(std::floor(inCoord[0]));
222  ijk[1] = int(std::floor(inCoord[1]));
223  ijk[2] = int(std::floor(inCoord[2]));
224  return inTree.probeValue(ijk, result);
225  }
226 };
227 
228 
230 
231 
232 // Convert a grid of one type to a grid of another type
233 template<typename FromGridT, typename ToGridT>
235 {
236  typedef typename FromGridT::Ptr FromGridPtrT;
237  typedef typename ToGridT::Ptr ToGridPtrT;
238  ToGridPtrT operator()(const FromGridPtrT& grid) { return ToGridPtrT(new ToGridT(*grid)); }
239 };
240 
241 // Partial specialization that avoids copying when
242 // the input and output grid types are the same
243 template<typename GridT>
244 struct ConvertGrid<GridT, GridT>
245 {
246  typedef typename GridT::Ptr GridPtrT;
247  GridPtrT operator()(const GridPtrT& grid) { return grid; }
248 };
249 
250 
252 
253 
254 // Convert a grid of arbitrary type to a boolean mask grid and return a pointer to the new grid.
255 template<typename GridT>
256 inline typename boost::disable_if<boost::is_same<bool, typename GridT::ValueType>,
257  typename GridT::template ValueConverter<bool>::Type::Ptr>::type
258 convertToBoolMaskGrid(const GridT& grid)
259 {
260  typedef typename GridT::template ValueConverter<bool>::Type BoolGridT;
261  typedef typename BoolGridT::Ptr BoolGridPtrT;
262 
263  // Convert the input grid to a boolean mask grid (with the same tree configuration).
264  BoolGridPtrT mask = BoolGridT::create(/*background=*/false);
265  mask->topologyUnion(grid);
266  mask->setTransform(grid.constTransform().copy());
267  return mask;
268 }
269 
270 // Overload that avoids any processing if the input grid is already a boolean grid
271 template<typename GridT>
272 inline typename boost::enable_if<boost::is_same<bool, typename GridT::ValueType>,
273  typename GridT::Ptr>::type
274 convertToBoolMaskGrid(const GridT& grid)
275 {
276  return grid.copy(); // shallow copy
277 }
278 
279 
281 
282 
283 template<typename GridType>
284 inline typename GridType::Ptr
285 doClip(const GridType& grid, const typename GridType::template ValueConverter<bool>::Type& aMask)
286 {
287  typedef typename GridType::TreeType TreeT;
288  typedef typename GridType::TreeType::template ValueConverter<bool>::Type BoolTreeT;
289 
290  const GridClass gridClass = grid.getGridClass();
291  const TreeT& tree = grid.tree();
292 
293  BoolTreeT mask(false);
294  mask.topologyUnion(tree);
295 
296  if (gridClass == GRID_LEVEL_SET) {
297  tree::LeafManager<BoolTreeT> leafNodes(mask);
298  leafNodes.foreach(MaskInteriorVoxels<TreeT>(tree));
299 
301 
302  typename BoolTreeT::ValueAllIter iter(mask);
303  iter.setMaxDepth(BoolTreeT::ValueAllIter::LEAF_DEPTH - 1);
304 
305  for ( ; iter; ++iter) {
306  iter.setActiveState(math::isNegative(acc.getValue(iter.getCoord())));
307  }
308  }
309 
310  mask.topologyIntersection(aMask.constTree());
311 
312  typename GridType::Ptr outGrid;
313  {
314  // Copy voxel values and states.
315  tree::LeafManager<const BoolTreeT> leafNodes(mask);
316  CopyLeafNodes<TreeT> maskOp(tree, leafNodes);
317  maskOp.run();
318  outGrid = GridType::create(maskOp.tree());
319  }
320  {
321  // Copy tile values and states.
324 
325  typename TreeT::ValueAllIter it(outGrid->tree());
326  it.setMaxDepth(TreeT::ValueAllIter::LEAF_DEPTH - 1);
327  for ( ; it; ++it) {
328  Coord ijk = it.getCoord();
329 
330  if (maskAcc.isValueOn(ijk)) {
331  typename TreeT::ValueType value;
332  bool isActive = refAcc.probeValue(ijk, value);
333 
334  it.setValue(value);
335  if (!isActive) it.setValueOff();
336  }
337  }
338  }
339 
340  outGrid->setTransform(grid.transform().copy());
341  if (gridClass != GRID_LEVEL_SET) outGrid->setGridClass(gridClass);
342 
343  return outGrid;
344 }
345 
346 } // namespace clip_internal
347 
348 
350 
351 
352 template<typename GridType>
354 inline typename GridType::Ptr
355 clip(const GridType& grid, const BBoxd& bbox)
356 {
357  typedef typename GridType::template ValueConverter<bool>::Type BoolGridT;
358 
359  // Transform the world-space bounding box into the source grid's index space.
360  Vec3d idxMin, idxMax;
361  math::calculateBounds(grid.constTransform(), bbox.min(), bbox.max(), idxMin, idxMax);
362  CoordBBox region(Coord::floor(idxMin), Coord::floor(idxMax));
363  // Construct a boolean mask grid that is true inside the index-space bounding box
364  // and false everywhere else.
365  BoolGridT clipMask(/*background=*/false);
366  clipMask.fill(region, /*value=*/true, /*active=*/true);
367 
368  return clip_internal::doClip(grid, clipMask);
369 }
370 
371 
372 template<typename GridType, typename MaskTreeType>
374 inline typename GridType::Ptr
375 clip(const GridType& grid, const Grid<MaskTreeType>& maskGrid)
376 {
377  typedef typename GridType::template ValueConverter<bool>::Type BoolGridT;
378  typedef typename BoolGridT::Ptr BoolGridPtrT;
379 
380  typedef Grid<MaskTreeType> MaskGridType;
381  typedef typename MaskGridType::template ValueConverter<bool>::Type BoolMaskGridT;
382  typedef typename BoolMaskGridT::Ptr BoolMaskGridPtrT;
383 
384  // Convert the mask grid to a boolean grid with the same tree configuration.
385  BoolMaskGridPtrT boolMaskGrid = clip_internal::convertToBoolMaskGrid(maskGrid);
386 
387  // Resample the boolean mask grid into the source grid's index space.
388  if (grid.constTransform() != boolMaskGrid->constTransform()) {
389  BoolMaskGridPtrT resampledMask = BoolMaskGridT::create(/*background=*/false);
390  resampledMask->setTransform(grid.constTransform().copy());
391  tools::resampleToMatch<clip_internal::BoolSampler>(*boolMaskGrid, *resampledMask);
392  tools::prune(resampledMask->tree());
393  boolMaskGrid = resampledMask;
394  }
395 
396  // Convert the bool mask grid to a bool grid of the same configuration as the source grid.
397  BoolGridPtrT clipMask =
398  clip_internal::ConvertGrid</*from=*/BoolMaskGridT, /*to=*/BoolGridT>()(boolMaskGrid);
399 
400  // Clip the source grid against the boolean mask grid.
401  return clip_internal::doClip(grid, *clipMask);
402 }
403 
404 } // namespace tools
405 } // namespace OPENVDB_VERSION_NAME
406 } // namespace openvdb
407 
408 #endif // OPENVDB_TOOLS_CLIP_HAS_BEEN_INCLUDED
409 
410 // Copyright (c) 2012-2015 DreamWorks Animation LLC
411 // All rights reserved. This software is distributed under the
412 // Mozilla Public License 2.0 ( http://www.mozilla.org/MPL/2.0/ )
Index32 Index
Definition: Types.h:58
static const char * name()
Definition: Clip.h:211
ToGridPtrT operator()(const FromGridPtrT &grid)
Definition: Clip.h:238
CopyLeafNodes(const TreeT &tree, const BoolLeafManagerT &leafNodes)
Definition: Clip.h:142
TreeT::template ValueConverter< bool >::Type BoolTreeT
Definition: Clip.h:120
OPENVDB_STATIC_SPECIALIZATION GridType::Ptr clip(const GridType &grid, const BBoxd &)
Clip the given grid against a world-space bounding box and return a new grid containing the result...
Definition: Clip.h:355
This class manages a linear array of pointers to a given tree's leaf nodes, as well as optional auxil...
Definition: LeafManager.h:114
TreeT::LeafNodeType LeafNodeT
Definition: Clip.h:91
FromGridT::Ptr FromGridPtrT
Definition: Clip.h:236
static bool sample(const TreeT &inTree, const Vec3R &inCoord, typename TreeT::ValueType &result)
Definition: Clip.h:217
void operator()(const tbb::blocked_range< size_t > &)
Definition: Clip.h:170
Defined various multi-threaded utility functions for trees.
const Vec3T & min() const
Return a const reference to the minimum point of the BBox.
Definition: BBox.h:81
const ValueType & getValue(const Coord &xyz) const
Return the value of the voxel at the given coordinates.
Definition: ValueAccessor.h:256
GridClass
Definition: Types.h:204
Container class that associates a tree with a transform and metadata.
Definition: Grid.h:54
GridType::Ptr doClip(const GridType &grid, const typename GridType::template ValueConverter< bool >::Type &aMask)
Definition: Clip.h:285
const Vec3T & max() const
Return a const reference to the maximum point of the BBox.
Definition: BBox.h:84
ToGridT::Ptr ToGridPtrT
Definition: Clip.h:237
math::BBox< Vec3d > BBoxd
Definition: Types.h:86
void operator()(LeafNodeType &leaf, size_t) const
Definition: Clip.h:96
#define OPENVDB_VERSION_NAME
Definition: version.h:43
bool isValueOn(const Coord &xyz) const
Return the active state of the voxel at the given coordinates.
Definition: ValueAccessor.h:263
tree::LeafManager< const BoolTreeT > BoolLeafManagerT
Definition: Clip.h:121
OPENVDB_API void calculateBounds(const Transform &t, const Vec3d &minWS, const Vec3d &maxWS, Vec3d &minIS, Vec3d &maxIS)
Calculate an axis-aligned bounding box in index space from an axis-aligned bounding box in world spac...
const LeafNodeT * probeConstLeaf(const Coord &xyz) const
Return a pointer to the leaf node that contains voxel (x, y, z), or NULL if no such node exists...
Definition: ValueAccessor.h:429
Definition: Exceptions.h:39
bool isNegative(const Type &x)
Return true if x is less than zero.
Definition: Math.h:354
void prune(TreeT &tree, typename TreeT::ValueType tolerance=zeroVal< typename TreeT::ValueType >(), bool threaded=true, size_t grainSize=1)
Reduce the memory footprint of a tree by replacing with tiles any nodes whose values are all the same...
Definition: Prune.h:347
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:481
MaskInteriorVoxels(const TreeT &tree)
Definition: Clip.h:93
RootNodeType::LeafNodeType LeafNodeType
Definition: Tree.h:211
GridPtrT operator()(const GridPtrT &grid)
Definition: Clip.h:247
Vec3< double > Vec3d
Definition: Vec3.h:643
Definition: Types.h:206
static bool mipmap()
Definition: Clip.h:213
Axis-aligned bounding box.
Definition: BBox.h:47
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:393
static bool consistent()
Definition: Clip.h:214
static int radius()
Definition: Clip.h:212
void run(bool threaded=true)
Definition: Clip.h:161
TreeT::Ptr tree() const
Definition: Clip.h:127
#define OPENVDB_STATIC_SPECIALIZATION
Macro for determining if there are sufficient C++0x/C++11 features.
Definition: Platform.h:91
void join(const CopyLeafNodes &rhs)
Definition: Clip.h:131
OPENVDB_STATIC_SPECIALIZATION GridType::Ptr clip(const GridType &grid, const Grid< MaskTreeType > &mask)
Clip a grid against the active voxels of another grid and return a new grid containing the result...
Definition: Clip.h:375
boost::disable_if< boost::is_same< bool, typename GridT::ValueType >, typename GridT::template ValueConverter< bool >::Type::Ptr >::type convertToBoolMaskGrid(const GridT &grid)
Definition: Clip.h:258
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h:71
bool probeValue(const Coord &xyz, ValueType &value) const
Return the active state of the voxel as well as its value.
Definition: ValueAccessor.h:266
TreeT::ValueType ValueT
Definition: Clip.h:90