Skip to content

Commit

Permalink
Add an example to demonstrate sampling and point advection using an a…
Browse files Browse the repository at this point in the history
…daptive grid

Signed-off-by: Dan Bailey <[email protected]>
  • Loading branch information
danrbailey committed Mar 12, 2024
1 parent 6145b10 commit ed5046d
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 38 deletions.
106 changes: 68 additions & 38 deletions openvdb/openvdb/tools/Interpolation.h
Original file line number Diff line number Diff line change
Expand Up @@ -629,29 +629,35 @@ template<class ValueT, class TreeT, size_t N>
inline void
BoxSampler::getValues(ValueT (&data)[N][N][N], const TreeT& inTree, Coord ijk)
{
data[0][0][0] = inTree.getValue(ijk); // i, j, k
// This algorithm is only defined for sparse grids

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

ijk[1] += 1;
data[0][1][1] = inTree.getValue(ijk); // i, j+1, k + 1
ijk[2] += 1;
data[0][0][1] = inTree.getValue(ijk); // i, j, k + 1

ijk[2] -= 1;
data[0][1][0] = inTree.getValue(ijk); // i, j+1, k
ijk[1] += 1;
data[0][1][1] = inTree.getValue(ijk); // i, j+1, k + 1

ijk[0] += 1;
ijk[1] -= 1;
data[1][0][0] = inTree.getValue(ijk); // i+1, j, k
ijk[2] -= 1;
data[0][1][0] = inTree.getValue(ijk); // i, j+1, k

ijk[2] += 1;
data[1][0][1] = inTree.getValue(ijk); // i+1, j, k + 1
ijk[0] += 1;
ijk[1] -= 1;
data[1][0][0] = inTree.getValue(ijk); // i+1, j, k

ijk[1] += 1;
data[1][1][1] = inTree.getValue(ijk); // i+1, j+1, k + 1
ijk[2] += 1;
data[1][0][1] = inTree.getValue(ijk); // i+1, j, k + 1

ijk[2] -= 1;
data[1][1][0] = inTree.getValue(ijk); // i+1, j+1, k
ijk[1] += 1;
data[1][1][1] = inTree.getValue(ijk); // i+1, j+1, k + 1

ijk[2] -= 1;
data[1][1][0] = inTree.getValue(ijk); // i+1, j+1, k
} else {
static_assert(AlwaysFalseValue<TreeT>, "Not Implemented");
}
}

template<class ValueT, class TreeT, size_t N>
Expand Down Expand Up @@ -744,39 +750,63 @@ inline bool
BoxSampler::sample(const TreeT& inTree, const Vec3R& inCoord,
typename TreeT::ValueType& result)
{
using ValueT = typename TreeT::ValueType;

const Vec3i inIdx = local_util::floorVec3(inCoord);
const Vec3R uvw = inCoord - inIdx;

// Retrieve the values of the eight voxels surrounding the
// fractional source coordinates.
ValueT data[2][2][2];

const bool hasActiveValues = BoxSampler::probeValues(data, inTree, Coord(inIdx));

result = BoxSampler::trilinearInterpolation(data, uvw);

return hasActiveValues;
if constexpr (TreeTraits<TreeT>::IsSparse) {
using ValueT = typename TreeT::ValueType;

const Vec3i inIdx = local_util::floorVec3(inCoord);
const Vec3R uvw = inCoord - inIdx;

// Retrieve the values of the eight voxels surrounding the
// fractional source coordinates.
ValueT data[2][2][2];

const bool hasActiveValues = BoxSampler::probeValues(data, inTree, Coord(inIdx));

result = BoxSampler::trilinearInterpolation(data, uvw);

return hasActiveValues;
} else if constexpr (TreeTraits<TreeT>::IsAdaptive) {
// 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
auto& tree = TreeAdapter<TreeT>::tree(inTree);
result = tree.background();
return true;
} else {
static_assert(AlwaysFalseValue<TreeT>, "Not Implemented");
}
std::abort(); // unreachable
}


template<class TreeT>
inline typename TreeT::ValueType
BoxSampler::sample(const TreeT& inTree, const Vec3R& inCoord)
{
using ValueT = typename TreeT::ValueType;
if constexpr (TreeTraits<TreeT>::IsSparse) {

const Vec3i inIdx = local_util::floorVec3(inCoord);
const Vec3R uvw = inCoord - inIdx;
using ValueT = typename TreeT::ValueType;

// Retrieve the values of the eight voxels surrounding the
// fractional source coordinates.
ValueT data[2][2][2];
const Vec3i inIdx = local_util::floorVec3(inCoord);
const Vec3R uvw = inCoord - inIdx;

// Retrieve the values of the eight voxels surrounding the
// fractional source coordinates.
ValueT data[2][2][2];

BoxSampler::getValues(data, inTree, Coord(inIdx));
BoxSampler::getValues(data, inTree, Coord(inIdx));

return BoxSampler::trilinearInterpolation(data, uvw);
return BoxSampler::trilinearInterpolation(data, uvw);
} else if constexpr (TreeTraits<TreeT>::IsAdaptive) {
// 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
auto& tree = TreeAdapter<TreeT>::tree(inTree);
return tree.background();
} else {
static_assert(AlwaysFalseValue<TreeT>, "Not Implemented");
}
std::abort(); // unreachable
}


Expand Down
34 changes: 34 additions & 0 deletions openvdb/openvdb/unittest/TestAdaptive.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// SPDX-License-Identifier: MPL-2.0

#include <openvdb/openvdb.h>
#include <openvdb/tools/Interpolation.h>
#include <openvdb/tools/PointAdvect.h>
#include <openvdb/adaptive/AdaptiveGrid.h>

#include <gtest/gtest.h>
Expand All @@ -23,3 +25,35 @@ TEST_F(TestAdaptive, test)

EXPECT_EQ(adaptiveGrid.background(), 5.0f);
}

TEST_F(TestAdaptive, testSample)
{
const float background = 5.0f;
openvdb::adaptive::AdaptiveGrid<float> adaptiveGrid(background);

float result = openvdb::tools::BoxSampler::sample(adaptiveGrid.tree(), openvdb::Vec3R(1.3, 1.6, 1.8));

EXPECT_EQ(result, background);
}

TEST_F(TestAdaptive, testAdvect)
{
using AdaptiveGridT = openvdb::adaptive::AdaptiveGrid<openvdb::Vec3s>;
using PointAdvectT = openvdb::tools::PointAdvect<AdaptiveGridT>;
using PointListT = PointAdvectT::PointListType;

const openvdb::Vec3s background(0.0f, 1.0f, 0.0f);
AdaptiveGridT adaptiveGrid(background);

openvdb::tools::PointAdvect<AdaptiveGridT> pointAdvect(adaptiveGrid);

PointListT points;
points.push_back(openvdb::Vec3s(0.0f, 0.0f, 0.0f));
points.push_back(openvdb::Vec3s(1.0f, 2.0f, 3.0f));

float dt = 1/24.0f;
pointAdvect.advect(points, dt);

EXPECT_EQ(points[0], openvdb::Vec3s(0.0f, dt, 0.0f));
EXPECT_EQ(points[1], openvdb::Vec3s(1.0f, 2.0f + dt, 3.0f));
}

0 comments on commit ed5046d

Please sign in to comment.