All files / packages/tools/src/utilities/dynamicVolume getDataInTime.ts

0% Statements 0/81
0% Branches 0/24
0% Functions 0/11
0% Lines 0/75

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                                                                                                                                                                                                                                                                                                                                                                                                                                                 
import { utilities, cache, Types } from '@cornerstonejs/core';
import { getVoxelOverlap } from '../segmentation/utilities';
import pointInShapeCallback from '../pointInShapeCallback';
 
/**
 * Gets the scalar data for a series of time points for either a single
 * coordinate or a segmentation mask, it will return the an array of scalar
 * data for a single coordinate or an array of arrays for a segmentation.
 *
 * @param dynamicVolume - 4D volume to compute time point data from
 * @param options - frameNumbers: which frames to use as timepoints, if left
 * blank, gets data timepoints over all frames
 * maskVolumeId: segmentationId to get timepoint data of
 * imageCoordinate: world coordinate to get timepoint data of
 * @returns
 */
function getDataInTime(
  dynamicVolume: Types.IDynamicImageVolume,
  options: {
    frameNumbers?;
    maskVolumeId?;
    imageCoordinate?;
  }
): number[] | number[][] {
  let dataInTime;
 
  // if frameNumbers is not provided, all frames are selected
  const frames = options.frameNumbers || [
    ...Array(dynamicVolume.numTimePoints).keys(),
  ];
 
  // You only need to provide either maskVolumeId OR imageCoordinate.
  // Throws error if neither maskVolumeId or imageCoordinate is given,
  // throws error if BOTH maskVolumeId and imageCoordinate is given
  if (!options.maskVolumeId && !options.imageCoordinate) {
    throw new Error(
      'You should provide either maskVolumeId or imageCoordinate'
    );
  }
 
  if (options.maskVolumeId && options.imageCoordinate) {
    throw new Error('You can only use one of maskVolumeId or imageCoordinate');
  }
 
  if (options.maskVolumeId) {
    const segmentationVolume = cache.getVolume(options.maskVolumeId);
 
    const [dataInTime, ijkCoords] = _getTimePointDataMask(
      frames,
      dynamicVolume,
      segmentationVolume
    );
 
    return [dataInTime, ijkCoords];
  }
 
  if (options.imageCoordinate) {
    const dataInTime = _getTimePointDataCoordinate(
      frames,
      options.imageCoordinate,
      dynamicVolume
    );
 
    return dataInTime;
  }
 
  return dataInTime;
}
 
function _getTimePointDataCoordinate(frames, coordinate, volume) {
  const { dimensions, imageData } = volume;
  const index = imageData.worldToIndex(coordinate);
 
  index[0] = Math.floor(index[0]);
  index[1] = Math.floor(index[1]);
  index[2] = Math.floor(index[2]);
 
  if (!utilities.indexWithinDimensions(index, dimensions)) {
    throw new Error('outside bounds');
  }
 
  // calculate offset for index
  const yMultiple = dimensions[0];
  const zMultiple = dimensions[0] * dimensions[1];
  const allScalarData = volume.getScalarDataArrays();
  const value = [];
 
  frames.forEach((frame) => {
    const activeScalarData = allScalarData[frame];
    const scalarIndex = index[2] * zMultiple + index[1] * yMultiple + index[0];
    value.push(activeScalarData[scalarIndex]);
  });
 
  return value;
}
 
function _getTimePointDataMask(frames, dynamicVolume, segmentationVolume) {
  const { imageData: maskImageData } = segmentationVolume;
  const segScalarData = segmentationVolume.getScalarData();
 
  const len = segScalarData.length;
 
  // Pre-allocate memory for array
  const nonZeroVoxelIndices = [];
  nonZeroVoxelIndices.length = len;
  const ijkCoords = [];
 
  const dimensions = segmentationVolume.dimensions;
 
  // Get the index of every non-zero voxel in mask
  let actualLen = 0;
  for (let i = 0, len = segScalarData.length; i < len; i++) {
    if (segScalarData[i] !== 0) {
      ijkCoords.push([
        i % dimensions[0],
        Math.floor((i / dimensions[0]) % dimensions[1]),
        Math.floor(i / (dimensions[0] * dimensions[1])),
      ]);
      nonZeroVoxelIndices[actualLen++] = i;
    }
  }
 
  // Trim the array to actual size
  nonZeroVoxelIndices.length = actualLen;
 
  const dynamicVolumeScalarDataArray = dynamicVolume.getScalarDataArrays();
  const values = [];
  const isSameVolume =
    dynamicVolumeScalarDataArray[0].length === len &&
    JSON.stringify(dynamicVolume.spacing) ===
      JSON.stringify(segmentationVolume.spacing);
 
  // if the segmentation mask is the same size as the dynamic volume (one frame)
  // means we can just return the scalar data for the non-zero voxels
  if (isSameVolume) {
    for (let i = 0; i < nonZeroVoxelIndices.length; i++) {
      const indexValues = [];
      frames.forEach((frame) => {
        const activeScalarData = dynamicVolumeScalarDataArray[frame];
        indexValues.push(activeScalarData[nonZeroVoxelIndices[i]]);
      });
      values.push(indexValues);
    }
 
    return [values, ijkCoords];
  }
 
  // In case that the segmentation mask is not the same size as the dynamic volume (one frame)
  // then we need to consider each voxel in the segmentation mask and check if it
  // overlaps with the other volume, and if so we need to average the values of the
  // overlapping voxels.
  const callback = ({
    pointLPS: segPointLPS,
    value: segValue,
    pointIJK: segPointIJK,
  }) => {
    // see if the value is non-zero
    if (segValue === 0) {
      // not interested
      return;
    }
 
    // Then for each non-zero voxel in the segmentation mask, we should
    // again perform the pointInShapeCallback to run the averaging callback
    // function to get the average value of the overlapping voxels.
    const overlapIJKMinMax = getVoxelOverlap(
      dynamicVolume.imageData,
      dynamicVolume.dimensions,
      dynamicVolume.spacing,
      segPointLPS
    );
 
    // count represents the number of voxels of the dynamic volume that represents
    // one voxel of the segmentation mask
    let count = 0;
    const perFrameSum = new Map();
 
    // Pre-initialize the Map
    frames.forEach((frame) => perFrameSum.set(frame, 0));
 
    const averageCallback = ({ index }) => {
      for (let i = 0; i < frames.length; i++) {
        const value = dynamicVolumeScalarDataArray[i][index];
        const frame = frames[i];
        perFrameSum.set(frame, perFrameSum.get(frame) + value);
      }
      count++;
    };
 
    pointInShapeCallback(
      dynamicVolume.imageData,
      () => true,
      averageCallback,
      overlapIJKMinMax
    );
 
    // average the values
    const averageValues = [];
    perFrameSum.forEach((sum) => {
      averageValues.push(sum / count);
    });
 
    ijkCoords.push(segPointIJK);
    values.push(averageValues);
  };
 
  // Since we have the non-zero voxel indices of the segmentation mask,
  // we theoretically can use them, however, we kind of need to compute the
  // pointLPS for each of the non-zero voxel indices, which is a bit of a pain.
  // Todo: consider using the nonZeroVoxelIndices to compute the pointLPS
  pointInShapeCallback(maskImageData, () => true, callback);
 
  return [values, ijkCoords];
}
 
export default getDataInTime;