issue_1109.py 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. import numpy as np
  2. from vedo import Volume, show
  3. # make up some fake data
  4. X, Y, Z = np.mgrid[:4, :4, :2]
  5. scalar_field = (X - 2) ** 2 + (Y - 2) ** 2 + (Z - 2) ** 2
  6. vol = Volume(scalar_field.astype(int))
  7. spacing = vol.spacing() # get the voxel size
  8. # print("spacing", spacing)
  9. # print('numpy array from Volume:', vol.pointdata)
  10. # print('input_scalars', vol.pointdata['input_scalars'])
  11. # extract a z-slice at index k=1
  12. zslice = vol.zslice(k=1)
  13. zslice.cmap("hot_r").lw(1).alpha(0.9).add_scalarbar3d()
  14. # print("input_scalars", zslice.pointdata["input_scalars"])
  15. # create a set of points at the cell centers and associate the scalar value
  16. # corresponding to the bottom left corner of each voxel
  17. cc = zslice.cell_centers().shift([-spacing[0] / 2, -spacing[1] / 2, 0])
  18. cc.resample_data_from(zslice)
  19. zslice.compute_normals()
  20. zslice2 = zslice.clone()
  21. zslice2.celldata["pixel_value"] = cc.pointdata["input_scalars"]
  22. print(zslice2.celldata["pixel_value"])
  23. lego = vol.legosurface(vmin=0, vmax=10).wireframe()
  24. show(
  25. [
  26. (vol, lego, vol.cell_centers()),
  27. (
  28. lego,
  29. cc,
  30. zslice,
  31. zslice.labels("id"),
  32. zslice.labels("cellid"),
  33. ),
  34. zslice2,
  35. ],
  36. N=3,
  37. axes=1,
  38. )