wren
Vulkan-based game engine
Toggle main menu visibility
Loading...
Searching...
No Matches
quaternion.hpp
Go to the documentation of this file.
1
#pragma once
2
3
#include "
matrix.hpp
"
4
#include "
vector.hpp
"
5
6
namespace
wren::math
{
7
8
template
<
typename
T>
9
struct
Quaternion
{
10
static
auto
identity
() {
return
Quaternion<T>
({0, 0, 0, 1}); }
11
12
Quaternion
() :
data
({0, 0, 0, 1}) {}
13
Quaternion
(T
x
, T
y
, T
z
, T
w
) :
data
({
x
,
y
,
z
,
w
}) {}
14
15
Quaternion
(
Vec<T, 3>
euler_angles) {
16
const
auto
roll = euler_angles.
data
.at(0);
17
const
auto
pitch = euler_angles.
data
.at(1);
18
const
auto
yaw = euler_angles.
data
.at(2);
19
20
const
auto
cr = std::cos(roll * 0.5f);
21
const
auto
sr = std::sin(roll * 0.5f);
22
const
auto
cp = std::cos(pitch * 0.5f);
23
const
auto
sp = std::sin(pitch * 0.5f);
24
const
auto
cy = std::cos(yaw * 0.5f);
25
const
auto
sy = std::sin(yaw * 0.5f);
26
27
data
.at(0) = sr * cp * cy - cr * sp * sy;
28
data
.at(1) = cr * sp * cy + sr * cp * sy;
29
data
.at(2) = cr * cp * sy - sr * sp * cy;
30
data
.at(3) = cr * cp * cy + sr * sp * sy;
31
}
32
33
[[nodiscard]]
auto
x
()
const
{
return
data
.at(0); }
34
auto
x
(T
x
) {
data
.at(0) =
x
; }
35
[[nodiscard]]
auto
y
()
const
{
return
data
.at(1); }
36
[[nodiscard]]
auto
z
()
const
{
return
data
.at(2); }
37
[[nodiscard]]
auto
w
()
const
{
return
data
.at(3); }
38
39
auto
normalize
() {
data
=
data
.normalized(); }
40
[[nodiscard]]
auto
normalized
()
const
{
41
return
Quaternion
(
data
.normalized());
42
}
43
44
auto
to_mat
() {
45
const
auto
vec =
data
.normalized();
46
const
auto
w
= vec.at(3);
47
const
auto
x
= vec.at(0);
48
const
auto
y
= vec.at(1);
49
const
auto
z
= vec.at(2);
50
// const auto ww = w * w;
51
const
auto
xx =
x
*
x
;
52
const
auto
yy =
y
*
y
;
53
const
auto
zz =
z
*
z
;
54
55
auto
result =
Mat3f::identity
();
56
result.at(0, 0) = 1 - 2 * (yy + zz);
57
result.at(1, 0) = 2 * (
x
*
y
+
w
*
z
);
58
result.at(2, 0) = 2 * (
x
*
z
-
w
*
y
);
59
60
result.at(0, 1) = 2 * (
x
*
y
-
w
*
z
);
61
result.at(1, 1) = 1 - 2 * (xx + zz);
62
result.at(2, 1) = 2 * (
y
*
z
+
w
*
x
);
63
64
result.at(0, 2) = 2 * (
x
*
z
+
w
*
y
);
65
result.at(1, 2) = 2 * (
y
*
z
-
w
*
x
);
66
result.at(2, 2) = 1 - 2 * (xx + yy);
67
68
return
result;
69
}
70
71
auto
operator*
(
const
Vec<T, 3>
& v) {
72
Vec<T, 3>
u(
x
(),
y
(),
z
());
73
T s =
w
();
74
return
2.0f * u.
dot
(v) * u + (s * s - u.
dot
(u)) * v + 2.0f * s * u.
cross
(v);
75
}
76
77
Vec<T, 4>
data
;
78
};
79
80
using
Quaternionf
=
Quaternion<float>
;
81
BOOST_DESCRIBE_STRUCT
(
Quaternionf
, (), (data));
82
83
using
Quaterniond
=
Quaternion<double>
;
84
BOOST_DESCRIBE_STRUCT
(
Quaterniond
, (), (data));
85
86
}
// namespace wren::math
wren::math::Mat< float, 3, 3 >::identity
static constexpr auto identity()
Definition
matrix.hpp:18
matrix.hpp
wren::math
Definition
geometry.hpp:8
wren::math::Quaterniond
Quaternion< double > Quaterniond
Definition
quaternion.hpp:83
wren::math::BOOST_DESCRIBE_STRUCT
BOOST_DESCRIBE_STRUCT(Quaternionf,(),(data))
wren::math::Quaternionf
Quaternion< float > Quaternionf
Definition
quaternion.hpp:80
wren::math::Quaternion
Definition
quaternion.hpp:9
wren::math::Quaternion::Quaternion
Quaternion(Vec< T, 3 > euler_angles)
Definition
quaternion.hpp:15
wren::math::Quaternion::operator*
auto operator*(const Vec< T, 3 > &v)
Definition
quaternion.hpp:71
wren::math::Quaternion::identity
static auto identity()
Definition
quaternion.hpp:10
wren::math::Quaternion::Quaternion
Quaternion()
Definition
quaternion.hpp:12
wren::math::Quaternion::normalized
auto normalized() const
Definition
quaternion.hpp:40
wren::math::Quaternion< float >::w
auto w() const
Definition
quaternion.hpp:37
wren::math::Quaternion::normalize
auto normalize()
Definition
quaternion.hpp:39
wren::math::Quaternion::to_mat
auto to_mat()
Definition
quaternion.hpp:44
wren::math::Quaternion::Quaternion
Quaternion(T x, T y, T z, T w)
Definition
quaternion.hpp:13
wren::math::Quaternion::x
auto x(T x)
Definition
quaternion.hpp:34
wren::math::Quaternion< float >::y
auto y() const
Definition
quaternion.hpp:35
wren::math::Quaternion< float >::z
auto z() const
Definition
quaternion.hpp:36
wren::math::Quaternion< float >::data
Vec< float, 4 > data
Definition
quaternion.hpp:77
wren::math::Quaternion< float >::x
auto x() const
Definition
quaternion.hpp:33
wren::math::Vec
Definition
vector.hpp:10
wren::math::Vec::data
std::array< T, N > data
Definition
vector.hpp:153
wren::math::Vec::dot
constexpr auto dot(const vec_t &other) const
Definition
vector.hpp:76
wren::math::Vec::cross
auto cross(const vec_t other) const
Definition
vector.hpp:83
vector.hpp
wren_math
include
wren
math
quaternion.hpp
Generated by
1.17.0