8 #ifndef __CSAMPLEFROMVOXELS__
9 #define __CSAMPLEFROMVOXELS__
11 #include "../CNanoVDB.h"
13 #ifdef __OPENCL_VERSION__
21 #ifdef __OPENCL_VERSION__
22 coord->mVec[0] =
floor(xyz->mVec[0]+0.5);
23 coord->mVec[1] =
floor(xyz->mVec[1]+0.5);
24 coord->mVec[2] =
floor(xyz->mVec[2]+0.5);
26 coord->mVec[0] = floorf(xyz->mVec[0]+0.5);
27 coord->mVec[1] = floorf(xyz->mVec[1]+0.5);
28 coord->mVec[2] = floorf(xyz->mVec[2]+0.5);
35 #ifdef __OPENCL_VERSION__
37 fraction->mVec[0] = fract(xyz->mVec[0], &i0);
39 fraction->mVec[1] = fract(xyz->mVec[1], &i1);
41 fraction->mVec[2] = fract(xyz->mVec[2], &i2);
45 i0 = floorf(xyz->mVec[0]);
46 fraction->mVec[0] = xyz->mVec[0] - i0;
48 i1 = floorf(xyz->mVec[1]);
49 fraction->mVec[1] = xyz->mVec[1] -
i1;
51 i2 = floorf(xyz->mVec[2]);
52 fraction->mVec[2] = xyz->mVec[2] -
i2;
57 #define CREATE_STENCIL(VALUETYPE, SUFFIX) \
60 VALUETYPE mStencil[2][2][2]; \
61 cnanovdb_coord mCoord; \
62 } cnanovdb_stencil1##SUFFIX; \
65 cnanovdb_stencil1##SUFFIX##_clear(cnanovdb_stencil1##SUFFIX *RESTRICT stencil) \
68 stencil->mCoord.mVec[0] = 0x80000000; \
69 stencil->mCoord.mVec[1] = 0x80000000; \
70 stencil->mCoord.mVec[2] = 0x80000000; \
74 cnanovdb_stencil1##SUFFIX##_fill(cnanovdb_stencil1##SUFFIX *RESTRICT stencil, cnanovdb_readaccessor *RESTRICT acc, cnanovdb_coord *RESTRICT coord) \
76 stencil->mStencil[0][0][0] = cnanovdb_readaccessor_getValue##SUFFIX(acc, coord); \
77 coord->mVec[2] += 1; \
78 stencil->mStencil[0][0][1] = cnanovdb_readaccessor_getValue##SUFFIX(acc, coord); \
79 coord->mVec[1] += 1; \
80 stencil->mStencil[0][1][1] = cnanovdb_readaccessor_getValue##SUFFIX(acc, coord); \
81 coord->mVec[2] -= 1; \
82 stencil->mStencil[0][1][0] = cnanovdb_readaccessor_getValue##SUFFIX(acc, coord); \
84 coord->mVec[0] += 1; \
85 stencil->mStencil[1][1][0] = cnanovdb_readaccessor_getValue##SUFFIX(acc, coord); \
86 coord->mVec[2] += 1; \
87 stencil->mStencil[1][1][1] = cnanovdb_readaccessor_getValue##SUFFIX(acc, coord); \
88 coord->mVec[1] -= 1; \
89 stencil->mStencil[1][0][1] = cnanovdb_readaccessor_getValue##SUFFIX(acc, coord); \
90 coord->mVec[2] -= 1; \
91 stencil->mStencil[1][0][0] = cnanovdb_readaccessor_getValue##SUFFIX(acc, coord); \
92 coord->mVec[0] -= 1; \
94 stencil->mCoord.mVec[0] = coord->mVec[0]; \
95 stencil->mCoord.mVec[1] = coord->mVec[1]; \
96 stencil->mCoord.mVec[2] = coord->mVec[2]; \
100 cnanovdb_stencil1##SUFFIX##_update(cnanovdb_stencil1##SUFFIX *RESTRICT stencil, cnanovdb_readaccessor *RESTRICT acc, cnanovdb_coord *RESTRICT coord) \
102 uint32_t change = (coord->mVec[0] ^ stencil->mCoord.mVec[0]) | \
103 (coord->mVec[1] ^ stencil->mCoord.mVec[1]) | \
104 (coord->mVec[2] ^ stencil->mCoord.mVec[2]); \
108 cnanovdb_stencil1##SUFFIX##_fill(stencil, acc, coord); \
115 #define CREATE_LERPSIMPLE(VALUETYPE, SUFFIX) \
117 cnanovdb_lerp##SUFFIX(VALUETYPE a, VALUETYPE b, float w) \
119 return a + w * (b - a); \
129 a.
mVec[0] = cnanovdb_lerpF(a.mVec[0], b.mVec[0], w);
130 a.mVec[1] = cnanovdb_lerpF(a.mVec[1], b.mVec[1], w);
131 a.mVec[2] = cnanovdb_lerpF(a.mVec[2], b.mVec[2], w);
135 #define CREATE_SAMPLE(VALUETYPE, SUFFIX) \
137 cnanovdb_sample##SUFFIX##_nearest(cnanovdb_readaccessor *RESTRICT acc, const cnanovdb_Vec3F *RESTRICT xyz) \
139 cnanovdb_coord coord; \
140 cnanovdb_coord_round(&coord, xyz); \
141 return cnanovdb_readaccessor_getValue##SUFFIX(acc, &coord); \
145 cnanovdb_sample##SUFFIX##_trilinear(cnanovdb_readaccessor *RESTRICT acc, const cnanovdb_Vec3F *RESTRICT xyz) \
147 cnanovdb_coord coord; \
148 cnanovdb_Vec3F fraction; \
149 cnanovdb_coord_fract(&coord, &fraction, xyz); \
151 VALUETYPE vx, vx1, vy, vy1, vz, vz1; \
153 vz = cnanovdb_readaccessor_getValue##SUFFIX(acc, &coord); \
154 coord.mVec[2] += 1; \
155 vz1 = cnanovdb_readaccessor_getValue##SUFFIX(acc, &coord); \
156 vy = cnanovdb_lerp##SUFFIX(vz, vz1, fraction.mVec[2]); \
158 coord.mVec[1] += 1; \
160 vz1 = cnanovdb_readaccessor_getValue##SUFFIX(acc, &coord); \
161 coord.mVec[2] -= 1; \
162 vz = cnanovdb_readaccessor_getValue##SUFFIX(acc, &coord); \
163 vy1 = cnanovdb_lerp##SUFFIX(vz, vz1, fraction.mVec[2]); \
165 vx = cnanovdb_lerp##SUFFIX(vy, vy1, fraction.mVec[1]); \
167 coord.mVec[0] += 1; \
169 vz = cnanovdb_readaccessor_getValue##SUFFIX(acc, &coord); \
170 coord.mVec[2] += 1; \
171 vz1 = cnanovdb_readaccessor_getValue##SUFFIX(acc, &coord); \
172 vy1 = cnanovdb_lerp##SUFFIX(vz, vz1, fraction.mVec[2]); \
174 coord.mVec[1] -= 1; \
176 vz1 = cnanovdb_readaccessor_getValue##SUFFIX(acc, &coord); \
177 coord.mVec[2] -= 1; \
178 vz = cnanovdb_readaccessor_getValue##SUFFIX(acc, &coord); \
179 vy = cnanovdb_lerp##SUFFIX(vz, vz1, fraction.mVec[2]); \
181 vx1 = cnanovdb_lerp##SUFFIX(vy, vy1, fraction.mVec[1]); \
183 return cnanovdb_lerp##SUFFIX(vx, vx1, fraction.mVec[0]); \
187 cnanovdb_sample##SUFFIX##_trilinear_stencil(cnanovdb_stencil1##SUFFIX *RESTRICT stencil, cnanovdb_readaccessor *RESTRICT acc, const cnanovdb_Vec3F *RESTRICT xyz) \
189 cnanovdb_coord coord; \
190 cnanovdb_Vec3F fraction; \
191 cnanovdb_coord_fract(&coord, &fraction, xyz); \
193 cnanovdb_stencil1##SUFFIX##_update(stencil, acc, &coord); \
195 VALUETYPE vx, vx1, vy, vy1, vz, vz1; \
197 vz = stencil->mStencil[0][0][0]; \
198 vz1 = stencil->mStencil[0][0][1]; \
199 vy = cnanovdb_lerp##SUFFIX(vz, vz1, fraction.mVec[2]); \
201 vz = stencil->mStencil[0][1][0]; \
202 vz1 = stencil->mStencil[0][1][1]; \
203 vy1 = cnanovdb_lerp##SUFFIX(vz, vz1, fraction.mVec[2]); \
205 vx = cnanovdb_lerp##SUFFIX(vy, vy1, fraction.mVec[1]); \
207 vz = stencil->mStencil[1][1][0]; \
208 vz1 = stencil->mStencil[1][1][1]; \
209 vy1 = cnanovdb_lerp##SUFFIX(vz, vz1, fraction.mVec[2]); \
211 vz = stencil->mStencil[1][0][0]; \
212 vz1 = stencil->mStencil[1][0][1]; \
213 vy = cnanovdb_lerp##SUFFIX(vz, vz1, fraction.mVec[2]); \
215 vx1 = cnanovdb_lerp##SUFFIX(vy, vy1, fraction.mVec[1]); \
217 return cnanovdb_lerp##SUFFIX(vx, vx1, fraction.mVec[0]); \
227 qxyz.
mVec[0] = xyz->mVec[0];
228 qxyz.
mVec[1] = xyz->mVec[1];
229 qxyz.
mVec[2] = xyz->mVec[2];
230 for (
int i = 0; i < 3; i++)
235 sm = cnanovdb_sampleF_trilinear(acc, &qxyz);
237 sp = cnanovdb_sampleF_trilinear(acc, &qxyz);
239 ret->mVec[i] = sp - sm;
247 cnanovdb_Vec3F fraction;
252 stencil[0][0][0] = cnanovdb_readaccessor_getValueF(acc, &coord);
254 stencil[0][0][1] = cnanovdb_readaccessor_getValueF(acc, &coord);
256 stencil[0][1][1] = cnanovdb_readaccessor_getValueF(acc, &coord);
258 stencil[0][1][0] = cnanovdb_readaccessor_getValueF(acc, &coord);
261 stencil[1][1][0] = cnanovdb_readaccessor_getValueF(acc, &coord);
263 stencil[1][1][1] = cnanovdb_readaccessor_getValueF(acc, &coord);
265 stencil[1][0][1] = cnanovdb_readaccessor_getValueF(acc, &coord);
267 stencil[1][0][0] = cnanovdb_readaccessor_getValueF(acc, &coord);
271 D[0] = stencil[0][0][1] - stencil[0][0][0];
272 D[1] = stencil[0][1][1] - stencil[0][1][0];
273 D[2] = stencil[1][0][1] - stencil[1][0][0];
274 D[3] = stencil[1][1][1] - stencil[1][1][0];
276 ret->mVec[2] = cnanovdb_lerpF(
277 cnanovdb_lerpF(D[0], D[1], fraction.
mVec[1]),
278 cnanovdb_lerpF(D[2], D[3], fraction.
mVec[1]),
281 float w = fraction.
mVec[2];
282 D[0] = stencil[0][0][0] + D[0] *
w;
283 D[1] = stencil[0][1][0] + D[1] *
w;
284 D[2] = stencil[1][0][0] + D[2] *
w;
285 D[3] = stencil[1][1][0] + D[3] *
w;
287 ret->mVec[0] = cnanovdb_lerpF(D[2], D[3], fraction.
mVec[1])
288 - cnanovdb_lerpF(D[0], D[1], fraction.
mVec[1]);
290 ret->mVec[1] = cnanovdb_lerpF(D[1] - D[0], D[3] - D[2], fraction.
mVec[0]);
297 cnanovdb_Vec3F fraction;
300 cnanovdb_stencil1F_update(stencil, acc, &coord);
304 D[0] = stencil->mStencil[0][0][1] - stencil->mStencil[0][0][0];
305 D[1] = stencil->mStencil[0][1][1] - stencil->mStencil[0][1][0];
306 D[2] = stencil->mStencil[1][0][1] - stencil->mStencil[1][0][0];
307 D[3] = stencil->mStencil[1][1][1] - stencil->mStencil[1][1][0];
309 ret->mVec[2] = cnanovdb_lerpF(
310 cnanovdb_lerpF(D[0], D[1], fraction.
mVec[1]),
311 cnanovdb_lerpF(D[2], D[3], fraction.
mVec[1]),
314 float w = fraction.
mVec[2];
315 D[0] = stencil->mStencil[0][0][0] + D[0] *
w;
316 D[1] = stencil->mStencil[0][1][0] + D[1] *
w;
317 D[2] = stencil->mStencil[1][0][0] + D[2] *
w;
318 D[3] = stencil->mStencil[1][1][0] + D[3] *
w;
320 ret->mVec[0] = cnanovdb_lerpF(D[2], D[3], fraction.
mVec[1])
321 - cnanovdb_lerpF(D[0], D[1], fraction.
mVec[1]);
323 ret->mVec[1] = cnanovdb_lerpF(D[1] - D[0], D[3] - D[2], fraction.
mVec[0]);
IMATH_HOSTDEVICE constexpr int floor(T x) IMATH_NOEXCEPT
GLboolean GLboolean GLboolean GLboolean a
#define CREATE_LERPSIMPLE(VALUETYPE, SUFFIX)
void cnanovdb_coord_round(cnanovdb_coord *RESTRICT coord, const cnanovdb_Vec3F *RESTRICT xyz)
cnanovdb_Vec3F cnanovdb_lerpF3(cnanovdb_Vec3F a, cnanovdb_Vec3F b, float w)
GLboolean GLboolean GLboolean b
#define CREATE_STENCIL(VALUETYPE, SUFFIX)
void cnanovdb_sampleF_gradient0_stencil(cnanovdb_Vec3F *RESTRICT ret, cnanovdb_stencil1F *RESTRICT stencil, cnanovdb_readaccessor *RESTRICT acc, const cnanovdb_Vec3F *RESTRICT xyz)
void cnanovdb_coord_fract(cnanovdb_coord *RESTRICT coord, cnanovdb_Vec3F *RESTRICT fraction, const cnanovdb_Vec3F *RESTRICT xyz)
void cnanovdb_sampleF_gradient(cnanovdb_Vec3F *RESTRICT ret, cnanovdb_readaccessor *RESTRICT acc, const cnanovdb_Vec3F *RESTRICT xyz)
void cnanovdb_sampleF_gradient0(cnanovdb_Vec3F *RESTRICT ret, cnanovdb_readaccessor *RESTRICT acc, const cnanovdb_Vec3F *RESTRICT xyz)
GLubyte GLubyte GLubyte GLubyte w
GLint GLfloat GLint stencil
#define CREATE_SAMPLE(VALUETYPE, SUFFIX)