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

93.1% Statements 27/29
78.57% Branches 22/28
100% Functions 8/8
94.44% Lines 17/18

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                                22x     308x 22x 22x   22x 132x 22x   44x 22x       44x     154x           110x 22x   22x 44x   44x   1x      
import { VolumeViewport } from '../';
import BaseVolumeViewport from '../BaseVolumeViewport';
import type { IVolumeInput, IRenderingEngine } from '../../types';
 
/**
 * For each provided viewport it adds a volume to the viewport using the
 * provided renderingEngine
 *
 *
 * @param renderingEngine - The rendering engine to use to get viewports from
 * @param volumeInputs - Array of volume inputs including volumeId. Other properties
 * such as visibility, callback, blendMode, slabThickness are optional
 * @param viewportIds - Array of viewport IDs to add the volume to
 * @param immediateRender - If true, the volumes will be rendered immediately
 * @returns A promise that resolves when all volumes have been added
 */
async function addVolumesToViewports(
  renderingEngine: IRenderingEngine,
  volumeInputs: Array<IVolumeInput>,
  viewportIds: Array<string>,
  immediateRender = false,
  suppressEvents = false
): Promise<void> {
  // Check if all viewports are volumeViewports
  for (const viewportId of viewportIds) {
    const viewport = renderingEngine.getViewport(viewportId)E;
 
    if (!viewport) {
      throw new Error(`Viewport with Id ${viewportId} does not exist`)E;
    }
 
    // if not instance of BaseVolumeViewport, throw
    if (!(viewport instanceof BaseVolumeViewport)) {
      console.warn(
        `Viewport with Id ${viewportId} is not a BaseVolumeViewport. Cannot add volume to this viewport.`
      );
 
      return;
    }
  }
 
  const addVolumePromises = viewportIds.map(async (viewportId) => {
    const viewport = renderingEngine.getViewport(viewportId) as VolumeViewport;
 
    await viewport.addVolumes(volumeInputs, immediateRender, suppressEvents);
  });
 
  await Promise.all(addVolumePromises);
  return;
}
 
export default addVolumesToViewports;