All files / packages/core/src/RenderingEngine/helpers volumeNewImageEventDispatcher.ts

86.95% Statements 20/23
80% Branches 8/10
100% Functions 2/2
85% Lines 17/20

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                        1x     51x 29x                                 233x 233x 233x   233x           233x 33x     233x   233x             233x   233x 127x     106x   106x             106x        
import {
  getImageSliceDataForVolumeViewport,
  triggerEvent,
} from '../../utilities';
import { EventTypes } from '../../types';
import { Events } from '../../enums';
import { getRenderingEngine } from '../getRenderingEngine';
import BaseVolumeViewport from '../BaseVolumeViewport';
 
// Keeping track of previous imageIndex for each viewportId
type VolumeImageState = Record<string, number>;
 
const state: VolumeImageState = {};
 
export function resetVolumeNewImageState(viewportId: string): void {
  if (state[viewportId] !== undefined) {
    delete state[viewportId];
  }
}
 
/**
 * It captures the camera modified event and with the camera focal point and viewPlaneNomad
 * it calculates the image index in the view direction. Finally it triggers
 * a VOLUME_NEW_IMAGE event with the image index.
 *
 * @internal
 *
 * @param cameraEvent - The camera modified event
 * @param viewportImageData - The image data of the viewport
 */
function volumeNewImageEventDispatcher(
  cameraEvent: EventTypes.CameraModifiedEvent
): void {
  const { renderingEngineId, viewportId } = cameraEvent.detail;
  const renderingEngine = getRenderingEngine(renderingEngineId);
  const viewport = renderingEngine.getViewport(viewportId);
 
  Iif (!(viewport instanceof BaseVolumeViewport)) {
    throw new Error(
      `volumeNewImageEventDispatcher: viewport is not a BaseVolumeViewport`
    );
  }
 
  if (state[viewport.id] === undefined) {
    state[viewport.id] = 0;
  }
 
  const sliceData = getImageSliceDataForVolumeViewport(viewport);
 
  Iif (!sliceData) {
    console.warn(
      `volumeNewImageEventDispatcher: sliceData is undefined for viewport ${viewport.id}`
    );
    return;
  }
 
  const { numberOfSlices, imageIndex } = sliceData;
 
  if (state[viewport.id] === imageIndex) {
    return;
  }
 
  state[viewport.id] = imageIndex;
 
  const eventDetail: EventTypes.VolumeNewImageEventDetail = {
    imageIndex,
    viewportId,
    renderingEngineId,
    numberOfSlices,
  };
 
  triggerEvent(viewport.element, Events.VOLUME_NEW_IMAGE, eventDetail);
}
 
export default volumeNewImageEventDispatcher;