Skip to content

Commit

Permalink
Merge pull request #1775 from danrbailey/tree_adapter_improvements
Browse files Browse the repository at this point in the history
Update TreeAdapter to work with const inputs
  • Loading branch information
danrbailey committed Sep 18, 2024
2 parents 1f9a882 + 0a3de16 commit 3b473dd
Show file tree
Hide file tree
Showing 2 changed files with 166 additions and 29 deletions.
93 changes: 64 additions & 29 deletions openvdb/openvdb/Grid.h
Original file line number Diff line number Diff line change
Expand Up @@ -1063,7 +1063,7 @@ struct TreeAdapter
using TreePtrType = typename TreeType::Ptr;
using ConstTreePtrType = typename TreeType::ConstPtr;
using NonConstTreePtrType = typename NonConstTreeType::Ptr;
using GridType = Grid<TreeType>;
using GridType = Grid<NonConstTreeType>;
using NonConstGridType = Grid<NonConstTreeType>;
using GridPtrType = typename GridType::Ptr;
using NonConstGridPtrType = typename NonConstGridType::Ptr;
Expand All @@ -1073,14 +1073,14 @@ struct TreeAdapter
using ConstAccessorType = typename tree::ValueAccessor<const TreeType>;
using NonConstAccessorType = typename tree::ValueAccessor<NonConstTreeType>;

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(); }
};


Expand All @@ -1103,19 +1103,19 @@ struct TreeAdapter<Grid<_TreeType> >
using ConstAccessorType = typename tree::ValueAccessor<const TreeType>;
using NonConstAccessorType = typename tree::ValueAccessor<NonConstTreeType>;

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 ValueAccessor types
/// Partial specialization for const Grid types
template<typename _TreeType>
struct TreeAdapter<tree::ValueAccessor<_TreeType> >
struct TreeAdapter<const Grid<_TreeType> >
{
using TreeType = _TreeType;
using NonConstTreeType = typename std::remove_const<TreeType>::type;
Expand All @@ -1132,16 +1132,51 @@ struct TreeAdapter<tree::ValueAccessor<_TreeType> >
using ConstAccessorType = typename tree::ValueAccessor<const TreeType>;
using NonConstAccessorType = typename tree::ValueAccessor<NonConstTreeType>;

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 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
template<typename _TreeType>
struct TreeAdapter<tree::ValueAccessor<_TreeType> >
{
using TreeType = _TreeType;
using NonConstTreeType = typename std::remove_const<TreeType>::type;
using TreePtrType = typename TreeType::Ptr;
using ConstTreePtrType = typename TreeType::ConstPtr;
using NonConstTreePtrType = typename NonConstTreeType::Ptr;
using GridType = Grid<NonConstTreeType>;
using NonConstGridType = Grid<NonConstTreeType>;
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<TreeType>;
using ConstAccessorType = typename tree::ValueAccessor<const NonConstTreeType>;
using NonConstAccessorType = typename tree::ValueAccessor<NonConstTreeType>;

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(); }
};

//@}
Expand Down
102 changes: 102 additions & 0 deletions openvdb/openvdb/unittest/TestGrid.cc
Original file line number Diff line number Diff line change
Expand Up @@ -517,3 +517,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<openvdb::FloatTree> floatAcc(floatGrid.tree());
openvdb::tree::ValueAccessor<const openvdb::FloatTree> constFloatAcc(floatGrid.constTree());

{
// test TreeAdapter<Tree>

using AdapterT = openvdb::TreeAdapter<openvdb::FloatTree>;

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<const Tree>

using ConstAdapterT = openvdb::TreeAdapter<const openvdb::FloatTree>;

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<Grid>

using AdapterT = openvdb::TreeAdapter<openvdb::FloatGrid>;

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<const Grid>

using ConstAdapterT = openvdb::TreeAdapter<const openvdb::FloatGrid>;

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<ValueAccessor<Tree>>

using AdapterT = openvdb::TreeAdapter<openvdb::tree::ValueAccessor<openvdb::FloatTree>>;

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<ValueAccessor<const Tree>>

using AdapterConstT = openvdb::TreeAdapter<openvdb::tree::ValueAccessor<const openvdb::FloatTree>>;

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);
}
}

0 comments on commit 3b473dd

Please sign in to comment.