test_surface.py 26 KB

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