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

12.5% Statements 2/16
0% Branches 0/3
0% Functions 0/2
12.5% Lines 2/16

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            428x 428x                                                                                                                              
import type { Types } from '@cornerstonejs/core';
import { Enums, geometryLoader } from '@cornerstonejs/core';
import type { RawSurfacesData } from './surfaceComputationStrategies';
import type { PolySegConversionOptions } from '../types';
import * as cornerstoneTools from '@cornerstonejs/tools';
 
const { getSegmentation } = cornerstoneTools.segmentation.state;
const { getSegmentIndexColor } = cornerstoneTools.segmentation.config.color;
 
/**
 * Creates and caches surfaces from raw surface data.
 *
 * @param segmentationId - The id of the segmentation
 * @param rawSurfacesData - The raw surface data
 * @param options - Optional parameters for creating and caching surfaces
 * @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
 
  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 = getSegmentIndexColor(
      options.viewport.id,
      segmentation.segmentationId,
      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',
      points: rawSurfaceData.data.points,
      polys: rawSurfaceData.data.polys,
      segmentIndex,
    };
 
    const geometryId = closedSurface.id;
    geometryIds.set(segmentIndex, geometryId);
 
    return geometryLoader.createAndCacheGeometry(geometryId, {
      type: Enums.GeometryType.SURFACE,
      geometryData: closedSurface as unknown as Types.PublicSurfaceData,
    });
  });
 
  await Promise.all(promises);
 
  return {
    geometryIds,
  };
}