2019-10-15 17:08:53 +02:00
|
|
|
/**
|
|
|
|
|
* Checks if a series is reconstructable to a 3D volume.
|
|
|
|
|
*
|
2020-03-09 20:03:23 +01:00
|
|
|
* @param {Object[]} instances An array of `OHIFInstanceMetadata` objects.
|
2019-10-15 17:08:53 +02:00
|
|
|
*/
|
2020-03-09 20:03:23 +01:00
|
|
|
export default function isDisplaySetReconstructable(instances) {
|
|
|
|
|
if (!instances.length) {
|
|
|
|
|
return { value: false };
|
|
|
|
|
}
|
2019-10-15 17:08:53 +02:00
|
|
|
|
2020-03-09 20:03:23 +01:00
|
|
|
const firstInstance = instances[0].getData().metadata;
|
2019-10-15 17:08:53 +02:00
|
|
|
|
2020-03-09 20:03:23 +01:00
|
|
|
const Modality = firstInstance.Modality;
|
|
|
|
|
const isMultiframe = firstInstance.NumberOfFrames > 1;
|
|
|
|
|
|
|
|
|
|
if (!constructableModalities.includes(Modality)) {
|
2019-10-15 17:08:53 +02:00
|
|
|
return { value: false };
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-09 20:03:23 +01:00
|
|
|
// Can't reconstruct if we only have one image.
|
2019-10-15 17:08:53 +02:00
|
|
|
if (!isMultiframe && instances.length === 1) {
|
2020-03-09 20:03:23 +01:00
|
|
|
return { value: false };
|
2019-10-15 17:08:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (isMultiframe) {
|
|
|
|
|
return processMultiframe(instances[0]);
|
|
|
|
|
} else {
|
|
|
|
|
return processSingleframe(instances);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function processMultiframe(instance) {
|
2020-03-09 20:03:23 +01:00
|
|
|
//TODO: deal with multriframe checks! return false for now as can't reconstruct.
|
|
|
|
|
return { value: false };
|
2019-10-15 17:08:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function processSingleframe(instances) {
|
2020-03-09 20:03:23 +01:00
|
|
|
const firstImage = instances[0].getData().metadata;
|
|
|
|
|
const firstImageRows = firstImage.Rows;
|
|
|
|
|
const firstImageColumns = firstImage.Columns;
|
|
|
|
|
const firstImageSamplesPerPixel = firstImage.SamplesPerPixel;
|
|
|
|
|
const firstImageOrientationPatient = firstImage.ImageOrientationPatient;
|
|
|
|
|
const firstImagePositionPatient = firstImage.ImagePositionPatient;
|
2019-10-15 17:08:53 +02:00
|
|
|
|
|
|
|
|
// Can't reconstruct if we:
|
|
|
|
|
// -- Have a different dimensions within a displaySet.
|
|
|
|
|
// -- Have a different number of components within a displaySet.
|
|
|
|
|
// -- Have different orientations within a displaySet.
|
|
|
|
|
for (let i = 1; i < instances.length; i++) {
|
2020-03-09 20:03:23 +01:00
|
|
|
const instance = instances[i].getData().metadata;
|
|
|
|
|
const {
|
|
|
|
|
Rows,
|
|
|
|
|
Columns,
|
|
|
|
|
SamplesPerPixel,
|
|
|
|
|
ImageOrientationPatient,
|
|
|
|
|
} = instance;
|
2019-10-15 17:08:53 +02:00
|
|
|
|
|
|
|
|
if (
|
2020-03-09 20:03:23 +01:00
|
|
|
Rows !== firstImageRows ||
|
|
|
|
|
Columns !== firstImageColumns ||
|
|
|
|
|
SamplesPerPixel !== firstImageSamplesPerPixel ||
|
|
|
|
|
!_isSameOrientation(ImageOrientationPatient, firstImageOrientationPatient)
|
2019-10-15 17:08:53 +02:00
|
|
|
) {
|
|
|
|
|
return { value: false };
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let missingFrames = 0;
|
|
|
|
|
|
2020-03-09 20:03:23 +01:00
|
|
|
// Check if frame spacing is approximately equal within a spacingTolerance.
|
2019-10-15 17:08:53 +02:00
|
|
|
// If spacing is on a uniform grid but we are missing frames,
|
|
|
|
|
// Allow reconstruction, but pass back the number of missing frames.
|
|
|
|
|
if (instances.length > 2) {
|
2020-03-09 20:03:23 +01:00
|
|
|
const lastIpp = instances[instances.length - 1].getData().metadata
|
|
|
|
|
.ImagePositionPatient;
|
2020-02-07 17:09:37 +01:00
|
|
|
|
2020-03-09 20:03:23 +01:00
|
|
|
// We can't reconstruct if we are missing ImagePositionPatient values
|
|
|
|
|
if (!firstImagePositionPatient || !lastIpp) {
|
2020-02-07 17:09:37 +01:00
|
|
|
return { value: false };
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-15 17:08:53 +02:00
|
|
|
const averageSpacingBetweenFrames =
|
2020-03-09 20:03:23 +01:00
|
|
|
_getPerpendicularDistance(firstImagePositionPatient, lastIpp) /
|
|
|
|
|
(instances.length - 1);
|
2019-10-15 17:08:53 +02:00
|
|
|
|
2020-03-09 20:03:23 +01:00
|
|
|
let previousImagePositionPatient = firstImagePositionPatient;
|
2019-10-15 17:08:53 +02:00
|
|
|
|
|
|
|
|
for (let i = 1; i < instances.length; i++) {
|
2020-03-09 20:03:23 +01:00
|
|
|
const instance = instances[i].getData().metadata;
|
|
|
|
|
const { ImagePositionPatient } = instance;
|
2019-10-15 17:08:53 +02:00
|
|
|
|
2020-03-09 20:03:23 +01:00
|
|
|
const spacingBetweenFrames = _getPerpendicularDistance(
|
|
|
|
|
ImagePositionPatient,
|
|
|
|
|
previousImagePositionPatient
|
|
|
|
|
);
|
2019-10-15 17:08:53 +02:00
|
|
|
const spacingIssue = _getSpacingIssue(
|
|
|
|
|
spacingBetweenFrames,
|
|
|
|
|
averageSpacingBetweenFrames
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
if (spacingIssue) {
|
|
|
|
|
const issue = spacingIssue.issue;
|
|
|
|
|
|
|
|
|
|
if (issue === reconstructionIssues.MISSING_FRAMES) {
|
|
|
|
|
missingFrames += spacingIssue.missingFrames;
|
|
|
|
|
} else if (issue === reconstructionIssues.IRREGULAR_SPACING) {
|
|
|
|
|
return { value: false };
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-09 20:03:23 +01:00
|
|
|
previousImagePositionPatient = ImagePositionPatient;
|
2019-10-15 17:08:53 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return { value: true, missingFrames };
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-09 20:03:23 +01:00
|
|
|
function _isSameOrientation(iop1, iop2) {
|
|
|
|
|
if (iop1 === undefined || !iop2 === undefined) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
Math.abs(iop1[0] - iop2[0]) < iopTolerance &&
|
|
|
|
|
Math.abs(iop1[1] - iop2[1]) < iopTolerance &&
|
|
|
|
|
Math.abs(iop1[2] - iop2[2]) < iopTolerance
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// TODO: Is 10% a reasonable spacingTolerance for spacing?
|
|
|
|
|
const spacingTolerance = 0.1;
|
|
|
|
|
const iopTolerance = 0.01;
|
2019-10-15 17:08:53 +02:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Checks for spacing issues.
|
|
|
|
|
*
|
|
|
|
|
* @param {number} spacing The spacing between two frames.
|
|
|
|
|
* @param {number} averageSpacing The average spacing between all frames.
|
|
|
|
|
*
|
|
|
|
|
* @returns {Object} An object containing the issue and extra information if necessary.
|
|
|
|
|
*/
|
|
|
|
|
function _getSpacingIssue(spacing, averageSpacing) {
|
|
|
|
|
const equalWithinTolerance =
|
2020-03-09 20:03:23 +01:00
|
|
|
Math.abs(spacing - averageSpacing) < averageSpacing * spacingTolerance;
|
2019-10-15 17:08:53 +02:00
|
|
|
|
|
|
|
|
if (equalWithinTolerance) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const multipleOfAverageSpacing = spacing / averageSpacing;
|
|
|
|
|
|
|
|
|
|
const numberOfSpacings = Math.round(multipleOfAverageSpacing);
|
|
|
|
|
|
|
|
|
|
const errorForEachSpacing =
|
|
|
|
|
Math.abs(spacing - numberOfSpacings * averageSpacing) / numberOfSpacings;
|
|
|
|
|
|
2020-03-09 20:03:23 +01:00
|
|
|
if (errorForEachSpacing < spacingTolerance * averageSpacing) {
|
2019-10-15 17:08:53 +02:00
|
|
|
return {
|
|
|
|
|
issue: reconstructionIssues.MISSING_FRAMES,
|
|
|
|
|
missingFrames: numberOfSpacings - 1,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return { issue: reconstructionIssues.IRREGULAR_SPACING };
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function _getPerpendicularDistance(a, b) {
|
|
|
|
|
return Math.sqrt(
|
|
|
|
|
Math.pow(a[0] - b[0], 2) +
|
2020-05-15 14:29:27 +02:00
|
|
|
Math.pow(a[1] - b[1], 2) +
|
|
|
|
|
Math.pow(a[2] - b[2], 2)
|
2019-10-15 17:08:53 +02:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const constructableModalities = ['MR', 'CT', 'PT', 'NM'];
|
|
|
|
|
const reconstructionIssues = {
|
|
|
|
|
MISSING_FRAMES: 'missingframes',
|
|
|
|
|
IRREGULAR_SPACING: 'irregularspacing',
|
|
|
|
|
};
|