@@ -37,6 +37,20 @@ def add_ball(
3737
3838 volume [dist_sq <= radius ** 2 ] = value
3939
40+ def apply_affine_to_polydata (affine :np .ndarray , polydata :vtk .vtkPolyData ) -> vtk .vtkPolyData :
41+ """Apply an affine transform to a vtkPolyData"""
42+ affine_vtkmat = vtk .vtkMatrix4x4 ()
43+ for i in range (4 ):
44+ for j in range (4 ):
45+ affine_vtkmat .SetElement (i , j , affine [i , j ])
46+ affine_vtktransform = vtk .vtkTransform ()
47+ affine_vtktransform .SetMatrix (affine_vtkmat )
48+ transform_filter = vtk .vtkTransformPolyDataFilter ()
49+ transform_filter .SetTransform (affine_vtktransform )
50+ transform_filter .SetInputData (polydata )
51+ transform_filter .Update ()
52+ return transform_filter .GetOutput ()
53+
4054def test_take_largest_connected_component ():
4155 vol_array = np .zeros ((20 ,20 ,20 ))
4256 add_ball (vol_array , (5 ,5 ,5 ), 4 ) # ball of radius 4 at (5,5,5)
@@ -76,11 +90,18 @@ def test_create_closed_surface_from_labelmap():
7690 sphere_radius = 7
7791 sphere_center = np .array ([10 ,10 ,10 ])
7892 add_ball (labelmap , tuple (sphere_center ), sphere_radius )
79- labelmap_vtk = vtk_img_from_array_and_affine (labelmap , affine = np .eye (4 ))
93+ rng = np .random .default_rng (6548 )
94+ affine = np .eye (4 )
95+ affine [:3 ,:3 ] = expm ((lambda A : (A - A .T )/ 2 )(rng .normal (size = (3 ,3 )))) # generate a random orthogonal matrix
96+ affine [:3 ,3 ] = rng .random (3 ) # generate a random origin
97+ labelmap_vtk = vtk_img_from_array_and_affine (labelmap , affine = affine )
8098
8199 # run the algorithm to be tested
82100 surface = create_closed_surface_from_labelmap (labelmap_vtk , decimation_factor = 0.1 )
83101
102+ # the mesh is in "physical space" mapped to by `affine`, transform it back to the ijk space of the original `labelmap`
103+ surface = apply_affine_to_polydata (np .linalg .inv (affine ), surface )
104+
84105 # verify that the points on the generated mesh are not too far off being at distance 7 from the ball center
85106 points = surface .GetPoints ()
86107 for i in range (points .GetNumberOfPoints ()):
0 commit comments