Skip to content

Commit

Permalink
Move orientation related to own file
Browse files Browse the repository at this point in the history
  • Loading branch information
ivmartel committed Apr 25, 2024
1 parent e9ba0c8 commit 4af0c4f
Show file tree
Hide file tree
Showing 10 changed files with 278 additions and 219 deletions.
2 changes: 1 addition & 1 deletion src/app/application.js
Expand Up @@ -4,7 +4,7 @@ import {
getMatrixFromName,
getOrientationStringLPS,
Orientation
} from '../math/matrix';
} from '../math/orientation';
import {Point3D} from '../math/point';
import {Stage} from '../gui/stage';
import {Style} from '../gui/style';
Expand Down
2 changes: 1 addition & 1 deletion src/dicom/dicomElementsWrapper.js
Expand Up @@ -13,7 +13,7 @@ import {
getTagFromKey
} from './dicomTag';
import {isNativeLittleEndian} from './dataReader';
import {getOrientationFromCosines} from '../math/matrix';
import {getOrientationFromCosines} from '../math/orientation';
import {Spacing} from '../image/spacing';
import {logger} from '../utils/logger';

Expand Down
2 changes: 1 addition & 1 deletion src/dicom/dicomParser.js
Expand Up @@ -15,7 +15,7 @@ import {
import {DataReader} from './dataReader';
import {logger} from '../utils/logger';
import {arrayEquals} from '../utils/array';
import {Orientation} from '../math/matrix';
import {Orientation} from '../math/orientation';

// doc imports
/* eslint-disable no-unused-vars */
Expand Down
3 changes: 2 additions & 1 deletion src/gui/layerGroup.js
@@ -1,4 +1,5 @@
import {getIdentityMat33, getCoronalMat33} from '../math/matrix';
import {getIdentityMat33} from '../math/matrix';
import {getCoronalMat33} from '../math/orientation';
import {Index} from '../math/index';
import {Point} from '../math/point';
import {Vector3D} from '../math/vector';
Expand Down
6 changes: 2 additions & 4 deletions src/index.js
Expand Up @@ -88,10 +88,8 @@ import {Point, Point2D, Point3D} from './math/point';
import {Vector3D} from './math/vector';
import {Index} from './math/index';
import {Scalar2D, Scalar3D} from './math/scalar';
import {
Matrix33,
Orientation
} from './math/matrix';
import {Matrix33} from './math/matrix';
import {Orientation} from './math/orientation';
import {getEllipseIndices} from './math/ellipse';
// tools
import {toolList} from './tools/index';
Expand Down
164 changes: 0 additions & 164 deletions src/math/matrix.js
Expand Up @@ -357,167 +357,3 @@ export function getIdentityMat33() {
export function isIdentityMat33(mat33) {
return mat33.equals(getIdentityMat33());
}

/**
* Create a 3x3 coronal (xzy) matrix.
*
* @returns {Matrix33} The coronal matrix.
*/
export function getCoronalMat33() {
/* eslint-disable array-element-newline */
return new Matrix33([
1, 0, 0,
0, 0, 1,
0, -1, 0
]);
/* eslint-enable array-element-newline */
}

/**
* Create a 3x3 sagittal (yzx) matrix.
*
* @returns {Matrix33} The sagittal matrix.
*/
export function getSagittalMat33() {
/* eslint-disable array-element-newline */
return new Matrix33([
0, 0, -1,
1, 0, 0,
0, -1, 0
]);
/* eslint-enable array-element-newline */
}

/**
* Default anatomical plane orientations.
*/
export const Orientation = {
/**
* Axial, also known as transverse.
*/
Axial: 'axial',
/**
* Coronal, also known as frontal.
*/
Coronal: 'coronal',
/**
* Sagittal, also known as anteroposterior.
*/
Sagittal: 'sagittal'
};

/**
* Get an orientation matrix from a name.
*
* @param {string} name The orientation name.
* @returns {Matrix33|undefined} The orientation matrix.
*/
export function getMatrixFromName(name) {
let matrix;
if (name === Orientation.Axial) {
matrix = getIdentityMat33();
} else if (name === Orientation.Coronal) {
matrix = getCoronalMat33();
} else if (name === Orientation.Sagittal) {
matrix = getSagittalMat33();
}
return matrix;
}

/**
* Get the orientation code of an orientation matrix. Each letter defines
* the towards direction. Letters are: R (right), L (left),
* A (anterior), P (posterior), I (inferior) and S (superior).
*
* @param {Matrix33} matrix The orientation matrix.
* @returns {string} The orientation code.
*/
export function getOrientationStringLPS(matrix) {
const v0 = new Vector3D(
matrix.get(0, 0),
matrix.get(1, 0),
matrix.get(2, 0)
);
const v1 = new Vector3D(
matrix.get(0, 1),
matrix.get(1, 1),
matrix.get(2, 1)
);
const v2 = new Vector3D(
matrix.get(0, 2),
matrix.get(1, 2),
matrix.get(2, 2)
);
return getVectorStringLPS(v0) +
getVectorStringLPS(v1) +
getVectorStringLPS(v2);
}

/**
* Get the orientation code of an orientation vector.
* Credits: David Clunie, https://www.dclunie.com/medical-image-faq/html/part2.html
*
* @param {*} vector The orientation vector.
* @returns {string} The orientation code.
*/
function getVectorStringLPS(vector) {
let abs = new Vector3D(
Math.abs(vector.getX()),
Math.abs(vector.getY()),
Math.abs(vector.getZ())
);

let orientation = '';
const orientationX = vector.getX() < 0 ? 'R' : 'L';
const orientationY = vector.getY() < 0 ? 'A' : 'P';
// as defined in DICOM
//const orientationZ = vector.getZ() < 0 ? 'F' : 'H';
const orientationZ = vector.getZ() < 0 ? 'I' : 'S';

const threshold = 0.0001;

for (let i = 0; i < 3; i++) {
if (abs.getX() > threshold &&
abs.getX() > abs.getY() &&
abs.getX() > abs.getZ()) {
orientation += orientationX;
abs = new Vector3D(0, abs.getY(), abs.getZ());
} else if (abs.getY() > threshold &&
abs.getY() > abs.getX() &&
abs.getY() > abs.getZ()) {
orientation += orientationY;
abs = new Vector3D(abs.getX(), 0, abs.getZ());
} else if (abs.getZ() > threshold &&
abs.getZ() > abs.getX() &&
abs.getZ() > abs.getY()) {
orientation += orientationZ;
abs = new Vector3D(abs.getX(), abs.getY(), 0);
} else {
break;
}
}

return orientation;
}

/**
* Get the orientation matrix associated to the direction cosines.
*
* @param {number[]} cosines The direction cosines.
* @returns {Matrix33} The orientation matrix.
*/
export function getOrientationFromCosines(cosines) {
let orientationMatrix;
if (typeof cosines !== 'undefined' && cosines.length === 6) {
const rowCosines = new Vector3D(cosines[0], cosines[1], cosines[2]);
const colCosines = new Vector3D(cosines[3], cosines[4], cosines[5]);
const normal = rowCosines.crossProduct(colCosines);
/* eslint-disable array-element-newline */
orientationMatrix = new Matrix33([
rowCosines.getX(), colCosines.getX(), normal.getX(),
rowCosines.getY(), colCosines.getY(), normal.getY(),
rowCosines.getZ(), colCosines.getZ(), normal.getZ()
]);
}
return orientationMatrix;
}
169 changes: 169 additions & 0 deletions src/math/orientation.js
@@ -0,0 +1,169 @@
import {Vector3D} from './vector';
import {
Matrix33,
getIdentityMat33
} from './matrix';

/**
* Create a 3x3 coronal (xzy) matrix.
*
* @returns {Matrix33} The coronal matrix.
*/
export function getCoronalMat33() {
/* eslint-disable array-element-newline */
return new Matrix33([
1, 0, 0,
0, 0, 1,
0, -1, 0
]);
/* eslint-enable array-element-newline */
}

/**
* Create a 3x3 sagittal (yzx) matrix.
*
* @returns {Matrix33} The sagittal matrix.
*/
export function getSagittalMat33() {
/* eslint-disable array-element-newline */
return new Matrix33([
0, 0, -1,
1, 0, 0,
0, -1, 0
]);
/* eslint-enable array-element-newline */
}

/**
* Default anatomical plane orientations.
*/
export const Orientation = {
/**
* Axial, also known as transverse.
*/
Axial: 'axial',
/**
* Coronal, also known as frontal.
*/
Coronal: 'coronal',
/**
* Sagittal, also known as anteroposterior.
*/
Sagittal: 'sagittal'
};

/**
* Get an orientation matrix from a name.
*
* @param {string} name The orientation name.
* @returns {Matrix33|undefined} The orientation matrix.
*/
export function getMatrixFromName(name) {
let matrix;
if (name === Orientation.Axial) {
matrix = getIdentityMat33();
} else if (name === Orientation.Coronal) {
matrix = getCoronalMat33();
} else if (name === Orientation.Sagittal) {
matrix = getSagittalMat33();
}
return matrix;
}

/**
* Get the orientation code of an orientation matrix. Each letter defines
* the towards direction. Letters are: R (right), L (left),
* A (anterior), P (posterior), I (inferior) and S (superior).
*
* @param {Matrix33} matrix The orientation matrix.
* @returns {string} The orientation code.
*/
export function getOrientationStringLPS(matrix) {
const v0 = new Vector3D(
matrix.get(0, 0),
matrix.get(1, 0),
matrix.get(2, 0)
);
const v1 = new Vector3D(
matrix.get(0, 1),
matrix.get(1, 1),
matrix.get(2, 1)
);
const v2 = new Vector3D(
matrix.get(0, 2),
matrix.get(1, 2),
matrix.get(2, 2)
);
return getVectorStringLPS(v0) +
getVectorStringLPS(v1) +
getVectorStringLPS(v2);
}

/**
* Get the orientation code of an orientation vector.
* Credits: David Clunie, https://www.dclunie.com/medical-image-faq/html/part2.html
*
* @param {Vector3D} vector The orientation vector.
* @returns {string} The orientation code.
*/
function getVectorStringLPS(vector) {
let abs = new Vector3D(
Math.abs(vector.getX()),
Math.abs(vector.getY()),
Math.abs(vector.getZ())
);

let orientation = '';
const orientationX = vector.getX() < 0 ? 'R' : 'L';
const orientationY = vector.getY() < 0 ? 'A' : 'P';
// as defined in DICOM
//const orientationZ = vector.getZ() < 0 ? 'F' : 'H';
const orientationZ = vector.getZ() < 0 ? 'I' : 'S';

const threshold = 0.0001;

for (let i = 0; i < 3; i++) {
if (abs.getX() > threshold &&
abs.getX() > abs.getY() &&
abs.getX() > abs.getZ()) {
orientation += orientationX;
abs = new Vector3D(0, abs.getY(), abs.getZ());
} else if (abs.getY() > threshold &&
abs.getY() > abs.getX() &&
abs.getY() > abs.getZ()) {
orientation += orientationY;
abs = new Vector3D(abs.getX(), 0, abs.getZ());
} else if (abs.getZ() > threshold &&
abs.getZ() > abs.getX() &&
abs.getZ() > abs.getY()) {
orientation += orientationZ;
abs = new Vector3D(abs.getX(), abs.getY(), 0);
} else {
break;
}
}

return orientation;
}

/**
* Get the orientation matrix associated to the direction cosines.
*
* @param {number[]} cosines The direction cosines.
* @returns {Matrix33} The orientation matrix.
*/
export function getOrientationFromCosines(cosines) {
let orientationMatrix;
if (typeof cosines !== 'undefined' && cosines.length === 6) {
const rowCosines = new Vector3D(cosines[0], cosines[1], cosines[2]);
const colCosines = new Vector3D(cosines[3], cosines[4], cosines[5]);
const normal = rowCosines.crossProduct(colCosines);
/* eslint-disable array-element-newline */
orientationMatrix = new Matrix33([
rowCosines.getX(), colCosines.getX(), normal.getX(),
rowCosines.getY(), colCosines.getY(), normal.getY(),
rowCosines.getZ(), colCosines.getZ(), normal.getZ()
]);
}
return orientationMatrix;
}
4 changes: 2 additions & 2 deletions tests/image/iterator.test.js
Expand Up @@ -2,9 +2,9 @@ import {Point3D} from '../../src/math/point';
import {Index} from '../../src/math/index';
import {
Matrix33,
getIdentityMat33,
getMatrixFromName
getIdentityMat33
} from '../../src/math/matrix';
import {getMatrixFromName} from '../../src/math/orientation';
import {Size} from '../../src/image/size';
import {Spacing} from '../../src/image/spacing';
import {Geometry} from '../../src/image/geometry';
Expand Down

0 comments on commit 4af0c4f

Please sign in to comment.