From 5862e8db76e2437c478d80b4a68d8899118881e0 Mon Sep 17 00:00:00 2001 From: Dan Bailey Date: Thu, 7 Mar 2024 20:05:52 -0800 Subject: [PATCH 1/2] Update TreeAdapter to work correctly with all const arguments and add extensive unit tests Signed-off-by: Dan Bailey --- openvdb/openvdb/Grid.h | 89 ++++++++++++++++------- openvdb/openvdb/unittest/TestGrid.cc | 102 +++++++++++++++++++++++++++ 2 files changed, 164 insertions(+), 27 deletions(-) diff --git a/openvdb/openvdb/Grid.h b/openvdb/openvdb/Grid.h index 14aad39122..402dd50f58 100644 --- a/openvdb/openvdb/Grid.h +++ b/openvdb/openvdb/Grid.h @@ -1073,14 +1073,14 @@ struct TreeAdapter using ConstAccessorType = typename tree::ValueAccessor; using NonConstAccessorType = typename tree::ValueAccessor; - static TreeType& tree(TreeType& t) { return t; } - static TreeType& tree(GridType& g) { return g.tree(); } - static const TreeType& tree(const TreeType& t) { return t; } - static const TreeType& tree(const GridType& g) { return g.tree(); } - static const TreeType& constTree(TreeType& t) { return t; } - static const TreeType& constTree(GridType& g) { return g.constTree(); } - static const TreeType& constTree(const TreeType& t) { return t; } - static const TreeType& constTree(const GridType& g) { return g.constTree(); } + static NonConstTreeType& tree(NonConstTreeType& t) { return t; } + static NonConstTreeType& tree(NonConstGridType& g) { return g.tree(); } + static const NonConstTreeType& tree(const NonConstTreeType& t) { return t; } + static const NonConstTreeType& tree(const NonConstGridType& g) { return g.tree(); } + static const NonConstTreeType& constTree(NonConstTreeType& t) { return t; } + static const NonConstTreeType& constTree(NonConstGridType& g) { return g.constTree(); } + static const NonConstTreeType& constTree(const NonConstTreeType& t) { return t; } + static const NonConstTreeType& constTree(const NonConstGridType& g) { return g.constTree(); } }; @@ -1103,14 +1103,43 @@ struct TreeAdapter > using ConstAccessorType = typename tree::ValueAccessor; using NonConstAccessorType = typename tree::ValueAccessor; - static TreeType& tree(TreeType& t) { return t; } - static TreeType& tree(GridType& g) { return g.tree(); } - static const TreeType& tree(const TreeType& t) { return t; } - static const TreeType& tree(const GridType& g) { return g.tree(); } - static const TreeType& constTree(TreeType& t) { return t; } - static const TreeType& constTree(GridType& g) { return g.constTree(); } - static const TreeType& constTree(const TreeType& t) { return t; } - static const TreeType& constTree(const GridType& g) { return g.constTree(); } + static NonConstTreeType& tree(NonConstTreeType& t) { return t; } + static NonConstTreeType& tree(NonConstGridType& g) { return g.tree(); } + static const NonConstTreeType& tree(const NonConstTreeType& t) { return t; } + static const NonConstTreeType& tree(const NonConstGridType& g) { return g.tree(); } + static const NonConstTreeType& constTree(NonConstTreeType& t) { return t; } + static const NonConstTreeType& constTree(NonConstGridType& g) { return g.constTree(); } + static const NonConstTreeType& constTree(const NonConstTreeType& t) { return t; } + static const NonConstTreeType& constTree(const NonConstGridType& g) { return g.constTree(); } +}; + +/// Partial specialization for const Grid types +template +struct TreeAdapter > +{ + using TreeType = _TreeType; + using NonConstTreeType = typename std::remove_const::type; + using TreePtrType = typename TreeType::Ptr; + using ConstTreePtrType = typename TreeType::ConstPtr; + using NonConstTreePtrType = typename NonConstTreeType::Ptr; + using GridType = Grid; + using NonConstGridType = Grid; + using GridPtrType = typename GridType::Ptr; + using NonConstGridPtrType = typename NonConstGridType::Ptr; + using ConstGridPtrType = typename GridType::ConstPtr; + using ValueType = typename TreeType::ValueType; + using AccessorType = typename tree::ValueAccessor; + using ConstAccessorType = typename tree::ValueAccessor; + using NonConstAccessorType = typename tree::ValueAccessor; + + static NonConstTreeType& tree(NonConstTreeType& t) { return t; } + static NonConstTreeType& tree(NonConstGridType& g) { return g.tree(); } + static const NonConstTreeType& tree(const NonConstTreeType& t) { return t; } + static const NonConstTreeType& tree(const NonConstGridType& g) { return g.tree(); } + static const NonConstTreeType& constTree(NonConstTreeType& t) { return t; } + static const NonConstTreeType& constTree(NonConstGridType& g) { return g.constTree(); } + static const NonConstTreeType& constTree(const NonConstTreeType& t) { return t; } + static const NonConstTreeType& constTree(const NonConstGridType& g) { return g.constTree(); } }; /// Partial specialization for ValueAccessor types @@ -1129,19 +1158,25 @@ struct TreeAdapter > using ConstGridPtrType = typename GridType::ConstPtr; using ValueType = typename TreeType::ValueType; using AccessorType = typename tree::ValueAccessor; - using ConstAccessorType = typename tree::ValueAccessor; + using ConstAccessorType = typename tree::ValueAccessor; using NonConstAccessorType = typename tree::ValueAccessor; - static TreeType& tree(TreeType& t) { return t; } - static TreeType& tree(GridType& g) { return g.tree(); } - static TreeType& tree(AccessorType& a) { return a.tree(); } - static const TreeType& tree(const TreeType& t) { return t; } - static const TreeType& tree(const GridType& g) { return g.tree(); } - static const TreeType& tree(const AccessorType& a) { return a.tree(); } - static const TreeType& constTree(TreeType& t) { return t; } - static const TreeType& constTree(GridType& g) { return g.constTree(); } - static const TreeType& constTree(const TreeType& t) { return t; } - static const TreeType& constTree(const GridType& g) { return g.constTree(); } + static NonConstTreeType& tree(NonConstTreeType& t) { return t; } + static NonConstTreeType& tree(NonConstGridType& g) { return g.tree(); } + static NonConstTreeType& tree(NonConstAccessorType& a) { return a.tree(); } + static const NonConstTreeType& tree(ConstAccessorType& a) { return a.tree(); } + static const NonConstTreeType& tree(const NonConstTreeType& t) { return t; } + static const NonConstTreeType& tree(const NonConstGridType& g) { return g.tree(); } + static const NonConstTreeType& tree(const NonConstAccessorType& a) { return a.tree(); } + static const NonConstTreeType& tree(const ConstAccessorType& a) { return a.tree(); } + static const NonConstTreeType& constTree(NonConstTreeType& t) { return t; } + static const NonConstTreeType& constTree(NonConstGridType& g) { return g.constTree(); } + static const NonConstTreeType& constTree(NonConstAccessorType& a) { return a.tree(); } + static const NonConstTreeType& constTree(ConstAccessorType& a) { return a.tree(); } + static const NonConstTreeType& constTree(const NonConstTreeType& t) { return t; } + static const NonConstTreeType& constTree(const NonConstGridType& g) { return g.constTree(); } + static const NonConstTreeType& constTree(const NonConstAccessorType& a) { return a.tree(); } + static const NonConstTreeType& constTree(const ConstAccessorType& a) { return a.tree(); } }; //@} diff --git a/openvdb/openvdb/unittest/TestGrid.cc b/openvdb/openvdb/unittest/TestGrid.cc index 1e9d058b77..f769f2f384 100644 --- a/openvdb/openvdb/unittest/TestGrid.cc +++ b/openvdb/openvdb/unittest/TestGrid.cc @@ -512,3 +512,105 @@ TEST_F(TestGrid, testApply) EXPECT_EQ(4, n); } } + +TEST_F(TestGrid, testAdapter) +{ + openvdb::FloatGrid floatGrid; + const openvdb::FloatGrid constFloatGrid = floatGrid; + openvdb::FloatTree& floatTree = floatGrid.tree(); + const openvdb::FloatTree& constFloatTree = floatGrid.constTree(); + openvdb::tree::ValueAccessor floatAcc(floatGrid.tree()); + openvdb::tree::ValueAccessor constFloatAcc(floatGrid.constTree()); + + { + // test TreeAdapter + + using AdapterT = openvdb::TreeAdapter; + + AdapterT::tree(floatTree); + AdapterT::tree(floatGrid); + AdapterT::tree(constFloatTree); + AdapterT::tree(constFloatGrid); + AdapterT::constTree(floatTree); + AdapterT::constTree(floatGrid); + AdapterT::constTree(constFloatTree); + AdapterT::constTree(constFloatGrid); + + // test TreeAdapter + + using ConstAdapterT = openvdb::TreeAdapter; + + ConstAdapterT::tree(floatTree); + ConstAdapterT::tree(floatGrid); + ConstAdapterT::tree(constFloatTree); + ConstAdapterT::tree(constFloatGrid); + ConstAdapterT::constTree(floatTree); + ConstAdapterT::constTree(floatGrid); + ConstAdapterT::constTree(constFloatTree); + ConstAdapterT::constTree(constFloatGrid); + } + + { + // test TreeAdapter + + using AdapterT = openvdb::TreeAdapter; + + AdapterT::tree(floatTree); + AdapterT::tree(floatGrid); + AdapterT::tree(constFloatTree); + AdapterT::tree(constFloatGrid); + AdapterT::constTree(floatTree); + AdapterT::constTree(floatGrid); + AdapterT::constTree(constFloatTree); + AdapterT::constTree(constFloatGrid); + + // test TreeAdapter + + using ConstAdapterT = openvdb::TreeAdapter; + + ConstAdapterT::tree(floatTree); + ConstAdapterT::tree(floatGrid); + ConstAdapterT::tree(constFloatTree); + ConstAdapterT::tree(constFloatGrid); + ConstAdapterT::constTree(floatTree); + ConstAdapterT::constTree(floatGrid); + ConstAdapterT::constTree(constFloatTree); + ConstAdapterT::constTree(constFloatGrid); + } + + { + // test TreeAdapter> + + using AdapterT = openvdb::TreeAdapter>; + + AdapterT::tree(floatTree); + AdapterT::tree(floatGrid); + AdapterT::tree(floatAcc); + AdapterT::tree(constFloatAcc); + AdapterT::tree(constFloatTree); + AdapterT::tree(constFloatGrid); + AdapterT::constTree(floatTree); + AdapterT::constTree(floatGrid); + AdapterT::constTree(floatAcc); + AdapterT::constTree(constFloatAcc); + AdapterT::constTree(constFloatTree); + AdapterT::constTree(constFloatGrid); + + // test TreeAdapter> + + using AdapterConstT = openvdb::TreeAdapter>; + + AdapterConstT::tree(floatTree); + AdapterConstT::tree(floatGrid); + AdapterConstT::tree(floatAcc); + AdapterConstT::tree(constFloatAcc); + AdapterConstT::tree(constFloatTree); + AdapterConstT::tree(constFloatGrid); + AdapterConstT::constTree(floatTree); + AdapterConstT::constTree(floatGrid); + AdapterConstT::constTree(floatAcc); + AdapterConstT::constTree(constFloatAcc); + AdapterConstT::constTree(constFloatTree); + AdapterConstT::constTree(constFloatGrid); + } +} From 6b9a6c23f534474edbd78ceec1c4660a7ba6169a Mon Sep 17 00:00:00 2001 From: Dan Bailey Date: Tue, 12 Mar 2024 19:13:17 -0700 Subject: [PATCH 2/2] Use non-const Grid in const version of type trait Signed-off-by: Dan Bailey --- openvdb/openvdb/Grid.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/openvdb/openvdb/Grid.h b/openvdb/openvdb/Grid.h index 402dd50f58..c90b9e730a 100644 --- a/openvdb/openvdb/Grid.h +++ b/openvdb/openvdb/Grid.h @@ -1063,7 +1063,7 @@ struct TreeAdapter using TreePtrType = typename TreeType::Ptr; using ConstTreePtrType = typename TreeType::ConstPtr; using NonConstTreePtrType = typename NonConstTreeType::Ptr; - using GridType = Grid; + using GridType = Grid; using NonConstGridType = Grid; using GridPtrType = typename GridType::Ptr; using NonConstGridPtrType = typename NonConstGridType::Ptr; @@ -1151,7 +1151,7 @@ struct TreeAdapter > using TreePtrType = typename TreeType::Ptr; using ConstTreePtrType = typename TreeType::ConstPtr; using NonConstTreePtrType = typename NonConstTreeType::Ptr; - using GridType = Grid; + using GridType = Grid; using NonConstGridType = Grid; using GridPtrType = typename GridType::Ptr; using NonConstGridPtrType = typename NonConstGridType::Ptr;