All files / packages/tools/src/stateManagement/segmentation/polySeg/Surface createAndCacheSurfacesFromRaw.ts

0% Statements 0/17
0% Branches 0/5
0% Functions 0/2
0% Lines 0/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                                                                                                                                                                   
import { Enums, Types, geometryLoader } from '@cornerstonejs/core';
import { getColorForSegmentIndex } from '../../config/segmentationColor';
import {
  findSegmentationRepresentationByUID,
  getSegmentation,
} from '../../segmentationState';
import { RawSurfacesData } from './surfaceComputationStrategies';
import { PolySegConversionOptions } from '../../../../types';
 
/**
 * Creates and caches surfaces from raw surface data.
 *
 * @param segmentationId - The ID of the segmentation.
 * @param rawSurfacesData - The raw surface data.
 * @param options - Additional options for creating and caching surfaces.
 * @param options.segmentIndices - An array of segment indices.
 * @param options.segmentationRepresentationUID - The UID of the segmentation representation.
 * @returns An object containing the IDs of the created surfaces.
 */
export async function createAndCacheSurfacesFromRaw(
  segmentationId: string,
  rawSurfacesData: RawSurfacesData,
  options: PolySegConversionOptions = {}
) {
  // Initialize segmentationRepresentation and toolGroupId if a representation UID is provided
  let segmentationRepresentation: any, toolGroupId: any;
  if (options.segmentationRepresentationUID) {
    ({ segmentationRepresentation, toolGroupId } =
      findSegmentationRepresentationByUID(
        options.segmentationRepresentationUID
      ));
  }
 
  const segmentation = getSegmentation(segmentationId);
 
  const geometryIds = new Map<number, string>();
 
  // Loop through raw surfaces data and create surfaces
  const promises = Object.keys(rawSurfacesData).map(async (index: string) => {
    const rawSurfaceData = rawSurfacesData[index];
    const segmentIndex = rawSurfaceData.segmentIndex;
 
    // Get the color either from the segmentation representation or randomly generated
    const color = segmentationRepresentation;
    getColorForSegmentIndex(
      toolGroupId,
      segmentationRepresentation.segmentationRepresentationUID,
      segmentIndex
    ).slice(0, 3);
 
    if (!color) {
      throw new Error(
        'No color found for segment index, unable to create surface'
      );
    }
 
    const closedSurface = {
      id: `segmentation_${segmentation.segmentationId}_surface_${segmentIndex}`,
      color,
      frameOfReferenceUID: 'test-frameOfReferenceUID',
      data: {
        points: rawSurfaceData.data.points,
        polys: rawSurfaceData.data.polys,
      },
    };
 
    const geometryId = closedSurface.id;
    geometryIds.set(segmentIndex, geometryId);
 
    return geometryLoader.createAndCacheGeometry(geometryId, {
      type: Enums.GeometryType.SURFACE,
      geometryData: closedSurface as Types.PublicSurfaceData,
    });
  });
 
  await Promise.all(promises);
 
  return {
    geometryIds,
  };
}