test_polygonal_chain.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. # freesurfer-surface - Read and Write Surface Files in Freesurfer’s TriangularSurface Format
  2. #
  3. # Copyright (C) 2020 Fabian Peter Hammerle <fabian@hammerle.me>
  4. #
  5. # This program is free software: you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License as published by
  7. # the Free Software Foundation, either version 3 of the License, or
  8. # any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. import pytest
  18. from freesurfer_surface import (
  19. LineSegment,
  20. PolygonalChain,
  21. PolygonalChainsNotOverlapingError,
  22. )
  23. def test_init():
  24. chain = PolygonalChain((0, 21, 42, 84))
  25. assert tuple(chain.vertex_indices) == (0, 21, 42, 84)
  26. def test_reassign_vertex_indices():
  27. chain = PolygonalChain((0, 21, 42, 84))
  28. chain.vertex_indices = (1, 2, 3, 4)
  29. assert tuple(chain.vertex_indices) == (1, 2, 3, 4)
  30. @pytest.mark.parametrize(
  31. ("indices_init", "indices_normalized"),
  32. (
  33. ([0, 3], [0, 3]),
  34. ([3, 0], [0, 3]),
  35. ([0, 3, 2, 4], [0, 3, 2, 4]),
  36. ([0, 4, 2, 3], [0, 3, 2, 4]),
  37. ([2, 3, 0, 4], [0, 3, 2, 4]),
  38. ([2, 4, 0, 3], [0, 3, 2, 4]),
  39. ([3, 0, 4, 2], [0, 3, 2, 4]),
  40. ([3, 2, 4, 0], [0, 3, 2, 4]),
  41. ([4, 0, 3, 2], [0, 3, 2, 4]),
  42. ([4, 2, 3, 0], [0, 3, 2, 4]),
  43. ),
  44. )
  45. def test_normalized(indices_init, indices_normalized):
  46. assert (
  47. list(PolygonalChain(indices_init).normalized().vertex_indices)
  48. == indices_normalized
  49. )
  50. def test_eq():
  51. assert PolygonalChain((0, 1, 2)) == PolygonalChain((0, 1, 2))
  52. # pylint: disable=unneeded-not
  53. assert not PolygonalChain((0, 1, 2)) == PolygonalChain((0, 1, 4))
  54. assert not PolygonalChain((0, 1, 2)) == PolygonalChain((0, 4, 2))
  55. assert not PolygonalChain((0, 1, 2)) == PolygonalChain((4, 1, 2))
  56. assert not PolygonalChain((0, 1, 2)) == PolygonalChain((1, 2, 3, 4))
  57. def test_eq_normalized():
  58. assert PolygonalChain((0, 1, 2)) == PolygonalChain((0, 2, 1))
  59. assert PolygonalChain((1, 0, 2)) == PolygonalChain((0, 2, 1))
  60. assert PolygonalChain((1, 0, 2, 4)) == PolygonalChain((0, 1, 4, 2))
  61. # pylint: disable=unneeded-not
  62. assert not PolygonalChain((1, 0, 2, 4)) == PolygonalChain((0, 1, 2, 4))
  63. def test_repr():
  64. assert repr(PolygonalChain([])) == "PolygonalChain(vertex_indices=())"
  65. assert repr(PolygonalChain((0, 2, 1))) == "PolygonalChain(vertex_indices=(0, 2, 1))"
  66. assert (
  67. repr(PolygonalChain((0, 2, 1, 4, 3)))
  68. == "PolygonalChain(vertex_indices=(0, 2, 1, 4, 3))"
  69. )
  70. @pytest.mark.parametrize(
  71. ("vertex_indices_a", "vertex_indices_b", "expected_vertex_indices"),
  72. [
  73. ((1, 2, 3), (3, 4), (1, 2, 3, 4)),
  74. ((1, 2, 3), (4, 3), (1, 2, 3, 4)),
  75. ((3, 2, 1), (3, 4), (4, 3, 2, 1)),
  76. ((3, 2, 1), (4, 3), (4, 3, 2, 1)),
  77. ((1,), (1,), (1,)),
  78. ((1, 2), (1,), (1, 2)),
  79. ((1, 2), (2,), (1, 2)),
  80. ((0, 3, 1, 5, 2), (3, 5, 2, 0), (3, 5, 2, 0, 3, 1, 5, 2)),
  81. ((98792, 98807, 98821), (98792, 98793), (98793, 98792, 98807, 98821)),
  82. ],
  83. )
  84. def test_connect(vertex_indices_a, vertex_indices_b, expected_vertex_indices):
  85. chain = PolygonalChain(vertex_indices_a)
  86. chain.connect(PolygonalChain(vertex_indices_b))
  87. assert PolygonalChain(expected_vertex_indices) == chain
  88. @pytest.mark.parametrize(
  89. ("vertex_indices_a", "vertex_indices_b"), [((1, 2, 3), (2, 4))]
  90. )
  91. def test_connect_fail(vertex_indices_a, vertex_indices_b):
  92. chain = PolygonalChain(vertex_indices_a)
  93. with pytest.raises(PolygonalChainsNotOverlapingError):
  94. chain.connect(PolygonalChain(vertex_indices_b))
  95. @pytest.mark.parametrize(
  96. ("vertex_indices_a", "vertex_indices_b"), [((1, 2, 3), ()), ((), (3, 4))]
  97. )
  98. def test_connect_fail_empty(vertex_indices_a, vertex_indices_b):
  99. chain = PolygonalChain(vertex_indices_a)
  100. with pytest.raises(Exception):
  101. chain.connect(PolygonalChain(vertex_indices_b))
  102. def test_adjacent_vertex_indices_1():
  103. chain = PolygonalChain((0, 1, 4, 8))
  104. pairs = list(chain.adjacent_vertex_indices(1))
  105. assert len(pairs) == 4
  106. assert pairs[0] == (0,)
  107. assert pairs[1] == (1,)
  108. assert pairs[2] == (4,)
  109. assert pairs[3] == (8,)
  110. def test_adjacent_vertex_indices_2():
  111. chain = PolygonalChain((0, 1, 4, 8))
  112. pairs = list(chain.adjacent_vertex_indices(2))
  113. assert len(pairs) == 3
  114. assert pairs[0] == (0, 1)
  115. assert pairs[1] == (1, 4)
  116. assert pairs[2] == (4, 8)
  117. def test_adjacent_vertex_indices_3():
  118. chain = PolygonalChain((0, 1, 4, 8))
  119. pairs = list(chain.adjacent_vertex_indices(3))
  120. assert len(pairs) == 2
  121. assert pairs[0] == (0, 1, 4)
  122. assert pairs[1] == (1, 4, 8)
  123. def test_segments():
  124. chain = PolygonalChain((0, 1, 4, 8))
  125. segments = list(chain.segments())
  126. assert len(segments) == 3
  127. assert segments[0] == LineSegment((0, 1))
  128. assert segments[1] == LineSegment((1, 4))
  129. assert segments[2] == LineSegment((4, 8))