14# include <openvdb/openvdb.h>
15# include <openvdb/tools/Interpolation.h>
22namespace grid_sampling {
24template<
int N,
class AccessorT>
25inline bool probe_values(
const AccessorT &accessor,
26 const openvdb::CoordBBox &index_box,
27 typename AccessorT::ValueType (&data)[
N][
N][
N])
30 bool any_active =
false;
31 for (
const int dx : IndexRange(
N)) {
32 for (
const int dy : IndexRange(
N)) {
33 for (
const int dz : IndexRange(
N)) {
34 if (accessor.probeValue(index_box.getStart() + openvdb::Coord(dx, dy, dz),
45template<
int N,
class AccessorT>
46inline void get_values(
const AccessorT &accessor,
47 const openvdb::CoordBBox &index_box,
48 typename AccessorT::ValueType (&data)[
N][
N][
N])
51 for (
const int dx : IndexRange(
N)) {
52 for (
const int dy : IndexRange(
N)) {
53 for (
const int dz : IndexRange(
N)) {
54 data[dx][dy][dz] = accessor.getValue(index_box.getStart() + openvdb::Coord(dx, dy, dz));
61template<
typename ValueT,
int N,
typename KernelFn>
62inline void interpolate_value_3d(ValueT (&data)[
N][
N][
N],
63 const openvdb::Vec3R &uvw,
68 for (
const int dx : IndexRange(
N)) {
70 for (
const int dy : IndexRange(
N)) {
71 const ValueT *vz = &
data[dx][dy][0];
72 vy[dy] = kernel_fn(vz, uvw.z());
74 vx[dx] = kernel_fn(vy, uvw.y());
76 result = kernel_fn(vx, uvw.x());
79template<
typename Kernel,
class AccessorT>
80inline bool sample_tree(
const AccessorT &accessor,
81 const openvdb::Vec3R &coord,
82 typename AccessorT::ValueType &result)
84 using ValueT =
typename AccessorT::ValueType;
86 const openvdb::Vec3R sample_offset = openvdb::Vec3R(Kernel::sample_offset);
87 const openvdb::Coord
index = openvdb::Coord(
88 openvdb::tools::local_util::floorVec3(coord + sample_offset));
89 const openvdb::CoordBBox index_box = openvdb::CoordBBox(
90 index - openvdb::Coord(Kernel::samples_left - 1),
91 index + openvdb::Coord(Kernel::samples_right));
92 const openvdb::Vec3R uvw = coord -
index;
95 constexpr int N = Kernel::samples_left + Kernel::samples_right;
97 bool active = probe_values(accessor, index_box, data);
98 interpolate_value_3d(data, uvw, Kernel::template sample_value<ValueT>, result);
103template<
typename Kernel,
class AccessorT>
104inline typename AccessorT::ValueType sample_tree(
const AccessorT &accessor,
105 const openvdb::Vec3R &coord)
107 using ValueT =
typename AccessorT::ValueType;
109 const openvdb::Vec3R sample_offset = openvdb::Vec3R(Kernel::sample_offset);
110 const openvdb::Coord
index = openvdb::Coord(
111 openvdb::tools::local_util::floorVec3(coord + sample_offset));
112 const openvdb::CoordBBox index_box = openvdb::CoordBBox(
113 index - openvdb::Coord(Kernel::samples_left - 1),
114 index + openvdb::Coord(Kernel::samples_right));
115 const openvdb::Vec3R uvw = coord -
index;
118 constexpr int N = Kernel::samples_left + Kernel::samples_right;
120 get_values(accessor, index_box, data);
123 interpolate_value_3d(data, uvw, Kernel::template sample_value<ValueT>, result);
135struct NearestPointKernel {
136 static constexpr int samples_left = 1;
137 static constexpr int samples_right = 0;
139 static constexpr float sample_offset = 0.5f;
141 static float weight(
float x)
152 static float derivative(
float )
157 template<
class ValueT>
static ValueT sample_value(
const ValueT *values,
float )
162 template<
class ValueT>
static ValueT sample_gradient(
const ValueT * ,
float )
172 static constexpr int samples_left = 1;
173 static constexpr int samples_right = 1;
174 static constexpr float sample_offset = 0.0f;
176 static float weight(
float x)
190 static float derivative(
float x)
204 template<
class ValueT>
static ValueT sample_value(
const ValueT *values,
float weight)
206 const ValueT lin = ValueT(values[1] - values[0]);
207 const ValueT con = ValueT(values[0]);
208 return ValueT(weight * lin) + con;
211 template<
class ValueT>
static ValueT sample_gradient(
const ValueT *values,
float )
213 const ValueT con = ValueT(values[1] - values[0]);
249struct QuadraticBSplineKernel {
250 static constexpr int samples_left = 2;
251 static constexpr int samples_right = 1;
253 static constexpr float sample_offset = 0.5f;
255 static float weight(
float x)
261 return (0.5f *
x + 1.5f) *
x + 1.125f;
264 return -
x *
x + 0.75f;
267 return (0.5f *
x - 1.5f) *
x + 1.125f;
272 static float derivative(
float x)
289 template<
class ValueT>
static ValueT sample_value(
const ValueT *values,
float weight)
291 const ValueT
sqr = ValueT(0.5 * (values[0] + values[2]) - values[1]);
292 const ValueT lin = ValueT(0.5 * (values[2] - values[0]));
293 const ValueT con = ValueT(0.125 * (values[0] + values[2]) + 0.75 * values[1]);
294 return weight * (weight *
sqr + lin) + con;
297 template<
class ValueT>
static ValueT sample_gradient(
const ValueT *values,
float weight)
299 const ValueT lin = ValueT(values[0] - 2.0 * values[1] + values[2]);
300 const ValueT con = ValueT(0.5 * (values[2] - values[0]));
301 return weight * lin + con;
342struct CubicBSplineKernel {
343 static constexpr int samples_left = 2;
344 static constexpr int samples_right = 2;
345 static constexpr float sample_offset = 0.0f;
347 static float weight(
float x)
353 return ((
x / 6.0f + 1.0f) *
x + 2.0f) *
x + 4.0f / 3.0f;
356 return (-
x * 0.5f - 1.0f) *
x *
x + 2.0f / 3.0f;
359 return (
x * 0.5f - 1.0f) *
x *
x + 2.0f / 3.0f;
362 return ((-
x / 6.0f + 1.0f) *
x - 2.0f) *
x + 4.0f / 3.0f;
367 static float derivative(
float x)
373 return (0.5 *
x + 2.0f) *
x + 2.0f;
376 return (-1.5f *
x - 2.0f) *
x;
379 return (1.5f *
x - 2.0f) *
x;
382 return (-0.5 *
x + 2.0f) *
x - 2.0f;
387 template<
class ValueT>
static ValueT sample_value(
const ValueT *values,
float weight)
389 constexpr double inv6 = 1.0 / 6.0;
390 const ValueT cub = ValueT(inv6 * (values[3] - values[0]) + 0.5 * (values[1] - values[2]));
391 const ValueT
sqr = ValueT(0.5 * (values[0] + values[2]) - values[1]);
392 const ValueT lin = ValueT(0.5 * (values[2] - values[0]));
393 const ValueT con = ValueT(inv6 * (values[0] + 4.0 * values[1] + values[2]));
394 return weight * (weight * (weight * cub +
sqr) + lin) + con;
397 template<
class ValueT>
static ValueT sample_gradient(
const ValueT *values,
float weight)
399 const ValueT
sqr = ValueT(0.5 * (values[3] - values[0]) + 1.5 * (values[1] - values[2]));
400 const ValueT lin = ValueT(values[0] - 2.0 * values[1] + values[2]);
401 const ValueT con = ValueT(0.5 * (values[2] - values[0]));
402 return weight * (weight *
sqr + lin) + con;
409template<
typename KernelT>
struct SamplerWithKernel {
410 template<
class AccessorT>
411 static bool sample(
const AccessorT &accessor,
412 const openvdb::Vec3R &coord,
413 typename AccessorT::ValueType &result)
415 return grid_sampling::sample_tree<KernelT>(accessor, coord, result);
418 template<
class AccessorT>
419 static typename AccessorT::ValueType
sample(
const AccessorT &accessor,
420 const openvdb::Vec3R &coord)
422 return grid_sampling::sample_tree<KernelT>(accessor, coord);
431using NearestPointSampler = grid_sampling::SamplerWithKernel<grid_sampling::NearestPointKernel>;
436using LinearSampler = grid_sampling::SamplerWithKernel<grid_sampling::LinearKernel>;
441using QuadraticBSplineSampler =
442 grid_sampling::SamplerWithKernel<grid_sampling::QuadraticBSplineKernel>;
447using CubicBSplineSampler = grid_sampling::SamplerWithKernel<grid_sampling::CubicBSplineKernel>;
BMesh const char void * data