/src/mozilla-central/gfx/2d/Matrix.cpp
Line | Count | Source (jump to first uncovered line) |
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/. */ |
6 | | |
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 |
14 | | |
15 | | #include "mozilla/FloatingPoint.h" // for UnspecifiedNaN |
16 | | |
17 | | using namespace std; |
18 | | |
19 | | |
20 | | namespace mozilla { |
21 | | namespace gfx { |
22 | | |
23 | | /* Force small values to zero. We do this to avoid having sin(360deg) |
24 | | * evaluate to a tiny but nonzero value. |
25 | | */ |
26 | | double |
27 | | FlushToZero(double aVal) |
28 | 0 | { |
29 | 0 | // XXX Is double precision really necessary here |
30 | 0 | if (-FLT_EPSILON < aVal && aVal < FLT_EPSILON) { |
31 | 0 | return 0.0f; |
32 | 0 | } else { |
33 | 0 | return aVal; |
34 | 0 | } |
35 | 0 | } |
36 | | |
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. |
40 | | */ |
41 | | double |
42 | | SafeTangent(double aTheta) |
43 | 0 | { |
44 | 0 | // XXX Is double precision really necessary here |
45 | 0 | const double kEpsilon = 0.0001; |
46 | 0 |
|
47 | 0 | /* tan(theta) = sin(theta)/cos(theta); problems arise when |
48 | 0 | * cos(theta) is too close to zero. Limit cos(theta) to the |
49 | 0 | * range [-1, -epsilon] U [epsilon, 1]. |
50 | 0 | */ |
51 | 0 |
|
52 | 0 | double sinTheta = sin(aTheta); |
53 | 0 | double cosTheta = cos(aTheta); |
54 | 0 |
|
55 | 0 | if (cosTheta >= 0 && cosTheta < kEpsilon) { |
56 | 0 | cosTheta = kEpsilon; |
57 | 0 | } else if (cosTheta < 0 && cosTheta >= -kEpsilon) { |
58 | 0 | cosTheta = -kEpsilon; |
59 | 0 | } |
60 | 0 | return FlushToZero(sinTheta / cosTheta); |
61 | 0 | } |
62 | | |
63 | | template<> Matrix |
64 | | Matrix::Rotation(Float aAngle) |
65 | 0 | { |
66 | 0 | Matrix newMatrix; |
67 | 0 |
|
68 | 0 | Float s = sinf(aAngle); |
69 | 0 | Float c = cosf(aAngle); |
70 | 0 |
|
71 | 0 | newMatrix._11 = c; |
72 | 0 | newMatrix._12 = s; |
73 | 0 | newMatrix._21 = -s; |
74 | 0 | newMatrix._22 = c; |
75 | 0 |
|
76 | 0 | return newMatrix; |
77 | 0 | } |
78 | | |
79 | | template<> MatrixDouble |
80 | | MatrixDouble::Rotation(Double aAngle) |
81 | 0 | { |
82 | 0 | MatrixDouble newMatrix; |
83 | 0 |
|
84 | 0 | Double s = sin(aAngle); |
85 | 0 | Double c = cos(aAngle); |
86 | 0 |
|
87 | 0 | newMatrix._11 = c; |
88 | 0 | newMatrix._12 = s; |
89 | 0 | newMatrix._21 = -s; |
90 | 0 | newMatrix._22 = c; |
91 | 0 |
|
92 | 0 | return newMatrix; |
93 | 0 | } |
94 | | |
95 | | template<> Matrix4x4 |
96 | | MatrixDouble::operator*(const Matrix4x4& aMatrix) const |
97 | 0 | { |
98 | 0 | Matrix4x4 resultMatrix; |
99 | 0 |
|
100 | 0 | resultMatrix._11 = this->_11 * aMatrix._11 + this->_12 * aMatrix._21; |
101 | 0 | resultMatrix._12 = this->_11 * aMatrix._12 + this->_12 * aMatrix._22; |
102 | 0 | resultMatrix._13 = this->_11 * aMatrix._13 + this->_12 * aMatrix._23; |
103 | 0 | resultMatrix._14 = this->_11 * aMatrix._14 + this->_12 * aMatrix._24; |
104 | 0 |
|
105 | 0 | resultMatrix._21 = this->_21 * aMatrix._11 + this->_22 * aMatrix._21; |
106 | 0 | resultMatrix._22 = this->_21 * aMatrix._12 + this->_22 * aMatrix._22; |
107 | 0 | resultMatrix._23 = this->_21 * aMatrix._13 + this->_22 * aMatrix._23; |
108 | 0 | resultMatrix._24 = this->_21 * aMatrix._14 + this->_22 * aMatrix._24; |
109 | 0 |
|
110 | 0 | resultMatrix._31 = aMatrix._31; |
111 | 0 | resultMatrix._32 = aMatrix._32; |
112 | 0 | resultMatrix._33 = aMatrix._33; |
113 | 0 | resultMatrix._34 = aMatrix._34; |
114 | 0 |
|
115 | 0 | resultMatrix._41 = this->_31 * aMatrix._11 + this->_32 * aMatrix._21 + aMatrix._41; |
116 | 0 | resultMatrix._42 = this->_31 * aMatrix._12 + this->_32 * aMatrix._22 + aMatrix._42; |
117 | 0 | resultMatrix._43 = this->_31 * aMatrix._13 + this->_32 * aMatrix._23 + aMatrix._43; |
118 | 0 | resultMatrix._44 = this->_31 * aMatrix._14 + this->_32 * aMatrix._24 + aMatrix._44; |
119 | 0 |
|
120 | 0 | return resultMatrix; |
121 | 0 | } |
122 | | |
123 | | } // namespace gfx |
124 | | } // namespace mozilla |