All files / polymorphic-segmentation/src canComputeRequestedRepresentation.ts

14.28% Statements 3/21
0% Branches 0/5
0% Functions 0/5
14.28% Lines 3/21

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          428x 428x       428x                                                                                                                                                                                              
import type { Types as ToolsTypes } from '@cornerstonejs/tools';
import { Enums, segmentation, utilities } from '@cornerstonejs/tools';
 
type RepresentationsData = ToolsTypes.RepresentationsData;
 
const { getSegmentation } = segmentation.state;
const { validateLabelmap } = utilities.segmentation;
 
// Map of conversion paths between source and target representations
// You should read it as "source" -> "targets"
const conversionPaths = new Map([
  [
    Enums.SegmentationRepresentations.Labelmap,
    new Set([
      Enums.SegmentationRepresentations.Surface,
      Enums.SegmentationRepresentations.Contour,
    ]),
  ],
  [
    Enums.SegmentationRepresentations.Contour,
    new Set([
      Enums.SegmentationRepresentations.Labelmap,
      Enums.SegmentationRepresentations.Surface,
    ]),
  ],
  [
    Enums.SegmentationRepresentations.Surface,
    new Set([Enums.SegmentationRepresentations.Labelmap]),
  ],
]);
 
/**
 * Determines whether the requested representation can be computed, based on
 * the existing representation types and available conversion paths.
 * This is used in the labelmapDisplay and surfaceDisplay logic if the
 * requested representation is not available whether we can use the existing
 * representation to compute the requested representation. You can checkout the polySeg
 * examples to see how this is used polyDataActorManipulationTools and others
 *
 * @param segmentationId - The id of the segmentation
 * @param representationType - The type of the representation to compute
 * @returns true if the representation can be computed, false otherwise
 */
function canComputeRequestedRepresentation(
  segmentationId: string,
  representationType: Enums.SegmentationRepresentations
): boolean {
  const { representationData } = getSegmentation(segmentationId);
 
  const existingRepresentationTypes =
    getExistingRepresentationTypes(representationData);
 
  return existingRepresentationTypes.some((existingRepresentationType) =>
    canConvertFromTo(existingRepresentationType, representationType)
  );
}
 
/**
 * Retrieves the existing representation types for the given representationData
 * by verifying the validity of each representation type.
 *
 * @param representationData - The representation data
 * @returns supportedTypes - An array of valid representation types
 */
function getExistingRepresentationTypes(
  representationData: RepresentationsData
): string[] {
  const supportedTypes: string[] = [];
 
  Object.keys(representationData).forEach((representationType) => {
    const representationTypeData = representationData[representationType];
 
    let validateFn;
    switch (representationType) {
      case Enums.SegmentationRepresentations.Labelmap:
        validateFn = validateLabelmap.validate;
        break;
      // Todo: add validation for other representation types
    }
 
    if (validateFn) {
      try {
        validateFn(representationTypeData);
        supportedTypes.push(representationType);
      } catch (error) {
        console.warn(
          `Validation failed for labelmap of type ${representationType}`
        );
      }
    } else {
      supportedTypes.push(representationType);
    }
  });
 
  return supportedTypes;
}
 
async function canConvertFromTo(fromRepresentationType, toRepresentationType) {
  return (
    conversionPaths.get(fromRepresentationType)?.has(toRepresentationType) ||
    false
  );
}
 
export { canComputeRequestedRepresentation };