warp.volume_sample_grad#

warp.volume_sample_grad(
id: uint64,
uvw: vec3f,
sampling_mode: int32,
grad: Any,
dtype: Any,
) Any#
  • Kernel

  • Differentiable

Sample the volume of type dtype given by id and its spatial gradient at the index-space point uvw.

Behaves like volume_sample(), additionally writing the gradient of the sampled value with respect to the index-space coordinates uvw into grad. For a scalar dtype, grad is a length-three vector with the same scalar type. For warp.vec3f and warp.vec3d, it is a 3-by-3 Jacobian matrix with one row per value component. Four-component vector data is not supported by this function.

For floating-point scalar and vector data under warp.Volume.LINEAR, this is the gradient of the trilinear interpolant. At integer voxel planes, where that interpolant is generally not differentiable, the gradient comes from the cell on the positive side. Under warp.Volume.CLOSEST the gradient is zero; use CLOSEST for integer data. The gradient is with respect to index-space coordinates, not world space.

Parameters:
  • id – The id of a warp.Volume to sample.

  • uvw – Sampling location in index space (voxel coordinates); may be fractional.

  • sampling_modewarp.Volume.CLOSEST or warp.Volume.LINEAR; use CLOSEST for integer data.

  • grad – Output gradient of the sampled value with respect to uvw (see above for its shape).

  • dtype – Value type stored by the volume (see volume_sample()).

Returns:

The sampled value of type dtype.

Example

@wp.kernel
def sample_grad(vid: wp.uint64, out: wp.array[wp.float32]):
    grad = wp.vec3()
    out[0] = wp.volume_sample_grad(vid, wp.vec3(0.5, 0.0, 0.0), wp.Volume.LINEAR, grad, dtype=float)
    out[1] = grad[0]

values = np.zeros((2, 2, 2), dtype=np.float32)
values[1, :, :] = 1.0  # f(i, j, k) = i
volume = wp.Volume.load_from_numpy(values, voxel_size=1.0, bg_value=0.0)
out = wp.zeros(2, dtype=wp.float32)
wp.launch(sample_grad, dim=1, inputs=[volume.id], outputs=[out])
print(round(float(out.numpy()[0]), 1), round(float(out.numpy()[1]), 1))
0.5 1.0