Compare commits

..

No commits in common. "87998a4bc34ec0efcc80fbf9c2c6f0c40be9f606" and "a52d6709037d0b85bce9c10041b982d75a486b5e" have entirely different histories.

2 changed files with 123 additions and 126 deletions

View File

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

View File

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