def MakeLegendPoly(): """ Creates a legend polydata object """ pts = np.zeros((4, 3)) vtkpoints = vtkInterface.MakevtkPoints(pts) triangles = np.array([[4, 0, 1, 2, 3]]) vtkcells = vtk.vtkCellArray() vtkcells.SetCells(triangles.shape[0], VN.numpy_to_vtkIdTypeArray(triangles, deep=True)) # Create polydata object mesh = vtk.vtkPolyData() mesh.SetPoints(vtkpoints) mesh.SetPolys(vtkcells) return mesh
def SetNumpyPoints(self, points, deep=True): """ Overwrites existing points with new points. Parameters ---------- points : np.ndarray Points to set to. deep : bool, optional Copies points when True. When False, does not copy array. """ vtkPoints = vtkInterface.MakevtkPoints(points, deep) self.SetPoints(vtkPoints) if not deep: self._point_ref = points
def SetNumpyPoints(self, points, deep=True): """ Overwrites existing points with new points. Parameters ---------- points : np.ndarray Points to set to. deep : bool, optional Copies points when True. When False, does not copy, but a reference to the original points must be kept or else Python will segfault. """ vtkPoints = vtkInterface.MakevtkPoints(points, deep) self.SetPoints(vtkPoints)
def MakeFromArrays(self, x, y, z): """ Create VTK structured grid directly from numpy arrays. Parameters ---------- x : np.ndarray Position of the points in x direction. y : np.ndarray Position of the points in y direction. z : np.ndarray Position of the points in z direction. Example ------- >>> x = np.arange(-10, 10, 0.25) >>> y = np.arange(-10, 10, 0.25) >>> x, y = np.meshgrid(x, y) >>> r = np.sqrt(x**2 + y**2) >>> z = np.sin(r) >>> grid = vtki.StructuredGrid(x, y, z) """ if not (x.shape == y.shape == z.shape): raise Exception('Input point array shapes must match exactly') # make the output points the same precision as the input arrays points = np.empty((x.size, 3), x.dtype) points[:, 0] = x.ravel('F') points[:, 1] = y.ravel('F') points[:, 2] = z.ravel('F') # ensure that the inputs are 3D dim = list(x.shape) while len(dim) < 3: dim.append(1) # Create structured grid self.SetDimensions(dim) self.SetPoints(vtki.MakevtkPoints(points))
def MakeVTKPointsMesh(points): """ Create a PolyData object from a numpy array containing just points """ if points.ndim != 2: points = points.reshape((-1, 3)) npoints = points.shape[0] # Make VTK cells array cells = np.hstack((np.ones( (npoints, 1)), np.arange(npoints).reshape(-1, 1))) cells = np.ascontiguousarray(cells, dtype=np.int64) vtkcells = vtk.vtkCellArray() vtkcells.SetCells(npoints, numpy_to_vtkIdTypeArray(cells, deep=True)) # Convert points to vtk object vtkPoints = vtkInterface.MakevtkPoints(points) # Create polydata pdata = vtkInterface.PolyData() pdata.SetPoints(vtkPoints) pdata.SetVerts(vtkcells) return pdata
def MakeFromArrays(self, offset, cells, cell_type, points, deep=True): """ Create VTK unstructured grid from numpy arrays Parameters ---------- offset : np.ndarray dtype=np.int64 Array indicating the start location of each cell in the cells array. cells : np.ndarray dtype=np.int64 Array of cells. Each cell contains the number of points in the cell and the node numbers of the cell. cell_type : np.uint8 Cell types of each cell. Each cell type numbers can be found from vtk documentation. See example below. points : np.ndarray Numpy array containing point locations. Examples -------- >>> offset = np.array([0, 9]) >>> cells = np.array([8, 0, 1, 2, 3, 4, 5, 6, 7, 8, 8, 9, 10, 11, 12, 13, 14, 15]) >>> cell_type = np.array([vtk.VTK_HEXAHEDRON, vtk.VTK_HEXAHEDRON], np.int8) >>> cell1 = np.array([[0, 0, 0], [1, 0, 0], [1, 1, 0], [0, 1, 0], [0, 0, 1], [1, 0, 1], [1, 1, 1], [0, 1, 1]]) >>> cell2 = np.array([[0, 0, 2], [1, 0, 2], [1, 1, 2], [0, 1, 2], [0, 0, 3], [1, 0, 3], [1, 1, 3], [0, 1, 3]]) >>> points = np.vstack((cell1, cell2)) >>> grid = vtki.UnstructuredGrid(offset, cells, cell_type, points) """ if offset.dtype != vtki.ID_TYPE: offset = offset.astype(np.int64) if cells.dtype != vtki.ID_TYPE: cells = cells.astype(vtki.ID_TYPE) if not cells.flags['C_CONTIGUOUS']: cells = np.ascontiguousarray(cells) if cells.ndim != 1: cells = cells.ravel() if cell_type.dtype != np.uint8: cell_type = cell_type.astype(np.uint8) # Get number of cells ncells = cell_type.size # Convert to vtk arrays cell_type = numpy_to_vtk(cell_type, deep=deep) offset = numpy_to_vtkIdTypeArray(offset, deep=deep) vtkcells = vtk.vtkCellArray() vtkcells.SetCells(ncells, numpy_to_vtkIdTypeArray(cells, deep=deep)) # Convert points to vtkPoints object points = vtki.MakevtkPoints(points, deep=deep) # Create unstructured grid self.SetPoints(points) self.SetCells(cell_type, offset, vtkcells)