def test_equality(): point = [0, 0, 0] vector = [1, 0, 0] plane = (point, vector) c = Circle(plane, 1.0) assert c == (plane, 1.0) assert c == Circle(plane, 1.0) assert c != 1.0 assert c != (plane, 2.0)
def to_compas(self): """Convert the curve to an equivalent geometry object. Returns ------- :class:`compas.geometry.Line` If the curve is a line (if it is a linear segment between two points). :class:`compas.geometry.Polyline` If the curve is a polyline (if it is comprised of multiple line segments). :class:`compas.geometry.Circle` If the curve is a circle. """ if self.is_line(): return Line(self.start, self.end) if self.is_polyline(): return Polyline(self.points) if self.is_circle(): success, circle = self.geometry.TryGetCircle() if not success: raise Exception("not a circle") plane = circle.Plane center = plane.Origin normal = plane.Normal radius = circle.Radius return Circle([center, normal], radius)
def plot(self): """Visualize the current map with the plotter. Returns ------- None """ from compas_plotters.plotter import Plotter from compas.geometry import Pointcloud from compas.geometry import Plane, Circle, Polygon plotter = Plotter(figsize=(16, 12)) w = 16 h = 10 n = len(self.colors) d = w / n cloud = Pointcloud.from_bounds(w, h, 0, n) white = Color.white() for i, color in enumerate(self.colors): c = Circle(Plane(cloud[i], [0, 0, 1]), 0.1) p = Polygon([[i * d, -2, 0], [(i + 1) * d, -2, 0], [(i + 1) * d, -1, 0], [i * d, -1, 0]]) plotter.add(c, facecolor=color, edgecolor=white, linewidth=0.5) plotter.add(p, facecolor=color, edgecolor=color) plotter.zoom_extents() plotter.show()
def from_data(cls, data): """Construct a cone from its data representation. Parameters ---------- data : :obj:`dict` The data dictionary. Returns ------- Cone The constructed cone. Examples -------- >>> from compas.geometry import Cone >>> from compas.geometry import Circle >>> from compas.geometry import Plane >>> data = {'circle': Circle(Plane.worldXY(), 5).data, 'height': 7.} >>> cone = Cone.from_data(data) """ cone = cls(Circle(Plane.worldXY(), 1), 1) cone.data = data return cone
def test_circle(): point = [0, 0, 0] vector = [1, 0, 0] plane = (point, vector) c = Circle(plane, 1.0) assert c.radius == 1.0 assert c.plane == plane
def test___getitem__(): point = [0, 0, 0] vector = [1, 0, 0] plane = (point, vector) c = Circle(plane, 1.0) assert c[0] == plane assert c[1] == 1.0
def to_circle(self) -> compas.geometry.Circle: """Convert the edge geometry to a circle. Returns ------- :class:`~compas.geometry.Circle` A COMPAS circle. Raises ------ ValueError If the underlying geometry is not a circle. """ if not self.is_circle: raise ValueError( f'The underlying geometry is not a circle: {self.type}') curve = self.adaptor.Curve() circle = curve.Circle() location = circle.Location() direction = circle.Axis().Direction() radius = circle.Radius() point = location.X(), location.Y(), location.Z() normal = direction.X(), direction.Y(), direction.Z() return Circle(Plane(point, normal), radius)
def circle_to_compas(circle): """Convert a Rhino circle to a COMPAS circle. Parameters ---------- circle : :class:`Rhino.Geometry.Circle` Returns ------- :class:`compas.geometry.Circle` """ return Circle(plane_to_compas(circle.Plane), circle.Radius)
def to_compas(self): if self.is_line(): return Line(self.start, self.end) if self.is_polyline(): return Polyline(self.points) if self.is_circle(): success, circle = self.geometry.TryGetCircle() if not success: raise Exception("not a circle") plane = circle.Plane center = plane.Origin normal = plane.Normal radius = circle.Radius return Circle([center, normal], radius)
def cone_to_compas(cone): """Convert a Rhino cone to a COMPAS cone. Parameters ---------- cone: :class:`Rhino.Geometry.Cone` Returns ------- :class:`compas.geometry.Cone` """ plane = Plane(cone.BasePoint, vector_to_compas(cone.Plane.Normal).inverted()) return Cone(Circle(plane, cone.Radius), cone.Height)
def __init__(self, scene, arrow, **kwargs): super(ArrowObject, self).__init__(scene, arrow, **kwargs) length = np.linalg.norm(arrow.direction) plane = Plane(arrow.point + arrow.direction * 0.7, arrow.direction) circle = Circle(plane, length * 0.15) cone = Cone(circle, length * 0.3) line = Line(Point(*arrow.point), Point(*(arrow.point + arrow.direction * 0.7))) self.view = ArrowView(arrow, ConeObject(None, cone, 3), LineObject(None, line))
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)
def from_data(cls, data): """Construct a cylinder from its data representation. Parameters ---------- data : dict The data dictionary. Returns ------- :class:`compas.geometry.Cylinder` The constructed cylinder. Examples -------- >>> from compas.geometry import Cylinder >>> from compas.geometry import Circle >>> from compas.geometry import Plane >>> data = {'circle': Circle(Plane.worldXY(), 5).data, 'height': 7.} >>> cylinder = Cylinder.from_data(data) """ cylinder = cls(Circle.from_data(data['circle']), data['height']) return cylinder
def data(self, data): self.circle = Circle.from_data(data['circle']) self.height = data['height']
from compas.geometry import Polyline from compas.geometry import Plane from compas.geometry import Circle from compas_viewers.objectviewer import ObjectViewer viewer = ObjectViewer() for i in range(10): line = Line( Point(random.uniform(0, 10), random.uniform(0, 10), random.uniform(0, 10)), Point(random.uniform(0, 10), random.uniform(0, 10), random.uniform(0, 10))) viewer.add(line) point = Point(0, 0, 0) viewer.add(point, settings={'vertices.size': 10}) polyline = Polyline([[2, 0, 0], [1, 0, 0], [1, 1, 0], [1, 1, 1]]) viewer.add(polyline, settings={'vertices.size': 10}) plane = Plane([0, 0, 2], [1, 1, 1]) viewer.add(plane) circle = Circle(Plane([0, 0, 3], [1, 1, 1]), 1) viewer.add(circle) viewer.update() viewer.show()
def circle(self, circle): self._circle = Circle(*circle)
from compas.geometry import Vector, Point, Plane from compas.geometry import Polyline from compas.geometry import Circle from compas_occ.geometry import OCCNurbsCurve from compas_view2.app import App circle = Circle(Plane(Point(0, 0, 0), Vector(0, 0, 1)), 1.0) curve = OCCNurbsCurve.from_circle(circle) # ============================================================================== # Visualisation # ============================================================================== view = App() view.add(Polyline(curve.locus()), linewidth=3) view.add(Polyline(curve.points), show_points=True, pointsize=20, pointcolor=(1, 0, 0), linewidth=1, linecolor=(0.3, 0.3, 0.3)) view.run()
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.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)
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()
# ============================================================================== if __name__ == '__main__': from compas.geometry import Circle from compas.geometry import Point from compas.geometry import Plane from compas.geometry import Vector from compas.geometry import Translation from compas_plotters import GeometryPlotter plotter = GeometryPlotter() plane = Plane(Point(0, 0, 0), Vector(0, 0, 1)) a = Circle(plane, 4.0) b = Circle(plane, 3.0) c = Circle(plane, 2.0) T = Translation.from_vector([0.1, 0.0, 0.0]) plotter.add(a, edgecolor='#ff0000') plotter.add(b, edgecolor='#00ff00') plotter.add(c, edgecolor='#0000ff') plotter.pause(1.0) for i in range(100): a.transform(T) plotter.redraw(pause=0.01)
self._mpl_circle.set_facecolor(self.facecolor) self.plotter.axes.update_datalim([self.circle.center[:2]]) # ============================================================================== # Main # ============================================================================== if __name__ == '__main__': from compas.geometry import Circle from compas.geometry import Point from compas.geometry import Plane from compas.geometry import Vector from compas_plotters import GeometryPlotter plotter = GeometryPlotter() plane = Plane(Point(0, 0, 0), Vector(0, 0, 1)) a = Circle(plane, 4.0) b = Circle(plane, 3.0) c = Circle(plane, 2.0) plotter.add(a, edgecolor='#ff0000') plotter.add(b, edgecolor='#00ff00') plotter.add(c, edgecolor='#0000ff') plotter.draw(pause=1.0) plotter.show()
import compas_vol.microstructures as cvm 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:
compas.json_dump(session, FILE_O) # ============================================================================== # Visualize Rings # ============================================================================== for vertex in mesh.vertices_where({'is_beam': False, 'is_anchor': False}): ring = mesh.vertex_attribute(vertex, 'ring') or 'S' point = mesh.vertex_coordinates(vertex) normal = mesh.vertex_normal(vertex) plane = point, normal radius = mesh.attributes['radius'][ring] circle = Circle(plane, radius) artist = CircleArtist(circle, color=(255, 255, 255), layer="HiLo::Cablenet::Rings") artist.draw() # ============================================================================== # Visualize Internal Cables # ============================================================================== for bracket1, cable, bracket2 in internal: artist = LineArtist(bracket1, color=(0, 255, 255), layer="HiLo::Cablenet::Cables::Internal") artist.draw()
# 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)
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)
import random from compas.geometry import Pointcloud, Circle from compas.utilities import i_to_red, i_to_green from compas_plotters import GeometryPlotter pcl = Pointcloud.from_bounds(10, 5, 0, 100) plotter = GeometryPlotter() for point in pcl.points: circle = Circle([point, [0, 0, 1]], random.random()) plotter.add(circle, facecolor=i_to_red(random.random(), normalize=True), edgecolor=i_to_green(random.random(), normalize=True)) plotter.zoom_extents() plotter.show()
def circle(self, circle): self._circle = Circle(circle[0], circle[1])
from random import uniform from compas.geometry import Circle from compas_plotters.geometryplotter import GeometryPlotter def pointcloud(N, xlim, ylim): xmin, xmax = xlim ymin, ymax = ylim x = [uniform(xmin, xmax) for i in range(N)] y = [uniform(ymin, ymax) for i in range(N)] z = [0.0] * N return list(zip(x, y, z)) xlim = (0, 8) ylim = (0, 5) cloud = pointcloud(30, xlim, ylim) plotter = GeometryPlotter(show_axes=False) for point in cloud: plane = [point, [0, 0, 1]] radius = 0.1 plotter.add(Circle(plane, radius)) plotter.show()
>>> T = Transformation.from_frame(frame) >>> circle_transformed = cylinder.transformed(T) """ cylinder = self.copy() cylinder.transform(transformation) return cylinder # ============================================================================== # Main # ============================================================================== if __name__ == "__main__": from compas.geometry import Transformation 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()