def _cell_scalar(self, name=None): """ Returns the cell scalars of a vtk object Parameters ---------- name : str Name of cell scalars to retrive. Returns ------- scalars : np.ndarray Numpy array of scalars """ if name is None: # use active scalar array field, name = self.active_scalar_info if field != CELL_DATA_FIELD: raise RuntimeError('Must specify an array to fetch.') vtkarr = self.GetCellData().GetAbstractArray(name) if vtkarr is None: raise AssertionError('({}) is not a cell scalar'.format(name)) # numpy does not support bit array data types if isinstance(vtkarr, vtk.vtkBitArray): vtkarr = vtk_bit_array_to_char(vtkarr) if name not in self._cell_bool_array_names: self._cell_bool_array_names.append(name) array = convert_array(vtkarr) if array.dtype == np.uint8 and name in self._cell_bool_array_names: array = array.view(np.bool) return array
def _field_scalar(self, name=None): """ Returns field scalars of a vtk object Parameters ---------- name : str Name of field scalars to retrive. Returns ------- scalars : np.ndarray Numpy array of scalars """ if name is None: raise RuntimeError('Must specify an array to fetch.') vtkarr = self.GetFieldData().GetAbstractArray(name) if vtkarr is None: raise AssertionError( '({}) is not a valid field scalar array'.format(name)) # numpy does not support bit array data types if isinstance(vtkarr, vtk.vtkBitArray): vtkarr = vtk_bit_array_to_char(vtkarr) if name not in self._point_bool_array_names: self._field_bool_array_names.append(name) array = convert_array(vtkarr) if array.dtype == np.uint8 and name in self._field_bool_array_names: array = array.view(np.bool) return array
def _row_array(self, name=None): """Return row scalars of a vtk object. Parameters ---------- name : str Name of row scalars to retrieve. Return ------ scalars : np.ndarray Numpy array of scalars """ if name is None: # use first array name = self.GetRowData().GetArrayName(0) if name is None: raise RuntimeError('No arrays present to fetch.') vtkarr = self.GetRowData().GetAbstractArray(name) if vtkarr is None: raise AssertionError('({}) is not a row scalar'.format(name)) # numpy does not support bit array data types if isinstance(vtkarr, vtk.vtkBitArray): vtkarr = vtk_bit_array_to_char(vtkarr) if name not in self._row_bool_array_names: self._row_bool_array_names.append(name) array = convert_array(vtkarr) if array.dtype == np.uint8 and name in self._row_bool_array_names: array = array.view(np.bool) return array
def _add_point_scalar(self, scalars, name, set_active=False, deep=True): """ Adds point scalars to the mesh Parameters ---------- scalars : numpy.ndarray Numpy array of scalars. Must match number of points. name : str Name of point scalars to add. set_active : bool, optional Sets the scalars to the active plotting scalars. Default False. deep : bool, optional Does not copy scalars when False. A reference to the scalars must be kept to avoid a segfault. """ if scalars is None: raise TypeError('Empty array unable to be added') if not isinstance(scalars, np.ndarray): scalars = np.array(scalars) if scalars.shape[0] != self.n_points: raise Exception('Number of scalars must match the number of ' + 'points') # need to track which arrays are boolean as all boolean arrays # must be stored as uint8 if scalars.dtype == np.bool: scalars = scalars.view(np.uint8) if name not in self._point_bool_array_names: self._point_bool_array_names.append(name) if not scalars.flags.c_contiguous: scalars = np.ascontiguousarray(scalars) vtkarr = convert_array(scalars, deep=deep) vtkarr.SetName(name) self.GetPointData().AddArray(vtkarr) if set_active or self.active_scalar_info[1] is None: self.GetPointData().SetActiveScalars(name) self._active_scalar_info = [POINT_DATA_FIELD, name]
def _add_cell_scalar(self, scalars, name, set_active=False, deep=True): """ Adds cell scalars to the vtk object. Parameters ---------- scalars : numpy.ndarray Numpy array of scalars. Must match number of points. name : str Name of point scalars to add. set_active : bool, optional Sets the scalars to the active plotting scalars. Default False. deep : bool, optional Does not copy scalars when False. A reference to the scalars must be kept to avoid a segfault. """ if scalars is None: raise TypeError('Empty array unable to be added') if not isinstance(scalars, np.ndarray): scalars = np.array(scalars) if scalars.shape[0] != self.n_cells: raise Exception( 'Number of scalars must match the number of cells (%d)' % self.n_cells) if not scalars.flags.c_contiguous: raise AssertionError('Array must be contigious') if scalars.dtype == np.bool: scalars = scalars.view(np.uint8) self._cell_bool_array_names.append(name) vtkarr = convert_array(scalars, deep=deep) vtkarr.SetName(name) self.GetCellData().AddArray(vtkarr) if set_active or self.active_scalar_info[1] is None: self.GetCellData().SetActiveScalars(name) self._active_scalar_info = [CELL_DATA_FIELD, name]
def _add_row_array(self, scalars, name, deep=True): """ Adds scalars to the vtk object. Parameters ---------- scalars : numpy.ndarray Numpy array of scalars. Must match number of points. name : str Name of point scalars to add. deep : bool, optional Does not copy scalars when False. A reference to the scalars must be kept to avoid a segfault. """ if scalars is None: raise TypeError('Empty array unable to be added') if not isinstance(scalars, np.ndarray): scalars = np.array(scalars) if self.n_rows == 0 or self.n_columns == 0: self.n_rows = scalars.shape[0] elif scalars.shape[0] != self.n_rows: raise Exception( 'Number of scalars must match the number of rows (%d)' % self.n_rows) if not scalars.flags.c_contiguous: scalars = np.ascontiguousarray(scalars) if scalars.dtype == np.bool: scalars = scalars.view(np.uint8) self._row_bool_array_names.append(name) vtkarr = convert_array(scalars, deep=deep) vtkarr.SetName(name) self.AddColumn(vtkarr)
def _add_field_scalar(self, scalars, name, deep=True): """ Adds field scalars to the mesh Parameters ---------- scalars : numpy.ndarray Numpy array of scalars. Does not have to match number of points or numbers of cells. name : str Name of field scalars to add. deep : bool, optional Does not copy scalars when False. A reference to the scalars must be kept to avoid a segfault. """ if scalars is None: raise TypeError('Empty array unable to be added') if not isinstance(scalars, np.ndarray): scalars = np.array(scalars) # need to track which arrays are boolean as all boolean arrays # must be stored as uint8 if scalars.dtype == np.bool: scalars = scalars.view(np.uint8) if name not in self._field_bool_array_names: self._field_bool_array_names.append(name) if not scalars.flags.c_contiguous: scalars = np.ascontiguousarray(scalars) vtkarr = convert_array(scalars, deep=deep) vtkarr.SetName(name) self.GetFieldData().AddArray(vtkarr)