Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

69 lines
2.0 KiB

  1. //========= Copyright c 1996-2009, Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //
  7. //=============================================================================//
  8. #ifndef CHOLESKY_HDR
  9. #define CHOLESKY_HDR
  10. #include "mathlib/vector.h"
  11. #include "mathlib/mathlib.h"
  12. struct matrix3x4_t;
  13. // this class holds cholesky decomposition of a 3x3 matrix
  14. struct Cholesky3x3_t
  15. {
  16. // lower diagonal matrix L such that LL' = input matrix
  17. float m_00, m_11, m_22; // these aren't used in computations! they're only for debugging and returning "canonical" form (L or R)
  18. float m_10, m_20, m_21; // these are off-diagonals used in compuations
  19. float m_inv00, m_inv11, m_inv22; // these are reciprocals of diagonals used in all computations
  20. public:
  21. /// @group Construction and initialization {
  22. Cholesky3x3_t( const matrix3x4_t &m )
  23. {
  24. Init( m );
  25. }
  26. Cholesky3x3_t( float a00, float a10, float a11, float a20, float a21, float a22 )
  27. {
  28. Init( a00, a10, a11, a20, a21, a22 );
  29. }
  30. bool Init( float a00, float a10, float a11, float a20, float a21, float a22 );
  31. bool Init( const matrix3x4_t &m )
  32. {
  33. return Init( m[0][0], m[1][0], m[1][1], m[2][0], m[2][1], m[2][2] );
  34. }
  35. //@}
  36. void FillLeft( matrix3x4_t & l );
  37. void FillRight( matrix3x4_t & r );
  38. bool IsValid( );
  39. const Vector SolveRight( const Vector &b );
  40. const Vector SolveLeft( const Vector &b );
  41. // using this decomposition LL', solve the following equation and return the result: LL' x = rhs
  42. const Vector Solve( const Vector &rhs )
  43. {
  44. // L R x = b
  45. // R x = L^-1 b
  46. // x = R^-1 L^-1 b
  47. return SolveRight( SolveLeft( rhs ) );
  48. }
  49. };
  50. //
  51. /// Solve this equation: M*(x,1)=(0,0,0,1) where M is 3x4 matrix and x is the unknown 3-vector
  52. /// that's returned uses cholesky decomposition 3x3 , so assumes 3x3 part of M is symmetrical
  53. /// positive definite
  54. //
  55. inline Vector CholeskySolve( const matrix3x4_t &m )
  56. {
  57. Cholesky3x3_t cholesky( m );
  58. return -cholesky.Solve( Vector( m.m_flMatVal[0][3], m.m_flMatVal[1][3], m.m_flMatVal[2][3] ) );
  59. }
  60. #endif