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