diff options
author | Stanislaw Halik <sthalik@misaki.pl> | 2023-11-22 15:29:40 +0100 |
---|---|---|
committer | Stanislaw Halik <sthalik@misaki.pl> | 2023-11-22 15:29:40 +0100 |
commit | a91f743cea0e72b7ef7c18683d51aa603407afa2 (patch) | |
tree | 750a924144ef2b1a97fa532dee27589b9f049625 | |
parent | 71461bb69d54cf461d7c1222b09094c6fc5c73fc (diff) |
a
-rw-r--r-- | compat/math.hpp | 88 | ||||
-rw-r--r-- | shaders/lightmap.cpp | 4 | ||||
-rw-r--r-- | src/critter.cpp | 1 | ||||
-rw-r--r-- | src/dijkstra.cpp | 3 | ||||
-rw-r--r-- | test/math.cpp | 64 | ||||
-rw-r--r-- | test/wall-atlas.cpp | 5 |
6 files changed, 35 insertions, 130 deletions
diff --git a/compat/math.hpp b/compat/math.hpp deleted file mode 100644 index fff2a234..00000000 --- a/compat/math.hpp +++ /dev/null @@ -1,88 +0,0 @@ -#pragma once - -#include <cstdlib> -#include <bit> -#include <cmath> - -namespace floormat::math::detail { - -template<typename T> struct int_type_for_; - -template<> struct int_type_for_<float> { using type = int32_t; }; -template<> struct int_type_for_<double> { using type = int64_t; }; -template<typename T> using int_type_for = typename int_type_for_<T>::type; - -} // namespace floormat::math::detail - -namespace floormat::math { - -template<typename T> -constexpr inline T abs(T x) -requires std::is_arithmetic_v<T> -{ - static_assert(std::is_floating_point_v<T> || - std::is_integral_v<T> && std::is_signed_v<T>); - return x < T{0} ? -x : x; -} - -template <typename T> -requires std::is_arithmetic_v<T> -constexpr inline T sgn(T val) -{ - return T(T{0} < val) - T(val < T{0}); -} - -template<typename T> -constexpr inline T sqrt(T x0) -requires std::is_floating_point_v<T> -{ - if (std::is_constant_evaluated()) - { - auto x = x0, prev = T{0}; - while (x != prev) - { - prev = x; - x = T(0.5) * (x + x0 / x); - } - return x; - } - else - return std::sqrt(x0); -} - -template<typename T> -requires std::is_integral_v<T> -constexpr inline double sqrt(T x) -{ - return sqrt(double(x)); -} - -template<typename T> -requires std::is_floating_point_v<T> -constexpr inline T ceil(T x) -{ - if (std::is_constant_evaluated()) - { - using int_ = detail::int_type_for<T>; - const auto x0 = int_(x); - return x0 + int_{1} * (x > x0); - } - else - return std::ceil(x); -} - -template<typename T> -requires std::is_floating_point_v<T> -constexpr inline T floor(T x) -{ - if (std::is_constant_evaluated()) - { - using int_ = detail::int_type_for<T>; - const auto x0 = int_(x); - return x0 - int_{1} * (x < T{0} && x != x0); - } - else - return std::floor(x); -} - -} // namespace floormat::math diff --git a/shaders/lightmap.cpp b/shaders/lightmap.cpp index d782a376..30c98055 100644 --- a/shaders/lightmap.cpp +++ b/shaders/lightmap.cpp @@ -1,6 +1,5 @@ #include "shaders/lightmap.hpp" #include "compat/assert.hpp" -#include "compat/math.hpp" #include "src/tile-defs.hpp" #include "src/chunk.hpp" #include "src/tile-bbox.hpp" @@ -12,6 +11,7 @@ #include <Corrade/Containers/PairStl.h> #include <Corrade/Containers/Iterable.h> #include <Corrade/Containers/ArrayViewStl.h> +#include <Magnum/Math/Functions.h> #include <Magnum/GL/Context.h> #include <Magnum/GL/MeshView.h> #include <Magnum/GL/Shader.h> @@ -30,7 +30,7 @@ constexpr float fuzz_pixels = 4; constexpr float shadow_wall_depth = 8; constexpr float real_image_size = 1024; -constexpr auto half_neighbors = (int)math::ceil(neighbor_count/2.f); +constexpr auto half_neighbors = (int)Math::ceil(neighbor_count/2.f); constexpr auto image_size = TILE_SIZE2 * TILE_MAX_DIM * neighbor_count; constexpr auto chunk_size = TILE_SIZE2 * TILE_MAX_DIM; diff --git a/src/critter.cpp b/src/critter.cpp index a24c6fea..4cb2c730 100644 --- a/src/critter.cpp +++ b/src/critter.cpp @@ -5,7 +5,6 @@ #include "src/object.hpp" #include "shaders/shader.hpp" #include "compat/exception.hpp" -#include "compat/math.hpp" #include <cmath> #include <utility> #include <algorithm> diff --git a/src/dijkstra.cpp b/src/dijkstra.cpp index d5ad2842..92bfeffb 100644 --- a/src/dijkstra.cpp +++ b/src/dijkstra.cpp @@ -2,7 +2,6 @@ #include "compat/format.hpp" #include "object.hpp" #include "point.hpp" -#include "compat/math.hpp" #include <cstdio> #include <Corrade/Containers/StaticArray.h> #include <Magnum/Math/Vector2.h> @@ -87,7 +86,7 @@ inline uint32_t distance(point a, point b) Vector2i dist; dist += (a.coord() - b.coord())*iTILE_SIZE2; dist += Vector2i(a.offset()) - Vector2i(b.offset()); - return (uint32_t)Math::ceil(Math::sqrt(dist.dot())); + return (uint32_t)Math::ceil(Math::sqrt(Vector2(dist).dot())); } inline uint32_t distance_l2(point a, point b) diff --git a/test/math.cpp b/test/math.cpp index d70a64fb..6535e998 100644 --- a/test/math.cpp +++ b/test/math.cpp @@ -1,5 +1,5 @@ #include "app.hpp" -#include "compat/math.hpp" +#include <Magnum/Math/Functions.h> namespace floormat { @@ -10,7 +10,7 @@ constexpr bool test_double_sqrt() using F = double; constexpr auto eps = F(1e-11); - static_assert(math::abs(math::sqrt((F)3) - (F)1.73205080757) < eps); + static_assert(Math::abs(Math::sqrt((F)3) - (F)1.73205080757) < eps); return true; } @@ -21,22 +21,22 @@ bool test_sqrt() constexpr auto test = [](double x) { auto x_ = (F)x; - auto y1 = math::sqrt(x_); + auto y1 = Math::sqrt(x_); auto y2 = std::sqrt(x_); - return math::abs(y1 - y2) < eps; + return Math::abs(y1 - y2) < eps; }; - static_assert(math::abs(math::sqrt((F)0) - (F)0) < eps); - static_assert(math::abs(math::sqrt((F)0.5) - (F)0.70710678118) < eps); - static_assert(math::abs(math::sqrt((F)1e-8) - (F)0.0001) < eps); - static_assert(math::abs(math::sqrt((F)2) - (F)1.41421356237) < eps); - static_assert(math::abs(math::sqrt((F)3) - (F)1.73205080757) < (F)1e-6); + static_assert(Math::abs(Math::sqrt((F)0) - (F)0) < eps); + static_assert(Math::abs(Math::sqrt((F)0.5) - (F)0.70710678118) < eps); + static_assert(Math::abs(Math::sqrt((F)1e-8) - (F)0.0001) < eps); + static_assert(Math::abs(Math::sqrt((F)2) - (F)1.41421356237) < eps); + static_assert(Math::abs(Math::sqrt((F)3) - (F)1.73205080757) < (F)1e-6); - static_assert(math::sqrt((F)0) == (F)0); - static_assert(math::sqrt((F)1) == (F)1); - static_assert(math::sqrt((F)4) == (F)2); - static_assert(math::sqrt((F)9) == (F)3); - static_assert(math::sqrt((F)36) == (F)6); + static_assert(Math::sqrt((F)0) == (F)0); + static_assert(Math::sqrt((F)1) == (F)1); + static_assert(Math::sqrt((F)4) == (F)2); + static_assert(Math::sqrt((F)9) == (F)3); + static_assert(Math::sqrt((F)36) == (F)6); fm_assert(test(0)); fm_assert(test(0.5)); @@ -60,15 +60,15 @@ bool test_sqrt() template<typename F> constexpr bool test_floor() { - fm_assert(math::floor((F)-1.5) == -2); - fm_assert(math::floor((F)0) == 0); - fm_assert(math::floor((F)1) == 1); - fm_assert(math::floor((F)-1) == -1); - fm_assert(math::floor((F)-2) == -2); - fm_assert(math::floor((F)1.0000001) == 1); - fm_assert(math::floor((F)-1.000001) == -2); - fm_assert(math::floor((F)1e-8) == 0); - fm_assert(math::floor((F)-1e-8) == -1); + fm_assert(Math::floor((F)-1.5) == -2); + fm_assert(Math::floor((F)0) == 0); + fm_assert(Math::floor((F)1) == 1); + fm_assert(Math::floor((F)-1) == -1); + fm_assert(Math::floor((F)-2) == -2); + fm_assert(Math::floor((F)1.0000001) == 1); + fm_assert(Math::floor((F)-1.000001) == -2); + fm_assert(Math::floor((F)1e-8) == 0); + fm_assert(Math::floor((F)-1e-8) == -1); return true; } @@ -76,15 +76,15 @@ constexpr bool test_floor() template<typename F> constexpr bool test_ceil() { - fm_assert(math::ceil((F)-1.5) == -1); - fm_assert(math::ceil((F)0) == 0); - fm_assert(math::ceil((F)1) == 1); - fm_assert(math::ceil((F)-1) == -1); - fm_assert(math::ceil((F)-2) == -2); - fm_assert(math::ceil((F)1.0000001) == 2); - fm_assert(math::ceil((F)-1.000001) == -1); - fm_assert(math::ceil((F)1e-8) == 1); - fm_assert(math::ceil((F)-1e-8) == 0); + fm_assert(Math::ceil((F)-1.5) == -1); + fm_assert(Math::ceil((F)0) == 0); + fm_assert(Math::ceil((F)1) == 1); + fm_assert(Math::ceil((F)-1) == -1); + fm_assert(Math::ceil((F)-2) == -2); + fm_assert(Math::ceil((F)1.0000001) == 2); + fm_assert(Math::ceil((F)-1.000001) == -1); + fm_assert(Math::ceil((F)1e-8) == 1); + fm_assert(Math::ceil((F)-1e-8) == 0); return true; } diff --git a/test/wall-atlas.cpp b/test/wall-atlas.cpp index 35096ea4..d544ac2b 100644 --- a/test/wall-atlas.cpp +++ b/test/wall-atlas.cpp @@ -104,11 +104,6 @@ void test_read_groups(StringView filename) return atlas; } -void write_to_temp_file(const wall_atlas_def& atlas) -{ - atlas.serialize(temp_filename()); -} - } // namespace } // namespace floormat::Wall::detail |