diff --git a/glm/detail/func_matrix.hpp b/glm/detail/func_matrix.hpp
index 238b3ad9..34f4f61d 100644
--- a/glm/detail/func_matrix.hpp
+++ b/glm/detail/func_matrix.hpp
@@ -40,15 +40,9 @@
#ifndef GLM_CORE_func_matrix
#define GLM_CORE_func_matrix
-#include "type_mat2x2.hpp"
-#include "type_mat2x3.hpp"
-#include "type_mat2x4.hpp"
-#include "type_mat3x2.hpp"
-#include "type_mat3x3.hpp"
-#include "type_mat3x4.hpp"
-#include "type_mat4x2.hpp"
-#include "type_mat4x3.hpp"
-#include "type_mat4x4.hpp"
+// Dependencies
+#include "../detail/precision.hpp"
+#include "../detail/setup.hpp"
namespace glm
{
@@ -92,65 +86,25 @@ namespace glm
GLM_FUNC_DECL typename matType::transpose_type transpose(
matType const & x);
- /// Return the determinant of a mat2 matrix.
+ /// Return the determinant of a squared matrix.
///
/// @tparam valType Floating-point scalar types.
///
/// @see GLSL determinant man page
/// @see GLSL 4.20.8 specification, section 8.6 Matrix Functions
- template
- GLM_FUNC_DECL typename detail::tmat2x2::value_type determinant(
- detail::tmat2x2 const & m);
+ template class matType>
+ GLM_FUNC_DECL T determinant(
+ matType const & m);
- /// Return the determinant of a mat3 matrix.
- ///
- /// @tparam valType Floating-point scalar types.
- ///
- /// @see GLSL determinant man page
- /// @see GLSL 4.20.8 specification, section 8.6 Matrix Functions
- template
- GLM_FUNC_DECL typename detail::tmat3x3::value_type determinant(
- detail::tmat3x3 const & m);
-
- /// Return the determinant of a mat4 matrix.
- ///
- /// @tparam valType Floating-point scalar types.
- ///
- /// @see GLSL determinant man page
- /// @see GLSL 4.20.8 specification, section 8.6 Matrix Functions
- template
- GLM_FUNC_DECL typename detail::tmat4x4::value_type determinant(
- detail::tmat4x4 const & m);
-
- /// Return the inverse of a mat2 matrix.
+ /// Return the inverse of a squared matrix.
///
/// @tparam valType Floating-point scalar types.
///
/// @see GLSL inverse man page
/// @see GLSL 4.20.8 specification, section 8.6 Matrix Functions
- template
- GLM_FUNC_DECL detail::tmat2x2 inverse(
- detail::tmat2x2 const & m);
-
- /// Return the inverse of a mat3 matrix.
- ///
- /// @tparam valType Floating-point scalar types.
- ///
- /// @see GLSL inverse man page
- /// @see GLSL 4.20.8 specification, section 8.6 Matrix Functions
- template
- GLM_FUNC_DECL detail::tmat3x3 inverse(
- detail::tmat3x3 const & m);
-
- /// Return the inverse of a mat4 matrix.
- ///
- /// @tparam valType Floating-point scalar types.
- ///
- /// @see GLSL inverse man page
- /// @see GLSL 4.20.8 specification, section 8.6 Matrix Functions
- template
- GLM_FUNC_DECL detail::tmat4x4 inverse(
- detail::tmat4x4 const & m);
+ template class matType>
+ GLM_FUNC_DECL matType inverse(
+ matType const & m);
/// @}
}//namespace glm
diff --git a/glm/detail/func_matrix.inl b/glm/detail/func_matrix.inl
index d1509e60..3f178bb2 100644
--- a/glm/detail/func_matrix.inl
+++ b/glm/detail/func_matrix.inl
@@ -30,6 +30,15 @@
#include "../vec2.hpp"
#include "../vec3.hpp"
#include "../vec4.hpp"
+#include "type_mat2x2.hpp"
+#include "type_mat2x3.hpp"
+#include "type_mat2x4.hpp"
+#include "type_mat3x2.hpp"
+#include "type_mat3x3.hpp"
+#include "type_mat3x4.hpp"
+#include "type_mat4x2.hpp"
+#include "type_mat4x3.hpp"
+#include "type_mat4x4.hpp"
#include
namespace glm
@@ -474,115 +483,121 @@ namespace glm
+ m[0][3] * DetCof[3];
}
+namespace detail
+{
+ template class matType, typename T, precision P>
+ struct compute_inverse{};
+
template
- GLM_FUNC_QUALIFIER detail::tmat2x2 inverse
+ struct compute_inverse
+ {
+ static detail::tmat2x2 call(detail::tmat2x2 const & m)
+ {
+ T Determinant = determinant(m);
+
+ detail::tmat2x2 Inverse(
+ + m[1][1] / Determinant,
+ - m[0][1] / Determinant,
+ - m[1][0] / Determinant,
+ + m[0][0] / Determinant);
+
+ return Inverse;
+ }
+ };
+
+ template
+ struct compute_inverse
+ {
+ static detail::tmat3x3 call(detail::tmat3x3 const & m)
+ {
+ T Determinant = determinant(m);
+
+ detail::tmat3x3 Inverse(detail::tmat3x3::_null);
+ Inverse[0][0] = + (m[1][1] * m[2][2] - m[2][1] * m[1][2]);
+ Inverse[1][0] = - (m[1][0] * m[2][2] - m[2][0] * m[1][2]);
+ Inverse[2][0] = + (m[1][0] * m[2][1] - m[2][0] * m[1][1]);
+ Inverse[0][1] = - (m[0][1] * m[2][2] - m[2][1] * m[0][2]);
+ Inverse[1][1] = + (m[0][0] * m[2][2] - m[2][0] * m[0][2]);
+ Inverse[2][1] = - (m[0][0] * m[2][1] - m[2][0] * m[0][1]);
+ Inverse[0][2] = + (m[0][1] * m[1][2] - m[1][1] * m[0][2]);
+ Inverse[1][2] = - (m[0][0] * m[1][2] - m[1][0] * m[0][2]);
+ Inverse[2][2] = + (m[0][0] * m[1][1] - m[1][0] * m[0][1]);
+ Inverse /= Determinant;
+
+ return Inverse;
+ }
+ };
+
+ template
+ struct compute_inverse
+ {
+ static detail::tmat4x4 call(detail::tmat4x4 const & m)
+ {
+ T Coef00 = m[2][2] * m[3][3] - m[3][2] * m[2][3];
+ T Coef02 = m[1][2] * m[3][3] - m[3][2] * m[1][3];
+ T Coef03 = m[1][2] * m[2][3] - m[2][2] * m[1][3];
+
+ T Coef04 = m[2][1] * m[3][3] - m[3][1] * m[2][3];
+ T Coef06 = m[1][1] * m[3][3] - m[3][1] * m[1][3];
+ T Coef07 = m[1][1] * m[2][3] - m[2][1] * m[1][3];
+
+ T Coef08 = m[2][1] * m[3][2] - m[3][1] * m[2][2];
+ T Coef10 = m[1][1] * m[3][2] - m[3][1] * m[1][2];
+ T Coef11 = m[1][1] * m[2][2] - m[2][1] * m[1][2];
+
+ T Coef12 = m[2][0] * m[3][3] - m[3][0] * m[2][3];
+ T Coef14 = m[1][0] * m[3][3] - m[3][0] * m[1][3];
+ T Coef15 = m[1][0] * m[2][3] - m[2][0] * m[1][3];
+
+ T Coef16 = m[2][0] * m[3][2] - m[3][0] * m[2][2];
+ T Coef18 = m[1][0] * m[3][2] - m[3][0] * m[1][2];
+ T Coef19 = m[1][0] * m[2][2] - m[2][0] * m[1][2];
+
+ T Coef20 = m[2][0] * m[3][1] - m[3][0] * m[2][1];
+ T Coef22 = m[1][0] * m[3][1] - m[3][0] * m[1][1];
+ T Coef23 = m[1][0] * m[2][1] - m[2][0] * m[1][1];
+
+ detail::tvec4 const SignA(+1, -1, +1, -1);
+ detail::tvec4 const SignB(-1, +1, -1, +1);
+
+ detail::tvec4 Fac0(Coef00, Coef00, Coef02, Coef03);
+ detail::tvec4 Fac1(Coef04, Coef04, Coef06, Coef07);
+ detail::tvec4 Fac2(Coef08, Coef08, Coef10, Coef11);
+ detail::tvec4 Fac3(Coef12, Coef12, Coef14, Coef15);
+ detail::tvec4 Fac4(Coef16, Coef16, Coef18, Coef19);
+ detail::tvec4 Fac5(Coef20, Coef20, Coef22, Coef23);
+
+ detail::tvec4 Vec0(m[1][0], m[0][0], m[0][0], m[0][0]);
+ detail::tvec4 Vec1(m[1][1], m[0][1], m[0][1], m[0][1]);
+ detail::tvec4 Vec2(m[1][2], m[0][2], m[0][2], m[0][2]);
+ detail::tvec4 Vec3(m[1][3], m[0][3], m[0][3], m[0][3]);
+
+ detail::tvec4 Inv0 = SignA * (Vec1 * Fac0 - Vec2 * Fac1 + Vec3 * Fac2);
+ detail::tvec4 Inv1 = SignB * (Vec0 * Fac0 - Vec2 * Fac3 + Vec3 * Fac4);
+ detail::tvec4 Inv2 = SignA * (Vec0 * Fac1 - Vec1 * Fac3 + Vec3 * Fac5);
+ detail::tvec4 Inv3 = SignB * (Vec0 * Fac2 - Vec1 * Fac4 + Vec2 * Fac5);
+
+ detail::tmat4x4 Inverse(Inv0, Inv1, Inv2, Inv3);
+
+ detail::tvec4 Row0(Inverse[0][0], Inverse[1][0], Inverse[2][0], Inverse[3][0]);
+
+ T Determinant = dot(m[0], Row0);
+
+ Inverse /= Determinant;
+
+ return Inverse;
+ }
+ };
+}//namespace detail
+
+ template class matType>
+ GLM_FUNC_DECL matType inverse
(
- detail::tmat2x2 const & m
+ matType const & m
)
{
GLM_STATIC_ASSERT(std::numeric_limits::is_iec559, "'inverse' only accept floating-point inputs");
-
- //valType Determinant = m[0][0] * m[1][1] - m[1][0] * m[0][1];
- T Determinant = determinant(m);
-
- detail::tmat2x2 Inverse(
- + m[1][1] / Determinant,
- - m[0][1] / Determinant,
- - m[1][0] / Determinant,
- + m[0][0] / Determinant);
-
- return Inverse;
+ return detail::compute_inverse::call(m);
}
- template
- GLM_FUNC_QUALIFIER detail::tmat3x3 inverse
- (
- detail::tmat3x3 const & m
- )
- {
- GLM_STATIC_ASSERT(std::numeric_limits::is_iec559, "'inverse' only accept floating-point inputs");
-
- //valType Determinant = m[0][0] * (m[1][1] * m[2][2] - m[2][1] * m[1][2])
- // - m[1][0] * (m[0][1] * m[2][2] - m[2][1] * m[0][2])
- // + m[2][0] * (m[0][1] * m[1][2] - m[1][1] * m[0][2]);
-
- T Determinant = determinant(m);
-
- detail::tmat3x3 Inverse(detail::tmat3x3::_null);
- Inverse[0][0] = + (m[1][1] * m[2][2] - m[2][1] * m[1][2]);
- Inverse[1][0] = - (m[1][0] * m[2][2] - m[2][0] * m[1][2]);
- Inverse[2][0] = + (m[1][0] * m[2][1] - m[2][0] * m[1][1]);
- Inverse[0][1] = - (m[0][1] * m[2][2] - m[2][1] * m[0][2]);
- Inverse[1][1] = + (m[0][0] * m[2][2] - m[2][0] * m[0][2]);
- Inverse[2][1] = - (m[0][0] * m[2][1] - m[2][0] * m[0][1]);
- Inverse[0][2] = + (m[0][1] * m[1][2] - m[1][1] * m[0][2]);
- Inverse[1][2] = - (m[0][0] * m[1][2] - m[1][0] * m[0][2]);
- Inverse[2][2] = + (m[0][0] * m[1][1] - m[1][0] * m[0][1]);
- Inverse /= Determinant;
-
- return Inverse;
- }
-
- template
- GLM_FUNC_QUALIFIER detail::tmat4x4 inverse
- (
- detail::tmat4x4 const & m
- )
- {
- GLM_STATIC_ASSERT(std::numeric_limits::is_iec559, "'inverse' only accept floating-point inputs");
-
- T Coef00 = m[2][2] * m[3][3] - m[3][2] * m[2][3];
- T Coef02 = m[1][2] * m[3][3] - m[3][2] * m[1][3];
- T Coef03 = m[1][2] * m[2][3] - m[2][2] * m[1][3];
-
- T Coef04 = m[2][1] * m[3][3] - m[3][1] * m[2][3];
- T Coef06 = m[1][1] * m[3][3] - m[3][1] * m[1][3];
- T Coef07 = m[1][1] * m[2][3] - m[2][1] * m[1][3];
-
- T Coef08 = m[2][1] * m[3][2] - m[3][1] * m[2][2];
- T Coef10 = m[1][1] * m[3][2] - m[3][1] * m[1][2];
- T Coef11 = m[1][1] * m[2][2] - m[2][1] * m[1][2];
-
- T Coef12 = m[2][0] * m[3][3] - m[3][0] * m[2][3];
- T Coef14 = m[1][0] * m[3][3] - m[3][0] * m[1][3];
- T Coef15 = m[1][0] * m[2][3] - m[2][0] * m[1][3];
-
- T Coef16 = m[2][0] * m[3][2] - m[3][0] * m[2][2];
- T Coef18 = m[1][0] * m[3][2] - m[3][0] * m[1][2];
- T Coef19 = m[1][0] * m[2][2] - m[2][0] * m[1][2];
-
- T Coef20 = m[2][0] * m[3][1] - m[3][0] * m[2][1];
- T Coef22 = m[1][0] * m[3][1] - m[3][0] * m[1][1];
- T Coef23 = m[1][0] * m[2][1] - m[2][0] * m[1][1];
-
- detail::tvec4 const SignA(+1, -1, +1, -1);
- detail::tvec4 const SignB(-1, +1, -1, +1);
-
- detail::tvec4 Fac0(Coef00, Coef00, Coef02, Coef03);
- detail::tvec4 Fac1(Coef04, Coef04, Coef06, Coef07);
- detail::tvec4 Fac2(Coef08, Coef08, Coef10, Coef11);
- detail::tvec4 Fac3(Coef12, Coef12, Coef14, Coef15);
- detail::tvec4 Fac4(Coef16, Coef16, Coef18, Coef19);
- detail::tvec4 Fac5(Coef20, Coef20, Coef22, Coef23);
-
- detail::tvec4 Vec0(m[1][0], m[0][0], m[0][0], m[0][0]);
- detail::tvec4 Vec1(m[1][1], m[0][1], m[0][1], m[0][1]);
- detail::tvec4 Vec2(m[1][2], m[0][2], m[0][2], m[0][2]);
- detail::tvec4 Vec3(m[1][3], m[0][3], m[0][3], m[0][3]);
-
- detail::tvec4 Inv0 = SignA * (Vec1 * Fac0 - Vec2 * Fac1 + Vec3 * Fac2);
- detail::tvec4 Inv1 = SignB * (Vec0 * Fac0 - Vec2 * Fac3 + Vec3 * Fac4);
- detail::tvec4 Inv2 = SignA * (Vec0 * Fac1 - Vec1 * Fac3 + Vec3 * Fac5);
- detail::tvec4 Inv3 = SignB * (Vec0 * Fac2 - Vec1 * Fac4 + Vec2 * Fac5);
-
- detail::tmat4x4 Inverse(Inv0, Inv1, Inv2, Inv3);
-
- detail::tvec4 Row0(Inverse[0][0], Inverse[1][0], Inverse[2][0], Inverse[3][0]);
-
- T Determinant = dot(m[0], Row0);
-
- Inverse /= Determinant;
-
- return Inverse;
- }
}//namespace glm