Skip to content

Commit

Permalink
Use an enum to distinguish between sparse vs adaptive trees
Browse files Browse the repository at this point in the history
Signed-off-by: Dan Bailey <[email protected]>
  • Loading branch information
danrbailey committed Apr 5, 2024
1 parent ed5046d commit 294f0f1
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 20 deletions.
18 changes: 15 additions & 3 deletions openvdb/openvdb/Types.h
Original file line number Diff line number Diff line change
Expand Up @@ -236,15 +236,27 @@ using make_index_sequence =

////////////////////////////////////////

/// @brief A Traits struct that can be used to query properties of an input T.
/// @brief An enum to distinguish between different types of Tree representation.
enum class TreeRepresentation : uint32_t {Unknown = 0, Sparse = 1, Adaptive = 2, End = 3};

/// @brief A TreeTraits struct that can be used to query properties of an input T.
template<typename T>
struct TreeTraits
{
static const bool IsSparse = false;
static const bool IsAdaptive = false;
constexpr static TreeRepresentation Representation = TreeRepresentation::Unknown;
};

template <typename T>
constexpr bool isSparseTree()
{
return TreeTraits<T>::Representation == TreeRepresentation::Sparse;
}

template <typename T>
constexpr bool isAdaptiveTree()
{
return TreeTraits<T>::Representation == TreeRepresentation::Adaptive;
}

////////////////////////////////////////

Expand Down
9 changes: 3 additions & 6 deletions openvdb/openvdb/adaptive/AdaptiveGrid.h
Original file line number Diff line number Diff line change
Expand Up @@ -289,24 +289,21 @@ struct TreeAdapter<adaptive::AdaptiveAccessor<_TreeType> >
template<typename ValueT>
struct TreeTraits<adaptive::AdaptiveTree<ValueT>>
{
static const bool IsSparse = false;
static const bool IsAdaptive = true;
constexpr static TreeRepresentation Representation = TreeRepresentation::Adaptive;
};

template<typename ValueT>
struct TreeTraits<const adaptive::AdaptiveTree<ValueT>>
{
static const bool IsSparse = false;
static const bool IsAdaptive = true;
constexpr static TreeRepresentation Representation = TreeRepresentation::Adaptive;
};

// Overload the TreeTraits struct to declare an AdaptiveAccessor as adaptive

template<typename TreeT>
struct TreeTraits<adaptive::AdaptiveAccessor<TreeT>>
{
static const bool IsSparse = false;
static const bool IsAdaptive = true;
constexpr static TreeRepresentation Representation = TreeRepresentation::Adaptive;
};


Expand Down
10 changes: 5 additions & 5 deletions openvdb/openvdb/tools/Interpolation.h
Original file line number Diff line number Diff line change
Expand Up @@ -631,7 +631,7 @@ BoxSampler::getValues(ValueT (&data)[N][N][N], const TreeT& inTree, Coord ijk)
{
// This algorithm is only defined for sparse grids

if constexpr (TreeTraits<TreeT>::IsSparse) {
if constexpr (isSparseTree<TreeT>()) {
data[0][0][0] = inTree.getValue(ijk); // i, j, k

ijk[2] += 1;
Expand Down Expand Up @@ -750,7 +750,7 @@ inline bool
BoxSampler::sample(const TreeT& inTree, const Vec3R& inCoord,
typename TreeT::ValueType& result)
{
if constexpr (TreeTraits<TreeT>::IsSparse) {
if constexpr (isSparseTree<TreeT>()) {
using ValueT = typename TreeT::ValueType;

const Vec3i inIdx = local_util::floorVec3(inCoord);
Expand All @@ -765,7 +765,7 @@ BoxSampler::sample(const TreeT& inTree, const Vec3R& inCoord,
result = BoxSampler::trilinearInterpolation(data, uvw);

return hasActiveValues;
} else if constexpr (TreeTraits<TreeT>::IsAdaptive) {
} else if constexpr (isAdaptiveTree<TreeT>()) {
// As an example, return the background value.
// This is where the logic that could sample against an adaptive tree would live.
// Extract the tree from the Tree or ValueAccessor
Expand All @@ -783,7 +783,7 @@ template<class TreeT>
inline typename TreeT::ValueType
BoxSampler::sample(const TreeT& inTree, const Vec3R& inCoord)
{
if constexpr (TreeTraits<TreeT>::IsSparse) {
if constexpr (isSparseTree<TreeT>()) {

using ValueT = typename TreeT::ValueType;

Expand All @@ -797,7 +797,7 @@ BoxSampler::sample(const TreeT& inTree, const Vec3R& inCoord)
BoxSampler::getValues(data, inTree, Coord(inIdx));

return BoxSampler::trilinearInterpolation(data, uvw);
} else if constexpr (TreeTraits<TreeT>::IsAdaptive) {
} else if constexpr (isAdaptiveTree<TreeT>()) {
// As an example, return the background value.
// This is where the logic that could sample against an adaptive tree would live.
// Extract the tree from the Tree or ValueAccessor
Expand Down
6 changes: 2 additions & 4 deletions openvdb/openvdb/tree/Tree.h
Original file line number Diff line number Diff line change
Expand Up @@ -2134,15 +2134,13 @@ Tree<RootNodeType>::print(std::ostream& os, int verboseLevel) const
template<typename NodeT>
struct TreeTraits<tree::Tree<NodeT>>
{
static const bool IsSparse = true;
static const bool IsAdaptive = false;
constexpr static TreeRepresentation Representation = TreeRepresentation::Sparse;
};

template<typename NodeT>
struct TreeTraits<const tree::Tree<NodeT>>
{
static const bool IsSparse = true;
static const bool IsAdaptive = false;
constexpr static TreeRepresentation Representation = TreeRepresentation::Sparse;
};


Expand Down
3 changes: 1 addition & 2 deletions openvdb/openvdb/tree/ValueAccessor.h
Original file line number Diff line number Diff line change
Expand Up @@ -1030,8 +1030,7 @@ class ValueAccessorImpl final :
template<typename TreeT, bool IsSafeT, typename MutexT, typename IndexSequenceT>
struct TreeTraits<tree::ValueAccessorImpl<TreeT, IsSafeT, MutexT, IndexSequenceT>>
{
static const bool IsSparse = true;
static const bool IsAdaptive = false;
constexpr static TreeRepresentation Representation = TreeRepresentation::Sparse;
};


Expand Down

0 comments on commit 294f0f1

Please sign in to comment.