OpenVDB  7.0.0
Grid.h
Go to the documentation of this file.
1 // Copyright Contributors to the OpenVDB Project
2 // SPDX-License-Identifier: MPL-2.0
3 
4 #ifndef OPENVDB_GRID_HAS_BEEN_INCLUDED
5 #define OPENVDB_GRID_HAS_BEEN_INCLUDED
6 
7 #include "Exceptions.h"
8 #include "MetaMap.h"
9 #include "Types.h"
10 #include "io/io.h"
11 #include "math/Transform.h"
12 #include "tree/Tree.h"
13 #include "util/logging.h"
14 #include "util/Name.h"
15 #include <cassert>
16 #include <iostream>
17 #include <set>
18 #include <type_traits>
19 #include <vector>
20 
21 
22 namespace openvdb {
24 namespace OPENVDB_VERSION_NAME {
25 
27 
28 template<typename> class Grid; // forward declaration
29 
30 
35 template<typename GridType>
36 inline typename GridType::Ptr createGrid(const typename GridType::ValueType& background);
37 
38 
42 template<typename GridType>
43 inline typename GridType::Ptr createGrid();
44 
45 
50 template<typename TreePtrType>
52 
53 
68 template<typename GridType>
69 typename GridType::Ptr createLevelSet(
70  Real voxelSize = 1.0, Real halfWidth = LEVEL_SET_HALF_WIDTH);
71 
72 
74 
75 
78 {
79 public:
82 
83  using GridFactory = Ptr (*)();
84 
85 
86  ~GridBase() override {}
87 
88 
91 
94  virtual GridBase::Ptr copyGrid() = 0;
97  virtual GridBase::ConstPtr copyGrid() const = 0;
100  virtual GridBase::Ptr copyGridWithNewTree() const = 0;
101 
102 #if OPENVDB_ABI_VERSION_NUMBER >= 7
103  virtual GridBase::ConstPtr copyGridReplacingMetadata(const MetaMap& meta) const = 0;
110  virtual GridBase::ConstPtr copyGridReplacingTransform(math::Transform::Ptr xform) const = 0;
114  virtual GridBase::ConstPtr copyGridReplacingMetadataAndTransform(const MetaMap& meta,
115  math::Transform::Ptr xform) const = 0;
116 #endif
117 
119  virtual GridBase::Ptr deepCopyGrid() const = 0;
120 
122 
123 
126 
128  static Ptr createGrid(const Name& type);
129 
131  static bool isRegistered(const Name &type);
132 
134  static void clearRegistry();
135 
137 
140 
142  virtual Name type() const = 0;
144  virtual Name valueType() const = 0;
145 
147  template<typename GridType>
148  bool isType() const { return (this->type() == GridType::gridType()); }
149 
151 
153  template<typename GridType>
156  static typename GridType::Ptr grid(const GridBase::Ptr&);
157  template<typename GridType>
158  static typename GridType::ConstPtr grid(const GridBase::ConstPtr&);
159  template<typename GridType>
160  static typename GridType::ConstPtr constGrid(const GridBase::Ptr&);
161  template<typename GridType>
162  static typename GridType::ConstPtr constGrid(const GridBase::ConstPtr&);
164 
167 
170  TreeBase::Ptr baseTreePtr();
173  TreeBase::ConstPtr baseTreePtr() const { return this->constBaseTreePtr(); }
176  virtual TreeBase::ConstPtr constBaseTreePtr() const = 0;
177 
182  TreeBase& baseTree() { return const_cast<TreeBase&>(this->constBaseTree()); }
187  const TreeBase& baseTree() const { return this->constBaseTree(); }
192  const TreeBase& constBaseTree() const { return *(this->constBaseTreePtr()); }
193 
200  virtual void setTree(TreeBase::Ptr) = 0;
201 
203  virtual void newTree() = 0;
204 
206 
208  virtual bool empty() const = 0;
210  virtual void clear() = 0;
211 
212 
215 
221  virtual void pruneGrid(float tolerance = 0.0) = 0;
222 
227  void clipGrid(const BBoxd&);
228 
233  virtual void clip(const CoordBBox&) = 0;
234 
236 
261  template<typename GridTypeListT, typename OpT> inline bool apply(OpT&) const;
262  template<typename GridTypeListT, typename OpT> inline bool apply(OpT&);
263  template<typename GridTypeListT, typename OpT> inline bool apply(const OpT&) const;
264  template<typename GridTypeListT, typename OpT> inline bool apply(const OpT&);
266 
269 
271  std::string getName() const;
273  void setName(const std::string&);
274 
276  std::string getCreator() const;
278  void setCreator(const std::string&);
279 
282  bool saveFloatAsHalf() const;
283  void setSaveFloatAsHalf(bool);
284 
288  GridClass getGridClass() const;
292  void setGridClass(GridClass);
294  void clearGridClass();
295 
297 
299  static std::string gridClassToString(GridClass);
301  static std::string gridClassToMenuName(GridClass);
306  static GridClass stringToGridClass(const std::string&);
307 
310 
314  VecType getVectorType() const;
318  void setVectorType(VecType);
320  void clearVectorType();
321 
323 
325  static std::string vecTypeToString(VecType);
328  static std::string vecTypeExamples(VecType);
331  static std::string vecTypeDescription(VecType);
332  static VecType stringToVecType(const std::string&);
333 
336 
340  bool isInWorldSpace() const;
342  void setIsInWorldSpace(bool);
343 
345 
346  // Standard metadata field names
347  // (These fields should normally not be accessed directly, but rather
348  // via the accessor methods above, when available.)
349  // Note: Visual C++ requires these declarations to be separate statements.
350  static const char* const META_GRID_CLASS;
351  static const char* const META_GRID_CREATOR;
352  static const char* const META_GRID_NAME;
353  static const char* const META_SAVE_HALF_FLOAT;
354  static const char* const META_IS_LOCAL_SPACE;
355  static const char* const META_VECTOR_TYPE;
356  static const char* const META_FILE_BBOX_MIN;
357  static const char* const META_FILE_BBOX_MAX;
358  static const char* const META_FILE_COMPRESSION;
359  static const char* const META_FILE_MEM_BYTES;
360  static const char* const META_FILE_VOXEL_COUNT;
361  static const char* const META_FILE_DELAYED_LOAD;
362 
363 
366 
368  virtual Index64 activeVoxelCount() const = 0;
369 
372  virtual CoordBBox evalActiveVoxelBoundingBox() const = 0;
373 
375  virtual Coord evalActiveVoxelDim() const = 0;
376 
378  virtual Index64 memUsage() const = 0;
379 
384  void addStatsMetadata();
390  MetaMap::Ptr getStatsMetadata() const;
391 
393 
394 
397 
399  math::Transform::Ptr transformPtr() { return mTransform; }
402  math::Transform::ConstPtr transformPtr() const { return mTransform; }
403  math::Transform::ConstPtr constTransformPtr() const { return mTransform; }
405 
406  math::Transform& transform() { return *mTransform; }
411  const math::Transform& transform() const { return *mTransform; }
412  const math::Transform& constTransform() const { return *mTransform; }
414 
416 
419 
426  void setTransform(math::Transform::Ptr);
427 
429  Vec3d voxelSize() const { return transform().voxelSize(); }
432  Vec3d voxelSize(const Vec3d& xyz) const { return transform().voxelSize(xyz); }
434  bool hasUniformVoxels() const { return mTransform->hasUniformScale(); }
436  Vec3d indexToWorld(const Vec3d& xyz) const { return transform().indexToWorld(xyz); }
438  Vec3d indexToWorld(const Coord& ijk) const { return transform().indexToWorld(ijk); }
440  Vec3d worldToIndex(const Vec3d& xyz) const { return transform().worldToIndex(xyz); }
441 
443 
444 
447 
450  virtual void readTopology(std::istream&) = 0;
453  virtual void writeTopology(std::ostream&) const = 0;
454 
456  virtual void readBuffers(std::istream&) = 0;
458  virtual void readBuffers(std::istream&, const CoordBBox&) = 0;
464  virtual void readNonresidentBuffers() const = 0;
466  virtual void writeBuffers(std::ostream&) const = 0;
467 
469  void readTransform(std::istream& is) { transform().read(is); }
471  void writeTransform(std::ostream& os) const { transform().write(os); }
472 
474  virtual void print(std::ostream& = std::cout, int verboseLevel = 1) const = 0;
475 
477 
478 
479 protected:
481  GridBase(): mTransform(math::Transform::createLinearTransform()) {}
482 
483 #if OPENVDB_ABI_VERSION_NUMBER >= 7
484  GridBase(const MetaMap& meta, math::Transform::Ptr xform);
487 #endif
488 
490  GridBase(const GridBase& other): MetaMap(other), mTransform(other.mTransform->copy()) {}
491 
493  GridBase(GridBase& other, ShallowCopy): MetaMap(other), mTransform(other.mTransform) {}
494 
496  static void registerGrid(const Name& type, GridFactory);
498  static void unregisterGrid(const Name& type);
499 
500 
501 private:
502  math::Transform::Ptr mTransform;
503 }; // class GridBase
504 
505 
507 
508 
509 using GridPtrVec = std::vector<GridBase::Ptr>;
510 using GridPtrVecIter = GridPtrVec::iterator;
511 using GridPtrVecCIter = GridPtrVec::const_iterator;
513 
514 using GridCPtrVec = std::vector<GridBase::ConstPtr>;
515 using GridCPtrVecIter = GridCPtrVec::iterator;
516 using GridCPtrVecCIter = GridCPtrVec::const_iterator;
518 
519 using GridPtrSet = std::set<GridBase::Ptr>;
520 using GridPtrSetIter = GridPtrSet::iterator;
521 using GridPtrSetCIter = GridPtrSet::const_iterator;
523 
524 using GridCPtrSet = std::set<GridBase::ConstPtr>;
525 using GridCPtrSetIter = GridCPtrSet::iterator;
526 using GridCPtrSetCIter = GridCPtrSet::const_iterator;
528 
529 
532 {
533  GridNamePred(const Name& _name): name(_name) {}
534  bool operator()(const GridBase::ConstPtr& g) const { return g && g->getName() == name; }
536 };
537 
539 template<typename GridPtrContainerT>
540 inline typename GridPtrContainerT::value_type
541 findGridByName(const GridPtrContainerT& container, const Name& name)
542 {
543  using GridPtrT = typename GridPtrContainerT::value_type;
544  typename GridPtrContainerT::const_iterator it =
545  std::find_if(container.begin(), container.end(), GridNamePred(name));
546  return (it == container.end() ? GridPtrT() : *it);
547 }
548 
550 template<typename KeyT, typename GridPtrT>
551 inline GridPtrT
552 findGridByName(const std::map<KeyT, GridPtrT>& container, const Name& name)
553 {
554  using GridPtrMapT = std::map<KeyT, GridPtrT>;
555  for (typename GridPtrMapT::const_iterator it = container.begin(), end = container.end();
556  it != end; ++it)
557  {
558  const GridPtrT& grid = it->second;
559  if (grid && grid->getName() == name) return grid;
560  }
561  return GridPtrT();
562 }
564 
565 
567 
568 
570 template<typename _TreeType>
571 class Grid: public GridBase
572 {
573 public:
576 
577  using TreeType = _TreeType;
578  using TreePtrType = typename _TreeType::Ptr;
579  using ConstTreePtrType = typename _TreeType::ConstPtr;
580  using ValueType = typename _TreeType::ValueType;
581  using BuildType = typename _TreeType::BuildType;
582 
583  using ValueOnIter = typename _TreeType::ValueOnIter;
584  using ValueOnCIter = typename _TreeType::ValueOnCIter;
585  using ValueOffIter = typename _TreeType::ValueOffIter;
586  using ValueOffCIter = typename _TreeType::ValueOffCIter;
587  using ValueAllIter = typename _TreeType::ValueAllIter;
588  using ValueAllCIter = typename _TreeType::ValueAllCIter;
589 
594 
601  template<typename OtherValueType>
602  struct ValueConverter {
604  };
605 
607  static Ptr create(const ValueType& background);
609  static Ptr create();
612  static Ptr create(TreePtrType);
615  static Ptr create(const GridBase& other);
616 
617 
619  Grid();
621  explicit Grid(const ValueType& background);
625  explicit Grid(TreePtrType);
627  Grid(const Grid&);
633  template<typename OtherTreeType>
634  explicit Grid(const Grid<OtherTreeType>&);
636  Grid(Grid&, ShallowCopy);
639  explicit Grid(const GridBase&);
640 
641  ~Grid() override {}
642 
644  Grid& operator=(const Grid&) = delete;
645 
648 
651  Ptr copy();
654  ConstPtr copy() const;
657  Ptr copyWithNewTree() const;
658 
661  GridBase::Ptr copyGrid() override;
664  GridBase::ConstPtr copyGrid() const override;
667  GridBase::Ptr copyGridWithNewTree() const override;
669 
672 
673 #if OPENVDB_ABI_VERSION_NUMBER >= 7
674  ConstPtr copyReplacingMetadata(const MetaMap& meta) const;
681  ConstPtr copyReplacingTransform(math::Transform::Ptr xform) const;
685  ConstPtr copyReplacingMetadataAndTransform(const MetaMap& meta,
686  math::Transform::Ptr xform) const;
687 
690  GridBase::ConstPtr copyGridReplacingMetadata(const MetaMap& meta) const override;
695  GridBase::ConstPtr copyGridReplacingTransform(math::Transform::Ptr xform) const override;
699  GridBase::ConstPtr copyGridReplacingMetadataAndTransform(const MetaMap& meta,
700  math::Transform::Ptr xform) const override;
701 #endif
702 
704  Ptr deepCopy() const { return Ptr(new Grid(*this)); }
706  GridBase::Ptr deepCopyGrid() const override { return this->deepCopy(); }
707 
709 
710 
712  Name type() const override { return this->gridType(); }
714  static Name gridType() { return TreeType::treeType(); }
715 
717  Name valueType() const override { return tree().valueType(); }
718 
719 
722 
725  const ValueType& background() const { return mTree->background(); }
726 
728  bool empty() const override { return tree().empty(); }
730  void clear() override { tree().clear(); }
731 
735  Accessor getAccessor() { return Accessor(tree()); }
745  ConstAccessor getAccessor() const { return ConstAccessor(tree()); }
747  ConstAccessor getConstAccessor() const { return ConstAccessor(tree()); }
756 
758  ValueOnIter beginValueOn() { return tree().beginValueOn(); }
760  ValueOnCIter beginValueOn() const { return tree().cbeginValueOn(); }
762  ValueOnCIter cbeginValueOn() const { return tree().cbeginValueOn(); }
764  ValueOffIter beginValueOff() { return tree().beginValueOff(); }
766  ValueOffCIter beginValueOff() const { return tree().cbeginValueOff(); }
768  ValueOffCIter cbeginValueOff() const { return tree().cbeginValueOff(); }
770  ValueAllIter beginValueAll() { return tree().beginValueAll(); }
772  ValueAllCIter beginValueAll() const { return tree().cbeginValueAll(); }
774  ValueAllCIter cbeginValueAll() const { return tree().cbeginValueAll(); }
775 
777 
780 
789  void sparseFill(const CoordBBox& bbox, const ValueType& value, bool active = true);
798  void fill(const CoordBBox& bbox, const ValueType& value, bool active = true);
799 
806  void denseFill(const CoordBBox& bbox, const ValueType& value, bool active = true);
807 
809  void pruneGrid(float tolerance = 0.0) override;
810 
815  void clip(const CoordBBox&) override;
816 
822  void merge(Grid& other, MergePolicy policy = MERGE_ACTIVE_STATES);
823 
837  template<typename OtherTreeType>
838  void topologyUnion(const Grid<OtherTreeType>& other);
839 
852  template<typename OtherTreeType>
853  void topologyIntersection(const Grid<OtherTreeType>& other);
854 
865  template<typename OtherTreeType>
866  void topologyDifference(const Grid<OtherTreeType>& other);
867 
869 
872 
874  Index64 activeVoxelCount() const override { return tree().activeVoxelCount(); }
876  CoordBBox evalActiveVoxelBoundingBox() const override;
878  Coord evalActiveVoxelDim() const override;
880  void evalMinMax(ValueType& minVal, ValueType& maxVal) const;
881 
884  Index64 memUsage() const override { return tree().memUsage(); }
885 
887 
888 
891 
893  TreePtrType treePtr() { return mTree; }
896  ConstTreePtrType treePtr() const { return mTree; }
897  ConstTreePtrType constTreePtr() const { return mTree; }
898  TreeBase::ConstPtr constBaseTreePtr() const override { return mTree; }
900 
901  TreeType& tree() { return *mTree; }
906  const TreeType& tree() const { return *mTree; }
907  const TreeType& constTree() const { return *mTree; }
909 
911 
914 
920  void setTree(TreeBase::Ptr) override;
921 
924  void newTree() override;
925 
927 
928 
931 
934  void readTopology(std::istream&) override;
937  void writeTopology(std::ostream&) const override;
938 
940  void readBuffers(std::istream&) override;
942  void readBuffers(std::istream&, const CoordBBox&) override;
948  void readNonresidentBuffers() const override;
950  void writeBuffers(std::ostream&) const override;
951 
953  void print(std::ostream& = std::cout, int verboseLevel = 1) const override;
954 
956 
960  static inline bool hasMultiPassIO();
961 
962 
965 
969  static void registerGrid()
970  {
971  GridBase::registerGrid(Grid::gridType(), Grid::factory);
973  OPENVDB_LOG_WARN("delayed loading of grids of type " << Grid::gridType()
974  << " might not be threadsafe on this platform");
975  }
976  }
979 
981 
982 
983 private:
984 #if OPENVDB_ABI_VERSION_NUMBER >= 7
985  Grid(TreePtrType tree, const MetaMap& meta, math::Transform::Ptr xform);
987 #endif
988 
990  static GridBase::Ptr factory() { return Grid::create(); }
991 
992  TreePtrType mTree;
993 }; // class Grid
994 
995 
997 
998 
1006 template<typename GridType>
1007 inline typename GridType::Ptr
1009 {
1010  return GridBase::grid<GridType>(grid);
1011 }
1012 
1013 
1022 template<typename GridType>
1023 inline typename GridType::ConstPtr
1025 {
1026  return GridBase::constGrid<GridType>(grid);
1027 }
1028 
1029 
1031 
1032 
1039 template<typename GridType>
1040 inline typename GridType::Ptr
1042 {
1043  if (!grid || !grid->isType<GridType>()) return typename GridType::Ptr();
1044  return gridPtrCast<GridType>(grid->deepCopyGrid());
1045 }
1046 
1047 
1048 template<typename GridType>
1049 inline typename GridType::Ptr
1051 {
1052  if (!grid.isType<GridType>()) return typename GridType::Ptr();
1053  return gridPtrCast<GridType>(grid.deepCopyGrid());
1054 }
1056 
1057 
1059 
1060 
1062 template<typename _TreeType>
1066 {
1067  using TreeType = _TreeType;
1068  using NonConstTreeType = typename std::remove_const<TreeType>::type;
1069  using TreePtrType = typename TreeType::Ptr;
1070  using ConstTreePtrType = typename TreeType::ConstPtr;
1071  using NonConstTreePtrType = typename NonConstTreeType::Ptr;
1074  using GridPtrType = typename GridType::Ptr;
1077  using ValueType = typename TreeType::ValueType;
1081 
1082  static TreeType& tree(TreeType& t) { return t; }
1083  static TreeType& tree(GridType& g) { return g.tree(); }
1084  static const TreeType& tree(const TreeType& t) { return t; }
1085  static const TreeType& tree(const GridType& g) { return g.tree(); }
1086  static const TreeType& constTree(TreeType& t) { return t; }
1087  static const TreeType& constTree(GridType& g) { return g.constTree(); }
1088  static const TreeType& constTree(const TreeType& t) { return t; }
1089  static const TreeType& constTree(const GridType& g) { return g.constTree(); }
1090 };
1091 
1092 
1094 template<typename _TreeType>
1095 struct TreeAdapter<Grid<_TreeType> >
1096 {
1097  using TreeType = _TreeType;
1098  using NonConstTreeType = typename std::remove_const<TreeType>::type;
1099  using TreePtrType = typename TreeType::Ptr;
1100  using ConstTreePtrType = typename TreeType::ConstPtr;
1101  using NonConstTreePtrType = typename NonConstTreeType::Ptr;
1104  using GridPtrType = typename GridType::Ptr;
1107  using ValueType = typename TreeType::ValueType;
1111 
1112  static TreeType& tree(TreeType& t) { return t; }
1113  static TreeType& tree(GridType& g) { return g.tree(); }
1114  static const TreeType& tree(const TreeType& t) { return t; }
1115  static const TreeType& tree(const GridType& g) { return g.tree(); }
1116  static const TreeType& constTree(TreeType& t) { return t; }
1117  static const TreeType& constTree(GridType& g) { return g.constTree(); }
1118  static const TreeType& constTree(const TreeType& t) { return t; }
1119  static const TreeType& constTree(const GridType& g) { return g.constTree(); }
1120 };
1121 
1123 template<typename _TreeType>
1124 struct TreeAdapter<tree::ValueAccessor<_TreeType> >
1125 {
1126  using TreeType = _TreeType;
1127  using NonConstTreeType = typename std::remove_const<TreeType>::type;
1128  using TreePtrType = typename TreeType::Ptr;
1129  using ConstTreePtrType = typename TreeType::ConstPtr;
1130  using NonConstTreePtrType = typename NonConstTreeType::Ptr;
1133  using GridPtrType = typename GridType::Ptr;
1136  using ValueType = typename TreeType::ValueType;
1140 
1141  static TreeType& tree(TreeType& t) { return t; }
1142  static TreeType& tree(GridType& g) { return g.tree(); }
1143  static TreeType& tree(AccessorType& a) { return a.tree(); }
1144  static const TreeType& tree(const TreeType& t) { return t; }
1145  static const TreeType& tree(const GridType& g) { return g.tree(); }
1146  static const TreeType& tree(const AccessorType& a) { return a.tree(); }
1147  static const TreeType& constTree(TreeType& t) { return t; }
1148  static const TreeType& constTree(GridType& g) { return g.constTree(); }
1149  static const TreeType& constTree(const TreeType& t) { return t; }
1150  static const TreeType& constTree(const GridType& g) { return g.constTree(); }
1151 };
1152 
1154 
1155 
1157 
1158 
1164 template<typename LeafNodeType>
1166  static const bool value = std::is_base_of<io::MultiPass, LeafNodeType>::value;
1167 };
1168 
1169 // Partial specialization for Tree types
1170 template<typename RootNodeType>
1171 struct HasMultiPassIO<tree::Tree<RootNodeType>> {
1172  // A tree is multi-pass if its (root node's) leaf node type is multi-pass.
1174 };
1175 
1176 // Partial specialization for Grid types
1177 template<typename TreeType>
1178 struct HasMultiPassIO<Grid<TreeType>> {
1179  // A grid is multi-pass if its tree's leaf node type is multi-pass.
1181 };
1182 
1183 
1185 
1186 #if OPENVDB_ABI_VERSION_NUMBER >= 7
1188  : MetaMap(meta)
1189  , mTransform(xform)
1190 {
1191  if (!xform) OPENVDB_THROW(ValueError, "Transform pointer is null");
1192 }
1193 #endif
1194 
1195 template<typename GridType>
1196 inline typename GridType::Ptr
1198 {
1199  // The string comparison on type names is slower than a dynamic pointer cast, but
1200  // it is safer when pointers cross DSO boundaries, as they do in many Houdini nodes.
1201  if (grid && grid->type() == GridType::gridType()) {
1202  return StaticPtrCast<GridType>(grid);
1203  }
1204  return typename GridType::Ptr();
1205 }
1206 
1207 
1208 template<typename GridType>
1209 inline typename GridType::ConstPtr
1211 {
1212  return ConstPtrCast<const GridType>(
1213  GridBase::grid<GridType>(ConstPtrCast<GridBase>(grid)));
1214 }
1215 
1216 
1217 template<typename GridType>
1218 inline typename GridType::ConstPtr
1220 {
1221  return ConstPtrCast<const GridType>(GridBase::grid<GridType>(grid));
1222 }
1223 
1224 
1225 template<typename GridType>
1226 inline typename GridType::ConstPtr
1228 {
1229  return ConstPtrCast<const GridType>(
1230  GridBase::grid<GridType>(ConstPtrCast<GridBase>(grid)));
1231 }
1232 
1233 
1234 inline TreeBase::Ptr
1236 {
1237  return ConstPtrCast<TreeBase>(this->constBaseTreePtr());
1238 }
1239 
1240 
1241 inline void
1243 {
1244  if (!xform) OPENVDB_THROW(ValueError, "Transform pointer is null");
1245  mTransform = xform;
1246 }
1247 
1248 
1250 
1251 
1252 template<typename TreeT>
1253 inline Grid<TreeT>::Grid(): mTree(new TreeType)
1254 {
1255 }
1256 
1257 
1258 template<typename TreeT>
1259 inline Grid<TreeT>::Grid(const ValueType &background): mTree(new TreeType(background))
1260 {
1261 }
1262 
1263 
1264 template<typename TreeT>
1265 inline Grid<TreeT>::Grid(TreePtrType tree): mTree(tree)
1266 {
1267  if (!tree) OPENVDB_THROW(ValueError, "Tree pointer is null");
1268 }
1269 
1270 
1271 #if OPENVDB_ABI_VERSION_NUMBER >= 7
1272 template<typename TreeT>
1273 inline Grid<TreeT>::Grid(TreePtrType tree, const MetaMap& meta, math::Transform::Ptr xform):
1274  GridBase(meta, xform),
1275  mTree(tree)
1276 {
1277  if (!tree) OPENVDB_THROW(ValueError, "Tree pointer is null");
1278 }
1279 #endif
1280 
1281 
1282 template<typename TreeT>
1283 inline Grid<TreeT>::Grid(const Grid& other):
1284  GridBase(other),
1285  mTree(StaticPtrCast<TreeType>(other.mTree->copy()))
1286 {
1287 }
1288 
1289 
1290 template<typename TreeT>
1291 template<typename OtherTreeType>
1293  GridBase(other),
1294  mTree(new TreeType(other.constTree()))
1295 {
1296 }
1297 
1298 
1299 template<typename TreeT>
1301  GridBase(other),
1302  mTree(other.mTree)
1303 {
1304 }
1305 
1306 
1307 template<typename TreeT>
1308 inline Grid<TreeT>::Grid(const GridBase& other):
1309  GridBase(other),
1310  mTree(new TreeType)
1311 {
1312 }
1313 
1314 
1315 //static
1316 template<typename TreeT>
1317 inline typename Grid<TreeT>::Ptr
1319 {
1320  return Grid::create(zeroVal<ValueType>());
1321 }
1322 
1323 
1324 //static
1325 template<typename TreeT>
1326 inline typename Grid<TreeT>::Ptr
1328 {
1329  return Ptr(new Grid(background));
1330 }
1331 
1332 
1333 //static
1334 template<typename TreeT>
1335 inline typename Grid<TreeT>::Ptr
1337 {
1338  return Ptr(new Grid(tree));
1339 }
1340 
1341 
1342 //static
1343 template<typename TreeT>
1344 inline typename Grid<TreeT>::Ptr
1346 {
1347  return Ptr(new Grid(other));
1348 }
1349 
1350 
1352 
1353 
1354 template<typename TreeT>
1355 inline typename Grid<TreeT>::ConstPtr
1357 {
1358  return ConstPtr{new Grid{*const_cast<Grid*>(this), ShallowCopy{}}};
1359 }
1360 
1361 
1362 #if OPENVDB_ABI_VERSION_NUMBER >= 7
1363 template<typename TreeT>
1364 inline typename Grid<TreeT>::ConstPtr
1366 {
1367  math::Transform::Ptr transformPtr = ConstPtrCast<math::Transform>(
1368  this->constTransformPtr());
1369  TreePtrType treePtr = ConstPtrCast<TreeT>(this->constTreePtr());
1370  return ConstPtr{new Grid<TreeT>{treePtr, meta, transformPtr}};
1371 }
1372 
1373 template<typename TreeT>
1374 inline typename Grid<TreeT>::ConstPtr
1376 {
1377  return this->copyReplacingMetadataAndTransform(*this, xform);
1378 }
1379 
1380 template<typename TreeT>
1381 inline typename Grid<TreeT>::ConstPtr
1383  math::Transform::Ptr xform) const
1384 {
1385  TreePtrType treePtr = ConstPtrCast<TreeT>(this->constTreePtr());
1386  return ConstPtr{new Grid<TreeT>{treePtr, meta, xform}};
1387 }
1388 #endif
1389 
1390 
1391 template<typename TreeT>
1392 inline typename Grid<TreeT>::Ptr
1394 {
1395  return Ptr{new Grid{*this, ShallowCopy{}}};
1396 }
1397 
1398 
1399 template<typename TreeT>
1400 inline typename Grid<TreeT>::Ptr
1402 {
1403  Ptr result{new Grid{*const_cast<Grid*>(this), ShallowCopy{}}};
1404  result->newTree();
1405  return result;
1406 }
1407 
1408 
1409 template<typename TreeT>
1410 inline GridBase::Ptr
1412 {
1413  return this->copy();
1414 }
1415 
1416 template<typename TreeT>
1417 inline GridBase::ConstPtr
1419 {
1420  return this->copy();
1421 }
1422 
1423 #if OPENVDB_ABI_VERSION_NUMBER >= 7
1424 template<typename TreeT>
1425 inline GridBase::ConstPtr
1427 {
1428  return this->copyReplacingMetadata(meta);
1429 }
1430 
1431 template<typename TreeT>
1432 inline GridBase::ConstPtr
1434 {
1435  return this->copyReplacingTransform(xform);
1436 }
1437 
1438 template<typename TreeT>
1439 inline GridBase::ConstPtr
1441  math::Transform::Ptr xform) const
1442 {
1443  return this->copyReplacingMetadataAndTransform(meta, xform);
1444 }
1445 #endif
1446 
1447 template<typename TreeT>
1448 inline GridBase::Ptr
1450 {
1451  return this->copyWithNewTree();
1452 }
1453 
1454 
1456 
1457 
1458 template<typename TreeT>
1459 inline void
1461 {
1462  if (!tree) OPENVDB_THROW(ValueError, "Tree pointer is null");
1463  if (tree->type() != TreeType::treeType()) {
1464  OPENVDB_THROW(TypeError, "Cannot assign a tree of type "
1465  + tree->type() + " to a grid of type " + this->type());
1466  }
1467  mTree = StaticPtrCast<TreeType>(tree);
1468 }
1469 
1470 
1471 template<typename TreeT>
1472 inline void
1474 {
1475  mTree.reset(new TreeType(this->background()));
1476 }
1477 
1478 
1480 
1481 
1482 template<typename TreeT>
1483 inline void
1484 Grid<TreeT>::sparseFill(const CoordBBox& bbox, const ValueType& value, bool active)
1485 {
1486  tree().sparseFill(bbox, value, active);
1487 }
1488 
1489 
1490 template<typename TreeT>
1491 inline void
1492 Grid<TreeT>::fill(const CoordBBox& bbox, const ValueType& value, bool active)
1493 {
1494  this->sparseFill(bbox, value, active);
1495 }
1496 
1497 template<typename TreeT>
1498 inline void
1499 Grid<TreeT>::denseFill(const CoordBBox& bbox, const ValueType& value, bool active)
1500 {
1501  tree().denseFill(bbox, value, active);
1502 }
1503 
1504 template<typename TreeT>
1505 inline void
1506 Grid<TreeT>::pruneGrid(float tolerance)
1507 {
1509  const auto value = zeroVal<ValueType>() + tolerance;
1511  this->tree().prune(static_cast<ValueType>(value));
1512 }
1513 
1514 template<typename TreeT>
1515 inline void
1516 Grid<TreeT>::clip(const CoordBBox& bbox)
1517 {
1518  tree().clip(bbox);
1519 }
1520 
1521 template<typename TreeT>
1522 inline void
1524 {
1525  tree().merge(other.tree(), policy);
1526 }
1527 
1528 
1529 template<typename TreeT>
1530 template<typename OtherTreeType>
1531 inline void
1533 {
1534  tree().topologyUnion(other.tree());
1535 }
1536 
1537 
1538 template<typename TreeT>
1539 template<typename OtherTreeType>
1540 inline void
1542 {
1543  tree().topologyIntersection(other.tree());
1544 }
1545 
1546 
1547 template<typename TreeT>
1548 template<typename OtherTreeType>
1549 inline void
1551 {
1552  tree().topologyDifference(other.tree());
1553 }
1554 
1555 
1557 
1558 
1559 template<typename TreeT>
1560 inline void
1562 {
1563  tree().evalMinMax(minVal, maxVal);
1564 }
1565 
1566 
1567 template<typename TreeT>
1568 inline CoordBBox
1570 {
1571  CoordBBox bbox;
1572  tree().evalActiveVoxelBoundingBox(bbox);
1573  return bbox;
1574 }
1575 
1576 
1577 template<typename TreeT>
1578 inline Coord
1580 {
1581  Coord dim;
1582  const bool nonempty = tree().evalActiveVoxelDim(dim);
1583  return (nonempty ? dim : Coord());
1584 }
1585 
1586 
1588 
1589 
1592 
1593 template<typename TreeT>
1594 inline void
1595 Grid<TreeT>::readTopology(std::istream& is)
1596 {
1597  tree().readTopology(is, saveFloatAsHalf());
1598 }
1599 
1600 
1601 template<typename TreeT>
1602 inline void
1603 Grid<TreeT>::writeTopology(std::ostream& os) const
1604 {
1605  tree().writeTopology(os, saveFloatAsHalf());
1606 }
1607 
1608 
1609 template<typename TreeT>
1610 inline void
1611 Grid<TreeT>::readBuffers(std::istream& is)
1612 {
1614  tree().readBuffers(is, saveFloatAsHalf());
1615  } else {
1616  uint16_t numPasses = 1;
1617  is.read(reinterpret_cast<char*>(&numPasses), sizeof(uint16_t));
1619  assert(bool(meta));
1620  for (uint16_t passIndex = 0; passIndex < numPasses; ++passIndex) {
1621  uint32_t pass = (uint32_t(numPasses) << 16) | uint32_t(passIndex);
1622  meta->setPass(pass);
1623  tree().readBuffers(is, saveFloatAsHalf());
1624  }
1625  }
1626 }
1627 
1628 
1631 template<typename TreeT>
1632 inline void
1633 Grid<TreeT>::readBuffers(std::istream& is, const CoordBBox& bbox)
1634 {
1636  tree().readBuffers(is, bbox, saveFloatAsHalf());
1637  } else {
1638  uint16_t numPasses = 1;
1639  is.read(reinterpret_cast<char*>(&numPasses), sizeof(uint16_t));
1641  assert(bool(meta));
1642  for (uint16_t passIndex = 0; passIndex < numPasses; ++passIndex) {
1643  uint32_t pass = (uint32_t(numPasses) << 16) | uint32_t(passIndex);
1644  meta->setPass(pass);
1645  tree().readBuffers(is, saveFloatAsHalf());
1646  }
1647  // Cannot clip inside readBuffers() when using multiple passes,
1648  // so instead clip afterwards.
1649  tree().clip(bbox);
1650  }
1651 }
1652 
1653 
1654 template<typename TreeT>
1655 inline void
1657 {
1658  tree().readNonresidentBuffers();
1659 }
1660 
1661 
1662 template<typename TreeT>
1663 inline void
1664 Grid<TreeT>::writeBuffers(std::ostream& os) const
1665 {
1666  if (!hasMultiPassIO()) {
1667  tree().writeBuffers(os, saveFloatAsHalf());
1668  } else {
1669  // Determine how many leaf buffer passes are required for this grid
1671  assert(bool(meta));
1672  uint16_t numPasses = 1;
1673  meta->setCountingPasses(true);
1674  meta->setPass(0);
1675  tree().writeBuffers(os, saveFloatAsHalf());
1676  numPasses = static_cast<uint16_t>(meta->pass());
1677  os.write(reinterpret_cast<const char*>(&numPasses), sizeof(uint16_t));
1678  meta->setCountingPasses(false);
1679 
1680  // Save out the data blocks of the grid.
1681  for (uint16_t passIndex = 0; passIndex < numPasses; ++passIndex) {
1682  uint32_t pass = (uint32_t(numPasses) << 16) | uint32_t(passIndex);
1683  meta->setPass(pass);
1684  tree().writeBuffers(os, saveFloatAsHalf());
1685  }
1686  }
1687 }
1688 
1689 
1690 //static
1691 template<typename TreeT>
1692 inline bool
1694 {
1696 }
1697 
1698 
1699 template<typename TreeT>
1700 inline void
1701 Grid<TreeT>::print(std::ostream& os, int verboseLevel) const
1702 {
1703  tree().print(os, verboseLevel);
1704 
1705  if (metaCount() > 0) {
1706  os << "Additional metadata:" << std::endl;
1707  for (ConstMetaIterator it = beginMeta(), end = endMeta(); it != end; ++it) {
1708  os << " " << it->first;
1709  if (it->second) {
1710  const std::string value = it->second->str();
1711  if (!value.empty()) os << ": " << value;
1712  }
1713  os << "\n";
1714  }
1715  }
1716 
1717  os << "Transform:" << std::endl;
1718  transform().print(os, /*indent=*/" ");
1719  os << std::endl;
1720 }
1721 
1722 
1724 
1725 
1726 template<typename GridType>
1727 inline typename GridType::Ptr
1728 createGrid(const typename GridType::ValueType& background)
1729 {
1730  return GridType::create(background);
1731 }
1732 
1733 
1734 template<typename GridType>
1735 inline typename GridType::Ptr
1737 {
1738  return GridType::create();
1739 }
1740 
1741 
1742 template<typename TreePtrType>
1745 {
1746  using TreeType = typename TreePtrType::element_type;
1747  return Grid<TreeType>::create(tree);
1748 }
1749 
1750 
1751 template<typename GridType>
1752 typename GridType::Ptr
1754 {
1755  using ValueType = typename GridType::ValueType;
1756 
1757  // GridType::ValueType is required to be a floating-point scalar.
1758  static_assert(std::is_floating_point<ValueType>::value,
1759  "level-set grids must be floating-point-valued");
1760 
1761  typename GridType::Ptr grid = GridType::create(
1762  /*background=*/static_cast<ValueType>(voxelSize * halfWidth));
1763  grid->setTransform(math::Transform::createLinearTransform(voxelSize));
1764  grid->setGridClass(GRID_LEVEL_SET);
1765  return grid;
1766 }
1767 
1768 
1770 
1771 
1772 namespace internal {
1773 
1775 template<typename OpT, typename GridBaseT, typename T, typename ...Ts>
1776 struct GridApplyImpl { static bool apply(GridBaseT&, OpT&) { return false; } };
1777 
1778 // Partial specialization for (nonempty) TypeLists
1780 template<typename OpT, typename GridBaseT, typename GridT, typename ...GridTs>
1781 struct GridApplyImpl<OpT, GridBaseT, TypeList<GridT, GridTs...>>
1782 {
1783  static bool apply(GridBaseT& grid, OpT& op)
1784  {
1785  if (grid.template isType<GridT>()) {
1786  op(static_cast<typename CopyConstness<GridBaseT, GridT>::Type&>(grid));
1787  return true;
1788  }
1789  return GridApplyImpl<OpT, GridBaseT, TypeList<GridTs...>>::apply(grid, op);
1790  }
1791 };
1792 
1793 } // namespace internal
1794 
1795 
1796 template<typename GridTypeListT, typename OpT>
1797 inline bool
1798 GridBase::apply(OpT& op) const
1799 {
1800  return internal::GridApplyImpl<OpT, const GridBase, GridTypeListT>::apply(*this, op);
1801 }
1802 
1803 template<typename GridTypeListT, typename OpT>
1804 inline bool
1806 {
1807  return internal::GridApplyImpl<OpT, GridBase, GridTypeListT>::apply(*this, op);
1808 }
1809 
1810 template<typename GridTypeListT, typename OpT>
1811 inline bool
1812 GridBase::apply(const OpT& op) const
1813 {
1814  return internal::GridApplyImpl<const OpT, const GridBase, GridTypeListT>::apply(*this, op);
1815 }
1816 
1817 template<typename GridTypeListT, typename OpT>
1818 inline bool
1819 GridBase::apply(const OpT& op)
1820 {
1821  return internal::GridApplyImpl<const OpT, GridBase, GridTypeListT>::apply(*this, op);
1822 }
1823 
1824 } // namespace OPENVDB_VERSION_NAME
1825 } // namespace openvdb
1826 
1827 #endif // OPENVDB_GRID_HAS_BEEN_INCLUDED
static void unregisterGrid(const Name &type)
Remove a grid type from the registry.
#define OPENVDB_NO_TYPE_CONVERSION_WARNING_BEGIN
Bracket code with OPENVDB_NO_TYPE_CONVERSION_WARNING_BEGIN/_END, to inhibit warnings about type conve...
Definition: Platform.h:196
GridPtrSet::const_iterator GridPtrSetCIter
Definition: Grid.h:521
_TreeType TreeType
Definition: Grid.h:1097
static void unregisterGrid()
Remove this grid type from the registry.
Definition: Grid.h:978
math::Transform::ConstPtr transformPtr() const
Return a pointer to this grid&#39;s transform, which might be shared with other grids.
Definition: Grid.h:402
typename _TreeType::ValueAllCIter ValueAllCIter
Definition: Grid.h:588
const TreeType & tree() const
Return a reference to this grid&#39;s tree, which might be shared with other grids.
Definition: Grid.h:906
typename std::remove_const< TreeType >::type NonConstTreeType
Definition: Grid.h:1068
ConstUnsafeAccessor getConstUnsafeAccessor() const
Return an unsafe accessor that provides random read-only access to this grid&#39;s voxels.
Definition: Grid.h:755
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:1401
#define OPENVDB_API
Helper macros for defining library symbol visibility.
Definition: Platform.h:230
ConstPtr copyReplacingMetadata(const MetaMap &meta) const
Return a new grid of the same type as this grid whose tree and transform is shared with this grid and...
Definition: Grid.h:1365
typename NonConstTreeType::Ptr NonConstTreePtrType
Definition: Grid.h:1130
static const char *const META_FILE_BBOX_MAX
Definition: Grid.h:357
static const char *const META_SAVE_HALF_FLOAT
Definition: Grid.h:353
static const Real LEVEL_SET_HALF_WIDTH
Definition: Types.h:460
GridBase()
Initialize with an identity linear transform.
Definition: Grid.h:481
static TreeType & tree(TreeType &t)
Definition: Grid.h:1112
void clip(const CoordBBox &) override
Clip this grid to the given index-space bounding box.
Definition: Grid.h:1516
Definition: ValueAccessor.h:193
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:1532
static bool isRegistered()
Return true if this grid type is registered.
Definition: Grid.h:967
Vec3d voxelSize(const Vec3d &xyz) const
Return the size of this grid&#39;s voxel at position (x, y, z).
Definition: Grid.h:432
static Ptr createGrid(const Name &type)
Create a new grid of the given (registered) type.
Definition: Types.h:506
Abstract base class for typed grids.
Definition: Grid.h:77
GridBase::ConstPtr copyGridReplacingMetadataAndTransform(const MetaMap &meta, math::Transform::Ptr xform) const override
Return a new grid of the same type as this grid whose tree is shared with this grid and whose transfo...
Definition: Grid.h:1440
Ptr deepCopy() const
Return a new grid whose metadata, transform and tree are deep copies of this grid&#39;s.
Definition: Grid.h:704
static const TreeType & constTree(GridType &g)
Definition: Grid.h:1117
GridPtrVec::iterator GridPtrVecIter
Definition: Grid.h:510
bool apply(OpT &) const
If this grid resolves to one of the listed grid types, invoke the given functor on the resolved grid...
Definition: Grid.h:1798
static const char *const META_GRID_NAME
Definition: Grid.h:352
Vec3< double > Vec3d
Definition: Vec3.h:662
SharedPtr< Grid > Ptr
Definition: Grid.h:574
UnsafeAccessor getUnsafeAccessor()
Return an unsafe accessor that provides random read and write access to this grid&#39;s voxels...
Definition: Grid.h:743
typename tree::ValueAccessor< TreeType > AccessorType
Definition: Grid.h:1108
GridNamePred(const Name &_name)
Definition: Grid.h:533
SharedPtr< const GridBase > ConstPtr
Definition: Grid.h:81
Name name
Definition: Grid.h:535
typename tree::ValueAccessor< NonConstTreeType > NonConstAccessorType
Definition: Grid.h:1139
static const TreeType & constTree(GridType &g)
Definition: Grid.h:1087
typename TreeType::ConstPtr ConstTreePtrType
Definition: Grid.h:1100
typename _TreeType::ValueOnCIter ValueOnCIter
Definition: Grid.h:584
typename GridType::ConstPtr ConstGridPtrType
Definition: Grid.h:1135
virtual TreeBase::ConstPtr constBaseTreePtr() const =0
Return a pointer to this grid&#39;s tree, which might be shared with other grids. The pointer is guarante...
bool hasUniformVoxels() const
Return true if the voxels in world space are uniformly sized cubes.
Definition: Grid.h:434
math::Transform::ConstPtr constTransformPtr() const
Return a pointer to this grid&#39;s transform, which might be shared with other grids.
Definition: Grid.h:403
GridBase(GridBase &other, ShallowCopy)
Copy another grid&#39;s metadata but share its transform.
Definition: Grid.h:493
typename NonConstGridType::Ptr NonConstGridPtrType
Definition: Grid.h:1134
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:1603
#define OPENVDB_THROW(exception, message)
Definition: Exceptions.h:82
const math::Transform & transform() const
Return a reference to this grid&#39;s transform, which might be shared with other grids.
Definition: Grid.h:411
std::string Name
Definition: Name.h:17
std::shared_ptr< T > SharedPtr
Definition: Types.h:91
typename TreeType::ValueType ValueType
Definition: Grid.h:1077
bool operator()(const GridBase::ConstPtr &g) const
Definition: Grid.h:534
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:1024
static const TreeType & tree(const GridType &g)
Definition: Grid.h:1115
TreePtrType treePtr()
Return a pointer to this grid&#39;s tree, which might be shared with other grids. The pointer is guarante...
Definition: Grid.h:895
static TreeType & tree(GridType &g)
Definition: Grid.h:1083
typename tree::ValueAccessor< NonConstTreeType > NonConstAccessorType
Definition: Grid.h:1080
typename _TreeType::ValueOffCIter ValueOffCIter
Definition: Grid.h:586
static const TreeType & constTree(const TreeType &t)
Definition: Grid.h:1088
Base class for typed trees.
Definition: Tree.h:35
static bool isRegistered(const Name &type)
Return true if the given grid type name is registered.
Name type() const override
Return the name of this grid&#39;s type.
Definition: Grid.h:712
static const TreeType & constTree(TreeType &t)
Definition: Grid.h:1116
Grid()
Construct a new grid with background value zero.
Definition: Grid.h:1253
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:1008
const TreeType & constTree() const
Return a reference to this grid&#39;s tree, which might be shared with other grids.
Definition: Grid.h:907
static TreeType & tree(TreeType &t)
Definition: Grid.h:1141
SharedPtr< GridBase > Ptr
Definition: Grid.h:80
CoordBBox evalActiveVoxelBoundingBox() const override
Return the axis-aligned bounding box of all active voxels.
Definition: Grid.h:1569
void pruneGrid(float tolerance=0.0) override
Reduce the memory footprint of this grid by increasing its sparseness.
Definition: Grid.h:1506
Coord evalActiveVoxelDim() const override
Return the dimensions of the axis-aligned bounding box of all active voxels.
Definition: Grid.h:1579
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:1050
static const char *const META_FILE_MEM_BYTES
Definition: Grid.h:359
bool isType() const
Return true if this grid is of the same type as the template parameter.
Definition: Grid.h:148
MetadataMap::const_iterator ConstMetaIterator
Definition: MetaMap.h:28
math::Transform & transform()
Return a reference to this grid&#39;s transform, which might be shared with other grids.
Definition: Grid.h:410
typename tree::ValueAccessor< TreeType > AccessorType
Definition: Grid.h:1137
SharedPtr< const Transform > ConstPtr
Definition: Transform.h:43
typename std::remove_const< TreeType >::type NonConstTreeType
Definition: Grid.h:1098
typename tree::ValueAccessor< TreeType > AccessorType
Definition: Grid.h:1078
GridBase::Ptr copyGrid() override
Return a new grid of the same type as this grid whose metadata is a deep copy of this grid&#39;s and whos...
Definition: Grid.h:1411
static const char *const META_GRID_CLASS
Definition: Grid.h:350
std::set< GridBase::Ptr > GridPtrSet
Definition: Grid.h:519
double Real
Definition: Types.h:37
Name valueType() const override
Return the name of the type of a voxel&#39;s value (e.g., "float" or "vec3d").
Definition: Grid.h:717
Vec3d voxelSize() const
Return the size of this grid&#39;s voxels.
Definition: Grid.h:429
GridBase(const GridBase &other)
Deep copy another grid&#39;s metadata and transform.
Definition: Grid.h:490
typename GridType::ConstPtr ConstGridPtrType
Definition: Grid.h:1076
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:1393
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:173
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:1484
static Ptr create()
Return a new grid with background value zero.
Definition: Grid.h:1318
GridPtrVec::const_iterator GridPtrVecCIter
Definition: Grid.h:511
#define OPENVDB_NO_TYPE_CONVERSION_WARNING_END
Definition: Platform.h:197
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:1492
~GridBase() override
Definition: Grid.h:86
~Grid() override
Definition: Grid.h:641
static const TreeType & tree(const TreeType &t)
Definition: Grid.h:1114
SharedPtr< T > StaticPtrCast(const SharedPtr< U > &ptr)
Return a new shared pointer that points to the same object as the given pointer after a static_cast...
Definition: Types.h:123
typename TreeType::ConstPtr ConstTreePtrType
Definition: Grid.h:1070
typename TreeType::Ptr TreePtrType
Definition: Grid.h:1099
static TreeType & tree(GridType &g)
Definition: Grid.h:1113
static const char *const META_VECTOR_TYPE
Definition: Grid.h:355
Vec3d indexToWorld(const Coord &ijk) const
Apply this grid&#39;s transform to the given coordinates.
Definition: Grid.h:438
typename tree::ValueAccessor< _TreeType, false > UnsafeAccessor
Definition: Grid.h:592
typename _TreeType::ValueOnIter ValueOnIter
Definition: Grid.h:583
TreeBase & baseTree()
Return a reference to this grid&#39;s tree, which might be shared with other grids.
Definition: Grid.h:182
ValueConverter<T>::Type is the type of a grid having the same hierarchy as this grid but a different ...
Definition: Grid.h:602
Definition: Exceptions.h:65
Metafunction that specifies whether a given leaf node, tree, or grid type requires multiple passes to...
Definition: Grid.h:1165
ValueOnIter beginValueOn()
Return an iterator over all of this grid&#39;s active values (tile and voxel).
Definition: Grid.h:758
Container that maps names (strings) to values of arbitrary types.
Definition: MetaMap.h:19
ValueAllCIter beginValueAll() const
Return an iterator over all of this grid&#39;s values (tile and voxel).
Definition: Grid.h:772
virtual GridBase::Ptr deepCopyGrid() const =0
Return a new grid whose metadata, transform and tree are deep copies of this grid&#39;s.
SharedPtr< const TreeBase > ConstPtr
Definition: Tree.h:39
static const char *const META_FILE_BBOX_MIN
Definition: Grid.h:356
static TreeType & tree(GridType &g)
Definition: Grid.h:1142
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:1753
openvdb::GridBase Grid
Definition: Utils.h:33
typename std::remove_const< ToType >::type Type
Definition: Types.h:298
typename std::remove_const< TreeType >::type NonConstTreeType
Definition: Grid.h:1127
SharedPtr< MetaMap > Ptr
Definition: MetaMap.h:22
GridCPtrVec::const_iterator GridCPtrVecCIter
Definition: Grid.h:516
SharedPtr< TreeBase > Ptr
Definition: Tree.h:38
#define OPENVDB_VERSION_NAME
The version namespace name for this library version.
Definition: version.h:102
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:898
typename GridType::Ptr GridPtrType
Definition: Grid.h:1074
SharedPtr< GridPtrVec > GridPtrVecPtr
Definition: Grid.h:512
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:897
SharedPtr< GridCPtrSet > GridCPtrSetPtr
Definition: Grid.h:527
GridBase::ConstPtr copyGridReplacingMetadata(const MetaMap &meta) const override
Return a new grid of the same type as this grid whose tree and transform is shared with this grid and...
Definition: Grid.h:1426
ValueOnCIter beginValueOn() const
Return an iterator over all of this grid&#39;s active values (tile and voxel).
Definition: Grid.h:760
static const char *const META_GRID_CREATOR
Definition: Grid.h:351
static const char *const META_FILE_COMPRESSION
Definition: Grid.h:358
Vec3d indexToWorld(const Vec3d &xyz) const
Apply this grid&#39;s transform to the given coordinates.
Definition: Grid.h:436
MetaIterator beginMeta()
Definition: MetaMap.h:84
_TreeType TreeType
Definition: Grid.h:577
TreeType & tree()
Return a reference to this grid&#39;s tree, which might be shared with other grids.
Definition: Grid.h:905
ConstAccessor getConstAccessor() const
Return an accessor that provides random read-only access to this grid&#39;s voxels.
Definition: Grid.h:747
void setTree(TreeBase::Ptr) override
Associate the given tree with this grid, in place of its existing tree.
Definition: Grid.h:1460
tree::TreeBase TreeBase
Definition: Grid.h:26
SharedPtr< GridPtrSet > GridPtrSetPtr
Definition: Grid.h:522
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...
This adapter allows code that is templated on a Tree type to accept either a Tree type or a Grid type...
Definition: Grid.h:1065
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:896
typename _TreeType::ValueAllIter ValueAllIter
Definition: Grid.h:587
SharedPtr< const Grid > ConstPtr
Definition: Grid.h:575
void newTree() override
Associate a new, empty tree with this grid, in place of its existing tree.
Definition: Grid.h:1473
const ValueType & background() const
Return this grid&#39;s background value.
Definition: Grid.h:725
static const char *const META_IS_LOCAL_SPACE
Definition: Grid.h:354
typename TreeType::Ptr TreePtrType
Definition: Grid.h:1069
Accessor getAccessor()
Return an accessor that provides random read and write access to this grid&#39;s voxels.
Definition: Grid.h:735
math::Transform::Ptr transformPtr()
Return a pointer to this grid&#39;s transform, which might be shared with other grids.
Definition: Grid.h:401
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:706
Definition: Exceptions.h:13
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:1219
typename _TreeType::ConstPtr ConstTreePtrType
Definition: Grid.h:579
bool empty() const override
Return true if this grid contains only inactive background voxels.
Definition: Grid.h:728
const math::Transform & constTransform() const
Return a reference to this grid&#39;s transform, which might be shared with other grids.
Definition: Grid.h:412
ConstAccessor getAccessor() const
Return an accessor that provides random read-only access to this grid&#39;s voxels.
Definition: Grid.h:745
static Transform::Ptr createLinearTransform(double voxelSize=1.0)
Create and return a shared pointer to a new transform.
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:1541
ValueOnCIter cbeginValueOn() const
Return an iterator over all of this grid&#39;s active values (tile and voxel).
Definition: Grid.h:762
GridCPtrVec::iterator GridCPtrVecIter
Definition: Grid.h:515
ValueAllIter beginValueAll()
Return an iterator over all of this grid&#39;s values (tile and voxel).
Definition: Grid.h:770
MergePolicy
Definition: Types.h:505
typename _TreeType::ValueType ValueType
Definition: Grid.h:580
void print(std::ostream &os=std::cout, const std::string &indent="") const
Print a description of this transform.
void clear() override
Empty this grid, so that all voxels become inactive background voxels.
Definition: Grid.h:730
VecType
Definition: Types.h:482
typename tree::ValueAccessor< _TreeType, true > Accessor
Definition: Grid.h:590
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:1449
static const TreeType & tree(const AccessorType &a)
Definition: Grid.h:1146
static void registerGrid(const Name &type, GridFactory)
Register a grid type along with a factory function.
std::vector< GridBase::Ptr > GridPtrVec
Definition: Grid.h:509
typename tree::ValueAccessor< const TreeType > ConstAccessorType
Definition: Grid.h:1079
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:1595
static const TreeType & tree(const TreeType &t)
Definition: Grid.h:1144
Definition: Transform.h:39
static const TreeType & constTree(const TreeType &t)
Definition: Grid.h:1118
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:1197
ValueOffCIter cbeginValueOff() const
Return an iterator over all of this grid&#39;s inactive values (tile and voxel).
Definition: Grid.h:768
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:1550
void evalMinMax(ValueType &minVal, ValueType &maxVal) const
Return the minimum and maximum active values in this grid.
Definition: Grid.h:1561
static const TreeType & constTree(const GridType &g)
Definition: Grid.h:1150
typename tree::ValueAccessor< const _TreeType, true > ConstAccessor
Definition: Grid.h:591
GridCPtrSet::const_iterator GridCPtrSetCIter
Definition: Grid.h:526
static const TreeType & constTree(GridType &g)
Definition: Grid.h:1148
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 ...
typename NonConstTreeType::Ptr NonConstTreePtrType
Definition: Grid.h:1101
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:552
static TreeType & tree(TreeType &t)
Definition: Grid.h:1082
OPENVDB_API uint32_t getFormatVersion(std::ios_base &)
Return the file format version number associated with the given input stream.
static bool hasMultiPassIO()
Return true if grids of this type require multiple I/O passes to read and write data buffers...
Definition: Grid.h:1693
void merge(Grid &other, MergePolicy policy=MERGE_ACTIVE_STATES)
Efficiently merge another grid into this grid using one of several schemes.
Definition: Grid.h:1523
std::vector< GridBase::ConstPtr > GridCPtrVec
Definition: Grid.h:514
static const TreeType & constTree(const GridType &g)
Definition: Grid.h:1119
Container class that associates a tree with a transform and metadata.
Definition: Grid.h:28
typename NonConstGridType::Ptr NonConstGridPtrType
Definition: Grid.h:1105
static TreeType & tree(AccessorType &a)
Definition: Grid.h:1143
typename tree::ValueAccessor< const TreeType > ConstAccessorType
Definition: Grid.h:1138
#define OPENVDB_LOG_WARN(message)
Log a warning message of the form &#39;someVar << "some text" << ...&#39;.
Definition: logging.h:253
Vec3d worldToIndex(const Vec3d &xyz) const
Apply the inverse of this grid&#39;s transform to the given coordinates.
Definition: Grid.h:440
void readTransform(std::istream &is)
Read in the transform for this grid.
Definition: Grid.h:469
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:1235
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:1499
static const TreeType & constTree(TreeType &t)
Definition: Grid.h:1086
OPENVDB_API SharedPtr< StreamMetadata > getStreamMetadataPtr(std::ios_base &)
Return a shared pointer to an object that stores metadata (file format, compression scheme...
typename _TreeType::BuildType BuildType
Definition: Grid.h:581
ValueAllCIter cbeginValueAll() const
Return an iterator over all of this grid&#39;s values (tile and voxel).
Definition: Grid.h:774
static Name gridType()
Return the name of this type of grid.
Definition: Grid.h:714
typename TreeType::ValueType ValueType
Definition: Grid.h:1107
typename NonConstGridType::Ptr NonConstGridPtrType
Definition: Grid.h:1075
static const char *const META_FILE_VOXEL_COUNT
Definition: Grid.h:360
uint64_t Index64
Definition: Types.h:30
GridClass
Definition: Types.h:452
ValueOffCIter beginValueOff() const
Return an iterator over all of this grid&#39;s inactive values (tile and voxel).
Definition: Grid.h:766
static const TreeType & constTree(const TreeType &t)
Definition: Grid.h:1149
typename TreeType::ValueType ValueType
Definition: Grid.h:1136
GridPtrSet::iterator GridPtrSetIter
Definition: Grid.h:520
ValueOffIter beginValueOff()
Return an iterator over all of this grid&#39;s inactive values (tile and voxel).
Definition: Grid.h:764
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h:154
GridCPtrSet::iterator GridCPtrSetIter
Definition: Grid.h:525
SharedPtr< GridCPtrVec > GridCPtrVecPtr
Definition: Grid.h:517
void writeTransform(std::ostream &os) const
Write out the transform for this grid.
Definition: Grid.h:471
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:348
void readBuffers(std::istream &) override
Read all data buffers for this grid.
Definition: Grid.h:1611
std::set< GridBase::ConstPtr > GridCPtrSet
Definition: Grid.h:524
GridBase::ConstPtr copyGridReplacingTransform(math::Transform::Ptr xform) const override
Return a new grid of the same type as this grid whose tree is shared with this grid, whose metadata is a deep copy of this grid&#39;s and whose transform is provided as an argument.
Definition: Grid.h:1433
typename tree::ValueAccessor< const _TreeType, false > ConstUnsafeAccessor
Definition: Grid.h:593
size_t metaCount() const
Definition: MetaMap.h:91
static const TreeType & constTree(const GridType &g)
Definition: Grid.h:1089
static const TreeType & constTree(TreeType &t)
Definition: Grid.h:1147
typename GridType::ConstPtr ConstGridPtrType
Definition: Grid.h:1106
typename _TreeType::Ptr TreePtrType
Definition: Grid.h:578
typename GridType::Ptr GridPtrType
Definition: Grid.h:1104
typename tree::ValueAccessor< NonConstTreeType > NonConstAccessorType
Definition: Grid.h:1110
ConstPtr copyReplacingTransform(math::Transform::Ptr xform) const
Return a new grid of the same type as this grid whose tree is shared with this grid, whose metadata is a deep copy of this grid&#39;s and whose transform is provided as an argument.
Definition: Grid.h:1375
const TreeBase & constBaseTree() const
Return a reference to this grid&#39;s tree, which might be shared with other grids.
Definition: Grid.h:192
ConstPtr copyReplacingMetadataAndTransform(const MetaMap &meta, math::Transform::Ptr xform) const
Return a new grid of the same type as this grid whose tree is shared with this grid and whose transfo...
Definition: Grid.h:1382
Predicate functor that returns true for grids that have a specified name.
Definition: Grid.h:531
typename TreeType::Ptr TreePtrType
Definition: Grid.h:1128
static const TreeType & tree(const GridType &g)
Definition: Grid.h:1085
Grid< typename TreePtrType::element_type >::Ptr createGrid(TreePtrType)
Create a new grid of the appropriate type that wraps the given tree.
Definition: Grid.h:1744
static void registerGrid()
Register this grid type along with a factory function.
Definition: Grid.h:969
const TreeBase & baseTree() const
Return a reference to this grid&#39;s tree, which might be shared with other grids.
Definition: Grid.h:187
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:1656
_TreeType TreeType
Definition: Grid.h:1067
MetaIterator endMeta()
Definition: MetaMap.h:85
static const TreeType & tree(const TreeType &t)
Definition: Grid.h:1084
typename _TreeType::ValueOffIter ValueOffIter
Definition: Grid.h:585
typename tree::ValueAccessor< const TreeType > ConstAccessorType
Definition: Grid.h:1109
Definition: Exceptions.h:64
bool saveFloatAsHalf() const
Return true if this grid should be written out with floating-point voxel values (including components...
Definition: Types.h:454
Index64 memUsage() const override
Definition: Grid.h:884
Index64 activeVoxelCount() const override
Return the number of active voxels.
Definition: Grid.h:874
void writeBuffers(std::ostream &) const override
Write out all data buffers for this grid.
Definition: Grid.h:1664
void setTransform(math::Transform::Ptr)
Associate the given transform with this grid, in place of its existing transform. ...
Definition: Grid.h:1242
typename NonConstTreeType::Ptr NonConstTreePtrType
Definition: Grid.h:1071
static const TreeType & tree(const GridType &g)
Definition: Grid.h:1145
SharedPtr< Transform > Ptr
Definition: Transform.h:42
void print(std::ostream &=std::cout, int verboseLevel=1) const override
Output a human-readable description of this grid.
Definition: Grid.h:1701
SharedPtr< StreamMetadata > Ptr
Definition: io.h:33
Tag dispatch class that distinguishes shallow copy constructors from deep copy constructors.
Definition: Types.h:678
typename GridType::Ptr GridPtrType
Definition: Grid.h:1133
typename TreeType::ConstPtr ConstTreePtrType
Definition: Grid.h:1129
static const char *const META_FILE_DELAYED_LOAD
Definition: Grid.h:361