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 | 200x 200x 200x 850x 850x 850x 850x 850x 850x 850x 850x 850x 3x 48096x 2x 845x 845x 27x 27x 27x 9x 9x 9x 8x 8x 3x 3x 22x 18x 4x 1x 1x 3x 3x 3x 3x 823x 823x 823x 823x 823x 823x 823x 47271x 47271x 47271x 47271x 44x 779x 48006x 779x 779x 779x 779x 779x 779x 779x 47227x 47227x 47227x 47227x 47227x 47227x 779x 141827x 141827x 94322x 94322x 94322x 98203x 48009x 782x 782x 3x 779x 779x 1558x 3x 3x 3x 3x 3x 200x 200x | import toNumber from './toNumber';
import sortInstancesByPosition from './sortInstancesByPosition';
// TODO: Is 10% a reasonable spacingTolerance for spacing?
const spacingTolerance = 0.2;
const iopTolerance = 0.01;
// Real slice spacing is on the order of millimetres; a set of frames whose
// positions all fall within this distance occupies a single location and has no
// through-plane extent to reconstruct (e.g. a cine / multi-time series).
const zeroExtentTolerance = 0.01;
/**
* Checks if a series is reconstructable to a 3D volume.
*
* @param {Object[]} instances An array of `OHIFInstanceMetadata` objects.
*/
export default function isDisplaySetReconstructable(instances, appConfig) {
const definedInstances = instances?.filter(Boolean) || [];
Iif (!definedInstances.length) {
return { value: false };
}
const firstInstance = definedInstances[0];
const isMultiframe = (firstInstance.NumberOfFrames || 0) > 1;
if (appConfig) {
const rows = toNumber(firstInstance.Rows);
const columns = toNumber(firstInstance.Columns);
Iif (rows > appConfig.max3DTextureSize || columns > appConfig.max3DTextureSize) {
return { value: false };
}
}
// We used to check is reconstructable modalities here, but the logic is removed
// in favor of the calculation by metadata (orientation and positions)
// Can't reconstruct if we only have one image.
if (!isMultiframe && definedInstances.length === 1) {
return { value: false };
}
// Can't reconstruct if all instances don't have the ImagePositionPatient.
if (!isMultiframe && !definedInstances.every(instance => instance.ImagePositionPatient)) {
return { value: false };
}
const sortedInstances = sortInstancesByPosition(definedInstances);
return isMultiframe ? processMultiframe(sortedInstances[0]) : processSingleframe(sortedInstances);
}
function hasPixelMeasurements(multiFrameInstance) {
const perFrameSequence = multiFrameInstance.PerFrameFunctionalGroupsSequence?.[0];
const sharedSequence = multiFrameInstance.SharedFunctionalGroupsSequence;
return (
Boolean(perFrameSequence?.PixelMeasuresSequence) ||
Boolean(sharedSequence?.PixelMeasuresSequence) ||
Boolean(
multiFrameInstance.PixelSpacing &&
(multiFrameInstance.SliceThickness || multiFrameInstance.SpacingBetweenFrames)
)
);
}
function hasOrientation(multiFrameInstance) {
const sharedSequence = multiFrameInstance.SharedFunctionalGroupsSequence;
const perFrameSequence = multiFrameInstance.PerFrameFunctionalGroupsSequence?.[0];
return (
Boolean(sharedSequence?.PlaneOrientationSequence) ||
Boolean(perFrameSequence?.PlaneOrientationSequence) ||
Boolean(
multiFrameInstance.ImageOrientationPatient ||
multiFrameInstance.DetectorInformationSequence?.[0]?.ImageOrientationPatient
)
);
}
function hasPosition(multiFrameInstance) {
const perFrameSequence = multiFrameInstance.PerFrameFunctionalGroupsSequence?.[0];
return (
Boolean(perFrameSequence?.PlanePositionSequence) ||
Boolean(perFrameSequence?.CTPositionSequence) ||
Boolean(
multiFrameInstance.ImagePositionPatient ||
multiFrameInstance.DetectorInformationSequence?.[0]?.ImagePositionPatient
)
);
}
function isNMReconstructable(multiFrameInstance) {
const imageSubType = multiFrameInstance.ImageType?.[2];
return imageSubType === 'RECON TOMO' || imageSubType === 'RECON GATED TOMO';
}
function processMultiframe(multiFrameInstance) {
// If we don't have the PixelMeasuresSequence, then the pixel spacing and
// slice thickness isn't specified or is changing and we can't reconstruct
// the dataset.
if (!hasPixelMeasurements(multiFrameInstance)) {
return { value: false };
}
if (!hasOrientation(multiFrameInstance)) {
console.log('No image orientation information, not reconstructable');
return { value: false };
}
Iif (!hasPosition(multiFrameInstance)) {
console.log('No image position information, not reconstructable');
return { value: false };
}
Iif (multiFrameInstance.Modality.includes('NM') && !isNMReconstructable(multiFrameInstance)) {
return { value: false };
}
// A multi-frame cine / multi-time series is many frames acquired at a single
// location (every frame shares one ImagePositionPatient), so it has no
// through-plane extent: building a volume yields zero slice spacing that
// renders black in every MPR plane. Treat it as non-reconstructable rather
// than a degenerate volume.
Iif (hasSingleSpatialLocation(getPerFramePositions(multiFrameInstance))) {
return { value: false };
}
// TODO - check spacing consistency
return { value: true };
}
function processSingleframe(instances) {
const firstImage = instances[0];
const firstImageRows = toNumber(firstImage.Rows);
const firstImageColumns = toNumber(firstImage.Columns);
const firstImageSamplesPerPixel = toNumber(firstImage.SamplesPerPixel);
const firstImageOrientationPatient = toNumber(firstImage.ImageOrientationPatient);
const firstImagePositionPatient = toNumber(firstImage.ImagePositionPatient);
// 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++) {
const instance = instances[i];
const { Rows, Columns, SamplesPerPixel, ImageOrientationPatient } = instance;
const imageOrientationPatient = toNumber(ImageOrientationPatient);
if (
Rows !== firstImageRows ||
Columns !== firstImageColumns ||
SamplesPerPixel !== firstImageSamplesPerPixel ||
!_isSameOrientation(imageOrientationPatient, firstImageOrientationPatient)
) {
return { value: false };
}
}
// Many single-frame instances acquired at one location (a cine / multi-time
// series) share one ImagePositionPatient and have no through-plane extent, so
// they can't form a volume. Guard it explicitly instead of relying on the
// degenerate zero-spacing path below.
Iif (
hasSingleSpatialLocation(instances.map(instance => toNumber(instance.ImagePositionPatient)))
) {
return { value: false };
}
let missingFrames = 0;
let averageSpacingBetweenFrames;
// Check if frame spacing is approximately equal within a spacingTolerance.
// 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) {
const lastIpp = toNumber(instances[instances.length - 1].ImagePositionPatient);
// We can't reconstruct if we are missing ImagePositionPatient values
Iif (!firstImagePositionPatient || !lastIpp) {
return { value: false };
}
averageSpacingBetweenFrames =
_getPerpendicularDistance(firstImagePositionPatient, lastIpp) / (instances.length - 1);
let previousImagePositionPatient = firstImagePositionPatient;
for (let i = 1; i < instances.length; i++) {
const instance = instances[i];
// Todo: get metadata from OHIF.MetadataProvider
const imagePositionPatient = toNumber(instance.ImagePositionPatient);
const spacingBetweenFrames = _getPerpendicularDistance(
imagePositionPatient,
previousImagePositionPatient
);
const spacingIssue = _getSpacingIssue(spacingBetweenFrames, averageSpacingBetweenFrames);
Iif (spacingIssue) {
const issue = spacingIssue.issue;
if (issue === reconstructionIssues.MISSING_FRAMES) {
missingFrames += spacingIssue.missingFrames;
} else Iif (issue === reconstructionIssues.IRREGULAR_SPACING) {
return { value: false };
}
}
previousImagePositionPatient = imagePositionPatient;
}
}
return { value: true, averageSpacingBetweenFrames };
}
function _isSameOrientation(iop1, iop2) {
Iif (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 &&
Math.abs(iop1[3] - iop2[3]) < iopTolerance &&
Math.abs(iop1[4] - iop2[4]) < iopTolerance &&
Math.abs(iop1[5] - iop2[5]) < iopTolerance
);
}
/**
* 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 =
Math.abs(spacing - averageSpacing) < averageSpacing * spacingTolerance;
if (equalWithinTolerance) {
return;
}
const multipleOfAverageSpacing = spacing / averageSpacing;
const numberOfSpacings = Math.round(multipleOfAverageSpacing);
const errorForEachSpacing =
Math.abs(spacing - numberOfSpacings * averageSpacing) / numberOfSpacings;
Iif (errorForEachSpacing < spacingTolerance * averageSpacing) {
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) + Math.pow(a[1] - b[1], 2) + Math.pow(a[2] - b[2], 2));
}
function isValidImagePosition(position) {
return Array.isArray(position) && position.length === 3 && position.every(Number.isFinite);
}
/**
* True when two or more frame positions are known and all collapse to one
* spatial location, so the series has no through-plane extent to reconstruct.
* Returns false whenever this can't be determined confidently (fewer than two
* valid positions), so a genuine volume is never wrongly blocked.
*
* @param {Array} positions The `ImagePositionPatient` of each frame.
*/
function hasSingleSpatialLocation(positions) {
const validPositions = positions.filter(isValidImagePosition);
if (validPositions.length < 2) {
return false;
}
const origin = validPositions[0];
return validPositions.every(
position => _getPerpendicularDistance(position, origin) < zeroExtentTolerance
);
}
/**
* The `ImagePositionPatient` of every frame of a multi-frame instance.
*
* Prefers the per-frame functional groups, but only once they carry the two
* valid positions `hasSingleSpatialLocation` needs to decide anything. Below
* that, falls back to the *shared* functional group broadcast to the frame
* count: a single-location cine has one constant position, which the more
* common DICOM encoding stores in `SharedFunctionalGroupsSequence` rather than
* repeating it per frame. Without this fallback a per-frame sequence that is
* absent — or present but too sparse to judge — would hide the constant
* position, so the single-location check would never fire and the degenerate
* volume would be wrongly reported reconstructable. Empty when neither source
* carries enough to inspect confidently.
*
* @param {Object} multiFrameInstance
*/
function getPerFramePositions(multiFrameInstance) {
const perFrameSequence = multiFrameInstance.PerFrameFunctionalGroupsSequence;
Iif (Array.isArray(perFrameSequence)) {
const perFramePositions = perFrameSequence.map(group =>
toNumber(group?.PlanePositionSequence?.[0]?.ImagePositionPatient)
);
Iif (perFramePositions.filter(isValidImagePosition).length >= 2) {
return perFramePositions;
}
}
const sharedPosition = toNumber(
multiFrameInstance.SharedFunctionalGroupsSequence?.[0]?.PlanePositionSequence?.[0]
?.ImagePositionPatient
);
if (!isValidImagePosition(sharedPosition)) {
return [];
}
const frameCount = Number(multiFrameInstance.NumberOfFrames) || perFrameSequence?.length || 0;
return new Array(frameCount).fill(sharedPosition);
}
const constructableModalities = ['MR', 'CT', 'PT', 'NM'];
const reconstructionIssues = {
MISSING_FRAMES: 'missingframes',
IRREGULAR_SPACING: 'irregularspacing',
};
export {
hasPixelMeasurements,
hasOrientation,
hasPosition,
isNMReconstructable,
_isSameOrientation,
_getSpacingIssue,
_getPerpendicularDistance,
hasSingleSpatialLocation,
getPerFramePositions,
reconstructionIssues,
constructableModalities,
};
|