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 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 | 2x 77x 77x 77x 77x 77x 77x 77x 77x 77x 77x 77x 77x 76x 76x 1x 1x 77x 1x 183x 1x 77x 77x 77x 77x 77x 77x 77x 77x 319x 319x 319x 319x 80x 239x 78x 78x 25x 53x 53x 53x 77x 77x 77x 155x 155x 155x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 4x 4x 4x 1x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 1x 4x 77x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 77x 1x 550x 55x 55x 55x 110x 55x 55x 55x 110x 55x 55x 55x 55x 55x 55x 55x 55x 56x 1x 220x 22x 22x 22x 44x 22x 44x 22x 22x 22x 22x 22x 22x 22x 22x 23x 22x 22x 22x 6x 1x 5x 5x 5x 2x 5x 3x 3x 5x 1x 462x 77x 154x 77x 78x 799x 9x 9x 9x 344x 344x 77x 267x 267x 267x 267x 3x 264x 264x 264x 264x 55x 77x 492x 492x 492x 492x 492x 492x 492x 492x 492x 492x 492x 492x 492x 77x 268x 268x 268x 268x 268x 268x 268x 268x 268x 268x 268x 268x 268x 77x 77x 77x 3x 3x 74x 74x 155x 77x | import vtkVolume from '@kitware/vtk.js/Rendering/Core/Volume'; import vtkColorTransferFunction from '@kitware/vtk.js/Rendering/Core/ColorTransferFunction'; import vtkColorMaps from '@kitware/vtk.js/Rendering/Core/ColorTransferFunction/ColorMaps'; import cache from '../cache'; import { MPR_CAMERA_VALUES, RENDERING_DEFAULTS, VIEWPORT_PRESETS, } from '../constants'; import { BlendModes, Events, OrientationAxis, VOILUTFunctionType, } from '../enums'; import ViewportType from '../enums/ViewportType'; import eventTarget from '../eventTarget'; import { getShouldUseCPURendering } from '../init'; import { loadVolume } from '../loaders/volumeLoader'; import type { ActorEntry, ColormapPublic, FlipDirection, IImageData, IVolumeInput, OrientationVectors, Point2, Point3, VOIRange, VolumeViewportProperties, } from '../types'; import { VoiModifiedEventDetail } from '../types/EventTypes'; import type { ViewportInput } from '../types/IViewport'; import type IVolumeViewport from '../types/IVolumeViewport'; import { actorIsA, applyPreset, createSigmoidRGBTransferFunction, getVoiFromSigmoidRGBTransferFunction, imageIdToURI, invertRgbTransferFunction, triggerEvent, colormap as colormapUtils, } from '../utilities'; import { createVolumeActor } from './helpers'; import volumeNewImageEventDispatcher, { resetVolumeNewImageState, } from './helpers/volumeNewImageEventDispatcher'; import Viewport from './Viewport'; import type { vtkSlabCamera as vtkSlabCameraType } from './vtkClasses/vtkSlabCamera'; import vtkSlabCamera from './vtkClasses/vtkSlabCamera'; import vtkPiecewiseFunction from '@kitware/vtk.js/Common/DataModel/PiecewiseFunction'; /** * Abstract base class for volume viewports. VolumeViewports are used to render * 3D volumes from which various orientations can be viewed. Since VolumeViewports * use SharedVolumeMappers behind the scene, memory footprint of visualizations * of the same volume in different orientations is very small. * * For setting volumes on viewports you need to use {@link addVolumesToViewports} * which will add volumes to the specified viewports. */ abstract class BaseVolumeViewport extends Viewport implements IVolumeViewport { useCPURendering = false; use16BitTexture = false; private _FrameOfReferenceUID: string; private inverted = false; // Viewport Properties // TODO: similar to setVoi, this is only applicable to first volume private VOILUTFunction: VOILUTFunctionType; constructor(props: ViewportInput) { super(props); this.useCPURendering = getShouldUseCPURendering(); this.use16BitTexture = this._shouldUseNativeDataType(); Iif (this.useCPURendering) { throw new Error( 'VolumeViewports cannot be used whilst CPU Fallback Rendering is enabled.' ); } const renderer = this.getRenderer(); const camera = vtkSlabCamera.newInstance(); renderer.setActiveCamera(camera); switch (this.type) { case ViewportType.ORTHOGRAPHIC: camera.setParallelProjection(true); break; case ViewportType.VOLUME_3D: camera.setParallelProjection(true); break; case ViewportType.PERSPECTIVE: camera.setParallelProjection(false); break; default: throw new Error(`Unrecognized viewport type: ${this.type}`); } this.initializeVolumeNewImageEventDispatcher(); } static get useCustomRenderingPipeline(): boolean { return false; } protected applyViewOrientation( orientation: OrientationAxis | OrientationVectors ) { const { viewPlaneNormal, viewUp } = this._getOrientationVectors(orientation); const camera = this.getVtkActiveCamera(); camera.setDirectionOfProjection( -viewPlaneNormal[0], -viewPlaneNormal[1], -viewPlaneNormal[2] ); camera.setViewUpFrom(viewUp); this.resetCamera(); } private initializeVolumeNewImageEventDispatcher(): void { const volumeNewImageHandlerBound = volumeNewImageHandler.bind(this); const volumeNewImageCleanUpBound = volumeNewImageCleanUp.bind(this); function volumeNewImageHandler(cameraEvent) { const { viewportId } = cameraEvent.detail; Iif (viewportId !== this.id || this.isDisabled) { return; } const viewportImageData = this.getImageData(); if (!viewportImageData) { return; } volumeNewImageEventDispatcher(cameraEvent); } function volumeNewImageCleanUp(evt) { const { viewportId } = evt.detail; if (viewportId !== this.id) { return; } this.element.removeEventListener( Events.CAMERA_MODIFIED, volumeNewImageHandlerBound ); eventTarget.removeEventListener( Events.ELEMENT_DISABLED, volumeNewImageCleanUpBound ); resetVolumeNewImageState(viewportId); } this.element.removeEventListener( Events.CAMERA_MODIFIED, volumeNewImageHandlerBound ); this.element.addEventListener( Events.CAMERA_MODIFIED, volumeNewImageHandlerBound ); eventTarget.addEventListener( Events.ELEMENT_DISABLED, volumeNewImageCleanUpBound ); } protected resetVolumeViewportClippingRange() { const activeCamera = this.getVtkActiveCamera(); Eif (activeCamera.getParallelProjection()) { activeCamera.setClippingRange( -RENDERING_DEFAULTS.MAXIMUM_RAY_DISTANCE, RENDERING_DEFAULTS.MAXIMUM_RAY_DISTANCE ); } else { activeCamera.setClippingRange( RENDERING_DEFAULTS.MINIMUM_SLAB_THICKNESS, RENDERING_DEFAULTS.MAXIMUM_RAY_DISTANCE ); } } /** * Sets the properties for the volume viewport on the volume * Sets the VOILUTFunction property for the volume viewport on the volume * * @param VOILUTFunction - Sets the voi mode (LINEAR or SAMPLED_SIGMOID) * @param volumeId - The volume id to set the properties for (if undefined, the first volume) * @param suppressEvents - If true, the viewport will not emit events */ private setVOILUTFunction( voiLUTFunction: VOILUTFunctionType, volumeId?: string, suppressEvents?: boolean ): void { // make sure the VOI LUT function is valid in the VOILUTFunctionType which is enum if (Object.values(VOILUTFunctionType).indexOf(voiLUTFunction) === -1) { voiLUTFunction = VOILUTFunctionType.LINEAR; } const { voiRange } = this.getProperties(); this.VOILUTFunction = voiLUTFunction; this.setVOI(voiRange, volumeId, suppressEvents); } /** * Sets the colormap for the volume with the given ID and optionally suppresses events. * * @param colormap - The colormap to apply (e.g., "hsv"). * @param volumeId - The ID of the volume to set the colormap for. * @param suppressEvents - If `true`, events will not be emitted during the colormap a * * @returns void */ private setColormap( colormap: ColormapPublic, volumeId: string, suppressEvents?: boolean ) { const applicableVolumeActorInfo = this._getApplicableVolumeActor(volumeId); if (!applicableVolumeActorInfo) { return; } const { volumeActor } = applicableVolumeActorInfo; const mapper = volumeActor.getMapper(); mapper.setSampleDistance(1.0); const cfun = vtkColorTransferFunction.newInstance(); let colormapObj = colormapUtils.getColormap(colormap); const { name, opacityMapping } = colormap; if (!colormapObj) { colormapObj = vtkColorMaps.getPresetByName(name); } if (!colormapObj) { throw new Error(`Colormap ${colormap} not found`); } const range = volumeActor .getProperty() .getRGBTransferFunction(0) .getRange(); cfun.applyColorMap(colormapObj); cfun.setMappingRange(range[0], range[1]); volumeActor.getProperty().setRGBTransferFunction(0, cfun); const ofun = vtkPiecewiseFunction.newInstance(); ofun.addPoint(range[0], 0.0); ofun.addPoint(range[1], 1.0); volumeActor.getProperty().setScalarOpacity(0, ofun); if (!opacityMapping) { return; } // add custom opacity points opacityMapping.forEach(({ opacity, value }) => { ofun.addPoint(value, opacity); }); } /** * Sets the inversion for the volume transfer function * * @param invert - Should the transfer function be inverted? * @param volumeId - volumeId * @param suppressEvents - If `true`, events will not be published * * @returns void */ private setInvert( invert: boolean, volumeId?: string, suppressEvents?: boolean ) { const applicableVolumeActorInfo = this._getApplicableVolumeActor(volumeId); Iif (!applicableVolumeActorInfo) { return; } const volumeIdToUse = applicableVolumeActorInfo.volumeId; const cfun = this._getOrCreateColorTransferFunction(volumeIdToUse); invertRgbTransferFunction(cfun); this.inverted = invert; const { voiRange } = this.getProperties(); Eif (!suppressEvents) { const eventDetail: VoiModifiedEventDetail = { viewportId: this.id, range: voiRange, volumeId: volumeIdToUse, VOILUTFunction: this.VOILUTFunction, invert: this.inverted, }; triggerEvent(this.element, Events.VOI_MODIFIED, eventDetail); } } private _getOrCreateColorTransferFunction( volumeId: string ): vtkColorTransferFunction { const applicableVolumeActorInfo = this._getApplicableVolumeActor(volumeId); Iif (!applicableVolumeActorInfo) { return null; } const { volumeActor } = applicableVolumeActorInfo; const rgbTransferFunction = volumeActor .getProperty() .getRGBTransferFunction(0); Eif (rgbTransferFunction) { return rgbTransferFunction; } const newRGBTransferFunction = vtkColorTransferFunction.newInstance(); volumeActor.getProperty().setRGBTransferFunction(0, newRGBTransferFunction); return newRGBTransferFunction; } /** * Sets the properties for the volume viewport on the volume * (if fusion, it sets it for the first volume in the fusion) * * @param voiRange - Sets the lower and upper voi * @param volumeId - The volume id to set the properties for (if undefined, the first volume) * @param suppressEvents - If true, the viewport will not emit events */ private setVOI( voiRange: VOIRange, volumeId?: string, suppressEvents = false ): void { const applicableVolumeActorInfo = this._getApplicableVolumeActor(volumeId); if (!applicableVolumeActorInfo) { return; } const { volumeActor } = applicableVolumeActorInfo; const volumeIdToUse = applicableVolumeActorInfo.volumeId; let voiRangeToUse = voiRange; Iif (typeof voiRangeToUse === 'undefined') { const imageData = volumeActor.getMapper().getInputData(); const range = imageData.getPointData().getScalars().getRange(); const maxVoiRange = { lower: range[0], upper: range[1] }; voiRangeToUse = maxVoiRange; } // scaling logic here // https://github.com/Kitware/vtk-js/blob/c6f2e12cddfe5c0386a73f0793eb6d9ab20d573e/Sources/Rendering/OpenGL/VolumeMapper/index.js#L957-L972 Iif (this.VOILUTFunction === VOILUTFunctionType.SAMPLED_SIGMOID) { const cfun = createSigmoidRGBTransferFunction(voiRangeToUse); volumeActor.getProperty().setRGBTransferFunction(0, cfun); } else { // TODO: refactor and make it work for PET series (inverted/colormap) // const cfun = createLinearRGBTransferFunction(voiRangeToUse); // volumeActor.getProperty().setRGBTransferFunction(0, cfun); // Todo: Moving from LINEAR to SIGMOID and back to LINEAR will not // work until we implement it in a different way because the // LINEAR transfer function is not recreated. const { lower, upper } = voiRangeToUse; volumeActor .getProperty() .getRGBTransferFunction(0) .setRange(lower, upper); } Eif (!suppressEvents) { const eventDetail: VoiModifiedEventDetail = { viewportId: this.id, range: voiRange, volumeId: volumeIdToUse, VOILUTFunction: this.VOILUTFunction, }; triggerEvent(this.element, Events.VOI_MODIFIED, eventDetail); } } /** * Sets the properties for the volume viewport on the volume * (if fusion, it sets it for the first volume in the fusion) * * @param VolumeViewportProperties - The properties to set * @param [VolumeViewportProperties.voiRange] - Sets the lower and upper voi * @param [VolumeViewportProperties.VOILUTFunction] - Sets the voi mode (LINEAR, or SAMPLED_SIGMOID) * @param [VolumeViewportProperties.invert] - Inverts the color transfer function * @param [VolumeViewportProperties.colormap] - Sets the colormap * @param [VolumeViewportProperties.preset] - Sets the colormap * @param volumeId - The volume id to set the properties for (if undefined, the first volume) * @param suppressEvents - If true, the viewport will not emit events */ public setProperties( { voiRange, VOILUTFunction, invert, colormap, preset, }: VolumeViewportProperties = {}, volumeId?: string, suppressEvents = false ): void { // Note: colormap should always be done first, since we can then // modify the voiRange Iif (colormap !== undefined) { this.setColormap(colormap, volumeId, suppressEvents); } Eif (voiRange !== undefined) { this.setVOI(voiRange, volumeId, suppressEvents); } Iif (VOILUTFunction !== undefined) { this.setVOILUTFunction(VOILUTFunction, volumeId, suppressEvents); } if (invert !== undefined && this.inverted !== invert) { this.setInvert(invert, volumeId, suppressEvents); } Iif (preset !== undefined) { this.setPreset(preset, volumeId, suppressEvents); } } /** * Sets the specified preset for the volume with the given ID * * @param presetName - The name of the preset to apply (e.g., "CT-Bone"). * @param volumeId - The ID of the volume to set the preset for. * @param suppressEvents - If `true`, events will not be emitted during the preset application. * * @returns void */ private setPreset(presetNameOrObj, volumeId, suppressEvents) { const applicableVolumeActorInfo = this._getApplicableVolumeActor(volumeId); if (!applicableVolumeActorInfo) { return; } const { volumeActor } = applicableVolumeActorInfo; let preset = presetNameOrObj; if (typeof preset === 'string') { preset = VIEWPORT_PRESETS.find((preset) => { return preset.name === presetNameOrObj; }); } if (!preset) { return; } applyPreset(volumeActor, preset); triggerEvent(this.element, Events.PRESET_MODIFIED, { viewportId: this.id, volumeId: applicableVolumeActorInfo.volumeId, actor: volumeActor, presetName: preset.name, }); } /** * Retrieve the viewport properties * @returns viewport properties including voi, interpolation type: TODO: slabThickness, invert, rotation, flip */ public getProperties = (): VolumeViewportProperties => { const voiRanges = this.getActors() .map((actorEntry) => { const volumeActor = actorEntry.actor as vtkVolume; const volumeId = actorEntry.uid; const volume = cache.getVolume(volumeId); Iif (!volume) return null; const cfun = volumeActor.getProperty().getRGBTransferFunction(0); const [lower, upper] = this.VOILUTFunction === 'SIGMOID' ? getVoiFromSigmoidRGBTransferFunction(cfun) : // @ts-ignore cfun.getRange(); return { volumeId, voiRange: { lower, upper } }; }) .filter(Boolean); const voiRange = voiRanges.length ? voiRanges[0].voiRange : null; const VOILUTFunction = this.VOILUTFunction; return { voiRange, VOILUTFunction, invert: this.inverted }; }; /** * Creates volume actors for all volumes defined in the `volumeInputArray`. * For each entry, if a `callback` is supplied, it will be called with the new volume actor as input. * For each entry, if a `blendMode` and/or `slabThickness` is defined, this will be set on the actor's * `VolumeMapper`. * * @param volumeInputArray - The array of `VolumeInput`s which define the volumes to add. * @param immediate - Whether the `Viewport` should be rendered as soon as volumes are added. */ public async setVolumes( volumeInputArray: Array<IVolumeInput>, immediate = false, suppressEvents = false ): Promise<void> { const firstImageVolume = cache.getVolume(volumeInputArray[0].volumeId)E; if (!firstImageVolume) { throw new Error( `imageVolume with id: ${firstImageVolume.volumeId} does not exist` ); } const FrameOfReferenceUID = firstImageVolume.metadata.FrameOfReferenceUID; await this._isValidVolumeInputArray(volumeInputArray, FrameOfReferenceUID); this._FrameOfReferenceUID = FrameOfReferenceUID; const volumeActors = []; // One actor per volume for (let i = 0; i < volumeInputArray.length; i++) { const { volumeId, actorUID, slabThickness } = volumeInputArray[i]; const actor = await createVolumeActor( volumeInputArray[i], this.element, this.id, suppressEvents, this.use16BitTexture ); // We cannot use only volumeId since then we cannot have for instance more // than one representation of the same volume (since actors would have the // same name, and we don't allow that) AND We cannot use only any uid, since // we rely on the volume in the cache for mapper. So we prefer actorUID if // it is defined, otherwise we use volumeId for the actor name. const uid = actorUID || volumeId; volumeActors.push({ uid, actor, slabThickness, referenceId: volumeId, }); } this._setVolumeActors(volumeActors); triggerEvent(this.element, Events.VOLUME_VIEWPORT_NEW_VOLUME, { viewportId: this.id, volumeActors, }); Iif (immediate) { this.render(); } } /** * Creates and adds volume actors for all volumes defined in the `volumeInputArray`. * For each entry, if a `callback` is supplied, it will be called with the new volume actor as input. * * @param volumeInputArray - The array of `VolumeInput`s which define the volumes to add. * @param immediate - Whether the `Viewport` should be rendered as soon as volumes are added. */ public async addVolumes( volumeInputArray: Array<IVolumeInput>, immediate = false, suppressEvents = false ): Promise<void> { const firstImageVolume = cache.getVolume(volumeInputArray[0].volumeId)E; if (!firstImageVolume) { throw new Error( `imageVolume with id: ${firstImageVolume.volumeId} does not exist` ); } const volumeActors = []; await this._isValidVolumeInputArray( volumeInputArray, this._FrameOfReferenceUID ); // One actor per volume for (let i = 0; i < volumeInputArray.length; i++) { const { volumeId, visibility, actorUID, slabThickness } = volumeInputArray[i]; const actor = await createVolumeActor( volumeInputArray[i], this.element, this.id, suppressEvents, this.use16BitTexture ); Iif (visibility === false) { actor.setVisibility(false); } // We cannot use only volumeId since then we cannot have for instance more // than one representation of the same volume (since actors would have the // same name, and we don't allow that) AND We cannot use only any uid, since // we rely on the volume in the cache for mapper. So we prefer actorUID if // it is defined, otherwise we use volumeId for the actor name. const uid = actorUID || volumeId; volumeActors.push({ uid, actor, slabThickness, // although the actor UID is defined, we need to use the volumeId for the // referenceId, since the actor UID is used to reference the actor in the // viewport, however, the actor is created from its volumeId // and if later we need to grab the referenced volume from cache, // we can use the referenceId to get the volume from the cache referenceId: volumeId, }); } this.addActors(volumeActors); Iif (immediate) { // render this.render(); } } /** * It removes the volume actor from the Viewport. If the volume actor is not in * the viewport, it does nothing. * @param actorUIDs - Array of actor UIDs to remove. In case of simple volume it will * be the volume Id, but in case of Segmentation it will be `{volumeId}-{representationType}` * since the same volume can be rendered in multiple representations. * @param immediate - If true, the Viewport will be rendered immediately */ public removeVolumeActors(actorUIDs: Array<string>, immediate = false): void { // Todo: This is actually removeActors this.removeActors(actorUIDs); Iif (immediate) { this.render(); } } /** * It sets the orientation for the camera, the orientation can be one of the * following: axial, sagittal, coronal, default. Use the Enums.OrientationAxis * to set the orientation. The "default" orientation is the orientation that * the volume was acquired in (scan axis) * * @param orientation - The orientation to set the camera to. * @param immediate - Whether the `Viewport` should be rendered as soon as the camera is set. */ public setOrientation(orientation: OrientationAxis, immediate = true): void { console.warn('Method "setOrientation" needs implementation'); } private _getApplicableVolumeActor(volumeId?: string) { if (volumeId !== undefined && !this.getActor(volumeId)) { return; } const actorEntries = this.getActors(); Iif (!actorEntries.length) { return; } let volumeActor; if (volumeId) { volumeActor = this.getActor(volumeId)?.actor as vtkVolume; } // // set it for the first volume (if there are more than one - fusion) if (!volumeActor) { volumeActor = actorEntries[0].actor as vtkVolume; volumeId = actorEntries[0].uid; } return { volumeActor, volumeId }; } private async _isValidVolumeInputArray( volumeInputArray: Array<IVolumeInput>, FrameOfReferenceUID: string ): Promise<boolean> { const numVolumes = volumeInputArray.length; // Check all other volumes exist and have the same FrameOfReference for (let i = 1E; i < numVolumes; i++) { const volumeInput = volumeInputArray[i]; const imageVolume = await loadVolume(volumeInput.volumeId); if (!imageVolume) { throw new Error( `imageVolume with id: ${imageVolume.volumeId} does not exist` ); } if (FrameOfReferenceUID !== imageVolume.metadata.FrameOfReferenceUID) { throw new Error( `Volumes being added to viewport ${this.id} do not share the same FrameOfReferenceUID. This is not yet supported` ); } } return true; } /** * gets the visible bounds of the viewport in the world coordinate system */ public getBounds(): number[] { const renderer = this.getRenderer(); const bounds = renderer.computeVisiblePropBounds(); return bounds; } /** * Flip the viewport along the desired axis * @param flipDirection - FlipDirection */ public flip(flipDirection: FlipDirection): void { super.flip(flipDirection); } public getFrameOfReferenceUID = (): string => { return this._FrameOfReferenceUID; }; /** * Checks if the viewport has a volume actor with the given volumeId * @param volumeId - the volumeId to look for * @returns Boolean indicating if the volume is present in the viewport */ public hasVolumeId(volumeId: string): boolean { // Note: this assumes that the uid of the volume is the same as the volumeId // which is not guaranteed to be the case for SEG. const actorEntries = this.getActors(); return actorEntries.some((actorEntry) => { return actorEntry.uid === volumeId; }); } /** * Returns the image and its properties that is being shown inside the * stack viewport. It returns, the image dimensions, image direction, * image scalar data, vtkImageData object, metadata, and scaling (e.g., PET suvbw) * Note: since the volume viewport supports fusion, to get the * image data for a specific volume, use the optional volumeId * argument. * * @param volumeId - The volumeId of the volume to get the image for. * @returns IImageData: {dimensions, direction, scalarData, vtkImageData, metadata, scaling} */ public getImageData(volumeId?: string): IImageData | undefined { const defaultActor = this.getDefaultActor(); if (!defaultActor) { return; } const { uid: defaultActorUID } = defaultActor; volumeId = volumeId ?? defaultActorUID; const actorEntry = this.getActor(volumeId); if (!actorIsA(actorEntry, 'vtkVolume')) { return; } const actor = actorEntry.actor; const volume = cache.getVolume(volumeId); const vtkImageData = actor.getMapper().getInputData(); return { dimensions: vtkImageData.getDimensions(), spacing: vtkImageData.getSpacing(), origin: vtkImageData.getOrigin(), direction: vtkImageData.getDirection(), scalarData: vtkImageData.getPointData().getScalars().isDeleted() ? null : vtkImageData.getPointData().getScalars().getData(), imageData: actor.getMapper().getInputData(), metadata: { Modality: volume?.metadata?.Modality, }, scaling: volume?.scaling, hasPixelSpacing: true, }; } /** * Attaches the volume actors to the viewport. * * @param volumeActorEntries - The volume actors to add the viewport. * */ private _setVolumeActors(volumeActorEntries: Array<ActorEntry>): void { this.setActors(volumeActorEntries); } /** * canvasToWorld Returns the world coordinates of the given `canvasPos` * projected onto the plane defined by the `Viewport`'s `vtkCamera`'s focal point * and the direction of projection. * * @param canvasPos - The position in canvas coordinates. * @returns The corresponding world coordinates. * @public */ public canvasToWorld = (canvasPos: Point2): Point3 => { const vtkCamera = this.getVtkActiveCamera() as vtkSlabCameraType; /** * NOTE: this is necessary because we want the coordinate transformation * respect to the view plane (plane orthogonal to the camera and passing to * the focal point). * * When vtk.js computes the coordinate transformations, it simply uses the * camera matrix (no ray casting). * * However for the volume viewport the clipping range is set to be * (-RENDERING_DEFAULTS.MAXIMUM_RAY_DISTANCE, RENDERING_DEFAULTS.MAXIMUM_RAY_DISTANCE). * The clipping range is used in the camera method getProjectionMatrix(). * The projection matrix is used then for viewToWorld/worldToView methods of * the renderer. This means that vkt.js will not return the coordinates of * the point on the view plane (i.e. the depth coordinate will correspond * to the focal point). * * Therefore the clipping range has to be set to (distance, distance + 0.01), * where now distance is the distance between the camera position and focal * point. This is done internally, in our camera customization when the flag * isPerformingCoordinateTransformation is set to true. */ vtkCamera.setIsPerformingCoordinateTransformation?.(true); const renderer = this.getRenderer(); const offscreenMultiRenderWindow = this.getRenderingEngine().offscreenMultiRenderWindow; const openGLRenderWindow = offscreenMultiRenderWindow.getOpenGLRenderWindow(); const size = openGLRenderWindow.getSize(); const devicePixelRatio = window.devicePixelRatio || 1; const canvasPosWithDPR = [ canvasPos[0] * devicePixelRatio, canvasPos[1] * devicePixelRatio, ]; const displayCoord = [ canvasPosWithDPR[0] + this.sx, canvasPosWithDPR[1] + this.sy, ]; // The y axis display coordinates are inverted with respect to canvas coords displayCoord[1] = size[1] - displayCoord[1]; const worldCoord = openGLRenderWindow.displayToWorld( displayCoord[0], displayCoord[1], 0, renderer ); vtkCamera.setIsPerformingCoordinateTransformation?.(false); return [worldCoord[0], worldCoord[1], worldCoord[2]]; }; /** * Returns the canvas coordinates of the given `worldPos` * projected onto the `Viewport`'s `canvas`. * * @param worldPos - The position in world coordinates. * @returns The corresponding canvas coordinates. * @public */ public worldToCanvas = (worldPos: Point3): Point2 => { const vtkCamera = this.getVtkActiveCamera() as vtkSlabCameraType; /** * NOTE: this is necessary because we want the coordinate trasformation * respect to the view plane (plane orthogonal to the camera and passing to * the focal point). * * When vtk.js computes the coordinate transformations, it simply uses the * camera matrix (no ray casting). * * However for the volume viewport the clipping range is set to be * (-RENDERING_DEFAULTS.MAXIMUM_RAY_DISTANCE, RENDERING_DEFAULTS.MAXIMUM_RAY_DISTANCE). * The clipping range is used in the camera method getProjectionMatrix(). * The projection matrix is used then for viewToWorld/worldToView methods of * the renderer. This means that vkt.js will not return the coordinates of * the point on the view plane (i.e. the depth coordinate will corresponded * to the focal point). * * Therefore the clipping range has to be set to (distance, distance + 0.01), * where now distance is the distance between the camera position and focal * point. This is done internally, in our camera customization when the flag * isPerformingCoordinateTransformation is set to true. */ vtkCamera.setIsPerformingCoordinateTransformation?.(true); const renderer = this.getRenderer(); const offscreenMultiRenderWindow = this.getRenderingEngine().offscreenMultiRenderWindow; const openGLRenderWindow = offscreenMultiRenderWindow.getOpenGLRenderWindow(); const size = openGLRenderWindow.getSize(); const displayCoord = openGLRenderWindow.worldToDisplay( ...worldPos, renderer ); // The y axis display coordinates are inverted with respect to canvas coords displayCoord[1] = size[1] - displayCoord[1]; const canvasCoord = <Point2>[ displayCoord[0] - this.sx, displayCoord[1] - this.sy, ]; const devicePixelRatio = window.devicePixelRatio || 1; const canvasCoordWithDPR = <Point2>[ canvasCoord[0] / devicePixelRatio, canvasCoord[1] / devicePixelRatio, ]; vtkCamera.setIsPerformingCoordinateTransformation?.(false); return canvasCoordWithDPR; }; /* * Checking if the imageURI is in the volumes that are being * rendered by the viewport. imageURI is the imageId without the schema * for instance for the imageId of wadors:http://..., the http://... is the imageURI. * Why we don't check the imageId is because the same image can be shown in * another viewport (StackViewport) with a different schema * * @param imageURI - The imageURI to check * @returns True if the imageURI is in the volumes that are being rendered by the viewport */ public hasImageURI = (imageURI: string): boolean => { const volumeActors = this.getActors().filter((actorEntry) => actorIsA(actorEntry, 'vtkVolume') ); return volumeActors.some(({ uid }) => { const volume = cache.getVolume(uid); if (!volume || !volume.imageIds) { return false; } const volumeImageURIs = volume.imageIds.map(imageIdToURI); return volumeImageURIs.includes(imageURI); }); }; protected _getOrientationVectors( orientation: OrientationAxis | OrientationVectors ): OrientationVectors { if (typeof orientation === 'object') { Eif (orientation.viewPlaneNormal && orientation.viewUp) { return orientation; } else { throw new Error( 'Invalid orientation object. It must contain viewPlaneNormal and viewUp' ); } } else Eif ( typeof orientation === 'string' && MPR_CAMERA_VALUES[orientation] ) { return MPR_CAMERA_VALUES[orientation]; } else { throw new Error( `Invalid orientation: ${orientation}. Valid orientations are: ${Object.keys( MPR_CAMERA_VALUES ).join(', ')}` ); } } /** * Reset the camera for the volume viewport */ resetCamera( resetPan?: boolean, resetZoom?: boolean, resetToCenter?: boolean ): boolean { return super.resetCamera(resetPan, resetZoom, resetToCenter); } getCurrentImageIdIndex = (): number => { throw new Error('Method not implemented.'); }; getCurrentImageId = (): string => { throw new Error('Method not implemented.'); }; getIntensityFromWorld(point: Point3): number { throw new Error('Method not implemented.'); } setBlendMode( blendMode: BlendModes, filterActorUIDs?: string[], immediate?: boolean ): void { throw new Error('Method not implemented.'); } setSlabThickness(slabThickness: number, filterActorUIDs?: string[]): void { throw new Error('Method not implemented.'); } getSlabThickness(): number { throw new Error('Method not implemented.'); } } export default BaseVolumeViewport; |