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

0% Statements 0/64
0% Branches 0/14
0% Functions 0/16
0% Lines 0/63

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                                                                                                                                                                                                                                                                                                                                                                                                                                               
import { eventTarget, triggerEvent, type Types } from '@cornerstonejs/core';
import BaseStreamingImageVolume from './BaseStreamingImageVolume';
import { Events as StreamingEvents } from './enums';
 
type TimePoint = {
  /** imageIds of each timepoint  */
  imageIds: Array<string>;
  /** volume scalar data  */
  scalarData: Types.PixelDataTypedArray;
};
 
/**
 * Streaming Image Volume Class that extends StreamingImageVolume base class.
 * It implements load method to load the imageIds and insert them into the volume.
 */
export default class StreamingDynamicImageVolume
  extends BaseStreamingImageVolume
  implements Types.IDynamicImageVolume
{
  private _numTimePoints: number;
  private _timePoints: TimePoint[];
  private _timePointIndex = 0;
  private _splittingTag: string;
 
  constructor(
    imageVolumeProperties: Types.ImageVolumeProps & { splittingTag: string },
    streamingProperties: Types.IStreamingVolumeProperties
  ) {
    StreamingDynamicImageVolume._ensureValidData(
      imageVolumeProperties,
      streamingProperties
    );
 
    super(imageVolumeProperties, streamingProperties);
    this._numTimePoints = (<Types.PixelDataTypedArray[]>this.scalarData).length;
    this._timePoints = this._getTimePointsData();
    this._splittingTag = imageVolumeProperties.splittingTag;
  }
 
  private static _ensureValidData(
    imageVolumeProperties: Types.ImageVolumeProps,
    streamingProperties: Types.IStreamingVolumeProperties
  ): void {
    const imageIds = streamingProperties.imageIds;
    const scalarDataArrays = <Types.PixelDataTypedArray[]>(
      imageVolumeProperties.scalarData
    );
 
    if (imageIds.length % scalarDataArrays.length !== 0) {
      throw new Error(
        `Number of imageIds is not a multiple of ${scalarDataArrays.length}`
      );
    }
  }
 
  /**
   * Use the image ids and scalar data array to create TimePoint objects
   * and make it a bit easier to work with when loading requests
   */
  private _getTimePointsData(): TimePoint[] {
    const { imageIds } = this;
    const scalarData = <Types.PixelDataTypedArray[]>this.scalarData;
 
    const { numFrames } = this;
    const numTimePoints = scalarData.length;
    const timePoints: TimePoint[] = [];
 
    for (let i = 0; i < numTimePoints; i++) {
      const start = i * numFrames;
      const end = start + numFrames;
 
      timePoints.push({
        imageIds: imageIds.slice(start, end),
        scalarData: scalarData[i],
      });
    }
 
    return timePoints;
  }
 
  private _getTimePointsToLoad() {
    const timePoints = this._timePoints;
    const initialTimePointIndex = this._timePointIndex;
    const timePointsToLoad = [timePoints[initialTimePointIndex]];
 
    let leftIndex = initialTimePointIndex - 1;
    let rightIndex = initialTimePointIndex + 1;
 
    while (leftIndex >= 0 || rightIndex < timePoints.length) {
      if (leftIndex >= 0) {
        timePointsToLoad.push(timePoints[leftIndex--]);
      }
 
      if (rightIndex < timePoints.length) {
        timePointsToLoad.push(timePoints[rightIndex++]);
      }
    }
 
    return timePointsToLoad;
  }
 
  private _getTimePointRequests = (timePoint, priority: number) => {
    const { imageIds } = timePoint;
 
    return this.getImageIdsRequests(imageIds, priority);
  };
 
  private _getTimePointsRequests = (priority: number) => {
    const timePoints = this._getTimePointsToLoad();
    let timePointsRequests = [];
 
    timePoints.forEach((timePoint) => {
      const timePointRequests = this._getTimePointRequests(timePoint, priority);
      timePointsRequests = timePointsRequests.concat(timePointRequests);
    });
 
    return timePointsRequests;
  };
 
  public getImageIdsToLoad(): string[] {
    const timePoints = this._getTimePointsToLoad();
    let imageIds = [];
 
    timePoints.forEach((timePoint) => {
      const { imageIds: timePointIds } = timePoint;
      imageIds = imageIds.concat(timePointIds);
    });
 
    return imageIds;
  }
 
  /** return true if it is a 4D volume or false if it is 3D volume */
  public isDynamicVolume(): boolean {
    return true;
  }
 
  /**
   * Returns the active time point index
   * @returns active time point index
   */
  public get timePointIndex(): number {
    return this._timePointIndex;
  }
 
  /**
   * Set the active time point index which also updates the active scalar data
   * @returns current time point index
   */
  public set timePointIndex(newTimePointIndex: number) {
    if (newTimePointIndex < 0 || newTimePointIndex >= this.numTimePoints) {
      throw new Error(`Invalid timePointIndex (${newTimePointIndex})`);
    }
 
    // Nothing to do when time point index does not change
    if (this._timePointIndex === newTimePointIndex) {
      return;
    }
 
    const { imageData } = this;
 
    this._timePointIndex = newTimePointIndex;
    imageData.getPointData().setActiveScalars(`timePoint-${newTimePointIndex}`);
    this.invalidateVolume(true);
 
    triggerEvent(
      eventTarget,
      StreamingEvents.DYNAMIC_VOLUME_TIME_POINT_INDEX_CHANGED,
      {
        volumeId: this.volumeId,
        timePointIndex: newTimePointIndex,
        numTimePoints: this.numTimePoints,
        splittingTag: this.splittingTag,
      }
    );
  }
 
  /**
   * Returns the splitting tag used to split the imageIds in 4D volume
   */
  public get splittingTag(): string {
    return this._splittingTag;
  }
 
  /**
   * Returns the number of time points
   * @returns number of time points
   */
  public get numTimePoints(): number {
    return this._numTimePoints;
  }
 
  /**
   * Return the active scalar data (buffer)
   * @returns volume scalar data
   */
  public getScalarData(): Types.PixelDataTypedArray {
    return (<Types.PixelDataTypedArray[]>this.scalarData)[this._timePointIndex];
  }
 
  /**
   * It returns the imageLoad requests for the streaming image volume instance.
   * It involves getting all the imageIds of the volume and creating a success callback
   * which would update the texture (when the image has loaded) and the failure callback.
   * Note that this method does not execute the requests but only returns the requests.
   * It can be used for sorting requests outside of the volume loader itself
   * e.g. loading a single slice of CT, followed by a single slice of PET (interleaved), before
   * moving to the next slice.
   *
   * @returns Array of requests including imageId of the request, its imageIdIndex,
   * options (targetBuffer and scaling parameters), and additionalDetails (volumeId)
   */
  public getImageLoadRequests = (priority: number) => {
    return this._getTimePointsRequests(priority);
  };
}