コード例 #1
0
    def makeFromHdf5Object(h5obj):
        """
        Create new :obj:`Mesh` instance from given hdf5 object. Complementary to :obj:`asHdf5Object`.

        :return: new instance
        :rtype: :obj:`Mesh` or its subclass
        """
        # instantiate the right Mesh subclass
        import importlib
        from mupif.Vertex import Vertex
        from mupif.Cell import Cell
        klass = getattr(importlib.import_module(h5obj.attrs['__module__']),
                        h5obj.attrs['__class__'])
        ret = klass()
        mvc, mct, mci = h5obj['vertex_coords'], h5obj['cell_types'], h5obj[
            'cell_vertices']
        # construct vertices
        vertices = [
            Vertex(number=vi, label=None, coords=tuple(mvc[vi]))
            for vi in range(mvc.shape[0])
        ]
        cells = [
            Cell.getClassForCellGeometryType(mct[ci])(
                mesh=ret,
                number=ci,
                label=None,
                # vertices=tuple(mci[ci])
                vertices=[vertices[i] for i in mci[ci]])
            for ci in range(mct.shape[0])
        ]
        ret.setup(vertexList=vertices, cellList=cells)
        return ret
コード例 #2
0
    def makeFromPyvtkUnstructuredGrid(ugr):
        '''Create a new instance of :obj:`UnstructuredMesh` based on pyvtk.UnstructuredGrid object. Cell types are mapped between pyvtk and mupif (supported: triangle, tetra, quad, hexahedron).

        :param ugr: instance of pyvtk.UnstructuredGrid
        :return: new instance of :obj:`UnstructuredMesh`
        '''
        import pyvtk
        from . import Cell, Vertex
        ret = UnstructuredMesh()
        vertices = []
        cells = []
        vertices = [
            Vertex.Vertex(number=ip, label=ip, coords=ugr.points[ip])
            for ip in range(len(ugr.points))
        ]
        for cellName in [
                'vertex', 'poly_vertex', 'line', 'poly_line', 'triangle',
                'triangle_strip', 'polygon', 'pixel', 'quad', 'tetra', 'voxel',
                'hexahedron', 'wedge', 'pyramid'
        ]:
            if not hasattr(ugr, cellName): continue
            val = getattr(ugr, cellName)
            if val == [] or (len(val) == 1 and val[0] == []):
                continue  # no cells of this type
            # print(cellName,val)
            cellGeomNameMap = {
                'triangle': CellGeometryType.CGT_TRIANGLE_1,
                'tetra': CellGeometryType.CGT_TETRA,
                'quad': CellGeometryType.CGT_QUAD,
                'hexahedron': CellGeometryType.CGT_HEXAHEDRON,
            }
            try:
                cgt = cellGeomNameMap[cellName]
            except KeyError:
                raise NotImplementedError(
                    "pyvtk cell type '%s' is not handled by the mupif import routine."
                    % cellName)
            cells.append([
                Cell.Cell.getClassForCellGeometryType(cgt)(
                    mesh=ret,
                    number=len(cells),
                    label=None,
                    vertices=[vertices[i] for iv in val[i]])
                for i in range(len(val))
            ])
        ret.setup(vertexList=vertices, cellList=cells)
        return ret
コード例 #3
0
ファイル: Mesh.py プロジェクト: stanislavsulc/mupif
    def makeFromVtkUnstructuredGrid(ugrid):
        """Create a new instance of :obj:`UnstructuredMesh` based on VTK's unstructured grid object. Cell types are
        mapped between VTK and mupif (supported: vtkTriangle, vtkQuadraticTriangle, vtkQuad, vtkTetra, vtkHexahedron).

        :param ugrid: instance of vtk.vtkUnstructuredGrid
        :return: new instance of :obj:`UnstructuredMesh`
        """
        import vtk
        from . import Cell, Vertex
        ret = UnstructuredMesh()
        np, nc = ugrid.GetNumberOfPoints(), ugrid.GetNumberOfCells()
        # vertices
        mupifVertices = [
            Vertex.Vertex(number=ip, label=ip, coords=ugrid.GetPoint(ip))
            for ip in range(np)
        ]
        # cells
        mupifCells = []
        for ic in range(nc):
            c = ugrid.GetCell(ic)
            pts = [c.GetPointId(i) for i in range(c.GetNumberOfPoints())]
            # map VTK type to our type?
            # or don't care and used cell types array to reconstruct cells
            # assuming that cell types were stored correctly and numbering did not change meanwhile
            # plus add safety check for the required number of points per cell
            cellGeomTypeMap = {
                vtk.VTK_TRIANGLE: CellGeometryType.CGT_TRIANGLE_1,
                vtk.VTK_QUADRATIC_TRIANGLE: CellGeometryType.CGT_TRIANGLE_2,
                vtk.VTK_TETRA: CellGeometryType.CGT_TETRA,
                vtk.VTK_QUAD: CellGeometryType.CGT_QUAD,
                vtk.VTK_HEXAHEDRON: CellGeometryType.CGT_HEXAHEDRON,
            }
            # find mupif class of the cell
            # if the lookup fails, KeyError propagates to the caller, which is what we want
            cgt = cellGeomTypeMap[c.GetCellType()]
            # create new cell and append to mupifCells
            mupifCells.append(
                Cell.Cell.getClassForCellGeometryType(cgt)(
                    #mesh=ret, number=ic, label=None, vertices=[mupifVertices[i] for i in pts]
                    mesh=ret,
                    number=ic,
                    label=None,
                    vertices=pts))
        ret.setup(vertexList=mupifVertices, cellList=mupifCells)
        return ret