All files / core/src/utilities/voxelSlab getVoxelThicknessAlongNormal.ts

100% Statements 5/5
100% Branches 0/0
100% Functions 1/1
100% Lines 5/5

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                                        2685x       2685x 2685x 2685x   2685x            
import type { mat3 } from 'gl-matrix';
import { vec3 } from 'gl-matrix';
import type { IImageVolume, Point3 } from '../../types';
 
/**
 * Calculates the thickness of a single voxel along `normal`: the support width
 * of the voxel box, `Σᵢ |dᵢ · n| * sᵢ`.
 *
 * This is the L1 length, deliberately **not** the L2 length that
 * {@link getSpacingInNormalDirection} returns. See
 * `docs/docs/concepts/cornerstone-tools/annotation/voxel-statistics.md`.
 *
 * @param volume - The volume, or anything carrying its `direction` and `spacing`.
 * @param normal - The direction to measure along. Assumed to be a unit vector.
 * @returns The voxel thickness along the normal, in world units (mm).
 */
export default function getVoxelThicknessAlongNormal(
  volume: IImageVolume | { direction: mat3; spacing: Point3 },
  normal: Point3
): number {
  const { direction, spacing } = volume;
 
  // Rows of the direction matrix are the unit vectors of the voxel axes, which
  // matches how getSpacingInNormalDirection and the rest of the codebase read it.
  const iVector = direction.slice(0, 3) as Point3;
  const jVector = direction.slice(3, 6) as Point3;
  const kVector = direction.slice(6, 9) as Point3;
 
  return (
    Math.abs(vec3.dot(iVector, normal as vec3)) * spacing[0] +
    Math.abs(vec3.dot(jVector, normal as vec3)) * spacing[1] +
    Math.abs(vec3.dot(kVector, normal as vec3)) * spacing[2]
  );
}