All files / packages/tools/src/stateManagement/segmentation/polySeg/Labelmap convertSurfaceToLabelmap.ts

9.09% Statements 2/22
0% Branches 0/2
0% Functions 0/5
9.09% Lines 2/22

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                      1x   1x                                                                                                                                                            
import {
  Enums,
  Types,
  cache,
  eventTarget,
  getWebWorkerManager,
  triggerEvent,
} from '@cornerstonejs/core';
import { SurfaceSegmentationData } from '../../../../types/SurfaceTypes';
import { WorkerTypes } from '../../../../enums';
 
const workerManager = getWebWorkerManager();
 
const triggerWorkerProgress = (eventTarget, progress) => {
  triggerEvent(eventTarget, Enums.Events.WEB_WORKER_PROGRESS, {
    progress,
    type: WorkerTypes.POLYSEG_SURFACE_TO_LABELMAP,
  });
};
 
export async function convertSurfaceToVolumeLabelmap(
  surfaceRepresentationData: SurfaceSegmentationData,
  segmentationVolume: Types.IImageVolume
) {
  const { geometryIds } = surfaceRepresentationData;
  if (!geometryIds?.size) {
    throw new Error('No geometry IDs found for surface representation');
  }
 
  const segmentsInfo = new Map() as Map<
    number,
    {
      points: number[];
      polys: number[];
    }
  >;
 
  geometryIds.forEach((geometryId, segmentIndex) => {
    const geometry = cache.getGeometry(geometryId);
    const geometryData = geometry.data as Types.ISurface;
    const points = geometryData.getPoints();
    const polys = geometryData.getPolys();
 
    segmentsInfo.set(segmentIndex, {
      points,
      polys,
    });
  });
 
  const { dimensions, direction, origin, spacing } = segmentationVolume;
 
  triggerWorkerProgress(eventTarget, 0);
 
  const newScalarData = await workerManager.executeTask(
    'polySeg',
    'convertSurfacesToVolumeLabelmap',
    {
      segmentsInfo,
      dimensions,
      spacing,
      direction,
      origin,
    },
    {
      callbacks: [
        (progress) => {
          triggerWorkerProgress(eventTarget, progress);
        },
      ],
    }
  );
 
  triggerWorkerProgress(eventTarget, 1);
 
  segmentationVolume.imageData
    .getPointData()
    .getScalars()
    .setData(newScalarData);
  segmentationVolume.imageData.modified();
 
  // update the scalarData in the volume as well
  segmentationVolume.modified();
 
  return {
    volumeId: segmentationVolume.volumeId,
  };
}
 
export async function convertSurfaceToStackLabelmap() {
  // TODO
}