All files / Sources/Filters/Sources/LineSource index.js

92.85% Statements 39/42
42.85% Branches 3/7
100% Functions 3/3
92.5% Lines 37/40

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        1x               5x   5x 4x       4x     4x     4x 4x 4x 4x           4x 4x     4x 4x     4x 4x   4x 4x 4x 39x   39x 39x 39x   39x         4x 4x 4x 39x       4x               1x                   5x     5x 5x 5x 5x 5x         1x          
import macro from 'vtk.js/Sources/macros';
import vtkPolyData from 'vtk.js/Sources/Common/DataModel/PolyData';
import * as vtkMath from 'vtk.js/Sources/Common/Core/Math';
 
const { vtkWarningMacro } = macro;
 
// ----------------------------------------------------------------------------
// vtkLineSource methods
// ----------------------------------------------------------------------------
 
function vtkLineSource(publicAPI, model) {
  // Set our className
  model.classHierarchy.push('vtkLineSource');
 
  publicAPI.requestData = (inData, outData) => {
    Iif (model.deleted) {
      return;
    }
 
    const dataset = outData[0];
 
    // Check input
    const pointDataType = dataset
      ? dataset.getPoints().getDataType()
      : model.pointType;
    const pd = vtkPolyData.newInstance();
    const v21 = [];
    vtkMath.subtract(model.point2, model.point1, v21);
    Iif (vtkMath.norm(v21) <= 0.0) {
      vtkWarningMacro('Zero-length line definition');
      return;
    }
 
    // hand create a line with special scalars
    const res = model.resolution;
    const numPts = res + 1;
 
    // Points
    const points = macro.newTypedArray(pointDataType, numPts * 3);
    pd.getPoints().setData(points, 3);
 
    // Cells
    const lines = new Uint32Array(numPts + 1);
    pd.getLines().setData(lines, 1);
 
    let idx = 0;
    let t = 0.0;
    for (let i = 0; i < res + 1; i++) {
      t = i / res;
 
      points[idx * 3] = model.point1[0] + t * v21[0];
      points[idx * 3 + 1] = model.point1[1] + t * v21[1];
      points[idx * 3 + 2] = model.point1[2] + t * v21[2];
 
      idx++;
    }
 
    // Generate line connectivity
    //
    idx = 0;
    lines[0] = numPts;
    for (let i = 0; i < numPts; i++) {
      lines[i + 1] = i;
    }
 
    // Update output
    outData[0] = pd;
  };
}
 
// ----------------------------------------------------------------------------
// Object factory
// ----------------------------------------------------------------------------
 
const DEFAULT_VALUES = {
  resolution: 10,
  point1: [-1, 0, 0],
  point2: [1, 0, 0],
  pointType: 'Float64Array',
};
 
// ----------------------------------------------------------------------------
 
export function extend(publicAPI, model, initialValues = {}) {
  Object.assign(model, DEFAULT_VALUES, initialValues);
 
  // Build VTK API
  macro.obj(publicAPI, model);
  macro.setGet(publicAPI, model, ['resolution']);
  macro.setGetArray(publicAPI, model, ['point1', 'point2'], 3);
  macro.algo(publicAPI, model, 0, 1);
  vtkLineSource(publicAPI, model);
}
 
// ----------------------------------------------------------------------------
 
export const newInstance = macro.newInstance(extend, 'vtkLineSource');
 
// ----------------------------------------------------------------------------
 
export default { newInstance, extend };