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