def test_arr2cell_array(self): """Test Numeric array to vtkCellArray conversion.""" # Test list of lists. a = [[0], [1, 2], [3, 4, 5], [6, 7, 8, 9]] cells = array_handler.array2vtkCellArray(a) z = numpy.array([1, 0, 2, 1,2, 3, 3,4,5, 4, 6,7,8,9]) arr = array_handler.vtk2array(cells.GetData()) self.assertEqual(numpy.sum(arr - z), 0) self.assertEqual(len(arr.shape), 1) self.assertEqual(len(arr), 14) # Test if optional argument stuff also works. cells = vtk.vtkCellArray() ident = id(cells) cells = array_handler.array2vtkCellArray(a, cells) self.assertEqual(id(cells), ident) arr = array_handler.vtk2array(cells.GetData()) self.assertEqual(numpy.sum(arr - z), 0) self.assertEqual(cells.GetNumberOfCells(), 4) # Make sure this resets the cell array and does not add to the # existing list! cells = array_handler.array2vtkCellArray(a, cells) self.assertEqual(cells.GetNumberOfCells(), 4) # Test Numeric array handling. N = 3 a = numpy.zeros((N,3), numpy.int) a[:,1] = 1 a[:,2] = 2 cells = array_handler.array2vtkCellArray(a) arr = array_handler.vtk2array(cells.GetData()) expect = numpy.array([3, 0, 1, 2]*3, numpy.int) self.assertEqual(numpy.alltrue(numpy.equal(arr, expect)), True) self.assertEqual(cells.GetNumberOfCells(), N) # Test if a list of Numeric arrays of different cell lengths works. l_a = [a[:,:1], a, a[:2,:2]] cells = array_handler.array2vtkCellArray(l_a) arr = array_handler.vtk2array(cells.GetData()) expect = numpy.array([1, 0]*3 + [3, 0, 1, 2]*3 + [2, 0,1]*2, numpy.int) self.assertEqual(numpy.alltrue(numpy.equal(arr, expect)), True) self.assertEqual(cells.GetNumberOfCells(), N*2 + 2) # This should not take a long while. This merely tests if a # million cells can be created rapidly. N = int(1e6) a = numpy.zeros((N,3), numpy.int) a[:,1] = 1 a[:,2] = 2 cells = array_handler.array2vtkCellArray(a) self.assertEqual(cells.GetNumberOfCells(), N)
def test_arr2cell_array(self): """Test Numeric array to vtkCellArray conversion.""" # Test list of lists. a = [[0], [1, 2], [3, 4, 5], [6, 7, 8, 9]] cells = array_handler.array2vtkCellArray(a) z = numpy.array([1, 0, 2, 1, 2, 3, 3, 4, 5, 4, 6, 7, 8, 9]) arr = array_handler.vtk2array(cells.GetData()) self.assertEqual(numpy.sum(arr - z), 0) self.assertEqual(len(arr.shape), 1) self.assertEqual(len(arr), 14) # Test if optional argument stuff also works. cells = vtk.vtkCellArray() ident = id(cells) cells = array_handler.array2vtkCellArray(a, cells) self.assertEqual(id(cells), ident) arr = array_handler.vtk2array(cells.GetData()) self.assertEqual(numpy.sum(arr - z), 0) self.assertEqual(cells.GetNumberOfCells(), 4) # Make sure this resets the cell array and does not add to the # existing list! cells = array_handler.array2vtkCellArray(a, cells) self.assertEqual(cells.GetNumberOfCells(), 4) # Test Numeric array handling. N = 3 a = numpy.zeros((N, 3), numpy.int) a[:, 1] = 1 a[:, 2] = 2 cells = array_handler.array2vtkCellArray(a) arr = array_handler.vtk2array(cells.GetData()) expect = numpy.array([3, 0, 1, 2] * 3, numpy.int) self.assertEqual(numpy.alltrue(numpy.equal(arr, expect)), True) self.assertEqual(cells.GetNumberOfCells(), N) # Test if a list of Numeric arrays of different cell lengths works. l_a = [a[:, :1], a, a[:2, :2]] cells = array_handler.array2vtkCellArray(l_a) arr = array_handler.vtk2array(cells.GetData()) expect = numpy.array([1, 0] * 3 + [3, 0, 1, 2] * 3 + [2, 0, 1] * 2, numpy.int) self.assertEqual(numpy.alltrue(numpy.equal(arr, expect)), True) self.assertEqual(cells.GetNumberOfCells(), N * 2 + 2) # This should not take a long while. This merely tests if a # million cells can be created rapidly. N = int(1e6) a = numpy.zeros((N, 3), numpy.int) a[:, 1] = 1 a[:, 2] = 2 cells = array_handler.array2vtkCellArray(a) self.assertEqual(cells.GetNumberOfCells(), N)
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
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