def Mesh2VTKUGrid(mesh): vtkcelltypes=((),(vtk.VTK_EMPTY_CELL,vtk.VTK_VERTEX,vtk.VTK_LINE),(vtk.VTK_EMPTY_CELL,vtk.VTK_VERTEX,vtk.VTK_LINE,vtk.VTK_TRIANGLE,vtk.VTK_QUAD,vtk.VTK_POLYGON,vtk.VTK_POLYGON),(vtk.VTK_EMPTY_CELL,vtk.VTK_VERTEX,vtk.VTK_LINE,vtk.VTK_TRIANGLE,vtk.VTK_TETRA,vtk.VTK_CONVEX_POINT_SET,vtk.VTK_CONVEX_POINT_SET,vtk.VTK_CONVEX_POINT_SET,vtk.VTK_HEXAHEDRON)) npoints=mesh.num_vertices() geom=mesh.geometry() pts=vtk.vtkPoints() pts.SetNumberOfPoints(npoints) for i in xrange(npoints): p=geom.point(i) pts.SetPoint(i,p.x(),p.y(),p.z()) dim = mesh.topology().dim() ncells=mesh.num_cells() cells=vtk.vtkCellArray() cellTypes=vtk.vtkUnsignedCharArray() cellTypes.SetNumberOfTuples(ncells) cellLocations=vtk.vtkIdTypeArray() cellLocations.SetNumberOfTuples(ncells) loc=0 for (cell,i) in zip(mesh.cells(),xrange(ncells)) : ncellpoints=len(cell) cells.InsertNextCell(ncellpoints) for cpoint in cell: cells.InsertCellPoint(cpoint) cellTypes.SetTuple1(i,vtkcelltypes[dim][ncellpoints]) cellLocations.SetTuple1(i,loc) loc+=1+ncellpoints ugrid = vtk.vtkUnstructuredGrid() ugrid.SetPoints(pts) ugrid.SetCells(cellTypes,cellLocations,cells) return ugrid
def _load_basis_functions(self): assert self._filenames is not None # Load basis functions from XDMF files basis_functions = dict() for (c, filenames) in self._filenames.items(): basis_functions_c = list() for (n, filename) in enumerate(filenames): reader = vtkXdmf3Reader() reader.SetFileName(filename) reader.UpdateInformation() reader.Update() basis_functions_c.append(reader) basis_functions[c] = basis_functions_c # Load arrays arrays = dict() for (c, basis_functions_c) in basis_functions.items(): arrays_c = list() for basis_function in basis_functions_c: basis_function = basis_function.GetOutputDataObject(0) assert basis_function.GetCellData().GetNumberOfArrays() == 0 assert basis_function.GetPointData().GetNumberOfArrays() == 1 arrays_c.append(basis_function.GetPointData().GetArray(0)) arrays[c] = arrays_c # Make sure that the number of basis functions is the same across all components num_basis_functions = None for (c, basis_functions_c) in basis_functions.items(): if num_basis_functions is None: num_basis_functions = len(basis_functions_c) else: assert num_basis_functions == len(basis_functions_c) # Store basis functions assert self._basis_functions is None self._basis_functions = list() self._arrays = defaultdict(list) for n in range(num_basis_functions): basis_function_n = vtkUnstructuredGrid() basis_function_n.DeepCopy(basis_functions[self._components[0]][n].GetOutputDataObject(0)) basis_function_n.GetPointData().RemoveArray(0) for c in self._components: array_c_n_copy = vtkFloatArray() array_c_n_copy.DeepCopy(arrays[c][n]) array_c_n_copy.SetName(c) basis_function_n.GetPointData().AddArray(array_c_n_copy) self._arrays[c].append(vtk_to_numpy(array_c_n_copy)) self._basis_functions.append(basis_function_n) for c in self._components: self._arrays[c] = np.stack(self._arrays[c], axis=-1)
def _mesh2unstructured_grid(cuds): point2index = {} unstructured_grid = vtk.vtkUnstructuredGrid() unstructured_grid.Allocate() # copy points points = vtk.vtkPoints() point_data = unstructured_grid.GetPointData() data_collector = CUBADataAccumulator(container=point_data) for index, point in enumerate(cuds.iter(item_type=CUBA.POINT)): point2index[point.uid] = index points.InsertNextPoint(*point.coordinates) data_collector.append(point.data) # prepare to copy elements cell_data = unstructured_grid.GetCellData() data_collector = CUBADataAccumulator(container=cell_data) # copy edges mapping = points2edge() for edge in cuds.iter(item_type=CUBA.EDGE): npoints = len(edge.points) ids = vtk.vtkIdList() for uid in edge.points: ids.InsertNextId(point2index[uid]) unstructured_grid.InsertNextCell(mapping[npoints], ids) data_collector.append(edge.data) # copy faces mapping = points2face() for face in cuds.iter(item_type=CUBA.FACE): npoints = len(face.points) ids = vtk.vtkIdList() for uid in face.points: ids.InsertNextId(point2index[uid]) unstructured_grid.InsertNextCell(mapping[npoints], ids) data_collector.append(face.data) # copy cells mapping = points2cell() for cell in cuds.iter(item_type=CUBA.CELL): npoints = len(cell.points) ids = vtk.vtkIdList() for uid in cell.points: ids.InsertNextId(point2index[uid]) unstructured_grid.InsertNextCell(mapping[npoints], ids) data_collector.append(cell.data) unstructured_grid.SetPoints(points) return unstructured_grid