From 661e820d509f6465516f2d3ab6b84f6c508a892a Mon Sep 17 00:00:00 2001 From: LanceX2214 Date: Fri, 17 Feb 2023 12:43:25 -0500 Subject: [PATCH 1/4] remove some setter methods --- .pre-commit-config.yaml | 3 ++ src/ansys/geometry/core/primitives/circle.py | 6 ---- src/ansys/geometry/core/primitives/ellipse.py | 6 ---- src/ansys/geometry/core/primitives/line.py | 14 --------- src/ansys/geometry/core/primitives/sphere.py | 6 ---- src/ansys/geometry/core/primitives/torus.py | 5 --- tests/test_primitives.py | 31 +++++-------------- 7 files changed, 11 insertions(+), 60 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 792b34363d..1c87078fc9 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -1,3 +1,6 @@ +default_language_version: + python: python3.11 + repos: - repo: https://github.com/psf/black diff --git a/src/ansys/geometry/core/primitives/circle.py b/src/ansys/geometry/core/primitives/circle.py index 488aed6353..63319de8bc 100644 --- a/src/ansys/geometry/core/primitives/circle.py +++ b/src/ansys/geometry/core/primitives/circle.py @@ -54,12 +54,6 @@ def origin(self) -> Point3D: """Origin of the circle.""" return self._origin - @origin.setter - @check_input_types - def origin(self, origin: Point3D) -> None: - """Set the origin of the circle.""" - self._origin = origin - @property def radius(self) -> Quantity: """Radius of the circle.""" diff --git a/src/ansys/geometry/core/primitives/ellipse.py b/src/ansys/geometry/core/primitives/ellipse.py index b23be16750..58099f1ef7 100644 --- a/src/ansys/geometry/core/primitives/ellipse.py +++ b/src/ansys/geometry/core/primitives/ellipse.py @@ -76,12 +76,6 @@ def origin(self) -> Point3D: """Origin of the ellipse.""" return self._origin - @origin.setter - @check_input_types - def origin(self, origin: Point3D) -> None: - """Set the origin of the ellipse.""" - self._origin = origin - @property def major_radius(self) -> Quantity: """Major radius of the ellipse.""" diff --git a/src/ansys/geometry/core/primitives/line.py b/src/ansys/geometry/core/primitives/line.py index 6b6308decf..1c6e644018 100644 --- a/src/ansys/geometry/core/primitives/line.py +++ b/src/ansys/geometry/core/primitives/line.py @@ -40,25 +40,11 @@ def origin(self) -> Point3D: """Origin of the line.""" return self._origin - @origin.setter - @check_input_types - def origin(self, origin: Point3D) -> None: - """Set the origin of the line.""" - self._origin = origin - @property def direction(self) -> UnitVector3D: """Direction of the line.""" return self._direction - @direction.setter - @check_input_types - def direction(self, direction: Union[np.ndarray, RealSequence, UnitVector3D, Vector3D]): - """Set the direction of the line.""" - self._direction = ( - UnitVector3D(direction) if not isinstance(direction, UnitVector3D) else direction - ) - def __eq__(self, other: object) -> bool: """Equals operator for the ``Line`` class.""" if isinstance(other, Line): diff --git a/src/ansys/geometry/core/primitives/sphere.py b/src/ansys/geometry/core/primitives/sphere.py index 0d1afd44e1..2e41bda33e 100644 --- a/src/ansys/geometry/core/primitives/sphere.py +++ b/src/ansys/geometry/core/primitives/sphere.py @@ -55,12 +55,6 @@ def origin(self) -> Point3D: """Origin of the sphere.""" return self._origin - @origin.setter - @check_input_types - def origin(self, origin: Point3D) -> None: - """Set the origin of the sphere.""" - self._origin = origin - @property def radius(self) -> Quantity: """Radius of the sphere.""" diff --git a/src/ansys/geometry/core/primitives/torus.py b/src/ansys/geometry/core/primitives/torus.py index db57d08fd0..b49a31ca66 100644 --- a/src/ansys/geometry/core/primitives/torus.py +++ b/src/ansys/geometry/core/primitives/torus.py @@ -63,11 +63,6 @@ def origin(self) -> Point3D: """Origin of the torus.""" return self._origin - @origin.setter - @check_input_types - def origin(self, origin: Point3D) -> None: - self._origin = origin - @property def major_radius(self) -> Real: """Semi-major radius of the torus.""" diff --git a/tests/test_primitives.py b/tests/test_primitives.py index 2d1408266c..142cab19e2 100644 --- a/tests/test_primitives.py +++ b/tests/test_primitives.py @@ -160,21 +160,6 @@ def test_sphere(): assert Accuracy.length_is_equal(s_1.surface_area.m, 1.25663706e5) assert Accuracy.length_is_equal(s_1.volume.m, 4.1887902e6) - s_1.origin = new_origin = Point3D([42, 88, 99]) - - assert s_1.origin.x == new_origin.x - assert s_1.origin.y == new_origin.y - assert s_1.origin.z == new_origin.z - - s_2.origin = new_origin - assert s_1 == s_2 - - with pytest.raises(BeartypeCallHintParamViolation): - Sphere(origin, "A") - - with pytest.raises(BeartypeCallHintParamViolation): - s_1.origin = "A" - def test_sphere_units(): """``Sphere`` units validation.""" @@ -413,12 +398,12 @@ def test_torus(): assert t_1.major_radius == new_major_radius assert t_1.minor_radius == new_minor_radius - t_1.origin = new_origin = Point3D([42, 88, 99]) - assert t_1.origin.x == new_origin.x - assert t_1.origin.y == new_origin.y - assert t_1.origin.z == new_origin.z - assert t_1.major_radius == new_major_radius - assert t_1.minor_radius == new_minor_radius + # t_1.origin = new_origin = Point3D([42, 88, 99]) + # assert t_1.origin.x == new_origin.x + # assert t_1.origin.y == new_origin.y + # assert t_1.origin.z == new_origin.z + # assert t_1.major_radius == new_major_radius + # assert t_1.minor_radius == new_minor_radius with pytest.raises(BeartypeCallHintParamViolation): Torus(origin, UnitVector3D([12, 31, 99]), UnitVector3D([25, 39, 82]), "A", 200) @@ -432,8 +417,8 @@ def test_torus(): with pytest.raises(BeartypeCallHintParamViolation): t_1.minor_radius = "A" - with pytest.raises(BeartypeCallHintParamViolation): - t_1.origin = "A" + # with pytest.raises(BeartypeCallHintParamViolation): + # t_1.origin = "A" with pytest.raises(BeartypeCallHintParamViolation): Torus(origin, "A", UnitVector3D([25, 39, 82]), 100, 200) From 2a890ed445488341df121bc3658d07b6585d6375 Mon Sep 17 00:00:00 2001 From: LanceX2214 Date: Mon, 20 Feb 2023 11:43:26 -0500 Subject: [PATCH 2/4] make it better now --- .pre-commit-config.yaml | 3 --- tests/test_primitives.py | 13 +++---------- 2 files changed, 3 insertions(+), 13 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 1c87078fc9..792b34363d 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -1,6 +1,3 @@ -default_language_version: - python: python3.11 - repos: - repo: https://github.com/psf/black diff --git a/tests/test_primitives.py b/tests/test_primitives.py index 142cab19e2..9ff5cfc30d 100644 --- a/tests/test_primitives.py +++ b/tests/test_primitives.py @@ -160,6 +160,9 @@ def test_sphere(): assert Accuracy.length_is_equal(s_1.surface_area.m, 1.25663706e5) assert Accuracy.length_is_equal(s_1.volume.m, 4.1887902e6) + with pytest.raises(BeartypeCallHintParamViolation): + Sphere(origin, "A") + def test_sphere_units(): """``Sphere`` units validation.""" @@ -398,13 +401,6 @@ def test_torus(): assert t_1.major_radius == new_major_radius assert t_1.minor_radius == new_minor_radius - # t_1.origin = new_origin = Point3D([42, 88, 99]) - # assert t_1.origin.x == new_origin.x - # assert t_1.origin.y == new_origin.y - # assert t_1.origin.z == new_origin.z - # assert t_1.major_radius == new_major_radius - # assert t_1.minor_radius == new_minor_radius - with pytest.raises(BeartypeCallHintParamViolation): Torus(origin, UnitVector3D([12, 31, 99]), UnitVector3D([25, 39, 82]), "A", 200) @@ -417,9 +413,6 @@ def test_torus(): with pytest.raises(BeartypeCallHintParamViolation): t_1.minor_radius = "A" - # with pytest.raises(BeartypeCallHintParamViolation): - # t_1.origin = "A" - with pytest.raises(BeartypeCallHintParamViolation): Torus(origin, "A", UnitVector3D([25, 39, 82]), 100, 200) From 401062c48224caf0757ebaf4b1ad819c5d724e4f Mon Sep 17 00:00:00 2001 From: Roberto Pastor Muela <37798125+RobPasMue@users.noreply.github.com> Date: Wed, 22 Feb 2023 08:43:51 +0100 Subject: [PATCH 3/4] Torus improvements --- src/ansys/geometry/core/primitives/torus.py | 64 +++++++-------------- tests/test_primitives.py | 57 ++++-------------- 2 files changed, 32 insertions(+), 89 deletions(-) diff --git a/src/ansys/geometry/core/primitives/torus.py b/src/ansys/geometry/core/primitives/torus.py index b49a31ca66..69ba4499a7 100644 --- a/src/ansys/geometry/core/primitives/torus.py +++ b/src/ansys/geometry/core/primitives/torus.py @@ -1,12 +1,12 @@ """Provides the ``Torus`` class.""" from beartype import beartype as check_input_types -from beartype.typing import Optional, Union +from beartype.typing import Union import numpy as np -from pint import Unit +from pint import Quantity from ansys.geometry.core.math import Point3D, UnitVector3D, Vector3D -from ansys.geometry.core.misc import DEFAULT_UNITS, UNITS, check_pint_unit_compatibility +from ansys.geometry.core.misc import Distance from ansys.geometry.core.typing import Real, RealSequence @@ -22,12 +22,10 @@ class Torus: X-axis direction. direction_y : Union[~numpy.ndarray, RealSequence, UnitVector3D, Vector3D] Y-axis direction. - major_radius : Real + major_radius : Union[Quantity, Distance, Real] Major radius of the torus. - minor_radius : Real + minor_radius : Union[Quantity, Distance, Real] Minor radius of ``Torus``. - unit : ~pint.Unit, optional - Units for defining the radius and minor radius. By default, ``DEFAULT_UNITS.LENGTH`` """ @check_input_types @@ -36,16 +34,11 @@ def __init__( origin: Union[np.ndarray, RealSequence, Point3D], direction_x: Union[np.ndarray, RealSequence, UnitVector3D, Vector3D], direction_y: Union[np.ndarray, RealSequence, UnitVector3D, Vector3D], - major_radius: Real, - minor_radius: Real, - unit: Optional[Unit] = DEFAULT_UNITS.LENGTH, + major_radius: Union[Quantity, Distance, Real], + minor_radius: Union[Quantity, Distance, Real], ): """Constructor method for the ``Torus`` class.""" - check_pint_unit_compatibility(unit, DEFAULT_UNITS.LENGTH) - self._unit = unit - _, self._base_unit = UNITS.get_base_units(unit) - self._origin = Point3D(origin) if not isinstance(origin, Point3D) else origin self._direction_x = ( UnitVector3D(direction_x) if not isinstance(direction_x, UnitVector3D) else direction_x @@ -55,8 +48,12 @@ def __init__( ) # Store values in base unit - self._major_radius = UNITS.convert(major_radius, self._unit, self._base_unit) - self._minor_radius = UNITS.convert(minor_radius, self._unit, self._base_unit) + self._major_radius = ( + major_radius if isinstance(major_radius, Distance) else Distance(major_radius) + ) + self._minor_radius = ( + minor_radius if isinstance(minor_radius, Distance) else Distance(minor_radius) + ) @property def origin(self) -> Point3D: @@ -64,43 +61,22 @@ def origin(self) -> Point3D: return self._origin @property - def major_radius(self) -> Real: + def major_radius(self) -> Quantity: """Semi-major radius of the torus.""" - return UNITS.convert(self._major_radius, self._base_unit, self._unit) - - @major_radius.setter - @check_input_types - def major_radius(self, major_radius: Real) -> None: - self._major_radius = UNITS.convert(major_radius, self._unit, self._base_unit) + return self._major_radius.value @property - def minor_radius(self) -> Real: + def minor_radius(self) -> Quantity: """Semi-minor radius of the torus.""" - return UNITS.convert(self._minor_radius, self._base_unit, self._unit) - - @minor_radius.setter - @check_input_types - def minor_radius(self, minor_radius: Real) -> None: - self._minor_radius = UNITS.convert(minor_radius, self._unit, self._base_unit) - - @property - def unit(self) -> Unit: - """Unit of the semi-major radius and semi-minor radius.""" - return self._unit - - @unit.setter - @check_input_types - def unit(self, unit: Unit) -> None: - check_pint_unit_compatibility(unit, DEFAULT_UNITS.LENGTH) - self._unit = unit + return self._minor_radius.value @check_input_types def __eq__(self, other: object) -> bool: """Equals operator for the ``Torus`` class.""" return ( - self._origin == other.origin - and self._major_radius == other.major_radius - and self._minor_radius == other.minor_radius + self._origin == other._origin + and self._major_radius == other._major_radius + and self._minor_radius == other._minor_radius and self._direction_x == other._direction_x and self._direction_y == other._direction_y ) diff --git a/tests/test_primitives.py b/tests/test_primitives.py index dd32e90539..bc5243bab1 100644 --- a/tests/test_primitives.py +++ b/tests/test_primitives.py @@ -11,7 +11,7 @@ UnitVector3D, Vector3D, ) -from ansys.geometry.core.misc import UNITS, Accuracy, Distance +from ansys.geometry.core.misc import DEFAULT_UNITS, UNITS, Accuracy, Distance from ansys.geometry.core.primitives import ( Circle, Cone, @@ -389,17 +389,8 @@ def test_torus(): assert t_1.origin.x == origin.x assert t_1.origin.y == origin.y assert t_1.origin.z == origin.z - assert t_1.major_radius == major_radius - assert t_1.minor_radius == minor_radius - - t_1.major_radius = new_major_radius = 2000 - t_1.minor_radius = new_minor_radius = 1000 - - assert t_1.origin.x == origin.x - assert t_1.origin.y == origin.y - assert t_1.origin.z == origin.z - assert t_1.major_radius == new_major_radius - assert t_1.minor_radius == new_minor_radius + assert t_1.major_radius == major_radius * DEFAULT_UNITS.LENGTH + assert t_1.minor_radius == minor_radius * DEFAULT_UNITS.LENGTH with pytest.raises(BeartypeCallHintParamViolation): Torus(origin, UnitVector3D([12, 31, 99]), UnitVector3D([25, 39, 82]), "A", 200) @@ -407,12 +398,6 @@ def test_torus(): with pytest.raises(BeartypeCallHintParamViolation): Torus(origin, UnitVector3D([12, 31, 99]), UnitVector3D([25, 39, 82]), 100, "A") - with pytest.raises(BeartypeCallHintParamViolation): - t_1.major_radius = "A" - - with pytest.raises(BeartypeCallHintParamViolation): - t_1.minor_radius = "A" - with pytest.raises(BeartypeCallHintParamViolation): Torus(origin, "A", UnitVector3D([25, 39, 82]), 100, 200) @@ -437,43 +422,25 @@ def test_torus_units(): origin, UnitVector3D([12, 31, 99]), UnitVector3D([25, 39, 82]), - major_radius, - minor_radius, - UNITS.celsius, + Quantity(major_radius, UNITS.celsius), + Quantity(minor_radius, UNITS.celsius), ) t_1 = Torus( origin, UnitVector3D([12, 31, 99]), UnitVector3D([25, 39, 82]), - major_radius, - minor_radius, - unit, + Quantity(major_radius, unit), + Quantity(minor_radius, unit), ) - # Verify rejection of invalid base unit type - with pytest.raises( - TypeError, - match=r"The pint.Unit provided as an input should be a \[length\] quantity.", - ): - t_1.unit = UNITS.celsius - # Check that the units are correctly in place - assert t_1.unit == unit - - # Request for radius/height and ensure they are in mm - assert t_1.major_radius == major_radius - assert t_1.minor_radius == minor_radius + assert t_1.major_radius.u == unit + assert t_1.minor_radius.u == unit - # Check that the actual values are in base units (i.e. DEFAULT_UNITS.LENGTH) - assert t_1._major_radius == (t_1.major_radius * t_1.unit).to_base_units().magnitude - assert t_1._minor_radius == (t_1.minor_radius * t_1.unit).to_base_units().magnitude - - # Set unit to cm now... and check if the values changed - t_1.unit = new_unit = UNITS.cm - assert t_1.major_radius == UNITS.convert(major_radius, unit, new_unit) - assert t_1.minor_radius == UNITS.convert(minor_radius, unit, new_unit) - assert t_1.unit == new_unit + # Request for radii and ensure they are in mm + assert t_1.major_radius.m == major_radius + assert t_1.minor_radius.m == minor_radius def test_circle(): From 434a535524e8f8e2a0ae3b73eaf27daf7f1355a7 Mon Sep 17 00:00:00 2001 From: Roberto Pastor Muela <37798125+RobPasMue@users.noreply.github.com> Date: Wed, 22 Feb 2023 08:44:02 +0100 Subject: [PATCH 4/4] Minor typing issues --- src/ansys/geometry/core/plotting/plotter.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ansys/geometry/core/plotting/plotter.py b/src/ansys/geometry/core/plotting/plotter.py index 277fa1b4e4..d9beec089c 100644 --- a/src/ansys/geometry/core/plotting/plotter.py +++ b/src/ansys/geometry/core/plotting/plotter.py @@ -377,7 +377,7 @@ def init_plotter(self): pl = Plotter() return pl - def show_plotter(self, plotter, screenshot): + def show_plotter(self, plotter: Plotter, screenshot: Optional[str] = None): """Shows the plotter or starts Trame service. Parameters