All files / platform/core/src/classes ImageSet.ts

43.9% Statements 18/41
75% Branches 3/4
26.66% Functions 4/15
45% Lines 18/40

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                                          116x 116x 116x 116x 116x   116x 116x         116x               116x             116x 116x 116x     116x             115x               231x 231x 2432x                       115x                                                                                                                      
import guid from '../utils/guid.js';
import { Vector3 } from 'cornerstone-math';
 
type Attributes = Record<string, unknown>;
type Image = {
  StudyInstanceUID?: string;
  getData(): {
    metadata: {
      ImagePositionPatient: number[];
      ImageOrientationPatient: number[];
    };
  };
};
 
/**
 * This class defines an ImageSet object which will be used across the viewer. This object represents
 * a list of images that are associated by any arbitrary criteria being thus content agnostic. Besides the
 * main attributes (images and uid) it allows additional attributes to be appended to it (currently
 * indiscriminately, but this should be changed).
 */
class ImageSet {
  images: Image[];
  uid: string;
  instances: Image[];
  instance?: Image;
  StudyInstanceUID?: string;
 
  constructor(images: Image[]) {
    Iif (!Array.isArray(images)) {
      throw new Error('ImageSet expects an array of images');
    }
 
    // @property "images"
    Object.defineProperty(this, 'images', {
      enumerable: false,
      configurable: false,
      writable: false,
      value: images,
    });
 
    // @property "uid"
    Object.defineProperty(this, 'uid', {
      enumerable: false,
      configurable: false,
      writable: false,
      value: guid(), // Unique ID of the instance
    });
 
    this.instances = images;
    this.instance = images[0];
    this.StudyInstanceUID = this.instance?.StudyInstanceUID;
  }
 
  load: () => Promise<void>;
 
  getUID(): string {
    return this.uid;
  }
 
  setAttribute(attribute: string, value: unknown): void {
    this[attribute] = value;
  }
 
  getAttribute(attribute: string): unknown {
    return this[attribute];
  }
 
  setAttributes(attributes: Attributes): void {
    if (typeof attributes === 'object' && attributes !== null) {
      for (const [attribute, value] of Object.entries(attributes)) {
        this[attribute] = value;
      }
    }
  }
 
  getNumImages = (): number => this.images.length;
 
  getImage(index: number): Image {
    return this.images[index];
  }
 
  sortBy(sortingCallback: (a: Image, b: Image) => number): Image[] {
    return this.images.sort(sortingCallback);
  }
 
  sortByImagePositionPatient(): void {
    const images = this.images;
    const referenceImagePositionPatient = _getImagePositionPatient(images[0]);
 
    const refIppVec = new Vector3(
      referenceImagePositionPatient[0],
      referenceImagePositionPatient[1],
      referenceImagePositionPatient[2]
    );
 
    const ImageOrientationPatient = _getImageOrientationPatient(images[0]);
 
    const scanAxisNormal = new Vector3(
      ImageOrientationPatient[0],
      ImageOrientationPatient[1],
      ImageOrientationPatient[2]
    ).cross(
      new Vector3(
        ImageOrientationPatient[3],
        ImageOrientationPatient[4],
        ImageOrientationPatient[5]
      )
    );
 
    const distanceImagePairs = images.map(function (image: Image) {
      const ippVec = new Vector3(..._getImagePositionPatient(image));
      const positionVector = refIppVec.clone().sub(ippVec);
      const distance = positionVector.dot(scanAxisNormal);
 
      return {
        distance,
        image,
      };
    });
 
    distanceImagePairs.sort(function (a, b) {
      return b.distance - a.distance;
    });
 
    const sortedImages = distanceImagePairs.map(a => a.image);
 
    images.sort(function (a, b) {
      return sortedImages.indexOf(a) - sortedImages.indexOf(b);
    });
  }
}
 
function _getImagePositionPatient(image) {
  return image.getData().metadata.ImagePositionPatient;
}
 
function _getImageOrientationPatient(image) {
  return image.getData().metadata.ImageOrientationPatient;
}
 
export default ImageSet;