Skip to content

Commit 40b0e2e

Browse files
committed
fix vector comparison and add test
1 parent 13a1ac6 commit 40b0e2e

File tree

4 files changed

+24
-2
lines changed

4 files changed

+24
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1414
* Fixed `NotImplementedError` when calling `compas_rhino.conversions.surface_to_compas` on NURBS Surface.
1515
* Fixed `NotImplementedError` when calling `compas_rhino.conversions.surface_to_compas` on Surface.
1616
* Changed point comparison (`compas.geometry.Point.__eq__`) to use `TOL.is_allclose` instead of raw coordinate comparison.
17+
* Changed vector comparison (`compas.geometry.Vector.__eq__`) to use `TOL.is_allclose` instead of raw coordinate comparison.
1718
* Fixed bug in frame comparison (`compas.geometry.Frame.__eq__`).
1819
* Fixed bug in `compas.geometry.oriented_bounding_box_numpy`.
1920

src/compas/geometry/frame.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ def __iter__(self):
145145
def __eq__(self, other):
146146
if not hasattr(other, "__iter__") or not hasattr(other, "__len__") or len(self) != len(other):
147147
return False
148-
return TOL.is_allclose(self.point, other[0]) and TOL.is_allclose(self.xaxis, other[1]) and TOL.is_allclose(self.yaxis, other[2])
148+
return self.point == other[0] and self.xaxis == other[1] and self.yaxis == other[2]
149149

150150
# ==========================================================================
151151
# Properties

src/compas/geometry/vector.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ def __iter__(self):
150150
return iter([self.x, self.y, self.z])
151151

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

155155
def __add__(self, other):
156156
return Vector(self.x + other[0], self.y + other[1], self.z + other[2])

tests/compas/geometry/test_vector.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import compas
55
from random import random
66
from compas.geometry import Vector
7+
from compas.tolerance import TOL
78

89

910
@pytest.mark.parametrize(
@@ -95,6 +96,26 @@ def test_vector_equality():
9596
assert not (p1 == p3)
9697

9798

99+
def test_vector_comparison_relative():
100+
a = Vector(random(), random(), random())
101+
b = Vector(a.x + a.x * TOL.relative * 0.1, a.y + a.y * TOL.relative * 0.1, a.z + a.z * TOL.relative * 0.1)
102+
c = Vector(a.x + a.x * TOL.relative, a.y + a.y * TOL.relative, a.z + a.z * TOL.relative)
103+
d = Vector(a.x + a.x * TOL.relative * 10.0, a.y + a.y * TOL.relative * 10.0, a.z + a.z * TOL.relative * 10.0)
104+
assert a == b
105+
assert a == c
106+
assert a != d
107+
108+
109+
def test_vector_comparison_absolute():
110+
a = Vector(0, 0, 0)
111+
b = Vector(a.x + TOL.absolute * 0.1, a.y + TOL.absolute * 0.1, a.z + TOL.absolute * 0.1)
112+
c = Vector(a.x + TOL.absolute, a.y + TOL.absolute, a.z + TOL.absolute)
113+
d = Vector(a.x + TOL.absolute * 10.0, a.y + TOL.absolute * 10.0, a.z + TOL.absolute * 10.0)
114+
assert a == b
115+
assert a == c
116+
assert a != d
117+
118+
98119
def test_vector_inplace_operators():
99120
pass
100121

0 commit comments

Comments
 (0)