33 #ifndef OPENVDB_TREE_TREE_HAS_BEEN_INCLUDED
34 #define OPENVDB_TREE_TREE_HAS_BEEN_INCLUDED
39 #include <boost/shared_ptr.hpp>
40 #include <boost/cstdint.hpp>
41 #include <tbb/atomic.h>
42 #include <tbb/concurrent_hash_map.h>
43 #include <openvdb/Types.h>
44 #include <openvdb/metadata/Metadata.h>
45 #include <openvdb/math/Math.h>
46 #include <openvdb/math/BBox.h>
47 #include <openvdb/util/Formats.h>
48 #include <openvdb/util/logging.h>
49 #include <openvdb/Platform.h>
67 typedef boost::shared_ptr<TreeBase>
Ptr;
68 typedef boost::shared_ptr<const TreeBase>
ConstPtr;
74 virtual const Name& type()
const = 0;
77 virtual Name valueType()
const = 0;
96 virtual bool evalLeafBoundingBox(CoordBBox& bbox)
const = 0;
101 virtual bool evalLeafDim(Coord& dim)
const = 0;
110 virtual bool evalActiveVoxelBoundingBox(CoordBBox& bbox)
const = 0;
115 virtual bool evalActiveVoxelDim(Coord& dim)
const = 0;
117 virtual void getIndexRange(CoordBBox& bbox)
const = 0;
126 virtual Index treeDepth()
const = 0;
128 virtual Index32 leafCount()
const = 0;
130 virtual Index32 nonLeafCount()
const = 0;
132 virtual Index64 activeLeafVoxelCount()
const = 0;
134 virtual Index64 inactiveLeafVoxelCount()
const = 0;
136 virtual Index64 activeVoxelCount()
const = 0;
138 virtual Index64 inactiveVoxelCount()
const = 0;
150 virtual void readTopology(std::istream&,
bool saveFloatAsHalf =
false);
154 virtual void writeTopology(std::ostream&,
bool saveFloatAsHalf =
false)
const;
157 virtual void readBuffers(std::istream&,
bool saveFloatAsHalf =
false) = 0;
159 virtual void writeBuffers(std::ostream&,
bool saveFloatAsHalf =
false)
const = 0;
165 virtual void print(std::ostream& os = std::cout,
int verboseLevel = 1)
const;
177 template<
typename _RootNodeType>
181 typedef boost::shared_ptr<Tree>
Ptr;
188 static const Index DEPTH = RootNodeType::LEVEL + 1;
196 template<
typename OtherValueType>
218 template<
typename OtherTreeType>
219 Tree(
const OtherTreeType& other,
224 mRoot(other.getRootNode(), inactiveValue, activeValue,
TopologyCopy())
238 template<
typename OtherTreeType>
248 virtual ~Tree() { releaseAllAccessors(); }
257 static const Name& treeType();
259 virtual const Name&
type()
const {
return this->treeType(); }
265 RootNodeType& root() {
return mRoot; }
279 template<
typename OtherRootNodeType>
282 virtual bool evalLeafBoundingBox(CoordBBox& bbox)
const;
283 virtual bool evalActiveVoxelBoundingBox(CoordBBox& bbox)
const;
284 virtual bool evalActiveVoxelDim(Coord& dim)
const;
285 virtual bool evalLeafDim(Coord& dim)
const;
290 static void getNodeLog2Dims(std::vector<Index>& dims);
299 virtual void readTopology(std::istream&,
bool saveFloatAsHalf =
false);
303 virtual void writeTopology(std::ostream&,
bool saveFloatAsHalf =
false)
const;
305 virtual void readBuffers(std::istream&,
bool saveFloatAsHalf =
false);
307 virtual void writeBuffers(std::ostream&,
bool saveFloatAsHalf =
false)
const;
309 virtual void print(std::ostream& os = std::cout,
int verboseLevel = 1)
const;
330 virtual Index64 inactiveVoxelCount()
const;
337 void evalMinMax(ValueType &
min, ValueType &
max)
const;
346 const ValueType& getValue(
const Coord& xyz)
const;
349 template<
typename AccessT>
const ValueType& getValue(
const Coord& xyz, AccessT&)
const;
354 int getValueDepth(
const Coord& xyz)
const;
357 void setActiveState(
const Coord& xyz,
bool on);
359 void setValueOnly(
const Coord& xyz,
const ValueType& value);
361 void setValueOn(
const Coord& xyz);
363 void setValueOn(
const Coord& xyz,
const ValueType& value);
365 void setValue(
const Coord& xyz,
const ValueType& value);
368 template<
typename AccessT>
void setValue(
const Coord& xyz,
const ValueType& value, AccessT&);
370 void setValueOff(
const Coord& xyz);
372 void setValueOff(
const Coord& xyz,
const ValueType& value);
392 template<
typename ModifyOp>
393 void modifyValue(
const Coord& xyz,
const ModifyOp& op);
414 template<
typename ModifyOp>
415 void modifyValueAndActiveState(
const Coord& xyz,
const ModifyOp& op);
419 bool probeValue(
const Coord& xyz, ValueType& value)
const;
422 bool isValueOn(
const Coord& xyz)
const {
return mRoot.isValueOn(xyz); }
424 bool isValueOff(
const Coord& xyz)
const {
return !this->isValueOn(xyz); }
437 void fill(
const CoordBBox& bbox,
const ValueType& value,
bool active =
true);
445 template<
typename PruneOp>
void pruneOp(PruneOp&);
450 void prune(
const ValueType& tolerance = zeroVal<ValueType>());
454 void pruneInactive(
const ValueType&);
458 void pruneInactive();
466 void pruneLevelSet();
476 void addTile(
Index level,
const Coord& xyz,
const ValueType& value,
bool active);
482 template<
typename NodeT>
483 NodeT* stealNode(
const Coord& xyz,
const ValueType& value,
bool active);
490 LeafNodeType* touchLeaf(
const Coord& xyz);
493 template<
typename NodeType> NodeType* probeNode(
const Coord& xyz);
496 template<
typename NodeType>
const NodeType* probeConstNode(
const Coord& xyz)
const;
497 template<
typename NodeType>
const NodeType* probeNode(
const Coord& xyz)
const;
501 LeafNodeType* probeLeaf(
const Coord& xyz);
504 const LeafNodeType* probeConstLeaf(
const Coord& xyz)
const;
514 bool empty()
const {
return mRoot.empty(); }
517 void clear() { this->clearAllAccessors(); mRoot.clear(); }
520 void clearAllAccessors();
544 virtual void getIndexRange(CoordBBox& bbox)
const { mRoot.getIndexRange(bbox); }
557 void signedFloodFill(
const ValueType& outside,
const ValueType& inside);
560 void voxelizeActiveTiles();
584 template<
typename OtherRootNodeType>
600 template<
typename OtherRootNodeType>
613 template<
typename OtherRootNodeType>
661 template<
typename CombineOp>
662 void combine(
Tree& other, CombineOp& op,
bool prune =
false);
664 template<
typename CombineOp>
665 void combine(
Tree& other,
const CombineOp& op,
bool prune =
false);
707 template<
typename ExtendedCombineOp>
708 void combineExtended(
Tree& other, ExtendedCombineOp& op,
bool prune =
false);
710 template<
typename ExtendedCombineOp>
711 void combineExtended(
Tree& other,
const ExtendedCombineOp& op,
bool prune =
false);
739 template<
typename CombineOp>
740 void combine2(
const Tree& a,
const Tree& b, CombineOp& op,
bool prune =
false);
742 template<
typename CombineOp>
743 void combine2(
const Tree& a,
const Tree& b,
const CombineOp& op,
bool prune =
false);
785 template<
typename ExtendedCombineOp>
786 void combine2Extended(
const Tree& a,
const Tree& b, ExtendedCombineOp& op,
789 template<
typename ExtendedCombineOp>
790 void combine2Extended(
const Tree& a,
const Tree& b,
const ExtendedCombineOp&,
840 template<
typename BBoxOp>
void visitActiveBBox(BBoxOp& op)
const { mRoot.visitActiveBBox(op); }
896 template<
typename VisitorOp>
void visit(VisitorOp& op);
897 template<
typename VisitorOp>
void visit(
const VisitorOp& op);
903 template<
typename VisitorOp>
void visit(VisitorOp& op)
const;
904 template<
typename VisitorOp>
void visit(
const VisitorOp& op)
const;
954 template<
typename OtherTreeType,
typename VisitorOp>
955 void visit2(OtherTreeType& other, VisitorOp& op);
956 template<
typename OtherTreeType,
typename VisitorOp>
957 void visit2(OtherTreeType& other,
const VisitorOp& op);
969 template<
typename OtherTreeType,
typename VisitorOp>
970 void visit2(OtherTreeType& other, VisitorOp& op)
const;
971 template<
typename OtherTreeType,
typename VisitorOp>
972 void visit2(OtherTreeType& other,
const VisitorOp& op)
const;
979 typename RootNodeType::ChildOnCIter beginRootChildren()
const {
return mRoot.cbeginChildOn(); }
986 typename RootNodeType::ChildOffCIter beginRootTiles()
const {
return mRoot.cbeginChildOff(); }
988 typename RootNodeType::ChildOffCIter
cbeginRootTiles()
const {
return mRoot.cbeginChildOff(); }
989 typename RootNodeType::ChildOffIter
beginRootTiles() {
return mRoot.beginChildOff(); }
993 typename RootNodeType::ChildAllCIter beginRootDense()
const {
return mRoot.cbeginChildAll(); }
995 typename RootNodeType::ChildAllCIter
cbeginRootDense()
const {
return mRoot.cbeginChildAll(); }
996 typename RootNodeType::ChildAllIter
beginRootDense() {
return mRoot.beginChildAll(); }
1020 LeafIter beginLeaf() {
return LeafIter(*
this); }
1040 ValueOnIter beginValueOn() {
return ValueOnIter(*
this); }
1046 ValueOffIter beginValueOff() {
return ValueOffIter(*
this); }
1054 template<
typename IterT> IterT begin();
1057 template<
typename CIterT> CIterT cbegin()
const;
1069 void releaseAllAccessors();
1085 template<
typename T, Index N1, Index N2>
1095 template<
typename T, Index N1, Index N2, Index N3>
1105 template<
typename T, Index N1, Index N2, Index N3, Index N4>
1118 int32_t bufferCount;
1119 is.read(reinterpret_cast<char*>(&bufferCount),
sizeof(int32_t));
1120 if (bufferCount != 1)
OPENVDB_LOG_WARN(
"multi-buffer trees are no longer supported");
1127 int32_t bufferCount = 1;
1128 os.write(reinterpret_cast<char*>(&bufferCount),
sizeof(int32_t));
1135 os <<
" Tree Type: " << type()
1136 <<
" Active Voxel Count: " << activeVoxelCount() << std::endl
1137 <<
" Inactive Voxel Count: " << inactiveVoxelCount() << std::endl
1138 <<
" Leaf Node Count: " << leafCount() << std::endl
1139 <<
" Non-leaf Node Count: " << nonLeafCount() << std::endl;
1154 template<
typename TreeT>
struct TreeIterTraits<TreeT, typename TreeT::RootNodeType::ChildOnIter> {
1155 static typename TreeT::RootNodeType::ChildOnIter
begin(TreeT& tree) {
1156 return tree.beginRootChildren();
1160 template<
typename TreeT>
struct TreeIterTraits<TreeT, typename TreeT::RootNodeType::ChildOnCIter> {
1161 static typename TreeT::RootNodeType::ChildOnCIter
begin(
const TreeT& tree) {
1162 return tree.cbeginRootChildren();
1166 template<
typename TreeT>
struct TreeIterTraits<TreeT, typename TreeT::RootNodeType::ChildOffIter> {
1167 static typename TreeT::RootNodeType::ChildOffIter
begin(TreeT& tree) {
1168 return tree.beginRootTiles();
1172 template<
typename TreeT>
struct TreeIterTraits<TreeT, typename TreeT::RootNodeType::ChildOffCIter> {
1173 static typename TreeT::RootNodeType::ChildOffCIter
begin(
const TreeT& tree) {
1174 return tree.cbeginRootTiles();
1178 template<
typename TreeT>
struct TreeIterTraits<TreeT, typename TreeT::RootNodeType::ChildAllIter> {
1179 static typename TreeT::RootNodeType::ChildAllIter
begin(TreeT& tree) {
1180 return tree.beginRootDense();
1184 template<
typename TreeT>
struct TreeIterTraits<TreeT, typename TreeT::RootNodeType::ChildAllCIter> {
1185 static typename TreeT::RootNodeType::ChildAllCIter
begin(
const TreeT& tree) {
1186 return tree.cbeginRootDense();
1191 static typename TreeT::NodeIter
begin(TreeT& tree) {
return tree.beginNode(); }
1195 static typename TreeT::NodeCIter
begin(
const TreeT& tree) {
return tree.cbeginNode(); }
1199 static typename TreeT::LeafIter
begin(TreeT& tree) {
return tree.beginLeaf(); }
1203 static typename TreeT::LeafCIter
begin(
const TreeT& tree) {
return tree.cbeginLeaf(); }
1207 static typename TreeT::ValueOnIter
begin(TreeT& tree) {
return tree.beginValueOn(); }
1210 template<
typename TreeT>
struct TreeIterTraits<TreeT, typename TreeT::ValueOnCIter> {
1211 static typename TreeT::ValueOnCIter
begin(
const TreeT& tree) {
return tree.cbeginValueOn(); }
1214 template<
typename TreeT>
struct TreeIterTraits<TreeT, typename TreeT::ValueOffIter> {
1215 static typename TreeT::ValueOffIter
begin(TreeT& tree) {
return tree.beginValueOff(); }
1218 template<
typename TreeT>
struct TreeIterTraits<TreeT, typename TreeT::ValueOffCIter> {
1219 static typename TreeT::ValueOffCIter
begin(
const TreeT& tree) {
return tree.cbeginValueOff(); }
1222 template<
typename TreeT>
struct TreeIterTraits<TreeT, typename TreeT::ValueAllIter> {
1223 static typename TreeT::ValueAllIter
begin(TreeT& tree) {
return tree.beginValueAll(); }
1226 template<
typename TreeT>
struct TreeIterTraits<TreeT, typename TreeT::ValueAllCIter> {
1227 static typename TreeT::ValueAllCIter
begin(
const TreeT& tree) {
return tree.cbeginValueAll(); }
1231 template<
typename RootNodeType>
1232 template<
typename IterT>
1240 template<
typename RootNodeType>
1241 template<
typename IterT>
1252 template<
typename RootNodeType>
1256 this->clearAllAccessors();
1258 mRoot.readTopology(is, saveFloatAsHalf);
1262 template<
typename RootNodeType>
1267 mRoot.writeTopology(os, saveFloatAsHalf);
1271 template<
typename RootNodeType>
1275 this->clearAllAccessors();
1276 mRoot.readBuffers(is, saveFloatAsHalf);
1280 template<
typename RootNodeType>
1291 template<
typename RootNodeType>
1295 typename AccessorRegistry::accessor a;
1296 mAccessorRegistry.insert(a, &accessor);
1300 template<
typename RootNodeType>
1304 typename ConstAccessorRegistry::accessor a;
1305 mConstAccessorRegistry.insert(a, &accessor);
1309 template<
typename RootNodeType>
1313 mAccessorRegistry.erase(&accessor);
1317 template<
typename RootNodeType>
1321 mConstAccessorRegistry.erase(&accessor);
1325 template<
typename RootNodeType>
1329 for (
typename AccessorRegistry::iterator it = mAccessorRegistry.begin();
1330 it != mAccessorRegistry.end(); ++it)
1332 if (it->first) it->first->
clear();
1335 for (
typename ConstAccessorRegistry::iterator it = mConstAccessorRegistry.begin();
1336 it != mConstAccessorRegistry.end(); ++it)
1338 if (it->first) it->first->clear();
1343 template<
typename RootNodeType>
1347 mAccessorRegistry.erase(NULL);
1348 for (
typename AccessorRegistry::iterator it = mAccessorRegistry.begin();
1349 it != mAccessorRegistry.end(); ++it)
1351 it->first->release();
1353 mAccessorRegistry.
clear();
1355 mAccessorRegistry.erase(NULL);
1356 for (
typename ConstAccessorRegistry::iterator it = mConstAccessorRegistry.begin();
1357 it != mConstAccessorRegistry.end(); ++it)
1359 it->first->release();
1361 mConstAccessorRegistry.clear();
1368 template<
typename RootNodeType>
1369 inline const typename RootNodeType::ValueType&
1376 template<
typename RootNodeType>
1377 template<
typename AccessT>
1378 inline const typename RootNodeType::ValueType&
1385 template<
typename RootNodeType>
1393 template<
typename RootNodeType>
1401 template<
typename RootNodeType>
1409 template<
typename RootNodeType>
1417 template<
typename RootNodeType>
1424 template<
typename RootNodeType>
1431 template<
typename RootNodeType>
1432 template<
typename AccessT>
1440 template<
typename RootNodeType>
1448 template<
typename RootNodeType>
1456 template<
typename RootNodeType>
1457 template<
typename ModifyOp>
1465 template<
typename RootNodeType>
1466 template<
typename ModifyOp>
1474 template<
typename RootNodeType>
1485 template<
typename RootNodeType>
1486 template<
typename PruneOp>
1490 this->clearAllAccessors();
1495 template<
typename RootNodeType>
1504 template<
typename RootNodeType>
1513 template<
typename RootNodeType>
1517 this->pruneInactive(this->background());
1521 template<
typename RootNodeType>
1530 template<
typename RootNodeType>
1535 mRoot.
addTile(level, xyz, value, active);
1539 template<
typename RootNodeType>
1540 template<
typename NodeT>
1544 this->clearAllAccessors();
1545 return mRoot.template stealNode<NodeT>(xyz, value, active);
1549 template<
typename RootNodeType>
1550 inline typename RootNodeType::LeafNodeType*
1557 template<
typename RootNodeType>
1558 inline typename RootNodeType::LeafNodeType*
1565 template<
typename RootNodeType>
1566 inline const typename RootNodeType::LeafNodeType*
1573 template<
typename RootNodeType>
1574 template<
typename NodeType>
1578 return mRoot.template probeNode<NodeType>(xyz);
1582 template<
typename RootNodeType>
1583 template<
typename NodeType>
1584 inline const NodeType*
1587 return this->
template probeConstNode<NodeType>(xyz);
1591 template<
typename RootNodeType>
1592 template<
typename NodeType>
1593 inline const NodeType*
1596 return mRoot.template probeConstNode<NodeType>(xyz);
1603 template<
typename RootNodeType>
1607 this->clearAllAccessors();
1608 return mRoot.fill(bbox, value, active);
1612 template<
typename RootNodeType>
1620 template<
typename RootNodeType>
1628 if (MetadataT* m = dynamic_cast<MetadataT*>(result.get())) {
1629 m->value() = mRoot.background();
1639 template<
typename RootNodeType>
1643 this->clearAllAccessors();
1644 mRoot.voxelizeActiveTiles();
1648 template<
typename RootNodeType>
1652 this->clearAllAccessors();
1656 mRoot.template merge<MERGE_ACTIVE_STATES>(other.
mRoot);
break;
1658 mRoot.template merge<MERGE_NODES>(other.
mRoot);
break;
1660 mRoot.template merge<MERGE_ACTIVE_STATES_AND_NODES>(other.
mRoot);
break;
1665 template<
typename RootNodeType>
1666 template<
typename OtherRootNodeType>
1670 this->clearAllAccessors();
1674 template<
typename RootNodeType>
1675 template<
typename OtherRootNodeType>
1679 this->clearAllAccessors();
1683 template<
typename RootNodeType>
1684 template<
typename OtherRootNodeType>
1688 this->clearAllAccessors();
1697 template<
typename ValueT,
typename CombineOp>
1703 op(args.
a(), args.
b(), args.
result());
1710 template<
typename RootNodeType>
1711 template<
typename CombineOp>
1716 this->combineExtended(other, extendedOp, prune);
1723 template<
typename RootNodeType>
1724 template<
typename CombineOp>
1729 this->combineExtended(other, extendedOp, prune);
1734 template<
typename RootNodeType>
1735 template<
typename ExtendedCombineOp>
1739 this->clearAllAccessors();
1747 template<
typename RootNodeType>
1748 template<
typename ExtendedCombineOp>
1752 this->clearAllAccessors();
1753 mRoot.template combine<const ExtendedCombineOp>(other.
mRoot, op, prune);
1758 template<
typename RootNodeType>
1759 template<
typename CombineOp>
1764 this->combine2Extended(a, b, extendedOp, prune);
1771 template<
typename RootNodeType>
1772 template<
typename CombineOp>
1777 this->combine2Extended(a, b, extendedOp, prune);
1782 template<
typename RootNodeType>
1783 template<
typename ExtendedCombineOp>
1786 ExtendedCombineOp& op,
bool prune)
1788 this->clearAllAccessors();
1796 template<
typename RootNodeType>
1797 template<
typename ExtendedCombineOp>
1800 const ExtendedCombineOp& op,
bool prune)
1802 this->clearAllAccessors();
1803 mRoot.template combine2<const ExtendedCombineOp>(a.
mRoot, b.
mRoot, op, prune);
1811 template<
typename RootNodeType>
1812 template<
typename VisitorOp>
1816 this->clearAllAccessors();
1817 mRoot.template visit<VisitorOp>(op);
1821 template<
typename RootNodeType>
1822 template<
typename VisitorOp>
1826 mRoot.template visit<VisitorOp>(op);
1832 template<
typename RootNodeType>
1833 template<
typename VisitorOp>
1837 this->clearAllAccessors();
1838 mRoot.template visit<const VisitorOp>(op);
1844 template<
typename RootNodeType>
1845 template<
typename VisitorOp>
1849 mRoot.template visit<const VisitorOp>(op);
1856 template<
typename RootNodeType>
1857 template<
typename OtherTreeType,
typename VisitorOp>
1861 this->clearAllAccessors();
1862 typedef typename OtherTreeType::RootNodeType OtherRootNodeType;
1863 mRoot.template visit2<OtherRootNodeType, VisitorOp>(other.getRootNode(), op);
1867 template<
typename RootNodeType>
1868 template<
typename OtherTreeType,
typename VisitorOp>
1872 typedef typename OtherTreeType::RootNodeType OtherRootNodeType;
1873 mRoot.template visit2<OtherRootNodeType, VisitorOp>(other.getRootNode(), op);
1879 template<
typename RootNodeType>
1880 template<
typename OtherTreeType,
typename VisitorOp>
1884 this->clearAllAccessors();
1885 typedef typename OtherTreeType::RootNodeType OtherRootNodeType;
1886 mRoot.template visit2<OtherRootNodeType, const VisitorOp>(other.getRootNode(), op);
1892 template<
typename RootNodeType>
1893 template<
typename OtherTreeType,
typename VisitorOp>
1897 typedef typename OtherTreeType::RootNodeType OtherRootNodeType;
1898 mRoot.template visit2<OtherRootNodeType, const VisitorOp>(other.getRootNode(), op);
1905 template<
typename RootNodeType>
1909 static tbb::atomic<const Name*> sTypeName;
1910 if (sTypeName == NULL) {
1911 std::vector<Index> dims;
1912 Tree::getNodeLog2Dims(dims);
1913 std::ostringstream ostr;
1914 ostr <<
"Tree_" << typeNameAsString<ValueType>();
1915 for (
size_t i = 1, N = dims.size(); i < N; ++i) {
1916 ostr <<
"_" << dims[i];
1919 if (sTypeName.compare_and_swap(s, NULL) != NULL)
delete s;
1925 template<
typename RootNodeType>
1926 template<
typename OtherRootNodeType>
1934 template<
typename RootNodeType>
1939 this->evalActiveVoxelDim(dim);
1941 totalVoxels = dim.x() * dim.y() * dim.z(),
1942 activeVoxels = this->activeVoxelCount();
1943 assert(totalVoxels >= activeVoxels);
1944 return totalVoxels - activeVoxels;
1948 template<
typename RootNodeType>
1954 if (this->empty())
return false;
1956 mRoot.evalActiveBoundingBox(bbox,
false);
1961 template<
typename RootNodeType>
1968 if (this->empty())
return false;
1970 mRoot.evalActiveBoundingBox(bbox,
true);
1976 template<
typename RootNodeType>
1981 bool notEmpty = this->evalActiveVoxelBoundingBox(bbox);
1982 dim = bbox.extents();
1987 template<
typename RootNodeType>
1992 bool notEmpty = this->evalLeafBoundingBox(bbox);
1993 dim = bbox.extents();
1998 template<
typename RootNodeType>
2002 minVal = maxVal = zeroVal<ValueType>();
2004 minVal = maxVal = *iter;
2005 for (++iter; iter; ++iter) {
2007 if (val < minVal) minVal = val;
2008 if (val > maxVal) maxVal = val;
2014 template<
typename RootNodeType>
2019 RootNodeType::getNodeLog2Dims(dims);
2023 template<
typename RootNodeType>
2027 if (verboseLevel <= 0)
return;
2031 std::streamsize savedPrecision;
2032 OnExit(std::ostream& os): os(os), savedPrecision(os.precision()) {}
2033 ~OnExit() { os.precision(savedPrecision); }
2035 OnExit restorePrecision(os);
2037 std::vector<Index> dims;
2038 Tree::getNodeLog2Dims(dims);
2040 std::vector<Index64> nodeCount;
2042 os <<
"Information about Tree:\n"
2043 <<
" Type: " << this->type() <<
"\n";
2045 os <<
" Configuration:\n";
2046 if (verboseLevel <= 1) {
2048 os <<
" Root(" << mRoot.getTableSize() <<
")";
2049 if (dims.size() > 1) {
2050 for (
size_t i = 1, N = dims.size() - 1; i < N; ++i) {
2051 os <<
", Internal(" << (1 << dims[i]) <<
"^3)";
2053 os <<
", Leaf(" << (1 << *dims.rbegin()) <<
"^3)\n";
2057 nodeCount.resize(dims.size());
2058 for (
NodeCIter it = cbeginNode(); it; ++it) {
2059 ++(nodeCount[it.getDepth()]);
2061 os <<
" Root(1 x " << mRoot.getTableSize() <<
")";
2062 if (dims.size() > 1) {
2063 for (
size_t i = 1, N = dims.size() - 1; i < N; ++i) {
2065 os <<
" x " << (1 << dims[i]) <<
"^3)";
2068 os <<
" x " << (1 << *dims.rbegin()) <<
"^3)\n";
2071 os <<
" Background value: " << mRoot.background() <<
"\n";
2073 if (verboseLevel == 1)
return;
2077 if (nodeCount.empty()) {
2078 nodeCount.resize(dims.size());
2079 for (
NodeCIter it = cbeginNode(); it; ++it) {
2080 ++(nodeCount[it.getDepth()]);
2086 this->evalMinMax(minVal, maxVal);
2087 os <<
" Min value: " << minVal <<
"\n";
2088 os <<
" Max value: " << maxVal <<
"\n";
2091 leafCount = *nodeCount.rbegin(),
2092 numActiveVoxels = this->activeVoxelCount(),
2093 numActiveLeafVoxels = this->activeLeafVoxelCount();
2098 uint64_t totalVoxels = 0;
2099 if (numActiveVoxels) {
2101 this->evalActiveVoxelBoundingBox(bbox);
2102 dim = bbox.extents();
2103 totalVoxels = dim.x() * uint64_t(dim.y()) * dim.z();
2105 os <<
" Bounding box of active voxels: " << bbox <<
"\n";
2106 os <<
" Dimensions of active voxels: "
2107 << dim[0] <<
" x " << dim[1] <<
" x " << dim[2] <<
"\n";
2109 const double activeRatio = (100.0 * numActiveVoxels) / totalVoxels;
2110 os <<
" Percentage of active voxels: " << std::setprecision(3) << activeRatio <<
"%\n";
2113 const double fillRatio =
2114 (100.0 * numActiveLeafVoxels) / (leafCount * LeafNodeType::NUM_VOXELS);
2115 os <<
" Average leaf node fill ratio: " << fillRatio <<
"%\n";
2118 os <<
" Tree is empty!\n";
2122 if (verboseLevel == 2)
return;
2126 actualMem = this->memUsage(),
2127 denseMem =
sizeof(
ValueType) * totalVoxels,
2128 voxelsMem =
sizeof(
ValueType) * numActiveLeafVoxels;
2131 os <<
"Memory footprint:\n";
2135 if (numActiveVoxels) {
2137 os <<
" Actual footprint is " << (100.0 * actualMem / denseMem)
2138 <<
"% of dense* footprint\n";
2139 os <<
" Leaf voxel footprint is " << (100.0 * voxelsMem / actualMem)
2140 <<
"% of actual footprint\n";
2141 os <<
" *Dense refers to the smallest equivalent non-sparse volume" << std::endl;
2149 #endif // OPENVDB_TREE_TREE_HAS_BEEN_INCLUDED
bool operator!=(const Tree &) const
Definition: Tree.h:262
Tree(const Tree &other)
Deep copy constructor.
Definition: Tree.h:205
boost::shared_ptr< const TreeBase > ConstPtr
Definition: Tree.h:68
static TreeT::ValueOnCIter begin(const TreeT &tree)
Definition: Tree.h:1211
Tree5<T, N1, N2, N3, N4>::Type is the type of a five-level tree (Root, Internal, Internal, Internal, Leaf) with value type T and internal and leaf node log dimensions N1, N2, N3 and N4, respectively.
Definition: Tree.h:1106
OPENVDB_API Hermite min(const Hermite &, const Hermite &)
min and max operations done directly on the compressed data.
static TreeT::ValueOffCIter begin(const TreeT &tree)
Definition: Tree.h:1219
Base class for tree-traversal iterators over all nodes.
Definition: TreeIterator.h:964
TreeValueIteratorBase< const Tree, typename RootNodeType::ValueOnCIter > ValueOnCIter
Definition: Tree.h:1029
static TreeT::ValueAllCIter begin(const TreeT &tree)
Definition: Tree.h:1227
virtual Index64 inactiveLeafVoxelCount() const
Return the number of inactive voxels stored in leaf nodes.
Definition: Tree.h:326
TreeValueIteratorBase< Tree, typename RootNodeType::ValueAllIter > ValueAllIter
Definition: Tree.h:1026
TreeValueIteratorBase< Tree, typename RootNodeType::ValueOnIter > ValueOnIter
Definition: Tree.h:1028
RootNodeType::ChildAllIter beginRootDense()
Return an iterator over all entries of the root node's table.
Definition: Tree.h:996
virtual const Name & type() const
Return the name of this type of tree.
Definition: Tree.h:259
TreeValueIteratorBase< Tree, typename RootNodeType::ValueOffIter > ValueOffIter
Definition: Tree.h:1030
static TreeT::LeafIter begin(TreeT &tree)
Definition: Tree.h:1199
ValueOnCIter cbeginValueOn() const
Return an iterator over active values (tile and voxel) across all nodes.
Definition: Tree.h:1043
Tree(const OtherTreeType &other, const ValueType &background, TopologyCopy)
Topology copy constructor from a tree of a different type.
Definition: Tree.h:239
void clear()
Remove all tiles from this tree and all nodes other than the root node.
Definition: Tree.h:517
virtual Index64 memUsage() const
Return the total amount of memory in bytes occupied by this tree.
Definition: Tree.h:339
int getValueDepth(const Coord &xyz) const
Return the tree depth (0 = root) at which the value of voxel (x, y, z) resides.
Definition: Tree.h:1387
Tree< RootNode< InternalNode< LeafNode< T, N2 >, N1 > > > Type
Definition: Tree.h:1087
const ValueType & a() const
Get the A input value.
Definition: Types.h:256
Helper class for use with Tree::pruneOp() to replace constant branches (to within the provided tolera...
Definition: tree/Util.h:47
void visitActiveBBox(BBoxOp &op) const
Definition: Tree.h:840
#define OPENVDB_THROW(exception, message)
Definition: Exceptions.h:97
virtual void readTopology(std::istream &, bool saveFloatAsHalf=false)
Read the tree topology from a stream.
Definition: Tree.h:1116
bool isValueOff(const Coord &xyz) const
Return true if the value at the given coordinates is inactive.
Definition: Tree.h:424
bool hasActiveTiles() const
Return true if this tree has any active tiles.
Definition: Tree.h:426
bool isValueOn(const Coord &xyz) const
Return true if the value at the given coordinates is active.
Definition: Tree.h:422
RootNodeType::ChildAllCIter cbeginRootDense() const
Return an iterator over all entries of the root node's table.
Definition: Tree.h:995
NodeCIter cbeginNode() const
Return an iterator over all nodes in this tree.
Definition: Tree.h:1016
Helper class for use with Tree::pruneOp() to replace inactive branches with more memory-efficient ina...
Definition: tree/Util.h:74
const LeafNodeType * probeConstLeaf(const Coord &xyz) const
Return a pointer to the leaf node that contains voxel (x, y, z). If no such node exists, return NULL.
Definition: Tree.h:1567
ValueOnCIter beginValueOn() const
Return an iterator over active values (tile and voxel) across all nodes.
Definition: Tree.h:1042
void clearAllAccessors()
Clear all registered accessors.
Definition: Tree.h:1327
void modifyValue(const Coord &xyz, const ModifyOp &op)
Apply a functor to the value of the voxel at the given coordinates and mark the voxel as active...
Definition: Tree.h:1459
ValueOffCIter beginValueOff() const
Return an iterator over inactive values (tile and voxel) across all nodes.
Definition: Tree.h:1048
TreeIterTraits provides, for all tree iterators, a begin(tree) function that returns an iterator over...
Definition: Tree.h:1152
Index32 Index
Definition: Types.h:56
static TreeT::NodeCIter begin(const TreeT &tree)
Definition: Tree.h:1195
boost::shared_ptr< const Tree > ConstPtr
Definition: Tree.h:182
LeafIteratorBase< const Tree, typename RootNodeType::ChildOnCIter > LeafCIter
Iterator over all leaf nodes in this tree.
Definition: Tree.h:1009
virtual Index32 nonLeafCount() const
Return the number of non-leaf nodes.
Definition: Tree.h:322
ValueOffCIter cbeginValueOff() const
Return an iterator over inactive values (tile and voxel) across all nodes.
Definition: Tree.h:1049
const ValueType & b() const
Get the B input value.
Definition: Types.h:258
Tree< RootNode< InternalNode< InternalNode< InternalNode< LeafNode< T, N4 >, N3 >, N2 >, N1 > > > Type
Definition: Tree.h:1108
RootNodeType::ValueType ValueType
Definition: Tree.h:185
MergePolicy
Definition: Types.h:188
bool hasSameTopology(const Tree< OtherRootNodeType > &other) const
Return true if the given tree has the same node and active value topology as this tree...
Definition: Tree.h:1928
Helper class for use with Tree::pruneOp() to prune any branches whose values are all inactive and rep...
Definition: tree/Util.h:99
static TreeT::ValueOnIter begin(TreeT &tree)
Definition: Tree.h:1207
Helper class to adapt a three-argument (a, b, result) CombineOp functor into a single-argument functo...
Definition: Tree.h:1698
LeafNodeType * touchLeaf(const Coord &xyz)
Return a pointer to the leaf node that contains voxel (x, y, z). If no such node exists, create one that preserves the values and active states of all voxels.
Definition: Tree.h:1551
CombineOp & op
Definition: Tree.h:1706
Base class for tree-traversal iterators over tile and voxel values.
Definition: TreeIterator.h:658
Index64 activeTileCount() const
Definition: Tree.h:334
void addLeaf(LeafNodeType &leaf)
Add the given leaf node to this tree, creating a new branch if necessary. If a leaf node with the sam...
Definition: Tree.h:470
TreeValueIteratorBase< const Tree, typename RootNodeType::ValueOffCIter > ValueOffCIter
Definition: Tree.h:1031
RootNodeType::ChildOnIter beginRootChildren()
Return an iterator over children of the root node.
Definition: Tree.h:982
virtual TreeBase::Ptr copy() const
Return a pointer to a deep copy of this tree.
Definition: Tree.h:251
This base class for ValueAccessors manages registration of an accessor with a tree so that the tree c...
Definition: ValueAccessor.h:102
virtual Metadata::Ptr getBackgroundValue() const
Return this tree's background value wrapped as metadata.
Definition: Tree.h:87
RootNodeType mRoot
Definition: Tree.h:1075
static TreeT::RootNodeType::ChildAllIter begin(TreeT &tree)
Definition: Tree.h:1179
This struct collects both input and output arguments to "grid combiner" functors used with the tree::...
Definition: Types.h:232
Tree4<T, N1, N2, N3>::Type is the type of a four-level tree (Root, Internal, Internal, Leaf) with value type T and internal and leaf node log dimensions N1, N2 and N3, respectively.
Definition: Tree.h:1096
Tree< typename RootNodeType::template ValueConverter< OtherValueType >::Type > Type
Definition: Tree.h:198
Tree< RootNode< InternalNode< InternalNode< LeafNode< T, N3 >, N2 >, N1 > > > Type
Definition: Tree.h:1097
void setValueOn(const Coord &xyz)
Mark the voxel at the given coordinates as active but don't change its value.
Definition: Tree.h:1442
Internal table nodes for OpenVDB trees.
NodeIteratorBase< const Tree, typename RootNodeType::ChildOnCIter > NodeCIter
Iterator over all nodes in this tree.
Definition: Tree.h:1003
Base class for typed trees.
Definition: Tree.h:64
const RootNodeType & getRootNode() const
Return this tree's root node.
Definition: Tree.h:270
#define OPENVDB_VERSION_NAME
Definition: version.h:45
LeafNodeType * probeLeaf(const Coord &xyz)
Return a pointer to the leaf node that contains voxel (x, y, z). If no such node exists, return NULL.
Definition: Tree.h:1559
static TreeT::ValueAllIter begin(TreeT &tree)
Definition: Tree.h:1223
const LeafNodeType * probeLeaf(const Coord &xyz) const
Return a pointer to the leaf node that contains voxel (x, y, z). If no such node exists, return NULL.
Definition: Tree.h:505
#define OPENVDB_LOG_WARN(message)
Definition: logging.h:39
virtual Index64 memUsage() const
Return the total amount of memory in bytes occupied by this tree.
Definition: Tree.h:141
static TreeT::RootNodeType::ChildOnIter begin(TreeT &tree)
Definition: Tree.h:1155
CombineOpAdapter(CombineOp &op)
Definition: Tree.h:1700
ValueConverter<T>::Type is the type of a tree having the same hierarchy as this tree but a different ...
Definition: Tree.h:197
const ValueType & background() const
Return this tree's background value.
Definition: Tree.h:539
uint32_t Index32
Definition: Types.h:54
uint64_t Index64
Definition: Types.h:55
bool probeValue(const Coord &xyz, ValueType &value) const
Get the value of the voxel at the given coordinates.
Definition: Tree.h:1476
Base class for tree-traversal iterators over all leaf nodes (but not leaf voxels) ...
Definition: TreeIterator.h:1211
ValueAllCIter cbeginValueAll() const
Return an iterator over all values (tile and voxel) across all nodes.
Definition: Tree.h:1037
virtual Index treeDepth() const
Return the depth of this tree.
Definition: Tree.h:318
LeafCIter cbeginLeaf() const
Return an iterator over all leaf nodes in this tree.
Definition: Tree.h:1023
void addTile(Index level, const Coord &xyz, const ValueType &value, bool active)
Add a tile containing voxel (x, y, z) at the specified tree level, creating a new branch if necessary...
Definition: Tree.h:1532
void setValueOff(const Coord &xyz)
Mark the voxel at the given coordinates as inactive but don't change its value.
Definition: Tree.h:1395
OPENVDB_API Hermite max(const Hermite &, const Hermite &)
min and max operations done directly on the compressed data.
Tree3<T, N1, N2>::Type is the type of a three-level tree (Root, Internal, Leaf) with value type T and...
Definition: Tree.h:1086
TreeBase()
Definition: Tree.h:70
RootNodeType::LeafNodeType LeafNodeType
Definition: Tree.h:186
static TreeT::RootNodeType::ChildOffIter begin(TreeT &tree)
Definition: Tree.h:1167
boost::shared_ptr< TreeBase > Ptr
Definition: Tree.h:67
virtual void print(std::ostream &os=std::cout, int verboseLevel=1) const
Print statistics, memory usage and other information about this tree.
Definition: Tree.h:1133
AccessorRegistry mAccessorRegistry
Definition: Tree.h:1076
FormattedInt< IntT > formattedInt(IntT n)
Definition: Formats.h:130
tbb::concurrent_hash_map< ValueAccessorBase< const Tree > *, bool > ConstAccessorRegistry
Definition: Tree.h:1062
void setActiveState(const Coord &xyz, bool on)
Set the active state of the voxel at the given coordinates but don't change its value.
Definition: Tree.h:1411
LeafCIter beginLeaf() const
Return an iterator over all leaf nodes in this tree.
Definition: Tree.h:1022
bool empty() const
Return true if this tree contains no nodes other than the root node and no tiles other than backgroun...
Definition: Tree.h:514
const ValueType & result() const
Get the output value.
Definition: Types.h:261
Definition: Exceptions.h:84
Tree()
Definition: Tree.h:202
static TreeT::RootNodeType::ChildAllCIter begin(const TreeT &tree)
Definition: Tree.h:1185
void setValue(const Coord &xyz, const ValueType &value)
Set the value of the voxel at the given coordinates and mark the voxel as active. ...
Definition: Tree.h:1419
virtual ~Tree()
Definition: Tree.h:248
OPENVDB_API int printBytes(std::ostream &os, uint64_t bytes, const std::string &head="", const std::string &tail="\n", bool exact=false, int width=8, int precision=3)
The root node of an OpenVDB tree.
const ValueType & getValue(const Coord &xyz) const
Return the value of the voxel at the given coordinates.
Definition: Tree.h:1370
static TreeT::LeafCIter begin(const TreeT &tree)
Definition: Tree.h:1203
_RootNodeType RootNodeType
Definition: Tree.h:184
static TreeT::NodeIter begin(TreeT &tree)
Definition: Tree.h:1191
NodeCIter beginNode() const
Return an iterator over all nodes in this tree.
Definition: Tree.h:1015
virtual Index32 leafCount() const
Return the number of leaf nodes.
Definition: Tree.h:320
Tree(const OtherTreeType &other, const ValueType &inactiveValue, const ValueType &activeValue, TopologyCopy)
Topology copy constructor from a tree of a different type.
Definition: Tree.h:219
void operator()(CombineArgs< ValueT > &args) const
Definition: Tree.h:1702
static TreeT::ValueOffIter begin(TreeT &tree)
Definition: Tree.h:1215
bool operator==(const Tree &) const
Definition: Tree.h:261
void setValueOnly(const Coord &xyz, const ValueType &value)
Set the value of the voxel at the given coordinates but don't change its active state.
Definition: Tree.h:1426
virtual void writeTopology(std::ostream &, bool saveFloatAsHalf=false) const
Write the tree topology to a stream.
Definition: Tree.h:1125
ConstAccessorRegistry mConstAccessorRegistry
Definition: Tree.h:1077
tbb::concurrent_hash_map< ValueAccessorBase< Tree > *, bool > AccessorRegistry
Definition: Tree.h:1061
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h:67
RootNodeType & getRootNode()
Return this tree's root node.
Definition: Tree.h:269
void modifyValueAndActiveState(const Coord &xyz, const ModifyOp &op)
Apply a functor to the voxel at the given coordinates.
Definition: Tree.h:1468
std::string Name
Definition: Name.h:44
RootNodeType::ChildOffIter beginRootTiles()
Return an iterator over non-child entries of the root node's table.
Definition: Tree.h:989
RootNodeType::ChildOnCIter cbeginRootChildren() const
Return an iterator over children of the root node.
Definition: Tree.h:981
Tree(const ValueType &background)
Empty tree constructor.
Definition: Tree.h:246
TreeValueIteratorBase< const Tree, typename RootNodeType::ValueAllCIter > ValueAllCIter
Definition: Tree.h:1027
static TreeT::RootNodeType::ChildOffCIter begin(const TreeT &tree)
Definition: Tree.h:1173
static TreeT::RootNodeType::ChildOnCIter begin(const TreeT &tree)
Definition: Tree.h:1161
void setBackground(const ValueType &background)
Replace this tree's background value.
Definition: Tree.h:541
ValueAllCIter beginValueAll() const
Return an iterator over all values (tile and voxel) across all nodes.
Definition: Tree.h:1036
virtual ~TreeBase()
Definition: Tree.h:71
const RootNodeType & root() const
Return this tree's root node.
Definition: Tree.h:267
virtual Index64 activeLeafVoxelCount() const
Return the number of active voxels stored in leaf nodes.
Definition: Tree.h:324
boost::shared_ptr< Tree > Ptr
Definition: Tree.h:181
virtual Index64 activeVoxelCount() const
Return the total number of active voxels.
Definition: Tree.h:328
virtual void writeBuffers(std::ostream &, bool saveFloatAsHalf=false) const
Write out all data buffers for this tree.
Definition: Tree.h:1282
RootNodeType::ChildOffCIter cbeginRootTiles() const
Return an iterator over non-child entries of the root node's table.
Definition: Tree.h:988
void signedFloodFill()
Set the values of all inactive voxels and tiles of a narrow-band level set from the signs of the acti...
Definition: Tree.h:550
virtual void getIndexRange(CoordBBox &bbox) const
Min and max are both inclusive.
Definition: Tree.h:544
virtual Name valueType() const
Return the name of the type of a voxel's value (e.g., "float" or "vec3d")
Definition: Tree.h:254