gfx/thebes/gfx3DMatrix.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/gfx/thebes/gfx3DMatrix.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,882 @@
     1.4 +/* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 4 -*-
     1.5 + * This Source Code Form is subject to the terms of the Mozilla Public
     1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.8 +
     1.9 +#include "gfxMatrix.h"
    1.10 +#include "gfx3DMatrix.h"
    1.11 +#include "mozilla/gfx/Tools.h"
    1.12 +#include <math.h>
    1.13 +#include <algorithm>
    1.14 +
    1.15 +using namespace std;
    1.16 +using namespace mozilla;
    1.17 +using namespace mozilla::gfx;
    1.18 +
    1.19 +/* Force small values to zero.  We do this to avoid having sin(360deg)
    1.20 + * evaluate to a tiny but nonzero value.
    1.21 + */
    1.22 +static double FlushToZero(double aVal)
    1.23 +{
    1.24 +  if (-FLT_EPSILON < aVal && aVal < FLT_EPSILON)
    1.25 +    return 0.0f;
    1.26 +  else
    1.27 +    return aVal;
    1.28 +}
    1.29 +
    1.30 +/* Computes tan(aTheta).  For values of aTheta such that tan(aTheta) is
    1.31 + * undefined or very large, SafeTangent returns a manageably large value
    1.32 + * of the correct sign.
    1.33 + */
    1.34 +static double SafeTangent(double aTheta)
    1.35 +{
    1.36 +  const double kEpsilon = 0.0001;
    1.37 +
    1.38 +  /* tan(theta) = sin(theta)/cos(theta); problems arise when
    1.39 +   * cos(theta) is too close to zero.  Limit cos(theta) to the
    1.40 +   * range [-1, -epsilon] U [epsilon, 1].
    1.41 +   */
    1.42 +  double sinTheta = sin(aTheta);
    1.43 +  double cosTheta = cos(aTheta);
    1.44 +
    1.45 +  if (cosTheta >= 0 && cosTheta < kEpsilon)
    1.46 +    cosTheta = kEpsilon;
    1.47 +  else if (cosTheta < 0 && cosTheta >= -kEpsilon)
    1.48 +    cosTheta = -kEpsilon;
    1.49 +
    1.50 +  return FlushToZero(sinTheta / cosTheta);
    1.51 +}
    1.52 +
    1.53 +gfx3DMatrix::gfx3DMatrix(void)
    1.54 +{
    1.55 +  _11 = _22 = _33 = _44 = 1.0f;
    1.56 +  _12 = _13 = _14 = 0.0f;
    1.57 +  _21 = _23 = _24 = 0.0f;
    1.58 +  _31 = _32 = _34 = 0.0f;
    1.59 +  _41 = _42 = _43 = 0.0f;
    1.60 +}
    1.61 +
    1.62 +gfx3DMatrix
    1.63 +gfx3DMatrix::operator*(const gfx3DMatrix &aMatrix) const
    1.64 +{
    1.65 +  if (Is2D() && aMatrix.Is2D()) {
    1.66 +    return Multiply2D(aMatrix);
    1.67 +  }
    1.68 +
    1.69 +  gfx3DMatrix matrix;
    1.70 +
    1.71 +  matrix._11 = _11 * aMatrix._11 + _12 * aMatrix._21 + _13 * aMatrix._31 + _14 * aMatrix._41;
    1.72 +  matrix._21 = _21 * aMatrix._11 + _22 * aMatrix._21 + _23 * aMatrix._31 + _24 * aMatrix._41;
    1.73 +  matrix._31 = _31 * aMatrix._11 + _32 * aMatrix._21 + _33 * aMatrix._31 + _34 * aMatrix._41;
    1.74 +  matrix._41 = _41 * aMatrix._11 + _42 * aMatrix._21 + _43 * aMatrix._31 + _44 * aMatrix._41;
    1.75 +  matrix._12 = _11 * aMatrix._12 + _12 * aMatrix._22 + _13 * aMatrix._32 + _14 * aMatrix._42;
    1.76 +  matrix._22 = _21 * aMatrix._12 + _22 * aMatrix._22 + _23 * aMatrix._32 + _24 * aMatrix._42;
    1.77 +  matrix._32 = _31 * aMatrix._12 + _32 * aMatrix._22 + _33 * aMatrix._32 + _34 * aMatrix._42;
    1.78 +  matrix._42 = _41 * aMatrix._12 + _42 * aMatrix._22 + _43 * aMatrix._32 + _44 * aMatrix._42;
    1.79 +  matrix._13 = _11 * aMatrix._13 + _12 * aMatrix._23 + _13 * aMatrix._33 + _14 * aMatrix._43;
    1.80 +  matrix._23 = _21 * aMatrix._13 + _22 * aMatrix._23 + _23 * aMatrix._33 + _24 * aMatrix._43;
    1.81 +  matrix._33 = _31 * aMatrix._13 + _32 * aMatrix._23 + _33 * aMatrix._33 + _34 * aMatrix._43;
    1.82 +  matrix._43 = _41 * aMatrix._13 + _42 * aMatrix._23 + _43 * aMatrix._33 + _44 * aMatrix._43;
    1.83 +  matrix._14 = _11 * aMatrix._14 + _12 * aMatrix._24 + _13 * aMatrix._34 + _14 * aMatrix._44;
    1.84 +  matrix._24 = _21 * aMatrix._14 + _22 * aMatrix._24 + _23 * aMatrix._34 + _24 * aMatrix._44;
    1.85 +  matrix._34 = _31 * aMatrix._14 + _32 * aMatrix._24 + _33 * aMatrix._34 + _34 * aMatrix._44;
    1.86 +  matrix._44 = _41 * aMatrix._14 + _42 * aMatrix._24 + _43 * aMatrix._34 + _44 * aMatrix._44;
    1.87 +
    1.88 +  return matrix;
    1.89 +}
    1.90 +
    1.91 +gfx3DMatrix&
    1.92 +gfx3DMatrix::operator*=(const gfx3DMatrix &aMatrix)
    1.93 +{
    1.94 +  return *this = *this * aMatrix;
    1.95 +}
    1.96 +
    1.97 +gfx3DMatrix
    1.98 +gfx3DMatrix::Multiply2D(const gfx3DMatrix &aMatrix) const
    1.99 +{
   1.100 +  gfx3DMatrix matrix;
   1.101 +
   1.102 +  matrix._11 = _11 * aMatrix._11 + _12 * aMatrix._21;
   1.103 +  matrix._21 = _21 * aMatrix._11 + _22 * aMatrix._21;
   1.104 +  matrix._41 = _41 * aMatrix._11 + _42 * aMatrix._21 + aMatrix._41;
   1.105 +  matrix._12 = _11 * aMatrix._12 + _12 * aMatrix._22;
   1.106 +  matrix._22 = _21 * aMatrix._12 + _22 * aMatrix._22;
   1.107 +  matrix._42 = _41 * aMatrix._12 + _42 * aMatrix._22 + aMatrix._42;
   1.108 +
   1.109 +  return matrix;
   1.110 +}
   1.111 +
   1.112 +bool
   1.113 +gfx3DMatrix::operator==(const gfx3DMatrix& o) const
   1.114 +{
   1.115 +  // XXX would be nice to memcmp here, but that breaks IEEE 754 semantics
   1.116 +  return _11 == o._11 && _12 == o._12 && _13 == o._13 && _14 == o._14 &&
   1.117 +         _21 == o._21 && _22 == o._22 && _23 == o._23 && _24 == o._24 &&
   1.118 +         _31 == o._31 && _32 == o._32 && _33 == o._33 && _34 == o._34 &&
   1.119 +         _41 == o._41 && _42 == o._42 && _43 == o._43 && _44 == o._44;
   1.120 +}
   1.121 +
   1.122 +bool
   1.123 +gfx3DMatrix::operator!=(const gfx3DMatrix& o) const
   1.124 +{
   1.125 +  return !((*this) == o);
   1.126 +}
   1.127 +
   1.128 +bool
   1.129 +gfx3DMatrix::FuzzyEqual(const gfx3DMatrix& o) const
   1.130 +{
   1.131 +  static const float error = 1e-4;
   1.132 +  return gfx::FuzzyEqual(_11, o._11, error) && gfx::FuzzyEqual(_12, o._12, error) && 
   1.133 +         gfx::FuzzyEqual(_13, o._13, error) && gfx::FuzzyEqual(_14, o._14, error) &&
   1.134 +         gfx::FuzzyEqual(_21, o._21, error) && gfx::FuzzyEqual(_22, o._22, error) && 
   1.135 +         gfx::FuzzyEqual(_23, o._23, error) && gfx::FuzzyEqual(_24, o._24, error) &&
   1.136 +         gfx::FuzzyEqual(_31, o._31, error) && gfx::FuzzyEqual(_32, o._32, error) && 
   1.137 +         gfx::FuzzyEqual(_33, o._33, error) && gfx::FuzzyEqual(_34, o._34, error) &&
   1.138 +         gfx::FuzzyEqual(_41, o._41, error) && gfx::FuzzyEqual(_42, o._42, error) && 
   1.139 +         gfx::FuzzyEqual(_43, o._43, error) && gfx::FuzzyEqual(_44, o._44, error);
   1.140 +}
   1.141 +
   1.142 +gfx3DMatrix&
   1.143 +gfx3DMatrix::operator/=(const gfxFloat scalar)
   1.144 +{
   1.145 +  _11 /= scalar;
   1.146 +  _12 /= scalar;
   1.147 +  _13 /= scalar;
   1.148 +  _14 /= scalar;
   1.149 +  _21 /= scalar;
   1.150 +  _22 /= scalar;
   1.151 +  _23 /= scalar;
   1.152 +  _24 /= scalar;
   1.153 +  _31 /= scalar;
   1.154 +  _32 /= scalar;
   1.155 +  _33 /= scalar;
   1.156 +  _34 /= scalar;
   1.157 +  _41 /= scalar;
   1.158 +  _42 /= scalar;
   1.159 +  _43 /= scalar;
   1.160 +  _44 /= scalar;
   1.161 +  return *this;
   1.162 +}
   1.163 +
   1.164 +gfx3DMatrix
   1.165 +gfx3DMatrix::From2D(const gfxMatrix &aMatrix)
   1.166 +{
   1.167 +  gfx3DMatrix matrix;
   1.168 +  matrix._11 = (float)aMatrix.xx;
   1.169 +  matrix._12 = (float)aMatrix.yx;
   1.170 +  matrix._21 = (float)aMatrix.xy;
   1.171 +  matrix._22 = (float)aMatrix.yy;
   1.172 +  matrix._41 = (float)aMatrix.x0;
   1.173 +  matrix._42 = (float)aMatrix.y0;
   1.174 +  return matrix;
   1.175 +}
   1.176 +
   1.177 +bool
   1.178 +gfx3DMatrix::IsIdentity() const
   1.179 +{
   1.180 +  return _11 == 1.0f && _12 == 0.0f && _13 == 0.0f && _14 == 0.0f &&
   1.181 +         _21 == 0.0f && _22 == 1.0f && _23 == 0.0f && _24 == 0.0f &&
   1.182 +         _31 == 0.0f && _32 == 0.0f && _33 == 1.0f && _34 == 0.0f &&
   1.183 +         _41 == 0.0f && _42 == 0.0f && _43 == 0.0f && _44 == 1.0f;
   1.184 +}
   1.185 +
   1.186 +void
   1.187 +gfx3DMatrix::Translate(const gfxPoint3D& aPoint)
   1.188 +{
   1.189 +    _41 += aPoint.x * _11 + aPoint.y * _21 + aPoint.z * _31;
   1.190 +    _42 += aPoint.x * _12 + aPoint.y * _22 + aPoint.z * _32;
   1.191 +    _43 += aPoint.x * _13 + aPoint.y * _23 + aPoint.z * _33;
   1.192 +    _44 += aPoint.x * _14 + aPoint.y * _24 + aPoint.z * _34;
   1.193 +}
   1.194 +
   1.195 +void
   1.196 +gfx3DMatrix::TranslatePost(const gfxPoint3D& aPoint)
   1.197 +{
   1.198 +    _11 += _14 * aPoint.x;
   1.199 +    _21 += _24 * aPoint.x;
   1.200 +    _31 += _34 * aPoint.x;
   1.201 +    _41 += _44 * aPoint.x;
   1.202 +    _12 += _14 * aPoint.y;
   1.203 +    _22 += _24 * aPoint.y;
   1.204 +    _32 += _34 * aPoint.y;
   1.205 +    _42 += _44 * aPoint.y;
   1.206 +    _13 += _14 * aPoint.z;
   1.207 +    _23 += _24 * aPoint.z;
   1.208 +    _33 += _34 * aPoint.z;
   1.209 +    _43 += _44 * aPoint.z;
   1.210 +}
   1.211 +
   1.212 +void
   1.213 +gfx3DMatrix::ScalePost(float aX, float aY, float aZ)
   1.214 +{
   1.215 +  _11 *= aX;
   1.216 +  _21 *= aX;
   1.217 +  _31 *= aX;
   1.218 +  _41 *= aX;
   1.219 +
   1.220 +  _12 *= aY;
   1.221 +  _22 *= aY;
   1.222 +  _32 *= aY;
   1.223 +  _42 *= aY;
   1.224 +
   1.225 +  _13 *= aZ;
   1.226 +  _23 *= aZ;
   1.227 +  _33 *= aZ;
   1.228 +  _43 *= aZ;
   1.229 +}
   1.230 +
   1.231 +void
   1.232 +gfx3DMatrix::SkewXY(double aSkew)
   1.233 +{
   1.234 +    (*this)[1] += (*this)[0] * aSkew;
   1.235 +}
   1.236 +
   1.237 +void 
   1.238 +gfx3DMatrix::SkewXZ(double aSkew)
   1.239 +{
   1.240 +    (*this)[2] += (*this)[0] * aSkew;
   1.241 +}
   1.242 +
   1.243 +void
   1.244 +gfx3DMatrix::SkewYZ(double aSkew)
   1.245 +{
   1.246 +    (*this)[2] += (*this)[1] * aSkew;
   1.247 +}
   1.248 +
   1.249 +void
   1.250 +gfx3DMatrix::Scale(float aX, float aY, float aZ)
   1.251 +{
   1.252 +    (*this)[0] *= aX;
   1.253 +    (*this)[1] *= aY;
   1.254 +    (*this)[2] *= aZ;
   1.255 +}
   1.256 +
   1.257 +void
   1.258 +gfx3DMatrix::Perspective(float aDepth)
   1.259 +{
   1.260 +  NS_ASSERTION(aDepth > 0.0f, "Perspective must be positive!");
   1.261 +  _31 += -1.0/aDepth * _41;
   1.262 +  _32 += -1.0/aDepth * _42;
   1.263 +  _33 += -1.0/aDepth * _43;
   1.264 +  _34 += -1.0/aDepth * _44;
   1.265 +}
   1.266 +
   1.267 +void gfx3DMatrix::SkewXY(double aXSkew, double aYSkew)
   1.268 +{
   1.269 +  float tanX = SafeTangent(aXSkew);
   1.270 +  float tanY = SafeTangent(aYSkew);
   1.271 +  float temp;
   1.272 +
   1.273 +  temp = _11;
   1.274 +  _11 += tanY * _21;
   1.275 +  _21 += tanX * temp;
   1.276 +
   1.277 +  temp = _12;
   1.278 +  _12 += tanY * _22;
   1.279 +  _22 += tanX * temp;
   1.280 +  
   1.281 +  temp = _13;
   1.282 +  _13 += tanY * _23;
   1.283 +  _23 += tanX * temp;
   1.284 +  
   1.285 +  temp = _14;
   1.286 +  _14 += tanY * _24;
   1.287 +  _24 += tanX * temp;
   1.288 +}
   1.289 +
   1.290 +void
   1.291 +gfx3DMatrix::RotateX(double aTheta)
   1.292 +{
   1.293 +  double cosTheta = FlushToZero(cos(aTheta));
   1.294 +  double sinTheta = FlushToZero(sin(aTheta));
   1.295 +
   1.296 +  float temp;
   1.297 +
   1.298 +  temp = _21;
   1.299 +  _21 = cosTheta * _21 + sinTheta * _31;
   1.300 +  _31 = -sinTheta * temp + cosTheta * _31;
   1.301 +  
   1.302 +  temp = _22;
   1.303 +  _22 = cosTheta * _22 + sinTheta * _32;
   1.304 +  _32 = -sinTheta * temp + cosTheta * _32;
   1.305 +  
   1.306 +  temp = _23;
   1.307 +  _23 = cosTheta * _23 + sinTheta * _33;
   1.308 +  _33 = -sinTheta * temp + cosTheta * _33;
   1.309 +  
   1.310 +  temp = _24;
   1.311 +  _24 = cosTheta * _24 + sinTheta * _34;
   1.312 +  _34 = -sinTheta * temp + cosTheta * _34;
   1.313 +}
   1.314 +
   1.315 +void
   1.316 +gfx3DMatrix::RotateY(double aTheta)
   1.317 +{
   1.318 +  double cosTheta = FlushToZero(cos(aTheta));
   1.319 +  double sinTheta = FlushToZero(sin(aTheta));
   1.320 +
   1.321 +  float temp;
   1.322 +
   1.323 +  temp = _11;
   1.324 +  _11 = cosTheta * _11 + -sinTheta * _31;
   1.325 +  _31 = sinTheta * temp + cosTheta * _31;
   1.326 +  
   1.327 +  temp = _12;
   1.328 +  _12 = cosTheta * _12 + -sinTheta * _32;
   1.329 +  _32 = sinTheta * temp + cosTheta * _32;
   1.330 +  
   1.331 +  temp = _13;
   1.332 +  _13 = cosTheta * _13 + -sinTheta * _33;
   1.333 +  _33 = sinTheta * temp + cosTheta * _33;
   1.334 +  
   1.335 +  temp = _14;
   1.336 +  _14 = cosTheta * _14 + -sinTheta * _34;
   1.337 +  _34 = sinTheta * temp + cosTheta * _34;
   1.338 +}
   1.339 +
   1.340 +void
   1.341 +gfx3DMatrix::RotateZ(double aTheta)
   1.342 +{
   1.343 +  double cosTheta = FlushToZero(cos(aTheta));
   1.344 +  double sinTheta = FlushToZero(sin(aTheta));
   1.345 +
   1.346 +  float temp;
   1.347 +
   1.348 +  temp = _11;
   1.349 +  _11 = cosTheta * _11 + sinTheta * _21;
   1.350 +  _21 = -sinTheta * temp + cosTheta * _21;
   1.351 +  
   1.352 +  temp = _12;
   1.353 +  _12 = cosTheta * _12 + sinTheta * _22;
   1.354 +  _22 = -sinTheta * temp + cosTheta * _22;
   1.355 +  
   1.356 +  temp = _13;
   1.357 +  _13 = cosTheta * _13 + sinTheta * _23;
   1.358 +  _23 = -sinTheta * temp + cosTheta * _23;
   1.359 +  
   1.360 +  temp = _14;
   1.361 +  _14 = cosTheta * _14 + sinTheta * _24;
   1.362 +  _24 = -sinTheta * temp + cosTheta * _24;
   1.363 +}
   1.364 +
   1.365 +void 
   1.366 +gfx3DMatrix::PreMultiply(const gfx3DMatrix& aOther)
   1.367 +{
   1.368 +  *this = aOther * (*this);
   1.369 +}
   1.370 +
   1.371 +void
   1.372 +gfx3DMatrix::PreMultiply(const gfxMatrix& aOther)
   1.373 +{
   1.374 +  gfx3DMatrix temp;
   1.375 +  temp._11 = aOther.xx * _11 + aOther.yx * _21;
   1.376 +  temp._21 = aOther.xy * _11 + aOther.yy * _21;
   1.377 +  temp._31 = _31;
   1.378 +  temp._41 = aOther.x0 * _11 + aOther.y0 * _21 + _41;
   1.379 +  temp._12 = aOther.xx * _12 + aOther.yx * _22;
   1.380 +  temp._22 = aOther.xy * _12 + aOther.yy * _22;
   1.381 +  temp._32 = _32;
   1.382 +  temp._42 = aOther.x0 * _12 + aOther.y0 * _22 + _42;
   1.383 +  temp._13 = aOther.xx * _13 + aOther.yx * _23;
   1.384 +  temp._23 = aOther.xy * _13 + aOther.yy * _23;
   1.385 +  temp._33 = _33;
   1.386 +  temp._43 = aOther.x0 * _13 + aOther.y0 * _23 + _43;
   1.387 +  temp._14 = aOther.xx * _14 + aOther.yx * _24;
   1.388 +  temp._24 = aOther.xy * _14 + aOther.yy * _24;
   1.389 +  temp._34 = _34;
   1.390 +  temp._44 = aOther.x0 * _14 + aOther.y0 * _24 + _44;
   1.391 +
   1.392 +  *this = temp;
   1.393 +}
   1.394 +
   1.395 +gfx3DMatrix
   1.396 +gfx3DMatrix::Translation(float aX, float aY, float aZ)
   1.397 +{
   1.398 +  gfx3DMatrix matrix;
   1.399 +
   1.400 +  matrix._41 = aX;
   1.401 +  matrix._42 = aY;
   1.402 +  matrix._43 = aZ;
   1.403 +  return matrix;
   1.404 +}
   1.405 +
   1.406 +gfx3DMatrix
   1.407 +gfx3DMatrix::Translation(const gfxPoint3D& aPoint)
   1.408 +{
   1.409 +  gfx3DMatrix matrix;
   1.410 +
   1.411 +  matrix._41 = aPoint.x;
   1.412 +  matrix._42 = aPoint.y;
   1.413 +  matrix._43 = aPoint.z;
   1.414 +  return matrix;
   1.415 +}
   1.416 +
   1.417 +gfx3DMatrix
   1.418 +gfx3DMatrix::ScalingMatrix(float aFactor)
   1.419 +{
   1.420 +  gfx3DMatrix matrix;
   1.421 +
   1.422 +  matrix._11 = matrix._22 = matrix._33 = aFactor;
   1.423 +  return matrix;
   1.424 +}
   1.425 +
   1.426 +gfx3DMatrix
   1.427 +gfx3DMatrix::ScalingMatrix(float aX, float aY, float aZ)
   1.428 +{
   1.429 +  gfx3DMatrix matrix;
   1.430 +
   1.431 +  matrix._11 = aX;
   1.432 +  matrix._22 = aY;
   1.433 +  matrix._33 = aZ;
   1.434 +
   1.435 +  return matrix;
   1.436 +}
   1.437 +
   1.438 +gfxFloat
   1.439 +gfx3DMatrix::Determinant() const
   1.440 +{
   1.441 +  return _14 * _23 * _32 * _41
   1.442 +       - _13 * _24 * _32 * _41
   1.443 +       - _14 * _22 * _33 * _41
   1.444 +       + _12 * _24 * _33 * _41
   1.445 +       + _13 * _22 * _34 * _41
   1.446 +       - _12 * _23 * _34 * _41
   1.447 +       - _14 * _23 * _31 * _42
   1.448 +       + _13 * _24 * _31 * _42
   1.449 +       + _14 * _21 * _33 * _42
   1.450 +       - _11 * _24 * _33 * _42
   1.451 +       - _13 * _21 * _34 * _42
   1.452 +       + _11 * _23 * _34 * _42
   1.453 +       + _14 * _22 * _31 * _43
   1.454 +       - _12 * _24 * _31 * _43
   1.455 +       - _14 * _21 * _32 * _43
   1.456 +       + _11 * _24 * _32 * _43
   1.457 +       + _12 * _21 * _34 * _43
   1.458 +       - _11 * _22 * _34 * _43
   1.459 +       - _13 * _22 * _31 * _44
   1.460 +       + _12 * _23 * _31 * _44
   1.461 +       + _13 * _21 * _32 * _44
   1.462 +       - _11 * _23 * _32 * _44
   1.463 +       - _12 * _21 * _33 * _44
   1.464 +       + _11 * _22 * _33 * _44;
   1.465 +}
   1.466 +
   1.467 +gfxFloat
   1.468 +gfx3DMatrix::Determinant3x3() const
   1.469 +{
   1.470 +    return _11 * (_22 * _33 - _23 * _32) +
   1.471 +           _12 * (_23 * _31 - _33 * _21) +
   1.472 +           _13 * (_21 * _32 - _22 * _31);
   1.473 +}
   1.474 +
   1.475 +gfx3DMatrix
   1.476 +gfx3DMatrix::Inverse3x3() const
   1.477 +{
   1.478 +    gfxFloat det = Determinant3x3();
   1.479 +    if (det == 0.0) {
   1.480 +        return *this;
   1.481 +    }
   1.482 +
   1.483 +    gfxFloat detInv = 1/det;
   1.484 +    gfx3DMatrix temp;
   1.485 +
   1.486 +    temp._11 = (_22 * _33 - _23 * _32) * detInv;
   1.487 +    temp._12 = (_13 * _32 - _12 * _33) * detInv;
   1.488 +    temp._13 = (_12 * _23 - _13 * _22) * detInv;
   1.489 +    temp._21 = (_23 * _31 - _33 * _21) * detInv;
   1.490 +    temp._22 = (_11 * _33 - _13 * _31) * detInv;
   1.491 +    temp._23 = (_13 * _21 - _11 * _23) * detInv;
   1.492 +    temp._31 = (_21 * _32 - _22 * _31) * detInv;
   1.493 +    temp._32 = (_31 * _12 - _11 * _32) * detInv;
   1.494 +    temp._33 = (_11 * _22 - _12 * _21) * detInv;
   1.495 +    return temp;
   1.496 +}
   1.497 +
   1.498 +bool
   1.499 +gfx3DMatrix::IsSingular() const
   1.500 +{
   1.501 +  return Determinant() == 0.0;
   1.502 +}
   1.503 +
   1.504 +gfx3DMatrix
   1.505 +gfx3DMatrix::Inverse() const
   1.506 +{
   1.507 +  if (TransposedVector(3) == gfxPointH3D(0, 0, 0, 1)) {
   1.508 +    /** 
   1.509 +     * When the matrix contains no perspective, the inverse
   1.510 +     * is the same as the 3x3 inverse of the rotation components
   1.511 +     * multiplied by the inverse of the translation components.
   1.512 +     * Doing these steps separately is faster and more numerically
   1.513 +     * stable.
   1.514 +     *
   1.515 +     * Inverse of the translation matrix is just negating
   1.516 +     * the values.
   1.517 +     */
   1.518 +    gfx3DMatrix matrix3 = Inverse3x3();
   1.519 +    matrix3.Translate(gfxPoint3D(-_41, -_42, -_43));
   1.520 +    return matrix3;
   1.521 + }
   1.522 +
   1.523 +  gfxFloat det = Determinant();
   1.524 +  if (det == 0.0) {
   1.525 +    return *this;
   1.526 +  }
   1.527 +
   1.528 +  gfx3DMatrix temp;
   1.529 +
   1.530 +  temp._11 = _23*_34*_42 - _24*_33*_42 + 
   1.531 +             _24*_32*_43 - _22*_34*_43 - 
   1.532 +             _23*_32*_44 + _22*_33*_44;
   1.533 +  temp._12 = _14*_33*_42 - _13*_34*_42 -
   1.534 +             _14*_32*_43 + _12*_34*_43 +
   1.535 +             _13*_32*_44 - _12*_33*_44;
   1.536 +  temp._13 = _13*_24*_42 - _14*_23*_42 +
   1.537 +             _14*_22*_43 - _12*_24*_43 -
   1.538 +             _13*_22*_44 + _12*_23*_44;
   1.539 +  temp._14 = _14*_23*_32 - _13*_24*_32 -
   1.540 +             _14*_22*_33 + _12*_24*_33 +
   1.541 +             _13*_22*_34 - _12*_23*_34;
   1.542 +  temp._21 = _24*_33*_41 - _23*_34*_41 -
   1.543 +             _24*_31*_43 + _21*_34*_43 +
   1.544 +             _23*_31*_44 - _21*_33*_44;
   1.545 +  temp._22 = _13*_34*_41 - _14*_33*_41 +
   1.546 +             _14*_31*_43 - _11*_34*_43 -
   1.547 +             _13*_31*_44 + _11*_33*_44;
   1.548 +  temp._23 = _14*_23*_41 - _13*_24*_41 -
   1.549 +             _14*_21*_43 + _11*_24*_43 +
   1.550 +             _13*_21*_44 - _11*_23*_44;
   1.551 +  temp._24 = _13*_24*_31 - _14*_23*_31 +
   1.552 +             _14*_21*_33 - _11*_24*_33 -
   1.553 +             _13*_21*_34 + _11*_23*_34;
   1.554 +  temp._31 = _22*_34*_41 - _24*_32*_41 +
   1.555 +             _24*_31*_42 - _21*_34*_42 -
   1.556 +             _22*_31*_44 + _21*_32*_44;
   1.557 +  temp._32 = _14*_32*_41 - _12*_34*_41 -
   1.558 +             _14*_31*_42 + _11*_34*_42 +
   1.559 +             _12*_31*_44 - _11*_32*_44;
   1.560 +  temp._33 = _12*_24*_41 - _14*_22*_41 +
   1.561 +             _14*_21*_42 - _11*_24*_42 -
   1.562 +             _12*_21*_44 + _11*_22*_44;
   1.563 +  temp._34 = _14*_22*_31 - _12*_24*_31 -
   1.564 +             _14*_21*_32 + _11*_24*_32 +
   1.565 +             _12*_21*_34 - _11*_22*_34;
   1.566 +  temp._41 = _23*_32*_41 - _22*_33*_41 -
   1.567 +             _23*_31*_42 + _21*_33*_42 +
   1.568 +             _22*_31*_43 - _21*_32*_43;
   1.569 +  temp._42 = _12*_33*_41 - _13*_32*_41 +
   1.570 +             _13*_31*_42 - _11*_33*_42 -
   1.571 +             _12*_31*_43 + _11*_32*_43;
   1.572 +  temp._43 = _13*_22*_41 - _12*_23*_41 -
   1.573 +             _13*_21*_42 + _11*_23*_42 +
   1.574 +             _12*_21*_43 - _11*_22*_43;
   1.575 +  temp._44 = _12*_23*_31 - _13*_22*_31 +
   1.576 +             _13*_21*_32 - _11*_23*_32 -
   1.577 +             _12*_21*_33 + _11*_22*_33;
   1.578 +
   1.579 +  temp /= det;
   1.580 +  return temp;
   1.581 +}
   1.582 +
   1.583 +gfx3DMatrix&
   1.584 +gfx3DMatrix::Normalize()
   1.585 +{
   1.586 +    for (int i = 0; i < 4; i++) {
   1.587 +        for (int j = 0; j < 4; j++) {
   1.588 +            (*this)[i][j] /= (*this)[3][3];
   1.589 +       }
   1.590 +    }
   1.591 +    return *this;
   1.592 +}
   1.593 +
   1.594 +gfx3DMatrix&
   1.595 +gfx3DMatrix::Transpose()
   1.596 +{
   1.597 +    *this = Transposed();
   1.598 +    return *this;
   1.599 +}
   1.600 +
   1.601 +gfx3DMatrix
   1.602 +gfx3DMatrix::Transposed() const
   1.603 +{
   1.604 +    gfx3DMatrix temp;
   1.605 +    for (int i = 0; i < 4; i++) {
   1.606 +        temp[i] = TransposedVector(i);
   1.607 +    }
   1.608 +    return temp;
   1.609 +}
   1.610 +
   1.611 +gfxPoint
   1.612 +gfx3DMatrix::Transform(const gfxPoint& point) const
   1.613 +{
   1.614 +  gfxPoint3D vec3d(point.x, point.y, 0);
   1.615 +  vec3d = Transform3D(vec3d);
   1.616 +  return gfxPoint(vec3d.x, vec3d.y);
   1.617 +}
   1.618 +
   1.619 +gfxPoint3D
   1.620 +gfx3DMatrix::Transform3D(const gfxPoint3D& point) const
   1.621 +{
   1.622 +  gfxFloat x = point.x * _11 + point.y * _21 + point.z * _31 + _41;
   1.623 +  gfxFloat y = point.x * _12 + point.y * _22 + point.z * _32 + _42;
   1.624 +  gfxFloat z = point.x * _13 + point.y * _23 + point.z * _33 + _43;
   1.625 +  gfxFloat w = point.x * _14 + point.y * _24 + point.z * _34 + _44;
   1.626 +
   1.627 +  x /= w;
   1.628 +  y /= w;
   1.629 +  z /= w;
   1.630 +
   1.631 +  return gfxPoint3D(x, y, z);
   1.632 +}
   1.633 +
   1.634 +gfxPointH3D
   1.635 +gfx3DMatrix::Transform4D(const gfxPointH3D& aPoint) const
   1.636 +{
   1.637 +    gfxFloat x = aPoint.x * _11 + aPoint.y * _21 + aPoint.z * _31 + aPoint.w * _41;
   1.638 +    gfxFloat y = aPoint.x * _12 + aPoint.y * _22 + aPoint.z * _32 + aPoint.w * _42;
   1.639 +    gfxFloat z = aPoint.x * _13 + aPoint.y * _23 + aPoint.z * _33 + aPoint.w * _43;
   1.640 +    gfxFloat w = aPoint.x * _14 + aPoint.y * _24 + aPoint.z * _34 + aPoint.w * _44;
   1.641 +
   1.642 +    return gfxPointH3D(x, y, z, w);
   1.643 +}
   1.644 +
   1.645 +gfxPointH3D
   1.646 +gfx3DMatrix::TransposeTransform4D(const gfxPointH3D& aPoint) const
   1.647 +{
   1.648 +    gfxFloat x = aPoint.x * _11 + aPoint.y * _12 + aPoint.z * _13 + aPoint.w * _14;
   1.649 +    gfxFloat y = aPoint.x * _21 + aPoint.y * _22 + aPoint.z * _23 + aPoint.w * _24;
   1.650 +    gfxFloat z = aPoint.x * _31 + aPoint.y * _32 + aPoint.z * _33 + aPoint.w * _34;
   1.651 +    gfxFloat w = aPoint.x * _41 + aPoint.y * _42 + aPoint.z * _43 + aPoint.w * _44;
   1.652 +
   1.653 +    return gfxPointH3D(x, y, z, w);
   1.654 +}
   1.655 +
   1.656 +gfxRect
   1.657 +gfx3DMatrix::TransformBounds(const gfxRect& rect) const
   1.658 +{
   1.659 +  gfxPoint points[4];
   1.660 +
   1.661 +  points[0] = Transform(rect.TopLeft());
   1.662 +  points[1] = Transform(gfxPoint(rect.X() + rect.Width(), rect.Y()));
   1.663 +  points[2] = Transform(gfxPoint(rect.X(), rect.Y() + rect.Height()));
   1.664 +  points[3] = Transform(gfxPoint(rect.X() + rect.Width(),
   1.665 +                                 rect.Y() + rect.Height()));
   1.666 +
   1.667 +  gfxFloat min_x, max_x;
   1.668 +  gfxFloat min_y, max_y;
   1.669 +
   1.670 +  min_x = max_x = points[0].x;
   1.671 +  min_y = max_y = points[0].y;
   1.672 +
   1.673 +  for (int i=1; i<4; i++) {
   1.674 +    min_x = min(points[i].x, min_x);
   1.675 +    max_x = max(points[i].x, max_x);
   1.676 +    min_y = min(points[i].y, min_y);
   1.677 +    max_y = max(points[i].y, max_y);
   1.678 +  }
   1.679 +
   1.680 +  return gfxRect(min_x, min_y, max_x - min_x, max_y - min_y);
   1.681 +}
   1.682 +
   1.683 +gfxQuad 
   1.684 +gfx3DMatrix::TransformRect(const gfxRect& aRect) const
   1.685 +{
   1.686 +  gfxPoint points[4];
   1.687 +
   1.688 +  points[0] = Transform(aRect.TopLeft());
   1.689 +  points[1] = Transform(gfxPoint(aRect.X() + aRect.Width(), aRect.Y()));
   1.690 +  points[2] = Transform(gfxPoint(aRect.X() + aRect.Width(),
   1.691 +                                 aRect.Y() + aRect.Height()));
   1.692 +  points[3] = Transform(gfxPoint(aRect.X(), aRect.Y() + aRect.Height()));
   1.693 +  
   1.694 +  // Could this ever result in lines that intersect? I don't think so.
   1.695 +  return gfxQuad(points[0], points[1], points[2], points[3]);
   1.696 +}
   1.697 +
   1.698 +bool
   1.699 +gfx3DMatrix::Is2D() const
   1.700 +{
   1.701 +  if (_13 != 0.0f || _14 != 0.0f ||
   1.702 +      _23 != 0.0f || _24 != 0.0f ||
   1.703 +      _31 != 0.0f || _32 != 0.0f || _33 != 1.0f || _34 != 0.0f ||
   1.704 +      _43 != 0.0f || _44 != 1.0f) {
   1.705 +    return false;
   1.706 +  }
   1.707 +  return true;
   1.708 +}
   1.709 +
   1.710 +bool
   1.711 +gfx3DMatrix::Is2D(gfxMatrix* aMatrix) const
   1.712 +{
   1.713 +  if (!Is2D()) {
   1.714 +    return false;
   1.715 +  }
   1.716 +  if (aMatrix) {
   1.717 +    aMatrix->xx = _11;
   1.718 +    aMatrix->yx = _12;
   1.719 +    aMatrix->xy = _21;
   1.720 +    aMatrix->yy = _22;
   1.721 +    aMatrix->x0 = _41;
   1.722 +    aMatrix->y0 = _42;
   1.723 +  }
   1.724 +  return true;
   1.725 +}
   1.726 +
   1.727 +bool
   1.728 +gfx3DMatrix::CanDraw2D(gfxMatrix* aMatrix) const
   1.729 +{
   1.730 +  if (_14 != 0.0f ||
   1.731 +      _24 != 0.0f ||
   1.732 +      _44 != 1.0f) {
   1.733 +    return false;
   1.734 +  }
   1.735 +  if (aMatrix) {
   1.736 +    aMatrix->xx = _11;
   1.737 +    aMatrix->yx = _12;
   1.738 +    aMatrix->xy = _21;
   1.739 +    aMatrix->yy = _22;
   1.740 +    aMatrix->x0 = _41;
   1.741 +    aMatrix->y0 = _42;
   1.742 +  }
   1.743 +  return true;
   1.744 +}
   1.745 +
   1.746 +gfx3DMatrix&
   1.747 +gfx3DMatrix::ProjectTo2D()
   1.748 +{
   1.749 +  _31 = 0.0f;
   1.750 +  _32 = 0.0f;
   1.751 +  _13 = 0.0f; 
   1.752 +  _23 = 0.0f; 
   1.753 +  _33 = 1.0f; 
   1.754 +  _43 = 0.0f; 
   1.755 +  _34 = 0.0f;
   1.756 +  return *this;
   1.757 +}
   1.758 +
   1.759 +gfxPoint gfx3DMatrix::ProjectPoint(const gfxPoint& aPoint) const
   1.760 +{
   1.761 +  // Define a ray of the form P + Ut where t is a real number
   1.762 +  // w is assumed to always be 1 when transforming 3d points with our
   1.763 +  // 4x4 matrix.
   1.764 +  // p is our click point, q is another point on the same ray.
   1.765 +  // 
   1.766 +  // Note: since the transformation is a general projective transformation and is not
   1.767 +  // necessarily affine, we can't just take a unit vector u, back-transform it, and use
   1.768 +  // it as unit vector on the back-transformed ray. Instead, we really must take two points
   1.769 +  // on the ray and back-transform them.
   1.770 +  gfxPoint3D p(aPoint.x, aPoint.y, 0);
   1.771 +  gfxPoint3D q(aPoint.x, aPoint.y, 1);
   1.772 +
   1.773 +  // Back transform the vectors (using w = 1) and normalize
   1.774 +  // back into 3d vectors by dividing by the w component.
   1.775 +  gfxPoint3D pback = Transform3D(p);
   1.776 +  gfxPoint3D qback = Transform3D(q);
   1.777 +  gfxPoint3D uback = qback - pback;
   1.778 +
   1.779 +  // Find the point where the back transformed line intersects z=0
   1.780 +  // and find t.
   1.781 +  
   1.782 +  float t = -pback.z / uback.z;
   1.783 +
   1.784 +  gfxPoint result(pback.x + t*uback.x, pback.y + t*uback.y);
   1.785 +
   1.786 +  return result;
   1.787 +}
   1.788 +
   1.789 +gfxRect gfx3DMatrix::ProjectRectBounds(const gfxRect& aRect) const
   1.790 +{
   1.791 +  gfxPoint points[4];
   1.792 +
   1.793 +  points[0] = ProjectPoint(aRect.TopLeft());
   1.794 +  points[1] = ProjectPoint(aRect.TopRight());
   1.795 +  points[2] = ProjectPoint(aRect.BottomLeft());
   1.796 +  points[3] = ProjectPoint(aRect.BottomRight());
   1.797 +
   1.798 +  gfxFloat min_x, max_x;
   1.799 +  gfxFloat min_y, max_y;
   1.800 +
   1.801 +  min_x = max_x = points[0].x;
   1.802 +  min_y = max_y = points[0].y;
   1.803 +
   1.804 +  for (int i=1; i<4; i++) {
   1.805 +    min_x = min(points[i].x, min_x);
   1.806 +    max_x = max(points[i].x, max_x);
   1.807 +    min_y = min(points[i].y, min_y);
   1.808 +    max_y = max(points[i].y, max_y);
   1.809 +  }
   1.810 +
   1.811 +  return gfxRect(min_x, min_y, max_x - min_x, max_y - min_y);
   1.812 +}
   1.813 +
   1.814 +gfxRect gfx3DMatrix::UntransformBounds(const gfxRect& aRect, const gfxRect& aChildBounds) const
   1.815 +{
   1.816 +  if (Is2D()) {
   1.817 +    return Inverse().TransformBounds(aRect);
   1.818 +  }
   1.819 +  gfxRect bounds = TransformBounds(aChildBounds);
   1.820 +
   1.821 +  gfxRect rect = aRect.Intersect(bounds);
   1.822 +
   1.823 +  return Inverse().ProjectRectBounds(rect);
   1.824 +}
   1.825 +
   1.826 +bool gfx3DMatrix::UntransformPoint(const gfxPoint& aPoint, const gfxRect& aChildBounds, gfxPoint* aOut) const
   1.827 +{
   1.828 +  if (Is2D()) {
   1.829 +    *aOut = Inverse().Transform(aPoint);
   1.830 +    return true;
   1.831 +  }
   1.832 +  gfxRect bounds = TransformBounds(aChildBounds);
   1.833 +
   1.834 +  if (!bounds.Contains(aPoint)) {
   1.835 +    return false;
   1.836 +  }
   1.837 +
   1.838 +  *aOut = Inverse().ProjectPoint(aPoint);
   1.839 +  return true;
   1.840 +}
   1.841 +
   1.842 +gfxPoint3D gfx3DMatrix::GetNormalVector() const
   1.843 +{
   1.844 +  // Define a plane in transformed space as the transformations
   1.845 +  // of 3 points on the z=0 screen plane.
   1.846 +  gfxPoint3D a = Transform3D(gfxPoint3D(0, 0, 0));
   1.847 +  gfxPoint3D b = Transform3D(gfxPoint3D(0, 1, 0));
   1.848 +  gfxPoint3D c = Transform3D(gfxPoint3D(1, 0, 0));
   1.849 +
   1.850 +  // Convert to two vectors on the surface of the plane.
   1.851 +  gfxPoint3D ab = b - a;
   1.852 +  gfxPoint3D ac = c - a;
   1.853 +
   1.854 +  return ac.CrossProduct(ab);
   1.855 +}
   1.856 +
   1.857 +bool gfx3DMatrix::IsBackfaceVisible() const
   1.858 +{
   1.859 +  // Inverse()._33 < 0;
   1.860 +  gfxFloat det = Determinant();
   1.861 +  float _33 = _12*_24*_41 - _14*_22*_41 +
   1.862 +              _14*_21*_42 - _11*_24*_42 -
   1.863 +              _12*_21*_44 + _11*_22*_44;
   1.864 +  return (_33 * det) < 0;
   1.865 +}
   1.866 +
   1.867 +void gfx3DMatrix::NudgeToIntegers(void)
   1.868 +{
   1.869 +  NudgeToInteger(&_11);
   1.870 +  NudgeToInteger(&_12);
   1.871 +  NudgeToInteger(&_13);
   1.872 +  NudgeToInteger(&_14);
   1.873 +  NudgeToInteger(&_21);
   1.874 +  NudgeToInteger(&_22);
   1.875 +  NudgeToInteger(&_23);
   1.876 +  NudgeToInteger(&_24);
   1.877 +  NudgeToInteger(&_31);
   1.878 +  NudgeToInteger(&_32);
   1.879 +  NudgeToInteger(&_33);
   1.880 +  NudgeToInteger(&_34);
   1.881 +  NudgeToInteger(&_41);
   1.882 +  NudgeToInteger(&_42);
   1.883 +  NudgeToInteger(&_43);
   1.884 +  NudgeToInteger(&_44);
   1.885 +}

mercurial