Compare commits

...

3 Commits

Author SHA1 Message Date
Mrna
87998a4bc3 Added Matrix class 2025-10-07 09:35:52 +02:00
Mrna
c05d198ad0 Provide MSVC alternative for __PRETTY_FUNCTION__ 2025-10-06 14:08:56 +02:00
Mrna
590624fea0 Added missing header (win compiler complained) 2025-10-06 14:07:15 +02:00
2 changed files with 127 additions and 124 deletions

View File

@ -2,13 +2,21 @@
#include <iostream> #include <iostream>
#define LOG_CRITICAL(...) Log::critical(__PRETTY_FUNCTION__, ": ", __VA_ARGS__) #if defined(__GNUC__) || defined(__clang__)
#define LOG_ERROR(...) Log::error(__PRETTY_FUNCTION__, ": ", __VA_ARGS__) # define PRETTY_FUNC __PRETTY_FUNCTION__
#define LOG_WARNING(...) Log::warning(__PRETTY_FUNCTION__, ": ", __VA_ARGS__) #elif defined(_MSC_VER)
#define LOG_INFO(...) Log::info(__PRETTY_FUNCTION__, ": ", __VA_ARGS__) # define PRETTY_FUNC __FUNCTION__
#define LOG_DEBUG(...) Log::debug(__PRETTY_FUNCTION__, ": ", __VA_ARGS__) #else
# define PRETTY_FUNC __func__
#endif
#define LOG_CRITICAL(...) Log::critical(PRETTY_FUNC, ": ", __VA_ARGS__)
#define LOG_ERROR(...) Log::error(PRETTY_FUNC, ": ", __VA_ARGS__)
#define LOG_WARNING(...) Log::warning(PRETTY_FUNC, ": ", __VA_ARGS__)
#define LOG_INFO(...) Log::info(PRETTY_FUNC, ": ", __VA_ARGS__)
#define LOG_DEBUG(...) Log::debug(PRETTY_FUNC, ": ", __VA_ARGS__)
#define LOG_PROFILING_DEBUG(...) \ #define LOG_PROFILING_DEBUG(...) \
Log::profiling_debug(__PRETTY_FUNCTION__, ": ", __VA_ARGS__) Log::profiling_debug(PRETTY_FUNC, ": ", __VA_ARGS__)
namespace Log { namespace Log {
enum class LevelTypes { enum class LevelTypes {

View File

@ -9,6 +9,7 @@
#include <numeric> #include <numeric>
#include <ranges> #include <ranges>
#include <utility> #include <utility>
#include <functional>
template <typename T> template <typename T>
requires std::floating_point<T> requires std::floating_point<T>
@ -37,6 +38,10 @@ public:
requires(std::same_as<ArgsT, T> && ...) && (sizeof...(ArgsT) == N) requires(std::same_as<ArgsT, T> && ...) && (sizeof...(ArgsT) == N)
vec(ArgsT... args) : m_Array{args...} {} vec(ArgsT... args) : m_Array{args...} {}
//
// Access to elements & data
//
const T &operator[](size_t index) const { const T &operator[](size_t index) const {
// we leave run-time checks to the underlying std::array // we leave run-time checks to the underlying std::array
return m_Array[index]; return m_Array[index];
@ -56,6 +61,8 @@ public:
return os; return os;
} }
std::array<T,N>& Data() { return m_Array; }
// //
// binary operators // binary operators
// //
@ -185,6 +192,19 @@ public:
return tmp; return tmp;
} }
static T DotProduct(const vec& a, const vec& b)
{
return std::inner_product(
a.m_Array.begin(), a.m_Array.end(), b.m_Array.begin(),
T{}, std::plus{}, std::multiplies{});
}
T DotProduct(const vec& b) const
{
const auto& a = *this;
return DotProduct(a, b);
}
// //
// Helpers // Helpers
// //
@ -263,7 +283,6 @@ using WorldSize = vec<float, 2, WorldSizeTag>;
using WindowSize = vec<float, 2, WindowSizeTag>; using WindowSize = vec<float, 2, WindowSizeTag>;
using TileSize = vec<int32_t, 2, TileSizeTag>; using TileSize = vec<int32_t, 2, TileSizeTag>;
// //
// Utils // Utils
// //
@ -276,121 +295,97 @@ struct TilePosHash {
} }
}; };
// old stuff - TODO delete //
// Matrix
//
// constexpr double EQUALITY_LIMIT = 1e-6; // Collumn major square matrix
// template <typename T> struct Vec2D { template <typename T, size_t N, typename Tag = Any>
// public: class Matrix {
// Vec2D() = default;
// ~Vec2D() = default; using vec_type = vec<T, N, Tag>;
//
// template <typename U> public:
// Vec2D(Vec2D<U> other) { Matrix() = default;
// this->x = static_cast<T>(other.x);
// this->y = static_cast<T>(other.y); // Initialization using flat array of N*N elements
// } template <typename Tarr, size_t M>
// requires (M == N*N && std::same_as<Tarr, T>)
// Vec2D& operator+=(const Vec2D &other) { Matrix(std::array<Tarr,M> array) : m_Array{}
// x += other.x; {
// y += other.y; std::size_t idx = 0;
// return *this; for (auto col : array | std::views::chunk(N))
// } {
// std::ranges::copy(col, m_Array[idx++].Data().begin());
// template <typename U> }
// requires std::is_arithmetic_v<U> }
// Vec2D& operator/=(U k) {
// this->x /= static_cast<T>(k); const vec_type& operator[](size_t index) const { return m_Array[index]; }
// this->y /= static_cast<T>(k); vec_type& operator[](size_t index) { return m_Array[index]; }
// return *this;
// } friend std::ostream &operator<<(std::ostream &os, const Matrix &obj)
// {
// friend Vec2D operator+(const Vec2D &a, const Vec2D &b) { os << "( ";
// return Vec2D{a.x + b.x, a.y + b.y}; for (const auto &element : obj.m_Array) {
// } os << element << " ";
// }
// friend Vec2D operator-(const Vec2D &a, const Vec2D &b) { os << ")";
// return Vec2D{a.x - b.x, a.y - b.y}; return os;
// } }
//
// template <typename U>
// requires std::is_arithmetic_v<U> friend Matrix operator+(const Matrix& A, const Matrix& B)
// friend Vec2D operator*(U k, const Vec2D &v) {
// { Matrix C;
// return Vec2D{k * v.x, k * v.y}; std::ranges::transform(A.m_Array, B.m_Array, C.m_Array.begin(), std::plus{});
// } return C;
// }
// template <typename U>
// requires std::is_arithmetic_v<U> friend Matrix operator-(const Matrix& A, const Matrix& B)
// friend Vec2D operator/(const Vec2D &v, U k) {
// { Matrix C;
// return Vec2D{v.x / k, v.y / k}; std::ranges::transform(A.m_Array, B.m_Array, C.m_Array.begin(), std::minus{});
// } return C;
// }
// friend bool operator==(const Vec2D &a, const Vec2D &b) {
// if constexpr (std::is_integral_v<T>) { friend Matrix operator*(const Matrix& A, const Matrix& B)
// return a.x == b.x && a.y == b.y; {
// } else if constexpr (std::is_floating_point_v<T>) { Matrix C;
// return a.distance(b) < EQUALITY_LIMIT;
// } else { for (size_t i = 0; i < N; i++)
// static_assert("Unhandled comparison"); {
// } for (size_t j = 0; j < N; j++)
// } {
// T sum = 0;
// Vec2D operator*(float b) const { return Vec2D{b * x, b * y}; } for (size_t k = 0; k < N; ++k) sum += A[i][k] * B[k][j];
// C[i][j] = sum;
// T distance_squared(const Vec2D &other) const { }
// T dx = x - other.x; }
// T dy = y - other.y; return C;
// return dx * dx + dy * dy; }
// }
// friend vec_type operator*(const Matrix& A, const vec_type& b)
// T distance(const Vec2D &other) const {
// requires std::floating_point<T> // we assume that b is row vector
// { vec_type c;
// return sqrt(distance_squared(other)); for (size_t i = 0; i < N; i++)
// } {
// c[i] = b.DotProduct(A[i]);
// void normalize() }
// requires std::floating_point<T> return c;
// { }
// auto length = sqrt(x * x + y * y);
// if (length < EQUALITY_LIMIT) { static constexpr Matrix Eye()
// x = y = 0; {
// } else { Matrix E;
// x /= length; for (size_t i = 0; i < N; i++)
// y /= length; {
// } E[i][i] = T{1};
// } }
// return E;
// Vec2D normalized() }
// requires std::floating_point<T>
// {
// Vec2D v(*this); private:
// v.normalize(); std::array<vec_type, N> m_Array;
// return v; };
// }
//
// Vec2D orthogonal() const
// {
// Vec2D v(*this);
//
// std::swap(v.x, v.y);
// v.x = -v.x;
// return v;
// }
//
// template <typename U> Vec2D(std::initializer_list<U> list) {
// assert(list.size() == 2);
// auto first_element = *list.begin();
// auto second_element = *(list.begin() + 1);
// x = static_cast<T>(first_element);
// y = static_cast<T>(second_element);
// }
//
// T x, y;
//
// friend std::ostream &operator<<(std::ostream &os, const Vec2D &obj) {
// os << "( " << obj.x << ", " << obj.y << ")";
// return os;
// }
// };