OpenVDB: Tree.h Source File

OpenVDB 7.1.0

Tree.h
Go to the documentation of this file.
1 // Copyright Contributors to the OpenVDB Project
2 // SPDX-License-Identifier: MPL-2.0
3
5
6 #ifndef OPENVDB_TREE_TREE_HAS_BEEN_INCLUDED
7 #define OPENVDB_TREE_TREE_HAS_BEEN_INCLUDED
8
9 #include <openvdb/Types.h>
10 #include <openvdb/Metadata.h>
11 #include <openvdb/math/Math.h>
12 #include <openvdb/math/BBox.h>
13 #include <openvdb/util/Formats.h>
14 #include <openvdb/util/logging.h>
15 #include <openvdb/Platform.h>
16 #include "RootNode.h"
17 #include "InternalNode.h"
18 #include "LeafNode.h"
19 #include "TreeIterator.h"
20 #include "ValueAccessor.h"
21 #include <tbb/concurrent_hash_map.h>
22 #include <cstdint>
23 #include <iostream>
24 #include <mutex>
25 #include <sstream>
26 #include <vector>
27
28
29 namespace openvdb {
30 OPENVDB_USE_VERSION_NAMESPACE
31 namespace OPENVDB_VERSION_NAME {
32 namespace tree {
33
35 class OPENVDB_API TreeBase
36 {
37 public:
38 using Ptr = SharedPtr<TreeBase>;
39 using ConstPtr = SharedPtr<const TreeBase>;
40
41 TreeBase() = default;
42 TreeBase(const TreeBase&) = default;
43 TreeBase& operator=(const TreeBase&) = delete; // disallow assignment
44 virtual ~TreeBase() = default;
45
47 virtual const Name& type() const = 0;
48
50 virtual Name valueType() const = 0;
51
53 virtual TreeBase::Ptr copy() const = 0;
54
55 //
56 // Tree methods
57 //
60 virtual Metadata::Ptr getBackgroundValue() const { return Metadata::Ptr(); }
61
69 virtual bool evalLeafBoundingBox(CoordBBox& bbox) const = 0;
70
74 virtual bool evalLeafDim(Coord& dim) const = 0;
75
83 virtual bool evalActiveVoxelBoundingBox(CoordBBox& bbox) const = 0;
84
88 virtual bool evalActiveVoxelDim(Coord& dim) const = 0;
89
90 virtual void getIndexRange(CoordBBox& bbox) const = 0;
91
97 virtual void clipUnallocatedNodes() = 0;
99 virtual Index32 unallocatedLeafCount() const = 0;
100
101
102 //
103 // Statistics
104 //
108 virtual Index treeDepth() const = 0;
110 virtual Index32 leafCount() const = 0;
111 #if OPENVDB_ABI_VERSION_NUMBER >= 7
112 virtual std::vector<Index32> nodeCount() const = 0;
116 #endif
117 virtual Index32 nonLeafCount() const = 0;
120 virtual Index64 activeLeafVoxelCount() const = 0;
122 virtual Index64 inactiveLeafVoxelCount() const = 0;
124 virtual Index64 activeVoxelCount() const = 0;
126 virtual Index64 inactiveVoxelCount() const = 0;
128 virtual Index64 activeTileCount() const = 0;
129
131 virtual Index64 memUsage() const { return 0; }
132
133
134 //
135 // I/O methods
136 //
140 virtual void readTopology(std::istream&, bool saveFloatAsHalf = false);
144 virtual void writeTopology(std::ostream&, bool saveFloatAsHalf = false) const;
145
147 virtual void readBuffers(std::istream&, bool saveFloatAsHalf = false) = 0;
149 virtual void readBuffers(std::istream&, const CoordBBox&, bool saveFloatAsHalf = false) = 0;
155 virtual void readNonresidentBuffers() const = 0;
157 virtual void writeBuffers(std::ostream&, bool saveFloatAsHalf = false) const = 0;
158
166 virtual void print(std::ostream& os = std::cout, int verboseLevel = 1) const;
167 };
168
169
171
172
173 template<typename _RootNodeType>
174 class Tree: public TreeBase
175 {
176 public:
177 using Ptr = SharedPtr<Tree>;
178 using ConstPtr = SharedPtr<const Tree>;
179
180 using RootNodeType = _RootNodeType;
181 using ValueType = typename RootNodeType::ValueType;
182 using BuildType = typename RootNodeType::BuildType;
183 using LeafNodeType = typename RootNodeType::LeafNodeType;
184
185 static const Index DEPTH = RootNodeType::LEVEL + 1;
186
193 template<typename OtherValueType>
194 struct ValueConverter {
195 using Type = Tree<typename RootNodeType::template ValueConverter<OtherValueType>::Type>;
196 };
197
198
199 Tree() {}
200
201 Tree& operator=(const Tree&) = delete; // disallow assignment
202
204 Tree(const Tree& other): TreeBase(other), mRoot(other.mRoot)
205 {
206 }
207
214 template<typename OtherRootType>
215 explicit Tree(const Tree<OtherRootType>& other): TreeBase(other), mRoot(other.root())
216 {
217 }
218
229 template<typename OtherTreeType>
230 Tree(const OtherTreeType& other,
231 const ValueType& inactiveValue,
232 const ValueType& activeValue,
233 TopologyCopy):
234 TreeBase(other),
235 mRoot(other.root(), inactiveValue, activeValue, TopologyCopy())
236 {
237 }
238
250 template<typename OtherTreeType>
251 Tree(const OtherTreeType& other, const ValueType& background, TopologyCopy):
252 TreeBase(other),
253 mRoot(other.root(), background, TopologyCopy())
254 {
255 }
256
258 Tree(const ValueType& background): mRoot(background) {}
259
260 ~Tree() override { this->clear(); releaseAllAccessors(); }
261
263 TreeBase::Ptr copy() const override { return TreeBase::Ptr(new Tree(*this)); }
264
266 Name valueType() const override { return typeNameAsString<ValueType>(); }
267
269 static const Name& treeType();
271 const Name& type() const override { return this->treeType(); }
272
273 bool operator==(const Tree&) const { OPENVDB_THROW(NotImplementedError, ""); }
274 bool operator!=(const Tree&) const { OPENVDB_THROW(NotImplementedError, ""); }
275
277 RootNodeType& root() { return mRoot; }
279 const RootNodeType& root() const { return mRoot; }
281
282
283 //
284 // Tree methods
285 //
288 template<typename OtherRootNodeType>
289 bool hasSameTopology(const Tree<OtherRootNodeType>& other) const;
290
291 bool evalLeafBoundingBox(CoordBBox& bbox) const override;
292 bool evalActiveVoxelBoundingBox(CoordBBox& bbox) const override;
293 bool evalActiveVoxelDim(Coord& dim) const override;
294 bool evalLeafDim(Coord& dim) const override;
295
299 static void getNodeLog2Dims(std::vector<Index>& dims);
300
301
302 //
303 // I/O methods
304 //
308 void readTopology(std::istream&, bool saveFloatAsHalf = false) override;
312 void writeTopology(std::ostream&, bool saveFloatAsHalf = false) const override;
314 void readBuffers(std::istream&, bool saveFloatAsHalf = false) override;
316 void readBuffers(std::istream&, const CoordBBox&, bool saveFloatAsHalf = false) override;
322 void readNonresidentBuffers() const override;
324 void writeBuffers(std::ostream&, bool saveFloatAsHalf = false) const override;
325
326 void print(std::ostream& os = std::cout, int verboseLevel = 1) const override;
327
328
329 //
330 // Statistics
331 //
335 Index treeDepth() const override { return DEPTH; }
337 Index32 leafCount() const override { return mRoot.leafCount(); }
338 #if OPENVDB_ABI_VERSION_NUMBER >= 7
339 std::vector<Index32> nodeCount() const override
343 {
344 std::vector<Index32> vec(DEPTH, 0);
345 mRoot.nodeCount( vec );
346 return vec;// Named Return Value Optimization
347 }
348 #endif
349 Index32 nonLeafCount() const override { return mRoot.nonLeafCount(); }
352 Index64 activeLeafVoxelCount() const override { return mRoot.onLeafVoxelCount(); }
354 Index64 inactiveLeafVoxelCount() const override { return mRoot.offLeafVoxelCount(); }
356 Index64 activeVoxelCount() const override { return mRoot.onVoxelCount(); }
358 Index64 inactiveVoxelCount() const override;
360 Index64 activeTileCount() const override { return mRoot.onTileCount(); }
361
363 void evalMinMax(ValueType &min, ValueType &max) const;
364
365 Index64 memUsage() const override { return sizeof(*this) + mRoot.memUsage(); }
366
367
368 //
369 // Voxel access methods (using signed indexing)
370 //
372 const ValueType& getValue(const Coord& xyz) const;
375 template<typename AccessT> const ValueType& getValue(const Coord& xyz, AccessT&) const;
376
380 int getValueDepth(const Coord& xyz) const;
381
383 void setActiveState(const Coord& xyz, bool on);
385 void setValueOnly(const Coord& xyz, const ValueType& value);
387 void setValueOn(const Coord& xyz);
389 void setValueOn(const Coord& xyz, const ValueType& value);
391 void setValue(const Coord& xyz, const ValueType& value);
394 template<typename AccessT> void setValue(const Coord& xyz, const ValueType& value, AccessT&);
396 void setValueOff(const Coord& xyz);
398 void setValueOff(const Coord& xyz, const ValueType& value);
399
418 template<typename ModifyOp>
419 void modifyValue(const Coord& xyz, const ModifyOp& op);
420
440 template<typename ModifyOp>
441 void modifyValueAndActiveState(const Coord& xyz, const ModifyOp& op);
442
445 bool probeValue(const Coord& xyz, ValueType& value) const;
446
448 bool isValueOn(const Coord& xyz) const { return mRoot.isValueOn(xyz); }
450 bool isValueOff(const Coord& xyz) const { return !this->isValueOn(xyz); }
452 bool hasActiveTiles() const { return mRoot.hasActiveTiles(); }
453
455 void clip(const CoordBBox&);
461 void clipUnallocatedNodes() override;
462
464 Index32 unallocatedLeafCount() const override;
465
467 void sparseFill(const CoordBBox& bbox, const ValueType& value, bool active = true);
476 void fill(const CoordBBox& bbox, const ValueType& value, bool active = true)
477 {
478 this->sparseFill(bbox, value, active);
479 }
481
489 void denseFill(const CoordBBox& bbox, const ValueType& value, bool active = true);
490
499 void voxelizeActiveTiles(bool threaded = true);
500
505 void prune(const ValueType& tolerance = zeroVal<ValueType>())
506 {
507 this->clearAllAccessors();
508 mRoot.prune(tolerance);
509 }
510
516 void addLeaf(LeafNodeType* leaf) { assert(leaf); mRoot.addLeaf(leaf); }
517
522 void addTile(Index level, const Coord& xyz, const ValueType& value, bool active);
523
528 template<typename NodeT>
529 NodeT* stealNode(const Coord& xyz, const ValueType& value, bool active);
530
536 LeafNodeType* touchLeaf(const Coord& xyz);
537
539 template<typename NodeType> NodeType* probeNode(const Coord& xyz);
542 template<typename NodeType> const NodeType* probeConstNode(const Coord& xyz) const;
543 template<typename NodeType> const NodeType* probeNode(const Coord& xyz) const;
545
547 LeafNodeType* probeLeaf(const Coord& xyz);
550 const LeafNodeType* probeConstLeaf(const Coord& xyz) const;
551 const LeafNodeType* probeLeaf(const Coord& xyz) const { return this->probeConstLeaf(xyz); }
553
555 template<typename ArrayT> void getNodes(ArrayT& array) { mRoot.getNodes(array); }
578 template<typename ArrayT> void getNodes(ArrayT& array) const { mRoot.getNodes(array); }
580
604 template<typename ArrayT>
605 void stealNodes(ArrayT& array) { this->clearAllAccessors(); mRoot.stealNodes(array); }
606 template<typename ArrayT>
607 void stealNodes(ArrayT& array, const ValueType& value, bool state)
608 {
609 this->clearAllAccessors();
610 mRoot.stealNodes(array, value, state);
611 }
612
613 //
614 // Aux methods
615 //
618 bool empty() const { return mRoot.empty(); }
619
621 void clear();
622
624 void clearAllAccessors();
625
627 void attachAccessor(ValueAccessorBase<Tree, true>&) const;
630 void attachAccessor(ValueAccessorBase<const Tree, true>&) const;
632
634 void attachAccessor(ValueAccessorBase<Tree, false>&) const {}
636 void attachAccessor(ValueAccessorBase<const Tree, false>&) const {}
638
640 void releaseAccessor(ValueAccessorBase<Tree, true>&) const;
642 void releaseAccessor(ValueAccessorBase<const Tree, true>&) const;
644
646 void releaseAccessor(ValueAccessorBase<Tree, false>&) const {}
648 void releaseAccessor(ValueAccessorBase<const Tree, false>&) const {}
650
653 Metadata::Ptr getBackgroundValue() const override;
654
660 const ValueType& background() const { return mRoot.background(); }
661
663 void getIndexRange(CoordBBox& bbox) const override { mRoot.getIndexRange(bbox); }
664
672 void merge(Tree& other, MergePolicy = MERGE_ACTIVE_STATES);
673
687 template<typename OtherRootNodeType>
688 void topologyUnion(const Tree<OtherRootNodeType>& other);
689
703 template<typename OtherRootNodeType>
704 void topologyIntersection(const Tree<OtherRootNodeType>& other);
705
716 template<typename OtherRootNodeType>
717 void topologyDifference(const Tree<OtherRootNodeType>& other);
718
763 template<typename CombineOp>
764 void combine(Tree& other, CombineOp& op, bool prune = false);
765 #ifndef _MSC_VER
766 template<typename CombineOp>
767 void combine(Tree& other, const CombineOp& op, bool prune = false);
768 #endif
769
808 template<typename ExtendedCombineOp>
809 void combineExtended(Tree& other, ExtendedCombineOp& op, bool prune = false);
810 #ifndef _MSC_VER
811 template<typename ExtendedCombineOp>
812 void combineExtended(Tree& other, const ExtendedCombineOp& op, bool prune = false);
813 #endif
814
843 template<typename CombineOp, typename OtherTreeType /*= Tree*/>
844 void combine2(const Tree& a, const OtherTreeType& b, CombineOp& op, bool prune = false);
845 #ifndef _MSC_VER
846 template<typename CombineOp, typename OtherTreeType /*= Tree*/>
847 void combine2(const Tree& a, const OtherTreeType& b, const CombineOp& op, bool prune = false);
848 #endif
849
923 template<typename ExtendedCombineOp, typename OtherTreeType /*= Tree*/>
924 void combine2Extended(const Tree& a, const OtherTreeType& b, ExtendedCombineOp& op,
925 bool prune = false);
926 #ifndef _MSC_VER
927 template<typename ExtendedCombineOp, typename OtherTreeType /*= Tree*/>
928 void combine2Extended(const Tree& a, const OtherTreeType& b, const ExtendedCombineOp&,
929 bool prune = false);
930 #endif
931
972 template<typename BBoxOp> void visitActiveBBox(BBoxOp& op) const { mRoot.visitActiveBBox(op); }
973
1027 template<typename VisitorOp> void visit(VisitorOp& op);
1028 template<typename VisitorOp> void visit(const VisitorOp& op);
1029
1034 template<typename VisitorOp> void visit(VisitorOp& op) const;
1035 template<typename VisitorOp> void visit(const VisitorOp& op) const;
1036
1084 template<typename OtherTreeType, typename VisitorOp>
1085 void visit2(OtherTreeType& other, VisitorOp& op);
1086 template<typename OtherTreeType, typename VisitorOp>
1087 void visit2(OtherTreeType& other, const VisitorOp& op);
1088
1099 template<typename OtherTreeType, typename VisitorOp>
1100 void visit2(OtherTreeType& other, VisitorOp& op) const;
1101 template<typename OtherTreeType, typename VisitorOp>
1102 void visit2(OtherTreeType& other, const VisitorOp& op) const;
1103
1104
1105 //
1106 // Iteration
1107 //
1109 typename RootNodeType::ChildOnCIter beginRootChildren() const { return mRoot.cbeginChildOn(); }
1111 typename RootNodeType::ChildOnCIter cbeginRootChildren() const { return mRoot.cbeginChildOn(); }
1112 typename RootNodeType::ChildOnIter beginRootChildren() { return mRoot.beginChildOn(); }
1114
1116 typename RootNodeType::ChildOffCIter beginRootTiles() const { return mRoot.cbeginChildOff(); }
1118 typename RootNodeType::ChildOffCIter cbeginRootTiles() const { return mRoot.cbeginChildOff(); }
1119 typename RootNodeType::ChildOffIter beginRootTiles() { return mRoot.beginChildOff(); }
1121
1123 typename RootNodeType::ChildAllCIter beginRootDense() const { return mRoot.cbeginChildAll(); }
1125 typename RootNodeType::ChildAllCIter cbeginRootDense() const { return mRoot.cbeginChildAll(); }
1126 typename RootNodeType::ChildAllIter beginRootDense() { return mRoot.beginChildAll(); }
1128
1129
1131 using NodeIter = NodeIteratorBase<Tree, typename RootNodeType::ChildOnIter>;
1133 using NodeCIter = NodeIteratorBase<const Tree, typename RootNodeType::ChildOnCIter>;
1135
1137 using LeafIter = LeafIteratorBase<Tree, typename RootNodeType::ChildOnIter>;
1139 using LeafCIter = LeafIteratorBase<const Tree, typename RootNodeType::ChildOnCIter>;
1141
1143 NodeIter beginNode() { return NodeIter(*this); }
1145 NodeCIter beginNode() const { return NodeCIter(*this); }
1146 NodeCIter cbeginNode() const { return NodeCIter(*this); }
1148
1150 LeafIter beginLeaf() { return LeafIter(*this); }
1152 LeafCIter beginLeaf() const { return LeafCIter(*this); }
1153 LeafCIter cbeginLeaf() const { return LeafCIter(*this); }
1155
1156 using ValueAllIter = TreeValueIteratorBase<Tree, typename RootNodeType::ValueAllIter>;
1157 using ValueAllCIter = TreeValueIteratorBase<const Tree, typename RootNodeType::ValueAllCIter>;
1158 using ValueOnIter = TreeValueIteratorBase<Tree, typename RootNodeType::ValueOnIter>;
1159 using ValueOnCIter = TreeValueIteratorBase<const Tree, typename RootNodeType::ValueOnCIter>;
1160 using ValueOffIter = TreeValueIteratorBase<Tree, typename RootNodeType::ValueOffIter>;
1161 using ValueOffCIter = TreeValueIteratorBase<const Tree, typename RootNodeType::ValueOffCIter>;
1162
1164 ValueAllIter beginValueAll() { return ValueAllIter(*this); }
1166 ValueAllCIter beginValueAll() const { return ValueAllCIter(*this); }
1167 ValueAllCIter cbeginValueAll() const { return ValueAllCIter(*this); }
1169
1170 ValueOnIter beginValueOn() { return ValueOnIter(*this); }
1172 ValueOnCIter beginValueOn() const { return ValueOnCIter(*this); }
1173 ValueOnCIter cbeginValueOn() const { return ValueOnCIter(*this); }
1175
1176 ValueOffIter beginValueOff() { return ValueOffIter(*this); }
1178 ValueOffCIter beginValueOff() const { return ValueOffCIter(*this); }
1179 ValueOffCIter cbeginValueOff() const { return ValueOffCIter(*this); }
1181
1184 template<typename IterT> IterT begin();
1187 template<typename CIterT> CIterT cbegin() const;
1188
1189
1190 protected:
1191 using AccessorRegistry = tbb::concurrent_hash_map<ValueAccessorBase<Tree, true>*, bool>;
1192 using ConstAccessorRegistry = tbb::concurrent_hash_map<ValueAccessorBase<const Tree, true>*, bool>;
1193
1196 void releaseAllAccessors();
1197
1198 // TBB body object used to deallocates nodes in parallel.
1199 template<typename NodeType>
1200 struct DeallocateNodes {
1201 DeallocateNodes(std::vector<NodeType*>& nodes)
1202 : mNodes(nodes.empty() ? nullptr : &nodes.front()) { }
1203 void operator()(const tbb::blocked_range<size_t>& range) const {
1204 for (size_t n = range.begin(), N = range.end(); n < N; ++n) {
1205 delete mNodes[n]; mNodes[n] = nullptr;
1206 }
1207 }
1208 NodeType ** const mNodes;
1209 };
1210
1211 //
1212 // Data members
1213 //
1214 RootNodeType mRoot; // root node of the tree
1215 mutable AccessorRegistry mAccessorRegistry;
1216 mutable ConstAccessorRegistry mConstAccessorRegistry;
1217
1218 static std::unique_ptr<const Name> sTreeTypeName;
1219 }; // end of Tree class
1220
1221 template<typename _RootNodeType>
1222 std::unique_ptr<const Name> Tree<_RootNodeType>::sTreeTypeName;
1223
1224
1229 template<typename T, Index N1=4, Index N2=3>
1230 struct Tree3 {
1231 using Type = Tree<RootNode<InternalNode<LeafNode<T, N2>, N1>>>;
1232 };
1233
1234
1239 template<typename T, Index N1=5, Index N2=4, Index N3=3>
1240 struct Tree4 {
1241 using Type = Tree<RootNode<InternalNode<InternalNode<LeafNode<T, N3>, N2>, N1>>>;
1242 };
1243
1248 template<typename T, Index N1=6, Index N2=5, Index N3=4, Index N4=3>
1249 struct Tree5 {
1250 using Type =
1251 Tree<RootNode<InternalNode<InternalNode<InternalNode<LeafNode<T, N4>, N3>, N2>, N1>>>;
1252 };
1253
1254
1256
1257
1258 inline void
1259 TreeBase::readTopology(std::istream& is, bool /*saveFloatAsHalf*/)
1260 {
1261 int32_t bufferCount;
1262 is.read(reinterpret_cast<char*>(&bufferCount), sizeof(int32_t));
1263 if (bufferCount != 1) OPENVDB_LOG_WARN("multi-buffer trees are no longer supported");
1264 }
1265
1266
1267 inline void
1268 TreeBase::writeTopology(std::ostream& os, bool /*saveFloatAsHalf*/) const
1269 {
1270 int32_t bufferCount = 1;
1271 os.write(reinterpret_cast<char*>(&bufferCount), sizeof(int32_t));
1272 }
1273
1274
1275 inline void
1276 TreeBase::print(std::ostream& os, int /*verboseLevel*/) const
1277 {
1278 os << " Tree Type: " << type()
1279 << " Active Voxel Count: " << activeVoxelCount() << std::endl
1280 << " Active tile Count: " << activeTileCount() << std::endl
1281 << " Inactive Voxel Count: " << inactiveVoxelCount() << std::endl
1282 << " Leaf Node Count: " << leafCount() << std::endl
1283 << " Non-leaf Node Count: " << nonLeafCount() << std::endl;
1284 }
1285
1286
1288
1289
1290 //
1291 // Type traits for tree iterators
1292 //
1293
1296 template<typename TreeT, typename IterT> struct TreeIterTraits;
1297
1298 template<typename TreeT> struct TreeIterTraits<TreeT, typename TreeT::RootNodeType::ChildOnIter> {
1299 static typename TreeT::RootNodeType::ChildOnIter begin(TreeT& tree) {
1300 return tree.beginRootChildren();
1301 }
1302 };
1303
1304 template<typename TreeT> struct TreeIterTraits<TreeT, typename TreeT::RootNodeType::ChildOnCIter> {
1305 static typename TreeT::RootNodeType::ChildOnCIter begin(const TreeT& tree) {
1306 return tree.cbeginRootChildren();
1307 }
1308 };
1309
1310 template<typename TreeT> struct TreeIterTraits<TreeT, typename TreeT::RootNodeType::ChildOffIter> {
1311 static typename TreeT::RootNodeType::ChildOffIter begin(TreeT& tree) {
1312 return tree.beginRootTiles();
1313 }
1314 };
1315
1316 template<typename TreeT> struct TreeIterTraits<TreeT, typename TreeT::RootNodeType::ChildOffCIter> {
1317 static typename TreeT::RootNodeType::ChildOffCIter begin(const TreeT& tree) {
1318 return tree.cbeginRootTiles();
1319 }
1320 };
1321
1322 template<typename TreeT> struct TreeIterTraits<TreeT, typename TreeT::RootNodeType::ChildAllIter> {
1323 static typename TreeT::RootNodeType::ChildAllIter begin(TreeT& tree) {
1324 return tree.beginRootDense();
1325 }
1326 };
1327
1328 template<typename TreeT> struct TreeIterTraits<TreeT, typename TreeT::RootNodeType::ChildAllCIter> {
1329 static typename TreeT::RootNodeType::ChildAllCIter begin(const TreeT& tree) {
1330 return tree.cbeginRootDense();
1331 }
1332 };
1333
1334 template<typename TreeT> struct TreeIterTraits<TreeT, typename TreeT::NodeIter> {
1335 static typename TreeT::NodeIter begin(TreeT& tree) { return tree.beginNode(); }
1336 };
1337
1338 template<typename TreeT> struct TreeIterTraits<TreeT, typename TreeT::NodeCIter> {
1339 static typename TreeT::NodeCIter begin(const TreeT& tree) { return tree.cbeginNode(); }
1340 };
1341
1342 template<typename TreeT> struct TreeIterTraits<TreeT, typename TreeT::LeafIter> {
1343 static typename TreeT::LeafIter begin(TreeT& tree) { return tree.beginLeaf(); }
1344 };
1345
1346 template<typename TreeT> struct TreeIterTraits<TreeT, typename TreeT::LeafCIter> {
1347 static typename TreeT::LeafCIter begin(const TreeT& tree) { return tree.cbeginLeaf(); }
1348 };
1349
1350 template<typename TreeT> struct TreeIterTraits<TreeT, typename TreeT::ValueOnIter> {
1351 static typename TreeT::ValueOnIter begin(TreeT& tree) { return tree.beginValueOn(); }
1352 };
1353
1354 template<typename TreeT> struct TreeIterTraits<TreeT, typename TreeT::ValueOnCIter> {
1355 static typename TreeT::ValueOnCIter begin(const TreeT& tree) { return tree.cbeginValueOn(); }
1356 };
1357
1358 template<typename TreeT> struct TreeIterTraits<TreeT, typename TreeT::ValueOffIter> {
1359 static typename TreeT::ValueOffIter begin(TreeT& tree) { return tree.beginValueOff(); }
1360 };
1361
1362 template<typename TreeT> struct TreeIterTraits<TreeT, typename TreeT::ValueOffCIter> {
1363 static typename TreeT::ValueOffCIter begin(const TreeT& tree) { return tree.cbeginValueOff(); }
1364 };
1365
1366 template<typename TreeT> struct TreeIterTraits<TreeT, typename TreeT::ValueAllIter> {
1367 static typename TreeT::ValueAllIter begin(TreeT& tree) { return tree.beginValueAll(); }
1368 };
1369
1370 template<typename TreeT> struct TreeIterTraits<TreeT, typename TreeT::ValueAllCIter> {
1371 static typename TreeT::ValueAllCIter begin(const TreeT& tree) { return tree.cbeginValueAll(); }
1372 };
1373
1374
1375 template<typename RootNodeType>
1376 template<typename IterT>
1377 inline IterT
1378 Tree<RootNodeType>::begin()
1379 {
1380 return TreeIterTraits<Tree, IterT>::begin(*this);
1381 }
1382
1383
1384 template<typename RootNodeType>
1385 template<typename IterT>
1386 inline IterT
1387 Tree<RootNodeType>::cbegin() const
1388 {
1389 return TreeIterTraits<Tree, IterT>::begin(*this);
1390 }
1391
1392
1394
1395
1396 template<typename RootNodeType>
1397 void
1398 Tree<RootNodeType>::readTopology(std::istream& is, bool saveFloatAsHalf)
1399 {
1400 this->clearAllAccessors();
1401 TreeBase::readTopology(is, saveFloatAsHalf);
1402 mRoot.readTopology(is, saveFloatAsHalf);
1403 }
1404
1405
1406 template<typename RootNodeType>
1407 void
1408 Tree<RootNodeType>::writeTopology(std::ostream& os, bool saveFloatAsHalf) const
1409 {
1410 TreeBase::writeTopology(os, saveFloatAsHalf);
1411 mRoot.writeTopology(os, saveFloatAsHalf);
1412 }
1413
1414
1415 template<typename RootNodeType>
1416 inline void
1417 Tree<RootNodeType>::readBuffers(std::istream &is, bool saveFloatAsHalf)
1418 {
1419 this->clearAllAccessors();
1420 mRoot.readBuffers(is, saveFloatAsHalf);
1421 }
1422
1423
1424 template<typename RootNodeType>
1425 inline void
1426 Tree<RootNodeType>::readBuffers(std::istream &is, const CoordBBox& bbox, bool saveFloatAsHalf)
1427 {
1428 this->clearAllAccessors();
1429 mRoot.readBuffers(is, bbox, saveFloatAsHalf);
1430 }
1431
1432
1433 template<typename RootNodeType>
1434 inline void
1435 Tree<RootNodeType>::readNonresidentBuffers() const
1436 {
1437 for (LeafCIter it = this->cbeginLeaf(); it; ++it) {
1438 // Retrieving the value of a leaf voxel forces loading of the leaf node's voxel buffer.
1439 it->getValue(Index(0));
1440 }
1441 }
1442
1443
1444 template<typename RootNodeType>
1445 inline void
1446 Tree<RootNodeType>::writeBuffers(std::ostream &os, bool saveFloatAsHalf) const
1447 {
1448 mRoot.writeBuffers(os, saveFloatAsHalf);
1449 }
1450
1451
1452 template<typename RootNodeType>
1453 inline void
1454 Tree<RootNodeType>::clear()
1455 {
1456 std::vector<LeafNodeType*> leafnodes;
1457 this->stealNodes(leafnodes);
1458
1459 tbb::parallel_for(tbb::blocked_range<size_t>(0, leafnodes.size()),
1460 DeallocateNodes<LeafNodeType>(leafnodes));
1461
1462 std::vector<typename RootNodeType::ChildNodeType*> internalNodes;
1463 this->stealNodes(internalNodes);
1464
1465 tbb::parallel_for(tbb::blocked_range<size_t>(0, internalNodes.size()),
1466 DeallocateNodes<typename RootNodeType::ChildNodeType>(internalNodes));
1467
1468 mRoot.clear();
1469
1470 this->clearAllAccessors();
1471 }
1472
1473
1475
1476
1477 template<typename RootNodeType>
1478 inline void
1479 Tree<RootNodeType>::attachAccessor(ValueAccessorBase<Tree, true>& accessor) const
1480 {
1481 typename AccessorRegistry::accessor a;
1482 mAccessorRegistry.insert(a, &accessor);
1483 }
1484
1485
1486 template<typename RootNodeType>
1487 inline void
1488 Tree<RootNodeType>::attachAccessor(ValueAccessorBase<const Tree, true>& accessor) const
1489 {
1490 typename ConstAccessorRegistry::accessor a;
1491 mConstAccessorRegistry.insert(a, &accessor);
1492 }
1493
1494
1495 template<typename RootNodeType>
1496 inline void
1497 Tree<RootNodeType>::releaseAccessor(ValueAccessorBase<Tree, true>& accessor) const
1498 {
1499 mAccessorRegistry.erase(&accessor);
1500 }
1501
1502
1503 template<typename RootNodeType>
1504 inline void
1505 Tree<RootNodeType>::releaseAccessor(ValueAccessorBase<const Tree, true>& accessor) const
1506 {
1507 mConstAccessorRegistry.erase(&accessor);
1508 }
1509
1510
1511 template<typename RootNodeType>
1512 inline void
1513 Tree<RootNodeType>::clearAllAccessors()
1514 {
1515 for (typename AccessorRegistry::iterator it = mAccessorRegistry.begin();
1516 it != mAccessorRegistry.end(); ++it)
1517 {
1518 if (it->first) it->first->clear();
1519 }
1520
1521 for (typename ConstAccessorRegistry::iterator it = mConstAccessorRegistry.begin();
1522 it != mConstAccessorRegistry.end(); ++it)
1523 {
1524 if (it->first) it->first->clear();
1525 }
1526 }
1527
1528
1529 template<typename RootNodeType>
1530 inline void
1531 Tree<RootNodeType>::releaseAllAccessors()
1532 {
1533 mAccessorRegistry.erase(nullptr);
1534 for (typename AccessorRegistry::iterator it = mAccessorRegistry.begin();
1535 it != mAccessorRegistry.end(); ++it)
1536 {
1537 it->first->release();
1538 }
1539 mAccessorRegistry.clear();
1540
1541 mAccessorRegistry.erase(nullptr);
1542 for (typename ConstAccessorRegistry::iterator it = mConstAccessorRegistry.begin();
1543 it != mConstAccessorRegistry.end(); ++it)
1544 {
1545 it->first->release();
1546 }
1547 mConstAccessorRegistry.clear();
1548 }
1549
1550
1552
1553
1554 template<typename RootNodeType>
1555 inline const typename RootNodeType::ValueType&
1556 Tree<RootNodeType>::getValue(const Coord& xyz) const
1557 {
1558 return mRoot.getValue(xyz);
1559 }
1560
1561
1562 template<typename RootNodeType>
1563 template<typename AccessT>
1564 inline const typename RootNodeType::ValueType&
1565 Tree<RootNodeType>::getValue(const Coord& xyz, AccessT& accessor) const
1566 {
1567 return accessor.getValue(xyz);
1568 }
1569
1570
1571 template<typename RootNodeType>
1572 inline int
1573 Tree<RootNodeType>::getValueDepth(const Coord& xyz) const
1574 {
1575 return mRoot.getValueDepth(xyz);
1576 }
1577
1578
1579 template<typename RootNodeType>
1580 inline void
1581 Tree<RootNodeType>::setValueOff(const Coord& xyz)
1582 {
1583 mRoot.setValueOff(xyz);
1584 }
1585
1586
1587 template<typename RootNodeType>
1588 inline void
1589 Tree<RootNodeType>::setValueOff(const Coord& xyz, const ValueType& value)
1590 {
1591 mRoot.setValueOff(xyz, value);
1592 }
1593
1594
1595 template<typename RootNodeType>
1596 inline void
1597 Tree<RootNodeType>::setActiveState(const Coord& xyz, bool on)
1598 {
1599 mRoot.setActiveState(xyz, on);
1600 }
1601
1602
1603 template<typename RootNodeType>
1604 inline void
1605 Tree<RootNodeType>::setValue(const Coord& xyz, const ValueType& value)
1606 {
1607 mRoot.setValueOn(xyz, value);
1608 }
1609
1610 template<typename RootNodeType>
1611 inline void
1612 Tree<RootNodeType>::setValueOnly(const Coord& xyz, const ValueType& value)
1613 {
1614 mRoot.setValueOnly(xyz, value);
1615 }
1616
1617 template<typename RootNodeType>
1618 template<typename AccessT>
1619 inline void
1620 Tree<RootNodeType>::setValue(const Coord& xyz, const ValueType& value, AccessT& accessor)
1621 {
1622 accessor.setValue(xyz, value);
1623 }
1624
1625
1626 template<typename RootNodeType>
1627 inline void
1628 Tree<RootNodeType>::setValueOn(const Coord& xyz)
1629 {
1630 mRoot.setActiveState(xyz, true);
1631 }
1632
1633
1634 template<typename RootNodeType>
1635 inline void
1636 Tree<RootNodeType>::setValueOn(const Coord& xyz, const ValueType& value)
1637 {
1638 mRoot.setValueOn(xyz, value);
1639 }
1640
1641
1642 template<typename RootNodeType>
1643 template<typename ModifyOp>
1644 inline void
1645 Tree<RootNodeType>::modifyValue(const Coord& xyz, const ModifyOp& op)
1646 {
1647 mRoot.modifyValue(xyz, op);
1648 }
1649
1650
1651 template<typename RootNodeType>
1652 template<typename ModifyOp>
1653 inline void
1654 Tree<RootNodeType>::modifyValueAndActiveState(const Coord& xyz, const ModifyOp& op)
1655 {
1656 mRoot.modifyValueAndActiveState(xyz, op);
1657 }
1658
1659
1660 template<typename RootNodeType>
1661 inline bool
1662 Tree<RootNodeType>::probeValue(const Coord& xyz, ValueType& value) const
1663 {
1664 return mRoot.probeValue(xyz, value);
1665 }
1666
1667
1669
1670
1671 template<typename RootNodeType>
1672 inline void
1673 Tree<RootNodeType>::addTile(Index level, const Coord& xyz,
1674 const ValueType& value, bool active)
1675 {
1676 mRoot.addTile(level, xyz, value, active);
1677 }
1678
1679
1680 template<typename RootNodeType>
1681 template<typename NodeT>
1682 inline NodeT*
1683 Tree<RootNodeType>::stealNode(const Coord& xyz, const ValueType& value, bool active)
1684 {
1685 this->clearAllAccessors();
1686 return mRoot.template stealNode<NodeT>(xyz, value, active);
1687 }
1688
1689
1690 template<typename RootNodeType>
1691 inline typename RootNodeType::LeafNodeType*
1692 Tree<RootNodeType>::touchLeaf(const Coord& xyz)
1693 {
1694 return mRoot.touchLeaf(xyz);
1695 }
1696
1697
1698 template<typename RootNodeType>
1699 inline typename RootNodeType::LeafNodeType*
1700 Tree<RootNodeType>::probeLeaf(const Coord& xyz)
1701 {
1702 return mRoot.probeLeaf(xyz);
1703 }
1704
1705
1706 template<typename RootNodeType>
1707 inline const typename RootNodeType::LeafNodeType*
1708 Tree<RootNodeType>::probeConstLeaf(const Coord& xyz) const
1709 {
1710 return mRoot.probeConstLeaf(xyz);
1711 }
1712
1713
1714 template<typename RootNodeType>
1715 template<typename NodeType>
1716 inline NodeType*
1717 Tree<RootNodeType>::probeNode(const Coord& xyz)
1718 {
1719 return mRoot.template probeNode<NodeType>(xyz);
1720 }
1721
1722
1723 template<typename RootNodeType>
1724 template<typename NodeType>
1725 inline const NodeType*
1726 Tree<RootNodeType>::probeNode(const Coord& xyz) const
1727 {
1728 return this->template probeConstNode<NodeType>(xyz);
1729 }
1730
1731
1732 template<typename RootNodeType>
1733 template<typename NodeType>
1734 inline const NodeType*
1735 Tree<RootNodeType>::probeConstNode(const Coord& xyz) const
1736 {
1737 return mRoot.template probeConstNode<NodeType>(xyz);
1738 }
1739
1740
1742
1743
1744 template<typename RootNodeType>
1745 inline void
1746 Tree<RootNodeType>::clip(const CoordBBox& bbox)
1747 {
1748 this->clearAllAccessors();
1749 return mRoot.clip(bbox);
1750 }
1751
1752
1753 template<typename RootNodeType>
1754 inline void
1755 Tree<RootNodeType>::clipUnallocatedNodes()
1756 {
1757 this->clearAllAccessors();
1758 for (LeafIter it = this->beginLeaf(); it; ) {
1759 const LeafNodeType* leaf = it.getLeaf();
1760 ++it; // advance the iterator before deleting the leaf node
1761 if (!leaf->isAllocated()) {
1762 this->addTile(/*level=*/0, leaf->origin(), this->background(), /*active=*/false);
1763 }
1764 }
1765 }
1766
1767 template<typename RootNodeType>
1768 inline Index32
1769 Tree<RootNodeType>::unallocatedLeafCount() const
1770 {
1771 Index32 sum = 0;
1772 for (auto it = this->cbeginLeaf(); it; ++it) if (!it->isAllocated()) ++sum;
1773 return sum;
1774 }
1775
1776
1777 template<typename RootNodeType>
1778 inline void
1779 Tree<RootNodeType>::sparseFill(const CoordBBox& bbox, const ValueType& value, bool active)
1780 {
1781 this->clearAllAccessors();
1782 return mRoot.sparseFill(bbox, value, active);
1783 }
1784
1785
1786 template<typename RootNodeType>
1787 inline void
1788 Tree<RootNodeType>::denseFill(const CoordBBox& bbox, const ValueType& value, bool active)
1789 {
1790 this->clearAllAccessors();
1791 return mRoot.denseFill(bbox, value, active);
1792 }
1793
1794
1795 template<typename RootNodeType>
1796 inline void
1797 Tree<RootNodeType>::voxelizeActiveTiles(bool threaded)
1798 {
1799 this->clearAllAccessors();
1800 mRoot.voxelizeActiveTiles(threaded);
1801 }
1802
1803
1804 template<typename RootNodeType>
1805 Metadata::Ptr
1806 Tree<RootNodeType>::getBackgroundValue() const
1807 {
1808 Metadata::Ptr result;
1809 if (Metadata::isRegisteredType(valueType())) {
1810 using MetadataT = TypedMetadata<ValueType>;
1811 result = Metadata::createMetadata(valueType());
1812 if (result->typeName() == MetadataT::staticTypeName()) {
1813 MetadataT* m = static_cast<MetadataT*>(result.get());
1814 m->value() = mRoot.background();
1815 }
1816 }
1817 return result;
1818 }
1819
1820
1822
1823
1824 template<typename RootNodeType>
1825 inline void
1826 Tree<RootNodeType>::merge(Tree& other, MergePolicy policy)
1827 {
1828 this->clearAllAccessors();
1829 other.clearAllAccessors();
1830 switch (policy) {
1831 case MERGE_ACTIVE_STATES:
1832 mRoot.template merge<MERGE_ACTIVE_STATES>(other.mRoot); break;
1833 case MERGE_NODES:
1834 mRoot.template merge<MERGE_NODES>(other.mRoot); break;
1835 case MERGE_ACTIVE_STATES_AND_NODES:
1836 mRoot.template merge<MERGE_ACTIVE_STATES_AND_NODES>(other.mRoot); break;
1837 }
1838 }
1839
1840
1841 template<typename RootNodeType>
1842 template<typename OtherRootNodeType>
1843 inline void
1844 Tree<RootNodeType>::topologyUnion(const Tree<OtherRootNodeType>& other)
1845 {
1846 this->clearAllAccessors();
1847 mRoot.topologyUnion(other.root());
1848 }
1849
1850 template<typename RootNodeType>
1851 template<typename OtherRootNodeType>
1852 inline void
1853 Tree<RootNodeType>::topologyIntersection(const Tree<OtherRootNodeType>& other)
1854 {
1855 this->clearAllAccessors();
1856 mRoot.topologyIntersection(other.root());
1857 }
1858
1859 template<typename RootNodeType>
1860 template<typename OtherRootNodeType>
1861 inline void
1862 Tree<RootNodeType>::topologyDifference(const Tree<OtherRootNodeType>& other)
1863 {
1864 this->clearAllAccessors();
1865 mRoot.topologyDifference(other.root());
1866 }
1867
1869
1870
1873 template<typename AValueT, typename CombineOp, typename BValueT = AValueT>
1874 struct CombineOpAdapter
1875 {
1876 CombineOpAdapter(CombineOp& _op): op(_op) {}
1877
1878 void operator()(CombineArgs<AValueT, BValueT>& args) const {
1879 op(args.a(), args.b(), args.result());
1880 }
1881
1882 CombineOp& op;
1883 };
1884
1885
1886 template<typename RootNodeType>
1887 template<typename CombineOp>
1888 inline void
1889 Tree<RootNodeType>::combine(Tree& other, CombineOp& op, bool prune)
1890 {
1891 CombineOpAdapter<ValueType, CombineOp> extendedOp(op);
1892 this->combineExtended(other, extendedOp, prune);
1893 }
1894
1895
1898 #ifndef _MSC_VER
1899 template<typename RootNodeType>
1900 template<typename CombineOp>
1901 inline void
1902 Tree<RootNodeType>::combine(Tree& other, const CombineOp& op, bool prune)
1903 {
1904 CombineOpAdapter<ValueType, const CombineOp> extendedOp(op);
1905 this->combineExtended(other, extendedOp, prune);
1906 }
1907 #endif
1908
1909
1910 template<typename RootNodeType>
1911 template<typename ExtendedCombineOp>
1912 inline void
1913 Tree<RootNodeType>::combineExtended(Tree& other, ExtendedCombineOp& op, bool prune)
1914 {
1915 this->clearAllAccessors();
1916 mRoot.combine(other.root(), op, prune);
1917 }
1918
1919
1922 #ifndef _MSC_VER
1923 template<typename RootNodeType>
1924 template<typename ExtendedCombineOp>
1925 inline void
1926 Tree<RootNodeType>::combineExtended(Tree& other, const ExtendedCombineOp& op, bool prune)
1927 {
1928 this->clearAllAccessors();
1929 mRoot.template combine<const ExtendedCombineOp>(other.mRoot, op, prune);
1930 }
1931 #endif
1932
1933
1934 template<typename RootNodeType>
1935 template<typename CombineOp, typename OtherTreeType>
1936 inline void
1937 Tree<RootNodeType>::combine2(const Tree& a, const OtherTreeType& b, CombineOp& op, bool prune)
1938 {
1939 CombineOpAdapter<ValueType, CombineOp, typename OtherTreeType::ValueType> extendedOp(op);
1940 this->combine2Extended(a, b, extendedOp, prune);
1941 }
1942
1943
1946 #ifndef _MSC_VER
1947 template<typename RootNodeType>
1948 template<typename CombineOp, typename OtherTreeType>
1949 inline void
1950 Tree<RootNodeType>::combine2(const Tree& a, const OtherTreeType& b, const CombineOp& op, bool prune)
1951 {
1952 CombineOpAdapter<ValueType, const CombineOp, typename OtherTreeType::ValueType> extendedOp(op);
1953 this->combine2Extended(a, b, extendedOp, prune);
1954 }
1955 #endif
1956
1957
1958 template<typename RootNodeType>
1959 template<typename ExtendedCombineOp, typename OtherTreeType>
1960 inline void
1961 Tree<RootNodeType>::combine2Extended(const Tree& a, const OtherTreeType& b,
1962 ExtendedCombineOp& op, bool prune)
1963 {
1964 this->clearAllAccessors();
1965 mRoot.combine2(a.root(), b.root(), op, prune);
1966 }
1967
1968
1972 #ifndef _MSC_VER
1973 template<typename RootNodeType>
1974 template<typename ExtendedCombineOp, typename OtherTreeType>
1975 inline void
1976 Tree<RootNodeType>::combine2Extended(const Tree& a, const OtherTreeType& b,
1977 const ExtendedCombineOp& op, bool prune)
1978 {
1979 this->clearAllAccessors();
1980 mRoot.template combine2<const ExtendedCombineOp>(a.root(), b.root(), op, prune);
1981 }
1982 #endif
1983
1984
1986
1987
1988 template<typename RootNodeType>
1989 template<typename VisitorOp>
1990 inline void
1991 Tree<RootNodeType>::visit(VisitorOp& op)
1992 {
1993 this->clearAllAccessors();
1994 mRoot.template visit<VisitorOp>(op);
1995 }
1996
1997
1998 template<typename RootNodeType>
1999 template<typename VisitorOp>
2000 inline void
2001 Tree<RootNodeType>::visit(VisitorOp& op) const
2002 {
2003 mRoot.template visit<VisitorOp>(op);
2004 }
2005
2006
2009 template<typename RootNodeType>
2010 template<typename VisitorOp>
2011 inline void
2012 Tree<RootNodeType>::visit(const VisitorOp& op)
2013 {
2014 this->clearAllAccessors();
2015 mRoot.template visit<const VisitorOp>(op);
2016 }
2017
2018
2021 template<typename RootNodeType>
2022 template<typename VisitorOp>
2023 inline void
2024 Tree<RootNodeType>::visit(const VisitorOp& op) const
2025 {
2026 mRoot.template visit<const VisitorOp>(op);
2027 }
2028
2029
2031
2032
2033 template<typename RootNodeType>
2034 template<typename OtherTreeType, typename VisitorOp>
2035 inline void
2036 Tree<RootNodeType>::visit2(OtherTreeType& other, VisitorOp& op)
2037 {
2038 this->clearAllAccessors();
2039 using OtherRootNodeType = typename OtherTreeType::RootNodeType;
2040 mRoot.template visit2<OtherRootNodeType, VisitorOp>(other.root(), op);
2041 }
2042
2043
2044 template<typename RootNodeType>
2045 template<typename OtherTreeType, typename VisitorOp>
2046 inline void
2047 Tree<RootNodeType>::visit2(OtherTreeType& other, VisitorOp& op) const
2048 {
2049 using OtherRootNodeType = typename OtherTreeType::RootNodeType;
2050 mRoot.template visit2<OtherRootNodeType, VisitorOp>(other.root(), op);
2051 }
2052
2053
2056 template<typename RootNodeType>
2057 template<typename OtherTreeType, typename VisitorOp>
2058 inline void
2059 Tree<RootNodeType>::visit2(OtherTreeType& other, const VisitorOp& op)
2060 {
2061 this->clearAllAccessors();
2062 using OtherRootNodeType = typename OtherTreeType::RootNodeType;
2063 mRoot.template visit2<OtherRootNodeType, const VisitorOp>(other.root(), op);
2064 }
2065
2066
2069 template<typename RootNodeType>
2070 template<typename OtherTreeType, typename VisitorOp>
2071 inline void
2072 Tree<RootNodeType>::visit2(OtherTreeType& other, const VisitorOp& op) const
2073 {
2074 using OtherRootNodeType = typename OtherTreeType::RootNodeType;
2075 mRoot.template visit2<OtherRootNodeType, const VisitorOp>(other.root(), op);
2076 }
2077
2078
2080
2081
2082 template<typename RootNodeType>
2083 inline const Name&
2084 Tree<RootNodeType>::treeType()
2085 {
2086 static std::once_flag once;
2087 std::call_once(once, []()
2088 {
2089 std::vector<Index> dims;
2090 Tree::getNodeLog2Dims(dims);
2091 std::ostringstream ostr;
2092 ostr << "Tree_" << typeNameAsString<BuildType>();
2093 for (size_t i = 1, N = dims.size(); i < N; ++i) { // start from 1 to skip the RootNode
2094 ostr << "_" << dims[i];
2095 }
2096 sTreeTypeName.reset(new Name(ostr.str()));
2097 });
2098 return *sTreeTypeName;
2099 }
2100
2101
2102 template<typename RootNodeType>
2103 template<typename OtherRootNodeType>
2104 inline bool
2105 Tree<RootNodeType>::hasSameTopology(const Tree<OtherRootNodeType>& other) const
2106 {
2107 return mRoot.hasSameTopology(other.root());
2108 }
2109
2110
2111 template<typename RootNodeType>
2112 Index64
2113 Tree<RootNodeType>::inactiveVoxelCount() const
2114 {
2115 Coord dim(0, 0, 0);
2116 this->evalActiveVoxelDim(dim);
2117 const Index64
2118 totalVoxels = dim.x() * dim.y() * dim.z(),
2119 activeVoxels = this->activeVoxelCount();
2120 assert(totalVoxels >= activeVoxels);
2121 return totalVoxels - activeVoxels;
2122 }
2123
2124
2125 template<typename RootNodeType>
2126 inline bool
2127 Tree<RootNodeType>::evalLeafBoundingBox(CoordBBox& bbox) const
2128 {
2129 bbox.reset(); // default invalid bbox
2130
2131 if (this->empty()) return false; // empty
2132
2133 mRoot.evalActiveBoundingBox(bbox, false);
2134
2135 return !bbox.empty();
2136 }
2137
2138 template<typename RootNodeType>
2139 inline bool
2140 Tree<RootNodeType>::evalActiveVoxelBoundingBox(CoordBBox& bbox) const
2141 {
2142 bbox.reset(); // default invalid bbox
2143
2144 if (this->empty()) return false; // empty
2145
2146 mRoot.evalActiveBoundingBox(bbox, true);
2147
2148 return !bbox.empty();
2149 }
2150
2151
2152 template<typename RootNodeType>
2153 inline bool
2154 Tree<RootNodeType>::evalActiveVoxelDim(Coord& dim) const
2155 {
2156 CoordBBox bbox;
2157 bool notEmpty = this->evalActiveVoxelBoundingBox(bbox);
2158 dim = bbox.extents();
2159 return notEmpty;
2160 }
2161
2162
2163 template<typename RootNodeType>
2164 inline bool
2165 Tree<RootNodeType>::evalLeafDim(Coord& dim) const
2166 {
2167 CoordBBox bbox;
2168 bool notEmpty = this->evalLeafBoundingBox(bbox);
2169 dim = bbox.extents();
2170 return notEmpty;
2171 }
2172
2173
2174 template<typename RootNodeType>
2175 inline void
2176 Tree<RootNodeType>::evalMinMax(ValueType& minVal, ValueType& maxVal) const
2177 {
2179 minVal = maxVal = zeroVal<ValueType>();
2180 if (ValueOnCIter iter = this->cbeginValueOn()) {
2181 minVal = maxVal = *iter;
2182 for (++iter; iter; ++iter) {
2183 const ValueType& val = *iter;
2184 if (val < minVal) minVal = val;
2185 if (val > maxVal) maxVal = val;
2186 }
2187 }
2188 }
2189
2190
2191 template<typename RootNodeType>
2192 inline void
2193 Tree<RootNodeType>::getNodeLog2Dims(std::vector<Index>& dims)
2194 {
2195 dims.clear();
2196 RootNodeType::getNodeLog2Dims(dims);
2197 }
2198
2199
2200 template<typename RootNodeType>
2201 inline void
2202 Tree<RootNodeType>::print(std::ostream& os, int verboseLevel) const
2203 {
2204 if (verboseLevel <= 0) return;
2205
2207 struct OnExit {
2208 std::ostream& os;
2209 std::streamsize savedPrecision;
2210 OnExit(std::ostream& _os): os(_os), savedPrecision(os.precision()) {}
2211 ~OnExit() { os.precision(savedPrecision); }
2212 };
2213 OnExit restorePrecision(os);
2214
2215 std::vector<Index> dims;
2216 Tree::getNodeLog2Dims(dims);// leaf is the last element
2217
2218 os << "Information about Tree:\n"
2219 << " Type: " << this->type() << "\n";
2220
2221 os << " Configuration:\n";
2222
2223 if (verboseLevel <= 1) {
2224 // Print node types and sizes.
2225 os << " Root(" << mRoot.getTableSize() << ")";
2226 if (dims.size() > 1) {
2227 for (size_t i = 1, N = dims.size() - 1; i < N; ++i) {
2228 os << ", Internal(" << (1 << dims[i]) << "^3)";
2229 }
2230 os << ", Leaf(" << (1 << dims.back()) << "^3)\n";
2231 }
2232 os << " Background value: " << mRoot.background() << "\n";
2233 return;
2234 }
2235
2236 // The following is tree information that is expensive to extract.
2237
2238 ValueType minVal = zeroVal<ValueType>(), maxVal = zeroVal<ValueType>();
2239 if (verboseLevel > 3) {
2240 // This forces loading of all non-resident nodes.
2241 this->evalMinMax(minVal, maxVal);
2242 }
2243
2244 #if OPENVDB_ABI_VERSION_NUMBER >= 7
2245 const auto nodeCount = this->nodeCount();//fast
2246 const Index32 leafCount = nodeCount.front();// leaf is the first element
2247 #else
2248 std::vector<Index64> nodeCount(dims.size());
2249 for (NodeCIter it = cbeginNode(); it; ++it) ++(nodeCount[it.getDepth()]);//slow
2250 const Index64 leafCount = *nodeCount.rbegin();// leaf is the last element
2251 #endif
2252 assert(dims.size() == nodeCount.size());
2253
2254 Index64 totalNodeCount = 0;
2255 for (size_t i = 0; i < nodeCount.size(); ++i) totalNodeCount += nodeCount[i];
2256
2257 // Print node types, counts and sizes.
2258 os << " Root(1 x " << mRoot.getTableSize() << ")";
2259 if (dims.size() >= 2) {
2260 for (size_t i = 1, N = dims.size() - 1; i < N; ++i) {
2261 #if OPENVDB_ABI_VERSION_NUMBER >= 7
2262 os << ", Internal(" << util::formattedInt(nodeCount[N - i]);
2263 #else
2264 os << ", Internal(" << util::formattedInt(nodeCount[i]);
2265 #endif
2266 os << " x " << (1 << dims[i]) << "^3)";
2267 }
2268 os << ", Leaf(" << util::formattedInt(leafCount);
2269 os << " x " << (1 << dims.back()) << "^3)\n";
2270 }
2271 os << " Background value: " << mRoot.background() << "\n";
2272
2273 // Statistics of topology and values
2274
2275 if (verboseLevel > 3) {
2276 os << " Min value: " << minVal << "\n";
2277 os << " Max value: " << maxVal << "\n";
2278 }
2279
2280 const Index64
2281 numActiveVoxels = this->activeVoxelCount(),
2282 numActiveLeafVoxels = this->activeLeafVoxelCount(),
2283 numActiveTiles = this->activeTileCount();
2284
2285 os << " Number of active voxels: " << util::formattedInt(numActiveVoxels) << "\n";
2286 os << " Number of active tiles: " << util::formattedInt(numActiveTiles) << "\n";
2287
2288 Coord dim(0, 0, 0);
2289 Index64 totalVoxels = 0;
2290 if (numActiveVoxels) { // nonempty
2291 CoordBBox bbox;
2292 this->evalActiveVoxelBoundingBox(bbox);
2293 dim = bbox.extents();
2294 totalVoxels = dim.x() * uint64_t(dim.y()) * dim.z();
2295
2296 os << " Bounding box of active voxels: " << bbox << "\n";
2297 os << " Dimensions of active voxels: "
2298 << dim[0] << " x " << dim[1] << " x " << dim[2] << "\n";
2299
2300 const double activeRatio = (100.0 * double(numActiveVoxels)) / double(totalVoxels);
2301 os << " Percentage of active voxels: " << std::setprecision(3) << activeRatio << "%\n";
2302
2303 if (leafCount > 0) {
2304 const double fillRatio = (100.0 * double(numActiveLeafVoxels))
2305 / (double(leafCount) * double(LeafNodeType::NUM_VOXELS));
2306 os << " Average leaf node fill ratio: " << fillRatio << "%\n";
2307 }
2308
2309 if (verboseLevel > 2) {
2310 Index64 sum = 0;// count the number of unallocated leaf nodes
2311 for (auto it = this->cbeginLeaf(); it; ++it) if (!it->isAllocated()) ++sum;
2312 os << " Number of unallocated nodes: "
2313 << util::formattedInt(sum) << " ("
2314 << (100.0 * double(sum) / double(totalNodeCount)) << "%)\n";
2315 }
2316 } else {
2317 os << " Tree is empty!\n";
2318 }
2319 os << std::flush;
2320
2321 if (verboseLevel == 2) return;
2322
2323 // Memory footprint in bytes
2324 const Index64
2325 actualMem = this->memUsage(),
2326 denseMem = sizeof(ValueType) * totalVoxels,
2327 voxelsMem = sizeof(ValueType) * numActiveLeafVoxels;
2329
2330 os << "Memory footprint:\n";
2331 util::printBytes(os, actualMem, " Actual: ");
2332 util::printBytes(os, voxelsMem, " Active leaf voxels: ");
2333
2334 if (numActiveVoxels) {
2335 util::printBytes(os, denseMem, " Dense equivalent: ");
2336 os << " Actual footprint is " << (100.0 * double(actualMem) / double(denseMem))
2337 << "% of an equivalent dense volume\n";
2338 os << " Leaf voxel footprint is " << (100.0 * double(voxelsMem) / double(actualMem))
2339 << "% of actual footprint\n";
2340 }
2341 }
2342
2343 } // namespace tree
2344 } // namespace OPENVDB_VERSION_NAME
2345 } // namespace openvdb
2346
2347 #endif // OPENVDB_TREE_TREE_HAS_BEEN_INCLUDED
openvdb::v7_1::tree::TreeIterTraits< TreeT, typename TreeT::RootNodeType::ChildOffCIter >::begin
static TreeT::RootNodeType::ChildOffCIter begin(const TreeT &tree)
Definition: Tree.h:1317
openvdb::v7_1::tree::TreeIterTraits< TreeT, typename TreeT::LeafIter >::begin
static TreeT::LeafIter begin(TreeT &tree)
Definition: Tree.h:1343
openvdb::v7_1::tree::Tree::getNodes
void getNodes(ArrayT &array) const
Definition: Tree.h:578
openvdb::v7_1::tree::Tree::cbeginNode
NodeCIter cbeginNode() const
Definition: Tree.h:1146
openvdb::v7_1::tree::Tree::leafCount
Index32 leafCount() const override
Return the number of leaf nodes.
Definition: Tree.h:337
openvdb::v7_1::tree::Tree::writeBuffers
void writeBuffers(std::ostream &, bool saveFloatAsHalf=false) const override
Write out all data buffers for this tree.
Definition: Tree.h:1446
RootNode.h
The root node of an OpenVDB tree.
openvdb::v7_1::tree::TreeIterTraits< TreeT, typename TreeT::ValueOnCIter >::begin
static TreeT::ValueOnCIter begin(const TreeT &tree)
Definition: Tree.h:1355
openvdb::v7_1::tree::TreeBase::inactiveVoxelCount
virtual Index64 inactiveVoxelCount() const =0
Return the number of inactive voxels within the bounding box of all active voxels.
openvdb::v7_1::tree::Tree::beginValueOn
ValueOnCIter beginValueOn() const
Definition: Tree.h:1172
openvdb::v7_1::tree::TreeBase::writeTopology
virtual void writeTopology(std::ostream &, bool saveFloatAsHalf=false) const
Write the tree topology to a stream.
Definition: Tree.h:1268
openvdb::v7_1::tree::TreeBase::Ptr
SharedPtr< TreeBase > Ptr
Definition: Tree.h:38
Metadata.h
openvdb::v7_1::tree::Tree::DeallocateNodes
Definition: Tree.h:1200
openvdb::v7_1::math::CoordBBox::extents
Coord extents() const
Definition: Coord.h:382
openvdb::v7_1::TypedMetadata
Templated metadata class to hold specific types.
Definition: Metadata.h:122
openvdb::v7_1::util::formattedInt
FormattedInt< IntT > formattedInt(IntT n)
Definition: Formats.h:118
openvdb::v7_1::tree::Tree::~Tree
~Tree() override
Definition: Tree.h:260
openvdb::v7_1::tree::Tree::addTile
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:1673
openvdb::v7_1::tree::TreeBase::TreeBase
TreeBase()=default
openvdb::v7_1::tree::TreeBase::getBackgroundValue
virtual Metadata::Ptr getBackgroundValue() const
Return this tree's background value wrapped as metadata.
Definition: Tree.h:60
openvdb::v7_1::tree::Tree::getValue
const ValueType & getValue(const Coord &xyz) const
Return the value of the voxel at the given coordinates.
Definition: Tree.h:1556
Types.h
openvdb::v7_1::tree::TreeIterTraits< TreeT, typename TreeT::NodeIter >::begin
static TreeT::NodeIter begin(TreeT &tree)
Definition: Tree.h:1335
openvdb::v7_1::tree::Tree::operator=
Tree & operator=(const Tree &)=delete
openvdb::v7_1::tree::Tree::Tree
Tree(const ValueType &background)
Empty tree constructor.
Definition: Tree.h:258
openvdb::v7_1::tree::TreeBase::unallocatedLeafCount
virtual Index32 unallocatedLeafCount() const =0
Return the total number of unallocated leaf nodes residing in this tree.
openvdb::v7_1::tree::Tree3
Tree3<T, N1, N2>::Type is the type of a three-level tree (Root, Internal, Leaf) with value type T and...
Definition: Tree.h:1230
openvdb::v7_1::tree::Tree::getValueDepth
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:1573
openvdb::v7_1::tree::TreeBase::ConstPtr
SharedPtr< const TreeBase > ConstPtr
Definition: Tree.h:39
openvdb::v7_1::tree::TreeBase::readBuffers
virtual void readBuffers(std::istream &, const CoordBBox &, bool saveFloatAsHalf=false)=0
Read all of this tree's data buffers that intersect the given bounding box.
openvdb::v7_1::tree::Tree::valueType
Name valueType() const override
Return the name of the type of a voxel's value (e.g., "float" or "vec3d")
Definition: Tree.h:266
openvdb::v7_1::tree::TreeIterTraits< TreeT, typename TreeT::RootNodeType::ChildOnCIter >::begin
static TreeT::RootNodeType::ChildOnCIter begin(const TreeT &tree)
Definition: Tree.h:1305
openvdb::v7_1::tree::Tree::probeLeaf
LeafNodeType * probeLeaf(const Coord &xyz)
Return a pointer to the leaf node that contains voxel (x, y, z). If no such node exists,...
Definition: Tree.h:1700
openvdb::v7_1::tree::TreeIterTraits
TreeIterTraits provides, for all tree iterators, a begin(tree) function that returns an iterator over...
Definition: Tree.h:1296
openvdb::v7_1::tree::CombineOpAdapter::CombineOpAdapter
CombineOpAdapter(CombineOp &_op)
Definition: Tree.h:1876
openvdb::v7_1::tree::Tree::Tree
Tree(const Tree &other)
Deep copy constructor.
Definition: Tree.h:204
openvdb::v7_1::tree::TreeBase::clipUnallocatedNodes
virtual void clipUnallocatedNodes()=0
Replace with background tiles any nodes whose voxel buffers have not yet been allocated.
openvdb::v7_1::TopologyCopy
Tag dispatch class that distinguishes topology copy constructors from deep copy constructors.
Definition: Types.h:1045
openvdb::v7_1::Index
Index32 Index
Definition: Types.h:31
openvdb::v7_1::tree::TreeBase::type
virtual const Name & type() const =0
Return the name of this tree's type.
openvdb::v7_1::tree::TreeBase::readTopology
virtual void readTopology(std::istream &, bool saveFloatAsHalf=false)
Read the tree topology from a stream.
Definition: Tree.h:1259
openvdb::v7_1::tree::Tree::mConstAccessorRegistry
ConstAccessorRegistry mConstAccessorRegistry
Definition: Tree.h:1216
openvdb::v7_1::tree::TreeBase::operator=
TreeBase & operator=(const TreeBase &)=delete
openvdb::v7_1::math::CoordBBox::reset
void reset()
Definition: Coord.h:327
openvdb::v7_1::tree::TreeBase::memUsage
virtual Index64 memUsage() const
Return the total amount of memory in bytes occupied by this tree.
Definition: Tree.h:131
openvdb::v7_1::Name
std::string Name
Definition: Name.h:17
openvdb::v7_1::tree::Tree::memUsage
Index64 memUsage() const override
Return the total amount of memory in bytes occupied by this tree.
Definition: Tree.h:365
openvdb::v7_1::tree::TreeBase::print
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:1276
openvdb::v7_1::tree::Tree::DeallocateNodes::DeallocateNodes
DeallocateNodes(std::vector< NodeType * > &nodes)
Definition: Tree.h:1201
openvdb::v7_1::tree::Tree::DeallocateNodes::operator()
void operator()(const tbb::blocked_range< size_t > &range) const
Definition: Tree.h:1203
openvdb::v7_1::tree::Tree::beginRootTiles
RootNodeType::ChildOffIter beginRootTiles()
Definition: Tree.h:1119
openvdb::v7_1::tree::TreeIterTraits< TreeT, typename TreeT::ValueOffCIter >::begin
static TreeT::ValueOffCIter begin(const TreeT &tree)
Definition: Tree.h:1363
openvdb::v7_1::tools::composite::min
const std::enable_if<!VecTraits< T >::IsVec, T >::type & min(const T &a, const T &b)
Definition: Composite.h:102
openvdb::v7_1::tree::TreeBase::writeBuffers
virtual void writeBuffers(std::ostream &, bool saveFloatAsHalf=false) const =0
Write out all the data buffers for this tree.
openvdb::v7_1::math::CoordBBox::empty
bool empty() const
Return true if this bounding box is empty (i.e., encloses no coordinates).
Definition: Coord.h:356
openvdb::v7_1::tree::TreeBase::evalLeafBoundingBox
virtual bool evalLeafBoundingBox(CoordBBox &bbox) const =0
Return in bbox the axis-aligned bounding box of all active tiles and leaf nodes with active values.
openvdb::v7_1::CombineArgs::a
const AValueType & a() const
Get the A input value.
Definition: Types.h:971
openvdb::v7_1::tree::TreeBase::evalLeafDim
virtual bool evalLeafDim(Coord &dim) const =0
Return in dim the dimensions of the axis-aligned bounding box of all leaf nodes.
openvdb::v7_1::tree::TreeBase::evalActiveVoxelBoundingBox
virtual bool evalActiveVoxelBoundingBox(CoordBBox &bbox) const =0
Return in bbox the axis-aligned bounding box of all active voxels and tiles.
openvdb::v7_1::MERGE_ACTIVE_STATES_AND_NODES
@ MERGE_ACTIVE_STATES_AND_NODES
Definition: Types.h:872
openvdb::v7_1::math::Coord::y
Int32 y() const
Definition: Coord.h:132
openvdb::v7_1::tree::Tree::attachAccessor
void attachAccessor(ValueAccessorBase< const Tree, false > &) const
Definition: Tree.h:636
openvdb::v7_1::Metadata::createMetadata
static Metadata::Ptr createMetadata(const Name &typeName)
Create new metadata of the given type.
openvdb::v7_1::tree::TreeBase::activeTileCount
virtual Index64 activeTileCount() const =0
Return the total number of active tiles.
openvdb::v7_1::tree::Tree::beginRootChildren
RootNodeType::ChildOnIter beginRootChildren()
Definition: Tree.h:1112
openvdb::v7_1::tree::TreeBase::activeLeafVoxelCount
virtual Index64 activeLeafVoxelCount() const =0
Return the number of active voxels stored in leaf nodes.
openvdb::v7_1::tree::Tree::setValueOff
void setValueOff(const Coord &xyz)
Mark the voxel at the given coordinates as inactive but don't change its value.
Definition: Tree.h:1581
openvdb::v7_1::tree::Tree::ValueConverter
ValueConverter<T>::Type is the type of a tree having the same hierarchy as this tree but a different ...
Definition: Tree.h:194
openvdb::v7_1::tree::TreeBase::getIndexRange
virtual void getIndexRange(CoordBBox &bbox) const =0
openvdb::v7_1::Index64
uint64_t Index64
Definition: Types.h:30
openvdb::v7_1::tree::Tree::fill
void fill(const CoordBBox &bbox, const ValueType &value, bool active=true)
Definition: Tree.h:476
openvdb::v7_1::tree::TreeIterTraits< TreeT, typename TreeT::NodeCIter >::begin
static TreeT::NodeCIter begin(const TreeT &tree)
Definition: Tree.h:1339
openvdb::v7_1::math::Coord::x
Int32 x() const
Definition: Coord.h:131
openvdb::v7_1::tree::Tree::cbeginValueOn
ValueOnCIter cbeginValueOn() const
Definition: Tree.h:1173
Platform.h
openvdb::v7_1::tree::Tree4
Tree4<T, N1, N2, N3>::Type is the type of a four-level tree (Root, Internal, Internal,...
Definition: Tree.h:1240
openvdb::v7_1::tree::TreeBase::treeDepth
virtual Index treeDepth() const =0
Return the depth of this tree.
openvdb::v7_1::tree::Tree::beginValueAll
ValueAllCIter beginValueAll() const
Definition: Tree.h:1166
openvdb::v7_1::tree::LeafIteratorBase
Base class for tree-traversal iterators over all leaf nodes (but not leaf voxels)
Definition: TreeIterator.h:1187
ValueAccessor.h
openvdb::v7_1::tree::Tree::operator==
bool operator==(const Tree &) const
Definition: Tree.h:273
openvdb::v7_1::MERGE_ACTIVE_STATES
@ MERGE_ACTIVE_STATES
Definition: Types.h:870
Formats.h
Utility routines to output nicely-formatted numeric values.
openvdb::v7_1::tree::Tree::probeLeaf
const LeafNodeType * probeLeaf(const Coord &xyz) const
Definition: Tree.h:551
openvdb::v7_1::tree::Tree
Definition: Tree.h:175
openvdb::v7_1::tree::TreeIterTraits< TreeT, typename TreeT::RootNodeType::ChildOffIter >::begin
static TreeT::RootNodeType::ChildOffIter begin(TreeT &tree)
Definition: Tree.h:1311
openvdb::v7_1::math::CoordBBox
Axis-aligned bounding box of signed integer coordinates.
Definition: Coord.h:249
OPENVDB_API
#define OPENVDB_API
Helper macros for defining library symbol visibility.
Definition: Platform.h:230
openvdb::v7_1::tree::Tree::isValueOn
bool isValueOn(const Coord &xyz) const
Return true if the value at the given coordinates is active.
Definition: Tree.h:448
openvdb::v7_1::tree::Tree::copy
TreeBase::Ptr copy() const override
Return a pointer to a deep copy of this tree.
Definition: Tree.h:263
openvdb::v7_1::Index32
uint32_t Index32
Definition: Types.h:29
openvdb::v7_1::tree::Tree::LeafNodeType
typename RootNodeType::LeafNodeType LeafNodeType
Definition: Tree.h:183
openvdb::v7_1::CombineArgs::b
const BValueType & b() const
Get the B input value.
Definition: Types.h:973
openvdb::v7_1::tree::TreeBase::readBuffers
virtual void readBuffers(std::istream &, bool saveFloatAsHalf=false)=0
Read all data buffers for this tree.
openvdb::v7_1::tree::TreeIterTraits< TreeT, typename TreeT::ValueAllCIter >::begin
static TreeT::ValueAllCIter begin(const TreeT &tree)
Definition: Tree.h:1371
openvdb::v7_1::tree::Tree::root
const RootNodeType & root() const
Definition: Tree.h:279
openvdb::v7_1::tree::TreeIterTraits< TreeT, typename TreeT::ValueOnIter >::begin
static TreeT::ValueOnIter begin(TreeT &tree)
Definition: Tree.h:1351
Math.h
General-purpose arithmetic and comparison routines, most of which accept arbitrary value types (or at...
openvdb_houdini::combine
ValueType combine(const ValueType &v0, const ValueType &v1, const ValueType &v2, const openvdb::Vec3d &w)
Combine different value types.
Definition: AttributeTransferUtil.h:140
openvdb::v7_1::tree::NodeIteratorBase
Base class for tree-traversal iterators over all nodes.
Definition: TreeIterator.h:936
openvdb::v7_1::tools::clip
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
openvdb::v7_1::tree::Tree::type
const Name & type() const override
Return the name of this type of tree.
Definition: Tree.h:271
openvdb::v7_1::tree::Tree::beginValueOff
ValueOffCIter beginValueOff() const
Definition: Tree.h:1178
openvdb::v7_1::tree::Tree::activeVoxelCount
Index64 activeVoxelCount() const override
Return the total number of active voxels.
Definition: Tree.h:356
openvdb::v7_1::tree::Tree::getIndexRange
void getIndexRange(CoordBBox &bbox) const override
Min and max are both inclusive.
Definition: Tree.h:663
openvdb::v7_1::tree::Tree::cbeginRootChildren
RootNodeType::ChildOnCIter cbeginRootChildren() const
Definition: Tree.h:1111
openvdb::v7_1::Metadata::Ptr
SharedPtr< Metadata > Ptr
Definition: Metadata.h:26
openvdb::v7_1::NotImplementedError
Definition: Exceptions.h:61
openvdb::v7_1::MERGE_NODES
@ MERGE_NODES
Definition: Types.h:871
openvdb::v7_1::tree::Tree::addLeaf
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:516
openvdb::v7_1::tree::Tree::activeTileCount
Index64 activeTileCount() const override
Return the total number of active tiles.
Definition: Tree.h:360
openvdb::v7_1::tree::Tree::probeValue
bool probeValue(const Coord &xyz, ValueType &value) const
Get the value of the voxel at the given coordinates.
Definition: Tree.h:1662
openvdb::v7_1::tree::TreeBase::inactiveLeafVoxelCount
virtual Index64 inactiveLeafVoxelCount() const =0
Return the number of inactive voxels stored in leaf nodes.
openvdb::v7_1::tree::TreeBase::copy
virtual TreeBase::Ptr copy() const =0
Return a pointer to a deep copy of this tree.
openvdb::v7_1::tree::TreeBase::evalActiveVoxelDim
virtual bool evalActiveVoxelDim(Coord &dim) const =0
Return in dim the dimensions of the axis-aligned bounding box of all active voxels....
openvdb::v7_1::tree::Tree::hasActiveTiles
bool hasActiveTiles() const
Return true if this tree has any active tiles.
Definition: Tree.h:452
InternalNode.h
Internal table nodes for OpenVDB trees.
openvdb::v7_1::tree::Tree::touchLeaf
LeafNodeType * touchLeaf(const Coord &xyz)
Return a pointer to the leaf node that contains voxel (x, y, z). If no such node exists,...
Definition: Tree.h:1692
openvdb::v7_1::tree::Tree::treeDepth
Index treeDepth() const override
Return the depth of this tree.
Definition: Tree.h:335
openvdb::v7_1::tree::Tree::releaseAccessor
void releaseAccessor(ValueAccessorBase< const Tree, false > &) const
Definition: Tree.h:648
openvdb::v7_1::tree::Tree::beginNode
NodeCIter beginNode() const
Definition: Tree.h:1145
openvdb::v7_1::tree::Tree::probeConstLeaf
const LeafNodeType * probeConstLeaf(const Coord &xyz) const
Definition: Tree.h:1708
openvdb::v7_1::tree::Tree::cbeginRootDense
RootNodeType::ChildAllCIter cbeginRootDense() const
Definition: Tree.h:1125
openvdb::v7_1::tree::TreeIterTraits< TreeT, typename TreeT::ValueAllIter >::begin
static TreeT::ValueAllIter begin(TreeT &tree)
Definition: Tree.h:1367
openvdb::v7_1::tree::Tree::cbeginValueOff
ValueOffCIter cbeginValueOff() const
Definition: Tree.h:1179
openvdb::v7_1::math::Coord
Signed (x, y, z) 32-bit integer coordinates.
Definition: Coord.h:26
openvdb::v7_1::math::Coord::z
Int32 z() const
Definition: Coord.h:133
openvdb::v7_1::tree::Tree::sTreeTypeName
static std::unique_ptr< const Name > sTreeTypeName
Definition: Tree.h:1218
openvdb::v7_1::tree::CombineOpAdapter::op
CombineOp & op
Definition: Tree.h:1882
openvdb::v7_1::tree::TreeBase::leafCount
virtual Index32 leafCount() const =0
Return the number of leaf nodes.
openvdb::v7_1::tree::Tree::cbeginRootTiles
RootNodeType::ChildOffCIter cbeginRootTiles() const
Definition: Tree.h:1118
openvdb::v7_1::tree::Tree::background
const ValueType & background() const
Return this tree's background value.
Definition: Tree.h:660
openvdb::v7_1::tree::Tree::stealNodes
void stealNodes(ArrayT &array)
Steals all nodes of a certain type from the tree and adds them to a container with the following API:
Definition: Tree.h:605
openvdb::v7_1::tree::Tree::ConstAccessorRegistry
tbb::concurrent_hash_map< ValueAccessorBase< const Tree, true > *, bool > ConstAccessorRegistry
Definition: Tree.h:1192
openvdb::v7_1::tree::Tree::getValue
const ValueType & getValue(const Coord &xyz, AccessT &) const
Return the value of the voxel at the given coordinates and update the given accessor's node cache.
openvdb::v7_1::tree::TreeBase::activeVoxelCount
virtual Index64 activeVoxelCount() const =0
Return the total number of active voxels.
openvdb::v7_1::tree::TreeBase::TreeBase
TreeBase(const TreeBase &)=default
openvdb::v7_1::tree::TreeValueIteratorBase
Base class for tree-traversal iterators over tile and voxel values.
Definition: TreeIterator.h:617
openvdb::v7_1::Metadata::isRegisteredType
static bool isRegisteredType(const Name &typeName)
Return true if the given type is known by the metadata type registry.
OPENVDB_USE_VERSION_NAMESPACE
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h:146
openvdb::v7_1::tree::TreeIterTraits< TreeT, typename TreeT::LeafCIter >::begin
static TreeT::LeafCIter begin(const TreeT &tree)
Definition: Tree.h:1347
openvdb::v7_1::CombineArgs
This struct collects both input and output arguments to "grid combiner" functors used with the tree::...
Definition: Types.h:932
openvdb::v7_1::tree::Tree::inactiveLeafVoxelCount
Index64 inactiveLeafVoxelCount() const override
Return the number of inactive voxels stored in leaf nodes.
Definition: Tree.h:354
openvdb::v7_1::tree::TreeIterTraits< TreeT, typename TreeT::RootNodeType::ChildOnIter >::begin
static TreeT::RootNodeType::ChildOnIter begin(TreeT &tree)
Definition: Tree.h:1299
openvdb::v7_1::tree::Tree5
Tree5<T, N1, N2, N3, N4>::Type is the type of a five-level tree (Root, Internal, Internal,...
Definition: Tree.h:1249
openvdb::v7_1::MergePolicy
MergePolicy
Definition: Types.h:869
openvdb::v7_1::tree::TreeBase::valueType
virtual Name valueType() const =0
Return the name of the type of a voxel's value (e.g., "float" or "vec3d").
openvdb::v7_1::tree::Tree::Tree
Tree(const Tree< OtherRootType > &other)
Value conversion deep copy constructor.
Definition: Tree.h:215
openvdb::v7_1::tree::Tree::modifyValue
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:1645
openvdb::v7_1::tree::Tree::Tree
Tree(const OtherTreeType &other, const ValueType &background, TopologyCopy)
Topology copy constructor from a tree of a different type.
Definition: Tree.h:251
openvdb::v7_1::tree::Tree::cbeginLeaf
LeafCIter cbeginLeaf() const
Definition: Tree.h:1153
openvdb::v7_1::tree::Tree::stealNodes
void stealNodes(ArrayT &array, const ValueType &value, bool state)
Definition: Tree.h:607
openvdb::v7_1::tree::Tree::empty
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:618
openvdb::v7_1::tree::Tree::setValueOn
void setValueOn(const Coord &xyz)
Mark the voxel at the given coordinates as active but don't change its value.
Definition: Tree.h:1628
openvdb::v7_1::tools::prune
void prune(TreeT &tree, typename TreeT::ValueType tolerance=zeroVal< typename TreeT::ValueType >(), bool threaded=true, size_t grainSize=1)
Reduce the memory footprint of a tree by replacing with tiles any nodes whose values are all the same...
Definition: Prune.h:334
openvdb::v7_1::tree::Tree::operator!=
bool operator!=(const Tree &) const
Definition: Tree.h:274
openvdb::v7_1::tree::Tree::root
RootNodeType & root()
Return this tree's root node.
Definition: Tree.h:278
logging.h
openvdb::v7_1::tree::Tree::isValueOff
bool isValueOff(const Coord &xyz) const
Return true if the value at the given coordinates is inactive.
Definition: Tree.h:450
openvdb::v7_1::tree::ValueAccessorBase
This base class for ValueAccessors manages registration of an accessor with a tree so that the tree c...
Definition: ValueAccessor.h:85
openvdb::v7_1::tree::Tree::cbegin
CIterT cbegin() const
Return a const iterator of type CIterT (for example, cbegin<ValueOnCIter>() is equivalent to cbeginVa...
openvdb::v7_1::tree::CombineOpAdapter::operator()
void operator()(CombineArgs< AValueT, BValueT > &args) const
Definition: Tree.h:1878
openvdb::v7_1::tree::Tree::beginLeaf
LeafCIter beginLeaf() const
Definition: Tree.h:1152
openvdb::v7_1::tree::TreeBase::~TreeBase
virtual ~TreeBase()=default
OPENVDB_VERSION_NAME
#define OPENVDB_VERSION_NAME
The version namespace name for this library version.
Definition: version.h:94
openvdb::v7_1::tree::TreeIterTraits< TreeT, typename TreeT::RootNodeType::ChildAllIter >::begin
static TreeT::RootNodeType::ChildAllIter begin(TreeT &tree)
Definition: Tree.h:1323
openvdb::v7_1::tree::Tree::DeallocateNodes::mNodes
NodeType **const mNodes
Definition: Tree.h:1208
openvdb::v7_1::tree::TreeIterTraits< TreeT, typename TreeT::RootNodeType::ChildAllCIter >::begin
static TreeT::RootNodeType::ChildAllCIter begin(const TreeT &tree)
Definition: Tree.h:1329
openvdb::v7_1::tree::Tree::visitActiveBBox
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:972
openvdb::v7_1::tree::Tree::AccessorRegistry
tbb::concurrent_hash_map< ValueAccessorBase< Tree, true > *, bool > AccessorRegistry
Definition: Tree.h:1191
openvdb::v7_1::tree::Tree::ValueType
typename RootNodeType::ValueType ValueType
Definition: Tree.h:181
openvdb::v7_1::util::printBytes
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)
BBox.h
openvdb::v7_1::tree::Tree::RootNodeType
_RootNodeType RootNodeType
Definition: Tree.h:180
openvdb::v7_1::tree::Tree::mAccessorRegistry
AccessorRegistry mAccessorRegistry
Definition: Tree.h:1215
openvdb::v7_1::tree::CombineOpAdapter
Helper class to adapt a three-argument (a, b, result) CombineOp functor into a single-argument functo...
Definition: Tree.h:1875
openvdb::v7_1::tree::Tree::hasSameTopology
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:2105
openvdb::v7_1::tree::Tree::clear
void clear()
Remove all tiles from this tree and all nodes other than the root node.
Definition: Tree.h:1454
openvdb::v7_1::tree::TreeIterTraits< TreeT, typename TreeT::ValueOffIter >::begin
static TreeT::ValueOffIter begin(TreeT &tree)
Definition: Tree.h:1359
openvdb::v7_1::tree::Tree::beginRootDense
RootNodeType::ChildAllIter beginRootDense()
Definition: Tree.h:1126
openvdb::v7_1::tree::Tree::activeLeafVoxelCount
Index64 activeLeafVoxelCount() const override
Return the number of active voxels stored in leaf nodes.
Definition: Tree.h:352
openvdb
Definition: Exceptions.h:13
openvdb::v7_1::tree::Tree::Tree
Tree(const OtherTreeType &other, const ValueType &inactiveValue, const ValueType &activeValue, TopologyCopy)
Topology copy constructor from a tree of a different type.
Definition: Tree.h:230
LeafNode.h
openvdb::v7_1::tree::TreeBase::readNonresidentBuffers
virtual void readNonresidentBuffers() const =0
Read all of this tree's data buffers that are not yet resident in memory (because delayed loading is ...
openvdb::v7_1::tree::Tree::mRoot
RootNodeType mRoot
Definition: Tree.h:1214
openvdb::v7_1::tree::Tree::Tree
Tree()
Definition: Tree.h:199
openvdb::v7_1::tree::Tree::modifyValueAndActiveState
void modifyValueAndActiveState(const Coord &xyz, const ModifyOp &op)
Apply a functor to the voxel at the given coordinates.
Definition: Tree.h:1654
openvdb::v7_1::tree::Tree::BuildType
typename RootNodeType::BuildType BuildType
Definition: Tree.h:182
openvdb::v7_1::tree::Tree::prune
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:505
openvdb::v7_1::SharedPtr
std::shared_ptr< T > SharedPtr
Definition: Types.h:91
openvdb::v7_1::tree::Tree::setValue
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:1605
openvdb::v7_1::tree::Tree::setActiveState
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:1597
openvdb::v7_1::tree::Tree::setValueOnly
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:1612
openvdb::v7_1::CombineArgs::result
const AValueType & result() const
Get the output value.
Definition: Types.h:976
TreeIterator.h
OPENVDB_THROW
#define OPENVDB_THROW(exception, message)
Definition: Exceptions.h:82
openvdb::v7_1::tree::Tree::clearAllAccessors
void clearAllAccessors()
Clear all registered accessors.
Definition: Tree.h:1513
openvdb::v7_1::tools::composite::max
const std::enable_if<!VecTraits< T >::IsVec, T >::type & max(const T &a, const T &b)
Definition: Composite.h:106
openvdb::v7_1::tree::Tree::cbeginValueAll
ValueAllCIter cbeginValueAll() const
Definition: Tree.h:1167
openvdb::v7_1::tree::TreeBase
Base class for typed trees.
Definition: Tree.h:36
OPENVDB_LOG_WARN
#define OPENVDB_LOG_WARN(message)
Log a warning message of the form 'someVar << "some text" << ...'.
Definition: logging.h:253