Skip to content

Commit 210dad6

Browse files
fsalmoirmtezzele
authored andcommitted
Bounding box (#71)
* first commit for utilities * some tests written * added documentation for utilities * some variable name made more clear * method write_original_box and write_modified_box merged in write_bounding_box * added vtk file and check on vtk version to make test passing on travis
1 parent 2af12e7 commit 210dad6

File tree

9 files changed

+258
-1
lines changed

9 files changed

+258
-1
lines changed

docs/source/code.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,5 @@ Code Documentation
1515
vtkhandler
1616
unvhandler
1717
igeshandler
18+
utilities
1819

docs/source/utilities.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Utilities
2+
=====================
3+
4+
.. automodule:: pygem.utilities
5+
:members:

pygem/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11

2-
__all__ = ['affine', 'filehandler', 'freeform', 'openfhandler', 'params', 'stlhandler', 'unvhandler', 'vtkhandler', 'igeshandler']
2+
__all__ = ['affine', 'filehandler', 'freeform', 'openfhandler', 'params', 'stlhandler', 'unvhandler', 'vtkhandler', 'igeshandler', 'utilities']
33

44
from . import affine
55
from . import freeform
@@ -10,3 +10,4 @@
1010
from . import unvhandler
1111
from . import vtkhandler
1212
from . import igeshandler
13+
from . import utilities

pygem/utilities.py

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
"""
2+
Auxiliary utilities for PyGeM.
3+
"""
4+
5+
import vtk
6+
import vtk.util.numpy_support as ns
7+
import numpy as np
8+
9+
# TODO: add the connectivity to the ffd control points to visualize the lattice.
10+
11+
12+
13+
def write_bounding_box(parameters, outfile, write_deformed=True):
14+
"""
15+
Method that writes a vtk file containing the FFD lattice. This method
16+
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+
20+
:param FFDParameters parameters: parameters of the Free Form Deformation.
21+
: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+
25+
:Example:
26+
27+
>>> import pygem.utilities as util
28+
>>> import pygem.params as pars
29+
>>> import numpy as np
30+
31+
>>> params = pars.FFDParameters()
32+
>>> 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')
34+
"""
35+
36+
aux_x = np.linspace(0, parameters.lenght_box_x, parameters.n_control_points[0])
37+
aux_y = np.linspace(0, parameters.lenght_box_y, parameters.n_control_points[1])
38+
aux_z = np.linspace(0, parameters.lenght_box_z, parameters.n_control_points[2])
39+
lattice_y_coords, lattice_x_coords, lattice_z_coords = np.meshgrid(aux_y, aux_x, aux_z)
40+
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+
48+
n_rows = box_points.shape[1]
49+
50+
box_points = np.dot(parameters.rotation_matrix,box_points) + np.transpose(np.tile(parameters.origin_box, (n_rows,1)))
51+
52+
_write_vtk_box(box_points, outfile)
53+
54+
55+
def _write_vtk_box(box_points, filename):
56+
"""
57+
Method that writes a vtk file containing FFD control points.
58+
59+
:param numpy.ndarray box_points: coordinates of the FFD control points.
60+
:param string filename: name of the output file.
61+
"""
62+
# setup points and vertices
63+
points = vtk.vtkPoints()
64+
vertices = vtk.vtkCellArray()
65+
66+
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])
68+
vertices.InsertNextCell(1)
69+
vertices.InsertCellPoint(id)
70+
71+
polydata = vtk.vtkPolyData()
72+
polydata.SetPoints(points)
73+
polydata.SetVerts(vertices)
74+
75+
polydata.Modified()
76+
writer = vtk.vtkDataSetWriter()
77+
writer.SetFileName(filename)
78+
79+
if vtk.VTK_MAJOR_VERSION <= 5:
80+
polydata.Update()
81+
writer.SetInput(polydata)
82+
else:
83+
writer.SetInputData(polydata)
84+
85+
writer.Write()
86+
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# vtk DataFile Version 3.0
2+
vtk output
3+
ASCII
4+
DATASET POLYDATA
5+
POINTS 12 float
6+
-20 -55 -45 -4.37166 -55 43.6327 -42.9398 31.9333 -40.9551
7+
-27.3115 31.9333 47.6776 1.40315 -49.1766 -48.774 17.0315 -49.1766 39.8587
8+
-21.5367 37.7568 -44.729 -5.90834 37.7568 43.9037 52.8929 -77.5023 -149.241
9+
38.4346 -43.3531 36.0848 -0.133524 43.5802 -48.503 15.4948 43.5802 40.1297
10+
11+
VERTICES 12 24
12+
1 0
13+
1 1
14+
1 2
15+
1 3
16+
1 4
17+
1 5
18+
1 6
19+
1 7
20+
1 8
21+
1 9
22+
1 10
23+
1 11
24+
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# vtk DataFile Version 4.0
2+
vtk output
3+
ASCII
4+
DATASET POLYDATA
5+
POINTS 12 float
6+
-20 -55 -45 -4.37166 -55 43.6327 -42.9398 31.9333 -40.9551
7+
-27.3115 31.9333 47.6776 1.40315 -49.1766 -48.774 17.0315 -49.1766 39.8587
8+
-21.5367 37.7568 -44.729 -5.90834 37.7568 43.9037 52.8929 -77.5023 -149.241
9+
38.4346 -43.3531 36.0848 -0.133524 43.5802 -48.503 15.4948 43.5802 40.1297
10+
11+
VERTICES 12 24
12+
1 0
13+
1 1
14+
1 2
15+
1 3
16+
1 4
17+
1 5
18+
1 6
19+
1 7
20+
1 8
21+
1 9
22+
1 10
23+
1 11
24+
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# vtk DataFile Version 3.0
2+
vtk output
3+
ASCII
4+
DATASET POLYDATA
5+
POINTS 12 float
6+
-20 -55 -45 -4.37166 -55 43.6327 -42.9398 31.9333 -40.9551
7+
-27.3115 31.9333 47.6776 1.40315 -49.1766 -48.774 17.0315 -49.1766 39.8587
8+
-21.5367 37.7568 -44.729 -5.90834 37.7568 43.9037 22.8063 -43.3531 -52.5479
9+
38.4346 -43.3531 36.0848 -0.133524 43.5802 -48.503 15.4948 43.5802 40.1297
10+
11+
VERTICES 12 24
12+
1 0
13+
1 1
14+
1 2
15+
1 3
16+
1 4
17+
1 5
18+
1 6
19+
1 7
20+
1 8
21+
1 9
22+
1 10
23+
1 11
24+
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# vtk DataFile Version 4.0
2+
vtk output
3+
ASCII
4+
DATASET POLYDATA
5+
POINTS 12 float
6+
-20 -55 -45 -4.37166 -55 43.6327 -42.9398 31.9333 -40.9551
7+
-27.3115 31.9333 47.6776 1.40315 -49.1766 -48.774 17.0315 -49.1766 39.8587
8+
-21.5367 37.7568 -44.729 -5.90834 37.7568 43.9037 22.8063 -43.3531 -52.5479
9+
38.4346 -43.3531 36.0848 -0.133524 43.5802 -48.503 15.4948 43.5802 40.1297
10+
11+
VERTICES 12 24
12+
1 0
13+
1 1
14+
1 2
15+
1 3
16+
1 4
17+
1 5
18+
1 6
19+
1 7
20+
1 8
21+
1 9
22+
1 10
23+
1 11
24+

tests/test_utilities.py

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
2+
from unittest import TestCase
3+
import unittest
4+
import pygem.utilities as util
5+
import pygem.params as pars
6+
import numpy as np
7+
import filecmp
8+
import os
9+
10+
11+
class TestVtkHandler(TestCase):
12+
13+
14+
def test_utilities_write_original_box(self):
15+
params = pars.FFDParameters()
16+
params.read_parameters(filename='tests/test_datasets/parameters_test_ffd_sphere.prm')
17+
18+
outfilename = 'tests/test_datasets/box_test_sphere.vtk'
19+
20+
util.write_bounding_box(params, outfilename, False)
21+
os.remove(outfilename)
22+
23+
24+
def test_utilities_write_modified_box(self):
25+
params = pars.FFDParameters()
26+
params.read_parameters(filename='tests/test_datasets/parameters_test_ffd_sphere.prm')
27+
28+
outfilename = 'tests/test_datasets/box_test_sphere.vtk'
29+
30+
util.write_bounding_box(params, outfilename)
31+
os.remove(outfilename)
32+
33+
34+
def test_utilities_check_vtk_original_box(self):
35+
import vtk
36+
37+
params = pars.FFDParameters()
38+
params.read_parameters(filename='tests/test_datasets/parameters_test_ffd_sphere.prm')
39+
40+
outfilename = 'tests/test_datasets/box_test_sphere.vtk'
41+
if vtk.VTK_MAJOR_VERSION <= 5:
42+
outfilename_expected = 'tests/test_datasets/box_test_sphere_true_version5.vtk'
43+
else:
44+
outfilename_expected = 'tests/test_datasets/box_test_sphere_true_version6.vtk'
45+
46+
util.write_bounding_box(params, outfilename, False)
47+
48+
self.assertTrue(filecmp.cmp(outfilename, outfilename_expected))
49+
os.remove(outfilename)
50+
51+
52+
def test_utilities_check_vtk_modified_box(self):
53+
import vtk
54+
55+
params = pars.FFDParameters()
56+
params.read_parameters(filename='tests/test_datasets/parameters_test_ffd_sphere.prm')
57+
58+
outfilename = 'tests/test_datasets/box_test_sphere.vtk'
59+
if vtk.VTK_MAJOR_VERSION <= 5:
60+
outfilename_expected = 'tests/test_datasets/box_modified_test_sphere_true_version5.vtk'
61+
else:
62+
outfilename_expected = 'tests/test_datasets/box_modified_test_sphere_true_version6.vtk'
63+
64+
util.write_bounding_box(params, outfilename)
65+
66+
self.assertTrue(filecmp.cmp(outfilename, outfilename_expected))
67+
os.remove(outfilename)
68+

0 commit comments

Comments
 (0)