test_intersect.py 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. import numpy
  2. import pytest
  3. from freesurfer_surface.geometry \
  4. import _Line, _intersect_planes, _intersect_line_segments
  5. @pytest.mark.parametrize(
  6. ('normal_vector_a', 'constant_a',
  7. 'normal_vector_b', 'constant_b',
  8. 'expected_line'),
  9. [([0, 0, 1], 0, [0, 1, 0], 0, _Line([0, 0, 0], [1, 0, 0])),
  10. ([0, 0, 1], 0, [0, 3, 0], 0, _Line([0, 0, 0], [1, 0, 0])),
  11. ([0, 0, 1], 0, [4, 0, 0], 0, _Line([0, 0, 0], [0, 1, 0])),
  12. ([0, 2, 2], 0, [3, 0, 3], 0, _Line([0, 0, 0], [1, 1, -1])),
  13. ([1, 2, 4], 0, [2, 3, 5], 0, _Line([0, 0, 0], [-2, 3, -1])),
  14. ([1, 2, 4], 0, [2, 3, 5], 2, _Line([2, 1, -1], [-2, 3, -1])),
  15. ([2, 3, 5], 2, [1, 2, 4], 0, _Line([2, 1, -1], [-2, 3, -1])),
  16. ([2, 3, 5], 2, [1, 2, 4], 7, _Line([-7, -3, 5], [-2, 3, -1])),
  17. ([1, 2, 4], 0, [2, 4, 8], 1, False),
  18. ([1, 2, 4], 0, [2, 4, 8], 0, True)],
  19. )
  20. def test__intersect_planes(normal_vector_a, constant_a,
  21. normal_vector_b, constant_b,
  22. expected_line):
  23. line = _intersect_planes(normal_vector_a, constant_a,
  24. normal_vector_b, constant_b)
  25. assert line == expected_line
  26. if not isinstance(expected_line, bool):
  27. assert numpy.isclose(numpy.inner(normal_vector_a, line.vector), 0)
  28. assert numpy.isclose(numpy.inner(normal_vector_b, line.vector), 0)
  29. assert numpy.isclose(numpy.inner(normal_vector_a, line.point),
  30. constant_a)
  31. assert numpy.isclose(numpy.inner(normal_vector_b, line.point),
  32. constant_b)
  33. other_point = line.point + line.vector
  34. assert numpy.isclose(numpy.inner(normal_vector_a, other_point),
  35. constant_a)
  36. assert numpy.isclose(numpy.inner(normal_vector_b, other_point),
  37. constant_b)
  38. @pytest.mark.parametrize(('points_a', 'points_b', 'expected_point'), [
  39. (([0, 0, 0], [0, 0, 2]), ([0, -1, 1], [0, 1, 1]), [0, 0, 1]),
  40. (([0, 0, 0], [0, 0, 2]), ([0, 0, 1], [0, 1, 1]), [0, 0, 1]),
  41. (([0, 0, 0], [0, 0, 2]), ([0, -1, 1], [0, 0, 1]), [0, 0, 1]),
  42. (([0, 0, 0], [0, 0, 2]), ([0, 0, 1], [0, -1, 1]), [0, 0, 1]),
  43. (([0, 0, 0], [2, 4, 8]), ([0, 0, 4], [2, 4, 4]), [1, 2, 4]),
  44. (([0, 0, 0], [2, 4, 8]), ([0, 0, 2], [2, 4, 2]), [0.5, 1, 2]),
  45. (([2, 4, 8], [0, 0, 0]), ([0, 0, 2], [2, 4, 2]), [0.5, 1, 2]),
  46. (([2, 4, 8], [0, 0, 0]), ([2, 4, 2], [0, 0, 2]), [0.5, 1, 2]),
  47. (([3, 5, 9], [1, 1, 1]), ([3, 5, 3], [1, 1, 3]), [1.5, 2, 3]),
  48. (([0, 0, 0], [0, 0, 4]), ([0, -8, 0], [0, 8, 0]), [0, 0, 0]),
  49. (([0, 0, 0], [0, 0, 4]), ([0, -8, 3], [0, 8, 3]), [0, 0, 3]),
  50. (([0, 0, 0], [0, 0, 4]), ([0, -8, 4], [0, 8, 4]), [0, 0, 4]),
  51. (([0, 0, 0], [0, 0, 4]), ([0, -8, 4.1], [0, 8, 4.1]), False),
  52. (([0, 0, 0], [0, 0, 4]), ([0, -8, -1], [0, 8, -1]), False),
  53. (([0, 0, 0], [0, 0, 4]), ([0, 0, 0], [0, 0, 4]), True),
  54. ])
  55. def test_intersect_line_segments(points_a, points_b, expected_point):
  56. # pylint: disable=protected-access
  57. point = _intersect_line_segments(numpy.array(points_a),
  58. numpy.array(points_b))
  59. if isinstance(expected_point, bool):
  60. assert isinstance(point, bool)
  61. assert point == expected_point
  62. else:
  63. assert numpy.allclose(point, expected_point)