All files / packages/tools/src/store/ToolGroupManager getToolGroupForViewport.ts

58.33% Statements 7/12
66.66% Branches 6/9
60% Functions 3/5
63.63% Lines 7/11

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                                            1726x           1726x 1865x   2423x         1726x       1726x             1726x        
import { getRenderingEngines } from '@cornerstonejs/core';
import { state } from '../index';
import { IToolGroup } from '../../types';
 
/**
 * Given a rendering engine Id and a viewport Id, return the tool group that
 * contains that rendering engine and viewport. Note: A viewport can only be
 * associated with a single tool group. You cannot have a viewport that belongs
 * to multiple tool groups. To achieve so, create a new viewport and a new toolGroup
 * for it. This will not impact memory usage much as the volume textures are
 * shared across all viewports rendering the same image.
 *
 * @param viewportId - The Id of the viewport that the tool is being
 * added to.
 * @param renderingEngineId - The Id of the rendering engine that the
 * tool group is associated with.
 * @returns A tool group.
 */
function getToolGroupForViewport(
  viewportId: string,
  renderingEngineId?: string
): IToolGroup | undefined {
  Iif (!renderingEngineId) {
    renderingEngineId = getRenderingEngines().find((re) =>
      re.getViewports().find((vp) => vp.id === viewportId)
    )?.id;
  }
 
  const toolGroupFilteredByIds = state.toolGroups.filter((tg) =>
    tg.viewportsInfo.some(
      (vp) =>
        vp.renderingEngineId === renderingEngineId &&
        (!vp.viewportId || vp.viewportId === viewportId)
    )
  );
 
  Iif (!toolGroupFilteredByIds.length) {
    return;
  }
 
  Iif (toolGroupFilteredByIds.length > 1) {
    throw new Error(
      `Multiple tool groups found for renderingEngineId: ${renderingEngineId} and viewportId: ${viewportId}. You should only
      have one tool group per viewport in a renderingEngine.`
    );
  }
 
  return toolGroupFilteredByIds[0];
}
 
export default getToolGroupForViewport;