NIfTI & NRRD Volumetric Medical Imaging (.nii, .nrrd, .nhdr)
@modelscript/nifti (languages/nifti) provides native WebAssembly parsing, qform quaternion and sform affine Right-Anterior-Superior (RAS) coordinate transformation validation, anatomical segmentation labelmap volume calculation, Marching Tetrahedra 3D iso-surface mesh extraction, DAEBuilder lowering, and ISO 10303-21 STEP / OpenSCAD projections for NIfTI-1 / NIfTI-2 and NRRD (Nearly Raw Raster Data) medical imaging volumes.
Supported Extensions & Standards
| Extension | Format | Description |
|---|---|---|
.nii | NIfTI-1 / NIfTI-2 | Neuroimaging & clinical volumetric format encoding 3D/4D voxel grids, pixdim spacings, and qform/sform RAS affines |
.nrrd, .nhdr | Teem / 3D Slicer NRRD | Nearly Raw Raster Data header/volume with space directions, space origin, and anatomical segmentation labelmaps |
Key Capabilities
1. Voxel-to-RAS Affine Geometry & Orientation Auditing
- Parses voxel dimensions
, millimeter spacings , unit quaternion parameters with qfac, and sformhomogeneous transformation matrices. - Detects singular affine matrices (
), left-right radiological/neurological handedness mismatches between qformandsform, and anisotropic slice gaps.
2. Segmentation Labelmaps & Marching Tetrahedra Iso-Surfacing
- Extracts organ and tumor volumes (
/ ), bounding boxes, and centroids for segmented ROIs, and triangulates watertight 3D surface meshes ( extractIsoSurfaceMesh).
3. CAD Reconstruction (STEP AP242 & OpenSCAD)
- Projects extracted anatomical iso-surfaces directly into ISO 10303-21 STEP B-Rep shells (
niftiToStep) and OpenSCADpolyhedron(...)CSG modules (niftiToScad) for patient-specific implant and surgical guide design.
Pre-Flight Diagnostics
10 rules8 error2 warning
| Code | Rule | Severity | Message |
|---|---|---|---|
M9501 | nifti-invalid-dimensions | error | Invalid NIfTI/NRRD volume dimensions: dim[0]='<ndim>', spatial size=(<nx>, <ny>, <nz>). |
M9502 | nifti-non-positive-voxel-spacing | error | Non-positive voxel spacing pixdim=(<dx>, <dy>, <dz>) mm (all spatial spacings must be > 0). |
M9503 | nifti-singular-affine-transform | error | 4x4 sform affine transformation matrix is singular or degenerate with det(A) = '<detA>'. |
M9504 | nifti-invalid-quaternion-norm | error | Invalid qform quaternion parameters (<b>, <c>, <d>): b^2 + c^2 + d^2 = '<normSq>' exceeds 1.0. |
M9505 | nifti-qform-sform-handedness-mismatch | error | Coordinate handedness mismatch between active qform (qfac sign '<qfacSign>') and sform (det sign '<sformDetSign>'). |
M9506 | nifti-zero-intensity-scale-slope | error | Intensity scaling has scl_slope='<sclSlope>' with active scaling (scl_inter='<sclInter>'), zeroing all voxel intensities. |
M9507 | nifti-unsupported-datatype | error | Unsupported or invalid NIfTI/NRRD voxel datatype '<datatypeName>' (code='<datatypeCode>'). |
M9508 | nifti-voxel-data-size-mismatch | error | Voxel payload count '<actualCount>' does not match expected '<expectedCount>' for dimensions <dimsDesc>. |
M9509 | nifti-empty-segmentation-mask | warning | Segmentation labelmap '<title>' contains zero foreground voxels across all anatomical labels. |
M9510 | nifti-anisotropic-slice-gap-warning | warning | Slice thickness dz='<dz>' mm exceeds in-plane resolution '<minInPlane>' mm by '<ratio>x' (> 5x threshold), risking severe 3D stair-step artifacts. |
Generated from languages/nifti/src/lints/rules.ts. All languages →
CLI Usage
bash
# Inspect NIfTI/NRRD dimensions, voxel spacings, and RAS affine transforms
msx nifti inspect BrainAtlas.nii
# Validate qform/sform affine non-singularity and handedness consistency
msx nifti validate BrainAtlas.nii
# Extract 3D iso-surface mesh and ROI volumes from segmentation volume
msx nifti mesh BrainAtlas.nii
# Export reconstructed anatomical geometry to ISO 10303-21 STEP or OpenSCAD
msx nifti to-step BrainAtlas.nii > BrainAtlas.stp
msx nifti to-scad BrainAtlas.nii > BrainAtlas.scad
