Euphoria
quat.h
Go to the documentation of this file.
1#pragma once
2
3#include "base/vec3.h"
4#include "base/numeric.h"
5#include "base/axisangle.h"
6#include "base/angle.h"
7#include "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
34 [[nodiscard]] static Q from(const AA& aa);
35
37 [[nodiscard]] static Q from(const Ypr& ypr);
38
40 [[nodiscard]] static Q from_to(const Q& from, const Q& to);
41
44 [[nodiscard]] static std::optional<Q> look_at(const v3& from, const v3& to, const n3& up);
45
47 [[nodiscard]] static Q look_in_direction(const n3& dir, const n3& up);
48
51 void normalize();
52
54 [[nodiscard]] Q then_get_rotated(const Q& q) const;
55
57 [[nodiscard]] n3 get_rotated(const n3& v) const;
58
61 [[nodiscard]] Q get_negated() const;
62
64 [[nodiscard]] v3 get_vec_part() const;
65
67 [[nodiscard]] Q get_conjugate() const;
68
72 [[nodiscard]] Q get_inverse() const;
73
76 [[nodiscard]] float get_length() const;
77
80 [[nodiscard]] Q get_normalized() const;
81
83 [[nodiscard]] n3 get_local_in() const;
84
86 [[nodiscard]] n3 get_local_out() const;
87
89 [[nodiscard]] n3 get_local_right() const;
90
92 [[nodiscard]] n3 get_local_left() const;
93
95 [[nodiscard]] n3 get_local_up() const;
96
98 [[nodiscard]] n3 get_local_down() const;
99
100 void operator*=(float rhs);
101 void operator*=(const Q& rhs);
102
106 static Q nlerp(const Q& f, float scale, const Q& t);
107
111 static Q slerp_fast(const Q& qa, float t, const Q& qb);
112
117 static Q slerp(const Q& from, float scale, const Q& to);
118 };
119
121 constexpr Q q_identity = Q(1, v3(0, 0, 0));
122
123 float dot(const Q& lhs, const Q& rhs);
124
126 std::string string_from(const Q& v);
127
128 Q operator*(const Q& lhs, const Q& rhs);
129 Q operator*(float scale, const Q& q);
130 Q operator*(const Q& q, float scale);
131
134}
135
136ADD_DEFAULT_FORMATTER(eu::Q, std::string, eu::string_from);
137
#define ADD_CATCH_FORMATTER_DEF(TYPE)
Definition pch.public.h:22
constexpr Q q_identity
The identity quaternion.
Definition quat.h:121
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:109
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.
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:92
a 3d vector
Definition vec3.h:27