From d8db86e40c571bf140dc0ff6844eaaa70e2c78e0 Mon Sep 17 00:00:00 2001 From: Henrik Hautakoski Date: Fri, 18 Aug 2023 17:54:19 +0200 Subject: [PATCH] include/Spectre/Math/Matrix3.h: Add inverse() --- include/Spectre/Math/Matrix3.h | 3 +++ include/Spectre/Math/Matrix3.inl | 24 ++++++++++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/include/Spectre/Math/Matrix3.h b/include/Spectre/Math/Matrix3.h index 04c47d0..1576348 100644 --- a/include/Spectre/Math/Matrix3.h +++ b/include/Spectre/Math/Matrix3.h @@ -78,6 +78,9 @@ struct Matrix3 // Transpose matrix. Matrix3 transpose() const; + + // Inverse + Matrix3 inverse() const; }; template diff --git a/include/Spectre/Math/Matrix3.inl b/include/Spectre/Math/Matrix3.inl index 465f770..9425c89 100644 --- a/include/Spectre/Math/Matrix3.inl +++ b/include/Spectre/Math/Matrix3.inl @@ -136,6 +136,30 @@ float Matrix3::det() const return det; } +template +Matrix3 Matrix3::inverse() const +{ + float d = det(); + + if (d != 0.0f) { + float a00 = (v[4] * v[8]) - (v[7] * v[5]); + float a01 = -((v[1] * v[8]) - (v[7] * v[2])); + float a02 = (v[1] * v[5]) - (v[4] * v[2]); + + float a10 = -((v[3] * v[8]) - (v[6] * v[5])); + float a11 = (v[0] * v[8]) - (v[6] * v[2]); + float a12 = -((v[0] * v[5]) - (v[3] * v[2])); + + float a20 = (v[3] * v[7]) - (v[6] * v[4]); + float a21 = -((v[0] * v[7]) - (v[6] * v[1])); + float a22 = (v[0] * v[4]) - (v[3] * v[1]); + + return Matrix3(a00, a01, a02, a10, a11, a12, a20, a21, a22) / d; + } + + return Matrix3::Identity; +} + template Matrix3 Matrix3::transpose() const {