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