UNPKG

@cornerstonejs/core

Version:
55 lines (54 loc) 1.9 kB
import { vec3 } from 'gl-matrix'; export const SOURCE_SLICE_INDEX_TOLERANCE = 1e-4; export const NEAREST_VOXEL_TIE_EPSILON = 1e-6; function dot(a, b) { return vec3.dot(a, b); } export function getIndexMajorAxis(volume, worldVector, majorThreshold = 0.995) { const row = volume.direction.slice(0, 3); const col = volume.direction.slice(3, 6); const scan = volume.direction.slice(6, 9); const components = [ dot(worldVector, row), dot(worldVector, col), dot(worldVector, scan), ]; const absComponents = components.map((value) => Math.abs(value)); const maxValue = Math.max(...absComponents); const axis = absComponents.indexOf(maxValue); if (maxValue < majorThreshold) { return; } const secondary = absComponents .filter((_value, index) => index !== axis) .some((value) => value > 1 - majorThreshold); if (secondary) { return; } return { axis, sign: components[axis] >= 0 ? 1 : -1, }; } export function getSpatiallyClampedContinuousCoordinate(dimension, value) { const upperBound = dimension - 0.5; if (value < -0.5 - SOURCE_SLICE_INDEX_TOLERANCE || value > upperBound + SOURCE_SLICE_INDEX_TOLERANCE) { return; } return Math.min(dimension - 1, Math.max(0, value)); } export function getNearestVoxelIndex(continuousIndex) { return Math.floor(continuousIndex + 0.5 - NEAREST_VOXEL_TIE_EPSILON); } export function getSpatiallyClampedContinuousIndex(dimensions, continuousIndex) { const clampedIndex = [0, 0, 0]; for (let axis = 0; axis < 3; axis++) { const clampedCoordinate = getSpatiallyClampedContinuousCoordinate(dimensions[axis], continuousIndex[axis]); if (clampedCoordinate === undefined) { return; } clampedIndex[axis] = clampedCoordinate; } return clampedIndex; }