All files / tools/src/stateManagement/segmentation/labelmapModel labelmapLegacyAdapter.ts

58.33% Statements 14/24
40% Branches 6/15
40% Functions 2/5
58.33% Lines 14/24

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                          480x 360x 360x     120x       120x 120x       120x   120x     120x   120x       120x 120x         120x 120x                                                        
import type { Segmentation } from '../../../types/SegmentationStateTypes';
import {
  ensureLabelmapState,
  getSegmentOrder,
} from './normalizeLabelmapSegmentationData';
import { getLabelmaps } from './labelmapLayerStore';
import { forEachLabelmapImageReference } from './labelmapImageIdMapping';
 
function syncOptionalLegacyProperty<T extends object, K extends keyof T>(
  target: T,
  key: K,
  value: T[K]
): void {
  if (value == null) {
    delete target[key];
    return;
  }
 
  target[key] = value;
}
 
function syncLegacyLabelmapData(segmentation: Segmentation): void {
  const labelmapState = ensureLabelmapState(segmentation);
  Iif (!labelmapState) {
    return;
  }
 
  const firstSegmentIndex = getSegmentOrder(segmentation)[0] ?? 1;
  const primaryLabelmapId =
    labelmapState.primaryLabelmapId ??
    labelmapState.segmentBindings[firstSegmentIndex]?.labelmapId ??
    Object.keys(labelmapState.labelmaps)[0];
  const primaryLayer = labelmapState.labelmaps[primaryLabelmapId];
 
  Iif (!primaryLayer) {
    return;
  }
 
  syncOptionalLegacyProperty(labelmapState, 'volumeId', primaryLayer.volumeId);
  syncOptionalLegacyProperty(
    labelmapState,
    'referencedVolumeId',
    primaryLayer.referencedVolumeId
  );
  syncOptionalLegacyProperty(labelmapState, 'imageIds', primaryLayer.imageIds);
  syncOptionalLegacyProperty(
    labelmapState,
    'referencedImageIds',
    primaryLayer.referencedImageIds
  );
}
 
function getReferencedImageIdToCurrentImageIdMap(
  segmentation: Segmentation
): Map<string, string[]> {
  const map = new Map<string, string[]>();
  getLabelmaps(segmentation).forEach((layer) => {
    forEachLabelmapImageReference(
      layer,
      (referenceImageId, labelmapImageId) => {
        const values = map.get(referenceImageId) ?? [];
        if (!values.includes(labelmapImageId)) {
          values.push(labelmapImageId);
        }
        map.set(referenceImageId, values);
      }
    );
  });
 
  return map;
}
 
export { getReferencedImageIdToCurrentImageIdMap, syncLegacyLabelmapData };