Переглянути джерело

fix _PolygonalCircuit.__eq__()

Fabian Peter Hammerle 5 роки тому
батько
коміт
3e76db629b

+ 10 - 4
freesurfer_surface/__init__.py

@@ -78,10 +78,16 @@ class _PolygonalCircuit:
     @vertex_indices.setter
     def vertex_indices(self, indices: _VERTEX_INDICES_TYPE):
         # pylint: disable=attribute-defined-outside-init
-        self._vertex_indices = indices
+        self._vertex_indices = tuple(indices)
+
+    def _normalize(self) -> '_PolygonalCircuit':
+        min_vertex_index_index = self.vertex_indices.index(min(self.vertex_indices))
+        return type(self)(self.vertex_indices[min_vertex_index_index:]
+                          + self.vertex_indices[:min_vertex_index_index])
 
     def __eq__(self, other: '_PolygonalCircuit') -> bool:
-        return self.vertex_indices == other.vertex_indices
+        # pylint: disable=protected-access
+        return self._normalize().vertex_indices == other._normalize().vertex_indices
 
     def __hash__(self) -> int:
         return hash(self._vertex_indices)
@@ -94,7 +100,7 @@ class _LineSegment(_PolygonalCircuit):
     def vertex_indices(self, indices: _PolygonalCircuit._VERTEX_INDICES_TYPE):
         assert len(indices) == 2
         # pylint: disable=attribute-defined-outside-init
-        self._vertex_indices = indices
+        self._vertex_indices = tuple(indices)
 
     def __repr__(self) -> str:
         return '_LineSegment(vertex_indices={})'.format(self.vertex_indices)
@@ -107,7 +113,7 @@ class Triangle(_PolygonalCircuit):
     def vertex_indices(self, indices: _PolygonalCircuit._VERTEX_INDICES_TYPE):
         assert len(indices) == 3
         # pylint: disable=attribute-defined-outside-init
-        self._vertex_indices = indices
+        self._vertex_indices = tuple(indices)
 
     def __repr__(self) -> str:
         return 'Triangle(vertex_indices={})'.format(self.vertex_indices)

+ 27 - 0
tests/test_polygonal_circuit.py

@@ -0,0 +1,27 @@
+import pytest
+
+from freesurfer_surface import _PolygonalCircuit
+
+
+@pytest.mark.parametrize(('source_vertex_indices', 'expected_vertex_indices'), [
+    ((1, 2, 3), (1, 2, 3)),
+    ((2, 3, 1), (1, 2, 3)),
+    ((3, 1, 2), (1, 2, 3)),
+    ((1,), (1,)),
+])
+def test__normalize(source_vertex_indices, expected_vertex_indices):
+    # pylint: disable=protected-access
+    circuit = _PolygonalCircuit(source_vertex_indices)
+    assert expected_vertex_indices == circuit._normalize()._vertex_indices
+
+
+def test_eq():
+    assert _PolygonalCircuit((0, 1)) == _PolygonalCircuit((0, 1))
+    assert _PolygonalCircuit((0, 1)) == _PolygonalCircuit((1, 0))
+    assert _PolygonalCircuit((0, 1, 2)) == _PolygonalCircuit((0, 1, 2))
+    assert _PolygonalCircuit((0, 1, 2)) == _PolygonalCircuit((1, 2, 0))
+    assert _PolygonalCircuit((0, 1, 2)) == _PolygonalCircuit((2, 0, 1))
+    # pylint: disable=unneeded-not
+    assert not _PolygonalCircuit((0, 1, 2)) == _PolygonalCircuit((0, 1, 4))
+    assert not _PolygonalCircuit((0, 1, 2)) == _PolygonalCircuit((0, 4, 2))
+    assert not _PolygonalCircuit((0, 1, 2)) == _PolygonalCircuit((4, 1, 2))

+ 1 - 0
tests/test_triangle.py

@@ -27,6 +27,7 @@ def test_reassign_vertex_indices_invalid_len():
 
 def test_eq():
     assert Triangle((0, 1, 2)) == Triangle((0, 1, 2))
+    assert Triangle((0, 1, 2)) == Triangle((1, 2, 0))
     # pylint: disable=unneeded-not
     assert not Triangle((0, 1, 2)) == Triangle((0, 1, 4))
     assert not Triangle((0, 1, 2)) == Triangle((0, 4, 2))