def show(plane): P = meshcut.cross_section_mesh(mesh, plane) colors = [(0, 1, 1), (1, 0, 1), (0, 0, 1)] print("num contours : ", len(P)) if True: utils.trimesh3d(mesh.verts, mesh.tris, color=(1, 1, 1), opacity=0.5, representation='wireframe') utils.show_plane(plane.orig, plane.n, scale=1, color=(1, 0, 0), opacity=0.5) for p, color in zip(P, itertools.cycle(colors)): p = np.array(p) mlab.plot3d(p[:, 0], p[:, 1], p[:, 2], tube_radius=None, line_width=3.0, color=color) return P
def test_sphere(): mesh = load_sphere() plane_orig = (0, 0.7, 0) plane_norm = (0, 0.5, 0.5) plane = meshcut.Plane(plane_orig, plane_norm) p = meshcut.cross_section_mesh(mesh, plane) assert len(p) == 1 assert len(p[0]) > 10 plane_orig = (0, 0.75, 0) plane_norm = (0, 1, 0) plane = meshcut.Plane(plane_orig, plane_norm) p = meshcut.cross_section_mesh(mesh, plane) assert len(p) == 1 assert len(p[0]) > 10
def show(plane, expected_n_contours): P = meshcut.cross_section_mesh(mesh, plane) colors = [(0, 1, 1), (1, 0, 1), (0, 0, 1)] print("num contours : ", len(P), ' expected : ', expected_n_contours) if True: utils.trimesh3d(mesh.verts, mesh.tris, color=(1, 1, 1), opacity=0.5) utils.show_plane(plane.orig, plane.n, scale=1, color=(1, 0, 0), opacity=0.5) for p, color in zip(P, itertools.cycle(colors)): p = np.array(p) #utils.points3d(np.array(p), point_size=3, color=(1,1,1)) mlab.plot3d(p[:, 0], p[:, 1], p[:, 2], tube_radius=None, line_width=3.0, color=color) return P
def test_plane_not_axis_aligned(): mesh = load_human() plane_orig = (0.7, 0, 0) plane_norm = (0.2, 0.5, 0.3) plane = meshcut.Plane(plane_orig, plane_norm) p = meshcut.cross_section_mesh(mesh, plane) assert len(p) == 2
def __init__(self, angle, mesh, origin = [0, 0, 0], color=(0,0,0)): self.angle = angle self.mesh = mesh self.origin = origin self.color = color self.plane = meshcut.Plane(origin, self.get_normal()) self.sliced = meshcut.cross_section_mesh(mesh, self.plane)
def test_plane_aligned_with_edges(): mesh = load_human() # This will align the plane with some edges, so this is a good test # for vertices intersection handling plane_orig = (1.28380000591278076172, -0.12510000169277191162, 0) plane_norm = (1, 0, 0) plane = meshcut.Plane(plane_orig, plane_norm) p = meshcut.cross_section_mesh(mesh, plane) assert len(p) == 3
def test_plane_contain_edge(): """ Test with a plane that contains the bottom edge of the mesh """ # Flattened view # 1 is connected to 7 and 6 to 0 # # -- 1 ---- 3 ---- 5 ---- 7 -- # / | / | / | / | # | / | / | / | / # -- 0 ---- 2 ---- 4 ---- 6 -- # # Top view # 6 - 4 # | | # 0 - 2 # verts = [ (0, 0, 0), (0, 1, 0), (1, 0, 0), (1, 1, 0), (1, 0, 1), (1, 1, 1), (0, 0, 1), (0, 1, 1) ] faces = [ (0, 1, 3), (0, 3, 2), (2, 3, 5), (2, 5, 4), (4, 5, 7), (4, 7, 6), (6, 7, 1), (6, 1, 0), ] mesh = meshcut.TriangleMesh(verts, faces) plane_orig = verts[2] plane_orig = (plane_orig[0], plane_orig[1], plane_orig[2]) print('plane_orig', plane_orig) # Align exactly with the 0 - 2 - 4 line plane_norm = (0, 1, 0) plane = meshcut.Plane(plane_orig, plane_norm) p = meshcut.cross_section_mesh(mesh, plane) assert len(p) == 1 # We should have the four bottom points in our slice assert len(p[0]) == 4 assert np.all(p[0][:, 1] == 0)
def test_plane_triangles_common_edge_on_plane(): """ Test with a plane that contains the middle edge of triangles """ # Flattened view # 1 is connected to 8 and 10 # # 2 5 8 # / \ / \ / \ # -1-----3----6----- (back to 1) # \ / \ / \ / # 4 7 10 # # Top view # 1 - 3 # | / # 6 verts = [ (0, 0, 0), (0, 1, 0), (1, 0, 0), (1, 1, 0), (1, 0, 1), (1, 1, 1), (0, 0, 1), (0, 1, 1) ] faces = [ (0, 1, 3), (0, 3, 2), (2, 3, 5), (2, 5, 4), (4, 5, 7), (4, 7, 6), (6, 7, 1), (6, 1, 0), ] mesh = meshcut.TriangleMesh(verts, faces) plane_orig = verts[2] plane_orig = (plane_orig[0], plane_orig[1], plane_orig[2]) print('plane_orig', plane_orig) # Align exactly with the 0 - 2 - 4 line plane_norm = (0, 1, 0) plane = meshcut.Plane(plane_orig, plane_norm) p = meshcut.cross_section_mesh(mesh, plane) assert len(p) == 1 # We should have the four bottom points in our slice assert len(p[0]) == 4 assert np.all(p[0][:, 1] == 0)
def fs2d_plot_data(self, plot_type="mpl"): import meshcut plane_orig = self._plane_orig plane_norm = self._plane_norm plane = meshcut.Plane(plane_orig, plane_norm) if plot_type == "mayavi": mlab.figure(figure=None, bgcolor=(1, 1, 1), size=(800, 800)) elif plot_type == "mpl": fig = plt.figure() ax = plt.axes(projection="3d") for surface in self._fs._iso_surface: verts = surface[0] faces = surface[1] mesh = meshcut.TriangleMesh(verts, faces) P = meshcut.cross_section_mesh(mesh, plane) for p in P: p = np.array(p) if plot_type == "mayavi": mlab.plot3d( p[:, 0], p[:, 1], p[:, 2], tube_radius=None, line_width=3.0, color=(0.0, 0.0, 0.0), ) elif plot_type == "mpl": ax.plot3D(p[:, 0], p[:, 1], p[:, 2], color="k") if plot_type == "mayavi": mlab.show() elif plot_type == "mpl": ax.set_xticks([]) ax.set_yticks([]) plt.show()
def show(verts, faces, plane, show_mesh): mesh = meshcut.TriangleMesh(verts, faces) P = meshcut.cross_section_mesh(mesh, plane) if show_mesh: trimesh3d(mesh.verts, mesh.tris, color=(0.8, 0.4, 0.2), opacity=1.0) for p in P: p = np.array(p) mlab.plot3d(p[:, 0], p[:, 1], p[:, 2], tube_radius=None, line_width=3.0, color=(0, 0, 1)) return P
def test_plane_contain_edge(): """ Test with a plane that contains the bottom edge of the mesh """ # Flattened view # 1 is connected to 7 and 6 to 0 # # -- 1 ---- 3 ---- 5 ---- 7 -- # / | / | / | / | # | / | / | / | / # -- 0 ---- 2 ---- 4 ---- 6 -- # # Top view # 6 - 4 # | | # 0 - 2 # verts = [(0, 0, 0), (0, 1, 0), (1, 0, 0), (1, 1, 0), (1, 0, 1), (1, 1, 1), (0, 0, 1), (0, 1, 1)] faces = [ (0, 1, 3), (0, 3, 2), (2, 3, 5), (2, 5, 4), (4, 5, 7), (4, 7, 6), (6, 7, 1), (6, 1, 0), ] mesh = meshcut.TriangleMesh(verts, faces) plane_orig = verts[2] plane_orig = (plane_orig[0], plane_orig[1], plane_orig[2]) print('plane_orig', plane_orig) # Align exactly with the 0 - 2 - 4 line plane_norm = (0, 1, 0) plane = meshcut.Plane(plane_orig, plane_norm) p = meshcut.cross_section_mesh(mesh, plane) assert len(p) == 1 # We should have the four bottom points in our slice assert len(p[0]) == 4 assert np.all(p[0][:, 1] == 0)
def test_plane_triangles_common_edge_on_plane(): """ Test with a plane that contains the middle edge of triangles """ # Flattened view # 1 is connected to 8 and 10 # # 2 5 8 # / \ / \ / \ # -1-----3----6----- (back to 1) # \ / \ / \ / # 4 7 10 # # Top view # 1 - 3 # | / # 6 verts = [(0, 0, 0), (0, 1, 0), (1, 0, 0), (1, 1, 0), (1, 0, 1), (1, 1, 1), (0, 0, 1), (0, 1, 1)] faces = [ (0, 1, 3), (0, 3, 2), (2, 3, 5), (2, 5, 4), (4, 5, 7), (4, 7, 6), (6, 7, 1), (6, 1, 0), ] mesh = meshcut.TriangleMesh(verts, faces) plane_orig = verts[2] plane_orig = (plane_orig[0], plane_orig[1], plane_orig[2]) print('plane_orig', plane_orig) # Align exactly with the 0 - 2 - 4 line plane_norm = (0, 1, 0) plane = meshcut.Plane(plane_orig, plane_norm) p = meshcut.cross_section_mesh(mesh, plane) assert len(p) == 1 # We should have the four bottom points in our slice assert len(p[0]) == 4 assert np.all(p[0][:, 1] == 0)
def show(plane, expected_n_contours): P = meshcut.cross_section_mesh(mesh, plane) colors = [ (0, 1, 1), (1, 0, 1), (0, 0, 1) ] print("num contours : ", len(P), ' expected : ', expected_n_contours) if True: utils.trimesh3d(mesh.verts, mesh.tris, color=(1, 1, 1), opacity=0.5) utils.show_plane(plane.orig, plane.n, scale=1, color=(1, 0, 0), opacity=0.5) for p, color in zip(P, itertools.cycle(colors)): p = np.array(p) mlab.plot3d(p[:, 0], p[:, 1], p[:, 2], tube_radius=None, line_width=3.0, color=color) return P