Skip to content

Commit a2038b2

Browse files
committed
fix: add copy in AxisAlignedRectangle since needed with new Rectangle copy method
1 parent d014edd commit a2038b2

File tree

6 files changed

+45
-14
lines changed

6 files changed

+45
-14
lines changed

docs/img/badge-enjoy-otary-gradient.svg

Lines changed: 2 additions & 2 deletions
Loading

otary/geometry/discrete/shape/axis_aligned_rectangle.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,3 +192,13 @@ def rotate_transform(
192192
is_clockwise=is_clockwise,
193193
)
194194
return Rectangle(points=rot_points)
195+
196+
def copy(self) -> AxisAlignedRectangle:
197+
"""Create a copy of the AxisAlignedRectangle object.
198+
199+
Returns:
200+
AxisAlignedRectangle: copy of the object
201+
"""
202+
return AxisAlignedRectangle(
203+
points=self.asarray.copy(), is_cast_int=self.is_cast_int
204+
)

otary/geometry/discrete/shape/polygon.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -351,10 +351,7 @@ def is_regular(self, margin_dist_error_pct: float = 1e-2) -> bool:
351351
cross_point = intersection_points[0]
352352
dist_mid_cross_diag1 = np.linalg.norm(cross_point - diag1.centroid)
353353
dist_mid_cross_diag2 = np.linalg.norm(cross_point - diag2.centroid)
354-
if (
355-
dist_mid_cross_diag1 > err_tol
356-
or dist_mid_cross_diag2 > err_tol
357-
):
354+
if dist_mid_cross_diag1 > err_tol or dist_mid_cross_diag2 > err_tol:
358355
return False
359356

360357
return True

otary/geometry/discrete/shape/rectangle.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ def __init__(
4141
on initialization. Must be in [0, 1]. Defaults to 1e-2.
4242
desintersect (bool, optional): whether to desintersect the rectangle or not.
4343
Can be useful if the input points are in a random order and
44-
self-intersection is possible. If you try to force to
44+
self-intersection is possible. If you try to force to
4545
instantiate a self-intersected rectangle a ValueError will be raised.
4646
Defaults to True.
4747
"""
@@ -64,7 +64,7 @@ def __init__(
6464
"do not form a valid Rectangle. Please check your input coordinates, "
6565
"the regularity_rtol and the desintersect parameters."
6666
)
67-
67+
6868
self.regularity_rtol = regularity_rtol
6969

7070
@classmethod
@@ -450,14 +450,14 @@ def get_vector_left_from_topleft(self, topleft_index: int) -> Vector:
450450
topleft_index=topleft_index, vertice="topright"
451451
)
452452
return Vector([self[topleft_index], rect_topright_vertice])
453-
454-
def copy(self) -> Self:
453+
454+
def copy(self) -> Rectangle:
455455
"""Create a copy of the Rectangle object.
456456
457457
Returns:
458458
Rectangle: new Rectangle object
459459
"""
460-
# having a dedicated copy method is important here because of the
460+
# having a dedicated copy method is important here because of the
461461
# regularity_rtol attribute in the special case of the Rectangle class.
462462
return Rectangle(
463463
points=self.asarray.copy(),

tests/unit/geometry/discrete/shape/test_axis_aligned_rectangle.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,3 +166,25 @@ def test_rotate_raises(self):
166166
# Attempt to rotate the rectangle by 45 degrees
167167
with pytest.raises(TypeError):
168168
rect.rotate(angle=45)
169+
170+
171+
class TestAxisAlignedRectangleCopy:
172+
173+
def test_copy_base(self):
174+
# Create an axis-aligned rectangle
175+
rect = AxisAlignedRectangle.from_topleft(topleft=[1, 1], width=3, height=2)
176+
177+
# Create a copy of the rectangle
178+
rect_copy = rect.copy()
179+
180+
assert isinstance(rect_copy, AxisAlignedRectangle)
181+
182+
# Assert the copy has the same properties as the original
183+
assert rect_copy.xmin == rect.xmin
184+
assert rect_copy.ymin == rect.ymin
185+
assert rect_copy.xmax == rect.xmax
186+
assert rect_copy.ymax == rect.ymax
187+
assert (rect_copy.asarray == rect.asarray).all()
188+
189+
# Assert the copy is a different object
190+
assert rect_copy is not rect

tests/unit/geometry/discrete/shape/test_rectangle.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -328,10 +328,12 @@ class TestRectangleCopy:
328328

329329
def test_copy(self):
330330
"""Useful test where a Rectangle that is not that regular should be copied
331-
with the same not that regular condition.
331+
with the same not that regular condition.
332332
"""
333-
rect = Rectangle([[422, 440], [419, 470], [499, 476], [500, 445]], regularity_rtol=1e-1)
333+
rect = Rectangle(
334+
[[422, 440], [419, 470], [499, 476], [500, 445]], regularity_rtol=1e-1
335+
)
334336
rect_copy = rect.copy()
335337
assert rect is not rect_copy
336338
assert np.array_equal(rect.asarray, rect_copy.asarray)
337-
assert rect.regularity_rtol == rect_copy.regularity_rtol
339+
assert rect.regularity_rtol == rect_copy.regularity_rtol

0 commit comments

Comments
 (0)