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 | 128x | import vtkMath from '@kitware/vtk.js/Common/Core/Math';
import { MPR_CAMERA_VALUES } from '../../../constants';
import { OrientationAxis } from '../../../enums';
import { getConfiguration } from '../../../init';
import type { IImageVolume, Point3 } from '../../../types';
import { getCubeSizeInView } from '../../../utilities/getPlaneCubeIntersectionDimensions';
import { getVolumeCenterIJK } from '../../Viewport';
import type {
Volume3DCamera,
Volume3DVtkVolumeAdapterContext,
} from './viewport3DTypes';
const VOLUME_3D_RADIUS_MULTIPLIER = 10;
export function getInitialVolume3DCamera(
ctx: Volume3DVtkVolumeAdapterContext,
imageVolume: IImageVolume
): Partial<Volume3DCamera> | undefined {
const imageData = imageVolume.imageData;
if (!imageData) {
return;
}
const vtkCamera = ctx.vtk.renderer.getActiveCamera();
const orientation = getOrientationVectors({
fallbackViewPlaneNormal: vtkCamera.getViewPlaneNormal() as Point3,
fallbackViewUp: vtkCamera.getViewUp() as Point3,
imageVolume,
orientation: ctx.viewport.options.orientation,
});
const { viewPlaneNormal, viewUp } = orientation;
const bounds = imageData.getBounds();
const focalPoint = [0, 0, 0] as Point3;
const dimensions = imageData.getDimensions();
const middleIJK = getVolumeCenterIJK(
dimensions,
imageData.getDirection(),
viewPlaneNormal
) as Point3;
imageData.indexToWorld(middleIJK, focalPoint);
const { widthWorld, heightWorld } = getCubeSizeInView(
imageData,
viewPlaneNormal,
viewUp
);
const canvasWidth =
ctx.vtk.canvas.width || ctx.viewport.element.clientWidth || 1;
const canvasHeight =
ctx.vtk.canvas.height || ctx.viewport.element.clientHeight || 1;
const boundsAspectRatio = widthWorld / heightWorld;
const canvasAspectRatio = canvasWidth / canvasHeight;
const scaleFactor = boundsAspectRatio / canvasAspectRatio;
const insetImageMultiplier = getConfiguration().rendering?.useLegacyCameraFOV
? 1.1
: 1;
const parallelScale =
scaleFactor < 1
? (insetImageMultiplier * heightWorld) / 2
: (insetImageMultiplier * heightWorld * scaleFactor) / 2;
const radius = getBoundsRadius(bounds) * VOLUME_3D_RADIUS_MULTIPLIER;
const distance = insetImageMultiplier * radius;
const viewUpToUse =
Math.abs(vtkMath.dot(viewUp, viewPlaneNormal)) > 0.999
? ([-viewUp[2], viewUp[0], viewUp[1]] as Point3)
: viewUp;
const position = [
focalPoint[0] + distance * viewPlaneNormal[0],
focalPoint[1] + distance * viewPlaneNormal[1],
focalPoint[2] + distance * viewPlaneNormal[2],
] as Point3;
if ('setPhysicalScale' in vtkCamera) {
(
vtkCamera as typeof vtkCamera & {
setPhysicalScale(value: number): void;
setPhysicalTranslation(x: number, y: number, z: number): void;
}
).setPhysicalScale(radius);
(
vtkCamera as typeof vtkCamera & {
setPhysicalScale(value: number): void;
setPhysicalTranslation(x: number, y: number, z: number): void;
}
).setPhysicalTranslation(-focalPoint[0], -focalPoint[1], -focalPoint[2]);
}
return {
focalPoint,
parallelProjection: ctx.viewport.options.parallelProjection ?? true,
parallelScale,
position,
viewAngle: 90,
viewPlaneNormal,
viewUp: viewUpToUse,
};
}
function getOrientationVectors(args: {
fallbackViewPlaneNormal: Point3;
fallbackViewUp: Point3;
imageVolume: IImageVolume;
orientation: Volume3DVtkVolumeAdapterContext['viewport']['options']['orientation'];
}): Pick<Volume3DCamera, 'viewPlaneNormal' | 'viewUp'> {
const { fallbackViewPlaneNormal, fallbackViewUp, imageVolume, orientation } =
args;
if (!orientation) {
return {
viewPlaneNormal: fallbackViewPlaneNormal,
viewUp: fallbackViewUp,
};
}
if (typeof orientation === 'object' && orientation.viewPlaneNormal) {
return {
viewPlaneNormal: [...orientation.viewPlaneNormal] as Point3,
viewUp: orientation.viewUp
? ([...orientation.viewUp] as Point3)
: fallbackViewUp,
};
}
if (orientation === OrientationAxis.ACQUISITION) {
const { direction } = imageVolume;
return {
viewPlaneNormal: direction.slice(6, 9).map((x) => -x) as Point3,
viewUp: direction.slice(3, 6).map((x) => -x) as Point3,
};
}
const cameraValues =
orientation === OrientationAxis.AXIAL_REFORMAT
? MPR_CAMERA_VALUES.axial
: orientation === OrientationAxis.SAGITTAL_REFORMAT
? MPR_CAMERA_VALUES.sagittal
: orientation === OrientationAxis.CORONAL_REFORMAT
? MPR_CAMERA_VALUES.coronal
: typeof orientation === 'string'
? MPR_CAMERA_VALUES[orientation]
: undefined;
if (!cameraValues) {
return {
viewPlaneNormal: fallbackViewPlaneNormal,
viewUp: fallbackViewUp,
};
}
return {
viewPlaneNormal: [...cameraValues.viewPlaneNormal] as Point3,
viewUp: [...cameraValues.viewUp] as Point3,
};
}
function getBoundsRadius(bounds: number[]): number {
const width = (bounds[1] - bounds[0]) ** 2;
const height = (bounds[3] - bounds[2]) ** 2;
const depth = (bounds[5] - bounds[4]) ** 2;
return Math.sqrt(width + height + depth || 1) * 0.5;
}
|