Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix comparisons point and frame #1420

Merged
merged 5 commits into from
Jan 14, 2025
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

* Fixed `NotImplementedError` when calling `compas_rhino.conversions.surface_to_compas` on NURBS Surface.
* Fixed `NotImplementedError` when calling `compas_rhino.conversions.surface_to_compas` on Surface.
* Changed point comparison (`compas.geometry.Point.__eq__`) to use `TOL.is_allclose` instead of raw coordinate comparison.
* Fixed bug in frame comparison (`compas.geometry.Frame.__eq__`).
* Fixed bug in `compas.geometry.oriented_bounding_box_numpy`.

### Removed

Expand Down
12 changes: 6 additions & 6 deletions src/compas/geometry/bbox_numpy.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,15 +95,15 @@
# also compute the axis aligned bounding box
# and compare the areas of the two

rect1, area1 = minimum_area_rectangle_xy(points, return_size=True)

points = world_to_local_coordinates_numpy(frame, points)
rect1, area1 = minimum_area_rectangle_xy(points, return_size=True)

Check warning on line 99 in src/compas/geometry/bbox_numpy.py

View check run for this annotation

Codecov / codecov/patch

src/compas/geometry/bbox_numpy.py#L99

Added line #L99 was not covered by tests
rect2 = bounding_box(points)[:4]
area2 = (rect2[1][0] - rect2[0][0]) * (rect2[3][1] - rect2[0][1])

if area1 < area2:
rect = [[pt[0], pt[1], 0.0] for pt in rect1]
bbox = rect + rect
bbox = local_to_world_coordinates_numpy(frame, rect)
bbox = vstack((bbox, bbox)).tolist()

Check warning on line 106 in src/compas/geometry/bbox_numpy.py

View check run for this annotation

Codecov / codecov/patch

src/compas/geometry/bbox_numpy.py#L105-L106

Added lines #L105 - L106 were not covered by tests
else:
rect = [[pt[0], pt[1], 0.0] for pt in rect2]
bbox = local_to_world_coordinates_numpy(frame, rect)
Expand Down Expand Up @@ -260,11 +260,12 @@
p0 = points[simplex[0]]
p1 = points[simplex[1]]

vn = xy - p0

# s direction
s = p1 - p0
sl = sum(s**2) ** 0.5
su = s / sl
vn = xy - p0
sc = (sum(vn * s, axis=1) / sl).reshape((-1, 1))
scmax = argmax(sc)
scmin = argmin(sc)
Expand All @@ -277,7 +278,6 @@
t = array([-s[1], s[0]])
tl = sum(t**2) ** 0.5
tu = t / tl
vn = xy - p0
tc = (sum(vn * t, axis=1) / tl).reshape((-1, 1))
tcmax = argmax(tc)
tcmin = argmin(tc)
Expand All @@ -287,7 +287,7 @@
h = tc[tcmax] - tc[tcmin]
a = w * h

# box corners
# other box corners
if dot(t, mean - p0) < 0:
b3 = b0 - h * tu
b2 = b1 - h * tu
Expand Down
4 changes: 2 additions & 2 deletions src/compas/geometry/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,10 +142,10 @@ def __setitem__(self, key, value):
def __iter__(self):
return iter([self.point, self.xaxis, self.yaxis])

def __eq__(self, other, tol=None):
def __eq__(self, other):
if not hasattr(other, "__iter__") or not hasattr(other, "__len__") or len(self) != len(other):
return False
return TOL.is_allclose(self, other, atol=tol)
return TOL.is_allclose(self.point, other[0]) and TOL.is_allclose(self.xaxis, other[1]) and TOL.is_allclose(self.yaxis, other[2])

# ==========================================================================
# Properties
Expand Down
2 changes: 1 addition & 1 deletion src/compas/geometry/point.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ def __iter__(self):
return iter([self.x, self.y, self.z])

def __eq__(self, other):
return self.x == other[0] and self.y == other[1] and self.z == other[2]
return TOL.is_allclose(self, other)

def __add__(self, other):
return Point(self.x + other[0], self.y + other[1], self.z + other[2])
Expand Down
32 changes: 32 additions & 0 deletions tests/compas/geometry/test_frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,3 +124,35 @@ def test_frame_inverted():
assert TOL.is_close(other.xaxis.dot([1, 0, 0]), 1.0)
assert TOL.is_close(other.yaxis.dot([0, -1, 0]), 1.0)
assert TOL.is_close(other.zaxis.dot([0, 0, -1]), 1.0)


def test_frame_comparison_relative():
a = Frame(Point(random(), random(), random()))

b = a.copy()
b.point.x += 0.1 * TOL.relative * b.point.x
assert a == b

c = a.copy()
c.point.x += TOL.relative * c.point.x
assert a == c

d = a.copy()
d.point.x += 10.0 * TOL.relative * d.point.x
assert a != d


def test_frame_comparison_absolute():
a = Frame(Point(0, 0, 0))

b = a.copy()
b.point.x += 0.1 * TOL.absolute
assert a == b

c = a.copy()
c.point.x += TOL.absolute
assert a == c

d = a.copy()
d.point.x += 10.0 * TOL.absolute
assert a != d
21 changes: 21 additions & 0 deletions tests/compas/geometry/test_point.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import compas
from random import random
from compas.geometry import Point
from compas.tolerance import TOL


@pytest.mark.parametrize(
Expand Down Expand Up @@ -64,6 +65,26 @@ def test_point_equality():
assert not (p1 == p3)


def test_point_comparison_relative():
a = Point(random(), random(), random())
b = Point(a.x + a.x * TOL.relative * 0.1, a.y + a.y * TOL.relative * 0.1, a.z + a.z * TOL.relative * 0.1)
c = Point(a.x + a.x * TOL.relative, a.y + a.y * TOL.relative, a.z + a.z * TOL.relative)
d = Point(a.x + a.x * TOL.relative * 10.0, a.y + a.y * TOL.relative * 10.0, a.z + a.z * TOL.relative * 10.0)
assert a == b
assert a == c
assert a != d


def test_point_comparison_absolute():
a = Point(0, 0, 0)
b = Point(a.x + TOL.absolute * 0.1, a.y + TOL.absolute * 0.1, a.z + TOL.absolute * 0.1)
c = Point(a.x + TOL.absolute, a.y + TOL.absolute, a.z + TOL.absolute)
d = Point(a.x + TOL.absolute * 10.0, a.y + TOL.absolute * 10.0, a.z + TOL.absolute * 10.0)
assert a == b
assert a == c
assert a != d


def test_point_inplace_operators():
pass

Expand Down
Loading