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/. */
8 #include "Quaternion.h"
13 #include <float.h> // for FLT_EPSILON
15 #include "mozilla/FloatingPoint.h" // for UnspecifiedNaN
23 /* Force small values to zero. We do this to avoid having sin(360deg)
24 * evaluate to a tiny but nonzero value.
27 FlushToZero(double aVal
)
29 // XXX Is double precision really necessary here
30 if (-FLT_EPSILON
< aVal
&& aVal
< FLT_EPSILON
) {
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.
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
) {
57 } else if (cosTheta
< 0 && cosTheta
>= -kEpsilon
) {
60 return FlushToZero(sinTheta
/ cosTheta
);
64 Matrix::Rotation(Float aAngle
)
68 Float s
= sinf(aAngle
);
69 Float c
= cosf(aAngle
);
79 template<> MatrixDouble
80 MatrixDouble::Rotation(Double aAngle
)
82 MatrixDouble newMatrix
;
84 Double s
= sin(aAngle
);
85 Double c
= cos(aAngle
);
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
;
124 } // namespace mozilla