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

0% Statements 0/11
0% Branches 0/2
0% Functions 0/1
0% Lines 0/11

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                                                                                                                   
import { vec3 } from 'gl-matrix';
import { metaData } from '..';
import { Point2, Point3 } from '../types';
 
/**
 * Given the imageId and a 2d coordinates on the image space with [0,0] being the top left corner
 * of the top left pixel, and options which includes the imageId, it returns the
 * 3d coordinates on the world space.
 * @param imageId - The image id
 * @param imageCoords - The 2d coordinates on the image
 * @returns The 3d coordinates on the world.
 *
 */
export default function imageToWorldCoords(
  imageId: string,
  imageCoords: Point2
): Point3 | undefined {
  const imagePlaneModule = metaData.get('imagePlaneModule', imageId);
 
  if (!imagePlaneModule) {
    throw new Error(`No imagePlaneModule found for imageId: ${imageId}`);
  }
 
  const {
    columnCosines,
    rowCosines,
    imagePositionPatient: origin,
  } = imagePlaneModule;
 
  let { columnPixelSpacing, rowPixelSpacing } = imagePlaneModule;
  // Use ||= to convert null and 0 as well as undefined to 1
  columnPixelSpacing ||= 1;
  rowPixelSpacing ||= 1;
 
  // calculate the image coordinates in the world space
  const imageCoordsInWorld = vec3.create();
 
  // move from origin in the direction of the row cosines with the amount of
  // row pixel spacing times the first element of the image coordinates vector
  vec3.scaleAndAdd(
    imageCoordsInWorld,
    origin,
    rowCosines,
    // to accommodate the [0,0] being on the top left corner of the top left pixel
    // but the origin is at the center of the top left pixel
    rowPixelSpacing * (imageCoords[0] - 0.5)
  );
 
  vec3.scaleAndAdd(
    imageCoordsInWorld,
    imageCoordsInWorld,
    columnCosines,
    columnPixelSpacing * (imageCoords[1] - 0.5)
  );
 
  return Array.from(imageCoordsInWorld) as Point3;
}