Compare commits

...

11 Commits

Author SHA1 Message Date
40a301186e Merge pull request #40 from orange-cpp/u/orange/inverted-matrix
U/orange/inverted matrix
2025-04-29 20:53:02 +03:00
a41526c494 style fix 2025-04-29 20:52:41 +03:00
a0d1dc4313 added test case 2025-04-29 20:49:59 +03:00
1c5c9360c8 added inverse method 2025-04-29 20:33:39 +03:00
4615769682 added additional methods 2025-04-29 20:10:17 +03:00
4ef674f7b4 fixed infinite recursion in compile time 2025-04-29 20:08:27 +03:00
69b9049fb0 fixed gimba lock for unity 2025-04-26 00:52:46 +03:00
2734b58bdd fixed gimba lock for opengl camera 2025-04-26 00:32:53 +03:00
d7f1f49165 resetting state 2025-04-25 23:52:10 +03:00
94b1453cae removed .idea folder 2025-04-23 02:48:45 +03:00
3e67d8a99c added credits 2025-04-23 02:46:08 +03:00
9 changed files with 72 additions and 29 deletions

8
.idea/modules.xml generated
View File

@@ -1,8 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/uml.iml" filepath="$PROJECT_DIR$/.idea/uml.iml" />
</modules>
</component>
</project>

2
.idea/uml.iml generated
View File

@@ -1,2 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<module classpath="CMake" type="CPP_MODULE" version="4" />

7
.idea/vcs.xml generated
View File

@@ -1,7 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="" vcs="Git" />
<mapping directory="$PROJECT_DIR$/extlibs/googletest" vcs="Git" />
</component>
</project>

11
CREDITS.md Normal file
View File

@@ -0,0 +1,11 @@
# OMATH CREDITS
Thanks to everyone who made this possible, including:
- Saikari aka luadebug for VCPKG port.
And a big hand to everyone else who has contributed over the past!
THANKS! <3
-- Orange++ <orange-cpp@yandex.ru>

View File

@@ -253,21 +253,24 @@ namespace omath
if constexpr (Rows == 2) if constexpr (Rows == 2)
return At(0, 0) * At(1, 1) - At(0, 1) * At(1, 0); return At(0, 0) * At(1, 1) - At(0, 1) * At(1, 0);
else
if constexpr (Rows > 2)
{ {
Type det = 0; Type det = 0;
for (size_t i = 0; i < Columns; ++i) for (size_t column = 0; column < Columns; ++column)
{ {
const Type cofactor = (i % 2 == 0 ? 1 : -1) * At(0, i) * Minor(0, i).Determinant(); const Type cofactor = At(0, column) * AlgComplement(0, column);
det += cofactor; det += cofactor;
} }
return det; return det;
} }
std::unreachable();
} }
[[nodiscard]] [[nodiscard]]
constexpr Mat<Rows - 1, Columns - 1, Type, StoreType> Minor(const size_t row, const size_t column) const constexpr Mat<Rows - 1, Columns - 1, Type, StoreType> Strip(const size_t row, const size_t column) const
{ {
static_assert(Rows-1 > 0 && Columns-1 > 0);
Mat<Rows - 1, Columns - 1, Type, StoreType> result; Mat<Rows - 1, Columns - 1, Type, StoreType> result;
for (size_t i = 0, m = 0; i < Rows; ++i) for (size_t i = 0, m = 0; i < Rows; ++i)
{ {
@@ -285,6 +288,19 @@ namespace omath
return result; return result;
} }
[[nodiscard]]
constexpr Type Minor(const size_t row, const size_t column) const
{
return Strip(row, column).Determinant();
}
[[nodiscard]]
constexpr Type AlgComplement(const size_t row, const size_t column) const
{
const auto minor = Minor(row, column);
return (row + column + 2) % 2 == 0 ? minor: -minor;
}
[[nodiscard]] [[nodiscard]]
constexpr const std::array<Type, Rows * Columns>& RawArray() const constexpr const std::array<Type, Rows * Columns>& RawArray() const
{ {
@@ -294,7 +310,7 @@ namespace omath
[[nodiscard]] [[nodiscard]]
constexpr std::array<Type, Rows * Columns>& RawArray() constexpr std::array<Type, Rows * Columns>& RawArray()
{ {
return const_cast<std::array<Type, Rows * Columns>>(std::as_const(*this).RawArray()); return m_data;
} }
[[nodiscard]] [[nodiscard]]
@@ -343,6 +359,25 @@ namespace omath
}; };
} }
[[nodiscard]]
constexpr std::optional<Mat> Inverted() const
{
const auto det = Determinant();
if (det == 0)
return std::nullopt;
const auto transposed = Transposed();
Mat result;
for (std::size_t row = 0; row < Rows; row++)
for (std::size_t column = 0; column < Rows; column++)
result.At(row, column) = transposed.AlgComplement(row, column);
result /= det;
return {result};
}
private: private:
std::array<Type, Rows * Columns> m_data; std::array<Type, Rows * Columns> m_data;
}; };

View File

@@ -62,31 +62,37 @@ namespace omath::projection
void SetFieldOfView(const FieldOfView& fov) void SetFieldOfView(const FieldOfView& fov)
{ {
m_fieldOfView = fov; m_fieldOfView = fov;
m_viewProjectionMatrix = std::nullopt;
} }
void SetNearPlane(const float near) void SetNearPlane(const float near)
{ {
m_nearPlaneDistance = near; m_nearPlaneDistance = near;
m_viewProjectionMatrix = std::nullopt;
} }
void SetFarPlane(const float far) void SetFarPlane(const float far)
{ {
m_farPlaneDistance = far; m_farPlaneDistance = far;
m_viewProjectionMatrix = std::nullopt;
} }
void SetViewAngles(const ViewAnglesType& viewAngles) void SetViewAngles(const ViewAnglesType& viewAngles)
{ {
m_viewAngles = viewAngles; m_viewAngles = viewAngles;
m_viewProjectionMatrix = std::nullopt;
} }
void SetOrigin(const Vector3<float>& origin) void SetOrigin(const Vector3<float>& origin)
{ {
m_origin = origin; m_origin = origin;
m_viewProjectionMatrix = std::nullopt;
} }
void SetViewPort(const ViewPort& viewPort) void SetViewPort(const ViewPort& viewPort)
{ {
m_viewPort = viewPort; m_viewPort = viewPort;
m_viewProjectionMatrix = std::nullopt;
} }
[[nodiscard]] const FieldOfView& GetFieldOfView() const [[nodiscard]] const FieldOfView& GetFieldOfView() const

View File

@@ -32,9 +32,9 @@ namespace omath::opengl_engine
} }
Mat4x4 RotationMatrix(const ViewAngles& angles) Mat4x4 RotationMatrix(const ViewAngles& angles)
{ {
return MatRotationAxisZ<float, MatStoreType::COLUMN_MAJOR>(angles.roll) * return MatRotationAxisX<float, MatStoreType::COLUMN_MAJOR>(-angles.pitch) *
MatRotationAxisY<float, MatStoreType::COLUMN_MAJOR>(-angles.yaw) * MatRotationAxisY<float, MatStoreType::COLUMN_MAJOR>(-angles.yaw) *
MatRotationAxisX<float, MatStoreType::COLUMN_MAJOR>(-angles.pitch); MatRotationAxisZ<float, MatStoreType::COLUMN_MAJOR>(angles.roll);
} }
Mat4x4 CalcPerspectiveProjectionMatrix(const float fieldOfView, const float aspectRatio, const float near, Mat4x4 CalcPerspectiveProjectionMatrix(const float fieldOfView, const float aspectRatio, const float near,
const float far) const float far)

View File

@@ -32,9 +32,9 @@ namespace omath::unity_engine
} }
Mat4x4 RotationMatrix(const ViewAngles& angles) Mat4x4 RotationMatrix(const ViewAngles& angles)
{ {
return MatRotationAxisZ(angles.roll) * return MatRotationAxisX<float, MatStoreType::ROW_MAJOR>(angles.pitch) *
MatRotationAxisY(angles.yaw) * MatRotationAxisY<float, MatStoreType::ROW_MAJOR>(angles.yaw) *
MatRotationAxisX(angles.pitch); MatRotationAxisZ<float, MatStoreType::ROW_MAJOR>(angles.roll);
} }
Mat4x4 CalcPerspectiveProjectionMatrix(const float fieldOfView, const float aspectRatio, const float near, Mat4x4 CalcPerspectiveProjectionMatrix(const float fieldOfView, const float aspectRatio, const float near,
const float far) const float far)

View File

@@ -180,10 +180,10 @@ TEST(UnitTestMatStandalone, Determinant_3x3)
} }
// Test Minor for 3x3 matrix // Test Minor for 3x3 matrix
TEST(UnitTestMatStandalone, Minor_3x3) TEST(UnitTestMatStandalone, Strip_3x3)
{ {
constexpr Mat<3, 3> m{{3, 0, 2}, {2, 0, -2}, {0, 1, 1}}; constexpr Mat<3, 3> m{{3, 0, 2}, {2, 0, -2}, {0, 1, 1}};
auto minor = m.Minor(0, 0); auto minor = m.Strip(0, 0);
EXPECT_EQ(minor.RowCount(), 2); EXPECT_EQ(minor.RowCount(), 2);
EXPECT_EQ(minor.ColumnsCount(), 2); EXPECT_EQ(minor.ColumnsCount(), 2);
EXPECT_FLOAT_EQ(minor.At(0, 0), 0.0f); EXPECT_FLOAT_EQ(minor.At(0, 0), 0.0f);
@@ -206,3 +206,11 @@ TEST(UnitTestMatStandalone, Transpose_NonSquare)
EXPECT_FLOAT_EQ(transposed.At(1, 1), 5.0f); EXPECT_FLOAT_EQ(transposed.At(1, 1), 5.0f);
EXPECT_FLOAT_EQ(transposed.At(2, 1), 6.0f); EXPECT_FLOAT_EQ(transposed.At(2, 1), 6.0f);
} }
TEST(UnitTestMatStandalone, Enverse)
{
constexpr Mat<2, 2> m{{1.0f, 3.0f}, {2.0f, 5.0f}};
constexpr Mat<2,2> mv{{-5.0f, 3.0f}, {2.0f, -1.0f}};
EXPECT_EQ(mv, m.Inverted());
}