1
1
import marching_cubes
2
- import numpy
2
+ import numpy as np
3
3
import pytest
4
4
import os
5
5
6
+ from laplacian_smooth import laplacian_smooth
6
7
7
8
@pytest .fixture
8
9
def volume ():
9
10
vol_path = os .path .join (os .path .split (__file__ )[0 ], "data/input/sample.npy" )
10
- return numpy .load (vol_path )
11
+ return np .load (vol_path )
11
12
12
13
13
14
@pytest .fixture
14
15
def mesh_loader ():
15
16
"""Load mesh data from npz file"""
16
17
17
18
def _loader (mesh_file_name ):
18
- data = numpy .load (mesh_file_name )
19
+ data = np .load (mesh_file_name )
19
20
vertices = data ["vertices" ]
20
21
normals = data ["normals" ]
21
22
faces = data ["faces" ]
@@ -36,6 +37,69 @@ def test_regression(volume, mesh_loader, smoothing, reference_mesh_file):
36
37
37
38
ref_vertices , ref_normals , ref_faces = mesh_loader (reference_mesh_file )
38
39
39
- numpy .testing .assert_array_almost_equal (vertices , ref_vertices )
40
- numpy .testing .assert_array_almost_equal (normals , ref_normals )
41
- numpy .testing .assert_array_almost_equal (faces , ref_faces )
40
+ np .testing .assert_array_almost_equal (vertices , ref_vertices )
41
+ np .testing .assert_array_almost_equal (normals , ref_normals )
42
+ assert (faces == ref_faces ).all ()
43
+
44
+
45
+ def test_smoothing (volume ):
46
+ ROUNDS = 3
47
+ vertices , normals , faces = marching_cubes .march (volume , 0 )
48
+ smoothed_vertices , smoothed_normals , smoothed_faces = marching_cubes .march (volume , ROUNDS )
49
+
50
+ # Compare with our reference implementation of laplacian smoothing.
51
+ ref_smoothed_vertices = laplacian_smooth (vertices , faces , ROUNDS )
52
+ np .allclose (smoothed_vertices , ref_smoothed_vertices , rtol = 0.001 )
53
+
54
+ assert (faces == smoothed_faces ).all (), \
55
+ "Smoothing should not affect face definitions."
56
+
57
+ assert not (normals == smoothed_normals ).all (), \
58
+ "Normals should not be the same after smoothing."
59
+
60
+
61
+ def test_reference_smoothing_trivial ():
62
+ """
63
+ This is a simple test of our laplacian_smoothing reference function.
64
+ """
65
+ vertices = np .array ([[0.0 , 0.0 , 0.0 ],
66
+ [0.0 , 0.0 , 1.0 ],
67
+ [0.0 , 0.0 , 2.0 ]])
68
+
69
+ # This "face" is actually straight line,
70
+ # which makes it easy to see what's going on
71
+ faces = np .array ([[0 ,1 ,2 ]])
72
+ average_vertex = vertices .sum (axis = 0 ) / 3
73
+ vertices = laplacian_smooth (vertices , faces , 1 )
74
+ assert (vertices == average_vertex ).all ()
75
+
76
+
77
+ def test_reference_smoothing_hexagon ():
78
+ """
79
+ This is a simple test of our laplacian_smoothing reference function.
80
+ Try 'smoothing' a simple 2D hexagon, which is an easy case to understand.
81
+ """
82
+ # This map is correctly labeled with the vertex indices
83
+ _ = - 1
84
+ hexagon = [[[_ ,_ ,_ ,_ ,_ ,_ ,_ ],
85
+ [_ ,_ ,0 ,_ ,1 ,_ ,_ ],
86
+ [_ ,_ ,_ ,_ ,_ ,_ ,_ ],
87
+ [_ ,2 ,_ ,3 ,_ ,4 ,_ ],
88
+ [_ ,_ ,_ ,_ ,_ ,_ ,_ ],
89
+ [_ ,_ ,5 ,_ ,6 ,_ ,_ ],
90
+ [_ ,_ ,_ ,_ ,_ ,_ ,_ ]]]
91
+
92
+ hexagon = 1 + np .array (hexagon )
93
+ original_vertices = np .transpose (hexagon .nonzero ())
94
+ faces = [[3 ,1 ,4 ],
95
+ [3 ,4 ,6 ],
96
+ [3 ,6 ,5 ],
97
+ [3 ,5 ,2 ],
98
+ [3 ,2 ,0 ],
99
+ [3 ,0 ,1 ]]
100
+
101
+ vertices = laplacian_smooth (original_vertices , faces , 1 )
102
+
103
+ # Since vertex 3 is exactly centered between the rest,
104
+ # its location never changes.
105
+ assert (vertices [3 ] == original_vertices [3 ]).all ()
0 commit comments