24
24
if TYPE_CHECKING : # pragma: no cover
25
25
from ansys .geometry .core .math .vector import Vector2D , Vector3D
26
26
27
+ BASE_UNIT_LENGTH = UNITS .get_base_units (UNIT_LENGTH )[1 ]
28
+
27
29
28
30
class Point2D (np .ndarray , PhysicalQuantity ):
29
31
"""
@@ -66,7 +68,8 @@ def __init__(
66
68
raise ValueError ("Point2D class must receive 2 arguments." ) # noqa: E501
67
69
68
70
# Store values
69
- self .flat = [(elem * self .unit ).to_base_units ().magnitude for elem in input ]
71
+ self ._quantities = [Quantity (elem , units = unit ) for elem in input ]
72
+ self .flat = [elem .to_base_units ().m for elem in self ._quantities ]
70
73
71
74
def __eq__ (self , other : "Point2D" ) -> bool :
72
75
"""Equals operator for ``Point2D``."""
@@ -80,29 +83,36 @@ def __ne__(self, other: "Point2D") -> bool:
80
83
def __set_value (self , input : Quantity , idx : int ) -> None :
81
84
"""General setter method for ``Point2D`` class."""
82
85
self [idx ] = self ._base_units_magnitude (input )
86
+ self ._quantities [idx ] = input .to (self .unit )
83
87
84
88
def __add__ (self , other : Union ["Point2D" , "Vector2D" ]) -> "Point2D" :
85
89
"""Add operation for ``Point2D``."""
86
90
from ansys .geometry .core .math .vector import Vector2D
87
91
88
92
check_type (other , (Point2D , Vector2D ))
89
- point = Point2D (np .add (self , other ), self .base_unit )
90
- point .unit = self .unit
93
+ point = np .add (self , other ).view (Point2D )
94
+ point ._unit = self .unit
95
+ point ._base_unit = self .base_unit
96
+ point ._quantities = [np .nan , np .nan ]
91
97
return point
92
98
93
99
def __sub__ (self , other : "Point2D" ) -> "Point2D" :
94
100
"""Subtraction operation for ``Point2D``."""
95
101
from ansys .geometry .core .math .vector import Vector2D
96
102
97
103
check_type (other , (Point2D , Vector2D ))
98
- point = Point2D (np .subtract (self , other ), self .base_unit )
99
- point .unit = self .unit
104
+ point = np .subtract (self , other ).view (Point2D )
105
+ point ._unit = self .unit
106
+ point ._base_unit = self .base_unit
107
+ point ._quantities = [np .nan , np .nan ]
100
108
return point
101
109
102
110
@property
103
111
def x (self ) -> Quantity :
104
112
"""Returns the X plane component value."""
105
- return self ._get_quantity (self [0 ])
113
+ if self ._quantities [0 ] is np .nan :
114
+ self ._quantities [0 ] = Quantity (self [0 ], units = self .base_unit ).to (self .unit )
115
+ return self ._quantities [0 ]
106
116
107
117
@x .setter
108
118
def x (self , x : Quantity ) -> None :
@@ -112,7 +122,9 @@ def x(self, x: Quantity) -> None:
112
122
@property
113
123
def y (self ) -> Quantity :
114
124
"""Returns the Y plane component value."""
115
- return self ._get_quantity (self [1 ])
125
+ if self ._quantities [1 ] is np .nan :
126
+ self ._quantities [1 ] = Quantity (self [1 ], units = self .base_unit ).to (self .unit )
127
+ return self ._quantities [1 ]
116
128
117
129
@y .setter
118
130
def y (self , y : Quantity ) -> None :
@@ -122,20 +134,20 @@ def y(self, y: Quantity) -> None:
122
134
@PhysicalQuantity .unit .getter
123
135
def unit (self ) -> Unit :
124
136
"""Returns the unit of the object."""
125
- try :
126
- return PhysicalQuantity . unit . fget ( self )
127
- except AttributeError :
137
+ if hasattr ( self , "_unit" ) :
138
+ return self . _unit
139
+ else :
128
140
self ._unit = UNIT_LENGTH
129
- return self .unit
141
+ return self ._unit
130
142
131
143
@PhysicalQuantity .base_unit .getter
132
144
def base_unit (self ) -> Unit :
133
145
"""Returns the base unit of the object."""
134
- try :
135
- return PhysicalQuantity . base_unit . fget ( self )
136
- except AttributeError :
137
- self ._base_unit = UNITS . get_base_units ( UNIT_LENGTH )[ 1 ]
138
- return self .base_unit
146
+ if hasattr ( self , "_base_unit" ) :
147
+ return self . _base_unit
148
+ else :
149
+ self ._base_unit = BASE_UNIT_LENGTH
150
+ return self ._base_unit
139
151
140
152
141
153
class Point3D (np .ndarray , PhysicalQuantity ):
@@ -179,7 +191,8 @@ def __init__(
179
191
raise ValueError ("Point3D class must receive 3 arguments." ) # noqa: E501
180
192
181
193
# Store values
182
- self .flat = [(elem * self .unit ).to_base_units ().magnitude for elem in input ]
194
+ self ._quantities = [Quantity (elem , units = unit ) for elem in input ]
195
+ self .flat = [elem .to_base_units ().m for elem in self ._quantities ]
183
196
184
197
def __eq__ (self , other : "Point3D" ) -> bool :
185
198
"""Equals operator for ``Point3D``."""
@@ -195,27 +208,34 @@ def __add__(self, other: Union["Point3D", "Vector3D"]) -> "Point3D":
195
208
from ansys .geometry .core .math .vector import Vector3D
196
209
197
210
check_type (other , (Point3D , Vector3D ))
198
- point = Point3D (np .add (self , other ), self .base_unit )
199
- point .unit = self .unit
211
+ point = np .add (self , other ).view (Point3D )
212
+ point ._unit = self .unit
213
+ point ._base_unit = self .base_unit
214
+ point ._quantities = [np .nan , np .nan , np .nan ]
200
215
return point
201
216
202
217
def __sub__ (self , other : Union ["Point3D" , "Vector3D" ]) -> "Point3D" :
203
218
"""Subtraction operation for ``Point3D``."""
204
219
from ansys .geometry .core .math .vector import Vector3D
205
220
206
221
check_type (other , (Point3D , Vector3D ))
207
- point = Point3D (np .subtract (self , other ), self .base_unit )
208
- point .unit = self .unit
222
+ point = np .subtract (self , other ).view (Point3D )
223
+ point ._unit = self .unit
224
+ point ._base_unit = self .base_unit
225
+ point ._quantities = [np .nan , np .nan , np .nan ]
209
226
return point
210
227
211
228
def __set_value (self , input : Quantity , idx : int ) -> None :
212
229
"""General setter method for ``Point3D`` class."""
213
230
self [idx ] = self ._base_units_magnitude (input )
231
+ self ._quantities [idx ] = input .to (self .unit )
214
232
215
233
@property
216
234
def x (self ) -> Quantity :
217
235
"""Returns the X plane component value."""
218
- return self ._get_quantity (self [0 ])
236
+ if self ._quantities [0 ] is np .nan :
237
+ self ._quantities [0 ] = Quantity (self [0 ], units = self .base_unit ).to (self .unit )
238
+ return self ._quantities [0 ]
219
239
220
240
@x .setter
221
241
def x (self , x : Quantity ) -> None :
@@ -225,7 +245,9 @@ def x(self, x: Quantity) -> None:
225
245
@property
226
246
def y (self ) -> Quantity :
227
247
"""Returns the Y plane component value."""
228
- return self ._get_quantity (self [1 ])
248
+ if self ._quantities [1 ] is np .nan :
249
+ self ._quantities [1 ] = Quantity (self [1 ], units = self .base_unit ).to (self .unit )
250
+ return self ._quantities [1 ]
229
251
230
252
@y .setter
231
253
def y (self , y : Quantity ) -> None :
@@ -235,7 +257,9 @@ def y(self, y: Quantity) -> None:
235
257
@property
236
258
def z (self ) -> Quantity :
237
259
"""Returns the Z plane component value."""
238
- return self ._get_quantity (self [2 ])
260
+ if self ._quantities [2 ] is np .nan :
261
+ self ._quantities [2 ] = Quantity (self [2 ], units = self .base_unit ).to (self .unit )
262
+ return self ._quantities [2 ]
239
263
240
264
@z .setter
241
265
def z (self , z : Quantity ) -> None :
@@ -245,17 +269,17 @@ def z(self, z: Quantity) -> None:
245
269
@PhysicalQuantity .unit .getter
246
270
def unit (self ) -> Unit :
247
271
"""Returns the unit of the object."""
248
- try :
249
- return PhysicalQuantity . unit . fget ( self )
250
- except AttributeError :
272
+ if hasattr ( self , "_unit" ) :
273
+ return self . _unit
274
+ else :
251
275
self ._unit = UNIT_LENGTH
252
- return self .unit
276
+ return self ._unit
253
277
254
278
@PhysicalQuantity .base_unit .getter
255
279
def base_unit (self ) -> Unit :
256
280
"""Returns the base unit of the object."""
257
- try :
258
- return PhysicalQuantity . base_unit . fget ( self )
259
- except AttributeError :
260
- self ._base_unit = UNITS . get_base_units ( UNIT_LENGTH )[ 1 ]
261
- return self .base_unit
281
+ if hasattr ( self , "_base_unit" ) :
282
+ return self . _base_unit
283
+ else :
284
+ self ._base_unit = BASE_UNIT_LENGTH
285
+ return self ._base_unit
0 commit comments