Skip to content

Commit 0c234a2

Browse files
authored
Merge pull request #784 from compas-dev/circle_line_eq
add __eq__ for circle and line
2 parents 4d10cb8 + 8289116 commit 0c234a2

File tree

5 files changed

+93
-1
lines changed

5 files changed

+93
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1212
* Added `divide_polyline`, `divide_polyline_by_length`, `Polyline.split_at_corners` and `Polyline.tangent_at_point_on_polyline`.
1313
* Added the magic method `__str__` to `compas.geoemetry.Transformation`.
1414
* Added `redraw` flag to the `compas_rhino` methods `delete_object`, `delete_objects` and `purge_objects`.
15+
* Added the `__eq__` method for `compas.geometry.Circle` and `compas.geometry.Line`.
1516

1617
### Changed
1718

src/compas/geometry/primitives/circle.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,14 @@ def __setitem__(self, key, value):
141141
def __iter__(self):
142142
return iter([self.plane, self.radius])
143143

144+
def __eq__(self, other):
145+
try:
146+
other_plane = other[0]
147+
other_radius = other[1]
148+
except: # noqa: E722
149+
return False
150+
return self.plane == other_plane and self.radius == other_radius
151+
144152
# ==========================================================================
145153
# constructors
146154
# ==========================================================================

src/compas/geometry/primitives/line.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,12 @@ def __iter__(self):
144144
return iter([self.start, self.end])
145145

146146
def __eq__(self, other):
147-
raise NotImplementedError
147+
try:
148+
other_start = other[0]
149+
other_end = other[1]
150+
except: # noqa: E722
151+
return False
152+
return self.start == other_start and self.end == other_end
148153

149154
# ==========================================================================
150155
# constructors
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
from compas.geometry import Circle
2+
# These imports are used to check __repr__.
3+
from compas.geometry import Plane # noqa: F401
4+
from compas.geometry import Point # noqa: F401
5+
from compas.geometry import Vector # noqa: F401
6+
7+
8+
def test_circle():
9+
point = [0, 0, 0]
10+
vector = [1, 0, 0]
11+
plane = (point, vector)
12+
c = Circle(plane, 1.0)
13+
assert c.radius == 1.0
14+
assert c.plane == plane
15+
16+
17+
def test_equality():
18+
point = [0, 0, 0]
19+
vector = [1, 0, 0]
20+
plane = (point, vector)
21+
c = Circle(plane, 1.0)
22+
assert c == (plane, 1.0)
23+
assert c == Circle(plane, 1.0)
24+
assert c != 1.0
25+
assert c != (plane, 2.0)
26+
27+
28+
def test___repr__():
29+
point = [0, 0, 0]
30+
vector = [1, 0, 0]
31+
plane = (point, vector)
32+
c = Circle(plane, 1.0)
33+
assert c == eval(repr(c))
34+
35+
36+
def test___getitem__():
37+
point = [0, 0, 0]
38+
vector = [1, 0, 0]
39+
plane = (point, vector)
40+
c = Circle(plane, 1.0)
41+
assert c[0] == plane
42+
assert c[1] == 1.0

tests/compas/geometry/test_line.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
from compas.geometry import Line
2+
# This import is use to test __repr__.
3+
from compas.geometry import Point # noqa: F401
4+
5+
6+
def test_line():
7+
p1 = [0, 0, 0]
8+
p2 = [1, 0, 0]
9+
line = Line(p1, p2)
10+
assert line.start == p1
11+
assert line.end == p2
12+
13+
14+
def test_equality():
15+
p1 = [0, 0, 0]
16+
p2 = [1, 0, 0]
17+
line = Line(p1, p2)
18+
assert (p1, p2) == line
19+
assert line == Line(p1, p2)
20+
assert line != (p2, p1)
21+
assert line != 1
22+
23+
24+
def test___repr__():
25+
p1 = [0, 0, 0]
26+
p2 = [1, 0, 0]
27+
line = Line(p1, p2)
28+
assert line == eval(repr(line))
29+
30+
31+
def test___getitem__():
32+
p1 = [0, 0, 0]
33+
p2 = [1, 0, 0]
34+
line = Line(p1, p2)
35+
assert line[0] == p1
36+
assert line[1] == p2

0 commit comments

Comments
 (0)