@@ -141,3 +141,55 @@ def plot_rbf_control_points(parameters, save_fig=False):
141141 plt .show ()
142142 else :
143143 fig .savefig ('RBF_control_points.png' )
144+
145+
146+ def write_points_in_vtp (points , outfile = 'points.vtp' , color = None ):
147+ """
148+ Method that writes a vtp file containing the given points. This method allows
149+ to visualize where the FFD control points are located before the geometrical
150+ morphing.
151+
152+ :param numpy.ndarray points: coordinates of the points. The shape has to be (n_points, 3).
153+ :param string outfile: name of the output file. The extension has to be .vtp. Default is 'points.vtp'.
154+ :param tuple color: tuple defining the RGB color to assign to all the points. Default is
155+ blue: (0, 0, 255).
156+
157+ :Example:
158+
159+ >>> import pygem.utils as ut
160+ >>> import numpy as np
161+
162+ >>> ctrl_points = np.arange(9).reshape(3, 3)
163+ >>> ut.write_points_in_vtp(ctrl_points, 'example_points.vtp', color=(255, 0, 0))
164+ """
165+ if color is None :
166+ color = (0 , 0 , 255 )
167+ # setup points and vertices
168+ Points = vtk .vtkPoints ()
169+ Vertices = vtk .vtkCellArray ()
170+
171+ Colors = vtk .vtkUnsignedCharArray ()
172+ Colors .SetNumberOfComponents (3 )
173+ Colors .SetName ("Colors" )
174+
175+ for i in range (points .shape [0 ]):
176+ ind = Points .InsertNextPoint (points [i ][0 ], points [i ][1 ], points [i ][2 ])
177+ Vertices .InsertNextCell (1 )
178+ Vertices .InsertCellPoint (ind )
179+ Colors .InsertNextTuple3 (color [0 ], color [1 ], color [2 ])
180+
181+ polydata = vtk .vtkPolyData ()
182+ polydata .SetPoints (Points )
183+ polydata .SetVerts (Vertices )
184+ polydata .GetPointData ().SetScalars (Colors )
185+ polydata .Modified ()
186+ if vtk .VTK_MAJOR_VERSION <= 5 :
187+ polydata .Update ()
188+
189+ writer = vtk .vtkXMLPolyDataWriter ()
190+ writer .SetFileName (outfile )
191+ if vtk .VTK_MAJOR_VERSION <= 5 :
192+ writer .SetInput (polydata )
193+ else :
194+ writer .SetInputData (polydata )
195+ writer .Write ()
0 commit comments