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