Skip to content

Commit

Permalink
Split matrix equals and isSimilar
Browse files Browse the repository at this point in the history
  • Loading branch information
ivmartel committed Feb 19, 2025
1 parent a3c108f commit 901c400
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/image/geometry.js
Original file line number Diff line number Diff line change
Expand Up @@ -642,7 +642,7 @@ export function getSliceGeometrySpacing(origins) {
export function mergeGeometries(geometry1, geometry2) {
const orientation = geometry1.getOrientation();
// check input
if (!orientation.equals(geometry2.getOrientation())) {
if (!orientation.isSimilar(geometry2.getOrientation())) {
throw new Error('Cannot merge geometries with different orientation');
}
const invOrientation = orientation.getInverse();
Expand Down
2 changes: 1 addition & 1 deletion src/image/image.js
Original file line number Diff line number Diff line change
Expand Up @@ -780,7 +780,7 @@ export class Image {
if (size.get(1) !== rhsSize.get(1)) {
throw new Error('Cannot append a slice with different number of rows');
}
if (!this.#geometry.getOrientation().equals(
if (!this.#geometry.getOrientation().isSimilar(
rhs.getGeometry().getOrientation(), 0.0001)) {
throw new Error('Cannot append a slice with different orientation');
}
Expand Down
23 changes: 21 additions & 2 deletions src/math/matrix.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,30 @@ export class Matrix33 {
* Check for Matrix33 equality.
*
* @param {Matrix33} rhs The other matrix to compare to.
* @returns {boolean} True if both matrices are equal.
*/
equals(rhs) {
// TODO: add type check
// check values
for (let i = 0; i < 3; ++i) {
for (let j = 0; j < 3; ++j) {
if (this.get(i, j) !== rhs.get(i, j)) {
return false;
}
}
}
return true;
}

/**
* Check for Matrix33 similarity.
*
* @param {Matrix33} rhs The other matrix to compare to.
* @param {number} [tol] Optional number comparison tolerance,
* defaults to Number.EPSILON.
* @returns {boolean} True if both matrices are equal.
* @returns {boolean} True if both matrices are similar.
*/
equals(rhs, tol) {
isSimilar(rhs, tol) {
// TODO: add type check
// check values
for (let i = 0; i < 3; ++i) {
Expand Down
2 changes: 1 addition & 1 deletion src/math/orientation.js
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ export function getTargetOrientation(imageOrientation, viewOrientation) {

// TODO: why abs???
const simpleImageOrientation = imageOrientation.asOneAndZeros().getAbs();
if (simpleImageOrientation.equals(getCoronalMat33().getAbs())) {
if (simpleImageOrientation.isSimilar(getCoronalMat33().getAbs())) {
targetOrientation = targetOrientation.getAbs();
}

Expand Down
8 changes: 4 additions & 4 deletions tests/math/matrix.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ QUnit.test('Matrix33 class', function (assert) {
assert.equal(m2.get(1, 2), 6, 'get(1,2)');

// equals with precision
assert.equal(m3.equals(m4, 0.01), true, 'equals true');
assert.equal(m3.equals(m4, 0.001), false, 'equals false');
assert.equal(m3.isSimilar(m4, 0.01), true, 'isSimilar true');
assert.equal(m3.isSimilar(m4, 0.001), false, 'isSimilar false');
});

/**
Expand Down Expand Up @@ -182,11 +182,11 @@ QUnit.test('Matrix33 inverse', function (assert) {
23 / 295,
-26 / 295
]);
assert.ok(invm10.equals(res10), 'inverse #1');
assert.ok(invm10.isSimilar(res10), 'inverse #1');

// double inverse
const invinvm10 = invm10.getInverse();
assert.ok(invinvm10.equals(m10, BIG_EPSILON), 'inverse #1 twice');
assert.ok(invinvm10.isSimilar(m10, BIG_EPSILON), 'inverse #1 twice');
});

/**
Expand Down

0 comments on commit 901c400

Please sign in to comment.