vector alignment optional

This commit is contained in:
Jorijn van der Graaf 2026-03-08 14:37:33 +01:00
commit 4624f30f1f

View file

@ -45,7 +45,7 @@ namespace Crafter {
T r, g; T r, g;
}; };
}; };
constexpr VectorBase<T, 2, Aligment>(float x, float y): x(x), y(y) { constexpr VectorBase<T, 2, Aligment>(T x, T y): x(x), y(y) {
} }
constexpr VectorBase<T, 2, Aligment>() = default; constexpr VectorBase<T, 2, Aligment>() = default;
@ -62,7 +62,7 @@ namespace Crafter {
T r, g, b; T r, g, b;
}; };
}; };
constexpr VectorBase<T, 3, Aligment>(float x, float y, float z): x(x), y(y), z(z) { constexpr VectorBase<T, 3, Aligment>(T x, T y, T z): x(x), y(y), z(z) {
} }
constexpr VectorBase<T, 3, Aligment>() = default; constexpr VectorBase<T, 3, Aligment>() = default;
@ -79,7 +79,7 @@ namespace Crafter {
T r, g, b, a; 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) { constexpr VectorBase<T, 4, Aligment>(T x, T y, T z, T w): x(x), y(y), z(z), w(w) {
} }
constexpr VectorBase<T, 4, Aligment>() = default; constexpr VectorBase<T, 4, Aligment>() = default;
@ -109,7 +109,7 @@ namespace Crafter {
T r, g; T r, g;
}; };
}; };
constexpr VectorBase<T, 2, 0>(float x, float y): x(x), y(y) { constexpr VectorBase<T, 2, 0>(T x, T y): x(x), y(y) {
} }
constexpr VectorBase<T, 2, 0>() = default; constexpr VectorBase<T, 2, 0>() = default;
@ -126,7 +126,7 @@ namespace Crafter {
T r, g, b; T r, g, b;
}; };
}; };
constexpr VectorBase<T, 3, 0>(float x, float y, float z): x(x), y(y), z(z) { constexpr VectorBase<T, 3, 0>(T x, T y, T z): x(x), y(y), z(z) {
} }
constexpr VectorBase<T, 3, 0>() = default; constexpr VectorBase<T, 3, 0>() = default;
@ -143,22 +143,22 @@ namespace Crafter {
T r, g, b, a; 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) { constexpr VectorBase<T, 4, 0>(T x, T y, T z, T w): x(x), y(y), z(z), w(w) {
} }
constexpr VectorBase<T, 4, 0>() = default; constexpr VectorBase<T, 4, 0>() = default;
}; };
export template <typename T, std::uint32_t Len, std::uint32_t Aligment> export template <typename T, std::uint32_t Len, std::uint32_t Aligment = 0>
class Vector : public VectorBase<T, Len, Aligment> { class Vector : public VectorBase<T, Len, Aligment> {
public: public:
constexpr 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) { constexpr Vector(T x, T y, T z, T w ) requires(std::same_as<T, T> && Len == 4) : VectorBase<T, Len, Aligment>(x, y, z, w) {
} }
constexpr Vector(float x, float y, float z) requires(std::same_as<T, float> && Len == 3) : VectorBase<T, Len, Aligment>(x, y, z) { constexpr Vector(T x, T y, T z) requires(std::same_as<T, T> && Len == 3) : VectorBase<T, Len, Aligment>(x, y, z) {
} }
constexpr Vector(float x, float y) requires(std::same_as<T, float> && Len == 2) : VectorBase<T, Len, Aligment>(x, y) { constexpr Vector(T x, T y) requires(std::same_as<T, T> && Len == 2) : VectorBase<T, Len, Aligment>(x, y) {
} }
@ -315,7 +315,7 @@ namespace Crafter {
} }
constexpr void Normalize() { constexpr void Normalize() {
float fLength = Length(); T fLength = Length();
if (fLength > 0) { if (fLength > 0) {
fLength = 1.0f / fLength; fLength = 1.0f / fLength;
@ -332,12 +332,12 @@ namespace Crafter {
} }
} }
constexpr float Length() const { constexpr T Length() const {
float Result = LengthSq(); T Result = LengthSq();
return std::sqrtf(Result); return std::sqrtf(Result);
} }
constexpr float LengthSq() const { constexpr T LengthSq() const {
return Dot(*this, *this); return Dot(*this, *this);
} }
@ -353,7 +353,7 @@ namespace Crafter {
template <typename AT, std::uint32_t Alen, std::uint32_t AAlignment> 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) { constexpr static Vector<T, Len, Aligment> Normalize(Vector<AT, Alen, AAlignment> a) requires(Len == Alen) {
Vector<T, 3, 0> returned; Vector<T, 3, 0> returned;
float fLength = a.Length(); T fLength = a.Length();
if (fLength > 0) { if (fLength > 0) {
fLength = 1.0f / fLength; fLength = 1.0f / fLength;
@ -366,8 +366,8 @@ namespace Crafter {
} }
template <typename AT, std::uint32_t Alen, std::uint32_t AAlignment, typename BT, std::uint32_t Blen, std::uint32_t BAlignment> 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) { constexpr static T Dot(Vector<AT, Alen, AAlignment> a, Vector<BT, Blen, BAlignment> b) requires(Alen >= Len && Blen >= Len) {
float accumulate = a.v[0] * b.v[0]; T accumulate = a.v[0] * b.v[0];
for(std::uint32_t i = 1; i < Len; i++) { for(std::uint32_t i = 1; i < Len; i++) {
accumulate += a.v[i] * b.v[i]; accumulate += a.v[i] * b.v[i];
} }