| 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 #pragma once
6
7 #include <ostream>
8
9 #include "impeller/geometry/vector.h"
10
11 namespace impeller {
12
13 struct Quaternion {
14 union {
15 struct {
16 Scalar x = 0.0;
17 Scalar y = 0.0;
18 Scalar z = 0.0;
19 Scalar w = 1.0;
20 };
21 Scalar e[4];
22 };
23
24 Quaternion() {}
25
26 Quaternion(Scalar px, Scalar py, Scalar pz, Scalar pw)
27 : x(px), y(py), z(pz), w(pw) {}
28
29 Quaternion(const Vector3& axis, Scalar angle) {
30 const auto sine = sin(angle * 0.5f);
31 x = sine * axis.x;
32 y = sine * axis.y;
33 z = sine * axis.z;
34 w = cos(angle * 0.5f);
35 }
36
37 Scalar Dot(const Quaternion& q) const {
38 return x * q.x + y * q.y + z * q.z + w * q.w;
39 }
40
41 Scalar Length() const { return sqrt(x * x + y * y + z * z + w * w); }
42
43 Quaternion Normalize() const {
44 auto m = 1.0f / Length();
45 return {x * m, y * m, z * m, w * m};
46 }
47
48 Quaternion Invert() const { return {-x, -y, -z, w}; }
49
50 Quaternion Slerp(const Quaternion& to, double time) const;
51
52 Quaternion operator*(const Quaternion& o) const {
53 return {
54 w * o.x + x * o.w + y * o.z - z * o.y,
55 w * o.y + y * o.w + z * o.x - x * o.z,
56 w * o.z + z * o.w + x * o.y - y * o.x,
57 w * o.w - x * o.x - y * o.y - z * o.z,
58 };
59 }
60
61 Quaternion operator*(Scalar scale) const {
62 return {scale * x, scale * y, scale * z, scale * w};
63 }
64
65 Vector3 operator*(Vector3 vector) const {
66 Vector3 v(x, y, z);
67 return v * v.Dot(vector) * 2 + //
68 vector * (w * w - v.Dot(v)) + //
69 v.Cross(vector) * 2 * w;
70 }
71
72 Quaternion operator+(const Quaternion& o) const {
73 return {x + o.x, y + o.y, z + o.z, w + o.w};
74 }
75
76 Quaternion operator-(const Quaternion& o) const {
77 return {x - o.x, y - o.y, z - o.z, w - o.w};
78 }
79
80 bool operator==(const Quaternion& o) const {
81 return x == o.x && y == o.y && z == o.z && w == o.w;
82 }
83
84 bool operator!=(const Quaternion& o) const {
85 return x != o.x || y != o.y || z != o.z || w != o.w;
86 }
87 };
88
89 } // namespace impeller
90
91 namespace std {
92
93 inline std::ostream& operator<<(std::ostream& out,
94 const impeller::Quaternion& q) {
95 out << "(" << q.x << ", " << q.y << ", " << q.z << ", " << q.w << ")";
96 return out;
97 }
98
99 } // namespace std
impeller::Vector3::Dot
constexpr Scalar Dot(const Vector3 &other) const
Definition: vector.h:51
impeller::Quaternion::operator*
Vector3 operator*(Vector3 vector) const
Definition: quaternion.h:65
impeller::Quaternion::z
Scalar z
Definition: quaternion.h:18
impeller::Scalar
float Scalar
Definition: scalar.h:15
impeller::Vector3::Cross
constexpr Vector3 Cross(const Vector3 &other) const
Definition: vector.h:59
impeller::Quaternion::e
Scalar e[4]
Definition: quaternion.h:21
impeller::Quaternion::operator+
Quaternion operator+(const Quaternion &o) const
Definition: quaternion.h:72
impeller::Quaternion::Invert
Quaternion Invert() const
Definition: quaternion.h:48
impeller::Quaternion::Quaternion
Quaternion(const Vector3 &axis, Scalar angle)
Definition: quaternion.h:29
impeller::Quaternion::w
Scalar w
Definition: quaternion.h:19
impeller::Quaternion::Quaternion
Quaternion()
Definition: quaternion.h:24
std::operator<<
std::ostream & operator<<(std::ostream &out, const impeller::Color &c)
Definition: color.h:945
impeller::Vector3::x
Scalar x
Definition: vector.h:20
impeller::Quaternion::Quaternion
Quaternion(Scalar px, Scalar py, Scalar pz, Scalar pw)
Definition: quaternion.h:26
impeller::Quaternion::x
Scalar x
Definition: quaternion.h:16
impeller::Quaternion
Definition: quaternion.h:13
impeller::Vector3::z
Scalar z
Definition: vector.h:22
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:37
impeller::Vector3::y
Scalar y
Definition: vector.h:21
impeller::Quaternion::operator*
Quaternion operator*(Scalar scale) const
Definition: quaternion.h:61
impeller::Quaternion::Normalize
Quaternion Normalize() const
Definition: quaternion.h:43
vector.h
std
Definition: comparable.h:98
impeller::Quaternion::operator==
bool operator==(const Quaternion &o) const
Definition: quaternion.h:80
impeller::Quaternion::y
Scalar y
Definition: quaternion.h:17
impeller::Quaternion::operator-
Quaternion operator-(const Quaternion &o) const
Definition: quaternion.h:76
impeller::Quaternion::Length
Scalar Length() const
Definition: quaternion.h:41
impeller
Definition: aiks_context.cc:10
impeller::Quaternion::operator*
Quaternion operator*(const Quaternion &o) const
Definition: quaternion.h:52
impeller::Vector3
Definition: vector.h:17
impeller::Quaternion::operator!=
bool operator!=(const Quaternion &o) const
Definition: quaternion.h:84