All files / polymorphic-segmentation/src/Surface convertLabelmapToSurface.ts

23.52% Statements 4/17
0% Branches 0/2
0% Functions 0/3
23.52% Lines 4/17

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                        428x   428x   428x   428x                                                                                                                                        
import type { Types } from '@cornerstonejs/core';
import {
  cache,
  eventTarget,
  getWebWorkerManager,
  triggerEvent,
  Enums,
} from '@cornerstonejs/core';
 
import * as cornerstoneTools from '@cornerstonejs/tools';
import type { Types as ToolsTypes } from '@cornerstonejs/tools';
 
const { WorkerTypes } = cornerstoneTools.Enums;
const { computeVolumeLabelmapFromStack } =
  cornerstoneTools.utilities.segmentation;
 
const workerManager = getWebWorkerManager();
 
const triggerWorkerProgress = (eventTarget, progress, id) => {
  triggerEvent(eventTarget, Enums.Events.WEB_WORKER_PROGRESS, {
    progress,
    type: WorkerTypes.POLYSEG_LABELMAP_TO_SURFACE,
    id,
  });
};
 
/**
 * Converts a labelmap representation to a surface representation.
 *
 * @param labelmapRepresentationData - The labelmap segmentation data.
 * @param segmentIndex - The index of the segment to convert.
 * @param isVolume - Optional flag indicating whether the labelmap is a volume or a stack. Default is true.
 * @returns A promise that resolves to the surface data.
 */
export async function convertLabelmapToSurface(
  labelmapRepresentationData: ToolsTypes.LabelmapSegmentationData,
  segmentIndex: number
): Promise<Types.SurfaceData> {
  let volumeId;
 
  if (
    (labelmapRepresentationData as ToolsTypes.LabelmapSegmentationDataVolume)
      .volumeId
  ) {
    volumeId = (
      labelmapRepresentationData as ToolsTypes.LabelmapSegmentationDataVolume
    ).volumeId;
  } else {
    const { imageIds } =
      labelmapRepresentationData as ToolsTypes.LabelmapSegmentationDataStack;
 
    ({ volumeId } = await computeVolumeLabelmapFromStack({
      imageIds,
    }));
  }
 
  const volume = cache.getVolume(volumeId);
  const scalarData = volume.voxelManager.getCompleteScalarDataArray();
  const { dimensions, spacing, origin, direction } = volume;
 
  triggerWorkerProgress(eventTarget, 0, segmentIndex);
 
  const results = await workerManager.executeTask(
    'polySeg',
    'convertLabelmapToSurface',
    {
      scalarData,
      dimensions,
      spacing,
      origin,
      direction,
      segmentIndex,
    },
    {
      callbacks: [
        (progress) => {
          triggerWorkerProgress(eventTarget, progress, segmentIndex);
        },
      ],
    }
  );
 
  triggerWorkerProgress(eventTarget, 100, segmentIndex);
 
  return results;
}