Browse Source

fix _PolygonalCircuit._normalize: consider order

Fabian Peter Hammerle 5 years ago
parent
commit
a45964275d
2 changed files with 43 additions and 4 deletions
  1. 12 3
      freesurfer_surface/__init__.py
  2. 31 1
      tests/test_polygonal_circuit.py

+ 12 - 3
freesurfer_surface/__init__.py

@@ -116,9 +116,18 @@ class _PolygonalCircuit:
         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])
+        min_vertex_index_index = self.vertex_indices.index(
+            min(self.vertex_indices))
+        previous_index = self.vertex_indices[min_vertex_index_index - 1]
+        next_index = self.vertex_indices[(min_vertex_index_index+1)
+                                         % len(self.vertex_indices)]
+        if previous_index < next_index:
+            vertex_indices = self.vertex_indices[:min_vertex_index_index+1][::-1] \
+                             + self.vertex_indices[min_vertex_index_index+1:][::-1]
+        else:
+            vertex_indices = self.vertex_indices[min_vertex_index_index:] \
+                              + self.vertex_indices[:min_vertex_index_index]
+        return type(self)(vertex_indices)
 
     def __eq__(self, other: '_PolygonalCircuit') -> bool:
         # pylint: disable=protected-access

+ 31 - 1
tests/test_polygonal_circuit.py

@@ -4,10 +4,20 @@ from freesurfer_surface import _PolygonalCircuit
 
 
 @pytest.mark.parametrize(('source_vertex_indices', 'expected_vertex_indices'), [
+    ((1,), (1,)),
+    ((1, 2), (1, 2)),
+    ((2, 1), (1, 2)),
     ((1, 2, 3), (1, 2, 3)),
     ((2, 3, 1), (1, 2, 3)),
     ((3, 1, 2), (1, 2, 3)),
-    ((1,), (1,)),
+    ((1, 3, 2), (1, 2, 3)),
+    ((2, 1, 3), (1, 2, 3)),
+    ((3, 2, 1), (1, 2, 3)),
+    ((1, 2, 3, 5), (1, 2, 3, 5)),
+    ((2, 3, 5, 1), (1, 2, 3, 5)),
+    ((3, 5, 1, 2), (1, 2, 3, 5)),
+    ((2, 1, 5, 3), (1, 2, 3, 5)),
+    ((5, 3, 2, 1), (1, 2, 3, 5)),
 ])
 def test__normalize(source_vertex_indices, expected_vertex_indices):
     # pylint: disable=protected-access
@@ -27,6 +37,13 @@ def test_eq():
     assert not _PolygonalCircuit((0, 1, 2)) == _PolygonalCircuit((4, 1, 2))
 
 
+def test_eq_reverse():
+    assert _PolygonalCircuit((0, 1, 2)) == _PolygonalCircuit((2, 1, 0))
+    assert _PolygonalCircuit((0, 1, 2)) == _PolygonalCircuit((0, 2, 1))
+    assert _PolygonalCircuit((0, 1, 2, 4)) == _PolygonalCircuit((4, 2, 1, 0))
+    assert _PolygonalCircuit((0, 1, 2, 4)) == _PolygonalCircuit((1, 0, 4, 2))
+
+
 def test_hash():
     assert hash(_PolygonalCircuit((0, 1, 2))) \
         == hash(_PolygonalCircuit((0, 1, 2)))
@@ -40,3 +57,16 @@ def test_hash():
         != hash(_PolygonalCircuit((0, 4, 2)))
     assert hash(_PolygonalCircuit((0, 1, 2))) \
         != hash(_PolygonalCircuit((4, 1, 2)))
+
+
+def test_hash_reverse():
+    assert hash(_PolygonalCircuit((0, 1, 2))) \
+        == hash(_PolygonalCircuit((2, 1, 0)))
+    assert hash(_PolygonalCircuit((0, 1, 2))) \
+        == hash(_PolygonalCircuit((0, 2, 1)))
+    assert hash(_PolygonalCircuit((0, 1, 2, 4))) \
+        == hash(_PolygonalCircuit((4, 2, 1, 0)))
+    assert hash(_PolygonalCircuit((0, 1, 2, 4))) \
+        == hash(_PolygonalCircuit((1, 0, 4, 2)))
+    assert hash(_PolygonalCircuit((0, 1, 2, 4))) \
+        != hash(_PolygonalCircuit((1, 4, 0, 2)))