Euphoria
quat.h
Go to the documentation of this file.
1#pragma once
2
3#include "eu/base/vec3.h"
4#include "eu/base/numeric.h"
5#include "eu/base/axisangle.h"
6#include "eu/base/angle.h"
7#include "eu/base/mat4.h"
8
9
10namespace eu
11{
18 struct Q
19 {
20 float w;
21 float x;
22 float y;
23 float z;
24
25 constexpr Q(float aw, const v3& v)
26 : w(aw)
27 , x(v.x)
28 , y(v.y)
29 , z(v.z)
30 {
31 }
32
33 constexpr Q(const float* arr)
34 : w(arr[0])
35 , x(arr[1])
36 , y(arr[2])
37 , z(arr[3])
38 {
39 }
40
42 [[nodiscard]] static Q from(const AA& aa);
43
45 [[nodiscard]] static Q from(const Ypr& ypr);
46
48 [[nodiscard]] static Q from_to(const Q& from, const Q& to);
49
52 [[nodiscard]] static std::optional<Q> look_at(const v3& from, const v3& to, const n3& up);
53
55 [[nodiscard]] static Q look_in_direction(const n3& dir, const n3& up);
56
59 void normalize();
60
62 [[nodiscard]] Q then_get_rotated(const Q& q) const;
63
65 [[nodiscard]] n3 get_rotated(const n3& v) const;
66
69 [[nodiscard]] Q get_negated() const;
70
72 [[nodiscard]] v3 get_vec_part() const;
73
75 [[nodiscard]] Q get_conjugate() const;
76
80 [[nodiscard]] Q get_inverse() const;
81
84 [[nodiscard]] float get_length() const;
85
88 [[nodiscard]] Q get_normalized() const;
89
91 [[nodiscard]] n3 get_local_in() const;
92
94 [[nodiscard]] n3 get_local_out() const;
95
97 [[nodiscard]] n3 get_local_right() const;
98
100 [[nodiscard]] n3 get_local_left() const;
101
103 [[nodiscard]] n3 get_local_up() const;
104
106 [[nodiscard]] n3 get_local_down() const;
107
108 void operator*=(float rhs);
109 void operator*=(const Q& rhs);
110
114 static Q nlerp(const Q& f, float scale, const Q& t);
115
119 static Q slerp_fast(const Q& qa, float t, const Q& qb);
120
125 static Q slerp(const Q& from, float scale, const Q& to);
126 };
127
129 constexpr Q q_identity = Q(1, v3(0, 0, 0));
130
131 float dot(const Q& lhs, const Q& rhs);
132
134 std::string string_from(const Q& v);
135
136 Q operator*(const Q& lhs, const Q& rhs);
137 Q operator*(float scale, const Q& q);
138 Q operator*(const Q& q, float scale);
139
142}
143
144ADD_DEFAULT_FORMATTER(eu::Q, std::string, eu::string_from);
145
#define ADD_CATCH_FORMATTER_DEF(TYPE)
Definition pch.public.h:22
constexpr Q q_identity
The identity quaternion.
Definition quat.h:129
float dot(const Q &lhs, const Q &rhs)
An operator*(const An &lhs, float rhs)
std::string string_from(const An &a)
Definition assert.h:118
Axis + Angle.
Definition axisangle.h:19
A quaternion representing a rotation in 3d.
Definition quat.h:19
static Q from_to(const Q &from, const Q &to)
Create a quaternion going from from to to.
void operator*=(float rhs)
n3 get_local_up() const
Get the local up vector.
float z
Definition quat.h:23
n3 get_local_right() const
Get the local right vector.
Q get_negated() const
Gets the negated quaternion.
constexpr Q(float aw, const v3 &v)
Definition quat.h:25
float get_length() const
Gets the length of the quaternion.
v3 get_vec_part() const
Get the [x,y,z] part as a regular 3d vector.
float y
Definition quat.h:22
void operator*=(const Q &rhs)
float w
Definition quat.h:20
void normalize()
Normalize the quaternion.
Q then_get_rotated(const Q &q) const
Return the passed rotation composed after the current rotation.
static Q slerp(const Q &from, float scale, const Q &to)
Shortest spherical lerp between 2 quaternions.
constexpr Q(const float *arr)
Definition quat.h:33
Q get_conjugate() const
Returns the conjugate of the quaternion.
n3 get_local_down() const
Get the local down vector.
static Q look_in_direction(const n3 &dir, const n3 &up)
Creates a look-at quaternion looking in a direction.
n3 get_rotated(const n3 &v) const
Rotate a unit vector according to the quaternion.
n3 get_local_left() const
Get the local left vector.
static Q from(const AA &aa)
Create a quaternion from an axis angle.
static Q nlerp(const Q &f, float scale, const Q &t)
Normalized lerp between 2 quaternions This will result in a non-linear rotation.
n3 get_local_out() const
Get the local out vector.
float x
Definition quat.h:21
static Q slerp_fast(const Q &qa, float t, const Q &qb)
Spherical lerp between 2 quaternions.
n3 get_local_in() const
Get the local in vector.
Q get_inverse() const
Gets the inverse rotation.
Q get_normalized() const
Return a normalized quaternion If it can't be normalized, the identity is returned.
static std::optional< Q > look_at(const v3 &from, const v3 &to, const n3 &up)
Creates a look-at quaternion from 2 positions.
static Q from(const Ypr &ypr)
Create a quaternion from a yaw-pitch-roll.
yaw + pitch + roll
Definition axisangle.h:33
a 3d unit (vector)
Definition vec3.h:94
a 3d vector
Definition vec3.h:27