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

0% Statements 0/35
0% Branches 0/10
0% Functions 0/5
0% Lines 0/35

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                                                                                                                                                                                               
import { Types, cache } from '@cornerstonejs/core';
import { getUniqueSegmentIndices } from '../../../../utilities/segmentation';
import {
  getSegmentation,
  getSegmentationRepresentations,
  getToolGroupIdsWithSegmentation,
} from '../../segmentationState';
import { triggerSegmentationModified } from '../../triggerSegmentationEvents';
import { ToolGroupSpecificRepresentations } from '../../../../types/SegmentationStateTypes';
import { SegmentationRepresentations } from '../../../../enums';
import { computeSurfaceFromLabelmapSegmentation } from './surfaceComputationStrategies';
import { createAndCacheSurfacesFromRaw } from './createAndCacheSurfacesFromRaw';
 
export async function updateSurfaceData(segmentationId) {
  const surfacesObj = await computeSurfaceFromLabelmapSegmentation(
    segmentationId
  );
 
  if (!surfacesObj) {
    return;
  }
 
  const segmentation = getSegmentation(segmentationId);
  const indices = getUniqueSegmentIndices(segmentationId);
 
  if (!indices.length) {
    // means all segments were removed so we need to empty out
    // the geometry data
    const geometryIds = segmentation.representationData.SURFACE.geometryIds;
    geometryIds.forEach((geometryId) => {
      const geometry = cache.getGeometry(geometryId);
      const surface = geometry.data as Types.ISurface;
      surface.setPoints([]);
      surface.setPolys([]);
    });
 
    triggerSegmentationModified(segmentationId);
 
    return;
  }
 
  const promises = surfacesObj.map(({ data, segmentIndex }) => {
    const geometryId = `segmentation_${segmentationId}_surface_${segmentIndex}`;
 
    const geometry = cache.getGeometry(geometryId);
 
    if (!geometry) {
      // means it is a new segment getting added while we were
      // listening to the segmentation data modified event
      const toolGroupIds = getToolGroupIdsWithSegmentation(segmentationId);
 
      return toolGroupIds.map((toolGroupId) => {
        const segmentationRepresentations = getSegmentationRepresentations(
          toolGroupId
        ) as ToolGroupSpecificRepresentations;
 
        return segmentationRepresentations.map((segmentationRepresentation) => {
          if (
            segmentationRepresentation.type !==
            SegmentationRepresentations.Surface
          ) {
            return;
          }
          segmentation.representationData.SURFACE.geometryIds.set(
            segmentIndex,
            geometryId
          );
 
          return createAndCacheSurfacesFromRaw(
            segmentationId,
            [{ segmentIndex, data }],
            {
              segmentationRepresentationUID:
                segmentationRepresentation.segmentationRepresentationUID,
            }
          );
        });
      });
    } else if (indices.includes(segmentIndex)) {
      // if the geometry already exists and the segmentIndex is
      // still present, update the geometry data
      const surface = geometry.data as Types.ISurface;
      surface.setPoints(data.points);
      surface.setPolys(data.polys);
    } else {
      const surface = geometry.data as Types.ISurface;
      surface.setPoints([]);
      surface.setPolys([]);
    }
  });
 
  await Promise.all(promises);
 
  triggerSegmentationModified(segmentationId);
}