From ad4ee12b1c26abe6aec50385f6fedd101f9d7654 Mon Sep 17 00:00:00 2001 From: Jan Petykiewicz Date: Sun, 15 Mar 2020 22:17:29 -0700 Subject: [PATCH] add some util functions for dealing with boundary representations --- adze/util.py | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/adze/util.py b/adze/util.py index c7c14b7..ee179b8 100644 --- a/adze/util.py +++ b/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