def test_binary_read_write_fidelity(): mesh = Mesh.from_stl(compas.get('cube_binary.stl')) fp = compas.get('cube_binary_2.stl') mesh.to_stl(fp, binary=True) mesh_2 = Mesh.from_stl(fp) assert mesh.adjacency == mesh_2.adjacency assert mesh.vertex == mesh_2.vertex
def test_from_stl(): mesh = Mesh.from_stl(compas.get('cube_ascii.stl')) assert mesh.number_of_faces() == 8016 assert mesh.number_of_vertices() == 4020 assert mesh.number_of_edges() == 11368 mesh = Mesh.from_stl(compas.get('cube_binary.stl')) assert mesh.number_of_faces() == 12 assert mesh.number_of_vertices() == 8 assert mesh.number_of_edges() == 18
def read_mesh_from_filename(self, filename, meshcls): if not os.path.isfile(filename): raise FileNotFoundError("No such file: '%s'" % filename) extension = filename[(filename.rfind(".") + 1):] if extension == "dae": # no dae support yet #mesh = Mesh.from_dae(filename) obj_filename = filename.replace(".dae", ".obj") if os.path.isfile(obj_filename): mesh = Mesh.from_obj(obj_filename) # former DAE files have yaxis and zaxis swapped # TODO: already fix in conversion to obj frame = Frame([0,0,0], [1,0,0], [0,0,1]) T = Transformation.from_frame(frame) mesh_transform(mesh, T) else: raise FileNotFoundError("Please convert '%s' into an OBJ file, \ since DAE is currently not supported \ yet." % filename) elif extension == "obj": mesh = Mesh.from_obj(filename) elif extension == "stl": mesh = Mesh.from_stl(filename) else: raise ValueError("%s file types not yet supported" % extension.upper()) return meshcls(mesh)
def test_insert_vertex(): mesh = Mesh.from_stl(compas.get('cube_binary.stl')) v = mesh.number_of_vertices() f = mesh.number_of_faces() e = mesh.number_of_edges() mesh.insert_vertex(0) assert mesh.number_of_vertices() == v + 1 assert mesh.number_of_faces() == f + 2 assert mesh.number_of_edges() == e + 3
def test_area(): mesh = Mesh.from_obj(compas.get('faces.obj')) assert mesh.area() == 100 mesh = Mesh.from_stl(compas.get('cube_binary.stl')) assert mesh.area() == 6 mesh = Mesh.from_obj(compas.get('quadmesh.obj')) assert mesh.area() == 22.802429316496635
def test_normal(): mesh = Mesh.from_obj(compas.get('faces.obj')) assert mesh.normal() == [0.0, 0.0, 1.0] mesh = Mesh.from_stl(compas.get('cube_binary.stl')) assert mesh.normal() == [0.0, 0.0, 0.0] mesh = Mesh.from_obj(compas.get('quadmesh.obj')) assert mesh.normal() == [-2.380849234996509e-06, 4.1056122145028854e-05, 0.8077953732329284]
def test_centroid(): mesh = Mesh.from_obj(compas.get('faces.obj')) assert mesh.centroid() == [5.0, 5.0, 0.0] mesh = Mesh.from_stl(compas.get('cube_binary.stl')) assert mesh.centroid() == [0.0, 0.0, 0.5] mesh = Mesh.from_obj(compas.get('quadmesh.obj')) assert mesh.centroid() == [2.508081952064351, 2.554046390557884, 1.2687133268242006]
def test_delete_vertex(): mesh = Mesh.from_stl(compas.get('cube_binary.stl')) n = mesh.number_of_vertices() fn = mesh.number_of_faces() en = mesh.number_of_edges() mesh.delete_vertex(0) assert mesh.number_of_vertices() == n - 1 assert mesh.number_of_faces() == fn - 4 assert mesh.number_of_edges() == en - 4
def test_insert_vertex(): mesh = Mesh.from_stl(compas.get('cube_binary.stl')) n = mesh.number_of_vertices() fn = mesh.number_of_faces() en = mesh.number_of_edges() mesh.insert_vertex(0) assert mesh.number_of_vertices() == n + 1 assert mesh.number_of_faces() == fn + 2 assert mesh.number_of_edges() == en + 3
def test_is_valid(): mesh = Mesh.from_stl(compas.get('cube_binary.stl')) assert mesh.is_valid() mesh_test = mesh.copy() del mesh_test.face[0] assert not mesh_test.is_valid() mesh_test = mesh.copy() del mesh_test.vertex[0] assert not mesh_test.is_valid()
def parse_collision_mesh_from_path(dir_path, filename, scale=1e-3): file_path = os.path.join(dir_path, filename) obj_name = filename.split('.')[0] if filename.endswith('.obj'): mesh = Mesh.from_obj(file_path) elif filename.endswith('.stl'): mesh = Mesh.from_stl(file_path) else: return None cm = CollisionMesh(mesh, obj_name) cm.scale(scale) return cm
def _mesh_import(name, file): """Internal function to load meshes using the correct loader. Name and file might be the same but not always, e.g. temp files.""" file_extension = _get_file_format(name) if file_extension not in SUPPORTED_FORMATS: raise NotImplementedError( 'Mesh type not supported: {}'.format(file_extension)) if file_extension == 'obj': return Mesh.from_obj(file) elif file_extension == 'stl': return Mesh.from_stl(file) elif file_extension == 'ply': return Mesh.from_ply(file) raise Exception
def test_slicer(): FILE = os.path.join(HERE, '../..', 'data', '3DBenchy.stl') # ============================================================================== # Get benchy and construct a mesh # ============================================================================== benchy = Mesh.from_stl(FILE) # ============================================================================== # Create planes # ============================================================================== # replace by planes along a curve bbox = benchy.bounding_box() x, y, z = zip(*bbox) zmin, zmax = min(z), max(z) normal = Vector(0, 0, 1) planes = [] for i in np.linspace(zmin, zmax, 50): plane = Plane(Point(0, 0, i), normal) planes.append(plane) # ============================================================================== # Slice # ============================================================================== M = benchy.to_vertices_and_faces() pointsets = slice_mesh(M, planes) # ============================================================================== # Process output # ============================================================================== polylines = [] for points in pointsets: points = [Point(*point) for point in points] polyline = Polyline(points) polylines.append(polyline)
def main(): ##################################### ### NOTE: Run this file in Rhino. ### ##################################### ### --- Load stl mesh = Mesh.from_stl(FILE) ### --- Get color list color_list = get_mesh_face_color_overhang(mesh, max_angle=85, mode="adaptive", infill=False) ### --- Create Rhino artist artist = MeshArtist(mesh, layer='COMPAS::MeshArtist') artist.clear_layer() artist.draw_faces( color={key: color_list[i] for i, key in enumerate(mesh.faces())}) artist.redraw()
def _mesh_import(url, filename): """Internal function to load meshes using the correct loader. Name and file might be the same but not always, e.g. temp files.""" file_extension = _get_file_format(url) if file_extension not in SUPPORTED_FORMATS: raise NotImplementedError( 'Mesh type not supported: {}'.format(file_extension)) print(filename) if file_extension == "dae": # no dae support yet #mesh = Mesh.from_dae(filename) obj_filename = filename.replace(".dae", ".obj") if os.path.isfile(obj_filename): mesh = Mesh.from_obj(obj_filename) # former DAE files have yaxis and zaxis swapped # TODO: already fix in conversion to obj frame = Frame([0,0,0], [1,0,0], [0,0,1]) T = Transformation.from_frame(frame) mesh_transform(mesh, T) return mesh else: raise FileNotFoundError("Please convert '%s' into an OBJ file, \ since DAE is currently not supported \ yet." % filename) if file_extension == 'obj': return Mesh.from_obj(filename) elif file_extension == 'stl': return Mesh.from_stl(filename) elif file_extension == 'ply': return Mesh.from_ply(filename) raise Exception
import time from compas.datastructures import Mesh import compas_fab from compas_fab.backends import RosClient from compas_fab.robots import CollisionMesh from compas_fab.robots import PlanningScene with RosClient('localhost') as client: robot = client.load_robot() scene = PlanningScene(robot) mesh = Mesh.from_stl(compas_fab.get('planning_scene/floor.stl')) cm = CollisionMesh(mesh, 'floor') scene.add_collision_mesh(cm) # sleep a bit before terminating the client #time.sleep(1) #scene.remove_collision_mesh('floor')
# Main # ============================================================================== if __name__ == '__main__': import os import compas from compas.datastructures import Mesh from compas.files.gltf.gltf import GLTF source = 'https://raw.githubusercontent.com/ros-industrial/abb/kinetic-devel/abb_irb6600_support/meshes/irb6640/visual/link_1.stl' stl_filepath = os.path.join(compas.APPDATA, 'data', 'meshes', 'ros', 'link_1.stl') download_file_from_remote(source, stl_filepath, overwrite=False) gltf_filepath = os.path.join(compas.APPDATA, 'data', 'gltfs', 'double_link_1.gltf') mesh = Mesh.from_stl(stl_filepath) cnt = GLTFContent() scene = cnt.add_scene() node_1 = scene.add_child(node_name='Node1') mesh_data = node_1.add_mesh(mesh) node_2 = node_1.add_child(child_name='Node2') node_2.translation = [0, 0, 5] node_2.add_mesh(mesh_data.key) gltf = GLTF(gltf_filepath) gltf.content = cnt gltf.export(embed_data=True)
def from_stl(self): filename, _ = get_stl_file() if filename: self.mesh = Mesh.from_stl(filename) self.view.make_buffers() self.view.updateGL()
def mesh(): return Mesh.from_stl(compas.get('cone.stl'))
#group = "axis_abb" # Try with this as well... picking_frame = Frame([1.926, 1.5, 1], [0, 1, 0], [1, 0, 0]) picking_configuration = Configuration.from_prismatic_and_revolute_values([-1.800], [0.569, 0.849, -0.235, 6.283, 0.957, 2.140]) save_vector = Vector(0, 0, 0.1) saveframe_pick = Frame(picking_frame.point + save_vector, picking_frame.xaxis, picking_frame.yaxis) # Load assembly assembly = Assembly.from_json(PATH_FROM) # COLLISION SETTINGS =========================================================== # Add platform as collision mesh package = "abb_linear_axis" mesh = Mesh.from_stl(os.path.join(DATA, 'robot_description', package, 'meshes', 'collision', 'platform.stl')) robot.add_collision_mesh_to_planning_scene('platform', mesh) # Remove brick_wall from planning scene robot.remove_collision_mesh_from_planning_scene("brick_wall") # Create attached collision object brick = Mesh.from_obj(os.path.join(DATA, "brick.obj")) aco = robot.create_collision_mesh_attached_to_end_effector('brick', brick, group) # tolerance vector for placing the brick at placing_frame, otherwise collision tolerance_vector = Vector(0, 0, 0.003) # PICKING PATH ================================================================= # Calculate cartesian path between picking frame and saveframe_pick
def test_add_vertex(): mesh = Mesh.from_stl(compas.get('cube_binary.stl')) n = mesh.number_of_vertices() key = mesh.add_vertex(x=0, y=1, z=2) assert mesh.vertex[key] == {'x': 0, 'y': 1, 'z': 2} assert mesh.number_of_vertices() == n + 1
def test_add_vertex(): mesh = Mesh.from_stl(compas.get('cube_binary.stl')) v = mesh.number_of_vertices() key = mesh.add_vertex(x=0, y=1, z=2) assert mesh.vertex_attributes(key, 'xyz') == [0, 1, 2] assert mesh.number_of_vertices() == v + 1
def test_add_face(): mesh = Mesh.from_stl(compas.get('cube_binary.stl')) v = mesh.number_of_faces() key = mesh.add_face([0, 1, 2]) assert mesh.face_vertices(key) == [0, 1, 2] assert mesh.number_of_faces() == v + 1
from ex23_load_robot import robot import os import time from compas.datastructures import Mesh from compas_fab.backends import RosClient HERE = os.path.dirname(__file__) DATA = os.path.join(HERE, '../data') PATH = os.path.join(DATA, 'floor.stl') robot.client = RosClient() robot.client.run() floor = Mesh.from_stl(PATH) robot.add_collision_mesh_to_planning_scene("floor", floor) time.sleep(2) robot.remove_collision_mesh_from_planning_scene("floor") time.sleep(2)
def test_cull_vertices(): mesh = Mesh.from_stl(compas.get('cube_binary.stl')) mesh.add_vertex() n = mesh.number_of_vertices() mesh.cull_vertices() assert mesh.number_of_vertices() == n - 1
def test_delete_face(): mesh = Mesh.from_stl(compas.get('cube_binary.stl')) fn = mesh.number_of_faces() mesh.delete_face(0) assert mesh.number_of_faces() == fn - 1
from compas.geometry import Plane from compas.geometry import Polyline from compas.datastructures import Mesh from compas_view2.app import App from compas_cgal.slicer import slice_mesh HERE = os.path.dirname(__file__) FILE = os.path.join(HERE, '../..', 'data', '3DBenchy.stl') # ============================================================================== # Get benchy and construct a mesh # ============================================================================== benchy = Mesh.from_stl(FILE) # ============================================================================== # Create planes # ============================================================================== # replace by planes along a curve bbox = benchy.bounding_box() x, y, z = zip(*bbox) zmin, zmax = min(z), max(z) normal = Vector(0, 0, 1) planes = [] for i in np.linspace(zmin, zmax, 50):
import time from compas.datastructures import Mesh import compas_fab from compas_fab.backends import RosClient from compas_fab.robots import CollisionMesh from compas_fab.robots import PlanningScene with RosClient() as client: robot = client.load_robot() scene = PlanningScene(robot) assert robot.name == 'ur5' # create collision object mesh = Mesh.from_stl(compas_fab.get('planning_scene/cone.stl')) cm = CollisionMesh(mesh, 'tip') # attach it to the end-effector group = robot.main_group_name scene.attach_collision_mesh_to_robot_end_effector(cm, group=group) # sleep a bit before removing the tip time.sleep(1) scene.reset()
import os import time from compas_fab.backends import RosClient from compas_fab.robots import PlanningScene from compas_fab.robots import Tool from compas.datastructures import Mesh from compas.geometry import Frame HERE = os.path.dirname(__file__) DATA = os.path.abspath(os.path.join(HERE, "..", "data")) # create tool from mesh and frame mesh = Mesh.from_stl(os.path.join(DATA, "vacuum_gripper.stl")) frame = Frame([0.07, 0, 0], [0, 0, 1], [0, 1, 0]) tool = Tool(mesh, frame) tool.to_json(os.path.join(DATA, "vacuum_gripper.json")) with RosClient('localhost') as client: robot = client.load_robot() scene = PlanningScene(robot) robot.attach_tool(tool) # add attached tool to planning scene scene.add_attached_tool() # now we can convert frames at robot's tool tip and flange frames_tcf = [ Frame((-0.309, -0.046, -0.266), (0.276, 0.926, -0.256), (0.879, -0.136, 0.456))
def test_add_face(): mesh = Mesh.from_stl(compas.get('cube_binary.stl')) n = mesh.number_of_faces() key = mesh.add_face([0, 1, 2]) assert mesh.face[key] == [0, 1, 2] assert mesh.number_of_faces() == n + 1