소스 검색

math: disable unstable LU decomposition for matrix inversion.

undefined
Sam Hocevar 9 년 전
부모
커밋
d8951b413a
2개의 변경된 파일54개의 추가작업 그리고 6개의 파일을 삭제
  1. +27
    -0
      src/lol/math/matrix.h
  2. +27
    -6
      src/t/math/matrix.cpp

+ 27
- 0
src/lol/math/matrix.h 파일 보기

@@ -547,6 +547,7 @@ void lu_decomposition(mat_t<T, N, N> const &m, mat_t<T, N, N> & L, mat_t<T, N, N
template<typename T, int N>
T determinant(mat_t<T, N, N> const &m)
{
#if 0 /* XXX: LU decomposition is currently broken */
mat_t<T, N, N> L, U;
lu_decomposition(m, L, U);

@@ -555,6 +556,18 @@ T determinant(mat_t<T, N, N> const &m)
det *= U[i][i];

return det;
#else
T ret = (T)0;
for (int i = 0; i < N; ++i)
ret += m[i][0] * cofactor(m, i, 0);
return ret;
#endif
}

template<typename T>
T const & determinant(mat_t<T, 1, 1> const &m)
{
return m[0][0];
}

/*
@@ -614,12 +627,26 @@ mat_t<T, N, N> u_inverse(mat_t<T, N, N> const & U)
template<typename T, int N>
mat_t<T, N, N> inverse(mat_t<T, N, N> const &m)
{
#if 0 /* XXX: LU decomposition is currently broken */
mat_t<T, N, N> L, U;
lu_decomposition(m, L, U);

mat_t<T, N, N> ret = u_inverse(U) * l_inverse(L);

return ret;
#else
mat_t<T, N, N> ret;

T d = determinant(m);
if (d)
{
/* Transpose result matrix on the fly */
for (int i = 0; i < N; ++i)
for (int j = 0; j < N; ++j)
ret[i][j] = cofactor(m, j, i) / d;
}
return ret;
#endif
}

/*


+ 27
- 6
src/t/math/matrix.cpp 파일 보기

@@ -76,15 +76,21 @@ lolunit_declare_fixture(MatrixTest)

lolunit_declare_test(inverse_2x2)
{
mat2 m0 = inv2;
mat2 m1 = inverse(m0);
mat2 m2 = m0 * m1;
mat2 m(vec2(4, 3),
vec2(3, 2));

mat2 m1 = inverse(m);
for (int j = 0; j < 2; ++j)
for (int i = 0; i < 2; ++i)
lolunit_assert_equal(m1[i][j], m1[i][j]);

mat2 m2 = m1 * m;
for (int j = 0; j < 2; ++j)
for (int i = 0; i < 2; ++i)
lolunit_assert_doubles_equal(m2[i][j], mat2(1.f)[i][j], 1e-5);
}

#if 0 /* XXX: LU decomposition is currently broken */
lolunit_declare_test(lu_decomposition_3x3)
{
mat3 m(vec3(2, 3, 5),
@@ -226,14 +232,20 @@ lolunit_declare_fixture(MatrixTest)
for (int i = 0; i < 4; ++i)
lolunit_assert_doubles_equal(m2[i][j], mat4(1.f)[i][j], 1e-5);
}
#endif

lolunit_declare_test(inverse_3x3)
{
mat3 m(vec3(2, 3, 5),
vec3(3, 2, 3),
vec3(9, 5, 7));
mat3 m2 = inverse(m) * m;

mat3 m1 = inverse(m);
for (int j = 0; j < 3; ++j)
for (int i = 0; i < 3; ++i)
lolunit_assert_equal(m1[i][j], m1[i][j]);

mat3 m2 = m1 * m;
for (int j = 0; j < 3; ++j)
for (int i = 0; i < 3; ++i)
lolunit_assert_doubles_equal(m2[i][j], mat3(1.f)[i][j], 1e-5);
@@ -245,8 +257,13 @@ lolunit_declare_fixture(MatrixTest)
vec4(-2, -1, -2, 2),
vec4( 4, 2, 5, -4),
vec4( 5, -3, -7, -6));
mat4 m2 = inverse(m) * m;

mat4 m1 = inverse(m);
for (int j = 0; j < 4; ++j)
for (int i = 0; i < 4; ++i)
lolunit_assert_equal(m1[i][j], m1[i][j]);

mat4 m2 = m1 * m;
for (int j = 0; j < 4; ++j)
for (int i = 0; i < 4; ++i)
lolunit_assert_doubles_equal(m2[i][j], mat4(1.f)[i][j], 1e-5);
@@ -258,8 +275,12 @@ lolunit_declare_fixture(MatrixTest)
vec4(0, 0, 1, 0),
vec4(0, -1, 0, 0),
vec4(0, 0, -1, 1));
mat4 m2 = inverse(m) * m;
mat4 m1 = inverse(m);
for (int j = 0; j < 4; ++j)
for (int i = 0; i < 4; ++i)
lolunit_assert_equal(m1[i][j], m1[i][j]);

mat4 m2 = m1 * m;
for (int j = 0; j < 4; ++j)
for (int i = 0; i < 4; ++i)
lolunit_assert_doubles_equal(m2[i][j], mat4(1.f)[i][j], 1e-5);


불러오는 중...
취소
저장