| Flutter Impeller |

quaternion.h
Go to the documentation of this file.
1 // Copyright 2013 The Flutter Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #ifndef FLUTTER_IMPELLER_GEOMETRY_QUATERNION_H_
6 #define FLUTTER_IMPELLER_GEOMETRY_QUATERNION_H_
7
8 #include <ostream>
9
10 #include "impeller/geometry/vector.h"
11
12 namespace impeller {
13
14 struct Quaternion {
15 union {
16 struct {
17 Scalar x = 0.0;
18 Scalar y = 0.0;
19 Scalar z = 0.0;
20 Scalar w = 1.0;
21 };
22 Scalar e[4];
23 };
24
25 Quaternion() {}
26
27 Quaternion(Scalar px, Scalar py, Scalar pz, Scalar pw)
28 : x(px), y(py), z(pz), w(pw) {}
29
30 Quaternion(const Vector3& axis, Scalar angle) {
31 const auto sine = sin(angle * 0.5f);
32 x = sine * axis.x;
33 y = sine * axis.y;
34 z = sine * axis.z;
35 w = cos(angle * 0.5f);
36 }
37
38 Scalar Dot(const Quaternion& q) const {
39 return x * q.x + y * q.y + z * q.z + w * q.w;
40 }
41
42 Scalar Length() const { return sqrt(x * x + y * y + z * z + w * w); }
43
44 Quaternion Normalize() const {
45 auto m = 1.0f / Length();
46 return {x * m, y * m, z * m, w * m};
47 }
48
49 Quaternion Invert() const { return {-x, -y, -z, w}; }
50
51 Quaternion Slerp(const Quaternion& to, double time) const;
52
53 Quaternion operator*(const Quaternion& o) const {
54 return {
55 w * o.x + x * o.w + y * o.z - z * o.y,
56 w * o.y + y * o.w + z * o.x - x * o.z,
57 w * o.z + z * o.w + x * o.y - y * o.x,
58 w * o.w - x * o.x - y * o.y - z * o.z,
59 };
60 }
61
62 Quaternion operator*(Scalar scale) const {
63 return {scale * x, scale * y, scale * z, scale * w};
64 }
65
66 Vector3 operator*(Vector3 vector) const {
67 Vector3 v(x, y, z);
68 return v * v.Dot(vector) * 2 + //
69 vector * (w * w - v.Dot(v)) + //
70 v.Cross(vector) * 2 * w;
71 }
72
73 Quaternion operator+(const Quaternion& o) const {
74 return {x + o.x, y + o.y, z + o.z, w + o.w};
75 }
76
77 Quaternion operator-(const Quaternion& o) const {
78 return {x - o.x, y - o.y, z - o.z, w - o.w};
79 }
80
81 bool operator==(const Quaternion& o) const {
82 return x == o.x && y == o.y && z == o.z && w == o.w;
83 }
84
85 bool operator!=(const Quaternion& o) const {
86 return x != o.x || y != o.y || z != o.z || w != o.w;
87 }
88 };
89
90 } // namespace impeller
91
92 namespace std {
93
94 inline std::ostream& operator<<(std::ostream& out,
95 const impeller::Quaternion& q) {
96 out << "(" << q.x << ", " << q.y << ", " << q.z << ", " << q.w << ")";
97 return out;
98 }
99
100 } // namespace std
101
102 #endif // FLUTTER_IMPELLER_GEOMETRY_QUATERNION_H_
impeller::Vector3::Dot
constexpr Scalar Dot(const Vector3 &other) const
Definition: vector.h:54
impeller::Quaternion::operator*
Vector3 operator*(Vector3 vector) const
Definition: quaternion.h:66
impeller::Quaternion::z
Scalar z
Definition: quaternion.h:19
impeller::Scalar
float Scalar
Definition: scalar.h:18
impeller::Vector3::Cross
constexpr Vector3 Cross(const Vector3 &other) const
Definition: vector.h:62
impeller::Quaternion::e
Scalar e[4]
Definition: quaternion.h:22
impeller::Quaternion::operator+
Quaternion operator+(const Quaternion &o) const
Definition: quaternion.h:73
impeller::Quaternion::Invert
Quaternion Invert() const
Definition: quaternion.h:49
impeller::Quaternion::Quaternion
Quaternion(const Vector3 &axis, Scalar angle)
Definition: quaternion.h:30
impeller::Quaternion::w
Scalar w
Definition: quaternion.h:20
impeller::Quaternion::Quaternion
Quaternion()
Definition: quaternion.h:25
std::operator<<
std::ostream & operator<<(std::ostream &out, const impeller::Color &c)
Definition: color.h:926
impeller::Vector3::x
Scalar x
Definition: vector.h:23
impeller::Quaternion::Quaternion
Quaternion(Scalar px, Scalar py, Scalar pz, Scalar pw)
Definition: quaternion.h:27
impeller::Quaternion::x
Scalar x
Definition: quaternion.h:17
impeller::Quaternion
Definition: quaternion.h:14
impeller::Vector3::z
Scalar z
Definition: vector.h:25
impeller::Quaternion::Slerp
Quaternion Slerp(const Quaternion &to, double time) const
Definition: quaternion.cc:10
impeller::Quaternion::Dot
Scalar Dot(const Quaternion &q) const
Definition: quaternion.h:38
impeller::Vector3::y
Scalar y
Definition: vector.h:24
impeller::Quaternion::operator*
Quaternion operator*(Scalar scale) const
Definition: quaternion.h:62
impeller::Quaternion::Normalize
Quaternion Normalize() const
Definition: quaternion.h:44
vector.h
std
Definition: comparable.h:95
impeller::Quaternion::operator==
bool operator==(const Quaternion &o) const
Definition: quaternion.h:81
impeller::Quaternion::y
Scalar y
Definition: quaternion.h:18
scale
const Scalar scale
Definition: stroke_path_geometry.cc:316
impeller::Quaternion::operator-
Quaternion operator-(const Quaternion &o) const
Definition: quaternion.h:77
impeller::Quaternion::Length
Scalar Length() const
Definition: quaternion.h:42
impeller
Definition: allocation.cc:12
impeller::Quaternion::operator*
Quaternion operator*(const Quaternion &o) const
Definition: quaternion.h:53
impeller::Vector3
Definition: vector.h:20
impeller::Quaternion::operator!=
bool operator!=(const Quaternion &o) const
Definition: quaternion.h:85