2024-11-27 13:08:18 -05:00
|
|
|
import { vec3 } from 'gl-matrix';
|
|
|
|
|
|
2022-09-20 14:47:18 -04:00
|
|
|
/**
|
|
|
|
|
* Combine the Per instance frame data, the shared frame data
|
|
|
|
|
* and the root data objects.
|
|
|
|
|
* The data is combined by taking nested sequence objects within
|
|
|
|
|
* the functional group sequences. Data that is directly contained
|
|
|
|
|
* within the functional group sequences, such as private creators
|
|
|
|
|
* will be ignored.
|
|
|
|
|
* This can be safely called with an undefined frame in order to handle
|
|
|
|
|
* single frame data. (eg frame is undefined is the same as frame===1).
|
|
|
|
|
*/
|
|
|
|
|
const combineFrameInstance = (frame, instance) => {
|
2024-11-27 13:08:18 -05:00
|
|
|
const {
|
|
|
|
|
PerFrameFunctionalGroupsSequence,
|
|
|
|
|
SharedFunctionalGroupsSequence,
|
|
|
|
|
NumberOfFrames,
|
|
|
|
|
SpacingBetweenSlices,
|
|
|
|
|
} = instance;
|
2022-10-13 11:07:56 -04:00
|
|
|
|
|
|
|
|
if (PerFrameFunctionalGroupsSequence || NumberOfFrames > 1) {
|
|
|
|
|
const frameNumber = Number.parseInt(frame || 1);
|
2024-11-27 13:08:18 -05:00
|
|
|
const shared = SharedFunctionalGroupsSequence
|
|
|
|
|
? Object.values(SharedFunctionalGroupsSequence[0])
|
|
|
|
|
.filter(Boolean)
|
|
|
|
|
.map(it => it[0])
|
|
|
|
|
.filter(it => typeof it === 'object')
|
|
|
|
|
: [];
|
|
|
|
|
|
|
|
|
|
const perFrame = PerFrameFunctionalGroupsSequence
|
|
|
|
|
? Object.values(PerFrameFunctionalGroupsSequence[frameNumber - 1])
|
|
|
|
|
.filter(Boolean)
|
|
|
|
|
.map(it => it[0])
|
|
|
|
|
.filter(it => typeof it === 'object')
|
|
|
|
|
: [];
|
2022-10-13 11:07:56 -04:00
|
|
|
|
2023-03-31 23:21:16 -03:00
|
|
|
// this is to fix NM multiframe datasets with position and orientation
|
|
|
|
|
// information inside DetectorInformationSequence
|
2023-09-01 16:19:39 -04:00
|
|
|
if (!instance.ImageOrientationPatient && instance.DetectorInformationSequence) {
|
2023-03-31 23:21:16 -03:00
|
|
|
instance.ImageOrientationPatient =
|
|
|
|
|
instance.DetectorInformationSequence[0].ImageOrientationPatient;
|
|
|
|
|
}
|
2024-11-27 13:08:18 -05:00
|
|
|
|
|
|
|
|
let ImagePositionPatientToUse = instance.ImagePositionPatient;
|
|
|
|
|
|
2023-09-01 16:19:39 -04:00
|
|
|
if (!instance.ImagePositionPatient && instance.DetectorInformationSequence) {
|
2024-11-27 13:08:18 -05:00
|
|
|
const imagePositionPatient = instance.DetectorInformationSequence[0].ImagePositionPatient;
|
|
|
|
|
const imageOrientationPatient = instance.ImageOrientationPatient;
|
|
|
|
|
|
|
|
|
|
// Calculate the position for the current frame
|
|
|
|
|
if (imageOrientationPatient && SpacingBetweenSlices) {
|
|
|
|
|
const rowOrientation = vec3.fromValues(
|
|
|
|
|
imageOrientationPatient[0],
|
|
|
|
|
imageOrientationPatient[1],
|
|
|
|
|
imageOrientationPatient[2]
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const colOrientation = vec3.fromValues(
|
|
|
|
|
imageOrientationPatient[3],
|
|
|
|
|
imageOrientationPatient[4],
|
|
|
|
|
imageOrientationPatient[5]
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const normalVector = vec3.cross(vec3.create(), rowOrientation, colOrientation);
|
|
|
|
|
|
|
|
|
|
const position = vec3.scaleAndAdd(
|
|
|
|
|
vec3.create(),
|
|
|
|
|
imagePositionPatient,
|
|
|
|
|
normalVector,
|
|
|
|
|
SpacingBetweenSlices * (frameNumber - 1)
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
ImagePositionPatientToUse = [position[0], position[1], position[2]];
|
|
|
|
|
}
|
2023-03-31 23:21:16 -03:00
|
|
|
}
|
2024-11-27 13:08:18 -05:00
|
|
|
console.debug('🚀 ~ ImagePositionPatientToUse:', ImagePositionPatientToUse);
|
2023-03-31 23:21:16 -03:00
|
|
|
|
|
|
|
|
const newInstance = Object.assign(instance, { frameNumber: frameNumber });
|
|
|
|
|
|
|
|
|
|
// merge the shared first then the per frame to override
|
|
|
|
|
[...shared, ...perFrame].forEach(item => {
|
|
|
|
|
Object.entries(item).forEach(([key, value]) => {
|
|
|
|
|
newInstance[key] = value;
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Todo: we should cache this combined instance somewhere, maybe add it
|
|
|
|
|
// back to the dicomMetaStore so we don't have to do this again.
|
2024-11-06 07:15:27 -05:00
|
|
|
return {
|
|
|
|
|
...newInstance,
|
2024-11-27 13:08:18 -05:00
|
|
|
ImagePositionPatient: ImagePositionPatientToUse ??
|
|
|
|
|
newInstance.ImagePositionPatient ?? [0, 0, frameNumber],
|
2024-11-06 07:15:27 -05:00
|
|
|
};
|
2022-10-13 11:07:56 -04:00
|
|
|
} else {
|
|
|
|
|
return instance;
|
|
|
|
|
}
|
2022-09-20 14:47:18 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export default combineFrameInstance;
|