section.py 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. import numpy as np
  2. from vedo import show, Plane, printc, download, settings
  3. import trimesh
  4. settings.immediate_rendering = False
  5. # load the mesh from filename, file objects are also supported
  6. f = download('https://github.com/mikedh/trimesh/raw/main/models/featuretype.STL')
  7. mesh = trimesh.load_mesh(f)
  8. # get a single cross section of the mesh
  9. txt = 'cross section of the mesh'
  10. mslice = mesh.section(plane_origin=mesh.centroid, plane_normal=[0,0,1])
  11. pl = Plane(mesh.centroid, normal=[0,0,1], s=[6,4], c='green', alpha=0.3)
  12. slice_2D, to_3D = mslice.to_planar()
  13. # show objects on N=2 non-synced renderers:
  14. show([(mesh, pl), (slice_2D, txt)], N=2, sharecam=False, axes=7).close()
  15. # if we wanted to take a bunch of parallel slices, like for a 3D printer
  16. # we can do that easily with the section_multiplane method
  17. # we're going to slice the mesh into evenly spaced chunks along z
  18. # this takes the (2,3) bounding box and slices it into [minz, maxz]
  19. z_extents = mesh.bounds[:,2]
  20. # slice every .125 model units (eg, inches)
  21. z_levels = np.arange(*z_extents, step=0.125)
  22. # find a bunch of parallel cross sections
  23. sections = mesh.section_multiplane(plane_origin=mesh.bounds[0],
  24. plane_normal=[0,0,1],
  25. heights=z_levels)
  26. N = len(sections)
  27. printc("nr. of sections:", N, c='green')
  28. # summing the array of Path2D objects will put all of the curves
  29. # into one Path2D object, which we can plot easily
  30. combined = np.sum(sections)
  31. sections.append([combined, 'combined'])
  32. # show objects in N synced renderers:
  33. show(sections, N=N, axes=1, new=True).interactive().close()