All files / core/src/utilities applyPreset.ts

100% Statements 67/67
50% Branches 1/2
100% Functions 8/8
100% Lines 60/60

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                                4x         4x 4x 4x 4x 4x 4x 20x 20x 20x 20x   20x 20x     4x   4x     4x         4x 4x 4x 20x 20x   20x   20x     4x   4x   4x           4x   4x 4x 4x 4x 4x   4x 4x       4x   4x 4x 4x 4x   4x 4x 4x 4x               4x 4x 4x 20x 20x     4x   4x               4x 20x             4x 20x   4x       4x 20x   4x 20x   4x    
import vtkColorTransferFunction from '@kitware/vtk.js/Rendering/Core/ColorTransferFunction';
import vtkPiecewiseFunction from '@kitware/vtk.js/Common/DataModel/PiecewiseFunction';
import type { ViewportPreset } from '../types';
import type { VolumeActor } from '../types/IActor';
 
/**
 * Applies a preset to a volume actor.
 *
 * @param actor - The volume actor to apply the preset to.
 * @param preset - The preset to apply.
 */
export default function applyPreset(
  actor: VolumeActor,
  preset: ViewportPreset
) {
  // Create color transfer function
  const colorTransferArray = preset.colorTransfer
    .split(' ')
    .splice(1)
    .map(parseFloat);
 
  const { shiftRange } = getShiftRange(colorTransferArray);
  const min = shiftRange[0];
  const width = shiftRange[1] - shiftRange[0];
  const cfun = vtkColorTransferFunction.newInstance();
  const normColorTransferValuePoints = [];
  for (let i = 0; i < colorTransferArray.length; i += 4) {
    let value = colorTransferArray[i];
    const r = colorTransferArray[i + 1];
    const g = colorTransferArray[i + 2];
    const b = colorTransferArray[i + 3];
 
    value = (value - min) / width;
    normColorTransferValuePoints.push([value, r, g, b]);
  }
 
  applyPointsToRGBFunction(normColorTransferValuePoints, shiftRange, cfun);
 
  actor.getProperty().setRGBTransferFunction(0, cfun);
 
  // Create scalar opacity function
  const scalarOpacityArray = preset.scalarOpacity
    .split(' ')
    .splice(1)
    .map(parseFloat);
 
  const ofun = vtkPiecewiseFunction.newInstance();
  const normPoints = [];
  for (let i = 0; i < scalarOpacityArray.length; i += 2) {
    let value = scalarOpacityArray[i];
    const opacity = scalarOpacityArray[i + 1];
 
    value = (value - min) / width;
 
    normPoints.push([value, opacity]);
  }
 
  applyPointsToPiecewiseFunction(normPoints, shiftRange, ofun);
 
  const property = actor.getProperty();
 
  property.setScalarOpacity(0, ofun);
  const [
    gradientMinValue,
    gradientMinOpacity,
    gradientMaxValue,
    gradientMaxOpacity,
  ] = preset.gradientOpacity.split(' ').splice(1).map(parseFloat);
 
  property.setUseGradientOpacity(0, true);
  property.setGradientOpacityMinimumValue(0, gradientMinValue);
  property.setGradientOpacityMinimumOpacity(0, gradientMinOpacity);
  property.setGradientOpacityMaximumValue(0, gradientMaxValue);
  property.setGradientOpacityMaximumOpacity(0, gradientMaxOpacity);
 
  Eif (preset.interpolation === '1') {
    property.setInterpolationTypeToFastLinear();
    //property.setInterpolationTypeToLinear()
  }
 
  property.setShade(preset.shade === '1');
 
  const ambient = parseFloat(preset.ambient);
  const diffuse = parseFloat(preset.diffuse);
  const specular = parseFloat(preset.specular);
  const specularPower = parseFloat(preset.specularPower);
 
  property.setAmbient(ambient);
  property.setDiffuse(diffuse);
  property.setSpecular(specular);
  property.setSpecularPower(specularPower);
}
 
function getShiftRange(colorTransferArray) {
  // Credit to paraview-glance
  // https://github.com/Kitware/paraview-glance/blob/3fec8eeff31e9c19ad5b6bff8e7159bd745e2ba9/src/components/controls/ColorBy/script.js#L133
 
  // shift range is original rgb/opacity range centered around 0
  let min = Infinity;
  let max = -Infinity;
  for (let i = 0; i < colorTransferArray.length; i += 4) {
    min = Math.min(min, colorTransferArray[i]);
    max = Math.max(max, colorTransferArray[i]);
  }
 
  const center = (max - min) / 2;
 
  return {
    shiftRange: [-center, center],
    min,
    max,
  };
}
 
function applyPointsToRGBFunction(points, range, cfun) {
  const width = range[1] - range[0];
  const rescaled = points.map(([x, r, g, b]) => [
    x * width + range[0],
    r,
    g,
    b,
  ]);
 
  cfun.removeAllPoints();
  rescaled.forEach(([x, r, g, b]) => cfun.addRGBPoint(x, r, g, b));
 
  return rescaled;
}
 
function applyPointsToPiecewiseFunction(points, range, pwf) {
  const width = range[1] - range[0];
  const rescaled = points.map(([x, y]) => [x * width + range[0], y]);
 
  pwf.removeAllPoints();
  rescaled.forEach(([x, y]) => pwf.addPoint(x, y));
 
  return rescaled;
}