1313from OCC .BRepMesh import BRepMesh_IncrementalMesh
1414from OCC .Bnd import Bnd_Box
1515
16+ import vtk
1617import pygem .affine as at
1718from math import radians
1819
@@ -23,7 +24,7 @@ class FFDParameters(object):
2324 bounding box and weight of the FFD control points.
2425
2526 :param list n_control_points: number of control points in the x, y, and z
26- direction. If not provided it is set to [2, 2, 2].
27+ direction. If not provided it is set to [2, 2, 2].
2728
2829 :cvar numpy.ndarray length_box: dimension of the FFD bounding box, in the
2930 x, y and z direction (local coordinate system).
@@ -114,7 +115,6 @@ def psi_mapping(self):
114115 """
115116 Map from the physical domain to the reference domain.
116117
117- :return: map from the pysical domain to the reference domain.
118118 :rtype: numpy.ndarray
119119 """
120120 return np .diag (np .reciprocal (self .lenght_box ))
@@ -124,16 +124,17 @@ def inv_psi_mapping(self):
124124 """
125125 Map from the reference domain to the physical domain.
126126
127- :return: map from the reference domain to domain.
128127 :rtype: numpy.ndarray
129128 """
130129 return np .diag (self .lenght_box )
131130
132131 @property
133132 def rotation_matrix (self ):
134133 """
135- :cvar numpy.ndarray rotation_matrix: rotation matrix (according to
136- rot_angle_x, rot_angle_y, rot_angle_z).
134+ The rotation matrix (according to rot_angle_x, rot_angle_y,
135+ rot_angle_z).
136+
137+ :rtype: numpy.ndarray
137138 """
138139 return at .angles2matrix (
139140 radians (self .rot_angle [2 ]), radians (self .rot_angle [1 ]),
@@ -321,6 +322,70 @@ def __str__(self):
321322 string += 'position_vertex_3 = {}\n ' .format (self .position_vertex_3 )
322323 return string
323324
325+ def save (self , filename , write_deformed = True ):
326+ """
327+ Method that writes a vtk file containing the FFD lattice. This method
328+ allows to visualize where the FFD control points are located before the
329+ geometrical morphing. If the `write_deformed` flag is set to True the
330+ method writes out the deformed lattice, otherwise it writes one the
331+ original undeformed lattice.
332+
333+ :param str filename: name of the output file.
334+ :param bool write_deformed: flag to write the original or modified FFD
335+ control lattice. The default is set to True.
336+
337+ :Example:
338+
339+ >>> from pygem.params_ffd import FFDParameters
340+ >>>
341+ >>> params = FFDParameters()
342+ >>> params.read_parameters(
343+ >>> filename='tests/test_datasets/parameters_test_ffd_sphere.prm')
344+ >>> params.save('tests/test_datasets/box_test_sphere.vtk')
345+ """
346+ x = np .linspace (0 , self .lenght_box [0 ], self .n_control_points [0 ])
347+ y = np .linspace (0 , self .lenght_box [1 ], self .n_control_points [1 ])
348+ z = np .linspace (0 , self .lenght_box [2 ], self .n_control_points [2 ])
349+
350+ lattice_y_coords , lattice_x_coords , lattice_z_coords = np .meshgrid (
351+ y , x , z )
352+
353+ if write_deformed :
354+ box_points = np .array ([
355+ lattice_x_coords .ravel () + self .array_mu_x .ravel () *
356+ self .lenght_box [0 ],
357+ lattice_y_coords .ravel () + self .array_mu_y .ravel () *
358+ self .lenght_box [1 ],
359+ lattice_z_coords .ravel () + self .array_mu_z .ravel () *
360+ self .lenght_box [2 ]
361+ ])
362+ else :
363+ box_points = np .array ([
364+ lattice_x_coords .ravel (), lattice_y_coords .ravel (),
365+ lattice_z_coords .ravel ()
366+ ])
367+
368+ n_rows = box_points .shape [1 ]
369+
370+ box_points = np .dot (self .rotation_matrix , box_points ) + np .transpose (
371+ np .tile (self .origin_box , (n_rows , 1 )))
372+
373+ points = vtk .vtkPoints ()
374+
375+ for box_point in box_points .T :
376+ points .InsertNextPoint (box_point [0 ], box_point [1 ], box_point [2 ])
377+
378+ data = vtk .vtkPolyData ()
379+ data .SetPoints (points )
380+
381+ writer = vtk .vtkPolyDataWriter ()
382+ writer .SetFileName (filename )
383+ if vtk .VTK_MAJOR_VERSION <= 5 :
384+ writer .SetInput (data )
385+ else :
386+ writer .SetInputData (data )
387+ writer .Write ()
388+
324389 def build_bounding_box (self ,
325390 shape ,
326391 tol = 1e-6 ,
0 commit comments