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 
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 
26 
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 
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_
float Scalar
Definition: scalar.h:19
Definition: comparable.h:95
std::ostream & operator<<(std::ostream &out, const impeller::Arc &a)
Definition: arc.h:141
Quaternion operator+(const Quaternion &o) const
Definition: quaternion.h:73
Scalar Length() const
Definition: quaternion.h:42
Quaternion operator*(const Quaternion &o) const
Definition: quaternion.h:53
bool operator==(const Quaternion &o) const
Definition: quaternion.h:81
Quaternion Slerp(const Quaternion &to, double time) const
Definition: quaternion.cc:10
Vector3 operator*(Vector3 vector) const
Definition: quaternion.h:66
Quaternion Invert() const
Definition: quaternion.h:49
Quaternion(Scalar px, Scalar py, Scalar pz, Scalar pw)
Definition: quaternion.h:27
Quaternion(const Vector3 &axis, Scalar angle)
Definition: quaternion.h:30
Quaternion Normalize() const
Definition: quaternion.h:44
bool operator!=(const Quaternion &o) const
Definition: quaternion.h:85
Quaternion operator*(Scalar scale) const
Definition: quaternion.h:62
Quaternion operator-(const Quaternion &o) const
Definition: quaternion.h:77
Scalar Dot(const Quaternion &q) const
Definition: quaternion.h:38
constexpr Vector3 Cross(const Vector3 &other) const
Definition: vector.h:62
constexpr Scalar Dot(const Vector3 &other) const
Definition: vector.h:54