Home | History | Annotate | Download | only in gfx
      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_MATRIX3_F_H_
      6 #define UI_GFX_MATRIX3_F_H_
      7 
      8 #include "base/logging.h"
      9 #include "ui/gfx/vector3d_f.h"
     10 
     11 namespace gfx {
     12 
     13 class UI_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 
     27   float get(int i, int j) const {
     28     return data_[MatrixToArrayCoords(i, j)];
     29   }
     30 
     31   void set(int i, int j, float v) {
     32     data_[MatrixToArrayCoords(i, j)] = v;
     33   }
     34 
     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 
     49   Vector3dF get_column(int i) const {
     50     return Vector3dF(
     51       data_[MatrixToArrayCoords(0, i)],
     52       data_[MatrixToArrayCoords(1, i)],
     53       data_[MatrixToArrayCoords(2, i)]);
     54   }
     55 
     56   void set_column(int i, const Vector3dF& c) {
     57     data_[MatrixToArrayCoords(0, i)] = c.x();
     58     data_[MatrixToArrayCoords(1, i)] = c.y();
     59     data_[MatrixToArrayCoords(2, i)] = c.z();
     60   }
     61 
     62   // Returns an inverse of this if the matrix is non-singular, zero (== Zero())
     63   // otherwise.
     64   Matrix3F Inverse() const;
     65 
     66   // Value of the determinant of the matrix.
     67   float Determinant() const;
     68 
     69   // Trace (sum of diagonal elements) of the matrix.
     70   float Trace() const {
     71     return data_[MatrixToArrayCoords(0, 0)] +
     72         data_[MatrixToArrayCoords(1, 1)] +
     73         data_[MatrixToArrayCoords(2, 2)];
     74   }
     75 
     76   // Compute eigenvalues and (optionally) normalized eigenvectors of
     77   // a positive defnite matrix *this. Eigenvectors are computed only if
     78   // non-null |eigenvectors| matrix is passed. If it is NULL, the routine
     79   // will not attempt to compute eigenvectors but will still return eigenvalues
     80   // if they can be computed.
     81   // If eigenvalues cannot be computed (the matrix does not meet constraints)
     82   // the 0-vector is returned. Note that to retrieve eigenvalues, the matrix
     83   // only needs to be symmetric while eigenvectors require it to be
     84   // positive-definite. Passing a non-positive definite matrix will result in
     85   // NaNs in vectors which cannot be computed.
     86   // Eigenvectors are placed as column in |eigenvectors| in order corresponding
     87   // to eigenvalues.
     88   Vector3dF SolveEigenproblem(Matrix3F* eigenvectors) const;
     89 
     90  private:
     91   Matrix3F();  // Uninitialized default.
     92 
     93   static int MatrixToArrayCoords(int i, int j) {
     94     DCHECK(i >= 0 && i < 3);
     95     DCHECK(j >= 0 && j < 3);
     96     return i * 3 + j;
     97   }
     98 
     99   float data_[9];
    100 };
    101 
    102 inline bool operator==(const Matrix3F& lhs, const Matrix3F& rhs) {
    103   return lhs.IsEqual(rhs);
    104 }
    105 
    106 }  // namespace gfx
    107 
    108 #endif  // UI_GFX_MATRIX3_F_H_
    109