All files / extensions/cornerstone/src/services/SegmentationService/backends LegacySegmentationBackend.ts

92% Statements 23/25
75% Branches 6/8
100% Functions 6/6
92% Lines 23/25

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 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140                            204x     204x                       204x                     138x     138x 138x   138x                               60x       60x         60x                   78x 2x 2x     76x 70x     6x             6x             26x       26x                                         177x     177x     177x 177x      
import type { Types as csTypes } from '@cornerstonejs/core';
import {
  Enums as csToolsEnums,
  segmentation as cstSegmentation,
  type Types as cstTypes,
} from '@cornerstonejs/tools';
import { isVolume3DViewportType, isVolumeViewportType } from '../../../utils/getLegacyViewportType';
import type {
  AssembleSegmentationForSEGParams,
  ISegmentationBackend,
  LabelmapAddClassification,
} from './ISegmentationBackend';
import type { ISegmentationServiceInternals } from './ISegmentationServiceInternals';
 
const { Labelmap: LABELMAP, Surface: SURFACE } = csToolsEnums.SegmentationRepresentations;
const {
  state: { updateLabelmapSegmentationImageReferences },
} = cstSegmentation;
 
/**
 * Legacy (default) segmentation backend. Selected when the target viewport is NOT a
 * native GenericViewport (the off path / legacy StackViewport / VolumeViewport).
 * Holds the labelmap-add decision tree verbatim (determine + handleViewportConversion
 * + the stack/volume case handlers) and delegates the servicesManager-coupled work
 * (convertStackToVolumeViewport / attemptStackToVolumeConversion / handleVolumeViewport)
 * back to the service via ISegmentationServiceInternals, so behavior is byte-identical
 * to before the backend split.
 */
export class LegacySegmentationBackend implements ISegmentationBackend {
  constructor(private readonly service: ISegmentationServiceInternals) {}
 
  async classifyAndPrepareLabelmapAdd(
    csViewport: csTypes.IViewport,
    segmentation: cstTypes.Segmentation,
    viewportId: string,
    segmentationId: string,
    // The case handlers return LABELMAP/SURFACE directly (byte-identical to the
    // pre-split handleViewportConversion), so the incoming type is unused here.
    _representationType: csToolsEnums.SegmentationRepresentations
  ): Promise<LabelmapAddClassification> {
    const isVolumeViewport = isVolumeViewportType(csViewport);
    // A missing labelmap representation (stale/partial segmentation state) must not
    // throw on the `'volumeId' in ...` probe; treat it as a non-volume segmentation.
    const labelmapData = segmentation?.representationData?.[LABELMAP];
    const isVolumeSegmentation = !!labelmapData && 'volumeId' in labelmapData;
 
    return isVolumeViewport
      ? this.handleVolumeViewportCase(csViewport, segmentation, isVolumeSegmentation)
      : this.handleStackViewportCase(
          csViewport,
          segmentation,
          isVolumeSegmentation,
          viewportId,
          segmentationId
        );
  }
 
  private async handleVolumeViewportCase(
    csViewport: csTypes.IViewport,
    segmentation: cstTypes.Segmentation,
    isVolumeSegmentation: boolean
  ): Promise<LabelmapAddClassification> {
    Iif (isVolume3DViewportType(csViewport)) {
      return { representationTypeToUse: SURFACE, isConverted: false };
    }
 
    await this.service.handleVolumeViewport(
      csViewport as csTypes.IVolumeViewport,
      segmentation,
      isVolumeSegmentation
    );
    return { representationTypeToUse: LABELMAP, isConverted: false };
  }
 
  private async handleStackViewportCase(
    csViewport: csTypes.IViewport,
    segmentation: cstTypes.Segmentation,
    isVolumeSegmentation: boolean,
    viewportId: string,
    segmentationId: string
  ): Promise<LabelmapAddClassification> {
    if (isVolumeSegmentation) {
      const isConverted = await this.service.convertStackToVolumeViewport(csViewport);
      return { representationTypeToUse: LABELMAP, isConverted };
    }
 
    if (updateLabelmapSegmentationImageReferences(viewportId, segmentationId)) {
      return { representationTypeToUse: LABELMAP, isConverted: false };
    }
 
    const isConverted = await this.service.attemptStackToVolumeConversion(
      csViewport as csTypes.IStackViewport,
      segmentation,
      viewportId,
      segmentationId
    );
 
    return { representationTypeToUse: LABELMAP, isConverted };
  }
 
  assembleSegmentationDataForSEG(
    params: AssembleSegmentationForSEGParams
  ): cstTypes.SegmentationPublicInput {
    const { segmentationId, derivedImageIds, referencedImageIds, label, fallbackLabel, segments } =
      params;
 
    // Single flattened labelmap layer — byte-identical to the pre-split builder in
    // createSegmentationForSEGDisplaySet. Overlap is collapsed (one voxel = one id).
    return {
      segmentationId,
      representation: {
        type: LABELMAP,
        data: {
          imageIds: derivedImageIds,
          referencedImageIds,
        },
      },
      config: {
        label,
        fallbackLabel,
        segments,
      },
    };
  }
 
  jumpToSegmentCenter(viewport: csTypes.IViewport, world: csTypes.Point3): boolean {
    // Byte-identical to the pre-split guarded recenter: legacy stack/volume viewports
    // have jumpToWorld; if absent (e.g. a native viewport reaching the legacy twin),
    // no-op and report it so the caller skips the highlight, exactly as before.
    const legacyViewport = viewport as csTypes.IViewport & {
      jumpToWorld?: (world: csTypes.Point3) => void;
    };
    Iif (!legacyViewport?.jumpToWorld) {
      return false;
    }
    legacyViewport.jumpToWorld(world);
    return true;
  }
}