warp.volume_sample_grad_index#
- warp.volume_sample_grad_index( ) Any#
Kernel
Differentiable
Sample the volume given by
idand its spatial gradient at the index-space pointuvw, reading voxel values from a separatevoxel_dataarray.Like
volume_sample_index(), but also writes the gradient of the sampled value with respect to the index-space coordinatesuvwintograd. For scalar data,gradis a length-three vector with the same scalar type. Forwarp.vec3fandwarp.vec3ddata, 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, the function is differentiable with respect touvw,voxel_data, andbackground. At integer voxel planes, the gradient and reverse-mode derivative with respect touvwcome from the cell on the positive side. Underwarp.Volume.CLOSEST,gradand the derivative with respect touvware zero; useCLOSESTfor integer data.- Parameters:
id – The
idof awarp.Volumeproviding the topology and voxel indices.uvw – Sampling location in index space (voxel coordinates); may be fractional.
sampling_mode –
warp.Volume.CLOSESTorwarp.Volume.LINEAR; useCLOSESTfor integer data.voxel_data – Per-voxel values indexed by each voxel’s linear index; shares the dtype of
background. Seevolume_sample_index()for sizing requirements.background – Value used for inactive voxels on
OnIndexandOnIndexMaskgrids and outside allocated leaves onIndexandIndexMaskgrids and classical value grids; its dtype must matchvoxel_data.grad – Output gradient of the sampled value with respect to
uvw.
- Returns:
The sampled value, of the same dtype as
voxel_data.
Example
@wp.kernel def fill(vid: wp.uint64, d: wp.array[wp.float32]): i, j, k = wp.tid() idx = wp.volume_lookup_index(vid, i, j, k) if idx >= 0: d[idx] = wp.float32(i) * 10.0 @wp.kernel def sample_grad(vid: wp.uint64, d: wp.array[wp.float32], out: wp.array[wp.float32]): grad = wp.vec3() out[0] = wp.volume_sample_grad_index(vid, wp.vec3(0.5, 0.0, 0.0), wp.Volume.LINEAR, d, 0.0, grad) out[1] = grad[0] voxels = wp.array([[0, 0, 0], [1, 0, 0]], dtype=wp.vec3i) volume = wp.Volume.allocate_by_voxels(voxels, voxel_size=1.0) data = wp.zeros(volume.get_voxel_count(), dtype=wp.float32) out = wp.zeros(2, dtype=wp.float32) wp.launch(fill, dim=(2, 1, 1), inputs=[volume.id, data]) wp.launch(sample_grad, dim=1, inputs=[volume.id, data], outputs=[out]) print(round(float(out.numpy()[0]), 1), round(float(out.numpy()[1]), 1))
5.0 10.0