Adding code x3
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
#include <J3ML/LinearAlgebra/Quaternion.h>
|
||||
|
||||
#include <J3ML/LinearAlgebra/Vector3.h>
|
||||
|
||||
#include <J3ML/LinearAlgebra/Vector4.h>
|
||||
#include <J3ML/LinearAlgebra/Matrix3x3.h>
|
||||
#include <J3ML/LinearAlgebra/Quaternion.h>
|
||||
|
||||
namespace LinearAlgebra {
|
||||
Quaternion Quaternion::operator-() const
|
||||
@@ -56,4 +56,90 @@ namespace LinearAlgebra {
|
||||
Quaternion Quaternion::operator+() const { return *this; }
|
||||
|
||||
Quaternion::Quaternion() {}
|
||||
|
||||
Quaternion::Quaternion(float X, float Y, float Z, float W) : Vector4(X,Y,Z,W) {}
|
||||
|
||||
// TODO: implement
|
||||
float Quaternion::Dot(const Quaternion &quaternion) const {}
|
||||
|
||||
Quaternion::Quaternion(Vector4 vector4) {
|
||||
|
||||
}
|
||||
|
||||
float Quaternion::Angle() const {
|
||||
return std::acos(w) * 2.f;
|
||||
}
|
||||
|
||||
Quaternion Quaternion::Normalize() const {
|
||||
float length = Length();
|
||||
if (length < 1e-4f)
|
||||
return {0,0,0,0};
|
||||
float reciprocal = 1.f / length;
|
||||
return {
|
||||
x * reciprocal,
|
||||
y * reciprocal,
|
||||
z * reciprocal,
|
||||
w * reciprocal
|
||||
};
|
||||
}
|
||||
|
||||
Quaternion Quaternion::Conjugate() const {
|
||||
return { -x, -y, -z, w };
|
||||
}
|
||||
|
||||
Quaternion Quaternion::Inverse() const {
|
||||
return Conjugate();
|
||||
}
|
||||
|
||||
Quaternion Quaternion::Slerp(const Quaternion &q2, float t) const {
|
||||
float angle = this->Dot(q2);
|
||||
float sign = 1.f;
|
||||
if (angle < 0.f)
|
||||
{
|
||||
angle = -angle;
|
||||
sign = -1.f;
|
||||
}
|
||||
|
||||
float a;
|
||||
float b;
|
||||
if (angle < 0.999)
|
||||
{
|
||||
// angle = Acos(angle); // After this, angle is in the range pi/2 -> 0 as the original angle variable ranged from 0 -> 1.
|
||||
angle = (-0.69813170079773212f * angle * angle - 0.87266462599716477f) * angle + 1.5707963267948966f;
|
||||
|
||||
float ta = t*angle;
|
||||
// Not using a lookup table, manually compute the two sines by using a very rough approximation.
|
||||
float ta2 = ta*ta;
|
||||
b = ((5.64311797634681035370e-03f * ta2 - 1.55271410633428644799e-01f) * ta2 + 9.87862135574673806965e-01f) * ta;
|
||||
a = angle - ta;
|
||||
float a2 = a*a;
|
||||
a = ((5.64311797634681035370e-03f * a2 - 1.55271410633428644799e-01f) * a2 + 9.87862135574673806965e-01f) * a;
|
||||
}
|
||||
else // If angle is close to taking the denominator to zero, resort to linear interpolation (and normalization).
|
||||
{
|
||||
a = 1.f - t;
|
||||
b = t;
|
||||
}
|
||||
// Lerp and renormalize.
|
||||
return (*this * (a * sign) + q2 * b).Normalize();
|
||||
}
|
||||
|
||||
AxisAngle Quaternion::ToAxisAngle() const {
|
||||
float halfAngle = std::acos(w);
|
||||
float angle = halfAngle * 2.f;
|
||||
// TODO: Can Implement Fast Inverse Sqrt Here
|
||||
float reciprocalSinAngle = 1.f / std::sqrt(1.f - w*w);
|
||||
|
||||
Vector3 axis = {
|
||||
x*reciprocalSinAngle,
|
||||
y*reciprocalSinAngle,
|
||||
z*reciprocalSinAngle
|
||||
};
|
||||
return {axis, angle};
|
||||
}
|
||||
|
||||
float Quaternion::AngleBetween(const Quaternion &target) const {
|
||||
Quaternion delta = target / *this;
|
||||
return delta.Normalize().Angle();
|
||||
}
|
||||
}
|
@@ -1,3 +1,5 @@
|
||||
//
|
||||
// Created by josh on 12/26/2023.
|
||||
//
|
||||
#include <J3ML/LinearAlgebra/Transform2D.h>
|
||||
|
||||
namespace LinearAlgebra {
|
||||
|
||||
}
|
@@ -118,6 +118,19 @@ Vector4 Vector4::operator-(const Vector4& rhs) const
|
||||
return {x-rhs.x, y-rhs.y, z-rhs.z, w-rhs.w};
|
||||
}
|
||||
|
||||
Vector4 Vector4::operator*(float rhs) const {
|
||||
return {
|
||||
this->x * rhs,
|
||||
this->y * rhs,
|
||||
this->z * rhs,
|
||||
this->w * rhs
|
||||
};
|
||||
}
|
||||
|
||||
bool Vector4::IsWithinMarginOfError(const Vector4 &rhs, float margin) const {
|
||||
return this->Distance(rhs) <= margin;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
#pragma endregion
|
Reference in New Issue
Block a user