Bug 1444940 [wpt PR 9917] - Writable streams: test changes to abort() under error...
[gecko.git] / gfx / 2d / Matrix.cpp
blob715e86562e1c6063115ff07924ce4ab1da3ff0bb
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include "Matrix.h"
8 #include "Quaternion.h"
9 #include "Tools.h"
10 #include <algorithm>
11 #include <ostream>
12 #include <math.h>
13 #include <float.h> // for FLT_EPSILON
15 #include "mozilla/FloatingPoint.h" // for UnspecifiedNaN
17 using namespace std;
20 namespace mozilla {
21 namespace gfx {
23 /* Force small values to zero. We do this to avoid having sin(360deg)
24 * evaluate to a tiny but nonzero value.
26 double
27 FlushToZero(double aVal)
29 // XXX Is double precision really necessary here
30 if (-FLT_EPSILON < aVal && aVal < FLT_EPSILON) {
31 return 0.0f;
32 } else {
33 return aVal;
37 /* Computes tan(aTheta). For values of aTheta such that tan(aTheta) is
38 * undefined or very large, SafeTangent returns a manageably large value
39 * of the correct sign.
41 double
42 SafeTangent(double aTheta)
44 // XXX Is double precision really necessary here
45 const double kEpsilon = 0.0001;
47 /* tan(theta) = sin(theta)/cos(theta); problems arise when
48 * cos(theta) is too close to zero. Limit cos(theta) to the
49 * range [-1, -epsilon] U [epsilon, 1].
52 double sinTheta = sin(aTheta);
53 double cosTheta = cos(aTheta);
55 if (cosTheta >= 0 && cosTheta < kEpsilon) {
56 cosTheta = kEpsilon;
57 } else if (cosTheta < 0 && cosTheta >= -kEpsilon) {
58 cosTheta = -kEpsilon;
60 return FlushToZero(sinTheta / cosTheta);
63 template<> Matrix
64 Matrix::Rotation(Float aAngle)
66 Matrix newMatrix;
68 Float s = sinf(aAngle);
69 Float c = cosf(aAngle);
71 newMatrix._11 = c;
72 newMatrix._12 = s;
73 newMatrix._21 = -s;
74 newMatrix._22 = c;
76 return newMatrix;
79 template<> MatrixDouble
80 MatrixDouble::Rotation(Double aAngle)
82 MatrixDouble newMatrix;
84 Double s = sin(aAngle);
85 Double c = cos(aAngle);
87 newMatrix._11 = c;
88 newMatrix._12 = s;
89 newMatrix._21 = -s;
90 newMatrix._22 = c;
92 return newMatrix;
95 template<> Matrix4x4
96 MatrixDouble::operator*(const Matrix4x4& aMatrix) const
98 Matrix4x4 resultMatrix;
100 resultMatrix._11 = this->_11 * aMatrix._11 + this->_12 * aMatrix._21;
101 resultMatrix._12 = this->_11 * aMatrix._12 + this->_12 * aMatrix._22;
102 resultMatrix._13 = this->_11 * aMatrix._13 + this->_12 * aMatrix._23;
103 resultMatrix._14 = this->_11 * aMatrix._14 + this->_12 * aMatrix._24;
105 resultMatrix._21 = this->_21 * aMatrix._11 + this->_22 * aMatrix._21;
106 resultMatrix._22 = this->_21 * aMatrix._12 + this->_22 * aMatrix._22;
107 resultMatrix._23 = this->_21 * aMatrix._13 + this->_22 * aMatrix._23;
108 resultMatrix._24 = this->_21 * aMatrix._14 + this->_22 * aMatrix._24;
110 resultMatrix._31 = aMatrix._31;
111 resultMatrix._32 = aMatrix._32;
112 resultMatrix._33 = aMatrix._33;
113 resultMatrix._34 = aMatrix._34;
115 resultMatrix._41 = this->_31 * aMatrix._11 + this->_32 * aMatrix._21 + aMatrix._41;
116 resultMatrix._42 = this->_31 * aMatrix._12 + this->_32 * aMatrix._22 + aMatrix._42;
117 resultMatrix._43 = this->_31 * aMatrix._13 + this->_32 * aMatrix._23 + aMatrix._43;
118 resultMatrix._44 = this->_31 * aMatrix._14 + this->_32 * aMatrix._24 + aMatrix._44;
120 return resultMatrix;
123 } // namespace gfx
124 } // namespace mozilla