All files / packages/tools/src/stateManagement/segmentation/polySeg canComputeRequestedRepresentation.ts

3.84% Statements 1/26
0% Branches 0/11
0% Functions 0/5
3.84% Lines 1/26

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                    1x                                                                                                                                                                                                                                  
import { SegmentationRepresentations } from '../../../enums';
import { validateLabelmap } from '../../../tools/displayTools/Labelmap';
import { SegmentationRepresentationData } from '../../../types';
import {
  findSegmentationRepresentationByUID,
  getSegmentation,
} from '../segmentationState';
 
// Map of conversion paths between source and target representations
// You should read it as "source" -> "targets"
const conversionPaths = new Map<
  SegmentationRepresentations,
  Set<SegmentationRepresentations>
>([
  [
    SegmentationRepresentations.Labelmap,
    new Set([
      SegmentationRepresentations.Surface,
      SegmentationRepresentations.Contour,
    ]),
  ],
  [
    SegmentationRepresentations.Contour,
    new Set([
      SegmentationRepresentations.Labelmap,
      SegmentationRepresentations.Surface,
    ]),
  ],
  [
    SegmentationRepresentations.Surface,
    new Set([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 segmentationRepresentationUID - The UID of the desired segmentation representation.
 * @returns true if the requested representation can be computed, otherwise false.
 */
function canComputeRequestedRepresentation(
  segmentationRepresentationUID: string
): boolean {
  const representationInfo = findSegmentationRepresentationByUID(
    segmentationRepresentationUID
  );
 
  if (!representationInfo?.segmentationRepresentation) {
    return false;
  }
 
  const { segmentationRepresentation } = representationInfo;
  const { type: representationType, polySeg } = segmentationRepresentation;
 
  if (!polySeg || !polySeg.enabled) {
    return false;
  }
 
  const { representationData } = getSegmentation(
    segmentationRepresentation.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: SegmentationRepresentationData
): string[] {
  const supportedTypes: string[] = [];
 
  Object.keys(representationData).forEach((representationType) => {
    const representationTypeData = representationData[representationType];
 
    let validateFn;
    switch (representationType) {
      case SegmentationRepresentations.Labelmap:
        validateFn = validateLabelmap;
        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 };