Skip to content

Commit 58e8338

Browse files
committed
radians and write_points_in_vtp
1 parent 62ab172 commit 58e8338

File tree

5 files changed

+105
-6
lines changed

5 files changed

+105
-6
lines changed

pygem/affine.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,11 @@ def angles2matrix(rot_z=0, rot_y=0, rot_x=0):
2727
2828
>>> import pygem.affine as at
2929
>>> import numpy as np
30+
>>> from math import radians
3031
>>> # Example of a rotation around x, y, z axis
31-
>>> rotz = 10*np.pi/180
32-
>>> roty = 20*np.pi/180
33-
>>> rotx = 30*np.pi/180
32+
>>> rotz = radians(10)
33+
>>> roty = radians(20)
34+
>>> rotx = radians(30)
3435
>>> rot_matrix = at.angles2matrix(rotz, roty, rotx)
3536
3637
.. note::

pygem/params.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
from OCC.Bnd import Bnd_Box
1515

1616
import pygem.affine as at
17+
from math import radians
1718

1819

1920
class FFDParameters(object):
@@ -179,9 +180,9 @@ def read_parameters(self, filename='parameters.prm'):
179180
self.array_mu_z[tuple(map(int, values[0:3]))] = float(values[3])
180181

181182
self.rotation_matrix = at.angles2matrix(
182-
self.rot_angle[2] * np.pi / 180,
183-
self.rot_angle[1] * np.pi / 180,
184-
self.rot_angle[0] * np.pi / 180
183+
radians(self.rot_angle[2]),
184+
radians(self.rot_angle[1]),
185+
radians(self.rot_angle[0])
185186
)
186187

187188
self.position_vertex_0 = self.origin_box

pygem/utils.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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()
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?xml version="1.0"?>
2+
<VTKFile type="PolyData" version="0.1" byte_order="LittleEndian" header_type="UInt32" compressor="vtkZLibDataCompressor">
3+
<PolyData>
4+
<Piece NumberOfPoints="4" NumberOfVerts="4" NumberOfLines="0" NumberOfStrips="0" NumberOfPolys="0" >
5+
<PointData Scalars="Colors">
6+
<DataArray type="UInt8" Name="Colors" NumberOfComponents="3" format="appended" RangeMin="255" RangeMax="255" offset="0" />
7+
</PointData>
8+
<CellData>
9+
</CellData>
10+
<Points>
11+
<DataArray type="Float32" Name="Points" NumberOfComponents="3" format="appended" RangeMin="2.2360679775" RangeMax="17.378147197" offset="44" />
12+
</Points>
13+
<Verts>
14+
<DataArray type="Int64" Name="connectivity" format="appended" RangeMin="" RangeMax="" offset="124" />
15+
<DataArray type="Int64" Name="offsets" format="appended" RangeMin="" RangeMax="" offset="176" />
16+
</Verts>
17+
<Lines>
18+
<DataArray type="Int64" Name="connectivity" format="appended" RangeMin="" RangeMax="" offset="228" />
19+
<DataArray type="Int64" Name="offsets" format="appended" RangeMin="" RangeMax="" offset="244" />
20+
</Lines>
21+
<Strips>
22+
<DataArray type="Int64" Name="connectivity" format="appended" RangeMin="" RangeMax="" offset="260" />
23+
<DataArray type="Int64" Name="offsets" format="appended" RangeMin="" RangeMax="" offset="276" />
24+
</Strips>
25+
<Polys>
26+
<DataArray type="Int64" Name="connectivity" format="appended" RangeMin="" RangeMax="" offset="292" />
27+
<DataArray type="Int64" Name="offsets" format="appended" RangeMin="" RangeMax="" offset="308" />
28+
</Polys>
29+
</Piece>
30+
</PolyData>
31+
<AppendedData encoding="base64">
32+
_AQAAAACAAAAMAAAADQAAAA==eJxjYPjPAEMAFfYD/Q==AQAAAACAAAAwAAAAKAAAAA==eJxjYACBBnsg4QBEQNwAxAuA+AAQPwBiBkcGBgEgVgBiA0cAmKMGpA==AQAAAACAAAAgAAAAEwAAAA==eJxjYIAARijNBKWZoTQAAHAABw==AQAAAACAAAAgAAAAEwAAAA==eJxjZIAAJijNDKVZoDQAAMAACw==AAAAAACAAAAAAAAAAAAAAACAAAAAAAAAAAAAAACAAAAAAAAAAAAAAACAAAAAAAAAAAAAAACAAAAAAAAAAAAAAACAAAAAAAAA
33+
</AppendedData>
34+
</VTKFile>

tests/test_utils.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,3 +88,14 @@ def test_utils_plot_rbf_control_points_save_fig(self):
8888
ut.plot_rbf_control_points(params, save_fig=True)
8989
self.assertTrue(os.path.isfile('RBF_control_points.png'))
9090
os.remove('RBF_control_points.png')
91+
92+
def test_utils_check_write_points_in_vtp(self):
93+
ctrl_points = np.arange(12).reshape(4, 3)
94+
95+
outfilename = 'tests/test_datasets/points_test.vtp'
96+
if vtk.VTK_MAJOR_VERSION >= 6:
97+
outfilename_expected = 'tests/test_datasets/points_test_true_version6.vtp'
98+
99+
ut.write_points_in_vtp(ctrl_points, outfile=outfilename)
100+
self.assertTrue(self.cmp(outfilename, outfilename_expected))
101+
os.remove(outfilename)

0 commit comments

Comments
 (0)