/work/obj-fuzz/dist/include/mozilla/gfx/Quaternion.h
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 | | #ifndef MOZILLA_GFX_QUATERNION_H_ |
8 | | #define MOZILLA_GFX_QUATERNION_H_ |
9 | | |
10 | | #include "Types.h" |
11 | | #include <math.h> |
12 | | #include <ostream> |
13 | | #include "mozilla/Attributes.h" |
14 | | #include "mozilla/DebugOnly.h" |
15 | | #include "mozilla/gfx/MatrixFwd.h" |
16 | | #include "mozilla/gfx/Point.h" |
17 | | |
18 | | namespace mozilla { |
19 | | namespace gfx { |
20 | | |
21 | | class Quaternion |
22 | | { |
23 | | public: |
24 | | Quaternion() |
25 | | : x(0.0f), y(0.0f), z(0.0f), w(1.0f) |
26 | 0 | {} |
27 | | |
28 | | Quaternion(Float aX, Float aY, Float aZ, Float aW) |
29 | | : x(aX), y(aY), z(aZ), w(aW) |
30 | 0 | {} |
31 | | |
32 | | |
33 | | Quaternion(const Quaternion& aOther) |
34 | 0 | { |
35 | 0 | memcpy(this, &aOther, sizeof(*this)); |
36 | 0 | } |
37 | | |
38 | | Float x, y, z, w; |
39 | | |
40 | | friend std::ostream& operator<<(std::ostream& aStream, const Quaternion& aQuat); |
41 | | |
42 | | void Set(Float aX, Float aY, Float aZ, Float aW) |
43 | 0 | { |
44 | 0 | x = aX; y = aY; z = aZ; w = aW; |
45 | 0 | } |
46 | | |
47 | | // Assumes upper 3x3 of aMatrix is a pure rotation matrix (no scaling) |
48 | | void SetFromRotationMatrix(const Matrix4x4& aMatrix); |
49 | | |
50 | | // result = this * aQuat |
51 | | Quaternion operator*(const Quaternion &aQuat) const |
52 | 0 | { |
53 | 0 | Quaternion o; |
54 | 0 | const Float bx = aQuat.x, by = aQuat.y, bz = aQuat.z, bw = aQuat.w; |
55 | 0 |
|
56 | 0 | o.x = x*bw + w*bx + y*bz - z*by; |
57 | 0 | o.y = y*bw + w*by + z*bx - x*bz; |
58 | 0 | o.z = z*bw + w*bz + x*by - y*bx; |
59 | 0 | o.w = w*bw - x*bx - y*by - z*bz; |
60 | 0 | return o; |
61 | 0 | } |
62 | | |
63 | | Quaternion& operator*=(const Quaternion &aQuat) |
64 | 0 | { |
65 | 0 | *this = *this * aQuat; |
66 | 0 | return *this; |
67 | 0 | } |
68 | | |
69 | | Float Length() const |
70 | 0 | { |
71 | 0 | return sqrt(x*x + y*y + z*z + w*w); |
72 | 0 | } |
73 | | |
74 | | Quaternion& Conjugate() |
75 | 0 | { |
76 | 0 | x *= -1.f; y *= -1.f; z *= -1.f; |
77 | 0 | return *this; |
78 | 0 | } |
79 | | |
80 | | Quaternion& Normalize() |
81 | 0 | { |
82 | 0 | Float l = Length(); |
83 | 0 | if (l) { |
84 | 0 | l = 1.0f / l; |
85 | 0 | x *= l; y *= l; z *= l; w *= l; |
86 | 0 | } else { |
87 | 0 | x = y = z = 0.f; |
88 | 0 | w = 1.f; |
89 | 0 | } |
90 | 0 | return *this; |
91 | 0 | } |
92 | | |
93 | | Quaternion& Invert() |
94 | 0 | { |
95 | 0 | return Conjugate().Normalize(); |
96 | 0 | } |
97 | | |
98 | 0 | Point3D RotatePoint(const Point3D& aPoint) { |
99 | 0 | Float uvx = Float(2.0) * (y*aPoint.z - z*aPoint.y); |
100 | 0 | Float uvy = Float(2.0) * (z*aPoint.x - x*aPoint.z); |
101 | 0 | Float uvz = Float(2.0) * (x*aPoint.y - y*aPoint.x); |
102 | 0 |
|
103 | 0 | return Point3D(aPoint.x + w*uvx + y*uvz - z*uvy, |
104 | 0 | aPoint.y + w*uvy + z*uvx - x*uvz, |
105 | 0 | aPoint.z + w*uvz + x*uvy - y*uvx); |
106 | 0 | } |
107 | | }; |
108 | | |
109 | | } // namespace gfx |
110 | | } // namespace mozilla |
111 | | |
112 | | #endif |