radius = 5 origin = (0., 0., 0.) count = 0 points = [] while count < 10: x = (random.random() - 0.5) * radius * 2 y = (random.random() - 0.5) * radius * 2 z = (random.random() - 0.5) * radius * 2 pt = x, y, z if distance_point_point(origin, pt) <= radius: points.append(pt) count += 1 vertices, faces = convex_hull_numpy(points) i_index = {i: index for index, i in enumerate(vertices)} mesh = Mesh.from_vertices_and_faces( [points[index] for index in vertices], [[i_index[i] for i in face] for face in faces[1:]] ) mesh_unify_cycles(mesh) viewer = MeshViewer(mesh) viewer.setup() viewer.show()
stl = STL(filepath, precision='6f') mesh = Mesh.from_vertices_and_faces(stl.parser.vertices, stl.parser.faces) vertexgroups = connected_components(mesh.halfedge) facegroups = [[] for _ in range(len(vertexgroups))] vertexsets = list(map(set, vertexgroups)) for fkey in mesh.faces(): vertices = set(mesh.face_vertices(fkey)) for i, vertexset in enumerate(vertexsets): if vertices.issubset(vertexset): facegroups[i].append(fkey) break meshes = [] for vertexgroup, facegroup in zip(vertexgroups, facegroups): key_index = {key: index for index, key in enumerate(vertexgroup)} vertices = mesh.get_vertices_attributes('xyz', keys=vertexgroup) faces = [[key_index[key] for key in mesh.face_vertices(fkey)] for fkey in facegroup] meshes.append(Mesh.from_vertices_and_faces(vertices, faces)) viewer = MeshViewer() viewer.mesh = meshes[0] viewer.show()
i_index = {i: index for index, i in enumerate(vertices)} vertices = [points[index] for index in vertices] faces = [[i_index[i] for i in face] for face in faces] if unify: faces = unify_cycles(vertices, faces) return vertices, faces # ============================================================================== # Main # ============================================================================== if __name__ == '__main__': import compas_assembly from compas.datastructures import Mesh from compas.viewers import MeshViewer from compas_assembly.datastructures import Assembly assembly = Assembly.from_json(compas_assembly.get('assembly.json')) vertices, faces = assembly_hull_numpy(assembly) hull = Mesh.from_vertices_and_faces(vertices, faces) viewer = MeshViewer() viewer.mesh = hull viewer.show()
points = [] while count < 1000: x = (random.random() - 0.5) * radius * 2 y = (random.random() - 0.5) * radius * 2 z = (random.random() - 0.5) * radius * 2 pt = x, y, z if distance_point_point(origin, pt) <= radius: points.append(pt) count += 1 vertices, faces = convex_hull_numpy(points) i_index = {i: index for index, i in enumerate(vertices)} vertices = [points[index] for index in vertices] faces = [[i_index[i] for i in face] for face in faces] # faces = unify_cycles(vertices, faces) mesh = Mesh.from_vertices_and_faces(vertices, faces) mesh_unify_cycles(mesh) # mesh_flip_cycles(mesh) viewer = MeshViewer() viewer.mesh = mesh viewer.show()
count = 0 points = [] while count < 1000: x = (random.random() - 0.5) * radius * 2 y = (random.random() - 0.5) * radius * 2 z = (random.random() - 0.5) * radius * 2 pt = x, y, z if distance_point_point(origin, pt) <= radius: points.append(pt) count += 1 vertices, faces = convex_hull_numpy(points) i_index = {i: index for index, i in enumerate(vertices)} vertices = [points[index] for index in vertices] faces = [[i_index[i] for i in face] for face in faces] faces = unify_cycles(vertices, faces) mesh = Mesh.from_vertices_and_faces(vertices, faces) viewer = MeshViewer(mesh) viewer.axes_on = False viewer.grid_on = False viewer.setup() viewer.show()
subd.add_face([uv, vw, wu]) del subd.face[fkey] return subd # ============================================================================== # Main # ============================================================================== if __name__ == "__main__": from functools import partial import compas from compas.datastructures import Mesh from compas.utilities import print_profile from compas.viewers import MeshViewer mesh = Mesh.from_polyhedron(6) # fixed = [mesh.get_any_vertex()] # print(fixed) subdivide = partial(mesh_subdivide_catmullclark) subd = subdivide(mesh, k=4) viewer = MeshViewer() viewer.mesh = subd viewer.show()
for u, v in mesh.edges_on_boundary(): mesh.edge[u][v]['q'] = 10.0 # extract the structural data required for form finding # run the force density method # extract the updated coordinates from the result key_index = mesh.key_index() xyz = mesh.get_vertices_attributes('xyz') loads = mesh.get_vertices_attributes(('px', 'py', 'pz')) fixed = [key_index[key] for key in mesh.vertices_where({'is_anchor': True})] edges = mesh.indexed_edges() q = mesh.get_edges_attribute('q') res = fd_numpy(xyz, edges, fixed, q, loads) xyz = res[0] # update the mesh coordinates # and display the result for index, (key, attr) in enumerate(mesh.vertices(True)): attr['x'] = xyz[index][0] attr['y'] = xyz[index][1] attr['z'] = xyz[index][2] viewer = MeshViewer(mesh, 800, 600) viewer.setup() viewer.camera.zoom_in(5) viewer.show()