All files / extensions/cornerstone/src/utils createSegmentationForViewport.ts

100% Statements 14/14
72.72% Branches 8/11
100% Functions 2/2
100% Lines 14/14

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            1x                                                                 1x 1x 1x   1x     1x   1x   1x 1x   1x   1x             1x         1x         1x    
import { utilities as csUtils } from '@cornerstonejs/core';
import { SegmentationRepresentations } from '@cornerstonejs/tools/enums';
import i18n from '@ohif/i18n';
import { ServicesManager } from '@ohif/core';
 
function _createDefaultSegments(createInitialSegment?: boolean) {
  return createInitialSegment
    ? {
        1: {
          label: `${i18n.t('Tools:Segment')} 1`,
          active: true,
        },
      }
    : {};
}
 
type CreateSegmentationForViewportOptions = {
  displaySetInstanceUID?: string;
  label?: string;
  segmentationId?: string;
  createInitialSegment?: boolean;
};
 
type CreateSegmentationForViewportParams = {
  viewportId: string;
  options?: CreateSegmentationForViewportOptions;
  segmentationType: SegmentationRepresentations;
};
 
/**
 * Creates a segmentation for the active viewport
 *
 * The created segmentation will be registered as a display set and also added
 * as a segmentation representation to the viewport.
 */
export async function createSegmentationForViewport(
  servicesManager: ServicesManager,
  { viewportId, options = {}, segmentationType }: CreateSegmentationForViewportParams
): Promise<string> {
  const { viewportGridService, displaySetService, segmentationService } = servicesManager.services;
  const { viewports } = viewportGridService.getState();
  const targetViewportId = viewportId;
 
  const viewport = viewports.get(targetViewportId);
 
  // Todo: add support for multiple display sets
  const displaySetInstanceUID = options.displaySetInstanceUID || viewport.displaySetInstanceUIDs[0];
 
  const segs = segmentationService.getSegmentations();
 
  const label = options.label || `${i18n.t('Tools:Segmentation')} ${segs.length + 1}`;
  const segmentationId = options.segmentationId || `${csUtils.uuidv4()}`;
 
  const displaySet = displaySetService.getDisplaySetByUID(displaySetInstanceUID);
 
  const segmentationCreationOptions = {
    label,
    segmentationId,
    segments: _createDefaultSegments(options.createInitialSegment),
  };
 
  // This will create the segmentation and register it as a display set
  const generatedSegmentationId = await (segmentationType === SegmentationRepresentations.Labelmap
    ? segmentationService.createLabelmapForDisplaySet(displaySet, segmentationCreationOptions)
    : segmentationService.createContourForDisplaySet(displaySet, segmentationCreationOptions));
 
  // Also add the segmentation representation to the viewport
  await segmentationService.addSegmentationRepresentation(viewportId, {
    segmentationId,
    type: segmentationType,
  });
 
  return generatedSegmentationId;
}