All files / Sources/Filters/Core/PolyDataNormals index.js

94.52% Statements 69/73
52.94% Branches 9/17
100% Functions 5/5
94.2% Lines 65/69

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                          4x   4x         3x       3x 3x 3x   3x 3x   3x 3x   3x 15x   15x       15x 45x     15x             15x 15x 15x   15x 15x 57x   57x 57x 57x             3x 3x 3x 55x 55x 55x   55x   55x 55x 55x       3x     4x 3x   3x       3x   3x       3x   3x 3x 3x 3x 3x   3x 3x 3x   3x           3x 3x         3x     3x 1x         1x     3x               4x                 4x       4x       4x   4x       4x         1x          
import macro from 'vtk.js/Sources/macros';
 
import vtkDataArray from 'vtk.js/Sources/Common/Core/DataArray';
import vtkMath from 'vtk.js/Sources/Common/Core/Math/index';
import vtkPolyData from 'vtk.js/Sources/Common/DataModel/PolyData';
import vtkTriangle from 'vtk.js/Sources/Common/DataModel/Triangle';
 
// ----------------------------------------------------------------------------
// vtkPolyDataNormals methods
// ----------------------------------------------------------------------------
 
function vtkPolyDataNormals(publicAPI, model) {
  // Set our className
  model.classHierarchy.push('vtkPolyDataNormals');
 
  publicAPI.vtkPolyDataNormalsExecute = (
    numberOfPolys,
    polysData,
    pointsData
  ) => {
    Iif (!pointsData) {
      return null;
    }
 
    const pointNormals = new Float32Array(pointsData.length);
    const cellNormals = new Float32Array(3 * numberOfPolys);
    let cellNormalComponent = 0;
 
    let numberOfPoints = 0;
    const polysDataLength = polysData.length;
 
    const cellPointIds = [0, 0, 0];
    const cellNormal = [0, 0, 0];
 
    for (let c = 0; c < polysDataLength; c += numberOfPoints + 1) {
      numberOfPoints = polysData[c];
 
      Iif (numberOfPoints < 3) {
        continue; // eslint-disable-line
      }
 
      for (let i = 1; i <= 3; ++i) {
        cellPointIds[i - 1] = 3 * polysData[c + i];
      }
 
      vtkTriangle.computeNormal(
        pointsData.slice(cellPointIds[0], cellPointIds[0] + 3),
        pointsData.slice(cellPointIds[1], cellPointIds[1] + 3),
        pointsData.slice(cellPointIds[2], cellPointIds[2] + 3),
        cellNormal
      );
 
      cellNormals[cellNormalComponent++] = cellNormal[0];
      cellNormals[cellNormalComponent++] = cellNormal[1];
      cellNormals[cellNormalComponent++] = cellNormal[2];
 
      if (model.computePointNormals) {
        for (let i = 1; i <= numberOfPoints; ++i) {
          let pointId = 3 * polysData[c + i];
 
          pointNormals[pointId] += cellNormal[0];
          pointNormals[++pointId] += cellNormal[1];
          pointNormals[++pointId] += cellNormal[2];
        }
      }
    }
 
    // Normalize point normals.
    // A point normal is the sum of all the cell normals the point belongs to
    if (model.computePointNormals) {
      const pointNormal = [0, 0, 0];
      for (let i = 0; i < pointsData.length; ) {
        pointNormal[0] = pointNormals[i];
        pointNormal[1] = pointNormals[i + 1];
        pointNormal[2] = pointNormals[i + 2];
 
        vtkMath.normalize(pointNormal);
 
        pointNormals[i++] = pointNormal[0];
        pointNormals[i++] = pointNormal[1];
        pointNormals[i++] = pointNormal[2];
      }
    }
 
    return [cellNormals, pointNormals];
  };
 
  publicAPI.requestData = (inData, outData) => {
    const numberOfInputs = publicAPI.getNumberOfInputPorts();
 
    Iif (!numberOfInputs) {
      return;
    }
 
    const input = inData[0];
 
    Iif (!input) {
      return;
    }
 
    const output = vtkPolyData.newInstance();
 
    output.setPoints(input.getPoints());
    output.setVerts(input.getVerts());
    output.setLines(input.getLines());
    output.setPolys(input.getPolys());
    output.setStrips(input.getStrips());
 
    output.getPointData().passData(input.getPointData());
    output.getCellData().passData(input.getCellData());
    output.getFieldData().passData(input.getFieldData());
 
    const [cellNormals, pointNormals] = publicAPI.vtkPolyDataNormalsExecute(
      input.getNumberOfPolys(),
      input.getPolys().getData(),
      input.getPoints().getData()
    );
 
    if (model.computePointNormals) {
      const outputPointNormals = vtkDataArray.newInstance({
        numberOfComponents: 3,
        name: 'Normals',
        values: pointNormals,
      });
      output.getPointData().setNormals(outputPointNormals);
    }
 
    if (model.computeCellNormals) {
      const outputCellNormals = vtkDataArray.newInstance({
        numberOfComponents: 3,
        name: 'Normals',
        values: cellNormals,
      });
      output.getCellData().setNormals(outputCellNormals);
    }
 
    outData[0] = output;
  };
}
 
// ----------------------------------------------------------------------------
// Object factory
// ----------------------------------------------------------------------------
function defaultValues(initialValues) {
  return {
    computeCellNormals: false,
    computePointNormals: true,
    ...initialValues,
  };
}
 
// ----------------------------------------------------------------------------
export function extend(publicAPI, model, initialValues = {}) {
  Object.assign(model, defaultValues(initialValues));
 
  /* Make this a VTK object */
 
  macro.obj(publicAPI, model);
 
  /* Also make it an algorithm with one input and one output */
 
  macro.algo(publicAPI, model, 1, 1);
 
  macro.setGet(publicAPI, model, ['computeCellNormals', 'computePointNormals']);
 
  /* Object specific methods */
 
  vtkPolyDataNormals(publicAPI, model);
}
 
// ----------------------------------------------------------------------------
 
export const newInstance = macro.newInstance(extend, 'vtkPolyDataNormals');
 
// ----------------------------------------------------------------------------
 
export default { newInstance, extend };