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 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 | 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x | import vtkDataArray from '@kitware/vtk.js/Common/Core/DataArray';
import vtkImageData from '@kitware/vtk.js/Common/DataModel/ImageData';
import type vtkImageSlice from '@kitware/vtk.js/Rendering/Core/ImageSlice';
import vtkColorTransferFunction from '@kitware/vtk.js/Rendering/Core/ColorTransferFunction';
import type vtkRenderer from '@kitware/vtk.js/Rendering/Core/Renderer';
import { InterpolationType, VOILUTFunctionType } from '../../enums';
import type { ColormapPublic, IImage, Point3, VOIRange } from '../../types';
import createLinearRGBTransferFunction from '../../utilities/createLinearRGBTransferFunction';
import createSigmoidRGBTransferFunction from '../../utilities/createSigmoidRGBTransferFunction';
import getVOIRangeFromWindowLevel from '../../utilities/getVOIRangeFromWindowLevel';
import isPTPrescaledWithSUV from '../../utilities/isPTPrescaledWithSUV';
import { getImageDataMetadata } from '../../utilities/getImageDataMetadata';
import invertRgbTransferFunction from '../../utilities/invertRgbTransferFunction';
import { resolveColormap } from '../../utilities/colormap';
import { updateVTKImageDataWithCornerstoneImage } from '../../utilities/updateVTKImageDataWithCornerstoneImage';
export interface PlanarCameraState {
focalPoint: Point3;
parallelScale: number;
position: Point3;
viewPlaneNormal: Point3;
viewUp: Point3;
}
export interface PlanarImagePresentation {
visible?: boolean;
opacity?: number;
interpolationType?: InterpolationType;
colormap?: ColormapPublic;
voiRange?: VOIRange;
voiLUTFunction?: VOILUTFunctionType;
invert?: boolean;
}
export interface PlanarImageViewState {
zoom?: number;
pan?: [number, number];
}
export function createEmptyVTKImageData(args: {
dimensions: Point3;
direction: number[] | ArrayLike<number>;
numberOfComponents: number;
origin: Point3;
pixelArray: ArrayLike<number>;
spacing: Point3;
}): vtkImageData {
const {
dimensions,
direction,
numberOfComponents,
origin,
pixelArray,
spacing,
} = args;
const values =
ArrayBuffer.isView(pixelArray) && !(pixelArray instanceof DataView)
? pixelArray
: Array.from(pixelArray);
const dataType =
ArrayBuffer.isView(values) && !(values instanceof DataView)
? vtkDataArray.getDataType(values as never)
: undefined;
const scalarArray = vtkDataArray.newInstance({
...(dataType ? { dataType } : {}),
name: 'Pixels',
numberOfComponents,
values,
});
const imageData = vtkImageData.newInstance();
imageData.setDimensions(dimensions);
imageData.setSpacing(spacing);
imageData.setDirection(new Float32Array(Array.from(direction)));
imageData.setOrigin(origin);
imageData.getPointData().setScalars(scalarArray);
return imageData;
}
export function createVTKImageDataFromImage(image: IImage): vtkImageData {
const { dimensions, direction, numberOfComponents, origin, spacing } =
getImageDataMetadata(image);
// Own a PRIVATE copy of the scalars rather than wrapping the source image's
// voxelManager buffer by reference. The reuse-in-place scroll path
// (updateVTKImageDataWithCornerstoneImage -> scalarData.set) overwrites this
// actor buffer with the *next* frame's pixels; if it aliased the source
// image's cached buffer, scrolling to another slice would corrupt the first
// image's cached pixel data, and scrolling back would then render the wrong
// (previously displayed) slice. Mirrors legacy StackViewport, whose actor
// buffer is independent of the image cache.
const pixelArray = image.voxelManager.getScalarData().slice();
const imageData = createEmptyVTKImageData({
dimensions,
direction: Array.from(direction),
numberOfComponents,
origin,
pixelArray,
spacing,
});
updateVTKImageDataWithCornerstoneImage(imageData, image);
return imageData;
}
/**
* Refreshes an existing vtkImageData's geometry (origin, direction, spacing) to
* match a new cornerstone image, mirroring what createVTKImageDataFromImage sets
* on a freshly built one. The reuse-in-place scroll path only rewrites scalars
* via updateVTKImageDataWithCornerstoneImage, so without this the actor keeps the
* previous frame's image plane. Multi-frame stacks (e.g. ultrasound cine) place
* each frame at a distinct world position and the camera follows that plane on
* scroll, so a stale origin leaves the actor off the focal plane and the viewport
* renders black from the second frame onward. Dimensions are intentionally left
* untouched - the reuse path only runs when they already match.
*/
export function updateVTKImageDataGeometryFromImage(
imageData: vtkImageData,
image: IImage
): void {
const { direction, origin, spacing } = getImageDataMetadata(image);
imageData.setOrigin(origin);
imageData.setDirection(new Float32Array(Array.from(direction)));
imageData.setSpacing(spacing);
}
export function getDefaultImageVOIRange(image: IImage): VOIRange | undefined {
// Mirror legacy StackViewport._getInitialVOIRange: a prescaled PT (SUV) image
// defaults to a 0-5 VOI range rather than its raw DICOM window center/width,
// which is too wide for PET and skews the display. Keyed off the loader-set
// preScale fields (not image.isPreScaled, which the native path never sets).
if (isPTPrescaledWithSUV(image)) {
return { lower: 0, upper: 5 };
}
return getVOIRangeFromWindowLevel(
image.windowWidth,
image.windowCenter,
image.voiLUTFunction
);
}
export function getPlanarCameraState(renderer: vtkRenderer): PlanarCameraState {
const camera = renderer.getActiveCamera();
return {
focalPoint: [...camera.getFocalPoint()] as Point3,
parallelScale: camera.getParallelScale(),
position: [...camera.getPosition()] as Point3,
viewPlaneNormal: [...camera.getViewPlaneNormal()] as Point3,
viewUp: [...camera.getViewUp()] as Point3,
};
}
export function applyPlanarImagePresentation(args: {
actor: vtkImageSlice;
defaultVOIRange?: VOIRange;
defaultVOILUTFunction?: VOILUTFunctionType;
props?: PlanarImagePresentation;
}): void {
const { actor, defaultVOIRange, defaultVOILUTFunction, props } = args;
const property = actor.getProperty();
const voiRange = props?.voiRange ?? defaultVOIRange;
if (props?.visible !== undefined) {
actor.setVisibility(props.visible);
}
if (props?.opacity !== undefined) {
property.setOpacity(props.opacity);
}
if (props?.interpolationType !== undefined) {
property.setInterpolationType(
props.interpolationType as Parameters<
typeof property.setInterpolationType
>[0]
);
}
if (!voiRange) {
return;
}
const transferFunction = createPlanarRGBTransferFunction({
colormap: props?.colormap,
invert: props?.invert,
voiRange,
voiLUTFunction: props?.voiLUTFunction ?? defaultVOILUTFunction,
});
property.setUseLookupTableScalarRange(true);
property.setRGBTransferFunction(0, transferFunction);
}
export function createPlanarRGBTransferFunction(args: {
colormap?: ColormapPublic;
invert?: boolean;
voiRange: VOIRange;
voiLUTFunction?: VOILUTFunctionType;
}): vtkColorTransferFunction {
const { colormap, invert, voiRange, voiLUTFunction } = args;
const transferFunction =
colormap?.name !== undefined
? createColormapTransferFunction(colormap, voiRange)
: voiLUTFunction === VOILUTFunctionType.SAMPLED_SIGMOID
? createSigmoidRGBTransferFunction(voiRange)
: createLinearRGBTransferFunction(voiRange);
if (invert) {
invertRgbTransferFunction(transferFunction);
}
return transferFunction;
}
function createColormapTransferFunction(
colormap: ColormapPublic,
voiRange: VOIRange
): vtkColorTransferFunction {
const colormapName = colormap.name;
const colormapDefinition = colormapName
? resolveColormap(colormapName)
: undefined;
if (!colormapDefinition) {
throw new Error(`Colormap ${colormapName} not found`);
}
const transferFunction = vtkColorTransferFunction.newInstance();
transferFunction.applyColorMap(colormapDefinition);
transferFunction.setMappingRange(voiRange.lower, voiRange.upper);
return transferFunction;
}
export function applyPlanarCameraViewState(args: {
initialCamera: PlanarCameraState;
renderer: vtkRenderer;
viewState?: PlanarImageViewState;
}): void {
const { initialCamera, renderer, viewState } = args;
const camera = renderer.getActiveCamera();
const zoom = Math.max(viewState?.zoom ?? 1, 0.001);
const [panX, panY] = viewState?.pan ?? [0, 0];
camera.setParallelProjection(true);
camera.setParallelScale(initialCamera.parallelScale / zoom);
camera.setFocalPoint(
initialCamera.focalPoint[0] + panX,
initialCamera.focalPoint[1] + panY,
initialCamera.focalPoint[2]
);
camera.setPosition(
initialCamera.position[0] + panX,
initialCamera.position[1] + panY,
initialCamera.position[2]
);
}
|