Coverage Report

Created: 2018-09-25 14:53

/src/mozilla-central/gfx/2d/Quaternion.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 "Quaternion.h"
8
#include "Matrix.h"
9
#include "Tools.h"
10
#include <algorithm>
11
#include <ostream>
12
#include <math.h>
13
14
using namespace std;
15
16
namespace mozilla {
17
namespace gfx {
18
19
std::ostream&
20
operator<<(std::ostream& aStream, const Quaternion& aQuat)
21
0
{
22
0
  return aStream << "< " << aQuat.x << " "  << aQuat.y << " " << aQuat.z << " " << aQuat.w << ">";
23
0
}
24
25
void
26
Quaternion::SetFromRotationMatrix(const Matrix4x4& m)
27
0
{
28
0
  // see http://www.euclideanspace.com/maths/geometry/rotations/conversions/matrixToQuaternion/index.htm
29
0
  const Float trace = m._11 + m._22 + m._33;
30
0
  if (trace > 0.0) {
31
0
    const Float s = 0.5f / sqrt(trace + 1.0f);
32
0
    w = 0.25f / s;
33
0
    x = (m._32 - m._23) * s;
34
0
    y = (m._13 - m._31) * s;
35
0
    z = (m._21 - m._12) * s;
36
0
  } else if (m._11 > m._22 && m._11 > m._33) {
37
0
    const Float s = 2.0f * sqrt(1.0f + m._11 - m._22 - m._33);
38
0
    w = (m._32 - m._23) / s;
39
0
    x = 0.25f * s;
40
0
    y = (m._12 + m._21) / s;
41
0
    z = (m._13 + m._31) / s;
42
0
  } else if (m._22 > m._33) {
43
0
    const Float s = 2.0 * sqrt(1.0f + m._22 - m._11 - m._33);
44
0
    w = (m._13 - m._31) / s;
45
0
    x = (m._12 + m._21) / s;
46
0
    y = 0.25f * s;
47
0
    z = (m._23 + m._32) / s;
48
0
  } else {
49
0
    const Float s = 2.0 * sqrt(1.0f + m._33 - m._11 - m._22);
50
0
    w = (m._21 - m._12) / s;
51
0
    x = (m._13 + m._31) / s;
52
0
    y = (m._23 + m._32) / s;
53
0
    z = 0.25f * s;
54
0
  }
55
0
}
56
57
} // namespace gfx
58
} // namespace mozilla