def test_arr2vtkPoints(self):
     """Test Numeric array to vtkPoints conversion."""
     a = [[0.0, 0.0, 0.0], [1.0, 1.0, 1.0]]
     p = array_handler.array2vtkPoints(a)
     self.assertEqual(p.GetPoint(0), (0.0, 0.0, 0.0))
     self.assertEqual(p.GetPoint(1), (1.0, 1.0, 1.0))
     p = vtk.vtkPoints()
     ident = id(p)
     p = array_handler.array2vtkPoints(numpy.array(a), p)
     self.assertEqual(p.GetPoint(0), (0.0, 0.0, 0.0))
     self.assertEqual(p.GetPoint(1), (1.0, 1.0, 1.0))
     self.assertEqual(id(p), ident)
     self.assertRaises(AssertionError, array_handler.array2vtkPoints, [0.0, 1.0])
     self.assertRaises(AssertionError, array_handler.array2vtkPoints, [0.0, 1.0, 1.0])
Ejemplo n.º 2
0
 def test_arr2vtkPoints(self):
     """Test Numeric array to vtkPoints conversion."""
     a = [[0.0, 0.0, 0.0], [1.0, 1.0, 1.0]]
     p = array_handler.array2vtkPoints(a)
     self.assertEqual(p.GetPoint(0), (0.0, 0.0, 0.0))
     self.assertEqual(p.GetPoint(1), (1.0, 1.0, 1.0))
     p = vtk.vtkPoints()
     ident = id(p)
     p = array_handler.array2vtkPoints(numpy.array(a), p)
     self.assertEqual(p.GetPoint(0), (0.0, 0.0, 0.0))
     self.assertEqual(p.GetPoint(1), (1.0, 1.0, 1.0))
     self.assertEqual(id(p), ident)
     self.assertRaises(AssertionError, array_handler.array2vtkPoints,
                       [0.0, 1.0])
     self.assertRaises(AssertionError, array_handler.array2vtkPoints,
                       [0.0, 1.0, 1.0])
Ejemplo n.º 3
0
def dolfin2polydata(mesh):
    """Convert dolfin mesh to vtk polydata

    :param mesh: dolfin mesh
    :returns: mesh in vtk polydata format
    :rtype: vtk.vtkPolyData

    """
    import vtk
    from tvtk.array_handler import array2vtkPoints, array2vtkCellArray

    gdim = mesh.geometry().dim()
    mdim = mesh.topology().dim()

    order = 1
    # coordinates
    pts = array2vtkPoints(mesh.coordinates())
    # connectivity
    dim = mesh.topology().dim()
    ndof = dolfin.Cell(mesh, 0).num_vertices()
    conn = mesh.topology()(dim, 0)().reshape((-1, ndof))
    elms = array2vtkCellArray(conn)

    grid = vtk.vtkPolyData()
    grid.SetPoints(pts)

    # only these are supported by dolfin
    vtk_shape = {
        1: {
            1: vtk.VTK_LINE,
            2: vtk.VTK_TRIANGLE,
            3: vtk.VTK_TETRA
        },
        2: {
            1: vtk.VTK_QUADRATIC_EDGE,
            2: vtk.VTK_QUADRATIC_TRIANGLE,
            3: vtk.VTK_QUADRATIC_TETRA,
        },
    }[1][mdim]

    grid.SetLines(elms)

    return grid
Ejemplo n.º 4
0
def dolfin2vtu(mesh):
    """Convert dolfin mesh to vtk unstructured grid

    :param mesh: dolfin mesh
    :returns: mesh in vtk unstructured grid format
    :rtype: vtk.vtkUnstructuredGrid

    """
    import vtk

    domain = mesh.ufl_domain()
    gdim = domain.geometric_dimension()
    mdim = domain.topological_dimension()
    order = 1
    # coordinates of the mesh
    coords = mesh.coordinates().copy()

    # connectivity
    conn = mesh.cells()

    coords = np.hstack([coords, np.zeros((coords.shape[0], 3 - gdim))])

    # only these are supported by dolfin
    vtk_shape = {
        1: {
            1: vtk.VTK_LINE,
            2: vtk.VTK_TRIANGLE,
            3: vtk.VTK_TETRA
        },
        2: {
            1: vtk.VTK_QUADRATIC_EDGE,
            2: vtk.VTK_QUADRATIC_TRIANGLE,
            3: vtk.VTK_QUADRATIC_TETRA,
        },
    }[order][mdim]

    # create the grid
    from tvtk.array_handler import array2vtkPoints, array2vtkCellArray

    grid = vtk.vtkUnstructuredGrid()
    grid.SetPoints(array2vtkPoints(coords))
    grid.SetCells(vtk_shape, array2vtkCellArray(conn))
    return grid