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