tet_explode.py 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. """Segment a TetMesh with a custom scalar.
  2. Press q to make it explode"""
  3. from vedo import TetMesh, Plotter, Text2D, dataurl
  4. n = 20000
  5. f1 = 0.005 # control the tetras resolution
  6. f2 = 0.15 # control the nr of seeds
  7. tmesh = TetMesh(dataurl + "limb.vtu")
  8. surf = tmesh.tomesh(fill=False)
  9. txt = Text2D(__doc__, font="Brachium")
  10. # pick points on the surface and use subsample to make them uniform
  11. seeds = surf.clone().subsample(f2).ps(10).c("black")
  12. # assign to each tetrahedron the id of the closest seed point
  13. cids = []
  14. for p in tmesh.cell_centers().coordinates:
  15. cid = seeds.closest_point(p, return_point_id=True)
  16. cids.append(cid)
  17. tmesh.celldata["fragment"] = cids
  18. pieces = []
  19. for i in range(seeds.npoints):
  20. tc = tmesh.clone().threshold(name="fragment", above=i-0.1, below=i+0.1)
  21. mc = tc.tomesh(fill=False).color(i)
  22. pieces.append(mc)
  23. ############### animate
  24. plt = Plotter(size=(1200, 800), axes=1)
  25. plt.show(txt, pieces)
  26. for i in range(20):
  27. for pc in pieces:
  28. cm = pc.center_of_mass()
  29. pc.shift(cm / 25)
  30. txt.text(f"{__doc__}\n\nNr. of pieces = {seeds.npoints}")
  31. plt.render()
  32. plt.interactive().close()