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

1.85% Statements 1/54
0% Branches 0/16
0% Functions 0/11
1.88% Lines 1/53

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                                        428x                                                                                                                                                                                                                                                
import type { Types } from '@cornerstonejs/core';
import { cache, getEnabledElementByViewportId } from '@cornerstonejs/core';
import * as cornerstoneTools from '@cornerstonejs/tools';
 
import { computeSurfaceFromLabelmapSegmentation } from './surfaceComputationStrategies';
import { createAndCacheSurfacesFromRaw } from './createAndCacheSurfacesFromRaw';
 
const {
  utilities: {
    segmentation: { getUniqueSegmentIndices },
  },
  segmentation: {
    state: {
      getViewportIdsWithSegmentation,
      getSegmentation,
      getSegmentationRepresentation,
    },
    triggerSegmentationEvents: { triggerSegmentationModified },
  },
  Enums: { SegmentationRepresentations },
} = cornerstoneTools;
 
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.points = [];
      surface.polys = [];
    });
 
    triggerSegmentationModified(segmentationId);
 
    return;
  }
 
  const viewportIds = getViewportIdsWithSegmentation(segmentationId);
 
  // if new surfaceObj does not have the same segment indices as the previous one,
  // and some were deleted, we need to remove the geometries for the deleted segments
  viewportIds.forEach((viewportId) => {
    const enabledElement = getEnabledElementByViewportId(viewportId);
    const viewport = enabledElement.viewport;
    if (viewport.type !== 'volume3d') {
      return;
    }
 
    const actorEntries = (viewport as Types.IVolumeViewport).getActors();
 
    const segmentIndicesAsSurface = actorEntries
      .map((actor) => (actor.representationUID as string)?.split('-').pop())
      .filter((segment) => segment && Number(segment))
      .map(Number);
 
    segmentIndicesAsSurface.forEach((segmentIndex) => {
      if (!indices.includes(segmentIndex)) {
        const filteredSurfaceActors = actorEntries.filter(
          (actor) =>
            actor.representationUID &&
            actor.representationUID ===
              `${segmentationId}-${SegmentationRepresentations.Surface}-${segmentIndex}`
        );
 
        const removingUIDs = filteredSurfaceActors.map((actor) => actor.uid);
        viewport.removeActors(removingUIDs);
        viewport.render();
 
        // remove the geometry from the cache
        const geometryId = `segmentation_${segmentationId}_surface_${segmentIndex}`;
        cache.removeGeometryLoadObject(geometryId);
      }
    });
  });
 
  const promises = surfacesObj.map(({ data, segmentIndex }) => {
    const geometryId = `segmentation_${segmentationId}_surface_${segmentIndex}`;
 
    const geometry = cache.getGeometry(geometryId) as Types.IGeometry;
 
    if (!geometry) {
      // means it is a new segment getting added while we were
      // listening to the segmentation data modified event
 
      return viewportIds.map((viewportId) => {
        const surfaceRepresentation = getSegmentationRepresentation(
          viewportId,
          {
            segmentationId,
            type: SegmentationRepresentations.Surface,
          }
        );
 
        return [surfaceRepresentation].map((surfaceRepresentation) => {
          segmentation.representationData.Surface.geometryIds.set(
            segmentIndex,
            geometryId
          );
 
          const enabledElement = getEnabledElementByViewportId(viewportId);
 
          return createAndCacheSurfacesFromRaw(
            segmentationId,
            [{ segmentIndex, data }],
            {
              segmentationId: surfaceRepresentation.segmentationId,
              viewport: enabledElement.viewport,
            }
          );
        });
      });
    } 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.points = data.points;
      surface.polys = data.polys;
    } else {
      const surface = geometry.data as Types.ISurface;
      surface.points = [];
      surface.polys = [];
    }
  });
 
  await Promise.all(promises);
 
  triggerSegmentationModified(segmentationId);
}