HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Grid.h
Go to the documentation of this file.
1 ///////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright (c) 2012-2017 DreamWorks Animation LLC
4 //
5 // All rights reserved. This software is distributed under the
6 // Mozilla Public License 2.0 ( http://www.mozilla.org/MPL/2.0/ )
7 //
8 // Redistributions of source code must retain the above copyright
9 // and license notice and the following restrictions and disclaimer.
10 //
11 // * Neither the name of DreamWorks Animation nor the names of
12 // its contributors may be used to endorse or promote products derived
13 // from this software without specific prior written permission.
14 //
15 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
18 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
19 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY INDIRECT, INCIDENTAL,
20 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
21 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 // IN NO EVENT SHALL THE COPYRIGHT HOLDERS' AND CONTRIBUTORS' AGGREGATE
27 // LIABILITY FOR ALL CLAIMS REGARDLESS OF THEIR BASIS EXCEED US$250.00.
28 //
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/Name.h"
41 #include <cassert>
42 #include <iostream>
43 #include <set>
44 #include <type_traits>
45 #include <vector>
46 
47 
48 namespace openvdb {
50 namespace OPENVDB_VERSION_NAME {
51 
53 
54 template<typename> class Grid; // forward declaration
55 
56 
57 /// @brief Create a new grid of type @c GridType with a given background value.
58 ///
59 /// @note Calling createGrid<GridType>(background) is equivalent to calling
60 /// GridType::create(background).
61 template<typename GridType>
62 inline typename GridType::Ptr createGrid(const typename GridType::ValueType& background);
63 
64 
65 /// @brief Create a new grid of type @c GridType with background value zero.
66 ///
67 /// @note Calling createGrid<GridType>() is equivalent to calling GridType::create().
68 template<typename GridType>
69 inline typename GridType::Ptr createGrid();
70 
71 
72 /// @brief Create a new grid of the appropriate type that wraps the given tree.
73 ///
74 /// @note This function can be called without specifying the template argument,
75 /// i.e., as createGrid(tree).
76 template<typename TreePtrType>
78 
79 
80 /// @brief Create a new grid of type @c GridType classified as a "Level Set",
81 /// i.e., a narrow-band level set.
82 ///
83 /// @note @c GridType::ValueType must be a floating-point scalar.
84 ///
85 /// @param voxelSize the size of a voxel in world units
86 /// @param halfWidth the half width of the narrow band in voxel units
87 ///
88 /// @details The voxel size and the narrow band half width define the grid's
89 /// background value as halfWidth*voxelWidth. The transform is linear
90 /// with a uniform scaling only corresponding to the specified voxel size.
91 ///
92 /// @note It is generally advisable to specify a half-width of the narrow band
93 /// that is larger than one voxel unit, otherwise zero crossings are not guaranteed.
94 template<typename GridType>
95 typename GridType::Ptr createLevelSet(
96  Real voxelSize = 1.0, Real halfWidth = LEVEL_SET_HALF_WIDTH);
97 
98 
99 ////////////////////////////////////////
100 
101 
102 /// @brief Abstract base class for typed grids
104 {
105 public:
108 
109  using GridFactory = Ptr (*)();
110 
111 
112  ~GridBase() override {}
113 
114 #ifdef OPENVDB_3_ABI_COMPATIBLE
115  /// @brief Return a new grid of the same type as this grid and whose
116  /// metadata and transform are deep copies of this grid's.
117  virtual GridBase::Ptr copyGrid(CopyPolicy treePolicy = CP_SHARE) const = 0;
118 #else
119  //@{
120  /// @brief Return a new grid of the same type as this grid whose metadata and
121  /// transform are deep copies of this grid's and whose tree is shared with this grid.
122  virtual GridBase::Ptr copyGrid() = 0;
123  virtual GridBase::ConstPtr copyGrid() const = 0;
124  //@}
125  /// @brief Return a new grid of the same type as this grid whose metadata and
126  /// transform are deep copies of this grid's and whose tree is default-constructed.
127  virtual GridBase::Ptr copyGridWithNewTree() const = 0;
128 #endif
129  /// Return a new grid whose metadata, transform and tree are deep copies of this grid's.
130  virtual GridBase::Ptr deepCopyGrid() const = 0;
131 
132 
133  //
134  // Registry methods
135  //
136  /// Create a new grid of the given (registered) type.
137  static Ptr createGrid(const Name& type);
138 
139  /// Return @c true if the given grid type name is registered.
140  static bool isRegistered(const Name &type);
141 
142  /// Clear the grid type registry.
143  static void clearRegistry();
144 
145 
146  //
147  // Grid type methods
148  //
149  /// Return the name of this grid's type.
150  virtual Name type() const = 0;
151  /// Return the name of the type of a voxel's value (e.g., "float" or "vec3d").
152  virtual Name valueType() const = 0;
153 
154  /// Return @c true if this grid is of the same type as the template parameter.
155  template<typename GridType>
156  bool isType() const { return (this->type() == GridType::gridType()); }
157 
158  //@{
159  /// @brief Return the result of downcasting a GridBase pointer to a Grid pointer
160  /// of the specified type, or return a null pointer if the types are incompatible.
161  template<typename GridType>
162  static typename GridType::Ptr grid(const GridBase::Ptr&);
163  template<typename GridType>
164  static typename GridType::ConstPtr grid(const GridBase::ConstPtr&);
165  template<typename GridType>
166  static typename GridType::ConstPtr constGrid(const GridBase::Ptr&);
167  template<typename GridType>
168  static typename GridType::ConstPtr constGrid(const GridBase::ConstPtr&);
169  //@}
170 
171  //@{
172  /// @brief Return a pointer to this grid's tree, which might be
173  /// shared with other grids. The pointer is guaranteed to be non-null.
174  TreeBase::Ptr baseTreePtr();
175  TreeBase::ConstPtr baseTreePtr() const { return this->constBaseTreePtr(); }
176  virtual TreeBase::ConstPtr constBaseTreePtr() const = 0;
177  //@}
178 
179  //@{
180  /// @brief Return a reference to this grid's tree, which might be
181  /// shared with other grids.
182  /// @note Calling setTree() on this grid invalidates all references
183  /// previously returned by this method.
184  TreeBase& baseTree() { return const_cast<TreeBase&>(this->constBaseTree()); }
185  const TreeBase& baseTree() const { return this->constBaseTree(); }
186  const TreeBase& constBaseTree() const { return *(this->constBaseTreePtr()); }
187  //@}
188 
189  /// @brief Associate the given tree with this grid, in place of its existing tree.
190  /// @throw ValueError if the tree pointer is null
191  /// @throw TypeError if the tree is not of the appropriate type
192  /// @note Invalidates all references previously returned by baseTree()
193  /// or constBaseTree().
194  virtual void setTree(TreeBase::Ptr) = 0;
195 
196  /// Set a new tree with the same background value as the previous tree.
197  virtual void newTree() = 0;
198 
199  /// Return @c true if this grid contains only background voxels.
200  virtual bool empty() const = 0;
201  /// Empty this grid, setting all voxels to the background.
202  virtual void clear() = 0;
203 
204  /// @brief Reduce the memory footprint of this grid by increasing its sparseness
205  /// either losslessly (@a tolerance = 0) or lossily (@a tolerance > 0).
206  /// @details With @a tolerance > 0, sparsify regions where voxels have the same
207  /// active state and have values that differ by no more than the tolerance
208  /// (converted to this grid's value type).
209  virtual void pruneGrid(float tolerance = 0.0) = 0;
210 
211 #ifndef OPENVDB_2_ABI_COMPATIBLE
212  /// @brief Clip this grid to the given world-space bounding box.
213  /// @details Voxels that lie outside the bounding box are set to the background.
214  /// @warning Clipping a level set will likely produce a grid that is
215  /// no longer a valid level set.
216  void clipGrid(const BBoxd&);
217 
218  /// @brief Clip this grid to the given index-space bounding box.
219  /// @details Voxels that lie outside the bounding box are set to the background.
220  /// @warning Clipping a level set will likely produce a grid that is
221  /// no longer a valid level set.
222  virtual void clip(const CoordBBox&) = 0;
223 #endif
224 
225 
226  //
227  // Metadata
228  //
229  /// Return this grid's user-specified name.
230  std::string getName() const;
231  /// Specify a name for this grid.
232  void setName(const std::string&);
233 
234  /// Return the user-specified description of this grid's creator.
235  std::string getCreator() const;
236  /// Provide a description of this grid's creator.
237  void setCreator(const std::string&);
238 
239  /// @brief Return @c true if this grid should be written out with floating-point
240  /// voxel values (including components of vectors) quantized to 16 bits.
241  bool saveFloatAsHalf() const;
242  void setSaveFloatAsHalf(bool);
243 
244  /// Return the class of volumetric data (level set, fog volume, etc.) stored in this grid.
245  GridClass getGridClass() const;
246  /// Specify the class of volumetric data (level set, fog volume, etc.) stored in this grid.
247  void setGridClass(GridClass);
248  /// Remove the setting specifying the class of this grid's volumetric data.
249  void clearGridClass();
250 
251  /// Return the metadata string value for the given class of volumetric data.
252  static std::string gridClassToString(GridClass);
253  /// Return a formatted string version of the grid class.
254  static std::string gridClassToMenuName(GridClass);
255  /// @brief Return the class of volumetric data specified by the given string.
256  /// @details If the string is not one of the ones returned by gridClassToString(),
257  /// return @c GRID_UNKNOWN.
258  static GridClass stringToGridClass(const std::string&);
259 
260  /// @brief Return the type of vector data (invariant, covariant, etc.) stored
261  /// in this grid, assuming that this grid contains a vector-valued tree.
262  VecType getVectorType() const;
263  /// @brief Specify the type of vector data (invariant, covariant, etc.) stored
264  /// in this grid, assuming that this grid contains a vector-valued tree.
265  void setVectorType(VecType);
266  /// Remove the setting specifying the type of vector data stored in this grid.
267  void clearVectorType();
268 
269  /// Return the metadata string value for the given type of vector data.
270  static std::string vecTypeToString(VecType);
271  /// Return a string listing examples of the given type of vector data
272  /// (e.g., "Gradient/Normal", given VEC_COVARIANT).
273  static std::string vecTypeExamples(VecType);
274  /// @brief Return a string describing how the given type of vector data is affected
275  /// by transformations (e.g., "Does not transform", given VEC_INVARIANT).
276  static std::string vecTypeDescription(VecType);
277  static VecType stringToVecType(const std::string&);
278 
279  /// Return @c true if this grid's voxel values are in world space and should be
280  /// affected by transformations, @c false if they are in local space and should
281  /// not be affected by transformations.
282  bool isInWorldSpace() const;
283  /// Specify whether this grid's voxel values are in world space or in local space.
284  void setIsInWorldSpace(bool);
285 
286  // Standard metadata field names
287  // (These fields should normally not be accessed directly, but rather
288  // via the accessor methods above, when available.)
289  // Note: Visual C++ requires these declarations to be separate statements.
290  static const char* const META_GRID_CLASS;
291  static const char* const META_GRID_CREATOR;
292  static const char* const META_GRID_NAME;
293  static const char* const META_SAVE_HALF_FLOAT;
294  static const char* const META_IS_LOCAL_SPACE;
295  static const char* const META_VECTOR_TYPE;
296  static const char* const META_FILE_BBOX_MIN;
297  static const char* const META_FILE_BBOX_MAX;
298  static const char* const META_FILE_COMPRESSION;
299  static const char* const META_FILE_MEM_BYTES;
300  static const char* const META_FILE_VOXEL_COUNT;
301 
302 
303  //
304  // Statistics
305  //
306  /// Return the number of active voxels.
307  virtual Index64 activeVoxelCount() const = 0;
308 
309  /// Return the axis-aligned bounding box of all active voxels. If
310  /// the grid is empty a default bbox is returned.
311  virtual CoordBBox evalActiveVoxelBoundingBox() const = 0;
312 
313  /// Return the dimensions of the axis-aligned bounding box of all active voxels.
314  virtual Coord evalActiveVoxelDim() const = 0;
315 
316  /// Return the number of bytes of memory used by this grid.
317  virtual Index64 memUsage() const = 0;
318 
319  /// @brief Add metadata to this grid comprising the current values
320  /// of statistics like the active voxel count and bounding box.
321  /// @note This metadata is not automatically kept up-to-date with
322  /// changes to this grid.
323  void addStatsMetadata();
324  /// @brief Return a new MetaMap containing just the metadata that
325  /// was added to this grid with addStatsMetadata().
326  /// @details If addStatsMetadata() was never called on this grid,
327  /// return an empty MetaMap.
328  MetaMap::Ptr getStatsMetadata() const;
329 
330 
331  //
332  // Transform methods
333  //
334  //@{
335  /// @brief Return a pointer to this grid's transform, which might be
336  /// shared with other grids.
337  math::Transform::Ptr transformPtr() { return mTransform; }
338  math::Transform::ConstPtr transformPtr() const { return mTransform; }
339  math::Transform::ConstPtr constTransformPtr() const { return mTransform; }
340  //@}
341  //@{
342  /// @brief Return a reference to this grid's transform, which might be
343  /// shared with other grids.
344  /// @note Calling setTransform() on this grid invalidates all references
345  /// previously returned by this method.
346  math::Transform& transform() { return *mTransform; }
347  const math::Transform& transform() const { return *mTransform; }
348  const math::Transform& constTransform() const { return *mTransform; }
349  //@}
350  /// @brief Associate the given transform with this grid, in place of
351  /// its existing transform.
352  /// @throw ValueError if the transform pointer is null
353  /// @note Invalidates all references previously returned by transform()
354  /// or constTransform().
355  void setTransform(math::Transform::Ptr);
356 
357  /// Return the size of this grid's voxels.
358  Vec3d voxelSize() const { return transform().voxelSize(); }
359  /// @brief Return the size of this grid's voxel at position (x, y, z).
360  /// @note Frustum and perspective transforms have position-dependent voxel size.
361  Vec3d voxelSize(const Vec3d& xyz) const { return transform().voxelSize(xyz); }
362  /// Return true if the voxels in world space are uniformly sized cubes
363  bool hasUniformVoxels() const { return mTransform->hasUniformScale(); }
364  //@{
365  /// Apply this grid's transform to the given coordinates.
366  Vec3d indexToWorld(const Vec3d& xyz) const { return transform().indexToWorld(xyz); }
367  Vec3d indexToWorld(const Coord& ijk) const { return transform().indexToWorld(ijk); }
368  //@}
369  /// Apply the inverse of this grid's transform to the given coordinates.
370  Vec3d worldToIndex(const Vec3d& xyz) const { return transform().worldToIndex(xyz); }
371 
372 
373  //
374  // I/O methods
375  //
376  /// @brief Read the grid topology from a stream.
377  /// This will read only the grid structure, not the actual data buffers.
378  virtual void readTopology(std::istream&) = 0;
379  /// @brief Write the grid topology to a stream.
380  /// This will write only the grid structure, not the actual data buffers.
381  virtual void writeTopology(std::ostream&) const = 0;
382 
383  /// Read all data buffers for this grid.
384  virtual void readBuffers(std::istream&) = 0;
385 #ifndef OPENVDB_2_ABI_COMPATIBLE
386  /// Read all of this grid's data buffers that intersect the given index-space bounding box.
387  virtual void readBuffers(std::istream&, const CoordBBox&) = 0;
388  /// @brief Read all of this grid's data buffers that are not yet resident in memory
389  /// (because delayed loading is in effect).
390  /// @details If this grid was read from a memory-mapped file, this operation
391  /// disconnects the grid from the file.
392  /// @sa io::File::open, io::MappedFile
393  virtual void readNonresidentBuffers() const = 0;
394 #endif
395  /// Write out all data buffers for this grid.
396  virtual void writeBuffers(std::ostream&) const = 0;
397 
398  /// Read in the transform for this grid.
399  void readTransform(std::istream& is) { transform().read(is); }
400  /// Write out the transform for this grid.
401  void writeTransform(std::ostream& os) const { transform().write(os); }
402 
403  /// Output a human-readable description of this grid.
404  virtual void print(std::ostream& = std::cout, int verboseLevel = 1) const = 0;
405 
406 
407 protected:
408  /// @brief Initialize with an identity linear transform.
409  GridBase(): mTransform(math::Transform::createLinearTransform()) {}
410 
411  /// @brief Deep copy another grid's metadata and transform.
412  GridBase(const GridBase& other): MetaMap(other), mTransform(other.mTransform->copy()) {}
413 
414 #ifdef OPENVDB_3_ABI_COMPATIBLE
415  /// @brief Copy another grid's metadata but share its transform.
416  GridBase(const GridBase& other, ShallowCopy): MetaMap(other), mTransform(other.mTransform) {}
417 #else
418  /// @brief Copy another grid's metadata but share its transform.
419  GridBase(GridBase& other, ShallowCopy): MetaMap(other), mTransform(other.mTransform) {}
420 #endif
421 
422  /// Register a grid type along with a factory function.
423  static void registerGrid(const Name& type, GridFactory);
424  /// Remove a grid type from the registry.
425  static void unregisterGrid(const Name& type);
426 
427 
428 private:
429  math::Transform::Ptr mTransform;
430 }; // class GridBase
431 
432 
433 ////////////////////////////////////////
434 
435 
436 using GridPtrVec = std::vector<GridBase::Ptr>;
437 using GridPtrVecIter = GridPtrVec::iterator;
438 using GridPtrVecCIter = GridPtrVec::const_iterator;
440 
441 using GridCPtrVec = std::vector<GridBase::ConstPtr>;
442 using GridCPtrVecIter = GridCPtrVec::iterator;
443 using GridCPtrVecCIter = GridCPtrVec::const_iterator;
445 
446 using GridPtrSet = std::set<GridBase::Ptr>;
447 using GridPtrSetIter = GridPtrSet::iterator;
448 using GridPtrSetCIter = GridPtrSet::const_iterator;
450 
451 using GridCPtrSet = std::set<GridBase::ConstPtr>;
452 using GridCPtrSetIter = GridCPtrSet::iterator;
453 using GridCPtrSetCIter = GridCPtrSet::const_iterator;
455 
456 
457 /// @brief Predicate functor that returns @c true for grids that have a specified name
459 {
460  GridNamePred(const Name& _name): name(_name) {}
461  bool operator()(const GridBase::ConstPtr& g) const { return g && g->getName() == name; }
463 };
464 
465 /// Return the first grid in the given container whose name is @a name.
466 template<typename GridPtrContainerT>
467 inline typename GridPtrContainerT::value_type
468 findGridByName(const GridPtrContainerT& container, const Name& name)
469 {
470  using GridPtrT = typename GridPtrContainerT::value_type;
471  typename GridPtrContainerT::const_iterator it =
472  std::find_if(container.begin(), container.end(), GridNamePred(name));
473  return (it == container.end() ? GridPtrT() : *it);
474 }
475 
476 /// Return the first grid in the given map whose name is @a name.
477 template<typename KeyT, typename GridPtrT>
478 inline GridPtrT
479 findGridByName(const std::map<KeyT, GridPtrT>& container, const Name& name)
480 {
481  using GridPtrMapT = std::map<KeyT, GridPtrT>;
482  for (typename GridPtrMapT::const_iterator it = container.begin(), end = container.end();
483  it != end; ++it)
484  {
485  const GridPtrT& grid = it->second;
486  if (grid && grid->getName() == name) return grid;
487  }
488  return GridPtrT();
489 }
490 //@}
491 
492 
493 ////////////////////////////////////////
494 
495 
496 /// @brief Container class that associates a tree with a transform and metadata
497 template<typename _TreeType>
498 class Grid: public GridBase
499 {
500 public:
503 
504  using TreeType = _TreeType;
505  using TreePtrType = typename _TreeType::Ptr;
506  using ConstTreePtrType = typename _TreeType::ConstPtr;
507  using ValueType = typename _TreeType::ValueType;
508  using BuildType = typename _TreeType::BuildType;
509 
510  using ValueOnIter = typename _TreeType::ValueOnIter;
511  using ValueOnCIter = typename _TreeType::ValueOnCIter;
512  using ValueOffIter = typename _TreeType::ValueOffIter;
513  using ValueOffCIter = typename _TreeType::ValueOffCIter;
514  using ValueAllIter = typename _TreeType::ValueAllIter;
515  using ValueAllCIter = typename _TreeType::ValueAllCIter;
516 
521 
522  /// @brief ValueConverter<T>::Type is the type of a grid having the same
523  /// hierarchy as this grid but a different value type, T.
524  ///
525  /// For example, FloatGrid::ValueConverter<double>::Type is equivalent to DoubleGrid.
526  /// @note If the source grid type is a template argument, it might be necessary
527  /// to write "typename SourceGrid::template ValueConverter<T>::Type".
528  template<typename OtherValueType>
529  struct ValueConverter {
531  };
532 
533  /// Return a new grid with the given background value.
534  static Ptr create(const ValueType& background);
535  /// Return a new grid with background value zero.
536  static Ptr create();
537  /// @brief Return a new grid that contains the given tree.
538  /// @throw ValueError if the tree pointer is null
539  static Ptr create(TreePtrType);
540  /// @brief Return a new, empty grid with the same transform and metadata as the
541  /// given grid and with background value zero.
542  static Ptr create(const GridBase& other);
543 
544 
545  /// Construct a new grid with background value zero.
546  Grid();
547  /// Construct a new grid with the given background value.
548  explicit Grid(const ValueType& background);
549  /// @brief Construct a new grid that shares the given tree and associates with it
550  /// an identity linear transform.
551  /// @throw ValueError if the tree pointer is null
552  explicit Grid(TreePtrType);
553  /// Deep copy another grid's metadata, transform and tree.
554  Grid(const Grid&);
555  /// @brief Deep copy the metadata, transform and tree of another grid whose tree
556  /// configuration is the same as this grid's but whose value type is different.
557  /// Cast the other grid's values to this grid's value type.
558  /// @throw TypeError if the other grid's tree configuration doesn't match this grid's
559  /// or if this grid's ValueType is not constructible from the other grid's ValueType.
560  template<typename OtherTreeType>
561  explicit Grid(const Grid<OtherTreeType>&);
562 #ifdef OPENVDB_3_ABI_COMPATIBLE
563  /// Deep copy another grid's metadata, but share its tree and transform.
564  Grid(const Grid&, ShallowCopy);
565 #else
566  /// Deep copy another grid's metadata and transform, but share its tree.
567  Grid(Grid&, ShallowCopy);
568 #endif
569  /// @brief Deep copy another grid's metadata and transform, but construct a new tree
570  /// with background value zero.
571  explicit Grid(const GridBase&);
572 
573  ~Grid() override {}
574 
575  /// Disallow assignment, since it wouldn't be obvious whether the copy is deep or shallow.
576  Grid& operator=(const Grid&) = delete;
577 
578 
579 #ifdef OPENVDB_3_ABI_COMPATIBLE
580  //@{
581  /// @brief Return a new grid of the same type as this grid and whose
582  /// metadata and transform are deep copies of this grid's.
583  /// @details If @a treePolicy is @c CP_NEW, give the new grid a new, empty tree;
584  /// if @c CP_SHARE, the new grid shares this grid's tree and transform;
585  /// if @c CP_COPY, the new grid's tree is a deep copy of this grid's tree and transform
586  Ptr copy(CopyPolicy treePolicy = CP_SHARE) const;
587  GridBase::Ptr copyGrid(CopyPolicy treePolicy = CP_SHARE) const override;
588  //@}
589 #else
590  //@{
591  /// @brief Return a new grid of the same type as this grid whose metadata and
592  /// transform are deep copies of this grid's and whose tree is shared with this grid.
593  Ptr copy();
594  ConstPtr copy() const;
595  //@}
596  /// @brief Return a new grid of the same type as this grid whose metadata and
597  /// transform are deep copies of this grid's and whose tree is default-constructed.
598  Ptr copyWithNewTree() const;
599 
600  GridBase::Ptr copyGrid() override;
601  GridBase::ConstPtr copyGrid() const override;
602  GridBase::Ptr copyGridWithNewTree() const override;
603 #endif
604  //@{
605  /// Return a new grid whose metadata, transform and tree are deep copies of this grid's.
606  Ptr deepCopy() const { return Ptr(new Grid(*this)); }
607  GridBase::Ptr deepCopyGrid() const override { return this->deepCopy(); }
608  //@}
609 
610  /// Return the name of this grid's type.
611  Name type() const override { return this->gridType(); }
612  /// Return the name of this type of grid.
613  static Name gridType() { return TreeType::treeType(); }
614 
615 
616  //
617  // Voxel access methods
618  //
619  /// Return the name of the type of a voxel's value (e.g., "float" or "vec3d").
620  Name valueType() const override { return tree().valueType(); }
621 
622  /// @brief Return this grid's background value.
623  ///
624  /// @note Use tools::changeBackground to efficiently modify the background values.
625  const ValueType& background() const { return mTree->background(); }
626 
627  /// Return @c true if this grid contains only inactive background voxels.
628  bool empty() const override { return tree().empty(); }
629  /// Empty this grid, so that all voxels become inactive background voxels.
630  void clear() override { tree().clear(); }
631 
632  /// @brief Return an accessor that provides random read and write access
633  /// to this grid's voxels. The accessor is safe in the sense that
634  /// it is registered by the tree of this grid.
636  /// @brief Return an accessor that provides random read and write access
637  /// to this grid's voxels. The accessor is unsafe in the sense that
638  /// it is not registered by the tree of this grid. In some rare
639  /// cases this can give a performance advantage over a registered
640  /// accessor but it is unsafe if the tree topology is modified.
641  ///
642  /// @warning Only use this method if you're an expert and know the
643  /// risks of using an unregistered accessor (see tree/ValueAccessor.h)
645  //@{
646  /// Return an accessor that provides random read-only access to this grid's voxels.
649  //@}
650  /// @brief Return an accessor that provides random read-only access
651  /// to this grid's voxels. The accessor is unsafe in the sense that
652  /// it is not registered by the tree of this grid. In some rare
653  /// cases this can give a performance advantage over a registered
654  /// accessor but it is unsafe if the tree topology is modified.
655  ///
656  /// @warning Only use this method if you're an expert and know the
657  /// risks of using an unregistered accessor (see tree/ValueAccessor.h)
659 
660  //@{
661  /// Return an iterator over all of this grid's active values (tile and voxel).
662  ValueOnIter beginValueOn() { return tree().beginValueOn(); }
663  ValueOnCIter beginValueOn() const { return tree().cbeginValueOn(); }
664  ValueOnCIter cbeginValueOn() const { return tree().cbeginValueOn(); }
665  //@}
666  //@{
667  /// Return an iterator over all of this grid's inactive values (tile and voxel).
668  ValueOffIter beginValueOff() { return tree().beginValueOff(); }
669  ValueOffCIter beginValueOff() const { return tree().cbeginValueOff(); }
670  ValueOffCIter cbeginValueOff() const { return tree().cbeginValueOff(); }
671  //@}
672  //@{
673  /// Return an iterator over all of this grid's values (tile and voxel).
674  ValueAllIter beginValueAll() { return tree().beginValueAll(); }
675  ValueAllCIter beginValueAll() const { return tree().cbeginValueAll(); }
676  ValueAllCIter cbeginValueAll() const { return tree().cbeginValueAll(); }
677  //@}
678 
679  /// Return the minimum and maximum active values in this grid.
680  void evalMinMax(ValueType& minVal, ValueType& maxVal) const;
681 
682  //@{
683  /// @brief Set all voxels within a given axis-aligned box to a constant value.
684  /// @param bbox inclusive coordinates of opposite corners of an axis-aligned box
685  /// @param value the value to which to set voxels within the box
686  /// @param active if true, mark voxels within the box as active,
687  /// otherwise mark them as inactive
688  /// @note This operation generates a sparse, but not always optimally sparse,
689  /// representation of the filled box. Follow fill operations with a prune()
690  /// operation for optimal sparseness.
691  void sparseFill(const CoordBBox& bbox, const ValueType& value, bool active = true);
692  void fill(const CoordBBox& bbox, const ValueType& value, bool active = true);
693  //@}
694 
695  /// @brief Set all voxels within a given axis-aligned box to a constant value.
696  /// @param bbox inclusive coordinates of opposite corners of an axis-aligned box.
697  /// @param value the value to which to set voxels within the box.
698  /// @param active if true, mark voxels within the box as active,
699  /// otherwise mark them as inactive.
700  /// @note This operation generates a dense representation of the filled box.
701  /// This implies that active tiles are voxelized, i.e., only active voxels
702  /// are generated from this fill operation.
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 #ifndef OPENVDB_2_ABI_COMPATIBLE
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 #ifndef OPENVDB_2_ABI_COMPATIBLE
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() { GridBase::registerGrid(Grid::gridType(), Grid::factory); }
856  /// Remove this grid type from the registry.
858 
859 
860 private:
861  /// Helper function for use with registerGrid()
862  static GridBase::Ptr factory() { return Grid::create(); }
863 
864  TreePtrType mTree;
865 }; // class Grid
866 
867 
868 ////////////////////////////////////////
869 
870 
871 /// @brief Cast a generic grid pointer to a pointer to a grid of a concrete class.
872 ///
873 /// Return a null pointer if the input pointer is null or if it
874 /// points to a grid that is not of type @c GridType.
875 ///
876 /// @note Calling gridPtrCast<GridType>(grid) is equivalent to calling
877 /// GridBase::grid<GridType>(grid).
878 template<typename GridType>
879 inline typename GridType::Ptr
881 {
882  return GridBase::grid<GridType>(grid);
883 }
884 
885 
886 /// @brief Cast a generic const grid pointer to a const pointer to a grid
887 /// of a concrete class.
888 ///
889 /// Return a null pointer if the input pointer is null or if it
890 /// points to a grid that is not of type @c GridType.
891 ///
892 /// @note Calling gridConstPtrCast<GridType>(grid) is equivalent to calling
893 /// GridBase::constGrid<GridType>(grid).
894 template<typename GridType>
895 inline typename GridType::ConstPtr
897 {
898  return GridBase::constGrid<GridType>(grid);
899 }
900 
901 
902 ////////////////////////////////////////
903 
904 
905 /// @{
906 /// @brief Return a pointer to a deep copy of the given grid, provided that
907 /// the grid's concrete type is @c GridType.
908 ///
909 /// Return a null pointer if the input pointer is null or if it
910 /// points to a grid that is not of type @c GridType.
911 template<typename GridType>
912 inline typename GridType::Ptr
914 {
915  if (!grid || !grid->isType<GridType>()) return typename GridType::Ptr();
916  return gridPtrCast<GridType>(grid->deepCopyGrid());
917 }
918 
919 
920 template<typename GridType>
921 inline typename GridType::Ptr
923 {
924  if (!grid.isType<GridType>()) return typename GridType::Ptr();
925  return gridPtrCast<GridType>(grid.deepCopyGrid());
926 }
927 /// @}
928 
929 
930 ////////////////////////////////////////
931 
932 
933 //@{
934 /// @brief This adapter allows code that is templated on a Tree type to
935 /// accept either a Tree type or a Grid type.
936 template<typename _TreeType>
938 {
939  using TreeType = _TreeType;
941  using TreePtrType = typename TreeType::Ptr;
942  using ConstTreePtrType = typename TreeType::ConstPtr;
943  using NonConstTreePtrType = typename NonConstTreeType::Ptr;
946  using GridPtrType = typename GridType::Ptr;
949  using ValueType = typename TreeType::ValueType;
953 
954  static TreeType& tree(TreeType& t) { return t; }
955  static TreeType& tree(GridType& g) { return g.tree(); }
956  static const TreeType& tree(const TreeType& t) { return t; }
957  static const TreeType& tree(const GridType& g) { return g.tree(); }
958  static const TreeType& constTree(TreeType& t) { return t; }
959  static const TreeType& constTree(GridType& g) { return g.constTree(); }
960  static const TreeType& constTree(const TreeType& t) { return t; }
961  static const TreeType& constTree(const GridType& g) { return g.constTree(); }
962 };
963 
964 
965 /// Partial specialization for Grid types
966 template<typename _TreeType>
967 struct TreeAdapter<Grid<_TreeType> >
968 {
969  using TreeType = _TreeType;
971  using TreePtrType = typename TreeType::Ptr;
972  using ConstTreePtrType = typename TreeType::ConstPtr;
973  using NonConstTreePtrType = typename NonConstTreeType::Ptr;
976  using GridPtrType = typename GridType::Ptr;
979  using ValueType = typename TreeType::ValueType;
983 
984  static TreeType& tree(TreeType& t) { return t; }
985  static TreeType& tree(GridType& g) { return g.tree(); }
986  static const TreeType& tree(const TreeType& t) { return t; }
987  static const TreeType& tree(const GridType& g) { return g.tree(); }
988  static const TreeType& constTree(TreeType& t) { return t; }
989  static const TreeType& constTree(GridType& g) { return g.constTree(); }
990  static const TreeType& constTree(const TreeType& t) { return t; }
991  static const TreeType& constTree(const GridType& g) { return g.constTree(); }
992 };
993 
994 /// Partial specialization for ValueAccessor types
995 template<typename _TreeType>
996 struct TreeAdapter<tree::ValueAccessor<_TreeType> >
997 {
998  using TreeType = _TreeType;
1000  using TreePtrType = typename TreeType::Ptr;
1001  using ConstTreePtrType = typename TreeType::ConstPtr;
1002  using NonConstTreePtrType = typename NonConstTreeType::Ptr;
1005  using GridPtrType = typename GridType::Ptr;
1008  using ValueType = typename TreeType::ValueType;
1012 
1013  static TreeType& tree(TreeType& t) { return t; }
1014  static TreeType& tree(GridType& g) { return g.tree(); }
1015  static TreeType& tree(AccessorType& a) { return a.tree(); }
1016  static const TreeType& tree(const TreeType& t) { return t; }
1017  static const TreeType& tree(const GridType& g) { return g.tree(); }
1018  static const TreeType& tree(const AccessorType& a) { return a.tree(); }
1019  static const TreeType& constTree(TreeType& t) { return t; }
1020  static const TreeType& constTree(GridType& g) { return g.constTree(); }
1021  static const TreeType& constTree(const TreeType& t) { return t; }
1022  static const TreeType& constTree(const GridType& g) { return g.constTree(); }
1023 };
1024 
1025 //@}
1026 
1027 
1028 ////////////////////////////////////////
1029 
1030 
1031 /// @brief Metafunction that specifies whether a given leaf node, tree, or grid type
1032 /// requires multiple passes to read and write voxel data
1033 /// @details Multi-pass I/O allows one to optimize the data layout of leaf nodes
1034 /// for certain access patterns during delayed loading.
1035 /// @sa io::MultiPass
1036 template<typename LeafNodeType>
1039 };
1040 
1041 // Partial specialization for Tree types
1042 template<typename RootNodeType>
1043 struct HasMultiPassIO<tree::Tree<RootNodeType>> {
1044  // A tree is multi-pass if its (root node's) leaf node type is multi-pass.
1046 };
1047 
1048 // Partial specialization for Grid types
1049 template<typename TreeType>
1050 struct HasMultiPassIO<Grid<TreeType>> {
1051  // A grid is multi-pass if its tree's leaf node type is multi-pass.
1053 };
1054 
1055 
1056 ////////////////////////////////////////
1057 
1058 
1059 template<typename GridType>
1060 inline typename GridType::Ptr
1062 {
1063  // The string comparison on type names is slower than a dynamic pointer cast, but
1064  // it is safer when pointers cross DSO boundaries, as they do in many Houdini nodes.
1065  if (grid && grid->type() == GridType::gridType()) {
1066  return StaticPtrCast<GridType>(grid);
1067  }
1068  return typename GridType::Ptr();
1069 }
1070 
1071 
1072 template<typename GridType>
1073 inline typename GridType::ConstPtr
1075 {
1076  return ConstPtrCast<const GridType>(
1077  GridBase::grid<GridType>(ConstPtrCast<GridBase>(grid)));
1078 }
1079 
1080 
1081 template<typename GridType>
1082 inline typename GridType::ConstPtr
1084 {
1085  return ConstPtrCast<const GridType>(GridBase::grid<GridType>(grid));
1086 }
1087 
1088 
1089 template<typename GridType>
1090 inline typename GridType::ConstPtr
1092 {
1093  return ConstPtrCast<const GridType>(
1094  GridBase::grid<GridType>(ConstPtrCast<GridBase>(grid)));
1095 }
1096 
1097 
1098 inline TreeBase::Ptr
1100 {
1101  return ConstPtrCast<TreeBase>(this->constBaseTreePtr());
1102 }
1103 
1104 
1105 inline void
1107 {
1108  if (!xform) OPENVDB_THROW(ValueError, "Transform pointer is null");
1109  mTransform = xform;
1110 }
1111 
1112 
1113 ////////////////////////////////////////
1114 
1115 
1116 template<typename TreeT>
1117 inline Grid<TreeT>::Grid(): mTree(new TreeType)
1118 {
1119 }
1120 
1121 
1122 template<typename TreeT>
1123 inline Grid<TreeT>::Grid(const ValueType &background): mTree(new TreeType(background))
1124 {
1125 }
1126 
1127 
1128 template<typename TreeT>
1129 inline Grid<TreeT>::Grid(TreePtrType tree): mTree(tree)
1130 {
1131  if (!tree) OPENVDB_THROW(ValueError, "Tree pointer is null");
1132 }
1133 
1134 
1135 template<typename TreeT>
1136 inline Grid<TreeT>::Grid(const Grid& other):
1137  GridBase(other),
1138  mTree(StaticPtrCast<TreeType>(other.mTree->copy()))
1139 {
1140 }
1141 
1142 
1143 template<typename TreeT>
1144 template<typename OtherTreeType>
1146  GridBase(other),
1147  mTree(new TreeType(other.constTree()))
1148 {
1149 }
1150 
1151 
1152 #ifdef OPENVDB_3_ABI_COMPATIBLE
1153 template<typename TreeT>
1154 inline Grid<TreeT>::Grid(const Grid& other, ShallowCopy):
1155  GridBase(other, ShallowCopy()),
1156  mTree(other.mTree)
1157 {
1158 }
1159 #else
1160 template<typename TreeT>
1162  GridBase(other),
1163  mTree(other.mTree)
1164 {
1165 }
1166 #endif
1167 
1168 
1169 template<typename TreeT>
1170 inline Grid<TreeT>::Grid(const GridBase& other):
1171  GridBase(other),
1172  mTree(new TreeType)
1173 {
1174 }
1175 
1176 
1177 //static
1178 template<typename TreeT>
1179 inline typename Grid<TreeT>::Ptr
1181 {
1182  return Grid::create(zeroVal<ValueType>());
1183 }
1184 
1185 
1186 //static
1187 template<typename TreeT>
1188 inline typename Grid<TreeT>::Ptr
1190 {
1191  return Ptr(new Grid(background));
1192 }
1193 
1194 
1195 //static
1196 template<typename TreeT>
1197 inline typename Grid<TreeT>::Ptr
1199 {
1200  return Ptr(new Grid(tree));
1201 }
1202 
1203 
1204 //static
1205 template<typename TreeT>
1206 inline typename Grid<TreeT>::Ptr
1208 {
1209  return Ptr(new Grid(other));
1210 }
1211 
1212 
1213 ////////////////////////////////////////
1214 
1215 
1216 #ifdef OPENVDB_3_ABI_COMPATIBLE
1217 
1218 template<typename TreeT>
1219 inline typename Grid<TreeT>::Ptr
1220 Grid<TreeT>::copy(CopyPolicy treePolicy) const
1221 {
1222  Ptr ret;
1223  switch (treePolicy) {
1224  case CP_NEW:
1225  ret.reset(new Grid(*this, ShallowCopy()));
1226  ret->newTree();
1227  break;
1228  case CP_COPY:
1229  ret.reset(new Grid(*this));
1230  break;
1231  case CP_SHARE:
1232  ret.reset(new Grid(*this, ShallowCopy()));
1233  break;
1234  }
1235  return ret;
1236 }
1237 
1238 
1239 template<typename TreeT>
1240 inline GridBase::Ptr
1241 Grid<TreeT>::copyGrid(CopyPolicy treePolicy) const
1242 {
1243  return this->copy(treePolicy);
1244 }
1245 
1246 #else // !OPENVDB_3_ABI_COMPATIBLE
1247 
1248 template<typename TreeT>
1249 inline typename Grid<TreeT>::ConstPtr
1251 {
1252  return ConstPtr{new Grid{*const_cast<Grid*>(this), ShallowCopy{}}};
1253 }
1254 
1255 template<typename TreeT>
1256 inline typename Grid<TreeT>::Ptr
1258 {
1259  return Ptr{new Grid{*this, ShallowCopy{}}};
1260 }
1261 
1262 
1263 template<typename TreeT>
1264 inline typename Grid<TreeT>::Ptr
1266 {
1267  Ptr result{new Grid{*const_cast<Grid*>(this), ShallowCopy{}}};
1268  result->newTree();
1269  return result;
1270 }
1271 
1272 
1273 template<typename TreeT>
1274 inline GridBase::Ptr
1276 {
1277  return this->copy();
1278 }
1279 
1280 template<typename TreeT>
1281 inline GridBase::ConstPtr
1283 {
1284  return this->copy();
1285 }
1286 
1287 
1288 template<typename TreeT>
1289 inline GridBase::Ptr
1291 {
1292  return this->copyWithNewTree();
1293 }
1294 
1295 #endif // OPENVDB_3_ABI_COMPATIBLE
1296 
1297 
1298 ////////////////////////////////////////
1299 
1300 
1301 template<typename TreeT>
1302 inline void
1304 {
1305  if (!tree) OPENVDB_THROW(ValueError, "Tree pointer is null");
1306  if (tree->type() != TreeType::treeType()) {
1307  OPENVDB_THROW(TypeError, "Cannot assign a tree of type "
1308  + tree->type() + " to a grid of type " + this->type());
1309  }
1310  mTree = StaticPtrCast<TreeType>(tree);
1311 }
1312 
1313 
1314 template<typename TreeT>
1315 inline void
1317 {
1318  mTree.reset(new TreeType(this->background()));
1319 }
1320 
1321 
1322 ////////////////////////////////////////
1323 
1324 
1325 template<typename TreeT>
1326 inline void
1327 Grid<TreeT>::sparseFill(const CoordBBox& bbox, const ValueType& value, bool active)
1328 {
1329  tree().sparseFill(bbox, value, active);
1330 }
1331 
1332 
1333 template<typename TreeT>
1334 inline void
1335 Grid<TreeT>::fill(const CoordBBox& bbox, const ValueType& value, bool active)
1336 {
1337  this->sparseFill(bbox, value, active);
1338 }
1339 
1340 template<typename TreeT>
1341 inline void
1342 Grid<TreeT>::denseFill(const CoordBBox& bbox, const ValueType& value, bool active)
1343 {
1344  tree().denseFill(bbox, value, active);
1345 }
1346 
1347 template<typename TreeT>
1348 inline void
1349 Grid<TreeT>::pruneGrid(float tolerance)
1350 {
1351  this->tree().prune(ValueType(zeroVal<ValueType>() + tolerance));
1352 }
1353 
1354 #ifndef OPENVDB_2_ABI_COMPATIBLE
1355 template<typename TreeT>
1356 inline void
1357 Grid<TreeT>::clip(const CoordBBox& bbox)
1358 {
1359  tree().clip(bbox);
1360 }
1361 #endif
1362 
1363 
1364 template<typename TreeT>
1365 inline void
1367 {
1368  tree().merge(other.tree(), policy);
1369 }
1370 
1371 
1372 template<typename TreeT>
1373 template<typename OtherTreeType>
1374 inline void
1376 {
1377  tree().topologyUnion(other.tree());
1378 }
1379 
1380 
1381 template<typename TreeT>
1382 template<typename OtherTreeType>
1383 inline void
1385 {
1386  tree().topologyIntersection(other.tree());
1387 }
1388 
1389 
1390 template<typename TreeT>
1391 template<typename OtherTreeType>
1392 inline void
1394 {
1395  tree().topologyDifference(other.tree());
1396 }
1397 
1398 
1399 ////////////////////////////////////////
1400 
1401 
1402 template<typename TreeT>
1403 inline void
1405 {
1406  tree().evalMinMax(minVal, maxVal);
1407 }
1408 
1409 
1410 template<typename TreeT>
1411 inline CoordBBox
1413 {
1414  CoordBBox bbox;
1415  tree().evalActiveVoxelBoundingBox(bbox);
1416  return bbox;
1417 }
1418 
1419 
1420 template<typename TreeT>
1421 inline Coord
1423 {
1424  Coord dim;
1425  const bool nonempty = tree().evalActiveVoxelDim(dim);
1426  return (nonempty ? dim : Coord());
1427 }
1428 
1429 
1430 ////////////////////////////////////////
1431 
1432 
1433 /// @internal Consider using the stream tagging mechanism (see io::Archive)
1434 /// to specify the float precision, but note that the setting is per-grid.
1435 
1436 template<typename TreeT>
1437 inline void
1438 Grid<TreeT>::readTopology(std::istream& is)
1439 {
1440  tree().readTopology(is, saveFloatAsHalf());
1441 }
1442 
1443 
1444 template<typename TreeT>
1445 inline void
1446 Grid<TreeT>::writeTopology(std::ostream& os) const
1447 {
1448  tree().writeTopology(os, saveFloatAsHalf());
1449 }
1450 
1451 
1452 template<typename TreeT>
1453 inline void
1454 Grid<TreeT>::readBuffers(std::istream& is)
1455 {
1456  if (!hasMultiPassIO() || (io::getFormatVersion(is) < OPENVDB_FILE_VERSION_MULTIPASS_IO)) {
1457  tree().readBuffers(is, saveFloatAsHalf());
1458  } else {
1459  uint16_t numPasses = 1;
1460  is.read(reinterpret_cast<char*>(&numPasses), sizeof(uint16_t));
1462  assert(bool(meta));
1463  for (uint32_t pass = 0; pass < uint32_t(numPasses); ++pass) {
1464  meta->setPass(pass);
1465  tree().readBuffers(is, saveFloatAsHalf());
1466  }
1467  }
1468 }
1469 
1470 
1471 #ifndef OPENVDB_2_ABI_COMPATIBLE
1472 
1473 /// @todo Refactor this and the readBuffers() above
1474 /// once support for ABI 2 compatibility is dropped.
1475 template<typename TreeT>
1476 inline void
1477 Grid<TreeT>::readBuffers(std::istream& is, const CoordBBox& bbox)
1478 {
1479  if (!hasMultiPassIO() || (io::getFormatVersion(is) < OPENVDB_FILE_VERSION_MULTIPASS_IO)) {
1480  tree().readBuffers(is, bbox, saveFloatAsHalf());
1481  } else {
1482  uint16_t numPasses = 1;
1483  is.read(reinterpret_cast<char*>(&numPasses), sizeof(uint16_t));
1485  assert(bool(meta));
1486  for (uint32_t pass = 0; pass < uint32_t(numPasses); ++pass) {
1487  meta->setPass(pass);
1488  tree().readBuffers(is, saveFloatAsHalf());
1489  }
1490  // Cannot clip inside readBuffers() when using multiple passes,
1491  // so instead clip afterwards.
1492  tree().clip(bbox);
1493  }
1494 }
1495 
1496 
1497 template<typename TreeT>
1498 inline void
1500 {
1501  tree().readNonresidentBuffers();
1502 }
1503 
1504 #endif // !OPENVDB_2_ABI_COMPATIBLE
1505 
1506 
1507 template<typename TreeT>
1508 inline void
1509 Grid<TreeT>::writeBuffers(std::ostream& os) const
1510 {
1511  if (!hasMultiPassIO()) {
1512  tree().writeBuffers(os, saveFloatAsHalf());
1513  } else {
1514  // Determine how many leaf buffer passes are required for this grid
1516  assert(bool(meta));
1517  uint16_t numPasses = 1;
1518  meta->setCountingPasses(true);
1519  meta->setPass(0);
1520  tree().writeBuffers(os, saveFloatAsHalf());
1521  numPasses = static_cast<uint16_t>(meta->pass());
1522  os.write(reinterpret_cast<const char*>(&numPasses), sizeof(uint16_t));
1523  meta->setCountingPasses(false);
1524 
1525  // Save out the data blocks of the grid.
1526  for (uint32_t pass = 0; pass < uint32_t(numPasses); ++pass) {
1527  meta->setPass(pass);
1528  tree().writeBuffers(os, saveFloatAsHalf());
1529  }
1530  }
1531 }
1532 
1533 
1534 //static
1535 template<typename TreeT>
1536 inline bool
1538 {
1540 }
1541 
1542 
1543 template<typename TreeT>
1544 inline void
1545 Grid<TreeT>::print(std::ostream& os, int verboseLevel) const
1546 {
1547  tree().print(os, verboseLevel);
1548 
1549  if (metaCount() > 0) {
1550  os << "Additional metadata:" << std::endl;
1551  for (ConstMetaIterator it = beginMeta(), end = endMeta(); it != end; ++it) {
1552  os << " " << it->first;
1553  if (it->second) {
1554  const std::string value = it->second->str();
1555  if (!value.empty()) os << ": " << value;
1556  }
1557  os << "\n";
1558  }
1559  }
1560 
1561  os << "Transform:" << std::endl;
1562  transform().print(os, /*indent=*/" ");
1563  os << std::endl;
1564 }
1565 
1566 
1567 ////////////////////////////////////////
1568 
1569 
1570 template<typename GridType>
1571 inline typename GridType::Ptr
1572 createGrid(const typename GridType::ValueType& background)
1573 {
1574  return GridType::create(background);
1575 }
1576 
1577 
1578 template<typename GridType>
1579 inline typename GridType::Ptr
1581 {
1582  return GridType::create();
1583 }
1584 
1585 
1586 template<typename TreePtrType>
1587 inline typename Grid<typename TreePtrType::element_type>::Ptr
1588 createGrid(TreePtrType tree)
1589 {
1590  using TreeType = typename TreePtrType::element_type;
1591  return Grid<TreeType>::create(tree);
1592 }
1593 
1594 
1595 template<typename GridType>
1596 typename GridType::Ptr
1597 createLevelSet(Real voxelSize, Real halfWidth)
1598 {
1599  using ValueType = typename GridType::ValueType;
1600 
1601  // GridType::ValueType is required to be a floating-point scalar.
1603  "level-set grids must be floating-point-valued");
1604 
1605  typename GridType::Ptr grid = GridType::create(
1606  /*background=*/static_cast<ValueType>(voxelSize * halfWidth));
1607  grid->setTransform(math::Transform::createLinearTransform(voxelSize));
1608  grid->setGridClass(GRID_LEVEL_SET);
1609  return grid;
1610 }
1611 
1612 } // namespace OPENVDB_VERSION_NAME
1613 } // namespace openvdb
1614 
1615 #endif // OPENVDB_GRID_HAS_BEEN_INCLUDED
1616 
1617 // Copyright (c) 2012-2017 DreamWorks Animation LLC
1618 // All rights reserved. This software is distributed under the
1619 // 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:161
typename _TreeType::ValueType ValueType
Definition: Grid.h:507
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:1083
typename tree::ValueAccessor< const TreeType > ConstAccessorType
Definition: Grid.h:981
typename tree::ValueAccessor< TreeType > AccessorType
Definition: Grid.h:980
static const TreeType & tree(const TreeType &t)
Definition: Grid.h:956
ConstUnsafeAccessor getConstUnsafeAccessor() const
Return an accessor that provides random read-only access to this grid's voxels. The accessor is unsaf...
Definition: Grid.h:658
Definition: ImfName.h:53
GridPtrSet::iterator GridPtrSetIter
Definition: Grid.h:447
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:1572
Vec3d indexToWorld(const Coord &ijk) const
Apply this grid's transform to the given coordinates.
Definition: Grid.h:367
Vec3d worldToIndex(const Vec3d &xyz) const
Apply the inverse of this grid's transform to the given coordinates.
Definition: Grid.h:370
SharedPtr< GridBase > Ptr
Definition: Grid.h:106
Tag dispatch class that distinguishes shallow copy constructors from deep copy constructors.
Definition: Types.h:500
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:401
SharedPtr< const Grid > ConstPtr
Definition: Grid.h:502
ValueAllIter beginValueAll()
Return an iterator over all of this grid's values (tile and voxel).
Definition: Grid.h:674
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:1384
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:620
void readTransform(std::istream &is)
Read in the transform for this grid.
Definition: Grid.h:399
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:292
typename tree::ValueAccessor< TreeType > AccessorType
Definition: Grid.h:950
typename TreeType::ConstPtr ConstTreePtrType
Definition: Grid.h:942
static const TreeType & constTree(TreeType &t)
Definition: Grid.h:988
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:662
Vec3d voxelSize(const Vec3d &xyz) const
Return the size of this grid's voxel at position (x, y, z).
Definition: Grid.h:361
typename _TreeType::ValueOffCIter ValueOffCIter
Definition: Grid.h:513
SharedPtr< GridCPtrVec > GridCPtrVecPtr
Definition: Grid.h:444
SharedPtr< StreamMetadata > Ptr
Definition: io.h:59
SharedPtr< GridPtrSet > GridPtrSetPtr
Definition: Grid.h:449
static const TreeType & constTree(const GridType &g)
Definition: Grid.h:991
ValueConverter<T>::Type is the type of a grid having the same hierarchy as this grid but a different ...
Definition: Grid.h:529
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:1265
GLboolean GLboolean g
Definition: glcorearb.h:1221
static const char *const META_SAVE_HALF_FLOAT
Definition: Grid.h:293
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:1393
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:949
typename tree::ValueAccessor< const TreeType > ConstAccessorType
Definition: Grid.h:951
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:454
GLboolean GLboolean GLboolean GLboolean a
Definition: glcorearb.h:1221
GridBase::Ptr deepCopyGrid() const override
Return a new grid whose metadata, transform and tree are deep copies of this grid's.
Definition: Grid.h:607
GridCPtrSet::const_iterator GridCPtrSetCIter
Definition: Grid.h:453
bool operator()(const GridBase::ConstPtr &g) const
Definition: Grid.h:461
typename tree::ValueAccessor< _TreeType, false > UnsafeAccessor
Definition: Grid.h:519
static const char *const META_FILE_COMPRESSION
Definition: Grid.h:298
static const char *const META_IS_LOCAL_SPACE
Definition: Grid.h:294
typename tree::ValueAccessor< NonConstTreeType > NonConstAccessorType
Definition: Grid.h:982
static const char *const META_GRID_CLASS
Definition: Grid.h:290
tree::TreeBase TreeBase
Definition: Grid.h:52
typename tree::ValueAccessor< const TreeType > ConstAccessorType
Definition: Grid.h:1010
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:1257
ConstAccessor getConstAccessor() const
Return an accessor that provides random read-only access to this grid's voxels.
Definition: Grid.h:648
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:1099
Container that maps names (strings) to values of arbitrary types.
Definition: MetaMap.h:46
std::vector< GridBase::Ptr > GridPtrVec
Definition: Grid.h:436
uint64 value_type
Definition: GA_PrimCompat.h:29
typename std::remove_const< TreeType >::type NonConstTreeType
Definition: Grid.h:999
std::set< GridBase::Ptr > GridPtrSet
Definition: Grid.h:446
typename _TreeType::ConstPtr ConstTreePtrType
Definition: Grid.h:506
This adapter allows code that is templated on a Tree type to accept either a Tree type or a Grid type...
Definition: Grid.h:937
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:1275
typename tree::ValueAccessor< NonConstTreeType > NonConstAccessorType
Definition: Grid.h:952
GridCPtrSet::iterator GridCPtrSetIter
Definition: Grid.h:452
static const char *const META_FILE_VOXEL_COUNT
Definition: Grid.h:300
ValueOffCIter cbeginValueOff() const
Return an iterator over all of this grid's inactive values (tile and voxel).
Definition: Grid.h:670
std::shared_ptr< T > SharedPtr
Definition: Types.h:130
static const TreeType & constTree(const TreeType &t)
Definition: Grid.h:990
static const TreeType & tree(const GridType &g)
Definition: Grid.h:987
Abstract base class for typed grids.
Definition: Grid.h:103
void clip(const CoordBBox &) override
Clip this grid to the given index-space bounding box.
Definition: Grid.h:1357
Vec3d indexToWorld(const Vec3d &xyz) const
Apply this grid's transform to the given coordinates.
Definition: Grid.h:366
static const char *const META_FILE_BBOX_MIN
Definition: Grid.h:296
typename std::remove_const< TreeType >::type NonConstTreeType
Definition: Grid.h:970
ValueOffIter beginValueOff()
Return an iterator over all of this grid's inactive values (tile and voxel).
Definition: Grid.h:668
bool isType() const
Return true if this grid is of the same type as the template parameter.
Definition: Grid.h:156
#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:1446
static void unregisterGrid()
Remove this grid type from the registry.
Definition: Grid.h:857
Calculate an axis-aligned bounding box in index space from a bounding sphere in world space...
Definition: Transform.h:66
typename _TreeType::ValueAllCIter ValueAllCIter
Definition: Grid.h:515
static Ptr create()
Return a new grid with background value zero.
Definition: Grid.h:1180
static void registerGrid()
Register this grid type along with a factory function.
Definition: Grid.h:855
#define OPENVDB_VERSION_NAME
Definition: version.h:43
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:1597
GridBase(GridBase &other, ShallowCopy)
Copy another grid's metadata but share its transform.
Definition: Grid.h:419
typename tree::ValueAccessor< const _TreeType, false > ConstUnsafeAccessor
Definition: Grid.h:520
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:338
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:1290
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:635
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:1327
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:946
CoordBBox evalActiveVoxelBoundingBox() const override
Return the axis-aligned bounding box of all active voxels.
Definition: Grid.h:1412
typename _TreeType::ValueOffIter ValueOffIter
Definition: Grid.h:512
typename NonConstGridType::Ptr NonConstGridPtrType
Definition: Grid.h:947
Name type() const override
Return the name of this grid's type.
Definition: Grid.h:611
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:880
const TreeBase & constBaseTree() const
Return a reference to this grid's tree, which might be shared with other grids.
Definition: Grid.h:186
ValueOffCIter beginValueOff() const
Return an iterator over all of this grid's inactive values (tile and voxel).
Definition: Grid.h:669
static TreeType & tree(TreeType &t)
Definition: Grid.h:954
GridBase()
Initialize with an identity linear transform.
Definition: Grid.h:409
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:941
math::Transform::Ptr transformPtr()
Return a pointer to this grid's transform, which might be shared with other grids.
Definition: Grid.h:337
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:175
math::Transform & transform()
Return a reference to this grid's transform, which might be shared with other grids.
Definition: Grid.h:346
Container class that associates a tree with a transform and metadata.
Definition: Grid.h:54
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:1375
SharedPtr< GridPtrVec > GridPtrVecPtr
Definition: Grid.h:439
typename _TreeType::ValueOnIter ValueOnIter
Definition: Grid.h:510
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:1499
GridPtrSet::const_iterator GridPtrSetCIter
Definition: Grid.h:448
ValueAllCIter cbeginValueAll() const
Return an iterator over all of this grid's values (tile and voxel).
Definition: Grid.h:676
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:1061
const ValueType & background() const
Return this grid's background value.
Definition: Grid.h:625
static const TreeType & constTree(GridType &g)
Definition: Grid.h:989
Base class for typed trees.
Definition: Tree.h:64
static const TreeType & tree(const TreeType &t)
Definition: Grid.h:986
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:913
typename std::remove_const< TreeType >::type NonConstTreeType
Definition: Grid.h:940
static bool hasMultiPassIO()
Return true if grids of this type require multiple I/O passes to read and write data buffers...
Definition: Grid.h:1537
Predicate functor that returns true for grids that have a specified name.
Definition: Grid.h:458
GridCPtrVec::iterator GridCPtrVecIter
Definition: Grid.h:442
void print(std::ostream &=std::cout, int verboseLevel=1) const override
Output a human-readable description of this grid.
Definition: Grid.h:1545
GLsizei const GLfloat * value
Definition: glcorearb.h:823
static const char *const META_FILE_MEM_BYTES
Definition: Grid.h:299
void denseFill(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
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:1335
TreeBase & baseTree()
Return a reference to this grid's tree, which might be shared with other grids.
Definition: Grid.h:184
Coord evalActiveVoxelDim() const override
Return the dimensions of the axis-aligned bounding box of all active voxels.
Definition: Grid.h:1422
bool hasUniformVoxels() const
Return true if the voxels in world space are uniformly sized cubes.
Definition: Grid.h:363
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:517
typename _TreeType::ValueOnCIter ValueOnCIter
Definition: Grid.h:511
void evalMinMax(ValueType &minVal, ValueType &maxVal) const
Return the minimum and maximum active values in this grid.
Definition: Grid.h:1404
GridPtrVec::iterator GridPtrVecIter
Definition: Grid.h:437
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:675
static const TreeType & constTree(const TreeType &t)
Definition: Grid.h:960
math::Transform::ConstPtr constTransformPtr() const
Return a pointer to this grid's transform, which might be shared with other grids.
Definition: Grid.h:339
void clear() override
Empty this grid, so that all voxels become inactive background voxels.
Definition: Grid.h:630
ConstAccessor getAccessor() const
Return an accessor that provides random read-only access to this grid's voxels.
Definition: Grid.h:647
SharedPtr< const GridBase > ConstPtr
Definition: Grid.h:107
void pruneGrid(float tolerance=0.0) override
Reduce the memory footprint of this grid by increasing its sparseness.
Definition: Grid.h:1349
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:644
SharedPtr< const TreeBase > ConstPtr
Definition: Tree.h:68
static const char *const META_GRID_CREATOR
Definition: Grid.h:291
void writeBuffers(std::ostream &) const override
Write out all data buffers for this grid.
Definition: Grid.h:1509
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:896
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:663
Vec3d voxelSize() const
Return the size of this grid's voxels.
Definition: Grid.h:358
static const TreeType & tree(const GridType &g)
Definition: Grid.h:957
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:628
static const TreeType & constTree(const GridType &g)
Definition: Grid.h:961
GridBase(const GridBase &other)
Deep copy another grid's metadata and transform.
Definition: Grid.h:412
Metafunction that specifies whether a given leaf node, tree, or grid type requires multiple passes to...
Definition: Grid.h:1037
const math::Transform & transform() const
Return a reference to this grid's transform, which might be shared with other grids.
Definition: Grid.h:347
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:468
typename _TreeType::BuildType BuildType
Definition: Grid.h:508
typename NonConstTreeType::Ptr NonConstTreePtrType
Definition: Grid.h:943
static const char *const META_VECTOR_TYPE
Definition: Grid.h:295
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h:71
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:1117
void readBuffers(std::istream &) override
Read all data buffers for this grid.
Definition: Grid.h:1454
void merge(Grid &other, MergePolicy policy=MERGE_ACTIVE_STATES)
Efficiently merge another grid into this grid using one of several schemes.
Definition: Grid.h:1366
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:451
void setTransform(math::Transform::Ptr)
Associate the given transform with this grid, in place of its existing transform. ...
Definition: Grid.h:1106
const math::Transform & constTransform() const
Return a reference to this grid's transform, which might be shared with other grids.
Definition: Grid.h:348
typename tree::ValueAccessor< const _TreeType, true > ConstAccessor
Definition: Grid.h:518
std::vector< GridBase::ConstPtr > GridCPtrVec
Definition: Grid.h:441
typename _TreeType::ValueAllIter ValueAllIter
Definition: Grid.h:514
static const TreeType & constTree(TreeType &t)
Definition: Grid.h:958
static const TreeType & constTree(GridType &g)
Definition: Grid.h:959
static Name gridType()
Return the name of this type of grid.
Definition: Grid.h:613
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:606
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:1438
GridCPtrVec::const_iterator GridCPtrVecCIter
Definition: Grid.h:443
void newTree() override
Associate a new, empty tree with this grid, in place of its existing tree.
Definition: Grid.h:1316
ValueOnCIter cbeginValueOn() const
Return an iterator over all of this grid's active values (tile and voxel).
Definition: Grid.h:664
static TreeType & tree(GridType &g)
Definition: Grid.h:955
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:297
GridPtrVec::const_iterator GridPtrVecCIter
Definition: Grid.h:438
#define OPENVDB_THROW(exception, message)
Definition: Exceptions.h:101
typename GridType::ConstPtr ConstGridPtrType
Definition: Grid.h:948
const TreeBase & baseTree() const
Return a reference to this grid's tree, which might be shared with other grids.
Definition: Grid.h:185
typename _TreeType::Ptr TreePtrType
Definition: Grid.h:505
Index64 activeVoxelCount() const override
Return the number of active voxels.
Definition: Grid.h:771
typename tree::ValueAccessor< NonConstTreeType > NonConstAccessorType
Definition: Grid.h:1011
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:1303