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,6 +1,6 @@
/* /*
Crafter.Math Crafter.Math
Copyright (C) 2025 Catcrafts Copyright (C) 2026 Catcrafts
Catcrafts.net Catcrafts.net
This library is free software; you can redistribute it and/or This library is free software; you can redistribute it and/or
@ -18,13 +18,9 @@ License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/ */
module;
#include <cstdint>
#include <stdfloat>
#include <format>
export module Crafter.Math:BasicTypes; export module Crafter.Math:BasicTypes;
import std;
namespace Crafter { namespace Crafter {

View file

@ -1,211 +0,0 @@
/*
Crafter.Math
Copyright (C) 2025 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.
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
*/
module;
#include <type_traits>
#include <concepts>
#include <immintrin.h>
#include <string>
#include <sstream>
#include <iostream>
#include <cmath>
export module Crafter.Math:MatrixCollumMajor;
import :BasicTypes;
import :Vector;
import :Misc;
namespace Crafter {
export template <typename T, uint32_t CollumSize, uint32_t RowSize, uint32_t Repeats>
class MatrixCollumMajor {
public:
T m[CollumSize*Repeats][RowSize];
MatrixCollumMajor() = default;
MatrixCollumMajor(
float x0, float y0, float z0, float w0,
float x1, float y1, float z1, float w1,
float x2, float y2, float z2, float w2,
float x3, float y3, float z3, float w3
) requires(CollumSize == 4 && RowSize == 4 && Repeats == 1 && std::same_as<T, float>) {
m[0][0] = x0;
m[0][1] = x1;
m[0][2] = x2;
m[0][3] = x3;
m[1][0] = y0;
m[1][1] = y1;
m[1][2] = y2;
m[1][3] = y3;
m[2][0] = z0;
m[2][1] = z1;
m[2][2] = z2;
m[2][3] = z3;
m[3][0] = w0;
m[3][1] = w1;
m[3][2] = w2;
m[3][3] = w3;
}
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[0][1] + b.z * m[0][2] + m[0][3],
b.x * m[1][0] + b.y * m[1][1] + b.z * m[1][2] + m[1][3],
b.x * m[2][0] + b.y * m[2][1] + b.z * m[2][2] + m[2][3]
);
}
MatrixCollumMajor<T, CollumSize, RowSize, Repeats> operator*(MatrixCollumMajor<T, CollumSize, RowSize, Repeats> b) const requires(CollumSize == 4 && RowSize == 4 && Repeats == 1 && std::same_as<T, float>) {
MatrixCollumMajor<T, CollumSize, RowSize, Repeats> result;
result.m[0][0] = b.m[0][0] * m[0][0] + b.m[1][0] * m[0][1] + b.m[2][0] * m[0][2] + b.m[3][0] * m[0][3];
result.m[0][1] = b.m[0][1] * m[0][0] + b.m[1][1] * m[0][1] + b.m[2][1] * m[0][2] + b.m[3][1] * m[0][3];
result.m[0][2] = b.m[0][2] * m[0][0] + b.m[1][2] * m[0][1] + b.m[2][2] * m[0][2] + b.m[3][2] * m[0][3];
result.m[0][3] = b.m[0][3] * m[0][0] + b.m[1][3] * m[0][1] + b.m[2][3] * m[0][2] + b.m[3][3] * m[0][3];
result.m[1][0] = b.m[0][0] * m[1][0] + b.m[1][0] * m[1][1] + b.m[2][0] * m[1][2] + b.m[3][0] * m[1][3];
result.m[1][1] = b.m[0][1] * m[1][0] + b.m[1][1] * m[1][1] + b.m[2][1] * m[1][2] + b.m[3][1] * m[1][3];
result.m[1][2] = b.m[0][2] * m[1][0] + b.m[1][2] * m[1][1] + b.m[2][2] * m[1][2] + b.m[3][2] * m[1][3];
result.m[1][3] = b.m[0][3] * m[1][0] + b.m[1][3] * m[1][1] + b.m[2][3] * m[1][2] + b.m[3][3] * m[1][3];
result.m[2][0] = b.m[0][0] * m[2][0] + b.m[1][0] * m[2][1] + b.m[2][0] * m[2][2] + b.m[3][0] * m[2][3];
result.m[2][1] = b.m[0][1] * m[2][0] + b.m[1][1] * m[2][1] + b.m[2][1] * m[2][2] + b.m[3][1] * m[2][3];
result.m[2][2] = b.m[0][2] * m[2][0] + b.m[1][2] * m[2][1] + b.m[2][2] * m[2][2] + b.m[3][2] * m[2][3];
result.m[2][3] = b.m[0][3] * m[2][0] + b.m[1][3] * m[2][1] + b.m[2][3] * m[2][2] + b.m[3][3] * m[2][3];
result.m[3][0] = b.m[0][0] * m[3][0] + b.m[1][0] * m[3][1] + b.m[2][0] * m[3][2] + b.m[3][0] * m[3][3];
result.m[3][1] = b.m[0][1] * m[3][0] + b.m[1][1] * m[3][1] + b.m[2][1] * m[3][2] + b.m[3][1] * m[3][3];
result.m[3][2] = b.m[0][2] * m[3][0] + b.m[1][2] * m[3][1] + b.m[2][2] * m[3][2] + b.m[3][2] * m[3][3];
result.m[3][3] = b.m[0][3] * m[3][0] + b.m[1][3] * m[3][1] + b.m[2][3] * m[3][2] + b.m[3][3] * m[3][3];
return result;
}
static MatrixCollumMajor<T, CollumSize, RowSize, Repeats> Perspective(float fovAngleY, float aspectRatio, float nearZ, float farZ) requires(CollumSize == 4 && RowSize == 4 && Repeats == 1 && std::same_as<T, float>) {
MatrixCollumMajor<T, CollumSize, RowSize, Repeats> result;
float SinFov;
float CosFov;
XMScalarSinCos(&SinFov, &CosFov, 0.5f * fovAngleY);
float Height = CosFov / SinFov;
float Width = Height / aspectRatio;
float fRange = farZ / (nearZ - farZ);
result.m[0][0] = Width;
result.m[1][0] = 0.0f;
result.m[2][0] = 0.0f;
result.m[3][0] = 0.0f;
result.m[0][1] = 0.0f;
result.m[1][1] = Height;
result.m[2][1] = 0.0f;
result.m[3][1] = 0.0f;
result.m[0][2] = 0.0f;
result.m[1][2] = 0.0f;
result.m[2][2] = fRange;
result.m[3][2] = -1.0f;
result.m[0][3] = 0.0f;
result.m[1][3] = 0.0f;
result.m[2][3] = fRange * nearZ;
result.m[3][3] = 0.0f;
return result;
}
static MatrixCollumMajor<T, CollumSize, RowSize, Repeats> Identity() requires(CollumSize == 4 && RowSize == 4 && Repeats == 1 && std::same_as<T, float>) {
return MatrixCollumMajor<T, CollumSize, RowSize, Repeats>(
1, 0, 0, 0,
0, 1, 0, 0,
0, 0, 1, 0,
0, 0, 0, 1
);
}
static MatrixCollumMajor<T, CollumSize, RowSize, Repeats> Scaling(float x, float y, float z) requires(CollumSize == 4 && RowSize == 4 && Repeats == 1 && std::same_as<T, float>) {
return MatrixCollumMajor<T, CollumSize, RowSize, Repeats>(
x, 0, 0, 0,
0, y, 0, 0,
0, 0, z, 0,
0, 0, 0, 1
);
}
static MatrixCollumMajor<T, CollumSize, RowSize, Repeats> Translation(float x, float y, float z) requires(CollumSize == 4 && RowSize == 4 && Repeats == 1 && std::same_as<T, float>) {
return MatrixCollumMajor<T, CollumSize, RowSize, Repeats>(
1, 0, 0, 0,
0, 1, 0, 0,
0, 0, 1, 0,
x, y, z, 1
);
}
static MatrixCollumMajor<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 cy = cosf(Yaw);
float sy = sinf(Yaw);
float cr = cosf(Roll);
float sr = sinf(Roll);
MatrixCollumMajor<T, CollumSize, RowSize, Repeats> M;
M.m[0][0] = cr * cy + sr * sp * sy;
M.m[1][0] = sr * cp;
M.m[2][0] = sr * sp * cy - cr * sy;
M.m[3][0] = 0.0f;
M.m[0][1] = cr * sp * sy - sr * cy;
M.m[1][1] = cr * cp;
M.m[2][1] = sr * sy + cr * sp * cy;
M.m[3][1] = 0.0f;
M.m[0][2] = cp * sy;
M.m[1][2] = -sp;
M.m[2][2] = cp * cy;
M.m[3][2] = 0.0f;
M.m[0][3] = 0.0f;
M.m[1][3] = 0.0f;
M.m[2][3] = 0.0f;
M.m[3][3] = 1.0f;
return M;
}
};
}
template <>
struct std::formatter<Crafter::MatrixCollumMajor<float, 4, 4, 1>> : std::formatter<std::string> {
auto format(const Crafter::MatrixCollumMajor<float, 4, 4, 1>& obj, format_context& ctx) const {
return std::formatter<std::string>::format(std::format("{{{}, {}, {}, {}\n{}, {}, {}, {}\n{}, {}, {}, {}\n{}, {}, {}, {}}}",
obj.m[0][0], obj.m[1][0], obj.m[2][0], obj.m[3][0],
obj.m[0][1], obj.m[1][1], obj.m[2][1], obj.m[3][1],
obj.m[0][2], obj.m[1][2], obj.m[2][2], obj.m[3][2],
obj.m[0][3], obj.m[1][3], obj.m[2][3], obj.m[3][3]
), ctx);
}
};

View file

@ -1,12 +1,11 @@
/* /*
Crafter.Math Crafter®.Math
Copyright (C) 2025 Catcrafts Copyright (C) 2026 Catcrafts®
Catcrafts.net catcrafts.net
This library is free software; you can redistribute it and/or This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either License version 3.0 as published by the Free Software Foundation;
version 3.0 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful, This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of 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 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; export module Crafter.Math:MatrixRowMajor;
import :BasicTypes; import :BasicTypes;
import :Vector; import :Vector;
import :Misc; import :Misc;
import std;
namespace Crafter { 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 { class MatrixRowMajor {
public: public:
T m[RowSize][CollumSize*Repeats]; T m[RowSize][CollumSize*Repeats];
@ -69,6 +59,27 @@ namespace Crafter {
m[3][3] = w3; 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>) { 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>( return Vector<T, 3>(
b.x * m[0][0] + b.y * m[1][0] + b.z * m[2][0] + m[3][0], 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 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>) { 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>( return MatrixRowMajor<T, CollumSize, RowSize, Repeats>(
@ -152,7 +171,13 @@ namespace Crafter {
0, 0, 0, 1 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>) { 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); return Scaling(vector.x, vector.y, vector.z);
} }
@ -165,20 +190,26 @@ namespace Crafter {
x, y, z, 1 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>) { 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); 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>) { 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 cp = std::cosf(Pitch);
float sp = sinf(Pitch); float sp = std::sinf(Pitch);
float cy = cosf(Yaw); float cy = std::cosf(Yaw);
float sy = sinf(Yaw); float sy = std::sinf(Yaw);
float cr = cosf(Roll); float cr = std::cosf(Roll);
float sr = sinf(Roll); float sr = std::sinf(Roll);
MatrixRowMajor<T, CollumSize, RowSize, Repeats> M; MatrixRowMajor<T, CollumSize, RowSize, Repeats> M;
M.m[0][0] = cr * cy + sr * sp * sy; M.m[0][0] = cr * cy + sr * sp * sy;
@ -204,6 +235,35 @@ namespace Crafter {
return M; 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>) { 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; MatrixRowMajor<T, CollumSize, RowSize, Repeats> M;
@ -251,7 +311,7 @@ namespace Crafter {
return M; 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); return Rotation(vector.x, vector.y, vector.z);
} }
@ -266,6 +326,93 @@ namespace Crafter {
return Result; 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;
// }
}; };
} }
@ -280,3 +427,14 @@ struct std::formatter<Crafter::MatrixRowMajor<float, 4, 4, 1>> : std::formatter<
), ctx); ), 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);
}
};

View file

@ -1,12 +1,11 @@
/* /*
Crafter.Math Crafter®.Math
Copyright (C) 2025 Catcrafts Copyright (C) 2026 Catcrafts®
Catcrafts.net catcrafts.net
This library is free software; you can redistribute it and/or This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either License version 3.0 as published by the Free Software Foundation;
version 3.0 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful, This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of but WITHOUT ANY WARRANTY; without even the implied warranty of
@ -18,13 +17,8 @@ License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/ */
module;
#include <cstdint>
#include <stdfloat>
#include <numeric>
export module Crafter.Math:Misc; export module Crafter.Math:Misc;
import std;
export namespace Crafter { export namespace Crafter {
//------------------------------------------------------------------------------------- //-------------------------------------------------------------------------------------

View file

@ -1,12 +1,11 @@
/* /*
Crafter.Math Crafter®.Math
Copyright (C) 2025 Catcrafts Copyright (C) 2026 Catcrafts®
Catcrafts.net catcrafts.net
This library is free software; you can redistribute it and/or This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either License version 3.0 as published by the Free Software Foundation;
version 3.0 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful, This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of but WITHOUT ANY WARRANTY; without even the implied warranty of
@ -18,23 +17,14 @@ License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 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:Vector; export module Crafter.Math:Vector;
import std;
import :BasicTypes; import :BasicTypes;
import :Misc; import :Misc;
namespace Crafter { namespace Crafter {
template <typename T, uint32_t Len> template <typename T, std::uint32_t Len>
struct VectorBase { struct VectorBase {
T v[Len]; T v[Len];
}; };
@ -89,7 +79,7 @@ namespace Crafter {
VectorBase<T, 4>() = default; VectorBase<T, 4>() = default;
}; };
export template <typename T, uint32_t Len> export template <typename T, std::uint32_t Len>
class Vector : public VectorBase<T, Len> { class Vector : public VectorBase<T, Len> {
public: public:
Vector(float x, float y, float z, float w ) requires(std::same_as<T, float> && Len == 4) : VectorBase<T, Len>(x, y, z, w) { Vector(float x, float y, float z, float w ) requires(std::same_as<T, float> && Len == 4) : VectorBase<T, Len>(x, y, z, w) {
@ -105,16 +95,16 @@ namespace Crafter {
static Vector<T, Len> QuaternionRotationRollPitchYaw(float Pitch, float Yaw, float Roll) requires(Len == 4) { static Vector<T, Len> QuaternionRotationRollPitchYaw(float Pitch, float Yaw, float Roll) requires(Len == 4) {
const float halfpitch = Pitch * 0.5f; const float halfpitch = Pitch * 0.5f;
float cp = cosf(halfpitch); float cp = std::cosf(halfpitch);
float sp = sinf(halfpitch); float sp = std::sinf(halfpitch);
const float halfyaw = Yaw * 0.5f; const float halfyaw = Yaw * 0.5f;
float cy = cosf(halfyaw); float cy = std::cosf(halfyaw);
float sy = sinf(halfyaw); float sy = std::sinf(halfyaw);
const float halfroll = Roll * 0.5f; const float halfroll = Roll * 0.5f;
float cr = cosf(halfroll); float cr = std::cosf(halfroll);
float sr = sinf(halfroll); float sr = std::sinf(halfroll);
return Vector<T, Len>( return Vector<T, Len>(
cr * sp * cy + sr * cp * sy, cr * sp * cy + sr * cp * sy,
@ -124,41 +114,41 @@ namespace Crafter {
); );
} }
template <uint32_t Blen> template <std::uint32_t Blen>
Vector<T, Len> operator+(Vector<T, Blen> b){ Vector<T, Len> operator+(Vector<T, Blen> b){
Vector<T, Len> resultVector; Vector<T, Len> resultVector;
for(uint32_t i = 0; i < std::min(Len, Blen); i++) { for(std::uint32_t i = 0; i < std::min(Len, Blen); i++) {
resultVector.v[i] = this->v[i]+b.v[i]; resultVector.v[i] = this->v[i]+b.v[i];
} }
return resultVector; return resultVector;
} }
template <uint32_t Blen> template <std::uint32_t Blen>
Vector<T, Len> operator-(Vector<T, Blen> b){ Vector<T, Len> operator-(Vector<T, Blen> b){
Vector<T, Len> resultVector; Vector<T, Len> resultVector;
for(uint32_t i = 0; i < std::min(Len, Blen); i++) { for(std::uint32_t i = 0; i < std::min(Len, Blen); i++) {
resultVector.v[i] = this->v[i]-b.v[i]; resultVector.v[i] = this->v[i]-b.v[i];
} }
return resultVector; return resultVector;
} }
Vector<T, Len> operator-(){ Vector<T, Len> operator-(){
Vector<T, Len> resultVector; Vector<T, Len> resultVector;
for(uint32_t i = 0; i < Len; i++) { for(std::uint32_t i = 0; i < Len; i++) {
resultVector.v[i] = -this->v[i]; resultVector.v[i] = -this->v[i];
} }
return resultVector; return resultVector;
} }
template <uint32_t Blen> template <std::uint32_t Blen>
Vector<T, Len> operator*(Vector<T, Blen> b){ Vector<T, Len> operator*(Vector<T, Blen> b){
Vector<T, Len> resultVector; Vector<T, Len> resultVector;
for(uint32_t i = 0; i < std::min(Len, Blen); i++) { for(std::uint32_t i = 0; i < std::min(Len, Blen); i++) {
resultVector.v[i] = this->v[i]*b.v[i]; resultVector.v[i] = this->v[i]*b.v[i];
} }
return resultVector; return resultVector;
} }
template <uint32_t Blen> template <std::uint32_t Blen>
Vector<T, Len> operator/(Vector<T, Blen> b){ Vector<T, Len> operator/(Vector<T, Blen> b){
Vector<T, Len> resultVector; Vector<T, Len> resultVector;
for(uint32_t i = 0; i < std::min(Len, Blen); i++) { for(std::uint32_t i = 0; i < std::min(Len, Blen); i++) {
resultVector.v[i] = this->v[i]/b.v[i]; resultVector.v[i] = this->v[i]/b.v[i];
} }
return resultVector; return resultVector;
@ -197,7 +187,7 @@ namespace Crafter {
float Length() float Length()
{ {
float Result = LengthSq(); float Result = LengthSq();
return sqrtf(Result); return std::sqrtf(Result);
} }
Vector<T, Len> ReciprocalLength() requires(Len == 3) { Vector<T, Len> ReciprocalLength() requires(Len == 3) {
@ -209,9 +199,9 @@ namespace Crafter {
Vector<T, Len> ReciprocalSqrt() requires(Len == 3) Vector<T, Len> ReciprocalSqrt() requires(Len == 3)
{ {
return Vector<T, Len>( return Vector<T, Len>(
1.f / sqrtf(this->v[0]), 1.f / std::sqrtf(this->v[0]),
1.f / sqrtf(this->v[1]), 1.f / std::sqrtf(this->v[1]),
1.f / sqrtf(this->v[2]) 1.f / std::sqrtf(this->v[2])
); );
} }
@ -271,7 +261,14 @@ namespace Crafter {
return N * Scale; return N * Scale;
} }
static Vector<T, 4> NegativeMultiplySubtract(Vector<T, 4> a, Vector<T, 4> b, Vector<T, 4> c) requires(Len == 4) {
return Vector<T, 4>(
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])
);
}

View file

@ -21,6 +21,5 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
export module Crafter.Math; export module Crafter.Math;
export import :BasicTypes; export import :BasicTypes;
export import :Vector; export import :Vector;
export import :MatrixCollumMajor;
export import :MatrixRowMajor; export import :MatrixRowMajor;
export import :Misc; export import :Misc;

View file

@ -1,30 +1,21 @@
{ {
"name": "crafter-math", "name": "crafter-match",
"configurations": [ "configurations": [
{ {
"name": "base", "name": "base",
"standard": "c++26", "interfaces": ["Crafter.Math-Vector", "Crafter.Math-BasicTypes", "Crafter.Math-MatrixRowMajor", "Crafter.Math-Misc", "Crafter.Math"],
"source_files": [], "implementations": []
"module_files": ["Crafter.Math-Vector", "Crafter.Math-BasicTypes", "Crafter.Math-MatrixCollumMajor", "Crafter.Math-MatrixRowMajor", "Crafter.Math-Misc", "Crafter.Math"], },
"build_dir": "build", {
"output_dir": "bin", "name": "lib",
"type":"library" "extends": ["base"],
"type":"library",
"dependencies": []
}, },
{ {
"name": "lib-debug", "name": "lib-debug",
"extends": ["base"], "extends": ["lib"],
"debug": true "debug": true
},
{
"name": "lib-release",
"extends": ["base"],
"optimization_level": "3"
},
{
"name": "develop",
"extends": ["debug"],
"type": "executable",
"source_files": ["main"]
} }
] ]
} }