tetgen1.py 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. """Use tetgenpy to tetrahedralize a cube."""
  2. try:
  3. import tetgenpy
  4. except ImportError:
  5. print("tetgenpy not installed, try: pip install tetgenpy")
  6. import vedo
  7. # Tetrahedralize unit cube, define points
  8. points = [
  9. [0.0, 0.0, 0.0],
  10. [1.0, 0.0, 0.0],
  11. [0.0, 1.0, 0.0],
  12. [1.0, 1.0, 0.0],
  13. [0.0, 0.0, 1.0],
  14. [1.0, 0.0, 1.0],
  15. [0.0, 1.0, 1.0],
  16. [1.0, 1.0, 1.0],
  17. ]
  18. # Define facets, here they are hexa faces
  19. facets = [
  20. [1, 0, 2, 3],
  21. [0, 1, 5, 4],
  22. [2, 0, 4, 6],
  23. [1, 3, 7, 5],
  24. [3, 2, 6, 7],
  25. [4, 5, 7, 6],
  26. ]
  27. # Prepare TetgenIO - input for tetgen
  28. tetgen_in = tetgenpy.TetgenIO()
  29. # Set points, facets, and facet_markers.
  30. # facet_markers can be useful for setting boundary conditions
  31. tetgen_in.setup_plc(
  32. points=points,
  33. facets=facets,
  34. facet_markers=[[i] for i in range(1, len(facets) + 1)],
  35. )
  36. # Tetgen's tetraheralize function with switches
  37. tetgen_out = tetgenpy.tetrahedralize("qa.05", tetgen_in)
  38. # Unpack output
  39. # print(tetgen_out.points())
  40. # print(tetgen_out.tetrahedra())
  41. # print(tetgen_out.trifaces())
  42. # print(tetgen_out.trifacemarkers())
  43. plt = vedo.Plotter().add_ambient_occlusion(0.1)
  44. tmesh = vedo.TetMesh(tetgen_out).shrink().color("pink7")
  45. plt.show(tmesh, __doc__, axes=14).close()
  46. # Or simply:
  47. # vedo.show(tetgen_out, axes=14).close()
  48. # Save to file
  49. # tmesh.write("tetramesh.vtu")