1
0
forked from josh/j3ml

Adding code

This commit is contained in:
2023-12-26 13:24:13 -06:00
parent d456d05c3d
commit d9bd070fd1
40 changed files with 1610 additions and 1656 deletions

View File

@@ -0,0 +1,93 @@
#pragma once
namespace LinearAlgebra
{
class Quaternion : public Vector4
{
public:
Quaternion() {}
Quaternion(const Quaternion& rhs) = default;
explicit Quaternion(Matrix3x3& rotationMtrx) {}
explicit Quaternion(Matrix4x4& rotationMtrx) {}
// @note The input data is not normalized after construction, this has to be done manually.
Quaternion(float x, float y, float z, float w);
// Constructs this quaternion by specifying a rotation axis and the amount of rotation to be performed about that axis
// @param rotationAxis The normalized rotation axis to rotate about. If using Vector4 version of the constructor, the w component of this vector must be 0.
Quaternion(const Vector3& rotationAxis, float rotationAngleBetween) { SetFromAxisAngle(rotationAxis, rotationAngleBetween); }
Quaternion(const Vector4& rotationAxis, float rotationAngleBetween) { SetFromAxisAngle(rotationAxis, rotationAngleBetween); }
//void Inverse();
Quaternion Inverse() const;
//void Normalize();
Quaternion Normalize() const;
Vector3 GetWorldX() const { return Transform(1.f, 0.f, 0.f); }
Vector3 GetWorldY() const { return Transform(0.f, 1.f, 0.f); }
Vector3 GetWorldZ() const { return Transform(0.f, 0.f, 1.f); }
Matrix3x3 ToMatrix3x3() const;
Vector3 Transform(const Vector3& vec) const
{
Matrix3x3 mat = this->ToMatrix3x3();
return mat * vec;
}
Vector3 Transform(float X, float Y, float Z) const
{
return Transform(Vector3{X, Y, Z});
}
// Note: We only transform the x,y,z components of 4D vectors, w is left untouched
Vector4 Transform(const Vector4& vec) const
{
return Vector4(Transform(vec.x, vec.y, vec.z), vec.w);
}
Vector4 Transform(float X, float Y, float Z, float W) const
{
return Transform(Vector4(X, Y, Z, W));
}
Quaternion GetInverse() const;
Quaternion Lerp(const Quaternion& b, float t) const
{
float angle = this->dot(b);
if (angle >= 0.f) // Make sure we rotate the shorter arc
return (*this * (1.f - t) + b * t).Normalize();
else
return (*this * (t - 1.f) + b * t).Normalize();
}
Quaternion Slerp(const Quaternion& target) const;
void SetFromAxisAngle(const Vector3& axis, float angle)
{
float sinz, cosz;
}
void SetFromAxisAngle(const Vector4& axis, float angle)
{
}
static Quaternion LookAt(const Vector3& position, const Vector3& direction, const Vector3& axisUp);
// Multiplies two quaternions together.
// The product q1 * q2 returns a quaternion that concatenates the two orientation rotations.
// The rotation q2 is applied first before q1.
Quaternion operator * (const Quaternion& rhs) const;
Quaternion operator * (float scalar) const
{
return Quaternion(x * scalar, y * scalar, z * scalar, w * scalar);
}
// Transforms the given vector by this Quaternion.
Vector3 operator * (const Vector3& rhs) const;
Vector4 operator * (const Vector4& rhs) const;
// Divides a quaternion by another. Divison "a / b" results in a quaternion that rotates the orientation b to coincide with orientation of
Quaternion operator / (const Quaternion& rhs) const;
Quaternion operator +(const Quaternion& rhs) const;
Quaternion operator +() const { return *this; }
Quaternion operator -() const;
};
}