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 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 | 128x 128x 128x | /**
* planarSliceBasis -- Builds the geometric basis that anchors a planar
* slice in world space.
*
* A `PlanarSliceBasis` is the second tier of the three-tier camera pipeline:
* PlanarViewState (user model) -> PlanarSliceBasis (geometric basis) -> ICamera (render camera)
*
* It encapsulates the properties of a specific cross-section through an image
* or volume -- its center, orientation, and the scale at which the slice fits
* the canvas exactly (zoom = 1). Downstream modules (`planarRenderCamera`)
* combine a PlanarSliceBasis with user-level pan/zoom/rotation to produce a
* renderer-ready ICamera.
*
* Two factory functions are provided:
* - `createPlanarImageSliceBasis` -- for single-image (stack) paths.
* - `createPlanarVolumeSliceBasis` -- for volume-slice paths (arbitrary
* orthogonal orientations with per-slice indexing).
*/
import { vec3 } from 'gl-matrix';
import { InterpolationType, OrientationAxis } from '../../../enums';
import type { IImage, IImageVolume, Point3 } from '../../../types';
import { getImageDataMetadata } from '../../../utilities/getImageDataMetadata';
import { getCubeSizeInView } from '../../../utilities/getPlaneCubeIntersectionDimensions';
import getSpacingInNormalDirection from '../../../utilities/getSpacingInNormalDirection';
import { getVolumeCenterIJK } from '../../Viewport';
import {
getCpuEquivalentParallelScale,
getOrthogonalVolumeSliceGeometry,
} from './planarAdapterCoordinateTransforms';
import { getPlanarViewStateVectors } from './planarOrientationVectors';
import { getSafeCanvasDimension, normalizePoint3 } from './planarMath';
import type { PlanarViewState } from './PlanarViewportTypes';
/**
* The geometric basis for a single planar cross-section through image data.
*
* @property sliceCenterWorld - The world-space center of this slice (the default
* focal point when pan is zero).
* @property viewPlaneNormal - Unit vector perpendicular to the slice plane,
* pointing toward the camera.
* @property viewUp - Unit vector defining the "up" direction in the slice
* plane (before user rotation is applied).
* @property fitParallelScale - The parallelScale value at which the full
* slice exactly fits the canvas (i.e. zoom = 1). Computed from image
* dimensions, spacing, and canvas aspect ratio.
* @property sliceWidthWorld - Width of the displayed slice in world units
* before canvas aspect-ratio fitting is applied.
* @property sliceHeightWorld - Height of the displayed slice in world units
* before canvas aspect-ratio fitting is applied.
* @property cameraDistance - Distance from the focal point to the camera
* position along the viewPlaneNormal. For single images this is a nominal
* value (1); for volumes it spans the full depth so clipping captures the
* entire volume.
*/
export interface PlanarSliceBasis {
sliceCenterWorld: Point3;
viewPlaneNormal: Point3;
viewUp: Point3;
fitParallelScale: number;
sliceWidthWorld: number;
sliceHeightWorld: number;
cameraDistance: number;
}
const MIN_CAMERA_DISTANCE = 1;
const MIN_SLICE_SPACING = 1e-6;
const ORTHONORMAL_EPSILON = 1e-6;
type VolumeSliceFitMetrics = {
fitParallelScale: number;
sliceWidthWorld: number;
sliceHeightWorld: number;
};
function getFitParallelScaleFromWorldSize(args: {
canvasWidth: number;
canvasHeight: number;
widthWorld: number;
heightWorld: number;
}): number {
const { canvasHeight, canvasWidth, heightWorld, widthWorld } = args;
const safeCanvasWidth = getSafeCanvasDimension(canvasWidth);
const safeCanvasHeight = getSafeCanvasDimension(canvasHeight);
const safeWidthWorld = Math.max(widthWorld, MIN_SLICE_SPACING);
const safeHeightWorld = Math.max(heightWorld, MIN_SLICE_SPACING);
const sliceAspectRatio = safeWidthWorld / safeHeightWorld;
const canvasAspectRatio = safeCanvasWidth / safeCanvasHeight;
const scaleFactor = sliceAspectRatio / canvasAspectRatio;
return scaleFactor < 1
? safeHeightWorld / 2
: (safeHeightWorld * scaleFactor) / 2;
}
function createFallbackVolumeSliceFitMetrics(): VolumeSliceFitMetrics {
return {
fitParallelScale: MIN_CAMERA_DISTANCE,
sliceWidthWorld: MIN_CAMERA_DISTANCE * 2,
sliceHeightWorld: MIN_CAMERA_DISTANCE * 2,
};
}
function isOne(value: number): boolean {
return Math.abs(Math.abs(value) - 1) < ORTHONORMAL_EPSILON;
}
function isUnitAxis(direction: ArrayLike<number>, offset: number): boolean {
return (
isOne(direction[offset]) ||
isOne(direction[offset + 1]) ||
isOne(direction[offset + 2])
);
}
function isOrthonormalDirection(direction: ArrayLike<number>): boolean {
return (
isUnitAxis(direction, 0) &&
isUnitAxis(direction, 3) &&
isUnitAxis(direction, 6)
);
}
/**
* Creates a section basis for a single DICOM image (stack path).
*
* The slice plane is derived from the image's direction cosines:
* - Row vector = first 3 elements of the direction matrix.
* - Column vector = next 3 elements.
* - viewPlaneNormal = negated normal (row x column), pointing toward camera.
* - viewUp = negated column vector (DICOM column runs top-to-bottom,
* but screen Y runs bottom-to-top in VTK convention).
*
* sliceCenterWorld is the geometric center of the image in world space,
* computed by offsetting the origin by half the image extent along each axis.
*
* @param args.image - The decoded DICOM image providing geometry metadata.
* @param args.canvasWidth - Current canvas width in CSS pixels.
* @param args.canvasHeight - Current canvas height in CSS pixels.
* @returns A PlanarSliceBasis anchored to this image's plane.
*/
function buildPlanarImageSliceBasis(args: {
image: IImage;
canvasWidth: number;
canvasHeight: number;
rowOffset: number;
columnOffset: number;
rowsForFit: number;
columnsForFit: number;
}): PlanarSliceBasis {
const {
canvasHeight,
canvasWidth,
columnOffset,
columnsForFit,
image,
rowOffset,
rowsForFit,
} = args;
const { direction, origin } = getImageDataMetadata(image);
const rowVector = direction.slice(0, 3) as Point3;
const columnVector = direction.slice(3, 6) as Point3;
const viewPlaneNormal = direction
.slice(6, 9)
.map((value) => -value) as Point3;
const viewUp = columnVector.map((value) => -value) as Point3;
const sliceCenterWorld = [...origin] as Point3;
const sliceWidthWorld =
Math.max(columnsForFit, 1) * (image.columnPixelSpacing || 1);
const sliceHeightWorld =
Math.max(rowsForFit, 1) * (image.rowPixelSpacing || 1);
// Stack images must resolve the same slice basis regardless of whether the
// pixels are drawn through VTK or the CPU fallback. Keeping the center and
// fit geometry shared prevents render-mode switches from nudging the camera.
vec3.scaleAndAdd(
sliceCenterWorld as unknown as vec3,
sliceCenterWorld as unknown as vec3,
rowVector as unknown as vec3,
rowOffset
);
vec3.scaleAndAdd(
sliceCenterWorld as unknown as vec3,
sliceCenterWorld as unknown as vec3,
columnVector as unknown as vec3,
columnOffset
);
return {
sliceCenterWorld,
viewPlaneNormal: vec3.clone(viewPlaneNormal as unknown as vec3) as Point3,
viewUp,
fitParallelScale: getCpuEquivalentParallelScale({
canvasHeight,
canvasWidth,
columnPixelSpacing: image.columnPixelSpacing || 1,
columns: columnsForFit,
rowPixelSpacing: image.rowPixelSpacing || 1,
rows: rowsForFit,
}),
sliceWidthWorld,
sliceHeightWorld,
cameraDistance: 1,
};
}
export function createPlanarImageSliceBasis(args: {
image: IImage;
canvasWidth: number;
canvasHeight: number;
}): PlanarSliceBasis {
const { image } = args;
const { dimensions, spacing } = getImageDataMetadata(image);
return buildPlanarImageSliceBasis({
...args,
rowOffset: ((dimensions[0] - 1) / 2) * spacing[0],
columnOffset: ((dimensions[1] - 1) / 2) * spacing[1],
rowsForFit: Math.max(image.rows, 1),
columnsForFit: Math.max(image.columns, 1),
});
}
/**
* CPU stack rendering reuses the VTK-compatible image slice basis so that the
* semantic camera is invariant across render modes.
*/
export function createPlanarCpuImageSliceBasis(args: {
image: IImage;
canvasWidth: number;
canvasHeight: number;
}): PlanarSliceBasis {
return createPlanarImageSliceBasis(args);
}
/**
* Computes the 8 world-space corners of a volume's bounding box.
* Used by `getSliceMetrics` to determine the range of slice positions
* along an arbitrary viewing direction.
*/
function buildImageVolumeCorners(imageVolume: IImageVolume): Point3[] {
const imageData = imageVolume.imageData;
if (!imageData) {
return [];
}
const direction = imageData.getDirection();
if (isOrthonormalDirection(direction)) {
const bounds = imageData.extentToBounds(imageData.getExtent());
return [
[bounds[0], bounds[2], bounds[4]],
[bounds[0], bounds[2], bounds[5]],
[bounds[0], bounds[3], bounds[4]],
[bounds[0], bounds[3], bounds[5]],
[bounds[1], bounds[2], bounds[4]],
[bounds[1], bounds[2], bounds[5]],
[bounds[1], bounds[3], bounds[4]],
[bounds[1], bounds[3], bounds[5]],
];
}
const [dx, dy, dz] = imageData.getDimensions();
const cornersIdx = [
[0, 0, 0],
[dx - 1, 0, 0],
[0, dy - 1, 0],
[dx - 1, dy - 1, 0],
[0, 0, dz - 1],
[dx - 1, 0, dz - 1],
[0, dy - 1, dz - 1],
[dx - 1, dy - 1, dz - 1],
] as Point3[];
return cornersIdx.map((it) => imageData.indexToWorld(it)) as Point3[];
}
/**
* Returns the world-space center of a volume, computed from the midpoint
* of the voxel-center index domain. Falls back to averaging the bounding-box
* corners if vtkImageData is unavailable.
*
* When `viewPlaneNormal` is supplied, the slice-direction axis is snapped to
* `Math.floor(d / 2)` via `getVolumeCenterIJK`, matching legacy resetCamera
* behavior. Without this snap, even-dimensioned slice axes (e.g. 512) land on
* `(d - 1) / 2 = 255.5` and the VTK reslice mapper rounds the focal point to
* the adjacent voxel, producing a one-slice offset on initial render.
*/
function getGeometricImageVolumeCenter(
imageVolume: IImageVolume,
viewPlaneNormal?: Point3
): Point3 {
const imageData = imageVolume.imageData;
if (imageData) {
const dimensions = imageData.getDimensions();
const ijk = viewPlaneNormal
? getVolumeCenterIJK(
dimensions,
imageData.getDirection(),
viewPlaneNormal
)
: dimensions.map((d) => (d - 1) / 2);
return imageData.indexToWorld(ijk as [number, number, number]) as Point3;
}
const corners = buildImageVolumeCorners(imageVolume);
if (!corners.length) {
return [0, 0, 0];
}
const center = vec3.create();
for (const corner of corners) {
vec3.add(center, center, corner as unknown as vec3);
}
return vec3.scale(center, center, 1 / corners.length) as Point3;
}
/**
* World-space center of the volume slice that backs `imageVolume.imageIds[k]`.
*
* Volumes are constructed so `imageIds[k]` is IJK slice k, so the exact slice
* center is `indexToWorld([centerI, centerJ, k])`. This is the ordering-safe
* way to anchor a slice for an index expressed in the volume's imageId list:
* the alternative — walking `min + k * spacing` along the viewPlaneNormal in
* `buildPlanarVolumeSliceBasis` — counts slices in CAMERA order, and for the
* acquisition orientation the camera normal is the negated scan axis, so an
* imageId-list index fed into that walk lands on the mirrored slice (and on
* oblique volumes drifts off slice centers, since the corner projections span
* the voxel bounding box rather than slice centers).
*/
export function getVolumeImageIdIndexWorldPoint(
imageVolume: IImageVolume | undefined,
imageIdIndex: number
): Point3 | undefined {
const imageData = imageVolume?.imageData;
if (!imageData) {
return;
}
const dimensions = imageData.getDimensions();
// Dynamic (4D) volumes flatten their imageIds across dimension groups while
// dimensions[2] is the slice count of a SINGLE group, so an image from any
// later group has a flattened index >= dimensions[2] — clamping that raw
// index would pin every such image to the final slice. Map it to its
// group-local slice first (the k axis repeats per group).
const flatToGroupLocal = (
imageVolume as {
flatImageIdIndexToImageIdIndex?: (flatImageIdIndex: number) => number;
}
).flatImageIdIndexToImageIdIndex;
const groupLocalIndex =
typeof flatToGroupLocal === 'function'
? flatToGroupLocal.call(imageVolume, Math.max(0, imageIdIndex))
: imageIdIndex;
const k = Math.min(Math.max(0, groupLocalIndex), dimensions[2] - 1);
return imageData.indexToWorld([
(dimensions[0] - 1) / 2,
(dimensions[1] - 1) / 2,
k,
]) as Point3;
}
/**
* Computes the min/max projections of a volume's corners onto the
* viewPlaneNormal, along with the spacing between adjacent slices and
* the maximum valid slice index.
*
* These metrics are used by `createPlanarVolumeSliceBasis` to position
* the sliceCenterWorld at the correct depth for a given imageIdIndex.
*/
function getSliceMetrics(args: {
imageVolume: IImageVolume;
viewPlaneNormal: Point3;
}) {
const { imageVolume, viewPlaneNormal } = args;
const corners = buildImageVolumeCorners(imageVolume);
const spacingInNormalDirection = Math.max(
getSpacingInNormalDirection(imageVolume, viewPlaneNormal),
MIN_SLICE_SPACING
);
if (!corners.length) {
return {
min: 0,
max: 0,
spacingInNormalDirection,
maxImageIdIndex: 0,
};
}
const projectedValues = corners.map((corner) =>
vec3.dot(corner as unknown as vec3, viewPlaneNormal as unknown as vec3)
);
const min = Math.min(...projectedValues);
const max = Math.max(...projectedValues);
const maxImageIdIndex = Math.max(
0,
Math.round((max - min) / spacingInNormalDirection)
);
return {
min,
max,
spacingInNormalDirection,
maxImageIdIndex,
};
}
/**
* Clamps an imageIdIndex to [0, maxImageIdIndex]. If the index is undefined,
* derives the centered slice index using the same projection math as the
* legacy volume viewport scroll utilities.
*/
function resolveCenteredImageIdIndex(args: {
centerProjection: number;
min: number;
max: number;
maxImageIdIndex: number;
}): number {
const { centerProjection, min, max, maxImageIdIndex } = args;
const range = max - min;
if (!(range > 0) || maxImageIdIndex === 0) {
return 0;
}
const fraction = (centerProjection - min) / range;
const floatingImageIdIndex = fraction * maxImageIdIndex;
return Math.round(floatingImageIdIndex);
}
function clampImageIdIndex(args: {
imageIdIndex: number | undefined;
centerProjection: number;
min: number;
max: number;
maxImageIdIndex: number;
}): number {
const { centerProjection, imageIdIndex, max, maxImageIdIndex, min } = args;
if (typeof imageIdIndex !== 'number') {
return resolveCenteredImageIdIndex({
centerProjection,
min,
max,
maxImageIdIndex,
});
}
return Math.min(Math.max(0, imageIdIndex), maxImageIdIndex);
}
export function resolvePlanarVolumeImageIdIndex(args: {
viewState?: PlanarViewState;
fallbackImageIdIndex?: number;
}): number | undefined {
const { viewState, fallbackImageIdIndex } = args;
const slice = viewState?.slice;
if (slice?.kind === 'volumePoint') {
return;
}
if (slice?.kind === 'stackIndex') {
return slice.imageIdIndex;
}
if (viewState?.orientation === OrientationAxis.ACQUISITION) {
return fallbackImageIdIndex;
}
}
/**
* Computes the `fitParallelScale` for a volume slice viewed from a given
* orientation. Uses `getOrthogonalVolumeSliceGeometry` to determine the
* effective rows/columns/spacing of the visible cross-section, then
* delegates to `getCpuEquivalentParallelScale` for aspect-ratio-aware
* scale computation.
*/
function getCpuVolumeSliceFitMetrics(args: {
imageVolume: IImageVolume;
viewPlaneNormal: Point3;
viewUp: Point3;
canvasWidth: number;
canvasHeight: number;
}): VolumeSliceFitMetrics {
const { imageVolume, viewPlaneNormal, viewUp, canvasWidth, canvasHeight } =
args;
const geometry = getOrthogonalVolumeSliceGeometry({
dimensions: imageVolume.dimensions,
direction: imageVolume.direction,
spacing: imageVolume.spacing,
viewPlaneNormal,
viewUp,
});
if (geometry) {
const columns = Math.max(geometry.columns, 1);
const rows = Math.max(geometry.rows, 1);
const sliceWidthWorld = columns * geometry.columnPixelSpacing;
const sliceHeightWorld = rows * geometry.rowPixelSpacing;
return {
fitParallelScale: getCpuEquivalentParallelScale({
canvasHeight: getSafeCanvasDimension(canvasHeight),
canvasWidth: getSafeCanvasDimension(canvasWidth),
columnPixelSpacing: geometry.columnPixelSpacing,
columns,
rowPixelSpacing: geometry.rowPixelSpacing,
rows,
}),
sliceWidthWorld,
sliceHeightWorld,
};
}
const imageData = imageVolume.imageData;
if (imageData) {
const { widthWorld, heightWorld } = getCubeSizeInView(
imageData,
viewPlaneNormal,
viewUp
);
if (widthWorld > 0 && heightWorld > 0) {
return {
fitParallelScale: getFitParallelScaleFromWorldSize({
canvasHeight,
canvasWidth,
widthWorld,
heightWorld,
}),
sliceWidthWorld: widthWorld,
sliceHeightWorld: heightWorld,
};
}
}
return createFallbackVolumeSliceFitMetrics();
}
/**
* Creates a section basis for a volume viewed from an orthogonal orientation.
*
* The orientation (axial, sagittal, coronal, or custom) determines the
* viewPlaneNormal and viewUp vectors. The imageIdIndex selects which slice
* along the normal to display.
*
* Slice positioning works by:
* 1. Computing the volume's bounding box projection onto the viewPlaneNormal.
* 2. Mapping `imageIdIndex` to a depth within [min, max] at `spacingInNormalDirection` intervals.
* 3. Offsetting the volume center along the normal to reach that depth.
*
* @param args.canvasWidth - Current canvas width in CSS pixels.
* @param args.canvasHeight - Current canvas height in CSS pixels.
* @param args.imageIdIndex - Desired slice index; defaults to the middle slice if undefined.
* @param args.imageVolume - The loaded volume providing geometry and vtkImageData.
* @param args.orientation - Orientation axis or custom orientation vectors.
* @returns The section basis, the clamped slice index, and the maximum valid index.
*/
function buildPlanarVolumeSliceBasis(args: {
canvasWidth: number;
canvasHeight: number;
imageIdIndex?: number;
imageVolume: IImageVolume;
orientation?: PlanarViewState['orientation'];
viewState?: PlanarViewState;
center: Point3;
fitParallelScale: number;
sliceWidthWorld: number;
sliceHeightWorld: number;
}): {
sliceBasis: PlanarSliceBasis;
currentImageIdIndex: number;
maxImageIdIndex: number;
} {
const {
center,
fitParallelScale,
imageIdIndex,
imageVolume,
orientation,
sliceHeightWorld,
sliceWidthWorld,
viewState,
} = args;
const cameraValues = getPlanarViewStateVectors({
imageVolume,
orientation,
});
if (!cameraValues) {
return {
sliceBasis: {
sliceCenterWorld: [0, 0, 0],
viewPlaneNormal: [0, 0, 1],
viewUp: [0, -1, 0],
fitParallelScale: MIN_CAMERA_DISTANCE,
sliceWidthWorld: MIN_CAMERA_DISTANCE * 2,
sliceHeightWorld: MIN_CAMERA_DISTANCE * 2,
cameraDistance: MIN_CAMERA_DISTANCE,
},
currentImageIdIndex: 0,
maxImageIdIndex: 0,
};
}
const viewPlaneNormal = normalizePoint3(cameraValues.viewPlaneNormal);
const viewUp = normalizePoint3(cameraValues.viewUp);
const { max, maxImageIdIndex, min, spacingInNormalDirection } =
getSliceMetrics({
imageVolume,
viewPlaneNormal,
});
const centerProjection = vec3.dot(
center as unknown as vec3,
viewPlaneNormal as unknown as vec3
);
const volumePoint =
viewState?.slice?.kind === 'volumePoint'
? viewState.slice.sliceWorldPoint
: undefined;
const requestedSliceProjection = Math.min(
max,
Math.max(
min,
volumePoint
? vec3.dot(
volumePoint as unknown as vec3,
viewPlaneNormal as unknown as vec3
)
: centerProjection
)
);
const currentImageIdIndex = clampImageIdIndex({
imageIdIndex,
centerProjection: requestedSliceProjection,
min,
max,
maxImageIdIndex,
});
// Project volume center onto the viewing direction to compute the
// scalar offset needed to reach the target slice depth.
const targetProjection =
typeof imageIdIndex === 'number'
? Math.min(max, min + currentImageIdIndex * spacingInNormalDirection)
: requestedSliceProjection;
const scalarOffset = targetProjection - centerProjection;
const sliceCenterWorld = vec3.scaleAndAdd(
vec3.create(),
center as unknown as vec3,
viewPlaneNormal as unknown as vec3,
scalarOffset
) as Point3;
// cameraDistance spans the full volume depth so the clipping range
// can capture the entire volume when needed.
const cameraDistance = Math.max(max - min, spacingInNormalDirection, 1);
return {
sliceBasis: {
sliceCenterWorld,
viewPlaneNormal,
viewUp,
fitParallelScale,
sliceWidthWorld,
sliceHeightWorld,
cameraDistance,
},
currentImageIdIndex,
maxImageIdIndex,
};
}
export function createPlanarVolumeSliceBasis(args: {
canvasWidth: number;
canvasHeight: number;
imageIdIndex?: number;
imageVolume: IImageVolume;
orientation?: PlanarViewState['orientation'];
viewState?: PlanarViewState;
}): {
sliceBasis: PlanarSliceBasis;
currentImageIdIndex: number;
maxImageIdIndex: number;
} {
const { imageVolume, orientation } = args;
const cameraValues = getPlanarViewStateVectors({
imageVolume,
orientation,
});
const sliceFitMetrics = cameraValues
? getCpuVolumeSliceFitMetrics({
...args,
viewPlaneNormal: cameraValues.viewPlaneNormal,
viewUp: cameraValues.viewUp,
})
: createFallbackVolumeSliceFitMetrics();
return buildPlanarVolumeSliceBasis({
...args,
center: getGeometricImageVolumeCenter(
imageVolume,
cameraValues?.viewPlaneNormal
),
...sliceFitMetrics,
});
}
export function createPlanarCpuVolumeSliceBasis(args: {
canvasWidth: number;
canvasHeight: number;
imageIdIndex?: number;
imageVolume: IImageVolume;
orientation?: PlanarViewState['orientation'];
viewState?: PlanarViewState;
}): {
sliceBasis: PlanarSliceBasis;
currentImageIdIndex: number;
maxImageIdIndex: number;
} {
return createPlanarVolumeSliceBasis(args);
}
export function shouldUsePlanarCpuVolumeSliceBasis(
interpolationType: InterpolationType = InterpolationType.LINEAR
): boolean {
return interpolationType === InterpolationType.NEAREST;
}
|