|
| 1 | +""" |
| 2 | +Auxiliary utilities for PyGeM. |
| 3 | +""" |
| 4 | + |
| 5 | +import vtk |
| 6 | +import vtk.util.numpy_support as ns |
| 7 | +import numpy as np |
| 8 | + |
| 9 | +# TODO: add the connectivity to the ffd control points to visualize the lattice. |
| 10 | + |
| 11 | + |
| 12 | + |
| 13 | +def write_bounding_box(parameters, outfile, write_deformed=True): |
| 14 | + """ |
| 15 | + Method that writes a vtk file containing the FFD lattice. This method |
| 16 | + allows to visualize where the FFD control points are located before the geometrical morphing. |
| 17 | + If the flag is set to original (default) the method writes out the undeformed lattice, |
| 18 | + if it is set to modified it writes out the deformed lattice. |
| 19 | + |
| 20 | + :param FFDParameters parameters: parameters of the Free Form Deformation. |
| 21 | + :param string outfile: name of the output file. |
| 22 | + :param bool write_deformed: flag to write the original or modified FFD control lattice. |
| 23 | + The default is set to deformed. |
| 24 | + |
| 25 | + :Example: |
| 26 | + |
| 27 | + >>> import pygem.utilities as util |
| 28 | + >>> import pygem.params as pars |
| 29 | + >>> import numpy as np |
| 30 | + |
| 31 | + >>> params = pars.FFDParameters() |
| 32 | + >>> params.read_parameters(filename='tests/test_datasets/parameters_test_ffd_sphere.prm') |
| 33 | + >>> util.write_bounding_box(params, 'tests/test_datasets/box_test_sphere.vtk') |
| 34 | + """ |
| 35 | + |
| 36 | + aux_x = np.linspace(0, parameters.lenght_box_x, parameters.n_control_points[0]) |
| 37 | + aux_y = np.linspace(0, parameters.lenght_box_y, parameters.n_control_points[1]) |
| 38 | + aux_z = np.linspace(0, parameters.lenght_box_z, parameters.n_control_points[2]) |
| 39 | + lattice_y_coords, lattice_x_coords, lattice_z_coords = np.meshgrid(aux_y, aux_x, aux_z) |
| 40 | + |
| 41 | + if write_deformed == False: |
| 42 | + box_points = np.array([lattice_x_coords.ravel(), lattice_y_coords.ravel(), lattice_z_coords.ravel()]) |
| 43 | + if write_deformed == True: |
| 44 | + box_points = np.array([lattice_x_coords.ravel() + parameters.array_mu_x.ravel()*parameters.lenght_box_x, \ |
| 45 | + lattice_y_coords.ravel() + parameters.array_mu_y.ravel()*parameters.lenght_box_y, \ |
| 46 | + lattice_z_coords.ravel() + parameters.array_mu_z.ravel()*parameters.lenght_box_z]) |
| 47 | + |
| 48 | + n_rows = box_points.shape[1] |
| 49 | + |
| 50 | + box_points = np.dot(parameters.rotation_matrix,box_points) + np.transpose(np.tile(parameters.origin_box, (n_rows,1))) |
| 51 | + |
| 52 | + _write_vtk_box(box_points, outfile) |
| 53 | + |
| 54 | + |
| 55 | +def _write_vtk_box(box_points, filename): |
| 56 | + """ |
| 57 | + Method that writes a vtk file containing FFD control points. |
| 58 | + |
| 59 | + :param numpy.ndarray box_points: coordinates of the FFD control points. |
| 60 | + :param string filename: name of the output file. |
| 61 | + """ |
| 62 | + # setup points and vertices |
| 63 | + points = vtk.vtkPoints() |
| 64 | + vertices = vtk.vtkCellArray() |
| 65 | + |
| 66 | + for index in range(0, box_points.shape[1]): |
| 67 | + id = points.InsertNextPoint(box_points[0, index], box_points[1, index], box_points[2, index]) |
| 68 | + vertices.InsertNextCell(1) |
| 69 | + vertices.InsertCellPoint(id) |
| 70 | + |
| 71 | + polydata = vtk.vtkPolyData() |
| 72 | + polydata.SetPoints(points) |
| 73 | + polydata.SetVerts(vertices) |
| 74 | + |
| 75 | + polydata.Modified() |
| 76 | + writer = vtk.vtkDataSetWriter() |
| 77 | + writer.SetFileName(filename) |
| 78 | + |
| 79 | + if vtk.VTK_MAJOR_VERSION <= 5: |
| 80 | + polydata.Update() |
| 81 | + writer.SetInput(polydata) |
| 82 | + else: |
| 83 | + writer.SetInputData(polydata) |
| 84 | + |
| 85 | + writer.Write() |
| 86 | + |
0 commit comments