2011-06-24 21:41:16 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
2012-05-21 15:12:37 +04:00
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2011-05-26 23:41:33 +04:00
|
|
|
|
2011-06-24 21:41:16 +04:00
|
|
|
#include "Matrix.h"
|
2015-04-01 23:02:20 +03:00
|
|
|
#include "Quaternion.h"
|
2012-09-12 09:24:09 +04:00
|
|
|
#include "Tools.h"
|
2014-08-22 17:40:02 +04:00
|
|
|
#include <algorithm>
|
2015-01-19 19:29:16 +03:00
|
|
|
#include <ostream>
|
2011-06-24 21:41:16 +04:00
|
|
|
#include <math.h>
|
2015-07-10 02:27:38 +03:00
|
|
|
#include <float.h> // for FLT_EPSILON
|
2011-05-26 23:41:33 +04:00
|
|
|
|
2014-06-24 22:15:00 +04:00
|
|
|
#include "mozilla/FloatingPoint.h" // for UnspecifiedNaN
|
|
|
|
|
2014-08-22 17:40:02 +04:00
|
|
|
using namespace std;
|
|
|
|
|
2015-12-03 02:52:00 +03:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace gfx {
|
2015-07-10 02:27:38 +03:00
|
|
|
|
|
|
|
/* Force small values to zero. We do this to avoid having sin(360deg)
|
|
|
|
* evaluate to a tiny but nonzero value.
|
|
|
|
*/
|
|
|
|
double
|
|
|
|
FlushToZero(double aVal)
|
|
|
|
{
|
|
|
|
// XXX Is double precision really necessary here
|
|
|
|
if (-FLT_EPSILON < aVal && aVal < FLT_EPSILON) {
|
|
|
|
return 0.0f;
|
|
|
|
} else {
|
|
|
|
return aVal;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Computes tan(aTheta). For values of aTheta such that tan(aTheta) is
|
|
|
|
* undefined or very large, SafeTangent returns a manageably large value
|
|
|
|
* of the correct sign.
|
|
|
|
*/
|
|
|
|
double
|
|
|
|
SafeTangent(double aTheta)
|
|
|
|
{
|
|
|
|
// XXX Is double precision really necessary here
|
|
|
|
const double kEpsilon = 0.0001;
|
|
|
|
|
|
|
|
/* tan(theta) = sin(theta)/cos(theta); problems arise when
|
|
|
|
* cos(theta) is too close to zero. Limit cos(theta) to the
|
|
|
|
* range [-1, -epsilon] U [epsilon, 1].
|
|
|
|
*/
|
|
|
|
|
|
|
|
double sinTheta = sin(aTheta);
|
|
|
|
double cosTheta = cos(aTheta);
|
|
|
|
|
|
|
|
if (cosTheta >= 0 && cosTheta < kEpsilon) {
|
|
|
|
cosTheta = kEpsilon;
|
|
|
|
} else if (cosTheta < 0 && cosTheta >= -kEpsilon) {
|
|
|
|
cosTheta = -kEpsilon;
|
|
|
|
}
|
|
|
|
return FlushToZero(sinTheta / cosTheta);
|
|
|
|
}
|
|
|
|
|
2015-01-19 19:29:16 +03:00
|
|
|
std::ostream&
|
|
|
|
operator<<(std::ostream& aStream, const Matrix& aMatrix)
|
|
|
|
{
|
|
|
|
return aStream << "[ " << aMatrix._11
|
|
|
|
<< " " << aMatrix._12
|
|
|
|
<< "; " << aMatrix._21
|
|
|
|
<< " " << aMatrix._22
|
|
|
|
<< "; " << aMatrix._31
|
|
|
|
<< " " << aMatrix._32
|
|
|
|
<< "; ]";
|
|
|
|
}
|
|
|
|
|
2011-06-24 21:41:16 +04:00
|
|
|
Matrix
|
|
|
|
Matrix::Rotation(Float aAngle)
|
|
|
|
{
|
|
|
|
Matrix newMatrix;
|
2011-05-26 23:41:33 +04:00
|
|
|
|
2015-09-29 03:12:07 +03:00
|
|
|
Float s = sinf(aAngle);
|
|
|
|
Float c = cosf(aAngle);
|
2011-05-26 23:41:33 +04:00
|
|
|
|
2011-06-24 21:41:16 +04:00
|
|
|
newMatrix._11 = c;
|
|
|
|
newMatrix._12 = s;
|
|
|
|
newMatrix._21 = -s;
|
|
|
|
newMatrix._22 = c;
|
2014-08-22 17:40:02 +04:00
|
|
|
|
2011-06-24 21:41:16 +04:00
|
|
|
return newMatrix;
|
|
|
|
}
|
2011-05-26 23:41:33 +04:00
|
|
|
|
2011-06-24 21:41:16 +04:00
|
|
|
Rect
|
|
|
|
Matrix::TransformBounds(const Rect &aRect) const
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
Point quad[4];
|
|
|
|
Float min_x, max_x;
|
|
|
|
Float min_y, max_y;
|
2011-05-26 23:41:33 +04:00
|
|
|
|
2016-09-08 19:26:03 +03:00
|
|
|
quad[0] = TransformPoint(aRect.TopLeft());
|
|
|
|
quad[1] = TransformPoint(aRect.TopRight());
|
|
|
|
quad[2] = TransformPoint(aRect.BottomLeft());
|
|
|
|
quad[3] = TransformPoint(aRect.BottomRight());
|
2011-05-26 23:41:33 +04:00
|
|
|
|
2011-06-24 21:41:16 +04:00
|
|
|
min_x = max_x = quad[0].x;
|
|
|
|
min_y = max_y = quad[0].y;
|
2011-05-26 23:41:33 +04:00
|
|
|
|
2011-06-24 21:41:16 +04:00
|
|
|
for (i = 1; i < 4; i++) {
|
|
|
|
if (quad[i].x < min_x)
|
|
|
|
min_x = quad[i].x;
|
|
|
|
if (quad[i].x > max_x)
|
|
|
|
max_x = quad[i].x;
|
2011-05-26 23:41:33 +04:00
|
|
|
|
2011-06-24 21:41:16 +04:00
|
|
|
if (quad[i].y < min_y)
|
|
|
|
min_y = quad[i].y;
|
|
|
|
if (quad[i].y > max_y)
|
|
|
|
max_y = quad[i].y;
|
2011-05-26 23:41:33 +04:00
|
|
|
}
|
|
|
|
|
2011-06-24 21:41:16 +04:00
|
|
|
return Rect(min_x, min_y, max_x - min_x, max_y - min_y);
|
2011-05-26 23:41:33 +04:00
|
|
|
}
|
|
|
|
|
2014-09-11 04:46:21 +04:00
|
|
|
Matrix&
|
2012-09-05 04:15:52 +04:00
|
|
|
Matrix::NudgeToIntegers()
|
|
|
|
{
|
|
|
|
NudgeToInteger(&_11);
|
|
|
|
NudgeToInteger(&_12);
|
|
|
|
NudgeToInteger(&_21);
|
|
|
|
NudgeToInteger(&_22);
|
|
|
|
NudgeToInteger(&_31);
|
|
|
|
NudgeToInteger(&_32);
|
2014-09-11 04:46:21 +04:00
|
|
|
return *this;
|
2012-09-05 04:15:52 +04:00
|
|
|
}
|
|
|
|
|
2015-07-13 18:25:42 +03:00
|
|
|
} // namespace gfx
|
|
|
|
} // namespace mozilla
|