matrix3_f.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. // Copyright (c) 2013 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef UI_GFX_GEOMETRY_MATRIX3_F_H_
  5. #define UI_GFX_GEOMETRY_MATRIX3_F_H_
  6. #include "base/check.h"
  7. #include "ui/gfx/geometry/vector3d_f.h"
  8. namespace gfx {
  9. class GEOMETRY_EXPORT Matrix3F {
  10. public:
  11. ~Matrix3F();
  12. static Matrix3F Zeros();
  13. static Matrix3F Ones();
  14. static Matrix3F Identity();
  15. static Matrix3F FromOuterProduct(const Vector3dF& a, const Vector3dF& bt);
  16. bool IsEqual(const Matrix3F& rhs) const;
  17. // Element-wise comparison with given precision.
  18. bool IsNear(const Matrix3F& rhs, float precision) const;
  19. float get(int i, int j) const {
  20. return data_[MatrixToArrayCoords(i, j)];
  21. }
  22. void set(int i, int j, float v) {
  23. data_[MatrixToArrayCoords(i, j)] = v;
  24. }
  25. void set(float m00, float m01, float m02,
  26. float m10, float m11, float m12,
  27. float m20, float m21, float m22) {
  28. data_[0] = m00;
  29. data_[1] = m01;
  30. data_[2] = m02;
  31. data_[3] = m10;
  32. data_[4] = m11;
  33. data_[5] = m12;
  34. data_[6] = m20;
  35. data_[7] = m21;
  36. data_[8] = m22;
  37. }
  38. Vector3dF get_row(int i) const {
  39. return Vector3dF(data_[MatrixToArrayCoords(i, 0)],
  40. data_[MatrixToArrayCoords(i, 1)],
  41. data_[MatrixToArrayCoords(i, 2)]);
  42. }
  43. Vector3dF get_column(int i) const {
  44. return Vector3dF(
  45. data_[MatrixToArrayCoords(0, i)],
  46. data_[MatrixToArrayCoords(1, i)],
  47. data_[MatrixToArrayCoords(2, i)]);
  48. }
  49. void set_column(int i, const Vector3dF& c) {
  50. data_[MatrixToArrayCoords(0, i)] = c.x();
  51. data_[MatrixToArrayCoords(1, i)] = c.y();
  52. data_[MatrixToArrayCoords(2, i)] = c.z();
  53. }
  54. // Produces a new matrix by adding the elements of |rhs| to this matrix
  55. Matrix3F Add(const Matrix3F& rhs) const;
  56. // Produces a new matrix by subtracting elements of |rhs| from this matrix.
  57. Matrix3F Subtract(const Matrix3F& rhs) const;
  58. // Returns an inverse of this if the matrix is non-singular, zero (== Zero())
  59. // otherwise.
  60. Matrix3F Inverse() const;
  61. // Returns a transpose of this matrix.
  62. Matrix3F Transpose() const;
  63. // Value of the determinant of the matrix.
  64. float Determinant() const;
  65. // Trace (sum of diagonal elements) of the matrix.
  66. float Trace() const {
  67. return data_[MatrixToArrayCoords(0, 0)] +
  68. data_[MatrixToArrayCoords(1, 1)] +
  69. data_[MatrixToArrayCoords(2, 2)];
  70. }
  71. std::string ToString() const;
  72. private:
  73. Matrix3F(); // Uninitialized default.
  74. static int MatrixToArrayCoords(int i, int j) {
  75. DCHECK(i >= 0 && i < 3);
  76. DCHECK(j >= 0 && j < 3);
  77. return i * 3 + j;
  78. }
  79. float data_[9];
  80. };
  81. inline bool operator==(const Matrix3F& lhs, const Matrix3F& rhs) {
  82. return lhs.IsEqual(rhs);
  83. }
  84. // Matrix addition. Produces a new matrix by adding the corresponding elements
  85. // together.
  86. inline Matrix3F operator+(const Matrix3F& lhs, const Matrix3F& rhs) {
  87. return lhs.Add(rhs);
  88. }
  89. // Matrix subtraction. Produces a new matrix by subtracting elements of rhs
  90. // from corresponding elements of lhs.
  91. inline Matrix3F operator-(const Matrix3F& lhs, const Matrix3F& rhs) {
  92. return lhs.Subtract(rhs);
  93. }
  94. GEOMETRY_EXPORT Matrix3F MatrixProduct(const Matrix3F& lhs,
  95. const Matrix3F& rhs);
  96. GEOMETRY_EXPORT Vector3dF MatrixProduct(const Matrix3F& lhs,
  97. const Vector3dF& rhs);
  98. } // namespace gfx
  99. #endif // UI_GFX_GEOMETRY_MATRIX3_F_H_