def draw_forces(self, color=(255, 0, 0), scale=0.1, tol=0.001): for edge in self.network.edges(): f = self.network.edge_attribute(edge, 'f') l = self.network.edge_length(*edge) # noqa E741 radius = scale * f if radius < tol: continue mp = self.network.edge_midpoint(*edge) direction = self.network.edge_direction(*edge) cylinder = Cylinder(((mp, direction), radius), l) artist = CylinderArtist(cylinder, layer=self.layer, color=color) artist.draw(u=16)
def cylinder_to_compas(cylinder): """Convert a Rhino cylinder to a COMPAS cylinder. Parameters ---------- cylinder: :class:`Rhino.Geometry.Cylinder` Returns ------- :class:`compas.geometry.Cylinder` """ plane = plane_to_compas(cylinder.BasePlane) height = cylinder.TotalHeight plane.point += plane.normal * (0.5 * height) return Cylinder(Circle(plane, cylinder.Radius), height)
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) mesh_quads_to_triangles(c) mesh_quads_to_triangles(d) mesh_quads_to_triangles(e)
# Rhino from compas.datastructures import Mesh from compas.geometry import Circle from compas.geometry import Cylinder from compas.geometry import Frame from compas.geometry import Plane from compas.geometry import Translation from compas.robots import Joint from compas.robots import RobotModel from compas_fab.rhino import RobotArtist from compas_fab.robots import Configuration # create cylinder in yz plane radius, length = 0.3, 5 cylinder = Cylinder(Circle(Plane([0, 0, 0], [1, 0, 0]), radius), length) cylinder.transform(Translation([length / 2., 0, 0])) # create robot robot = RobotModel("robot", links=[], joints=[]) # add first link to robot link0 = robot.add_link("world") # add second link to robot mesh = Mesh.from_shape(cylinder) link1 = robot.add_link("link1", visual_mesh=mesh, visual_color=(0.2, 0.5, 0.6)) # add the joint between the links axis = (0, 0, 1) origin = Frame.worldXY() robot.add_joint("joint1", Joint.CONTINUOUS, link0, link1, origin, axis)
layer = "ITA20::L5::FormFinding" artist = NetworkArtist(network, layer=layer) node_color = {node: (255, 0, 0) for node in network.nodes_where({'is_anchor': True})} artist.draw_nodes(color=node_color) artist.draw_edges() # visualize the forces height = 1.0 world = Frame.worldXY() circle = [[0, 0, 0.5 * 0.7 * height], [0, 0, 1]], 0.05 cylinder = Cylinder(circle, 0.7 * height) circle = [[0, 0, 0.7 * height], [0, 0, 1]], 0.1 cone = Cone(circle, 0.3 * height) for node in network.nodes(): a = network.node_attributes(node, 'xyz') for nbr in network.neighbors(node): edge = node, nbr if not network.has_edge(*edge): edge = nbr, node b = network.node_attributes(nbr, 'xyz') force = network.edge_attribute(edge, 'f') direction = normalize_vector(subtract_vectors(b, a))
from compas_vol.combinations import * from compas_rhino.artists import PointArtist import rhinoscriptsyntax as rs import Rhino.Geometry as rg import math frame = Frame([10, 0, 0], [1, 0, 0], [0, 1, 0]) # box = Box(Frame.worldXY(), 150, 90, 200) box = Box(frame, 150, 90, 200) volbox = VolBox(box) plane = Plane([0, 0, 0], [0, 0, 1]) circle = Circle(plane, 50) cylinder = Cylinder(circle, 90) volcylinder = VolCylinder(cylinder) sphere = Sphere((40, 20, 0), 50) volshpere = VolSphere(sphere) sphere_big = Sphere((40, 20, 0), 100) volshpere_big = VolSphere(sphere_big) union = Union(volbox, volshpere) substract = Subtraction(volshpere_big, volshpere) class FuckYouBernhardFunction: X_SCALE = 5.0
def from_data(cls, data): cylinder = Cylinder.from_data(data) vcylinder = cls(cylinder) return vcylinder
from compas.utilities import pairwise from compas.utilities import linspace from compas.utilities import remap_values import compas_rhino from compas_rhino.artists import FrameArtist, LineArtist, CylinderArtist from compas_rhino.artists import RobotModelArtist robot = RobotModel('tom') # ============================================================================== # Links # ============================================================================== cylinder = Cylinder(Circle(Plane(Point(0, 0, 0), Vector(0, 0, 1)), 0.5), 0.02) mesh = Mesh.from_shape(cylinder, u=32) base = robot.add_link('base', visual_meshes=[mesh], visual_color=(0.1, 0.1, 0.1)) cylinder = Cylinder(Circle(Plane(Point(0, 0, 0), Vector(0, 0, 1)), 0.2), 0.5) cylinder.transform(Translation.from_vector([0, 0, 0.25])) mesh = Mesh.from_shape(cylinder, u=24) shoulder = robot.add_link('shoulder', visual_meshes=[mesh], visual_color=(0, 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)
from compas.geometry import Polyline from compas.geometry import Box, Cylinder from compas_occ.brep import BRep from compas_view2.app import App 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) cx = Cylinder((YZ, 0.7 * R), 4 * R) cy = Cylinder((ZX, 0.7 * R), 4 * R) cz = Cylinder((XY, 0.7 * R), 4 * R) A = BRep.from_box(box) B1 = BRep.from_cylinder(cx) B2 = BRep.from_cylinder(cy) B3 = BRep.from_cylinder(cz) # C = BRep.from_boolean_difference( # A, # BRep.from_boolean_union( # BRep.from_boolean_union(B1, B2), # B3 # ) # )
mesh_quads_to_triangles(a) mesh_quads_to_triangles(b) A = a.to_vertices_and_faces() B = b.to_vertices_and_faces() # ============================================================================== # Booleans # ============================================================================== D = igl.mesh_intersection(A, B) for n in ([1.0, 0.0, 0.0], [0.0, 1.0, 0.0], [0.0, 0.0, 1.0]): circle = Circle((point, n), 0.6 * R) cylinder = Cylinder(circle, 2.1 * R) mesh = Mesh.from_shape(cylinder, u=30) mesh_quads_to_triangles(mesh) C = mesh.to_vertices_and_faces() D = igl.mesh_difference(D, C) mesh = Mesh.from_vertices_and_faces(*D) # ============================================================================== # Visualization # ============================================================================== # c_union.transform(Translation([7.5, 0, 0])) # c_intersection.transform(Translation([15, 0, 0])) # c_diff.transform(Translation([22.5, 0, 0]))
# ============================================================================== 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 model.boolean_difference( model.boolean_intersection([model.add_sphere(sphere)], [model.add_box(box)]),
mesh = Mesh.from_off(compas.get('tubemesh.off')) start = random.choice(list(mesh.edges())) loop = mesh.edge_loop(start) strip = [mesh.edge_faces(*edge) for edge in mesh.edge_strip(start)] strip[:] = list(set(flatten(strip))) edgecolor = {} for edge in loop: edgecolor[edge] = (0, 255, 0) edgecolor[start] = (255, 0, 0) facecolor = {} for face in strip: facecolor[face] = (255, 200, 200) artist = MeshArtist(mesh, layer='Tubemesh') artist.clear_layer() artist.draw_faces(color=facecolor) # artist.draw_edges(color=edgecolor) for edge in edgecolor: o = mesh.edge_midpoint(*edge) n = mesh.edge_direction(*edge) h = mesh.edge_length(*edge) cylinder = Cylinder([(o, n), 0.02], h) artist = CylinderArtist(cylinder, color=(0, 255, 0), layer='Tubemesh') artist.draw(show_vertices=False)
frame = joint.origin.transformed(transformations[joint.name]) frame.name = joint.name frames.append(frame) axis = joint.axis.transformed(transformations[joint.name]) axis.name = joint.name axes.append(axis) for frame in frames: artist = FrameArtist(frame, scale=0.3, layer="Frames::{}".format(frame.name)) artist.clear_layer() artist.draw() tpl = Cylinder(Circle(Plane(Point(0, 0, 0), Vector(0, 0, 1)), 0.05), 0.2) for frame, axis in zip(frames, axes): point = frame.point normal = Vector(axis.x, axis.y, axis.z) plane = Plane(point, normal) frame = Frame.from_plane(plane) X = Transformation.from_frame(frame) cylinder = tpl.transformed(X) artist = CylinderArtist(cylinder, layer="Axes::{}".format(axis.name)) artist.clear_layer() artist.draw() for a, b in pairwise(frames): line = Line(a.point, b.point) artist = LineArtist(line, layer="Links::{}".format(a.name)) artist.draw()
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 from compas.geometry import Vector from compas_rhino.artists import RobotModelArtist model = RobotModel('drinking_bird') foot_1 = Box(Frame([2, .5, .25], [1, 0, 0], [0, 1, 0]), 1, 2, .5) foot_2 = Box(Frame([-2, .5, .25], [1, 0, 0], [0, 1, 0]), 1, 2, .5) leg_1 = Box(Frame([2, 0, 4], [1, 0, 0], [0, 1, 0]), .5, 1, 7) leg_2 = Box(Frame([-2, 0, 4], [1, 0, 0], [0, 1, 0]), .5, 1, 7) axis = Cylinder(Circle(Plane([0, 0, 7], [1, 0, 0]), .01), 4) legs_link = model.add_link('legs', visual_meshes=[foot_1, foot_2, leg_1, leg_2, axis]) torso = Cylinder(Circle(Plane([0, 0, 0], [0, 0, 1]), .5), 8) torso_link = model.add_link('torso', visual_meshes=[torso]) legs_joint_origin = Frame([0, 0, 7], [1, 0, 0], [0, 1, 0]) joint_axis = Vector(1, 0, 0) model.add_joint('torso_base_joint', Joint.CONTINUOUS, legs_link, torso_link, legs_joint_origin, joint_axis) head = Sphere([0, 0, 0], 1) beak = Cylinder(Circle(Plane([0, 1, -.3], [0, 1, 0]), .3), 1.5) head_link = model.add_link('head', visual_meshes=[head, beak]) neck_joint_origin = Frame([0, 0, 4], [1, 0, 0], [0, 1, 0])
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)
""" cylinder = self.copy() cylinder.transform(transformation) return cylinder # ============================================================================== # Main # ============================================================================== if __name__ == "__main__": from compas.geometry import Frame from compas.geometry import Transformation from compas.geometry import Circle from compas.geometry import Cylinder cylinder = Cylinder(Circle(Plane.worldXY(), 5), 7) frame = Frame([1, 1, 1], [0.68, 0.68, 0.27], [-0.67, 0.73, -0.15]) print(frame.normal) T = Transformation.from_frame(frame) cylinder.transform(T) print(cylinder) print(Plane.worldXY().data) data = {'circle': Circle(Plane.worldXY(), 5).data, 'height': 7.} cylinder = Cylinder.from_data(data) print(cylinder) import doctest doctest.testmod()
def data(self, data): self.cylinder = Cylinder.from_data(data)
d = np.sqrt(xt**2 + yt**2) - self.cylinder.radius out = np.maximum(d, np.abs(zt) - self.cylinder.height / 2.0) return out # ============================================================================== # Main # ============================================================================== if __name__ == "__main__": import numpy as np from compas.geometry import Circle, Plane import matplotlib.pyplot as plt o = VolCylinder( Cylinder(Circle(Plane([1, 2, 3], [0.3, 0.4, 1.]), 5.0), 7.0)) print(o) x, y, z = np.ogrid[-15:15:60j, -15:15:60j, -15:15:60j] d = o.get_distance_numpy(x, y, z) plt.imshow( abs(d[:, :, 30].T), cmap='RdBu' ) # transpose because numpy indexing is 1)row 2) column instead of x y # plt.colorbar() plt.axis('equal') plt.show() # for y in range(-15, 15): # s = '' # for x in range(-30, 30): # d = o.get_distance(Point(x * 0.5, -y, 0))
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() viewer.add(b.geometry) viewer.add(b.frame) viewer.show()