def load_sphere(): fname = os.path.join(DATA_DIR, 'sphere.stl') m = stl.mesh.Mesh.from_file(fname) # Flatten our vert array to Nx3 and generate corresponding faces array verts = m.vectors.reshape(-1, 3) faces = np.arange(len(verts)).reshape(-1, 3) verts, faces = meshcut.merge_close_vertices(verts, faces) return meshcut.TriangleMesh(verts, faces)
def load_human(): # TODO: This is ugly, make ply a submodule of meshcut (requires # some refactoring) print('examples :', EXAMPLES_DIR) sys.path.append(EXAMPLES_DIR) import ply fname = os.path.join(DATA_DIR, 'mesh.ply') with open(fname) as f: verts, faces, _ = ply.load_ply(f) return meshcut.TriangleMesh(verts, faces)
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 test_triangle_plane_intersection_edge_on_plane(): verts = [ (0, 0, 0), (0, 1, 0), (1, 0, 0), ] faces = [ (0, 1, 2), ] mesh = meshcut.TriangleMesh(verts, faces) plane = meshcut.Plane( orig=(0, 0, 0), normal=(0, 1, 0) ) intersections = meshcut.compute_triangle_plane_intersections( mesh, 0, plane) assert len(intersections) == 2
def test_triangle_plane_intersection_edge_edge(): verts = [ (0, 0, 0), (0, 1, 0), (1, 0, 0), ] faces = [ (0, 1, 2), ] mesh = meshcut.TriangleMesh(verts, faces) plane = meshcut.Plane( orig=(0, 0.5, 0), normal=(0, 1, 0) ) intersections = meshcut.compute_triangle_plane_intersections( mesh, 0, plane) assert len(intersections) == 2 assert intersections[0][0] == meshcut.INTERSECT_EDGE assert np.allclose(intersections[0][1][1], 0.5) assert intersections[1][0] == meshcut.INTERSECT_EDGE assert np.allclose(intersections[1][1][1], 0.5)
# Flatten our vert array to Nx3 and generate corresponding faces array verts = m.vectors.reshape(-1, 3) faces = np.arange(len(verts)).reshape(-1, 3) verts, faces = meshcut.merge_close_vertices(verts, faces) return verts, faces ## if __name__ == '__main__': ## example_fname = os.path.join('data', 'sphere.stl') verts, faces = load_stl(example_fname) mesh = meshcut.TriangleMesh(verts, faces) ## 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')
else: texcoords.append(0) if len(w) >= 3 and len(w[2]) > 0: norms.append(int(w[2])) else: norms.append(0) self.faces.append((face, norms, texcoords, material)) self normals = LoadNormals("C:\\Users\\omerm\\Downloads\\octahedron2.obj") model = OBJ("C:\\3D Project\\Santa Claus.obj") model.gl_list = glGenLists(1) newVectors = [] vectors = mesh.get_attribute("vertex_normal") mesh = meshcut.TriangleMesh(vectors, faces) """for vector in vectors: sqrsum = math.pow(vector.x, 2) + math.pow(vector.y, 2) + math.pow(vector.z, 2) sqrt = math.sqrt(sqrsum) newVector = (vector.x, vector.y, vector.z) if (sqrt != 1): newVector = (vector.x / sqrt, vector.y / sqrt, vector.z / sqrt) newVectors.append(newVector) offset = (0, 0.5, 0) for vector in newVectors: plane = meshcut.Plane((offset, vector)) textureSurface = pygame.image.load('pic.jpg') textureData = pygame.image.tostring(textureSurface, "RGBA", 1) hdc=windll.user32.GetDC(1) print(hdc)
def plot_2D(self): for seg in self.sliced: unrotated_seg = rotate(seg, [0, 0, 1], 2*np.pi - self.angle) pyplot.plot(unrotated_seg[:, 1], unrotated_seg[:, 2]) #Load and plot the pretty mesh with open(r"C:\Users\aaron\sfm\tranquilitatis\cross-sections\MTP_V2.ply") as f: display_mesh = ply.load_ply(f) # Draw the mesh with Mayavi utils.trimesh3d(verts=display_mesh[0], faces=display_mesh[1]) #Load the mesh for calculations with open(r"C:\Users\aaron\sfm\tranquilitatis\cross-sections\MTP_V2_print.ply") as f: display_mesh = ply.load_ply(f) mesh = meshcut.TriangleMesh(verts=display_mesh[0], tris=display_mesh[1]) #create all the cut planes xs_angles = np.arange(0, 2*np.pi, (2.0*np.pi)/10) slices = [] initial_subplot = pyplot.subplot(2, 5, 1) for n, xs_angle in enumerate(xs_angles): newcut = cutplane(angle=xs_angle, mesh=mesh) slices.append(newcut) pyplot.subplot(2, 5, n+1, sharex=initial_subplot, sharey=initial_subplot) newcut.plot_2D() pyplot.show() print('end') #mlab.show()
newNormals = [] hashDict = {} for normal in orc.normals: sqr = math.pow(normal[0], 2) + math.pow(normal[1], 2) + math.pow( normal[2], 2) norm = math.sqrt(sqr) normalTuple = (normal[0] / norm, normal[1] / norm, normal[2] / norm) if (normalTuple in hashDict or (-normalTuple[0], -normalTuple[1], -normalTuple[2]) in hashDict): continue hashDict[normalTuple] = 1 newNormals.append(normalTuple) # TODO: need to add here: trying a few origins for each plane - need to add measurements of boundries of the model # on a given vector direction so we can adjust the offset of the surface to be between the boundries orig = (0.2, 0, 0) mesh = meshcut.TriangleMesh(tuple(obj.vertices), tuple(obj.triangles)) mesh.normals = obj.normals i = 0 orig = [0, 0, 0] for normal in newNormals: for i in range(10): if normal[0] > 0: orig[0] = orig[0] + 0.05 elif normal[0] < 0: orig[0] = orig[0] - 0.05 if normal[1] > 0: orig[1] = orig[1] + 0.05 elif normal[1] < 0: orig[1] = orig[1] - 0.05