def test_booleans(): # ============================================================================== # Make a box and a sphere # ============================================================================== box = Box.from_width_height_depth(2, 2, 2) box = Mesh.from_shape(box) box.quads_to_triangles() A = box.to_vertices_and_faces() sphere = Sphere(Point(1, 1, 1), 1) sphere = Mesh.from_shape(sphere, u=30, v=30) sphere.quads_to_triangles() B = sphere.to_vertices_and_faces() # ============================================================================== # Remesh the sphere # ============================================================================== B = remesh(B, 0.3, 10) # ============================================================================== # Compute the boolean mesh # ============================================================================== V, F = boolean_union(A, B) mesh = Mesh.from_vertices_and_faces(V, F)
def volume(self): if self.radius == 0: return self.box.volume else: xr = self.box.xsize - 2 * self.radius yr = self.box.ysize - 2 * self.radius zr = self.box.zsize - 2 * self.radius # box without the radius layer inner = xr * yr * zr # sides sides = Box.from_width_height_depth(xr, yr, zr).area * self.radius # cylinder along all edges, base circle x height edges = (self.radius**2 * pi) * (xr + yr + zr) # eight time corner, 1/8th of a sphere = 1 sphere corns = 4. / 3. * pi * self.radius**3 return inner + sides + edges + corns
import time from compas.datastructures import Mesh from compas.geometry import Box 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_robot' brick = Box.from_width_height_depth(0.11, 0.07, 0.25) for i in range(5): mesh = Mesh.from_vertices_and_faces(brick.vertices, brick.faces) cm = CollisionMesh(mesh, 'brick') cm.frame.point.y += 0.5 cm.frame.point.z += brick.zsize * i scene.append_collision_mesh(cm) # sleep a bit before removing the bricks time.sleep(1) scene.remove_collision_mesh('brick')
self.mpl_polygon.set_linewidth(self.linewidth) # ============================================================================== # Main # ============================================================================== if __name__ == '__main__': from random import uniform from compas.geometry import Box from compas.geometry import Polygon from compas_plotters import GeometryPlotter n = 100 box = Box.from_width_height_depth(10, 3, 5) x, y, _ = zip(*box.points) xmin, xmax = min(x), max(x) ymin, ymax = min(y), max(y) x = [uniform(xmin, xmax) for i in range(n)] y = [uniform(ymin, ymax) for i in range(n)] z = [0] * n points = list(zip(x, y, z)) plotter = GeometryPlotter(show_axes=False) polygon = Polygon(points) plotter.add(polygon) plotter.zoom_extents()
from compas.geometry import Box, Translation from compas.geometry import dot_vectors from compas.datastructures import Mesh from compas_view2.app import App box = Box.from_width_height_depth(1, 1, 1) cage = Mesh.from_shape(box) cage.update_default_edge_attributes({'crease': 0}) top = sorted( cage.faces(), key=lambda face: dot_vectors(cage.face_normal(face), [0, 0, 1]))[-1] bottom = sorted( cage.faces(), key=lambda face: dot_vectors(cage.face_normal(face), [0, 0, -1]))[-1] c0 = cage.copy() c1 = c0.transformed(Translation.from_vector([1.5, 0, 0])) c2 = c1.transformed(Translation.from_vector([1.5, 0, 0])) c3 = c2.transformed(Translation.from_vector([1.5, 0, 0])) c4 = c3.transformed(Translation.from_vector([1.5, 0, 0])) c5 = c4.transformed(Translation.from_vector([1.5, 0, 0])) c1.edges_attribute('crease', 1, keys=list(cage.face_halfedges(top))) c1.edges_attribute('crease', 1, keys=list(cage.face_halfedges(bottom))) c2.edges_attribute('crease', 2, keys=list(cage.face_halfedges(top))) c2.edges_attribute('crease', 2, keys=list(cage.face_halfedges(bottom))) c3.edges_attribute('crease', 3, keys=list(cage.face_halfedges(top)))
number_of_courses = 7 # brick dimensions width = 0.240 height = 0.052 depth = 0.116 # horizontal joints gap = 0.02 # brick geometry box = Box.from_width_height_depth(width, height, depth) brick = Block.from_vertices_and_faces(box.vertices, box.faces) # halfbrick geometry box = Box.from_width_height_depth(0.5 * (width - gap), height, depth) halfbrick = Block.from_vertices_and_faces(box.vertices, box.faces) # empty assembly assembly = Assembly() # add bricks in a staggered pattern for i in range(number_of_courses): dy = i * height
from compas.datastructures import Assembly from compas.datastructures import Part from compas.geometry import Box from compas.geometry import Cylinder from compas.geometry import Circle from compas.geometry import Plane from compas.geometry import Translation from compas.geometry import Rotation from compas.geometry import Frame from compas_view2.app import App assembly = Assembly() a = Part(name='A', geometry=Box.from_width_height_depth(1, 1, 1)) b = Part(name='B', frame=Frame([0, 0, 1], [1, 0, 0], [0, 1, 0]), shape=Box.from_width_height_depth(1, 1, 1), features=[(Cylinder(Circle(Plane.worldXY(), 0.2), 1.0), 'difference')]) b.transform(Rotation.from_axis_and_angle([0, 0, 1], radians(45))) b.transform(Translation.from_vector([0, 0, 1])) assembly.add_part(a) assembly.add_part(b) assembly.add_connection(a, b) viewer = App()
from compas.geometry import Polyline from compas.datastructures import Mesh from compas.utilities import hex_to_rgb from compas_rhino.artists import PointArtist, PolylineArtist, MeshArtist # @me add restart option to init from compas.rpc import Proxy proxy = Proxy() # ============================================================================== # Make a box and a sphere # ============================================================================== box = Box.from_width_height_depth(2, 2, 2) box = Mesh.from_shape(box) box.quads_to_triangles() A = box.to_vertices_and_faces() sphere = Sphere(Point(1, 1, 1), 1) sphere = Mesh.from_shape(sphere, u=30, v=30) sphere.quads_to_triangles() B = sphere.to_vertices_and_faces() # ============================================================================== # Remesh the sphere # ==============================================================================
from compas.geometry import Box from compas.geometry import Translation from compas.datastructures import Mesh from compas.datastructures import mesh_quads_to_triangles from compas.datastructures import mesh_subdivide_quad from compas_viewers.multimeshviewer import MultiMeshViewer from compas_viewers.multimeshviewer import MeshObject import compas_libigl as igl # ============================================================================== # Input Geometry # ============================================================================== # create a box mesh around the center of the world box = Box.from_width_height_depth(5.0, 3.0, 1.0) a = Mesh.from_shape(box) mesh_quads_to_triangles(a) # create a box mesh around the center of the world box = Box.from_width_height_depth(1.0, 5.0, 3.0) b = Mesh.from_shape(box) mesh_quads_to_triangles(b) # ============================================================================== # Booleans # ============================================================================== # convert meshes to data
def box(): box = Box.from_width_height_depth(2, 2, 2) return Mesh.from_shape(box)
from compas.geometry import Box from compas.geometry import Circle from compas.geometry import Cylinder from compas.geometry import Frame from compas.geometry import Plane from compas.geometry import Sphere # Box b1 = Box(Frame.worldXY(), 5, 1, 3) # xsize, ysize, zsize b2 = Box.from_width_height_depth(5, 3, 1) # width=xsize, height=zsize, depth=ysize assert str(b1) == str(b2) print(b1) # Sphere s1 = Sphere([0, 0, 0], 5) print(s1) # Cylinder plane = Plane([0, 0, 0], [0, 0, 1]) circle = Circle(plane, 5) c1 = Cylinder(circle, height=4) print(c1)
from compas.geometry import Box, Translation from compas_occ.brep import BRep A = BRep.from_box(Box.from_width_height_depth(1, 1, 1)) B = BRep.from_box( Box.from_width_height_depth(1, 1, 1).transformed( Translation.from_vector([0.5, 0, 1]))) print(A.type) C = BRep.from_boolean_union(A, B) print(C.type) C.to_step('c.stp')
from compas.datastructures import Mesh from compas.datastructures import mesh_transform_numpy from compas.utilities import rgb_to_hex from compas.geometry import Translation from compas.geometry import Rotation from compas_viewers.objectviewer import ObjectViewer viewer = ObjectViewer(activate_selection=True) # make 10 random meshes # with random position and orientation for i in range(10): T = Translation.from_vector([rdi(0, 10), rdi(0, 10), rdi(0, 5)]) R = Rotation.from_axis_and_angle([0, 0, 1.0], radians(rdi(0, 180))) X = T * R box = Box.from_width_height_depth(rdi(1, 3), rdi(1, 3), rdi(1, 3)) mesh = Mesh.from_shape(box) mesh_transform_numpy(mesh, X) viewer.add(mesh, name="Mesh.%s" % i, settings={ 'color': rgb_to_hex((rdi(0, 255), rdi(0, 255), rdi(0, 255))), 'edges.width': 2, 'opacity': 0.7, 'vertices.size': 10, 'vertices.on': True, 'edges.on': False, 'faces.on': True, })
from compas.geometry import Plane from compas.geometry import centroid_points from compas.datastructures import Mesh from compas.datastructures import mesh_quads_to_triangles from compas.datastructures import mesh_subdivide_quad from compas_viewers.multimeshviewer import MultiMeshViewer from compas_viewers.multimeshviewer.model import MeshObject import compas_libigl as igl origin = Point(0.0, 0.0, 0.0) cube = Box.from_width_height_depth(1.0, 1.0, 1.0) sphere = Sphere(origin, 0.95 * sqrt(0.5 ** 2 + 0.5 ** 2)) xcyl = Cylinder(Circle(Plane(origin, Vector(1.0, 0.0, 0.0)), 0.35), 2.0) ycyl = Cylinder(Circle(Plane(origin, Vector(0.0, 1.0, 0.0)), 0.35), 2.0) zcyl = Cylinder(Circle(Plane(origin, Vector(0.0, 0.0, 1.0)), 0.35), 2.0) a = Mesh.from_vertices_and_faces(cube.vertices, cube.faces) a = mesh_subdivide_quad(a, k=3) b = Mesh.from_vertices_and_faces(* sphere.to_vertices_and_faces(u=30, v=30)) c = Mesh.from_vertices_and_faces(* xcyl.to_vertices_and_faces(u=30)) d = Mesh.from_vertices_and_faces(* ycyl.to_vertices_and_faces(u=30)) e = Mesh.from_vertices_and_faces(* zcyl.to_vertices_and_faces(u=30)) mesh_quads_to_triangles(a) mesh_quads_to_triangles(b)
# ============================================================================== # Geometry # ============================================================================== R = 1.4 P = Point(0, 0, 0) X = Vector(1, 0, 0) Y = Vector(0, 1, 0) Z = Vector(0, 0, 1) YZ = Plane(P, X) ZX = Plane(P, Y) XY = Plane(P, Z) box = Box.from_width_height_depth(2 * R, 2 * R, 2 * R) sphere = Sphere(P, 1.25 * R) cylx = Cylinder((YZ, 0.7 * R), 3 * R) cyly = Cylinder((ZX, 0.7 * R), 3 * R) cylz = Cylinder((XY, 0.7 * R), 3 * R) # ============================================================================== # CSG Model # ============================================================================== model = ShapeModel(name="booleans") model.mesh_lmin = 0.2 model.mesh_lmax = 0.2
import time from compas.datastructures import Mesh from compas.geometry import Box, Translation 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) brick = Box.from_width_height_depth(0.016, 0.012, 0.031) brick.transform(Translation.from_vector([0, 0, brick.zsize / 2.])) for i in range(5): mesh = Mesh.from_shape(brick) cm = CollisionMesh(mesh, 'brick_wall') cm.frame.point.y += 0.5 cm.frame.point.z += brick.zsize * i scene.append_collision_mesh(cm) # sleep a bit before terminating the client time.sleep(1)
from compas.utilities import rgb_to_hex from compas.geometry import Translation from compas.geometry import Rotation from compas_viewers.multimeshviewer import MultiMeshViewer from compas_viewers.multimeshviewer import MeshObject # make 10 random meshes # with random position and orientation meshes = [] for i in range(10): T = Translation( [random.randint(0, 10), random.randint(0, 10), random.randint(0, 5)]) R = Rotation.from_axis_and_angle([0, 0, 1.0], radians(random.randint(0, 180))) X = T * R box = Box.from_width_height_depth(random.randint(1, 3), random.randint(1, 3), random.randint(1, 3)) mesh = Mesh.from_shape(box) mesh_transform_numpy(mesh, X) # this is not ideal and should be handled behind the screens meshes.append(MeshObject(mesh, color=rgb_to_hex((210, 210, 210)))) viewer = MultiMeshViewer() viewer.meshes = meshes viewer.show()
# block dimensions W = 2.0 H = 0.5 D = 1.0 # ============================================================================== # Assembly # ============================================================================== assembly = Assembly() # default block box = Box.from_width_height_depth(W, H, D) brick = Block.from_shape(box) # make all blocks # place each block on top of previous # shift block randomly in XY plane for i in range(N): block = brick.copy() block.transform(Translation.from_vector([0, 0, 0.5 * H + i * H])) shift(block) assembly.add_block(block) # mark the bottom block as support assembly.node_attribute(0, 'is_support', True)
arm = robot.add_link('arm', visual_meshes=[mesh], visual_color=(0.0, 1.0, 1.0)) cylinder = Cylinder(Circle(Plane(Point(0, 0, 0), Vector(0, 0, 1)), 0.08), 1.0) cylinder.transform(Translation.from_vector([0, 0, 0.5])) mesh = Mesh.from_shape(cylinder) forearm = robot.add_link('forearm', visual_meshes=[mesh], visual_color=(0.0, 1.0, 1.0)) cylinder = Cylinder(Circle(Plane(Point(0, 0, 0), Vector(0, 0, 1)), 0.11), 0.01) mesh = Mesh.from_shape(cylinder, u=32) wrist = robot.add_link('wrist', visual_meshes=[mesh], visual_color=(0.1, 0.1, 0.1)) box = Box.from_width_height_depth(0.04, 0.3, 0.22) box.transform(Translation.from_vector([0, 0, 0.15])) mesh = Mesh.from_shape(box) hand = robot.add_link('hand', visual_meshes=[mesh], visual_color=(0, 0, 1.0)) # ============================================================================== # Joints # ============================================================================== base_joint = robot.add_joint('base-shoulder', Joint.REVOLUTE, base, shoulder, origin=Frame(Point(0, 0, 0), Vector(1, 0, 0), Vector(0, 1, 0)), axis=Vector(0, 0, 1),
import compas_rhino from compas_rhino.artists import BoxArtist, MeshArtist # ============================================================================== # Paths # ============================================================================== HERE = os.path.dirname(__file__) FILE_O = os.path.join(HERE, 'data', 'block.json') # ============================================================================== # Block and Blank # ============================================================================== box = Box.from_width_height_depth(200, 200, 200) block = Mesh.from_shape(box) vertex = next( block.vertices_where({ 'x': (float('-inf'), 0), 'y': (0, float('inf')), 'z': (0, float('inf')) })) block.vertex_attribute(vertex, 'z', 120) vertex = next( block.vertices_where({ 'x': (0, float('inf')), 'y': (0, float('inf')), 'z': (0, float('inf'))
# from compas_assembly.geometry import Arch from compas_assembly.datastructures import Block, Assembly, assembly_interfaces_numpy # from compas_assembly.rhino import AssemblyArtist # from compas_assembly.blender import AssemblyArtist # from compas.rpc import Proxy from compas_view2.app import App from compas_view2.objects import Object, MeshObject, NetworkObject Object.register(Block, MeshObject) Object.register(Assembly, NetworkObject) # proxy = Proxy('compas_assembly.datastructures') # proxy.restart_server() b1 = Block.from_shape(Box.from_width_height_depth(1, 1, 1)) T = Translation.from_vector([0, 0, 1]) R = Rotation.from_axis_and_angle([0, 0, 1], radians(45)) b2 = b1.transformed(T * R) assembly = Assembly() assembly.add_block(b1) assembly.add_block(b2) # arch = Arch(rise=5, span=10, thickness=0.7, depth=0.5, n=40) # assembly = Assembly.from_geometry(arch) assembly_interfaces_numpy(assembly)