/
cone.py
311 lines (256 loc) · 9.4 KB
/
cone.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from math import cos
from math import pi
from math import sin
from math import sqrt
from compas.utilities import pairwise
from compas.geometry import transform_points
from compas.geometry import Circle
from compas.geometry import Plane
from compas.geometry import Line
from compas.geometry import Frame
from .shape import Shape
class Cone(Shape):
"""A cone is defined by a frame, radius, and height.
The cone is oriented along the z-axis of the frame.
The base point of the cone (i.e. the centre of the base circle) is at the origin of the frame.
The entire cone is in the positive z-direction.
Parameters
----------
frame : :class:`compas.geometry.Frame`, optional
The local coordinate system of the cone.
Default is ``None``, in which case the world coordinate system is used.
radius : float, optional
The radius of the base of the cone.
height : float, optional
The height of the cone along the z-axis of the frame.
The base of the cone is at the origin of the frame.
The entire cone is above the XY plane of the frame.
Attributes
----------
area : float, read-only
The surface area of the cone.
axis : :class:`compas.geometry.Line`, read-only
The central axis of the cone.
base : :class:`compas.geometry.Point`, read-only
The base point of the cone.
The base point is at the origin of the local coordinate system.
circle : :class:`compas.geometry.Circle`, read-only
The base circle of the cone.
The center of the circle is at the origin of the local coordinate system.
diameter : float, read-only
The diameter of the base circle of the cone.
frame : :class:`compas.geometry.Frame`
The local coordinate system of the cone.
The cone is oriented along the local z-axis.
height : float
The height of the cone.
plane : :class:`compas.geometry.Plane`, read-only
The plane of the cone.
The base point of the plane is at the origin of the local coordinate system.
The normal of the plane is in the direction of the z-axis of the local coordinate system.
radius : float
The radius of the base circle of the cone.
transformation : :class:`compas.geometry.Transformation`
The transformation of the cone to global coordinates.
volume : float, read-only
The volume of the cone.
Examples
--------
>>> frame = Frame.worldXY()
>>> cone = Cone(frame=frame, radius=0.3, height=1.0)
>>> cone = Cone(radius=0.3, height=1.0)
>>> cone = Cone()
"""
DATASCHEMA = {
"type": "object",
"properties": {
"radius": {"type": "number", "minimum": 0},
"height": {"type": "number", "minimum": 0},
"frame": Frame.DATASCHEMA,
},
"required": ["radius", "height", "frame"],
}
def __init__(self, radius, height, frame=None, **kwargs):
super(Cone, self).__init__(frame=frame, **kwargs)
self._radius = None
self._height = None
self.radius = radius
self.height = height
def __repr__(self):
return "{0}(radius={1}, height={2}, frame={3!r})".format(
type(self).__name__,
self.radius,
self.height,
self.frame,
)
# ==========================================================================
# data
# ==========================================================================
@property
def data(self):
return {
"radius": self.radius,
"height": self.height,
"frame": self.frame.data,
}
@classmethod
def from_data(cls, data):
return cls(
radius=data["radius"],
height=data["height"],
frame=Frame.from_data(data["frame"]),
)
# ==========================================================================
# properties
# ==========================================================================
@property
def radius(self):
if self._radius is None:
raise ValueError("The cone radius has not been set.")
return self._radius
@radius.setter
def radius(self, radius):
if radius < 0:
raise ValueError("The cone radius should be larger than or equal to zero.")
self._radius = float(radius)
@property
def height(self):
if self._height is None:
raise ValueError("The cone height has not been set.")
return self._height
@height.setter
def height(self, height):
if height < 0:
raise ValueError("The cone height should be larger than or equal to zero.")
self._height = float(height)
@property
def axis(self):
return Line(self.frame.point, self.frame.point + self.frame.normal * self.height)
@property
def circle(self):
return Circle(self.radius, self.frame)
@property
def diameter(self):
return 2 * self.radius
@property
def area(self):
r = self.radius
h = self.height
return pi * r * (r + sqrt(h**2 + r**2))
@property
def volume(self):
r = self.radius
h = self.height
return pi * r**2 * (h / 3)
# ==========================================================================
# Constructors
# ==========================================================================
@classmethod
def from_line_and_radius(cls, line, radius): # type: (...) -> Cone
"""Construct a cone from a line and a radius.
Parameters
----------
line : :class:`compas.geometry.Line`
The axis of the cone.
radius : float
The radius of the base circle of the cone.
Returns
-------
:class:`compas.geometry.Cone`
The cone.
See Also
--------
:meth:`Cone.from_circle_and_height`
Examples
--------
>>> line = Line(Point(0, 0, 0), Point(0, 0, 1))
>>> cone = Cone.from_line_and_radius(line, 0.3)
"""
frame = Frame.from_plane(Plane(line.midpoint, line.direction))
return cls(frame=frame, radius=radius, height=line.length)
@classmethod
def from_circle_and_height(cls, circle, height): # type: (...) -> Cone
"""Construct a cone from a circle and a height.
Parameters
----------
circle : :class:`compas.geometry.Circle`
The base circle of the cone.
height : float
The height of the cone.
Returns
-------
:class:`compas.geometry.Cone`
The cone.
See Also
--------
:meth:`Cone.from_line_and_radius`
Examples
--------
>>> circle = Circle(Frame.worldXY(), 0.3)
>>> cone = Cone.from_circle_and_height(circle, 1.0)
"""
frame = circle.frame
return cls(frame=frame, radius=circle.radius, height=height)
# ==========================================================================
# Conversions
# ==========================================================================
def to_vertices_and_faces(self, u=16, triangulated=False):
"""Returns a list of vertices and faces.
Parameters
----------
u : int, optional
Number of faces in the "u" direction.
triangulated: bool, optional
If True, triangulate the faces.
Returns
-------
list[list[float]], list[list[int]]
A list of vertex locations, and a list of faces,
with each face defined as a list of indices into the list of vertices.
"""
if u < 3:
raise ValueError("The value for u should be u > 3.")
vertices = [[0, 0, 0]]
a = 2 * pi / u
radius = self.circle.radius
for i in range(u):
x = radius * cos(i * a)
y = radius * sin(i * a)
vertices.append([x, y, 0]) # type: ignore
vertices.append([0, 0, self.height]) # type: ignore
faces = []
first = 0
last = len(vertices) - 1
for i, j in pairwise(range(1, last)):
faces.append([i, j, last])
faces.append([j, i, first])
faces.append([last - 1, 1, last])
faces.append([1, last - 1, first])
if triangulated:
triangles = []
for face in faces:
if len(face) == 4:
triangles.append(face[0:3])
triangles.append([face[0], face[2], face[3]])
else:
triangles.append(face)
faces = triangles
vertices = transform_points(vertices, self.transformation)
return vertices, faces
def to_brep(self):
"""Returns a BRep representation of the cone.
Returns
-------
:class:`compas.brep.Brep`
"""
from compas.geometry import Brep
return Brep.from_cone(self)
# ==========================================================================
# Transformations
# ==========================================================================
# ==========================================================================
# Methods
# ==========================================================================