add some util functions for dealing with boundary representations
This commit is contained in:
parent
e497895ae3
commit
ad4ee12b1c
1 changed files with 43 additions and 0 deletions
43
adze/util.py
43
adze/util.py
|
|
@ -270,3 +270,46 @@ def check_poly(poly: poly_t, check_connectivity: bool = True):
|
|||
# Make sure we touched all vertices in the component
|
||||
if not visited[components == part_id].all():
|
||||
raise Exception('Vertex graph is not 3-vertex-connected')
|
||||
|
||||
|
||||
def plot_brep(brep):
|
||||
"""
|
||||
Plot a boundary representation
|
||||
"""
|
||||
from matplotlib import pyplot
|
||||
from mpl_toolkits import mplot3d
|
||||
|
||||
meshes = brep_to_stls(brep)
|
||||
|
||||
figure = pyplot.figure()
|
||||
axes = mplot3d.Axes3D(figure)
|
||||
scales = []
|
||||
for mesh in meshes:
|
||||
coll = mplot3d.art3d.Poly3DCollection(mesh.vectors)
|
||||
coll.set_edgecolors('black')
|
||||
axes.add_collection3d(coll)
|
||||
scales.append(mesh.points.flatten())
|
||||
scale = numpy.hstack(scales)
|
||||
axes.auto_scale_xyz(scale, scale, scale)
|
||||
pyplot.show()
|
||||
|
||||
|
||||
def brep_to_stls(brep):
|
||||
import stl
|
||||
|
||||
meshes = []
|
||||
for component in brep:
|
||||
triangles = []
|
||||
for face in component['faces']:
|
||||
for i in range(1, len(face) - 1):
|
||||
triangles.append([face[0], face[i], face[i+1]])
|
||||
triangles = numpy.array(triangles, dtype=int)
|
||||
|
||||
m = stl.mesh.Mesh(numpy.empty(triangles.shape[0], dtype=stl.mesh.Mesh.dtype))
|
||||
for i, f in enumerate(triangles):
|
||||
for j in range(3):
|
||||
m.vectors[i, j] = component['vertices'][f[j], :]
|
||||
|
||||
# m.save('pyr.stl')
|
||||
meshes.append(m)
|
||||
return meshes
|
||||
|
|
|
|||
Loading…
Reference in a new issue