All files / packages/core/src/utilities convertStackToVolumeViewport.ts

0% Statements 0/23
0% Branches 0/4
0% Functions 0/3
0% Lines 0/23

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                                                                                                                                                                                                                                       
import { IStackViewport, IVolumeViewport, Point3 } from '../types';
import { setVolumesForViewports } from '../RenderingEngine/helpers';
import {
  createAndCacheVolume,
  getUnknownVolumeLoaderSchema,
} from '../loaders/volumeLoader';
import { Events, OrientationAxis, ViewportType } from '../enums';
 
/**
 * Converts a stack viewport to a volume viewport.
 *
 * @param params - The parameters for the conversion.
 * @param params.viewport - The stack viewport to convert.
 * @param params.options - The options for the conversion.
 * @param [params.options.volumeId] - The volumeId that will get generated, it should have the volume loader schema defined if not we will use the default one.
 * @param [params.options.viewportId] - The viewportId that will get used for new viewport. If not provided, the stack viewport id will be used.
 * @param [params.options.background] - The background color of the volume viewport.
 * @returns The converted volume viewport.
 */
async function convertStackToVolumeViewport({
  viewport,
  options,
}: {
  viewport: IStackViewport;
  options: {
    volumeId: string;
    viewportId?: string;
    background?: Point3;
    orientation?: OrientationAxis;
  };
}): Promise<IVolumeViewport> {
  const renderingEngine = viewport.getRenderingEngine();
 
  let { volumeId } = options;
 
  // if there is no loader schema for the volume Id, we will use the default one
  // which we can get from the volume loader
  if (volumeId.split(':').length === 1) {
    const schema = getUnknownVolumeLoaderSchema();
    volumeId = `${schema}:${volumeId}`;
  }
 
  const { id, element } = viewport;
  const viewportId = options.viewportId || id;
 
  const imageIds = viewport.getImageIds();
 
  // It is important to keep the camera before enabling the viewport
  const prevCamera = viewport.getCamera();
 
  // this will disable the stack viewport and remove it from the toolGroup
  renderingEngine.enableElement({
    viewportId,
    type: ViewportType.ORTHOGRAPHIC,
    element,
    defaultOptions: {
      background: options.background,
      orientation: options.orientation,
    },
  });
 
  // Ideally here we should be able to just create a local volume and not use the
  //  volume louder but we don't know if the stack is already pre-cached for all its
  // imageIds or not so we just let the loader handle it and we have cache
  // optimizations in place to avoid fetching the same imageId if it is already
  // cached
  const volume = await createAndCacheVolume(volumeId, {
    imageIds,
  });
 
  volume.load();
 
  // we should get the new viewport from the renderingEngine since the stack viewport
  // was disabled and replaced with a volume viewport of the same id
  const volumeViewport = <IVolumeViewport>(
    renderingEngine.getViewport(viewportId)
  );
 
  setVolumesForViewports(
    renderingEngine,
    [
      {
        volumeId,
      },
    ],
    [viewportId]
  );
 
  const volumeViewportNewVolumeHandler = () => {
    volumeViewport.setCamera({
      ...prevCamera,
    });
    volumeViewport.render();
 
    element.removeEventListener(
      Events.VOLUME_VIEWPORT_NEW_VOLUME,
      volumeViewportNewVolumeHandler
    );
  };
 
  const addVolumeViewportNewVolumeListener = () => {
    element.addEventListener(
      Events.VOLUME_VIEWPORT_NEW_VOLUME,
      volumeViewportNewVolumeHandler
    );
  };
 
  addVolumeViewportNewVolumeListener();
 
  volumeViewport.render();
 
  return volumeViewport;
}
 
export { convertStackToVolumeViewport };