Skip to content

Commit d6d5785

Browse files
LanceX2214RobPasMuejonahrb
authored
Feat/caching in all evaluation methods (#460)
Co-authored-by: Roberto Pastor Muela <37798125+RobPasMue@users.noreply.github.com> Co-authored-by: jonahrb <jonahboling@gmail.com> Co-authored-by: Jonah Boling <56607167+jonahrb@users.noreply.github.com>
1 parent ba84542 commit d6d5785

File tree

11 files changed

+232
-124
lines changed

11 files changed

+232
-124
lines changed

AUTHORS.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,4 @@
1515
* [Alexander Kaszynski](https://github.com/akaszynski)
1616
* [Jorge Martínez](https://github.com/jorgepiloto)
1717
* [Alejandro Fernández](https://github.com/AlejandroFernandezLuces)
18+
* [Lance Lance](https://github.com/LanceX2214)

doc/source/examples/01_getting_started/01_math.mystnb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -156,10 +156,10 @@ print(eval.parameter.v)
156156

157157
```{code-cell} ipython3
158158
print("Point on the sphere:")
159-
eval.position()
159+
eval.position
160160
```
161161

162162
```{code-cell} ipython3
163163
print("Normal to the surface of the sphere at the evaluation position:")
164-
eval.normal()
164+
eval.normal
165165
```

src/ansys/geometry/core/primitives/circle.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
""" Provides the ``Circle`` class."""
2+
from functools import cached_property
23

34
from beartype import beartype as check_input_types
45
from beartype.typing import Union
@@ -206,6 +207,7 @@ def parameter(self) -> Real:
206207
"""The parameter that the evaluation is based upon."""
207208
return self._parameter
208209

210+
@cached_property
209211
def position(self) -> Point3D:
210212
"""
211213
The position of the evaluation.
@@ -221,6 +223,7 @@ def position(self) -> Point3D:
221223
+ ((self.circle.radius * np.sin(self.parameter)) * self.circle.dir_y).m
222224
)
223225

226+
@cached_property
224227
def tangent(self) -> UnitVector3D:
225228
"""
226229
The tangent of the evaluation.
@@ -234,6 +237,7 @@ def tangent(self) -> UnitVector3D:
234237
np.cos(self.parameter) * self.circle.dir_y - np.sin(self.parameter) * self.circle.dir_x
235238
)
236239

240+
@cached_property
237241
def normal(self) -> UnitVector3D:
238242
"""
239243
The normal to the circle.
@@ -247,6 +251,7 @@ def normal(self) -> UnitVector3D:
247251
np.cos(self.parameter) * self.circle.dir_x + np.sin(self.parameter) * self.circle.dir_y
248252
)
249253

254+
@cached_property
250255
def first_derivative(self) -> Vector3D:
251256
"""
252257
The first derivative of the evaluation. The first derivative is in the direction of the
@@ -262,6 +267,7 @@ def first_derivative(self) -> Vector3D:
262267
np.cos(self.parameter) * self.circle.dir_y - np.sin(self.parameter) * self.circle.dir_x
263268
)
264269

270+
@cached_property
265271
def second_derivative(self) -> Vector3D:
266272
"""
267273
The second derivative of the evaluation.
@@ -275,6 +281,7 @@ def second_derivative(self) -> Vector3D:
275281
np.cos(self.parameter) * self.circle.dir_x + np.sin(self.parameter) * self.circle.dir_y
276282
)
277283

284+
@cached_property
278285
def curvature(self) -> Real:
279286
"""
280287
The curvature of the circle.

src/ansys/geometry/core/primitives/cone.py

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
"""Provides the ``Cone`` class."""
22

3+
from functools import cached_property
34

45
from beartype import beartype as check_input_types
56
from beartype.typing import Union
@@ -169,7 +170,7 @@ def project_point(self, point: Point3D) -> "ConeEvaluation":
169170
v = line_eval.parameter
170171

171172
cone_radius = self.radius.m + v * np.tan(self.half_angle.m)
172-
point_radius = np.linalg.norm(point - line_eval.position())
173+
point_radius = np.linalg.norm(point - line_eval.position)
173174
dist_to_cone = (point_radius - cone_radius) * np.cos(self.half_angle.m)
174175
v += dist_to_cone * np.sin(self.half_angle.m)
175176

@@ -232,6 +233,7 @@ def parameter(self) -> ParamUV:
232233
"""The parameter that the evaluation is based upon."""
233234
return self._parameter
234235

236+
@cached_property
235237
def position(self) -> Point3D:
236238
"""
237239
The position of the evaluation.
@@ -244,9 +246,10 @@ def position(self) -> Point3D:
244246
return (
245247
self.cone.origin
246248
+ self.parameter.v * self.cone.dir_z
247-
+ self.__radius_v() * self.__cone_normal()
249+
+ self.__radius_v * self.__cone_normal
248250
)
249251

252+
@cached_property
250253
def normal(self) -> UnitVector3D:
251254
"""
252255
The normal to the surface.
@@ -257,26 +260,30 @@ def normal(self) -> UnitVector3D:
257260
The normal unit vector to the cone at this evaluation.
258261
"""
259262
return UnitVector3D(
260-
self.__cone_normal() * np.cos(self.cone.half_angle.m)
263+
self.__cone_normal * np.cos(self.cone.half_angle.m)
261264
- self.cone.dir_z * np.sin(self.cone.half_angle.m)
262265
)
263266

267+
@cached_property
264268
def __radius_v(self) -> Real:
265269
"""Private radius helper method."""
266270
return self.cone.radius.m + self.parameter.v * np.tan(self.cone.half_angle.m)
267271

272+
@cached_property
268273
def __cone_normal(self) -> Vector3D:
269274
"""Private normal helper method."""
270275
return (
271276
np.cos(self.parameter.u) * self.cone.dir_x + np.sin(self.parameter.u) * self.cone.dir_y
272277
)
273278

279+
@cached_property
274280
def __cone_tangent(self) -> Vector3D:
275281
"""Private tangent helper method."""
276282
return (
277283
-np.sin(self.parameter.u) * self.cone.dir_x + np.cos(self.parameter.u) * self.cone.dir_y
278284
)
279285

286+
@cached_property
280287
def u_derivative(self) -> Vector3D:
281288
"""
282289
The first derivative with respect to u.
@@ -286,8 +293,9 @@ def u_derivative(self) -> Vector3D:
286293
Vector3D
287294
The first derivative with respect to u.
288295
"""
289-
return self.__radius_v() * self.__cone_tangent()
296+
return self.__radius_v * self.__cone_tangent
290297

298+
@cached_property
291299
def v_derivative(self) -> Vector3D:
292300
"""
293301
The first derivative with respect to v.
@@ -297,8 +305,9 @@ def v_derivative(self) -> Vector3D:
297305
Vector3D
298306
The first derivative with respect to v.
299307
"""
300-
return self.cone.dir_z + np.tan(self.cone.half_angle.m) * self.__cone_normal()
308+
return self.cone.dir_z + np.tan(self.cone.half_angle.m) * self.__cone_normal
301309

310+
@cached_property
302311
def uu_derivative(self) -> Vector3D:
303312
"""
304313
The second derivative with respect to u.
@@ -308,8 +317,9 @@ def uu_derivative(self) -> Vector3D:
308317
Vector3D
309318
The second derivative with respect to u.
310319
"""
311-
return -self.__radius_v() * self.__cone_normal()
320+
return -self.__radius_v * self.__cone_normal
312321

322+
@cached_property
313323
def uv_derivative(self) -> Vector3D:
314324
"""
315325
The second derivative with respect to u and v.
@@ -319,8 +329,9 @@ def uv_derivative(self) -> Vector3D:
319329
Vector3D
320330
The second derivative with respect to u and v.
321331
"""
322-
return np.tan(self.cone.half_angle.m) * self.__cone_tangent()
332+
return np.tan(self.cone.half_angle.m) * self.__cone_tangent
323333

334+
@cached_property
324335
def vv_derivative(self) -> Vector3D:
325336
"""
326337
The second derivative with respect to v.
@@ -332,6 +343,7 @@ def vv_derivative(self) -> Vector3D:
332343
"""
333344
return Vector3D([0, 0, 0])
334345

346+
@cached_property
335347
def min_curvature(self) -> Real:
336348
"""
337349
The minimum curvature of the cone.
@@ -343,6 +355,7 @@ def min_curvature(self) -> Real:
343355
"""
344356
return 0
345357

358+
@cached_property
346359
def min_curvature_direction(self) -> UnitVector3D:
347360
"""
348361
The minimum curvature direction.
@@ -352,8 +365,9 @@ def min_curvature_direction(self) -> UnitVector3D:
352365
UnitVector3D
353366
The minimum curvature direction.
354367
"""
355-
return UnitVector3D(self.v_derivative())
368+
return UnitVector3D(self.v_derivative)
356369

370+
@cached_property
357371
def max_curvature(self) -> Real:
358372
"""
359373
The maximum curvature of the cone.
@@ -363,8 +377,9 @@ def max_curvature(self) -> Real:
363377
Real
364378
The maximum curvature of the cone.
365379
"""
366-
return 1.0 / self.__radius_v()
380+
return 1.0 / self.__radius_v
367381

382+
@cached_property
368383
def max_curvature_direction(self) -> UnitVector3D:
369384
"""
370385
The maximum curvature direction.
@@ -374,4 +389,4 @@ def max_curvature_direction(self) -> UnitVector3D:
374389
UnitVector3D
375390
The maximum curvature direction.
376391
"""
377-
return UnitVector3D(self.u_derivative())
392+
return UnitVector3D(self.u_derivative)

src/ansys/geometry/core/primitives/curve_evaluation.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from functools import cached_property
2+
13
from ansys.geometry.core.math import Point3D, Vector3D
24
from ansys.geometry.core.typing import Real
35

@@ -17,18 +19,22 @@ def parameter(self) -> Real:
1719
"""The parameter that the evaluation is based upon."""
1820
raise NotImplementedError("Each evaluation must provide the parameter definition.")
1921

22+
@cached_property
2023
def position(self) -> Point3D:
2124
"""The position of the evaluation."""
2225
raise NotImplementedError("Each evaluation must provide the position definition.")
2326

27+
@cached_property
2428
def first_derivative(self) -> Vector3D:
2529
"""The first derivative of the evaluation."""
2630
raise NotImplementedError("Each evaluation must provide the first_derivative definition.")
2731

32+
@cached_property
2833
def second_derivative(self) -> Vector3D:
2934
"""The second derivative of the evaluation."""
3035
raise NotImplementedError("Each evaluation must provide the second_derivative definition.")
3136

37+
@cached_property
3238
def curvature(self) -> Real:
3339
"""The curvature of the evaluation."""
3440
raise NotImplementedError("Each evaluation must provide the curvature definition.")

src/ansys/geometry/core/primitives/cylinder.py

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
""" Provides the ``Cylinder`` class."""
22

3+
from functools import cached_property
4+
35
from beartype import beartype as check_input_types
46
from beartype.typing import Union
57
import numpy as np
@@ -230,6 +232,7 @@ def parameter(self) -> ParamUV:
230232
"""The parameter that the evaluation is based upon."""
231233
return self._parameter
232234

235+
@cached_property
233236
def position(self) -> Point3D:
234237
"""
235238
The position of the evaluation.
@@ -241,10 +244,11 @@ def position(self) -> Point3D:
241244
"""
242245
return (
243246
self.cylinder.origin
244-
+ self.cylinder.radius.m * self.__cylinder_normal()
247+
+ self.cylinder.radius.m * self.__cylinder_normal
245248
+ self.parameter.v * self.cylinder.dir_z
246249
)
247250

251+
@cached_property
248252
def normal(self) -> UnitVector3D:
249253
"""
250254
The normal to the surface.
@@ -254,8 +258,9 @@ def normal(self) -> UnitVector3D:
254258
UnitVector3D
255259
The normal unit vector to the cylinder at this evaluation.
256260
"""
257-
return UnitVector3D(self.__cylinder_normal())
261+
return UnitVector3D(self.__cylinder_normal)
258262

263+
@cached_property
259264
def __cylinder_normal(self) -> Vector3D:
260265
"""
261266
The normal to the surface.
@@ -270,13 +275,15 @@ def __cylinder_normal(self) -> Vector3D:
270275
+ np.sin(self.parameter.u) * self.cylinder.dir_y
271276
)
272277

278+
@cached_property
273279
def __cylinder_tangent(self) -> Vector3D:
274280
"""Private tangent helper method."""
275281
return (
276282
-np.sin(self.parameter.u) * self.cylinder.dir_x
277283
+ np.cos(self.parameter.u) * self.cylinder.dir_y
278284
)
279285

286+
@cached_property
280287
def u_derivative(self) -> Vector3D:
281288
"""
282289
The first derivative with respect to u.
@@ -286,8 +293,9 @@ def u_derivative(self) -> Vector3D:
286293
Vector3D
287294
The first derivative with respect to u.
288295
"""
289-
return self.cylinder.radius.m * self.__cylinder_tangent()
296+
return self.cylinder.radius.m * self.__cylinder_tangent
290297

298+
@cached_property
291299
def v_derivative(self) -> Vector3D:
292300
"""
293301
The first derivative with respect to v.
@@ -299,6 +307,7 @@ def v_derivative(self) -> Vector3D:
299307
"""
300308
return self.cylinder.dir_z
301309

310+
@cached_property
302311
def uu_derivative(self) -> Vector3D:
303312
"""
304313
The second derivative with respect to u.
@@ -308,8 +317,9 @@ def uu_derivative(self) -> Vector3D:
308317
Vector3D
309318
The second derivative with respect to u.
310319
"""
311-
return -self.cylinder.radius.m * self.__cylinder_normal()
320+
return -self.cylinder.radius.m * self.__cylinder_normal
312321

322+
@cached_property
313323
def uv_derivative(self) -> Vector3D:
314324
"""
315325
The second derivative with respect to u and v.
@@ -321,6 +331,7 @@ def uv_derivative(self) -> Vector3D:
321331
"""
322332
return Vector3D([0, 0, 0])
323333

334+
@cached_property
324335
def vv_derivative(self) -> Vector3D:
325336
"""
326337
The second derivative with respect to v.
@@ -332,6 +343,7 @@ def vv_derivative(self) -> Vector3D:
332343
"""
333344
return Vector3D([0, 0, 0])
334345

346+
@cached_property
335347
def min_curvature(self) -> Real:
336348
"""
337349
The minimum curvature of the cylinder.
@@ -343,6 +355,7 @@ def min_curvature(self) -> Real:
343355
"""
344356
return 0
345357

358+
@cached_property
346359
def min_curvature_direction(self) -> UnitVector3D:
347360
"""
348361
The minimum curvature direction.
@@ -354,6 +367,7 @@ def min_curvature_direction(self) -> UnitVector3D:
354367
"""
355368
return UnitVector3D(self.cylinder.dir_z)
356369

370+
@cached_property
357371
def max_curvature(self) -> Real:
358372
"""
359373
The maximum curvature of the cylinder.
@@ -365,6 +379,7 @@ def max_curvature(self) -> Real:
365379
"""
366380
return 1.0 / self.cylinder.radius.m
367381

382+
@cached_property
368383
def max_curvature_direction(self) -> UnitVector3D:
369384
"""
370385
The maximum curvature direction.
@@ -374,4 +389,4 @@ def max_curvature_direction(self) -> UnitVector3D:
374389
UnitVector3D
375390
The maximum curvature direction.
376391
"""
377-
return UnitVector3D(self.u_derivative())
392+
return UnitVector3D(self.u_derivative)

0 commit comments

Comments
 (0)