warp.volume_sample#
- warp.volume_sample( ) Any#
Kernel
Differentiable
Sample the volume of type
dtypegiven byidat the index-space pointuvw.uvwis expressed in index space (voxel coordinates) and may be fractional; convert a world-space position first withvolume_world_to_index().sampling_modemust bewarp.Volume.CLOSESTorwarp.Volume.LINEAR.CLOSESTrounds each coordinate to the nearest voxel. Currently, exact halfway cases are rounded away from zero. UseCLOSESTfor integer data;LINEARperforms trilinear interpolation for floating-point scalar and vector data.dtypemust match the volume’s stored value type.For floating-point scalar and vector data,
LINEARsampling is differentiable with respect touvwaway from integer voxel planes. The field is not generally differentiable at those planes. Currently, the derivative comes from the cell on the positive side, but callers should not rely on that behavior. The derivative is zero forCLOSEST. Currently, gradients are not propagated to the stored voxel values. To read values held in a separate array, usevolume_sample_index(). Seewarp.Volumeand the Volume sampling user-guide examples.- Parameters:
id – The
idof awarp.Volumeto sample.uvw – Sampling location in index space (voxel coordinates); may be fractional.
sampling_mode –
warp.Volume.CLOSESTorwarp.Volume.LINEAR; useCLOSESTfor integer data.dtype – Value type stored by the volume.
dtypemust be one ofint32,int64,uint32,float32,float64,warp.vec3f,warp.vec3d,warp.vec4f, orwarp.vec4d.
- Returns:
The sampled value of type
dtype. Locations without a stored value use the volume’s background value.
Example
@wp.kernel def sample(vid: wp.uint64, out: wp.array[wp.float32]): p = wp.volume_world_to_index(vid, wp.vec3(0.5, 0.0, 0.0)) out[0] = wp.volume_sample(vid, p, wp.Volume.LINEAR, dtype=float) 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(1, dtype=wp.float32) wp.launch(sample, dim=1, inputs=[volume.id], outputs=[out]) print(round(float(out.numpy()[0]), 1))
0.5