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

57.47% Statements 50/87
33.33% Branches 2/6
37.03% Functions 10/27
59.49% Lines 47/79

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 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264            1x                                                 3x           3x 3x 3x     3x     3x   3x 54x 54x 761x         3x 3x   3x 54x 54x 761x                                                                                         3x 3x   608x       3x 3x     3x             3x         3x         3x           3x                   3x             3x                   3x                       3x               3x             3x             3x               3x                 3x 761x     3x 3x 608x             3x 761x               1x                 3x   3x   3x         1x          
import macro from 'vtk.js/Sources/macros';
import vtkCell from 'vtk.js/Sources/Common/DataModel/Cell';
 
// ----------------------------------------------------------------------------
// Global methods
// ----------------------------------------------------------------------------
export const InitLink = {
  ncells: 0,
  cells: null,
};
 
function resize(model, sz) {
  let newSize = sz;
  if (sz >= model.array.length) {
    newSize += model.array.length;
  }
 
  while (newSize > model.array.length)
    model.array.push({
      ncells: 0,
      cells: null,
    });
  model.array.length = newSize;
}
 
// ----------------------------------------------------------------------------
// vtkCellLinks methods
// ----------------------------------------------------------------------------
 
function vtkCellLinks(publicAPI, model) {
  // Set our className
  model.classHierarchy.push('vtkCellLinks');
 
  /**
   * Build the link list array. All subclasses of vtkAbstractCellLinks
   * must support this method.
   */
  publicAPI.buildLinks = (data) => {
    const numPts = data.getPoints().getNumberOfPoints();
    const numCells = data.getNumberOfCells();
 
    // fill out lists with number of references to cells
    const linkLoc = new Uint32Array(numPts);
 
    // Use fast path if polydata
    if (data.isA('vtkPolyData')) {
      // traverse data to determine number of uses of each point
      for (let cellId = 0; cellId < numCells; ++cellId) {
        const { cellPointIds } = data.getCellPoints(cellId);
        cellPointIds.forEach((cellPointId) => {
          publicAPI.incrementLinkCount(cellPointId);
        });
      }
 
      // now allocate storage for the links
      publicAPI.allocateLinks(numPts);
      model.maxId = numPts - 1;
 
      for (let cellId = 0; cellId < numCells; ++cellId) {
        const { cellPointIds } = data.getCellPoints(cellId);
        cellPointIds.forEach((cellPointId) => {
          publicAPI.insertCellReference(
            cellPointId,
            linkLoc[cellPointId]++,
            cellId
          );
        });
      }
    } // any other type of dataset
    else E{
      // traverse data to determine number of uses of each point
      for (let cellId = 0; cellId < numCells; cellId++) {
        // TODO: Currently not supported: const cell = data.getCell(cellId);
        const cell = vtkCell.newInstance();
        cell.getPointsIds().forEach((cellPointId) => {
          publicAPI.incrementLinkCount(cellPointId);
        });
      }
 
      // now allocate storage for the links
      publicAPI.allocateLinks(numPts);
      model.maxId = numPts - 1;
 
      for (let cellId = 0; cellId < numCells; ++cellId) {
        // TODO: Currently not supported: const cell = data.getCell(cellId);
        const cell = vtkCell.newInstance();
        cell.getPointsIds().forEach((cellPointId) => {
          publicAPI.insertCellReference(
            cellPointId,
            linkLoc[cellPointId]++,
            cellId
          );
        });
      }
    } // end else
  };
 
  /**
   * Build the link list array with a provided connectivity array.
   */
  // publicAPI.buildLinks = (data, connectivity) => {};
 
  /**
   * Allocate the specified number of links (i.e., number of points) that
   * will be built.
   */
  publicAPI.allocate = (numLinks, ext = 1000) => {
    model.array = Array(numLinks)
      .fill()
      .map(() => ({
        ncells: 0,
        cells: null,
      }));
    model.extend = ext;
    model.maxId = -1;
  };
 
  publicAPI.initialize = () => {
    model.array = null;
  };
 
  /**
   * Get a link structure given a point id.
   */
  publicAPI.getLink = (ptId) => model.array[ptId];
 
  /**
   * Get the number of cells using the point specified by ptId.
   */
  publicAPI.getNcells = (ptId) => model.array[ptId].ncells;
 
  /**
   * Return a list of cell ids using the point.
   */
  publicAPI.getCells = (ptId) => model.array[ptId].cells;
 
  /**
   * Insert a new point into the cell-links data structure. The size parameter
   * is the initial size of the list.
   */
  publicAPI.insertNextPoint = (numLinks) => {
    model.array.push({ ncells: numLinks, cells: Array(numLinks) });
    ++model.maxId;
  };
 
  /**
   * Insert a cell id into the list of cells (at the end) using the cell id
   * provided. (Make sure to extend the link list (if necessary) using the
   * method resizeCellList().)
   */
  publicAPI.insertNextCellReference = (ptId, cellId) => {
    model.array[ptId].cells[model.array[ptId].ncells++] = cellId;
  };
 
  /**
   * Delete point (and storage) by destroying links to using cells.
   */
  publicAPI.deletePoint = (ptId) => {
    model.array[ptId].ncells = 0;
    model.array[ptId].cells = null;
  };
 
  /**
   * Delete the reference to the cell (cellId) from the point (ptId). This
   * removes the reference to the cellId from the cell list, but does not
   * resize the list (recover memory with resizeCellList(), if necessary).
   */
  publicAPI.removeCellReference = (cellId, ptId) => {
    model.array[ptId].cells = model.array[ptId].cells.filter(
      (cell) => cell !== cellId
    );
    model.array[ptId].ncells = model.array[ptId].cells.length;
  };
 
  /**
   * Add the reference to the cell (cellId) from the point (ptId). This
   * adds a reference to the cellId from the cell list, but does not resize
   * the list (extend memory with resizeCellList(), if necessary).
   */
  publicAPI.addCellReference = (cellId, ptId) => {
    model.array[ptId].cells[model.array[ptId].ncells++] = cellId;
  };
 
  /**
   * Change the length of a point's link list (i.e., list of cells using a
   * point) by the size specified.
   */
  publicAPI.resizeCellList = (ptId, size) => {
    model.array[ptId].cells.length = size;
  };
 
  /**
   * Reclaim any unused memory.
   */
  publicAPI.squeeze = () => {
    resize(model, model.maxId + 1);
  };
 
  /**
   * Reset to a state of no entries without freeing the 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) => {
    model.array = [...src.array];
    model.extend = src.extend;
    model.maxId = src.maxId;
  };
 
  /**
   * Increment the count of the number of cells using the point.
   */
  publicAPI.incrementLinkCount = (ptId) => {
    ++model.array[ptId].ncells;
  };
 
  publicAPI.allocateLinks = (n) => {
    for (let i = 0; i < n; ++i) {
      model.array[i].cells = new Array(model.array[i].ncells);
    }
  };
 
  /**
   * Insert a cell id into the list of cells using the point.
   */
  publicAPI.insertCellReference = (ptId, pos, cellId) => {
    model.array[ptId].cells[pos] = cellId;
  };
}
 
// ----------------------------------------------------------------------------
// Object factory
// ----------------------------------------------------------------------------
 
const DEFAULT_VALUES = {
  array: null, // pointer to data
  maxId: 0, // maximum index inserted thus far
  extend: 0, // grow array by this point
};
 
// ----------------------------------------------------------------------------
 
export function extend(publicAPI, model, initialValues = {}) {
  Object.assign(model, DEFAULT_VALUES, initialValues);
 
  macro.obj(publicAPI, model);
 
  vtkCellLinks(publicAPI, model);
}
 
// ----------------------------------------------------------------------------
 
export const newInstance = macro.newInstance(extend, 'vtkCellLinks');
 
// ----------------------------------------------------------------------------
 
export default { newInstance, extend };