Skip to content

Commit f7d96ff

Browse files
committed
typing
1 parent f839e31 commit f7d96ff

File tree

1 file changed

+45
-11
lines changed

1 file changed

+45
-11
lines changed

src/magpylib_material_response/polyline.py

Lines changed: 45 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,13 @@
44
from loguru import logger
55

66

7-
def _find_circle_center_and_tangent_points(a, b, c, r, max_ratio=1):
7+
def _find_circle_center_and_tangent_points(
8+
a: np.ndarray,
9+
b: np.ndarray,
10+
c: np.ndarray,
11+
r: float,
12+
max_ratio: float = 1
13+
) -> tuple[np.ndarray, np.ndarray, np.ndarray] | None:
814
"""
915
Find the center of a circle and its tangent points with given vertices and radius.
1016
@@ -31,7 +37,8 @@ def _find_circle_center_and_tangent_points(a, b, c, r, max_ratio=1):
3137

3238
dot_babc = np.dot(ba_unit, bc_unit)
3339
if dot_babc == -1: # angle is 180°
34-
return None
40+
msg = "Triangle is flat"
41+
raise ValueError(msg)
3542
theta = np.arccos(dot_babc) / 2
3643
tan_theta = np.tan(theta)
3744
d = r / tan_theta
@@ -52,7 +59,7 @@ def _find_circle_center_and_tangent_points(a, b, c, r, max_ratio=1):
5259
return circle_center, ta, tb
5360

5461

55-
def _interpolate_circle(center, start, end, n_points):
62+
def _interpolate_circle(center: np.ndarray, start: np.ndarray, end: np.ndarray, n_points: int) -> list[np.ndarray]:
5663
"""
5764
Interpolate points along a circle arc between two points.
5865
@@ -83,7 +90,13 @@ def _interpolate_circle(center, start, end, n_points):
8390
]
8491

8592

86-
def _create_fillet_segment(a, b, c, r, N):
93+
def _create_fillet_segment(
94+
a: np.ndarray,
95+
b: np.ndarray,
96+
c: np.ndarray,
97+
r: float,
98+
N: int
99+
) -> list[np.ndarray]:
87100
"""
88101
Create a fillet segment with a given radius between three vertices.
89102
@@ -101,14 +114,15 @@ def _create_fillet_segment(a, b, c, r, N):
101114
list
102115
List of NumPy arrays representing the fillet points.
103116
"""
104-
res = _find_circle_center_and_tangent_points(a, b, c, r)
105-
if res is None:
117+
try:
118+
res = _find_circle_center_and_tangent_points(a, b, c, r)
119+
circle_center, ta, tb = res
120+
except ValueError:
106121
return [b]
107-
circle_center, ta, tb = res
108122
return _interpolate_circle(circle_center, ta, tb, N)
109123

110124

111-
def create_polyline_fillet(polyline, max_radius, N):
125+
def create_polyline_fillet(polyline: list | np.ndarray, max_radius: float, N: int) -> np.ndarray:
112126
"""
113127
Create a filleted polyline with specified maximum radius and number of points.
114128
@@ -157,7 +171,7 @@ def create_polyline_fillet(polyline, max_radius, N):
157171
return np.array(filleted_points)
158172

159173

160-
def _bisectors(polyline):
174+
def _bisectors(polyline: np.ndarray) -> np.ndarray:
161175
"""
162176
Calculate and normalize bisectors of the segments in a polyline.
163177
@@ -187,7 +201,12 @@ def _bisectors(polyline):
187201
return bisectors / np.linalg.norm(bisectors, axis=1)[:, np.newaxis]
188202

189203

190-
def _line_plane_intersection(plane_point, plane_normal, line_points, line_directions):
204+
def _line_plane_intersection(
205+
plane_point: np.ndarray,
206+
plane_normal: np.ndarray,
207+
line_points: np.ndarray,
208+
line_directions: np.ndarray
209+
) -> np.ndarray:
191210
"""
192211
Find the intersection points of multiple lines and a plane.
193212
@@ -223,7 +242,22 @@ def _line_plane_intersection(plane_point, plane_normal, line_points, line_direct
223242
return line_points + np.expand_dims(t, axis=-1) * line_directions
224243

225244

226-
def move_grid_along_polyline(verts, grid):
245+
def move_grid_along_polyline(verts: np.ndarray, grid: np.ndarray) -> np.ndarray:
246+
"""
247+
Move a grid along a polyline, defined by the vertices.
248+
249+
Parameters
250+
----------
251+
verts : np.ndarray, shape (n, d)
252+
Array of polyline vertices, where n is the number of vertices and d is the dimension.
253+
grid : np.ndarray, shape (m, d)
254+
Array of grid points to move along the polyline, where m is the number of points.
255+
256+
Returns
257+
-------
258+
np.ndarray, shape (m, n, d)
259+
Array of moved grid points along the polyline, with the same dimensions as the input grid.
260+
"""
227261
"""
228262
Move a grid along a polyline, defined by the vertices.
229263

0 commit comments

Comments
 (0)