Blender V5.3
GEO_grid_samplers.hh
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#pragma once
10
11#include "BLI_index_range.hh"
12
13#ifdef WITH_OPENVDB
14# include <openvdb/openvdb.h>
15# include <openvdb/tools/Interpolation.h>
16#endif
17
18namespace blender::geometry {
19
20#ifdef WITH_OPENVDB
21
22namespace grid_sampling {
23
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])
28{
29 /* Retrieve the values of the voxels surrounding the fractional source coordinates. */
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),
35 data[dx][dy][dz]))
36 {
37 any_active = true;
38 }
39 }
40 }
41 }
42 return any_active;
43}
44
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])
49{
50 /* Retrieve the values of the voxels surrounding the fractional source coordinates. */
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));
55 }
56 }
57 }
58}
59
60/* Interpolate values in 3D using a function that combines a 1-dimensional array. */
61template<typename ValueT, int N, typename KernelFn>
62inline void interpolate_value_3d(ValueT (&data)[N][N][N],
63 const openvdb::Vec3R &uvw,
64 KernelFn kernel_fn,
65 ValueT &result)
66{
67 ValueT vx[N];
68 for (const int dx : IndexRange(N)) {
69 ValueT vy[N];
70 for (const int dy : IndexRange(N)) {
71 const ValueT *vz = &data[dx][dy][0];
72 vy[dy] = kernel_fn(vz, uvw.z());
73 }
74 vx[dx] = kernel_fn(vy, uvw.y());
75 }
76 result = kernel_fn(vx, uvw.x());
77}
78
79template<typename Kernel, class AccessorT>
80inline bool sample_tree(const AccessorT &accessor,
81 const openvdb::Vec3R &coord,
82 typename AccessorT::ValueType &result)
83{
84 using ValueT = typename AccessorT::ValueType;
85
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;
93
94 /* Retrieve the values of the voxels surrounding the fractional source coordinates. */
95 constexpr int N = Kernel::samples_left + Kernel::samples_right;
96 ValueT data[N][N][N];
97 bool active = probe_values(accessor, index_box, data);
98 interpolate_value_3d(data, uvw, Kernel::template sample_value<ValueT>, result);
99
100 return active;
101}
102
103template<typename Kernel, class AccessorT>
104inline typename AccessorT::ValueType sample_tree(const AccessorT &accessor,
105 const openvdb::Vec3R &coord)
106{
107 using ValueT = typename AccessorT::ValueType;
108
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;
116
117 /* Retrieve the values of the voxels surrounding the fractional source coordinates. */
118 constexpr int N = Kernel::samples_left + Kernel::samples_right;
119 ValueT data[N][N][N];
120 get_values(accessor, index_box, data);
121
122 ValueT result;
123 interpolate_value_3d(data, uvw, Kernel::template sample_value<ValueT>, result);
124 return result;
125}
126
135struct NearestPointKernel {
136 static constexpr int samples_left = 1;
137 static constexpr int samples_right = 0;
138 /* Shift the sampling interval (round the coordinate), to use the value the closest point. */
139 static constexpr float sample_offset = 0.5f;
140
141 static float weight(float x)
142 {
143 if (x < -0.5f) {
144 return 0.0f;
145 }
146 if (x < 0.5f) {
147 return 1.0f;
148 }
149 return 0.0f;
150 }
151
152 static float derivative(float /*x*/)
153 {
154 return 0.0f;
155 }
156
157 template<class ValueT> static ValueT sample_value(const ValueT *values, float /*weight*/)
158 {
159 return values[0];
160 }
161
162 template<class ValueT> static ValueT sample_gradient(const ValueT * /*values*/, float /*weight*/)
163 {
164 return ValueT(0.0);
165 }
166};
167
171struct LinearKernel {
172 static constexpr int samples_left = 1;
173 static constexpr int samples_right = 1;
174 static constexpr float sample_offset = 0.0f;
175
176 static float weight(float x)
177 {
178 if (x < -1.0f) {
179 return 0.0f;
180 }
181 if (x < 0.0f) {
182 return x + 1.0f;
183 }
184 if (x < 1.0f) {
185 return -x + 1.0f;
186 }
187 return 0.0f;
188 }
189
190 static float derivative(float x)
191 {
192 if (x < -1.0f) {
193 return 0.0f;
194 }
195 if (x < 0.0f) {
196 return 1.0f;
197 }
198 if (x < 1.0f) {
199 return -1.0f;
200 }
201 return 0.0f;
202 }
203
204 template<class ValueT> static ValueT sample_value(const ValueT *values, float weight)
205 {
206 const ValueT lin = ValueT(values[1] - values[0]);
207 const ValueT con = ValueT(values[0]);
208 return ValueT(weight * lin) + con;
209 }
210
211 template<class ValueT> static ValueT sample_gradient(const ValueT *values, float /*weight*/)
212 {
213 const ValueT con = ValueT(values[1] - values[0]);
214 return con;
215 }
216};
217
249struct QuadraticBSplineKernel {
250 static constexpr int samples_left = 2;
251 static constexpr int samples_right = 1;
252 /* Shift the sampling interval to simplify interpolation. */
253 static constexpr float sample_offset = 0.5f;
254
255 static float weight(float x)
256 {
257 if (x < -1.5f) {
258 return 0.0f;
259 }
260 if (x < -0.5f) {
261 return (0.5f * x + 1.5f) * x + 1.125f;
262 }
263 if (x < 0.5f) {
264 return -x * x + 0.75f;
265 }
266 if (x < 1.5f) {
267 return (0.5f * x - 1.5f) * x + 1.125f;
268 }
269 return 0.0f;
270 }
271
272 static float derivative(float x)
273 {
274 if (x < -1.5f) {
275 return 0.0f;
276 }
277 if (x < -0.5f) {
278 return x + 1.5f;
279 }
280 if (x < 0.5f) {
281 return -2.0f * x;
282 }
283 if (x < 1.5f) {
284 return x - 1.5f;
285 }
286 return 0.0f;
287 }
288
289 template<class ValueT> static ValueT sample_value(const ValueT *values, float weight)
290 {
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;
295 }
296
297 template<class ValueT> static ValueT sample_gradient(const ValueT *values, float weight)
298 {
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;
302 }
303};
304
342struct CubicBSplineKernel {
343 static constexpr int samples_left = 2;
344 static constexpr int samples_right = 2;
345 static constexpr float sample_offset = 0.0f;
346
347 static float weight(float x)
348 {
349 if (x < -2.0f) {
350 return 0.0f;
351 }
352 if (x < -1.0f) {
353 return ((x / 6.0f + 1.0f) * x + 2.0f) * x + 4.0f / 3.0f;
354 }
355 if (x < 0.0f) {
356 return (-x * 0.5f - 1.0f) * x * x + 2.0f / 3.0f;
357 }
358 if (x < 1.0f) {
359 return (x * 0.5f - 1.0f) * x * x + 2.0f / 3.0f;
360 }
361 if (x < 2.0f) {
362 return ((-x / 6.0f + 1.0f) * x - 2.0f) * x + 4.0f / 3.0f;
363 }
364 return 0.0f;
365 }
366
367 static float derivative(float x)
368 {
369 if (x < -2.0f) {
370 return 0.0f;
371 }
372 if (x < -1.0f) {
373 return (0.5 * x + 2.0f) * x + 2.0f;
374 }
375 if (x < 0.0f) {
376 return (-1.5f * x - 2.0f) * x;
377 }
378 if (x < 1.0f) {
379 return (1.5f * x - 2.0f) * x;
380 }
381 if (x < 2.0f) {
382 return (-0.5 * x + 2.0f) * x - 2.0f;
383 }
384 return 0.0f;
385 }
386
387 template<class ValueT> static ValueT sample_value(const ValueT *values, float weight)
388 {
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;
395 }
396
397 template<class ValueT> static ValueT sample_gradient(const ValueT *values, float weight)
398 {
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;
403 }
404};
405
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)
414 {
415 return grid_sampling::sample_tree<KernelT>(accessor, coord, result);
416 }
417
418 template<class AccessorT>
419 static typename AccessorT::ValueType sample(const AccessorT &accessor,
420 const openvdb::Vec3R &coord)
421 {
422 return grid_sampling::sample_tree<KernelT>(accessor, coord);
423 }
424};
425
426} // namespace grid_sampling
427
431using NearestPointSampler = grid_sampling::SamplerWithKernel<grid_sampling::NearestPointKernel>;
432
436using LinearSampler = grid_sampling::SamplerWithKernel<grid_sampling::LinearKernel>;
437
441using QuadraticBSplineSampler =
442 grid_sampling::SamplerWithKernel<grid_sampling::QuadraticBSplineKernel>;
443
447using CubicBSplineSampler = grid_sampling::SamplerWithKernel<grid_sampling::CubicBSplineKernel>;
448
449#endif
450
451} // namespace blender::geometry
#define index(i)
#define N
BMesh const char void * data
#define sqr