isosurfaces2.py 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. """Isosurface extraction from a volume dataset
  2. with discrete values (labels)."""
  3. from vedo import *
  4. settings.default_font = "Antares"
  5. lut = build_lut(
  6. [
  7. [0, "lightyellow"],
  8. [1, "red8"],
  9. [2, "blue"],
  10. [3, "yellow"],
  11. [4, "orange"],
  12. [5, "cyan"],
  13. [6, "magenta"],
  14. [7, "white"],
  15. [8, "pink"],
  16. [9, "brown"],
  17. [10, "lightblue"],
  18. [11, "lightgreen"],
  19. [12, "red4"],
  20. ],
  21. interpolate=False,
  22. )
  23. # This dataset is a 3D volume of 64x64x64 voxels containing 12 "blobs"
  24. blobs = Volume(dataurl + "blobs.vti")
  25. box = blobs.box()
  26. isovalues = list(range(1, 13))
  27. iso_discrete = blobs.isosurface_discrete(
  28. isovalues,
  29. background_label=0,
  30. internal_boundaries=True,
  31. nsmooth=10,
  32. )
  33. iso_discrete.cmap(lut)
  34. separate_blobs = []
  35. txt_vols = []
  36. for i in isovalues:
  37. b = iso_discrete.clone().threshold(0, i - 0.5, i + 0.5, on="cells")
  38. b.cap().clean().compute_normals()
  39. b.color(i).alpha(0.1).wireframe().lighting("off")
  40. v = b.volume() / 1e3
  41. cm = b.center_of_mass()
  42. txt = Text3D(f"blob {i}\nvol={v:.2f}", c=i, justify="center", pos=cm)
  43. txt_vols.append(txt)
  44. separate_blobs.append(b)
  45. show([[iso_discrete, box, __doc__], [separate_blobs, txt_vols, box]], N=2, axes=1)