Skip to content

Commit 2406a4a

Browse files
authored
Merge pull request #90 from mtezzele/utils
utility to plot rbf control points
2 parents 9d11782 + e878111 commit 2406a4a

File tree

3 files changed

+68
-13
lines changed

3 files changed

+68
-13
lines changed

pygem/stlhandler.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ def plot(self, plot_file=None, save_fig=False):
8686
8787
:param string plot_file: the stl filename you want to plot.
8888
:param bool save_fig: a flag to save the figure in png or not. If True the
89-
plot is not shown.
89+
plot is not shown. The default value is False.
9090
9191
:return: figure: matlplotlib structure for the figure of the chosen geometry
9292
:rtype: matplotlib.pyplot.figure
@@ -104,7 +104,7 @@ def plot(self, plot_file=None, save_fig=False):
104104
stl_mesh = mesh.Mesh.from_file(plot_file)
105105
axes.add_collection3d(mplot3d.art3d.Poly3DCollection(stl_mesh.vectors))
106106

107-
## Get the limits of the axis and center the geometry
107+
# Get the limits of the axis and center the geometry
108108
max_dim = np.array([np.max(stl_mesh.vectors[:,:,0]), \
109109
np.max(stl_mesh.vectors[:,:,1]), \
110110
np.max(stl_mesh.vectors[:,:,2])])
@@ -123,5 +123,5 @@ def plot(self, plot_file=None, save_fig=False):
123123
else:
124124
figure.savefig(plot_file.split('.')[0] + '.png')
125125

126-
return figure
126+
return figure
127127

pygem/utils.py

Lines changed: 48 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
"""
44
import vtk
55
import numpy as np
6+
import matplotlib.pyplot as plt
7+
68

79
def write_bounding_box(parameters, outfile, write_deformed=True):
810
"""
@@ -32,29 +34,30 @@ def write_bounding_box(parameters, outfile, write_deformed=True):
3234
lattice_y_coords, lattice_x_coords, lattice_z_coords = np.meshgrid(aux_y, aux_x, aux_z)
3335

3436
if write_deformed:
35-
box_points = np.array([lattice_x_coords.ravel() + parameters.array_mu_x.ravel() * parameters.lenght_box_x,\
37+
box_points = np.array([ \
38+
lattice_x_coords.ravel() + parameters.array_mu_x.ravel() * parameters.lenght_box_x, \
3639
lattice_y_coords.ravel() + parameters.array_mu_y.ravel() * parameters.lenght_box_y, \
3740
lattice_z_coords.ravel() + parameters.array_mu_z.ravel() * parameters.lenght_box_z])
3841
else:
3942
box_points = np.array([lattice_x_coords.ravel(), lattice_y_coords.ravel(), \
4043
lattice_z_coords.ravel()])
41-
44+
4245
n_rows = box_points.shape[1]
4346

4447
box_points = np.dot(parameters.rotation_matrix, box_points) + \
4548
np.transpose(np.tile(parameters.origin_box, (n_rows, 1)))
46-
49+
4750
# step necessary to set the correct order to the box points for vtkStructuredGrid:
4851
# Data in vtkStructuredGrid are ordered with x increasing fastest, then y, then z
4952
dims = lattice_y_coords.shape
50-
aux_xx = box_points[0,:].reshape(dims).ravel(order='f')
51-
aux_yy = box_points[1,:].reshape(dims).ravel(order='f')
52-
aux_zz = box_points[2,:].reshape(dims).ravel(order='f')
53+
aux_xx = box_points[0, :].reshape(dims).ravel(order='f')
54+
aux_yy = box_points[1, :].reshape(dims).ravel(order='f')
55+
aux_zz = box_points[2, :].reshape(dims).ravel(order='f')
5356
reordered_box_points = np.array((aux_xx, aux_yy, aux_zz))
5457

5558
_write_vtk_box(reordered_box_points, outfile, parameters.n_control_points)
56-
57-
59+
60+
5861
def _write_vtk_box(box_points, filename, dimensions):
5962
"""
6063
Private method that writes a vtk file containing FFD control points.
@@ -91,3 +94,40 @@ def _write_vtk_box(box_points, filename, dimensions):
9194

9295
writer.Write()
9396

97+
98+
def plot_rbf_control_points(parameters, save_fig=False):
99+
"""
100+
Method to plot the control points of a RBFParameters class. It is possible to save the
101+
resulting figure.
102+
103+
:param bool save_fig: a flag to save the figure in png or not. If True the
104+
plot is not shown and the figure is saved with the name 'RBF_control_points.png'.
105+
The default value is False.
106+
"""
107+
fig = plt.figure(1)
108+
axes = fig.add_subplot(111, projection='3d')
109+
orig = axes.scatter(parameters.original_control_points[:, 0], \
110+
parameters.original_control_points[:, 1], \
111+
parameters.original_control_points[:, 2], c='blue', marker='o')
112+
defor = axes.scatter(parameters.deformed_control_points[:, 0], \
113+
parameters.deformed_control_points[:, 1], \
114+
parameters.deformed_control_points[:, 2], c='red', marker='x')
115+
116+
axes.set_xlabel('X axis')
117+
axes.set_ylabel('Y axis')
118+
axes.set_zlabel('Z axis')
119+
120+
plt.legend((orig, defor), \
121+
('Original', 'Deformed'), \
122+
scatterpoints=1, \
123+
loc='lower left', \
124+
ncol=2, \
125+
fontsize=10)
126+
127+
# Show the plot to the screen
128+
if not save_fig:
129+
plt.show()
130+
else:
131+
fig.savefig('RBF_control_points.png')
132+
133+

tests/test_utils.py

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ def test_utils_write_original_box(self):
1818

1919
outfilename = 'tests/test_datasets/box_test_sphere.vtk'
2020

21-
ut.write_bounding_box(params, outfilename, False)
21+
ut.write_bounding_box(params, outfilename, write_deformed=False)
2222
os.remove(outfilename)
2323

2424

@@ -42,7 +42,7 @@ def test_utils_check_vtk_original_box(self):
4242
else:
4343
outfilename_expected = 'tests/test_datasets/box_test_sphere_true_version6.vtk'
4444

45-
ut.write_bounding_box(params, outfilename, False)
45+
ut.write_bounding_box(params, outfilename, write_deformed=False)
4646

4747
self.assertTrue(filecmp.cmp(outfilename, outfilename_expected))
4848
os.remove(outfilename)
@@ -63,3 +63,18 @@ def test_utils_check_vtk_modified_box(self):
6363
self.assertTrue(filecmp.cmp(outfilename, outfilename_expected))
6464
os.remove(outfilename)
6565

66+
67+
def test_utils_plot_rbf_control_points(self):
68+
params = pars.RBFParameters()
69+
params.read_parameters(filename='tests/test_datasets/parameters_rbf_cube.prm')
70+
ut.plot_rbf_control_points(params, save_fig=False)
71+
72+
73+
def test_utils_plot_rbf_control_points_save_fig(self):
74+
params = pars.RBFParameters()
75+
params.read_parameters(filename='tests/test_datasets/parameters_rbf_cube.prm')
76+
ut.plot_rbf_control_points(params, save_fig=True)
77+
self.assertTrue(os.path.isfile('RBF_control_points.png'))
78+
os.remove('RBF_control_points.png')
79+
80+

0 commit comments

Comments
 (0)