def numpy_to_vtk_cells(data, is_coords=True): """Convert numpy array to a vtk cell array. Parameters ---------- data : ndarray points coordinate or connectivity array (e.g triangles). is_coords : ndarray Select the type of array. default: True. Returns ------- vtk_cell : vtkCellArray connectivity + offset information """ data = np.array(data, dtype=object) nb_cells = len(data) # Get lines_array in vtk input format connectivity = data.flatten() if not is_coords else [] offset = [0, ] current_position = 0 cell_array = CellArray() if VTK_9_PLUS: for i in range(nb_cells): current_len = len(data[i]) offset.append(offset[-1] + current_len) if is_coords: end_position = current_position + current_len connectivity += list(range(current_position, end_position)) current_position = end_position connectivity = np.array(connectivity, np.intp) offset = np.array(offset, dtype=connectivity.dtype) vtk_array_type = numpy_support.get_vtk_array_type(connectivity.dtype) cell_array.SetData( numpy_support.numpy_to_vtk(offset, deep=True, array_type=vtk_array_type), numpy_support.numpy_to_vtk(connectivity, deep=True, array_type=vtk_array_type)) else: for i in range(nb_cells): current_len = len(data[i]) end_position = current_position + current_len connectivity += [current_len] connectivity += list(range(current_position, end_position)) current_position = end_position connectivity = np.array(connectivity) cell_array.GetData().DeepCopy(numpy_support.numpy_to_vtk(connectivity)) cell_array.SetNumberOfCells(nb_cells) return cell_array
def _points_to_vtk_cells(points, points_per_line=2): """ Returns the VTK cell array for the peaks given the set of points coordinates. Parameters ---------- points : (N, 3) array or ndarray points coordinates array. points_per_line : int (1 or 2), optional number of points per peak direction. Returns ------- cell_array : vtkCellArray connectivity + offset information. """ num_pnts = len(points) num_cells = num_pnts // points_per_line cell_array = CellArray() """ Connectivity is an array that contains the indices of the points that need to be connected in the visualization. The indices start from 0. """ connectivity = np.asarray(list(range(0, num_pnts)), dtype=int) """ Offset is an array that contains the indices of the first point of each line. The indices start from 0 and given the known geometry of this actor the creation of this array requires a 2 points padding between indices. """ offset = np.asarray(list(range(0, num_pnts + 1, points_per_line)), dtype=int) vtk_array_type = numpy_support.get_vtk_array_type(connectivity.dtype) cell_array.SetData( numpy_support.numpy_to_vtk(offset, deep=True, array_type=vtk_array_type), numpy_support.numpy_to_vtk(connectivity, deep=True, array_type=vtk_array_type)) cell_array.SetNumberOfCells(num_cells) return cell_array
def _points_to_vtk_cells(points, points_per_line=2): """ Returns the VTK cell array for the peaks given the set of points coordinates. Parameters ---------- points : (N, 3) array or ndarray points coordinates array. points_per_line : int (1 or 2), optional number of points per peak direction. Returns ------- cell_array : vtkCellArray connectivity + offset information. """ num_pnts = len(points) num_cells = num_pnts // points_per_line cell_array = CellArray() if VTK_9_PLUS: """ Connectivity is an array that contains the indices of the points that need to be connected in the visualization. The indices start from 0. """ connectivity = np.asarray(list(range(0, num_pnts)), dtype=int) """ Offset is an array that contains the indices of the first point of each line. The indices start from 0 and given the known geometry of this actor the creation of this array requires a 2 points padding between indices. """ offset = np.asarray(list(range(0, num_pnts + 1, points_per_line)), dtype=int) vtk_array_type = numpy_support.get_vtk_array_type(connectivity.dtype) cell_array.SetData( numpy_support.numpy_to_vtk(offset, deep=True, array_type=vtk_array_type), numpy_support.numpy_to_vtk(connectivity, deep=True, array_type=vtk_array_type)) else: connectivity = np.array([], dtype=int) i_pos = 0 while i_pos < num_pnts: e_pos = i_pos + points_per_line """ In old versions of VTK (<9.0) the connectivity array should include the length of each line and immediately after the indices of the points in each line. """ connectivity = np.append(connectivity, [points_per_line, i_pos, e_pos - 1]) i_pos = e_pos cell_array.GetData().DeepCopy(numpy_support.numpy_to_vtk(connectivity)) cell_array.SetNumberOfCells(num_cells) return cell_array