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

0% Statements 0/42
0% Branches 0/21
0% Functions 0/7
0% Lines 0/41

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 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168                                                                                                                                                                                                                                                                                                                                               
import type { Types } from '@cornerstonejs/core';
import {
  ContourSegmentationData,
  PolySegConversionOptions,
} from '../../../../types';
import { getUniqueSegmentIndices } from '../../../../utilities/segmentation';
import { getSegmentation } from '../../segmentationState';
import { convertContourToSurface } from './convertContourToSurface';
import { createAndCacheSurfacesFromRaw } from './createAndCacheSurfacesFromRaw';
import {
  LabelmapSegmentationData,
  LabelmapSegmentationDataStack,
  LabelmapSegmentationDataVolume,
} from '../../../../types/LabelmapTypes';
import { isVolumeSegmentation } from '../../../../tools/segmentation/strategies/utils/stackVolumeCheck';
import { convertLabelmapToSurface } from './convertLabelmapToSurface';
 
export type RawSurfacesData = {
  segmentIndex: number;
  data: Types.SurfaceData;
}[];
 
/**
 * Computes surface data for a given segmentation.
 * @param segmentationId - The ID of the segmentation.
 * @param options - Additional options for surface computation.
 * @returns A promise that resolves to the computed surface data.
 * @throws An error if there is no surface data available for the segmentation.
 */
export async function computeSurfaceData(
  segmentationId: string,
  options: PolySegConversionOptions = {}
) {
  const segmentIndices = options.segmentIndices?.length
    ? options.segmentIndices
    : getUniqueSegmentIndices(segmentationId);
 
  let rawSurfacesData: RawSurfacesData;
  const segmentation = getSegmentation(segmentationId);
  const representationData = segmentation.representationData;
 
  try {
    if (representationData.CONTOUR) {
      rawSurfacesData = await computeSurfaceFromContourSegmentation(
        segmentationId,
        {
          segmentIndices,
          ...options,
        }
      );
    } else if (representationData.LABELMAP as LabelmapSegmentationData) {
      // convert volume labelmap to surface
      rawSurfacesData = await computeSurfaceFromLabelmapSegmentation(
        segmentation.segmentationId,
        {
          segmentIndices,
          ...options,
        }
      );
    }
  } catch (error) {
    console.error(error);
    throw error;
  }
 
  if (!rawSurfacesData) {
    throw new Error(
      'Not enough data to convert to surface, currently only support converting volume labelmap to surface if available'
    );
  }
 
  const surfacesData = await createAndCacheSurfacesFromRaw(
    segmentationId,
    rawSurfacesData,
    options
  );
 
  return surfacesData;
}
 
async function computeSurfaceFromLabelmapSegmentation(
  segmentationId,
  options: PolySegConversionOptions = {}
): Promise<RawSurfacesData> {
  // Todo: validate valid labelmap representation
  const segmentation = getSegmentation(segmentationId);
 
  if (!segmentation?.representationData?.LABELMAP) {
    console.warn('Only support surface update from labelmaps');
    return;
  }
 
  const isVolume = isVolumeSegmentation(
    segmentation.representationData.LABELMAP
  );
 
  const labelmapRepresentationData = segmentation.representationData.LABELMAP;
 
  const segmentIndices =
    options.segmentIndices || getUniqueSegmentIndices(segmentationId);
 
  const promises = segmentIndices.map((index) => {
    const surface = convertLabelmapToSurface(
      labelmapRepresentationData as
        | LabelmapSegmentationDataVolume
        | LabelmapSegmentationDataStack,
      index,
      isVolume
    );
 
    return surface;
  });
 
  const surfaces = await Promise.allSettled(promises);
  const errors = surfaces.filter((p) => p.status === 'rejected');
 
  if (errors.length > 0) {
    console.error(errors);
    throw new Error('Failed to convert labelmap to surface');
  }
 
  const rawSurfacesData = surfaces
    .map((surface, index) => {
      if (surface.status === 'fulfilled') {
        return { segmentIndex: segmentIndices[index], data: surface.value };
      }
    })
    .filter(Boolean);
 
  return rawSurfacesData;
}
 
/**
 * Computes the surface from contour segmentation.
 * @param segmentationId - The ID of the segmentation.
 * @param options - The options for surface computation.
 * @returns A promise that resolves to the raw surfaces data.
 */
async function computeSurfaceFromContourSegmentation(
  segmentationId: string,
  options: PolySegConversionOptions = {}
): Promise<RawSurfacesData> {
  const segmentation = getSegmentation(segmentationId);
 
  const contourRepresentationData = segmentation.representationData.CONTOUR;
 
  const segmentIndices =
    options.segmentIndices || getUniqueSegmentIndices(segmentationId);
 
  const promises = segmentIndices.map(async (index) => {
    const surface = await convertContourToSurface(
      contourRepresentationData as ContourSegmentationData,
      index
    );
 
    return { segmentIndex: index, data: surface };
  });
 
  const surfaces = await Promise.all(promises);
 
  return surfaces;
}
 
export {
  computeSurfaceFromContourSegmentation,
  computeSurfaceFromLabelmapSegmentation,
};