Skip to content

Commit

Permalink
feature: add mat3Determinant
Browse files Browse the repository at this point in the history
  • Loading branch information
0b5vr committed Feb 12, 2022
1 parent 1d08821 commit 7e42ee1
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/math/mat3/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export { mat3Determinant } from './mat3Determinant';
export { mat3FromMat4 } from './mat3FromMat4';
export { mat3FromMat4Transpose } from './mat3FromMat4Transpose';
export { mat3FromQuaternion } from './mat3FromQuaternion';
Expand Down
13 changes: 13 additions & 0 deletions src/math/mat3/mat3Determinant.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import type { RawMatrix3 } from './RawMatrix3';

export function mat3Determinant( m: RawMatrix3 ): number {
const
n11 = m[ 0 ], n21 = m[ 1 ], n31 = m[ 2 ],
n12 = m[ 3 ], n22 = m[ 4 ], n32 = m[ 5 ],
n13 = m[ 6 ], n23 = m[ 7 ], n33 = m[ 8 ],
t11 = n33 * n22 - n32 * n23,
t12 = n32 * n13 - n33 * n12,
t13 = n23 * n12 - n22 * n13;

return n11 * t11 + n21 * t12 + n31 * t13;
}
17 changes: 17 additions & 0 deletions src/math/mat3/tests/mat3Determinant.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import '../../../tests/matchers/toBeCloseToArray';
import { mat3Determinant } from '../mat3Determinant';
import type { RawMatrix3 } from '../RawMatrix3';

describe( 'mat3Determinant', () => {
it( 'returns a determinant of the matrix', () => {
const rawMatrixLookAtFrom345: RawMatrix3 = [
0.857, 0.000, -0.514,
-0.291, 0.825, -0.485,
0.424, 0.566, 0.707,
];

const subject = mat3Determinant( rawMatrixLookAtFrom345 );

expect( subject ).toBeCloseTo( 1.0 );
} );
} );

0 comments on commit 7e42ee1

Please sign in to comment.