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 RequestData(self, request, inInfo, outInfo): logger.debug("Requesting data...") input = self.GetInputDataObject(0, 0) trajectory_data = dsa.WrapDataObject(input) output = dsa.WrapDataObject(vtkPolyData.GetData(outInfo)) # Retrieve current time time = timesteps_util.get_timestep(self, logger=logger) # Retrieve trajectory data trajectory_times = trajectory_data.PointData["Time"] trajectory_points = trajectory_data.Points # Interpolate along the trajectory to find current position current_position = [ np.interp(time, trajectory_times, trajectory_points[:, i]) for i in range(3) ] # Expose to VTK points_vtk = vtk.vtkPoints() verts_vtk = vtk.vtkCellArray() verts_vtk.InsertNextCell(1) points_vtk.InsertPoint(0, *current_position) verts_vtk.InsertCellPoint(0) output.SetPoints(points_vtk) output.SetVerts(verts_vtk) # Interpolate remaining point data along the trajectory for dataset in trajectory_data.PointData.keys(): if dataset == "Time": continue point_data = trajectory_data.PointData[dataset] data_at_position = np.zeros(point_data.shape[1:]) if len(data_at_position.shape) > 0: for i in itertools.product( *map(range, data_at_position.shape)): point_data_i = point_data[(slice(None), ) + i] if len(trajectory_times) == len(point_data_i): data_at_position[i] = np.interp( time, trajectory_times, point_data_i) else: logger.warning( "Unable to interpolate trajectory dataset" f" {dataset}[{i}]: Length of dataset" f" ({len(point_data_i)}) does not match length of" f" trajectory times ({len(trajectory_times)}).") else: data_at_position = np.interp(time, trajectory_times, point_data) data_vtk = vtknp.numpy_to_vtk(np.array([data_at_position])) data_vtk.SetName(dataset) output.GetPointData().AddArray(data_vtk) return 1
def _particles2poly_data(cuds): particle2index = {} points = vtk.vtkPoints() lines = vtk.vtkCellArray() poly_data = vtk.vtkPolyData() point_data = poly_data.GetPointData() data_collector = CUBADataAccumulator(container=point_data) for index, particle in enumerate(cuds.iter(item_type=CUBA.PARTICLE)): particle2index[particle.uid] = index points.InsertPoint(index, *particle.coordinates) data_collector.append(particle.data) cell_data = poly_data.GetCellData() data_collector = CUBADataAccumulator(container=cell_data) for bond in cuds.iter(item_type=CUBA.BOND): lines.InsertNextCell(len(bond.particles)) for uuid in bond.particles: lines.InsertCellPoint(particle2index[uuid]) data_collector.append(bond.data) poly_data.SetPoints(points) poly_data.SetLines(lines) return poly_data