matrix.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. // This code is in the public domain -- [email protected]
  2. #ifndef NV_MATH_MATRIX_H
  3. #define NV_MATH_MATRIX_H
  4. #include "vector.h"
  5. // - Matrices are stored in memory in *column major* order.
  6. // - Points are to be though of as column vectors.
  7. // - Transformation of a point p by a matrix M is: p' = M * p
  8. namespace nv
  9. {
  10. enum identity_t { identity };
  11. // 3x3 matrix.
  12. class NVMATH_CLASS Matrix3
  13. {
  14. public:
  15. Matrix3();
  16. explicit Matrix3(float f);
  17. explicit Matrix3(identity_t);
  18. Matrix3(const Matrix3 & m);
  19. Matrix3(Vector3::Arg v0, Vector3::Arg v1, Vector3::Arg v2);
  20. float data(uint idx) const;
  21. float & data(uint idx);
  22. float get(uint row, uint col) const;
  23. float operator()(uint row, uint col) const;
  24. float & operator()(uint row, uint col);
  25. Vector3 row(uint i) const;
  26. Vector3 column(uint i) const;
  27. void operator*=(float s);
  28. void operator/=(float s);
  29. void operator+=(const Matrix3 & m);
  30. void operator-=(const Matrix3 & m);
  31. void scale(float s);
  32. void scale(Vector3::Arg s);
  33. float determinant() const;
  34. private:
  35. float m_data[9];
  36. };
  37. // Solve equation system using LU decomposition and back-substitution.
  38. extern bool solveLU(const Matrix3 & m, const Vector3 & b, Vector3 * x);
  39. // Solve equation system using Cramer's inverse.
  40. extern bool solveCramer(const Matrix3 & A, const Vector3 & b, Vector3 * x);
  41. // 4x4 matrix.
  42. class NVMATH_CLASS Matrix
  43. {
  44. public:
  45. typedef Matrix const & Arg;
  46. Matrix();
  47. explicit Matrix(float f);
  48. explicit Matrix(identity_t);
  49. Matrix(const Matrix3 & m);
  50. Matrix(const Matrix & m);
  51. Matrix(Vector4::Arg v0, Vector4::Arg v1, Vector4::Arg v2, Vector4::Arg v3);
  52. //explicit Matrix(const float m[]); // m is assumed to contain 16 elements
  53. float data(uint idx) const;
  54. float & data(uint idx);
  55. float get(uint row, uint col) const;
  56. float operator()(uint row, uint col) const;
  57. float & operator()(uint row, uint col);
  58. const float * ptr() const;
  59. Vector4 row(uint i) const;
  60. Vector4 column(uint i) const;
  61. void zero();
  62. void identity();
  63. void scale(float s);
  64. void scale(Vector3::Arg s);
  65. void translate(Vector3::Arg t);
  66. void rotate(float theta, float v0, float v1, float v2);
  67. float determinant() const;
  68. void operator+=(const Matrix & m);
  69. void operator-=(const Matrix & m);
  70. void apply(Matrix::Arg m);
  71. private:
  72. float m_data[16];
  73. };
  74. // Solve equation system using LU decomposition and back-substitution.
  75. extern bool solveLU(const Matrix & A, const Vector4 & b, Vector4 * x);
  76. // Solve equation system using Cramer's inverse.
  77. extern bool solveCramer(const Matrix & A, const Vector4 & b, Vector4 * x);
  78. // Compute inverse using LU decomposition.
  79. extern Matrix inverseLU(const Matrix & m);
  80. // Compute inverse using Gaussian elimination and partial pivoting.
  81. extern Matrix inverse(const Matrix & m);
  82. extern Matrix3 inverse(const Matrix3 & m);
  83. } // nv namespace
  84. #endif // NV_MATH_MATRIX_H