All files / Sources/Common/DataModel/CellTypes index.js

50% Statements 28/56
15.78% Branches 3/19
33.33% Functions 6/18
50.98% Lines 26/51

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211                                                                                      5x                     1x                         7x         7x                     7x                       7x                 7x 7x   7x 7x   7x           420x         7x             7x         7x                           7x         627x                   7x               7x                       1x                     7x   7x   7x 7x   7x         1x          
import macro from 'vtk.js/Sources/macros';
import {
  CellType,
  CellTypesStrings,
} from 'vtk.js/Sources/Common/DataModel/CellTypes/Constants';
 
// ----------------------------------------------------------------------------
// Global methods
// ----------------------------------------------------------------------------
 
/**
 * Given an int (as defined in vtkCellType.h) identifier for a class
 * return it's classname.
 */
function getClassNameFromTypeId(typeId) {
  return typeId < CellTypesStrings.length
    ? CellTypesStrings[typeId]
    : 'UnknownClass';
}
 
/**
 * Given a data object classname, return it's int identified (as
 * defined in vtkCellType.h)
 */
function getTypeIdFromClassName(cellTypeString) {
  return CellTypesStrings.findIndex(cellTypeString);
}
 
/**
 * This convenience method is a fast check to determine if a cell type
 * represents a linear or nonlinear cell.  This is generally much more
 * efficient than getting the appropriate vtkCell and checking its IsLinear
 * method.
 */
function isLinear(type) {
  return (
    type < CellType.VTK_QUADRATIC_EDGE ||
    type === CellType.VTK_CONVEX_POINT_SET ||
    type === CellType.VTK_POLYHEDRON
  );
}
 
function hasSubCells(cellType) {
  return (
    cellType === CellType.VTK_TRIANGLE_STRIP ||
    cellType === CellType.VTK_POLY_LINE ||
    cellType === CellType.VTK_POLY_VERTEX
  );
}
 
// ----------------------------------------------------------------------------
// Static API
// ----------------------------------------------------------------------------
 
export const STATIC = {
  getClassNameFromTypeId,
  getTypeIdFromClassName,
  isLinear,
  hasSubCells,
};
 
// ----------------------------------------------------------------------------
// vtkCellTypes methods
// ----------------------------------------------------------------------------
 
function vtkCellTypes(publicAPI, model) {
  // Set our className
  model.classHierarchy.push('vtkCellTypes');
 
  /**
   * Allocate memory for this array. Delete old storage only if necessary.
   */
  publicAPI.allocate = (sz = 512, ext = 1000) => {
    model.size = sz > 0 ? sz : 1;
    model.extend = ext > 0 ? ext : 1;
    model.maxId = -1;
    model.typeArray = new Uint8Array(sz);
    model.locationArray = new Uint32Array(sz);
  };
 
  /**
   * Add a cell at specified id.
   */
  publicAPI.insertCell = (cellId, type, loc) => {
    model.typeArray[cellId] = type;
    model.locationArray[cellId] = loc;
 
    if (cellId > model.maxId) {
      model.maxId = cellId;
    }
  };
 
  /**
   * Add a cell to the object in the next available slot.
   */
  publicAPI.insertNextCell = (type, loc) => {
    publicAPI.insertCell(++model.maxId, type, loc);
    return model.maxId;
  };
 
  /**
   * Specify a group of cell types. This version is provided to maintain
   * backwards compatibility and does a copy of the cellLocations
   */
  publicAPI.setCellTypes = (ncells, cellTypes, cellLocations) => {
    model.size = ncells;
 
    model.typeArray = cellTypes;
    model.locationArray = cellLocations;
 
    model.maxId = ncells - 1;
  };
 
  /**
   * Return the location of the cell in the associated vtkCellArray.
   */
  publicAPI.getCellLocation = (cellId) => model.locationArray[cellId];
 
  /**
   * Delete cell by setting to nullptr cell type.
   */
  publicAPI.deleteCell = (cellId) => {
    model.typeArray[cellId] = CellType.VTK_EMPTY_CELL;
  };
 
  /**
   * Return the number of types in the list.
   */
  publicAPI.getNumberOfTypes = () => model.maxId + 1;
 
  /**
   * Return true if type specified is contained in list; false otherwise.
   */
  publicAPI.isType = (type) => {
    const numTypes = publicAPI.getNumberOfTypes();
 
    for (let i = 0; i < numTypes; ++i) {
      if (type === publicAPI.getCellType(i)) {
        return true;
      }
    }
    return false;
  };
 
  /**
   * Add the type specified to the end of the list. Range checking is performed.
   */
  publicAPI.insertNextType = (type) => publicAPI.insertNextCell(type, -1);
 
  /**
   * Return the type of cell.
   */
  publicAPI.getCellType = (cellId) => model.typeArray[cellId];
 
  /**
   * Reclaim any extra memory.
   */
  // TODO: publicAPI.squeeze = () =>  {};
 
  /**
   * Initialize object without releasing memory.
   */
  publicAPI.reset = () => {
    model.maxId = -1;
  };
 
  /**
   * Standard DeepCopy method.  Since this object contains no reference
   * to other objects, there is no ShallowCopy.
   */
  publicAPI.deepCopy = (src) => {
    publicAPI.allocate(src.getSize(), src.getExtend());
    model.typeArray.set(src.getTypeArray());
    model.locationArray.set(src.getLocationArray());
    model.maxId = src.getMaxId();
  };
}
 
// ----------------------------------------------------------------------------
// Object factory
// ----------------------------------------------------------------------------
 
const DEFAULT_VALUES = {
  // typeArray: null, // pointer to types array
  // locationArray: null;   // pointer to array of offsets
  size: 0, // allocated size of data
  maxId: -1, // maximum index inserted thus far
  extend: 1000, // grow array by this point
};
 
// ----------------------------------------------------------------------------
 
export function extend(publicAPI, model, initialValues = {}) {
  Object.assign(model, DEFAULT_VALUES, initialValues);
 
  macro.obj(publicAPI, model);
 
  macro.get(publicAPI, model, ['size', 'maxId', 'extend']);
  macro.getArray(publicAPI, model, ['typeArray', 'locationArray']);
 
  vtkCellTypes(publicAPI, model);
}
 
// ----------------------------------------------------------------------------
 
export const newInstance = macro.newInstance(extend, 'vtkCellTypes');
 
// ----------------------------------------------------------------------------
 
export default { newInstance, extend, ...STATIC };