8000 Global improvement by orange-cpp · Pull Request #15 · orange-cpp/omath · GitHub
[go: up one dir, main page]

Skip to content

Global improvement #15

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 24 commits into from
Dec 16, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
modified output dir
  • Loading branch information
orange-cpp committed Nov 30, 2024
commit 7e29ea339e6d48df9d4d618bbe711a269abf790e
3 changes: 3 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ project(omath VERSION 1.0.0)

set(CMAKE_CXX_STANDARD 26)

set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_SOURCE_DIR}/out/${CMAKE_BUILD_TYPE}")
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${CMAKE_SOURCE_DIR}/out/${CMAKE_BUILD_TYPE}")

option(OMATH_BUILD_TESTS "Build unit tests" ON)
option(OMATH_THREAT_WARNING_AS_ERROR "Set highest level of warnings and force compiler to treat them as errors" ON)
option(OMATH_BUILD_AS_SHARED_LIBRARY "Build Omath as .so or .dll" OFF)
Expand Down
103 changes: 46 additions & 57 deletions source/Matrix.cpp

Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
#include "omath/Matrix.hpp"
#include "omath/Vector3.hpp"
#include "omath/Angles.hpp"
#include "omath/Vector3.hpp"


#include <complex>
#include <format>
#include <utility>
#include <stdexcept>
#include <utility>
#include <complex>

namespace omath
Expand All @@ -31,23 +30,23 @@ namespace omath
m_columns = rows.begin()->size();


for (const auto& row : rows)
for (const auto& row: rows)
if (row.size() != m_columns)
throw std::invalid_argument("All rows must have the same number of columns.");

m_data = std::make_unique<float[]>(m_rows * m_columns);

size_t i = 0;
for (const auto& row : rows)
for (const auto& row: rows)
{
size_t j = 0;
for (const auto& value : row)
for (const auto& value: row)
At(i, j++) = value;
++i;
}
}

Matrix::Matrix(const Matrix &other)
Matrix::Matrix(const Matrix& other)
{
m_rows = other.m_rows;
m_columns = other.m_columns;
Expand All @@ -59,25 +58,24 @@ namespace omath
At(i, j) = other.At(i, j);
}

Matrix::Matrix(const size_t rows, const size_t columns, const float *pRaw)
Matrix::Matrix(const size_t rows, const size_t columns, const float* pRaw)
{
m_rows = rows;
m_columns = columns;


m_data = std::make_unique<float[]>(m_rows * m_columns);

for (size_t i = 0; i < rows*columns; ++i)
for (size_t i = 0; i < rows * columns; ++i)
At(i / rows, i % columns) = pRaw[i];

}

size_t Matrix::RowCount() const noexcept
{
return m_rows;
}

Matrix::Matrix(Matrix &&other) noexcept
Matrix::Matrix(Matrix&& other) noexcept
{
m_rows = other.m_rows;
m_columns = other.m_columns;
Expand All @@ -99,7 +97,7 @@ namespace omath
return {RowCount(), ColumnsCount()};
}

float &Matrix::At(const size_t iRow, const size_t iCol)
float& Matrix::At(const size_t iRow, const size_t iCol)
{
return const_cast<float&>(std::as_const(*this).At(iRow, iCol));
}
Expand All @@ -115,12 +113,12 @@ namespace omath
return sum;
}

const float &Matrix::At(const size_t iRow, const size_t iCol) const
const float& Matrix::At(const size_t iRow, const size_t iCol) const
{
return m_data[iRow * m_columns + iCol];
}

Matrix Matrix::operator*(const Matrix &other) const
Matrix Matrix::operator*(const Matrix& other) const
{
if (m_columns != other.m_rows)
throw std::runtime_error("n != m");
Expand All @@ -136,7 +134,7 @@ namespace omath
return outMat;
}

Matrix & Matrix::operator*=(const Matrix &other)
Matrix& Matrix::operator*=(const Matrix& other)
{
*this = *this * other;
return *this;
Expand All @@ -152,7 +150,7 @@ namespace omath
return out;
}

Matrix &Matrix::operator*=(const float f)
Matrix& Matrix::operator*=(const float f)
{
for (size_t i = 0; i < RowCount(); i++)
for (size_t j = 0; j < ColumnsCount(); j++)
Expand All @@ -164,8 +162,8 @@ namespace omath
{
Set(0.f);
}
Matrix &Matrix::operator=(const Matrix &other)

Matrix& Matrix::operator=(const Matrix& other)
{
if (this == &other)
return *this;
Expand All @@ -175,10 +173,9 @@ namespace omath
At(i, j) = other.At(i, j);

return *this;

}

Matrix &Matrix::operator=(Matrix &&other) noexcept
Matrix& Matrix::operator=(Matrix&& other) noexcept
{
if (this == &other)
return *this;
Expand All @@ -191,10 +188,9 @@ namespace omath
other.m_columns = 0;

return *this;

}

Matrix &Matrix::operator/=(const float f)
Matrix& Matrix::operator/=(const float f)
{
for (size_t i = 0; i < m_rows; ++i)
for (size_t j = 0; j < m_columns; ++j)
Expand All @@ -221,9 +217,9 @@ namespace omath
{
for (size_t j = 0; j < m_columns; ++j)
{
str += std::format("{:.1f}",At(i, j));
str += std::format("{:.1f}", At(i, j));

if (j == m_columns-1)
if (j == m_columns - 1)
str += '\n';
else
str += ' ';
Expand Down Expand Up @@ -306,59 +302,52 @@ namespace omath

Matrix Matrix::ToScreenMatrix(const float screenWidth, const float screenHeight)
{
return
{
{screenWidth / 2.f, 0.f, 0.f, 0.f},
{0.f, -screenHeight / 2.f, 0.f, 0.f},
{0.f, 0.f, 1.f, 0.f},
{screenWidth / 2.f, screenHeight / 2.f, 0.f, 1.f},
};
return {
{screenWidth / 2.f, 0.f, 0.f, 0.f},
{0.f, -screenHeight / 2.f, 0.f, 0.f},
{0.f, 0.f, 1.f, 0.f},
{screenWidth / 2.f, screenHeight / 2.f, 0.f, 1.f},
};
}

Matrix Matrix::TranslationMatrix(const Vector3 &diff)
Matrix Matrix::TranslationMatrix(const Vector3& diff)
{
return
{
{1.f, 0.f, 0.f, 0.f},
{0.f, 1.f, 0.f, 0.f},
{0.f, 0.f, 1.f, 0.f},
{diff.x, diff.y, diff.z, 1.f},
return {
{1.f, 0.f, 0.f, 0.f},
{0.f, 1.f, 0.f, 0.f},
{0.f, 0.f, 1.f, 0.f},
{diff.x, diff.y, diff.z, 1.f},
};
}

Matrix Matrix::OrientationMatrix(const Vector3 &forward, const Vector3 &right, const Vector3 &up)
Matrix Matrix::OrientationMatrix(const Vector3& forward, const Vector3& right, const Vector3& up)
{
return
{
{right.x, up.x, forward.x, 0.f},
{right.y, up.y, forward.y, 0.f},
{right.z, up.z, forward.z, 0.f},
{0.f, 0.f, 0.f, 1.f},
return {
{right.x, up.x, forward.x, 0.f},
{right.y, up.y, forward.y, 0.f},
{right.z, up.z, forward.z, 0.f},
{0.f, 0.f, 0.f, 1.f},
};
}

Matrix Matrix::ProjectionMatrix(const float fieldOfView, const float aspectRatio, const float near,
const float far)
Matrix Matrix::ProjectionMatrix(const float fieldOfView, const float aspectRatio, const float near, const float far)
{
const float fovHalfTan = std::tan(angles::DegreesToRadians(fieldOfView) / 2.f);

return
{
{1.f / (aspectRatio*fovHalfTan), 0.f, 0.f, 0.f},
{0.f, 1.f / fovHalfTan, 0.f, 0.f},
{0.f, 0.f, (far + near) / (far - near), 2.f * near * far / (far - near)},
{0.f, 0.f, -1.f, 0.f}
};
return {{1.f / (aspectRatio * fovHalfTan), 0.f, 0.f, 0.f},
{0.f, 1.f / fovHalfTan, 0.f, 0.f},
{0.f, 0.f, (far + near) / (far - near), 2.f * near * far / (far - near)},
{0.f, 0.f, -1.f, 0.f}};
}

const float* Matrix::Raw() const
{
return m_data.get();
}

void Matrix::SetDataFromRaw(const float *pRawMatrix)
void Matrix::SetDataFromRaw(const float* pRawMatrix)
{
for (size_t i = 0; i < m_columns*m_rows; ++i)
for (size_t i = 0; i < m_columns * m_rows; ++i)
At(i / m_rows, i % m_columns) = pRawMatrix[i];
}

Expand All @@ -368,4 +357,4 @@ namespace omath
m_rows = 0;
m_data = nullptr;
}
}
} // namespace omath
0