-
Notifications
You must be signed in to change notification settings - Fork 9
/
circle.py
166 lines (140 loc) · 4.55 KB
/
circle.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
"""``Circle`` class module."""
from typing import List, Optional
import numpy as np
from ansys.geometry.core.math import UNIT_VECTOR_X, UNIT_VECTOR_Y
from ansys.geometry.core.math.point import Point3D
from ansys.geometry.core.math.vector import UnitVector3D
from ansys.geometry.core.shapes.base import BaseShape
from ansys.geometry.core.typing import Real
class Circle(BaseShape):
"""A class for modeling circles.
Parameters
----------
radius : Real
The radius of the circle.
origin : Point3D
A :class:`Point3D` representing the origin of the shape.
dir_1 : Optional[UnitVector3D]
A :class:`UnitVector3D` representing the first fundamental direction
of the reference plane where the shape is contained.
By default, ``UNIT_VECTOR_X``.
dir_2 : Optional[UnitVector3D]
A :class:`UnitVector3D` representing the second fundamental direction
of the reference plane where the shape is contained.
By default, ``UNIT_VECTOR_Y``.
"""
def __init__(
self,
radius: Real,
origin: Point3D,
dir_1: Optional[UnitVector3D] = UNIT_VECTOR_X,
dir_2: Optional[UnitVector3D] = UNIT_VECTOR_Y,
):
"""Initializes the circle shape."""
super().__init__(origin, dir_1=dir_1, dir_2=dir_2, is_closed=True)
self._radius = radius
@property
def radius(self) -> Real:
"""The radius of the circle.
Returns
-------
Real
The radius of the circle.
"""
return self._radius
@property
def r(self) -> Real:
"""The radius of the circle.
Returns
-------
Real
The radius of the circle.
"""
return self.radius
@property
def diameter(self) -> Real:
"""The diameter of the circle.
Returns
-------
Real
The diameter of the circle.
"""
return 2 * self.r
@property
def d(self) -> Real:
"""The diameter of the circle.
Returns
-------
Real
The diameter of the circle.
"""
return self.diameter
@property
def perimeter(self) -> Real:
"""Return the perimeter of the circle.
Returns
-------
Real
The perimeter of the circle.
"""
return 2 * np.pi * self.r
@property
def area(self) -> Real:
"""Return the area of the circle.
Returns
-------
Real
The area of the circle.
"""
return np.pi * self.r**2
def local_points(self, num_points: Optional[int] = 100) -> List[Point3D]:
"""Returns a list containing all the points belonging to the shape.
Points are given in the local space.
Parameters
----------
num_points : int
Desired number of points belonging to the shape.
Returns
-------
List[Point3D]
A list of points representing the shape.
"""
theta = np.linspace(0, 2 * np.pi, num_points)
x_local = self.r * np.cos(theta)
y_local = self.r * np.sin(theta)
z_local = np.zeros(num_points)
return [x_local, y_local, z_local]
@classmethod
def from_radius(
cls,
radius: Real,
origin: Optional[Point3D] = Point3D([0, 0, 0]),
dir_1: Optional[UnitVector3D] = UNIT_VECTOR_X,
dir_2: Optional[UnitVector3D] = UNIT_VECTOR_Y,
):
"""Create a circle from its origin and radius.
Parameters
----------
radius : Real
The radius of the circle.
origin : Optional[Point3D]
A :class:`Point3D` representing the origin of the ellipse.
By default, [0, 0, 0].
dir_1 : Optional[UnitVector3D]
A :class:`UnitVector3D` representing the first fundamental direction
of the reference plane where the shape is contained.
By default, ``UNIT_VECTOR_X``.
dir_2 : Optional[UnitVector3D]
A :class:`UnitVector3D` representing the second fundamental direction
of the reference plane where the shape is contained.
By default, ``UNIT_VECTOR_Y``.
Returns
-------
Circle
An object for modeling circular shapes.
"""
# Verify that the radius is a real positive value
if radius <= 0:
raise ValueError("Radius must be a real positive value.")
# Generate all the point instances
return cls(radius, origin, dir_1=dir_1, dir_2=dir_2)