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 | 1002x 1002x 1002x 1002x 1002x 1002x 1002x 1002x 1002x 1002x 3006x 3006x 1002x 1002x 1002x 1002x 1002x 119390x 119390x 858x 858x 858x 2574x 2574x 858x 119390x 119390x 119390x 119390x 358170x 358170x 358170x 119390x 137x 137x 465x 111x 354x 354x 354x 465x 196x 269x 269x 269x 119102x 119102x 119102x 30452x 88650x 88650x 88650x 88650x 119250x 30452x 88798x 88798x 88798x | import { vec3 } from 'gl-matrix';
import type { Point2, Point3 } from '../../../types';
import type { IndexSpaceSlab, VolumeGeometry } from '../indexSpaceSlab';
/**
* A plane-anchored shape that can drive {@link iterateVoxelsInShape}.
*
* `containsPoint` is the definition and `getRuns` the optimisation, and the two
* must always select the same voxels. Boundaries are **inclusive**.
*
* See `docs/docs/concepts/cornerstone-tools/annotation/voxel-statistics.md`.
*/
export interface VoxelSlabShape {
/** The definition. Does the shape contain this world point? */
containsPoint(point: Point3): boolean;
/**
* The optimisation. Exact inclusive runs along `slab.columnAxis`.
*
* Signature matches `ShapeRunProvider`, so it can be handed straight to
* `iterateVoxelsInShape` as `getShapeRuns`.
*/
getRuns(
outerIndex: number,
rowIndex: number,
depthRun: Point2,
slab: IndexSpaceSlab
): Iterable<Point2>;
/**
* The smallest reference plane thickness for which Rule M's slab contains the
* whole shape. A planar shape returns 0; a shape with depth returns that
* depth, and a smaller `referencePlaneThickness` clips it.
*/
getRequiredThickness(): number;
}
/**
* An orthonormal frame for a plane: `u` and `v` span it, `n` is the normal.
*/
export interface PlaneBasis {
u: Point3;
v: Point3;
n: Point3;
}
/**
* Builds an orthonormal in-plane frame from a normal and an orientation vector.
*
* `orientation` gives the shape's major axis. It need not be unit length, nor
* already lie in the plane - its component along the normal is removed.
*
* @throws if `orientation` is parallel to the normal, since it then defines no
* in-plane direction at all.
*/
export function createPlaneBasis(
normal: Point3,
orientation: Point3
): PlaneBasis {
const n = vec3.normalize(vec3.create(), normal as vec3) as Point3;
const along = vec3.dot(orientation as vec3, n as vec3);
const u = vec3.sub(
vec3.create(),
orientation as vec3,
vec3.scale(vec3.create(), n as vec3, along)
);
const uLength = vec3.length(u);
Iif (!(uLength > 1e-12)) {
throw new Error(
'Shape orientation vector is parallel to the plane normal, so it defines no in-plane direction'
);
}
vec3.scale(u, u, 1 / uLength);
const v = vec3.cross(vec3.create(), n as vec3, u) as Point3;
return { u: u as Point3, v, n };
}
/**
* The world displacement per unit step of each voxel index.
*/
export function getAxisSteps(volume: VolumeGeometry): [Point3, Point3, Point3] {
const { direction, spacing } = volume;
return [0, 1, 2].map((axis) => {
const unit = direction.slice(axis * 3, axis * 3 + 3);
return [
unit[0] * spacing[axis],
unit[1] * spacing[axis],
unit[2] * spacing[axis],
] as Point3;
}) as [Point3, Point3, Point3];
}
/**
* The line, parameterised by column index, that a run of voxel centres traces.
*
* For fixed outer and row positions, both the centre and its projection onto
* the annotation plane are straight lines in `col`:
*
* ```
* centre(col) = base + col * step
* projected(col) = projectedBase + col * projectedStep
* projectedStep = step - g[columnAxis] * n
* ```
*
* That is what reduces every shape test to a one-dimensional intersection.
*/
export interface ColumnLine {
/** Voxel centre at column 0. */
base: Point3;
/** Voxel centre displacement per column step. */
step: Point3;
/** Projected point at column 0. */
projectedBase: Point3;
/** Projected point displacement per column step. */
projectedStep: Point3;
}
/**
* Creates a reusable resolver for the column line of a given volume and plane.
*
* The step vectors depend only on the volume and the slab, so they are computed
* once per slab; only the bases change as the outer and row indices advance.
* The returned `ColumnLine` is reused between calls - copy anything retained.
*/
export function createColumnLineResolver(
volume: VolumeGeometry,
normal: Point3
) {
const axisSteps = getAxisSteps(volume);
const { origin } = volume;
const line: ColumnLine = {
base: [0, 0, 0],
step: [0, 0, 0],
projectedBase: [0, 0, 0],
projectedStep: [0, 0, 0],
};
let preparedSlab: IndexSpaceSlab | null = null;
return function resolve(
slab: IndexSpaceSlab,
outerIndex: number,
rowIndex: number
): ColumnLine {
const { g, c0, outerAxis, rowAxis, columnAxis } = slab;
if (preparedSlab !== slab) {
const columnStep = axisSteps[columnAxis];
const gColumn = g[columnAxis];
for (let axis = 0; axis < 3; axis++) {
line.step[axis] = columnStep[axis];
line.projectedStep[axis] = columnStep[axis] - gColumn * normal[axis];
}
preparedSlab = slab;
}
const outerStep = axisSteps[outerAxis];
const rowStep = axisSteps[rowAxis];
const baseDepth = outerIndex * g[outerAxis] + rowIndex * g[rowAxis] + c0;
for (let axis = 0; axis < 3; axis++) {
const base =
origin[axis] + outerIndex * outerStep[axis] + rowIndex * rowStep[axis];
line.base[axis] = base;
line.projectedBase[axis] = base - baseDepth * normal[axis];
}
return line;
};
}
/**
* Relative slack applied to a shape's boundary, so that a voxel centre lying
* on the outline is reliably inside it.
*
* Without it `containsPoint` and `getRuns` disagree at exact ties. Matched to
* `SLAB_RELATIVE_EPSILON`, but applied only to shape outlines: Rule M's depth
* test tightens instead, because there the neighbouring layer must be excluded.
*
* See `docs/docs/concepts/cornerstone-tools/annotation/voxel-statistics.md`.
*/
export const SHAPE_BOUNDARY_EPSILON = 1e-5;
/** An inclusive real interval, possibly unbounded. */
export type RealRange = [number, number];
/** The whole real line. */
export const UNBOUNDED: RealRange = [-Infinity, Infinity];
/** Intersects two inclusive real intervals, or returns null if disjoint. */
export function intersectRanges(
a: RealRange | null,
b: RealRange | null
): RealRange | null {
if (!a || !b) {
return null;
}
const low = Math.max(a[0], b[0]);
const high = Math.min(a[1], b[1]);
return low > high ? null : [low, high];
}
/**
* The `t` satisfying `|a + t * b| <= halfExtent`, as an inclusive interval.
*
* Used for every flat-sided constraint: a box face, or the depth slab of a
* prism.
*/
export function solveAbsLinearLeq(
a: number,
b: number,
halfExtent: number
): RealRange | null {
if (b === 0) {
return Math.abs(a) <= halfExtent ? UNBOUNDED : null;
}
// The + 0 normalises a negative zero, which would otherwise survive into a
// run bound and read confusingly.
const first = (-halfExtent - a) / b + 0;
const second = (halfExtent - a) / b + 0;
return first <= second ? [first, second] : [second, first];
}
/**
* The `t` satisfying `A t^2 + B t + C <= 0`, as an inclusive interval.
*
* `A` is a squared length so it is never negative; `A === 0` degenerates to the
* linear case, which happens when the column step has no component in the
* shape's scaled space.
*/
export function solveQuadraticLeqZero(
A: number,
B: number,
C: number
): RealRange | null {
Iif (A === 0) {
if (B === 0) {
return C <= 0 ? UNBOUNDED : null;
}
const root = -C / B + 0;
return B > 0 ? [-Infinity, root] : [root, Infinity];
}
const discriminant = B * B - 4 * A * C;
if (discriminant < 0) {
return null;
}
const rootOfDiscriminant = Math.sqrt(discriminant);
const first = (-B - rootOfDiscriminant) / (2 * A) + 0;
const second = (-B + rootOfDiscriminant) / (2 * A) + 0;
return first <= second ? [first, second] : [second, first];
}
/**
* The inclusive integer run inside a real interval.
*
* Boundaries are inclusive, unlike the depth runs of Rule M, whose endpoints
* are exclusive so that a one-voxel-thick annotation selects one layer. Infinite bounds are
* preserved; the iterator intersects them with the always-finite depth run.
*/
export function toIntegerRun(range: RealRange | null): Point2 | null {
if (!range) {
return null;
}
const low = Math.ceil(range[0]);
const high = Math.floor(range[1]);
return low > high ? null : [low, high];
}
|