OpenVDB  5.2.0
Grid.h
Go to the documentation of this file.
1 //
3 // Copyright (c) 2012-2018 DreamWorks Animation LLC
4 //
5 // All rights reserved. This software is distributed under the
6 // Mozilla Public License 2.0 ( http://www.mozilla.org/MPL/2.0/ )
7 //
8 // Redistributions of source code must retain the above copyright
9 // and license notice and the following restrictions and disclaimer.
10 //
11 // * Neither the name of DreamWorks Animation nor the names of
12 // its contributors may be used to endorse or promote products derived
13 // from this software without specific prior written permission.
14 //
15 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
18 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
19 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY INDIRECT, INCIDENTAL,
20 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
21 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 // IN NO EVENT SHALL THE COPYRIGHT HOLDERS' AND CONTRIBUTORS' AGGREGATE
27 // LIABILITY FOR ALL CLAIMS REGARDLESS OF THEIR BASIS EXCEED US$250.00.
28 //
30 
31 #ifndef OPENVDB_GRID_HAS_BEEN_INCLUDED
32 #define OPENVDB_GRID_HAS_BEEN_INCLUDED
33 
34 #include "Exceptions.h"
35 #include "MetaMap.h"
36 #include "Types.h"
37 #include "io/io.h"
38 #include "math/Transform.h"
39 #include "tree/Tree.h"
40 #include "util/logging.h"
41 #include "util/Name.h"
42 #include <cassert>
43 #include <iostream>
44 #include <set>
45 #include <type_traits>
46 #include <vector>
47 
48 
49 namespace openvdb {
51 namespace OPENVDB_VERSION_NAME {
52 
54 
55 template<typename> class Grid; // forward declaration
56 
57 
62 template<typename GridType>
63 inline typename GridType::Ptr createGrid(const typename GridType::ValueType& background);
64 
65 
69 template<typename GridType>
70 inline typename GridType::Ptr createGrid();
71 
72 
77 template<typename TreePtrType>
79 
80 
95 template<typename GridType>
96 typename GridType::Ptr createLevelSet(
97  Real voxelSize = 1.0, Real halfWidth = LEVEL_SET_HALF_WIDTH);
98 
99 
101 
102 
105 {
106 public:
109 
110  using GridFactory = Ptr (*)();
111 
112 
113  ~GridBase() override {}
114 
115 #if OPENVDB_ABI_VERSION_NUMBER <= 3
116  virtual GridBase::Ptr copyGrid(CopyPolicy treePolicy = CP_SHARE) const = 0;
119 #else
120 
121  virtual GridBase::Ptr copyGrid() = 0;
124  virtual GridBase::ConstPtr copyGrid() const = 0;
126  virtual GridBase::Ptr copyGridWithNewTree() const = 0;
129 #endif
130  virtual GridBase::Ptr deepCopyGrid() const = 0;
132 
133 
134  //
135  // Registry methods
136  //
138  static Ptr createGrid(const Name& type);
139 
141  static bool isRegistered(const Name &type);
142 
144  static void clearRegistry();
145 
146 
147  //
148  // Grid type methods
149  //
151  virtual Name type() const = 0;
153  virtual Name valueType() const = 0;
154 
156  template<typename GridType>
157  bool isType() const { return (this->type() == GridType::gridType()); }
158 
160  template<typename GridType>
163  static typename GridType::Ptr grid(const GridBase::Ptr&);
164  template<typename GridType>
165  static typename GridType::ConstPtr grid(const GridBase::ConstPtr&);
166  template<typename GridType>
167  static typename GridType::ConstPtr constGrid(const GridBase::Ptr&);
168  template<typename GridType>
169  static typename GridType::ConstPtr constGrid(const GridBase::ConstPtr&);
171 
173  TreeBase::Ptr baseTreePtr();
176  TreeBase::ConstPtr baseTreePtr() const { return this->constBaseTreePtr(); }
177  virtual TreeBase::ConstPtr constBaseTreePtr() const = 0;
179 
181  TreeBase& baseTree() { return const_cast<TreeBase&>(this->constBaseTree()); }
186  const TreeBase& baseTree() const { return this->constBaseTree(); }
187  const TreeBase& constBaseTree() const { return *(this->constBaseTreePtr()); }
189 
195  virtual void setTree(TreeBase::Ptr) = 0;
196 
198  virtual void newTree() = 0;
199 
201  virtual bool empty() const = 0;
203  virtual void clear() = 0;
204 
210  virtual void pruneGrid(float tolerance = 0.0) = 0;
211 
212 #if OPENVDB_ABI_VERSION_NUMBER >= 3
213  void clipGrid(const BBoxd&);
218 
223  virtual void clip(const CoordBBox&) = 0;
224 #endif
225 
226 
227  //
228  // Metadata
229  //
231  std::string getName() const;
233  void setName(const std::string&);
234 
236  std::string getCreator() const;
238  void setCreator(const std::string&);
239 
242  bool saveFloatAsHalf() const;
243  void setSaveFloatAsHalf(bool);
244 
246  GridClass getGridClass() const;
248  void setGridClass(GridClass);
250  void clearGridClass();
251 
253  static std::string gridClassToString(GridClass);
255  static std::string gridClassToMenuName(GridClass);
259  static GridClass stringToGridClass(const std::string&);
260 
263  VecType getVectorType() const;
266  void setVectorType(VecType);
268  void clearVectorType();
269 
271  static std::string vecTypeToString(VecType);
274  static std::string vecTypeExamples(VecType);
277  static std::string vecTypeDescription(VecType);
278  static VecType stringToVecType(const std::string&);
279 
283  bool isInWorldSpace() const;
285  void setIsInWorldSpace(bool);
286 
287  // Standard metadata field names
288  // (These fields should normally not be accessed directly, but rather
289  // via the accessor methods above, when available.)
290  // Note: Visual C++ requires these declarations to be separate statements.
291  static const char* const META_GRID_CLASS;
292  static const char* const META_GRID_CREATOR;
293  static const char* const META_GRID_NAME;
294  static const char* const META_SAVE_HALF_FLOAT;
295  static const char* const META_IS_LOCAL_SPACE;
296  static const char* const META_VECTOR_TYPE;
297  static const char* const META_FILE_BBOX_MIN;
298  static const char* const META_FILE_BBOX_MAX;
299  static const char* const META_FILE_COMPRESSION;
300  static const char* const META_FILE_MEM_BYTES;
301  static const char* const META_FILE_VOXEL_COUNT;
302 
303 
304  //
305  // Statistics
306  //
308  virtual Index64 activeVoxelCount() const = 0;
309 
312  virtual CoordBBox evalActiveVoxelBoundingBox() const = 0;
313 
315  virtual Coord evalActiveVoxelDim() const = 0;
316 
318  virtual Index64 memUsage() const = 0;
319 
324  void addStatsMetadata();
329  MetaMap::Ptr getStatsMetadata() const;
330 
331 
332  //
333  // Transform methods
334  //
336  math::Transform::Ptr transformPtr() { return mTransform; }
339  math::Transform::ConstPtr transformPtr() const { return mTransform; }
340  math::Transform::ConstPtr constTransformPtr() const { return mTransform; }
342 
343  math::Transform& transform() { return *mTransform; }
348  const math::Transform& transform() const { return *mTransform; }
349  const math::Transform& constTransform() const { return *mTransform; }
351  void setTransform(math::Transform::Ptr);
357 
359  Vec3d voxelSize() const { return transform().voxelSize(); }
362  Vec3d voxelSize(const Vec3d& xyz) const { return transform().voxelSize(xyz); }
364  bool hasUniformVoxels() const { return mTransform->hasUniformScale(); }
366  Vec3d indexToWorld(const Vec3d& xyz) const { return transform().indexToWorld(xyz); }
368  Vec3d indexToWorld(const Coord& ijk) const { return transform().indexToWorld(ijk); }
370  Vec3d worldToIndex(const Vec3d& xyz) const { return transform().worldToIndex(xyz); }
372 
373 
374  //
375  // I/O methods
376  //
379  virtual void readTopology(std::istream&) = 0;
382  virtual void writeTopology(std::ostream&) const = 0;
383 
385  virtual void readBuffers(std::istream&) = 0;
386 #if OPENVDB_ABI_VERSION_NUMBER >= 3
387  virtual void readBuffers(std::istream&, const CoordBBox&) = 0;
394  virtual void readNonresidentBuffers() const = 0;
395 #endif
396  virtual void writeBuffers(std::ostream&) const = 0;
398 
400  void readTransform(std::istream& is) { transform().read(is); }
402  void writeTransform(std::ostream& os) const { transform().write(os); }
403 
405  virtual void print(std::ostream& = std::cout, int verboseLevel = 1) const = 0;
406 
407 
408 protected:
410  GridBase(): mTransform(math::Transform::createLinearTransform()) {}
411 
413  GridBase(const GridBase& other): MetaMap(other), mTransform(other.mTransform->copy()) {}
414 
415 #if OPENVDB_ABI_VERSION_NUMBER <= 3
416  GridBase(const GridBase& other, ShallowCopy): MetaMap(other), mTransform(other.mTransform) {}
418 #else
419  GridBase(GridBase& other, ShallowCopy): MetaMap(other), mTransform(other.mTransform) {}
421 #endif
422 
424  static void registerGrid(const Name& type, GridFactory);
426  static void unregisterGrid(const Name& type);
427 
428 
429 private:
430  math::Transform::Ptr mTransform;
431 }; // class GridBase
432 
433 
435 
436 
437 using GridPtrVec = std::vector<GridBase::Ptr>;
438 using GridPtrVecIter = GridPtrVec::iterator;
439 using GridPtrVecCIter = GridPtrVec::const_iterator;
441 
442 using GridCPtrVec = std::vector<GridBase::ConstPtr>;
443 using GridCPtrVecIter = GridCPtrVec::iterator;
444 using GridCPtrVecCIter = GridCPtrVec::const_iterator;
446 
447 using GridPtrSet = std::set<GridBase::Ptr>;
448 using GridPtrSetIter = GridPtrSet::iterator;
449 using GridPtrSetCIter = GridPtrSet::const_iterator;
451 
452 using GridCPtrSet = std::set<GridBase::ConstPtr>;
453 using GridCPtrSetIter = GridCPtrSet::iterator;
454 using GridCPtrSetCIter = GridCPtrSet::const_iterator;
456 
457 
460 {
461  GridNamePred(const Name& _name): name(_name) {}
462  bool operator()(const GridBase::ConstPtr& g) const { return g && g->getName() == name; }
464 };
465 
467 template<typename GridPtrContainerT>
468 inline typename GridPtrContainerT::value_type
469 findGridByName(const GridPtrContainerT& container, const Name& name)
470 {
471  using GridPtrT = typename GridPtrContainerT::value_type;
472  typename GridPtrContainerT::const_iterator it =
473  std::find_if(container.begin(), container.end(), GridNamePred(name));
474  return (it == container.end() ? GridPtrT() : *it);
475 }
476 
478 template<typename KeyT, typename GridPtrT>
479 inline GridPtrT
480 findGridByName(const std::map<KeyT, GridPtrT>& container, const Name& name)
481 {
482  using GridPtrMapT = std::map<KeyT, GridPtrT>;
483  for (typename GridPtrMapT::const_iterator it = container.begin(), end = container.end();
484  it != end; ++it)
485  {
486  const GridPtrT& grid = it->second;
487  if (grid && grid->getName() == name) return grid;
488  }
489  return GridPtrT();
490 }
492 
493 
495 
496 
498 template<typename _TreeType>
499 class Grid: public GridBase
500 {
501 public:
504 
505  using TreeType = _TreeType;
506  using TreePtrType = typename _TreeType::Ptr;
507  using ConstTreePtrType = typename _TreeType::ConstPtr;
508  using ValueType = typename _TreeType::ValueType;
509  using BuildType = typename _TreeType::BuildType;
510 
511  using ValueOnIter = typename _TreeType::ValueOnIter;
512  using ValueOnCIter = typename _TreeType::ValueOnCIter;
513  using ValueOffIter = typename _TreeType::ValueOffIter;
514  using ValueOffCIter = typename _TreeType::ValueOffCIter;
515  using ValueAllIter = typename _TreeType::ValueAllIter;
516  using ValueAllCIter = typename _TreeType::ValueAllCIter;
517 
522 
529  template<typename OtherValueType>
530  struct ValueConverter {
532  };
533 
535  static Ptr create(const ValueType& background);
537  static Ptr create();
540  static Ptr create(TreePtrType);
543  static Ptr create(const GridBase& other);
544 
545 
547  Grid();
549  explicit Grid(const ValueType& background);
553  explicit Grid(TreePtrType);
555  Grid(const Grid&);
561  template<typename OtherTreeType>
562  explicit Grid(const Grid<OtherTreeType>&);
563 #if OPENVDB_ABI_VERSION_NUMBER <= 3
564  Grid(const Grid&, ShallowCopy);
566 #else
567  Grid(Grid&, ShallowCopy);
569 #endif
570  explicit Grid(const GridBase&);
573 
574  ~Grid() override {}
575 
577  Grid& operator=(const Grid&) = delete;
578 
579 
580 #if OPENVDB_ABI_VERSION_NUMBER <= 3
581 
582  Ptr copy(CopyPolicy treePolicy = CP_SHARE) const;
589  GridBase::Ptr copyGrid(CopyPolicy treePolicy = CP_SHARE) const override;
591 #else
592 
593  Ptr copy();
596  ConstPtr copy() const;
598  Ptr copyWithNewTree() const;
601 
602  GridBase::Ptr copyGrid() override;
603  GridBase::ConstPtr copyGrid() const override;
604  GridBase::Ptr copyGridWithNewTree() const override;
605 #endif
606 
607  Ptr deepCopy() const { return Ptr(new Grid(*this)); }
609  GridBase::Ptr deepCopyGrid() const override { return this->deepCopy(); }
611 
613  Name type() const override { return this->gridType(); }
615  static Name gridType() { return TreeType::treeType(); }
616 
617 
618  //
619  // Voxel access methods
620  //
622  Name valueType() const override { return tree().valueType(); }
623 
627  const ValueType& background() const { return mTree->background(); }
628 
630  bool empty() const override { return tree().empty(); }
632  void clear() override { tree().clear(); }
633 
637  Accessor getAccessor() { return Accessor(tree()); }
648  ConstAccessor getAccessor() const { return ConstAccessor(tree()); }
650  ConstAccessor getConstAccessor() const { return ConstAccessor(tree()); }
652  ConstUnsafeAccessor getConstUnsafeAccessor() const { return ConstUnsafeAccessor(tree()); }
661 
663  ValueOnIter beginValueOn() { return tree().beginValueOn(); }
665  ValueOnCIter beginValueOn() const { return tree().cbeginValueOn(); }
666  ValueOnCIter cbeginValueOn() const { return tree().cbeginValueOn(); }
668 
669  ValueOffIter beginValueOff() { return tree().beginValueOff(); }
671  ValueOffCIter beginValueOff() const { return tree().cbeginValueOff(); }
672  ValueOffCIter cbeginValueOff() const { return tree().cbeginValueOff(); }
674 
675  ValueAllIter beginValueAll() { return tree().beginValueAll(); }
677  ValueAllCIter beginValueAll() const { return tree().cbeginValueAll(); }
678  ValueAllCIter cbeginValueAll() const { return tree().cbeginValueAll(); }
680 
682  void evalMinMax(ValueType& minVal, ValueType& maxVal) const;
683 
685  void sparseFill(const CoordBBox& bbox, const ValueType& value, bool active = true);
694  void fill(const CoordBBox& bbox, const ValueType& value, bool active = true);
696 
703  void denseFill(const CoordBBox& bbox, const ValueType& value, bool active = true);
704 
706  void pruneGrid(float tolerance = 0.0) override;
707 
708 #if OPENVDB_ABI_VERSION_NUMBER >= 3
709  void clip(const CoordBBox&) override;
714 #endif
715 
721  void merge(Grid& other, MergePolicy policy = MERGE_ACTIVE_STATES);
722 
736  template<typename OtherTreeType>
737  void topologyUnion(const Grid<OtherTreeType>& other);
738 
751  template<typename OtherTreeType>
752  void topologyIntersection(const Grid<OtherTreeType>& other);
753 
764  template<typename OtherTreeType>
765  void topologyDifference(const Grid<OtherTreeType>& other);
766 
767  //
768  // Statistics
769  //
771  Index64 activeVoxelCount() const override { return tree().activeVoxelCount(); }
773  CoordBBox evalActiveVoxelBoundingBox() const override;
775  Coord evalActiveVoxelDim() const override;
776 
779  Index64 memUsage() const override { return tree().memUsage(); }
780 
781 
782  //
783  // Tree methods
784  //
786  TreePtrType treePtr() { return mTree; }
789  ConstTreePtrType treePtr() const { return mTree; }
790  ConstTreePtrType constTreePtr() const { return mTree; }
791  TreeBase::ConstPtr constBaseTreePtr() const override { return mTree; }
793 
794  TreeType& tree() { return *mTree; }
799  const TreeType& tree() const { return *mTree; }
800  const TreeType& constTree() const { return *mTree; }
802 
808  void setTree(TreeBase::Ptr) override;
809 
812  void newTree() override;
813 
814 
815  //
816  // I/O methods
817  //
820  void readTopology(std::istream&) override;
823  void writeTopology(std::ostream&) const override;
824 
826  void readBuffers(std::istream&) override;
827 #if OPENVDB_ABI_VERSION_NUMBER >= 3
828  void readBuffers(std::istream&, const CoordBBox&) override;
835  void readNonresidentBuffers() const override;
836 #endif
837  void writeBuffers(std::ostream&) const override;
839 
841  void print(std::ostream& = std::cout, int verboseLevel = 1) const override;
842 
846  static inline bool hasMultiPassIO();
847 
848 
849  //
850  // Registry methods
851  //
855  static void registerGrid()
856  {
857  GridBase::registerGrid(Grid::gridType(), Grid::factory);
859  OPENVDB_LOG_WARN("delayed loading of grids of type " << Grid::gridType()
860  << " might not be threadsafe on this platform");
861  }
862  }
865 
866 
867 private:
869  static GridBase::Ptr factory() { return Grid::create(); }
870 
871  TreePtrType mTree;
872 }; // class Grid
873 
874 
876 
877 
885 template<typename GridType>
886 inline typename GridType::Ptr
888 {
889  return GridBase::grid<GridType>(grid);
890 }
891 
892 
901 template<typename GridType>
902 inline typename GridType::ConstPtr
904 {
905  return GridBase::constGrid<GridType>(grid);
906 }
907 
908 
910 
911 
918 template<typename GridType>
919 inline typename GridType::Ptr
921 {
922  if (!grid || !grid->isType<GridType>()) return typename GridType::Ptr();
923  return gridPtrCast<GridType>(grid->deepCopyGrid());
924 }
925 
926 
927 template<typename GridType>
928 inline typename GridType::Ptr
930 {
931  if (!grid.isType<GridType>()) return typename GridType::Ptr();
932  return gridPtrCast<GridType>(grid.deepCopyGrid());
933 }
935 
936 
938 
939 
941 template<typename _TreeType>
945 {
946  using TreeType = _TreeType;
947  using NonConstTreeType = typename std::remove_const<TreeType>::type;
948  using TreePtrType = typename TreeType::Ptr;
949  using ConstTreePtrType = typename TreeType::ConstPtr;
950  using NonConstTreePtrType = typename NonConstTreeType::Ptr;
953  using GridPtrType = typename GridType::Ptr;
956  using ValueType = typename TreeType::ValueType;
960 
961  static TreeType& tree(TreeType& t) { return t; }
962  static TreeType& tree(GridType& g) { return g.tree(); }
963  static const TreeType& tree(const TreeType& t) { return t; }
964  static const TreeType& tree(const GridType& g) { return g.tree(); }
965  static const TreeType& constTree(TreeType& t) { return t; }
966  static const TreeType& constTree(GridType& g) { return g.constTree(); }
967  static const TreeType& constTree(const TreeType& t) { return t; }
968  static const TreeType& constTree(const GridType& g) { return g.constTree(); }
969 };
970 
971 
973 template<typename _TreeType>
974 struct TreeAdapter<Grid<_TreeType> >
975 {
976  using TreeType = _TreeType;
977  using NonConstTreeType = typename std::remove_const<TreeType>::type;
978  using TreePtrType = typename TreeType::Ptr;
979  using ConstTreePtrType = typename TreeType::ConstPtr;
980  using NonConstTreePtrType = typename NonConstTreeType::Ptr;
983  using GridPtrType = typename GridType::Ptr;
986  using ValueType = typename TreeType::ValueType;
990 
991  static TreeType& tree(TreeType& t) { return t; }
992  static TreeType& tree(GridType& g) { return g.tree(); }
993  static const TreeType& tree(const TreeType& t) { return t; }
994  static const TreeType& tree(const GridType& g) { return g.tree(); }
995  static const TreeType& constTree(TreeType& t) { return t; }
996  static const TreeType& constTree(GridType& g) { return g.constTree(); }
997  static const TreeType& constTree(const TreeType& t) { return t; }
998  static const TreeType& constTree(const GridType& g) { return g.constTree(); }
999 };
1000 
1002 template<typename _TreeType>
1003 struct TreeAdapter<tree::ValueAccessor<_TreeType> >
1004 {
1005  using TreeType = _TreeType;
1006  using NonConstTreeType = typename std::remove_const<TreeType>::type;
1007  using TreePtrType = typename TreeType::Ptr;
1008  using ConstTreePtrType = typename TreeType::ConstPtr;
1009  using NonConstTreePtrType = typename NonConstTreeType::Ptr;
1012  using GridPtrType = typename GridType::Ptr;
1015  using ValueType = typename TreeType::ValueType;
1019 
1020  static TreeType& tree(TreeType& t) { return t; }
1021  static TreeType& tree(GridType& g) { return g.tree(); }
1022  static TreeType& tree(AccessorType& a) { return a.tree(); }
1023  static const TreeType& tree(const TreeType& t) { return t; }
1024  static const TreeType& tree(const GridType& g) { return g.tree(); }
1025  static const TreeType& tree(const AccessorType& a) { return a.tree(); }
1026  static const TreeType& constTree(TreeType& t) { return t; }
1027  static const TreeType& constTree(GridType& g) { return g.constTree(); }
1028  static const TreeType& constTree(const TreeType& t) { return t; }
1029  static const TreeType& constTree(const GridType& g) { return g.constTree(); }
1030 };
1031 
1033 
1034 
1036 
1037 
1043 template<typename LeafNodeType>
1045  static const bool value = std::is_base_of<io::MultiPass, LeafNodeType>::value;
1046 };
1047 
1048 // Partial specialization for Tree types
1049 template<typename RootNodeType>
1050 struct HasMultiPassIO<tree::Tree<RootNodeType>> {
1051  // A tree is multi-pass if its (root node's) leaf node type is multi-pass.
1053 };
1054 
1055 // Partial specialization for Grid types
1056 template<typename TreeType>
1057 struct HasMultiPassIO<Grid<TreeType>> {
1058  // A grid is multi-pass if its tree's leaf node type is multi-pass.
1060 };
1061 
1062 
1064 
1065 
1066 template<typename GridType>
1067 inline typename GridType::Ptr
1069 {
1070  // The string comparison on type names is slower than a dynamic pointer cast, but
1071  // it is safer when pointers cross DSO boundaries, as they do in many Houdini nodes.
1072  if (grid && grid->type() == GridType::gridType()) {
1073  return StaticPtrCast<GridType>(grid);
1074  }
1075  return typename GridType::Ptr();
1076 }
1077 
1078 
1079 template<typename GridType>
1080 inline typename GridType::ConstPtr
1082 {
1083  return ConstPtrCast<const GridType>(
1084  GridBase::grid<GridType>(ConstPtrCast<GridBase>(grid)));
1085 }
1086 
1087 
1088 template<typename GridType>
1089 inline typename GridType::ConstPtr
1091 {
1092  return ConstPtrCast<const GridType>(GridBase::grid<GridType>(grid));
1093 }
1094 
1095 
1096 template<typename GridType>
1097 inline typename GridType::ConstPtr
1099 {
1100  return ConstPtrCast<const GridType>(
1101  GridBase::grid<GridType>(ConstPtrCast<GridBase>(grid)));
1102 }
1103 
1104 
1105 inline TreeBase::Ptr
1107 {
1108  return ConstPtrCast<TreeBase>(this->constBaseTreePtr());
1109 }
1110 
1111 
1112 inline void
1114 {
1115  if (!xform) OPENVDB_THROW(ValueError, "Transform pointer is null");
1116  mTransform = xform;
1117 }
1118 
1119 
1121 
1122 
1123 template<typename TreeT>
1124 inline Grid<TreeT>::Grid(): mTree(new TreeType)
1125 {
1126 }
1127 
1128 
1129 template<typename TreeT>
1130 inline Grid<TreeT>::Grid(const ValueType &background): mTree(new TreeType(background))
1131 {
1132 }
1133 
1134 
1135 template<typename TreeT>
1136 inline Grid<TreeT>::Grid(TreePtrType tree): mTree(tree)
1137 {
1138  if (!tree) OPENVDB_THROW(ValueError, "Tree pointer is null");
1139 }
1140 
1141 
1142 template<typename TreeT>
1143 inline Grid<TreeT>::Grid(const Grid& other):
1144  GridBase(other),
1145  mTree(StaticPtrCast<TreeType>(other.mTree->copy()))
1146 {
1147 }
1148 
1149 
1150 template<typename TreeT>
1151 template<typename OtherTreeType>
1153  GridBase(other),
1154  mTree(new TreeType(other.constTree()))
1155 {
1156 }
1157 
1158 
1159 #if OPENVDB_ABI_VERSION_NUMBER <= 3
1160 template<typename TreeT>
1161 inline Grid<TreeT>::Grid(const Grid& other, ShallowCopy):
1162  GridBase(other, ShallowCopy()),
1163  mTree(other.mTree)
1164 {
1165 }
1166 #else
1167 template<typename TreeT>
1169  GridBase(other),
1170  mTree(other.mTree)
1171 {
1172 }
1173 #endif
1174 
1175 
1176 template<typename TreeT>
1177 inline Grid<TreeT>::Grid(const GridBase& other):
1178  GridBase(other),
1179  mTree(new TreeType)
1180 {
1181 }
1182 
1183 
1184 //static
1185 template<typename TreeT>
1186 inline typename Grid<TreeT>::Ptr
1188 {
1189  return Grid::create(zeroVal<ValueType>());
1190 }
1191 
1192 
1193 //static
1194 template<typename TreeT>
1195 inline typename Grid<TreeT>::Ptr
1197 {
1198  return Ptr(new Grid(background));
1199 }
1200 
1201 
1202 //static
1203 template<typename TreeT>
1204 inline typename Grid<TreeT>::Ptr
1206 {
1207  return Ptr(new Grid(tree));
1208 }
1209 
1210 
1211 //static
1212 template<typename TreeT>
1213 inline typename Grid<TreeT>::Ptr
1215 {
1216  return Ptr(new Grid(other));
1217 }
1218 
1219 
1221 
1222 
1223 #if OPENVDB_ABI_VERSION_NUMBER <= 3
1224 
1225 template<typename TreeT>
1226 inline typename Grid<TreeT>::Ptr
1227 Grid<TreeT>::copy(CopyPolicy treePolicy) const
1228 {
1229  Ptr ret;
1230  switch (treePolicy) {
1231  case CP_NEW:
1232  ret.reset(new Grid(*this, ShallowCopy()));
1233  ret->newTree();
1234  break;
1235  case CP_COPY:
1236  ret.reset(new Grid(*this));
1237  break;
1238  case CP_SHARE:
1239  ret.reset(new Grid(*this, ShallowCopy()));
1240  break;
1241  }
1242  return ret;
1243 }
1244 
1245 
1246 template<typename TreeT>
1247 inline GridBase::Ptr
1248 Grid<TreeT>::copyGrid(CopyPolicy treePolicy) const
1249 {
1250  return this->copy(treePolicy);
1251 }
1252 
1253 #else // if OPENVDB_ABI_VERSION_NUMBER > 3
1254 
1255 template<typename TreeT>
1256 inline typename Grid<TreeT>::ConstPtr
1258 {
1259  return ConstPtr{new Grid{*const_cast<Grid*>(this), ShallowCopy{}}};
1260 }
1261 
1262 template<typename TreeT>
1263 inline typename Grid<TreeT>::Ptr
1265 {
1266  return Ptr{new Grid{*this, ShallowCopy{}}};
1267 }
1268 
1269 
1270 template<typename TreeT>
1271 inline typename Grid<TreeT>::Ptr
1273 {
1274  Ptr result{new Grid{*const_cast<Grid*>(this), ShallowCopy{}}};
1275  result->newTree();
1276  return result;
1277 }
1278 
1279 
1280 template<typename TreeT>
1281 inline GridBase::Ptr
1283 {
1284  return this->copy();
1285 }
1286 
1287 template<typename TreeT>
1288 inline GridBase::ConstPtr
1290 {
1291  return this->copy();
1292 }
1293 
1294 
1295 template<typename TreeT>
1296 inline GridBase::Ptr
1298 {
1299  return this->copyWithNewTree();
1300 }
1301 
1302 #endif
1303 
1304 
1306 
1307 
1308 template<typename TreeT>
1309 inline void
1311 {
1312  if (!tree) OPENVDB_THROW(ValueError, "Tree pointer is null");
1313  if (tree->type() != TreeType::treeType()) {
1314  OPENVDB_THROW(TypeError, "Cannot assign a tree of type "
1315  + tree->type() + " to a grid of type " + this->type());
1316  }
1317  mTree = StaticPtrCast<TreeType>(tree);
1318 }
1319 
1320 
1321 template<typename TreeT>
1322 inline void
1324 {
1325  mTree.reset(new TreeType(this->background()));
1326 }
1327 
1328 
1330 
1331 
1332 template<typename TreeT>
1333 inline void
1334 Grid<TreeT>::sparseFill(const CoordBBox& bbox, const ValueType& value, bool active)
1335 {
1336  tree().sparseFill(bbox, value, active);
1337 }
1338 
1339 
1340 template<typename TreeT>
1341 inline void
1342 Grid<TreeT>::fill(const CoordBBox& bbox, const ValueType& value, bool active)
1343 {
1344  this->sparseFill(bbox, value, active);
1345 }
1346 
1347 template<typename TreeT>
1348 inline void
1349 Grid<TreeT>::denseFill(const CoordBBox& bbox, const ValueType& value, bool active)
1350 {
1351  tree().denseFill(bbox, value, active);
1352 }
1353 
1354 template<typename TreeT>
1355 inline void
1356 Grid<TreeT>::pruneGrid(float tolerance)
1357 {
1358  this->tree().prune(ValueType(zeroVal<ValueType>() + tolerance));
1359 }
1360 
1361 #if OPENVDB_ABI_VERSION_NUMBER >= 3
1362 template<typename TreeT>
1363 inline void
1364 Grid<TreeT>::clip(const CoordBBox& bbox)
1365 {
1366  tree().clip(bbox);
1367 }
1368 #endif
1369 
1370 
1371 template<typename TreeT>
1372 inline void
1374 {
1375  tree().merge(other.tree(), policy);
1376 }
1377 
1378 
1379 template<typename TreeT>
1380 template<typename OtherTreeType>
1381 inline void
1383 {
1384  tree().topologyUnion(other.tree());
1385 }
1386 
1387 
1388 template<typename TreeT>
1389 template<typename OtherTreeType>
1390 inline void
1392 {
1393  tree().topologyIntersection(other.tree());
1394 }
1395 
1396 
1397 template<typename TreeT>
1398 template<typename OtherTreeType>
1399 inline void
1401 {
1402  tree().topologyDifference(other.tree());
1403 }
1404 
1405 
1407 
1408 
1409 template<typename TreeT>
1410 inline void
1412 {
1413  tree().evalMinMax(minVal, maxVal);
1414 }
1415 
1416 
1417 template<typename TreeT>
1418 inline CoordBBox
1420 {
1421  CoordBBox bbox;
1422  tree().evalActiveVoxelBoundingBox(bbox);
1423  return bbox;
1424 }
1425 
1426 
1427 template<typename TreeT>
1428 inline Coord
1430 {
1431  Coord dim;
1432  const bool nonempty = tree().evalActiveVoxelDim(dim);
1433  return (nonempty ? dim : Coord());
1434 }
1435 
1436 
1438 
1439 
1442 
1443 template<typename TreeT>
1444 inline void
1445 Grid<TreeT>::readTopology(std::istream& is)
1446 {
1447  tree().readTopology(is, saveFloatAsHalf());
1448 }
1449 
1450 
1451 template<typename TreeT>
1452 inline void
1453 Grid<TreeT>::writeTopology(std::ostream& os) const
1454 {
1455  tree().writeTopology(os, saveFloatAsHalf());
1456 }
1457 
1458 
1459 template<typename TreeT>
1460 inline void
1461 Grid<TreeT>::readBuffers(std::istream& is)
1462 {
1464  tree().readBuffers(is, saveFloatAsHalf());
1465  } else {
1466  uint16_t numPasses = 1;
1467  is.read(reinterpret_cast<char*>(&numPasses), sizeof(uint16_t));
1469  assert(bool(meta));
1470  for (uint16_t passIndex = 0; passIndex < numPasses; ++passIndex) {
1471  uint32_t pass = (uint32_t(numPasses) << 16) | uint32_t(passIndex);
1472  meta->setPass(pass);
1473  tree().readBuffers(is, saveFloatAsHalf());
1474  }
1475  }
1476 }
1477 
1478 
1479 #if OPENVDB_ABI_VERSION_NUMBER >= 3
1480 
1483 template<typename TreeT>
1484 inline void
1485 Grid<TreeT>::readBuffers(std::istream& is, const CoordBBox& bbox)
1486 {
1488  tree().readBuffers(is, bbox, saveFloatAsHalf());
1489  } else {
1490  uint16_t numPasses = 1;
1491  is.read(reinterpret_cast<char*>(&numPasses), sizeof(uint16_t));
1493  assert(bool(meta));
1494  for (uint16_t passIndex = 0; passIndex < numPasses; ++passIndex) {
1495  uint32_t pass = (uint32_t(numPasses) << 16) | uint32_t(passIndex);
1496  meta->setPass(pass);
1497  tree().readBuffers(is, saveFloatAsHalf());
1498  }
1499  // Cannot clip inside readBuffers() when using multiple passes,
1500  // so instead clip afterwards.
1501  tree().clip(bbox);
1502  }
1503 }
1504 
1505 
1506 template<typename TreeT>
1507 inline void
1509 {
1510  tree().readNonresidentBuffers();
1511 }
1512 
1513 #endif
1514 
1515 
1516 template<typename TreeT>
1517 inline void
1518 Grid<TreeT>::writeBuffers(std::ostream& os) const
1519 {
1520  if (!hasMultiPassIO()) {
1521  tree().writeBuffers(os, saveFloatAsHalf());
1522  } else {
1523  // Determine how many leaf buffer passes are required for this grid
1525  assert(bool(meta));
1526  uint16_t numPasses = 1;
1527  meta->setCountingPasses(true);
1528  meta->setPass(0);
1529  tree().writeBuffers(os, saveFloatAsHalf());
1530  numPasses = static_cast<uint16_t>(meta->pass());
1531  os.write(reinterpret_cast<const char*>(&numPasses), sizeof(uint16_t));
1532  meta->setCountingPasses(false);
1533 
1534  // Save out the data blocks of the grid.
1535  for (uint16_t passIndex = 0; passIndex < numPasses; ++passIndex) {
1536  uint32_t pass = (uint32_t(numPasses) << 16) | uint32_t(passIndex);
1537  meta->setPass(pass);
1538  tree().writeBuffers(os, saveFloatAsHalf());
1539  }
1540  }
1541 }
1542 
1543 
1544 //static
1545 template<typename TreeT>
1546 inline bool
1548 {
1550 }
1551 
1552 
1553 template<typename TreeT>
1554 inline void
1555 Grid<TreeT>::print(std::ostream& os, int verboseLevel) const
1556 {
1557  tree().print(os, verboseLevel);
1558 
1559  if (metaCount() > 0) {
1560  os << "Additional metadata:" << std::endl;
1561  for (ConstMetaIterator it = beginMeta(), end = endMeta(); it != end; ++it) {
1562  os << " " << it->first;
1563  if (it->second) {
1564  const std::string value = it->second->str();
1565  if (!value.empty()) os << ": " << value;
1566  }
1567  os << "\n";
1568  }
1569  }
1570 
1571  os << "Transform:" << std::endl;
1572  transform().print(os, /*indent=*/" ");
1573  os << std::endl;
1574 }
1575 
1576 
1578 
1579 
1580 template<typename GridType>
1581 inline typename GridType::Ptr
1582 createGrid(const typename GridType::ValueType& background)
1583 {
1584  return GridType::create(background);
1585 }
1586 
1587 
1588 template<typename GridType>
1589 inline typename GridType::Ptr
1591 {
1592  return GridType::create();
1593 }
1594 
1595 
1596 template<typename TreePtrType>
1599 {
1600  using TreeType = typename TreePtrType::element_type;
1601  return Grid<TreeType>::create(tree);
1602 }
1603 
1604 
1605 template<typename GridType>
1606 typename GridType::Ptr
1608 {
1609  using ValueType = typename GridType::ValueType;
1610 
1611  // GridType::ValueType is required to be a floating-point scalar.
1612  static_assert(std::is_floating_point<ValueType>::value,
1613  "level-set grids must be floating-point-valued");
1614 
1615  typename GridType::Ptr grid = GridType::create(
1616  /*background=*/static_cast<ValueType>(voxelSize * halfWidth));
1617  grid->setTransform(math::Transform::createLinearTransform(voxelSize));
1618  grid->setGridClass(GRID_LEVEL_SET);
1619  return grid;
1620 }
1621 
1622 } // namespace OPENVDB_VERSION_NAME
1623 } // namespace openvdb
1624 
1625 #endif // OPENVDB_GRID_HAS_BEEN_INCLUDED
1626 
1627 // Copyright (c) 2012-2018 DreamWorks Animation LLC
1628 // All rights reserved. This software is distributed under the
1629 // Mozilla Public License 2.0 ( http://www.mozilla.org/MPL/2.0/ )
SharedPtr< const TreeBase > ConstPtr
Definition: Tree.h:66
ValueOnCIter cbeginValueOn() const
Return an iterator over all of this grid&#39;s active values (tile and voxel).
Definition: Grid.h:666
static Ptr createGrid(const Name &type)
Create a new grid of the given (registered) type.
GridType::ConstPtr gridConstPtrCast(const GridBase::ConstPtr &grid)
Cast a generic const grid pointer to a const pointer to a grid of a concrete class.
Definition: Grid.h:903
static const TreeType & constTree(GridType &g)
Definition: Grid.h:996
typename GridType::Ptr GridPtrType
Definition: Grid.h:983
static const char *const META_GRID_CLASS
Definition: Grid.h:291
void setTree(TreeBase::Ptr) override
Associate the given tree with this grid, in place of its existing tree.
Definition: Grid.h:1310
static TreeType & tree(AccessorType &a)
Definition: Grid.h:1022
Name valueType() const override
Return the name of the type of a voxel&#39;s value (e.g., "float" or "vec3d").
Definition: Grid.h:622
typename tree::ValueAccessor< TreeType > AccessorType
Definition: Grid.h:987
#define OPENVDB_API
Helper macros for defining library symbol visibility.
Definition: Platform.h:194
GridBase::Ptr deepCopyGrid() const override
Return a new grid whose metadata, transform and tree are deep copies of this grid&#39;s.
Definition: Grid.h:609
GridCPtrSet::iterator GridCPtrSetIter
Definition: Grid.h:453
static const TreeType & constTree(GridType &g)
Definition: Grid.h:966
static const char *const META_FILE_COMPRESSION
Definition: Grid.h:299
static const TreeType & tree(const TreeType &t)
Definition: Grid.h:993
std::set< GridBase::Ptr > GridPtrSet
Definition: Grid.h:447
double Real
Definition: Types.h:67
void newTree() override
Associate a new, empty tree with this grid, in place of its existing tree.
Definition: Grid.h:1323
std::set< GridBase::ConstPtr > GridCPtrSet
Definition: Grid.h:452
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...
static TreeType & tree(TreeType &t)
Definition: Grid.h:961
ValueConverter<T>::Type is the type of a grid having the same hierarchy as this grid but a different ...
Definition: Grid.h:530
static const char *const META_VECTOR_TYPE
Definition: Grid.h:296
static const TreeType & constTree(TreeType &t)
Definition: Grid.h:965
typename GridType::Ptr GridPtrType
Definition: Grid.h:1012
void clear() override
Empty this grid, so that all voxels become inactive background voxels.
Definition: Grid.h:632
static TreeType & tree(GridType &g)
Definition: Grid.h:962
ConstTreePtrType treePtr() const
Return a pointer to this grid&#39;s tree, which might be shared with other grids. The pointer is guarante...
Definition: Grid.h:789
ValueOffCIter cbeginValueOff() const
Return an iterator over all of this grid&#39;s inactive values (tile and voxel).
Definition: Grid.h:672
void evalMinMax(ValueType &minVal, ValueType &maxVal) const
Return the minimum and maximum active values in this grid.
Definition: Grid.h:1411
void merge(Grid &other, MergePolicy policy=MERGE_ACTIVE_STATES)
Efficiently merge another grid into this grid using one of several schemes.
Definition: Grid.h:1373
virtual GridBase::Ptr deepCopyGrid() const =0
Return a new grid whose metadata, transform and tree are deep copies of this grid&#39;s.
typename _TreeType::ValueType ValueType
Definition: Grid.h:508
void readTransform(std::istream &is)
Read in the transform for this grid.
Definition: Grid.h:400
OPENVDB_API uint32_t getFormatVersion(std::ios_base &)
Return the file format version number associated with the given input stream.
static GridType::Ptr grid(const GridBase::Ptr &)
Return the result of downcasting a GridBase pointer to a Grid pointer of the specified type...
Definition: Grid.h:1068
typename NonConstGridType::Ptr NonConstGridPtrType
Definition: Grid.h:954
SharedPtr< Transform > Ptr
Definition: Transform.h:69
tree::TreeBase TreeBase
Definition: Grid.h:53
typename _TreeType::ConstPtr ConstTreePtrType
Definition: Grid.h:507
#define OPENVDB_THROW(exception, message)
Definition: Exceptions.h:109
typename tree::ValueAccessor< TreeType > AccessorType
Definition: Grid.h:957
void readTopology(std::istream &) override
Read the grid topology from a stream. This will read only the grid structure, not the actual data buf...
Definition: Grid.h:1445
typename GridType::ConstPtr ConstGridPtrType
Definition: Grid.h:985
static const TreeType & constTree(const TreeType &t)
Definition: Grid.h:997
typename tree::ValueAccessor< NonConstTreeType > NonConstAccessorType
Definition: Grid.h:989
std::string Name
Definition: Name.h:44
typename GridType::ConstPtr ConstGridPtrType
Definition: Grid.h:1014
static void registerGrid(const Name &type, GridFactory)
Register a grid type along with a factory function.
Vec3d voxelSize() const
Return the size of this grid&#39;s voxels.
Definition: Grid.h:359
ConstAccessor getConstAccessor() const
Return an accessor that provides random read-only access to this grid&#39;s voxels.
Definition: Grid.h:650
Definition: Exceptions.h:91
GridNamePred(const Name &_name)
Definition: Grid.h:461
Name name
Definition: Grid.h:463
void setTransform(math::Transform::Ptr)
Associate the given transform with this grid, in place of its existing transform. ...
Definition: Grid.h:1113
Metafunction that specifies whether a given leaf node, tree, or grid type requires multiple passes to...
Definition: Grid.h:1044
std::shared_ptr< T > SharedPtr
Definition: Types.h:139
typename tree::ValueAccessor< NonConstTreeType > NonConstAccessorType
Definition: Grid.h:1018
MetaIterator beginMeta()
Definition: MetaMap.h:111
SharedPtr< const Grid > ConstPtr
Definition: Grid.h:503
GridType::Ptr deepCopyTypedGrid(const GridBase &grid)
Return a pointer to a deep copy of the given grid, provided that the grid&#39;s concrete type is GridType...
Definition: Grid.h:929
typename tree::ValueAccessor< const TreeType > ConstAccessorType
Definition: Grid.h:988
void print(std::ostream &=std::cout, int verboseLevel=1) const override
Output a human-readable description of this grid.
Definition: Grid.h:1555
GridPtrVec::const_iterator GridPtrVecCIter
Definition: Grid.h:439
typename tree::ValueAccessor< _TreeType, true > Accessor
Definition: Grid.h:518
static const TreeType & tree(const GridType &g)
Definition: Grid.h:964
const math::Transform & transform() const
Return a reference to this grid&#39;s transform, which might be shared with other grids.
Definition: Grid.h:348
static const TreeType & tree(const GridType &g)
Definition: Grid.h:1024
Accessor getAccessor()
Return an accessor that provides random read and write access to this grid&#39;s voxels. The accessor is safe in the sense that it is registered by the tree of this grid.
Definition: Grid.h:637
static GridType::ConstPtr constGrid(const GridBase::Ptr &)
Return the result of downcasting a GridBase pointer to a Grid pointer of the specified type...
Definition: Grid.h:1090
bool operator()(const GridBase::ConstPtr &g) const
Definition: Grid.h:462
Container that maps names (strings) to values of arbitrary types.
Definition: MetaMap.h:46
typename TreeType::ConstPtr ConstTreePtrType
Definition: Grid.h:1008
const TreeType & tree() const
Return a reference to this grid&#39;s tree, which might be shared with other grids.
Definition: Grid.h:799
typename TreeType::ValueType ValueType
Definition: Grid.h:956
typename tree::ValueAccessor< const _TreeType, true > ConstAccessor
Definition: Grid.h:519
typename tree::ValueAccessor< const _TreeType, false > ConstUnsafeAccessor
Definition: Grid.h:521
static bool isRegistered(const Name &type)
Return true if the given grid type name is registered.
void readNonresidentBuffers() const override
Read all of this grid&#39;s data buffers that are not yet resident in memory (because delayed loading is ...
Definition: Grid.h:1508
Base class for typed trees.
Definition: Tree.h:62
static bool hasMultiPassIO()
Return true if grids of this type require multiple I/O passes to read and write data buffers...
Definition: Grid.h:1547
Definition: Types.h:277
Definition: ValueAccessor.h:220
static const Real LEVEL_SET_HALF_WIDTH
Definition: Types.h:283
typename std::remove_const< TreeType >::type NonConstTreeType
Definition: Grid.h:947
static const TreeType & constTree(const GridType &g)
Definition: Grid.h:1029
SharedPtr< GridPtrVec > GridPtrVecPtr
Definition: Grid.h:440
static bool isRegistered()
Return true if this grid type is registered.
Definition: Grid.h:853
Predicate functor that returns true for grids that have a specified name.
Definition: Grid.h:459
typename tree::ValueAccessor< const TreeType > ConstAccessorType
Definition: Grid.h:958
GridBase::Ptr copyGridWithNewTree() const override
Return a new grid of the same type as this grid whose metadata and transform are deep copies of this ...
Definition: Grid.h:1297
typename _TreeType::BuildType BuildType
Definition: Grid.h:509
typename TreeType::ValueType ValueType
Definition: Grid.h:1015
void readBuffers(std::istream &) override
Read all data buffers for this grid.
Definition: Grid.h:1461
static const char *const META_GRID_CREATOR
Definition: Grid.h:292
static Transform::Ptr createLinearTransform(double voxelSize=1.0)
Create and return a shared pointer to a new transform.
UnsafeAccessor getUnsafeAccessor()
Return an accessor that provides random read and write access to this grid&#39;s voxels. The accessor is unsafe in the sense that it is not registered by the tree of this grid. In some rare cases this can give a performance advantage over a registered accessor but it is unsafe if the tree topology is modified.
Definition: Grid.h:646
void denseFill(const CoordBBox &bbox, const ValueType &value, bool active=true)
Set all voxels within a given axis-aligned box to a constant value and ensure that those voxels are a...
Definition: Grid.h:1349
SharedPtr< const GridBase > ConstPtr
Definition: Grid.h:108
static void registerGrid()
Register this grid type along with a factory function.
Definition: Grid.h:855
ValueAllCIter cbeginValueAll() const
Return an iterator over all of this grid&#39;s values (tile and voxel).
Definition: Grid.h:678
static Name gridType()
Return the name of this type of grid.
Definition: Grid.h:615
void print(std::ostream &os=std::cout, const std::string &indent="") const
Print a description of this transform.
typename _TreeType::ValueOnCIter ValueOnCIter
Definition: Grid.h:512
typename _TreeType::ValueAllCIter ValueAllCIter
Definition: Grid.h:516
Vec3< double > Vec3d
Definition: Vec3.h:679
ValueOffCIter beginValueOff() const
Return an iterator over all of this grid&#39;s inactive values (tile and voxel).
Definition: Grid.h:671
typename TreeType::Ptr TreePtrType
Definition: Grid.h:948
typename tree::ValueAccessor< const TreeType > ConstAccessorType
Definition: Grid.h:1017
typename NonConstTreeType::Ptr NonConstTreePtrType
Definition: Grid.h:980
static const TreeType & constTree(const GridType &g)
Definition: Grid.h:998
Ptr copyWithNewTree() const
Return a new grid of the same type as this grid whose metadata and transform are deep copies of this ...
Definition: Grid.h:1272
GridPtrVec::iterator GridPtrVecIter
Definition: Grid.h:438
#define OPENVDB_VERSION_NAME
The version namespace name for this library version.
Definition: version.h:136
Vec3d voxelSize(const Vec3d &xyz) const
Return the size of this grid&#39;s voxel at position (x, y, z).
Definition: Grid.h:362
typename TreeType::ConstPtr ConstTreePtrType
Definition: Grid.h:979
typename std::remove_const< TreeType >::type NonConstTreeType
Definition: Grid.h:1006
math::Transform::ConstPtr transformPtr() const
Return a pointer to this grid&#39;s transform, which might be shared with other grids.
Definition: Grid.h:339
static const TreeType & tree(const TreeType &t)
Definition: Grid.h:963
Index64 memUsage() const override
Definition: Grid.h:779
static const TreeType & constTree(GridType &g)
Definition: Grid.h:1027
typename NonConstGridType::Ptr NonConstGridPtrType
Definition: Grid.h:1013
GridPtrSet::const_iterator GridPtrSetCIter
Definition: Grid.h:449
const TreeBase & baseTree() const
Return a reference to this grid&#39;s tree, which might be shared with other grids.
Definition: Grid.h:186
Definition: Exceptions.h:92
static const TreeType & tree(const AccessorType &a)
Definition: Grid.h:1025
typename _TreeType::Ptr TreePtrType
Definition: Grid.h:506
Tag dispatch class that distinguishes shallow copy constructors from deep copy constructors.
Definition: Types.h:515
typename TreeType::ValueType ValueType
Definition: Grid.h:986
GridPtrT findGridByName(const std::map< KeyT, GridPtrT > &container, const Name &name)
Return the first grid in the given map whose name is name.
Definition: Grid.h:480
static const TreeType & tree(const GridType &g)
Definition: Grid.h:994
const TreeType & constTree() const
Return a reference to this grid&#39;s tree, which might be shared with other grids.
Definition: Grid.h:800
bool hasUniformVoxels() const
Return true if the voxels in world space are uniformly sized cubes.
Definition: Grid.h:364
GridType::Ptr clip(const GridType &grid, const BBoxd &bbox, bool keepInterior=true)
Clip the given grid against a world-space bounding box and return a new grid containing the result...
Definition: Clip.h:375
SharedPtr< GridPtrSet > GridPtrSetPtr
Definition: Grid.h:450
Coord evalActiveVoxelDim() const override
Return the dimensions of the axis-aligned bounding box of all active voxels.
Definition: Grid.h:1429
SharedPtr< Grid > Ptr
Definition: Grid.h:502
Index64 activeVoxelCount() const override
Return the number of active voxels.
Definition: Grid.h:771
void writeBuffers(std::ostream &) const override
Write out all data buffers for this grid.
Definition: Grid.h:1518
GridClass
Definition: Types.h:275
static TreeType & tree(TreeType &t)
Definition: Grid.h:1020
GridCPtrVec::const_iterator GridCPtrVecCIter
Definition: Grid.h:444
static const char *const META_FILE_MEM_BYTES
Definition: Grid.h:300
static const TreeType & constTree(TreeType &t)
Definition: Grid.h:995
bool isType() const
Return true if this grid is of the same type as the template parameter.
Definition: Grid.h:157
size_t metaCount() const
Definition: MetaMap.h:118
Definition: Transform.h:66
Definition: Exceptions.h:40
void topologyUnion(const Grid< OtherTreeType > &other)
Union this grid&#39;s set of active values with the active values of the other grid, whose value type may...
Definition: Grid.h:1382
void writeTransform(std::ostream &os) const
Write out the transform for this grid.
Definition: Grid.h:402
static const char *const META_FILE_BBOX_MAX
Definition: Grid.h:298
GridType::Ptr createLevelSet(Real voxelSize=1.0, Real halfWidth=LEVEL_SET_HALF_WIDTH)
Create a new grid of type GridType classified as a "Level Set", i.e., a narrow-band level set...
Definition: Grid.h:1607
static const char *const META_SAVE_HALF_FLOAT
Definition: Grid.h:294
static const TreeType & constTree(const GridType &g)
Definition: Grid.h:968
TreeBase::ConstPtr baseTreePtr() const
Return a pointer to this grid&#39;s tree, which might be shared with other grids. The pointer is guarante...
Definition: Grid.h:176
typename std::remove_const< TreeType >::type NonConstTreeType
Definition: Grid.h:977
GridPtrSet::iterator GridPtrSetIter
Definition: Grid.h:448
VecType
Definition: Types.h:305
static Ptr create()
Return a new grid with background value zero.
Definition: Grid.h:1187
typename GridType::ConstPtr ConstGridPtrType
Definition: Grid.h:955
GridBase()
Initialize with an identity linear transform.
Definition: Grid.h:410
Abstract base class for typed grids.
Definition: Grid.h:104
MetadataMap::const_iterator ConstMetaIterator
Definition: MetaMap.h:55
static void unregisterGrid()
Remove this grid type from the registry.
Definition: Grid.h:864
const TreeBase & constBaseTree() const
Return a reference to this grid&#39;s tree, which might be shared with other grids.
Definition: Grid.h:187
GridType::Ptr gridPtrCast(const GridBase::Ptr &grid)
Cast a generic grid pointer to a pointer to a grid of a concrete class.
Definition: Grid.h:887
SharedPtr< MetaMap > Ptr
Definition: MetaMap.h:49
Grid()
Construct a new grid with background value zero.
Definition: Grid.h:1124
static TreeType & tree(GridType &g)
Definition: Grid.h:992
uint64_t Index64
Definition: Types.h:60
GridCPtrVec::iterator GridCPtrVecIter
Definition: Grid.h:443
MergePolicy
Definition: Types.h:328
typename NonConstGridType::Ptr NonConstGridPtrType
Definition: Grid.h:984
OPENVDB_API SharedPtr< StreamMetadata > getStreamMetadataPtr(std::ios_base &)
Return a shared pointer to an object that stores metadata (file format, compression scheme...
static const char *const META_GRID_NAME
Definition: Grid.h:293
typename _TreeType::ValueOffIter ValueOffIter
Definition: Grid.h:513
typename NonConstTreeType::Ptr NonConstTreePtrType
Definition: Grid.h:950
typename TreeType::Ptr TreePtrType
Definition: Grid.h:1007
bool saveFloatAsHalf() const
Return true if this grid should be written out with floating-point voxel values (including components...
typename TreeType::ConstPtr ConstTreePtrType
Definition: Grid.h:949
SharedPtr< T > StaticPtrCast(const SharedPtr< U > &ptr)
Return a new shared pointer that points to the same object as the given pointer after a static_cast...
Definition: Types.h:171
void clip(const CoordBBox &) override
Clip this grid to the given index-space bounding box.
Definition: Grid.h:1364
void writeTopology(std::ostream &) const override
Write the grid topology to a stream. This will write only the grid structure, not the actual data buf...
Definition: Grid.h:1453
~GridBase() override
Definition: Grid.h:113
MetaIterator endMeta()
Definition: MetaMap.h:112
_TreeType TreeType
Definition: Grid.h:976
static const char *const META_FILE_BBOX_MIN
Definition: Grid.h:297
TreeBase::Ptr baseTreePtr()
Return a pointer to this grid&#39;s tree, which might be shared with other grids. The pointer is guarante...
Definition: Grid.h:1106
Vec3d indexToWorld(const Coord &ijk) const
Apply this grid&#39;s transform to the given coordinates.
Definition: Grid.h:368
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 ...
static void unregisterGrid(const Name &type)
Remove a grid type from the registry.
SharedPtr< TreeBase > Ptr
Definition: Tree.h:65
std::vector< GridBase::Ptr > GridPtrVec
Definition: Grid.h:437
CoordBBox evalActiveVoxelBoundingBox() const override
Return the axis-aligned bounding box of all active voxels.
Definition: Grid.h:1419
static TreeType & tree(TreeType &t)
Definition: Grid.h:991
typename tree::ValueAccessor< NonConstTreeType > NonConstAccessorType
Definition: Grid.h:959
typename tree::ValueAccessor< TreeType > AccessorType
Definition: Grid.h:1016
ValueOnCIter beginValueOn() const
Return an iterator over all of this grid&#39;s active values (tile and voxel).
Definition: Grid.h:665
ValueAllCIter beginValueAll() const
Return an iterator over all of this grid&#39;s values (tile and voxel).
Definition: Grid.h:677
#define OPENVDB_LOG_WARN(message)
Log a warning message of the form &#39;someVar << "some text" << ...&#39;.
Definition: logging.h:280
static const TreeType & tree(const TreeType &t)
Definition: Grid.h:1023
SharedPtr< GridCPtrVec > GridCPtrVecPtr
Definition: Grid.h:445
Definition: Types.h:329
SharedPtr< GridCPtrSet > GridCPtrSetPtr
Definition: Grid.h:455
GridBase::Ptr copyGrid() override
Return a new grid of the same type as this grid whose metadata and transform are deep copies of this ...
Definition: Grid.h:1282
const ValueType & background() const
Return this grid&#39;s background value.
Definition: Grid.h:627
static const char *const META_IS_LOCAL_SPACE
Definition: Grid.h:295
Name type() const override
Return the name of this grid&#39;s type.
Definition: Grid.h:613
Grid< typename TreePtrType::element_type >::Ptr createGrid(TreePtrType)
Create a new grid of the appropriate type that wraps the given tree.
Definition: Grid.h:1598
GridCPtrSet::const_iterator GridCPtrSetCIter
Definition: Grid.h:454
static const TreeType & constTree(const TreeType &t)
Definition: Grid.h:1028
_TreeType TreeType
Definition: Grid.h:946
bool empty() const override
Return true if this grid contains only inactive background voxels.
Definition: Grid.h:630
const math::Transform & constTransform() const
Return a reference to this grid&#39;s transform, which might be shared with other grids.
Definition: Grid.h:349
ConstTreePtrType constTreePtr() const
Return a pointer to this grid&#39;s tree, which might be shared with other grids. The pointer is guarante...
Definition: Grid.h:790
Container class that associates a tree with a transform and metadata.
Definition: Grid.h:55
std::vector< GridBase::ConstPtr > GridCPtrVec
Definition: Grid.h:442
typename TreeType::Ptr TreePtrType
Definition: Grid.h:978
GridBase(const GridBase &other)
Deep copy another grid&#39;s metadata and transform.
Definition: Grid.h:413
TreeBase::ConstPtr constBaseTreePtr() const override
Return a pointer to this grid&#39;s tree, which might be shared with other grids. The pointer is guarante...
Definition: Grid.h:791
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h:188
void topologyIntersection(const Grid< OtherTreeType > &other)
Intersect this grid&#39;s set of active values with the active values of the other grid, whose value type may be different.
Definition: Grid.h:1391
void pruneGrid(float tolerance=0.0) override
Reduce the memory footprint of this grid by increasing its sparseness.
Definition: Grid.h:1356
static const TreeType & constTree(const TreeType &t)
Definition: Grid.h:967
SharedPtr< GridBase > Ptr
Definition: Grid.h:107
math::Transform::ConstPtr constTransformPtr() const
Return a pointer to this grid&#39;s transform, which might be shared with other grids.
Definition: Grid.h:340
static TreeType & tree(GridType &g)
Definition: Grid.h:1021
typename _TreeType::ValueAllIter ValueAllIter
Definition: Grid.h:515
_TreeType TreeType
Definition: Grid.h:505
void sparseFill(const CoordBBox &bbox, const ValueType &value, bool active=true)
Set all voxels within a given axis-aligned box to a constant value.
Definition: Grid.h:1334
TreeType & tree()
Return a reference to this grid&#39;s tree, which might be shared with other grids.
Definition: Grid.h:798
static const char *const META_FILE_VOXEL_COUNT
Definition: Grid.h:301
math::Transform & transform()
Return a reference to this grid&#39;s transform, which might be shared with other grids.
Definition: Grid.h:347
SharedPtr< const Transform > ConstPtr
Definition: Transform.h:70
Ptr copy()
Return a new grid of the same type as this grid whose metadata and transform are deep copies of this ...
Definition: Grid.h:1264
This adapter allows code that is templated on a Tree type to accept either a Tree type or a Grid type...
Definition: Grid.h:944
typename _TreeType::ValueOffCIter ValueOffCIter
Definition: Grid.h:514
void fill(const CoordBBox &bbox, const ValueType &value, bool active=true)
Set all voxels within a given axis-aligned box to a constant value.
Definition: Grid.h:1342
~Grid() override
Definition: Grid.h:574
static const TreeType & constTree(TreeType &t)
Definition: Grid.h:1026
void topologyDifference(const Grid< OtherTreeType > &other)
Difference this grid&#39;s set of active values with the active values of the other grid, whose value type may be different.
Definition: Grid.h:1400
typename NonConstTreeType::Ptr NonConstTreePtrType
Definition: Grid.h:1009
typename tree::ValueAccessor< _TreeType, false > UnsafeAccessor
Definition: Grid.h:520
typename _TreeType::ValueOnIter ValueOnIter
Definition: Grid.h:511
typename GridType::Ptr GridPtrType
Definition: Grid.h:953
SharedPtr< StreamMetadata > Ptr
Definition: io.h:59