All files / extensions/cornerstone/src/utils/measurementServiceMappings Bidirectional.ts

69.56% Statements 48/69
63.15% Branches 12/19
50% Functions 5/10
70.14% Lines 47/67

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 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194              34x                 11x 11x   11x 11x   11x         11x 11x   11x       11x               11x 11x               11x   11x   11x 11x     11x                                                 11x 11x 11x 11x   11x 4x     7x 7x 12x   12x           12x   12x 12x   12x                     7x                                                                                   11x         11x 4x       7x 7x 7x   107x     7x 7x     7x 7x   7x 7x 7x   7x        
import SUPPORTED_TOOLS from './constants/supportedTools';
import getSOPInstanceAttributes from './utils/getSOPInstanceAttributes';
import { utils } from '@ohif/core';
import { getDisplayUnit } from './utils';
import { getIsLocked } from './utils/getIsLocked';
import { getIsVisible } from './utils/getIsVisible';
 
const Bidirectional = {
  toAnnotation: measurement => {},
  toMeasurement: (
    csToolsEventDetail,
    displaySetService,
    cornerstoneViewportService,
    getValueTypeFromToolType,
    customizationService
  ) => {
    const { annotation } = csToolsEventDetail;
    const { metadata, data, annotationUID } = annotation;
 
    const isLocked = getIsLocked(annotationUID);
    const isVisible = getIsVisible(annotationUID);
 
    Iif (!metadata || !data) {
      console.warn('Length tool: Missing metadata or data');
      return null;
    }
 
    const { toolName, referencedImageId, FrameOfReferenceUID } = metadata;
    const validToolType = SUPPORTED_TOOLS.includes(toolName);
 
    Iif (!validToolType) {
      throw new Error('Tool not supported');
    }
 
    const { SOPInstanceUID, SeriesInstanceUID, StudyInstanceUID } = getSOPInstanceAttributes(
      referencedImageId,
      displaySetService,
      annotation
    );
 
    let displaySet;
 
    if (SOPInstanceUID) {
      displaySet = displaySetService.getDisplaySetForSOPInstanceUID(
        SOPInstanceUID,
        SeriesInstanceUID
      );
    } else E{
      displaySet = displaySetService.getDisplaySetsForSeries(SeriesInstanceUID)[0];
    }
 
    const { points, textBox } = data.handles;
 
    const mappedAnnotations = getMappedAnnotations(annotation, displaySetService);
 
    const displayText = getDisplayText(mappedAnnotations, displaySet);
    const getReport = () =>
      _getReport(mappedAnnotations, points, FrameOfReferenceUID, customizationService);
 
    return {
      uid: annotationUID,
      SOPInstanceUID,
      FrameOfReferenceUID,
      points,
      textBox,
      isLocked,
      isVisible,
      metadata,
      referenceSeriesUID: SeriesInstanceUID,
      referenceStudyUID: StudyInstanceUID,
      referencedImageId,
      frameNumber: mappedAnnotations[0]?.frameNumber || 1,
      toolName: metadata.toolName,
      displaySetInstanceUID: displaySet.displaySetInstanceUID,
      label: data.label,
      displayText: displayText,
      data: data.cachedStats,
      type: getValueTypeFromToolType(toolName),
      getReport,
    };
  },
};
 
function getMappedAnnotations(annotation, displaySetService) {
  const { metadata, data } = annotation;
  const { cachedStats } = data;
  const { referencedImageId } = metadata;
  const targets = Object.keys(cachedStats);
 
  if (!targets.length) {
    return [];
  }
 
  const annotations = [];
  Object.keys(cachedStats).forEach(targetId => {
    const targetStats = cachedStats[targetId];
 
    const { SOPInstanceUID, SeriesInstanceUID, frameNumber } = getSOPInstanceAttributes(
      referencedImageId,
      displaySetService,
      annotation
    );
 
    const displaySet = displaySetService.getDisplaySetsForSeries(SeriesInstanceUID)[0];
 
    const { SeriesNumber } = displaySet;
    const { length, width, unit } = targetStats;
 
    annotations.push({
      SeriesInstanceUID,
      SOPInstanceUID,
      SeriesNumber,
      frameNumber,
      unit,
      length,
      width,
    });
  });
 
  return annotations;
}
 
/*
This function is used to convert the measurement data to a format that is
suitable for the report generation (e.g. for the csv report). The report
returns a list of columns and corresponding values.
*/
function _getReport(mappedAnnotations, points, FrameOfReferenceUID, customizationService) {
  const columns = [];
  const values = [];
 
  // Add Type
  columns.push('AnnotationType');
  values.push('Cornerstone:Bidirectional');
 
  mappedAnnotations.forEach(annotation => {
    const { length, width, unit } = annotation;
    columns.push(`Length`, `Width`, 'Unit');
    values.push(length, width, unit);
  });
 
  Iif (FrameOfReferenceUID) {
    columns.push('FrameOfReferenceUID');
    values.push(FrameOfReferenceUID);
  }
 
  Iif (points) {
    columns.push('points');
    // points has the form of [[x1, y1, z1], [x2, y2, z2], ...]
    // convert it to string of [[x1 y1 z1];[x2 y2 z2];...]
    // so that it can be used in the csv report
    values.push(points.map(p => p.join(' ')).join(';'));
  }
 
  return {
    columns,
    values,
  };
}
 
function getDisplayText(mappedAnnotations, displaySet) {
  const displayText = {
    primary: [],
    secondary: [],
  };
 
  if (!mappedAnnotations || !mappedAnnotations.length) {
    return displayText;
  }
 
  // Area is the same for all series
  const { length, width, unit, SeriesNumber, SOPInstanceUID, frameNumber } = mappedAnnotations[0];
  const roundedLength = utils.roundNumber(length, 2);
  const roundedWidth = utils.roundNumber(width, 2);
 
  const instance = displaySet.instances.find(image => image.SOPInstanceUID === SOPInstanceUID);
 
  let InstanceNumber;
  if (instance) {
    InstanceNumber = instance.InstanceNumber;
  }
 
  const instanceText = InstanceNumber ? ` I: ${InstanceNumber}` : '';
  const frameText = displaySet.isMultiFrame ? ` F: ${frameNumber}` : '';
 
  displayText.primary.push(`L: ${roundedLength} ${getDisplayUnit(unit)}`);
  displayText.primary.push(`W: ${roundedWidth} ${getDisplayUnit(unit)}`);
  displayText.secondary.push(`S: ${SeriesNumber}${instanceText}${frameText}`);
 
  return displayText;
}
 
export default Bidirectional;