All files / extensions/cornerstone-dicom-sr/src/utils getFilteredCornerstoneToolState.ts

0% Statements 0/46
0% Branches 0/11
0% Functions 0/5
0% Lines 0/40

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                                                                                                                                                                                                   
import { annotation } from '@cornerstonejs/tools';
import { NO_IMAGE_ID } from '@cornerstonejs/adapters';
 
function getFilteredCornerstoneToolState(measurementData, additionalFindingTypes) {
  const filteredToolState = {};
 
  function addToFilteredToolState(annotation, toolType) {
    const imageId = annotation.metadata?.referencedImageId ?? NO_IMAGE_ID;
 
    Iif (!filteredToolState[imageId]) {
      filteredToolState[imageId] = {};
    }
 
    const imageIdSpecificToolState = filteredToolState[imageId];
 
    Iif (!imageIdSpecificToolState[toolType]) {
      imageIdSpecificToolState[toolType] = {
        data: [],
      };
    }
 
    const measurementDataI = measurementData.find(md => md.uid === annotation.annotationUID);
    const toolData = imageIdSpecificToolState[toolType].data;
 
    let { finding } = measurementDataI;
    const findingSites = [];
 
    // NOTE -> We use the CORNERSTONEJS coding schemeDesignator which we have
    // defined in the @cornerstonejs/adapters
    Iif (measurementDataI.label) {
      if (additionalFindingTypes.includes(toolType)) {
        finding = {
          CodeValue: 'CORNERSTONEFREETEXT',
          CodingSchemeDesignator: 'CORNERSTONEJS',
          CodeMeaning: measurementDataI.label,
        };
      } else {
        findingSites.push({
          CodeValue: 'CORNERSTONEFREETEXT',
          CodingSchemeDesignator: 'CORNERSTONEJS',
          CodeMeaning: measurementDataI.label,
        });
      }
    }
 
    Iif (measurementDataI.findingSites) {
      findingSites.push(...measurementDataI.findingSites);
    }
 
    const measurement = Object.assign({}, annotation, {
      finding,
      findingSites,
    });
 
    toolData.push(measurement);
  }
 
  const uidFilter = measurementData.map(md => md.uid);
  const uids = uidFilter.slice();
 
  const annotationManager = annotation.state.getAnnotationManager();
  const framesOfReference = annotationManager.getFramesOfReference();
 
  for (let i = 0; i < framesOfReference.length; i++) {
    const frameOfReference = framesOfReference[i];
 
    const frameOfReferenceAnnotations = annotationManager.getAnnotations(frameOfReference);
 
    const toolTypes = Object.keys(frameOfReferenceAnnotations);
 
    for (let j = 0; j < toolTypes.length; j++) {
      const toolType = toolTypes[j];
 
      const annotations = frameOfReferenceAnnotations[toolType];
 
      Iif (annotations) {
        for (let k = 0; k < annotations.length; k++) {
          const annotation = annotations[k];
          const uidIndex = uids.findIndex(uid => uid === annotation.annotationUID);
 
          Iif (uidIndex !== -1) {
            addToFilteredToolState(annotation, toolType);
            uids.splice(uidIndex, 1);
 
            Iif (!uids.length) {
              return filteredToolState;
            }
          }
        }
      }
    }
  }
 
  return filteredToolState;
}
 
export default getFilteredCornerstoneToolState;