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