All files / Sources/Filters/General/ScalarToRGBA index.js

23.8% Statements 10/42
0% Branches 0/9
66.66% Functions 2/3
24.39% Lines 10/41

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        1x               1x   1x                                                                                                                           1x         1x     1x     1x   1x     1x         1x          
import macro from 'vtk.js/Sources/macros';
import vtkImageData from 'vtk.js/Sources/Common/DataModel/ImageData';
import vtkDataArray from 'vtk.js/Sources/Common/Core/DataArray';
 
const { vtkErrorMacro } = macro;
 
// ----------------------------------------------------------------------------
// vtkScalarToRGBA methods
// ----------------------------------------------------------------------------
 
function vtkScalarToRGBA(publicAPI, model) {
  // Set our className
  model.classHierarchy.push('vtkScalarToRGBA');
 
  publicAPI.requestData = (inData, outData) => {
    // implement requestData
    const input = inData[0];
 
    if (!input) {
      vtkErrorMacro('Invalid or missing input');
      return;
    }
 
    const scalars = input.getPointData().getScalars();
 
    if (!scalars) {
      vtkErrorMacro('No scalars from input');
      return;
    }
 
    if (!model.lookupTable) {
      vtkErrorMacro('No lookupTable available');
      return;
    }
 
    if (!model.piecewiseFunction) {
      vtkErrorMacro('No piecewiseFunction available');
      return;
    }
 
    const rgba = [0, 0, 0, 0];
    const data = scalars.getData();
    const rgbaArray = new Uint8ClampedArray(data.length * 4);
    let offset = 0;
    for (let idx = 0; idx < data.length; idx++) {
      const x = data[idx];
      model.lookupTable.getColor(x, rgba);
      rgba[3] = model.piecewiseFunction.getValue(x);
      rgbaArray[offset++] = 255 * rgba[0];
      rgbaArray[offset++] = 255 * rgba[1];
      rgbaArray[offset++] = 255 * rgba[2];
      rgbaArray[offset++] = 255 * rgba[3];
    }
 
    const colorArray = vtkDataArray.newInstance({
      name: 'rgba',
      numberOfComponents: 4,
      values: rgbaArray,
    });
    const datasetDefinition = input.get(
      'extent',
      'spacing',
      'origin',
      'direction'
    );
    const output = vtkImageData.newInstance(datasetDefinition);
    output.getPointData().setScalars(colorArray);
 
    outData[0] = output;
  };
}
 
// ----------------------------------------------------------------------------
// Object factory
// ----------------------------------------------------------------------------
 
const DEFAULT_VALUES = {};
 
// ----------------------------------------------------------------------------
 
export function extend(publicAPI, model, initialValues = {}) {
  Object.assign(model, DEFAULT_VALUES, 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, ['lookupTable', 'piecewiseFunction']);
 
  // Object specific methods
  vtkScalarToRGBA(publicAPI, model);
}
 
// ----------------------------------------------------------------------------
 
export const newInstance = macro.newInstance(extend, 'vtkScalarToRGBA');
 
// ----------------------------------------------------------------------------
 
export default { newInstance, extend };