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>
66 typedef boost::shared_ptr<TreeBase>
Ptr;
67 typedef boost::shared_ptr<const TreeBase>
ConstPtr;
73 virtual const Name& type()
const = 0;
76 virtual Name valueType()
const = 0;
95 virtual bool evalLeafBoundingBox(CoordBBox& bbox)
const = 0;
100 virtual bool evalLeafDim(Coord& dim)
const = 0;
109 virtual bool evalActiveVoxelBoundingBox(CoordBBox& bbox)
const = 0;
114 virtual bool evalActiveVoxelDim(Coord& dim)
const = 0;
116 virtual void getIndexRange(CoordBBox& bbox)
const = 0;
118 #ifndef OPENVDB_2_ABI_COMPATIBLE
119 virtual void clipUnallocatedNodes() = 0;
134 virtual Index treeDepth()
const = 0;
136 virtual Index32 leafCount()
const = 0;
138 virtual Index32 nonLeafCount()
const = 0;
140 virtual Index64 activeLeafVoxelCount()
const = 0;
142 virtual Index64 inactiveLeafVoxelCount()
const = 0;
144 virtual Index64 activeVoxelCount()
const = 0;
146 virtual Index64 inactiveVoxelCount()
const = 0;
147 #ifndef OPENVDB_2_ABI_COMPATIBLE
148 virtual Index64 activeTileCount()
const = 0;
162 virtual void readTopology(std::istream&,
bool saveFloatAsHalf =
false);
166 virtual void writeTopology(std::ostream&,
bool saveFloatAsHalf =
false)
const;
169 virtual void readBuffers(std::istream&,
bool saveFloatAsHalf =
false) = 0;
170 #ifndef OPENVDB_2_ABI_COMPATIBLE
171 virtual void readBuffers(std::istream&,
const CoordBBox&,
bool saveFloatAsHalf =
false) = 0;
178 virtual void readNonresidentBuffers()
const = 0;
180 virtual void writeBuffers(std::ostream&,
bool saveFloatAsHalf =
false)
const = 0;
190 virtual void print(std::ostream& os = std::cout,
int verboseLevel = 1)
const;
202 template<
typename _RootNodeType>
206 typedef boost::shared_ptr<Tree>
Ptr;
213 static const Index DEPTH = RootNodeType::LEVEL + 1;
221 template<
typename OtherValueType>
240 template<
typename OtherRootType>
255 template<
typename OtherTreeType>
256 Tree(
const OtherTreeType& other,
257 const ValueType& inactiveValue,
258 const ValueType& activeValue,
261 mRoot(other.root(), inactiveValue, activeValue,
TopologyCopy())
276 template<
typename OtherTreeType>
284 Tree(
const ValueType& background): mRoot(background) {}
286 virtual ~Tree() { releaseAllAccessors(); }
295 static const Name& treeType();
297 virtual const Name&
type()
const {
return this->treeType(); }
303 RootNodeType& root() {
return mRoot; }
305 const RootNodeType&
root()
const {
return mRoot; }
314 template<
typename OtherRootNodeType>
317 virtual bool evalLeafBoundingBox(CoordBBox& bbox)
const;
318 virtual bool evalActiveVoxelBoundingBox(CoordBBox& bbox)
const;
319 virtual bool evalActiveVoxelDim(Coord& dim)
const;
320 virtual bool evalLeafDim(Coord& dim)
const;
325 static void getNodeLog2Dims(std::vector<Index>& dims);
334 virtual void readTopology(std::istream&,
bool saveFloatAsHalf =
false);
338 virtual void writeTopology(std::ostream&,
bool saveFloatAsHalf =
false)
const;
340 virtual void readBuffers(std::istream&,
bool saveFloatAsHalf =
false);
341 #ifndef OPENVDB_2_ABI_COMPATIBLE
342 virtual void readBuffers(std::istream&,
const CoordBBox&,
bool saveFloatAsHalf =
false);
349 virtual void readNonresidentBuffers()
const;
351 virtual void writeBuffers(std::ostream&,
bool saveFloatAsHalf =
false)
const;
354 virtual void print(std::ostream& os = std::cout,
int verboseLevel = 1)
const;
375 virtual Index64 inactiveVoxelCount()
const;
380 void evalMinMax(ValueType &
min, ValueType &
max)
const;
389 const ValueType& getValue(
const Coord& xyz)
const;
392 template<
typename AccessT>
const ValueType& getValue(
const Coord& xyz, AccessT&)
const;
397 int getValueDepth(
const Coord& xyz)
const;
400 void setActiveState(
const Coord& xyz,
bool on);
402 void setValueOnly(
const Coord& xyz,
const ValueType& value);
404 void setValueOn(
const Coord& xyz);
406 void setValueOn(
const Coord& xyz,
const ValueType& value);
408 void setValue(
const Coord& xyz,
const ValueType& value);
411 template<
typename AccessT>
void setValue(
const Coord& xyz,
const ValueType& value, AccessT&);
413 void setValueOff(
const Coord& xyz);
415 void setValueOff(
const Coord& xyz,
const ValueType& value);
435 template<
typename ModifyOp>
436 void modifyValue(
const Coord& xyz,
const ModifyOp& op);
457 template<
typename ModifyOp>
458 void modifyValueAndActiveState(
const Coord& xyz,
const ModifyOp& op);
462 bool probeValue(
const Coord& xyz, ValueType& value)
const;
465 bool isValueOn(
const Coord& xyz)
const {
return mRoot.isValueOn(xyz); }
467 bool isValueOff(
const Coord& xyz)
const {
return !this->isValueOn(xyz); }
472 void clip(
const CoordBBox&);
474 #ifndef OPENVDB_2_ABI_COMPATIBLE
475 virtual void clipUnallocatedNodes();
492 void fill(
const CoordBBox& bbox,
const ValueType& value,
bool active =
true);
498 void prune(
const ValueType& tolerance = zeroVal<ValueType>())
500 this->clearAllAccessors();
501 mRoot.prune(tolerance);
506 void addLeaf(LeafNodeType& leaf) { mRoot.addLeaf(&leaf); }
512 void addTile(
Index level,
const Coord& xyz,
const ValueType& value,
bool active);
518 template<
typename NodeT>
519 NodeT* stealNode(
const Coord& xyz,
const ValueType& value,
bool active);
526 LeafNodeType* touchLeaf(
const Coord& xyz);
529 template<
typename NodeType> NodeType* probeNode(
const Coord& xyz);
532 template<
typename NodeType>
const NodeType* probeConstNode(
const Coord& xyz)
const;
533 template<
typename NodeType>
const NodeType* probeNode(
const Coord& xyz)
const;
537 LeafNodeType* probeLeaf(
const Coord& xyz);
540 const LeafNodeType* probeConstLeaf(
const Coord& xyz)
const;
541 const LeafNodeType*
probeLeaf(
const Coord& xyz)
const {
return this->probeConstLeaf(xyz); }
546 template<
typename ArrayT>
void getNodes(ArrayT& array) { mRoot.getNodes(array); }
569 template<
typename ArrayT>
void getNodes(ArrayT& array)
const { mRoot.getNodes(array); }
577 bool empty()
const {
return mRoot.empty(); }
580 void clear() { this->clearAllAccessors(); mRoot.clear(); }
583 void clearAllAccessors();
606 const ValueType&
background()
const {
return mRoot.background(); }
609 virtual void getIndexRange(CoordBBox& bbox)
const { mRoot.getIndexRange(bbox); }
612 void voxelizeActiveTiles();
636 template<
typename OtherRootNodeType>
652 template<
typename OtherRootNodeType>
665 template<
typename OtherRootNodeType>
712 template<
typename CombineOp>
715 template<
typename CombineOp>
757 template<
typename ExtendedCombineOp>
758 void combineExtended(
Tree& other, ExtendedCombineOp& op,
bool prune =
false);
760 template<
typename ExtendedCombineOp>
761 void combineExtended(
Tree& other,
const ExtendedCombineOp& op,
bool prune =
false);
792 template<
typename CombineOp,
typename OtherTreeType >
793 void combine2(
const Tree& a,
const OtherTreeType& b, CombineOp& op,
bool prune =
false);
795 template<
typename CombineOp,
typename OtherTreeType >
796 void combine2(
const Tree& a,
const OtherTreeType& b,
const CombineOp& op,
bool prune =
false);
872 template<
typename ExtendedCombineOp,
typename OtherTreeType >
873 void combine2Extended(
const Tree& a,
const OtherTreeType& b, ExtendedCombineOp& op,
876 template<
typename ExtendedCombineOp,
typename OtherTreeType >
877 void combine2Extended(
const Tree& a,
const OtherTreeType& b,
const ExtendedCombineOp&,
921 template<
typename BBoxOp>
void visitActiveBBox(BBoxOp& op)
const { mRoot.visitActiveBBox(op); }
976 template<
typename VisitorOp>
void visit(VisitorOp& op);
977 template<
typename VisitorOp>
void visit(
const VisitorOp& op);
983 template<
typename VisitorOp>
void visit(VisitorOp& op)
const;
984 template<
typename VisitorOp>
void visit(
const VisitorOp& op)
const;
1033 template<
typename OtherTreeType,
typename VisitorOp>
1034 void visit2(OtherTreeType& other, VisitorOp& op);
1035 template<
typename OtherTreeType,
typename VisitorOp>
1036 void visit2(OtherTreeType& other,
const VisitorOp& op);
1048 template<
typename OtherTreeType,
typename VisitorOp>
1049 void visit2(OtherTreeType& other, VisitorOp& op)
const;
1050 template<
typename OtherTreeType,
typename VisitorOp>
1051 void visit2(OtherTreeType& other,
const VisitorOp& op)
const;
1058 typename RootNodeType::ChildOnCIter beginRootChildren()
const {
return mRoot.cbeginChildOn(); }
1065 typename RootNodeType::ChildOffCIter beginRootTiles()
const {
return mRoot.cbeginChildOff(); }
1067 typename RootNodeType::ChildOffCIter
cbeginRootTiles()
const {
return mRoot.cbeginChildOff(); }
1068 typename RootNodeType::ChildOffIter
beginRootTiles() {
return mRoot.beginChildOff(); }
1072 typename RootNodeType::ChildAllCIter beginRootDense()
const {
return mRoot.cbeginChildAll(); }
1074 typename RootNodeType::ChildAllCIter
cbeginRootDense()
const {
return mRoot.cbeginChildAll(); }
1075 typename RootNodeType::ChildAllIter
beginRootDense() {
return mRoot.beginChildAll(); }
1092 NodeIter beginNode() {
return NodeIter(*
this); }
1099 LeafIter beginLeaf() {
return LeafIter(*
this); }
1113 ValueAllIter beginValueAll() {
return ValueAllIter(*
this); }
1119 ValueOnIter beginValueOn() {
return ValueOnIter(*
this); }
1125 ValueOffIter beginValueOff() {
return ValueOffIter(*
this); }
1133 template<
typename IterT> IterT begin();
1136 template<
typename CIterT> CIterT cbegin()
const;
1148 void releaseAllAccessors();
1161 template<
typename _RootNodeType>
1169 template<
typename T, Index N1, Index N2>
1179 template<
typename T, Index N1, Index N2, Index N3>
1189 template<
typename T, Index N1, Index N2, Index N3, Index N4>
1202 int32_t bufferCount;
1203 is.read(reinterpret_cast<char*>(&bufferCount),
sizeof(int32_t));
1204 if (bufferCount != 1)
OPENVDB_LOG_WARN(
"multi-buffer trees are no longer supported");
1211 int32_t bufferCount = 1;
1212 os.write(reinterpret_cast<char*>(&bufferCount),
sizeof(int32_t));
1219 os <<
" Tree Type: " << type()
1220 <<
" Active Voxel Count: " << activeVoxelCount() << std::endl
1221 <<
" Inactive Voxel Count: " << inactiveVoxelCount() << std::endl
1222 <<
" Leaf Node Count: " << leafCount() << std::endl
1223 <<
" Non-leaf Node Count: " << nonLeafCount() << std::endl;
1238 template<
typename TreeT>
struct TreeIterTraits<TreeT, typename TreeT::RootNodeType::ChildOnIter> {
1239 static typename TreeT::RootNodeType::ChildOnIter
begin(TreeT& tree) {
1240 return tree.beginRootChildren();
1244 template<
typename TreeT>
struct TreeIterTraits<TreeT, typename TreeT::RootNodeType::ChildOnCIter> {
1245 static typename TreeT::RootNodeType::ChildOnCIter
begin(
const TreeT& tree) {
1246 return tree.cbeginRootChildren();
1250 template<
typename TreeT>
struct TreeIterTraits<TreeT, typename TreeT::RootNodeType::ChildOffIter> {
1251 static typename TreeT::RootNodeType::ChildOffIter
begin(TreeT& tree) {
1252 return tree.beginRootTiles();
1256 template<
typename TreeT>
struct TreeIterTraits<TreeT, typename TreeT::RootNodeType::ChildOffCIter> {
1257 static typename TreeT::RootNodeType::ChildOffCIter
begin(
const TreeT& tree) {
1258 return tree.cbeginRootTiles();
1262 template<
typename TreeT>
struct TreeIterTraits<TreeT, typename TreeT::RootNodeType::ChildAllIter> {
1263 static typename TreeT::RootNodeType::ChildAllIter
begin(TreeT& tree) {
1264 return tree.beginRootDense();
1268 template<
typename TreeT>
struct TreeIterTraits<TreeT, typename TreeT::RootNodeType::ChildAllCIter> {
1269 static typename TreeT::RootNodeType::ChildAllCIter
begin(
const TreeT& tree) {
1270 return tree.cbeginRootDense();
1275 static typename TreeT::NodeIter
begin(TreeT& tree) {
return tree.beginNode(); }
1279 static typename TreeT::NodeCIter
begin(
const TreeT& tree) {
return tree.cbeginNode(); }
1283 static typename TreeT::LeafIter
begin(TreeT& tree) {
return tree.beginLeaf(); }
1287 static typename TreeT::LeafCIter
begin(
const TreeT& tree) {
return tree.cbeginLeaf(); }
1291 static typename TreeT::ValueOnIter
begin(TreeT& tree) {
return tree.beginValueOn(); }
1294 template<
typename TreeT>
struct TreeIterTraits<TreeT, typename TreeT::ValueOnCIter> {
1295 static typename TreeT::ValueOnCIter
begin(
const TreeT& tree) {
return tree.cbeginValueOn(); }
1298 template<
typename TreeT>
struct TreeIterTraits<TreeT, typename TreeT::ValueOffIter> {
1299 static typename TreeT::ValueOffIter
begin(TreeT& tree) {
return tree.beginValueOff(); }
1302 template<
typename TreeT>
struct TreeIterTraits<TreeT, typename TreeT::ValueOffCIter> {
1303 static typename TreeT::ValueOffCIter
begin(
const TreeT& tree) {
return tree.cbeginValueOff(); }
1306 template<
typename TreeT>
struct TreeIterTraits<TreeT, typename TreeT::ValueAllIter> {
1307 static typename TreeT::ValueAllIter
begin(TreeT& tree) {
return tree.beginValueAll(); }
1310 template<
typename TreeT>
struct TreeIterTraits<TreeT, typename TreeT::ValueAllCIter> {
1311 static typename TreeT::ValueAllCIter
begin(
const TreeT& tree) {
return tree.cbeginValueAll(); }
1315 template<
typename RootNodeType>
1316 template<
typename IterT>
1324 template<
typename RootNodeType>
1325 template<
typename IterT>
1336 template<
typename RootNodeType>
1340 this->clearAllAccessors();
1342 mRoot.readTopology(is, saveFloatAsHalf);
1346 template<
typename RootNodeType>
1351 mRoot.writeTopology(os, saveFloatAsHalf);
1355 template<
typename RootNodeType>
1359 this->clearAllAccessors();
1360 mRoot.readBuffers(is, saveFloatAsHalf);
1364 #ifndef OPENVDB_2_ABI_COMPATIBLE
1366 template<
typename RootNodeType>
1370 this->clearAllAccessors();
1371 mRoot.readBuffers(is, bbox, saveFloatAsHalf);
1375 template<
typename RootNodeType>
1379 for (
LeafCIter it = this->cbeginLeaf(); it; ++it) {
1385 #endif // !OPENVDB_2_ABI_COMPATIBLE
1388 template<
typename RootNodeType>
1399 template<
typename RootNodeType>
1403 typename AccessorRegistry::accessor a;
1404 mAccessorRegistry.insert(a, &accessor);
1408 template<
typename RootNodeType>
1412 typename ConstAccessorRegistry::accessor a;
1413 mConstAccessorRegistry.insert(a, &accessor);
1417 template<
typename RootNodeType>
1421 mAccessorRegistry.erase(&accessor);
1425 template<
typename RootNodeType>
1429 mConstAccessorRegistry.erase(&accessor);
1433 template<
typename RootNodeType>
1437 for (
typename AccessorRegistry::iterator it = mAccessorRegistry.begin();
1438 it != mAccessorRegistry.end(); ++it)
1440 if (it->first) it->first->
clear();
1443 for (
typename ConstAccessorRegistry::iterator it = mConstAccessorRegistry.begin();
1444 it != mConstAccessorRegistry.end(); ++it)
1446 if (it->first) it->first->clear();
1451 template<
typename RootNodeType>
1455 mAccessorRegistry.erase(NULL);
1456 for (
typename AccessorRegistry::iterator it = mAccessorRegistry.begin();
1457 it != mAccessorRegistry.end(); ++it)
1459 it->first->release();
1461 mAccessorRegistry.
clear();
1463 mAccessorRegistry.erase(NULL);
1464 for (
typename ConstAccessorRegistry::iterator it = mConstAccessorRegistry.begin();
1465 it != mConstAccessorRegistry.end(); ++it)
1467 it->first->release();
1469 mConstAccessorRegistry.clear();
1476 template<
typename RootNodeType>
1477 inline const typename RootNodeType::ValueType&
1484 template<
typename RootNodeType>
1485 template<
typename AccessT>
1486 inline const typename RootNodeType::ValueType&
1493 template<
typename RootNodeType>
1501 template<
typename RootNodeType>
1509 template<
typename RootNodeType>
1517 template<
typename RootNodeType>
1525 template<
typename RootNodeType>
1532 template<
typename RootNodeType>
1539 template<
typename RootNodeType>
1540 template<
typename AccessT>
1548 template<
typename RootNodeType>
1556 template<
typename RootNodeType>
1564 template<
typename RootNodeType>
1565 template<
typename ModifyOp>
1573 template<
typename RootNodeType>
1574 template<
typename ModifyOp>
1582 template<
typename RootNodeType>
1593 template<
typename RootNodeType>
1596 const ValueType& value,
bool active)
1598 mRoot.
addTile(level, xyz, value, active);
1602 template<
typename RootNodeType>
1603 template<
typename NodeT>
1607 this->clearAllAccessors();
1608 return mRoot.template stealNode<NodeT>(xyz, value, active);
1612 template<
typename RootNodeType>
1613 inline typename RootNodeType::LeafNodeType*
1620 template<
typename RootNodeType>
1621 inline typename RootNodeType::LeafNodeType*
1628 template<
typename RootNodeType>
1629 inline const typename RootNodeType::LeafNodeType*
1636 template<
typename RootNodeType>
1637 template<
typename NodeType>
1641 return mRoot.template probeNode<NodeType>(xyz);
1645 template<
typename RootNodeType>
1646 template<
typename NodeType>
1647 inline const NodeType*
1650 return this->
template probeConstNode<NodeType>(xyz);
1654 template<
typename RootNodeType>
1655 template<
typename NodeType>
1656 inline const NodeType*
1659 return mRoot.template probeConstNode<NodeType>(xyz);
1666 template<
typename RootNodeType>
1670 this->clearAllAccessors();
1671 return mRoot.clip(bbox);
1675 #ifndef OPENVDB_2_ABI_COMPATIBLE
1676 template<
typename RootNodeType>
1680 this->clearAllAccessors();
1681 for (
LeafIter it = this->beginLeaf(); it; ) {
1682 const LeafNodeType* leaf = it.getLeaf();
1684 if (!leaf->isAllocated()) {
1685 this->addTile(0, leaf->origin(), this->background(),
false);
1692 template<
typename RootNodeType>
1696 this->clearAllAccessors();
1697 return mRoot.fill(bbox, value, active);
1701 template<
typename RootNodeType>
1709 if (MetadataT* m = dynamic_cast<MetadataT*>(result.get())) {
1710 m->value() = mRoot.background();
1720 template<
typename RootNodeType>
1724 this->clearAllAccessors();
1725 mRoot.voxelizeActiveTiles();
1729 template<
typename RootNodeType>
1733 this->clearAllAccessors();
1737 mRoot.template merge<MERGE_ACTIVE_STATES>(other.
mRoot);
break;
1739 mRoot.template merge<MERGE_NODES>(other.
mRoot);
break;
1741 mRoot.template merge<MERGE_ACTIVE_STATES_AND_NODES>(other.
mRoot);
break;
1746 template<
typename RootNodeType>
1747 template<
typename OtherRootNodeType>
1751 this->clearAllAccessors();
1752 mRoot.topologyUnion(other.
root());
1755 template<
typename RootNodeType>
1756 template<
typename OtherRootNodeType>
1760 this->clearAllAccessors();
1761 mRoot.topologyIntersection(other.
root());
1764 template<
typename RootNodeType>
1765 template<
typename OtherRootNodeType>
1769 this->clearAllAccessors();
1770 mRoot.topologyDifference(other.
root());
1778 template<
typename AValueT,
typename CombineOp,
typename BValueT = AValueT>
1784 op(args.
a(), args.
b(), args.
result());
1791 template<
typename RootNodeType>
1792 template<
typename CombineOp>
1797 this->combineExtended(other, extendedOp, prune);
1804 template<
typename RootNodeType>
1805 template<
typename CombineOp>
1810 this->combineExtended(other, extendedOp, prune);
1815 template<
typename RootNodeType>
1816 template<
typename ExtendedCombineOp>
1820 this->clearAllAccessors();
1828 template<
typename RootNodeType>
1829 template<
typename ExtendedCombineOp>
1833 this->clearAllAccessors();
1834 mRoot.template combine<const ExtendedCombineOp>(other.
mRoot, op,
prune);
1839 template<
typename RootNodeType>
1840 template<
typename CombineOp,
typename OtherTreeType>
1845 this->combine2Extended(a, b, extendedOp, prune);
1852 template<
typename RootNodeType>
1853 template<
typename CombineOp,
typename OtherTreeType>
1858 this->combine2Extended(a, b, extendedOp, prune);
1863 template<
typename RootNodeType>
1864 template<
typename ExtendedCombineOp,
typename OtherTreeType>
1867 ExtendedCombineOp& op,
bool prune)
1869 this->clearAllAccessors();
1870 mRoot.combine2(a.
root(), b.root(), op,
prune);
1878 template<
typename RootNodeType>
1879 template<
typename ExtendedCombineOp,
typename OtherTreeType>
1882 const ExtendedCombineOp& op,
bool prune)
1884 this->clearAllAccessors();
1885 mRoot.template combine2<const ExtendedCombineOp>(a.
root(), b.root(), op,
prune);
1893 template<
typename RootNodeType>
1894 template<
typename VisitorOp>
1898 this->clearAllAccessors();
1899 mRoot.template visit<VisitorOp>(op);
1903 template<
typename RootNodeType>
1904 template<
typename VisitorOp>
1908 mRoot.template visit<VisitorOp>(op);
1914 template<
typename RootNodeType>
1915 template<
typename VisitorOp>
1919 this->clearAllAccessors();
1920 mRoot.template visit<const VisitorOp>(op);
1926 template<
typename RootNodeType>
1927 template<
typename VisitorOp>
1931 mRoot.template visit<const VisitorOp>(op);
1938 template<
typename RootNodeType>
1939 template<
typename OtherTreeType,
typename VisitorOp>
1943 this->clearAllAccessors();
1944 typedef typename OtherTreeType::RootNodeType OtherRootNodeType;
1945 mRoot.template visit2<OtherRootNodeType, VisitorOp>(other.root(), op);
1949 template<
typename RootNodeType>
1950 template<
typename OtherTreeType,
typename VisitorOp>
1954 typedef typename OtherTreeType::RootNodeType OtherRootNodeType;
1955 mRoot.template visit2<OtherRootNodeType, VisitorOp>(other.root(), op);
1961 template<
typename RootNodeType>
1962 template<
typename OtherTreeType,
typename VisitorOp>
1966 this->clearAllAccessors();
1967 typedef typename OtherTreeType::RootNodeType OtherRootNodeType;
1968 mRoot.template visit2<OtherRootNodeType, const VisitorOp>(other.root(), op);
1974 template<
typename RootNodeType>
1975 template<
typename OtherTreeType,
typename VisitorOp>
1979 typedef typename OtherTreeType::RootNodeType OtherRootNodeType;
1980 mRoot.template visit2<OtherRootNodeType, const VisitorOp>(other.root(), op);
1987 template<
typename RootNodeType>
1991 if (sTreeTypeName == NULL) {
1992 std::vector<Index> dims;
1993 Tree::getNodeLog2Dims(dims);
1994 std::ostringstream ostr;
1995 ostr <<
"Tree_" << typeNameAsString<ValueType>();
1996 for (
size_t i = 1, N = dims.size(); i < N; ++i) {
1997 ostr <<
"_" << dims[i];
2000 if (sTreeTypeName.compare_and_swap(s, NULL) != NULL)
delete s;
2002 return *sTreeTypeName;
2006 template<
typename RootNodeType>
2007 template<
typename OtherRootNodeType>
2015 template<
typename RootNodeType>
2020 this->evalActiveVoxelDim(dim);
2022 totalVoxels = dim.x() * dim.y() * dim.z(),
2023 activeVoxels = this->activeVoxelCount();
2024 assert(totalVoxels >= activeVoxels);
2025 return totalVoxels - activeVoxels;
2029 template<
typename RootNodeType>
2035 if (this->empty())
return false;
2037 mRoot.evalActiveBoundingBox(bbox,
false);
2042 template<
typename RootNodeType>
2048 if (this->empty())
return false;
2050 mRoot.evalActiveBoundingBox(bbox,
true);
2056 template<
typename RootNodeType>
2061 bool notEmpty = this->evalActiveVoxelBoundingBox(bbox);
2062 dim = bbox.extents();
2067 template<
typename RootNodeType>
2072 bool notEmpty = this->evalLeafBoundingBox(bbox);
2073 dim = bbox.extents();
2078 template<
typename RootNodeType>
2082 minVal = maxVal = zeroVal<ValueType>();
2084 minVal = maxVal = *iter;
2085 for (++iter; iter; ++iter) {
2086 const ValueType& val = *iter;
2087 if (val < minVal) minVal = val;
2088 if (val > maxVal) maxVal = val;
2094 template<
typename RootNodeType>
2099 RootNodeType::getNodeLog2Dims(dims);
2103 template<
typename RootNodeType>
2107 if (verboseLevel <= 0)
return;
2112 std::streamsize savedPrecision;
2113 OnExit(std::ostream& _os): os(_os), savedPrecision(os.precision()) {}
2114 ~OnExit() { os.precision(savedPrecision); }
2116 OnExit restorePrecision(os);
2118 std::vector<Index> dims;
2119 Tree::getNodeLog2Dims(dims);
2121 os <<
"Information about Tree:\n"
2122 <<
" Type: " << this->type() <<
"\n";
2124 os <<
" Configuration:\n";
2126 if (verboseLevel <= 1) {
2128 os <<
" Root(" << mRoot.getTableSize() <<
")";
2129 if (dims.size() > 1) {
2130 for (
size_t i = 1, N = dims.size() - 1; i < N; ++i) {
2131 os <<
", Internal(" << (1 << dims[i]) <<
"^3)";
2133 os <<
", Leaf(" << (1 << *dims.rbegin()) <<
"^3)\n";
2135 os <<
" Background value: " << mRoot.background() <<
"\n";
2141 ValueType minVal = zeroVal<ValueType>(), maxVal = zeroVal<ValueType>();
2142 if (verboseLevel > 3) {
2144 this->evalMinMax(minVal, maxVal);
2147 std::vector<Index64> nodeCount(dims.size());
2148 #ifndef OPENVDB_2_ABI_COMPATIBLE
2149 Index64 unallocatedLeafCount = 0;
2151 for (
NodeCIter it = cbeginNode(); it; ++it) {
2152 ++(nodeCount[it.getDepth()]);
2154 #ifndef OPENVDB_2_ABI_COMPATIBLE
2155 if (it.getLevel() == 0) {
2156 const LeafNodeType* leaf = NULL;
2158 if (leaf && !leaf->isAllocated()) ++unallocatedLeafCount;
2163 for (
size_t i = 0; i < nodeCount.size(); ++i) totalNodeCount += nodeCount[i];
2166 os <<
" Root(1 x " << mRoot.getTableSize() <<
")";
2167 if (dims.size() > 1) {
2168 for (
size_t i = 1, N = dims.size() - 1; i < N; ++i) {
2170 os <<
" x " << (1 << dims[i]) <<
"^3)";
2173 os <<
" x " << (1 << *dims.rbegin()) <<
"^3)\n";
2175 os <<
" Background value: " << mRoot.background() <<
"\n";
2179 if (verboseLevel > 3) {
2180 os <<
" Min value: " << minVal <<
"\n";
2181 os <<
" Max value: " << maxVal <<
"\n";
2185 leafCount = *nodeCount.rbegin(),
2186 numActiveVoxels = this->activeVoxelCount(),
2187 numActiveLeafVoxels = this->activeLeafVoxelCount();
2192 uint64_t totalVoxels = 0;
2193 if (numActiveVoxels) {
2195 this->evalActiveVoxelBoundingBox(bbox);
2196 dim = bbox.extents();
2197 totalVoxels = dim.x() * uint64_t(dim.y()) * dim.z();
2199 os <<
" Bounding box of active voxels: " << bbox <<
"\n";
2200 os <<
" Dimensions of active voxels: "
2201 << dim[0] <<
" x " << dim[1] <<
" x " << dim[2] <<
"\n";
2203 const double activeRatio = (100.0 * double(numActiveVoxels)) /
double(totalVoxels);
2204 os <<
" Percentage of active voxels: " << std::setprecision(3) << activeRatio <<
"%\n";
2206 if (leafCount > 0) {
2207 const double fillRatio = (100.0 * double(numActiveLeafVoxels))
2208 / (
double(leafCount) * double(LeafNodeType::NUM_VOXELS));
2209 os <<
" Average leaf node fill ratio: " << fillRatio <<
"%\n";
2212 #ifndef OPENVDB_2_ABI_COMPATIBLE
2213 if (verboseLevel > 2) {
2214 os <<
" Number of unallocated nodes: "
2216 << (100.0 * double(unallocatedLeafCount) / double(totalNodeCount)) <<
"%)\n";
2220 os <<
" Tree is empty!\n";
2224 if (verboseLevel == 2)
return;
2228 actualMem = this->memUsage(),
2229 denseMem =
sizeof(ValueType) * totalVoxels,
2230 voxelsMem =
sizeof(ValueType) * numActiveLeafVoxels;
2233 os <<
"Memory footprint:\n";
2237 if (numActiveVoxels) {
2239 os <<
" Actual footprint is " << (100.0 * double(actualMem) / double(denseMem))
2240 <<
"% of an equivalent dense volume\n";
2241 os <<
" Leaf voxel footprint is " << (100.0 * double(voxelsMem) / double(actualMem))
2242 <<
"% of actual footprint\n";
2250 #endif // OPENVDB_TREE_TREE_HAS_BEEN_INCLUDED
virtual Metadata::Ptr getBackgroundValue() const
Return this tree's background value wrapped as metadata.
Definition: Tree.h:86
virtual ~TreeBase()
Definition: Tree.h:70
Tree5::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:1190
TreeBase()
Definition: Tree.h:69
virtual void readTopology(std::istream &, bool saveFloatAsHalf=false)
Read the tree topology from a stream.
Definition: Tree.h:1200
virtual Index64 memUsage() const
Return the total amount of memory in bytes occupied by this tree.
Definition: Tree.h:153
AccessorRegistry mAccessorRegistry
Definition: Tree.h:1155
boost::shared_ptr< const TreeBase > ConstPtr
Definition: Tree.h:67
static TreeT::ValueOffCIter begin(const TreeT &tree)
Definition: Tree.h:1303
std::string Name
Definition: Name.h:44
static tbb::atomic< const Name * > sTreeTypeName
Definition: Tree.h:1158
LeafCIter cbeginLeaf() const
Return an iterator over all leaf nodes in this tree.
Definition: Tree.h:1102
_RootNodeType RootNodeType
Definition: Tree.h:209
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:541
Tree()
Definition: Tree.h:227
void setValueOff(const Coord &xyz)
Mark the voxel at the given coordinates as inactive but don't change its value.
Definition: Tree.h:1503
static TreeT::ValueAllCIter begin(const TreeT &tree)
Definition: Tree.h:1311
Base class for tree-traversal iterators over all nodes.
Definition: TreeIterator.h:977
void operator()(CombineArgs< AValueT, BValueT > &args) const
Definition: Tree.h:1783
uint64_t Index64
Definition: Types.h:58
const ValueType & getValue(const Coord &xyz) const
Return the value of the voxel at the given coordinates.
Definition: Tree.h:1478
void prune(const ValueType &tolerance=zeroVal< ValueType >())
Reduce the memory footprint of this tree by replacing with tiles any nodes whose values are all the s...
Definition: Tree.h:498
tbb::concurrent_hash_map< ValueAccessorBase< Tree > *, bool > AccessorRegistry
Definition: Tree.h:1140
NodeIteratorBase< const Tree, typename RootNodeType::ChildOnCIter > NodeCIter
Iterator over all nodes in this tree.
Definition: Tree.h:1082
#define OPENVDB_THROW(exception, message)
Definition: Exceptions.h:97
const ValueType & background() const
Return this tree's background value.
Definition: Tree.h:606
RootNodeType & root()
Return this tree's root node.
Definition: Tree.h:304
LeafCIter beginLeaf() const
Return an iterator over all leaf nodes in this tree.
Definition: Tree.h:1101
static TreeT::RootNodeType::ChildAllIter begin(TreeT &tree)
Definition: Tree.h:1263
const RootNodeType & root() const
Return this tree's root node.
Definition: Tree.h:305
virtual void writeTopology(std::ostream &, bool saveFloatAsHalf=false) const
Write the tree topology to a stream.
Definition: Tree.h:1209
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:1595
Tree(const ValueType &background)
Empty tree constructor.
Definition: Tree.h:284
boost::shared_ptr< Tree > Ptr
Definition: Tree.h:206
ValueConverter::Type is the type of a tree having the same hierarchy as this tree but a different ...
Definition: Tree.h:222
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:1527
ValueAllCIter cbeginValueAll() const
Return an iterator over all values (tile and voxel) across all nodes.
Definition: Tree.h:1116
ValueAllCIter beginValueAll() const
Return an iterator over all values (tile and voxel) across all nodes.
Definition: Tree.h:1115
void getNodes(ArrayT &array) const
Adds all nodes of a certain type to a container with the following API:
Definition: Tree.h:569
boost::shared_ptr< TreeBase > Ptr
Definition: Tree.h:66
static TreeT::NodeCIter begin(const TreeT &tree)
Definition: Tree.h:1279
bool operator==(const Tree &) const
Definition: Tree.h:299
RootNodeType::ChildOffIter beginRootTiles()
Return an iterator over non-child entries of the root node's table.
Definition: Tree.h:1068
static TreeT::RootNodeType::ChildOffCIter begin(const TreeT &tree)
Definition: Tree.h:1257
NodeCIter beginNode() const
Return an iterator over all nodes in this tree.
Definition: Tree.h:1094
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:1534
virtual void writeBuffers(std::ostream &, bool saveFloatAsHalf=false) const
Write out all data buffers for this tree.
Definition: Tree.h:1390
RootNodeType::ChildOffCIter cbeginRootTiles() const
Return an iterator over non-child entries of the root node's table.
Definition: Tree.h:1067
static TreeT::RootNodeType::ChildOffIter begin(TreeT &tree)
Definition: Tree.h:1251
virtual void getIndexRange(CoordBBox &bbox) const
Min and max are both inclusive.
Definition: Tree.h:609
void modifyValueAndActiveState(const Coord &xyz, const ModifyOp &op)
Apply a functor to the voxel at the given coordinates.
Definition: Tree.h:1576
Tree4::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:1180
virtual Name valueType() const
Return the name of the type of a voxel's value (e.g., "float" or "vec3d")
Definition: Tree.h:292
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:1519
Helper class to adapt a three-argument (a, b, result) CombineOp functor into a single-argument functo...
Definition: Tree.h:1779
virtual Index32 leafCount() const
Return the number of leaf nodes.
Definition: Tree.h:365
Tree(const OtherTreeType &other, const ValueType &inactiveValue, const ValueType &activeValue, TopologyCopy)
Topology copy constructor from a tree of a different type.
Definition: Tree.h:256
RootNodeType::ChildOnCIter cbeginRootChildren() const
Return an iterator over children of the root node.
Definition: Tree.h:1060
static TreeT::ValueAllIter begin(TreeT &tree)
Definition: Tree.h:1307
Internal table nodes for OpenVDB trees.
virtual ~Tree()
Definition: Tree.h:286
TreeValueIteratorBase< Tree, typename RootNodeType::ValueOnIter > ValueOnIter
Definition: Tree.h:1107
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:577
#define OPENVDB_VERSION_NAME
Definition: version.h:43
FormattedInt< IntT > formattedInt(IntT n)
Definition: Formats.h:130
bool operator!=(const Tree &) const
Definition: Tree.h:300
TreeValueIteratorBase< const Tree, typename RootNodeType::ValueAllCIter > ValueAllCIter
Definition: Tree.h:1106
virtual Index64 inactiveLeafVoxelCount() const
Return the number of inactive voxels stored in leaf nodes.
Definition: Tree.h:371
Index32 Index
Definition: Types.h:59
TreeValueIteratorBase< Tree, typename RootNodeType::ValueOffIter > ValueOffIter
Definition: Tree.h:1109
#define OPENVDB_LOG_WARN(message)
Log a warning message of the form 'someVar << "some text" << ...'.
Definition: logging.h:39
static TreeT::LeafCIter begin(const TreeT &tree)
Definition: Tree.h:1287
This struct collects both input and output arguments to "grid combiner" functors used with the tree::...
Definition: Types.h:309
static TreeT::RootNodeType::ChildAllCIter begin(const TreeT &tree)
Definition: Tree.h:1269
Base class for tree-traversal iterators over all leaf nodes (but not leaf voxels) ...
Definition: TreeIterator.h:1228
bool isValueOn(const Coord &xyz) const
Return true if the value at the given coordinates is active.
Definition: Tree.h:465
virtual Index64 activeLeafVoxelCount() const
Return the number of active voxels stored in leaf nodes.
Definition: Tree.h:369
ConstAccessorRegistry mConstAccessorRegistry
Definition: Tree.h:1156
ValueOffCIter beginValueOff() const
Return an iterator over inactive values (tile and voxel) across all nodes.
Definition: Tree.h:1127
Tree< typename RootNodeType::template ValueConverter< OtherValueType >::Type > Type
Definition: Tree.h:223
RootNodeType::ChildAllIter beginRootDense()
Return an iterator over all entries of the root node's table.
Definition: Tree.h:1075
Definition: Exceptions.h:39
Tree3::Type is the type of a three-level tree (Root, Internal, Leaf) with value type T and...
Definition: Tree.h:1170
virtual Index64 activeVoxelCount() const
Return the total number of active voxels.
Definition: Tree.h:373
void clear()
Remove all tiles from this tree and all nodes other than the root node.
Definition: Tree.h:580
OPENVDB_API Hermite min(const Hermite &, const Hermite &)
min and max operations done directly on the compressed data.
virtual const Name & type() const
Return the name of this type of tree.
Definition: Tree.h:297
const AValueType & a() const
Get the A input value.
Definition: Types.h:334
TreeIterTraits provides, for all tree iterators, a begin(tree) function that returns an iterator over...
Definition: Tree.h:1236
ValueOnCIter cbeginValueOn() const
Return an iterator over active values (tile and voxel) across all nodes.
Definition: Tree.h:1122
LeafIteratorBase< const Tree, typename RootNodeType::ChildOnCIter > LeafCIter
Iterator over all leaf nodes in this tree.
Definition: Tree.h:1088
const AValueType & result() const
Get the output value.
Definition: Types.h:339
virtual Index64 memUsage() const
Return the total amount of memory in bytes occupied by this tree.
Definition: Tree.h:382
void clearAllAccessors()
Clear all registered accessors.
Definition: Tree.h:1435
Tree< RootNode< InternalNode< InternalNode< LeafNode< T, N3 >, N2 >, N1 > > > Type
Definition: Tree.h:1181
static TreeT::RootNodeType::ChildOnIter begin(TreeT &tree)
Definition: Tree.h:1239
MergePolicy
Definition: Types.h:261
void visitActiveBBox(BBoxOp &op) const
Use sparse traversal to call the given functor with bounding box information for all active tiles and...
Definition: Tree.h:921
Tree(const Tree &other)
Deep copy constructor.
Definition: Tree.h:230
virtual Index32 nonLeafCount() const
Return the number of non-leaf nodes.
Definition: Tree.h:367
Tree< RootNode< InternalNode< LeafNode< T, N2 >, N1 > > > Type
Definition: Tree.h:1171
TreeValueIteratorBase< const Tree, typename RootNodeType::ValueOnCIter > ValueOnCIter
Definition: Tree.h:1108
static TreeT::NodeIter begin(TreeT &tree)
Definition: Tree.h:1275
Tree< RootNode< InternalNode< InternalNode< InternalNode< LeafNode< T, N4 >, N3 >, N2 >, N1 > > > Type
Definition: Tree.h:1192
boost::shared_ptr< const Tree > ConstPtr
Definition: Tree.h:207
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:1614
static TreeT::LeafIter begin(TreeT &tree)
Definition: Tree.h:1283
Tree(const OtherTreeType &other, const ValueType &background, TopologyCopy)
Topology copy constructor from a tree of a different type.
Definition: Tree.h:277
CombineOp & op
Definition: Tree.h:1787
bool hasActiveTiles() const
Return true if this tree has any active tiles.
Definition: Tree.h:469
Base class for tree-traversal iterators over tile and voxel values.
Definition: TreeIterator.h:658
static TreeT::RootNodeType::ChildOnCIter begin(const TreeT &tree)
Definition: Tree.h:1245
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:1567
NodeCIter cbeginNode() const
Return an iterator over all nodes in this tree.
Definition: Tree.h:1095
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:506
Definition: Exceptions.h:84
The root node of an OpenVDB tree.
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:1630
RootNodeType::ChildOnIter beginRootChildren()
Return an iterator over children of the root node.
Definition: Tree.h:1061
RootNodeType::ChildAllCIter cbeginRootDense() const
Return an iterator over all entries of the root node's table.
Definition: Tree.h:1074
RootNodeType::ValueType ValueType
Definition: Tree.h:210
ValueOnCIter beginValueOn() const
Return an iterator over active values (tile and voxel) across all nodes.
Definition: Tree.h:1121
TreeValueIteratorBase< Tree, typename RootNodeType::ValueAllIter > ValueAllIter
Definition: Tree.h:1105
RootNodeType mRoot
Definition: Tree.h:1154
OPENVDB_API Hermite max(const Hermite &, const Hermite &)
min and max operations done directly on the compressed data.
Index64 activeTileCount() const
Return the total number of active tiles.
Definition: Tree.h:377
static TreeT::ValueOnCIter begin(const TreeT &tree)
Definition: Tree.h:1295
bool isValueOff(const Coord &xyz) const
Return true if the value at the given coordinates is inactive.
Definition: Tree.h:467
virtual TreeBase::Ptr copy() const
Return a pointer to a deep copy of this tree.
Definition: Tree.h:289
bool probeValue(const Coord &xyz, ValueType &value) const
Get the value of the voxel at the given coordinates.
Definition: Tree.h:1584
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:1622
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:1495
This base class for ValueAccessors manages registration of an accessor with a tree so that the tree c...
Definition: ValueAccessor.h:102
void setValueOn(const Coord &xyz)
Mark the voxel at the given coordinates as active but don't change its value.
Definition: Tree.h:1550
uint32_t Index32
Definition: Types.h:57
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h:71
ValueOffCIter cbeginValueOff() const
Return an iterator over inactive values (tile and voxel) across all nodes.
Definition: Tree.h:1128
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:2009
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:1217
static TreeT::ValueOffIter begin(TreeT &tree)
Definition: Tree.h:1299
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)
Base class for typed trees.
Definition: Tree.h:63
TreeValueIteratorBase< const Tree, typename RootNodeType::ValueOffCIter > ValueOffCIter
Definition: Tree.h:1110
static TreeT::ValueOnIter begin(TreeT &tree)
Definition: Tree.h:1291
virtual Index treeDepth() const
Return the depth of this tree.
Definition: Tree.h:363
Tree(const Tree< OtherRootType > &other)
Value conversion deep copy constructor.
Definition: Tree.h:241
tbb::concurrent_hash_map< ValueAccessorBase< const Tree > *, bool > ConstAccessorRegistry
Definition: Tree.h:1141
RootNodeType::LeafNodeType LeafNodeType
Definition: Tree.h:211
const BValueType & b() const
Get the B input value.
Definition: Types.h:336
CombineOpAdapter(CombineOp &_op)
Definition: Tree.h:1781