gfx/thebes/gfx3DMatrix.cpp

Tue, 06 Jan 2015 21:39:09 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Tue, 06 Jan 2015 21:39:09 +0100
branch
TOR_BUG_9701
changeset 8
97036ab72558
permissions
-rw-r--r--

Conditionally force memory storage according to privacy.thirdparty.isolate;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.

     1 /* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 4 -*-
     2  * This Source Code Form is subject to the terms of the Mozilla Public
     3  * License, v. 2.0. If a copy of the MPL was not distributed with this
     4  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     6 #include "gfxMatrix.h"
     7 #include "gfx3DMatrix.h"
     8 #include "mozilla/gfx/Tools.h"
     9 #include <math.h>
    10 #include <algorithm>
    12 using namespace std;
    13 using namespace mozilla;
    14 using namespace mozilla::gfx;
    16 /* Force small values to zero.  We do this to avoid having sin(360deg)
    17  * evaluate to a tiny but nonzero value.
    18  */
    19 static double FlushToZero(double aVal)
    20 {
    21   if (-FLT_EPSILON < aVal && aVal < FLT_EPSILON)
    22     return 0.0f;
    23   else
    24     return aVal;
    25 }
    27 /* Computes tan(aTheta).  For values of aTheta such that tan(aTheta) is
    28  * undefined or very large, SafeTangent returns a manageably large value
    29  * of the correct sign.
    30  */
    31 static double SafeTangent(double aTheta)
    32 {
    33   const double kEpsilon = 0.0001;
    35   /* tan(theta) = sin(theta)/cos(theta); problems arise when
    36    * cos(theta) is too close to zero.  Limit cos(theta) to the
    37    * range [-1, -epsilon] U [epsilon, 1].
    38    */
    39   double sinTheta = sin(aTheta);
    40   double cosTheta = cos(aTheta);
    42   if (cosTheta >= 0 && cosTheta < kEpsilon)
    43     cosTheta = kEpsilon;
    44   else if (cosTheta < 0 && cosTheta >= -kEpsilon)
    45     cosTheta = -kEpsilon;
    47   return FlushToZero(sinTheta / cosTheta);
    48 }
    50 gfx3DMatrix::gfx3DMatrix(void)
    51 {
    52   _11 = _22 = _33 = _44 = 1.0f;
    53   _12 = _13 = _14 = 0.0f;
    54   _21 = _23 = _24 = 0.0f;
    55   _31 = _32 = _34 = 0.0f;
    56   _41 = _42 = _43 = 0.0f;
    57 }
    59 gfx3DMatrix
    60 gfx3DMatrix::operator*(const gfx3DMatrix &aMatrix) const
    61 {
    62   if (Is2D() && aMatrix.Is2D()) {
    63     return Multiply2D(aMatrix);
    64   }
    66   gfx3DMatrix matrix;
    68   matrix._11 = _11 * aMatrix._11 + _12 * aMatrix._21 + _13 * aMatrix._31 + _14 * aMatrix._41;
    69   matrix._21 = _21 * aMatrix._11 + _22 * aMatrix._21 + _23 * aMatrix._31 + _24 * aMatrix._41;
    70   matrix._31 = _31 * aMatrix._11 + _32 * aMatrix._21 + _33 * aMatrix._31 + _34 * aMatrix._41;
    71   matrix._41 = _41 * aMatrix._11 + _42 * aMatrix._21 + _43 * aMatrix._31 + _44 * aMatrix._41;
    72   matrix._12 = _11 * aMatrix._12 + _12 * aMatrix._22 + _13 * aMatrix._32 + _14 * aMatrix._42;
    73   matrix._22 = _21 * aMatrix._12 + _22 * aMatrix._22 + _23 * aMatrix._32 + _24 * aMatrix._42;
    74   matrix._32 = _31 * aMatrix._12 + _32 * aMatrix._22 + _33 * aMatrix._32 + _34 * aMatrix._42;
    75   matrix._42 = _41 * aMatrix._12 + _42 * aMatrix._22 + _43 * aMatrix._32 + _44 * aMatrix._42;
    76   matrix._13 = _11 * aMatrix._13 + _12 * aMatrix._23 + _13 * aMatrix._33 + _14 * aMatrix._43;
    77   matrix._23 = _21 * aMatrix._13 + _22 * aMatrix._23 + _23 * aMatrix._33 + _24 * aMatrix._43;
    78   matrix._33 = _31 * aMatrix._13 + _32 * aMatrix._23 + _33 * aMatrix._33 + _34 * aMatrix._43;
    79   matrix._43 = _41 * aMatrix._13 + _42 * aMatrix._23 + _43 * aMatrix._33 + _44 * aMatrix._43;
    80   matrix._14 = _11 * aMatrix._14 + _12 * aMatrix._24 + _13 * aMatrix._34 + _14 * aMatrix._44;
    81   matrix._24 = _21 * aMatrix._14 + _22 * aMatrix._24 + _23 * aMatrix._34 + _24 * aMatrix._44;
    82   matrix._34 = _31 * aMatrix._14 + _32 * aMatrix._24 + _33 * aMatrix._34 + _34 * aMatrix._44;
    83   matrix._44 = _41 * aMatrix._14 + _42 * aMatrix._24 + _43 * aMatrix._34 + _44 * aMatrix._44;
    85   return matrix;
    86 }
    88 gfx3DMatrix&
    89 gfx3DMatrix::operator*=(const gfx3DMatrix &aMatrix)
    90 {
    91   return *this = *this * aMatrix;
    92 }
    94 gfx3DMatrix
    95 gfx3DMatrix::Multiply2D(const gfx3DMatrix &aMatrix) const
    96 {
    97   gfx3DMatrix matrix;
    99   matrix._11 = _11 * aMatrix._11 + _12 * aMatrix._21;
   100   matrix._21 = _21 * aMatrix._11 + _22 * aMatrix._21;
   101   matrix._41 = _41 * aMatrix._11 + _42 * aMatrix._21 + aMatrix._41;
   102   matrix._12 = _11 * aMatrix._12 + _12 * aMatrix._22;
   103   matrix._22 = _21 * aMatrix._12 + _22 * aMatrix._22;
   104   matrix._42 = _41 * aMatrix._12 + _42 * aMatrix._22 + aMatrix._42;
   106   return matrix;
   107 }
   109 bool
   110 gfx3DMatrix::operator==(const gfx3DMatrix& o) const
   111 {
   112   // XXX would be nice to memcmp here, but that breaks IEEE 754 semantics
   113   return _11 == o._11 && _12 == o._12 && _13 == o._13 && _14 == o._14 &&
   114          _21 == o._21 && _22 == o._22 && _23 == o._23 && _24 == o._24 &&
   115          _31 == o._31 && _32 == o._32 && _33 == o._33 && _34 == o._34 &&
   116          _41 == o._41 && _42 == o._42 && _43 == o._43 && _44 == o._44;
   117 }
   119 bool
   120 gfx3DMatrix::operator!=(const gfx3DMatrix& o) const
   121 {
   122   return !((*this) == o);
   123 }
   125 bool
   126 gfx3DMatrix::FuzzyEqual(const gfx3DMatrix& o) const
   127 {
   128   static const float error = 1e-4;
   129   return gfx::FuzzyEqual(_11, o._11, error) && gfx::FuzzyEqual(_12, o._12, error) && 
   130          gfx::FuzzyEqual(_13, o._13, error) && gfx::FuzzyEqual(_14, o._14, error) &&
   131          gfx::FuzzyEqual(_21, o._21, error) && gfx::FuzzyEqual(_22, o._22, error) && 
   132          gfx::FuzzyEqual(_23, o._23, error) && gfx::FuzzyEqual(_24, o._24, error) &&
   133          gfx::FuzzyEqual(_31, o._31, error) && gfx::FuzzyEqual(_32, o._32, error) && 
   134          gfx::FuzzyEqual(_33, o._33, error) && gfx::FuzzyEqual(_34, o._34, error) &&
   135          gfx::FuzzyEqual(_41, o._41, error) && gfx::FuzzyEqual(_42, o._42, error) && 
   136          gfx::FuzzyEqual(_43, o._43, error) && gfx::FuzzyEqual(_44, o._44, error);
   137 }
   139 gfx3DMatrix&
   140 gfx3DMatrix::operator/=(const gfxFloat scalar)
   141 {
   142   _11 /= scalar;
   143   _12 /= scalar;
   144   _13 /= scalar;
   145   _14 /= scalar;
   146   _21 /= scalar;
   147   _22 /= scalar;
   148   _23 /= scalar;
   149   _24 /= scalar;
   150   _31 /= scalar;
   151   _32 /= scalar;
   152   _33 /= scalar;
   153   _34 /= scalar;
   154   _41 /= scalar;
   155   _42 /= scalar;
   156   _43 /= scalar;
   157   _44 /= scalar;
   158   return *this;
   159 }
   161 gfx3DMatrix
   162 gfx3DMatrix::From2D(const gfxMatrix &aMatrix)
   163 {
   164   gfx3DMatrix matrix;
   165   matrix._11 = (float)aMatrix.xx;
   166   matrix._12 = (float)aMatrix.yx;
   167   matrix._21 = (float)aMatrix.xy;
   168   matrix._22 = (float)aMatrix.yy;
   169   matrix._41 = (float)aMatrix.x0;
   170   matrix._42 = (float)aMatrix.y0;
   171   return matrix;
   172 }
   174 bool
   175 gfx3DMatrix::IsIdentity() const
   176 {
   177   return _11 == 1.0f && _12 == 0.0f && _13 == 0.0f && _14 == 0.0f &&
   178          _21 == 0.0f && _22 == 1.0f && _23 == 0.0f && _24 == 0.0f &&
   179          _31 == 0.0f && _32 == 0.0f && _33 == 1.0f && _34 == 0.0f &&
   180          _41 == 0.0f && _42 == 0.0f && _43 == 0.0f && _44 == 1.0f;
   181 }
   183 void
   184 gfx3DMatrix::Translate(const gfxPoint3D& aPoint)
   185 {
   186     _41 += aPoint.x * _11 + aPoint.y * _21 + aPoint.z * _31;
   187     _42 += aPoint.x * _12 + aPoint.y * _22 + aPoint.z * _32;
   188     _43 += aPoint.x * _13 + aPoint.y * _23 + aPoint.z * _33;
   189     _44 += aPoint.x * _14 + aPoint.y * _24 + aPoint.z * _34;
   190 }
   192 void
   193 gfx3DMatrix::TranslatePost(const gfxPoint3D& aPoint)
   194 {
   195     _11 += _14 * aPoint.x;
   196     _21 += _24 * aPoint.x;
   197     _31 += _34 * aPoint.x;
   198     _41 += _44 * aPoint.x;
   199     _12 += _14 * aPoint.y;
   200     _22 += _24 * aPoint.y;
   201     _32 += _34 * aPoint.y;
   202     _42 += _44 * aPoint.y;
   203     _13 += _14 * aPoint.z;
   204     _23 += _24 * aPoint.z;
   205     _33 += _34 * aPoint.z;
   206     _43 += _44 * aPoint.z;
   207 }
   209 void
   210 gfx3DMatrix::ScalePost(float aX, float aY, float aZ)
   211 {
   212   _11 *= aX;
   213   _21 *= aX;
   214   _31 *= aX;
   215   _41 *= aX;
   217   _12 *= aY;
   218   _22 *= aY;
   219   _32 *= aY;
   220   _42 *= aY;
   222   _13 *= aZ;
   223   _23 *= aZ;
   224   _33 *= aZ;
   225   _43 *= aZ;
   226 }
   228 void
   229 gfx3DMatrix::SkewXY(double aSkew)
   230 {
   231     (*this)[1] += (*this)[0] * aSkew;
   232 }
   234 void 
   235 gfx3DMatrix::SkewXZ(double aSkew)
   236 {
   237     (*this)[2] += (*this)[0] * aSkew;
   238 }
   240 void
   241 gfx3DMatrix::SkewYZ(double aSkew)
   242 {
   243     (*this)[2] += (*this)[1] * aSkew;
   244 }
   246 void
   247 gfx3DMatrix::Scale(float aX, float aY, float aZ)
   248 {
   249     (*this)[0] *= aX;
   250     (*this)[1] *= aY;
   251     (*this)[2] *= aZ;
   252 }
   254 void
   255 gfx3DMatrix::Perspective(float aDepth)
   256 {
   257   NS_ASSERTION(aDepth > 0.0f, "Perspective must be positive!");
   258   _31 += -1.0/aDepth * _41;
   259   _32 += -1.0/aDepth * _42;
   260   _33 += -1.0/aDepth * _43;
   261   _34 += -1.0/aDepth * _44;
   262 }
   264 void gfx3DMatrix::SkewXY(double aXSkew, double aYSkew)
   265 {
   266   float tanX = SafeTangent(aXSkew);
   267   float tanY = SafeTangent(aYSkew);
   268   float temp;
   270   temp = _11;
   271   _11 += tanY * _21;
   272   _21 += tanX * temp;
   274   temp = _12;
   275   _12 += tanY * _22;
   276   _22 += tanX * temp;
   278   temp = _13;
   279   _13 += tanY * _23;
   280   _23 += tanX * temp;
   282   temp = _14;
   283   _14 += tanY * _24;
   284   _24 += tanX * temp;
   285 }
   287 void
   288 gfx3DMatrix::RotateX(double aTheta)
   289 {
   290   double cosTheta = FlushToZero(cos(aTheta));
   291   double sinTheta = FlushToZero(sin(aTheta));
   293   float temp;
   295   temp = _21;
   296   _21 = cosTheta * _21 + sinTheta * _31;
   297   _31 = -sinTheta * temp + cosTheta * _31;
   299   temp = _22;
   300   _22 = cosTheta * _22 + sinTheta * _32;
   301   _32 = -sinTheta * temp + cosTheta * _32;
   303   temp = _23;
   304   _23 = cosTheta * _23 + sinTheta * _33;
   305   _33 = -sinTheta * temp + cosTheta * _33;
   307   temp = _24;
   308   _24 = cosTheta * _24 + sinTheta * _34;
   309   _34 = -sinTheta * temp + cosTheta * _34;
   310 }
   312 void
   313 gfx3DMatrix::RotateY(double aTheta)
   314 {
   315   double cosTheta = FlushToZero(cos(aTheta));
   316   double sinTheta = FlushToZero(sin(aTheta));
   318   float temp;
   320   temp = _11;
   321   _11 = cosTheta * _11 + -sinTheta * _31;
   322   _31 = sinTheta * temp + cosTheta * _31;
   324   temp = _12;
   325   _12 = cosTheta * _12 + -sinTheta * _32;
   326   _32 = sinTheta * temp + cosTheta * _32;
   328   temp = _13;
   329   _13 = cosTheta * _13 + -sinTheta * _33;
   330   _33 = sinTheta * temp + cosTheta * _33;
   332   temp = _14;
   333   _14 = cosTheta * _14 + -sinTheta * _34;
   334   _34 = sinTheta * temp + cosTheta * _34;
   335 }
   337 void
   338 gfx3DMatrix::RotateZ(double aTheta)
   339 {
   340   double cosTheta = FlushToZero(cos(aTheta));
   341   double sinTheta = FlushToZero(sin(aTheta));
   343   float temp;
   345   temp = _11;
   346   _11 = cosTheta * _11 + sinTheta * _21;
   347   _21 = -sinTheta * temp + cosTheta * _21;
   349   temp = _12;
   350   _12 = cosTheta * _12 + sinTheta * _22;
   351   _22 = -sinTheta * temp + cosTheta * _22;
   353   temp = _13;
   354   _13 = cosTheta * _13 + sinTheta * _23;
   355   _23 = -sinTheta * temp + cosTheta * _23;
   357   temp = _14;
   358   _14 = cosTheta * _14 + sinTheta * _24;
   359   _24 = -sinTheta * temp + cosTheta * _24;
   360 }
   362 void 
   363 gfx3DMatrix::PreMultiply(const gfx3DMatrix& aOther)
   364 {
   365   *this = aOther * (*this);
   366 }
   368 void
   369 gfx3DMatrix::PreMultiply(const gfxMatrix& aOther)
   370 {
   371   gfx3DMatrix temp;
   372   temp._11 = aOther.xx * _11 + aOther.yx * _21;
   373   temp._21 = aOther.xy * _11 + aOther.yy * _21;
   374   temp._31 = _31;
   375   temp._41 = aOther.x0 * _11 + aOther.y0 * _21 + _41;
   376   temp._12 = aOther.xx * _12 + aOther.yx * _22;
   377   temp._22 = aOther.xy * _12 + aOther.yy * _22;
   378   temp._32 = _32;
   379   temp._42 = aOther.x0 * _12 + aOther.y0 * _22 + _42;
   380   temp._13 = aOther.xx * _13 + aOther.yx * _23;
   381   temp._23 = aOther.xy * _13 + aOther.yy * _23;
   382   temp._33 = _33;
   383   temp._43 = aOther.x0 * _13 + aOther.y0 * _23 + _43;
   384   temp._14 = aOther.xx * _14 + aOther.yx * _24;
   385   temp._24 = aOther.xy * _14 + aOther.yy * _24;
   386   temp._34 = _34;
   387   temp._44 = aOther.x0 * _14 + aOther.y0 * _24 + _44;
   389   *this = temp;
   390 }
   392 gfx3DMatrix
   393 gfx3DMatrix::Translation(float aX, float aY, float aZ)
   394 {
   395   gfx3DMatrix matrix;
   397   matrix._41 = aX;
   398   matrix._42 = aY;
   399   matrix._43 = aZ;
   400   return matrix;
   401 }
   403 gfx3DMatrix
   404 gfx3DMatrix::Translation(const gfxPoint3D& aPoint)
   405 {
   406   gfx3DMatrix matrix;
   408   matrix._41 = aPoint.x;
   409   matrix._42 = aPoint.y;
   410   matrix._43 = aPoint.z;
   411   return matrix;
   412 }
   414 gfx3DMatrix
   415 gfx3DMatrix::ScalingMatrix(float aFactor)
   416 {
   417   gfx3DMatrix matrix;
   419   matrix._11 = matrix._22 = matrix._33 = aFactor;
   420   return matrix;
   421 }
   423 gfx3DMatrix
   424 gfx3DMatrix::ScalingMatrix(float aX, float aY, float aZ)
   425 {
   426   gfx3DMatrix matrix;
   428   matrix._11 = aX;
   429   matrix._22 = aY;
   430   matrix._33 = aZ;
   432   return matrix;
   433 }
   435 gfxFloat
   436 gfx3DMatrix::Determinant() const
   437 {
   438   return _14 * _23 * _32 * _41
   439        - _13 * _24 * _32 * _41
   440        - _14 * _22 * _33 * _41
   441        + _12 * _24 * _33 * _41
   442        + _13 * _22 * _34 * _41
   443        - _12 * _23 * _34 * _41
   444        - _14 * _23 * _31 * _42
   445        + _13 * _24 * _31 * _42
   446        + _14 * _21 * _33 * _42
   447        - _11 * _24 * _33 * _42
   448        - _13 * _21 * _34 * _42
   449        + _11 * _23 * _34 * _42
   450        + _14 * _22 * _31 * _43
   451        - _12 * _24 * _31 * _43
   452        - _14 * _21 * _32 * _43
   453        + _11 * _24 * _32 * _43
   454        + _12 * _21 * _34 * _43
   455        - _11 * _22 * _34 * _43
   456        - _13 * _22 * _31 * _44
   457        + _12 * _23 * _31 * _44
   458        + _13 * _21 * _32 * _44
   459        - _11 * _23 * _32 * _44
   460        - _12 * _21 * _33 * _44
   461        + _11 * _22 * _33 * _44;
   462 }
   464 gfxFloat
   465 gfx3DMatrix::Determinant3x3() const
   466 {
   467     return _11 * (_22 * _33 - _23 * _32) +
   468            _12 * (_23 * _31 - _33 * _21) +
   469            _13 * (_21 * _32 - _22 * _31);
   470 }
   472 gfx3DMatrix
   473 gfx3DMatrix::Inverse3x3() const
   474 {
   475     gfxFloat det = Determinant3x3();
   476     if (det == 0.0) {
   477         return *this;
   478     }
   480     gfxFloat detInv = 1/det;
   481     gfx3DMatrix temp;
   483     temp._11 = (_22 * _33 - _23 * _32) * detInv;
   484     temp._12 = (_13 * _32 - _12 * _33) * detInv;
   485     temp._13 = (_12 * _23 - _13 * _22) * detInv;
   486     temp._21 = (_23 * _31 - _33 * _21) * detInv;
   487     temp._22 = (_11 * _33 - _13 * _31) * detInv;
   488     temp._23 = (_13 * _21 - _11 * _23) * detInv;
   489     temp._31 = (_21 * _32 - _22 * _31) * detInv;
   490     temp._32 = (_31 * _12 - _11 * _32) * detInv;
   491     temp._33 = (_11 * _22 - _12 * _21) * detInv;
   492     return temp;
   493 }
   495 bool
   496 gfx3DMatrix::IsSingular() const
   497 {
   498   return Determinant() == 0.0;
   499 }
   501 gfx3DMatrix
   502 gfx3DMatrix::Inverse() const
   503 {
   504   if (TransposedVector(3) == gfxPointH3D(0, 0, 0, 1)) {
   505     /** 
   506      * When the matrix contains no perspective, the inverse
   507      * is the same as the 3x3 inverse of the rotation components
   508      * multiplied by the inverse of the translation components.
   509      * Doing these steps separately is faster and more numerically
   510      * stable.
   511      *
   512      * Inverse of the translation matrix is just negating
   513      * the values.
   514      */
   515     gfx3DMatrix matrix3 = Inverse3x3();
   516     matrix3.Translate(gfxPoint3D(-_41, -_42, -_43));
   517     return matrix3;
   518  }
   520   gfxFloat det = Determinant();
   521   if (det == 0.0) {
   522     return *this;
   523   }
   525   gfx3DMatrix temp;
   527   temp._11 = _23*_34*_42 - _24*_33*_42 + 
   528              _24*_32*_43 - _22*_34*_43 - 
   529              _23*_32*_44 + _22*_33*_44;
   530   temp._12 = _14*_33*_42 - _13*_34*_42 -
   531              _14*_32*_43 + _12*_34*_43 +
   532              _13*_32*_44 - _12*_33*_44;
   533   temp._13 = _13*_24*_42 - _14*_23*_42 +
   534              _14*_22*_43 - _12*_24*_43 -
   535              _13*_22*_44 + _12*_23*_44;
   536   temp._14 = _14*_23*_32 - _13*_24*_32 -
   537              _14*_22*_33 + _12*_24*_33 +
   538              _13*_22*_34 - _12*_23*_34;
   539   temp._21 = _24*_33*_41 - _23*_34*_41 -
   540              _24*_31*_43 + _21*_34*_43 +
   541              _23*_31*_44 - _21*_33*_44;
   542   temp._22 = _13*_34*_41 - _14*_33*_41 +
   543              _14*_31*_43 - _11*_34*_43 -
   544              _13*_31*_44 + _11*_33*_44;
   545   temp._23 = _14*_23*_41 - _13*_24*_41 -
   546              _14*_21*_43 + _11*_24*_43 +
   547              _13*_21*_44 - _11*_23*_44;
   548   temp._24 = _13*_24*_31 - _14*_23*_31 +
   549              _14*_21*_33 - _11*_24*_33 -
   550              _13*_21*_34 + _11*_23*_34;
   551   temp._31 = _22*_34*_41 - _24*_32*_41 +
   552              _24*_31*_42 - _21*_34*_42 -
   553              _22*_31*_44 + _21*_32*_44;
   554   temp._32 = _14*_32*_41 - _12*_34*_41 -
   555              _14*_31*_42 + _11*_34*_42 +
   556              _12*_31*_44 - _11*_32*_44;
   557   temp._33 = _12*_24*_41 - _14*_22*_41 +
   558              _14*_21*_42 - _11*_24*_42 -
   559              _12*_21*_44 + _11*_22*_44;
   560   temp._34 = _14*_22*_31 - _12*_24*_31 -
   561              _14*_21*_32 + _11*_24*_32 +
   562              _12*_21*_34 - _11*_22*_34;
   563   temp._41 = _23*_32*_41 - _22*_33*_41 -
   564              _23*_31*_42 + _21*_33*_42 +
   565              _22*_31*_43 - _21*_32*_43;
   566   temp._42 = _12*_33*_41 - _13*_32*_41 +
   567              _13*_31*_42 - _11*_33*_42 -
   568              _12*_31*_43 + _11*_32*_43;
   569   temp._43 = _13*_22*_41 - _12*_23*_41 -
   570              _13*_21*_42 + _11*_23*_42 +
   571              _12*_21*_43 - _11*_22*_43;
   572   temp._44 = _12*_23*_31 - _13*_22*_31 +
   573              _13*_21*_32 - _11*_23*_32 -
   574              _12*_21*_33 + _11*_22*_33;
   576   temp /= det;
   577   return temp;
   578 }
   580 gfx3DMatrix&
   581 gfx3DMatrix::Normalize()
   582 {
   583     for (int i = 0; i < 4; i++) {
   584         for (int j = 0; j < 4; j++) {
   585             (*this)[i][j] /= (*this)[3][3];
   586        }
   587     }
   588     return *this;
   589 }
   591 gfx3DMatrix&
   592 gfx3DMatrix::Transpose()
   593 {
   594     *this = Transposed();
   595     return *this;
   596 }
   598 gfx3DMatrix
   599 gfx3DMatrix::Transposed() const
   600 {
   601     gfx3DMatrix temp;
   602     for (int i = 0; i < 4; i++) {
   603         temp[i] = TransposedVector(i);
   604     }
   605     return temp;
   606 }
   608 gfxPoint
   609 gfx3DMatrix::Transform(const gfxPoint& point) const
   610 {
   611   gfxPoint3D vec3d(point.x, point.y, 0);
   612   vec3d = Transform3D(vec3d);
   613   return gfxPoint(vec3d.x, vec3d.y);
   614 }
   616 gfxPoint3D
   617 gfx3DMatrix::Transform3D(const gfxPoint3D& point) const
   618 {
   619   gfxFloat x = point.x * _11 + point.y * _21 + point.z * _31 + _41;
   620   gfxFloat y = point.x * _12 + point.y * _22 + point.z * _32 + _42;
   621   gfxFloat z = point.x * _13 + point.y * _23 + point.z * _33 + _43;
   622   gfxFloat w = point.x * _14 + point.y * _24 + point.z * _34 + _44;
   624   x /= w;
   625   y /= w;
   626   z /= w;
   628   return gfxPoint3D(x, y, z);
   629 }
   631 gfxPointH3D
   632 gfx3DMatrix::Transform4D(const gfxPointH3D& aPoint) const
   633 {
   634     gfxFloat x = aPoint.x * _11 + aPoint.y * _21 + aPoint.z * _31 + aPoint.w * _41;
   635     gfxFloat y = aPoint.x * _12 + aPoint.y * _22 + aPoint.z * _32 + aPoint.w * _42;
   636     gfxFloat z = aPoint.x * _13 + aPoint.y * _23 + aPoint.z * _33 + aPoint.w * _43;
   637     gfxFloat w = aPoint.x * _14 + aPoint.y * _24 + aPoint.z * _34 + aPoint.w * _44;
   639     return gfxPointH3D(x, y, z, w);
   640 }
   642 gfxPointH3D
   643 gfx3DMatrix::TransposeTransform4D(const gfxPointH3D& aPoint) const
   644 {
   645     gfxFloat x = aPoint.x * _11 + aPoint.y * _12 + aPoint.z * _13 + aPoint.w * _14;
   646     gfxFloat y = aPoint.x * _21 + aPoint.y * _22 + aPoint.z * _23 + aPoint.w * _24;
   647     gfxFloat z = aPoint.x * _31 + aPoint.y * _32 + aPoint.z * _33 + aPoint.w * _34;
   648     gfxFloat w = aPoint.x * _41 + aPoint.y * _42 + aPoint.z * _43 + aPoint.w * _44;
   650     return gfxPointH3D(x, y, z, w);
   651 }
   653 gfxRect
   654 gfx3DMatrix::TransformBounds(const gfxRect& rect) const
   655 {
   656   gfxPoint points[4];
   658   points[0] = Transform(rect.TopLeft());
   659   points[1] = Transform(gfxPoint(rect.X() + rect.Width(), rect.Y()));
   660   points[2] = Transform(gfxPoint(rect.X(), rect.Y() + rect.Height()));
   661   points[3] = Transform(gfxPoint(rect.X() + rect.Width(),
   662                                  rect.Y() + rect.Height()));
   664   gfxFloat min_x, max_x;
   665   gfxFloat min_y, max_y;
   667   min_x = max_x = points[0].x;
   668   min_y = max_y = points[0].y;
   670   for (int i=1; i<4; i++) {
   671     min_x = min(points[i].x, min_x);
   672     max_x = max(points[i].x, max_x);
   673     min_y = min(points[i].y, min_y);
   674     max_y = max(points[i].y, max_y);
   675   }
   677   return gfxRect(min_x, min_y, max_x - min_x, max_y - min_y);
   678 }
   680 gfxQuad 
   681 gfx3DMatrix::TransformRect(const gfxRect& aRect) const
   682 {
   683   gfxPoint points[4];
   685   points[0] = Transform(aRect.TopLeft());
   686   points[1] = Transform(gfxPoint(aRect.X() + aRect.Width(), aRect.Y()));
   687   points[2] = Transform(gfxPoint(aRect.X() + aRect.Width(),
   688                                  aRect.Y() + aRect.Height()));
   689   points[3] = Transform(gfxPoint(aRect.X(), aRect.Y() + aRect.Height()));
   691   // Could this ever result in lines that intersect? I don't think so.
   692   return gfxQuad(points[0], points[1], points[2], points[3]);
   693 }
   695 bool
   696 gfx3DMatrix::Is2D() const
   697 {
   698   if (_13 != 0.0f || _14 != 0.0f ||
   699       _23 != 0.0f || _24 != 0.0f ||
   700       _31 != 0.0f || _32 != 0.0f || _33 != 1.0f || _34 != 0.0f ||
   701       _43 != 0.0f || _44 != 1.0f) {
   702     return false;
   703   }
   704   return true;
   705 }
   707 bool
   708 gfx3DMatrix::Is2D(gfxMatrix* aMatrix) const
   709 {
   710   if (!Is2D()) {
   711     return false;
   712   }
   713   if (aMatrix) {
   714     aMatrix->xx = _11;
   715     aMatrix->yx = _12;
   716     aMatrix->xy = _21;
   717     aMatrix->yy = _22;
   718     aMatrix->x0 = _41;
   719     aMatrix->y0 = _42;
   720   }
   721   return true;
   722 }
   724 bool
   725 gfx3DMatrix::CanDraw2D(gfxMatrix* aMatrix) const
   726 {
   727   if (_14 != 0.0f ||
   728       _24 != 0.0f ||
   729       _44 != 1.0f) {
   730     return false;
   731   }
   732   if (aMatrix) {
   733     aMatrix->xx = _11;
   734     aMatrix->yx = _12;
   735     aMatrix->xy = _21;
   736     aMatrix->yy = _22;
   737     aMatrix->x0 = _41;
   738     aMatrix->y0 = _42;
   739   }
   740   return true;
   741 }
   743 gfx3DMatrix&
   744 gfx3DMatrix::ProjectTo2D()
   745 {
   746   _31 = 0.0f;
   747   _32 = 0.0f;
   748   _13 = 0.0f; 
   749   _23 = 0.0f; 
   750   _33 = 1.0f; 
   751   _43 = 0.0f; 
   752   _34 = 0.0f;
   753   return *this;
   754 }
   756 gfxPoint gfx3DMatrix::ProjectPoint(const gfxPoint& aPoint) const
   757 {
   758   // Define a ray of the form P + Ut where t is a real number
   759   // w is assumed to always be 1 when transforming 3d points with our
   760   // 4x4 matrix.
   761   // p is our click point, q is another point on the same ray.
   762   // 
   763   // Note: since the transformation is a general projective transformation and is not
   764   // necessarily affine, we can't just take a unit vector u, back-transform it, and use
   765   // it as unit vector on the back-transformed ray. Instead, we really must take two points
   766   // on the ray and back-transform them.
   767   gfxPoint3D p(aPoint.x, aPoint.y, 0);
   768   gfxPoint3D q(aPoint.x, aPoint.y, 1);
   770   // Back transform the vectors (using w = 1) and normalize
   771   // back into 3d vectors by dividing by the w component.
   772   gfxPoint3D pback = Transform3D(p);
   773   gfxPoint3D qback = Transform3D(q);
   774   gfxPoint3D uback = qback - pback;
   776   // Find the point where the back transformed line intersects z=0
   777   // and find t.
   779   float t = -pback.z / uback.z;
   781   gfxPoint result(pback.x + t*uback.x, pback.y + t*uback.y);
   783   return result;
   784 }
   786 gfxRect gfx3DMatrix::ProjectRectBounds(const gfxRect& aRect) const
   787 {
   788   gfxPoint points[4];
   790   points[0] = ProjectPoint(aRect.TopLeft());
   791   points[1] = ProjectPoint(aRect.TopRight());
   792   points[2] = ProjectPoint(aRect.BottomLeft());
   793   points[3] = ProjectPoint(aRect.BottomRight());
   795   gfxFloat min_x, max_x;
   796   gfxFloat min_y, max_y;
   798   min_x = max_x = points[0].x;
   799   min_y = max_y = points[0].y;
   801   for (int i=1; i<4; i++) {
   802     min_x = min(points[i].x, min_x);
   803     max_x = max(points[i].x, max_x);
   804     min_y = min(points[i].y, min_y);
   805     max_y = max(points[i].y, max_y);
   806   }
   808   return gfxRect(min_x, min_y, max_x - min_x, max_y - min_y);
   809 }
   811 gfxRect gfx3DMatrix::UntransformBounds(const gfxRect& aRect, const gfxRect& aChildBounds) const
   812 {
   813   if (Is2D()) {
   814     return Inverse().TransformBounds(aRect);
   815   }
   816   gfxRect bounds = TransformBounds(aChildBounds);
   818   gfxRect rect = aRect.Intersect(bounds);
   820   return Inverse().ProjectRectBounds(rect);
   821 }
   823 bool gfx3DMatrix::UntransformPoint(const gfxPoint& aPoint, const gfxRect& aChildBounds, gfxPoint* aOut) const
   824 {
   825   if (Is2D()) {
   826     *aOut = Inverse().Transform(aPoint);
   827     return true;
   828   }
   829   gfxRect bounds = TransformBounds(aChildBounds);
   831   if (!bounds.Contains(aPoint)) {
   832     return false;
   833   }
   835   *aOut = Inverse().ProjectPoint(aPoint);
   836   return true;
   837 }
   839 gfxPoint3D gfx3DMatrix::GetNormalVector() const
   840 {
   841   // Define a plane in transformed space as the transformations
   842   // of 3 points on the z=0 screen plane.
   843   gfxPoint3D a = Transform3D(gfxPoint3D(0, 0, 0));
   844   gfxPoint3D b = Transform3D(gfxPoint3D(0, 1, 0));
   845   gfxPoint3D c = Transform3D(gfxPoint3D(1, 0, 0));
   847   // Convert to two vectors on the surface of the plane.
   848   gfxPoint3D ab = b - a;
   849   gfxPoint3D ac = c - a;
   851   return ac.CrossProduct(ab);
   852 }
   854 bool gfx3DMatrix::IsBackfaceVisible() const
   855 {
   856   // Inverse()._33 < 0;
   857   gfxFloat det = Determinant();
   858   float _33 = _12*_24*_41 - _14*_22*_41 +
   859               _14*_21*_42 - _11*_24*_42 -
   860               _12*_21*_44 + _11*_22*_44;
   861   return (_33 * det) < 0;
   862 }
   864 void gfx3DMatrix::NudgeToIntegers(void)
   865 {
   866   NudgeToInteger(&_11);
   867   NudgeToInteger(&_12);
   868   NudgeToInteger(&_13);
   869   NudgeToInteger(&_14);
   870   NudgeToInteger(&_21);
   871   NudgeToInteger(&_22);
   872   NudgeToInteger(&_23);
   873   NudgeToInteger(&_24);
   874   NudgeToInteger(&_31);
   875   NudgeToInteger(&_32);
   876   NudgeToInteger(&_33);
   877   NudgeToInteger(&_34);
   878   NudgeToInteger(&_41);
   879   NudgeToInteger(&_42);
   880   NudgeToInteger(&_43);
   881   NudgeToInteger(&_44);
   882 }

mercurial