LCOV - code coverage report
Current view: directory - objdir/dist/include/mozilla/gfx - Matrix.h (source / functions) Found Hit Coverage
Test: app.info Lines: 60 3 5.0 %
Date: 2012-06-02 Functions: 11 1 9.1 %

       1                 : /* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 2 -*-
       2                 :  * ***** BEGIN LICENSE BLOCK *****
       3                 :  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
       4                 :  *
       5                 :  * The contents of this file are subject to the Mozilla Public License Version
       6                 :  * 1.1 (the "License"); you may not use this file except in compliance with
       7                 :  * the License. You may obtain a copy of the License at
       8                 :  * http://www.mozilla.org/MPL/
       9                 :  *
      10                 :  * Software distributed under the License is distributed on an "AS IS" basis,
      11                 :  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
      12                 :  * for the specific language governing rights and limitations under the
      13                 :  * License.
      14                 :  *
      15                 :  * The Original Code is Mozilla Corporation code.
      16                 :  *
      17                 :  * The Initial Developer of the Original Code is Mozilla Foundation.
      18                 :  * Portions created by the Initial Developer are Copyright (C) 2011
      19                 :  * the Initial Developer. All Rights Reserved.
      20                 :  *
      21                 :  * Contributor(s):
      22                 :  *   Bas Schouten <bschouten@mozilla.com>
      23                 :  *
      24                 :  * Alternatively, the contents of this file may be used under the terms of
      25                 :  * either the GNU General Public License Version 2 or later (the "GPL"), or
      26                 :  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
      27                 :  * in which case the provisions of the GPL or the LGPL are applicable instead
      28                 :  * of those above. If you wish to allow use of your version of this file only
      29                 :  * under the terms of either the GPL or the LGPL, and not to allow others to
      30                 :  * use your version of this file under the terms of the MPL, indicate your
      31                 :  * decision by deleting the provisions above and replace them with the notice
      32                 :  * and other provisions required by the GPL or the LGPL. If you do not delete
      33                 :  * the provisions above, a recipient may use your version of this file under
      34                 :  * the terms of any one of the MPL, the GPL or the LGPL.
      35                 :  *
      36                 :  * ***** END LICENSE BLOCK ***** */
      37                 : 
      38                 : #ifndef MOZILLA_GFX_MATRIX_H_
      39                 : #define MOZILLA_GFX_MATRIX_H_
      40                 : 
      41                 : #include "Types.h"
      42                 : #include "Rect.h"
      43                 : #include "Point.h"
      44                 : #include <math.h>
      45                 : 
      46                 : namespace mozilla {
      47                 : namespace gfx {
      48                 : 
      49                 : class Matrix
      50                 : {
      51                 : public:
      52             147 :   Matrix()
      53                 :     : _11(1.0f), _12(0)
      54                 :     , _21(0), _22(1.0f)
      55             147 :     , _31(0), _32(0)
      56             147 :   {}
      57               0 :   Matrix(Float a11, Float a12, Float a21, Float a22, Float a31, Float a32)
      58                 :     : _11(a11), _12(a12)
      59                 :     , _21(a21), _22(a22)
      60               0 :     , _31(a31), _32(a32)
      61               0 :   {}
      62                 :   Float _11, _12;
      63                 :   Float _21, _22;
      64                 :   Float _31, _32;
      65                 : 
      66               0 :   Point operator *(const Point &aPoint) const
      67                 :   {
      68               0 :     Point retPoint;
      69                 : 
      70               0 :     retPoint.x = aPoint.x * _11 + aPoint.y * _21 + _31;
      71               0 :     retPoint.y = aPoint.x * _12 + aPoint.y * _22 + _32;
      72                 : 
      73                 :     return retPoint;
      74                 :   }
      75                 : 
      76               0 :   Size operator *(const Size &aSize) const
      77                 :   {
      78               0 :     Size retSize;
      79                 : 
      80               0 :     retSize.width = aSize.width * _11 + aSize.height * _21;
      81               0 :     retSize.height = aSize.width * _12 + aSize.height * _22;
      82                 : 
      83                 :     return retSize;
      84                 :   }
      85                 : 
      86                 :   Rect TransformBounds(const Rect& rect) const;
      87                 : 
      88                 :   // Apply a scale to this matrix. This scale will be applied -before- the
      89                 :   // existing transformation of the matrix.
      90               0 :   Matrix &Scale(Float aX, Float aY)
      91                 :   {
      92               0 :     _11 *= aX;
      93               0 :     _12 *= aX;
      94               0 :     _21 *= aY;
      95               0 :     _22 *= aY;
      96                 : 
      97               0 :     return *this;
      98                 :   }
      99                 : 
     100               0 :   Matrix &Translate(Float aX, Float aY)
     101                 :   {
     102               0 :     _31 += _11 * aX + _21 * aY;
     103               0 :     _32 += _12 * aX + _22 * aY;
     104                 : 
     105               0 :     return *this;
     106                 :   }
     107                 : 
     108               0 :   bool Invert()
     109                 :   {
     110                 :     // Compute co-factors.
     111               0 :     Float A = _22;
     112               0 :     Float B = -_21;
     113               0 :     Float C = _21 * _32 - _22 * _31;
     114               0 :     Float D = -_12;
     115               0 :     Float E = _11;
     116               0 :     Float F = _31 * _12 - _11 * _32;
     117                 : 
     118               0 :     Float det = Determinant();
     119                 : 
     120               0 :     if (!det) {
     121               0 :       return false;
     122                 :     }
     123                 : 
     124               0 :     Float inv_det = 1 / det;
     125                 : 
     126               0 :     _11 = inv_det * A;
     127               0 :     _12 = inv_det * D;
     128               0 :     _21 = inv_det * B;
     129               0 :     _22 = inv_det * E;
     130               0 :     _31 = inv_det * C;
     131               0 :     _32 = inv_det * F;
     132                 : 
     133               0 :     return true;
     134                 :   }
     135                 : 
     136               0 :   Float Determinant() const
     137                 :   {
     138               0 :     return _11 * _22 - _12 * _21;
     139                 :   }
     140                 :   
     141                 :   static Matrix Rotation(Float aAngle);
     142                 : 
     143               0 :   Matrix operator*(const Matrix &aMatrix) const
     144                 :   {
     145               0 :     Matrix resultMatrix;
     146                 : 
     147               0 :     resultMatrix._11 = this->_11 * aMatrix._11 + this->_12 * aMatrix._21;
     148               0 :     resultMatrix._12 = this->_11 * aMatrix._12 + this->_12 * aMatrix._22;
     149               0 :     resultMatrix._21 = this->_21 * aMatrix._11 + this->_22 * aMatrix._21;
     150               0 :     resultMatrix._22 = this->_21 * aMatrix._12 + this->_22 * aMatrix._22;
     151               0 :     resultMatrix._31 = this->_31 * aMatrix._11 + this->_32 * aMatrix._21 + aMatrix._31;
     152               0 :     resultMatrix._32 = this->_31 * aMatrix._12 + this->_32 * aMatrix._22 + aMatrix._32;
     153                 : 
     154                 :     return resultMatrix;
     155                 :   }
     156                 : 
     157                 :   /* Returns true if the other matrix is fuzzy-equal to this matrix.
     158                 :    * Note that this isn't a cheap comparison!
     159                 :    */
     160                 :   bool operator==(const Matrix& other) const
     161                 :   {
     162                 :     return FuzzyEqual(_11, other._11) && FuzzyEqual(_12, other._12) &&
     163                 :            FuzzyEqual(_21, other._21) && FuzzyEqual(_22, other._22) &&
     164                 :            FuzzyEqual(_31, other._31) && FuzzyEqual(_32, other._32);
     165                 :   }
     166                 : 
     167                 :   bool operator!=(const Matrix& other) const
     168                 :   {
     169                 :     return !(*this == other);
     170                 :   }
     171                 : 
     172                 :   /* Returns true if the matrix is a rectilinear transformation (i.e.
     173                 :    * grid-aligned rectangles are transformed to grid-aligned rectangles)
     174                 :    */
     175               0 :   bool IsRectilinear() {
     176               0 :     if (FuzzyEqual(_12, 0) && FuzzyEqual(_21, 0)) {
     177               0 :       return true;
     178               0 :     } else if (FuzzyEqual(_22, 0) && FuzzyEqual(_11, 0)) {
     179               0 :       return true;
     180                 :     }
     181                 : 
     182               0 :     return false;
     183                 :   }
     184                 : 
     185                 :   /* Returns true if the matrix is an identity matrix.
     186                 :    */
     187                 :   bool IsIdentity() const
     188                 :   {
     189                 :     return _11 == 1.0f && _12 == 0.0f &&
     190                 :            _21 == 0.0f && _22 == 1.0f &&
     191                 :            _31 == 0.0f && _32 == 0.0f;
     192                 :   }
     193                 : 
     194                 : private:
     195               0 :   static bool FuzzyEqual(Float aV1, Float aV2) {
     196                 :     // XXX - Check if fabs does the smart thing and just negates the sign bit.
     197               0 :     return fabs(aV2 - aV1) < 1e-6;
     198                 :   }
     199                 : };
     200                 : 
     201                 : }
     202                 : }
     203                 : 
     204                 : #endif /* MOZILLA_GFX_MATRIX_H_ */

Generated by: LCOV version 1.7