Skip to content

Commit

Permalink
feature: add mat3CreateNormalMatrix
Browse files Browse the repository at this point in the history
and its equivalen in classes
  • Loading branch information
0b5vr committed Feb 13, 2022
1 parent 0c5ddaf commit 17d5add
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/math/mat3/Matrix3.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Matrix4 } from '../mat4/Matrix4';
import { Quaternion } from '../quat/Quaternion';
import { mat3CreateNormalMatrix } from './mat3CreateNormalMatrix';
import { mat3Determinant } from './mat3Determinant';
import { mat3FromMat4 } from './mat3FromMat4';
import { mat3FromQuaternion } from './mat3FromQuaternion';
Expand Down Expand Up @@ -98,6 +99,14 @@ export class Matrix3 {
}
}

/**
* Create a normal matrix out of matrix4.
* @param matrix4 A matrix4
*/
public static createNormalMatrix( matrix4: Matrix4 ): Matrix3 {
return new Matrix3( mat3CreateNormalMatrix( matrix4.elements ) );
}

/**
* Cast a {@link Matrix4} into a Matrix3.
* @param matrix4 A matrix4
Expand Down
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 { mat3CreateNormalMatrix } from './mat3CreateNormalMatrix';
export { mat3Determinant } from './mat3Determinant';
export { mat3FromMat4 } from './mat3FromMat4';
export { mat3FromMat4Transpose } from './mat3FromMat4Transpose';
Expand Down
13 changes: 13 additions & 0 deletions src/math/mat3/mat3CreateNormalMatrix.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { mat3FromMat4Transpose } from './mat3FromMat4Transpose';
import { mat3Inverse } from './mat3Inverse';
import type { RawMatrix3 } from './RawMatrix3';
import type { RawMatrix4 } from '../mat4/RawMatrix4';

/**
* Create a normal matrix out of a matrix4.
*
* @param matrix A matrix4
*/
export function mat3CreateNormalMatrix( m: RawMatrix4 ): RawMatrix3 {
return mat3Inverse( mat3FromMat4Transpose( m ) );
}
1 change: 1 addition & 0 deletions src/math/mat3/mat3FromMat4Transpose.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import type { RawMatrix4 } from '../mat4/RawMatrix4';

/**
* Who needs this?
* Intended to be used by {@link mat3CreateNormalMatrix}.
*/
export function mat3FromMat4Transpose(
source: RawMatrix4,
Expand Down
24 changes: 24 additions & 0 deletions src/math/mat3/tests/mat3CreateNormalMatrix.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import '../../../tests/matchers/toBeCloseToArray';
import { mat3CreateNormalMatrix } from '../mat3CreateNormalMatrix';
import type { RawMatrix3 } from '../RawMatrix3';
import type { RawMatrix4 } from '../../mat4/RawMatrix4';

const rawMatrixLookAtFrom345: RawMatrix4 = [
0.8574929257125442, 0, -0.5144957554275265, 0,
-0.2910427500435996, 0.824621125123532, -0.48507125007266594, 0,
0.4242640687119285, 0.565685424949238, 0.7071067811865475, 0,
3, 4, 5, 1
];

const rawMatrixLookAtFrom345Normal: RawMatrix3 = [
0.857, 0.0, -0.514,
-0.291, 0.825, -0.485,
0.424, 0.566, 0.707,
];

describe( 'mat3CreateNormalMatrix', () => {
it( 'returns a determinant of the matrix', () => {
const subject = mat3CreateNormalMatrix( rawMatrixLookAtFrom345 );
expect( subject ).toBeCloseToArray( rawMatrixLookAtFrom345Normal );
} );
} );
7 changes: 7 additions & 0 deletions src/math/mat4/Matrix4.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,13 @@ export class Matrix4 {
return Matrix3.fromMatrix4( this );
}

/**
* Itself but normal matrix.
*/
public get normalMatrix(): Matrix3 {
return Matrix3.createNormalMatrix( this );
}

public toString(): string {
const m = this.elements.map( ( v ) => v.toFixed( 3 ) );
return `Matrix4( ${ m[ 0 ] }, ${ m[ 4 ] }, ${ m[ 8 ] }, ${ m[ 12 ] }; ${ m[ 1 ] }, ${ m[ 5 ] }, ${ m[ 9 ] }, ${ m[ 13 ] }; ${ m[ 2 ] }, ${ m[ 6 ] }, ${ m[ 10 ] }, ${ m[ 14 ] }; ${ m[ 3 ] }, ${ m[ 7 ] }, ${ m[ 11 ] }, ${ m[ 15 ] } )`;
Expand Down

0 comments on commit 17d5add

Please sign in to comment.