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 | import vtkColorTransferFunction from '@kitware/vtk.js/Rendering/Core/ColorTransferFunction'; import vtkPiecewiseFunction from '@kitware/vtk.js/Common/DataModel/PiecewiseFunction'; import { ViewportPreset } from '../types'; import { 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 ) { const eps = 1e-4; // 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: Array<[number, number, number, number]> = []; 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]); } // add a first and last value // to make sure the transfer function is defined on the whole range // normColorTransferValuePoints.unshift([ // normColorTransferValuePoints[0][0] - eps, // normColorTransferValuePoints[0][1], // normColorTransferValuePoints[0][2], // normColorTransferValuePoints[0][3], // ]); // normColorTransferValuePoints.push([ // normColorTransferValuePoints[normColorTransferValuePoints.length - 1][0] + // eps, // normColorTransferValuePoints[normColorTransferValuePoints.length - 1][1], // normColorTransferValuePoints[normColorTransferValuePoints.length - 1][2], // normColorTransferValuePoints[normColorTransferValuePoints.length - 1][3], // ]); applyPointsToRGBFunction(normColorTransferValuePoints, shiftRange, cfun); const property = actor.getProperty(); property.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]); } // add a first and last value // to make sure the transfer function is defined on the whole range // normPoints.unshift([normPoints[0][0] - eps, normPoints[0][1]]); // normPoints.push([ // normPoints[normPoints.length - 1][0] + eps, // normPoints[normPoints.length - 1][1], // ]); applyPointsToPiecewiseFunction(normPoints, shiftRange, ofun); 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); if (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; } |