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

62.41% Statements 88/141
52% Branches 26/50
60% Functions 12/20
61.76% Lines 84/136

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 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352                  55x 55x 55x 55x   55x 4x 4x             55x 2x     2x   2x   2x 2x       2x                         1x 1x   1x 1x   1x     1x         1x           1x           1x 1x   1x 2x   2x 2x 2x   2x 2x         1x     1x                   1x   1x               18x 1x   1x 2x 2x         1x     1x 2x   2x         2x   2x     2x 2x           1x   1x 2x 2x     2x   2x 1x     2x 1x 1x                           2x   2x     2x       2x           2x   2x           1x 2x   2x 2x 2x 2x   2x               2x   2x       2x 2x 2x 2x           5x   2x               2x         2x       2x           1x   1x                                                                                                                                                                                            
import { utilities, metaData, type Types } from '@cornerstonejs/core';
import OHIF, { DicomMetadataStore } from '@ohif/core';
import { vec3 } from 'gl-matrix';
 
import getLabelFromDCMJSImportedToolData from './getLabelFromDCMJSImportedToolData';
import { adaptersSR } from '@cornerstonejs/adapters';
import { annotation as CsAnnotation, type Types as ToolTypes } from '@cornerstonejs/tools';
import { Enums as CSExtensionEnums } from '@ohif/extension-cornerstone';
 
const { locking } = CsAnnotation;
const { guid } = OHIF.utils;
const { MeasurementReport } = adaptersSR.Cornerstone3D;
const { CORNERSTONE_3D_TOOLS_SOURCE_NAME, CORNERSTONE_3D_TOOLS_SOURCE_VERSION } = CSExtensionEnums;
 
const convertCode = (codingValues, code) => {
  if (!code || code.CodingSchemeDesignator === 'CORNERSTONEJS') {
    return;
  }
  const ref = `${code.CodingSchemeDesignator}:${code.CodeValue}`;
  const ret = { ...codingValues[ref], ref, ...code, text: code.CodeMeaning };
  return ret;
};
 
const convertSites = (codingValues, sites) => {
  Iif (!sites || !sites.length) {
    return;
  }
  const ret = [];
  // Do as a loop to convert away from Proxy instances
  for (let i = 0; i < sites.length; i++) {
    // Deal with irregular conversion from dcmjs
    const site = convertCode(codingValues, sites[i][0] || sites[i]);
    Iif (site) {
      ret.push(site);
    }
  }
  return (ret.length && ret) || undefined;
};
 
/**
 * Hydrates a structured report
 * Handles 2d and 3d hydration from SCOORD and SCOORD3D points
 * For 3D hydration, chooses a volume display set to display with
 * FOr 2D hydration, chooses the (first) display set containing the referenced image.
 */
export default function hydrateStructuredReport(
  { servicesManager, extensionManager, commandsManager }: withAppTypes,
  displaySetInstanceUID
) {
  const dataSource = extensionManager.getActiveDataSource()[0];
  const { measurementService, displaySetService, customizationService } = servicesManager.services;
 
  const codingValues = customizationService.getCustomization('codingValues');
  const disableEditing = customizationService.getCustomization('panelMeasurement.disableEditing');
 
  const displaySet = displaySetService.getDisplaySetByUID(displaySetInstanceUID);
 
  // TODO -> We should define a strict versioning somewhere.
  const mappings = measurementService.getSourceMappings(
    CORNERSTONE_3D_TOOLS_SOURCE_NAME,
    CORNERSTONE_3D_TOOLS_SOURCE_VERSION
  );
 
  Iif (!mappings || !mappings.length) {
    throw new Error(
      `Attempting to hydrate measurements service when no mappings present. This shouldn't be reached.`
    );
  }
 
  const instance = DicomMetadataStore.getInstance(
    displaySet.StudyInstanceUID,
    displaySet.SeriesInstanceUID,
    displaySet.SOPInstanceUID
  );
 
  const sopInstanceUIDToImageId = {};
  const imageIdsForToolState = {};
 
  displaySet.measurements.forEach(measurement => {
    const { ReferencedSOPInstanceUID, imageId, frameNumber } = measurement;
 
    if (!sopInstanceUIDToImageId[ReferencedSOPInstanceUID]) {
      sopInstanceUIDToImageId[ReferencedSOPInstanceUID] = imageId;
      imageIdsForToolState[ReferencedSOPInstanceUID] = [];
    }
    if (!imageIdsForToolState[ReferencedSOPInstanceUID][frameNumber]) {
      imageIdsForToolState[ReferencedSOPInstanceUID][frameNumber] = imageId;
    }
  });
 
  // Mapping of legacy datasets is now directly handled by adapters module
  const datasetToUse = instance;
 
  // Use CS3D adapters to generate toolState.
  let storedMeasurementByAnnotationType = MeasurementReport.generateToolState(
    datasetToUse,
    // NOTE: we need to pass in the imageIds to dcmjs since the we use them
    // for the imageToWorld transformation. The following assumes that the order
    // that measurements were added to the display set are the same order as
    // the measurementGroups in the instance.
    sopInstanceUIDToImageId,
    metaData
  );
 
  const onBeforeSRHydration = customizationService.getCustomization('onBeforeSRHydration')?.value;
 
  Iif (typeof onBeforeSRHydration === 'function') {
    storedMeasurementByAnnotationType = onBeforeSRHydration({
      storedMeasurementByAnnotationType,
      displaySet,
    });
  }
 
  // Filter what is found by DICOM SR to measurements we support.
  const mappingDefinitions = mappings.map(m => m.annotationType);
  const hydratableMeasurementsInSR = {};
 
  Object.keys(storedMeasurementByAnnotationType).forEach(key => {
    if (mappingDefinitions.includes(key)) {
      hydratableMeasurementsInSR[key] = storedMeasurementByAnnotationType[key];
    }
  });
 
  // Set the series touched as tracked.
  const imageIds = [];
 
  // TODO: notification if no hydratable?
  Object.keys(hydratableMeasurementsInSR).forEach(annotationType => {
    const toolDataForAnnotationType = hydratableMeasurementsInSR[annotationType];
 
    toolDataForAnnotationType.forEach(toolData => {
      // Add the measurement to toolState
      // dcmjs and Cornerstone3D has structural defect in supporting multi-frame
      // files, and looking up the imageId from sopInstanceUIDToImageId results
      // in the wrong value.
      const frameNumber = (toolData.annotation.data && toolData.annotation.data.frameNumber) || 1;
      const imageId =
        imageIdsForToolState[toolData.sopInstanceUid][frameNumber] ||
        sopInstanceUIDToImageId[toolData.sopInstanceUid];
 
      if (!imageIds.includes(imageId)) {
        imageIds.push(imageId);
      }
    });
  });
 
  let targetStudyInstanceUID;
  const SeriesInstanceUIDs = [];
 
  for (let i = 0; i < imageIds.length; i++) {
    const imageId = imageIds[i];
    Iif (!imageId) {
      continue;
    }
    const { SeriesInstanceUID, StudyInstanceUID } = metaData.get('instance', imageId);
 
    if (!SeriesInstanceUIDs.includes(SeriesInstanceUID)) {
      SeriesInstanceUIDs.push(SeriesInstanceUID);
    }
 
    if (!targetStudyInstanceUID) {
      targetStudyInstanceUID = StudyInstanceUID;
    } else Iif (targetStudyInstanceUID !== StudyInstanceUID) {
      console.warn('NO SUPPORT FOR SRs THAT HAVE MEASUREMENTS FROM MULTIPLE STUDIES.');
    }
  }
 
  /**
   * Gets reference data for what frame of reference and the referenced
   * image id, or for 3d measurements, the volumeId to apply this annotation to.
   */
  function getReferenceData(toolData): ToolTypes.AnnotationMetadata {
    // Add the measurement to toolState
    // dcmjs and Cornerstone3D has structural defect in supporting multi-frame
    // files, and looking up the imageId from sopInstanceUIDToImageId results
    // in the wrong value.
    const frameNumber = (toolData.annotation.data && toolData.annotation.data.frameNumber) || 1;
    const imageId =
      imageIdsForToolState[toolData.sopInstanceUid][frameNumber] ||
      sopInstanceUIDToImageId[toolData.sopInstanceUid];
 
    Iif (!imageId) {
      return getReferenceData3D(toolData, servicesManager);
    }
 
    const instance = metaData.get('instance', imageId);
    const {
      FrameOfReferenceUID,
      // SOPInstanceUID,
      // SeriesInstanceUID,
      // StudyInstanceUID,
    } = instance;
 
    return {
      referencedImageId: imageId,
      FrameOfReferenceUID,
    };
  }
 
  Object.keys(hydratableMeasurementsInSR).forEach(annotationType => {
    const toolDataForAnnotationType = hydratableMeasurementsInSR[annotationType];
 
    toolDataForAnnotationType.forEach(toolData => {
      toolData.uid = guid();
      const referenceData = getReferenceData(toolData);
      const { imageId } = referenceData;
 
      const annotation = {
        annotationUID: toolData.annotation.annotationUID,
        data: toolData.annotation.data,
        metadata: {
          ...referenceData,
          toolName: annotationType,
        },
      };
      utilities.updatePlaneRestriction(annotation.data.handles.points, annotation.metadata);
 
      const source = measurementService.getSource(
        CORNERSTONE_3D_TOOLS_SOURCE_NAME,
        CORNERSTONE_3D_TOOLS_SOURCE_VERSION
      );
      annotation.data.label = getLabelFromDCMJSImportedToolData(toolData);
      annotation.data.finding = convertCode(codingValues, toolData.finding?.[0]);
      annotation.data.findingSites = convertSites(codingValues, toolData.findingSites);
      annotation.data.findingSites?.forEach(site => {
        Iif (site.type) {
          annotation.data[site.type] = site;
        }
      });
 
      const matchingMapping = mappings.find(m => m.annotationType === annotationType);
 
      const newAnnotationUID = measurementService.addRawMeasurement(
        source,
        annotationType,
        { annotation },
        matchingMapping.toMeasurementSchema,
        dataSource
      );
 
      commandsManager.runCommand('updateMeasurement', {
        uid: newAnnotationUID,
        code: annotation.data.finding,
      });
 
      Iif (disableEditing) {
        locking.setAnnotationLocked(newAnnotationUID, true);
      }
 
      Iif (imageId && !imageIds.includes(imageId)) {
        imageIds.push(imageId);
      }
    });
  });
 
  displaySet.isHydrated = true;
 
  return {
    StudyInstanceUID: targetStudyInstanceUID,
    SeriesInstanceUIDs,
  };
}
 
/**
 * For 3d annotations, there are often several display sets which could
 * be used to display the annotation.  Choose the first annotation with the
 * same frame of reference that is reconstructable, or the first display set
 * otherwise.
 */
function chooseDisplaySet(displaySets, annotation) {
  Iif (!displaySets?.length) {
    console.warn('No display set found for', annotation);
    return;
  }
  Iif (displaySets.length === 1) {
    return displaySets[0];
  }
  const volumeDs = displaySets.find(ds => ds.isReconstructable);
  Iif (volumeDs) {
    return volumeDs;
  }
  return displaySets[0];
}
 
/**
 * Gets the additional reference data appropriate for a 3d reference.
 * This will choose a volume id, frame of reference and a plane restriction.
 */
function getReferenceData3D(toolData, servicesManager: Types.ServicesManager) {
  const { FrameOfReferenceUID } = toolData.annotation.metadata;
  const { points } = toolData.annotation.data.handles;
  const { displaySetService } = servicesManager.services;
  const displaySetsFOR = displaySetService.getDisplaySetsBy(
    ds => ds.FrameOfReferenceUID === FrameOfReferenceUID
  );
  Iif (!displaySetsFOR.length || !points?.length) {
    return {
      FrameOfReferenceUID,
    };
  }
  const ds = chooseDisplaySet(displaySetsFOR, toolData.annotation);
  const cameraView = chooseCameraView(ds, points);
 
  const viewReference = {
    ...cameraView,
    volumeId: ds.displaySetInstanceUID,
    FrameOfReferenceUID,
  };
  utilities.updatePlaneRestriction(points, viewReference);
  return viewReference;
}
 
/**
 * Chooses a possible camera view - right now this is fairly basic,
 * just setting the unknowns to null.
 */
function chooseCameraView(_ds, points) {
  const selectedPoints = choosePoints(points);
  const cameraFocalPoint = <Point3>centerOf(selectedPoints);
  // These are sufficient to be null for now and can be set on first view
  let viewPlaneNormal: Types.Point3 = null;
  let viewUp: Types.Point3 = null;
 
  return {
    cameraFocalPoint,
    viewPlaneNormal,
    viewUp,
  };
}
 
function centerOf(points) {
  const scale = 1 / points.length;
  const center = vec3.create();
  for (const point of points) {
    vec3.scaleAndAdd(center, center, point, scale);
  }
  return center;
}
 
function choosePoints(points) {
  Iif (points.length === 1 || points.length === 2) {
    return points;
  }
  const firstIndex = 0;
  const secondIndex = Math.ceil(points.length / 4);
  const thirdIndex = Math.ceil(points.length / 2);
  // TODO - check if colinear, if so try to find another 3 points.
 
  const newPoints = [points[firstIndex], points[secondIndex], points[thirdIndex]];
  return newPoints;
}