diff options
author | Stanislaw Halik <sthalik@misaki.pl> | 2023-10-09 07:59:21 +0200 |
---|---|---|
committer | Stanislaw Halik <sthalik@misaki.pl> | 2023-10-09 07:59:21 +0200 |
commit | 92124b599a4de4ea072bc80156bd7d1da53d2ef4 (patch) | |
tree | 93bc0a1b5a599be32c8fec4dbc88ab1890ccc4bd | |
parent | 3a16258e42b6f198c2fb4bfde45c66ee405adac4 (diff) |
a
-rw-r--r-- | compat/assert.hpp | 12 | ||||
-rw-r--r-- | src/global-coords.hpp | 32 | ||||
-rw-r--r-- | src/object.cpp | 5 | ||||
-rw-r--r-- | src/object.hpp | 1 | ||||
-rw-r--r-- | src/point.cpp | 23 | ||||
-rw-r--r-- | src/point.hpp | 3 | ||||
-rw-r--r-- | test/app.hpp | 1 | ||||
-rw-r--r-- | test/coords.cpp | 28 | ||||
-rw-r--r-- | test/main.cpp | 1 |
9 files changed, 100 insertions, 6 deletions
diff --git a/compat/assert.hpp b/compat/assert.hpp index 8cbb55be..d9ff2cbe 100644 --- a/compat/assert.hpp +++ b/compat/assert.hpp @@ -1,4 +1,5 @@ #pragma once +#include "defs.hpp" #include <cstdlib> #include <cstdio> #include <type_traits> @@ -86,6 +87,17 @@ } \ } while (false) +#define fm_assert_equal(...) \ + ([](auto a, auto b) -> void \ + { \ + if (a != b) [[unlikely]] \ + { \ + DBG_nospace << "fatal: '" << a << "' != '" << b \ + << "' in " << __FILE__ << ":" << __LINE__; \ + fm_EMIT_ABORT(); \ + } \ + })(__VA_ARGS__) + #ifdef __GNUG__ # pragma GCC diagnostic pop #endif diff --git a/src/global-coords.hpp b/src/global-coords.hpp index 017d7c90..e5218b17 100644 --- a/src/global-coords.hpp +++ b/src/global-coords.hpp @@ -14,12 +14,18 @@ struct chunk_coords final { constexpr bool operator==(const chunk_coords& other) const noexcept = default; template<typename T> - requires (std::is_floating_point_v<T> || std::is_integral_v<T> && (sizeof(T) > sizeof(x) || std::is_same_v<T, std::decay_t<decltype(x)>>)) - explicit constexpr operator Math::Vector2<T>() const noexcept { return Math::Vector2<T>(T(x), T(y)); } + explicit constexpr operator Math::Vector2<T>() const noexcept + { + static_assert(std::is_signed_v<T> && sizeof(T) >= sizeof(int16_t)); + return Math::Vector2<T>(T(x), T(y)); + } template<typename T> - requires (std::is_floating_point_v<T> || std::is_integral_v<T> && (sizeof(T) > sizeof(x) || std::is_same_v<T, std::decay_t<decltype(x)>>)) - explicit constexpr operator Math::Vector3<T>() const noexcept { return Math::Vector3<T>(T(x), T(y), T(0)); } + explicit constexpr inline operator Math::Vector3<T>() const noexcept + { + static_assert(std::is_signed_v<T> && sizeof(T) >= sizeof(int16_t)); + return Math::Vector3<T>(T(x), T(y), T(0)); + } constexpr Vector2i operator-(chunk_coords other) const noexcept { return Vector2i{x - other.x, y - other.y }; } }; @@ -61,7 +67,13 @@ struct chunk_coords_ final { constexpr Vector3i operator-(chunk_coords_ other) const noexcept { return Vector3i{x - other.x, y - other.y, z - other.z}; } - explicit operator chunk_coords() const noexcept { return chunk_coords{x, y}; } + explicit constexpr inline operator chunk_coords() const noexcept { return chunk_coords{x, y}; } + + template<typename T> explicit constexpr inline operator Math::Vector3<T>() const noexcept + { + static_assert(std::is_signed_v<T> && sizeof(T) >= sizeof(int16_t)); + return Math::Vector3<T>(x, y, z); + } }; constexpr inline int8_t chunk_z_min = -1, chunk_z_max = 14; @@ -99,13 +111,15 @@ public: constexpr local_coords local() const noexcept; constexpr chunk_coords chunk() const noexcept; - constexpr operator chunk_coords_() const noexcept; // todo make invoking this take less typing + constexpr chunk_coords_ chunk3() const noexcept { return chunk_coords_(*this); } + constexpr operator chunk_coords_() const noexcept; constexpr raw_coords_ raw() const noexcept; constexpr raw_coords raw() noexcept; constexpr int8_t z() const noexcept; constexpr Vector2i to_signed() const noexcept; constexpr Vector3i to_signed3() const noexcept; + template<typename T> explicit constexpr inline operator Math::Vector3<T>() const noexcept; constexpr bool operator==(const global_coords& other) const noexcept = default; constexpr global_coords operator+(Vector2i vec) const noexcept; @@ -143,6 +157,12 @@ constexpr Vector2i global_coords::to_signed() const noexcept return { int32_t((x & ~z_mask::value) - (s0::value<<4)), int32_t(y - (s0::value<<4)), }; } +template<typename T> constexpr global_coords::operator Math::Vector3<T>() const noexcept +{ + static_assert(std::is_signed_v<T> && sizeof(T) >= sizeof(int32_t)); + return Math::Vector3<T>(to_signed3()); +} + constexpr Vector3i global_coords::to_signed3() const noexcept { return Vector3i(to_signed(), z()); diff --git a/src/object.cpp b/src/object.cpp index ee21c14f..d5133337 100644 --- a/src/object.cpp +++ b/src/object.cpp @@ -132,6 +132,11 @@ point object::normalize_coords(global_coords coord, Vector2b cur_offset, Vector2 return { coord, Vector2b(off_new) }; } +point object::normalize_coords(const point& pt, Vector2i delta) +{ + return object::normalize_coords(pt.coord, pt.offset, delta); +} + template<bool neighbor = true> static bool do_search(struct chunk* c, chunk_coords_ coord, object_id id, Vector2 min, Vector2 max, Vector2b off = {}) { diff --git a/src/object.hpp b/src/object.hpp index 3832572b..bd5066b9 100644 --- a/src/object.hpp +++ b/src/object.hpp @@ -77,6 +77,7 @@ struct object object_type type_of() const noexcept; static point normalize_coords(global_coords coord, Vector2b cur_offset, Vector2i delta); + static point normalize_coords(const point& pt, Vector2i delta); virtual bool is_dynamic() const; bool can_rotate(rotation new_r); diff --git a/src/point.cpp b/src/point.cpp new file mode 100644 index 00000000..23049a14 --- /dev/null +++ b/src/point.cpp @@ -0,0 +1,23 @@ +#include "point.hpp" + +namespace floormat { + +Debug& operator<<(Debug& dbg, const point& pt) +{ + const auto flags = dbg.flags(); + dbg.setFlags(flags | Debug::Flag::NoSpace); + + auto c = Vector3i(chunk_coords_{pt.coord}); + auto t = Vector2i(pt.coord.local()); + + dbg << "point( "; + dbg << c << ", "; + dbg << t << ", "; + dbg << pt.offset; + dbg << " )"; + + dbg.setFlags(flags); + return dbg; +} + +} // namespace floormat diff --git a/src/point.hpp b/src/point.hpp index 99c4acdf..b24f1015 100644 --- a/src/point.hpp +++ b/src/point.hpp @@ -1,5 +1,6 @@ #pragma once #include "global-coords.hpp" +#include <Corrade/Utility/Move.h> #include <compare> namespace floormat { @@ -12,6 +13,8 @@ struct point constexpr bool operator==(const point&) const = default; friend constexpr std::strong_ordering operator<=>(const point& a, const point& b) noexcept; + + friend Debug& operator<<(Debug& dbg, const point& pt); }; constexpr std::strong_ordering operator<=>(const point& p1, const point& p2) noexcept diff --git a/test/app.hpp b/test/app.hpp index ef98a8b4..3544ed1b 100644 --- a/test/app.hpp +++ b/test/app.hpp @@ -44,6 +44,7 @@ struct test_app final : private FM_APPLICATION static void test_hash(); static void test_path_search_node_pool(); static void test_dijkstra(); + static void test_coords(); static void zzz_test_misc(); }; } // namespace floormat diff --git a/test/coords.cpp b/test/coords.cpp new file mode 100644 index 00000000..f673e7cd --- /dev/null +++ b/test/coords.cpp @@ -0,0 +1,28 @@ +#include "app.hpp" +#include "src/object.hpp" + +namespace floormat { + +namespace { + +constexpr auto norm = [](const point& pt, Vector2i delta) { return object::normalize_coords(pt, delta); }; + +void test_normalize_point() +{ + auto a = point{{{ 0, 0, 0}, { 0, 0}}, { 0, 0} }, + b = point{{{ 0, -1, 0}, {15, 15}}, { 0, 0} }, + c = point{{{-1, 1, 1}, { 0, 0}}, { 1, 31} }, + d = point{{{ 1, 0, 1}, {15, 15}}, {-31, 31} }, + e = point{{{ 1, 0, 1}, {15, 15}}, {-31, 31} }; + + fm_assert_equal(norm(a, {}), point{{{ 0, 0, 0}, { 0, 0}}, { 0, 0} }); +} + +} // namespace + +void test_app::test_coords() +{ + test_normalize_point(); +} + +} // namespace floormat diff --git a/test/main.cpp b/test/main.cpp index 57dd8fc5..7855a87a 100644 --- a/test/main.cpp +++ b/test/main.cpp @@ -34,6 +34,7 @@ int test_app::exec() test_hash(); test_path_search_node_pool(); test_dijkstra(); + test_coords(); zzz_test_misc(); return 0; |