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 5 #ifndef UI_GFX_GEOMETRY_MATRIX3_F_H_ 6 #define UI_GFX_GEOMETRY_MATRIX3_F_H_ 7 8 #include "base/logging.h" 9 #include "ui/gfx/geometry/vector3d_f.h" 10 11 namespace gfx { 12 13 class GFX_EXPORT Matrix3F { 14 public: 15 ~Matrix3F(); 16 17 static Matrix3F Zeros(); 18 static Matrix3F Ones(); 19 static Matrix3F Identity(); 20 static Matrix3F FromOuterProduct(const Vector3dF& a, const Vector3dF& bt); 21 22 bool IsEqual(const Matrix3F& rhs) const; 23 24 // Element-wise comparison with given precision. 25 bool IsNear(const Matrix3F& rhs, float precision) const; 26 get(int i,int j)27 float get(int i, int j) const { 28 return data_[MatrixToArrayCoords(i, j)]; 29 } 30 set(int i,int j,float v)31 void set(int i, int j, float v) { 32 data_[MatrixToArrayCoords(i, j)] = v; 33 } 34 set(float m00,float m01,float m02,float m10,float m11,float m12,float m20,float m21,float m22)35 void set(float m00, float m01, float m02, 36 float m10, float m11, float m12, 37 float m20, float m21, float m22) { 38 data_[0] = m00; 39 data_[1] = m01; 40 data_[2] = m02; 41 data_[3] = m10; 42 data_[4] = m11; 43 data_[5] = m12; 44 data_[6] = m20; 45 data_[7] = m21; 46 data_[8] = m22; 47 } 48 get_row(int i)49 Vector3dF get_row(int i) const { 50 return Vector3dF(data_[MatrixToArrayCoords(i, 0)], 51 data_[MatrixToArrayCoords(i, 1)], 52 data_[MatrixToArrayCoords(i, 2)]); 53 } 54 get_column(int i)55 Vector3dF get_column(int i) const { 56 return Vector3dF( 57 data_[MatrixToArrayCoords(0, i)], 58 data_[MatrixToArrayCoords(1, i)], 59 data_[MatrixToArrayCoords(2, i)]); 60 } 61 set_column(int i,const Vector3dF & c)62 void set_column(int i, const Vector3dF& c) { 63 data_[MatrixToArrayCoords(0, i)] = c.x(); 64 data_[MatrixToArrayCoords(1, i)] = c.y(); 65 data_[MatrixToArrayCoords(2, i)] = c.z(); 66 } 67 68 // Produces a new matrix by adding the elements of |rhs| to this matrix 69 Matrix3F Add(const Matrix3F& rhs) const; 70 // Produces a new matrix by subtracting elements of |rhs| from this matrix. 71 Matrix3F Subtract(const Matrix3F& rhs) const; 72 73 // Returns an inverse of this if the matrix is non-singular, zero (== Zero()) 74 // otherwise. 75 Matrix3F Inverse() const; 76 77 // Returns a transpose of this matrix. 78 Matrix3F Transpose() const; 79 80 // Value of the determinant of the matrix. 81 float Determinant() const; 82 83 // Trace (sum of diagonal elements) of the matrix. Trace()84 float Trace() const { 85 return data_[MatrixToArrayCoords(0, 0)] + 86 data_[MatrixToArrayCoords(1, 1)] + 87 data_[MatrixToArrayCoords(2, 2)]; 88 } 89 90 // Compute eigenvalues and (optionally) normalized eigenvectors of 91 // a positive defnite matrix *this. Eigenvectors are computed only if 92 // non-null |eigenvectors| matrix is passed. If it is NULL, the routine 93 // will not attempt to compute eigenvectors but will still return eigenvalues 94 // if they can be computed. 95 // If eigenvalues cannot be computed (the matrix does not meet constraints) 96 // the 0-vector is returned. Note that to retrieve eigenvalues, the matrix 97 // only needs to be symmetric while eigenvectors require it to be 98 // positive-definite. Passing a non-positive definite matrix will result in 99 // NaNs in vectors which cannot be computed. 100 // Eigenvectors are placed as column in |eigenvectors| in order corresponding 101 // to eigenvalues. 102 Vector3dF SolveEigenproblem(Matrix3F* eigenvectors) const; 103 104 std::string ToString() const; 105 106 private: 107 Matrix3F(); // Uninitialized default. 108 MatrixToArrayCoords(int i,int j)109 static int MatrixToArrayCoords(int i, int j) { 110 DCHECK(i >= 0 && i < 3); 111 DCHECK(j >= 0 && j < 3); 112 return i * 3 + j; 113 } 114 115 float data_[9]; 116 }; 117 118 inline bool operator==(const Matrix3F& lhs, const Matrix3F& rhs) { 119 return lhs.IsEqual(rhs); 120 } 121 122 // Matrix addition. Produces a new matrix by adding the corresponding elements 123 // together. 124 inline Matrix3F operator+(const Matrix3F& lhs, const Matrix3F& rhs) { 125 return lhs.Add(rhs); 126 } 127 128 // Matrix subtraction. Produces a new matrix by subtracting elements of rhs 129 // from corresponding elements of lhs. 130 inline Matrix3F operator-(const Matrix3F& lhs, const Matrix3F& rhs) { 131 return lhs.Subtract(rhs); 132 } 133 134 GFX_EXPORT Matrix3F MatrixProduct(const Matrix3F& lhs, const Matrix3F& rhs); 135 GFX_EXPORT Vector3dF MatrixProduct(const Matrix3F& lhs, const Vector3dF& rhs); 136 137 } // namespace gfx 138 139 #endif // UI_GFX_GEOMETRY_MATRIX3_F_H_ 140