All files / packages/streaming-image-volume-loader/src/helpers sortImageIdsAndGetSpacing.ts

100% Statements 21/21
100% Branches 0/0
100% Functions 4/4
100% Lines 16/16

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                                            1x         1x   1x             1x 5x   5x   5x           5x   5x           4x   5x 1x           1x           1x         1x           1x    
import { vec3 } from 'gl-matrix';
import { metaData } from '@cornerstonejs/core';
import type { Types } from '@cornerstonejs/core';
 
type SortedImageIdsItem = {
  zSpacing: number;
  origin: Types.Point3;
  sortedImageIds: Array<string>;
};
/**
 * Given an array of imageIds, sort them based on their imagePositionPatient, and
 * also returns the spacing between images and the origin of the reference image
 *
 * @param imageIds - array of imageIds
 * @param scanAxisNormal - [x, y, z] array or gl-matrix vec3
 *
 * @returns The sortedImageIds, zSpacing, and origin of the first image in the series.
 */
export default function sortImageIdsAndGetSpacing(
  imageIds: Array<string>,
  scanAxisNormal: vec3 // Get gl matrix types?
): SortedImageIdsItem {
  const { imagePositionPatient: referenceImagePositionPatient } = metaData.get(
    'imagePlaneModule',
    imageIds[0]
  );
 
  const refIppVec = vec3.create();
 
  vec3.set(
    refIppVec,
    referenceImagePositionPatient[0],
    referenceImagePositionPatient[1],
    referenceImagePositionPatient[2]
  );
 
  const distanceImagePairs = imageIds.map((imageId) => {
    const { imagePositionPatient } = metaData.get('imagePlaneModule', imageId);
 
    const positionVector = vec3.create();
 
    vec3.sub(
      positionVector,
      referenceImagePositionPatient,
      imagePositionPatient
    );
 
    const distance = vec3.dot(positionVector, scanAxisNormal);
 
    return {
      distance,
      imageId,
    };
  });
 
  distanceImagePairs.sort((a, b) => b.distance - a.distance);
 
  const sortedImageIds = distanceImagePairs.map((a) => a.imageId);
  const numImages = distanceImagePairs.length;
 
  // Calculated average spacing.
  // We would need to resample if these are not similar.
  // It should be up to the host app to do this if it needed to.
  const zSpacing =
    Math.abs(
      distanceImagePairs[numImages - 1].distance -
        distanceImagePairs[0].distance
    ) /
    (numImages - 1);
 
  const { imagePositionPatient: origin } = metaData.get(
    'imagePlaneModule',
    sortedImageIds[0]
  );
 
  const result: SortedImageIdsItem = {
    zSpacing,
    origin,
    sortedImageIds,
  };
 
  return result;
}