From bfdf3dbce1a83d1b8aa9db4c0c672a3798df1e05 Mon Sep 17 00:00:00 2001 From: Marco Tezzele Date: Fri, 1 Apr 2016 23:45:04 +0200 Subject: [PATCH] new structure --- docs/source/code.rst | 4 + docs/source/filehandler.rst | 27 -- docs/source/openfhandler.rst | 9 + docs/source/stlhandler.rst | 9 + docs/source/unvhandler.rst | 9 + docs/source/vtkhandler.rst | 9 + pygem/__init__.py | 10 +- pygem/filehandler.py | 425 ++-------------------------- pygem/openfhandler.py | 90 ++++++ pygem/params.py | 1 - pygem/stlhandler.py | 108 +++++++ pygem/unvhandler.py | 109 ++++++++ pygem/vtkhandler.py | 95 +++++++ tests/test_filehandler.py | 528 ----------------------------------- tests/test_openfhandler.py | 136 +++++++++ tests/test_package.py | 58 ++-- tests/test_stlhandler.py | 150 ++++++++++ tests/test_unvhandler.py | 131 +++++++++ tests/test_vtkhandler.py | 135 +++++++++ 19 files changed, 1064 insertions(+), 979 deletions(-) create mode 100644 docs/source/openfhandler.rst create mode 100644 docs/source/stlhandler.rst create mode 100644 docs/source/unvhandler.rst create mode 100644 docs/source/vtkhandler.rst create mode 100644 pygem/openfhandler.py create mode 100644 pygem/stlhandler.py create mode 100644 pygem/unvhandler.py create mode 100644 pygem/vtkhandler.py create mode 100644 tests/test_openfhandler.py create mode 100644 tests/test_stlhandler.py create mode 100644 tests/test_unvhandler.py create mode 100644 tests/test_vtkhandler.py diff --git a/docs/source/code.rst b/docs/source/code.rst index be9b44a..db581a7 100644 --- a/docs/source/code.rst +++ b/docs/source/code.rst @@ -10,4 +10,8 @@ Code Documentation freeform params filehandler + openfhandler + stlhandler + vtkhandler + unvhandler diff --git a/docs/source/filehandler.rst b/docs/source/filehandler.rst index a3178a4..aee3136 100644 --- a/docs/source/filehandler.rst +++ b/docs/source/filehandler.rst @@ -6,30 +6,3 @@ Filehandler .. autoclass:: FileHandler :members: :private-members: - -.. autoclass:: UnvHandler - :members: - :private-members: - :special-members: - -.. autoclass:: VtkHandler - :members: - :private-members: - :special-members: - -.. autoclass:: StlHandler - :members: - :private-members: - :special-members: - -.. autoclass:: OpenFoamHandler - :members: - :private-members: - :special-members: - -.. autofunction:: _check_extension - -.. autofunction:: _check_filename_type - -.. autofunction:: _check_infile_instantiation - diff --git a/docs/source/openfhandler.rst b/docs/source/openfhandler.rst new file mode 100644 index 0000000..bb0a151 --- /dev/null +++ b/docs/source/openfhandler.rst @@ -0,0 +1,9 @@ +Openfhandler +================= + +.. automodule:: pygem.openfhandler + +.. autoclass:: OpenFoamHandler + :members: + :private-members: + :special-members: diff --git a/docs/source/stlhandler.rst b/docs/source/stlhandler.rst new file mode 100644 index 0000000..a1a11ae --- /dev/null +++ b/docs/source/stlhandler.rst @@ -0,0 +1,9 @@ +Stlhandler +================= + +.. automodule:: pygem.stlhandler + +.. autoclass:: StlHandler + :members: + :private-members: + :special-members: diff --git a/docs/source/unvhandler.rst b/docs/source/unvhandler.rst new file mode 100644 index 0000000..7441341 --- /dev/null +++ b/docs/source/unvhandler.rst @@ -0,0 +1,9 @@ +Unvhandler +================= + +.. automodule:: pygem.unvhandler + +.. autoclass:: UnvHandler + :members: + :private-members: + :special-members: diff --git a/docs/source/vtkhandler.rst b/docs/source/vtkhandler.rst new file mode 100644 index 0000000..1e56614 --- /dev/null +++ b/docs/source/vtkhandler.rst @@ -0,0 +1,9 @@ +Vtkhandler +================= + +.. automodule:: pygem.vtkhandler + +.. autoclass:: VtkHandler + :members: + :private-members: + :special-members: diff --git a/pygem/__init__.py b/pygem/__init__.py index 3be214c..f5f3910 100644 --- a/pygem/__init__.py +++ b/pygem/__init__.py @@ -1,7 +1,11 @@ -__all__ = ['affine', 'params', 'filehandler', 'freeform'] +__all__ = ['affine', 'filehandler', 'freeform', 'openfhandler', 'params', 'stlhandler', 'unvhandler', 'vtkhandler'] from . import affine -from . import params +from . import freeform from . import filehandler -from . import freeform \ No newline at end of file +from . import openfhandler +from . import params +from . import stlhandler +from . import unvhandler +from . import vtkhandler diff --git a/pygem/filehandler.py b/pygem/filehandler.py index 774d1a6..9c643a0 100644 --- a/pygem/filehandler.py +++ b/pygem/filehandler.py @@ -2,21 +2,15 @@ Utilities for reading and writing different CAD files. """ import os -import numpy as np -from mpl_toolkits import mplot3d -from matplotlib import pyplot -from stl import mesh -import vtk - class FileHandler(object): """ A base class for file handling. - :cvar string infile: name of the input file to be processed - :cvar string outfile: name of the output file where to write in + :cvar string infile: name of the input file to be processed. + :cvar string outfile: name of the output file where to write in. :cvar string extension: extension of the input/output files. It is specific for each - subclass + subclass. """ def __init__(self): self.infile = None @@ -44,409 +38,36 @@ def write(self, mesh_points, outfile): + self.__class__.__name__ + ".write") - -class UnvHandler(FileHandler): - """ - Universal file handler class - - .. todo:: DOCS - """ - def __init__(self): - super(UnvHandler, self).__init__() - self.extension = '.unv' - - - def parse(self, filename): - """ - Method to parse the file `filename`. It returns a matrix with all the coordinates. - - :return: mesh_points: it is a `n_points`-by-3 matrix containing the coordinates of - the points of the mesh - :rtype: numpy.ndarray - - .. todo:: - - - specify when it works - """ - _check_filename_type(filename) - _check_extension(filename, self.extension) - - self.infile = filename - - input_file = open(self.infile, 'r') - nline = 0 - while True: - line = input_file.readline() - nline += 1 - if len(line) == 0: - break - if line.startswith(' -1'): - section_id = input_file.readline().strip() - nline += 1 - if section_id == '2411': - count = 0 - while not input_file.readline().startswith(' -1'): - count += 1 - start_line = nline + 2 - last_line = start_line + count - else: - while not input_file.readline().startswith(' -1'): - nline += 1 - - input_file.close() - - n_points = count/2 - mesh_points = np.zeros(shape=(n_points, 3)) - - nline = 0 - i = 0 - with open(self.infile, 'r') as input_file: - for line in input_file: - nline += 1 - if nline % 2 == 1 and nline > start_line and nline < last_line: - line = line.strip() - j = 0 - for number in line.split(): - mesh_points[i][j] = float(number) - j += 1 - i += 1 - - return mesh_points - - - def write(self, mesh_points, filename): - """ - Writes a unv file, called filename, copying all the lines from self.filename but - the coordinates. mesh_points is a matrix that contains the new coordinates to - write in the unv file. - - :param numpy.ndarray mesh_points: it is a `n_points`-by-3 matrix containing - the coordinates of the points of the mesh - :param string filename: name of the output file. - - .. todo:: DOCS - """ - _check_filename_type(filename) - _check_extension(filename, self.extension) - _check_infile_instantiation(self.infile) - - self.outfile = filename - - n_points = mesh_points.shape[0] - nrow = 0 - i = 0 - with open(self.infile, 'r') as input_file, open(self.outfile, 'w') as output_file: - for line in input_file: - nrow += 1 - if nrow % 2 == 1 and nrow > 20 and nrow <= (20 + n_points * 2): - for j in range(0, 3): - output_file.write(' ' + str(mesh_points[i][j])) - output_file.write('\n') - i += 1 - elif nrow > 17: - output_file.write(line) - - - -class VtkHandler(FileHandler): - """ - Vtk file handler class - - .. todo:: DOCS - """ - def __init__(self): - super(VtkHandler, self).__init__() - self.extension = '.vtk' - - - def parse(self, filename): - """ - Method to parse the file `filename`. It returns a matrix with all the coordinates. - - :return: mesh_points: it is a `n_points`-by-3 matrix containing the coordinates of - the points of the mesh - :rtype: numpy.ndarray - - .. todo:: - - - specify when it works - """ - _check_filename_type(filename) - _check_extension(filename, self.extension) - - self.infile = filename - - reader = vtk.vtkDataSetReader() - reader.SetFileName(self.infile) - reader.ReadAllVectorsOn() - reader.ReadAllScalarsOn() - reader.Update() - data = reader.GetOutput() - - n_points = data.GetNumberOfPoints() - mesh_points = np.zeros([n_points, 3]) - - for i in range(n_points): - mesh_points[i, 0], mesh_points[i, 1], mesh_points[i, 2] = data.GetPoint(i) - - return mesh_points - - - def write(self, mesh_points, filename): - """ - Writes a vtk file, called filename, copying all the structures from self.filename but - the coordinates. mesh_points is a matrix that contains the new coordinates to - write in the vtk file. - - :param numpy.ndarray mesh_points: it is a `n_points`-by-3 matrix containing - the coordinates of the points of the mesh - :param string filename: name of the output file. - - .. todo:: DOCS - """ - _check_filename_type(filename) - _check_extension(filename, self.extension) - _check_infile_instantiation(self.infile) - - self.outfile = filename - - reader = vtk.vtkDataSetReader() - reader.SetFileName(self.infile) - reader.ReadAllVectorsOn() - reader.ReadAllScalarsOn() - reader.Update() - data = reader.GetOutput() - - points = vtk.vtkPoints() - - for i in range(data.GetNumberOfPoints()): - points.InsertNextPoint(mesh_points[i, :]) - - data.SetPoints(points) - - writer = vtk.vtkDataSetWriter() - writer.SetFileName(self.outfile) - - if vtk.VTK_MAJOR_VERSION <= 5: - writer.SetInput(data) - else: - writer.SetInputData(data) - - writer.Write() - - -class StlHandler(FileHandler): - """ - STereoLithography file handler class - - .. todo:: DOCS - """ - def __init__(self): - super(StlHandler, self).__init__() - self.extension = '.stl' - - - def parse(self, filename): - """ - Method to parse the `filename`. It returns a matrix with all the coordinates. - - :return: mesh_points: it is a `n_points`-by-3 matrix containing the coordinates of - the points of the mesh - :rtype: numpy.ndarray - - .. todo:: - - - specify when it works + def _check_extension(self, filename): """ - _check_filename_type(filename) - _check_extension(filename, self.extension) - - self.infile = filename - - stl_mesh = mesh.Mesh.from_file(self.infile) - mesh_points = np.array([stl_mesh.x.ravel(), stl_mesh.y.ravel(), stl_mesh.z.ravel()]) - mesh_points = mesh_points.T + This private method checks if the given `filename` has the proper `extension` set + in the child class. If not it raises a ValueError. - return mesh_points - - - def write(self, mesh_points, filename): + :param string filename: file to check. """ - Writes a unv file, called filename, copying all the lines from self.filename but - the coordinates. mesh_points is a matrix that contains the new coordinates to - write in the unv file. - - :param numpy.ndarray mesh_points: it is a `n_points`-by-3 matrix containing - the coordinates of the points of the mesh. - :param string filename: name of the output file. - - .. todo:: DOCS - """ - _check_filename_type(filename) - _check_extension(filename, self.extension) - _check_infile_instantiation(self.infile) - - self.outfile = filename - - n_vertices = mesh_points.shape[0] - # number of triplets of vertices - n_triplets = n_vertices/3 - data = np.zeros(n_triplets, dtype=mesh.Mesh.dtype) - stl_mesh = mesh.Mesh(data, remove_empty_areas=False) - - for i in range(0, n_triplets): - for j in range(0, 3): - data['vectors'][i][j] = mesh_points[3*i + j] - - stl_mesh.save(self.outfile, mode=1, update_normals=True) - - - def plot(self, plot_file=None, save_fig=False): - """ - Method to plot an stl file. If `plot_file` is not given it plots `self.infile`. - - :param string plot_file: the stl filename you want to plot. - :param bool save_fig: a flag to save the figure in png or not. If True the - plot is not shown. - """ - if plot_file is None: - plot_file = self.infile - else: - _check_filename_type(plot_file) - - # Create a new plot - figure = pyplot.figure() - axes = mplot3d.Axes3D(figure) - - # Load the STL files and add the vectors to the plot - stl_mesh = mesh.Mesh.from_file(plot_file) - axes.add_collection3d(mplot3d.art3d.Poly3DCollection(stl_mesh.vectors)) - - # Auto scale to the mesh size - scale = stl_mesh.points.flatten(-1) - axes.auto_scale_xyz(scale, scale, scale) - - # Show the plot to the screen - if not save_fig: - pyplot.show() - else: - figure.savefig(plot_file.split('.')[0] + '.png') - + __, file_ext = os.path.splitext(filename) + if not file_ext == self.extension: + raise ValueError('The input file does not have the proper extension. \ + It is %s, instead of %s.' % (file_ext, self.extension)) -class OpenFoamHandler(FileHandler): - """ - OpenFOAM mesh file handler class. - - .. todo:: DOCS - """ - def __init__(self): - super(OpenFoamHandler, self).__init__() - self.extension = '' - - - def parse(self, filename): + def _check_filename_type(self, filename): """ - Method to parse the `filename`. It returns a matrix with all the coordinates. - - :return: mesh_points: it is a `n_points`-by-3 matrix containing the coordinates of - the points of the mesh - :rtype: numpy.ndarray - - .. todo:: + This private method checks if `filename` is a string. If not it raises a TypeError. - - specify when it works + :param string filename: file to check. """ - _check_filename_type(filename) - _check_extension(filename, self.extension) + if not isinstance(filename, basestring): + raise TypeError('The given filename (%s) must be a string' % filename) - self.infile = filename - nrow = 0 - i = 0 - with open(self.infile, 'r') as input_file: - for line in input_file: - nrow += 1 - if nrow == 19: - n_points = int(line) - mesh_points = np.zeros(shape=(n_points, 3)) - if nrow > 20 and nrow < 21 + n_points: - line = line[line.index("(") + 1:line.rindex(")")] - j = 0 - for number in line.split(): - mesh_points[i][j] = float(number) - j += 1 - i += 1 - - return mesh_points - - - - def write(self, mesh_points, filename): + def _check_infile_instantiation(self, infile): """ - Writes a openFOAM file, called filename, copying all the lines from self.filename but - the coordinates. mesh_points is a matrix that contains the new coordinates to - write in the openFOAM file. + This private method checks if the input file `infile` is instantiated. If not it means + that nobody called the parse method, i.e. `self.infile` is None. If the check fails + it raises a RuntimeError. - :param numpy.ndarray mesh_points: it is a `n_points`-by-3 matrix containing - the coordinates of the points of the mesh. - :param string filename: name of the output file. - - .. todo:: DOCS + :param string infile: file to check. """ - _check_filename_type(filename) - _check_extension(filename, self.extension) - _check_infile_instantiation(self.infile) - - self.outfile = filename - - n_points = mesh_points.shape[0] - nrow = 0 - i = 0 - with open(self.infile, 'r') as input_file, open(self.outfile, 'w') as output_file: - for line in input_file: - nrow += 1 - if nrow > 20 and nrow < 21 + n_points: - output_file.write('(' + str(mesh_points[i][0]) + ' ' + str(mesh_points[i][1]) + \ - ' ' + str(mesh_points[i][2]) +')') - output_file.write('\n') - i += 1 - else: - output_file.write(line) - - - -def _check_extension(filename, extension): - """ - This private method checks if the given `filename` has the proper `extension`. - If not it raises a ValueError. - - :param string filename: file to check. - :param string extension: file extension to check. - """ - __, file_ext = os.path.splitext(filename) - if not file_ext == extension: - raise ValueError('The input file does not have the proper extension. \ - It is %s, instead of %s.' % (file_ext, extension)) - - -def _check_filename_type(filename): - """ - This private method checks if `filename` is a string. If not it raises a TypeError. - - :param string filename: file to check. - """ - if not isinstance(filename, basestring): - raise TypeError('The given filename (%s) must be a string' % filename) - - -def _check_infile_instantiation(infile): - """ - This private method checks if the input file `infile` is instantiated. If not it means - that nobody called the parse method, i.e. `self.infile` is None. If the check fails - it raises a RuntimeError. - - :param string infile: file to check. - """ - if not infile: - raise RuntimeError("You can not write a file without having parsed one.") + if not infile: + raise RuntimeError("You can not write a file without having parsed one.") diff --git a/pygem/openfhandler.py b/pygem/openfhandler.py new file mode 100644 index 0000000..d2bed09 --- /dev/null +++ b/pygem/openfhandler.py @@ -0,0 +1,90 @@ +""" +Utilities for reading and writing different CAD files. +""" +import numpy as np +import pygem.filehandler as fh + + +class OpenFoamHandler(fh.FileHandler): + """ + OpenFOAM mesh file handler class. + + :cvar string infile: name of the input file to be processed. + :cvar string outfile: name of the output file where to write in. + :cvar string extension: extension of the input/output files. It is equal to '' since + openFOAM files do not have extension. + """ + def __init__(self): + super(OpenFoamHandler, self).__init__() + self.extension = '' + + + def parse(self, filename): + """ + Method to parse the `filename`. It returns a matrix with all the coordinates. + + :return: mesh_points: it is a `n_points`-by-3 matrix containing the coordinates of + the points of the mesh + :rtype: numpy.ndarray + + .. todo:: + + - specify when it works + """ + self._check_filename_type(filename) + self._check_extension(filename) + + self.infile = filename + + nrow = 0 + i = 0 + with open(self.infile, 'r') as input_file: + for line in input_file: + nrow += 1 + if nrow == 19: + n_points = int(line) + mesh_points = np.zeros(shape=(n_points, 3)) + if nrow > 20 and nrow < 21 + n_points: + line = line[line.index("(") + 1:line.rindex(")")] + j = 0 + for number in line.split(): + mesh_points[i][j] = float(number) + j += 1 + i += 1 + + return mesh_points + + + + def write(self, mesh_points, filename): + """ + Writes a openFOAM file, called filename, copying all the lines from self.filename but + the coordinates. mesh_points is a matrix that contains the new coordinates to + write in the openFOAM file. + + :param numpy.ndarray mesh_points: it is a `n_points`-by-3 matrix containing + the coordinates of the points of the mesh. + :param string filename: name of the output file. + + .. todo:: DOCS + """ + self._check_filename_type(filename) + self._check_extension(filename) + self._check_infile_instantiation(self.infile) + + self.outfile = filename + + n_points = mesh_points.shape[0] + nrow = 0 + i = 0 + with open(self.infile, 'r') as input_file, open(self.outfile, 'w') as output_file: + for line in input_file: + nrow += 1 + if nrow > 20 and nrow < 21 + n_points: + output_file.write('(' + str(mesh_points[i][0]) + ' ' + str(mesh_points[i][1]) + \ + ' ' + str(mesh_points[i][2]) +')') + output_file.write('\n') + i += 1 + else: + output_file.write(line) + diff --git a/pygem/params.py b/pygem/params.py index 2654f75..6e75375 100644 --- a/pygem/params.py +++ b/pygem/params.py @@ -65,7 +65,6 @@ class FFDParameters(object): If the four vertex are coplanar, an assert is thrown when affine_points_fit is used. """ - def __init__(self, n_control_points=None): self.conversion_unit = 1. diff --git a/pygem/stlhandler.py b/pygem/stlhandler.py new file mode 100644 index 0000000..4cc794f --- /dev/null +++ b/pygem/stlhandler.py @@ -0,0 +1,108 @@ +""" +Utilities for reading and writing different CAD files. +""" +import numpy as np +from mpl_toolkits import mplot3d +from matplotlib import pyplot +from stl import mesh +import pygem.filehandler as fh + + +class StlHandler(fh.FileHandler): + """ + STereoLithography file handler class + + :cvar string infile: name of the input file to be processed. + :cvar string outfile: name of the output file where to write in. + :cvar string extension: extension of the input/output files. It is equal to '.stl'. + """ + def __init__(self): + super(StlHandler, self).__init__() + self.extension = '.stl' + + + def parse(self, filename): + """ + Method to parse the `filename`. It returns a matrix with all the coordinates. + + :return: mesh_points: it is a `n_points`-by-3 matrix containing the coordinates of + the points of the mesh + :rtype: numpy.ndarray + + .. todo:: + + - specify when it works + """ + self._check_filename_type(filename) + self._check_extension(filename) + + self.infile = filename + + stl_mesh = mesh.Mesh.from_file(self.infile) + mesh_points = np.array([stl_mesh.x.ravel(), stl_mesh.y.ravel(), stl_mesh.z.ravel()]) + mesh_points = mesh_points.T + + return mesh_points + + + def write(self, mesh_points, filename): + """ + Writes a unv file, called filename, copying all the lines from self.filename but + the coordinates. mesh_points is a matrix that contains the new coordinates to + write in the unv file. + + :param numpy.ndarray mesh_points: it is a `n_points`-by-3 matrix containing + the coordinates of the points of the mesh. + :param string filename: name of the output file. + + .. todo:: DOCS + """ + self._check_filename_type(filename) + self._check_extension(filename) + self._check_infile_instantiation(self.infile) + + self.outfile = filename + + n_vertices = mesh_points.shape[0] + # number of triplets of vertices + n_triplets = n_vertices/3 + data = np.zeros(n_triplets, dtype=mesh.Mesh.dtype) + stl_mesh = mesh.Mesh(data, remove_empty_areas=False) + + for i in range(0, n_triplets): + for j in range(0, 3): + data['vectors'][i][j] = mesh_points[3*i + j] + + stl_mesh.save(self.outfile, mode=1, update_normals=True) + + + def plot(self, plot_file=None, save_fig=False): + """ + Method to plot an stl file. If `plot_file` is not given it plots `self.infile`. + + :param string plot_file: the stl filename you want to plot. + :param bool save_fig: a flag to save the figure in png or not. If True the + plot is not shown. + """ + if plot_file is None: + plot_file = self.infile + else: + self._check_filename_type(plot_file) + + # Create a new plot + figure = pyplot.figure() + axes = mplot3d.Axes3D(figure) + + # Load the STL files and add the vectors to the plot + stl_mesh = mesh.Mesh.from_file(plot_file) + axes.add_collection3d(mplot3d.art3d.Poly3DCollection(stl_mesh.vectors)) + + # Auto scale to the mesh size + scale = stl_mesh.points.flatten(-1) + axes.auto_scale_xyz(scale, scale, scale) + + # Show the plot to the screen + if not save_fig: + pyplot.show() + else: + figure.savefig(plot_file.split('.')[0] + '.png') diff --git a/pygem/unvhandler.py b/pygem/unvhandler.py new file mode 100644 index 0000000..fae560e --- /dev/null +++ b/pygem/unvhandler.py @@ -0,0 +1,109 @@ +""" +Utilities for reading and writing different CAD files. +""" +import numpy as np +import pygem.filehandler as fh + + +class UnvHandler(fh.FileHandler): + """ + Universal file handler class + + :cvar string infile: name of the input file to be processed. + :cvar string outfile: name of the output file where to write in. + :cvar string extension: extension of the input/output files. It is equal to '.unv'. + """ + def __init__(self): + super(UnvHandler, self).__init__() + self.extension = '.unv' + + + def parse(self, filename): + """ + Method to parse the file `filename`. It returns a matrix with all the coordinates. + + :return: mesh_points: it is a `n_points`-by-3 matrix containing the coordinates of + the points of the mesh + :rtype: numpy.ndarray + + .. todo:: + + - specify when it works + """ + self._check_filename_type(filename) + self._check_extension(filename) + + self.infile = filename + + input_file = open(self.infile, 'r') + nline = 0 + while True: + line = input_file.readline() + nline += 1 + if len(line) == 0: + break + if line.startswith(' -1'): + section_id = input_file.readline().strip() + nline += 1 + if section_id == '2411': + count = 0 + while not input_file.readline().startswith(' -1'): + count += 1 + start_line = nline + 2 + last_line = start_line + count + else: + while not input_file.readline().startswith(' -1'): + nline += 1 + + input_file.close() + + n_points = count/2 + mesh_points = np.zeros(shape=(n_points, 3)) + + nline = 0 + i = 0 + with open(self.infile, 'r') as input_file: + for line in input_file: + nline += 1 + if nline % 2 == 1 and nline > start_line and nline < last_line: + line = line.strip() + j = 0 + for number in line.split(): + mesh_points[i][j] = float(number) + j += 1 + i += 1 + + return mesh_points + + + def write(self, mesh_points, filename): + """ + Writes a unv file, called filename, copying all the lines from self.filename but + the coordinates. mesh_points is a matrix that contains the new coordinates to + write in the unv file. + + :param numpy.ndarray mesh_points: it is a `n_points`-by-3 matrix containing + the coordinates of the points of the mesh + :param string filename: name of the output file. + + .. todo:: DOCS + """ + self._check_filename_type(filename) + self._check_extension(filename) + self._check_infile_instantiation(self.infile) + + self.outfile = filename + + n_points = mesh_points.shape[0] + nrow = 0 + i = 0 + with open(self.infile, 'r') as input_file, open(self.outfile, 'w') as output_file: + for line in input_file: + nrow += 1 + if nrow % 2 == 1 and nrow > 20 and nrow <= (20 + n_points * 2): + for j in range(0, 3): + output_file.write(' ' + str(mesh_points[i][j])) + output_file.write('\n') + i += 1 + elif nrow > 17: + output_file.write(line) diff --git a/pygem/vtkhandler.py b/pygem/vtkhandler.py new file mode 100644 index 0000000..db47c06 --- /dev/null +++ b/pygem/vtkhandler.py @@ -0,0 +1,95 @@ +""" +Utilities for reading and writing different CAD files. +""" +import numpy as np +import vtk +import pygem.filehandler as fh + + +class VtkHandler(fh.FileHandler): + """ + Vtk file handler class + + :cvar string infile: name of the input file to be processed. + :cvar string outfile: name of the output file where to write in. + :cvar string extension: extension of the input/output files. It is equal to '.vtk'. + """ + def __init__(self): + super(VtkHandler, self).__init__() + self.extension = '.vtk' + + + def parse(self, filename): + """ + Method to parse the file `filename`. It returns a matrix with all the coordinates. + + :return: mesh_points: it is a `n_points`-by-3 matrix containing the coordinates of + the points of the mesh + :rtype: numpy.ndarray + + .. todo:: + + - specify when it works + """ + self._check_filename_type(filename) + self._check_extension(filename) + + self.infile = filename + + reader = vtk.vtkDataSetReader() + reader.SetFileName(self.infile) + reader.ReadAllVectorsOn() + reader.ReadAllScalarsOn() + reader.Update() + data = reader.GetOutput() + + n_points = data.GetNumberOfPoints() + mesh_points = np.zeros([n_points, 3]) + + for i in range(n_points): + mesh_points[i, 0], mesh_points[i, 1], mesh_points[i, 2] = data.GetPoint(i) + + return mesh_points + + + def write(self, mesh_points, filename): + """ + Writes a vtk file, called filename, copying all the structures from self.filename but + the coordinates. mesh_points is a matrix that contains the new coordinates to + write in the vtk file. + + :param numpy.ndarray mesh_points: it is a `n_points`-by-3 matrix containing + the coordinates of the points of the mesh + :param string filename: name of the output file. + + .. todo:: DOCS + """ + self._check_filename_type(filename) + self._check_extension(filename) + self._check_infile_instantiation(self.infile) + + self.outfile = filename + + reader = vtk.vtkDataSetReader() + reader.SetFileName(self.infile) + reader.ReadAllVectorsOn() + reader.ReadAllScalarsOn() + reader.Update() + data = reader.GetOutput() + + points = vtk.vtkPoints() + + for i in range(data.GetNumberOfPoints()): + points.InsertNextPoint(mesh_points[i, :]) + + data.SetPoints(points) + + writer = vtk.vtkDataSetWriter() + writer.SetFileName(self.outfile) + + if vtk.VTK_MAJOR_VERSION <= 5: + writer.SetInput(data) + else: + writer.SetInputData(data) + + writer.Write() diff --git a/tests/test_filehandler.py b/tests/test_filehandler.py index ab5b621..b29834d 100644 --- a/tests/test_filehandler.py +++ b/tests/test_filehandler.py @@ -36,531 +36,3 @@ def test_base_class_write(self): mesh_points = np.zeros((3, 3)) with self.assertRaises(NotImplementedError): file_handler.write(mesh_points, 'output') - - - -class TestUnvHandler(TestCase): - - - def test_unv_instantiation(self): - unv_handler = fh.UnvHandler() - - - def test_unv_default_infile_member(self): - unv_handler = fh.UnvHandler() - assert unv_handler.infile == None - - - def test_unv_default_outfile_member(self): - unv_handler = fh.UnvHandler() - assert unv_handler.outfile == None - - - def test_unv_default_extension_member(self): - unv_handler = fh.UnvHandler() - assert unv_handler.extension == '.unv' - - - def test_unv_parse_failing_filename_type(self): - unv_handler = fh.UnvHandler() - with self.assertRaises(TypeError): - mesh_points = unv_handler.parse(5.2) - - - def test_unv_parse_failing_check_extension(self): - unv_handler = fh.UnvHandler() - with self.assertRaises(ValueError): - mesh_points = unv_handler.parse('tests/test_datasets/test_square.iges') - - - def test_unv_parse_infile(self): - unv_handler = fh.UnvHandler() - mesh_points = unv_handler.parse('tests/test_datasets/test_square.unv') - assert unv_handler.infile == 'tests/test_datasets/test_square.unv' - - - def test_unv_parse_shape(self): - unv_handler = fh.UnvHandler() - mesh_points = unv_handler.parse('tests/test_datasets/test_square.unv') - assert mesh_points.shape == (256, 3) - - - def test_unv_parse_coords_1(self): - unv_handler = fh.UnvHandler() - mesh_points = unv_handler.parse('tests/test_datasets/test_square.unv') - np.testing.assert_almost_equal(mesh_points[33][0], 1.0) - - - def test_unv_parse_coords_2(self): - unv_handler = fh.UnvHandler() - mesh_points = unv_handler.parse('tests/test_datasets/test_square.unv') - np.testing.assert_almost_equal(mesh_points[178][1], 0.4) - - - def test_unv_parse_coords_3(self): - unv_handler = fh.UnvHandler() - mesh_points = unv_handler.parse('tests/test_datasets/test_square.unv') - np.testing.assert_almost_equal(mesh_points[100][2], 0.0) - - - def test_unv_parse_coords_4(self): - unv_handler = fh.UnvHandler() - mesh_points = unv_handler.parse('tests/test_datasets/test_square.unv') - np.testing.assert_almost_equal(mesh_points[0][0], 0.0) - - - def test_unv_parse_coords_5(self): - unv_handler = fh.UnvHandler() - mesh_points = unv_handler.parse('tests/test_datasets/test_square.unv') - np.testing.assert_almost_equal(mesh_points[-1][2], 0.0) - - - def test_unv_write_failing_filename_type(self): - unv_handler = fh.UnvHandler() - mesh_points = unv_handler.parse('tests/test_datasets/test_square.unv') - with self.assertRaises(TypeError): - unv_handler.write(mesh_points, -2) - - - def test_unv_write_failing_check_extension(self): - unv_handler = fh.UnvHandler() - mesh_points = unv_handler.parse('tests/test_datasets/test_square.unv') - with self.assertRaises(ValueError): - unv_handler.write(mesh_points, 'tests/test_datasets/test_square.iges') - - - def test_unv_write_failing_infile_instantiation(self): - unv_handler = fh.UnvHandler() - mesh_points = np.zeros((20, 3)) - with self.assertRaises(RuntimeError): - unv_handler.write(mesh_points, 'tests/test_datasets/test_square_out.unv') - - - def test_unv_write_outfile(self): - unv_handler = fh.UnvHandler() - mesh_points = unv_handler.parse('tests/test_datasets/test_square.unv') - outfilename = 'tests/test_datasets/test_square_out.unv' - unv_handler.write(mesh_points, outfilename) - assert unv_handler.outfile == outfilename - os.remove(outfilename) - - - def test_unv_write_comparison(self): - unv_handler = fh.UnvHandler() - mesh_points = unv_handler.parse('tests/test_datasets/test_square.unv') - mesh_points[0][0] = 2.2 - mesh_points[5][1] = 4.3 - mesh_points[9][2] = 0.5 - mesh_points[45][0] = 7.2 - mesh_points[132][1] = -1.2 - mesh_points[255][2] = -3.6 - - outfilename = 'tests/test_datasets/test_square_out.unv' - outfilename_expected = 'tests/test_datasets/test_square_out_true.unv' - - unv_handler.write(mesh_points, outfilename) - self.assertTrue(filecmp.cmp(outfilename, outfilename_expected)) - os.remove(outfilename) - - - -class TestVtkHandler(TestCase): - - - def test_vtk_instantiation(self): - vtk_handler = fh.VtkHandler() - - - def test_vtk_default_infile_member(self): - vtk_handler = fh.VtkHandler() - assert vtk_handler.infile == None - - - def test_vtk_default_outfile_member(self): - vtk_handler = fh.VtkHandler() - assert vtk_handler.outfile == None - - - def test_vtk_default_extension_member(self): - vtk_handler = fh.VtkHandler() - assert vtk_handler.extension == '.vtk' - - - def test_vtk_parse_failing_filename_type(self): - vtk_handler = fh.VtkHandler() - with self.assertRaises(TypeError): - mesh_points = vtk_handler.parse(5.2) - - - def test_vtk_parse_failing_check_extension(self): - vtk_handler = fh.VtkHandler() - with self.assertRaises(ValueError): - mesh_points = vtk_handler.parse('tests/test_datasets/test_square.iges') - - - def test_vtk_parse_infile(self): - vtk_handler = fh.VtkHandler() - mesh_points = vtk_handler.parse('tests/test_datasets/test_red_blood_cell.vtk') - assert vtk_handler.infile == 'tests/test_datasets/test_red_blood_cell.vtk' - - - def test_vtk_parse_shape(self): - vtk_handler = fh.VtkHandler() - mesh_points = vtk_handler.parse('tests/test_datasets/test_red_blood_cell.vtk') - assert mesh_points.shape == (500, 3) - - - def test_vtk_parse_coords_1(self): - vtk_handler = fh.VtkHandler() - mesh_points = vtk_handler.parse('tests/test_datasets/test_red_blood_cell.vtk') - np.testing.assert_almost_equal(mesh_points[33][0], -2.2977099) - - - def test_vtk_parse_coords_2(self): - vtk_handler = fh.VtkHandler() - mesh_points = vtk_handler.parse('tests/test_datasets/test_red_blood_cell.vtk') - np.testing.assert_almost_equal(mesh_points[178][1], 0.143506) - - - def test_vtk_parse_coords_3(self): - vtk_handler = fh.VtkHandler() - mesh_points = vtk_handler.parse('tests/test_datasets/test_red_blood_cell.vtk') - np.testing.assert_almost_equal(mesh_points[100][2], 2.3306999) - - - def test_vtk_parse_coords_4(self): - vtk_handler = fh.VtkHandler() - mesh_points = vtk_handler.parse('tests/test_datasets/test_red_blood_cell.vtk') - np.testing.assert_almost_equal(mesh_points[0][0], -3.42499995) - - - def test_vtk_parse_coords_5(self): - vtk_handler = fh.VtkHandler() - mesh_points = vtk_handler.parse('tests/test_datasets/test_red_blood_cell.vtk') - np.testing.assert_almost_equal(mesh_points[-1][2], -2.8480699) - - - def test_vtk_write_failing_filename_type(self): - vtk_handler = fh.VtkHandler() - mesh_points = vtk_handler.parse('tests/test_datasets/test_red_blood_cell.vtk') - with self.assertRaises(TypeError): - vtk_handler.write(mesh_points, -2) - - - def test_vtk_write_failing_check_extension(self): - vtk_handler = fh.VtkHandler() - mesh_points = vtk_handler.parse('tests/test_datasets/test_red_blood_cell.vtk') - with self.assertRaises(ValueError): - vtk_handler.write(mesh_points, 'tests/test_datasets/test_square.iges') - - - def test_vtk_write_failing_infile_instantiation(self): - vtk_handler = fh.VtkHandler() - mesh_points = np.zeros((20, 3)) - with self.assertRaises(RuntimeError): - vtk_handler.write(mesh_points, 'tests/test_datasets/test_red_blood_cell_out.vtk') - - - def test_vtk_write_outfile(self): - vtk_handler = fh.VtkHandler() - mesh_points = vtk_handler.parse('tests/test_datasets/test_red_blood_cell.vtk') - outfilename = 'tests/test_datasets/test_red_blood_cell_out.vtk' - vtk_handler.write(mesh_points, outfilename) - assert vtk_handler.outfile == outfilename - os.remove(outfilename) - - - def test_vtk_write_comparison(self): - import vtk - vtk_handler = fh.VtkHandler() - mesh_points = vtk_handler.parse('tests/test_datasets/test_red_blood_cell.vtk') - mesh_points[0][0] = 2.2 - mesh_points[5][1] = 4.3 - mesh_points[9][2] = 0.5 - mesh_points[45][0] = 7.2 - mesh_points[132][1] = -1.2 - mesh_points[255][2] = -3.6 - - outfilename = 'tests/test_datasets/test_red_blood_cell_out.vtk' - if vtk.VTK_MAJOR_VERSION <= 5: - outfilename_expected = 'tests/test_datasets/test_red_blood_cell_out_true_version5.vtk' - else: - outfilename_expected = 'tests/test_datasets/test_red_blood_cell_out_true_version6.vtk' - - vtk_handler.write(mesh_points, outfilename) - self.assertTrue(filecmp.cmp(outfilename, outfilename_expected)) - os.remove(outfilename) - - - -class TestStlHandler(TestCase): - - - def test_stl_instantiation(self): - stl_handler = fh.StlHandler() - - - def test_stl_default_infile_member(self): - stl_handler = fh.StlHandler() - assert stl_handler.infile == None - - - def test_stl_default_outfile_member(self): - stl_handler = fh.StlHandler() - assert stl_handler.outfile == None - - - def test_stl_default_extension_member(self): - stl_handler = fh.StlHandler() - assert stl_handler.extension == '.stl' - - - def test_stl_parse_failing_filename_type(self): - stl_handler = fh.StlHandler() - with self.assertRaises(TypeError): - mesh_points = stl_handler.parse(5.2) - - - def test_stl_parse_failing_check_extension(self): - stl_handler = fh.StlHandler() - with self.assertRaises(ValueError): - mesh_points = stl_handler.parse('tests/test_datasets/test_square.iges') - - - def test_stl_parse_infile(self): - stl_handler = fh.StlHandler() - mesh_points = stl_handler.parse('tests/test_datasets/test_sphere.stl') - assert stl_handler.infile == 'tests/test_datasets/test_sphere.stl' - - - def test_stl_parse_shape(self): - stl_handler = fh.StlHandler() - mesh_points = stl_handler.parse('tests/test_datasets/test_sphere.stl') - assert mesh_points.shape == (7200, 3) - - - def test_stl_parse_coords_1(self): - stl_handler = fh.StlHandler() - mesh_points = stl_handler.parse('tests/test_datasets/test_sphere.stl') - np.testing.assert_almost_equal(mesh_points[33][0], -21.31975937) - - - def test_stl_parse_coords_2(self): - stl_handler = fh.StlHandler() - mesh_points = stl_handler.parse('tests/test_datasets/test_sphere.stl') - np.testing.assert_almost_equal(mesh_points[1708][1], 2.58431911) - - - def test_stl_parse_coords_3(self): - stl_handler = fh.StlHandler() - mesh_points = stl_handler.parse('tests/test_datasets/test_sphere.stl') - np.testing.assert_almost_equal(mesh_points[3527][2], -2.47207999) - - - def test_stl_parse_coords_4(self): - stl_handler = fh.StlHandler() - mesh_points = stl_handler.parse('tests/test_datasets/test_sphere.stl') - np.testing.assert_almost_equal(mesh_points[0][0], -21.31975937) - - - def test_stl_parse_coords_5(self): - stl_handler = fh.StlHandler() - mesh_points = stl_handler.parse('tests/test_datasets/test_sphere.stl') - np.testing.assert_almost_equal(mesh_points[-1][2], -39.05963898) - - - def test_stl_write_failing_filename_type(self): - stl_handler = fh.StlHandler() - mesh_points = stl_handler.parse('tests/test_datasets/test_sphere.stl') - with self.assertRaises(TypeError): - stl_handler.write(mesh_points, 4.) - - - def test_stl_write_failing_check_extension(self): - stl_handler = fh.StlHandler() - mesh_points = stl_handler.parse('tests/test_datasets/test_sphere.stl') - with self.assertRaises(ValueError): - stl_handler.write(mesh_points, 'tests/test_datasets/test_square.iges') - - - def test_stl_write_failing_infile_instantiation(self): - stl_handler = fh.StlHandler() - mesh_points = np.zeros((40, 3)) - with self.assertRaises(RuntimeError): - stl_handler.write(mesh_points, 'tests/test_datasets/test_sphere_out.stl') - - - def test_stl_write_outfile(self): - stl_handler = fh.StlHandler() - mesh_points = stl_handler.parse('tests/test_datasets/test_sphere.stl') - outfilename = 'tests/test_datasets/test_sphere_out.stl' - stl_handler.write(mesh_points, outfilename) - assert stl_handler.outfile == outfilename - os.remove(outfilename) - - - def test_stl_write_comparison(self): - stl_handler = fh.StlHandler() - mesh_points = stl_handler.parse('tests/test_datasets/test_sphere.stl') - mesh_points[0] = [-40.2, -20.5, 60.9] - mesh_points[1] = [-40.2, -10.5, 60.9] - mesh_points[2] = [-40.2, -10.5, 60.9] - mesh_points[2000] = [-40.2, -20.5, 60.9] - mesh_points[2001] = [-40.2, -10.5, 60.9] - mesh_points[2002] = [-40.2, -10.5, 60.9] - mesh_points[6100] = [-40.2, -20.5, 60.9] - mesh_points[6101] = [-40.2, -10.5, 60.9] - mesh_points[6102] = [-40.2, -10.5, 60.9] - - outfilename = 'tests/test_datasets/test_sphere_out.stl' - outfilename_expected = 'tests/test_datasets/test_sphere_out_true.stl' - - stl_handler.write(mesh_points, outfilename) - self.assertTrue(filecmp.cmp(outfilename, outfilename_expected)) - os.remove(outfilename) - - - def test_stl_plot_save_fig(self): - stl_handler = fh.StlHandler() - mesh_points = stl_handler.parse('tests/test_datasets/test_sphere.stl') - stl_handler.plot(save_fig=True) - if not os.path.isfile('tests/test_datasets/test_sphere.png'): - assert False - else: - os.remove('tests/test_datasets/test_sphere.png') - - - def test_stl_plot_failing_outfile_type(self): - stl_handler = fh.StlHandler() - with self.assertRaises(TypeError): - stl_handler.plot(plot_file=3) - - - -class TestOpenFoamHandler(TestCase): - - - def test_open_foam_instantiation(self): - open_foam_handler = fh.OpenFoamHandler() - - - def test_open_foam_default_infile_member(self): - open_foam_handler = fh.OpenFoamHandler() - assert open_foam_handler.infile == None - - - def test_open_foam_default_outfile_member(self): - open_foam_handler = fh.OpenFoamHandler() - assert open_foam_handler.outfile == None - - - def test_open_foam_default_extension_member(self): - open_foam_handler = fh.OpenFoamHandler() - assert open_foam_handler.extension == '' - - - def test_open_foam_parse_failing_filename_type(self): - open_foam_handler = fh.OpenFoamHandler() - with self.assertRaises(TypeError): - mesh_points = open_foam_handler.parse(.2) - - - def test_open_foam_parse_failing_check_extension(self): - open_foam_handler = fh.OpenFoamHandler() - with self.assertRaises(ValueError): - mesh_points = open_foam_handler.parse('tests/test_datasets/test_square.iges') - - - def test_open_foam_parse_infile(self): - open_foam_handler = fh.OpenFoamHandler() - mesh_points = open_foam_handler.parse('tests/test_datasets/test_openFOAM') - assert open_foam_handler.infile == 'tests/test_datasets/test_openFOAM' - - - def test_open_foam_parse_shape(self): - open_foam_handler = fh.OpenFoamHandler() - mesh_points = open_foam_handler.parse('tests/test_datasets/test_openFOAM') - assert mesh_points.shape == (21812, 3) - - - def test_open_foam_parse_coords_1(self): - open_foam_handler = fh.OpenFoamHandler() - mesh_points = open_foam_handler.parse('tests/test_datasets/test_openFOAM') - np.testing.assert_almost_equal(mesh_points[33][0], 1.42254) - - - def test_open_foam_parse_coords_2(self): - open_foam_handler = fh.OpenFoamHandler() - mesh_points = open_foam_handler.parse('tests/test_datasets/test_openFOAM') - np.testing.assert_almost_equal(mesh_points[1708][1], -3.13059) - - - def test_open_foam_parse_coords_3(self): - open_foam_handler = fh.OpenFoamHandler() - mesh_points = open_foam_handler.parse('tests/test_datasets/test_openFOAM') - np.testing.assert_almost_equal(mesh_points[3527][2], .0) - - - def test_open_foam_parse_coords_4(self): - open_foam_handler = fh.OpenFoamHandler() - mesh_points = open_foam_handler.parse('tests/test_datasets/test_openFOAM') - np.testing.assert_almost_equal(mesh_points[0][0], -17.5492) - - - def test_open_foam_parse_coords_5(self): - open_foam_handler = fh.OpenFoamHandler() - mesh_points = open_foam_handler.parse('tests/test_datasets/test_openFOAM') - np.testing.assert_almost_equal(mesh_points[-1][2], 0.05) - - - def test_open_foam_write_failing_filename_type(self): - open_foam_handler = fh.OpenFoamHandler() - mesh_points = open_foam_handler.parse('tests/test_datasets/test_openFOAM') - with self.assertRaises(TypeError): - open_foam_handler.write(mesh_points, -1.) - - - def test_open_foam_write_failing_check_extension(self): - open_foam_handler = fh.OpenFoamHandler() - mesh_points = open_foam_handler.parse('tests/test_datasets/test_openFOAM') - with self.assertRaises(ValueError): - open_foam_handler.write(mesh_points, 'tests/test_datasets/test_square.iges') - - - def test_open_foam_write_failing_infile_instantiation(self): - open_foam_handler = fh.OpenFoamHandler() - mesh_points = np.zeros((40, 3)) - with self.assertRaises(RuntimeError): - open_foam_handler.write(mesh_points, 'tests/test_datasets/test_openFOAM_out') - - - def test_open_foam_write_outfile(self): - open_foam_handler = fh.OpenFoamHandler() - mesh_points = open_foam_handler.parse('tests/test_datasets/test_openFOAM') - outfilename = 'tests/test_datasets/test_openFOAM_out' - open_foam_handler.write(mesh_points, outfilename) - assert open_foam_handler.outfile == outfilename - os.remove(outfilename) - - - def test_open_foam_write_comparison(self): - open_foam_handler = fh.OpenFoamHandler() - mesh_points = open_foam_handler.parse('tests/test_datasets/test_openFOAM') - mesh_points[0] = [-14., 1.55, 0.2] - mesh_points[1] = [-14.3, 2.55, 0.3] - mesh_points[2] = [-14.3, 2.55, 0.3] - mesh_points[2000] = [7.8, -42.8, .0] - mesh_points[2001] = [8.8, -41.8, .1] - mesh_points[2002] = [9.8, -40.8, .0] - mesh_points[-3] = [236.3, 183.7, 0.06] - mesh_points[-2] = [237.3, 183.7, 0.06] - mesh_points[-1] = [236.3, 185.7, 0.06] - - outfilename = 'tests/test_datasets/test_openFOAM_out' - outfilename_expected = 'tests/test_datasets/test_openFOAM_out_true' - - open_foam_handler.write(mesh_points, outfilename) - self.assertTrue(filecmp.cmp(outfilename, outfilename_expected)) - os.remove(outfilename) - - diff --git a/tests/test_openfhandler.py b/tests/test_openfhandler.py new file mode 100644 index 0000000..b5c9f33 --- /dev/null +++ b/tests/test_openfhandler.py @@ -0,0 +1,136 @@ + +from unittest import TestCase +import unittest +import pygem.openfhandler as ofh +import numpy as np +import filecmp +import os + + +class TestOpenFoamHandler(TestCase): + + + def test_open_foam_instantiation(self): + open_foam_handler = ofh.OpenFoamHandler() + + + def test_open_foam_default_infile_member(self): + open_foam_handler = ofh.OpenFoamHandler() + assert open_foam_handler.infile == None + + + def test_open_foam_default_outfile_member(self): + open_foam_handler = ofh.OpenFoamHandler() + assert open_foam_handler.outfile == None + + + def test_open_foam_default_extension_member(self): + open_foam_handler = ofh.OpenFoamHandler() + assert open_foam_handler.extension == '' + + + def test_open_foam_parse_failing_filename_type(self): + open_foam_handler = ofh.OpenFoamHandler() + with self.assertRaises(TypeError): + mesh_points = open_foam_handler.parse(.2) + + + def test_open_foam_parse_failing_check_extension(self): + open_foam_handler = ofh.OpenFoamHandler() + with self.assertRaises(ValueError): + mesh_points = open_foam_handler.parse('tests/test_datasets/test_square.iges') + + + def test_open_foam_parse_infile(self): + open_foam_handler = ofh.OpenFoamHandler() + mesh_points = open_foam_handler.parse('tests/test_datasets/test_openFOAM') + assert open_foam_handler.infile == 'tests/test_datasets/test_openFOAM' + + + def test_open_foam_parse_shape(self): + open_foam_handler = ofh.OpenFoamHandler() + mesh_points = open_foam_handler.parse('tests/test_datasets/test_openFOAM') + assert mesh_points.shape == (21812, 3) + + + def test_open_foam_parse_coords_1(self): + open_foam_handler = ofh.OpenFoamHandler() + mesh_points = open_foam_handler.parse('tests/test_datasets/test_openFOAM') + np.testing.assert_almost_equal(mesh_points[33][0], 1.42254) + + + def test_open_foam_parse_coords_2(self): + open_foam_handler = ofh.OpenFoamHandler() + mesh_points = open_foam_handler.parse('tests/test_datasets/test_openFOAM') + np.testing.assert_almost_equal(mesh_points[1708][1], -3.13059) + + + def test_open_foam_parse_coords_3(self): + open_foam_handler = ofh.OpenFoamHandler() + mesh_points = open_foam_handler.parse('tests/test_datasets/test_openFOAM') + np.testing.assert_almost_equal(mesh_points[3527][2], .0) + + + def test_open_foam_parse_coords_4(self): + open_foam_handler = ofh.OpenFoamHandler() + mesh_points = open_foam_handler.parse('tests/test_datasets/test_openFOAM') + np.testing.assert_almost_equal(mesh_points[0][0], -17.5492) + + + def test_open_foam_parse_coords_5(self): + open_foam_handler = ofh.OpenFoamHandler() + mesh_points = open_foam_handler.parse('tests/test_datasets/test_openFOAM') + np.testing.assert_almost_equal(mesh_points[-1][2], 0.05) + + + def test_open_foam_write_failing_filename_type(self): + open_foam_handler = ofh.OpenFoamHandler() + mesh_points = open_foam_handler.parse('tests/test_datasets/test_openFOAM') + with self.assertRaises(TypeError): + open_foam_handler.write(mesh_points, -1.) + + + def test_open_foam_write_failing_check_extension(self): + open_foam_handler = ofh.OpenFoamHandler() + mesh_points = open_foam_handler.parse('tests/test_datasets/test_openFOAM') + with self.assertRaises(ValueError): + open_foam_handler.write(mesh_points, 'tests/test_datasets/test_square.iges') + + + def test_open_foam_write_failing_infile_instantiation(self): + open_foam_handler = ofh.OpenFoamHandler() + mesh_points = np.zeros((40, 3)) + with self.assertRaises(RuntimeError): + open_foam_handler.write(mesh_points, 'tests/test_datasets/test_openFOAM_out') + + + def test_open_foam_write_outfile(self): + open_foam_handler = ofh.OpenFoamHandler() + mesh_points = open_foam_handler.parse('tests/test_datasets/test_openFOAM') + outfilename = 'tests/test_datasets/test_openFOAM_out' + open_foam_handler.write(mesh_points, outfilename) + assert open_foam_handler.outfile == outfilename + os.remove(outfilename) + + + def test_open_foam_write_comparison(self): + open_foam_handler = ofh.OpenFoamHandler() + mesh_points = open_foam_handler.parse('tests/test_datasets/test_openFOAM') + mesh_points[0] = [-14., 1.55, 0.2] + mesh_points[1] = [-14.3, 2.55, 0.3] + mesh_points[2] = [-14.3, 2.55, 0.3] + mesh_points[2000] = [7.8, -42.8, .0] + mesh_points[2001] = [8.8, -41.8, .1] + mesh_points[2002] = [9.8, -40.8, .0] + mesh_points[-3] = [236.3, 183.7, 0.06] + mesh_points[-2] = [237.3, 183.7, 0.06] + mesh_points[-1] = [236.3, 185.7, 0.06] + + outfilename = 'tests/test_datasets/test_openFOAM_out' + outfilename_expected = 'tests/test_datasets/test_openFOAM_out_true' + + open_foam_handler.write(mesh_points, outfilename) + self.assertTrue(filecmp.cmp(outfilename, outfilename_expected)) + os.remove(outfilename) + + diff --git a/tests/test_package.py b/tests/test_package.py index c2601d7..4d63c69 100644 --- a/tests/test_package.py +++ b/tests/test_package.py @@ -9,12 +9,44 @@ class TestPackage(TestCase): + def test_import_pg_1(self): + import pygem as pg + params = pg.params.FFDParameters() + + + def test_import_pg_2(self): + import pygem as pg + mat = pg.affine.angles2matrix(2) + + + def test_import_pg_3(self): + import pygem as pg + fh = pg.filehandler.FileHandler() + + + def test_import_pg_4(self): + import pygem as pg + unvh = pg.unvhandler.UnvHandler() + + + def test_import_pg_5(self): + import pygem as pg + stlh = pg.stlhandler.StlHandler() + + + def test_import_pg_6(self): + import pygem as pg + opfh = pg.openfhandler.OpenFoamHandler() + + + def test_import_pg_7(self): + import pygem as pg + vtkh = pg.vtkhandler.VtkHandler() + + def test_modules_name(self): import pygem package = pygem - mod = ['__init__.py'] - for __, modname, __ in pkgutil.iter_modules(package.__path__): - mod.append(modname + '.py') f_aux = [] for (__, __, filenames) in walk('pygem'): @@ -22,18 +54,8 @@ def test_modules_name(self): f = [] for i in f_aux: - __, file_ext = path.splitext(i) - if file_ext == '.py': - f.append(i) - - self.assertItemsEqual(mod, f) - - - def test_import_pg_1(self): - import pygem as pg - params = pg.params.FFDParameters() - - - def test_import_pg_2(self): - import pygem as pg - mat = pg.affine.angles2matrix(2) \ No newline at end of file + file_name, file_ext = path.splitext(i) + if file_name != '__init__' and file_ext == '.py': + f.append(file_name) + + self.assertItemsEqual(package.__all__, f) diff --git a/tests/test_stlhandler.py b/tests/test_stlhandler.py new file mode 100644 index 0000000..77586f5 --- /dev/null +++ b/tests/test_stlhandler.py @@ -0,0 +1,150 @@ + +from unittest import TestCase +import unittest +import pygem.stlhandler as sh +import numpy as np +import filecmp +import os + + +class TestStlHandler(TestCase): + + + def test_stl_instantiation(self): + stl_handler = sh.StlHandler() + + + def test_stl_default_infile_member(self): + stl_handler = sh.StlHandler() + assert stl_handler.infile == None + + + def test_stl_default_outfile_member(self): + stl_handler = sh.StlHandler() + assert stl_handler.outfile == None + + + def test_stl_default_extension_member(self): + stl_handler = sh.StlHandler() + assert stl_handler.extension == '.stl' + + + def test_stl_parse_failing_filename_type(self): + stl_handler = sh.StlHandler() + with self.assertRaises(TypeError): + mesh_points = stl_handler.parse(5.2) + + + def test_stl_parse_failing_check_extension(self): + stl_handler = sh.StlHandler() + with self.assertRaises(ValueError): + mesh_points = stl_handler.parse('tests/test_datasets/test_square.iges') + + + def test_stl_parse_infile(self): + stl_handler = sh.StlHandler() + mesh_points = stl_handler.parse('tests/test_datasets/test_sphere.stl') + assert stl_handler.infile == 'tests/test_datasets/test_sphere.stl' + + + def test_stl_parse_shape(self): + stl_handler = sh.StlHandler() + mesh_points = stl_handler.parse('tests/test_datasets/test_sphere.stl') + assert mesh_points.shape == (7200, 3) + + + def test_stl_parse_coords_1(self): + stl_handler = sh.StlHandler() + mesh_points = stl_handler.parse('tests/test_datasets/test_sphere.stl') + np.testing.assert_almost_equal(mesh_points[33][0], -21.31975937) + + + def test_stl_parse_coords_2(self): + stl_handler = sh.StlHandler() + mesh_points = stl_handler.parse('tests/test_datasets/test_sphere.stl') + np.testing.assert_almost_equal(mesh_points[1708][1], 2.58431911) + + + def test_stl_parse_coords_3(self): + stl_handler = sh.StlHandler() + mesh_points = stl_handler.parse('tests/test_datasets/test_sphere.stl') + np.testing.assert_almost_equal(mesh_points[3527][2], -2.47207999) + + + def test_stl_parse_coords_4(self): + stl_handler = sh.StlHandler() + mesh_points = stl_handler.parse('tests/test_datasets/test_sphere.stl') + np.testing.assert_almost_equal(mesh_points[0][0], -21.31975937) + + + def test_stl_parse_coords_5(self): + stl_handler = sh.StlHandler() + mesh_points = stl_handler.parse('tests/test_datasets/test_sphere.stl') + np.testing.assert_almost_equal(mesh_points[-1][2], -39.05963898) + + + def test_stl_write_failing_filename_type(self): + stl_handler = sh.StlHandler() + mesh_points = stl_handler.parse('tests/test_datasets/test_sphere.stl') + with self.assertRaises(TypeError): + stl_handler.write(mesh_points, 4.) + + + def test_stl_write_failing_check_extension(self): + stl_handler = sh.StlHandler() + mesh_points = stl_handler.parse('tests/test_datasets/test_sphere.stl') + with self.assertRaises(ValueError): + stl_handler.write(mesh_points, 'tests/test_datasets/test_square.iges') + + + def test_stl_write_failing_infile_instantiation(self): + stl_handler = sh.StlHandler() + mesh_points = np.zeros((40, 3)) + with self.assertRaises(RuntimeError): + stl_handler.write(mesh_points, 'tests/test_datasets/test_sphere_out.stl') + + + def test_stl_write_outfile(self): + stl_handler = sh.StlHandler() + mesh_points = stl_handler.parse('tests/test_datasets/test_sphere.stl') + outfilename = 'tests/test_datasets/test_sphere_out.stl' + stl_handler.write(mesh_points, outfilename) + assert stl_handler.outfile == outfilename + os.remove(outfilename) + + + def test_stl_write_comparison(self): + stl_handler = sh.StlHandler() + mesh_points = stl_handler.parse('tests/test_datasets/test_sphere.stl') + mesh_points[0] = [-40.2, -20.5, 60.9] + mesh_points[1] = [-40.2, -10.5, 60.9] + mesh_points[2] = [-40.2, -10.5, 60.9] + mesh_points[2000] = [-40.2, -20.5, 60.9] + mesh_points[2001] = [-40.2, -10.5, 60.9] + mesh_points[2002] = [-40.2, -10.5, 60.9] + mesh_points[6100] = [-40.2, -20.5, 60.9] + mesh_points[6101] = [-40.2, -10.5, 60.9] + mesh_points[6102] = [-40.2, -10.5, 60.9] + + outfilename = 'tests/test_datasets/test_sphere_out.stl' + outfilename_expected = 'tests/test_datasets/test_sphere_out_true.stl' + + stl_handler.write(mesh_points, outfilename) + self.assertTrue(filecmp.cmp(outfilename, outfilename_expected)) + os.remove(outfilename) + + + def test_stl_plot_save_fig(self): + stl_handler = sh.StlHandler() + mesh_points = stl_handler.parse('tests/test_datasets/test_sphere.stl') + stl_handler.plot(save_fig=True) + if not os.path.isfile('tests/test_datasets/test_sphere.png'): + assert False + else: + os.remove('tests/test_datasets/test_sphere.png') + + + def test_stl_plot_failing_outfile_type(self): + stl_handler = sh.StlHandler() + with self.assertRaises(TypeError): + stl_handler.plot(plot_file=3) diff --git a/tests/test_unvhandler.py b/tests/test_unvhandler.py new file mode 100644 index 0000000..42f6b27 --- /dev/null +++ b/tests/test_unvhandler.py @@ -0,0 +1,131 @@ + +from unittest import TestCase +import unittest +import pygem.unvhandler as uh +import numpy as np +import filecmp +import os + + +class TestUnvHandler(TestCase): + + + def test_unv_instantiation(self): + unv_handler = uh.UnvHandler() + + + def test_unv_default_infile_member(self): + unv_handler = uh.UnvHandler() + assert unv_handler.infile == None + + + def test_unv_default_outfile_member(self): + unv_handler = uh.UnvHandler() + assert unv_handler.outfile == None + + + def test_unv_default_extension_member(self): + unv_handler = uh.UnvHandler() + assert unv_handler.extension == '.unv' + + + def test_unv_parse_failing_filename_type(self): + unv_handler = uh.UnvHandler() + with self.assertRaises(TypeError): + mesh_points = unv_handler.parse(5.2) + + + def test_unv_parse_failing_check_extension(self): + unv_handler = uh.UnvHandler() + with self.assertRaises(ValueError): + mesh_points = unv_handler.parse('tests/test_datasets/test_square.iges') + + + def test_unv_parse_infile(self): + unv_handler = uh.UnvHandler() + mesh_points = unv_handler.parse('tests/test_datasets/test_square.unv') + assert unv_handler.infile == 'tests/test_datasets/test_square.unv' + + + def test_unv_parse_shape(self): + unv_handler = uh.UnvHandler() + mesh_points = unv_handler.parse('tests/test_datasets/test_square.unv') + assert mesh_points.shape == (256, 3) + + + def test_unv_parse_coords_1(self): + unv_handler = uh.UnvHandler() + mesh_points = unv_handler.parse('tests/test_datasets/test_square.unv') + np.testing.assert_almost_equal(mesh_points[33][0], 1.0) + + + def test_unv_parse_coords_2(self): + unv_handler = uh.UnvHandler() + mesh_points = unv_handler.parse('tests/test_datasets/test_square.unv') + np.testing.assert_almost_equal(mesh_points[178][1], 0.4) + + + def test_unv_parse_coords_3(self): + unv_handler = uh.UnvHandler() + mesh_points = unv_handler.parse('tests/test_datasets/test_square.unv') + np.testing.assert_almost_equal(mesh_points[100][2], 0.0) + + + def test_unv_parse_coords_4(self): + unv_handler = uh.UnvHandler() + mesh_points = unv_handler.parse('tests/test_datasets/test_square.unv') + np.testing.assert_almost_equal(mesh_points[0][0], 0.0) + + + def test_unv_parse_coords_5(self): + unv_handler = uh.UnvHandler() + mesh_points = unv_handler.parse('tests/test_datasets/test_square.unv') + np.testing.assert_almost_equal(mesh_points[-1][2], 0.0) + + + def test_unv_write_failing_filename_type(self): + unv_handler = uh.UnvHandler() + mesh_points = unv_handler.parse('tests/test_datasets/test_square.unv') + with self.assertRaises(TypeError): + unv_handler.write(mesh_points, -2) + + + def test_unv_write_failing_check_extension(self): + unv_handler = uh.UnvHandler() + mesh_points = unv_handler.parse('tests/test_datasets/test_square.unv') + with self.assertRaises(ValueError): + unv_handler.write(mesh_points, 'tests/test_datasets/test_square.iges') + + + def test_unv_write_failing_infile_instantiation(self): + unv_handler = uh.UnvHandler() + mesh_points = np.zeros((20, 3)) + with self.assertRaises(RuntimeError): + unv_handler.write(mesh_points, 'tests/test_datasets/test_square_out.unv') + + + def test_unv_write_outfile(self): + unv_handler = uh.UnvHandler() + mesh_points = unv_handler.parse('tests/test_datasets/test_square.unv') + outfilename = 'tests/test_datasets/test_square_out.unv' + unv_handler.write(mesh_points, outfilename) + assert unv_handler.outfile == outfilename + os.remove(outfilename) + + + def test_unv_write_comparison(self): + unv_handler = uh.UnvHandler() + mesh_points = unv_handler.parse('tests/test_datasets/test_square.unv') + mesh_points[0][0] = 2.2 + mesh_points[5][1] = 4.3 + mesh_points[9][2] = 0.5 + mesh_points[45][0] = 7.2 + mesh_points[132][1] = -1.2 + mesh_points[255][2] = -3.6 + + outfilename = 'tests/test_datasets/test_square_out.unv' + outfilename_expected = 'tests/test_datasets/test_square_out_true.unv' + + unv_handler.write(mesh_points, outfilename) + self.assertTrue(filecmp.cmp(outfilename, outfilename_expected)) + os.remove(outfilename) diff --git a/tests/test_vtkhandler.py b/tests/test_vtkhandler.py new file mode 100644 index 0000000..95df6aa --- /dev/null +++ b/tests/test_vtkhandler.py @@ -0,0 +1,135 @@ + +from unittest import TestCase +import unittest +import pygem.vtkhandler as vh +import numpy as np +import filecmp +import os + + +class TestVtkHandler(TestCase): + + + def test_vtk_instantiation(self): + vtk_handler = vh.VtkHandler() + + + def test_vtk_default_infile_member(self): + vtk_handler = vh.VtkHandler() + assert vtk_handler.infile == None + + + def test_vtk_default_outfile_member(self): + vtk_handler = vh.VtkHandler() + assert vtk_handler.outfile == None + + + def test_vtk_default_extension_member(self): + vtk_handler = vh.VtkHandler() + assert vtk_handler.extension == '.vtk' + + + def test_vtk_parse_failing_filename_type(self): + vtk_handler = vh.VtkHandler() + with self.assertRaises(TypeError): + mesh_points = vtk_handler.parse(5.2) + + + def test_vtk_parse_failing_check_extension(self): + vtk_handler = vh.VtkHandler() + with self.assertRaises(ValueError): + mesh_points = vtk_handler.parse('tests/test_datasets/test_square.iges') + + + def test_vtk_parse_infile(self): + vtk_handler = vh.VtkHandler() + mesh_points = vtk_handler.parse('tests/test_datasets/test_red_blood_cell.vtk') + assert vtk_handler.infile == 'tests/test_datasets/test_red_blood_cell.vtk' + + + def test_vtk_parse_shape(self): + vtk_handler = vh.VtkHandler() + mesh_points = vtk_handler.parse('tests/test_datasets/test_red_blood_cell.vtk') + assert mesh_points.shape == (500, 3) + + + def test_vtk_parse_coords_1(self): + vtk_handler = vh.VtkHandler() + mesh_points = vtk_handler.parse('tests/test_datasets/test_red_blood_cell.vtk') + np.testing.assert_almost_equal(mesh_points[33][0], -2.2977099) + + + def test_vtk_parse_coords_2(self): + vtk_handler = vh.VtkHandler() + mesh_points = vtk_handler.parse('tests/test_datasets/test_red_blood_cell.vtk') + np.testing.assert_almost_equal(mesh_points[178][1], 0.143506) + + + def test_vtk_parse_coords_3(self): + vtk_handler = vh.VtkHandler() + mesh_points = vtk_handler.parse('tests/test_datasets/test_red_blood_cell.vtk') + np.testing.assert_almost_equal(mesh_points[100][2], 2.3306999) + + + def test_vtk_parse_coords_4(self): + vtk_handler = vh.VtkHandler() + mesh_points = vtk_handler.parse('tests/test_datasets/test_red_blood_cell.vtk') + np.testing.assert_almost_equal(mesh_points[0][0], -3.42499995) + + + def test_vtk_parse_coords_5(self): + vtk_handler = vh.VtkHandler() + mesh_points = vtk_handler.parse('tests/test_datasets/test_red_blood_cell.vtk') + np.testing.assert_almost_equal(mesh_points[-1][2], -2.8480699) + + + def test_vtk_write_failing_filename_type(self): + vtk_handler = vh.VtkHandler() + mesh_points = vtk_handler.parse('tests/test_datasets/test_red_blood_cell.vtk') + with self.assertRaises(TypeError): + vtk_handler.write(mesh_points, -2) + + + def test_vtk_write_failing_check_extension(self): + vtk_handler = vh.VtkHandler() + mesh_points = vtk_handler.parse('tests/test_datasets/test_red_blood_cell.vtk') + with self.assertRaises(ValueError): + vtk_handler.write(mesh_points, 'tests/test_datasets/test_square.iges') + + + def test_vtk_write_failing_infile_instantiation(self): + vtk_handler = vh.VtkHandler() + mesh_points = np.zeros((20, 3)) + with self.assertRaises(RuntimeError): + vtk_handler.write(mesh_points, 'tests/test_datasets/test_red_blood_cell_out.vtk') + + + def test_vtk_write_outfile(self): + vtk_handler = vh.VtkHandler() + mesh_points = vtk_handler.parse('tests/test_datasets/test_red_blood_cell.vtk') + outfilename = 'tests/test_datasets/test_red_blood_cell_out.vtk' + vtk_handler.write(mesh_points, outfilename) + assert vtk_handler.outfile == outfilename + os.remove(outfilename) + + + def test_vtk_write_comparison(self): + import vtk + vtk_handler = vh.VtkHandler() + mesh_points = vtk_handler.parse('tests/test_datasets/test_red_blood_cell.vtk') + mesh_points[0][0] = 2.2 + mesh_points[5][1] = 4.3 + mesh_points[9][2] = 0.5 + mesh_points[45][0] = 7.2 + mesh_points[132][1] = -1.2 + mesh_points[255][2] = -3.6 + + outfilename = 'tests/test_datasets/test_red_blood_cell_out.vtk' + if vtk.VTK_MAJOR_VERSION <= 5: + outfilename_expected = 'tests/test_datasets/test_red_blood_cell_out_true_version5.vtk' + else: + outfilename_expected = 'tests/test_datasets/test_red_blood_cell_out_true_version6.vtk' + + vtk_handler.write(mesh_points, outfilename) + self.assertTrue(filecmp.cmp(outfilename, outfilename_expected)) + os.remove(outfilename)