GCC Code Coverage Report


./
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
7 of 13, 0 excluded
53.8%
Functions:
2 of 3, 0 excluded
66.7%
Branches:
2 of 4, 0 excluded
50.0%

libs/base/src/eu/base/quat.h
Line Branch Exec Source
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
10 namespace eu
11 {
12 /** \addtogroup math
13 * @{
14 */
15
16
17 /// A quaternion representing a rotation in 3d.
18 struct Q
19 {
20 float w;
21 float x;
22 float y;
23 float z;
24
25 3225 constexpr Q(float aw, const v3& v)
26 3225 : w(aw)
27 3225 , x(v.x)
28 3225 , y(v.y)
29 3225 , z(v.z)
30 {
31 3225 }
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
41 /// Create a quaternion from an axis angle
42 [[nodiscard]] static Q from(const AA& aa);
43
44 /// Create a quaternion from a yaw-pitch-roll
45 [[nodiscard]] static Q from(const Ypr& ypr);
46 [[nodiscard]] static Q from_fast(const Ypr& ypr);
47
48 /// Create a quaternion going from `from` to `to`.
49 [[nodiscard]] static Q from_to(const Q& from, const Q& to);
50
51 /// Creates a look-at quaternion from 2 positions.
52 /// Standing at `from` and up is `up`, the result will be a quaternion looking at `to`
53 [[nodiscard]] static std::optional<Q> look_at(const v3& from, const v3& to, const n3& up);
54
55 /// Creates a look-at quaternion looking in a direction.
56 [[nodiscard]] static Q look_in_direction(const n3& dir, const n3& up);
57
58 /// Normalize the quaternion.
59 /// If it can't be normalized, it is set to the identity.
60 void normalize();
61
62 /// Return the passed rotation composed after the current rotation.
63 [[nodiscard]] Q then_get_rotated(const Q& q) const;
64
65 /// Rotate a unit vector according to the quaternion
66 [[nodiscard]] n3 get_rotated(const n3& v) const;
67
68 /// Gets the negated quaternion.
69 /// The negated represents the same rotation
70 [[nodiscard]] Q get_negated() const;
71
72 /// Get the `[x,y,z]` part as a regular 3d vector.
73 [[nodiscard]] v3 get_vec_part() const;
74
75 /// Returns the conjugate of the quaternion.
76 [[nodiscard]] Q get_conjugate() const;
77
78 /// Gets the inverse rotation.
79 /// Implemented as a conjugate with assert that the quaternion is normalized.
80 /// @see \ref get_conjugate()
81 [[nodiscard]] Q get_inverse() const;
82
83 /// Gets the length of the quaternion.
84 /// Since the quaternion should be a unit, this should always be `1`
85 [[nodiscard]] float get_length() const;
86
87 /// Return a normalized quaternion
88 /// If it can't be normalized, the identity is returned
89 [[nodiscard]] Q get_normalized() const;
90
91 /// Get the local in vector.
92 [[nodiscard]] n3 get_local_in() const;
93
94 /// Get the local out vector.
95 [[nodiscard]] n3 get_local_out() const;
96
97 /// Get the local right vector.
98 [[nodiscard]] n3 get_local_right() const;
99
100 /// Get the local left vector.
101 [[nodiscard]] n3 get_local_left() const;
102
103 /// Get the local up vector.
104 [[nodiscard]] n3 get_local_up() const;
105
106 /// Get the local down vector.
107 [[nodiscard]] n3 get_local_down() const;
108
109 void operator*=(float rhs);
110 void operator*=(const Q& rhs);
111
112 /// Normalized lerp between 2 quaternions
113 /// This will result in a non-linear rotation
114 /// @see \ref slerp()
115 static Q nlerp(const Q& f, float scale, const Q& t);
116
117 /// Spherical lerp between 2 quaternions.
118 /// Will take the longer route sometimes but is technically faster.
119 /// @see \ref nlerp()
120 static Q slerp_fast(const Q& qa, float t, const Q& qb);
121
122 /// Shortest spherical lerp between 2 quaternions.
123 /// Has extra logic to take the shortest route.
124 /// @see \ref slerp_fast()
125 /// @see \ref nlerp()
126 static Q slerp(const Q& from, float scale, const Q& to);
127 };
128
129 /// The identity quaternion.
130 constexpr Q q_identity = Q(1, v3(0, 0, 0));
131
132 float dot(const Q& lhs, const Q& rhs);
133
134 /// Converts a quaternion to string, prefer fmt.
135 std::string string_from(const Q& v);
136
137 Q operator*(const Q& lhs, const Q& rhs);
138 Q operator*(float scale, const Q& q);
139 Q operator*(const Q& q, float scale);
140
141 /** @}*/
142 ADD_CATCH_FORMATTER_DEF(Q)
143 }
144
145
2/4
✓ Branch 2 → 3 taken 1 time.
✗ Branch 2 → 11 not taken.
✓ Branch 4 → 5 taken 1 time.
✗ Branch 4 → 9 not taken.
1 ADD_DEFAULT_FORMATTER(eu::Q, std::string, eu::string_from);
146
147