|
| 1 | +import math |
| 2 | + |
| 3 | +from compas.geometry import close |
| 4 | +from compas.geometry.primitives import Frame |
| 5 | +from compas.geometry.primitives import Primitive |
| 6 | + |
| 7 | + |
| 8 | +class Arc(Primitive): |
| 9 | + """Represents a portion of a circle's arc. |
| 10 | +
|
| 11 | + Parameters |
| 12 | + ---------- |
| 13 | + frame : :class:`~compas.geometry.Frame` |
| 14 | + Coordinate frame at the center of the arc's circle. |
| 15 | + radius : float |
| 16 | + Radius of the arc's circle. |
| 17 | + end_angle : float |
| 18 | + The angle in radians of the end of this Arc. |
| 19 | + start_angle : |
| 20 | + The angle in radians of the start of this Arc. |
| 21 | +
|
| 22 | + Attributes |
| 23 | + ---------- |
| 24 | + length : float |
| 25 | + The length of the arc (radius * angle) |
| 26 | + angle : float |
| 27 | + The sweep angle in radians (between incrementing end angle and start angle). |
| 28 | + domain : tuple(float, float) |
| 29 | + A tuple containing the start and end angles of this arc, in radians. |
| 30 | + center : :class:`~compas.geometry.Point` |
| 31 | + The center point of the circle which coincides with this Arc. |
| 32 | + circumference : float |
| 33 | + The circumference of the circle which coincides with this Arc. |
| 34 | + diameter : float |
| 35 | + The diameter of the circle which coincides with this Arc. |
| 36 | +
|
| 37 | + """ |
| 38 | + |
| 39 | + def __init__(self, frame=None, radius=None, end_angle=None, start_angle=None, **kwargs): |
| 40 | + super(Arc, self).__init__(**kwargs) |
| 41 | + |
| 42 | + self._frame = frame |
| 43 | + self._radius = radius |
| 44 | + self._start_angle = start_angle if start_angle is not None else 0.0 |
| 45 | + self._end_angle = end_angle |
| 46 | + |
| 47 | + if self._radius is not None and self._end_angle is not None: |
| 48 | + self._verify() |
| 49 | + |
| 50 | + @property |
| 51 | + def data(self): |
| 52 | + return { |
| 53 | + "frame": self._frame.data, |
| 54 | + "radius": self._radius, |
| 55 | + "start": self._start_angle, |
| 56 | + "end": self._end_angle, |
| 57 | + } |
| 58 | + |
| 59 | + @data.setter |
| 60 | + def data(self, value): |
| 61 | + self._frame = Frame.from_data(value["frame"]) |
| 62 | + self._radius = value["radius"] |
| 63 | + self._start_angle = value["start"] |
| 64 | + self._end_angle = value["end"] |
| 65 | + |
| 66 | + @property |
| 67 | + def frame(self): |
| 68 | + return self._frame |
| 69 | + |
| 70 | + @frame.setter |
| 71 | + def frame(self, value): |
| 72 | + self._frame = value |
| 73 | + |
| 74 | + @property |
| 75 | + def radius(self): |
| 76 | + return self._radius |
| 77 | + |
| 78 | + @radius.setter |
| 79 | + def radius(self, value): |
| 80 | + self._radius = value |
| 81 | + |
| 82 | + @property |
| 83 | + def start_angle(self): |
| 84 | + return self._start_angle |
| 85 | + |
| 86 | + @start_angle.setter |
| 87 | + def start_angle(self, value): |
| 88 | + self._start_angle = value |
| 89 | + self._verify() |
| 90 | + |
| 91 | + @property |
| 92 | + def end_angle(self): |
| 93 | + return self._end_angle |
| 94 | + |
| 95 | + @end_angle.setter |
| 96 | + def end_angle(self, value): |
| 97 | + self._end_angle = value |
| 98 | + self._verify |
| 99 | + |
| 100 | + @property |
| 101 | + def length(self): |
| 102 | + return self.radius * self.angle |
| 103 | + |
| 104 | + @property |
| 105 | + def angle(self): |
| 106 | + return self._end_angle - self._start_angle |
| 107 | + |
| 108 | + @property |
| 109 | + def domain(self): |
| 110 | + return self._start_angle, self._end_angle |
| 111 | + |
| 112 | + @property |
| 113 | + def center(self): |
| 114 | + return self.frame.point |
| 115 | + |
| 116 | + @property |
| 117 | + def circumference(self): |
| 118 | + return 2.0 * math.pi * self.radius |
| 119 | + |
| 120 | + @property |
| 121 | + def diameter(self): |
| 122 | + return 2.0 * self.radius |
| 123 | + |
| 124 | + @property |
| 125 | + def is_circle(self): |
| 126 | + return close(abs(abs(self.angle) - 2.0 * math.pi), 0.0) |
| 127 | + |
| 128 | + @classmethod |
| 129 | + def from_circle(cls, circle, start_angle, end_angle): |
| 130 | + """Creates an Arc from a circle and start and end angles. |
| 131 | +
|
| 132 | + Parameters |
| 133 | + ---------- |
| 134 | + circle : :class:`~compas.geometry.Circle` |
| 135 | + The center point and radius of this circle will be used to create an Arc. |
| 136 | + start_angle : float |
| 137 | + The start angle in radians. |
| 138 | + end_angle : float |
| 139 | + The end angle in radians. |
| 140 | +
|
| 141 | + Returns |
| 142 | + ------- |
| 143 | + :class:`~compas.geometry.Arc` |
| 144 | +
|
| 145 | + """ |
| 146 | + frame = Frame.worldXY() |
| 147 | + # TODO: take circle.frame once it has one |
| 148 | + frame.point = circle.center.copy() |
| 149 | + return cls(frame, circle.radius, end_angle, start_angle) |
| 150 | + |
| 151 | + def _verify(self): |
| 152 | + if self.angle < 0.0 or self.angle > 2.0 * math.pi: |
| 153 | + raise ValueError("Sweep angle must satisfy 0 < angle < 2 * Pi. Currently:{}".format(self.angle)) |
0 commit comments