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

97.14% Statements 34/35
40% Branches 2/5
100% Functions 3/3
96.96% Lines 32/33

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                    2x   2x 1x       1x     1x     1x     1x     1x 1x     1x 1x             1x 125x 125x 125x 125x 125x 125x 125x 125x         1x 1x 125x       1x               1x                   2x     2x 2x 2x 2x 2x         1x          
import macro from 'vtk.js/Sources/macros';
import * as vtkMath from 'vtk.js/Sources/Common/Core/Math';
import vtkPolyData from 'vtk.js/Sources/Common/DataModel/PolyData';
 
// ----------------------------------------------------------------------------
// vtkPointSource methods
// ----------------------------------------------------------------------------
 
function vtkPointSource(publicAPI, model) {
  // Set our className
  model.classHierarchy.push('vtkPointSource');
 
  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();
 
    // hand create a point cloud
    const numPts = model.numberOfPoints;
 
    // Points
    const points = macro.newTypedArray(pointDataType, numPts * 3);
    pd.getPoints().setData(points, 3);
 
    // Cells
    const verts = new Uint32Array(numPts + 1);
    pd.getVerts().setData(verts, 1);
 
    let cosphi;
    let sinphi;
    let rho;
    let radius;
    let theta;
    for (let i = 0; i < numPts; i++) {
      cosphi = 1 - 2.0 * vtkMath.random();
      sinphi = Math.sqrt(1 - cosphi * cosphi);
      rho = model.radius * vtkMath.random() ** 0.33333333;
      radius = rho * sinphi;
      theta = 2.0 * Math.PI * vtkMath.random();
      points[i * 3] = model.center[0] + radius * Math.cos(theta);
      points[i * 3 + 1] = model.center[1] + radius * Math.sin(theta);
      points[i * 3 + 2] = model.center[2] + rho * cosphi;
    }
 
    // Generate point connectivity
    //
    verts[0] = numPts;
    for (let i = 0; i < numPts; i++) {
      verts[i + 1] = i;
    }
 
    // Update output
    outData[0] = pd;
  };
}
 
// ----------------------------------------------------------------------------
// Object factory
// ----------------------------------------------------------------------------
 
const DEFAULT_VALUES = {
  numberOfPoints: 10,
  center: [0, 0, 0],
  radius: 0.5,
  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, ['numberOfPoints', 'radius']);
  macro.setGetArray(publicAPI, model, ['center'], 3);
  macro.algo(publicAPI, model, 0, 1);
  vtkPointSource(publicAPI, model);
}
 
// ----------------------------------------------------------------------------
 
export const newInstance = macro.newInstance(extend, 'vtkPointSource');
 
// ----------------------------------------------------------------------------
 
export default { newInstance, extend };