updated to latest crafter version

This commit is contained in:
Jorijn van der Graaf 2026-01-29 20:19:20 +01:00
commit d01d482cd6
7 changed files with 234 additions and 310 deletions

View file

@ -1,12 +1,11 @@
/*
Crafter.Math
Copyright (C) 2025 Catcrafts
Catcrafts.net
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 as published by the Free Software Foundation; either
version 3.0 of the License, or (at your option) any later version.
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
@ -18,24 +17,15 @@ License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
module;
#include <type_traits>
#include <concepts>
#include <immintrin.h>
#include <string>
#include <sstream>
#include <iostream>
#include <cmath>
export module Crafter.Math:MatrixRowMajor;
import :BasicTypes;
import :Vector;
import :Misc;
import std;
namespace Crafter {
export template <typename T, uint32_t CollumSize, uint32_t RowSize, uint32_t Repeats>
export template <typename T, std::uint32_t CollumSize, std::uint32_t RowSize, std::uint32_t Repeats>
class MatrixRowMajor {
public:
T m[RowSize][CollumSize*Repeats];
@ -69,6 +59,27 @@ namespace Crafter {
m[3][3] = w3;
}
MatrixRowMajor(
float x0, float y0, float z0, float w0,
float x1, float y1, float z1, float w1,
float x2, float y2, float z2, float w2
) requires(CollumSize == 4 && RowSize == 3 && Repeats == 1 && std::same_as<T, float>) {
m[0][0] = x0;
m[0][1] = y0;
m[0][2] = z0;
m[0][3] = w0;
m[1][0] = x1;
m[1][1] = y1;
m[1][2] = z1;
m[1][3] = w1;
m[2][0] = x2;
m[2][1] = y2;
m[2][2] = z2;
m[2][3] = w2;
}
Vector<T, 3> operator*(Vector<T, 3> b) const requires(CollumSize == 4 && RowSize == 4 && Repeats == 1 && std::same_as<T, float>) {
return Vector<T, 3>(
b.x * m[0][0] + b.y * m[1][0] + b.z * m[2][0] + m[3][0],
@ -143,6 +154,14 @@ namespace Crafter {
0, 0, 0, 1
);
}
static MatrixRowMajor<T, CollumSize, RowSize, Repeats> Identity() requires(CollumSize == 4 && RowSize == 3 && Repeats == 1 && std::same_as<T, float>) {
return MatrixRowMajor<T, CollumSize, RowSize, Repeats>(
1, 0, 0, 0,
0, 1, 0, 0,
0, 0, 1, 0
);
}
static MatrixRowMajor<T, CollumSize, RowSize, Repeats> Scaling(float x, float y, float z) requires(CollumSize == 4 && RowSize == 4 && Repeats == 1 && std::same_as<T, float>) {
return MatrixRowMajor<T, CollumSize, RowSize, Repeats>(
@ -152,7 +171,13 @@ namespace Crafter {
0, 0, 0, 1
);
}
static MatrixRowMajor<T, CollumSize, RowSize, Repeats> Scaling(float x, float y, float z) requires(CollumSize == 4 && RowSize == 3 && Repeats == 1 && std::same_as<T, float>) {
return MatrixRowMajor<T, CollumSize, RowSize, Repeats>(
x, 0, 0, 0,
0, y, 0, 0,
0, 0, z, 0
);
}
static MatrixRowMajor<T, CollumSize, RowSize, Repeats> Scaling(Vector<float, 3> vector) requires(CollumSize == 4 && RowSize == 4 && Repeats == 1 && std::same_as<T, float>) {
return Scaling(vector.x, vector.y, vector.z);
}
@ -165,20 +190,26 @@ namespace Crafter {
x, y, z, 1
);
}
static MatrixRowMajor<T, CollumSize, RowSize, Repeats> Translation(float x, float y, float z) requires(CollumSize == 4 && RowSize == 3 && Repeats == 1 && std::same_as<T, float>) {
return MatrixRowMajor<T, CollumSize, RowSize, Repeats>(
1, 0, 0, x,
0, 1, 0, y,
0, 0, 1, z
);
}
static MatrixRowMajor<T, CollumSize, RowSize, Repeats> Translation(Vector<float, 3> vector) requires(CollumSize == 4 && RowSize == 4 && Repeats == 1 && std::same_as<T, float>) {
return Translation(vector.x, vector.y, vector.z);
}
static MatrixRowMajor<T, CollumSize, RowSize, Repeats> Rotation(float Pitch, float Yaw, float Roll) requires(CollumSize == 4 && RowSize == 4 && Repeats == 1 && std::same_as<T, float>) {
float cp = cosf(Pitch);
float sp = sinf(Pitch);
float cp = std::cosf(Pitch);
float sp = std::sinf(Pitch);
float cy = cosf(Yaw);
float sy = sinf(Yaw);
float cy = std::cosf(Yaw);
float sy = std::sinf(Yaw);
float cr = cosf(Roll);
float sr = sinf(Roll);
float cr = std::cosf(Roll);
float sr = std::sinf(Roll);
MatrixRowMajor<T, CollumSize, RowSize, Repeats> M;
M.m[0][0] = cr * cy + sr * sp * sy;
@ -204,6 +235,35 @@ namespace Crafter {
return M;
}
static MatrixRowMajor<T, CollumSize, RowSize, Repeats> Rotation(float Pitch, float Yaw, float Roll) requires(CollumSize == 4 && RowSize == 3 && Repeats == 1 && std::same_as<T, float>) {
float cp = std::cosf(Pitch);
float sp = std::sinf(Pitch);
float cy = std::cosf(Yaw);
float sy = std::sinf(Yaw);
float cr = std::cosf(Roll);
float sr = std::sinf(Roll);
MatrixRowMajor<T, CollumSize, RowSize, Repeats> M;
M.m[0][0] = cr * cy + sr * sp * sy;
M.m[0][1] = sr * cp;
M.m[0][2] = sr * sp * cy - cr * sy;
M.m[0][3] = 0.0f;
M.m[1][0] = cr * sp * sy - sr * cy;
M.m[1][1] = cr * cp;
M.m[1][2] = sr * sy + cr * sp * cy;
M.m[1][3] = 0.0f;
M.m[2][0] = cp * sy;
M.m[2][1] = -sp;
M.m[2][2] = cp * cy;
M.m[2][3] = 0.0f;
return M;
}
static MatrixRowMajor<T, CollumSize, RowSize, Repeats> LookAt(Vector<float, 3> eyePosition, Vector<float, 3> focusPosition, Vector<float, 3> upDirection) requires(CollumSize == 4 && RowSize == 4 && Repeats == 1 && std::same_as<T, float>) {
MatrixRowMajor<T, CollumSize, RowSize, Repeats> M;
@ -251,7 +311,7 @@ namespace Crafter {
return M;
}
static MatrixRowMajor<T, CollumSize, RowSize, Repeats> Rotation(Vector<float, 3> vector) requires(CollumSize == 4 && RowSize == 4 && Repeats == 1 && std::same_as<T, float>) {
static MatrixRowMajor<T, CollumSize, RowSize, Repeats> Rotation(Vector<float, 3> vector) requires(CollumSize == 4 && RowSize == 4 && Repeats == 1 && std::same_as<T, float>) {
return Rotation(vector.x, vector.y, vector.z);
}
@ -266,6 +326,93 @@ namespace Crafter {
return Result;
}
// MatrixRowMajor<T, CollumSize, RowSize, Repeats> Inverse() requires(CollumSize == 4 && RowSize == 4 && Repeats == 1 && std::same_as<T, float>) {
// Vector<float, 4> V0[4], V1[4];
// V0[0] = Vector<float, 4>(m[0][2], m[0][2], m[1][2], m[1][2]);
// V1[0] = Vector<float, 4>(m[2][3], m[3][3], m[2][3], m[3][3]);
// V0[1] = Vector<float, 4>(m[0][0], m[0][0], m[1][0], m[1][0]);
// V1[1] = Vector<float, 4>(m[2][1], m[3][1], m[2][1], m[3][1]);
// V0[2] = Vector<float, 4>(m[0][2], m[2][2], m[0][0], m[2][0]);
// V1[2] = Vector<float, 4>(m[1][3], m[3][3], m[1][1], m[3][1]);
// Vector<float, 4> D0 = V0[0] * V1[0];
// Vector<float, 4> D1 = V0[1] * V1[1];
// Vector<float, 4> D2 = V0[2] * V1[2];
// V0[0] = Vector<float, 4>(m[2][2], m[3][2], m[2][2], m[3][2]);
// V1[0] = Vector<float, 4>(m[0][3], m[0][3], m[1][3], m[1][3]);
// V0[1] = Vector<float, 4>(m[2][0], m[3][0], m[2][0], m[3][0]);
// V1[1] = Vector<float, 4>(m[0][1], m[0][1], m[1][1], m[1][1]);
// V0[2] = Vector<float, 4>(m[1][2], m[3][2], m[1][0], m[3][0]);
// V1[2] = Vector<float, 4>(m[0][3], m[2][3], m[0][1], m[2][1]);
// D0 = Vector<float, 4>::NegativeMultiplySubtract(V0[0], V1[0], D0);
// D1 = Vector<float, 4>::NegativeMultiplySubtract(V0[1], V1[1], D1);
// D2 = Vector<float, 4>::NegativeMultiplySubtract(V0[2], V1[2], D2);
// V0[0] = Vector<float, 4>(m[1][1], m[2][1], m[0][1], m[1][1]);
// V1[0] = Vector<float, 4>(D2.v[1], D0.v[1], D0.v[3], D0.v[0]);
// V0[1] = Vector<float, 4>(m[2][0], m[0][0], m[1][0], m[0][0]);
// V1[1] = Vector<float, 4>(D0.v[3], D2.v[1], D0.v[1], D0.v[2]);
// V0[2] = Vector<float, 4>(m[1][3], m[2][3], m[0][3], m[1][3]);
// V1[2] = Vector<float, 4>(D2.v[3], D1.v[1], D1.v[3], D1.v[0]);
// V0[3] = Vector<float, 4>(m[2][2], m[0][2], m[1][2], m[0][2]);
// V1[3] = Vector<float, 4>(D1.v[3], D2.v[3], D1.v[1], D1.v[2]);
// Vector<float, 4> C0 = V0[0] * V1[0];
// Vector<float, 4> C2 = V0[1] * V1[1];
// Vector<float, 4> C4 = V0[2] * V1[2];
// Vector<float, 4> C6 = V0[3] * V1[3];
// V0[0] = Vector<float, 4>(m[2][1], m[3][1], m[1][1], m[2][1]);
// V1[0] = Vector<float, 4>(D0.v[3], D0.v[0], D0.v[1], D2.v[0]);
// V0[1] = Vector<float, 4>(m[3][0], m[2][0], m[3][0], m[1][0]);
// V1[1] = Vector<float, 4>(D0.v[2], D0.v[1], D2.v[0], D0.v[0]);
// V0[2] = Vector<float, 4>(m[2][3], m[3][3], m[1][3], m[2][3]);
// V1[2] = Vector<float, 4>(D1.v[3], D1.v[0], D1.v[1], D2.v[2]);
// V0[3] = XMVectorSwizzle<XM_SWIZZLE_W, XM_SWIZZLE_Z, XM_SWIZZLE_W, XM_SWIZZLE_Y>(MT.r[2]);
// V1[3] = XMVectorPermute<XM_PERMUTE_0Z, XM_PERMUTE_0Y, XM_PERMUTE_1Z, XM_PERMUTE_0X>(D1, D2);
// C0 = XMVectorNegativeMultiplySubtract(V0[0], V1[0], C0);
// C2 = XMVectorNegativeMultiplySubtract(V0[1], V1[1], C2);
// C4 = XMVectorNegativeMultiplySubtract(V0[2], V1[2], C4);
// C6 = XMVectorNegativeMultiplySubtract(V0[3], V1[3], C6);
// V0[0] = XMVectorSwizzle<XM_SWIZZLE_W, XM_SWIZZLE_X, XM_SWIZZLE_W, XM_SWIZZLE_X>(MT.r[1]);
// V1[0] = XMVectorPermute<XM_PERMUTE_0Z, XM_PERMUTE_1Y, XM_PERMUTE_1X, XM_PERMUTE_0Z>(D0, D2);
// V0[1] = XMVectorSwizzle<XM_SWIZZLE_Y, XM_SWIZZLE_W, XM_SWIZZLE_X, XM_SWIZZLE_Z>(MT.r[0]);
// V1[1] = XMVectorPermute<XM_PERMUTE_1Y, XM_PERMUTE_0X, XM_PERMUTE_0W, XM_PERMUTE_1X>(D0, D2);
// V0[2] = XMVectorSwizzle<XM_SWIZZLE_W, XM_SWIZZLE_X, XM_SWIZZLE_W, XM_SWIZZLE_X>(MT.r[3]);
// V1[2] = XMVectorPermute<XM_PERMUTE_0Z, XM_PERMUTE_1W, XM_PERMUTE_1Z, XM_PERMUTE_0Z>(D1, D2);
// V0[3] = XMVectorSwizzle<XM_SWIZZLE_Y, XM_SWIZZLE_W, XM_SWIZZLE_X, XM_SWIZZLE_Z>(MT.r[2]);
// V1[3] = XMVectorPermute<XM_PERMUTE_1W, XM_PERMUTE_0X, XM_PERMUTE_0W, XM_PERMUTE_1Z>(D1, D2);
// XMVECTOR C1 = XMVectorNegativeMultiplySubtract(V0[0], V1[0], C0);
// C0 = XMVectorMultiplyAdd(V0[0], V1[0], C0);
// XMVECTOR C3 = XMVectorMultiplyAdd(V0[1], V1[1], C2);
// C2 = XMVectorNegativeMultiplySubtract(V0[1], V1[1], C2);
// XMVECTOR C5 = XMVectorNegativeMultiplySubtract(V0[2], V1[2], C4);
// C4 = XMVectorMultiplyAdd(V0[2], V1[2], C4);
// XMVECTOR C7 = XMVectorMultiplyAdd(V0[3], V1[3], C6);
// C6 = XMVectorNegativeMultiplySubtract(V0[3], V1[3], C6);
// XMMATRIX R;
// R.r[0] = XMVectorSelect(C0, C1, g_XMSelect0101.v);
// R.r[1] = XMVectorSelect(C2, C3, g_XMSelect0101.v);
// R.r[2] = XMVectorSelect(C4, C5, g_XMSelect0101.v);
// R.r[3] = XMVectorSelect(C6, C7, g_XMSelect0101.v);
// XMVECTOR Determinant = XMVector4Dot(R.r[0], MT.r[0]);
// XMVECTOR Reciprocal = XMVectorReciprocal(Determinant);
// XMMATRIX Result;
// Result.r[0] = XMVectorMultiply(R.r[0], Reciprocal);
// Result.r[1] = XMVectorMultiply(R.r[1], Reciprocal);
// Result.r[2] = XMVectorMultiply(R.r[2], Reciprocal);
// Result.r[3] = XMVectorMultiply(R.r[3], Reciprocal);
// return Result;
// }
};
}
@ -279,4 +426,15 @@ struct std::formatter<Crafter::MatrixRowMajor<float, 4, 4, 1>> : std::formatter<
obj.m[3][0], obj.m[3][1], obj.m[3][2], obj.m[3][3]
), ctx);
}
};
template <>
struct std::formatter<Crafter::MatrixRowMajor<float, 4, 3, 1>> : std::formatter<std::string> {
auto format(const Crafter::MatrixRowMajor<float, 4, 3, 1>& obj, format_context& ctx) const {
return std::formatter<std::string>::format(std::format("{{{}, {}, {}, {}\n{}, {}, {}, {}\n{}, {}, {}, {}}}",
obj.m[0][0], obj.m[0][1], obj.m[0][2], obj.m[0][3],
obj.m[1][0], obj.m[1][1], obj.m[1][2], obj.m[1][3],
obj.m[2][0], obj.m[2][1], obj.m[2][2], obj.m[2][3]
), ctx);
}
};