From df8f9c8b21ba45e8434c1b8032018314abf69182 Mon Sep 17 00:00:00 2001 From: Dan Bailey Date: Wed, 6 Mar 2024 22:31:45 -0800 Subject: [PATCH] Adjust existing grid API to move ValueAccessor methods to the tree Signed-off-by: Dan Bailey --- openvdb/openvdb/Grid.h | 18 +++---- openvdb/openvdb/tree/Tree.h | 65 +++++++++++++++++++++++++ openvdb/openvdb/unittest/TestGrid.cc | 5 ++ pendingchanges/value_accessors_tree.txt | 3 ++ 4 files changed, 82 insertions(+), 9 deletions(-) create mode 100644 pendingchanges/value_accessors_tree.txt diff --git a/openvdb/openvdb/Grid.h b/openvdb/openvdb/Grid.h index 14aad39122..bd123d2c0f 100644 --- a/openvdb/openvdb/Grid.h +++ b/openvdb/openvdb/Grid.h @@ -586,10 +586,10 @@ class Grid: public GridBase using ValueAllIter = typename _TreeType::ValueAllIter; using ValueAllCIter = typename _TreeType::ValueAllCIter; - using Accessor = typename tree::ValueAccessor<_TreeType, true>; - using ConstAccessor = typename tree::ValueAccessor; - using UnsafeAccessor = typename tree::ValueAccessor<_TreeType, false>; - using ConstUnsafeAccessor = typename tree::ValueAccessor; + using Accessor = typename _TreeType::Accessor; + using ConstAccessor = typename _TreeType::ConstAccessor; + using UnsafeAccessor = typename _TreeType::UnsafeAccessor; + using ConstUnsafeAccessor = typename _TreeType::ConstUnsafeAccessor; /// @brief ValueConverter::Type is the type of a grid having the same /// hierarchy as this grid but a different value type, T. @@ -729,7 +729,7 @@ class Grid: public GridBase /// @brief Return an accessor that provides random read and write access /// to this grid's voxels. /// @details The accessor is safe in the sense that it is registered with this grid's tree. - Accessor getAccessor() { return Accessor(tree()); } + Accessor getAccessor() { return mTree->getAccessor(); } /// @brief Return an unsafe accessor that provides random read and write access /// to this grid's voxels. /// @details The accessor is unsafe in the sense that it is not registered @@ -737,11 +737,11 @@ class Grid: public GridBase /// over a registered accessor, but it is unsafe if the tree topology is modified. /// @warning Only use this method if you're an expert and know the /// risks of using an unregistered accessor (see tree/ValueAccessor.h) - UnsafeAccessor getUnsafeAccessor() { return UnsafeAccessor(tree()); } + UnsafeAccessor getUnsafeAccessor() { return mTree->getUnsafeAccessor(); } /// Return an accessor that provides random read-only access to this grid's voxels. - ConstAccessor getAccessor() const { return ConstAccessor(tree()); } + ConstAccessor getAccessor() const { return mTree->getConstAccessor(); } /// Return an accessor that provides random read-only access to this grid's voxels. - ConstAccessor getConstAccessor() const { return ConstAccessor(tree()); } + ConstAccessor getConstAccessor() const { return mTree->getConstAccessor(); } /// @brief Return an unsafe accessor that provides random read-only access /// to this grid's voxels. /// @details The accessor is unsafe in the sense that it is not registered @@ -749,7 +749,7 @@ class Grid: public GridBase /// over a registered accessor, but it is unsafe if the tree topology is modified. /// @warning Only use this method if you're an expert and know the /// risks of using an unregistered accessor (see tree/ValueAccessor.h) - ConstUnsafeAccessor getConstUnsafeAccessor() const { return ConstUnsafeAccessor(tree()); } + ConstUnsafeAccessor getConstUnsafeAccessor() const { return mTree->getConstUnsafeAccessor(); } /// Return an iterator over all of this grid's active values (tile and voxel). ValueOnIter beginValueOn() { return tree().beginValueOn(); } diff --git a/openvdb/openvdb/tree/Tree.h b/openvdb/openvdb/tree/Tree.h index 44e77c5307..22ea00a914 100644 --- a/openvdb/openvdb/tree/Tree.h +++ b/openvdb/openvdb/tree/Tree.h @@ -188,6 +188,11 @@ class Tree: public TreeBase static const Index DEPTH = RootNodeType::LEVEL + 1; + using Accessor = ValueAccessor; + using ConstAccessor = ValueAccessor; + using UnsafeAccessor = ValueAccessor; + using ConstUnsafeAccessor = ValueAccessor; + /// @brief ValueConverter::Type is the type of a tree having the same /// hierarchy as this tree but a different value type, T. /// @@ -623,6 +628,31 @@ class Tree: public TreeBase /// Remove all tiles from this tree and all nodes other than the root node. void clear(); + /// @brief Return an accessor that provides random read and write access + /// to this tree's voxels. + /// @details The accessor is safe in the sense that it is registered with this tree. + Accessor getAccessor(); + /// @brief Return an unsafe accessor that provides random read and write access + /// to this tree's voxels. + /// @details The accessor is unsafe in the sense that it is not registered + /// with this tree's tree. In some rare cases this can give a performance advantage + /// over a registered accessor, but it is unsafe if the tree topology is modified. + /// @warning Only use this method if you're an expert and know the + /// risks of using an unregistered accessor (see tree/ValueAccessor.h) + UnsafeAccessor getUnsafeAccessor(); + /// Return an accessor that provides random read-only access to this tree's voxels. + ConstAccessor getAccessor() const; + /// Return an accessor that provides random read-only access to this tree's voxels. + ConstAccessor getConstAccessor() const; + /// @brief Return an unsafe accessor that provides random read-only access + /// to this tree's voxels. + /// @details The accessor is unsafe in the sense that it is not registered + /// with this tree. In some rare cases this can give a performance advantage + /// over a registered accessor, but it is unsafe if the tree topology is modified. + /// @warning Only use this method if you're an expert and know the + /// risks of using an unregistered accessor (see tree/ValueAccessor.h) + ConstUnsafeAccessor getConstUnsafeAccessor(); + /// Clear all registered accessors. void clearAllAccessors(); @@ -1318,6 +1348,41 @@ Tree::clear() //////////////////////////////////////// +template +typename Tree::Accessor +Tree::getAccessor() +{ + return Accessor(*this); +} + +template +typename Tree::UnsafeAccessor +Tree::getUnsafeAccessor() +{ + return UnsafeAccessor(*this); +} + +template +typename Tree::ConstAccessor +Tree::getAccessor() const +{ + return ConstAccessor(*this); +} + +template +typename Tree::ConstAccessor +Tree::getConstAccessor() const +{ + return ConstAccessor(*this); +} + +template +typename Tree::ConstUnsafeAccessor +Tree::getConstUnsafeAccessor() +{ + return ConstUnsafeAccessor(*this); +} + template inline void Tree::attachAccessor(ValueAccessorBase& accessor) const diff --git a/openvdb/openvdb/unittest/TestGrid.cc b/openvdb/openvdb/unittest/TestGrid.cc index 1e9d058b77..9214ece3ef 100644 --- a/openvdb/openvdb/unittest/TestGrid.cc +++ b/openvdb/openvdb/unittest/TestGrid.cc @@ -40,6 +40,11 @@ class ProxyTree: public openvdb::TreeBase using Ptr = openvdb::SharedPtr; using ConstPtr = openvdb::SharedPtr; + using Accessor = void; + using ConstAccessor = void; + using UnsafeAccessor = void; + using ConstUnsafeAccessor = void; + static const openvdb::Index DEPTH; static const ValueType backg; diff --git a/pendingchanges/value_accessors_tree.txt b/pendingchanges/value_accessors_tree.txt new file mode 100644 index 0000000000..8076f7cd6c --- /dev/null +++ b/pendingchanges/value_accessors_tree.txt @@ -0,0 +1,3 @@ +OpenVDB: + Improvements: + - ValueAccessors are now defined and created in the Tree class instead of in the Grid class so that custom Tree implementations may define and create their own ValueAccessors if desired.