added comparison

This commit is contained in:
Jorijn van der Graaf 2026-02-19 02:36:36 +01:00
commit 05fbd3685b
5 changed files with 255 additions and 82 deletions

View file

@ -30,7 +30,7 @@ namespace Crafter {
struct __attribute__((packed)) VectorBase<T, 1, Aligment> {
union {
T v[Aligment];
T x;
T x, r;
};
};
@ -41,6 +41,9 @@ namespace Crafter {
struct __attribute__((packed)) {
T x, y;
};
struct __attribute__((packed)) {
T r, g;
};
};
constexpr VectorBase<T, 2, Aligment>(float x, float y): x(x), y(y) {
@ -55,6 +58,9 @@ namespace Crafter {
struct __attribute__((packed)) {
T x, y, z;
};
struct __attribute__((packed)) {
T r, g, b;
};
};
constexpr VectorBase<T, 3, Aligment>(float x, float y, float z): x(x), y(y), z(z) {
@ -69,6 +75,9 @@ namespace Crafter {
struct __attribute__((packed)) {
T x, y, z, w;
};
struct __attribute__((packed)) {
T r, g, b, a;
};
};
constexpr VectorBase<T, 4, Aligment>(float x, float y, float z, float w): x(x), y(y), z(z), w(w) {
@ -85,7 +94,7 @@ namespace Crafter {
struct VectorBase<T, 1, 0> {
union {
T v[1];
T x;
T x, r;
};
};
@ -96,6 +105,9 @@ namespace Crafter {
struct {
T x, y;
};
struct {
T r, g;
};
};
constexpr VectorBase<T, 2, 0>(float x, float y): x(x), y(y) {
@ -110,6 +122,9 @@ namespace Crafter {
struct {
T x, y, z;
};
struct {
T r, g, b;
};
};
constexpr VectorBase<T, 3, 0>(float x, float y, float z): x(x), y(y), z(z) {
@ -124,6 +139,9 @@ namespace Crafter {
struct {
T x, y, z, w;
};
struct {
T r, g, b, a;
};
};
constexpr VectorBase<T, 4, 0>(float x, float y, float z, float w): x(x), y(y), z(z), w(w) {
@ -274,6 +292,25 @@ namespace Crafter {
return returnVector;
}
template <typename BT>
constexpr bool operator==(BT b) const {
for(std::uint32_t i = 0; i < Len; i++) {
if(this->v[i] != this->v[i]) {
return false;
}
}
return true;
}
template <typename BT>
constexpr bool operator!=(BT b) const {
for(std::uint32_t i = 0; i < Len; i++) {
if(this->v[i] != this->v[i]) {
return true;
}
}
return false;
}
constexpr void Normalize() {
float fLength = Length();
@ -310,6 +347,21 @@ namespace Crafter {
);
}
template <typename AT, std::uint32_t Alen, std::uint32_t AAlignment>
constexpr static Vector<T, Len, Aligment> Normalize(Vector<AT, Alen, AAlignment> a) requires(Len == Alen) {
Vector<T, 3, 0> returned;
float fLength = a.Length();
if (fLength > 0) {
fLength = 1.0f / fLength;
}
for(std::uint32_t i = 0; i < Len; i++) {
returned.v[i] = a.v[i] * fLength;
}
return returned;
}
template <typename AT, std::uint32_t Alen, std::uint32_t AAlignment, typename BT, std::uint32_t Blen, std::uint32_t BAlignment>
constexpr static float Dot(Vector<AT, Alen, AAlignment> a, Vector<BT, Blen, BAlignment> b) requires(Alen >= Len && Blen >= Len) {
float accumulate = a.v[0] * b.v[0];
@ -318,6 +370,64 @@ namespace Crafter {
}
return accumulate;
}
template <typename AT, std::uint32_t AAlignment, typename BT, std::uint32_t BAlignment>
constexpr static Vector<T, 3, Aligment> Rotate(Vector<AT, 3, AAlignment> v, Vector<BT, 4, BAlignment> q) requires(Len == 3) {
Vector<T, 3, 0> qv(q.x, q.y, q.z);
Vector<T, 3, 0> t = Vector<T, 3, Aligment>::Cross(qv, v) * T(2);
return v + t * q.w + Vector<T, 3, Aligment>::Cross(qv, t);
}
template <typename AT, std::uint32_t AAlignment, typename BT, std::uint32_t BAlignment, typename CT, std::uint32_t CAlignment>
constexpr static Vector<T, 4, Aligment> QuanternionFromBasis(Vector<AT, 3, AAlignment> right, Vector<BT, 3, BAlignment> up, Vector<CT, 3, CAlignment> forward) requires(Len == 4) {
T m00 = right.x;
T m01 = up.x;
T m02 = forward.x;
T m10 = right.y;
T m11 = up.y;
T m12 = forward.y;
T m20 = right.z;
T m21 = up.z;
T m22 = forward.z;
T trace = m00 + m11 + m22;
Vector<T, 4, Aligment> q;
if (trace > std::numeric_limits<T>::epsilon()) {
T s = std::sqrt(trace + T(1)) * T(2);
q.w = T(0.25) * s;
q.x = (m21 - m12) / s;
q.y = (m02 - m20) / s;
q.z = (m10 - m01) / s;
}
else if ((m00 > m11) && (m00 > m22)) {
T s = std::sqrt(T(1) + m00 - m11 - m22) * T(2);
q.w = (m21 - m12) / s;
q.x = T(0.25) * s;
q.y = (m01 + m10) / s;
q.z = (m02 + m20) / s;
}
else if (m11 > m22) {
T s = std::sqrt(T(1) + m11 - m00 - m22) * T(2);
q.w = (m02 - m20) / s;
q.x = (m01 + m10) / s;
q.y = T(0.25) * s;
q.z = (m12 + m21) / s;
}
else {
T s = std::sqrt(T(1) + m22 - m00 - m11) * T(2);
q.w = (m10 - m01) / s;
q.x = (m02 + m20) / s;
q.y = (m12 + m21) / s;
q.z = T(0.25) * s;
}
q.Normalize();
return q;
}
};
}