test_surface.py 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725
  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 copy
  18. import datetime
  19. import unittest.mock
  20. import numpy
  21. import pytest
  22. from conftest import ANNOTATION_FILE_PATH, SURFACE_FILE_PATH
  23. from freesurfer_surface import (
  24. Annotation,
  25. LineSegment,
  26. PolygonalCircuit,
  27. Surface,
  28. Triangle,
  29. Vertex,
  30. setlocale,
  31. )
  32. # pylint: disable=protected-access
  33. def test_read_triangular():
  34. surface = Surface.read_triangular(SURFACE_FILE_PATH)
  35. assert surface.creator == b"fabianpeter"
  36. assert surface.creation_datetime == datetime.datetime(2019, 5, 9, 22, 37, 41)
  37. assert len(surface.vertices) == 155622
  38. assert len(surface.triangles) == 311240
  39. assert not surface.using_old_real_ras
  40. assert surface.volume_geometry_info == (
  41. b"valid = 1 # volume info valid\n",
  42. b"filename = ../mri/filled-pretess255.mgz\n",
  43. b"volume = 256 256 256\n",
  44. b"voxelsize = 1.000000000000000e+00 1.000000000000000e+00 1.000000000000000e+00\n",
  45. b"xras = -1.000000000000000e+00 0.000000000000000e+00 1.862645149230957e-09\n",
  46. b"yras = 0.000000000000000e+00 -6.655682227574289e-09 -1.000000000000000e+00\n",
  47. b"zras = 0.000000000000000e+00 1.000000000000000e+00 -8.300048648379743e-09\n",
  48. b"cras = -2.773597717285156e+00 1.566547393798828e+01 -7.504364013671875e+00\n",
  49. )
  50. assert surface.command_lines == [
  51. b"mris_remove_intersection ../surf/lh.orig ../surf/lh.orig"
  52. b" ProgramVersion: $Name: stable6 $"
  53. b" TimeStamp: 2019/05/09-17:42:36-GMT"
  54. b" BuildTimeStamp: Jan 18 2017 16:38:58"
  55. b" CVS: $Id: mris_remove_intersection.c,v 1.6 2011/03/02 00:04:32 nicks Exp $"
  56. b" User: fabianpeter"
  57. b" Machine: host12345"
  58. b" Platform: Linux"
  59. b" PlatformVersion: 4.15.0-46-generic"
  60. b" CompilerName: GCC"
  61. b" CompilerVersion: 40400"
  62. b" ",
  63. b"mris_make_surfaces -orig_white white.preaparc -orig_pial white.preaparc"
  64. b" -aseg ../mri/aseg.presurf -mgz -T1 brain.finalsurfs"
  65. b" fabian20190509 lh ProgramVersion: $Name: $"
  66. b" TimeStamp: 2019/05/09-20:27:28-GMT"
  67. b" BuildTimeStamp: Jan 18 2017 16:38:58"
  68. b" CVS: $Id: mris_make_surfaces.c,v 1.164.2.4 2016/12/13 22:26:32 zkaufman Exp $"
  69. b" User: fabianpeter"
  70. b" Machine: host12345"
  71. b" Platform: Linux"
  72. b" PlatformVersion: 4.15.0-46-generic"
  73. b" CompilerName: GCC"
  74. b" CompilerVersion: 40400"
  75. b" ",
  76. ]
  77. def test_read_triangular_locale():
  78. with setlocale("de_AT.utf8"):
  79. surface = Surface.read_triangular(SURFACE_FILE_PATH)
  80. assert surface.creation_datetime == datetime.datetime(2019, 5, 9, 22, 37, 41)
  81. @pytest.mark.parametrize(
  82. ("creation_datetime", "expected_str"),
  83. [
  84. (datetime.datetime(2019, 5, 9, 22, 37, 41), b"Thu May 9 22:37:41 2019"),
  85. (datetime.datetime(2019, 4, 24, 23, 29, 22), b"Wed Apr 24 23:29:22 2019"),
  86. ],
  87. )
  88. def test_triangular_strftime(creation_datetime, expected_str):
  89. # pylint: disable=protected-access
  90. assert expected_str == Surface._triangular_strftime(creation_datetime)
  91. def test_read_write_triangular_same(tmpdir):
  92. surface = Surface.read_triangular(SURFACE_FILE_PATH)
  93. output_path = tmpdir.join("surface").strpath
  94. surface.write_triangular(output_path, creation_datetime=surface.creation_datetime)
  95. with open(output_path, "rb") as output_file:
  96. with open(SURFACE_FILE_PATH, "rb") as expected_file:
  97. assert expected_file.read() == output_file.read()
  98. def test_read_write_datetime(tmpdir):
  99. surface = Surface.read_triangular(SURFACE_FILE_PATH)
  100. original_creation_datetime = surface.creation_datetime
  101. output_path = tmpdir.join("surface").strpath
  102. surface.write_triangular(output_path)
  103. assert original_creation_datetime == surface.creation_datetime
  104. new_surface = Surface.read_triangular(output_path)
  105. assert new_surface.creation_datetime > original_creation_datetime
  106. assert datetime.datetime.now() > new_surface.creation_datetime
  107. assert (
  108. datetime.datetime.now() - new_surface.creation_datetime
  109. ) < datetime.timedelta(seconds=20)
  110. def test_write_read_triangular_same(tmpdir):
  111. expected_surface = Surface()
  112. expected_surface.creator = b"pytest"
  113. expected_surface.creation_datetime = datetime.datetime.now().replace(microsecond=0)
  114. expected_surface.vertices = [
  115. Vertex(0.0, 0.0, 0.0),
  116. Vertex(1.0, 2.0, 3.0),
  117. Vertex(2.0, 4.0, 6.0),
  118. Vertex(3.0, 5.0, 7.0),
  119. ]
  120. expected_surface.triangles = [
  121. Triangle((0, 1, 2)),
  122. Triangle((0, 1, 3)),
  123. Triangle((3, 2, 1)),
  124. ]
  125. expected_surface.using_old_real_ras = False
  126. expected_surface.volume_geometry_info = tuple(b"?\n" for _ in range(8))
  127. expected_surface.command_lines = [b"?", b"!"]
  128. output_path = tmpdir.join("surface").strpath
  129. expected_surface.write_triangular(
  130. output_path, creation_datetime=expected_surface.creation_datetime
  131. )
  132. resulted_surface = Surface.read_triangular(output_path)
  133. assert numpy.array_equal(expected_surface.vertices, resulted_surface.vertices)
  134. expected_surface.vertices = resulted_surface.vertices = []
  135. assert vars(expected_surface) == vars(resulted_surface)
  136. def test_write_triangular_same_locale(tmpdir):
  137. surface = Surface()
  138. surface.creator = b"pytest"
  139. surface.volume_geometry_info = tuple(b"?" for _ in range(8))
  140. creation_datetime = datetime.datetime(2018, 12, 31, 21, 42)
  141. output_path = tmpdir.join("surface").strpath
  142. with setlocale("de_AT.utf8"):
  143. surface.write_triangular(output_path, creation_datetime=creation_datetime)
  144. resulted_surface = Surface.read_triangular(output_path)
  145. assert resulted_surface.creation_datetime == creation_datetime
  146. with open(output_path, "rb") as output_file:
  147. assert (
  148. output_file.read()
  149. .split(b" on ")[1]
  150. .startswith(b"Mon Dec 31 21:42:00 2018\n")
  151. )
  152. def test_load_annotation():
  153. surface = Surface.read_triangular(SURFACE_FILE_PATH)
  154. assert not surface.annotation
  155. surface.load_annotation_file(ANNOTATION_FILE_PATH)
  156. assert isinstance(surface.annotation, Annotation)
  157. assert len(surface.annotation.vertex_label_index) == 155622
  158. assert surface.annotation.vertex_label_index[0] == 5
  159. def test_add_vertex():
  160. surface = Surface()
  161. assert not surface.vertices
  162. assert surface.add_vertex(Vertex(1.0, 1.5, 2.0)) == 0
  163. assert len(surface.vertices) == 1
  164. assert surface.vertices[0].anterior == pytest.approx(1.5)
  165. assert surface.add_vertex(Vertex(-3.0, 0.0, 4.0)) == 1
  166. assert len(surface.vertices) == 2
  167. assert surface.vertices[1].right == pytest.approx(-3.0)
  168. @pytest.mark.parametrize(
  169. "vertices_coords",
  170. [
  171. ((0, 0, 0), (2, 4, 0), (2, 4, 3)),
  172. ((0, 0, 0), (2, 4, 0), (2, 4, 3), (0, 0, 3)),
  173. ((1, 1, 0), (3, 5, 0), (3, 5, 3), (1, 1, 3)),
  174. ((1, 1, 7), (3, 5, 7), (3, 5, 3), (1, 1, 3)),
  175. ((1, 1, 1), (3, 5, 7), (3, 5, 9), (1, 1, 3)),
  176. ((3, 5, 7), (1, 1, 1), (1, 1, 3)),
  177. ((3, 5, 7), (1, 1, 1), (1, 1, 3), (3, 5, 9)),
  178. ],
  179. )
  180. def test_add_rectangle(vertices_coords):
  181. surface = Surface()
  182. for vertex_coords in vertices_coords:
  183. surface.add_vertex(Vertex(*(float(c) for c in vertex_coords)))
  184. surface.add_rectangle(range(len(vertices_coords)))
  185. assert len(surface.vertices) == 4
  186. assert len(surface.triangles) == 2
  187. assert surface.triangles[0].vertex_indices == (0, 1, 2)
  188. assert surface.triangles[1].vertex_indices == (2, 3, 0)
  189. @pytest.mark.parametrize(
  190. ("vertices_coords", "expected_extra_vertex_coords"),
  191. [
  192. (((0, 0, 0), (2, 4, 0), (2, 4, 3)), (0, 0, 3)),
  193. (((1, 1, 0), (3, 5, 0), (3, 5, 3)), (1, 1, 3)),
  194. (((1, 1, 7), (3, 5, 7), (3, 5, 3)), (1, 1, 3)),
  195. (((1, 1, 1), (3, 5, 7), (3, 5, 9)), (1, 1, 3)),
  196. (((3, 5, 7), (1, 1, 1), (1, 1, 3)), (3, 5, 9)),
  197. ],
  198. )
  199. def test_add_rectangle_3(vertices_coords, expected_extra_vertex_coords):
  200. surface = Surface()
  201. for vertex_coords in vertices_coords:
  202. surface.add_vertex(Vertex(*(float(c) for c in vertex_coords)))
  203. surface.add_rectangle(range(3))
  204. assert tuple(surface.vertices[3]) == pytest.approx(expected_extra_vertex_coords)
  205. def test__triangle_count_by_adjacent_vertex_indices_empty():
  206. surface = Surface()
  207. assert surface._triangle_count_by_adjacent_vertex_indices() == {}
  208. def test__triangle_count_by_adjacent_vertex_indices_none():
  209. surface = Surface()
  210. surface.vertices.append(Vertex(1, 0, 0))
  211. surface.vertices.append(Vertex(2, 0, 0))
  212. surface.vertices.append(Vertex(3, 0, 0))
  213. assert surface._triangle_count_by_adjacent_vertex_indices() == {0: {}, 1: {}, 2: {}}
  214. def test__triangle_count_by_adjacent_vertex_indices_single():
  215. surface = Surface()
  216. surface.triangles.append(
  217. Triangle([surface.add_vertex(Vertex(i, 0, 0)) for i in range(3)])
  218. )
  219. assert surface._triangle_count_by_adjacent_vertex_indices() == {
  220. 0: {1: 1, 2: 1},
  221. 1: {0: 1, 2: 1},
  222. 2: {0: 1, 1: 1},
  223. }
  224. def test__triangle_count_by_adjacent_vertex_indices_multiple():
  225. surface = Surface()
  226. for i in range(5):
  227. surface.add_vertex(Vertex(i, 0, 0))
  228. surface.triangles.append(Triangle((0, 1, 2)))
  229. surface.triangles.append(Triangle((3, 1, 2)))
  230. assert surface._triangle_count_by_adjacent_vertex_indices() == {
  231. 0: {1: 1, 2: 1},
  232. 1: {0: 1, 2: 2, 3: 1},
  233. 2: {0: 1, 1: 2, 3: 1},
  234. 3: {1: 1, 2: 1},
  235. 4: {},
  236. }
  237. surface.triangles.append(Triangle((3, 4, 2)))
  238. assert surface._triangle_count_by_adjacent_vertex_indices() == {
  239. 0: {1: 1, 2: 1},
  240. 1: {0: 1, 2: 2, 3: 1},
  241. 2: {0: 1, 1: 2, 3: 2, 4: 1},
  242. 3: {1: 1, 2: 2, 4: 1},
  243. 4: {2: 1, 3: 1},
  244. }
  245. surface.triangles.append(Triangle((3, 0, 2)))
  246. assert surface._triangle_count_by_adjacent_vertex_indices() == {
  247. 0: {1: 1, 2: 2, 3: 1},
  248. 1: {0: 1, 2: 2, 3: 1},
  249. 2: {0: 2, 1: 2, 3: 3, 4: 1},
  250. 3: {0: 1, 1: 1, 2: 3, 4: 1},
  251. 4: {2: 1, 3: 1},
  252. }
  253. def test__triangle_count_by_adjacent_vertex_indices_real():
  254. surface = Surface.read_triangular(SURFACE_FILE_PATH)
  255. counts = surface._triangle_count_by_adjacent_vertex_indices()
  256. assert len(counts) == len(surface.vertices)
  257. assert all(counts.values())
  258. assert all(
  259. count == 2
  260. for vertex_counts in counts.values()
  261. for count in vertex_counts.values()
  262. )
  263. assert (
  264. sum(
  265. count
  266. for vertex_counts in counts.values()
  267. for count in vertex_counts.values()
  268. )
  269. == len(surface.triangles) * 6
  270. )
  271. def test_find_borders_none():
  272. surface = Surface.read_triangular(SURFACE_FILE_PATH)
  273. assert not list(surface.find_borders())
  274. def test_find_borders_single():
  275. surface = Surface.read_triangular(SURFACE_FILE_PATH)
  276. single_index = surface.add_vertex(Vertex(0, 21, 42))
  277. borders = list(surface.find_borders())
  278. assert len(borders) == 1
  279. assert borders[0] == PolygonalCircuit((single_index,))
  280. def test_find_borders_singles():
  281. surface = Surface.read_triangular(SURFACE_FILE_PATH)
  282. single_indices = [surface.add_vertex(Vertex(i, 21, 42)) for i in range(3)]
  283. borders = set(surface.find_borders())
  284. assert len(borders) == 3
  285. assert PolygonalCircuit((single_indices[0],)) in borders
  286. assert PolygonalCircuit((single_indices[1],)) in borders
  287. assert PolygonalCircuit((single_indices[2],)) in borders
  288. def test_find_borders_single_triangle_simple():
  289. surface = Surface()
  290. vertex_indices = [surface.add_vertex(Vertex(i, 21, 42)) for i in range(3)]
  291. surface.triangles.append(Triangle(vertex_indices))
  292. borders = set(surface.find_borders())
  293. assert len(borders) == 1
  294. assert PolygonalCircuit(vertex_indices) in borders
  295. def test_find_borders_single_triangle_real():
  296. surface = Surface.read_triangular(SURFACE_FILE_PATH)
  297. vertex_indices = [surface.add_vertex(Vertex(i, 21, 42)) for i in range(3)]
  298. surface.triangles.append(Triangle(vertex_indices))
  299. borders = set(surface.find_borders())
  300. assert len(borders) == 1
  301. assert PolygonalCircuit(vertex_indices) in borders
  302. def test_find_borders_remove_triangle():
  303. surface = Surface.read_triangular(SURFACE_FILE_PATH)
  304. triangle = surface.triangles.pop()
  305. borders = set(surface.find_borders())
  306. assert len(borders) == 1
  307. assert triangle in borders
  308. def test_find_borders_remove_non_adjacent_triangles():
  309. surface = Surface.read_triangular(SURFACE_FILE_PATH)
  310. triangles = [surface.triangles.pop(), surface.triangles.pop()]
  311. borders = set(surface.find_borders())
  312. assert len(borders) == 2
  313. assert triangles[0] in borders
  314. assert triangles[1] in borders
  315. def test_find_borders_remove_adjacent_triangles():
  316. surface = Surface.read_triangular(SURFACE_FILE_PATH)
  317. triangles = [surface.triangles.pop(), surface.triangles.pop()]
  318. triangles.append(surface.triangles.pop(270682))
  319. assert triangles[1] == Triangle((136141, 136142, 137076))
  320. assert triangles[2] == Triangle((136141, 136142, 135263))
  321. borders = set(surface.find_borders())
  322. assert len(borders) == 2
  323. assert triangles[0] in borders
  324. assert PolygonalCircuit((137076, 136141, 135263, 136142)) in borders
  325. surface.triangles.pop(270682)
  326. borders = set(surface.find_borders())
  327. assert len(borders) == 2
  328. assert triangles[0] in borders
  329. assert PolygonalCircuit((137076, 136141, 135263, 135264, 136142)) in borders
  330. surface.triangles.pop(274320)
  331. borders = set(surface.find_borders())
  332. assert len(borders) == 2
  333. assert PolygonalCircuit((136143, 138007, 138008, 137078)) in borders
  334. assert PolygonalCircuit((137076, 136141, 135263, 135264, 136142)) in borders
  335. @pytest.mark.parametrize(
  336. ("label_name", "expected_border_lens"),
  337. [
  338. ("precentral", [416]),
  339. ("postcentral", [395]),
  340. ("medialorbitofrontal", [6, 246]),
  341. # ...--2343 2347
  342. # \ / \
  343. # 2345 2348
  344. # / \ /
  345. # ...--2344 2346
  346. ("posteriorcingulate", [4, 190]),
  347. ("unknown", [3, 390]),
  348. ],
  349. )
  350. def test_find_borders_real(label_name, expected_border_lens):
  351. surface = Surface.read_triangular(SURFACE_FILE_PATH)
  352. surface.load_annotation_file(ANNOTATION_FILE_PATH)
  353. (label,) = filter(
  354. lambda l: l.name == label_name, surface.annotation.labels.values()
  355. )
  356. surface.triangles = list(
  357. filter(
  358. lambda t: all(
  359. surface.annotation.vertex_label_index[vertex_idx] == label.index
  360. for vertex_idx in t.vertex_indices
  361. ),
  362. surface.triangles,
  363. )
  364. )
  365. surface.remove_unused_vertices()
  366. borders = list(surface.find_borders())
  367. border_lens = [len(b.vertex_indices) for b in borders]
  368. # self-crossing borders may or may not be split into
  369. # separate polygonal circuits
  370. assert sorted(border_lens) == expected_border_lens or sum(border_lens) == sum(
  371. expected_border_lens
  372. )
  373. def test__get_vertex_label_index():
  374. surface = Surface.read_triangular(SURFACE_FILE_PATH)
  375. surface.load_annotation_file(ANNOTATION_FILE_PATH)
  376. # pylint: disable=protected-access
  377. assert surface._get_vertex_label_index(64290) == 22
  378. assert surface._get_vertex_label_index(72160) == 22
  379. assert surface._get_vertex_label_index(84028) == 24
  380. assert surface._get_vertex_label_index(97356) == 24
  381. assert surface._get_vertex_label_index(123173) == 27
  382. assert surface._get_vertex_label_index(140727) == 27
  383. assert surface._get_vertex_label_index(93859) == 28
  384. assert surface._get_vertex_label_index(78572) == 0
  385. assert surface._get_vertex_label_index(120377) == 0
  386. vertex_index = surface.add_vertex(Vertex(0.0, 21.0, 42.0))
  387. assert surface._get_vertex_label_index(vertex_index) is None
  388. del surface.annotation.vertex_label_index[140727]
  389. assert surface._get_vertex_label_index(140727) is None
  390. def test__find_label_border_segments():
  391. surface = Surface.read_triangular(SURFACE_FILE_PATH)
  392. surface.load_annotation_file(ANNOTATION_FILE_PATH)
  393. (precentral_label,) = filter(
  394. lambda l: l.name == "precentral", surface.annotation.labels.values()
  395. )
  396. # pylint: disable=protected-access
  397. border_segments = set(surface._find_label_border_segments(precentral_label))
  398. assert len(border_segments) == 417
  399. assert LineSegment((33450, 32065)) in border_segments
  400. assert LineSegment((33454, 33450)) in border_segments
  401. for border_vertex_index in [33450, 33454, 32065]:
  402. assert (
  403. surface.annotation.vertex_label_index[border_vertex_index]
  404. == precentral_label.index
  405. )
  406. for other_vertex_index in [32064, 33449, 33455, 33449, 33455]:
  407. assert (
  408. LineSegment((other_vertex_index, border_vertex_index))
  409. not in border_segments
  410. )
  411. assert (
  412. LineSegment((border_vertex_index, other_vertex_index))
  413. not in border_segments
  414. )
  415. def test__find_label_border_segments_incomplete_annotation():
  416. surface = Surface.read_triangular(SURFACE_FILE_PATH)
  417. surface.load_annotation_file(ANNOTATION_FILE_PATH)
  418. (precentral_label,) = filter(
  419. lambda l: l.name == "precentral", surface.annotation.labels.values()
  420. )
  421. # pylint: disable=protected-access
  422. assert surface._find_label_border_segments(precentral_label)
  423. surface.triangles.append(
  424. Triangle(
  425. [
  426. surface.add_vertex(Vertex(0.0, 21.0 * factor, 42.0 * factor))
  427. for factor in range(3)
  428. ]
  429. )
  430. )
  431. border_segments = set(surface._find_label_border_segments(precentral_label))
  432. assert len(border_segments) == 417
  433. def test_find_label_border_polygonal_chains():
  434. surface = Surface.read_triangular(SURFACE_FILE_PATH)
  435. surface.load_annotation_file(ANNOTATION_FILE_PATH)
  436. (precentral_label,) = filter(
  437. lambda l: l.name == "precentral", surface.annotation.labels.values()
  438. )
  439. (border_chain,) = surface.find_label_border_polygonal_chains(precentral_label)
  440. vertex_indices_normalized = list(border_chain.normalized().vertex_indices)
  441. assert len(vertex_indices_normalized) == 418
  442. assert vertex_indices_normalized[66:73] == [
  443. 61044,
  444. 62119,
  445. 62118,
  446. 62107,
  447. 62118,
  448. 63255,
  449. 63264,
  450. ]
  451. assert vertex_indices_normalized[:4] == [32065, 32072, 32073, 32080]
  452. assert vertex_indices_normalized[-4:] == [36281, 34870, 33454, 33450]
  453. def test_find_label_border_polygonal_chains_long_leaf():
  454. surface = Surface()
  455. with unittest.mock.patch.object(
  456. surface,
  457. "_find_label_border_segments",
  458. return_value=[
  459. LineSegment((0, 1)),
  460. LineSegment((1, 2)),
  461. LineSegment((0, 3)),
  462. LineSegment((2, 3)),
  463. LineSegment((2, 4)),
  464. LineSegment((4, 5)), # leaf
  465. ],
  466. ):
  467. (border_chain,) = surface.find_label_border_polygonal_chains("dummy")
  468. assert list(border_chain.normalized().vertex_indices) == [0, 1, 2, 4, 5, 4, 2, 3]
  469. def test__unused_vertices():
  470. surface = Surface()
  471. assert not surface._unused_vertices()
  472. for i in range(4):
  473. surface.add_vertex(Vertex(i, i, i))
  474. assert surface._unused_vertices() == {0, 1, 2, 3}
  475. surface.triangles.append(Triangle((0, 2, 3)))
  476. assert surface._unused_vertices() == {1}
  477. surface.triangles.append(Triangle((0, 3, 1)))
  478. assert not surface._unused_vertices()
  479. del surface.triangles[0]
  480. assert surface._unused_vertices() == {2}
  481. def test__unused_vertices_real():
  482. surface = Surface.read_triangular(SURFACE_FILE_PATH)
  483. assert not surface._unused_vertices()
  484. surface.triangles = list(
  485. filter(lambda t: 42 not in t.vertex_indices, surface.triangles)
  486. )
  487. assert surface._unused_vertices() == {42}
  488. def test_remove_unused_vertices_all():
  489. surface = Surface()
  490. for i in range(5):
  491. surface.add_vertex(Vertex(i, i, i))
  492. assert len(surface.vertices) == 5
  493. surface.remove_unused_vertices()
  494. assert not surface.vertices
  495. surface.remove_unused_vertices()
  496. assert not surface.vertices
  497. def test_remove_unused_vertices_almost_all():
  498. surface = Surface()
  499. for i in range(5):
  500. surface.add_vertex(Vertex(i, i, i))
  501. assert len(surface.vertices) == 5
  502. surface.triangles.append(Triangle((0, 2, 3)))
  503. surface.remove_unused_vertices()
  504. assert len(surface.vertices) == 3
  505. assert surface.vertices[0] == pytest.approx(Vertex(0, 0, 0))
  506. assert surface.vertices[1] == pytest.approx(Vertex(2, 2, 2))
  507. assert surface.vertices[2] == pytest.approx(Vertex(3, 3, 3))
  508. assert len(surface.triangles) == 1
  509. assert surface.triangles[0] == Triangle((0, 1, 2))
  510. del surface.triangles[0]
  511. surface.remove_unused_vertices()
  512. assert not surface.vertices
  513. def test_remove_unused_vertices_some():
  514. surface = Surface()
  515. for i in range(9):
  516. surface.add_vertex(Vertex(i, i, i))
  517. surface.triangles.append(Triangle((0, 2, 3)))
  518. surface.triangles.append(Triangle((3, 4, 5)))
  519. surface.triangles.append(Triangle((3, 2, 5)))
  520. surface.triangles.append(Triangle((3, 2, 8)))
  521. surface.remove_unused_vertices()
  522. assert len(surface.vertices) == 6
  523. assert surface.vertices[0] == pytest.approx(Vertex(0, 0, 0))
  524. assert surface.vertices[1] == pytest.approx(Vertex(2, 2, 2))
  525. assert surface.vertices[2] == pytest.approx(Vertex(3, 3, 3))
  526. assert surface.vertices[3] == pytest.approx(Vertex(4, 4, 4))
  527. assert surface.vertices[4] == pytest.approx(Vertex(5, 5, 5))
  528. assert surface.vertices[5] == pytest.approx(Vertex(8, 8, 8))
  529. assert len(surface.triangles) == 4
  530. assert surface.triangles[0] == Triangle((0, 1, 2))
  531. assert surface.triangles[1] == Triangle((2, 3, 4))
  532. assert surface.triangles[2] == Triangle((2, 1, 4))
  533. assert surface.triangles[3] == Triangle((2, 1, 5))
  534. surface.remove_unused_vertices()
  535. assert len(surface.triangles) == 4
  536. def test_remove_unused_vertices_none():
  537. surface = Surface.read_triangular(SURFACE_FILE_PATH)
  538. assert len(surface.vertices) == 155622
  539. assert len(surface.triangles) == 311240
  540. surface.remove_unused_vertices()
  541. assert len(surface.vertices) == 155622
  542. assert len(surface.triangles) == 311240
  543. def test_remove_unused_vertices_single():
  544. surface = Surface.read_triangular(SURFACE_FILE_PATH)
  545. assert len(surface.vertices) == 155622
  546. assert len(surface.triangles) == 311240
  547. assert surface.triangles[-1] == Triangle((136143, 138007, 137078))
  548. surface.triangles = list(
  549. filter(lambda t: 42 not in t.vertex_indices, surface.triangles)
  550. )
  551. assert surface._unused_vertices() == {42}
  552. surface.remove_unused_vertices()
  553. assert len(surface.vertices) == 155622 - 1
  554. assert len(surface.triangles) == 311240 - 7
  555. assert surface.triangles[-1] == Triangle((136142, 138006, 137077))
  556. assert all(
  557. vertex_index < len(surface.vertices)
  558. for triangle in surface.triangles
  559. for vertex_index in triangle.vertex_indices
  560. )
  561. def test_select_vertices():
  562. surface = Surface()
  563. for i in range(4):
  564. surface.add_vertex(Vertex(i, i, i))
  565. assert numpy.allclose(
  566. surface.select_vertices([2, 1]), [surface.vertices[2], surface.vertices[1]]
  567. )
  568. assert numpy.allclose(
  569. surface.select_vertices([3, 2]), [surface.vertices[3], surface.vertices[2]]
  570. )
  571. assert numpy.allclose(surface.select_vertices((3, 2)), [[3, 3, 3], [2, 2, 2]])
  572. assert numpy.allclose(
  573. surface.select_vertices(filter(lambda i: i % 2 == 1, range(4))),
  574. [[1, 1, 1], [3, 3, 3]],
  575. )
  576. def test_unite_2():
  577. surface_a = Surface()
  578. for i in range(0, 4):
  579. surface_a.add_vertex(Vertex(i, i, i))
  580. surface_a.triangles.append(Triangle((0, 1, 2)))
  581. surface_a.triangles.append(Triangle((1, 2, 3)))
  582. surface_b = Surface()
  583. for i in range(10, 14):
  584. surface_b.add_vertex(Vertex(i, i, i))
  585. surface_b.triangles.append(Triangle((0, 1, 3)))
  586. surface_a_copy = copy.deepcopy(surface_a)
  587. surface_b_copy = copy.deepcopy(surface_b)
  588. union = Surface.unite([surface_a, surface_b])
  589. assert numpy.allclose(surface_a.vertices, surface_a_copy.vertices)
  590. assert numpy.allclose(surface_b.vertices, surface_b_copy.vertices)
  591. assert numpy.allclose(union.vertices[:4], surface_a.vertices)
  592. assert numpy.allclose(union.vertices[4:], surface_b.vertices)
  593. assert surface_a.triangles == surface_a_copy.triangles
  594. assert surface_b.triangles == surface_b_copy.triangles
  595. assert union.triangles[:2] == surface_a.triangles
  596. assert union.triangles[2:] == [Triangle((4, 5, 7))]
  597. def test_unite_3():
  598. surface_a = Surface()
  599. for i in range(0, 4):
  600. surface_a.add_vertex(Vertex(i, i, i))
  601. surface_a.triangles.append(Triangle((0, 1, 2)))
  602. surface_a.triangles.append(Triangle((1, 2, 3)))
  603. surface_b = Surface()
  604. for i in range(10, 14):
  605. surface_b.add_vertex(Vertex(i, i, i))
  606. surface_b.triangles.append(Triangle((0, 1, 3)))
  607. surface_c = Surface()
  608. for i in range(20, 23):
  609. surface_c.add_vertex(Vertex(i, i, i))
  610. surface_c.triangles.append(Triangle((0, 1, 2)))
  611. surface_c.triangles.append(Triangle((0, 1, 2)))
  612. union = Surface.unite(filter(None, [surface_a, surface_b, surface_c]))
  613. assert numpy.allclose(union.vertices[:4], surface_a.vertices)
  614. assert numpy.allclose(union.vertices[4:8], surface_b.vertices)
  615. assert numpy.allclose(union.vertices[8:], surface_c.vertices)
  616. assert union.triangles[:2] == surface_a.triangles
  617. assert union.triangles[2:3] == [Triangle((4, 5, 7))]
  618. assert union.triangles[3:] == [Triangle((8, 9, 10)), Triangle((8, 9, 10))]
  619. def test_unite_real():
  620. surface_a = Surface.read_triangular(SURFACE_FILE_PATH)
  621. surface_b = Surface()
  622. for i in range(5):
  623. surface_b.add_vertex(Vertex(i, i, i))
  624. surface_b.triangles.append(Triangle((0, 1, 3)))
  625. surface_b.triangles.append(Triangle((1, 3, 4)))
  626. union = Surface.unite((surface_a, surface_b))
  627. assert numpy.allclose(union.vertices[:-5], surface_a.vertices)
  628. assert numpy.allclose(union.vertices[-5:], surface_b.vertices)
  629. assert union.triangles[:-2] == surface_a.triangles
  630. assert union.triangles[-2:] == [
  631. Triangle((155622, 155623, 155625)),
  632. Triangle((155623, 155625, 155626)),
  633. ]
  634. assert union.creator == surface_a.creator
  635. assert union.creation_datetime == surface_a.creation_datetime
  636. assert union.using_old_real_ras == surface_a.using_old_real_ras
  637. assert union.volume_geometry_info == surface_a.volume_geometry_info
  638. assert union.command_lines == surface_a.command_lines
  639. assert union.annotation == surface_a.annotation