2025-07-31 06:21:26 +02:00
|
|
|
import { utilities, metaData, type Types } from '@cornerstonejs/core';
|
2020-08-04 11:11:23 +02:00
|
|
|
import OHIF, { DicomMetadataStore } from '@ohif/core';
|
2025-07-31 06:21:26 +02:00
|
|
|
import { vec3 } from 'gl-matrix';
|
|
|
|
|
|
2022-12-29 16:13:06 +01:00
|
|
|
import getLabelFromDCMJSImportedToolData from './getLabelFromDCMJSImportedToolData';
|
2023-02-06 23:05:13 +01:00
|
|
|
import { adaptersSR } from '@cornerstonejs/adapters';
|
2025-07-31 06:21:26 +02:00
|
|
|
import { annotation as CsAnnotation, type Types as ToolTypes } from '@cornerstonejs/tools';
|
2024-10-17 15:36:24 +02:00
|
|
|
import { Enums as CSExtensionEnums } from '@ohif/extension-cornerstone';
|
2020-08-04 11:11:23 +02:00
|
|
|
|
2024-10-17 15:36:24 +02:00
|
|
|
const { locking } = CsAnnotation;
|
2020-08-04 11:11:23 +02:00
|
|
|
const { guid } = OHIF.utils;
|
2025-07-31 06:21:26 +02:00
|
|
|
const { MeasurementReport } = adaptersSR.Cornerstone3D;
|
2024-10-17 15:36:24 +02:00
|
|
|
const { CORNERSTONE_3D_TOOLS_SOURCE_NAME, CORNERSTONE_3D_TOOLS_SOURCE_VERSION } = CSExtensionEnums;
|
2020-08-04 11:11:23 +02:00
|
|
|
|
2023-03-24 21:45:21 +01:00
|
|
|
const convertCode = (codingValues, code) => {
|
2023-08-09 16:07:33 +02:00
|
|
|
if (!code || code.CodingSchemeDesignator === 'CORNERSTONEJS') {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2023-03-24 21:45:21 +01:00
|
|
|
const ref = `${code.CodingSchemeDesignator}:${code.CodeValue}`;
|
|
|
|
|
const ret = { ...codingValues[ref], ref, ...code, text: code.CodeMeaning };
|
|
|
|
|
return ret;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const convertSites = (codingValues, sites) => {
|
2023-08-09 16:07:33 +02:00
|
|
|
if (!sites || !sites.length) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2023-03-24 21:45:21 +01:00
|
|
|
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]);
|
2023-08-09 16:07:33 +02:00
|
|
|
if (site) {
|
|
|
|
|
ret.push(site);
|
|
|
|
|
}
|
2023-03-24 21:45:21 +01:00
|
|
|
}
|
|
|
|
|
return (ret.length && ret) || undefined;
|
|
|
|
|
};
|
|
|
|
|
|
2020-08-04 11:11:23 +02:00
|
|
|
/**
|
2025-07-31 06:21:26 +02:00
|
|
|
* 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.
|
2020-08-04 11:11:23 +02:00
|
|
|
*/
|
2022-12-29 16:13:06 +01:00
|
|
|
export default function hydrateStructuredReport(
|
2025-01-23 20:24:13 +01:00
|
|
|
{ servicesManager, extensionManager, commandsManager }: withAppTypes,
|
2020-08-04 11:11:23 +02:00
|
|
|
displaySetInstanceUID
|
|
|
|
|
) {
|
2021-05-28 20:32:48 +02:00
|
|
|
const dataSource = extensionManager.getActiveDataSource()[0];
|
2023-09-01 22:19:39 +02:00
|
|
|
const { measurementService, displaySetService, customizationService } = servicesManager.services;
|
|
|
|
|
|
2025-01-23 20:24:13 +01:00
|
|
|
const codingValues = customizationService.getCustomization('codingValues');
|
|
|
|
|
const disableEditing = customizationService.getCustomization('panelMeasurement.disableEditing');
|
2024-11-06 13:15:27 +01:00
|
|
|
|
2023-09-01 22:19:39 +02:00
|
|
|
const displaySet = displaySetService.getDisplaySetByUID(displaySetInstanceUID);
|
2020-08-04 11:11:23 +02:00
|
|
|
|
|
|
|
|
// TODO -> We should define a strict versioning somewhere.
|
2023-02-10 22:46:09 +01:00
|
|
|
const mappings = measurementService.getSourceMappings(
|
2022-07-27 18:39:04 +02:00
|
|
|
CORNERSTONE_3D_TOOLS_SOURCE_NAME,
|
|
|
|
|
CORNERSTONE_3D_TOOLS_SOURCE_VERSION
|
2020-08-04 11:11:23 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
if (!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 = {};
|
2022-10-13 17:07:56 +02:00
|
|
|
const imageIdsForToolState = {};
|
2020-08-04 11:11:23 +02:00
|
|
|
|
|
|
|
|
displaySet.measurements.forEach(measurement => {
|
2022-10-13 17:07:56 +02:00
|
|
|
const { ReferencedSOPInstanceUID, imageId, frameNumber } = measurement;
|
|
|
|
|
|
2020-08-04 11:11:23 +02:00
|
|
|
if (!sopInstanceUIDToImageId[ReferencedSOPInstanceUID]) {
|
|
|
|
|
sopInstanceUIDToImageId[ReferencedSOPInstanceUID] = imageId;
|
2022-10-13 17:07:56 +02:00
|
|
|
imageIdsForToolState[ReferencedSOPInstanceUID] = [];
|
|
|
|
|
}
|
|
|
|
|
if (!imageIdsForToolState[ReferencedSOPInstanceUID][frameNumber]) {
|
|
|
|
|
imageIdsForToolState[ReferencedSOPInstanceUID][frameNumber] = imageId;
|
2020-08-04 11:11:23 +02:00
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2025-02-28 20:16:39 +01:00
|
|
|
// Mapping of legacy datasets is now directly handled by adapters module
|
|
|
|
|
const datasetToUse = instance;
|
2022-07-27 18:39:04 +02:00
|
|
|
|
2025-07-31 06:21:26 +02:00
|
|
|
// Use CS3D adapters to generate toolState.
|
2023-12-13 20:21:46 +01:00
|
|
|
let storedMeasurementByAnnotationType = MeasurementReport.generateToolState(
|
2022-07-27 18:39:04 +02:00
|
|
|
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
|
2020-08-04 11:11:23 +02:00
|
|
|
);
|
|
|
|
|
|
2025-01-23 20:24:13 +01:00
|
|
|
const onBeforeSRHydration = customizationService.getCustomization('onBeforeSRHydration')?.value;
|
2023-12-13 20:21:46 +01:00
|
|
|
|
|
|
|
|
if (typeof onBeforeSRHydration === 'function') {
|
|
|
|
|
storedMeasurementByAnnotationType = onBeforeSRHydration({
|
|
|
|
|
storedMeasurementByAnnotationType,
|
|
|
|
|
displaySet,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-04 11:11:23 +02:00
|
|
|
// Filter what is found by DICOM SR to measurements we support.
|
2022-07-27 18:39:04 +02:00
|
|
|
const mappingDefinitions = mappings.map(m => m.annotationType);
|
2020-08-04 11:11:23 +02:00
|
|
|
const hydratableMeasurementsInSR = {};
|
|
|
|
|
|
2022-07-27 18:39:04 +02:00
|
|
|
Object.keys(storedMeasurementByAnnotationType).forEach(key => {
|
2020-08-04 11:11:23 +02:00
|
|
|
if (mappingDefinitions.includes(key)) {
|
2022-07-27 18:39:04 +02:00
|
|
|
hydratableMeasurementsInSR[key] = storedMeasurementByAnnotationType[key];
|
2020-08-04 11:11:23 +02:00
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Set the series touched as tracked.
|
|
|
|
|
const imageIds = [];
|
|
|
|
|
|
|
|
|
|
// TODO: notification if no hydratable?
|
2022-07-27 18:39:04 +02:00
|
|
|
Object.keys(hydratableMeasurementsInSR).forEach(annotationType => {
|
2023-09-01 22:19:39 +02:00
|
|
|
const toolDataForAnnotationType = hydratableMeasurementsInSR[annotationType];
|
2020-08-04 11:11:23 +02:00
|
|
|
|
2022-07-27 18:39:04 +02:00
|
|
|
toolDataForAnnotationType.forEach(toolData => {
|
2020-08-04 11:11:23 +02:00
|
|
|
// Add the measurement to toolState
|
2022-10-13 17:07:56 +02:00
|
|
|
// dcmjs and Cornerstone3D has structural defect in supporting multi-frame
|
|
|
|
|
// files, and looking up the imageId from sopInstanceUIDToImageId results
|
|
|
|
|
// in the wrong value.
|
2023-09-01 22:19:39 +02:00
|
|
|
const frameNumber = (toolData.annotation.data && toolData.annotation.data.frameNumber) || 1;
|
2022-10-13 17:07:56 +02:00
|
|
|
const imageId =
|
|
|
|
|
imageIdsForToolState[toolData.sopInstanceUid][frameNumber] ||
|
|
|
|
|
sopInstanceUIDToImageId[toolData.sopInstanceUid];
|
2020-08-04 11:11:23 +02:00
|
|
|
|
|
|
|
|
if (!imageIds.includes(imageId)) {
|
|
|
|
|
imageIds.push(imageId);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
let targetStudyInstanceUID;
|
|
|
|
|
const SeriesInstanceUIDs = [];
|
|
|
|
|
|
|
|
|
|
for (let i = 0; i < imageIds.length; i++) {
|
|
|
|
|
const imageId = imageIds[i];
|
2025-07-31 06:21:26 +02:00
|
|
|
if (!imageId) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2023-09-01 22:19:39 +02:00
|
|
|
const { SeriesInstanceUID, StudyInstanceUID } = metaData.get('instance', imageId);
|
2020-08-04 11:11:23 +02:00
|
|
|
|
|
|
|
|
if (!SeriesInstanceUIDs.includes(SeriesInstanceUID)) {
|
|
|
|
|
SeriesInstanceUIDs.push(SeriesInstanceUID);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!targetStudyInstanceUID) {
|
|
|
|
|
targetStudyInstanceUID = StudyInstanceUID;
|
|
|
|
|
} else if (targetStudyInstanceUID !== StudyInstanceUID) {
|
2023-09-01 22:19:39 +02:00
|
|
|
console.warn('NO SUPPORT FOR SRs THAT HAVE MEASUREMENTS FROM MULTIPLE STUDIES.');
|
2020-08-04 11:11:23 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-31 06:21:26 +02:00
|
|
|
/**
|
|
|
|
|
* 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];
|
|
|
|
|
|
|
|
|
|
if (!imageId) {
|
|
|
|
|
return getReferenceData3D(toolData, servicesManager);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const instance = metaData.get('instance', imageId);
|
|
|
|
|
const {
|
|
|
|
|
FrameOfReferenceUID,
|
|
|
|
|
// SOPInstanceUID,
|
|
|
|
|
// SeriesInstanceUID,
|
|
|
|
|
// StudyInstanceUID,
|
|
|
|
|
} = instance;
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
referencedImageId: imageId,
|
|
|
|
|
FrameOfReferenceUID,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-27 18:39:04 +02:00
|
|
|
Object.keys(hydratableMeasurementsInSR).forEach(annotationType => {
|
2023-09-01 22:19:39 +02:00
|
|
|
const toolDataForAnnotationType = hydratableMeasurementsInSR[annotationType];
|
2020-08-04 11:11:23 +02:00
|
|
|
|
2022-07-27 18:39:04 +02:00
|
|
|
toolDataForAnnotationType.forEach(toolData => {
|
|
|
|
|
toolData.uid = guid();
|
2025-07-31 06:21:26 +02:00
|
|
|
const referenceData = getReferenceData(toolData);
|
|
|
|
|
const { imageId } = referenceData;
|
2020-08-04 11:11:23 +02:00
|
|
|
|
2022-07-27 18:39:04 +02:00
|
|
|
const annotation = {
|
|
|
|
|
annotationUID: toolData.annotation.annotationUID,
|
|
|
|
|
data: toolData.annotation.data,
|
|
|
|
|
metadata: {
|
2025-07-31 06:21:26 +02:00
|
|
|
...referenceData,
|
2022-07-27 18:39:04 +02:00
|
|
|
toolName: annotationType,
|
|
|
|
|
},
|
|
|
|
|
};
|
2025-07-31 06:21:26 +02:00
|
|
|
utilities.updatePlaneRestriction(annotation.data.handles.points, annotation.metadata);
|
2022-07-27 18:39:04 +02:00
|
|
|
|
2023-02-10 22:46:09 +01:00
|
|
|
const source = measurementService.getSource(
|
2022-07-27 18:39:04 +02:00
|
|
|
CORNERSTONE_3D_TOOLS_SOURCE_NAME,
|
|
|
|
|
CORNERSTONE_3D_TOOLS_SOURCE_VERSION
|
2020-08-04 11:11:23 +02:00
|
|
|
);
|
2022-07-27 18:39:04 +02:00
|
|
|
annotation.data.label = getLabelFromDCMJSImportedToolData(toolData);
|
2023-09-01 22:19:39 +02:00
|
|
|
annotation.data.finding = convertCode(codingValues, toolData.finding?.[0]);
|
|
|
|
|
annotation.data.findingSites = convertSites(codingValues, toolData.findingSites);
|
2025-01-09 15:28:02 +01:00
|
|
|
annotation.data.findingSites?.forEach(site => {
|
|
|
|
|
if (site.type) {
|
|
|
|
|
annotation.data[site.type] = site;
|
|
|
|
|
}
|
|
|
|
|
});
|
2020-08-04 11:11:23 +02:00
|
|
|
|
2023-09-01 22:19:39 +02:00
|
|
|
const matchingMapping = mappings.find(m => m.annotationType === annotationType);
|
2020-08-04 11:11:23 +02:00
|
|
|
|
2023-08-16 16:26:28 +02:00
|
|
|
const newAnnotationUID = measurementService.addRawMeasurement(
|
2020-08-04 11:11:23 +02:00
|
|
|
source,
|
2022-07-27 18:39:04 +02:00
|
|
|
annotationType,
|
|
|
|
|
{ annotation },
|
|
|
|
|
matchingMapping.toMeasurementSchema,
|
2021-05-28 20:32:48 +02:00
|
|
|
dataSource
|
2020-08-04 11:11:23 +02:00
|
|
|
);
|
|
|
|
|
|
2025-01-09 15:28:02 +01:00
|
|
|
commandsManager.runCommand('updateMeasurement', {
|
|
|
|
|
uid: newAnnotationUID,
|
|
|
|
|
code: annotation.data.finding,
|
|
|
|
|
});
|
|
|
|
|
|
2023-08-16 16:26:28 +02:00
|
|
|
if (disableEditing) {
|
2025-01-23 20:24:13 +01:00
|
|
|
locking.setAnnotationLocked(newAnnotationUID, true);
|
2023-08-16 16:26:28 +02:00
|
|
|
}
|
|
|
|
|
|
2025-07-31 06:21:26 +02:00
|
|
|
if (imageId && !imageIds.includes(imageId)) {
|
2020-08-04 11:11:23 +02:00
|
|
|
imageIds.push(imageId);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
displaySet.isHydrated = true;
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
StudyInstanceUID: targetStudyInstanceUID,
|
|
|
|
|
SeriesInstanceUIDs,
|
|
|
|
|
};
|
|
|
|
|
}
|
2025-07-31 06:21:26 +02:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 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) {
|
|
|
|
|
if (!displaySets?.length) {
|
|
|
|
|
console.warn('No display set found for', annotation);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (displaySets.length === 1) {
|
|
|
|
|
return displaySets[0];
|
|
|
|
|
}
|
|
|
|
|
const volumeDs = displaySets.find(ds => ds.isReconstructable);
|
|
|
|
|
if (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
|
|
|
|
|
);
|
|
|
|
|
if (!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) {
|
|
|
|
|
if (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;
|
|
|
|
|
}
|