343 lines
No EOL
9.9 KiB
C++
Executable file
343 lines
No EOL
9.9 KiB
C++
Executable file
/*
|
|
Crafter®.Math
|
|
Copyright (C) 2026 Catcrafts®
|
|
catcrafts.net
|
|
|
|
This library is free software; you can redistribute it and/or
|
|
modify it under the terms of the GNU Lesser General Public
|
|
License version 3.0 as published by the Free Software Foundation;
|
|
|
|
This library is distributed in the hope that it will be useful,
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
Lesser General Public License for more details.
|
|
|
|
You should have received a copy of the GNU Lesser General Public
|
|
License along with this library; if not, write to the Free Software
|
|
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
*/
|
|
|
|
export module Crafter.Math:Vector;
|
|
import std;
|
|
|
|
import :BasicTypes;
|
|
import :Misc;
|
|
|
|
namespace Crafter {
|
|
template <typename T, std::uint32_t Len, std::uint32_t Aligment>
|
|
struct __attribute__((packed)) VectorBase {
|
|
T v[Aligment];
|
|
};
|
|
|
|
template <typename T, std::uint32_t Aligment>
|
|
struct __attribute__((packed)) VectorBase<T, 1, Aligment> {
|
|
union {
|
|
T v[Aligment];
|
|
T x;
|
|
};
|
|
};
|
|
|
|
template <typename T, std::uint32_t Aligment>
|
|
struct __attribute__((packed)) VectorBase<T, 2, Aligment> {
|
|
union {
|
|
T v[Aligment];
|
|
struct __attribute__((packed)) {
|
|
T x, y;
|
|
};
|
|
};
|
|
VectorBase<T, 2, Aligment>(float x, float y): x(x), y(y) {
|
|
|
|
}
|
|
VectorBase<T, 2, Aligment>() = default;
|
|
};
|
|
|
|
template <typename T, std::uint32_t Aligment>
|
|
struct __attribute__((packed)) VectorBase<T, 3, Aligment> {
|
|
union {
|
|
T v[Aligment];
|
|
struct __attribute__((packed)) {
|
|
T x, y, z;
|
|
};
|
|
};
|
|
VectorBase<T, 3, Aligment>(float x, float y, float z): x(x), y(y), z(z) {
|
|
|
|
}
|
|
VectorBase<T, 3, Aligment>() = default;
|
|
};
|
|
|
|
template <typename T, std::uint32_t Aligment>
|
|
struct __attribute__((packed)) VectorBase<T, 4, Aligment> {
|
|
union {
|
|
T v[Aligment];
|
|
struct __attribute__((packed)) {
|
|
T x, y, z, w;
|
|
};
|
|
};
|
|
VectorBase<T, 4, Aligment>(float x, float y, float z, float w): x(x), y(y), z(z), w(w) {
|
|
|
|
}
|
|
VectorBase<T, 4, Aligment>() = default;
|
|
};
|
|
|
|
template <typename T, std::uint32_t Len>
|
|
struct VectorBase<T, Len, 0> {
|
|
T v[Len];
|
|
};
|
|
|
|
template <typename T>
|
|
struct VectorBase<T, 1, 0> {
|
|
union {
|
|
T v[1];
|
|
T x;
|
|
};
|
|
};
|
|
|
|
template <typename T>
|
|
struct VectorBase<T, 2, 0> {
|
|
union {
|
|
T v[2];
|
|
struct {
|
|
T x, y;
|
|
};
|
|
};
|
|
VectorBase<T, 2, 0>(float x, float y): x(x), y(y) {
|
|
|
|
}
|
|
VectorBase<T, 2, 0>() = default;
|
|
};
|
|
|
|
template <typename T>
|
|
struct VectorBase<T, 3, 0> {
|
|
union {
|
|
T v[3];
|
|
struct {
|
|
T x, y, z;
|
|
};
|
|
};
|
|
VectorBase<T, 3, 0>(float x, float y, float z): x(x), y(y), z(z) {
|
|
|
|
}
|
|
VectorBase<T, 3, 0>() = default;
|
|
};
|
|
|
|
template <typename T>
|
|
struct VectorBase<T, 4, 0> {
|
|
union {
|
|
T v[4];
|
|
struct {
|
|
T x, y, z, w;
|
|
};
|
|
};
|
|
VectorBase<T, 4, 0>(float x, float y, float z, float w): x(x), y(y), z(z), w(w) {
|
|
|
|
}
|
|
VectorBase<T, 4, 0>() = default;
|
|
};
|
|
|
|
export template <typename T, std::uint32_t Len, std::uint32_t Aligment>
|
|
class Vector : public VectorBase<T, Len, Aligment> {
|
|
public:
|
|
Vector(float x, float y, float z, float w ) requires(std::same_as<T, float> && Len == 4) : VectorBase<T, Len, Aligment>(x, y, z, w) {
|
|
|
|
}
|
|
Vector(float x, float y, float z) requires(std::same_as<T, float> && Len == 3) : VectorBase<T, Len, Aligment>(x, y, z) {
|
|
|
|
}
|
|
Vector(float x, float y) requires(std::same_as<T, float> && Len == 2) : VectorBase<T, Len, Aligment>(x, y) {
|
|
|
|
}
|
|
Vector() = default;
|
|
|
|
static Vector<T, Len, Aligment> QuaternionRotationRollPitchYaw(float Pitch, float Yaw, float Roll) requires(Len == 4) {
|
|
const float halfpitch = Pitch * 0.5f;
|
|
float cp = std::cosf(halfpitch);
|
|
float sp = std::sinf(halfpitch);
|
|
|
|
const float halfyaw = Yaw * 0.5f;
|
|
float cy = std::cosf(halfyaw);
|
|
float sy = std::sinf(halfyaw);
|
|
|
|
const float halfroll = Roll * 0.5f;
|
|
float cr = std::cosf(halfroll);
|
|
float sr = std::sinf(halfroll);
|
|
|
|
return Vector<T, Len, Aligment>(
|
|
cr * sp * cy + sr * cp * sy,
|
|
cr * cp * sy - sr * sp * cy,
|
|
sr * cp * cy - cr * sp * sy,
|
|
cr * cp * cy + sr * sp * sy
|
|
);
|
|
}
|
|
|
|
template <std::uint32_t Blen, std::uint32_t BAlignment>
|
|
Vector<T, Len, Aligment> operator+(Vector<T, Blen, BAlignment> b){
|
|
Vector<T, Len, Aligment> resultVector;
|
|
for(std::uint32_t i = 0; i < std::min(Len, Blen); i++) {
|
|
resultVector.v[i] = this->v[i]+b.v[i];
|
|
}
|
|
return resultVector;
|
|
}
|
|
template <std::uint32_t Blen, std::uint32_t BAlignment>
|
|
Vector<T, Len, Aligment> operator-(Vector<T, Blen, BAlignment> b){
|
|
Vector<T, Len, Aligment> resultVector;
|
|
for(std::uint32_t i = 0; i < std::min(Len, Blen); i++) {
|
|
resultVector.v[i] = this->v[i]-b.v[i];
|
|
}
|
|
return resultVector;
|
|
}
|
|
Vector<T, Len, Aligment> operator-(){
|
|
Vector<T, Len, Aligment> resultVector;
|
|
for(std::uint32_t i = 0; i < Len; i++) {
|
|
resultVector.v[i] = -this->v[i];
|
|
}
|
|
return resultVector;
|
|
}
|
|
template <std::uint32_t Blen, std::uint32_t BAlignment>
|
|
Vector<T, Len, Aligment> operator*(Vector<T, Blen, BAlignment> b){
|
|
Vector<T, Len, Aligment> resultVector;
|
|
for(std::uint32_t i = 0; i < std::min(Len, Blen); i++) {
|
|
resultVector.v[i] = this->v[i]*b.v[i];
|
|
}
|
|
return resultVector;
|
|
}
|
|
template <std::uint32_t Blen, std::uint32_t BAlignment>
|
|
Vector<T, Len, Aligment> operator/(Vector<T, Blen, BAlignment> b){
|
|
Vector<T, Len, Aligment> resultVector;
|
|
for(std::uint32_t i = 0; i < std::min(Len, Blen); i++) {
|
|
resultVector.v[i] = this->v[i]/b.v[i];
|
|
}
|
|
return resultVector;
|
|
}
|
|
|
|
Vector<T, Len, Aligment> Rotate(Vector<T, 4, Aligment> rotation) requires(Len == 3) {
|
|
Vector<T, 4, Aligment> q = rotation.QuaternionConjugate();
|
|
Vector<T, 4, Aligment> result = q.QuaternionMultiply(Vector<T, 4, Aligment>(this->x, this->y, this->z, 0));
|
|
return Vector<T, Len, Aligment>(result.x, result.y, result.z);
|
|
}
|
|
|
|
Vector<T, Len, Aligment> Normalize() requires(Len == 3) {
|
|
float fLength = Length();
|
|
|
|
// Prevent divide by zero
|
|
if (fLength > 0)
|
|
{
|
|
fLength = 1.0f / fLength;
|
|
}
|
|
|
|
return Vector<T, Len, Aligment>(this->v[0] * fLength, this->v[1] * fLength, this->v[2] * fLength);
|
|
}
|
|
|
|
Vector<T, Len, Aligment> Normalize() requires(Len == 4) {
|
|
float fLength = Length();
|
|
|
|
// Prevent divide by zero
|
|
if (fLength > 0)
|
|
{
|
|
fLength = 1.0f / fLength;
|
|
}
|
|
|
|
return Vector<T, Len, Aligment>(this->v[0] * fLength, this->v[1] * fLength, this->v[2] * fLength, this->v[3] * fLength);
|
|
}
|
|
|
|
float Length()
|
|
{
|
|
float Result = LengthSq();
|
|
return std::sqrtf(Result);
|
|
}
|
|
|
|
Vector<T, Len, Aligment> ReciprocalLength() requires(Len == 3) {
|
|
Vector<T, Len, Aligment> Result = LengthSq();
|
|
Result = ReciprocalSqrt(Result);
|
|
return Result;
|
|
}
|
|
|
|
Vector<T, Len, Aligment> ReciprocalSqrt() requires(Len == 3)
|
|
{
|
|
return Vector<T, Len, Aligment>(
|
|
1.f / std::sqrtf(this->v[0]),
|
|
1.f / std::sqrtf(this->v[1]),
|
|
1.f / std::sqrtf(this->v[2])
|
|
);
|
|
}
|
|
|
|
float LengthSq()
|
|
{
|
|
return Dot(*this);
|
|
}
|
|
|
|
float Dot(Vector<T, Len, Aligment> v2) requires(Len == 3)
|
|
{
|
|
return this->v[0] * v2.v[0] + this->v[1] * v2.v[1] + this->v[2] * v2.v[2];
|
|
}
|
|
|
|
float Dot(Vector<T, Len, Aligment> v2) requires(Len == 4)
|
|
{
|
|
return this->v[0] * v2.v[0] + this->v[1] * v2.v[1] + this->v[2] * v2.v[2] + this->v[3] * v2.v[3];
|
|
}
|
|
|
|
Vector<T, Len, Aligment> Cross(Vector<T, Len, Aligment> v2) requires(Len == 3) {
|
|
return Vector<T, Len, Aligment>(
|
|
(this->v[1] * v2.v[2]) - (this->v[2] * v2.v[1]),
|
|
(this->v[2] * v2.v[0]) - (this->v[0] * v2.v[2]),
|
|
(this->v[0] * v2.v[1]) - (this->v[1] * v2.v[0])
|
|
);
|
|
};
|
|
|
|
Vector<T, Len, Aligment> QuaternionConjugate() requires(Len == 4) {
|
|
return Vector<T, Len, Aligment>(
|
|
-this->x,
|
|
-this->y,
|
|
-this->z,
|
|
this->w
|
|
);
|
|
}
|
|
|
|
Vector<T, Len, Aligment> QuaternionMultiply(Vector<T, Len, Aligment> q2) requires(Len == 4) {
|
|
return Vector<T, Len, Aligment>(
|
|
(q2.v[3] * this->v[0]) + (q2.v[0] * this->v[3]) + (q2.v[1] * this->v[2]) - (q2.v[2] * this->v[1]),
|
|
(q2.v[3] * this->v[1]) - (q2.v[0] * this->v[2]) + (q2.v[1] * this->v[3]) + (q2.v[2] * this->v[0]),
|
|
(q2.v[3] * this->v[2]) + (q2.v[0] * this->v[1]) - (q2.v[1] * this->v[0]) + (q2.v[2] * this->v[3]),
|
|
(q2.v[3] * this->v[3]) - (q2.v[0] * this->v[0]) - (q2.v[1] * this->v[1]) - (q2.v[2] * this->v[2])
|
|
);
|
|
}
|
|
|
|
Vector<T, 4, Aligment> QuaternionRotationAxis(float angle) requires(Len == 3) {
|
|
Vector<T, 3, Aligment> Normal = Normalize();
|
|
return Normal.QuaternionRotationNormal(angle);
|
|
}
|
|
|
|
Vector<T, 4, Aligment> QuaternionRotationNormal(float angle) requires(Len == 3) {
|
|
Vector<T, 4, Aligment> N = Vector<T, 4, Aligment>(this->x, this->y, this->z, 1);
|
|
|
|
float SinV, CosV;
|
|
XMScalarSinCos(&SinV, &CosV, 0.5f * angle);
|
|
|
|
Vector<T, 4, Aligment> Scale = Vector<T, 4, Aligment>(SinV, SinV, SinV, CosV);
|
|
return N * Scale;
|
|
}
|
|
|
|
static Vector<T, 4, Aligment> NegativeMultiplySubtract(Vector<T, 4, Aligment> a, Vector<T, 4, Aligment> b, Vector<T, 4, Aligment> c) requires(Len == 4) {
|
|
return Vector<T, 4, Aligment>(
|
|
c.v[0] - (a.v[0] * b.v[0]),
|
|
c.v[1] - (a.v[1] * b.v[1]),
|
|
c.v[2] - (a.v[2] * b.v[2]),
|
|
c.v[3] - (a.v[3] * b.v[3])
|
|
);
|
|
}
|
|
};
|
|
}
|
|
|
|
|
|
template <>
|
|
struct std::formatter<Crafter::Vector<float, 3, 4>> : std::formatter<std::string> {
|
|
auto format(const Crafter::Vector<float, 3, 4>& obj, format_context& ctx) const {
|
|
return std::formatter<std::string>::format(std::format("{{{}, {}, {}}}",
|
|
obj.x, obj.y, obj.z
|
|
), ctx);
|
|
}
|
|
auto format(const Crafter::Vector<float, 4, 4>& obj, format_context& ctx) const {
|
|
return std::formatter<std::string>::format(std::format("{{{}, {}, {}, {}}}",
|
|
obj.x, obj.y, obj.z, obj.w
|
|
), ctx);
|
|
}
|
|
}; |