Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft PR: Half Grid Support for OpenVDB #1730

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ include(GNUInstallDirs)
option(OPENVDB_BUILD_CORE "Enable the core OpenVDB library. Both static and shared versions are enabled by default" ON)
option(OPENVDB_BUILD_BINARIES "Enable the vdb binaries. Only vdb_print is enabled by default" ON)
option(OPENVDB_BUILD_PYTHON_MODULE "Build the pyopenvdb Python module" OFF)
option(OPENVDB_BUILD_UNITTESTS "Build the OpenVDB unit tests" OFF)
option(OPENVDB_BUILD_UNITTESTS "Build the OpenVDB unit tests" ON)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I assume some of these changes are just part of the draft PR and won't end up in the final PR?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Exactly! I changed a few things in this Draft PR to highlight some of the processes I'm going through for the people from Autodesk.

option(OPENVDB_BUILD_DOCS "Build the OpenVDB documentation" OFF)
option(OPENVDB_BUILD_HOUDINI_PLUGIN "Build the Houdini plugin" OFF)
option(OPENVDB_BUILD_HOUDINI_ABITESTS "Build the Houdini ABI tests" OFF)
Expand Down
24 changes: 24 additions & 0 deletions openvdb/openvdb/Types.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ using Int64 = int64_t;
using Int = Int32;
using Byte = unsigned char;
using Real = double;
using Half = math::half;

// Two-dimensional vector types
using Vec2R = math::Vec2<Real>;
Expand Down Expand Up @@ -446,6 +447,29 @@ template<typename FromType, typename ToType> struct CopyConstness<const FromType
/// @endcond


////////////////////////////////////////
template<class T>
struct is_floating_point : std::is_floating_point<T> { };

template<>
struct is_floating_point<math::half> : std::is_floating_point<float> { };

/// @brief Maps one type (e.g. the value types) to other types
template<typename T>
struct ValueToComputeMap
{
using Type = T;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should probably pick one style and go with that? Either Type or type.

using type = T;
};

template<>
struct ValueToComputeMap<math::half>
{
using Type = float;
using type = float;
};


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


Expand Down
16 changes: 16 additions & 0 deletions openvdb/openvdb/math/Vec3.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <algorithm>
#include <cmath>
#include <type_traits>
#include "Half.h"


namespace openvdb {
Expand Down Expand Up @@ -668,6 +669,21 @@ OPENVDB_IS_POD(Vec3ui)
OPENVDB_IS_POD(Vec3s)
OPENVDB_IS_POD(Vec3d)

// Half WIP
template<>
inline auto cwiseAdd(const Vec3<math::internal::half>& v, const float s)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this use math::half not math::internal::half ? Otherwise it won't work when compiling against an external half?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right. Thanks for catching this, Dan.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just pushed an updated version of this PR that moves this function to Types.h because that's where we first define math::half depending on the macro OPENVDB_USE_IMATH_HALF.

{
Vec3<math::internal::half> out;
const math::internal::half* ip = v.asPointer();
math::internal::half* op = out.asPointer();
for (unsigned i = 0; i < 3; ++i, ++op, ++ip) {
OPENVDB_NO_TYPE_CONVERSION_WARNING_BEGIN
*op = *ip + s;
OPENVDB_NO_TYPE_CONVERSION_WARNING_END
}
return out;
}

} // namespace math
} // namespace OPENVDB_VERSION_NAME
} // namespace openvdb
Expand Down
2 changes: 2 additions & 0 deletions openvdb/openvdb/openvdb.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ namespace io { class DelayedLoadMetadata; }
using BoolTree = tree::Tree4<bool, 5, 4, 3>::Type;
using DoubleTree = tree::Tree4<double, 5, 4, 3>::Type;
using FloatTree = tree::Tree4<float, 5, 4, 3>::Type;
using HalfTree = tree::Tree4<Half, 5, 4, 3>::Type;
using Int32Tree = tree::Tree4<int32_t, 5, 4, 3>::Type;
using Int64Tree = tree::Tree4<int64_t, 5, 4, 3>::Type;
using MaskTree = tree::Tree4<ValueMask, 5, 4, 3>::Type;
Expand All @@ -73,6 +74,7 @@ using VectorTree = Vec3fTree;
using BoolGrid = Grid<BoolTree>;
using DoubleGrid = Grid<DoubleTree>;
using FloatGrid = Grid<FloatTree>;
using HalfGrid = Grid<HalfTree>;
using Int32Grid = Grid<Int32Tree>;
using Int64Grid = Grid<Int64Tree>;
using MaskGrid = Grid<MaskTree>;
Expand Down
32 changes: 24 additions & 8 deletions openvdb/openvdb/tools/Interpolation.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,16 @@
#ifndef OPENVDB_TOOLS_INTERPOLATION_HAS_BEEN_INCLUDED
#define OPENVDB_TOOLS_INTERPOLATION_HAS_BEEN_INCLUDED

#include <openvdb/version.h> // for OPENVDB_VERSION_NAME
#include <openvdb/Grid.h>
#include <openvdb/Platform.h> // for round()
#include <openvdb/Types.h> // for ValueToComputeMap
#include <openvdb/math/Math.h>// for SmoothUnitStep
#include <openvdb/math/Transform.h> // for Transform
#include <openvdb/Grid.h>
#include <openvdb/tree/ValueAccessor.h>
#include <openvdb/version.h> // for OPENVDB_VERSION_NAME
#include <cmath>
#include <type_traits>
#include <iostream>

namespace openvdb {
OPENVDB_USE_VERSION_NAMESPACE
Expand Down Expand Up @@ -313,7 +315,7 @@ class GridSampler
/// @brief Sample value in integer index space
/// @param i Integer x-coordinate in index space
/// @param j Integer y-coordinate in index space
/// @param k Integer x-coordinate in index space
/// @param k Integer z-coordinate in index space
ValueType sampleVoxel(typename Coord::ValueType i,
typename Coord::ValueType j,
typename Coord::ValueType k) const
Expand Down Expand Up @@ -392,7 +394,7 @@ class GridSampler<tree::ValueAccessor<TreeT>, SamplerType>
/// @brief Sample value in integer index space
/// @param i Integer x-coordinate in index space
/// @param j Integer y-coordinate in index space
/// @param k Integer x-coordinate in index space
/// @param k Integer z-coordinate in index space
ValueType sampleVoxel(typename Coord::ValueType i,
typename Coord::ValueType j,
typename Coord::ValueType k) const
Expand Down Expand Up @@ -744,17 +746,31 @@ BoxSampler::sample(const TreeT& inTree, const Vec3R& inCoord,
typename TreeT::ValueType& result)
{
using ValueT = typename TreeT::ValueType;
using ComputeT = typename ValueToComputeMap<ValueT>::Type;

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));
ValueT probeData[2][2][2];
ComputeT computeData[2][2][2];
bool hasActiveValues;

if constexpr(std::is_same<ValueT, ComputeT>::value) {
hasActiveValues = BoxSampler::probeValues(computeData, inTree, Coord(inIdx));
} else {
hasActiveValues = BoxSampler::probeValues(probeData, inTree, Coord(inIdx));
for (int dx = 0; dx < 2; ++dx) {
for (int dy = 0; dy < 2; ++dy) {
for (int dz = 0; dz < 2; ++dz) {
computeData[dx][dy][dz] = probeData[dx][dy][dz];
}
}
}
}

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

return hasActiveValues;
}
Expand Down
2 changes: 1 addition & 1 deletion openvdb/openvdb/tools/RayIntersector.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ class LevelSetRayIntersector
using TreeT = typename GridT::TreeType;

static_assert(NodeLevel >= -1 && NodeLevel < int(TreeT::DEPTH)-1, "NodeLevel out of range");
static_assert(std::is_floating_point<ValueT>::value,
static_assert(openvdb::is_floating_point<ValueT>::value,
"level set grids must have scalar, floating-point value types");

/// @brief Constructor
Expand Down
2 changes: 1 addition & 1 deletion openvdb/openvdb/tools/RayTracer.h
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ class VolumeRender
using ValueType = typename GridType::ValueType;
using AccessorType = typename GridType::ConstAccessor;
using SamplerType = tools::GridSampler<AccessorType, SamplerT>;
static_assert(std::is_floating_point<ValueType>::value,
static_assert(openvdb::is_floating_point<ValueType>::value,
"VolumeRender requires a floating-point-valued grid");

/// @brief Constructor taking an intersector and a base camera.
Expand Down
Loading
Loading