interpolate_volume.py 1.4 KB

12345678910111213141516171819202122232425262728293031323334
  1. """Generate a Volume by interpolating a scalar
  2. which is only known on a scattered set of points or mesh.
  3. The blue layer is the result of thresholding the volume
  4. between 0.3 and 0.4 and assigning it the new value 0.9 (blue)"""
  5. from vedo import Points, show
  6. from vedo.pyplot import CornerHistogram
  7. import numpy as np
  8. npts = 500 # nr. of points where the scalar value is known
  9. coords = np.random.rand(npts, 3) # range is [0, 1]
  10. scals = coords[:, 2] # let the scalar be the z of the point itself
  11. pts = Points(coords)
  12. pts.pointdata["scals"] = scals
  13. # Now interpolate the values at these points to the full Volume
  14. # available interpolation kernels are: shepard, gaussian, voronoi, linear.
  15. vol = pts.tovolume(kernel='shepard', n=4, dims=(90,90,90))
  16. vol.cmap(["maroon","g","b"]) # set color transfer function
  17. vol.alpha([0.3, 0.9]) # set opacity transfer function
  18. #vol.alpha([(0.3,0.3), (0.9,0.9)]) # alternative way, by specifying (xscalar, alpha)
  19. vol.alpha_unit(0.5) # make the whole object less transparent (default is 1)
  20. # replace voxels of specific range with a new value
  21. vol.threshold(above=0.3, below=0.4, replace=0.9)
  22. # Note that scalar range now has changed (you may want to reapply vol.c().alpha())
  23. ch = CornerHistogram(vol, pos="bottom-left")
  24. vol.add_scalarbar3d('Height is the voxel scalar', size=[None,1])
  25. vol.scalarbar.rotate_x(90).pos(0,1,0)
  26. show(pts, vol, ch, __doc__, axes=1, elevation=-90).close()