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 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 | import { vec3 } from 'gl-matrix';
import { ViewportType } from '../../../enums';
import type {
ActorEntry,
ICamera,
IImageData,
Point3,
ViewReference,
ViewReferenceSpecifier,
} from '../../../types';
import type ViewportInputOptions from '../../../types/ViewportInputOptions';
import renderingEngineCache from '../../renderingEngineCache';
import type {
DataAddOptions,
LoadedData,
ViewportDataBinding,
} from '../ViewportArchitectureTypes';
import GenericViewport from '../GenericViewport';
import {
getDimensionGroupReferenceContext,
type GenericViewportReferenceContext,
} from '../genericViewportReferenceCompatibility';
import {
getGenericViewportImageDisplaySet,
isGenericViewportImageDisplaySet,
} from '../genericViewportDisplaySetAccess';
import { DefaultVolume3DDataProvider } from './DefaultVolume3DDataProvider';
import { createVolume3DRenderPathResolver } from './Volume3DRenderPathResolver';
import Volume3DResolvedView from './Volume3DResolvedView';
import applyVolume3DCamera from './applyVolume3DCamera';
import {
getVolume3DProjectionSnapshot,
type Volume3DProjectionSnapshot,
} from './volume3DProjectionAdapter';
import type {
Volume3DCamera,
Volume3DPayload,
Volume3DDataPresentation,
Volume3DRegisteredDataSet,
Volume3DRendering,
Volume3DSetDataOptions,
Volume3DViewportRenderContext,
VolumeViewport3DInput,
} from './viewport3DTypes';
class VolumeViewport3D extends GenericViewport<
Volume3DCamera,
Volume3DDataPresentation,
Volume3DViewportRenderContext
> {
readonly type = ViewportType.VOLUME_3D_NEXT;
readonly renderingEngineId: string;
readonly canvas: HTMLCanvasElement;
sWidth: number;
sHeight: number;
defaultOptions: ViewportInputOptions;
suppressEvents = false;
protected renderContext: Volume3DViewportRenderContext;
private primaryDataId?: string;
static get useCustomRenderingPipeline(): boolean {
return false;
}
getUseCustomRenderingPipeline(): boolean {
return false;
}
setRendered(): void {
super.setRendered();
}
constructor(args: VolumeViewport3DInput) {
super(args);
this.renderingEngineId = args.renderingEngineId;
this.canvas = args.canvas;
// The 3D viewport renders VTK directly to this on-screen canvas (it has no CPU
// canvas like PlanarViewport). When the same element previously hosted a CPU
// PlanarViewport, that viewport hid this shared canvas (display:none) in favor of
// its cpuCanvas; ensure it is visible again so the volume rendering is shown.
this.canvas.style.display = '';
this.sWidth = args.sWidth;
this.sHeight = args.sHeight;
this.defaultOptions = args.defaultOptions || {};
this.element.style.position = this.element.style.position || 'relative';
this.element.style.overflow = 'hidden';
this.element.style.background = this.element.style.background || '#000';
this.dataProvider = args.dataProvider || new DefaultVolume3DDataProvider();
this.renderPathResolver =
args.renderPathResolver || createVolume3DRenderPathResolver();
const renderingEngine = renderingEngineCache.get(this.renderingEngineId);
const renderer = renderingEngine?.getRenderer(this.id);
if (!renderer) {
throw new Error(
'[VolumeViewport3D] No renderer available. Ensure WebGL is supported and the rendering engine has been properly initialized.'
);
}
renderer
.getActiveCamera()
.setParallelProjection(this.defaultOptions.parallelProjection ?? true);
this.renderContext = {
viewportId: this.id,
renderingEngineId: this.renderingEngineId,
type: '3d',
viewport: {
element: this.element,
options: {
orientation: this.defaultOptions.orientation,
parallelProjection: this.defaultOptions.parallelProjection,
},
},
display: {
requestRender: () => {
this.requestRenderingEngineRender();
},
},
vtk: {
canvas: this.canvas,
renderer,
},
};
this.viewState = {
parallelProjection: this.defaultOptions.parallelProjection ?? true,
} as Volume3DCamera;
this.element.setAttribute('data-viewport-uid', this.id);
this.element.setAttribute(
'data-rendering-engine-uid',
this.renderingEngineId
);
}
/**
* Replaces all mounted 3D display sets with the provided ones. The first
* entry is mounted as the source binding; subsequent entries default to the
* overlay role unless they specify one explicitly.
*
* @param entries - Display sets to mount, each with its own render-mode options.
*/
async setDisplaySets(
...entries: Array<{
displaySetId: string;
options?: Volume3DSetDataOptions;
}>
): Promise<void> {
this.removeAllData();
for (const [index, { displaySetId, options = {} }] of entries.entries()) {
await this.addDisplaySet(displaySetId, {
...options,
role: options.role ?? (index === 0 ? 'source' : 'overlay'),
});
}
}
/**
* Adds a single 3D display set and selects the effective 3D render mode.
*
* @param displaySetId - Logical display set id to add.
* @param options - Requested 3D render-mode options.
*/
async addDisplaySet(
displaySetId: string,
options: Volume3DSetDataOptions | DataAddOptions = {}
): Promise<void> {
const volumeOptions = options as Volume3DSetDataOptions;
const renderMode = this.resolveRenderMode(
displaySetId,
volumeOptions.renderMode
);
await super.addDisplaySet(displaySetId, {
renderMode,
role: volumeOptions.role,
});
if (renderMode === 'vtkVolume3d' && volumeOptions.role === 'source') {
this.primaryDataId = displaySetId;
}
this.setDefaultDataPresentation(displaySetId, {
visible: true,
opacity: 1,
});
this.viewState = this.getViewState();
}
/**
* Returns the rendering engine that owns this viewport.
*
* @returns The parent rendering engine, if it is still registered.
*/
getRenderingEngine() {
return renderingEngineCache.get(this.renderingEngineId);
}
/**
* Returns image ids for the primary volume dataset when present.
*
* @returns The image ids for the primary volume dataset, if available.
*/
getImageIds(): string[] {
const binding = this.getCurrentBinding();
if (!binding) {
return [];
}
const data = this.getVolume3DPayload(binding);
const rendering = this.getVolume3DRendering(binding);
if (
!data ||
data.renderMode !== 'vtkVolume3d' ||
rendering.renderMode !== 'vtkVolume3d'
) {
return [];
}
return data.imageIds;
}
/**
* Returns the underlying VTK renderer for direct integration points.
*
* @returns The VTK renderer used by this viewport.
*/
getRenderer() {
return this.renderContext.vtk.renderer;
}
/**
* Returns the viewport canvas element.
*
* @returns The canvas owned by this viewport.
*/
getCanvas(): HTMLCanvasElement {
return this.canvas;
}
/**
* Returns the active VTK camera instance.
*
* @returns The active VTK camera object.
*/
getVtkActiveCamera() {
return this.getRenderer().getActiveCamera();
}
/**
* Returns the current 3D camera state in the compatibility camera shape.
*
* @returns The current 3D camera state.
*/
getViewState(): Volume3DCamera & ICamera {
return this.getRuntimeCamera();
}
setViewState(viewStatePatch: Partial<Volume3DCamera>): void {
if (this.isDestroyed) {
return;
}
const previousCamera = this.getCameraForEvent();
applyVolume3DCamera(this.renderContext, viewStatePatch, {
resetClippingRange: true,
});
this.viewState = this.getRuntimeCamera();
this.modified(previousCamera);
}
protected getRuntimeCamera(): Volume3DCamera & ICamera {
const camera = this.getRenderer().getActiveCamera();
return {
clippingRange: camera.getClippingRange(),
focalPoint: camera.getFocalPoint(),
parallelProjection: camera.getParallelProjection(),
parallelScale: camera.getParallelScale(),
position: camera.getPosition(),
rotation: 0,
viewAngle: camera.getViewAngle(),
viewPlaneNormal: camera.getViewPlaneNormal(),
viewUp: camera.getViewUp(),
} as Volume3DCamera & ICamera;
}
getResolvedView(): Volume3DResolvedView {
return new Volume3DResolvedView({
camera: this.getViewState(),
canvas: this.canvas,
frameOfReferenceUID: this.resolveFrameOfReferenceUID(),
renderer: this.getRenderer(),
});
}
getViewReference(
_viewRefSpecifier: ViewReferenceSpecifier = {}
): ViewReference {
const binding = this.getCurrentBinding();
const data = binding ? this.getVolume3DPayload(binding) : undefined;
const camera = this.getViewState();
const FrameOfReferenceUID = this.getFrameOfReferenceUID();
const cameraFocalPoint = camera.focalPoint as Point3 | undefined;
const viewPlaneNormal = camera.viewPlaneNormal as Point3 | undefined;
const viewUp = camera.viewUp as Point3 | undefined;
const viewReference: ViewReference = {
FrameOfReferenceUID,
dataId: binding?.data.id,
cameraFocalPoint,
viewPlaneNormal,
viewUp,
};
if (data?.renderMode === 'vtkVolume3d') {
viewReference.volumeId = data.volumeId;
Object.assign(
viewReference,
getDimensionGroupReferenceContext(data.imageVolume)
);
}
if (cameraFocalPoint && viewPlaneNormal && viewUp) {
viewReference.planeRestriction = {
FrameOfReferenceUID,
point: cameraFocalPoint,
inPlaneVector1: viewUp,
inPlaneVector2: vec3.cross(
vec3.create(),
viewUp as unknown as vec3,
viewPlaneNormal as unknown as vec3
) as Point3,
};
}
return viewReference;
}
/**
* Returns the primary volume id when the active rendering is volume-backed.
*
* @returns The primary volume id, if one is active.
*/
getVolumeId(): string | undefined {
const binding = this.getCurrentBinding();
if (!binding) {
return;
}
const data = this.getVolume3DPayload(binding);
const rendering = this.getVolume3DRendering(binding);
if (
!data ||
data.renderMode !== 'vtkVolume3d' ||
rendering.renderMode !== 'vtkVolume3d'
) {
return;
}
return data.volumeId;
}
/**
* Returns whether the viewport currently contains the given volume id.
*
* @param volumeId - Volume id to look up in the current actors.
* @returns `true` when a matching volume actor is present.
*/
hasVolumeId(volumeId: string): boolean {
return this.getActors().some(
(actorEntry) => actorEntry.referencedId === volumeId
);
}
/**
* Returns whether any actor reference id contains the given volume URI.
*
* @param volumeURI - Volume URI substring to test against actor references.
* @returns `true` when a matching actor reference is present.
*/
hasVolumeURI(volumeURI: string): boolean {
return this.getActors().some((actorEntry) =>
String(actorEntry.referencedId || '').includes(volumeURI)
);
}
/**
* Returns image data from the current binding when exposed by the render
* path.
*
* @returns The current image-data object, if exposed by the render path.
*/
getImageData(): IImageData | undefined {
return this.getCurrentBinding()?.getImageData?.() as IImageData | undefined;
}
/**
* Returns all actor entries contributed by the active 3D bindings.
*
* @returns Actor entries for all active 3D bindings.
*/
getActors(): ActorEntry[] {
const actors: ActorEntry[] = [];
for (const binding of this.bindings.values()) {
const data = this.getVolume3DPayload(binding);
if (!data) {
continue;
}
actors.push(
...this.getActorEntriesForRendering(
this.getVolume3DRendering(binding),
data
)
);
}
return actors;
}
/**
* Returns the default actor for tool integration and legacy compatibility.
*
* @returns The primary actor entry, if one is available.
*/
getDefaultActor(): ActorEntry | undefined {
const binding = this.getCurrentBinding();
if (!binding) {
return this.getActors()[0];
}
const data = this.getVolume3DPayload(binding);
if (!data) {
return undefined;
}
return this.getActorEntriesForRendering(
this.getVolume3DRendering(binding),
data
)[0];
}
/**
* Resets the VTK-backed view state and clipping range.
*
* Mirrors the legacy `VolumeViewport3D.resetCamera` semantics for the
* options that 3D-camera tooling relies on:
* - `resetZoom: false` keeps the current parallel scale (zoom) instead of
* refitting to bounds. (OrientationControllerTool animates orientation with
* `resetZoom: false`; VolumeCroppingControlTool also forwards options.)
* - `resetPan: false` / `resetToCenter: false` keep the current focal point
* and position rather than recentering on the bounds.
*
* `vtkRenderer.resetCamera()` preserves the current view direction
* (viewPlaneNormal) and viewUp - it only repositions the camera along the
* existing direction of projection and recomputes the parallel scale - so an
* orientation applied via `setViewState` immediately before this call is not
* clobbered.
*
* @param options - Reset options matching the legacy `resetCamera` contract.
* @returns Always `true` for compatibility with legacy viewport contracts.
*/
resetViewState(options?: {
resetPan?: boolean;
resetZoom?: boolean;
resetToCenter?: boolean;
}): boolean {
const {
resetPan = true,
resetZoom = true,
resetToCenter = true,
} = options || {};
const previousCamera = this.getCameraForEvent();
const renderer = this.getRenderer();
const camera = renderer.getActiveCamera();
const previousParallelScale = camera.getParallelScale();
const previousPosition = camera.getPosition();
const previousFocalPoint = camera.getFocalPoint();
renderer.resetCamera();
renderer.resetCameraClippingRange();
// resetCamera() always recomputes the parallel scale (zoom) to fit the
// bounds; restore the previous zoom when the caller opts out.
if (!resetZoom) {
camera.setParallelScale(previousParallelScale);
}
// resetCamera() recenters the focal point on the bounds; restore the
// previous pan/center when the caller opts out of recentering.
if (!resetPan || !resetToCenter) {
camera.setFocalPoint(...previousFocalPoint);
camera.setPosition(...previousPosition);
renderer.resetCameraClippingRange();
}
this.viewState = this.getViewState();
this.render();
this.triggerCameraModifiedEvent(previousCamera);
this.triggerCameraResetEvent();
return true;
}
/**
* Resets the 3D view state after resize using the same behavior as
* `resetViewState`.
*
* @returns Always `true` for compatibility with legacy viewport contracts.
*/
resetViewStateForResize(): boolean {
return this.resetViewState();
}
/**
* Updates cached size state and notifies active render bindings.
*/
resize(): void {
if (this.isDestroyed) {
return;
}
this.sWidth = this.canvas.width;
this.sHeight = this.canvas.height;
this.resizeBindings();
}
/**
* Renders active 3D bindings or queues an engine-driven render.
*/
render(): void {
if (this.isDestroyed) {
return;
}
if (!this.renderBindings()) {
this.requestRenderingEngineRender();
}
}
protected override onDestroy(): void {
this.primaryDataId = undefined;
}
/**
* Resolves the current VTK-backed 3D camera through the projection adapter
* without making the adapter responsible for mutating the viewport.
*/
private getProjectionSnapshot(): Volume3DProjectionSnapshot {
const snapshot = getVolume3DProjectionSnapshot({
viewport: this,
canvasHeight: this.canvas.clientHeight || this.element.clientHeight,
canvasWidth: this.canvas.clientWidth || this.element.clientWidth,
camera: this.getViewState(),
frameOfReferenceUID: this.resolveFrameOfReferenceUID(),
resolvedView: this.getResolvedView(),
});
if (!snapshot) {
throw new Error(
'[VolumeViewport3D] Unable to resolve projection snapshot'
);
}
return snapshot;
}
protected getCurrentBinding() {
if (this.primaryDataId) {
return this.getBinding(this.primaryDataId) ?? this.getFirstBinding();
}
return this.getFirstBinding();
}
protected getReferenceViewContexts(): GenericViewportReferenceContext[] {
const contexts: GenericViewportReferenceContext[] = [];
const camera = this.getViewState();
for (const [dataId, binding] of this.bindings.entries()) {
const data = this.getVolume3DPayload(binding);
const volumeId =
data?.renderMode === 'vtkVolume3d' ? data.volumeId : undefined;
contexts.push({
dataId,
dataIds: [binding.data.id],
frameOfReferenceUID:
binding.getFrameOfReferenceUID() ?? this.getFrameOfReferenceUID(),
imageIds:
data?.renderMode === 'vtkVolume3d' ? data.imageIds : undefined,
volumeId,
volumeIds: volumeId ? [volumeId] : undefined,
cameraFocalPoint: camera.focalPoint as Point3 | undefined,
viewPlaneNormal: camera.viewPlaneNormal as Point3 | undefined,
...(data?.renderMode === 'vtkVolume3d'
? getDimensionGroupReferenceContext(data.imageVolume)
: {}),
});
}
return contexts.length ? contexts : super.getReferenceViewContexts();
}
private requestRenderingEngineRender(): void {
const renderingEngine = renderingEngineCache.get(this.renderingEngineId);
if (renderingEngine) {
renderingEngine.renderViewport(this.id);
}
}
private resolveRenderMode(
dataId: string,
requestedRenderMode: Volume3DSetDataOptions['renderMode'] = 'auto'
): 'vtkVolume3d' | 'vtkGeometry3d' {
if (requestedRenderMode && requestedRenderMode !== 'auto') {
return requestedRenderMode;
}
const dataSet = this.getDataSet(dataId);
if (dataSet?.imageIds?.length) {
return 'vtkVolume3d';
}
return 'vtkGeometry3d';
}
private getDataSet(dataId: string): Volume3DRegisteredDataSet | undefined {
const dataSet = getGenericViewportImageDisplaySet(dataId);
if (!isVolume3DRegisteredDataSet(dataSet)) {
return;
}
return dataSet;
}
private getVolume3DPayload(
binding: ViewportDataBinding<Volume3DDataPresentation>
): LoadedData<Volume3DPayload> | undefined {
if (!isVolume3DData(binding.data)) {
return;
}
return binding.data;
}
private resolveFrameOfReferenceUID(): string | undefined {
const binding = this.getCurrentBinding();
if (!binding) {
return;
}
const data = this.getVolume3DPayload(binding);
const rendering = this.getVolume3DRendering(binding);
if (!data) {
return;
}
if (data.renderMode === 'vtkVolume3d') {
return data.imageVolume.metadata?.FrameOfReferenceUID;
}
if (rendering.renderMode === 'vtkGeometry3d') {
return rendering.frameOfReferenceUID;
}
}
private getVolume3DRendering(
binding: ViewportDataBinding<Volume3DDataPresentation>
): Volume3DRendering {
if (!isVolume3DRendering(binding.rendering)) {
throw new Error(
'[VolumeViewport3D] Binding render mode is not a supported 3D rendering'
);
}
return binding.rendering;
}
private getActorEntriesForRendering(
rendering: Volume3DRendering,
data: LoadedData<Volume3DPayload>
): ActorEntry[] {
if (
rendering.renderMode === 'vtkVolume3d' &&
data.renderMode === 'vtkVolume3d'
) {
return [
{
actor: rendering.actor,
referencedId: data.volumeId,
uid: rendering.actorEntryUID,
},
];
}
if (rendering.renderMode === 'vtkGeometry3d') {
return rendering.actors;
}
return [];
}
}
export default VolumeViewport3D;
function isVolume3DData(data: LoadedData): data is LoadedData<Volume3DPayload> {
if (typeof data !== 'object' || data === null) {
return false;
}
const payload = data as Record<string, unknown>;
return (
(payload.type === 'image' && payload.renderMode === 'vtkVolume3d') ||
(payload.type === 'geometry' && payload.renderMode === 'vtkGeometry3d')
);
}
function isVolume3DRendering(rendering: {
renderMode: string;
}): rendering is Volume3DRendering {
return (
rendering.renderMode === 'vtkVolume3d' ||
rendering.renderMode === 'vtkGeometry3d'
);
}
function isVolume3DRegisteredDataSet(
value: unknown
): value is Volume3DRegisteredDataSet {
if (!isGenericViewportImageDisplaySet(value)) {
return false;
}
return (
(value.geometryId === undefined || typeof value.geometryId === 'string') &&
(value.volumeId === undefined || typeof value.volumeId === 'string') &&
(value.geometryLoadOptions === undefined ||
(typeof value.geometryLoadOptions === 'object' &&
!Array.isArray(value.geometryLoadOptions)))
);
}
|