LCOV - code coverage report
Current view: directory - gfx/thebes - gfx3DMatrix.cpp (source / functions) Found Hit Coverage
Test: app.info Lines: 405 0 0.0 %
Date: 2012-06-02 Functions: 49 0 0.0 %

       1                 : /* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 4 -*-
       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 Oracle Corporation code.
      16                 :  *
      17                 :  * The Initial Developer of the Original Code is Oracle Corporation.
      18                 :  * Portions created by the Initial Developer are Copyright (C) 2005
      19                 :  * the Initial Developer. All Rights Reserved.
      20                 :  *
      21                 :  * Contributor(s):
      22                 :  *   Bas Schouten <bschouten@mozilla.com>
      23                 :  *   Matt Woodrow <mwoodrow@mozilla.com>
      24                 :  *
      25                 :  * Alternatively, the contents of this file may be used under the terms of
      26                 :  * either the GNU General Public License Version 2 or later (the "GPL"), or
      27                 :  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
      28                 :  * in which case the provisions of the GPL or the LGPL are applicable instead
      29                 :  * of those above. If you wish to allow use of your version of this file only
      30                 :  * under the terms of either the GPL or the LGPL, and not to allow others to
      31                 :  * use your version of this file under the terms of the MPL, indicate your
      32                 :  * decision by deleting the provisions above and replace them with the notice
      33                 :  * and other provisions required by the GPL or the LGPL. If you do not delete
      34                 :  * the provisions above, a recipient may use your version of this file under
      35                 :  * the terms of any one of the MPL, the GPL or the LGPL.
      36                 :  *
      37                 :  * ***** END LICENSE BLOCK ***** */
      38                 : 
      39                 : #include "gfxMatrix.h"
      40                 : #include "gfx3DMatrix.h"
      41                 : #include <math.h>
      42                 : #include <algorithm>
      43                 : using namespace std;
      44                 : 
      45                 : /* Force small values to zero.  We do this to avoid having sin(360deg)
      46                 :  * evaluate to a tiny but nonzero value.
      47                 :  */
      48               0 : static double FlushToZero(double aVal)
      49                 : {
      50               0 :   if (-FLT_EPSILON < aVal && aVal < FLT_EPSILON)
      51               0 :     return 0.0f;
      52                 :   else
      53               0 :     return aVal;
      54                 : }
      55                 : 
      56                 : /* Computes tan(aTheta).  For values of aTheta such that tan(aTheta) is
      57                 :  * undefined or very large, SafeTangent returns a manageably large value
      58                 :  * of the correct sign.
      59                 :  */
      60               0 : static double SafeTangent(double aTheta)
      61                 : {
      62               0 :   const double kEpsilon = 0.0001;
      63                 : 
      64                 :   /* tan(theta) = sin(theta)/cos(theta); problems arise when
      65                 :    * cos(theta) is too close to zero.  Limit cos(theta) to the
      66                 :    * range [-1, -epsilon] U [epsilon, 1].
      67                 :    */
      68               0 :   double sinTheta = sin(aTheta);
      69               0 :   double cosTheta = cos(aTheta);
      70                 : 
      71               0 :   if (cosTheta >= 0 && cosTheta < kEpsilon)
      72               0 :     cosTheta = kEpsilon;
      73               0 :   else if (cosTheta < 0 && cosTheta >= -kEpsilon)
      74               0 :     cosTheta = -kEpsilon;
      75                 : 
      76               0 :   return FlushToZero(sinTheta / cosTheta);
      77                 : }
      78                 : 
      79               0 : gfx3DMatrix::gfx3DMatrix(void)
      80                 : {
      81               0 :   _11 = _22 = _33 = _44 = 1.0f;
      82               0 :   _12 = _13 = _14 = 0.0f;
      83               0 :   _21 = _23 = _24 = 0.0f;
      84               0 :   _31 = _32 = _34 = 0.0f;
      85               0 :   _41 = _42 = _43 = 0.0f;
      86               0 : }
      87                 : 
      88                 : gfx3DMatrix
      89               0 : gfx3DMatrix::operator*(const gfx3DMatrix &aMatrix) const
      90                 : {
      91               0 :   if (Is2D() && aMatrix.Is2D()) {
      92               0 :     return Multiply2D(aMatrix);
      93                 :   }
      94                 : 
      95               0 :   gfx3DMatrix matrix;
      96                 : 
      97               0 :   matrix._11 = _11 * aMatrix._11 + _12 * aMatrix._21 + _13 * aMatrix._31 + _14 * aMatrix._41;
      98               0 :   matrix._21 = _21 * aMatrix._11 + _22 * aMatrix._21 + _23 * aMatrix._31 + _24 * aMatrix._41;
      99               0 :   matrix._31 = _31 * aMatrix._11 + _32 * aMatrix._21 + _33 * aMatrix._31 + _34 * aMatrix._41;
     100               0 :   matrix._41 = _41 * aMatrix._11 + _42 * aMatrix._21 + _43 * aMatrix._31 + _44 * aMatrix._41;
     101               0 :   matrix._12 = _11 * aMatrix._12 + _12 * aMatrix._22 + _13 * aMatrix._32 + _14 * aMatrix._42;
     102               0 :   matrix._22 = _21 * aMatrix._12 + _22 * aMatrix._22 + _23 * aMatrix._32 + _24 * aMatrix._42;
     103               0 :   matrix._32 = _31 * aMatrix._12 + _32 * aMatrix._22 + _33 * aMatrix._32 + _34 * aMatrix._42;
     104               0 :   matrix._42 = _41 * aMatrix._12 + _42 * aMatrix._22 + _43 * aMatrix._32 + _44 * aMatrix._42;
     105               0 :   matrix._13 = _11 * aMatrix._13 + _12 * aMatrix._23 + _13 * aMatrix._33 + _14 * aMatrix._43;
     106               0 :   matrix._23 = _21 * aMatrix._13 + _22 * aMatrix._23 + _23 * aMatrix._33 + _24 * aMatrix._43;
     107               0 :   matrix._33 = _31 * aMatrix._13 + _32 * aMatrix._23 + _33 * aMatrix._33 + _34 * aMatrix._43;
     108               0 :   matrix._43 = _41 * aMatrix._13 + _42 * aMatrix._23 + _43 * aMatrix._33 + _44 * aMatrix._43;
     109               0 :   matrix._14 = _11 * aMatrix._14 + _12 * aMatrix._24 + _13 * aMatrix._34 + _14 * aMatrix._44;
     110               0 :   matrix._24 = _21 * aMatrix._14 + _22 * aMatrix._24 + _23 * aMatrix._34 + _24 * aMatrix._44;
     111               0 :   matrix._34 = _31 * aMatrix._14 + _32 * aMatrix._24 + _33 * aMatrix._34 + _34 * aMatrix._44;
     112               0 :   matrix._44 = _41 * aMatrix._14 + _42 * aMatrix._24 + _43 * aMatrix._34 + _44 * aMatrix._44;
     113                 : 
     114               0 :   return matrix;
     115                 : }
     116                 : 
     117                 : gfx3DMatrix&
     118               0 : gfx3DMatrix::operator*=(const gfx3DMatrix &aMatrix)
     119                 : {
     120               0 :   return *this = *this * aMatrix;
     121                 : }
     122                 : 
     123                 : gfx3DMatrix
     124               0 : gfx3DMatrix::Multiply2D(const gfx3DMatrix &aMatrix) const
     125                 : {
     126               0 :   gfx3DMatrix matrix;
     127                 : 
     128               0 :   matrix._11 = _11 * aMatrix._11 + _12 * aMatrix._21;
     129               0 :   matrix._21 = _21 * aMatrix._11 + _22 * aMatrix._21;
     130               0 :   matrix._41 = _41 * aMatrix._11 + _42 * aMatrix._21 + aMatrix._41;
     131               0 :   matrix._12 = _11 * aMatrix._12 + _12 * aMatrix._22;
     132               0 :   matrix._22 = _21 * aMatrix._12 + _22 * aMatrix._22;
     133               0 :   matrix._42 = _41 * aMatrix._12 + _42 * aMatrix._22 + aMatrix._42;
     134                 : 
     135                 :   return matrix;
     136                 : }
     137                 : 
     138                 : bool
     139               0 : gfx3DMatrix::operator==(const gfx3DMatrix& o) const
     140                 : {
     141                 :   // XXX would be nice to memcmp here, but that breaks IEEE 754 semantics
     142                 :   return _11 == o._11 && _12 == o._12 && _13 == o._13 && _14 == o._14 &&
     143                 :          _21 == o._21 && _22 == o._22 && _23 == o._23 && _24 == o._24 &&
     144                 :          _31 == o._31 && _32 == o._32 && _33 == o._33 && _34 == o._34 &&
     145               0 :          _41 == o._41 && _42 == o._42 && _43 == o._43 && _44 == o._44;
     146                 : }
     147                 : 
     148                 : gfx3DMatrix&
     149               0 : gfx3DMatrix::operator/=(const gfxFloat scalar)
     150                 : {
     151               0 :   _11 /= scalar;
     152               0 :   _12 /= scalar;
     153               0 :   _13 /= scalar;
     154               0 :   _14 /= scalar;
     155               0 :   _21 /= scalar;
     156               0 :   _22 /= scalar;
     157               0 :   _23 /= scalar;
     158               0 :   _24 /= scalar;
     159               0 :   _31 /= scalar;
     160               0 :   _32 /= scalar;
     161               0 :   _33 /= scalar;
     162               0 :   _34 /= scalar;
     163               0 :   _41 /= scalar;
     164               0 :   _42 /= scalar;
     165               0 :   _43 /= scalar;
     166               0 :   _44 /= scalar;
     167               0 :   return *this;
     168                 : }
     169                 : 
     170                 : gfx3DMatrix
     171               0 : gfx3DMatrix::From2D(const gfxMatrix &aMatrix)
     172                 : {
     173               0 :   gfx3DMatrix matrix;
     174               0 :   matrix._11 = (float)aMatrix.xx;
     175               0 :   matrix._12 = (float)aMatrix.yx;
     176               0 :   matrix._21 = (float)aMatrix.xy;
     177               0 :   matrix._22 = (float)aMatrix.yy;
     178               0 :   matrix._41 = (float)aMatrix.x0;
     179               0 :   matrix._42 = (float)aMatrix.y0;
     180                 :   return matrix;
     181                 : }
     182                 : 
     183                 : bool
     184               0 : gfx3DMatrix::IsIdentity() const
     185                 : {
     186                 :   return _11 == 1.0f && _12 == 0.0f && _13 == 0.0f && _14 == 0.0f &&
     187                 :          _21 == 0.0f && _22 == 1.0f && _23 == 0.0f && _24 == 0.0f &&
     188                 :          _31 == 0.0f && _32 == 0.0f && _33 == 1.0f && _34 == 0.0f &&
     189               0 :          _41 == 0.0f && _42 == 0.0f && _43 == 0.0f && _44 == 1.0f;
     190                 : }
     191                 : 
     192                 : void
     193               0 : gfx3DMatrix::Translate(const gfxPoint3D& aPoint)
     194                 : {
     195               0 :     _41 += aPoint.x * _11 + aPoint.y * _21 + aPoint.z * _31;
     196               0 :     _42 += aPoint.x * _12 + aPoint.y * _22 + aPoint.z * _32;
     197               0 :     _43 += aPoint.x * _13 + aPoint.y * _23 + aPoint.z * _33;
     198               0 :     _44 += aPoint.x * _14 + aPoint.y * _24 + aPoint.z * _34;
     199               0 : }
     200                 : 
     201                 : void
     202               0 : gfx3DMatrix::TranslatePost(const gfxPoint3D& aPoint)
     203                 : {
     204               0 :     _11 += _14 * aPoint.x;
     205               0 :     _21 += _24 * aPoint.x;
     206               0 :     _31 += _34 * aPoint.x;
     207               0 :     _41 += _44 * aPoint.x;
     208               0 :     _12 += _14 * aPoint.y;
     209               0 :     _22 += _24 * aPoint.y;
     210               0 :     _32 += _34 * aPoint.y;
     211               0 :     _42 += _44 * aPoint.y;
     212               0 :     _13 += _14 * aPoint.z;
     213               0 :     _23 += _24 * aPoint.z;
     214               0 :     _33 += _34 * aPoint.z;
     215               0 :     _43 += _44 * aPoint.z;
     216               0 : }
     217                 : 
     218                 : void
     219               0 : gfx3DMatrix::SkewXY(double aSkew)
     220                 : {
     221               0 :     (*this)[1] += (*this)[0] * aSkew;
     222               0 : }
     223                 : 
     224                 : void 
     225               0 : gfx3DMatrix::SkewXZ(double aSkew)
     226                 : {
     227               0 :     (*this)[2] += (*this)[0] * aSkew;
     228               0 : }
     229                 : 
     230                 : void
     231               0 : gfx3DMatrix::SkewYZ(double aSkew)
     232                 : {
     233               0 :     (*this)[2] += (*this)[1] * aSkew;
     234               0 : }
     235                 : 
     236                 : void
     237               0 : gfx3DMatrix::Scale(float aX, float aY, float aZ)
     238                 : {
     239               0 :     (*this)[0] *= aX;
     240               0 :     (*this)[1] *= aY;
     241               0 :     (*this)[2] *= aZ;
     242               0 : }
     243                 : 
     244                 : void
     245               0 : gfx3DMatrix::Perspective(float aDepth)
     246                 : {
     247               0 :   NS_ASSERTION(aDepth > 0.0f, "Perspective must be positive!");
     248               0 :   _31 += -1.0/aDepth * _41;
     249               0 :   _32 += -1.0/aDepth * _42;
     250               0 :   _33 += -1.0/aDepth * _43;
     251               0 :   _34 += -1.0/aDepth * _44;
     252               0 : }
     253                 : 
     254               0 : void gfx3DMatrix::SkewXY(double aXSkew, double aYSkew)
     255                 : {
     256               0 :   float tanX = SafeTangent(aXSkew);
     257               0 :   float tanY = SafeTangent(aYSkew);
     258                 :   float temp;
     259                 : 
     260               0 :   temp = _11;
     261               0 :   _11 += tanY * _21;
     262               0 :   _21 += tanX * temp;
     263                 : 
     264               0 :   temp = _12;
     265               0 :   _12 += tanY * _22;
     266               0 :   _22 += tanX * temp;
     267                 :   
     268               0 :   temp = _13;
     269               0 :   _13 += tanY * _23;
     270               0 :   _23 += tanX * temp;
     271                 :   
     272               0 :   temp = _14;
     273               0 :   _14 += tanY * _24;
     274               0 :   _24 += tanX * temp;
     275               0 : }
     276                 : 
     277                 : void
     278               0 : gfx3DMatrix::RotateX(double aTheta)
     279                 : {
     280               0 :   double cosTheta = FlushToZero(cos(aTheta));
     281               0 :   double sinTheta = FlushToZero(sin(aTheta));
     282                 : 
     283                 :   float temp;
     284                 : 
     285               0 :   temp = _21;
     286               0 :   _21 = cosTheta * _21 + sinTheta * _31;
     287               0 :   _31 = -sinTheta * temp + cosTheta * _31;
     288                 :   
     289               0 :   temp = _22;
     290               0 :   _22 = cosTheta * _22 + sinTheta * _32;
     291               0 :   _32 = -sinTheta * temp + cosTheta * _32;
     292                 :   
     293               0 :   temp = _23;
     294               0 :   _23 = cosTheta * _23 + sinTheta * _33;
     295               0 :   _33 = -sinTheta * temp + cosTheta * _33;
     296                 :   
     297               0 :   temp = _24;
     298               0 :   _24 = cosTheta * _24 + sinTheta * _34;
     299               0 :   _34 = -sinTheta * temp + cosTheta * _34;
     300               0 : }
     301                 : 
     302                 : void
     303               0 : gfx3DMatrix::RotateY(double aTheta)
     304                 : {
     305               0 :   double cosTheta = FlushToZero(cos(aTheta));
     306               0 :   double sinTheta = FlushToZero(sin(aTheta));
     307                 : 
     308                 :   float temp;
     309                 : 
     310               0 :   temp = _11;
     311               0 :   _11 = cosTheta * _11 + -sinTheta * _31;
     312               0 :   _31 = sinTheta * temp + cosTheta * _31;
     313                 :   
     314               0 :   temp = _12;
     315               0 :   _12 = cosTheta * _12 + -sinTheta * _32;
     316               0 :   _32 = sinTheta * temp + cosTheta * _32;
     317                 :   
     318               0 :   temp = _13;
     319               0 :   _13 = cosTheta * _13 + -sinTheta * _33;
     320               0 :   _33 = sinTheta * temp + cosTheta * _33;
     321                 :   
     322               0 :   temp = _14;
     323               0 :   _14 = cosTheta * _14 + -sinTheta * _34;
     324               0 :   _34 = sinTheta * temp + cosTheta * _34;
     325               0 : }
     326                 : 
     327                 : void
     328               0 : gfx3DMatrix::RotateZ(double aTheta)
     329                 : {
     330               0 :   double cosTheta = FlushToZero(cos(aTheta));
     331               0 :   double sinTheta = FlushToZero(sin(aTheta));
     332                 : 
     333                 :   float temp;
     334                 : 
     335               0 :   temp = _11;
     336               0 :   _11 = cosTheta * _11 + sinTheta * _21;
     337               0 :   _21 = -sinTheta * temp + cosTheta * _21;
     338                 :   
     339               0 :   temp = _12;
     340               0 :   _12 = cosTheta * _12 + sinTheta * _22;
     341               0 :   _22 = -sinTheta * temp + cosTheta * _22;
     342                 :   
     343               0 :   temp = _13;
     344               0 :   _13 = cosTheta * _13 + sinTheta * _23;
     345               0 :   _23 = -sinTheta * temp + cosTheta * _23;
     346                 :   
     347               0 :   temp = _14;
     348               0 :   _14 = cosTheta * _14 + sinTheta * _24;
     349               0 :   _24 = -sinTheta * temp + cosTheta * _24;
     350               0 : }
     351                 : 
     352                 : void 
     353               0 : gfx3DMatrix::PreMultiply(const gfx3DMatrix& aOther)
     354                 : {
     355               0 :   *this = aOther * (*this);
     356               0 : }
     357                 : 
     358                 : void
     359               0 : gfx3DMatrix::PreMultiply(const gfxMatrix& aOther)
     360                 : {
     361               0 :   gfx3DMatrix temp;
     362               0 :   temp._11 = aOther.xx * _11 + aOther.yx * _21;
     363               0 :   temp._21 = aOther.xy * _11 + aOther.yy * _21;
     364               0 :   temp._31 = _31;
     365               0 :   temp._41 = aOther.x0 * _11 + aOther.y0 * _21 + _41;
     366               0 :   temp._12 = aOther.xx * _12 + aOther.yx * _22;
     367               0 :   temp._22 = aOther.xy * _12 + aOther.yy * _22;
     368               0 :   temp._32 = _32;
     369               0 :   temp._42 = aOther.x0 * _12 + aOther.y0 * _22 + _42;
     370               0 :   temp._13 = aOther.xx * _13 + aOther.yx * _23;
     371               0 :   temp._23 = aOther.xy * _13 + aOther.yy * _23;
     372               0 :   temp._33 = _33;
     373               0 :   temp._43 = aOther.x0 * _13 + aOther.y0 * _23 + _43;
     374               0 :   temp._14 = aOther.xx * _14 + aOther.yx * _24;
     375               0 :   temp._24 = aOther.xy * _14 + aOther.yy * _24;
     376               0 :   temp._34 = _34;
     377               0 :   temp._44 = aOther.x0 * _14 + aOther.y0 * _24 + _44;
     378                 : 
     379               0 :   *this = temp;
     380               0 : }
     381                 : 
     382                 : gfx3DMatrix
     383               0 : gfx3DMatrix::Translation(float aX, float aY, float aZ)
     384                 : {
     385               0 :   gfx3DMatrix matrix;
     386                 : 
     387               0 :   matrix._41 = aX;
     388               0 :   matrix._42 = aY;
     389               0 :   matrix._43 = aZ;
     390                 :   return matrix;
     391                 : }
     392                 : 
     393                 : gfx3DMatrix
     394               0 : gfx3DMatrix::Translation(const gfxPoint3D& aPoint)
     395                 : {
     396               0 :   gfx3DMatrix matrix;
     397                 : 
     398               0 :   matrix._41 = aPoint.x;
     399               0 :   matrix._42 = aPoint.y;
     400               0 :   matrix._43 = aPoint.z;
     401                 :   return matrix;
     402                 : }
     403                 : 
     404                 : gfx3DMatrix
     405               0 : gfx3DMatrix::ScalingMatrix(float aFactor)
     406                 : {
     407               0 :   gfx3DMatrix matrix;
     408                 : 
     409               0 :   matrix._11 = matrix._22 = matrix._33 = aFactor;
     410                 :   return matrix;
     411                 : }
     412                 : 
     413                 : gfx3DMatrix
     414               0 : gfx3DMatrix::ScalingMatrix(float aX, float aY, float aZ)
     415                 : {
     416               0 :   gfx3DMatrix matrix;
     417                 : 
     418               0 :   matrix._11 = aX;
     419               0 :   matrix._22 = aY;
     420               0 :   matrix._33 = aZ;
     421                 : 
     422                 :   return matrix;
     423                 : }
     424                 : 
     425                 : gfxFloat
     426               0 : gfx3DMatrix::Determinant() const
     427                 : {
     428                 :   return _14 * _23 * _32 * _41
     429                 :        - _13 * _24 * _32 * _41
     430                 :        - _14 * _22 * _33 * _41
     431                 :        + _12 * _24 * _33 * _41
     432                 :        + _13 * _22 * _34 * _41
     433                 :        - _12 * _23 * _34 * _41
     434                 :        - _14 * _23 * _31 * _42
     435                 :        + _13 * _24 * _31 * _42
     436                 :        + _14 * _21 * _33 * _42
     437                 :        - _11 * _24 * _33 * _42
     438                 :        - _13 * _21 * _34 * _42
     439                 :        + _11 * _23 * _34 * _42
     440                 :        + _14 * _22 * _31 * _43
     441                 :        - _12 * _24 * _31 * _43
     442                 :        - _14 * _21 * _32 * _43
     443                 :        + _11 * _24 * _32 * _43
     444                 :        + _12 * _21 * _34 * _43
     445                 :        - _11 * _22 * _34 * _43
     446                 :        - _13 * _22 * _31 * _44
     447                 :        + _12 * _23 * _31 * _44
     448                 :        + _13 * _21 * _32 * _44
     449                 :        - _11 * _23 * _32 * _44
     450                 :        - _12 * _21 * _33 * _44
     451               0 :        + _11 * _22 * _33 * _44;
     452                 : }
     453                 : 
     454                 : gfxFloat
     455               0 : gfx3DMatrix::Determinant3x3() const
     456                 : {
     457                 :     return _11 * (_22 * _33 - _23 * _32) +
     458                 :            _12 * (_23 * _31 - _33 * _21) +
     459               0 :            _13 * (_21 * _32 - _22 * _31);
     460                 : }
     461                 : 
     462                 : gfx3DMatrix
     463               0 : gfx3DMatrix::Inverse3x3() const
     464                 : {
     465               0 :     gfxFloat det = Determinant3x3();
     466               0 :     if (det == 0.0) {
     467               0 :         return *this;
     468                 :     }
     469                 : 
     470               0 :     gfxFloat detInv = 1/det;
     471               0 :     gfx3DMatrix temp;
     472                 : 
     473               0 :     temp._11 = (_22 * _33 - _23 * _32) * detInv;
     474               0 :     temp._12 = (_13 * _32 - _12 * _33) * detInv;
     475               0 :     temp._13 = (_12 * _23 - _13 * _22) * detInv;
     476               0 :     temp._21 = (_23 * _31 - _33 * _21) * detInv;
     477               0 :     temp._22 = (_11 * _33 - _13 * _31) * detInv;
     478               0 :     temp._23 = (_13 * _21 - _11 * _23) * detInv;
     479               0 :     temp._31 = (_21 * _32 - _22 * _31) * detInv;
     480               0 :     temp._32 = (_31 * _12 - _11 * _32) * detInv;
     481               0 :     temp._33 = (_11 * _22 - _12 * _21) * detInv;
     482               0 :     return temp;
     483                 : }
     484                 : 
     485                 : bool
     486               0 : gfx3DMatrix::IsSingular() const
     487                 : {
     488               0 :   return Determinant() == 0.0;
     489                 : }
     490                 : 
     491                 : gfx3DMatrix
     492               0 : gfx3DMatrix::Inverse() const
     493                 : {
     494               0 :   if (TransposedVector(3) == gfxPointH3D(0, 0, 0, 1)) {
     495                 :     /** 
     496                 :      * When the matrix contains no perspective, the inverse
     497                 :      * is the same as the 3x3 inverse of the rotation components
     498                 :      * multiplied by the inverse of the translation components.
     499                 :      * Doing these steps separately is faster and more numerically
     500                 :      * stable.
     501                 :      *
     502                 :      * Inverse of the translation matrix is just negating
     503                 :      * the values.
     504                 :      */
     505               0 :     gfx3DMatrix matrix3 = Inverse3x3();
     506               0 :     matrix3.Translate(gfxPoint3D(-_41, -_42, -_43));
     507               0 :     return matrix3;
     508                 :  }
     509                 : 
     510               0 :   gfxFloat det = Determinant();
     511               0 :   if (det == 0.0) {
     512               0 :     return *this;
     513                 :   }
     514                 : 
     515               0 :   gfx3DMatrix temp;
     516                 : 
     517                 :   temp._11 = _23*_34*_42 - _24*_33*_42 + 
     518                 :              _24*_32*_43 - _22*_34*_43 - 
     519               0 :              _23*_32*_44 + _22*_33*_44;
     520                 :   temp._12 = _14*_33*_42 - _13*_34*_42 -
     521                 :              _14*_32*_43 + _12*_34*_43 +
     522               0 :              _13*_32*_44 - _12*_33*_44;
     523                 :   temp._13 = _13*_24*_42 - _14*_23*_42 +
     524                 :              _14*_22*_43 - _12*_24*_43 -
     525               0 :              _13*_22*_44 + _12*_23*_44;
     526                 :   temp._14 = _14*_23*_32 - _13*_24*_32 -
     527                 :              _14*_22*_33 + _12*_24*_33 +
     528               0 :              _13*_22*_34 - _12*_23*_34;
     529                 :   temp._21 = _24*_33*_41 - _23*_34*_41 -
     530                 :              _24*_31*_43 + _21*_34*_43 +
     531               0 :              _23*_31*_44 - _21*_33*_44;
     532                 :   temp._22 = _13*_34*_41 - _14*_33*_41 +
     533                 :              _14*_31*_43 - _11*_34*_43 -
     534               0 :              _13*_31*_44 + _11*_33*_44;
     535                 :   temp._23 = _14*_23*_41 - _13*_24*_41 -
     536                 :              _14*_21*_43 + _11*_24*_43 +
     537               0 :              _13*_21*_44 - _11*_23*_44;
     538                 :   temp._24 = _13*_24*_31 - _14*_23*_31 +
     539                 :              _14*_21*_33 - _11*_24*_33 -
     540               0 :              _13*_21*_34 + _11*_23*_34;
     541                 :   temp._31 = _22*_34*_41 - _24*_32*_41 +
     542                 :              _24*_31*_42 - _21*_34*_42 -
     543               0 :              _22*_31*_44 + _21*_32*_44;
     544                 :   temp._32 = _14*_32*_41 - _12*_34*_41 -
     545                 :              _14*_31*_42 + _11*_34*_42 +
     546               0 :              _12*_31*_44 - _11*_32*_44;
     547                 :   temp._33 = _12*_24*_41 - _14*_22*_41 +
     548                 :              _14*_21*_42 - _11*_24*_42 -
     549               0 :              _12*_21*_44 + _11*_22*_44;
     550                 :   temp._34 = _14*_22*_31 - _12*_24*_31 -
     551                 :              _14*_21*_32 + _11*_24*_32 +
     552               0 :              _12*_21*_34 - _11*_22*_34;
     553                 :   temp._41 = _23*_32*_41 - _22*_33*_41 -
     554                 :              _23*_31*_42 + _21*_33*_42 +
     555               0 :              _22*_31*_43 - _21*_32*_43;
     556                 :   temp._42 = _12*_33*_41 - _13*_32*_41 +
     557                 :              _13*_31*_42 - _11*_33*_42 -
     558               0 :              _12*_31*_43 + _11*_32*_43;
     559                 :   temp._43 = _13*_22*_41 - _12*_23*_41 -
     560                 :              _13*_21*_42 + _11*_23*_42 +
     561               0 :              _12*_21*_43 - _11*_22*_43;
     562                 :   temp._44 = _12*_23*_31 - _13*_22*_31 +
     563                 :              _13*_21*_32 - _11*_23*_32 -
     564               0 :              _12*_21*_33 + _11*_22*_33;
     565                 : 
     566               0 :   temp /= det;
     567               0 :   return temp;
     568                 : }
     569                 : 
     570                 : gfx3DMatrix&
     571               0 : gfx3DMatrix::Normalize()
     572                 : {
     573               0 :     for (int i = 0; i < 4; i++) {
     574               0 :         for (int j = 0; j < 4; j++) {
     575               0 :             (*this)[i][j] /= (*this)[3][3];
     576                 :        }
     577                 :     }
     578               0 :     return *this;
     579                 : }
     580                 : 
     581                 : gfx3DMatrix&
     582               0 : gfx3DMatrix::Transpose()
     583                 : {
     584               0 :     *this = Transposed();
     585               0 :     return *this;
     586                 : }
     587                 : 
     588                 : gfx3DMatrix
     589               0 : gfx3DMatrix::Transposed() const
     590                 : {
     591               0 :     gfx3DMatrix temp;
     592               0 :     for (int i = 0; i < 4; i++) {
     593               0 :         temp[i] = TransposedVector(i);
     594                 :     }
     595                 :     return temp;
     596                 : }
     597                 : 
     598                 : gfxPoint
     599               0 : gfx3DMatrix::Transform(const gfxPoint& point) const
     600                 : {
     601               0 :   gfxPoint3D vec3d(point.x, point.y, 0);
     602               0 :   vec3d = Transform3D(vec3d);
     603               0 :   return gfxPoint(vec3d.x, vec3d.y);
     604                 : }
     605                 : 
     606                 : gfxPoint3D
     607               0 : gfx3DMatrix::Transform3D(const gfxPoint3D& point) const
     608                 : {
     609               0 :   gfxFloat x = point.x * _11 + point.y * _21 + point.z * _31 + _41;
     610               0 :   gfxFloat y = point.x * _12 + point.y * _22 + point.z * _32 + _42;
     611               0 :   gfxFloat z = point.x * _13 + point.y * _23 + point.z * _33 + _43;
     612               0 :   gfxFloat w = point.x * _14 + point.y * _24 + point.z * _34 + _44;
     613                 : 
     614               0 :   x /= w;
     615               0 :   y /= w;
     616               0 :   z /= w;
     617                 : 
     618               0 :   return gfxPoint3D(x, y, z);
     619                 : }
     620                 : 
     621                 : gfxPointH3D
     622               0 : gfx3DMatrix::Transform4D(const gfxPointH3D& aPoint) const
     623                 : {
     624               0 :     gfxFloat x = aPoint.x * _11 + aPoint.y * _21 + aPoint.z * _31 + aPoint.w * _41;
     625               0 :     gfxFloat y = aPoint.x * _12 + aPoint.y * _22 + aPoint.z * _32 + aPoint.w * _42;
     626               0 :     gfxFloat z = aPoint.x * _13 + aPoint.y * _23 + aPoint.z * _33 + aPoint.w * _43;
     627               0 :     gfxFloat w = aPoint.x * _14 + aPoint.y * _24 + aPoint.z * _34 + aPoint.w * _44;
     628                 : 
     629               0 :     return gfxPointH3D(x, y, z, w);
     630                 : }
     631                 : 
     632                 : gfxPointH3D
     633               0 : gfx3DMatrix::TransposeTransform4D(const gfxPointH3D& aPoint) const
     634                 : {
     635               0 :     gfxFloat x = aPoint.x * _11 + aPoint.y * _12 + aPoint.z * _13 + aPoint.w * _14;
     636               0 :     gfxFloat y = aPoint.x * _21 + aPoint.y * _22 + aPoint.z * _23 + aPoint.w * _24;
     637               0 :     gfxFloat z = aPoint.x * _31 + aPoint.y * _32 + aPoint.z * _33 + aPoint.w * _34;
     638               0 :     gfxFloat w = aPoint.x * _41 + aPoint.y * _42 + aPoint.z * _43 + aPoint.w * _44;
     639                 : 
     640               0 :     return gfxPointH3D(x, y, z, w);
     641                 : }
     642                 : 
     643                 : gfxRect
     644               0 : gfx3DMatrix::TransformBounds(const gfxRect& rect) const
     645                 : {
     646               0 :   gfxPoint points[4];
     647                 : 
     648               0 :   points[0] = Transform(rect.TopLeft());
     649               0 :   points[1] = Transform(gfxPoint(rect.X() + rect.Width(), rect.Y()));
     650               0 :   points[2] = Transform(gfxPoint(rect.X(), rect.Y() + rect.Height()));
     651               0 :   points[3] = Transform(gfxPoint(rect.X() + rect.Width(),
     652               0 :                                  rect.Y() + rect.Height()));
     653                 : 
     654                 :   gfxFloat min_x, max_x;
     655                 :   gfxFloat min_y, max_y;
     656                 : 
     657               0 :   min_x = max_x = points[0].x;
     658               0 :   min_y = max_y = points[0].y;
     659                 : 
     660               0 :   for (int i=1; i<4; i++) {
     661               0 :     min_x = min(points[i].x, min_x);
     662               0 :     max_x = max(points[i].x, max_x);
     663               0 :     min_y = min(points[i].y, min_y);
     664               0 :     max_y = max(points[i].y, max_y);
     665                 :   }
     666                 : 
     667               0 :   return gfxRect(min_x, min_y, max_x - min_x, max_y - min_y);
     668                 : }
     669                 : 
     670                 : gfxQuad 
     671               0 : gfx3DMatrix::TransformRect(const gfxRect& aRect) const
     672                 : {
     673               0 :   gfxPoint points[4];
     674                 : 
     675               0 :   points[0] = Transform(aRect.TopLeft());
     676               0 :   points[1] = Transform(gfxPoint(aRect.X() + aRect.Width(), aRect.Y()));
     677               0 :   points[2] = Transform(gfxPoint(aRect.X() + aRect.Width(),
     678               0 :                                  aRect.Y() + aRect.Height()));
     679               0 :   points[3] = Transform(gfxPoint(aRect.X(), aRect.Y() + aRect.Height()));
     680                 :   
     681                 :   // Could this ever result in lines that intersect? I don't think so.
     682               0 :   return gfxQuad(points[0], points[1], points[2], points[3]);
     683                 : }
     684                 : 
     685                 : bool
     686               0 : gfx3DMatrix::Is2D() const
     687                 : {
     688               0 :   if (_13 != 0.0f || _14 != 0.0f ||
     689                 :       _23 != 0.0f || _24 != 0.0f ||
     690                 :       _31 != 0.0f || _32 != 0.0f || _33 != 1.0f || _34 != 0.0f ||
     691                 :       _43 != 0.0f || _44 != 1.0f) {
     692               0 :     return false;
     693                 :   }
     694               0 :   return true;
     695                 : }
     696                 : 
     697                 : bool
     698               0 : gfx3DMatrix::Is2D(gfxMatrix* aMatrix) const
     699                 : {
     700               0 :   if (!Is2D()) {
     701               0 :     return false;
     702                 :   }
     703               0 :   if (aMatrix) {
     704               0 :     aMatrix->xx = _11;
     705               0 :     aMatrix->yx = _12;
     706               0 :     aMatrix->xy = _21;
     707               0 :     aMatrix->yy = _22;
     708               0 :     aMatrix->x0 = _41;
     709               0 :     aMatrix->y0 = _42;
     710                 :   }
     711               0 :   return true;
     712                 : }
     713                 : 
     714                 : bool
     715               0 : gfx3DMatrix::CanDraw2D(gfxMatrix* aMatrix) const
     716                 : {
     717               0 :   if (_14 != 0.0f ||
     718                 :       _24 != 0.0f ||
     719                 :       _44 != 1.0f) {
     720               0 :     return false;
     721                 :   }
     722               0 :   if (aMatrix) {
     723               0 :     aMatrix->xx = _11;
     724               0 :     aMatrix->yx = _12;
     725               0 :     aMatrix->xy = _21;
     726               0 :     aMatrix->yy = _22;
     727               0 :     aMatrix->x0 = _41;
     728               0 :     aMatrix->y0 = _42;
     729                 :   }
     730               0 :   return true;
     731                 : }
     732                 : 
     733                 : gfx3DMatrix&
     734               0 : gfx3DMatrix::ProjectTo2D()
     735                 : {
     736               0 :   _31 = 0.0f;
     737               0 :   _32 = 0.0f;
     738               0 :   _13 = 0.0f; 
     739               0 :   _23 = 0.0f; 
     740               0 :   _33 = 1.0f; 
     741               0 :   _43 = 0.0f; 
     742               0 :   _34 = 0.0f;
     743               0 :   return *this;
     744                 : }
     745                 : 
     746               0 : gfxPoint gfx3DMatrix::ProjectPoint(const gfxPoint& aPoint) const
     747                 : {
     748                 :   // Define a ray of the form P + Ut where t is a real number
     749                 :   // w is assumed to always be 1 when transforming 3d points with our
     750                 :   // 4x4 matrix.
     751                 :   // p is our click point, q is another point on the same ray.
     752                 :   // 
     753                 :   // Note: since the transformation is a general projective transformation and is not
     754                 :   // necessarily affine, we can't just take a unit vector u, back-transform it, and use
     755                 :   // it as unit vector on the back-transformed ray. Instead, we really must take two points
     756                 :   // on the ray and back-transform them.
     757               0 :   gfxPoint3D p(aPoint.x, aPoint.y, 0);
     758               0 :   gfxPoint3D q(aPoint.x, aPoint.y, 1);
     759                 : 
     760                 :   // Back transform the vectors (using w = 1) and normalize
     761                 :   // back into 3d vectors by dividing by the w component.
     762               0 :   gfxPoint3D pback = Transform3D(p);
     763               0 :   gfxPoint3D qback = Transform3D(q);
     764               0 :   gfxPoint3D uback = qback - pback;
     765                 : 
     766                 :   // Find the point where the back transformed line intersects z=0
     767                 :   // and find t.
     768                 :   
     769               0 :   float t = -pback.z / uback.z;
     770                 : 
     771               0 :   gfxPoint result(pback.x + t*uback.x, pback.y + t*uback.y);
     772                 : 
     773                 :   return result;
     774                 : }
     775                 : 
     776               0 : gfxRect gfx3DMatrix::ProjectRectBounds(const gfxRect& aRect) const
     777                 : {
     778               0 :   gfxPoint points[4];
     779                 : 
     780               0 :   points[0] = ProjectPoint(aRect.TopLeft());
     781               0 :   points[1] = ProjectPoint(gfxPoint(aRect.X() + aRect.Width(), aRect.Y()));
     782               0 :   points[2] = ProjectPoint(gfxPoint(aRect.X(), aRect.Y() + aRect.Height()));
     783               0 :   points[3] = ProjectPoint(gfxPoint(aRect.X() + aRect.Width(),
     784               0 :                                     aRect.Y() + aRect.Height()));
     785                 : 
     786                 :   gfxFloat min_x, max_x;
     787                 :   gfxFloat min_y, max_y;
     788                 : 
     789               0 :   min_x = max_x = points[0].x;
     790               0 :   min_y = max_y = points[0].y;
     791                 : 
     792               0 :   for (int i=1; i<4; i++) {
     793               0 :     min_x = min(points[i].x, min_x);
     794               0 :     max_x = max(points[i].x, max_x);
     795               0 :     min_y = min(points[i].y, min_y);
     796               0 :     max_y = max(points[i].y, max_y);
     797                 :   }
     798                 : 
     799               0 :   return gfxRect(min_x, min_y, max_x - min_x, max_y - min_y);
     800                 : }
     801                 : 
     802               0 : gfxPoint3D gfx3DMatrix::GetNormalVector() const
     803                 : {
     804                 :   // Define a plane in transformed space as the transformations
     805                 :   // of 3 points on the z=0 screen plane.
     806               0 :   gfxPoint3D a = Transform3D(gfxPoint3D(0, 0, 0));
     807               0 :   gfxPoint3D b = Transform3D(gfxPoint3D(0, 1, 0));
     808               0 :   gfxPoint3D c = Transform3D(gfxPoint3D(1, 0, 0));
     809                 : 
     810                 :   // Convert to two vectors on the surface of the plane.
     811               0 :   gfxPoint3D ab = b - a;
     812               0 :   gfxPoint3D ac = c - a;
     813                 : 
     814               0 :   return ac.CrossProduct(ab);
     815                 : }
     816                 : 
     817               0 : bool gfx3DMatrix::IsBackfaceVisible() const
     818                 : {
     819                 :   // Inverse()._33 < 0;
     820               0 :   gfxFloat det = Determinant();
     821                 :   float _33 = _12*_24*_41 - _14*_22*_41 +
     822                 :               _14*_21*_42 - _11*_24*_42 -
     823               0 :               _12*_21*_44 + _11*_22*_44;
     824               0 :   return (_33 * det) < 0;
     825                 : }
     826                 : 

Generated by: LCOV version 1.7