Browse Source

added Vertex.distance_mm()

Fabian Peter Hammerle 5 years ago
parent
commit
d4d50f724e
3 changed files with 35 additions and 0 deletions
  1. 3 0
      freesurfer_surface/__init__.py
  2. 17 0
      tests/test_surface.py
  3. 15 0
      tests/test_vertex.py

+ 3 - 0
freesurfer_surface/__init__.py

@@ -98,6 +98,9 @@ class Vertex(numpy.ndarray):
     def __repr__(self) -> str:
         return '{}({})'.format(type(self).__name__, self.__format_coords())
 
+    def distance_mm(self, other: 'Vertex') -> float:
+        return numpy.linalg.norm(self - other)
+
 
 class PolygonalCircuit:
 

+ 17 - 0
tests/test_surface.py

@@ -493,3 +493,20 @@ def test_remove_unused_vertices_none():
     surface.remove_unused_vertices()
     assert len(surface.vertices) == 155622
     assert len(surface.triangles) == 311240
+
+
+def test_remove_unused_vertices_single():
+    surface = Surface.read_triangular(SURFACE_FILE_PATH)
+    assert len(surface.vertices) == 155622
+    assert len(surface.triangles) == 311240
+    assert surface.triangles[-1] == Triangle((136143, 138007, 137078))
+    surface.triangles = list(filter(lambda t: 42 not in t.vertex_indices,
+                                    surface.triangles))
+    assert surface._unused_vertices() == {42}
+    surface.remove_unused_vertices()
+    assert len(surface.vertices) == 155622 - 1
+    assert len(surface.triangles) == 311240 - 7
+    assert surface.triangles[-1] == Triangle((136142, 138006, 137077))
+    assert all(vertex_index < len(surface.vertices)
+               for triangle in surface.triangles
+               for vertex_index in triangle.vertex_indices)

+ 15 - 0
tests/test_vertex.py

@@ -39,3 +39,18 @@ def test_vars():
     assert attrs['right'] == pytest.approx(-1.5)
     assert attrs['anterior'] == pytest.approx(4)
     assert attrs['superior'] == pytest.approx(2)
+
+
+@pytest.mark.parametrize(('vertex_a', 'vertex_b', 'expected_distance_mm'), [
+    (Vertex(0, 0, 0), Vertex(0, 0, 0), 0),
+    (Vertex(0, 0, 0), Vertex(1, 0, 0), 1),
+    (Vertex(0, 0, 0), Vertex(0, 1, 0), 1),
+    (Vertex(0, 0, 0), Vertex(0, 0, 1), 1),
+    (Vertex(0, 0, 0), Vertex(1, 1, 0), 2**(1/2)),
+    (Vertex(0, 0, 0), Vertex(1, 1, 1), 3**(1/2)),
+    (Vertex(1, 2, 3), Vertex(2, 3, 4), 3**(1/2)),
+    (Vertex(1, 2, 3), Vertex(5, 8, -1), (16+36+16)**(1/2)),
+])
+def test_distance(vertex_a, vertex_b, expected_distance_mm):
+    assert vertex_a.distance_mm(vertex_b) \
+        == pytest.approx(expected_distance_mm)