mirror of
https://github.com/g-truc/glm.git
synced 2024-11-26 10:14:35 +00:00
Factorize glm::inverse code for matrices
This commit is contained in:
parent
dde5178b84
commit
9b5bec81f2
@ -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.
|
/// @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::tmat2x2<T, P> inverse(
|
GLM_FUNC_DECL matType<T, P> inverse(
|
||||||
detail::tmat2x2<T, P> const & m);
|
matType<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.
|
|
||||||
///
|
|
||||||
/// @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::tmat4x4<T, P> inverse(
|
|
||||||
detail::tmat4x4<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,115 +483,121 @@ namespace glm
|
|||||||
+ m[0][3] * DetCof[3];
|
+ m[0][3] * DetCof[3];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
namespace detail
|
||||||
|
{
|
||||||
|
template <template <class, precision> class matType, typename T, precision P>
|
||||||
|
struct compute_inverse{};
|
||||||
|
|
||||||
template <typename T, precision P>
|
template <typename T, precision P>
|
||||||
GLM_FUNC_QUALIFIER detail::tmat2x2<T, P> inverse
|
struct compute_inverse<detail::tmat2x2, T, P>
|
||||||
|
{
|
||||||
|
static detail::tmat2x2<T, P> call(detail::tmat2x2<T, P> const & m)
|
||||||
|
{
|
||||||
|
T Determinant = determinant(m);
|
||||||
|
|
||||||
|
detail::tmat2x2<T, P> Inverse(
|
||||||
|
+ m[1][1] / Determinant,
|
||||||
|
- m[0][1] / Determinant,
|
||||||
|
- m[1][0] / Determinant,
|
||||||
|
+ m[0][0] / Determinant);
|
||||||
|
|
||||||
|
return Inverse;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
template <typename T, precision P>
|
||||||
|
struct compute_inverse<detail::tmat3x3, T, P>
|
||||||
|
{
|
||||||
|
static detail::tmat3x3<T, P> call(detail::tmat3x3<T, P> const & m)
|
||||||
|
{
|
||||||
|
T Determinant = determinant(m);
|
||||||
|
|
||||||
|
detail::tmat3x3<T, P> Inverse(detail::tmat3x3<T, P>::_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 <typename T, precision P>
|
||||||
|
struct compute_inverse<detail::tmat4x4, T, P>
|
||||||
|
{
|
||||||
|
static detail::tmat4x4<T, P> call(detail::tmat4x4<T, P> 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<T, P> const SignA(+1, -1, +1, -1);
|
||||||
|
detail::tvec4<T, P> const SignB(-1, +1, -1, +1);
|
||||||
|
|
||||||
|
detail::tvec4<T, P> Fac0(Coef00, Coef00, Coef02, Coef03);
|
||||||
|
detail::tvec4<T, P> Fac1(Coef04, Coef04, Coef06, Coef07);
|
||||||
|
detail::tvec4<T, P> Fac2(Coef08, Coef08, Coef10, Coef11);
|
||||||
|
detail::tvec4<T, P> Fac3(Coef12, Coef12, Coef14, Coef15);
|
||||||
|
detail::tvec4<T, P> Fac4(Coef16, Coef16, Coef18, Coef19);
|
||||||
|
detail::tvec4<T, P> Fac5(Coef20, Coef20, Coef22, Coef23);
|
||||||
|
|
||||||
|
detail::tvec4<T, P> Vec0(m[1][0], m[0][0], m[0][0], m[0][0]);
|
||||||
|
detail::tvec4<T, P> Vec1(m[1][1], m[0][1], m[0][1], m[0][1]);
|
||||||
|
detail::tvec4<T, P> Vec2(m[1][2], m[0][2], m[0][2], m[0][2]);
|
||||||
|
detail::tvec4<T, P> Vec3(m[1][3], m[0][3], m[0][3], m[0][3]);
|
||||||
|
|
||||||
|
detail::tvec4<T, P> Inv0 = SignA * (Vec1 * Fac0 - Vec2 * Fac1 + Vec3 * Fac2);
|
||||||
|
detail::tvec4<T, P> Inv1 = SignB * (Vec0 * Fac0 - Vec2 * Fac3 + Vec3 * Fac4);
|
||||||
|
detail::tvec4<T, P> Inv2 = SignA * (Vec0 * Fac1 - Vec1 * Fac3 + Vec3 * Fac5);
|
||||||
|
detail::tvec4<T, P> Inv3 = SignB * (Vec0 * Fac2 - Vec1 * Fac4 + Vec2 * Fac5);
|
||||||
|
|
||||||
|
detail::tmat4x4<T, P> Inverse(Inv0, Inv1, Inv2, Inv3);
|
||||||
|
|
||||||
|
detail::tvec4<T, P> 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 <typename T, precision P, template <typename, precision> class matType>
|
||||||
|
GLM_FUNC_DECL matType<T, P> inverse
|
||||||
(
|
(
|
||||||
detail::tmat2x2<T, P> const & m
|
matType<T, P> const & m
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
GLM_STATIC_ASSERT(std::numeric_limits<T>::is_iec559, "'inverse' only accept floating-point inputs");
|
GLM_STATIC_ASSERT(std::numeric_limits<T>::is_iec559, "'inverse' only accept floating-point inputs");
|
||||||
|
return detail::compute_inverse<matType, T, P>::call(m);
|
||||||
//valType Determinant = m[0][0] * m[1][1] - m[1][0] * m[0][1];
|
|
||||||
T Determinant = determinant(m);
|
|
||||||
|
|
||||||
detail::tmat2x2<T, P> Inverse(
|
|
||||||
+ m[1][1] / Determinant,
|
|
||||||
- m[0][1] / Determinant,
|
|
||||||
- m[1][0] / Determinant,
|
|
||||||
+ m[0][0] / Determinant);
|
|
||||||
|
|
||||||
return Inverse;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename T, precision P>
|
|
||||||
GLM_FUNC_QUALIFIER detail::tmat3x3<T, P> inverse
|
|
||||||
(
|
|
||||||
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);
|
|
||||||
|
|
||||||
detail::tmat3x3<T, P> Inverse(detail::tmat3x3<T, P>::_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 <typename T, precision P>
|
|
||||||
GLM_FUNC_QUALIFIER detail::tmat4x4<T, P> inverse
|
|
||||||
(
|
|
||||||
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 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<T, P> const SignA(+1, -1, +1, -1);
|
|
||||||
detail::tvec4<T, P> const SignB(-1, +1, -1, +1);
|
|
||||||
|
|
||||||
detail::tvec4<T, P> Fac0(Coef00, Coef00, Coef02, Coef03);
|
|
||||||
detail::tvec4<T, P> Fac1(Coef04, Coef04, Coef06, Coef07);
|
|
||||||
detail::tvec4<T, P> Fac2(Coef08, Coef08, Coef10, Coef11);
|
|
||||||
detail::tvec4<T, P> Fac3(Coef12, Coef12, Coef14, Coef15);
|
|
||||||
detail::tvec4<T, P> Fac4(Coef16, Coef16, Coef18, Coef19);
|
|
||||||
detail::tvec4<T, P> Fac5(Coef20, Coef20, Coef22, Coef23);
|
|
||||||
|
|
||||||
detail::tvec4<T, P> Vec0(m[1][0], m[0][0], m[0][0], m[0][0]);
|
|
||||||
detail::tvec4<T, P> Vec1(m[1][1], m[0][1], m[0][1], m[0][1]);
|
|
||||||
detail::tvec4<T, P> Vec2(m[1][2], m[0][2], m[0][2], m[0][2]);
|
|
||||||
detail::tvec4<T, P> Vec3(m[1][3], m[0][3], m[0][3], m[0][3]);
|
|
||||||
|
|
||||||
detail::tvec4<T, P> Inv0 = SignA * (Vec1 * Fac0 - Vec2 * Fac1 + Vec3 * Fac2);
|
|
||||||
detail::tvec4<T, P> Inv1 = SignB * (Vec0 * Fac0 - Vec2 * Fac3 + Vec3 * Fac4);
|
|
||||||
detail::tvec4<T, P> Inv2 = SignA * (Vec0 * Fac1 - Vec1 * Fac3 + Vec3 * Fac5);
|
|
||||||
detail::tvec4<T, P> Inv3 = SignB * (Vec0 * Fac2 - Vec1 * Fac4 + Vec2 * Fac5);
|
|
||||||
|
|
||||||
detail::tmat4x4<T, P> Inverse(Inv0, Inv1, Inv2, Inv3);
|
|
||||||
|
|
||||||
detail::tvec4<T, P> 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
|
}//namespace glm
|
||||||
|
Loading…
Reference in New Issue
Block a user