Browse Source

PolygonalCircuit: fix equals operator for circuits with different but equivalent vertex orders

Fabian Peter Hammerle 3 years ago
parent
commit
dc1fa618a8
3 changed files with 13 additions and 3 deletions
  1. 2 0
      CHANGELOG.md
  2. 3 3
      freesurfer_surface/__init__.py
  3. 8 0
      tests/test_polygonal_chain.py

+ 2 - 0
CHANGELOG.md

@@ -12,6 +12,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
 - `PolygonalCircuit.find_label_border_polygonal_chains`:
   always include vertices along border with single neighbour
   (previously indeterministic behaviour)
+- `PolygonalCircuit`: fix equals operator for circuits
+  with different but equivalent vertex orders
 - type hints
 
 ## [1.1.1] - 2020-10-18

+ 3 - 3
freesurfer_surface/__init__.py

@@ -199,9 +199,9 @@ class PolygonalChain:
         return PolygonalChain(indices_min_first[0:1] + indices_min_first[-1:0:-1])
 
     def __eq__(self, other: object) -> bool:
-        return (
-            isinstance(other, PolygonalChain)
-            and self.vertex_indices == other.vertex_indices
+        return isinstance(other, PolygonalChain) and (
+            self.vertex_indices == other.vertex_indices
+            or self.normalized().vertex_indices == other.normalized().vertex_indices
         )
 
     def __repr__(self) -> str:

+ 8 - 0
tests/test_polygonal_chain.py

@@ -49,6 +49,14 @@ def test_eq():
     assert not PolygonalChain((0, 1, 2)) == PolygonalChain((1, 2, 3, 4))
 
 
+def test_eq_normalized():
+    assert PolygonalChain((0, 1, 2)) == PolygonalChain((0, 2, 1))
+    assert PolygonalChain((1, 0, 2)) == PolygonalChain((0, 2, 1))
+    assert PolygonalChain((1, 0, 2, 4)) == PolygonalChain((0, 1, 4, 2))
+    # pylint: disable=unneeded-not
+    assert not PolygonalChain((1, 0, 2, 4)) == PolygonalChain((0, 1, 2, 4))
+
+
 def test_repr():
     assert repr(PolygonalChain([])) == "PolygonalChain(vertex_indices=())"
     assert repr(PolygonalChain((0, 2, 1))) == "PolygonalChain(vertex_indices=(0, 2, 1))"