All files / packages/core/src/utilities VoxelManager.ts

36.87% Statements 59/160
26.92% Branches 14/52
33.33% Functions 13/39
37.9% Lines 58/153

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 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484                              1x           19x 19x                       19x                                       19x 19x 19x 19x 19x           19x                 19x                           1x     1x 1x   1x                                           19x             19x         11988x         19x 11988x 11988x 11988x 11988x               11988x                                                     19x                                                                                                               1x             11988x                 11988x 11988x 11988x 11988x 11988x 11988x                                                                                                     18x         18x 18x       18x           18x             18x                     18x   11988x   5994x 5994x 5994x     18x 18x                                                     1x 1x 1x       5994x 5994x 5994x       5994x       5994x     1x 1x 1x 1x                                                                                                                                                                                      
import type {
  BoundsIJK,
  Point3,
  PixelDataTypedArray,
  IImage,
  RGB,
} from '../types';
import RLEVoxelMap from './RLEVoxelMap';
import isEqual from './isEqual';
 
/**
 * Have a default size for cached RLE encoded images.  This is hard to guess
 * up front because the RLE is usually used to store new/updated data, but this
 * is a first guess.
 */
const DEFAULT_RLE_SIZE = 5 * 1024;
 
/**
 * This is a simple, standard interface to values associated with a voxel.
 */
export default class VoxelManager<T> {
  public modifiedSlices = new Set<number>();
  public boundsIJK = [
    [Infinity, -Infinity],
    [Infinity, -Infinity],
    [Infinity, -Infinity],
  ] as BoundsIJK;
 
  // Provide direct access to the underlying data, if any
  public scalarData: PixelDataTypedArray;
  public map: Map<number, T> | RLEVoxelMap<T>;
  public sourceVoxelManager: VoxelManager<T>;
  public isInObject: (pointIPS, pointIJK) => boolean;
  public readonly dimensions: Point3;
  public numComps = 1;
 
  points: Set<number>;
  width: number;
  frameSize: number;
  _get: (index: number) => T;
  _set: (index: number, v: T) => boolean | void;
 
  /**
   * Creates a generic voxel value accessor, with access to the values
   * provided by the _get and optionally _set values.
   * @param dimensions - for the voxel volume
   * @param _get - called to get a value by index
   * @param _set  - called when setting a value
   */
  constructor(
    dimensions,
    _get: (index: number) => T,
    _set?: (index: number, v: T) => boolean | void
  ) {
    this.dimensions = dimensions;
    this.width = dimensions[0];
    this.frameSize = this.width * dimensions[1];
    this._get = _get;
    this._set = _set;
  }
 
  /**
   * Gets the voxel value at position i,j,k.
   */
  public getAtIJK = (i, j, k) => {
    const index = i + j * this.width + k * this.frameSize;
    return this._get(index);
  };
 
  /**
   * Sets the voxel value at position i,j,k and records the slice
   * that was modified.
   */
  public setAtIJK = (i: number, j: number, k: number, v) => {
    const index = i + j * this.width + k * this.frameSize;
    if (this._set(index, v) !== false) {
      this.modifiedSlices.add(k);
      VoxelManager.addBounds(this.boundsIJK, [i, j, k]);
    }
  };
 
  /**
   * Adds a point as an array or an index value to the set of points
   * associated with this voxel value.
   * Can be used for tracking clicked points or other modified values.
   */
  public addPoint(point: Point3 | number) {
    const index = Array.isArray(point)
      ? point[0] + this.width * point[1] + this.frameSize * point[2]
      : point;
    Eif (!this.points) {
      this.points = new Set<number>();
    }
    this.points.add(index);
  }
 
  /**
   * Gets the list of added points as an array of Point3 values
   */
  public getPoints(): Point3[] {
    return this.points
      ? [...this.points].map((index) => this.toIJK(index))
      : [];
  }
 
  /**
   * Gets the points added using addPoint as an array of indices.
   */
  public getPointIndices(): number[] {
    return this.points ? [...this.points] : [];
  }
 
  /**
   * Gets the voxel value at the given Point3 location.
   */
  public getAtIJKPoint = ([i, j, k]) => this.getAtIJK(i, j, k);
 
  /**
   * Sets the voxel value at the given point3 location to the specified value.
   * Records the z index modified.
   * Will record the index value if the VoxelManager is backed by a map.
   */
  public setAtIJKPoint = ([i, j, k]: Point3, v) => this.setAtIJK(i, j, k, v);
 
  /**
   * Gets the value at the given index.
   */
  public getAtIndex = (index) => this._get(index);
 
  /**
   * Sets the value at the given index
   */
  public setAtIndex = (index, v) => {
    Eif (this._set(index, v) !== false) {
      const pointIJK = this.toIJK(index);
      this.modifiedSlices.add(pointIJK[2]);
      VoxelManager.addBounds(this.boundsIJK, pointIJK);
    }
  };
 
  /**
   * Converts an index value to a Point3 IJK value
   */
  public toIJK(index: number): Point3 {
    return [
      index % this.width,
      Math.floor((index % this.frameSize) / this.width),
      Math.floor(index / this.frameSize),
    ];
  }
 
  /**
   * Converts an IJK Point3 value to an index value
   */
  public toIndex(ijk: Point3) {
    return ijk[0] + ijk[1] * this.width + ijk[2] * this.frameSize;
  }
 
  /**
   * Gets the bounds for the modified set of values.
   */
  public getBoundsIJK(): BoundsIJK {
    if (this.boundsIJK[0][0] < this.dimensions[0]) {
      return this.boundsIJK;
    }
    return this.dimensions.map((dimension) => [0, dimension - 1]) as BoundsIJK;
  }
 
  /**
   * Iterate over the points within the bounds, or the modified points if recorded.
   */
  public forEach = (callback, options?) => {
    const boundsIJK = options?.boundsIJK || this.getBoundsIJK();
    const { isWithinObject } = options || {};
    if (this.map) {
      // Optimize this for only values in the map
      for (const index of this.map.keys()) {
        const pointIJK = this.toIJK(index);
        const value = this._get(index);
        const callbackArguments = { value, index, pointIJK };
        if (isWithinObject?.(callbackArguments) === false) {
          continue;
        }
        callback(callbackArguments);
      }
    } else {
      for (let k = boundsIJK[2][0]; k <= boundsIJK[2][1]; k++) {
        const kIndex = k * this.frameSize;
        for (let j = boundsIJK[1][0]; j <= boundsIJK[1][1]; j++) {
          const jIndex = kIndex + j * this.width;
          for (
            let i = boundsIJK[0][0], index = jIndex + i;
            i <= boundsIJK[0][1];
            i++, index++
          ) {
            const value = this.getAtIndex(index);
            const callbackArguments = { value, index, pointIJK: [i, j, k] };
            if (isWithinObject?.(callbackArguments) === false) {
              continue;
            }
            callback(callbackArguments);
          }
        }
      }
    }
  };
 
  /**
   * Clears any map specific data, as wellas the modified slices, points and
   * bounds.
   */
  public clear() {
    if (this.map) {
      this.map.clear();
    }
    this.boundsIJK.map((bound) => {
      bound[0] = Infinity;
      bound[1] = -Infinity;
    });
    this.modifiedSlices.clear();
    this.points?.clear();
  }
 
  /**
   * @returns The array of modified k indices
   */
  public getArrayOfSlices(): number[] {
    return Array.from(this.modifiedSlices);
  }
 
  /**
   * Extends the bounds of this object to include the specified point
   */
  public static addBounds(bounds: BoundsIJK, point: Point3) {
    Iif (!bounds) {
      bounds = [
        [Infinity, -Infinity],
        [Infinity, -Infinity],
        [Infinity, -Infinity],
      ];
    }
 
    // Directly update the bounds for each axis
    bounds[0][0] = Math.min(point[0], bounds[0][0]);
    bounds[0][1] = Math.max(point[0], bounds[0][1]);
    bounds[1][0] = Math.min(point[1], bounds[1][0]);
    bounds[1][1] = Math.max(point[1], bounds[1][1]);
    bounds[2][0] = Math.min(point[2], bounds[2][0]);
    bounds[2][1] = Math.max(point[2], bounds[2][1]);
  }
 
  /**
   * Gets the pixel data for the given array.
   */
  public getPixelData: (
    sliceIndex?: number,
    pixelData?: PixelDataTypedArray
  ) => PixelDataTypedArray;
 
  /**
   * Creates a voxel manager backed by an array of scalar data having the
   * given number of components.
   * Note that the number of components can be larger than three, in case data
   * is stored in additional pixels.  However, the return type is still RGB.
   */
  public static createRGBVolumeVoxelManager(
    dimensions: Point3,
    scalarData,
    numComponents
  ): VoxelManager<RGB> {
    const voxels = new VoxelManager<RGB>(
      dimensions,
      (index) => {
        index *= numComponents;
        return [scalarData[index++], scalarData[index++], scalarData[index++]];
      },
      (index, v) => {
        index *= 3;
        const isChanged = !isEqual(scalarData[index], v);
        scalarData[index++] = v[0];
        scalarData[index++] = v[1];
        scalarData[index++] = v[2];
        return isChanged;
      }
    );
    voxels.numComps = numComponents;
    voxels.scalarData = scalarData;
    return voxels;
  }
 
  /**
   *  Creates a volume value accessor, based on a volume scalar data instance.
   * This also works for image value accessors for single plane (k=0) accessors.
   */
  public static createVolumeVoxelManager(
    dimensions: Point3,
    scalarData,
    numComponents = 0
  ): VoxelManager<number> | VoxelManager<RGB> {
    Iif (dimensions.length !== 3) {
      throw new Error(
        'Dimensions must be provided as [number, number, number] for [width, height, depth]'
      );
    }
    Eif (!numComponents) {
      numComponents =
        scalarData.length / dimensions[0] / dimensions[1] / dimensions[2];
      // We only support 1,3,4 component data, and sometimes the scalar data
      // doesn't match for some reason, so throw an exception
      Iif (numComponents > 4 || numComponents < 1 || numComponents === 2) {
        throw new Error(
          `Number of components ${numComponents} must be 1, 3 or 4`
        );
      }
    }
    Iif (numComponents > 1) {
      return VoxelManager.createRGBVolumeVoxelManager(
        dimensions,
        scalarData,
        numComponents
      );
    }
    return VoxelManager.createNumberVolumeVoxelManager(dimensions, scalarData);
  }
 
  /**
   * Creates a volume voxel manager that works on single numeric values stored
   * in an array like structure of numbers.
   */
  public static createNumberVolumeVoxelManager(
    dimensions: Point3,
    scalarData
  ): VoxelManager<number> {
    const voxels = new VoxelManager<number>(
      dimensions,
      (index) => scalarData[index],
      (index, v) => {
        const isChanged = scalarData[index] !== v;
        scalarData[index] = v;
        return isChanged;
      }
    );
    voxels.scalarData = scalarData;
    return voxels;
  }
 
  /**
   * Creates a volume map value accessor.  This is initially empty and
   * the map stores the index to value instances.
   * This is useful for sparse matrices containing pixel data.
   */
  public static createMapVoxelManager<T>(dimension: Point3): VoxelManager<T> {
    const map = new Map<number, T>();
    const voxelManager = new VoxelManager(
      dimension,
      map.get.bind(map),
      (index, v) => map.set(index, v) && true
    );
    voxelManager.map = map;
    return voxelManager;
  }
 
  /**
   * Creates a history remembering voxel manager.
   * This will remember the original values in the voxels, and will apply the
   * update to the underlying source voxel manager.
   */
  public static createHistoryVoxelManager<T>(
    sourceVoxelManager: VoxelManager<T>
  ): VoxelManager<T> {
    const map = new Map<number, T>();
    const { dimensions } = sourceVoxelManager;
    const voxelManager = new VoxelManager(
      dimensions,
      (index) => map.get(index),
      function (index, v) {
        if (!map.has(index)) {
          const oldV = this.sourceVoxelManager.getAtIndex(index);
          Iif (oldV === v) {
            // No-op
            return false;
          }
          map.set(index, oldV);
        } else Eif (v === map.get(index)) {
          map.delete(index);
        }
        this.sourceVoxelManager.setAtIndex(index, v);
      }
    );
    voxelManager.map = map;
    voxelManager.scalarData = sourceVoxelManager.scalarData;
    voxelManager.sourceVoxelManager = sourceVoxelManager;
    return voxelManager;
  }
 
  /**
   * Creates a lazy voxel manager that will create an image plane as required
   * for each slice of a volume as it gets changed.  This can be used to
   * store image data that gets created as required.
   */
  public static createLazyVoxelManager<T>(
    dimensions: Point3,
    planeFactory: (width: number, height: number) => T
  ): VoxelManager<T> {
    const map = new Map<number, T>();
    const [width, height, depth] = dimensions;
    const planeSize = width * height;
 
    const voxelManager = new VoxelManager(
      dimensions,
      (index) => map.get(Math.floor(index / planeSize))?.[index % planeSize],
      (index, v) => {
        const k = Math.floor(index / planeSize);
        let layer = map.get(k);
        if (!layer) {
          layer = planeFactory(width, height);
          map.set(k, layer);
        }
        layer[index % planeSize] = v;
      }
    );
    voxelManager.map = map;
    return voxelManager;
  }
 
  /**
   * Creates a RLE based voxel manager.  This is effective for storing
   * segmentation maps or already RLE encoded data such as ultrasounds.
   */
  public static createRLEVoxelManager<T>(dimensions: Point3): VoxelManager<T> {
    const [width, height, depth] = dimensions;
    const map = new RLEVoxelMap<T>(width, height, depth);
 
    const voxelManager = new VoxelManager<T>(
      dimensions,
      (index) => map.get(index),
      (index, v) => map.set(index, v)
    );
    voxelManager.map = map;
    voxelManager.getPixelData = map.getPixelData.bind(map);
    return voxelManager;
  }
 
  /**
   * This method adds a voxelManager instance to the image object
   * where the object added is of type:
   * 1. RLE map if the scalar data is missing or too small (dummy data)
   * 2. Volume VoxelManager scalar data representations
   */
  public static addInstanceToImage(image: IImage) {
    const { width, height } = image;
    const scalarData = image.getPixelData();
    // This test works for single images, or single representations of images
    // from a volume representation, for grayscale, indexed and RGB or RGBA images.
    if (scalarData?.length >= width * height) {
      // This case means there is enough scalar data for at least one image,
      // with 1 or more components, and creates a volume voxel manager
      // that can lookup the data
      image.voxelManager = VoxelManager.createVolumeVoxelManager(
        [width, height, 1],
        scalarData
      );
      return;
    }
    // This case occurs when the image data is a dummy image data set
    // created just to prevent exceptions in the caching logic.  Then, the
    // RLE voxel manager can be created to store the data instead.
    image.voxelManager = VoxelManager.createRLEVoxelManager<number>([
      width,
      height,
      1,
    ]);
    // The RLE voxel manager knows how to get scalar data pixel data representations.
    // That allows using the RLE representation as a normal pixel data representation
    // for VIEWING purposes.
    image.getPixelData = image.voxelManager.getPixelData;
    // Assign a different size to the cached data because this is actually
    // storing an RLE representation, which doesn't have an up front size.
    image.sizeInBytes = DEFAULT_RLE_SIZE;
  }
}
 
export type { VoxelManager };