OpenVDB  5.1.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;
588  GridBase::Ptr copyGrid(CopyPolicy treePolicy = CP_SHARE) const override;
590 #else
591 
592  Ptr copy();
595  ConstPtr copy() const;
597  Ptr copyWithNewTree() const;
600 
601  GridBase::Ptr copyGrid() override;
602  GridBase::ConstPtr copyGrid() const override;
603  GridBase::Ptr copyGridWithNewTree() const override;
604 #endif
605 
606  Ptr deepCopy() const { return Ptr(new Grid(*this)); }
608  GridBase::Ptr deepCopyGrid() const override { return this->deepCopy(); }
610 
612  Name type() const override { return this->gridType(); }
614  static Name gridType() { return TreeType::treeType(); }
615 
616 
617  //
618  // Voxel access methods
619  //
621  Name valueType() const override { return tree().valueType(); }
622 
626  const ValueType& background() const { return mTree->background(); }
627 
629  bool empty() const override { return tree().empty(); }
631  void clear() override { tree().clear(); }
632 
636  Accessor getAccessor() { return Accessor(tree()); }
647  ConstAccessor getAccessor() const { return ConstAccessor(tree()); }
649  ConstAccessor getConstAccessor() const { return ConstAccessor(tree()); }
651  ConstUnsafeAccessor getConstUnsafeAccessor() const { return ConstUnsafeAccessor(tree()); }
660 
662  ValueOnIter beginValueOn() { return tree().beginValueOn(); }
664  ValueOnCIter beginValueOn() const { return tree().cbeginValueOn(); }
665  ValueOnCIter cbeginValueOn() const { return tree().cbeginValueOn(); }
667 
668  ValueOffIter beginValueOff() { return tree().beginValueOff(); }
670  ValueOffCIter beginValueOff() const { return tree().cbeginValueOff(); }
671  ValueOffCIter cbeginValueOff() const { return tree().cbeginValueOff(); }
673 
674  ValueAllIter beginValueAll() { return tree().beginValueAll(); }
676  ValueAllCIter beginValueAll() const { return tree().cbeginValueAll(); }
677  ValueAllCIter cbeginValueAll() const { return tree().cbeginValueAll(); }
679 
681  void evalMinMax(ValueType& minVal, ValueType& maxVal) const;
682 
684  void sparseFill(const CoordBBox& bbox, const ValueType& value, bool active = true);
693  void fill(const CoordBBox& bbox, const ValueType& value, bool active = true);
695 
702  void denseFill(const CoordBBox& bbox, const ValueType& value, bool active = true);
703 
705  void pruneGrid(float tolerance = 0.0) override;
706 
707 #if OPENVDB_ABI_VERSION_NUMBER >= 3
708  void clip(const CoordBBox&) override;
713 #endif
714 
720  void merge(Grid& other, MergePolicy policy = MERGE_ACTIVE_STATES);
721 
735  template<typename OtherTreeType>
736  void topologyUnion(const Grid<OtherTreeType>& other);
737 
750  template<typename OtherTreeType>
751  void topologyIntersection(const Grid<OtherTreeType>& other);
752 
763  template<typename OtherTreeType>
764  void topologyDifference(const Grid<OtherTreeType>& other);
765 
766  //
767  // Statistics
768  //
770  Index64 activeVoxelCount() const override { return tree().activeVoxelCount(); }
772  CoordBBox evalActiveVoxelBoundingBox() const override;
774  Coord evalActiveVoxelDim() const override;
775 
778  Index64 memUsage() const override { return tree().memUsage(); }
779 
780 
781  //
782  // Tree methods
783  //
785  TreePtrType treePtr() { return mTree; }
788  ConstTreePtrType treePtr() const { return mTree; }
789  ConstTreePtrType constTreePtr() const { return mTree; }
790  TreeBase::ConstPtr constBaseTreePtr() const override { return mTree; }
792 
793  TreeType& tree() { return *mTree; }
798  const TreeType& tree() const { return *mTree; }
799  const TreeType& constTree() const { return *mTree; }
801 
807  void setTree(TreeBase::Ptr) override;
808 
811  void newTree() override;
812 
813 
814  //
815  // I/O methods
816  //
819  void readTopology(std::istream&) override;
822  void writeTopology(std::ostream&) const override;
823 
825  void readBuffers(std::istream&) override;
826 #if OPENVDB_ABI_VERSION_NUMBER >= 3
827  void readBuffers(std::istream&, const CoordBBox&) override;
834  void readNonresidentBuffers() const override;
835 #endif
836  void writeBuffers(std::ostream&) const override;
838 
840  void print(std::ostream& = std::cout, int verboseLevel = 1) const override;
841 
845  static inline bool hasMultiPassIO();
846 
847 
848  //
849  // Registry methods
850  //
854  static void registerGrid()
855  {
856  GridBase::registerGrid(Grid::gridType(), Grid::factory);
858  OPENVDB_LOG_WARN("delayed loading of grids of type " << Grid::gridType()
859  << " might not be threadsafe on this platform");
860  }
861  }
864 
865 
866 private:
868  static GridBase::Ptr factory() { return Grid::create(); }
869 
870  TreePtrType mTree;
871 }; // class Grid
872 
873 
875 
876 
884 template<typename GridType>
885 inline typename GridType::Ptr
887 {
888  return GridBase::grid<GridType>(grid);
889 }
890 
891 
900 template<typename GridType>
901 inline typename GridType::ConstPtr
903 {
904  return GridBase::constGrid<GridType>(grid);
905 }
906 
907 
909 
910 
917 template<typename GridType>
918 inline typename GridType::Ptr
920 {
921  if (!grid || !grid->isType<GridType>()) return typename GridType::Ptr();
922  return gridPtrCast<GridType>(grid->deepCopyGrid());
923 }
924 
925 
926 template<typename GridType>
927 inline typename GridType::Ptr
929 {
930  if (!grid.isType<GridType>()) return typename GridType::Ptr();
931  return gridPtrCast<GridType>(grid.deepCopyGrid());
932 }
934 
935 
937 
938 
940 template<typename _TreeType>
944 {
945  using TreeType = _TreeType;
946  using NonConstTreeType = typename std::remove_const<TreeType>::type;
947  using TreePtrType = typename TreeType::Ptr;
948  using ConstTreePtrType = typename TreeType::ConstPtr;
949  using NonConstTreePtrType = typename NonConstTreeType::Ptr;
952  using GridPtrType = typename GridType::Ptr;
955  using ValueType = typename TreeType::ValueType;
959 
960  static TreeType& tree(TreeType& t) { return t; }
961  static TreeType& tree(GridType& g) { return g.tree(); }
962  static const TreeType& tree(const TreeType& t) { return t; }
963  static const TreeType& tree(const GridType& g) { return g.tree(); }
964  static const TreeType& constTree(TreeType& t) { return t; }
965  static const TreeType& constTree(GridType& g) { return g.constTree(); }
966  static const TreeType& constTree(const TreeType& t) { return t; }
967  static const TreeType& constTree(const GridType& g) { return g.constTree(); }
968 };
969 
970 
972 template<typename _TreeType>
973 struct TreeAdapter<Grid<_TreeType> >
974 {
975  using TreeType = _TreeType;
976  using NonConstTreeType = typename std::remove_const<TreeType>::type;
977  using TreePtrType = typename TreeType::Ptr;
978  using ConstTreePtrType = typename TreeType::ConstPtr;
979  using NonConstTreePtrType = typename NonConstTreeType::Ptr;
982  using GridPtrType = typename GridType::Ptr;
985  using ValueType = typename TreeType::ValueType;
989 
990  static TreeType& tree(TreeType& t) { return t; }
991  static TreeType& tree(GridType& g) { return g.tree(); }
992  static const TreeType& tree(const TreeType& t) { return t; }
993  static const TreeType& tree(const GridType& g) { return g.tree(); }
994  static const TreeType& constTree(TreeType& t) { return t; }
995  static const TreeType& constTree(GridType& g) { return g.constTree(); }
996  static const TreeType& constTree(const TreeType& t) { return t; }
997  static const TreeType& constTree(const GridType& g) { return g.constTree(); }
998 };
999 
1001 template<typename _TreeType>
1002 struct TreeAdapter<tree::ValueAccessor<_TreeType> >
1003 {
1004  using TreeType = _TreeType;
1005  using NonConstTreeType = typename std::remove_const<TreeType>::type;
1006  using TreePtrType = typename TreeType::Ptr;
1007  using ConstTreePtrType = typename TreeType::ConstPtr;
1008  using NonConstTreePtrType = typename NonConstTreeType::Ptr;
1011  using GridPtrType = typename GridType::Ptr;
1014  using ValueType = typename TreeType::ValueType;
1018 
1019  static TreeType& tree(TreeType& t) { return t; }
1020  static TreeType& tree(GridType& g) { return g.tree(); }
1021  static TreeType& tree(AccessorType& a) { return a.tree(); }
1022  static const TreeType& tree(const TreeType& t) { return t; }
1023  static const TreeType& tree(const GridType& g) { return g.tree(); }
1024  static const TreeType& tree(const AccessorType& a) { return a.tree(); }
1025  static const TreeType& constTree(TreeType& t) { return t; }
1026  static const TreeType& constTree(GridType& g) { return g.constTree(); }
1027  static const TreeType& constTree(const TreeType& t) { return t; }
1028  static const TreeType& constTree(const GridType& g) { return g.constTree(); }
1029 };
1030 
1032 
1033 
1035 
1036 
1042 template<typename LeafNodeType>
1044  static const bool value = std::is_base_of<io::MultiPass, LeafNodeType>::value;
1045 };
1046 
1047 // Partial specialization for Tree types
1048 template<typename RootNodeType>
1049 struct HasMultiPassIO<tree::Tree<RootNodeType>> {
1050  // A tree is multi-pass if its (root node's) leaf node type is multi-pass.
1052 };
1053 
1054 // Partial specialization for Grid types
1055 template<typename TreeType>
1056 struct HasMultiPassIO<Grid<TreeType>> {
1057  // A grid is multi-pass if its tree's leaf node type is multi-pass.
1059 };
1060 
1061 
1063 
1064 
1065 template<typename GridType>
1066 inline typename GridType::Ptr
1068 {
1069  // The string comparison on type names is slower than a dynamic pointer cast, but
1070  // it is safer when pointers cross DSO boundaries, as they do in many Houdini nodes.
1071  if (grid && grid->type() == GridType::gridType()) {
1072  return StaticPtrCast<GridType>(grid);
1073  }
1074  return typename GridType::Ptr();
1075 }
1076 
1077 
1078 template<typename GridType>
1079 inline typename GridType::ConstPtr
1081 {
1082  return ConstPtrCast<const GridType>(
1083  GridBase::grid<GridType>(ConstPtrCast<GridBase>(grid)));
1084 }
1085 
1086 
1087 template<typename GridType>
1088 inline typename GridType::ConstPtr
1090 {
1091  return ConstPtrCast<const GridType>(GridBase::grid<GridType>(grid));
1092 }
1093 
1094 
1095 template<typename GridType>
1096 inline typename GridType::ConstPtr
1098 {
1099  return ConstPtrCast<const GridType>(
1100  GridBase::grid<GridType>(ConstPtrCast<GridBase>(grid)));
1101 }
1102 
1103 
1104 inline TreeBase::Ptr
1106 {
1107  return ConstPtrCast<TreeBase>(this->constBaseTreePtr());
1108 }
1109 
1110 
1111 inline void
1113 {
1114  if (!xform) OPENVDB_THROW(ValueError, "Transform pointer is null");
1115  mTransform = xform;
1116 }
1117 
1118 
1120 
1121 
1122 template<typename TreeT>
1123 inline Grid<TreeT>::Grid(): mTree(new TreeType)
1124 {
1125 }
1126 
1127 
1128 template<typename TreeT>
1129 inline Grid<TreeT>::Grid(const ValueType &background): mTree(new TreeType(background))
1130 {
1131 }
1132 
1133 
1134 template<typename TreeT>
1135 inline Grid<TreeT>::Grid(TreePtrType tree): mTree(tree)
1136 {
1137  if (!tree) OPENVDB_THROW(ValueError, "Tree pointer is null");
1138 }
1139 
1140 
1141 template<typename TreeT>
1142 inline Grid<TreeT>::Grid(const Grid& other):
1143  GridBase(other),
1144  mTree(StaticPtrCast<TreeType>(other.mTree->copy()))
1145 {
1146 }
1147 
1148 
1149 template<typename TreeT>
1150 template<typename OtherTreeType>
1152  GridBase(other),
1153  mTree(new TreeType(other.constTree()))
1154 {
1155 }
1156 
1157 
1158 #if OPENVDB_ABI_VERSION_NUMBER <= 3
1159 template<typename TreeT>
1160 inline Grid<TreeT>::Grid(const Grid& other, ShallowCopy):
1161  GridBase(other, ShallowCopy()),
1162  mTree(other.mTree)
1163 {
1164 }
1165 #else
1166 template<typename TreeT>
1168  GridBase(other),
1169  mTree(other.mTree)
1170 {
1171 }
1172 #endif
1173 
1174 
1175 template<typename TreeT>
1176 inline Grid<TreeT>::Grid(const GridBase& other):
1177  GridBase(other),
1178  mTree(new TreeType)
1179 {
1180 }
1181 
1182 
1183 //static
1184 template<typename TreeT>
1185 inline typename Grid<TreeT>::Ptr
1187 {
1188  return Grid::create(zeroVal<ValueType>());
1189 }
1190 
1191 
1192 //static
1193 template<typename TreeT>
1194 inline typename Grid<TreeT>::Ptr
1195 Grid<TreeT>::create(const ValueType& background)
1196 {
1197  return Ptr(new Grid(background));
1198 }
1199 
1200 
1201 //static
1202 template<typename TreeT>
1203 inline typename Grid<TreeT>::Ptr
1205 {
1206  return Ptr(new Grid(tree));
1207 }
1208 
1209 
1210 //static
1211 template<typename TreeT>
1212 inline typename Grid<TreeT>::Ptr
1214 {
1215  return Ptr(new Grid(other));
1216 }
1217 
1218 
1220 
1221 
1222 #if OPENVDB_ABI_VERSION_NUMBER <= 3
1223 
1224 template<typename TreeT>
1225 inline typename Grid<TreeT>::Ptr
1226 Grid<TreeT>::copy(CopyPolicy treePolicy) const
1227 {
1228  Ptr ret;
1229  switch (treePolicy) {
1230  case CP_NEW:
1231  ret.reset(new Grid(*this, ShallowCopy()));
1232  ret->newTree();
1233  break;
1234  case CP_COPY:
1235  ret.reset(new Grid(*this));
1236  break;
1237  case CP_SHARE:
1238  ret.reset(new Grid(*this, ShallowCopy()));
1239  break;
1240  }
1241  return ret;
1242 }
1243 
1244 
1245 template<typename TreeT>
1246 inline GridBase::Ptr
1247 Grid<TreeT>::copyGrid(CopyPolicy treePolicy) const
1248 {
1249  return this->copy(treePolicy);
1250 }
1251 
1252 #else // if OPENVDB_ABI_VERSION_NUMBER > 3
1253 
1254 template<typename TreeT>
1255 inline typename Grid<TreeT>::ConstPtr
1257 {
1258  return ConstPtr{new Grid{*const_cast<Grid*>(this), ShallowCopy{}}};
1259 }
1260 
1261 template<typename TreeT>
1262 inline typename Grid<TreeT>::Ptr
1264 {
1265  return Ptr{new Grid{*this, ShallowCopy{}}};
1266 }
1267 
1268 
1269 template<typename TreeT>
1270 inline typename Grid<TreeT>::Ptr
1272 {
1273  Ptr result{new Grid{*const_cast<Grid*>(this), ShallowCopy{}}};
1274  result->newTree();
1275  return result;
1276 }
1277 
1278 
1279 template<typename TreeT>
1280 inline GridBase::Ptr
1282 {
1283  return this->copy();
1284 }
1285 
1286 template<typename TreeT>
1287 inline GridBase::ConstPtr
1289 {
1290  return this->copy();
1291 }
1292 
1293 
1294 template<typename TreeT>
1295 inline GridBase::Ptr
1297 {
1298  return this->copyWithNewTree();
1299 }
1300 
1301 #endif
1302 
1303 
1305 
1306 
1307 template<typename TreeT>
1308 inline void
1310 {
1311  if (!tree) OPENVDB_THROW(ValueError, "Tree pointer is null");
1312  if (tree->type() != TreeType::treeType()) {
1313  OPENVDB_THROW(TypeError, "Cannot assign a tree of type "
1314  + tree->type() + " to a grid of type " + this->type());
1315  }
1316  mTree = StaticPtrCast<TreeType>(tree);
1317 }
1318 
1319 
1320 template<typename TreeT>
1321 inline void
1323 {
1324  mTree.reset(new TreeType(this->background()));
1325 }
1326 
1327 
1329 
1330 
1331 template<typename TreeT>
1332 inline void
1333 Grid<TreeT>::sparseFill(const CoordBBox& bbox, const ValueType& value, bool active)
1334 {
1335  tree().sparseFill(bbox, value, active);
1336 }
1337 
1338 
1339 template<typename TreeT>
1340 inline void
1341 Grid<TreeT>::fill(const CoordBBox& bbox, const ValueType& value, bool active)
1342 {
1343  this->sparseFill(bbox, value, active);
1344 }
1345 
1346 template<typename TreeT>
1347 inline void
1348 Grid<TreeT>::denseFill(const CoordBBox& bbox, const ValueType& value, bool active)
1349 {
1350  tree().denseFill(bbox, value, active);
1351 }
1352 
1353 template<typename TreeT>
1354 inline void
1355 Grid<TreeT>::pruneGrid(float tolerance)
1356 {
1357  this->tree().prune(ValueType(zeroVal<ValueType>() + tolerance));
1358 }
1359 
1360 #if OPENVDB_ABI_VERSION_NUMBER >= 3
1361 template<typename TreeT>
1362 inline void
1364 {
1365  tree().clip(bbox);
1366 }
1367 #endif
1368 
1369 
1370 template<typename TreeT>
1371 inline void
1373 {
1374  tree().merge(other.tree(), policy);
1375 }
1376 
1377 
1378 template<typename TreeT>
1379 template<typename OtherTreeType>
1380 inline void
1382 {
1383  tree().topologyUnion(other.tree());
1384 }
1385 
1386 
1387 template<typename TreeT>
1388 template<typename OtherTreeType>
1389 inline void
1391 {
1392  tree().topologyIntersection(other.tree());
1393 }
1394 
1395 
1396 template<typename TreeT>
1397 template<typename OtherTreeType>
1398 inline void
1400 {
1401  tree().topologyDifference(other.tree());
1402 }
1403 
1404 
1406 
1407 
1408 template<typename TreeT>
1409 inline void
1411 {
1412  tree().evalMinMax(minVal, maxVal);
1413 }
1414 
1415 
1416 template<typename TreeT>
1417 inline CoordBBox
1419 {
1420  CoordBBox bbox;
1421  tree().evalActiveVoxelBoundingBox(bbox);
1422  return bbox;
1423 }
1424 
1425 
1426 template<typename TreeT>
1427 inline Coord
1429 {
1430  Coord dim;
1431  const bool nonempty = tree().evalActiveVoxelDim(dim);
1432  return (nonempty ? dim : Coord());
1433 }
1434 
1435 
1437 
1438 
1441 
1442 template<typename TreeT>
1443 inline void
1444 Grid<TreeT>::readTopology(std::istream& is)
1445 {
1446  tree().readTopology(is, saveFloatAsHalf());
1447 }
1448 
1449 
1450 template<typename TreeT>
1451 inline void
1452 Grid<TreeT>::writeTopology(std::ostream& os) const
1453 {
1454  tree().writeTopology(os, saveFloatAsHalf());
1455 }
1456 
1457 
1458 template<typename TreeT>
1459 inline void
1460 Grid<TreeT>::readBuffers(std::istream& is)
1461 {
1462  if (!hasMultiPassIO() || (io::getFormatVersion(is) < OPENVDB_FILE_VERSION_MULTIPASS_IO)) {
1463  tree().readBuffers(is, saveFloatAsHalf());
1464  } else {
1465  uint16_t numPasses = 1;
1466  is.read(reinterpret_cast<char*>(&numPasses), sizeof(uint16_t));
1468  assert(bool(meta));
1469  for (uint16_t passIndex = 0; passIndex < numPasses; ++passIndex) {
1470  uint32_t pass = (uint32_t(numPasses) << 16) | uint32_t(passIndex);
1471  meta->setPass(pass);
1472  tree().readBuffers(is, saveFloatAsHalf());
1473  }
1474  }
1475 }
1476 
1477 
1478 #if OPENVDB_ABI_VERSION_NUMBER >= 3
1479 
1482 template<typename TreeT>
1483 inline void
1484 Grid<TreeT>::readBuffers(std::istream& is, const CoordBBox& bbox)
1485 {
1486  if (!hasMultiPassIO() || (io::getFormatVersion(is) < OPENVDB_FILE_VERSION_MULTIPASS_IO)) {
1487  tree().readBuffers(is, bbox, saveFloatAsHalf());
1488  } else {
1489  uint16_t numPasses = 1;
1490  is.read(reinterpret_cast<char*>(&numPasses), sizeof(uint16_t));
1492  assert(bool(meta));
1493  for (uint16_t passIndex = 0; passIndex < numPasses; ++passIndex) {
1494  uint32_t pass = (uint32_t(numPasses) << 16) | uint32_t(passIndex);
1495  meta->setPass(pass);
1496  tree().readBuffers(is, saveFloatAsHalf());
1497  }
1498  // Cannot clip inside readBuffers() when using multiple passes,
1499  // so instead clip afterwards.
1500  tree().clip(bbox);
1501  }
1502 }
1503 
1504 
1505 template<typename TreeT>
1506 inline void
1508 {
1509  tree().readNonresidentBuffers();
1510 }
1511 
1512 #endif
1513 
1514 
1515 template<typename TreeT>
1516 inline void
1517 Grid<TreeT>::writeBuffers(std::ostream& os) const
1518 {
1519  if (!hasMultiPassIO()) {
1520  tree().writeBuffers(os, saveFloatAsHalf());
1521  } else {
1522  // Determine how many leaf buffer passes are required for this grid
1524  assert(bool(meta));
1525  uint16_t numPasses = 1;
1526  meta->setCountingPasses(true);
1527  meta->setPass(0);
1528  tree().writeBuffers(os, saveFloatAsHalf());
1529  numPasses = static_cast<uint16_t>(meta->pass());
1530  os.write(reinterpret_cast<const char*>(&numPasses), sizeof(uint16_t));
1531  meta->setCountingPasses(false);
1532 
1533  // Save out the data blocks of the grid.
1534  for (uint16_t passIndex = 0; passIndex < numPasses; ++passIndex) {
1535  uint32_t pass = (uint32_t(numPasses) << 16) | uint32_t(passIndex);
1536  meta->setPass(pass);
1537  tree().writeBuffers(os, saveFloatAsHalf());
1538  }
1539  }
1540 }
1541 
1542 
1543 //static
1544 template<typename TreeT>
1545 inline bool
1547 {
1549 }
1550 
1551 
1552 template<typename TreeT>
1553 inline void
1554 Grid<TreeT>::print(std::ostream& os, int verboseLevel) const
1555 {
1556  tree().print(os, verboseLevel);
1557 
1558  if (metaCount() > 0) {
1559  os << "Additional metadata:" << std::endl;
1560  for (ConstMetaIterator it = beginMeta(), end = endMeta(); it != end; ++it) {
1561  os << " " << it->first;
1562  if (it->second) {
1563  const std::string value = it->second->str();
1564  if (!value.empty()) os << ": " << value;
1565  }
1566  os << "\n";
1567  }
1568  }
1569 
1570  os << "Transform:" << std::endl;
1571  transform().print(os, /*indent=*/" ");
1572  os << std::endl;
1573 }
1574 
1575 
1577 
1578 
1579 template<typename GridType>
1580 inline typename GridType::Ptr
1581 createGrid(const typename GridType::ValueType& background)
1582 {
1583  return GridType::create(background);
1584 }
1585 
1586 
1587 template<typename GridType>
1588 inline typename GridType::Ptr
1590 {
1591  return GridType::create();
1592 }
1593 
1594 
1595 template<typename TreePtrType>
1597 createGrid(TreePtrType tree)
1598 {
1599  using TreeType = typename TreePtrType::element_type;
1600  return Grid<TreeType>::create(tree);
1601 }
1602 
1603 
1604 template<typename GridType>
1605 typename GridType::Ptr
1606 createLevelSet(Real voxelSize, Real halfWidth)
1607 {
1608  using ValueType = typename GridType::ValueType;
1609 
1610  // GridType::ValueType is required to be a floating-point scalar.
1611  static_assert(std::is_floating_point<ValueType>::value,
1612  "level-set grids must be floating-point-valued");
1613 
1614  typename GridType::Ptr grid = GridType::create(
1615  /*background=*/static_cast<ValueType>(voxelSize * halfWidth));
1616  grid->setTransform(math::Transform::createLinearTransform(voxelSize));
1617  grid->setGridClass(GRID_LEVEL_SET);
1618  return grid;
1619 }
1620 
1621 } // namespace OPENVDB_VERSION_NAME
1622 } // namespace openvdb
1623 
1624 #endif // OPENVDB_GRID_HAS_BEEN_INCLUDED
1625 
1626 // Copyright (c) 2012-2018 DreamWorks Animation LLC
1627 // All rights reserved. This software is distributed under the
1628 // Mozilla Public License 2.0 ( http://www.mozilla.org/MPL/2.0/ )
typename tree::ValueAccessor< TreeType > AccessorType
Definition: Grid.h:986
static const TreeType & tree(const GridType &g)
Definition: Grid.h:963
typename tree::ValueAccessor< NonConstTreeType > NonConstAccessorType
Definition: Grid.h:958
SharedPtr< const TreeBase > ConstPtr
Definition: Tree.h:66
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:1281
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:788
#define OPENVDB_API
Helper macros for defining library symbol visibility.
Definition: Platform.h:194
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:790
SharedPtr< const Grid > ConstPtr
Definition: Grid.h:503
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:789
static const char *const META_IS_LOCAL_SPACE
Definition: Grid.h:295
static const TreeType & constTree(const TreeType &t)
Definition: Grid.h:1027
Grid< typename TreePtrType::element_type >::Ptr createGrid(TreePtrType)
Create a new grid of the appropriate type that wraps the given tree.
Definition: Grid.h:1597
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:636
This adapter allows code that is templated on a Tree type to accept either a Tree type or a Grid type...
Definition: Grid.h:943
Vec3d indexToWorld(const Coord &ijk) const
Apply this grid&#39;s transform to the given coordinates.
Definition: Grid.h:368
std::string Name
Definition: Name.h:44
ValueConverter<T>::Type is the type of a grid having the same hierarchy as this grid but a different ...
Definition: Grid.h:530
typename tree::ValueAccessor< const _TreeType, true > ConstAccessor
Definition: Grid.h:519
typename TreeType::Ptr TreePtrType
Definition: Grid.h:947
void readTransform(std::istream &is)
Read in the transform for this grid.
Definition: Grid.h:400
typename std::remove_const< TreeType >::type NonConstTreeType
Definition: Grid.h:946
Axis-aligned bounding box of signed integer coordinates.
Definition: Coord.h:264
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
typename tree::ValueAccessor< const TreeType > ConstAccessorType
Definition: Grid.h:957
static const TreeType & tree(const TreeType &t)
Definition: Grid.h:962
SharedPtr< GridBase > Ptr
Definition: Grid.h:107
typename _TreeType::ValueAllIter ValueAllIter
Definition: Grid.h:515
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:1333
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...
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:1263
typename _TreeType::ValueOffCIter ValueOffCIter
Definition: Grid.h:514
static const char *const META_GRID_CREATOR
Definition: Grid.h:292
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:1341
GridCPtrSet::iterator GridCPtrSetIter
Definition: Grid.h:453
~Grid() override
Definition: Grid.h:574
void readBuffers(std::istream &) override
Read all data buffers for this grid.
Definition: Grid.h:1460
double Real
Definition: Types.h:67
#define OPENVDB_THROW(exception, message)
Definition: Exceptions.h:109
std::set< GridBase::ConstPtr > GridCPtrSet
Definition: Grid.h:452
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:1067
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:1399
typename tree::ValueAccessor< _TreeType, false > UnsafeAccessor
Definition: Grid.h:520
typename std::remove_const< TreeType >::type NonConstTreeType
Definition: Grid.h:976
static Name gridType()
Return the name of this type of grid.
Definition: Grid.h:614
typename _TreeType::ValueAllCIter ValueAllCIter
Definition: Grid.h:516
GridPtrVec::const_iterator GridPtrVecCIter
Definition: Grid.h:439
GridPtrVec::iterator GridPtrVecIter
Definition: Grid.h:438
SharedPtr< StreamMetadata > Ptr
Definition: io.h:59
GridNamePred(const Name &_name)
Definition: Grid.h:461
typename NonConstTreeType::Ptr NonConstTreePtrType
Definition: Grid.h:979
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:1089
Name name
Definition: Grid.h:463
typename GridType::ConstPtr ConstGridPtrType
Definition: Grid.h:954
Abstract base class for typed grids.
Definition: Grid.h:104
typename TreeType::Ptr TreePtrType
Definition: Grid.h:977
void newTree() override
Associate a new, empty tree with this grid, in place of its existing tree.
Definition: Grid.h:1322
typename GridType::Ptr GridPtrType
Definition: Grid.h:1011
typename _TreeType::Ptr TreePtrType
Definition: Grid.h:506
void setTransform(math::Transform::Ptr)
Associate the given transform with this grid, in place of its existing transform. ...
Definition: Grid.h:1112
std::shared_ptr< T > SharedPtr
Definition: Types.h:139
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:1296
bool isType() const
Return true if this grid is of the same type as the template parameter.
Definition: Grid.h:157
typename NonConstGridType::Ptr NonConstGridPtrType
Definition: Grid.h:1012
typename NonConstTreeType::Ptr NonConstTreePtrType
Definition: Grid.h:949
ValueOnCIter cbeginValueOn() const
Return an iterator over all of this grid&#39;s active values (tile and voxel).
Definition: Grid.h:665
GridCPtrVec::const_iterator GridCPtrVecCIter
Definition: Grid.h:444
Metafunction that specifies whether a given leaf node, tree, or grid type requires multiple passes to...
Definition: Grid.h:1043
Base class for typed trees.
Definition: Tree.h:62
static TreeType & tree(TreeType &t)
Definition: Grid.h:960
static const TreeType & constTree(const GridType &g)
Definition: Grid.h:997
CoordBBox evalActiveVoxelBoundingBox() const override
Return the axis-aligned bounding box of all active voxels.
Definition: Grid.h:1418
static TreeType & tree(GridType &g)
Definition: Grid.h:1020
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
Definition: Types.h:326
static const char *const META_FILE_BBOX_MAX
Definition: Grid.h:298
typename TreeType::ValueType ValueType
Definition: Grid.h:1014
static const TreeType & tree(const AccessorType &a)
Definition: Grid.h:1024
virtual GridBase::Ptr deepCopyGrid() const =0
Return a new grid whose metadata, transform and tree are deep copies of this grid&#39;s.
bool operator()(const GridBase::ConstPtr &g) const
Definition: Grid.h:462
GridBase()
Initialize with an identity linear transform.
Definition: Grid.h:410
Index64 memUsage() const override
Definition: Grid.h:778
static const TreeType & tree(const GridType &g)
Definition: Grid.h:993
static const TreeType & tree(const GridType &g)
Definition: Grid.h:1023
Ptr(*)() GridFactory
Definition: Grid.h:110
typename _TreeType::ConstPtr ConstTreePtrType
Definition: Grid.h:507
SharedPtr< GridPtrSet > GridPtrSetPtr
Definition: Grid.h:450
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:1444
void writeTransform(std::ostream &os) const
Write out the transform for this grid.
Definition: Grid.h:402
static void unregisterGrid()
Remove this grid type from the registry.
Definition: Grid.h:863
typename TreeType::ConstPtr ConstTreePtrType
Definition: Grid.h:948
Predicate functor that returns true for grids that have a specified name.
Definition: Grid.h:459
static TreeType & tree(TreeType &t)
Definition: Grid.h:1019
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:1507
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:928
Grid()
Construct a new grid with background value zero.
Definition: Grid.h:1123
#define OPENVDB_VERSION_NAME
The version namespace name for this library version.
Definition: version.h:136
SharedPtr< const GridBase > ConstPtr
Definition: Grid.h:108
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
static TreeType & tree(TreeType &t)
Definition: Grid.h:990
static const TreeType & tree(const TreeType &t)
Definition: Grid.h:992
SharedPtr< TreeBase > Ptr
Definition: Tree.h:65
Definition: Transform.h:66
Tag dispatch class that distinguishes shallow copy constructors from deep copy constructors.
Definition: Types.h:512
Definition: Exceptions.h:92
typename tree::ValueAccessor< _TreeType, true > Accessor
Definition: Grid.h:518
Vec3< double > Vec3d
Definition: Vec3.h:679
#define OPENVDB_LOG_WARN(message)
Log a warning message of the form &#39;someVar << "some text" << ...&#39;.
Definition: logging.h:280
const TreeType & constTree() const
Return a reference to this grid&#39;s tree, which might be shared with other grids.
Definition: Grid.h:799
typename NonConstGridType::Ptr NonConstGridPtrType
Definition: Grid.h:953
void evalMinMax(ValueType &minVal, ValueType &maxVal) const
Return the minimum and maximum active values in this grid.
Definition: Grid.h:1410
bool hasUniformVoxels() const
Return true if the voxels in world space are uniformly sized cubes.
Definition: Grid.h:364
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:608
static const TreeType & tree(const TreeType &t)
Definition: Grid.h:1022
typename tree::ValueAccessor< const _TreeType, false > ConstUnsafeAccessor
Definition: Grid.h:521
static const TreeType & constTree(const TreeType &t)
Definition: Grid.h:966
Vec3d voxelSize() const
Return the size of this grid&#39;s voxels.
Definition: Grid.h:359
uint64_t Index64
Definition: Types.h:60
GridCPtrVec::iterator GridCPtrVecIter
Definition: Grid.h:443
MergePolicy
Definition: Types.h:325
static const TreeType & constTree(TreeType &t)
Definition: Grid.h:1025
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:1452
static bool hasMultiPassIO()
Return true if grids of this type require multiple I/O passes to read and write data buffers...
Definition: Grid.h:1546
typename tree::ValueAccessor< TreeType > AccessorType
Definition: Grid.h:956
GridType::Ptr createGrid(const typename GridType::ValueType &background)
Create a new grid of type GridType with a given background value.
Definition: Grid.h:1581
static bool isRegistered(const Name &type)
Return true if the given grid type name is registered.
static const char *const META_SAVE_HALF_FLOAT
Definition: Grid.h:294
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:1271
void print(std::ostream &=std::cout, int verboseLevel=1) const override
Output a human-readable description of this grid.
Definition: Grid.h:1554
typename TreeType::ValueType ValueType
Definition: Grid.h:955
Definition: Exceptions.h:40
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:886
ConstAccessor getConstAccessor() const
Return an accessor that provides random read-only access to this grid&#39;s voxels.
Definition: Grid.h:649
static const TreeType & constTree(GridType &g)
Definition: Grid.h:1026
_TreeType TreeType
Definition: Grid.h:945
SharedPtr< const Transform > ConstPtr
Definition: Transform.h:70
const ValueType & background() const
Return this grid&#39;s background value.
Definition: Grid.h:626
tree::TreeBase TreeBase
Definition: Grid.h:53
Definition: Exceptions.h:91
SharedPtr< GridCPtrSet > GridCPtrSetPtr
Definition: Grid.h:455
static bool isRegistered()
Return true if this grid type is registered.
Definition: Grid.h:852
SharedPtr< GridPtrVec > GridPtrVecPtr
Definition: Grid.h:440
MetadataMap::const_iterator ConstMetaIterator
Definition: MetaMap.h:55
static const char *const META_FILE_MEM_BYTES
Definition: Grid.h:300
GridCPtrSet::const_iterator GridCPtrSetCIter
Definition: Grid.h:454
typename _TreeType::BuildType BuildType
Definition: Grid.h:509
Definition: Types.h:274
typename NonConstTreeType::Ptr NonConstTreePtrType
Definition: Grid.h:1008
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:645
static const char *const META_GRID_NAME
Definition: Grid.h:293
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:1348
SharedPtr< MetaMap > Ptr
Definition: MetaMap.h:49
std::vector< GridBase::ConstPtr > GridCPtrVec
Definition: Grid.h:442
static void registerGrid()
Register this grid type along with a factory function.
Definition: Grid.h:854
Name type() const override
Return the name of this grid&#39;s type.
Definition: Grid.h:612
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
typename NonConstGridType::Ptr NonConstGridPtrType
Definition: Grid.h:983
typename _TreeType::ValueOnCIter ValueOnCIter
Definition: Grid.h:512
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:902
~GridBase() override
Definition: Grid.h:113
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:1105
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:1390
void pruneGrid(float tolerance=0.0) override
Reduce the memory footprint of this grid by increasing its sparseness.
Definition: Grid.h:1355
Name valueType() const override
Return the name of the type of a voxel&#39;s value (e.g., "float" or "vec3d").
Definition: Grid.h:621
_TreeType TreeType
Definition: Grid.h:975
typename GridType::ConstPtr ConstGridPtrType
Definition: Grid.h:1013
OPENVDB_API SharedPtr< StreamMetadata > getStreamMetadataPtr(std::ios_base &)
Return a shared pointer to an object that stores metadata (file format, compression scheme...
static void unregisterGrid(const Name &type)
Remove a grid type from the registry.
ValueOffCIter cbeginValueOff() const
Return an iterator over all of this grid&#39;s inactive values (tile and voxel).
Definition: Grid.h:671
static const TreeType & constTree(const TreeType &t)
Definition: Grid.h:996
_TreeType TreeType
Definition: Grid.h:505
TreeType & tree()
Return a reference to this grid&#39;s tree, which might be shared with other grids.
Definition: Grid.h:797
const TreeType & tree() const
Return a reference to this grid&#39;s tree, which might be shared with other grids.
Definition: Grid.h:798
static const TreeType & constTree(TreeType &t)
Definition: Grid.h:964
typename GridType::Ptr GridPtrType
Definition: Grid.h:952
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:1606
void writeBuffers(std::ostream &) const override
Write out all data buffers for this grid.
Definition: Grid.h:1517
GridPtrSet::iterator GridPtrSetIter
Definition: Grid.h:448
bool empty() const override
Return true if this grid contains only inactive background voxels.
Definition: Grid.h:629
VecType
Definition: Types.h:302
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 const char *const META_FILE_BBOX_MIN
Definition: Grid.h:297
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
static TreeType & tree(AccessorType &a)
Definition: Grid.h:1021
static TreeType & tree(GridType &g)
Definition: Grid.h:961
SharedPtr< Grid > Ptr
Definition: Grid.h:502
Signed (x, y, z) 32-bit integer coordinates.
Definition: Coord.h:51
static Transform::Ptr createLinearTransform(double voxelSize=1.0)
Create and return a shared pointer to a new transform.
typename TreeType::ConstPtr ConstTreePtrType
Definition: Grid.h:1007
typename _TreeType::ValueOnIter ValueOnIter
Definition: Grid.h:511
GridClass
Definition: Types.h:272
std::vector< GridBase::Ptr > GridPtrVec
Definition: Grid.h:437
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:1381
static const TreeType & constTree(GridType &g)
Definition: Grid.h:965
Index64 activeVoxelCount() const override
Return the number of active voxels.
Definition: Grid.h:770
static const TreeType & constTree(const GridType &g)
Definition: Grid.h:1028
typename tree::ValueAccessor< NonConstTreeType > NonConstAccessorType
Definition: Grid.h:1017
void setTree(TreeBase::Ptr) override
Associate the given tree with this grid, in place of its existing tree.
Definition: Grid.h:1309
Definition: ValueAccessor.h:220
typename TreeType::ValueType ValueType
Definition: Grid.h:985
typename TreeType::Ptr TreePtrType
Definition: Grid.h:1006
SharedPtr< Transform > Ptr
Definition: Transform.h:69
static const char *const META_FILE_COMPRESSION
Definition: Grid.h:299
GridBase(const GridBase &other)
Deep copy another grid&#39;s metadata and transform.
Definition: Grid.h:413
static Ptr create()
Return a new grid with background value zero.
Definition: Grid.h:1186
Coord evalActiveVoxelDim() const override
Return the dimensions of the axis-aligned bounding box of all active voxels.
Definition: Grid.h:1428
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:1005
typename tree::ValueAccessor< NonConstTreeType > NonConstAccessorType
Definition: Grid.h:988
static const char *const META_VECTOR_TYPE
Definition: Grid.h:296
typename tree::ValueAccessor< TreeType > AccessorType
Definition: Grid.h:1015
Container class that associates a tree with a transform and metadata.
Definition: Grid.h:55
static const TreeType & constTree(const GridType &g)
Definition: Grid.h:967
typename TreeType::ConstPtr ConstTreePtrType
Definition: Grid.h:978
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h:188
static const TreeType & constTree(GridType &g)
Definition: Grid.h:995
const TreeBase & baseTree() const
Return a reference to this grid&#39;s tree, which might be shared with other grids.
Definition: Grid.h:186
ValueOnCIter beginValueOn() const
Return an iterator over all of this grid&#39;s active values (tile and voxel).
Definition: Grid.h:664
typename _TreeType::ValueOffIter ValueOffIter
Definition: Grid.h:513
void clear() override
Empty this grid, so that all voxels become inactive background voxels.
Definition: Grid.h:631
GridPtrSet::const_iterator GridPtrSetCIter
Definition: Grid.h:449
typename GridType::ConstPtr ConstGridPtrType
Definition: Grid.h:984
ValueOffCIter beginValueOff() const
Return an iterator over all of this grid&#39;s inactive values (tile and voxel).
Definition: Grid.h:670
void clip(const CoordBBox &) override
Clip this grid to the given index-space bounding box.
Definition: Grid.h:1363
void merge(Grid &other, MergePolicy policy=MERGE_ACTIVE_STATES)
Efficiently merge another grid into this grid using one of several schemes.
Definition: Grid.h:1372
typename _TreeType::ValueType ValueType
Definition: Grid.h:508
typename tree::ValueAccessor< const TreeType > ConstAccessorType
Definition: Grid.h:1016
ValueAllCIter beginValueAll() const
Return an iterator over all of this grid&#39;s values (tile and voxel).
Definition: Grid.h:676
const TreeBase & constBaseTree() const
Return a reference to this grid&#39;s tree, which might be shared with other grids.
Definition: Grid.h:187
static const TreeType & constTree(TreeType &t)
Definition: Grid.h:994
OPENVDB_API uint32_t getFormatVersion(std::ios_base &)
Return the file format version number associated with the given input stream.
Container that maps names (strings) to values of arbitrary types.
Definition: MetaMap.h:46
static TreeType & tree(GridType &g)
Definition: Grid.h:991
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 ...
SharedPtr< GridCPtrVec > GridCPtrVecPtr
Definition: Grid.h:445
static const Real LEVEL_SET_HALF_WIDTH
Definition: Types.h:280
typename tree::ValueAccessor< const TreeType > ConstAccessorType
Definition: Grid.h:987
static const char *const META_FILE_VOXEL_COUNT
Definition: Grid.h:301
static const char *const META_GRID_CLASS
Definition: Grid.h:291
ValueAllCIter cbeginValueAll() const
Return an iterator over all of this grid&#39;s values (tile and voxel).
Definition: Grid.h:677
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
static void registerGrid(const Name &type, GridFactory)
Register a grid type along with a factory function.
typename GridType::Ptr GridPtrType
Definition: Grid.h:982
Vec3d voxelSize(const Vec3d &xyz) const
Return the size of this grid&#39;s voxel at position (x, y, z).
Definition: Grid.h:362
std::set< GridBase::Ptr > GridPtrSet
Definition: Grid.h:447