system
The vocabulary types everything else shares
Loading...
Searching...
No Matches
Quaternion.hpp
Go to the documentation of this file.
1
11#pragma once
12
13#include "Vector3.hpp"
14
22template <typename T>
24
25 public:
26 Quaternion() : w(1), x(0), y(0), z(0) {}
27 Quaternion(T w, T x, T y, T z) : w(w), x(x), y(y), z(z) {}
28
29 static Quaternion identity() { return Quaternion(1, 0, 0, 0); }
30
33 const Vector3<T> half = {x / 2, y / 2, z / 2};
34
35 q.w = std::cos(half.x) * std::cos(half.y) * std::cos(half.z) + std::sin(half.x) * std::sin(half.y) * std::sin(half.z);
36 q.x = std::sin(half.x) * std::cos(half.y) * std::cos(half.z) - std::cos(half.x) * std::sin(half.y) * std::sin(half.z);
37 q.y = std::cos(half.x) * std::sin(half.y) * std::cos(half.z) + std::sin(half.x) * std::cos(half.y) * std::sin(half.z);
38 q.z = std::cos(half.x) * std::cos(half.y) * std::sin(half.z) - std::sin(half.x) * std::sin(half.y) * std::cos(half.z);
39 return q;
40 }
41
50 const T half = angle / 2;
51
52 q.w = std::cos(half);
53 q.x = axis.x * std::sin(half);
54 q.y = axis.y * std::sin(half);
55 q.z = axis.z * std::sin(half);
56 return q;
57 }
58
63 const Vector3<T> referenceUp = {0, 1, 0}) {
65
66 const Vector3<T> u1 = v1.normalized();
67 const Vector3<T> u2 = v2.normalized();
68 const T dot = u1.dot(u2);
69
70 //todo: check if this check is indeed working well with 180 rotation
71 if (dot > T(0.999999))
72 return Quaternion(1, 0, 0, 0);
73
74 if (dot < T(-0.999999)) {
75 Vector3<T> axis = Vector3<T>{1, 0, 0}.cross(u1);
76
77 if (axis.x == 0 && axis.y == 0 && axis.z == 0)
78 axis = Vector3<T>{0, 1, 0}.cross(u1);
79
80 Quaternion q(0, axis.x, axis.y, axis.z);
81
82 q.normalize();
83 return q;
84 }
85
86 const Vector3<T> axis = u1.cross(u2);
87 const T u1Length = u1.magnitude();
88 const T u2Length = u2.magnitude();
89 const T w = std::sqrt((u1Length * u1Length) * (u2Length * u2Length)) + dot;
90 Quaternion q(w, axis.x, axis.y, axis.z);
91
92 /* La construction rend un quaternion de norme quelconque. Non
93 * normalise, rotate() mettrait le point a l'echelle |q|^2. */
94 q.normalize();
95 return q;
96 }
97
98 Quaternion conjugate() const { return Quaternion(w, -x, -y, -z); }
99
101 void normalize() {
102 const T magnitude = std::sqrt(w * w + x * x + y * y + z * z);
103
104 if (magnitude == 0) {
105 w = 1; x = 0; y = 0; z = 0;
106 return;
107 }
108 w /= magnitude; x /= magnitude; y /= magnitude; z /= magnitude;
109 }
110
117 void enforceSign() {
118 if (w < 0) {
119 w = -w; x = -x; y = -y; z = -z;
120 }
121 }
122
124 Vector3<T> rotate(Vector3<T> point, Vector3<T> center = {0, 0, 0}) const {
125 const Quaternion p(0, point.x - center.x, point.y - center.y, point.z - center.z);
126 const Quaternion q = (*this * p) * conjugate();
127
128 return {q.x + center.x, q.y + center.y, q.z + center.z};
129 }
130
133 return Quaternion(
134 w * other.w - x * other.x - y * other.y - z * other.z,
135 w * other.x + x * other.w + y * other.z - z * other.y,
136 w * other.y - x * other.z + y * other.w + z * other.x,
137 w * other.z + x * other.y - y * other.x + z * other.w
138 );
139 }
140
141 T w, x, y, z;
142};
143
145
Une rotation dans l'espace, parametree par son scalaire.
Definition Quaternion.hpp:23
void enforceSign()
Choisit la representation a w positif.
Definition Quaternion.hpp:117
T y
Definition Quaternion.hpp:141
Quaternion(T w, T x, T y, T z)
Definition Quaternion.hpp:27
static Quaternion fromEulerAngles(T x, T y, T z)
Definition Quaternion.hpp:31
static Quaternion fromAxisAngle(T angle, Vector3< T > axis)
Une rotation d'un angle autour d'un axe.
Definition Quaternion.hpp:48
static Quaternion identity()
Definition Quaternion.hpp:29
Quaternion conjugate() const
Definition Quaternion.hpp:98
T x
Definition Quaternion.hpp:141
Quaternion operator*(const Quaternion &other) const
Compose deux rotations. L'ordre compte.
Definition Quaternion.hpp:132
T w
Definition Quaternion.hpp:141
void normalize()
Le ramene sur la sphere unite. Un quaternion nul devient l'identite.
Definition Quaternion.hpp:101
static Quaternion fromVectors(const Vector3< T > &v1, const Vector3< T > &v2, const Vector3< T > referenceUp={0, 1, 0})
La rotation qui amene v1 sur v2.
Definition Quaternion.hpp:62
Quaternion()
Definition Quaternion.hpp:26
Vector3< T > rotate(Vector3< T > point, Vector3< T > center={0, 0, 0}) const
Tourne un point autour d'un centre.
Definition Quaternion.hpp:124
T z
Definition Quaternion.hpp:141
constexpr T epsilon_v
Definition Constants.hpp:32
Definition Vector3.hpp:20
Vector3 normalized() const
Definition Vector3.hpp:94
T x
Definition Vector3.hpp:21
std::conditional< std::is_floating_point< T >::value, T, double >::type magnitude() const
Definition Vector3.hpp:78
T y
Definition Vector3.hpp:21
Vector3 cross(const Vector3 &other) const
Definition Vector3.hpp:65
T dot(const Vector3 &other) const
Definition Vector3.hpp:62
T z
Definition Vector3.hpp:21