Added Quaternion constructors, SetFrom(AxisAngle)
Some checks failed
Build Docs With Doxygen / Explore-Gitea-Actions (push) Has been cancelled
Some checks failed
Build Docs With Doxygen / Explore-Gitea-Actions (push) Has been cancelled
This commit is contained in:
@@ -1,5 +1,5 @@
|
|||||||
# Josh's 3D Math Library - J3ML
|
# Josh's 3D Math Library - J3ML
|
||||||
===============================
|
|
||||||
|
|
||||||
Yet Another C++ Math Standard
|
Yet Another C++ Math Standard
|
||||||
|
|
||||||
|
@@ -11,6 +11,7 @@ namespace J3ML::LinearAlgebra
|
|||||||
/// Transitional datatype, not useful for internal representation of rotation
|
/// Transitional datatype, not useful for internal representation of rotation
|
||||||
/// But has uses for conversion and manipulation.
|
/// But has uses for conversion and manipulation.
|
||||||
class AxisAngle {
|
class AxisAngle {
|
||||||
|
public:
|
||||||
Vector3 axis;
|
Vector3 axis;
|
||||||
float angle;
|
float angle;
|
||||||
public:
|
public:
|
||||||
|
@@ -38,10 +38,13 @@ namespace J3ML::LinearAlgebra
|
|||||||
//void Inverse();
|
//void Inverse();
|
||||||
|
|
||||||
explicit Quaternion(Vector4 vector4);
|
explicit Quaternion(Vector4 vector4);
|
||||||
|
explicit Quaternion(const EulerAngle& angle);
|
||||||
|
explicit Quaternion(const AxisAngle& angle);
|
||||||
|
|
||||||
void SetFromAxisAngle(const Vector3 &vector3, float between);
|
void SetFromAxisAngle(const Vector3 &vector3, float between);
|
||||||
|
|
||||||
void SetFromAxisAngle(const Vector4 &vector4, float between);
|
void SetFromAxisAngle(const Vector4 &vector4, float between);
|
||||||
|
void SetFrom(const AxisAngle& angle);
|
||||||
|
|
||||||
Quaternion Inverse() const;
|
Quaternion Inverse() const;
|
||||||
|
|
||||||
@@ -58,6 +61,22 @@ namespace J3ML::LinearAlgebra
|
|||||||
|
|
||||||
float GetAngle() const;
|
float GetAngle() const;
|
||||||
|
|
||||||
|
EulerAngle ToEulerAngle() const
|
||||||
|
{
|
||||||
|
// roll (x-axis rotation)
|
||||||
|
double sinr_cosp = 2 * (w * x + y * z);
|
||||||
|
double cosr_cosp = 1 - 2 * (x * x + y * y);
|
||||||
|
|
||||||
|
// pitch (y-axis rotation)
|
||||||
|
double sinp = std::sqrt(1 + 2 * (w * y - x * z));
|
||||||
|
double
|
||||||
|
|
||||||
|
return {
|
||||||
|
.roll = std::atan2(sinr_cosp, cosr_cosp),
|
||||||
|
.pitch =
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
Matrix3x3 ToMatrix3x3() const;
|
Matrix3x3 ToMatrix3x3() const;
|
||||||
|
|
||||||
|
@@ -201,4 +201,35 @@ namespace J3ML::LinearAlgebra {
|
|||||||
Quaternion::Quaternion(const Vector4 &rotationAxis, float rotationAngleBetween) {
|
Quaternion::Quaternion(const Vector4 &rotationAxis, float rotationAngleBetween) {
|
||||||
SetFromAxisAngle(rotationAxis, rotationAngleBetween);
|
SetFromAxisAngle(rotationAxis, rotationAngleBetween);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Quaternion::Quaternion(const AxisAngle &angle) {
|
||||||
|
double s = std::sin(angle.angle / 2);
|
||||||
|
x = angle.axis.x * s;
|
||||||
|
y = angle.axis.y * s;
|
||||||
|
z = angle.axis.z * s;
|
||||||
|
w = std::cos(angle.angle / 2);
|
||||||
|
}
|
||||||
|
|
||||||
|
Quaternion::Quaternion(const EulerAngle &angle) {
|
||||||
|
// Abbreviations for the various angular functions
|
||||||
|
double cr = std::cos(angle.roll * 0.5);
|
||||||
|
double sr = std::sin(angle.roll * 0.5);
|
||||||
|
double cp = std::cos(angle.pitch * 0.5);
|
||||||
|
double sp = std::sin(angle.pitch * 0.5);
|
||||||
|
double cy = std::cos(angle.yaw * 0.5);
|
||||||
|
double sy = std::sin(angle.yaw * 0.5);
|
||||||
|
|
||||||
|
w = cr * cp * cy + sr * sp * sy;
|
||||||
|
x = sr * cp * cy - cr * sp * sy;
|
||||||
|
y = cr * sp * cy + sr * cp * sy;
|
||||||
|
z = cr * cp * sy - sr * sp * cy;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Quaternion::SetFrom(const AxisAngle &angle) {
|
||||||
|
double s = std::sin(angle.angle / 2);
|
||||||
|
x = angle.axis.x * s;
|
||||||
|
y = angle.axis.y * s;
|
||||||
|
z = angle.axis.z * s;
|
||||||
|
w = std::cos(angle.angle / 2);
|
||||||
|
}
|
||||||
}
|
}
|
Reference in New Issue
Block a user