Skip to content

Commit 5862e8d

Browse files
committed
Update TreeAdapter to work correctly with all const arguments and add extensive unit tests
Signed-off-by: Dan Bailey <[email protected]>
1 parent 78614d0 commit 5862e8d

File tree

2 files changed

+164
-27
lines changed

2 files changed

+164
-27
lines changed

openvdb/openvdb/Grid.h

Lines changed: 62 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1073,14 +1073,14 @@ struct TreeAdapter
10731073
using ConstAccessorType = typename tree::ValueAccessor<const TreeType>;
10741074
using NonConstAccessorType = typename tree::ValueAccessor<NonConstTreeType>;
10751075

1076-
static TreeType& tree(TreeType& t) { return t; }
1077-
static TreeType& tree(GridType& g) { return g.tree(); }
1078-
static const TreeType& tree(const TreeType& t) { return t; }
1079-
static const TreeType& tree(const GridType& g) { return g.tree(); }
1080-
static const TreeType& constTree(TreeType& t) { return t; }
1081-
static const TreeType& constTree(GridType& g) { return g.constTree(); }
1082-
static const TreeType& constTree(const TreeType& t) { return t; }
1083-
static const TreeType& constTree(const GridType& g) { return g.constTree(); }
1076+
static NonConstTreeType& tree(NonConstTreeType& t) { return t; }
1077+
static NonConstTreeType& tree(NonConstGridType& g) { return g.tree(); }
1078+
static const NonConstTreeType& tree(const NonConstTreeType& t) { return t; }
1079+
static const NonConstTreeType& tree(const NonConstGridType& g) { return g.tree(); }
1080+
static const NonConstTreeType& constTree(NonConstTreeType& t) { return t; }
1081+
static const NonConstTreeType& constTree(NonConstGridType& g) { return g.constTree(); }
1082+
static const NonConstTreeType& constTree(const NonConstTreeType& t) { return t; }
1083+
static const NonConstTreeType& constTree(const NonConstGridType& g) { return g.constTree(); }
10841084
};
10851085

10861086

@@ -1103,14 +1103,43 @@ struct TreeAdapter<Grid<_TreeType> >
11031103
using ConstAccessorType = typename tree::ValueAccessor<const TreeType>;
11041104
using NonConstAccessorType = typename tree::ValueAccessor<NonConstTreeType>;
11051105

1106-
static TreeType& tree(TreeType& t) { return t; }
1107-
static TreeType& tree(GridType& g) { return g.tree(); }
1108-
static const TreeType& tree(const TreeType& t) { return t; }
1109-
static const TreeType& tree(const GridType& g) { return g.tree(); }
1110-
static const TreeType& constTree(TreeType& t) { return t; }
1111-
static const TreeType& constTree(GridType& g) { return g.constTree(); }
1112-
static const TreeType& constTree(const TreeType& t) { return t; }
1113-
static const TreeType& constTree(const GridType& g) { return g.constTree(); }
1106+
static NonConstTreeType& tree(NonConstTreeType& t) { return t; }
1107+
static NonConstTreeType& tree(NonConstGridType& g) { return g.tree(); }
1108+
static const NonConstTreeType& tree(const NonConstTreeType& t) { return t; }
1109+
static const NonConstTreeType& tree(const NonConstGridType& g) { return g.tree(); }
1110+
static const NonConstTreeType& constTree(NonConstTreeType& t) { return t; }
1111+
static const NonConstTreeType& constTree(NonConstGridType& g) { return g.constTree(); }
1112+
static const NonConstTreeType& constTree(const NonConstTreeType& t) { return t; }
1113+
static const NonConstTreeType& constTree(const NonConstGridType& g) { return g.constTree(); }
1114+
};
1115+
1116+
/// Partial specialization for const Grid types
1117+
template<typename _TreeType>
1118+
struct TreeAdapter<const Grid<_TreeType> >
1119+
{
1120+
using TreeType = _TreeType;
1121+
using NonConstTreeType = typename std::remove_const<TreeType>::type;
1122+
using TreePtrType = typename TreeType::Ptr;
1123+
using ConstTreePtrType = typename TreeType::ConstPtr;
1124+
using NonConstTreePtrType = typename NonConstTreeType::Ptr;
1125+
using GridType = Grid<TreeType>;
1126+
using NonConstGridType = Grid<NonConstTreeType>;
1127+
using GridPtrType = typename GridType::Ptr;
1128+
using NonConstGridPtrType = typename NonConstGridType::Ptr;
1129+
using ConstGridPtrType = typename GridType::ConstPtr;
1130+
using ValueType = typename TreeType::ValueType;
1131+
using AccessorType = typename tree::ValueAccessor<TreeType>;
1132+
using ConstAccessorType = typename tree::ValueAccessor<const TreeType>;
1133+
using NonConstAccessorType = typename tree::ValueAccessor<NonConstTreeType>;
1134+
1135+
static NonConstTreeType& tree(NonConstTreeType& t) { return t; }
1136+
static NonConstTreeType& tree(NonConstGridType& g) { return g.tree(); }
1137+
static const NonConstTreeType& tree(const NonConstTreeType& t) { return t; }
1138+
static const NonConstTreeType& tree(const NonConstGridType& g) { return g.tree(); }
1139+
static const NonConstTreeType& constTree(NonConstTreeType& t) { return t; }
1140+
static const NonConstTreeType& constTree(NonConstGridType& g) { return g.constTree(); }
1141+
static const NonConstTreeType& constTree(const NonConstTreeType& t) { return t; }
1142+
static const NonConstTreeType& constTree(const NonConstGridType& g) { return g.constTree(); }
11141143
};
11151144

11161145
/// Partial specialization for ValueAccessor types
@@ -1129,19 +1158,25 @@ struct TreeAdapter<tree::ValueAccessor<_TreeType> >
11291158
using ConstGridPtrType = typename GridType::ConstPtr;
11301159
using ValueType = typename TreeType::ValueType;
11311160
using AccessorType = typename tree::ValueAccessor<TreeType>;
1132-
using ConstAccessorType = typename tree::ValueAccessor<const TreeType>;
1161+
using ConstAccessorType = typename tree::ValueAccessor<const NonConstTreeType>;
11331162
using NonConstAccessorType = typename tree::ValueAccessor<NonConstTreeType>;
11341163

1135-
static TreeType& tree(TreeType& t) { return t; }
1136-
static TreeType& tree(GridType& g) { return g.tree(); }
1137-
static TreeType& tree(AccessorType& a) { return a.tree(); }
1138-
static const TreeType& tree(const TreeType& t) { return t; }
1139-
static const TreeType& tree(const GridType& g) { return g.tree(); }
1140-
static const TreeType& tree(const AccessorType& a) { return a.tree(); }
1141-
static const TreeType& constTree(TreeType& t) { return t; }
1142-
static const TreeType& constTree(GridType& g) { return g.constTree(); }
1143-
static const TreeType& constTree(const TreeType& t) { return t; }
1144-
static const TreeType& constTree(const GridType& g) { return g.constTree(); }
1164+
static NonConstTreeType& tree(NonConstTreeType& t) { return t; }
1165+
static NonConstTreeType& tree(NonConstGridType& g) { return g.tree(); }
1166+
static NonConstTreeType& tree(NonConstAccessorType& a) { return a.tree(); }
1167+
static const NonConstTreeType& tree(ConstAccessorType& a) { return a.tree(); }
1168+
static const NonConstTreeType& tree(const NonConstTreeType& t) { return t; }
1169+
static const NonConstTreeType& tree(const NonConstGridType& g) { return g.tree(); }
1170+
static const NonConstTreeType& tree(const NonConstAccessorType& a) { return a.tree(); }
1171+
static const NonConstTreeType& tree(const ConstAccessorType& a) { return a.tree(); }
1172+
static const NonConstTreeType& constTree(NonConstTreeType& t) { return t; }
1173+
static const NonConstTreeType& constTree(NonConstGridType& g) { return g.constTree(); }
1174+
static const NonConstTreeType& constTree(NonConstAccessorType& a) { return a.tree(); }
1175+
static const NonConstTreeType& constTree(ConstAccessorType& a) { return a.tree(); }
1176+
static const NonConstTreeType& constTree(const NonConstTreeType& t) { return t; }
1177+
static const NonConstTreeType& constTree(const NonConstGridType& g) { return g.constTree(); }
1178+
static const NonConstTreeType& constTree(const NonConstAccessorType& a) { return a.tree(); }
1179+
static const NonConstTreeType& constTree(const ConstAccessorType& a) { return a.tree(); }
11451180
};
11461181

11471182
//@}

openvdb/openvdb/unittest/TestGrid.cc

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -512,3 +512,105 @@ TEST_F(TestGrid, testApply)
512512
EXPECT_EQ(4, n);
513513
}
514514
}
515+
516+
TEST_F(TestGrid, testAdapter)
517+
{
518+
openvdb::FloatGrid floatGrid;
519+
const openvdb::FloatGrid constFloatGrid = floatGrid;
520+
openvdb::FloatTree& floatTree = floatGrid.tree();
521+
const openvdb::FloatTree& constFloatTree = floatGrid.constTree();
522+
openvdb::tree::ValueAccessor<openvdb::FloatTree> floatAcc(floatGrid.tree());
523+
openvdb::tree::ValueAccessor<const openvdb::FloatTree> constFloatAcc(floatGrid.constTree());
524+
525+
{
526+
// test TreeAdapter<Tree>
527+
528+
using AdapterT = openvdb::TreeAdapter<openvdb::FloatTree>;
529+
530+
AdapterT::tree(floatTree);
531+
AdapterT::tree(floatGrid);
532+
AdapterT::tree(constFloatTree);
533+
AdapterT::tree(constFloatGrid);
534+
AdapterT::constTree(floatTree);
535+
AdapterT::constTree(floatGrid);
536+
AdapterT::constTree(constFloatTree);
537+
AdapterT::constTree(constFloatGrid);
538+
539+
// test TreeAdapter<const Tree>
540+
541+
using ConstAdapterT = openvdb::TreeAdapter<const openvdb::FloatTree>;
542+
543+
ConstAdapterT::tree(floatTree);
544+
ConstAdapterT::tree(floatGrid);
545+
ConstAdapterT::tree(constFloatTree);
546+
ConstAdapterT::tree(constFloatGrid);
547+
ConstAdapterT::constTree(floatTree);
548+
ConstAdapterT::constTree(floatGrid);
549+
ConstAdapterT::constTree(constFloatTree);
550+
ConstAdapterT::constTree(constFloatGrid);
551+
}
552+
553+
{
554+
// test TreeAdapter<Grid>
555+
556+
using AdapterT = openvdb::TreeAdapter<openvdb::FloatGrid>;
557+
558+
AdapterT::tree(floatTree);
559+
AdapterT::tree(floatGrid);
560+
AdapterT::tree(constFloatTree);
561+
AdapterT::tree(constFloatGrid);
562+
AdapterT::constTree(floatTree);
563+
AdapterT::constTree(floatGrid);
564+
AdapterT::constTree(constFloatTree);
565+
AdapterT::constTree(constFloatGrid);
566+
567+
// test TreeAdapter<const Grid>
568+
569+
using ConstAdapterT = openvdb::TreeAdapter<const openvdb::FloatGrid>;
570+
571+
ConstAdapterT::tree(floatTree);
572+
ConstAdapterT::tree(floatGrid);
573+
ConstAdapterT::tree(constFloatTree);
574+
ConstAdapterT::tree(constFloatGrid);
575+
ConstAdapterT::constTree(floatTree);
576+
ConstAdapterT::constTree(floatGrid);
577+
ConstAdapterT::constTree(constFloatTree);
578+
ConstAdapterT::constTree(constFloatGrid);
579+
}
580+
581+
{
582+
// test TreeAdapter<ValueAccessor<Tree>>
583+
584+
using AdapterT = openvdb::TreeAdapter<openvdb::tree::ValueAccessor<openvdb::FloatTree>>;
585+
586+
AdapterT::tree(floatTree);
587+
AdapterT::tree(floatGrid);
588+
AdapterT::tree(floatAcc);
589+
AdapterT::tree(constFloatAcc);
590+
AdapterT::tree(constFloatTree);
591+
AdapterT::tree(constFloatGrid);
592+
AdapterT::constTree(floatTree);
593+
AdapterT::constTree(floatGrid);
594+
AdapterT::constTree(floatAcc);
595+
AdapterT::constTree(constFloatAcc);
596+
AdapterT::constTree(constFloatTree);
597+
AdapterT::constTree(constFloatGrid);
598+
599+
// test TreeAdapter<ValueAccessor<const Tree>>
600+
601+
using AdapterConstT = openvdb::TreeAdapter<openvdb::tree::ValueAccessor<const openvdb::FloatTree>>;
602+
603+
AdapterConstT::tree(floatTree);
604+
AdapterConstT::tree(floatGrid);
605+
AdapterConstT::tree(floatAcc);
606+
AdapterConstT::tree(constFloatAcc);
607+
AdapterConstT::tree(constFloatTree);
608+
AdapterConstT::tree(constFloatGrid);
609+
AdapterConstT::constTree(floatTree);
610+
AdapterConstT::constTree(floatGrid);
611+
AdapterConstT::constTree(floatAcc);
612+
AdapterConstT::constTree(constFloatAcc);
613+
AdapterConstT::constTree(constFloatTree);
614+
AdapterConstT::constTree(constFloatGrid);
615+
}
616+
}

0 commit comments

Comments
 (0)