Factorize glm::inverse code for matrices

master
Christophe Riccio ago%!(EXTRA string=12 years)
parent dde5178b84
commit 9b5bec81f2
  1. 68
      glm/detail/func_matrix.hpp
  2. 61
      glm/detail/func_matrix.inl

@ -40,15 +40,9 @@
#ifndef GLM_CORE_func_matrix #ifndef GLM_CORE_func_matrix
#define GLM_CORE_func_matrix #define GLM_CORE_func_matrix
#include "type_mat2x2.hpp" // Dependencies
#include "type_mat2x3.hpp" #include "../detail/precision.hpp"
#include "type_mat2x4.hpp" #include "../detail/setup.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"
namespace glm namespace glm
{ {
@ -92,65 +86,25 @@ namespace glm
GLM_FUNC_DECL typename matType::transpose_type transpose( GLM_FUNC_DECL typename matType::transpose_type transpose(
matType const & x); matType const & x);
/// Return the determinant of a mat2 matrix. /// Return the determinant of a squared matrix.
/// ///
/// @tparam valType Floating-point scalar types. /// @tparam valType Floating-point scalar types.
/// ///
/// @see <a href="http://www.opengl.org/sdk/docs/manglsl/xhtml/determinant.xml">GLSL determinant man page</a> /// @see <a href="http://www.opengl.org/sdk/docs/manglsl/xhtml/determinant.xml">GLSL determinant man page</a>
/// @see <a href="http://www.opengl.org/registry/doc/GLSLangSpec.4.20.8.pdf">GLSL 4.20.8 specification, section 8.6 Matrix Functions</a> /// @see <a href="http://www.opengl.org/registry/doc/GLSLangSpec.4.20.8.pdf">GLSL 4.20.8 specification, section 8.6 Matrix Functions</a>
template <typename T, precision P> template <typename T, precision P, template <typename, precision> class matType>
GLM_FUNC_DECL typename detail::tmat2x2<T, P>::value_type determinant( GLM_FUNC_DECL T determinant(
detail::tmat2x2<T, P> const & m); matType<T, P> const & m);
/// Return the determinant of a mat3 matrix. /// Return the inverse of a squared matrix.
///
/// @tparam valType Floating-point scalar types.
///
/// @see <a href="http://www.opengl.org/sdk/docs/manglsl/xhtml/determinant.xml">GLSL determinant man page</a>
/// @see <a href="http://www.opengl.org/registry/doc/GLSLangSpec.4.20.8.pdf">GLSL 4.20.8 specification, section 8.6 Matrix Functions</a>
template <typename T, precision P>
GLM_FUNC_DECL typename detail::tmat3x3<T, P>::value_type determinant(
detail::tmat3x3<T, P> const & m);
/// Return the determinant of a mat4 matrix.
///
/// @tparam valType Floating-point scalar types.
///
/// @see <a href="http://www.opengl.org/sdk/docs/manglsl/xhtml/determinant.xml">GLSL determinant man page</a>
/// @see <a href="http://www.opengl.org/registry/doc/GLSLangSpec.4.20.8.pdf">GLSL 4.20.8 specification, section 8.6 Matrix Functions</a>
template <typename T, precision P>
GLM_FUNC_DECL typename detail::tmat4x4<T, P>::value_type determinant(
detail::tmat4x4<T, P> const & m);
/// Return the inverse of a mat2 matrix.
///
/// @tparam valType Floating-point scalar types.
///
/// @see <a href="http://www.opengl.org/sdk/docs/manglsl/xhtml/inverse.xml">GLSL inverse man page</a>
/// @see <a href="http://www.opengl.org/registry/doc/GLSLangSpec.4.20.8.pdf">GLSL 4.20.8 specification, section 8.6 Matrix Functions</a>
template <typename T, precision P>
GLM_FUNC_DECL detail::tmat2x2<T, P> inverse(
detail::tmat2x2<T, P> const & m);
/// Return the inverse of a mat3 matrix.
///
/// @tparam valType Floating-point scalar types.
///
/// @see <a href="http://www.opengl.org/sdk/docs/manglsl/xhtml/inverse.xml">GLSL inverse man page</a>
/// @see <a href="http://www.opengl.org/registry/doc/GLSLangSpec.4.20.8.pdf">GLSL 4.20.8 specification, section 8.6 Matrix Functions</a>
template <typename T, precision P>
GLM_FUNC_DECL detail::tmat3x3<T, P> inverse(
detail::tmat3x3<T, P> const & m);
/// Return the inverse of a mat4 matrix.
/// ///
/// @tparam valType Floating-point scalar types. /// @tparam valType Floating-point scalar types.
/// ///
/// @see <a href="http://www.opengl.org/sdk/docs/manglsl/xhtml/inverse.xml">GLSL inverse man page</a> /// @see <a href="http://www.opengl.org/sdk/docs/manglsl/xhtml/inverse.xml">GLSL inverse man page</a>
/// @see <a href="http://www.opengl.org/registry/doc/GLSLangSpec.4.20.8.pdf">GLSL 4.20.8 specification, section 8.6 Matrix Functions</a> /// @see <a href="http://www.opengl.org/registry/doc/GLSLangSpec.4.20.8.pdf">GLSL 4.20.8 specification, section 8.6 Matrix Functions</a>
template <typename T, precision P> template <typename T, precision P, template <typename, precision> class matType>
GLM_FUNC_DECL detail::tmat4x4<T, P> inverse( GLM_FUNC_DECL matType<T, P> inverse(
detail::tmat4x4<T, P> const & m); matType<T, P> const & m);
/// @} /// @}
}//namespace glm }//namespace glm

@ -30,6 +30,15 @@
#include "../vec2.hpp" #include "../vec2.hpp"
#include "../vec3.hpp" #include "../vec3.hpp"
#include "../vec4.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 <limits> #include <limits>
namespace glm namespace glm
@ -474,15 +483,16 @@ namespace glm
+ m[0][3] * DetCof[3]; + m[0][3] * DetCof[3];
} }
template <typename T, precision P> namespace detail
GLM_FUNC_QUALIFIER detail::tmat2x2<T, P> inverse
(
detail::tmat2x2<T, P> const & m
)
{ {
GLM_STATIC_ASSERT(std::numeric_limits<T>::is_iec559, "'inverse' only accept floating-point inputs"); template <template <class, precision> class matType, typename T, precision P>
struct compute_inverse{};
//valType Determinant = m[0][0] * m[1][1] - m[1][0] * m[0][1]; template <typename T, precision P>
struct compute_inverse<detail::tmat2x2, T, P>
{
static detail::tmat2x2<T, P> call(detail::tmat2x2<T, P> const & m)
{
T Determinant = determinant(m); T Determinant = determinant(m);
detail::tmat2x2<T, P> Inverse( detail::tmat2x2<T, P> Inverse(
@ -493,19 +503,13 @@ namespace glm
return Inverse; return Inverse;
} }
};
template <typename T, precision P> template <typename T, precision P>
GLM_FUNC_QUALIFIER detail::tmat3x3<T, P> inverse struct compute_inverse<detail::tmat3x3, T, P>
( {
detail::tmat3x3<T, P> const & m static detail::tmat3x3<T, P> call(detail::tmat3x3<T, P> const & m)
)
{ {
GLM_STATIC_ASSERT(std::numeric_limits<T>::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); T Determinant = determinant(m);
detail::tmat3x3<T, P> Inverse(detail::tmat3x3<T, P>::_null); detail::tmat3x3<T, P> Inverse(detail::tmat3x3<T, P>::_null);
@ -522,15 +526,13 @@ namespace glm
return Inverse; return Inverse;
} }
};
template <typename T, precision P> template <typename T, precision P>
GLM_FUNC_QUALIFIER detail::tmat4x4<T, P> inverse struct compute_inverse<detail::tmat4x4, T, P>
( {
detail::tmat4x4<T, P> const & m static detail::tmat4x4<T, P> call(detail::tmat4x4<T, P> const & m)
)
{ {
GLM_STATIC_ASSERT(std::numeric_limits<T>::is_iec559, "'inverse' only accept floating-point inputs");
T Coef00 = m[2][2] * m[3][3] - m[3][2] * m[2][3]; 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 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 Coef03 = m[1][2] * m[2][3] - m[2][2] * m[1][3];
@ -585,4 +587,17 @@ namespace glm
return Inverse; return Inverse;
} }
};
}//namespace detail
template <typename T, precision P, template <typename, precision> class matType>
GLM_FUNC_DECL matType<T, P> inverse
(
matType<T, P> const & m
)
{
GLM_STATIC_ASSERT(std::numeric_limits<T>::is_iec559, "'inverse' only accept floating-point inputs");
return detail::compute_inverse<matType, T, P>::call(m);
}
}//namespace glm }//namespace glm

Loading…
Cancel
Save