2021-10-19 13:48:19 +00:00
|
|
|
#ifndef _QUAT_H
|
|
|
|
#define _QUAT_H
|
|
|
|
|
|
|
|
#include "types.h"
|
|
|
|
#include "Vector3.h"
|
2022-08-12 15:59:15 +00:00
|
|
|
#include "Matrixf.h"
|
2022-11-26 20:16:54 +00:00
|
|
|
#include "Matrix3f.h"
|
2021-10-19 13:48:19 +00:00
|
|
|
|
2024-01-25 12:02:29 +00:00
|
|
|
/**
|
|
|
|
* @struct RPY
|
|
|
|
* @brief Represents roll, pitch, and yaw angles in radians.
|
|
|
|
*/
|
2023-10-27 13:27:31 +00:00
|
|
|
struct RPY {
|
|
|
|
f32 mRoll; // _00
|
|
|
|
f32 mPitch; // _04
|
|
|
|
f32 mYaw; // _08
|
|
|
|
};
|
|
|
|
|
2024-01-25 12:02:29 +00:00
|
|
|
/**
|
|
|
|
* @brief Represents a quaternion.
|
|
|
|
*/
|
2021-10-19 13:48:19 +00:00
|
|
|
struct Quat {
|
2024-01-25 12:02:29 +00:00
|
|
|
/**
|
|
|
|
* @brief Default constructor.
|
|
|
|
*/
|
2022-08-07 04:00:18 +00:00
|
|
|
Quat();
|
2023-10-08 14:00:47 +00:00
|
|
|
|
2024-01-25 12:02:29 +00:00
|
|
|
/**
|
|
|
|
* @brief Copy constructor.
|
|
|
|
* @param other The quaternion to copy from.
|
|
|
|
*/
|
|
|
|
Quat(Quat& other);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Constructor that initializes the quaternion with a scalar and a vector.
|
|
|
|
* @param w The scalar component of the quaternion.
|
|
|
|
* @param xyz The vector component of the quaternion.
|
|
|
|
*/
|
|
|
|
Quat(f32 w, Vector3f xyz);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Constructor that initializes the quaternion from roll, pitch, and yaw angles.
|
|
|
|
* @param rpy The roll, pitch, and yaw angles.
|
|
|
|
* @note This constructor is unused and inlined.
|
|
|
|
*/
|
|
|
|
Quat(RPY& rpy);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Sets the quaternion using a vector.
|
|
|
|
* @param xyz The vector to set the quaternion.
|
|
|
|
*/
|
|
|
|
void set(Vector3f& xyz);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Sets the values of the quaternion.
|
|
|
|
*
|
|
|
|
* @param w The w component of the quaternion.
|
|
|
|
* @param x The x component of the quaternion.
|
|
|
|
* @param y The y component of the quaternion.
|
|
|
|
* @param z The z component of the quaternion.
|
|
|
|
*/
|
|
|
|
void set(f32 w, f32 x, f32 y, f32 z);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Sets the quaternion using a scalar and a vector.
|
|
|
|
* @param w The scalar component of the quaternion.
|
|
|
|
* @param xyz The vector component of the quaternion.
|
|
|
|
*/
|
|
|
|
inline void set(f32 w, Vector3f& xyz);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Sets the quaternion using roll, pitch, and yaw angles.
|
|
|
|
* @param rpy The roll, pitch, and yaw angles.
|
|
|
|
*/
|
2023-10-27 13:27:31 +00:00
|
|
|
void set(RPY& rpy);
|
2024-01-25 12:02:29 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Normalizes the quaternion.
|
|
|
|
* @note Inlined, and is currently a stub.
|
|
|
|
*/
|
2024-04-21 07:40:26 +00:00
|
|
|
f32 norm();
|
2024-01-25 12:02:29 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Computes the conjugate of the quaternion.
|
|
|
|
* @note Inlined, and is currently a stub.
|
|
|
|
*/
|
2022-11-26 20:16:54 +00:00
|
|
|
void conjugate();
|
2024-01-25 12:02:29 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Computes the inverse of the quaternion.
|
|
|
|
* @return The inverse quaternion.
|
|
|
|
*/
|
2023-10-08 14:00:47 +00:00
|
|
|
Quat inverse();
|
2024-01-25 12:02:29 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Normalizes the quaternion.
|
|
|
|
* @note This function is an alias for the `norm()` function.
|
|
|
|
*/
|
2022-08-07 04:00:18 +00:00
|
|
|
void normalise();
|
2023-10-08 14:00:47 +00:00
|
|
|
|
2024-01-25 12:02:29 +00:00
|
|
|
/**
|
|
|
|
* @brief Performs spherical linear interpolation between two quaternions.
|
|
|
|
* @param a The first quaternion.
|
|
|
|
* @param t The interpolation factor.
|
|
|
|
* @param b The second quaternion.
|
|
|
|
*/
|
|
|
|
void slerp(Quat& a, f32 t, Quat& b);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Sets the quaternion to represent a rotation around an axis.
|
|
|
|
* @param axis The axis of rotation.
|
2024-05-04 19:48:13 +00:00
|
|
|
* @param angle The angle of rotation, in degrees
|
|
|
|
* @note Inline, and is currently a stub for match (not for modding).
|
2024-01-25 12:02:29 +00:00
|
|
|
*/
|
|
|
|
void setAxisRotation(Vector3f& axis, f32 angle);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Converts the quaternion to a rotation matrix.
|
|
|
|
* @param out The output rotation matrix.
|
|
|
|
*/
|
|
|
|
void toMatrix(Matrix3f& out);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Converts a rotation matrix to a quaternion.
|
|
|
|
* @param out The input rotation matrix.
|
|
|
|
*/
|
|
|
|
void fromMatrixf(Matrixf& out);
|
|
|
|
|
2024-05-21 16:38:48 +00:00
|
|
|
inline const f32 dot(Quat& q1) const { return (w * q1.w) + ((v.z * q1.v.z) + ((v.x * q1.v.x) + (v.y * q1.v.y))); }
|
2024-05-21 15:25:23 +00:00
|
|
|
|
2024-01-25 12:02:29 +00:00
|
|
|
/**
|
|
|
|
* @brief Multiplies the quaternion by a scalar.
|
|
|
|
* @param other The scalar to multiply by.
|
|
|
|
* @return The resulting quaternion.
|
|
|
|
*/
|
2024-04-21 07:40:26 +00:00
|
|
|
inline Quat operator*(f32 scale)
|
2023-10-27 13:27:31 +00:00
|
|
|
{
|
|
|
|
Quat result;
|
2024-04-21 07:40:26 +00:00
|
|
|
result.w = scale * w;
|
|
|
|
result.v.x = scale * v.x;
|
|
|
|
result.v.y = scale * v.y;
|
|
|
|
result.v.z = scale * v.z;
|
2023-10-27 13:27:31 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2024-06-05 11:38:15 +00:00
|
|
|
inline Quat operator+(const Quat& other)
|
2023-10-08 14:00:47 +00:00
|
|
|
{
|
2024-04-21 07:40:26 +00:00
|
|
|
Quat result;
|
|
|
|
result.w = w + other.w;
|
|
|
|
result.v = v + other.v;
|
|
|
|
return result;
|
2023-10-08 14:00:47 +00:00
|
|
|
}
|
|
|
|
|
2024-04-21 07:40:26 +00:00
|
|
|
f32 w; // _00
|
|
|
|
Vector3f v; // _04
|
2021-10-19 13:48:19 +00:00
|
|
|
};
|
|
|
|
|
2024-04-21 07:40:26 +00:00
|
|
|
inline Quat operator*(Quat& q1, Quat& q2)
|
|
|
|
{
|
|
|
|
Quat result;
|
|
|
|
result.w = q1.w * q2.w - q1.v.dot(q2.v);
|
|
|
|
result.v = q1.v.cross(q2.v) + q2.v * q1.w + q1.v * q2.w;
|
|
|
|
return Quat(result.w, result.v);
|
|
|
|
}
|
2023-10-27 13:27:31 +00:00
|
|
|
|
2021-10-19 13:48:19 +00:00
|
|
|
#endif
|