|
| 1 | +/** |
| 2 | + * Checks if a series is reconstructable to a 3D volume. |
| 3 | + * |
| 4 | + * @param {Object} series The `OHIFSeriesMetadata` object. |
| 5 | + * @param {Object[]} instances The `OHIFInstanceMetadata` object |
| 6 | + */ |
| 7 | +export default function isDisplaySetReconstructable(series, instances) { |
| 8 | + // Can't reconstruct if we only have one image. |
| 9 | + |
| 10 | + const modality = series._data.modality; // TODO -> Is there a better way to get this? |
| 11 | + const isMultiframe = instances[0].getRawValue('x00280008') > 1; |
| 12 | + |
| 13 | + if (!constructableModalities.includes(modality)) { |
| 14 | + return { value: false }; |
| 15 | + } |
| 16 | + |
| 17 | + if (!isMultiframe && instances.length === 1) { |
| 18 | + return { values: false }; |
| 19 | + } |
| 20 | + |
| 21 | + if (isMultiframe) { |
| 22 | + return processMultiframe(instances[0]); |
| 23 | + } else { |
| 24 | + return processSingleframe(instances); |
| 25 | + } |
| 26 | +} |
| 27 | + |
| 28 | +function processMultiframe(instance) { |
| 29 | + //TODO: deal with multriframe checks! return true for now. |
| 30 | + return { value: true }; |
| 31 | +} |
| 32 | + |
| 33 | +function processSingleframe(instances) { |
| 34 | + const firstImage = instances[0]; |
| 35 | + const firstImageRows = firstImage.getTagValue('x00280010'); |
| 36 | + const firstImageColumns = firstImage.getTagValue('x00280011'); |
| 37 | + const firstImageSamplesPerPixel = firstImage.getTagValue('x00280002'); |
| 38 | + // Note: No need to unpack iop, can compare string form. |
| 39 | + const firstImageOrientationPatient = firstImage.getTagValue('x00200037'); |
| 40 | + |
| 41 | + // Can't reconstruct if we: |
| 42 | + // -- Have a different dimensions within a displaySet. |
| 43 | + // -- Have a different number of components within a displaySet. |
| 44 | + // -- Have different orientations within a displaySet. |
| 45 | + for (let i = 1; i < instances.length; i++) { |
| 46 | + const instance = instances[i]; |
| 47 | + const rows = instance.getTagValue('x00280010'); |
| 48 | + const columns = instance.getTagValue('x00280011'); |
| 49 | + const samplesPerPixel = instance.getTagValue('x00280002'); |
| 50 | + const imageOrientationPatient = instance.getTagValue('x00200037'); |
| 51 | + |
| 52 | + if ( |
| 53 | + rows !== firstImageRows || |
| 54 | + columns !== firstImageColumns || |
| 55 | + samplesPerPixel !== firstImageSamplesPerPixel || |
| 56 | + imageOrientationPatient !== firstImageOrientationPatient |
| 57 | + ) { |
| 58 | + return { value: false }; |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + let missingFrames = 0; |
| 63 | + |
| 64 | + // Check if frame spacing is approximately equal within a tolerance. |
| 65 | + // If spacing is on a uniform grid but we are missing frames, |
| 66 | + // Allow reconstruction, but pass back the number of missing frames. |
| 67 | + if (instances.length > 2) { |
| 68 | + const firstIpp = _getImagePositionPatient(firstImage); |
| 69 | + const lastIpp = _getImagePositionPatient(instances[instances.length - 1]); |
| 70 | + const averageSpacingBetweenFrames = |
| 71 | + _getPerpendicularDistance(firstIpp, lastIpp) / (instances.length - 1); |
| 72 | + |
| 73 | + let previousIpp = firstIpp; |
| 74 | + |
| 75 | + for (let i = 1; i < instances.length; i++) { |
| 76 | + const instance = instances[i]; |
| 77 | + const ipp = _getImagePositionPatient(instance); |
| 78 | + |
| 79 | + const spacingBetweenFrames = _getPerpendicularDistance(ipp, previousIpp); |
| 80 | + const spacingIssue = _getSpacingIssue( |
| 81 | + spacingBetweenFrames, |
| 82 | + averageSpacingBetweenFrames |
| 83 | + ); |
| 84 | + |
| 85 | + if (spacingIssue) { |
| 86 | + const issue = spacingIssue.issue; |
| 87 | + |
| 88 | + if (issue === reconstructionIssues.MISSING_FRAMES) { |
| 89 | + missingFrames += spacingIssue.missingFrames; |
| 90 | + } else if (issue === reconstructionIssues.IRREGULAR_SPACING) { |
| 91 | + return { value: false }; |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + previousIpp = ipp; |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + return { value: true, missingFrames }; |
| 100 | +} |
| 101 | + |
| 102 | +// TODO: Is 10% a reasonable tolerance for spacing? |
| 103 | +const tolerance = 0.1; |
| 104 | + |
| 105 | +/** |
| 106 | + * Checks for spacing issues. |
| 107 | + * |
| 108 | + * @param {number} spacing The spacing between two frames. |
| 109 | + * @param {number} averageSpacing The average spacing between all frames. |
| 110 | + * |
| 111 | + * @returns {Object} An object containing the issue and extra information if necessary. |
| 112 | + */ |
| 113 | +function _getSpacingIssue(spacing, averageSpacing) { |
| 114 | + const equalWithinTolerance = |
| 115 | + Math.abs(spacing - averageSpacing) < averageSpacing * tolerance; |
| 116 | + |
| 117 | + if (equalWithinTolerance) { |
| 118 | + return; |
| 119 | + } |
| 120 | + |
| 121 | + const multipleOfAverageSpacing = spacing / averageSpacing; |
| 122 | + |
| 123 | + const numberOfSpacings = Math.round(multipleOfAverageSpacing); |
| 124 | + |
| 125 | + const errorForEachSpacing = |
| 126 | + Math.abs(spacing - numberOfSpacings * averageSpacing) / numberOfSpacings; |
| 127 | + |
| 128 | + if (errorForEachSpacing < tolerance * averageSpacing) { |
| 129 | + return { |
| 130 | + issue: reconstructionIssues.MISSING_FRAMES, |
| 131 | + missingFrames: numberOfSpacings - 1, |
| 132 | + }; |
| 133 | + } |
| 134 | + |
| 135 | + return { issue: reconstructionIssues.IRREGULAR_SPACING }; |
| 136 | +} |
| 137 | + |
| 138 | +function _getImagePositionPatient(instance) { |
| 139 | + return instance |
| 140 | + .getTagValue('x00200032') |
| 141 | + .split('\\') |
| 142 | + .map(element => Number(element)); |
| 143 | +} |
| 144 | + |
| 145 | +function _getPerpendicularDistance(a, b) { |
| 146 | + return Math.sqrt( |
| 147 | + Math.pow(a[0] - b[0], 2) + |
| 148 | + Math.pow(a[1] - b[1], 2) + |
| 149 | + Math.pow(a[2] - b[2], 2) |
| 150 | + ); |
| 151 | +} |
| 152 | + |
| 153 | +const constructableModalities = ['MR', 'CT', 'PT', 'NM']; |
| 154 | +const reconstructionIssues = { |
| 155 | + MISSING_FRAMES: 'missingframes', |
| 156 | + IRREGULAR_SPACING: 'irregularspacing', |
| 157 | +}; |
0 commit comments