Browse Source

added geometry._collinear

Fabian Peter Hammerle 5 years ago
parent
commit
8645672933
2 changed files with 27 additions and 0 deletions
  1. 7 0
      freesurfer_surface/geometry.py
  2. 20 0
      tests/geometry/test_collinear.py

+ 7 - 0
freesurfer_surface/geometry.py

@@ -0,0 +1,7 @@
+import numpy
+
+
+def _collinear(vector_a: numpy.ndarray, vector_b: numpy.ndarray) -> bool:
+    # null vector: https://math.stackexchange.com/a/1772580
+    return numpy.allclose(numpy.cross(vector_a, vector_b),
+                          numpy.zeros(len(vector_a)))

+ 20 - 0
tests/geometry/test_collinear.py

@@ -0,0 +1,20 @@
+import pytest
+
+from freesurfer_surface.geometry import _collinear
+
+
+@pytest.mark.parametrize(('vector_a', 'vector_b', 'collinear'), [
+    ([1, 0, 0], [1, 0, 0], True),
+    ([1, 0, 0], [2, 0, 0], True),
+    ([1, 0, 0], [-2, 0, 0], True),
+    ([1, 0, 0], [-2, 0, 0], True),
+    ([1, 2, 3], [1, 2, 3], True),
+    ([1, 2, 3], [1, 2, 4], False),
+    ([1, 2, 3], [2, 4, 6], True),
+    ([1, 2, 3], [2, 4, 7], False),
+    ([1, 2, 3], [2, 5, 6], False),
+    ([1, 2, 3], [3, 4, 6], False),
+])
+def test__collinear(vector_a, vector_b, collinear):
+    # pylint: disable=protected-access
+    assert _collinear(vector_a, vector_b) == collinear