HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Grid.h
Go to the documentation of this file.
1 ///////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright (c) 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 //
29 ///////////////////////////////////////////////////////////////////////////
30 
31 #ifndef OPENVDB_GRID_HAS_BEEN_INCLUDED
32 #define OPENVDB_GRID_HAS_BEEN_INCLUDED
33 
34 #include "Exceptions.h"
35 #include "MetaMap.h"
36 #include "Types.h"
37 #include "io/io.h"
38 #include "math/Transform.h"
39 #include "tree/Tree.h"
40 #include "util/logging.h"
41 #include "util/Name.h"
42 #include <cassert>
43 #include <iostream>
44 #include <set>
45 #include <type_traits>
46 #include <vector>
47 
48 
49 namespace openvdb {
51 namespace OPENVDB_VERSION_NAME {
52 
54 
55 template<typename> class Grid; // forward declaration
56 
57 
58 /// @brief Create a new grid of type @c GridType with a given background value.
59 ///
60 /// @note Calling createGrid<GridType>(background) is equivalent to calling
61 /// GridType::create(background).
62 template<typename GridType>
63 inline typename GridType::Ptr createGrid(const typename GridType::ValueType& background);
64 
65 
66 /// @brief Create a new grid of type @c GridType with background value zero.
67 ///
68 /// @note Calling createGrid<GridType>() is equivalent to calling GridType::create().
69 template<typename GridType>
70 inline typename GridType::Ptr createGrid();
71 
72 
73 /// @brief Create a new grid of the appropriate type that wraps the given tree.
74 ///
75 /// @note This function can be called without specifying the template argument,
76 /// i.e., as createGrid(tree).
77 template<typename TreePtrType>
79 
80 
81 /// @brief Create a new grid of type @c GridType classified as a "Level Set",
82 /// i.e., a narrow-band level set.
83 ///
84 /// @note @c GridType::ValueType must be a floating-point scalar.
85 ///
86 /// @param voxelSize the size of a voxel in world units
87 /// @param halfWidth the half width of the narrow band in voxel units
88 ///
89 /// @details The voxel size and the narrow band half width define the grid's
90 /// background value as halfWidth*voxelWidth. The transform is linear
91 /// with a uniform scaling only corresponding to the specified voxel size.
92 ///
93 /// @note It is generally advisable to specify a half-width of the narrow band
94 /// that is larger than one voxel unit, otherwise zero crossings are not guaranteed.
95 template<typename GridType>
96 typename GridType::Ptr createLevelSet(
97  Real voxelSize = 1.0, Real halfWidth = LEVEL_SET_HALF_WIDTH);
98 
99 
100 ////////////////////////////////////////
101 
102 
103 /// @brief Abstract base class for typed grids
105 {
106 public:
109 
110  using GridFactory = Ptr (*)();
111 
112 
113  ~GridBase() override {}
114 
115 
116  /// @name Copying
117  /// @{
118 
119 #if OPENVDB_ABI_VERSION_NUMBER <= 3
120  /// @brief Return a new grid of the same type as this grid and whose
121  /// metadata and transform are deep copies of this grid's.
122  /// @deprecated ABI versions older than 4 are deprecated.
123  OPENVDB_DEPRECATED virtual GridBase::Ptr copyGrid(CopyPolicy treePolicy = CP_SHARE) const = 0;
124 #else
125  /// @brief Return a new grid of the same type as this grid whose metadata is a
126  /// deep copy of this grid's and whose tree and transform are shared with this grid.
127  virtual GridBase::Ptr copyGrid() = 0;
128  /// @brief Return a new grid of the same type as this grid whose metadata is a
129  /// deep copy of this grid's and whose tree and transform are shared with this grid.
130  virtual GridBase::ConstPtr copyGrid() const = 0;
131  /// @brief Return a new grid of the same type as this grid whose metadata and
132  /// transform are deep copies of this grid's and whose tree is default-constructed.
133  virtual GridBase::Ptr copyGridWithNewTree() const = 0;
134 #endif
135  /// Return a new grid whose metadata, transform and tree are deep copies of this grid's.
136  virtual GridBase::Ptr deepCopyGrid() const = 0;
137 
138  /// @}
139 
140 
141  /// @name Registry
142  /// @{
143 
144  /// Create a new grid of the given (registered) type.
145  static Ptr createGrid(const Name& type);
146 
147  /// Return @c true if the given grid type name is registered.
148  static bool isRegistered(const Name &type);
149 
150  /// Clear the grid type registry.
151  static void clearRegistry();
152 
153  /// @}
154 
155  /// @name Type access
156  /// @{
157 
158  /// Return the name of this grid's type.
159  virtual Name type() const = 0;
160  /// Return the name of the type of a voxel's value (e.g., "float" or "vec3d").
161  virtual Name valueType() const = 0;
162 
163  /// Return @c true if this grid is of the same type as the template parameter.
164  template<typename GridType>
165  bool isType() const { return (this->type() == GridType::gridType()); }
166 
167  /// @}
168 
169  //@{
170  /// @brief Return the result of downcasting a GridBase pointer to a Grid pointer
171  /// of the specified type, or return a null pointer if the types are incompatible.
172  template<typename GridType>
173  static typename GridType::Ptr grid(const GridBase::Ptr&);
174  template<typename GridType>
175  static typename GridType::ConstPtr grid(const GridBase::ConstPtr&);
176  template<typename GridType>
177  static typename GridType::ConstPtr constGrid(const GridBase::Ptr&);
178  template<typename GridType>
179  static typename GridType::ConstPtr constGrid(const GridBase::ConstPtr&);
180  //@}
181 
182  /// @name Tree
183  /// @{
184 
185  /// @brief Return a pointer to this grid's tree, which might be
186  /// shared with other grids. The pointer is guaranteed to be non-null.
187  TreeBase::Ptr baseTreePtr();
188  /// @brief Return a pointer to this grid's tree, which might be
189  /// shared with other grids. The pointer is guaranteed to be non-null.
190  TreeBase::ConstPtr baseTreePtr() const { return this->constBaseTreePtr(); }
191  /// @brief Return a pointer to this grid's tree, which might be
192  /// shared with other grids. The pointer is guaranteed to be non-null.
193  virtual TreeBase::ConstPtr constBaseTreePtr() const = 0;
194 
195  /// @brief Return a reference to this grid's tree, which might be
196  /// shared with other grids.
197  /// @note Calling @vdblink::GridBase::setTree() setTree@endlink
198  /// on this grid invalidates all references previously returned by this method.
199  TreeBase& baseTree() { return const_cast<TreeBase&>(this->constBaseTree()); }
200  /// @brief Return a reference to this grid's tree, which might be
201  /// shared with other grids.
202  /// @note Calling @vdblink::GridBase::setTree() setTree@endlink
203  /// on this grid invalidates all references previously returned by this method.
204  const TreeBase& baseTree() const { return this->constBaseTree(); }
205  /// @brief Return a reference to this grid's tree, which might be
206  /// shared with other grids.
207  /// @note Calling @vdblink::GridBase::setTree() setTree@endlink
208  /// on this grid invalidates all references previously returned by this method.
209  const TreeBase& constBaseTree() const { return *(this->constBaseTreePtr()); }
210 
211  /// @brief Associate the given tree with this grid, in place of its existing tree.
212  /// @throw ValueError if the tree pointer is null
213  /// @throw TypeError if the tree is not of the appropriate type
214  /// @note Invalidates all references previously returned by
215  /// @vdblink::GridBase::baseTree() baseTree@endlink
216  /// or @vdblink::GridBase::constBaseTree() constBaseTree@endlink.
217  virtual void setTree(TreeBase::Ptr) = 0;
218 
219  /// Set a new tree with the same background value as the previous tree.
220  virtual void newTree() = 0;
221 
222  /// @}
223 
224  /// Return @c true if this grid contains only background voxels.
225  virtual bool empty() const = 0;
226  /// Empty this grid, setting all voxels to the background.
227  virtual void clear() = 0;
228 
229 
230  /// @name Tools
231  /// @{
232 
233  /// @brief Reduce the memory footprint of this grid by increasing its sparseness
234  /// either losslessly (@a tolerance = 0) or lossily (@a tolerance > 0).
235  /// @details With @a tolerance > 0, sparsify regions where voxels have the same
236  /// active state and have values that differ by no more than the tolerance
237  /// (converted to this grid's value type).
238  virtual void pruneGrid(float tolerance = 0.0) = 0;
239 
240 #if OPENVDB_ABI_VERSION_NUMBER >= 3
241  /// @brief Clip this grid to the given world-space bounding box.
242  /// @details Voxels that lie outside the bounding box are set to the background.
243  /// @warning Clipping a level set will likely produce a grid that is
244  /// no longer a valid level set.
245  void clipGrid(const BBoxd&);
246 
247  /// @brief Clip this grid to the given index-space bounding box.
248  /// @details Voxels that lie outside the bounding box are set to the background.
249  /// @warning Clipping a level set will likely produce a grid that is
250  /// no longer a valid level set.
251  virtual void clip(const CoordBBox&) = 0;
252 #endif
253 
254  /// @}
255 
256  /// @{
257  /// @brief If this grid resolves to one of the listed grid types,
258  /// invoke the given functor on the resolved grid.
259  /// @return @c false if this grid's type is not one of the listed types
260  ///
261  /// @par Example:
262  /// @code
263  /// using AllowedGridTypes = openvdb::TypeList<
264  /// openvdb::Int32Grid, openvdb::Int64Grid,
265  /// openvdb::FloatGrid, openvdb::DoubleGrid>;
266  ///
267  /// const openvdb::CoordBBox bbox{
268  /// openvdb::Coord{0,0,0}, openvdb::Coord{10,10,10}};
269  ///
270  /// // Fill the grid if it is one of the allowed types.
271  /// myGridBasePtr->apply<AllowedGridTypes>(
272  /// [&bbox](auto& grid) { // C++14
273  /// using GridType = typename std::decay<decltype(grid)>::type;
274  /// grid.fill(bbox, typename GridType::ValueType(1));
275  /// }
276  /// );
277  /// @endcode
278  ///
279  /// @see @vdblink::TypeList TypeList@endlink
280  template<typename GridTypeListT, typename OpT> inline bool apply(OpT&) const;
281  template<typename GridTypeListT, typename OpT> inline bool apply(OpT&);
282  template<typename GridTypeListT, typename OpT> inline bool apply(const OpT&) const;
283  template<typename GridTypeListT, typename OpT> inline bool apply(const OpT&);
284  /// @}
285 
286  /// @name Metadata
287  /// @{
288 
289  /// Return this grid's user-specified name.
290  std::string getName() const;
291  /// Specify a name for this grid.
292  void setName(const std::string&);
293 
294  /// Return the user-specified description of this grid's creator.
295  std::string getCreator() const;
296  /// Provide a description of this grid's creator.
297  void setCreator(const std::string&);
298 
299  /// @brief Return @c true if this grid should be written out with floating-point
300  /// voxel values (including components of vectors) quantized to 16 bits.
301  bool saveFloatAsHalf() const;
302  void setSaveFloatAsHalf(bool);
303 
304  /// @brief Return the class of volumetric data (level set, fog volume, etc.)
305  /// that is stored in this grid.
306  /// @sa gridClassToString, gridClassToMenuName, stringToGridClass
307  GridClass getGridClass() const;
308  /// @brief Specify the class of volumetric data (level set, fog volume, etc.)
309  /// that is stored in this grid.
310  /// @sa gridClassToString, gridClassToMenuName, stringToGridClass
311  void setGridClass(GridClass);
312  /// Remove the setting specifying the class of this grid's volumetric data.
313  void clearGridClass();
314 
315  /// @}
316 
317  /// Return the metadata string value for the given class of volumetric data.
318  static std::string gridClassToString(GridClass);
319  /// Return a formatted string version of the grid class.
320  static std::string gridClassToMenuName(GridClass);
321  /// @brief Return the class of volumetric data specified by the given string.
322  /// @details If the string is not one of the ones returned by
323  /// @vdblink::GridBase::gridClassToString() gridClassToString@endlink,
324  /// return @c GRID_UNKNOWN.
325  static GridClass stringToGridClass(const std::string&);
326 
327  /// @name Metadata
328  /// @{
329 
330  /// @brief Return the type of vector data (invariant, covariant, etc.) stored
331  /// in this grid, assuming that this grid contains a vector-valued tree.
332  /// @sa vecTypeToString, vecTypeExamples, vecTypeDescription, stringToVecType
333  VecType getVectorType() const;
334  /// @brief Specify the type of vector data (invariant, covariant, etc.) stored
335  /// in this grid, assuming that this grid contains a vector-valued tree.
336  /// @sa vecTypeToString, vecTypeExamples, vecTypeDescription, stringToVecType
337  void setVectorType(VecType);
338  /// Remove the setting specifying the type of vector data stored in this grid.
339  void clearVectorType();
340 
341  /// @}
342 
343  /// Return the metadata string value for the given type of vector data.
344  static std::string vecTypeToString(VecType);
345  /// Return a string listing examples of the given type of vector data
346  /// (e.g., "Gradient/Normal", given VEC_COVARIANT).
347  static std::string vecTypeExamples(VecType);
348  /// @brief Return a string describing how the given type of vector data is affected
349  /// by transformations (e.g., "Does not transform", given VEC_INVARIANT).
350  static std::string vecTypeDescription(VecType);
351  static VecType stringToVecType(const std::string&);
352 
353  /// @name Metadata
354  /// @{
355 
356  /// Return @c true if this grid's voxel values are in world space and should be
357  /// affected by transformations, @c false if they are in local space and should
358  /// not be affected by transformations.
359  bool isInWorldSpace() const;
360  /// Specify whether this grid's voxel values are in world space or in local space.
361  void setIsInWorldSpace(bool);
362 
363  /// @}
364 
365  // Standard metadata field names
366  // (These fields should normally not be accessed directly, but rather
367  // via the accessor methods above, when available.)
368  // Note: Visual C++ requires these declarations to be separate statements.
369  static const char* const META_GRID_CLASS;
370  static const char* const META_GRID_CREATOR;
371  static const char* const META_GRID_NAME;
372  static const char* const META_SAVE_HALF_FLOAT;
373  static const char* const META_IS_LOCAL_SPACE;
374  static const char* const META_VECTOR_TYPE;
375  static const char* const META_FILE_BBOX_MIN;
376  static const char* const META_FILE_BBOX_MAX;
377  static const char* const META_FILE_COMPRESSION;
378  static const char* const META_FILE_MEM_BYTES;
379  static const char* const META_FILE_VOXEL_COUNT;
380  static const char* const META_FILE_DELAYED_LOAD;
381 
382 
383  /// @name Statistics
384  /// @{
385 
386  /// Return the number of active voxels.
387  virtual Index64 activeVoxelCount() const = 0;
388 
389  /// Return the axis-aligned bounding box of all active voxels. If
390  /// the grid is empty a default bbox is returned.
391  virtual CoordBBox evalActiveVoxelBoundingBox() const = 0;
392 
393  /// Return the dimensions of the axis-aligned bounding box of all active voxels.
394  virtual Coord evalActiveVoxelDim() const = 0;
395 
396  /// Return the number of bytes of memory used by this grid.
397  virtual Index64 memUsage() const = 0;
398 
399  /// @brief Add metadata to this grid comprising the current values
400  /// of statistics like the active voxel count and bounding box.
401  /// @note This metadata is not automatically kept up-to-date with
402  /// changes to this grid.
403  void addStatsMetadata();
404  /// @brief Return a new MetaMap containing just the metadata that
405  /// was added to this grid with @vdblink::GridBase::addStatsMetadata()
406  /// addStatsMetadata@endlink.
407  /// @details If @vdblink::GridBase::addStatsMetadata() addStatsMetadata@endlink
408  /// was never called on this grid, return an empty MetaMap.
409  MetaMap::Ptr getStatsMetadata() const;
410 
411  /// @}
412 
413 
414  /// @name Transform
415  /// @{
416 
417  //@{
418  /// @brief Return a pointer to this grid's transform, which might be
419  /// shared with other grids.
420  math::Transform::Ptr transformPtr() { return mTransform; }
421  math::Transform::ConstPtr transformPtr() const { return mTransform; }
422  math::Transform::ConstPtr constTransformPtr() const { return mTransform; }
423  //@}
424  //@{
425  /// @brief Return a reference to this grid's transform, which might be
426  /// shared with other grids.
427  /// @note Calling @vdblink::GridBase::setTransform() setTransform@endlink
428  /// on this grid invalidates all references previously returned by this method.
429  math::Transform& transform() { return *mTransform; }
430  const math::Transform& transform() const { return *mTransform; }
431  const math::Transform& constTransform() const { return *mTransform; }
432  //@}
433 
434  /// @}
435 
436  /// @name Transform
437  /// @{
438 
439  /// @brief Associate the given transform with this grid, in place of
440  /// its existing transform.
441  /// @throw ValueError if the transform pointer is null
442  /// @note Invalidates all references previously returned by
443  /// @vdblink::GridBase::transform() transform@endlink
444  /// or @vdblink::GridBase::constTransform() constTransform@endlink.
445  void setTransform(math::Transform::Ptr);
446 
447  /// Return the size of this grid's voxels.
448  Vec3d voxelSize() const { return transform().voxelSize(); }
449  /// @brief Return the size of this grid's voxel at position (x, y, z).
450  /// @note Frustum and perspective transforms have position-dependent voxel size.
451  Vec3d voxelSize(const Vec3d& xyz) const { return transform().voxelSize(xyz); }
452  /// Return true if the voxels in world space are uniformly sized cubes
453  bool hasUniformVoxels() const { return mTransform->hasUniformScale(); }
454  /// Apply this grid's transform to the given coordinates.
455  Vec3d indexToWorld(const Vec3d& xyz) const { return transform().indexToWorld(xyz); }
456  /// Apply this grid's transform to the given coordinates.
457  Vec3d indexToWorld(const Coord& ijk) const { return transform().indexToWorld(ijk); }
458  /// Apply the inverse of this grid's transform to the given coordinates.
459  Vec3d worldToIndex(const Vec3d& xyz) const { return transform().worldToIndex(xyz); }
460 
461  /// @}
462 
463 
464  /// @name I/O
465  /// @{
466 
467  /// @brief Read the grid topology from a stream.
468  /// This will read only the grid structure, not the actual data buffers.
469  virtual void readTopology(std::istream&) = 0;
470  /// @brief Write the grid topology to a stream.
471  /// This will write only the grid structure, not the actual data buffers.
472  virtual void writeTopology(std::ostream&) const = 0;
473 
474  /// Read all data buffers for this grid.
475  virtual void readBuffers(std::istream&) = 0;
476 #if OPENVDB_ABI_VERSION_NUMBER >= 3
477  /// Read all of this grid's data buffers that intersect the given index-space bounding box.
478  virtual void readBuffers(std::istream&, const CoordBBox&) = 0;
479  /// @brief Read all of this grid's data buffers that are not yet resident in memory
480  /// (because delayed loading is in effect).
481  /// @details If this grid was read from a memory-mapped file, this operation
482  /// disconnects the grid from the file.
483  /// @sa io::File::open, io::MappedFile
484  virtual void readNonresidentBuffers() const = 0;
485 #endif
486  /// Write out all data buffers for this grid.
487  virtual void writeBuffers(std::ostream&) const = 0;
488 
489  /// Read in the transform for this grid.
490  void readTransform(std::istream& is) { transform().read(is); }
491  /// Write out the transform for this grid.
492  void writeTransform(std::ostream& os) const { transform().write(os); }
493 
494  /// Output a human-readable description of this grid.
495  virtual void print(std::ostream& = std::cout, int verboseLevel = 1) const = 0;
496 
497  /// @}
498 
499 
500 protected:
501  /// @brief Initialize with an identity linear transform.
502  GridBase(): mTransform(math::Transform::createLinearTransform()) {}
503 
504  /// @brief Deep copy another grid's metadata and transform.
505  GridBase(const GridBase& other): MetaMap(other), mTransform(other.mTransform->copy()) {}
506 
507 #if OPENVDB_ABI_VERSION_NUMBER <= 3
508  /// @brief Copy another grid's metadata but share its transform.
509  /// @deprecated ABI versions older than 4 are deprecated.
511  GridBase(const GridBase& other, ShallowCopy): MetaMap(other), mTransform(other.mTransform) {}
512 #else
513  /// @brief Copy another grid's metadata but share its transform.
514  GridBase(GridBase& other, ShallowCopy): MetaMap(other), mTransform(other.mTransform) {}
515 #endif
516 
517  /// Register a grid type along with a factory function.
518  static void registerGrid(const Name& type, GridFactory);
519  /// Remove a grid type from the registry.
520  static void unregisterGrid(const Name& type);
521 
522 
523 private:
524  math::Transform::Ptr mTransform;
525 }; // class GridBase
526 
527 
528 ////////////////////////////////////////
529 
530 
531 using GridPtrVec = std::vector<GridBase::Ptr>;
532 using GridPtrVecIter = GridPtrVec::iterator;
533 using GridPtrVecCIter = GridPtrVec::const_iterator;
535 
536 using GridCPtrVec = std::vector<GridBase::ConstPtr>;
537 using GridCPtrVecIter = GridCPtrVec::iterator;
538 using GridCPtrVecCIter = GridCPtrVec::const_iterator;
540 
541 using GridPtrSet = std::set<GridBase::Ptr>;
542 using GridPtrSetIter = GridPtrSet::iterator;
543 using GridPtrSetCIter = GridPtrSet::const_iterator;
545 
546 using GridCPtrSet = std::set<GridBase::ConstPtr>;
547 using GridCPtrSetIter = GridCPtrSet::iterator;
548 using GridCPtrSetCIter = GridCPtrSet::const_iterator;
550 
551 
552 /// @brief Predicate functor that returns @c true for grids that have a specified name
554 {
555  GridNamePred(const Name& _name): name(_name) {}
556  bool operator()(const GridBase::ConstPtr& g) const { return g && g->getName() == name; }
558 };
559 
560 /// Return the first grid in the given container whose name is @a name.
561 template<typename GridPtrContainerT>
562 inline typename GridPtrContainerT::value_type
563 findGridByName(const GridPtrContainerT& container, const Name& name)
564 {
565  using GridPtrT = typename GridPtrContainerT::value_type;
566  typename GridPtrContainerT::const_iterator it =
567  std::find_if(container.begin(), container.end(), GridNamePred(name));
568  return (it == container.end() ? GridPtrT() : *it);
569 }
570 
571 /// Return the first grid in the given map whose name is @a name.
572 template<typename KeyT, typename GridPtrT>
573 inline GridPtrT
574 findGridByName(const std::map<KeyT, GridPtrT>& container, const Name& name)
575 {
576  using GridPtrMapT = std::map<KeyT, GridPtrT>;
577  for (typename GridPtrMapT::const_iterator it = container.begin(), end = container.end();
578  it != end; ++it)
579  {
580  const GridPtrT& grid = it->second;
581  if (grid && grid->getName() == name) return grid;
582  }
583  return GridPtrT();
584 }
585 //@}
586 
587 
588 ////////////////////////////////////////
589 
590 
591 /// @brief Container class that associates a tree with a transform and metadata
592 template<typename _TreeType>
593 class Grid: public GridBase
594 {
595 public:
598 
599  using TreeType = _TreeType;
600  using TreePtrType = typename _TreeType::Ptr;
601  using ConstTreePtrType = typename _TreeType::ConstPtr;
602  using ValueType = typename _TreeType::ValueType;
603  using BuildType = typename _TreeType::BuildType;
604 
605  using ValueOnIter = typename _TreeType::ValueOnIter;
606  using ValueOnCIter = typename _TreeType::ValueOnCIter;
607  using ValueOffIter = typename _TreeType::ValueOffIter;
608  using ValueOffCIter = typename _TreeType::ValueOffCIter;
609  using ValueAllIter = typename _TreeType::ValueAllIter;
610  using ValueAllCIter = typename _TreeType::ValueAllCIter;
611 
616 
617  /// @brief ValueConverter<T>::Type is the type of a grid having the same
618  /// hierarchy as this grid but a different value type, T.
619  ///
620  /// For example, FloatGrid::ValueConverter<double>::Type is equivalent to DoubleGrid.
621  /// @note If the source grid type is a template argument, it might be necessary
622  /// to write "typename SourceGrid::template ValueConverter<T>::Type".
623  template<typename OtherValueType>
624  struct ValueConverter {
626  };
627 
628  /// Return a new grid with the given background value.
629  static Ptr create(const ValueType& background);
630  /// Return a new grid with background value zero.
631  static Ptr create();
632  /// @brief Return a new grid that contains the given tree.
633  /// @throw ValueError if the tree pointer is null
634  static Ptr create(TreePtrType);
635  /// @brief Return a new, empty grid with the same transform and metadata as the
636  /// given grid and with background value zero.
637  static Ptr create(const GridBase& other);
638 
639 
640  /// Construct a new grid with background value zero.
641  Grid();
642  /// Construct a new grid with the given background value.
643  explicit Grid(const ValueType& background);
644  /// @brief Construct a new grid that shares the given tree and associates with it
645  /// an identity linear transform.
646  /// @throw ValueError if the tree pointer is null
647  explicit Grid(TreePtrType);
648  /// Deep copy another grid's metadata, transform and tree.
649  Grid(const Grid&);
650  /// @brief Deep copy the metadata, transform and tree of another grid whose tree
651  /// configuration is the same as this grid's but whose value type is different.
652  /// Cast the other grid's values to this grid's value type.
653  /// @throw TypeError if the other grid's tree configuration doesn't match this grid's
654  /// or if this grid's ValueType is not constructible from the other grid's ValueType.
655  template<typename OtherTreeType>
656  explicit Grid(const Grid<OtherTreeType>&);
657 #if OPENVDB_ABI_VERSION_NUMBER <= 3
658  /// Deep copy another grid's metadata, but share its tree and transform.
659  /// @deprecated ABI versions older than 4 are deprecated.
661 #else
662  /// Deep copy another grid's metadata and transform, but share its tree.
663  Grid(Grid&, ShallowCopy);
664 #endif
665  /// @brief Deep copy another grid's metadata and transform, but construct a new tree
666  /// with background value zero.
667  explicit Grid(const GridBase&);
668 
669  ~Grid() override {}
670 
671  /// Disallow assignment, since it wouldn't be obvious whether the copy is deep or shallow.
672  Grid& operator=(const Grid&) = delete;
673 
674 
675 #if OPENVDB_ABI_VERSION_NUMBER <= 3
676  //@{
677  /// @brief Return a new grid of the same type as this grid whose metadata
678  /// is a deep copy of this grid's.
679  /// @details If @a treePolicy is @c CP_NEW, the new grid is given a new, empty tree,
680  /// and it shares its transform with this grid;
681  /// if @c CP_SHARE, the new grid shares this grid's tree and transform;
682  /// if @c CP_COPY, the new grid's tree and transform are deep copies of this grid's.
683  /// @deprecated ABI versions older than 4 are deprecated.
684  OPENVDB_DEPRECATED Ptr copy(CopyPolicy treePolicy = CP_SHARE) const;
685  OPENVDB_DEPRECATED GridBase::Ptr copyGrid(CopyPolicy treePolicy = CP_SHARE) const override;
686  //@}
687 #else
688  /// @name Copying
689  /// @{
690 
691  /// @brief Return a new grid of the same type as this grid whose metadata and
692  /// transform are deep copies of this grid's and whose tree is shared with this grid.
693  Ptr copy();
694  /// @brief Return a new grid of the same type as this grid whose metadata and
695  /// transform are deep copies of this grid's and whose tree is shared with this grid.
696  ConstPtr copy() const;
697  /// @brief Return a new grid of the same type as this grid whose metadata and
698  /// transform are deep copies of this grid's and whose tree is default-constructed.
699  Ptr copyWithNewTree() const;
700 
701  /// @brief Return a new grid of the same type as this grid whose metadata is a
702  /// deep copy of this grid's and whose tree and transform are shared with this grid.
703  GridBase::Ptr copyGrid() override;
704  /// @brief Return a new grid of the same type as this grid whose metadata is a
705  /// deep copy of this grid's and whose tree and transform are shared with this grid.
706  GridBase::ConstPtr copyGrid() const override;
707  /// @brief Return a new grid of the same type as this grid whose metadata and
708  /// transform are deep copies of this grid's and whose tree is default-constructed.
709  GridBase::Ptr copyGridWithNewTree() const override;
710  //@}
711 #endif
712  /// @name Copying
713  /// @{
714 
715  /// @brief Return a new grid whose metadata, transform and tree are deep copies of this grid's.
716  Ptr deepCopy() const { return Ptr(new Grid(*this)); }
717  /// @brief Return a new grid whose metadata, transform and tree are deep copies of this grid's.
718  GridBase::Ptr deepCopyGrid() const override { return this->deepCopy(); }
719 
720  //@}
721 
722 
723  /// Return the name of this grid's type.
724  Name type() const override { return this->gridType(); }
725  /// Return the name of this type of grid.
726  static Name gridType() { return TreeType::treeType(); }
727 
728  /// Return the name of the type of a voxel's value (e.g., "float" or "vec3d").
729  Name valueType() const override { return tree().valueType(); }
730 
731 
732  /// @name Voxel access
733  /// @{
734 
735  /// @brief Return this grid's background value.
736  /// @note Use tools::changeBackground to efficiently modify the background value.
737  const ValueType& background() const { return mTree->background(); }
738 
739  /// Return @c true if this grid contains only inactive background voxels.
740  bool empty() const override { return tree().empty(); }
741  /// Empty this grid, so that all voxels become inactive background voxels.
742  void clear() override { tree().clear(); }
743 
744  /// @brief Return an accessor that provides random read and write access
745  /// to this grid's voxels.
746  /// @details The accessor is safe in the sense that it is registered with this grid's tree.
748  /// @brief Return an unsafe accessor that provides random read and write access
749  /// to this grid's voxels.
750  /// @details The accessor is unsafe in the sense that it is not registered
751  /// with this grid's tree. In some rare cases this can give a performance advantage
752  /// over a registered accessor, but it is unsafe if the tree topology is modified.
753  /// @warning Only use this method if you're an expert and know the
754  /// risks of using an unregistered accessor (see tree/ValueAccessor.h)
756  /// Return an accessor that provides random read-only access to this grid's voxels.
758  /// Return an accessor that provides random read-only access to this grid's voxels.
760  /// @brief Return an unsafe accessor that provides random read-only access
761  /// to this grid's voxels.
762  /// @details The accessor is unsafe in the sense that it is not registered
763  /// with this grid's tree. In some rare cases this can give a performance advantage
764  /// over a registered accessor, but it is unsafe if the tree topology is modified.
765  /// @warning Only use this method if you're an expert and know the
766  /// risks of using an unregistered accessor (see tree/ValueAccessor.h)
768 
769  /// Return an iterator over all of this grid's active values (tile and voxel).
770  ValueOnIter beginValueOn() { return tree().beginValueOn(); }
771  /// Return an iterator over all of this grid's active values (tile and voxel).
772  ValueOnCIter beginValueOn() const { return tree().cbeginValueOn(); }
773  /// Return an iterator over all of this grid's active values (tile and voxel).
774  ValueOnCIter cbeginValueOn() const { return tree().cbeginValueOn(); }
775  /// Return an iterator over all of this grid's inactive values (tile and voxel).
776  ValueOffIter beginValueOff() { return tree().beginValueOff(); }
777  /// Return an iterator over all of this grid's inactive values (tile and voxel).
778  ValueOffCIter beginValueOff() const { return tree().cbeginValueOff(); }
779  /// Return an iterator over all of this grid's inactive values (tile and voxel).
780  ValueOffCIter cbeginValueOff() const { return tree().cbeginValueOff(); }
781  /// Return an iterator over all of this grid's values (tile and voxel).
782  ValueAllIter beginValueAll() { return tree().beginValueAll(); }
783  /// Return an iterator over all of this grid's values (tile and voxel).
784  ValueAllCIter beginValueAll() const { return tree().cbeginValueAll(); }
785  /// Return an iterator over all of this grid's values (tile and voxel).
786  ValueAllCIter cbeginValueAll() const { return tree().cbeginValueAll(); }
787 
788  /// @}
789 
790  /// @name Tools
791  /// @{
792 
793  /// @brief Set all voxels within a given axis-aligned box to a constant value.
794  /// @param bbox inclusive coordinates of opposite corners of an axis-aligned box
795  /// @param value the value to which to set voxels within the box
796  /// @param active if true, mark voxels within the box as active,
797  /// otherwise mark them as inactive
798  /// @note This operation generates a sparse, but not always optimally sparse,
799  /// representation of the filled box. Follow fill operations with a prune()
800  /// operation for optimal sparseness.
801  void sparseFill(const CoordBBox& bbox, const ValueType& value, bool active = true);
802  /// @brief Set all voxels within a given axis-aligned box to a constant value.
803  /// @param bbox inclusive coordinates of opposite corners of an axis-aligned box
804  /// @param value the value to which to set voxels within the box
805  /// @param active if true, mark voxels within the box as active,
806  /// otherwise mark them as inactive
807  /// @note This operation generates a sparse, but not always optimally sparse,
808  /// representation of the filled box. Follow fill operations with a prune()
809  /// operation for optimal sparseness.
810  void fill(const CoordBBox& bbox, const ValueType& value, bool active = true);
811 
812  /// @brief Set all voxels within a given axis-aligned box to a constant value
813  /// and ensure that those voxels are all represented at the leaf level.
814  /// @param bbox inclusive coordinates of opposite corners of an axis-aligned box.
815  /// @param value the value to which to set voxels within the box.
816  /// @param active if true, mark voxels within the box as active,
817  /// otherwise mark them as inactive.
818  void denseFill(const CoordBBox& bbox, const ValueType& value, bool active = true);
819 
820  /// Reduce the memory footprint of this grid by increasing its sparseness.
821  void pruneGrid(float tolerance = 0.0) override;
822 
823 #if OPENVDB_ABI_VERSION_NUMBER >= 3
824  /// @brief Clip this grid to the given index-space bounding box.
825  /// @details Voxels that lie outside the bounding box are set to the background.
826  /// @warning Clipping a level set will likely produce a grid that is
827  /// no longer a valid level set.
828  void clip(const CoordBBox&) override;
829 #endif
830 
831  /// @brief Efficiently merge another grid into this grid using one of several schemes.
832  /// @details This operation is primarily intended to combine grids that are mostly
833  /// non-overlapping (for example, intermediate grids from computations that are
834  /// parallelized across disjoint regions of space).
835  /// @warning This operation always empties the other grid.
837 
838  /// @brief Union this grid's set of active values with the active values
839  /// of the other grid, whose value type may be different.
840  /// @details The resulting state of a value is active if the corresponding value
841  /// was already active OR if it is active in the other grid. Also, a resulting
842  /// value maps to a voxel if the corresponding value already mapped to a voxel
843  /// OR if it is a voxel in the other grid. Thus, a resulting value can only
844  /// map to a tile if the corresponding value already mapped to a tile
845  /// AND if it is a tile value in the other grid.
846  ///
847  /// @note This operation modifies only active states, not values.
848  /// Specifically, active tiles and voxels in this grid are not changed, and
849  /// tiles or voxels that were inactive in this grid but active in the other grid
850  /// are marked as active in this grid but left with their original values.
851  template<typename OtherTreeType>
852  void topologyUnion(const Grid<OtherTreeType>& other);
853 
854  /// @brief Intersect this grid's set of active values with the active values
855  /// of the other grid, whose value type may be different.
856  /// @details The resulting state of a value is active only if the corresponding
857  /// value was already active AND if it is active in the other tree. Also, a
858  /// resulting value maps to a voxel if the corresponding value
859  /// already mapped to an active voxel in either of the two grids
860  /// and it maps to an active tile or voxel in the other grid.
861  ///
862  /// @note This operation can delete branches of this grid that overlap with
863  /// inactive tiles in the other grid. Also, because it can deactivate voxels,
864  /// it can create leaf nodes with no active values. Thus, it is recommended
865  /// to prune this grid after calling this method.
866  template<typename OtherTreeType>
867  void topologyIntersection(const Grid<OtherTreeType>& other);
868 
869  /// @brief Difference this grid's set of active values with the active values
870  /// of the other grid, whose value type may be different.
871  /// @details After this method is called, voxels in this grid will be active
872  /// only if they were active to begin with and if the corresponding voxels
873  /// in the other grid were inactive.
874  ///
875  /// @note This operation can delete branches of this grid that overlap with
876  /// active tiles in the other grid. Also, because it can deactivate voxels,
877  /// it can create leaf nodes with no active values. Thus, it is recommended
878  /// to prune this grid after calling this method.
879  template<typename OtherTreeType>
880  void topologyDifference(const Grid<OtherTreeType>& other);
881 
882  /// @}
883 
884  /// @name Statistics
885  /// @{
886 
887  /// Return the number of active voxels.
888  Index64 activeVoxelCount() const override { return tree().activeVoxelCount(); }
889  /// Return the axis-aligned bounding box of all active voxels.
890  CoordBBox evalActiveVoxelBoundingBox() const override;
891  /// Return the dimensions of the axis-aligned bounding box of all active voxels.
892  Coord evalActiveVoxelDim() const override;
893  /// Return the minimum and maximum active values in this grid.
894  void evalMinMax(ValueType& minVal, ValueType& maxVal) const;
895 
896  /// Return the number of bytes of memory used by this grid.
897  /// @todo Add transform().memUsage()
898  Index64 memUsage() const override { return tree().memUsage(); }
899 
900  /// @}
901 
902 
903  /// @name Tree
904  /// @{
905 
906  //@{
907  /// @brief Return a pointer to this grid's tree, which might be
908  /// shared with other grids. The pointer is guaranteed to be non-null.
909  TreePtrType treePtr() { return mTree; }
910  ConstTreePtrType treePtr() const { return mTree; }
911  ConstTreePtrType constTreePtr() const { return mTree; }
912  TreeBase::ConstPtr constBaseTreePtr() const override { return mTree; }
913  //@}
914  //@{
915  /// @brief Return a reference to this grid's tree, which might be
916  /// shared with other grids.
917  /// @note Calling setTree() on this grid invalidates all references
918  /// previously returned by this method.
919  TreeType& tree() { return *mTree; }
920  const TreeType& tree() const { return *mTree; }
921  const TreeType& constTree() const { return *mTree; }
922  //@}
923 
924  /// @}
925 
926  /// @name Tree
927  /// @{
928 
929  /// @brief Associate the given tree with this grid, in place of its existing tree.
930  /// @throw ValueError if the tree pointer is null
931  /// @throw TypeError if the tree is not of type TreeType
932  /// @note Invalidates all references previously returned by baseTree(),
933  /// constBaseTree(), tree() or constTree().
934  void setTree(TreeBase::Ptr) override;
935 
936  /// @brief Associate a new, empty tree with this grid, in place of its existing tree.
937  /// @note The new tree has the same background value as the existing tree.
938  void newTree() override;
939 
940  /// @}
941 
942 
943  /// @name I/O
944  /// @{
945 
946  /// @brief Read the grid topology from a stream.
947  /// This will read only the grid structure, not the actual data buffers.
948  void readTopology(std::istream&) override;
949  /// @brief Write the grid topology to a stream.
950  /// This will write only the grid structure, not the actual data buffers.
951  void writeTopology(std::ostream&) const override;
952 
953  /// Read all data buffers for this grid.
954  void readBuffers(std::istream&) override;
955 #if OPENVDB_ABI_VERSION_NUMBER >= 3
956  /// Read all of this grid's data buffers that intersect the given index-space bounding box.
957  void readBuffers(std::istream&, const CoordBBox&) override;
958  /// @brief Read all of this grid's data buffers that are not yet resident in memory
959  /// (because delayed loading is in effect).
960  /// @details If this grid was read from a memory-mapped file, this operation
961  /// disconnects the grid from the file.
962  /// @sa io::File::open, io::MappedFile
963  void readNonresidentBuffers() const override;
964 #endif
965  /// Write out all data buffers for this grid.
966  void writeBuffers(std::ostream&) const override;
967 
968  /// Output a human-readable description of this grid.
969  void print(std::ostream& = std::cout, int verboseLevel = 1) const override;
970 
971  /// @}
972 
973  /// @brief Return @c true if grids of this type require multiple I/O passes
974  /// to read and write data buffers.
975  /// @sa HasMultiPassIO
976  static inline bool hasMultiPassIO();
977 
978 
979  /// @name Registry
980  /// @{
981 
982  /// Return @c true if this grid type is registered.
984  /// Register this grid type along with a factory function.
985  static void registerGrid()
986  {
987  GridBase::registerGrid(Grid::gridType(), Grid::factory);
989  OPENVDB_LOG_WARN("delayed loading of grids of type " << Grid::gridType()
990  << " might not be threadsafe on this platform");
991  }
992  }
993  /// Remove this grid type from the registry.
995 
996  /// @}
997 
998 
999 private:
1000  /// Helper function for use with registerGrid()
1001  static GridBase::Ptr factory() { return Grid::create(); }
1002 
1003  TreePtrType mTree;
1004 }; // class Grid
1005 
1006 
1007 ////////////////////////////////////////
1008 
1009 
1010 /// @brief Cast a generic grid pointer to a pointer to a grid of a concrete class.
1011 ///
1012 /// Return a null pointer if the input pointer is null or if it
1013 /// points to a grid that is not of type @c GridType.
1014 ///
1015 /// @note Calling gridPtrCast<GridType>(grid) is equivalent to calling
1016 /// GridBase::grid<GridType>(grid).
1017 template<typename GridType>
1018 inline typename GridType::Ptr
1020 {
1021  return GridBase::grid<GridType>(grid);
1022 }
1023 
1024 
1025 /// @brief Cast a generic const grid pointer to a const pointer to a grid
1026 /// of a concrete class.
1027 ///
1028 /// Return a null pointer if the input pointer is null or if it
1029 /// points to a grid that is not of type @c GridType.
1030 ///
1031 /// @note Calling gridConstPtrCast<GridType>(grid) is equivalent to calling
1032 /// GridBase::constGrid<GridType>(grid).
1033 template<typename GridType>
1034 inline typename GridType::ConstPtr
1036 {
1037  return GridBase::constGrid<GridType>(grid);
1038 }
1039 
1040 
1041 ////////////////////////////////////////
1042 
1043 
1044 /// @{
1045 /// @brief Return a pointer to a deep copy of the given grid, provided that
1046 /// the grid's concrete type is @c GridType.
1047 ///
1048 /// Return a null pointer if the input pointer is null or if it
1049 /// points to a grid that is not of type @c GridType.
1050 template<typename GridType>
1051 inline typename GridType::Ptr
1053 {
1054  if (!grid || !grid->isType<GridType>()) return typename GridType::Ptr();
1055  return gridPtrCast<GridType>(grid->deepCopyGrid());
1056 }
1057 
1058 
1059 template<typename GridType>
1060 inline typename GridType::Ptr
1062 {
1063  if (!grid.isType<GridType>()) return typename GridType::Ptr();
1064  return gridPtrCast<GridType>(grid.deepCopyGrid());
1065 }
1066 /// @}
1067 
1068 
1069 ////////////////////////////////////////
1070 
1071 
1072 //@{
1073 /// @brief This adapter allows code that is templated on a Tree type to
1074 /// accept either a Tree type or a Grid type.
1075 template<typename _TreeType>
1077 {
1078  using TreeType = _TreeType;
1080  using TreePtrType = typename TreeType::Ptr;
1081  using ConstTreePtrType = typename TreeType::ConstPtr;
1082  using NonConstTreePtrType = typename NonConstTreeType::Ptr;
1085  using GridPtrType = typename GridType::Ptr;
1088  using ValueType = typename TreeType::ValueType;
1092 
1093  static TreeType& tree(TreeType& t) { return t; }
1094  static TreeType& tree(GridType& g) { return g.tree(); }
1095  static const TreeType& tree(const TreeType& t) { return t; }
1096  static const TreeType& tree(const GridType& g) { return g.tree(); }
1097  static const TreeType& constTree(TreeType& t) { return t; }
1098  static const TreeType& constTree(GridType& g) { return g.constTree(); }
1099  static const TreeType& constTree(const TreeType& t) { return t; }
1100  static const TreeType& constTree(const GridType& g) { return g.constTree(); }
1101 };
1102 
1103 
1104 /// Partial specialization for Grid types
1105 template<typename _TreeType>
1106 struct TreeAdapter<Grid<_TreeType> >
1107 {
1108  using TreeType = _TreeType;
1110  using TreePtrType = typename TreeType::Ptr;
1111  using ConstTreePtrType = typename TreeType::ConstPtr;
1112  using NonConstTreePtrType = typename NonConstTreeType::Ptr;
1115  using GridPtrType = typename GridType::Ptr;
1118  using ValueType = typename TreeType::ValueType;
1122 
1123  static TreeType& tree(TreeType& t) { return t; }
1124  static TreeType& tree(GridType& g) { return g.tree(); }
1125  static const TreeType& tree(const TreeType& t) { return t; }
1126  static const TreeType& tree(const GridType& g) { return g.tree(); }
1127  static const TreeType& constTree(TreeType& t) { return t; }
1128  static const TreeType& constTree(GridType& g) { return g.constTree(); }
1129  static const TreeType& constTree(const TreeType& t) { return t; }
1130  static const TreeType& constTree(const GridType& g) { return g.constTree(); }
1131 };
1132 
1133 /// Partial specialization for ValueAccessor types
1134 template<typename _TreeType>
1135 struct TreeAdapter<tree::ValueAccessor<_TreeType> >
1136 {
1137  using TreeType = _TreeType;
1139  using TreePtrType = typename TreeType::Ptr;
1140  using ConstTreePtrType = typename TreeType::ConstPtr;
1141  using NonConstTreePtrType = typename NonConstTreeType::Ptr;
1144  using GridPtrType = typename GridType::Ptr;
1147  using ValueType = typename TreeType::ValueType;
1151 
1152  static TreeType& tree(TreeType& t) { return t; }
1153  static TreeType& tree(GridType& g) { return g.tree(); }
1154  static TreeType& tree(AccessorType& a) { return a.tree(); }
1155  static const TreeType& tree(const TreeType& t) { return t; }
1156  static const TreeType& tree(const GridType& g) { return g.tree(); }
1157  static const TreeType& tree(const AccessorType& a) { return a.tree(); }
1158  static const TreeType& constTree(TreeType& t) { return t; }
1159  static const TreeType& constTree(GridType& g) { return g.constTree(); }
1160  static const TreeType& constTree(const TreeType& t) { return t; }
1161  static const TreeType& constTree(const GridType& g) { return g.constTree(); }
1162 };
1163 
1164 //@}
1165 
1166 
1167 ////////////////////////////////////////
1168 
1169 
1170 /// @brief Metafunction that specifies whether a given leaf node, tree, or grid type
1171 /// requires multiple passes to read and write voxel data
1172 /// @details Multi-pass I/O allows one to optimize the data layout of leaf nodes
1173 /// for certain access patterns during delayed loading.
1174 /// @sa io::MultiPass
1175 template<typename LeafNodeType>
1178 };
1179 
1180 // Partial specialization for Tree types
1181 template<typename RootNodeType>
1182 struct HasMultiPassIO<tree::Tree<RootNodeType>> {
1183  // A tree is multi-pass if its (root node's) leaf node type is multi-pass.
1185 };
1186 
1187 // Partial specialization for Grid types
1188 template<typename TreeType>
1189 struct HasMultiPassIO<Grid<TreeType>> {
1190  // A grid is multi-pass if its tree's leaf node type is multi-pass.
1192 };
1193 
1194 
1195 ////////////////////////////////////////
1196 
1197 
1198 template<typename GridType>
1199 inline typename GridType::Ptr
1201 {
1202  // The string comparison on type names is slower than a dynamic pointer cast, but
1203  // it is safer when pointers cross DSO boundaries, as they do in many Houdini nodes.
1204  if (grid && grid->type() == GridType::gridType()) {
1205  return StaticPtrCast<GridType>(grid);
1206  }
1207  return typename GridType::Ptr();
1208 }
1209 
1210 
1211 template<typename GridType>
1212 inline typename GridType::ConstPtr
1214 {
1215  return ConstPtrCast<const GridType>(
1216  GridBase::grid<GridType>(ConstPtrCast<GridBase>(grid)));
1217 }
1218 
1219 
1220 template<typename GridType>
1221 inline typename GridType::ConstPtr
1223 {
1224  return ConstPtrCast<const GridType>(GridBase::grid<GridType>(grid));
1225 }
1226 
1227 
1228 template<typename GridType>
1229 inline typename GridType::ConstPtr
1231 {
1232  return ConstPtrCast<const GridType>(
1233  GridBase::grid<GridType>(ConstPtrCast<GridBase>(grid)));
1234 }
1235 
1236 
1237 inline TreeBase::Ptr
1239 {
1240  return ConstPtrCast<TreeBase>(this->constBaseTreePtr());
1241 }
1242 
1243 
1244 inline void
1246 {
1247  if (!xform) OPENVDB_THROW(ValueError, "Transform pointer is null");
1248  mTransform = xform;
1249 }
1250 
1251 
1252 ////////////////////////////////////////
1253 
1254 
1255 template<typename TreeT>
1256 inline Grid<TreeT>::Grid(): mTree(new TreeType)
1257 {
1258 }
1259 
1260 
1261 template<typename TreeT>
1262 inline Grid<TreeT>::Grid(const ValueType &background): mTree(new TreeType(background))
1263 {
1264 }
1265 
1266 
1267 template<typename TreeT>
1268 inline Grid<TreeT>::Grid(TreePtrType tree): mTree(tree)
1269 {
1270  if (!tree) OPENVDB_THROW(ValueError, "Tree pointer is null");
1271 }
1272 
1273 
1274 template<typename TreeT>
1275 inline Grid<TreeT>::Grid(const Grid& other):
1276  GridBase(other),
1277  mTree(StaticPtrCast<TreeType>(other.mTree->copy()))
1278 {
1279 }
1280 
1281 
1282 template<typename TreeT>
1283 template<typename OtherTreeType>
1285  GridBase(other),
1286  mTree(new TreeType(other.constTree()))
1287 {
1288 }
1289 
1290 
1291 #if OPENVDB_ABI_VERSION_NUMBER <= 3
1292 template<typename TreeT>
1293 inline Grid<TreeT>::Grid(const Grid& other, ShallowCopy):
1294  GridBase(other, ShallowCopy()),
1295  mTree(other.mTree)
1296 {
1297 }
1298 #else
1299 template<typename TreeT>
1301  GridBase(other),
1302  mTree(other.mTree)
1303 {
1304 }
1305 #endif
1306 
1307 
1308 template<typename TreeT>
1309 inline Grid<TreeT>::Grid(const GridBase& other):
1310  GridBase(other),
1311  mTree(new TreeType)
1312 {
1313 }
1314 
1315 
1316 //static
1317 template<typename TreeT>
1318 inline typename Grid<TreeT>::Ptr
1320 {
1321  return Grid::create(zeroVal<ValueType>());
1322 }
1323 
1324 
1325 //static
1326 template<typename TreeT>
1327 inline typename Grid<TreeT>::Ptr
1328 Grid<TreeT>::create(const ValueType& background)
1329 {
1330  return Ptr(new Grid(background));
1331 }
1332 
1333 
1334 //static
1335 template<typename TreeT>
1336 inline typename Grid<TreeT>::Ptr
1338 {
1339  return Ptr(new Grid(tree));
1340 }
1341 
1342 
1343 //static
1344 template<typename TreeT>
1345 inline typename Grid<TreeT>::Ptr
1347 {
1348  return Ptr(new Grid(other));
1349 }
1350 
1351 
1352 ////////////////////////////////////////
1353 
1354 
1355 #if OPENVDB_ABI_VERSION_NUMBER <= 3
1356 
1357 template<typename TreeT>
1358 inline typename Grid<TreeT>::Ptr
1359 Grid<TreeT>::copy(CopyPolicy treePolicy) const
1360 {
1361  Ptr ret;
1362  switch (treePolicy) {
1363  case CP_NEW:
1364  ret.reset(new Grid(*this, ShallowCopy()));
1365  ret->newTree();
1366  break;
1367  case CP_COPY:
1368  ret.reset(new Grid(*this));
1369  break;
1370  case CP_SHARE:
1371  ret.reset(new Grid(*this, ShallowCopy()));
1372  break;
1373  }
1374  return ret;
1375 }
1376 
1377 
1378 template<typename TreeT>
1379 inline GridBase::Ptr
1380 Grid<TreeT>::copyGrid(CopyPolicy treePolicy) const
1381 {
1382  return this->copy(treePolicy);
1383 }
1384 
1385 #else // if OPENVDB_ABI_VERSION_NUMBER > 3
1386 
1387 template<typename TreeT>
1388 inline typename Grid<TreeT>::ConstPtr
1390 {
1391  return ConstPtr{new Grid{*const_cast<Grid*>(this), ShallowCopy{}}};
1392 }
1393 
1394 template<typename TreeT>
1395 inline typename Grid<TreeT>::Ptr
1397 {
1398  return Ptr{new Grid{*this, ShallowCopy{}}};
1399 }
1400 
1401 
1402 template<typename TreeT>
1403 inline typename Grid<TreeT>::Ptr
1405 {
1406  Ptr result{new Grid{*const_cast<Grid*>(this), ShallowCopy{}}};
1407  result->newTree();
1408  return result;
1409 }
1410 
1411 
1412 template<typename TreeT>
1413 inline GridBase::Ptr
1415 {
1416  return this->copy();
1417 }
1418 
1419 template<typename TreeT>
1420 inline GridBase::ConstPtr
1422 {
1423  return this->copy();
1424 }
1425 
1426 
1427 template<typename TreeT>
1428 inline GridBase::Ptr
1430 {
1431  return this->copyWithNewTree();
1432 }
1433 
1434 #endif
1435 
1436 
1437 ////////////////////////////////////////
1438 
1439 
1440 template<typename TreeT>
1441 inline void
1443 {
1444  if (!tree) OPENVDB_THROW(ValueError, "Tree pointer is null");
1445  if (tree->type() != TreeType::treeType()) {
1446  OPENVDB_THROW(TypeError, "Cannot assign a tree of type "
1447  + tree->type() + " to a grid of type " + this->type());
1448  }
1449  mTree = StaticPtrCast<TreeType>(tree);
1450 }
1451 
1452 
1453 template<typename TreeT>
1454 inline void
1456 {
1457  mTree.reset(new TreeType(this->background()));
1458 }
1459 
1460 
1461 ////////////////////////////////////////
1462 
1463 
1464 template<typename TreeT>
1465 inline void
1466 Grid<TreeT>::sparseFill(const CoordBBox& bbox, const ValueType& value, bool active)
1467 {
1468  tree().sparseFill(bbox, value, active);
1469 }
1470 
1471 
1472 template<typename TreeT>
1473 inline void
1474 Grid<TreeT>::fill(const CoordBBox& bbox, const ValueType& value, bool active)
1475 {
1476  this->sparseFill(bbox, value, active);
1477 }
1478 
1479 template<typename TreeT>
1480 inline void
1481 Grid<TreeT>::denseFill(const CoordBBox& bbox, const ValueType& value, bool active)
1482 {
1483  tree().denseFill(bbox, value, active);
1484 }
1485 
1486 template<typename TreeT>
1487 inline void
1488 Grid<TreeT>::pruneGrid(float tolerance)
1489 {
1491  const auto value = zeroVal<ValueType>() + tolerance;
1493  this->tree().prune(static_cast<ValueType>(value));
1494 }
1495 
1496 #if OPENVDB_ABI_VERSION_NUMBER >= 3
1497 template<typename TreeT>
1498 inline void
1499 Grid<TreeT>::clip(const CoordBBox& bbox)
1500 {
1501  tree().clip(bbox);
1502 }
1503 #endif
1504 
1505 
1506 template<typename TreeT>
1507 inline void
1509 {
1510  tree().merge(other.tree(), policy);
1511 }
1512 
1513 
1514 template<typename TreeT>
1515 template<typename OtherTreeType>
1516 inline void
1518 {
1519  tree().topologyUnion(other.tree());
1520 }
1521 
1522 
1523 template<typename TreeT>
1524 template<typename OtherTreeType>
1525 inline void
1527 {
1528  tree().topologyIntersection(other.tree());
1529 }
1530 
1531 
1532 template<typename TreeT>
1533 template<typename OtherTreeType>
1534 inline void
1536 {
1537  tree().topologyDifference(other.tree());
1538 }
1539 
1540 
1541 ////////////////////////////////////////
1542 
1543 
1544 template<typename TreeT>
1545 inline void
1547 {
1548  tree().evalMinMax(minVal, maxVal);
1549 }
1550 
1551 
1552 template<typename TreeT>
1553 inline CoordBBox
1555 {
1556  CoordBBox bbox;
1557  tree().evalActiveVoxelBoundingBox(bbox);
1558  return bbox;
1559 }
1560 
1561 
1562 template<typename TreeT>
1563 inline Coord
1565 {
1566  Coord dim;
1567  const bool nonempty = tree().evalActiveVoxelDim(dim);
1568  return (nonempty ? dim : Coord());
1569 }
1570 
1571 
1572 ////////////////////////////////////////
1573 
1574 
1575 /// @internal Consider using the stream tagging mechanism (see io::Archive)
1576 /// to specify the float precision, but note that the setting is per-grid.
1577 
1578 template<typename TreeT>
1579 inline void
1580 Grid<TreeT>::readTopology(std::istream& is)
1581 {
1582  tree().readTopology(is, saveFloatAsHalf());
1583 }
1584 
1585 
1586 template<typename TreeT>
1587 inline void
1588 Grid<TreeT>::writeTopology(std::ostream& os) const
1589 {
1590  tree().writeTopology(os, saveFloatAsHalf());
1591 }
1592 
1593 
1594 template<typename TreeT>
1595 inline void
1596 Grid<TreeT>::readBuffers(std::istream& is)
1597 {
1598  if (!hasMultiPassIO() || (io::getFormatVersion(is) < OPENVDB_FILE_VERSION_MULTIPASS_IO)) {
1599  tree().readBuffers(is, saveFloatAsHalf());
1600  } else {
1601  uint16_t numPasses = 1;
1602  is.read(reinterpret_cast<char*>(&numPasses), sizeof(uint16_t));
1604  assert(bool(meta));
1605  for (uint16_t passIndex = 0; passIndex < numPasses; ++passIndex) {
1606  uint32_t pass = (uint32_t(numPasses) << 16) | uint32_t(passIndex);
1607  meta->setPass(pass);
1608  tree().readBuffers(is, saveFloatAsHalf());
1609  }
1610  }
1611 }
1612 
1613 
1614 #if OPENVDB_ABI_VERSION_NUMBER >= 3
1615 
1616 /// @todo Refactor this and the readBuffers() above
1617 /// once support for ABI 2 compatibility is dropped.
1618 template<typename TreeT>
1619 inline void
1620 Grid<TreeT>::readBuffers(std::istream& is, const CoordBBox& bbox)
1621 {
1622  if (!hasMultiPassIO() || (io::getFormatVersion(is) < OPENVDB_FILE_VERSION_MULTIPASS_IO)) {
1623  tree().readBuffers(is, bbox, saveFloatAsHalf());
1624  } else {
1625  uint16_t numPasses = 1;
1626  is.read(reinterpret_cast<char*>(&numPasses), sizeof(uint16_t));
1628  assert(bool(meta));
1629  for (uint16_t passIndex = 0; passIndex < numPasses; ++passIndex) {
1630  uint32_t pass = (uint32_t(numPasses) << 16) | uint32_t(passIndex);
1631  meta->setPass(pass);
1632  tree().readBuffers(is, saveFloatAsHalf());
1633  }
1634  // Cannot clip inside readBuffers() when using multiple passes,
1635  // so instead clip afterwards.
1636  tree().clip(bbox);
1637  }
1638 }
1639 
1640 
1641 template<typename TreeT>
1642 inline void
1644 {
1645  tree().readNonresidentBuffers();
1646 }
1647 
1648 #endif
1649 
1650 
1651 template<typename TreeT>
1652 inline void
1653 Grid<TreeT>::writeBuffers(std::ostream& os) const
1654 {
1655  if (!hasMultiPassIO()) {
1656  tree().writeBuffers(os, saveFloatAsHalf());
1657  } else {
1658  // Determine how many leaf buffer passes are required for this grid
1660  assert(bool(meta));
1661  uint16_t numPasses = 1;
1662  meta->setCountingPasses(true);
1663  meta->setPass(0);
1664  tree().writeBuffers(os, saveFloatAsHalf());
1665  numPasses = static_cast<uint16_t>(meta->pass());
1666  os.write(reinterpret_cast<const char*>(&numPasses), sizeof(uint16_t));
1667  meta->setCountingPasses(false);
1668 
1669  // Save out the data blocks of the grid.
1670  for (uint16_t passIndex = 0; passIndex < numPasses; ++passIndex) {
1671  uint32_t pass = (uint32_t(numPasses) << 16) | uint32_t(passIndex);
1672  meta->setPass(pass);
1673  tree().writeBuffers(os, saveFloatAsHalf());
1674  }
1675  }
1676 }
1677 
1678 
1679 //static
1680 template<typename TreeT>
1681 inline bool
1683 {
1685 }
1686 
1687 
1688 template<typename TreeT>
1689 inline void
1690 Grid<TreeT>::print(std::ostream& os, int verboseLevel) const
1691 {
1692  tree().print(os, verboseLevel);
1693 
1694  if (metaCount() > 0) {
1695  os << "Additional metadata:" << std::endl;
1696  for (ConstMetaIterator it = beginMeta(), end = endMeta(); it != end; ++it) {
1697  os << " " << it->first;
1698  if (it->second) {
1699  const std::string value = it->second->str();
1700  if (!value.empty()) os << ": " << value;
1701  }
1702  os << "\n";
1703  }
1704  }
1705 
1706  os << "Transform:" << std::endl;
1707  transform().print(os, /*indent=*/" ");
1708  os << std::endl;
1709 }
1710 
1711 
1712 ////////////////////////////////////////
1713 
1714 
1715 template<typename GridType>
1716 inline typename GridType::Ptr
1717 createGrid(const typename GridType::ValueType& background)
1718 {
1719  return GridType::create(background);
1720 }
1721 
1722 
1723 template<typename GridType>
1724 inline typename GridType::Ptr
1726 {
1727  return GridType::create();
1728 }
1729 
1730 
1731 template<typename TreePtrType>
1732 inline typename Grid<typename TreePtrType::element_type>::Ptr
1733 createGrid(TreePtrType tree)
1734 {
1735  using TreeType = typename TreePtrType::element_type;
1736  return Grid<TreeType>::create(tree);
1737 }
1738 
1739 
1740 template<typename GridType>
1741 typename GridType::Ptr
1742 createLevelSet(Real voxelSize, Real halfWidth)
1743 {
1744  using ValueType = typename GridType::ValueType;
1745 
1746  // GridType::ValueType is required to be a floating-point scalar.
1748  "level-set grids must be floating-point-valued");
1749 
1750  typename GridType::Ptr grid = GridType::create(
1751  /*background=*/static_cast<ValueType>(voxelSize * halfWidth));
1752  grid->setTransform(math::Transform::createLinearTransform(voxelSize));
1753  grid->setGridClass(GRID_LEVEL_SET);
1754  return grid;
1755 }
1756 
1757 
1758 ////////////////////////////////////////
1759 
1760 
1761 namespace internal {
1762 
1763 /// @private
1764 template<typename OpT, typename GridBaseT, typename T, typename ...Ts>
1765 struct GridApplyImpl { static bool apply(GridBaseT&, OpT&) { return false; } };
1766 
1767 // Partial specialization for (nonempty) TypeLists
1768 /// @private
1769 template<typename OpT, typename GridBaseT, typename GridT, typename ...GridTs>
1770 struct GridApplyImpl<OpT, GridBaseT, TypeList<GridT, GridTs...>>
1771 {
1772  static bool apply(GridBaseT& grid, OpT& op)
1773  {
1774  if (grid.template isType<GridT>()) {
1775  op(static_cast<typename CopyConstness<GridBaseT, GridT>::Type&>(grid));
1776  return true;
1777  }
1778  return GridApplyImpl<OpT, GridBaseT, TypeList<GridTs...>>::apply(grid, op);
1779  }
1780 };
1781 
1782 } // namespace internal
1783 
1784 
1785 template<typename GridTypeListT, typename OpT>
1786 inline bool
1787 GridBase::apply(OpT& op) const
1788 {
1789  return internal::GridApplyImpl<OpT, const GridBase, GridTypeListT>::apply(*this, op);
1790 }
1791 
1792 template<typename GridTypeListT, typename OpT>
1793 inline bool
1795 {
1796  return internal::GridApplyImpl<OpT, GridBase, GridTypeListT>::apply(*this, op);
1797 }
1798 
1799 template<typename GridTypeListT, typename OpT>
1800 inline bool
1801 GridBase::apply(const OpT& op) const
1802 {
1803  return internal::GridApplyImpl<const OpT, const GridBase, GridTypeListT>::apply(*this, op);
1804 }
1805 
1806 template<typename GridTypeListT, typename OpT>
1807 inline bool
1808 GridBase::apply(const OpT& op)
1809 {
1810  return internal::GridApplyImpl<const OpT, GridBase, GridTypeListT>::apply(*this, op);
1811 }
1812 
1813 } // namespace OPENVDB_VERSION_NAME
1814 } // namespace openvdb
1815 
1816 #endif // OPENVDB_GRID_HAS_BEEN_INCLUDED
1817 
1818 // Copyright (c) DreamWorks Animation LLC
1819 // All rights reserved. This software is distributed under the
1820 // Mozilla Public License 2.0 ( http://www.mozilla.org/MPL/2.0/ )
SharedPtr< T > StaticPtrCast(const SharedPtr< U > &ptr)
Return a new shared pointer that points to the same object as the given pointer after a static_cast...
Definition: Types.h:171
typename _TreeType::ValueType ValueType
Definition: Grid.h:602
static GridType::ConstPtr constGrid(const GridBase::Ptr &)
Return the result of downcasting a GridBase pointer to a Grid pointer of the specified type...
Definition: Grid.h:1222
#define OPENVDB_NO_TYPE_CONVERSION_WARNING_END
Definition: Platform.h:224
typename tree::ValueAccessor< const TreeType > ConstAccessorType
Definition: Grid.h:1120
typename tree::ValueAccessor< TreeType > AccessorType
Definition: Grid.h:1119
static const TreeType & tree(const TreeType &t)
Definition: Grid.h:1095
ConstUnsafeAccessor getConstUnsafeAccessor() const
Return an unsafe accessor that provides random read-only access to this grid's voxels.
Definition: Grid.h:767
Definition: ImfName.h:53
GridPtrSet::iterator GridPtrSetIter
Definition: Grid.h:542
OPENVDB_API void setGridClass(std::ios_base &, uint32_t)
Associate with the given stream the class (GRID_LEVEL_SET, GRID_UNKNOWN, etc.) of the grid currently ...
GridType::Ptr createGrid(const typename GridType::ValueType &background)
Create a new grid of type GridType with a given background value.
Definition: Grid.h:1717
#define OPENVDB_LOG_WARN(mesg)
Definition: logging.h:301
Vec3d indexToWorld(const Coord &ijk) const
Apply this grid's transform to the given coordinates.
Definition: Grid.h:457
GLuint const GLchar * name
Definition: glew.h:1814
Vec3d worldToIndex(const Vec3d &xyz) const
Apply the inverse of this grid's transform to the given coordinates.
Definition: Grid.h:459
SharedPtr< GridBase > Ptr
Definition: Grid.h:107
Tag dispatch class that distinguishes shallow copy constructors from deep copy constructors.
Definition: Types.h:747
hboost::math::policies::policy< hboost::math::policies::domain_error< hboost::math::policies::ignore_error >, hboost::math::policies::pole_error< hboost::math::policies::ignore_error >, hboost::math::policies::overflow_error< hboost::math::policies::ignore_error >, hboost::math::policies::underflow_error< hboost::math::policies::ignore_error >, hboost::math::policies::denorm_error< hboost::math::policies::ignore_error >, hboost::math::policies::rounding_error< hboost::math::policies::ignore_error >, hboost::math::policies::evaluation_error< hboost::math::policies::ignore_error >, hboost::math::policies::indeterminate_result_error< hboost::math::policies::ignore_error > > policy
Definition: SYS_MathCbrt.h:35
void writeTransform(std::ostream &os) const
Write out the transform for this grid.
Definition: Grid.h:492
SharedPtr< const Grid > ConstPtr
Definition: Grid.h:597
ValueAllIter beginValueAll()
Return an iterator over all of this grid's values (tile and voxel).
Definition: Grid.h:782
void topologyIntersection(const Grid< OtherTreeType > &other)
Intersect this grid's set of active values with the active values of the other grid, whose value type may be different.
Definition: Grid.h:1526
SharedPtr< const Transform > ConstPtr
Definition: Transform.h:70
Name valueType() const override
Return the name of the type of a voxel's value (e.g., "float" or "vec3d").
Definition: Grid.h:729
void readTransform(std::istream &is)
Read in the transform for this grid.
Definition: Grid.h:490
Grid & operator=(const Grid &)=delete
Disallow assignment, since it wouldn't be obvious whether the copy is deep or shallow.
static const char *const META_GRID_NAME
Definition: Grid.h:371
typename tree::ValueAccessor< TreeType > AccessorType
Definition: Grid.h:1089
GLuint GLenum GLenum transform
Definition: glew.h:14742
typename TreeType::ConstPtr ConstTreePtrType
Definition: Grid.h:1081
static const TreeType & constTree(TreeType &t)
Definition: Grid.h:1127
ValueOnIter beginValueOn()
Return an iterator over all of this grid's active values (tile and voxel).
Definition: Grid.h:770
Vec3d voxelSize(const Vec3d &xyz) const
Return the size of this grid's voxel at position (x, y, z).
Definition: Grid.h:451
typename _TreeType::ValueOffCIter ValueOffCIter
Definition: Grid.h:608
GLboolean GLboolean GLboolean GLboolean a
Definition: glew.h:9477
SharedPtr< GridCPtrVec > GridCPtrVecPtr
Definition: Grid.h:539
SharedPtr< StreamMetadata > Ptr
Definition: io.h:60
SharedPtr< GridPtrSet > GridPtrSetPtr
Definition: Grid.h:544
static const TreeType & constTree(const GridType &g)
Definition: Grid.h:1130
ValueConverter<T>::Type is the type of a grid having the same hierarchy as this grid but a different ...
Definition: Grid.h:624
Ptr copyWithNewTree() const
Return a new grid of the same type as this grid whose metadata and transform are deep copies of this ...
Definition: Grid.h:1404
static const char *const META_SAVE_HALF_FLOAT
Definition: Grid.h:372
void topologyDifference(const Grid< OtherTreeType > &other)
Difference this grid's set of active values with the active values of the other grid, whose value type may be different.
Definition: Grid.h:1535
TreePtrType treePtr()
Return a pointer to this grid's tree, which might be shared with other grids. The pointer is guarante...
Definition: Grid.h:909
typename TreeType::ValueType ValueType
Definition: Grid.h:1088
typename tree::ValueAccessor< const TreeType > ConstAccessorType
Definition: Grid.h:1090
TreeType & tree()
Return a reference to this grid's tree, which might be shared with other grids.
Definition: Grid.h:919
SharedPtr< GridCPtrSet > GridCPtrSetPtr
Definition: Grid.h:549
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h:200
GridBase::Ptr deepCopyGrid() const override
Return a new grid whose metadata, transform and tree are deep copies of this grid's.
Definition: Grid.h:718
GridCPtrSet::const_iterator GridCPtrSetCIter
Definition: Grid.h:548
bool operator()(const GridBase::ConstPtr &g) const
Definition: Grid.h:556
typename tree::ValueAccessor< _TreeType, false > UnsafeAccessor
Definition: Grid.h:614
static const char *const META_FILE_COMPRESSION
Definition: Grid.h:377
static const char *const META_IS_LOCAL_SPACE
Definition: Grid.h:373
typename tree::ValueAccessor< NonConstTreeType > NonConstAccessorType
Definition: Grid.h:1121
static const char *const META_GRID_CLASS
Definition: Grid.h:369
tree::TreeBase TreeBase
Definition: Grid.h:53
typename tree::ValueAccessor< const TreeType > ConstAccessorType
Definition: Grid.h:1149
Ptr copy()
Return a new grid of the same type as this grid whose metadata and transform are deep copies of this ...
Definition: Grid.h:1396
ConstAccessor getConstAccessor() const
Return an accessor that provides random read-only access to this grid's voxels.
Definition: Grid.h:759
TreeBase::Ptr baseTreePtr()
Return a pointer to this grid's tree, which might be shared with other grids. The pointer is guarante...
Definition: Grid.h:1238
Container that maps names (strings) to values of arbitrary types.
Definition: MetaMap.h:46
std::vector< GridBase::Ptr > GridPtrVec
Definition: Grid.h:531
uint64 value_type
Definition: GA_PrimCompat.h:29
typename std::remove_const< TreeType >::type NonConstTreeType
Definition: Grid.h:1138
std::set< GridBase::Ptr > GridPtrSet
Definition: Grid.h:541
typename _TreeType::ConstPtr ConstTreePtrType
Definition: Grid.h:601
This adapter allows code that is templated on a Tree type to accept either a Tree type or a Grid type...
Definition: Grid.h:1076
GridBase::Ptr copyGrid() override
Return a new grid of the same type as this grid whose metadata is a deep copy of this grid's and whos...
Definition: Grid.h:1414
typename tree::ValueAccessor< NonConstTreeType > NonConstAccessorType
Definition: Grid.h:1091
GridCPtrSet::iterator GridCPtrSetIter
Definition: Grid.h:547
static const char *const META_FILE_VOXEL_COUNT
Definition: Grid.h:379
ValueOffCIter cbeginValueOff() const
Return an iterator over all of this grid's inactive values (tile and voxel).
Definition: Grid.h:780
std::shared_ptr< T > SharedPtr
Definition: Types.h:139
static const TreeType & constTree(const TreeType &t)
Definition: Grid.h:1129
static const TreeType & tree(const GridType &g)
Definition: Grid.h:1126
Abstract base class for typed grids.
Definition: Grid.h:104
void clip(const CoordBBox &) override
Clip this grid to the given index-space bounding box.
Definition: Grid.h:1499
Vec3d indexToWorld(const Vec3d &xyz) const
Apply this grid's transform to the given coordinates.
Definition: Grid.h:455
static const char *const META_FILE_BBOX_MIN
Definition: Grid.h:375
typename std::remove_const< TreeType >::type NonConstTreeType
Definition: Grid.h:1109
ValueOffIter beginValueOff()
Return an iterator over all of this grid's inactive values (tile and voxel).
Definition: Grid.h:776
bool isType() const
Return true if this grid is of the same type as the template parameter.
Definition: Grid.h:165
#define OPENVDB_API
Helper macros for defining library symbol visibility.
Definition: Platform.h:288
void writeTopology(std::ostream &) const override
Write the grid topology to a stream. This will write only the grid structure, not the actual data buf...
Definition: Grid.h:1588
static void unregisterGrid()
Remove this grid type from the registry.
Definition: Grid.h:994
typename _TreeType::ValueAllCIter ValueAllCIter
Definition: Grid.h:610
static Ptr create()
Return a new grid with background value zero.
Definition: Grid.h:1319
#define OPENVDB_DEPRECATED
Definition: Platform.h:69
static void registerGrid()
Register this grid type along with a factory function.
Definition: Grid.h:985
GridType::Ptr createLevelSet(Real voxelSize=1.0, Real halfWidth=LEVEL_SET_HALF_WIDTH)
Create a new grid of type GridType classified as a "Level Set", i.e., a narrow-band level set...
Definition: Grid.h:1742
GridBase(GridBase &other, ShallowCopy)
Copy another grid's metadata but share its transform.
Definition: Grid.h:514
typename tree::ValueAccessor< const _TreeType, false > ConstUnsafeAccessor
Definition: Grid.h:615
virtual GridBase::Ptr deepCopyGrid() const =0
Return a new grid whose metadata, transform and tree are deep copies of this grid's.
math::Transform::ConstPtr transformPtr() const
Return a pointer to this grid's transform, which might be shared with other grids.
Definition: Grid.h:421
GridBase::Ptr copyGridWithNewTree() const override
Return a new grid of the same type as this grid whose metadata and transform are deep copies of this ...
Definition: Grid.h:1429
Accessor getAccessor()
Return an accessor that provides random read and write access to this grid's voxels.
Definition: Grid.h:747
void sparseFill(const CoordBBox &bbox, const ValueType &value, bool active=true)
Set all voxels within a given axis-aligned box to a constant value.
Definition: Grid.h:1466
ConstTreePtrType treePtr() const
Return a pointer to this grid's tree, which might be shared with other grids. The pointer is guarante...
Definition: Grid.h:910
bool apply(OpT &) const
If this grid resolves to one of the listed grid types, invoke the given functor on the resolved grid...
Definition: Grid.h:1787
typename std::remove_const< ToType >::type Type
Definition: Types.h:348
typename GridType::Ptr GridPtrType
Definition: Grid.h:1085
GLuint GLuint end
Definition: glew.h:1253
CoordBBox evalActiveVoxelBoundingBox() const override
Return the axis-aligned bounding box of all active voxels.
Definition: Grid.h:1554
typename _TreeType::ValueOffIter ValueOffIter
Definition: Grid.h:607
typename NonConstGridType::Ptr NonConstGridPtrType
Definition: Grid.h:1086
Name type() const override
Return the name of this grid's type.
Definition: Grid.h:724
Index64 memUsage() const override
Definition: Grid.h:898
GridType::Ptr gridPtrCast(const GridBase::Ptr &grid)
Cast a generic grid pointer to a pointer to a grid of a concrete class.
Definition: Grid.h:1019
const TreeBase & constBaseTree() const
Return a reference to this grid's tree, which might be shared with other grids.
Definition: Grid.h:209
ValueOffCIter beginValueOff() const
Return an iterator over all of this grid's inactive values (tile and voxel).
Definition: Grid.h:778
static TreeType & tree(TreeType &t)
Definition: Grid.h:1093
GridBase()
Initialize with an identity linear transform.
Definition: Grid.h:502
static void unregisterGrid(const Name &type)
Remove a grid type from the registry.
typename TreeType::Ptr TreePtrType
Definition: Grid.h:1080
math::Transform::Ptr transformPtr()
Return a pointer to this grid's transform, which might be shared with other grids.
Definition: Grid.h:420
GLuint GLuint GLsizei GLenum type
Definition: glew.h:1253
TreeBase::ConstPtr baseTreePtr() const
Return a pointer to this grid's tree, which might be shared with other grids. The pointer is guarante...
Definition: Grid.h:190
math::Transform & transform()
Return a reference to this grid's transform, which might be shared with other grids.
Definition: Grid.h:429
Container class that associates a tree with a transform and metadata.
Definition: Grid.h:55
ConstTreePtrType constTreePtr() const
Return a pointer to this grid's tree, which might be shared with other grids. The pointer is guarante...
Definition: Grid.h:911
void topologyUnion(const Grid< OtherTreeType > &other)
Union this grid's set of active values with the active values of the other grid, whose value type may...
Definition: Grid.h:1517
SharedPtr< GridPtrVec > GridPtrVecPtr
Definition: Grid.h:534
typename _TreeType::ValueOnIter ValueOnIter
Definition: Grid.h:605
void readNonresidentBuffers() const override
Read all of this grid's data buffers that are not yet resident in memory (because delayed loading is ...
Definition: Grid.h:1643
GridPtrSet::const_iterator GridPtrSetCIter
Definition: Grid.h:543
ValueAllCIter cbeginValueAll() const
Return an iterator over all of this grid's values (tile and voxel).
Definition: Grid.h:786
static GridType::Ptr grid(const GridBase::Ptr &)
Return the result of downcasting a GridBase pointer to a Grid pointer of the specified type...
Definition: Grid.h:1200
const ValueType & background() const
Return this grid's background value.
Definition: Grid.h:737
static const TreeType & constTree(GridType &g)
Definition: Grid.h:1128
Base class for typed trees.
Definition: Tree.h:62
static const TreeType & tree(const TreeType &t)
Definition: Grid.h:1125
GridType::Ptr deepCopyTypedGrid(const GridBase::ConstPtr &grid)
Return a pointer to a deep copy of the given grid, provided that the grid's concrete type is GridType...
Definition: Grid.h:1052
typename std::remove_const< TreeType >::type NonConstTreeType
Definition: Grid.h:1079
static bool hasMultiPassIO()
Return true if grids of this type require multiple I/O passes to read and write data buffers...
Definition: Grid.h:1682
Predicate functor that returns true for grids that have a specified name.
Definition: Grid.h:553
GridCPtrVec::iterator GridCPtrVecIter
Definition: Grid.h:537
GLsizei const GLchar *const * string
Definition: glew.h:1844
void print(std::ostream &=std::cout, int verboseLevel=1) const override
Output a human-readable description of this grid.
Definition: Grid.h:1690
static const char *const META_FILE_MEM_BYTES
Definition: Grid.h:378
void denseFill(const CoordBBox &bbox, const ValueType &value, bool active=true)
Set all voxels within a given axis-aligned box to a constant value and ensure that those voxels are a...
Definition: Grid.h:1481
IMATH_INTERNAL_NAMESPACE_HEADER_ENTER T clip(const T &p, const Box< T > &box)
Definition: ImathBoxAlgo.h:89
void fill(const CoordBBox &bbox, const ValueType &value, bool active=true)
Set all voxels within a given axis-aligned box to a constant value.
Definition: Grid.h:1474
TreeBase & baseTree()
Return a reference to this grid's tree, which might be shared with other grids.
Definition: Grid.h:199
Coord evalActiveVoxelDim() const override
Return the dimensions of the axis-aligned bounding box of all active voxels.
Definition: Grid.h:1564
bool hasUniformVoxels() const
Return true if the voxels in world space are uniformly sized cubes.
Definition: Grid.h:453
OPENVDB_API uint32_t getGridClass(std::ios_base &)
Return the class (GRID_LEVEL_SET, GRID_UNKNOWN, etc.) of the grid currently being read from or writte...
typename tree::ValueAccessor< _TreeType, true > Accessor
Definition: Grid.h:612
typename _TreeType::ValueOnCIter ValueOnCIter
Definition: Grid.h:606
void evalMinMax(ValueType &minVal, ValueType &maxVal) const
Return the minimum and maximum active values in this grid.
Definition: Grid.h:1546
GridPtrVec::iterator GridPtrVecIter
Definition: Grid.h:532
MetadataMap::const_iterator ConstMetaIterator
Definition: MetaMap.h:55
static const char *const META_FILE_DELAYED_LOAD
Definition: Grid.h:380
#define OPENVDB_NO_TYPE_CONVERSION_WARNING_BEGIN
Bracket code with OPENVDB_NO_TYPE_CONVERSION_WARNING_BEGIN/_END, to inhibit warnings about type conve...
Definition: Platform.h:223
ValueAllCIter beginValueAll() const
Return an iterator over all of this grid's values (tile and voxel).
Definition: Grid.h:784
static const TreeType & constTree(const TreeType &t)
Definition: Grid.h:1099
math::Transform::ConstPtr constTransformPtr() const
Return a pointer to this grid's transform, which might be shared with other grids.
Definition: Grid.h:422
void clear() override
Empty this grid, so that all voxels become inactive background voxels.
Definition: Grid.h:742
ConstAccessor getAccessor() const
Return an accessor that provides random read-only access to this grid's voxels.
Definition: Grid.h:757
SharedPtr< const GridBase > ConstPtr
Definition: Grid.h:108
void pruneGrid(float tolerance=0.0) override
Reduce the memory footprint of this grid by increasing its sparseness.
Definition: Grid.h:1488
OIIO_API bool copy(string_view from, string_view to, std::string &err)
UnsafeAccessor getUnsafeAccessor()
Return an unsafe accessor that provides random read and write access to this grid's voxels...
Definition: Grid.h:755
SharedPtr< const TreeBase > ConstPtr
Definition: Tree.h:66
static const char *const META_GRID_CREATOR
Definition: Grid.h:370
void writeBuffers(std::ostream &) const override
Write out all data buffers for this grid.
Definition: Grid.h:1653
GridType::ConstPtr gridConstPtrCast(const GridBase::ConstPtr &grid)
Cast a generic const grid pointer to a const pointer to a grid of a concrete class.
Definition: Grid.h:1035
OPENVDB_API SharedPtr< StreamMetadata > getStreamMetadataPtr(std::ios_base &)
Return a shared pointer to an object that stores metadata (file format, compression scheme...
ValueOnCIter beginValueOn() const
Return an iterator over all of this grid's active values (tile and voxel).
Definition: Grid.h:772
Vec3d voxelSize() const
Return the size of this grid's voxels.
Definition: Grid.h:448
static const TreeType & tree(const GridType &g)
Definition: Grid.h:1096
static bool isRegistered()
Return true if this grid type is registered.
Definition: Grid.h:983
GLuint64EXT * result
Definition: glew.h:14007
bool empty() const override
Return true if this grid contains only inactive background voxels.
Definition: Grid.h:740
static const TreeType & constTree(const GridType &g)
Definition: Grid.h:1100
GridBase(const GridBase &other)
Deep copy another grid's metadata and transform.
Definition: Grid.h:505
Metafunction that specifies whether a given leaf node, tree, or grid type requires multiple passes to...
Definition: Grid.h:1176
const math::Transform & transform() const
Return a reference to this grid's transform, which might be shared with other grids.
Definition: Grid.h:430
GridPtrContainerT::value_type findGridByName(const GridPtrContainerT &container, const Name &name)
Return the first grid in the given container whose name is name.
Definition: Grid.h:563
typename _TreeType::BuildType BuildType
Definition: Grid.h:603
typename NonConstTreeType::Ptr NonConstTreePtrType
Definition: Grid.h:1082
static const char *const META_VECTOR_TYPE
Definition: Grid.h:374
virtual TreeBase::ConstPtr constBaseTreePtr() const =0
Return a pointer to this grid's tree, which might be shared with other grids. The pointer is guarante...
Grid()
Construct a new grid with background value zero.
Definition: Grid.h:1256
void readBuffers(std::istream &) override
Read all data buffers for this grid.
Definition: Grid.h:1596
void merge(Grid &other, MergePolicy policy=MERGE_ACTIVE_STATES)
Efficiently merge another grid into this grid using one of several schemes.
Definition: Grid.h:1508
static Transform::Ptr createLinearTransform(double voxelSize=1.0)
Create and return a shared pointer to a new transform.
static bool isRegistered(const Name &type)
Return true if the given grid type name is registered.
std::set< GridBase::ConstPtr > GridCPtrSet
Definition: Grid.h:546
void setTransform(math::Transform::Ptr)
Associate the given transform with this grid, in place of its existing transform. ...
Definition: Grid.h:1245
const math::Transform & constTransform() const
Return a reference to this grid's transform, which might be shared with other grids.
Definition: Grid.h:431
typename tree::ValueAccessor< const _TreeType, true > ConstAccessor
Definition: Grid.h:613
std::vector< GridBase::ConstPtr > GridCPtrVec
Definition: Grid.h:536
typename _TreeType::ValueAllIter ValueAllIter
Definition: Grid.h:609
static const TreeType & constTree(TreeType &t)
Definition: Grid.h:1097
static const TreeType & constTree(GridType &g)
Definition: Grid.h:1098
static Name gridType()
Return the name of this type of grid.
Definition: Grid.h:726
const TreeType & constTree() const
Return a reference to this grid's tree, which might be shared with other grids.
Definition: Grid.h:921
Ptr deepCopy() const
Return a new grid whose metadata, transform and tree are deep copies of this grid's.
Definition: Grid.h:716
#define OPENVDB_VERSION_NAME
The version namespace name for this library version.
Definition: version.h:146
GLsizei const GLfloat * value
Definition: glew.h:1849
OPENVDB_API uint32_t getFormatVersion(std::ios_base &)
Return the file format version number associated with the given input stream.
TreeBase::ConstPtr constBaseTreePtr() const override
Return a pointer to this grid's tree, which might be shared with other grids. The pointer is guarante...
Definition: Grid.h:912
void readTopology(std::istream &) override
Read the grid topology from a stream. This will read only the grid structure, not the actual data buf...
Definition: Grid.h:1580
GLdouble GLdouble t
Definition: glew.h:1398
GridCPtrVec::const_iterator GridCPtrVecCIter
Definition: Grid.h:538
void newTree() override
Associate a new, empty tree with this grid, in place of its existing tree.
Definition: Grid.h:1455
ValueOnCIter cbeginValueOn() const
Return an iterator over all of this grid's active values (tile and voxel).
Definition: Grid.h:774
static TreeType & tree(GridType &g)
Definition: Grid.h:1094
static void registerGrid(const Name &type, GridFactory)
Register a grid type along with a factory function.
static const char *const META_FILE_BBOX_MAX
Definition: Grid.h:376
GridPtrVec::const_iterator GridPtrVecCIter
Definition: Grid.h:533
#define OPENVDB_THROW(exception, message)
Definition: Exceptions.h:109
typename GridType::ConstPtr ConstGridPtrType
Definition: Grid.h:1087
type
Definition: core.h:528
const TreeBase & baseTree() const
Return a reference to this grid's tree, which might be shared with other grids.
Definition: Grid.h:204
typename _TreeType::Ptr TreePtrType
Definition: Grid.h:600
GLboolean GLboolean g
Definition: glew.h:9477
Index64 activeVoxelCount() const override
Return the number of active voxels.
Definition: Grid.h:888
typename tree::ValueAccessor< NonConstTreeType > NonConstAccessorType
Definition: Grid.h:1150
const TreeType & tree() const
Return a reference to this grid's tree, which might be shared with other grids.
Definition: Grid.h:920
std::enable_if< internal::is_string< S >::value >::type print(std::basic_ostream< FMT_CHAR(S)> &os, const S &format_str, const Args &...args)
Definition: ostream.h:146
void setTree(TreeBase::Ptr) override
Associate the given tree with this grid, in place of its existing tree.
Definition: Grid.h:1442