diff options
23 files changed, 120 insertions, 65 deletions
diff --git a/include/mbgl/math/clamp.hpp b/include/mbgl/math/clamp.hpp new file mode 100644 index 0000000000..38d430547d --- /dev/null +++ b/include/mbgl/math/clamp.hpp @@ -0,0 +1,14 @@ +#pragma once + +#include <mbgl/math/minmax.hpp> + +namespace mbgl { +namespace util { + +template <typename T> +T clamp(T value, T min_, T max_) { + return max(min_, min(max_, value)); +} + +} // namespace util +} // namespace mbgl diff --git a/include/mbgl/math/minmax.hpp b/include/mbgl/math/minmax.hpp new file mode 100644 index 0000000000..e8c50a39cf --- /dev/null +++ b/include/mbgl/math/minmax.hpp @@ -0,0 +1,41 @@ +#pragma once + +#include <type_traits> +#include <algorithm> +#include <cmath> + +namespace mbgl { +namespace util { + +template <typename T> +typename std::enable_if_t<std::is_integral<T>::value, T> max(T a, T b) { + return std::max(a, b); +} + +template <typename T> +typename std::enable_if_t<std::is_floating_point<T>::value, T> max(T a, T b) { + return ::fmax(a, b); +} + +template <typename T, typename... Ts> +typename std::enable_if_t<std::is_arithmetic<T>::value, T> max(T a, T b, Ts... args) { + return max(a, max(b, args...)); +} + +template <typename T> +typename std::enable_if_t<std::is_integral<T>::value, T> min(T a, T b) { + return std::min(a, b); +} + +template <typename T> +typename std::enable_if_t<std::is_floating_point<T>::value, T> min(T a, T b) { + return ::fmin(a, b); +} + +template <typename T, typename... Ts> +typename std::enable_if_t<std::is_arithmetic<T>::value, T> min(T a, T b, Ts... args) { + return min(a, min(b, args...)); +} + +} // namespace util +} // namespace mbgl diff --git a/include/mbgl/math/wrap.hpp b/include/mbgl/math/wrap.hpp new file mode 100644 index 0000000000..16dc598c22 --- /dev/null +++ b/include/mbgl/math/wrap.hpp @@ -0,0 +1,17 @@ +#pragma once + +#include <cmath> + +namespace mbgl { +namespace util { + +// Constrains n to the given range (including min, excluding max) via modular +// arithmetic. +template <typename T> +T wrap(T value, T min, T max) { + T d = max - min; + return std::fmod((std::fmod((value - min), d) + d), d) + min; +} + +} // namespace util +} // namespace mbgl diff --git a/include/mbgl/util/geo.hpp b/include/mbgl/util/geo.hpp index 84128caae7..f0c708dcd4 100644 --- a/include/mbgl/util/geo.hpp +++ b/include/mbgl/util/geo.hpp @@ -1,7 +1,7 @@ #ifndef MBGL_UTIL_GEO #define MBGL_UTIL_GEO -#include <mbgl/util/math.hpp> +#include <mbgl/math/wrap.hpp> #include <mbgl/util/vec.hpp> #include <mbgl/util/constants.hpp> diff --git a/include/mbgl/util/projection.hpp b/include/mbgl/util/projection.hpp index 09c8ce74ab..cc1c391962 100644 --- a/include/mbgl/util/projection.hpp +++ b/include/mbgl/util/projection.hpp @@ -3,7 +3,7 @@ #include <mbgl/util/constants.hpp> #include <mbgl/util/geo.hpp> -#include <mbgl/util/math.hpp> +#include <mbgl/math/clamp.hpp> #include <cmath> diff --git a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/utils/MathUtils.java b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/utils/MathUtils.java index 922fb11868..a92999c0d5 100644 --- a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/utils/MathUtils.java +++ b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/utils/MathUtils.java @@ -27,7 +27,7 @@ public class MathUtils { /** * Constrains value to the given range (including min, excluding max) via modular arithmetic. * - * Same formula as used in Core GL (math.hpp) + * Same formula as used in Core GL (wrap.hpp) * std::fmod((std::fmod((value - min), d) + d), d) + min; * * @param value Value to wrap diff --git a/platform/ios/src/MGLMapView.mm b/platform/ios/src/MGLMapView.mm index 3d2679498a..eecc71ad27 100644 --- a/platform/ios/src/MGLMapView.mm +++ b/platform/ios/src/MGLMapView.mm @@ -17,8 +17,8 @@ #include <mbgl/storage/default_file_source.hpp> #include <mbgl/storage/network_status.hpp> #include <mbgl/style/property_transition.hpp> +#include <mbgl/math/wrap.hpp> #include <mbgl/util/geo.hpp> -#include <mbgl/util/math.hpp> #include <mbgl/util/constants.hpp> #include <mbgl/util/image.hpp> #include <mbgl/util/projection.hpp> diff --git a/platform/osx/src/MGLMapView.mm b/platform/osx/src/MGLMapView.mm index 7d1f70dae9..df244c2fdd 100644 --- a/platform/osx/src/MGLMapView.mm +++ b/platform/osx/src/MGLMapView.mm @@ -24,8 +24,8 @@ #import <mbgl/sprite/sprite_image.hpp> #import <mbgl/storage/default_file_source.hpp> #import <mbgl/storage/network_status.hpp> +#import <mbgl/math/wrap.hpp> #import <mbgl/util/constants.hpp> -#import <mbgl/util/math.hpp> #import <mbgl/util/chrono.hpp> #import <map> diff --git a/src/mbgl/annotation/shape_annotation_impl.cpp b/src/mbgl/annotation/shape_annotation_impl.cpp index e0e2eb51b2..a233e0358e 100644 --- a/src/mbgl/annotation/shape_annotation_impl.cpp +++ b/src/mbgl/annotation/shape_annotation_impl.cpp @@ -4,11 +4,11 @@ #include <mbgl/annotation/annotation_manager.hpp> #include <mbgl/annotation/annotation_tile.hpp> #include <mbgl/util/constants.hpp> -#include <mbgl/util/math.hpp> #include <mbgl/util/string.hpp> #include <mbgl/style/style.hpp> #include <mbgl/layer/line_layer.hpp> #include <mbgl/layer/fill_layer.hpp> +#include <mbgl/math/clamp.hpp> namespace mbgl { diff --git a/src/mbgl/geometry/feature_index.cpp b/src/mbgl/geometry/feature_index.cpp index 3e345fe817..b7ec99803d 100644 --- a/src/mbgl/geometry/feature_index.cpp +++ b/src/mbgl/geometry/feature_index.cpp @@ -1,11 +1,12 @@ #include <mbgl/geometry/feature_index.hpp> -#include <mbgl/util/math.hpp> #include <mbgl/style/style.hpp> #include <mbgl/style/style_layer.hpp> #include <mbgl/layer/symbol_layer.hpp> -#include <mbgl/util/get_geometries.hpp> #include <mbgl/text/collision_tile.hpp> +#include <mbgl/util/get_geometries.hpp> #include <mbgl/util/constants.hpp> +#include <mbgl/util/math.hpp> +#include <mbgl/math/minmax.hpp> #include <cassert> #include <string> diff --git a/src/mbgl/map/transform.cpp b/src/mbgl/map/transform.cpp index 7dcfee65dc..06cde18c6e 100644 --- a/src/mbgl/map/transform.cpp +++ b/src/mbgl/map/transform.cpp @@ -6,9 +6,10 @@ #include <mbgl/util/math.hpp> #include <mbgl/util/unitbezier.hpp> #include <mbgl/util/interpolate.hpp> +#include <mbgl/util/chrono.hpp> +#include <mbgl/math/clamp.hpp> #include <mbgl/platform/log.hpp> #include <mbgl/platform/platform.hpp> -#include <mbgl/util/chrono.hpp> #include <cstdio> #include <cmath> diff --git a/src/mbgl/map/transform_state.cpp b/src/mbgl/map/transform_state.cpp index a23ee71f99..cac80ff0bc 100644 --- a/src/mbgl/map/transform_state.cpp +++ b/src/mbgl/map/transform_state.cpp @@ -3,6 +3,7 @@ #include <mbgl/util/constants.hpp> #include <mbgl/util/interpolate.hpp> #include <mbgl/util/math.hpp> +#include <mbgl/math/clamp.hpp> namespace mbgl { diff --git a/src/mbgl/renderer/frame_history.cpp b/src/mbgl/renderer/frame_history.cpp index 63aea0417c..bf339edc6f 100644 --- a/src/mbgl/renderer/frame_history.cpp +++ b/src/mbgl/renderer/frame_history.cpp @@ -1,5 +1,5 @@ #include <mbgl/renderer/frame_history.hpp> -#include <mbgl/util/math.hpp> +#include <mbgl/math/minmax.hpp> using namespace mbgl; diff --git a/src/mbgl/renderer/symbol_bucket.cpp b/src/mbgl/renderer/symbol_bucket.cpp index e14bad6735..55138363fe 100644 --- a/src/mbgl/renderer/symbol_bucket.cpp +++ b/src/mbgl/renderer/symbol_bucket.cpp @@ -28,6 +28,7 @@ #include <mbgl/util/get_geometries.hpp> #include <mbgl/util/constants.hpp> #include <mbgl/util/string.hpp> +#include <mbgl/math/minmax.hpp> namespace mbgl { diff --git a/src/mbgl/source/source.cpp b/src/mbgl/source/source.cpp index 78bffbb9b3..4841767751 100644 --- a/src/mbgl/source/source.cpp +++ b/src/mbgl/source/source.cpp @@ -9,12 +9,12 @@ #include <mbgl/util/constants.hpp> #include <mbgl/storage/resource.hpp> #include <mbgl/storage/response.hpp> -#include <mbgl/util/math.hpp> #include <mbgl/storage/file_source.hpp> #include <mbgl/style/style_layer.hpp> #include <mbgl/style/style_update_parameters.hpp> #include <mbgl/platform/log.hpp> -#include <mbgl/util/math.hpp> +#include <mbgl/math/minmax.hpp> +#include <mbgl/math/clamp.hpp> #include <mbgl/util/std.hpp> #include <mbgl/util/token.hpp> #include <mbgl/util/string.hpp> diff --git a/src/mbgl/style/style.cpp b/src/mbgl/style/style.cpp index fc0f1de9c8..5e0a087fdb 100644 --- a/src/mbgl/style/style.cpp +++ b/src/mbgl/style/style.cpp @@ -19,6 +19,7 @@ #include <mbgl/util/string.hpp> #include <mbgl/platform/log.hpp> #include <mbgl/layer/background_layer.hpp> +#include <mbgl/math/minmax.hpp> #include <csscolorparser/csscolorparser.hpp> diff --git a/src/mbgl/text/glyph_set.cpp b/src/mbgl/text/glyph_set.cpp index 40041dff91..f63c1eae7f 100644 --- a/src/mbgl/text/glyph_set.cpp +++ b/src/mbgl/text/glyph_set.cpp @@ -1,6 +1,6 @@ #include <mbgl/text/glyph_set.hpp> #include <mbgl/platform/log.hpp> -#include <mbgl/util/math.hpp> +#include <mbgl/math/minmax.hpp> #include <cassert> diff --git a/src/mbgl/util/geo.cpp b/src/mbgl/util/geo.cpp index 7d9db65c4c..2cb5d09806 100644 --- a/src/mbgl/util/geo.cpp +++ b/src/mbgl/util/geo.cpp @@ -1,4 +1,4 @@ -#include <mbgl/util/math.hpp> +#include <mbgl/math/clamp.hpp> #include <mbgl/util/geo.hpp> #include <mbgl/util/constants.hpp> #include <mbgl/map/tile_id.hpp> diff --git a/include/mbgl/util/math.hpp b/src/mbgl/util/math.hpp index c0fdea47fb..6c6b02d126 100644 --- a/include/mbgl/util/math.hpp +++ b/src/mbgl/util/math.hpp @@ -10,36 +10,7 @@ namespace mbgl { namespace util { - -template <typename T> -typename std::enable_if_t<std::is_integral<T>::value, T> max(T a, T b) { - return std::max(a, b); -} - -template <typename T> -typename std::enable_if_t<std::is_floating_point<T>::value, T> max(T a, T b) { - return ::fmax(a, b); -} - -template <typename T, typename... Ts> -typename std::enable_if_t<std::is_arithmetic<T>::value, T> max(T a, T b, Ts... args) { - return max(a, max(b, args...)); -} - -template <typename T> -typename std::enable_if_t<std::is_integral<T>::value, T> min(T a, T b) { - return std::min(a, b); -} - -template <typename T> -typename std::enable_if_t<std::is_floating_point<T>::value, T> min(T a, T b) { - return ::fmin(a, b); -} - -template <typename T, typename... Ts> -typename std::enable_if_t<std::is_arithmetic<T>::value, T> min(T a, T b, Ts... args) { - return min(a, min(b, args...)); -} +// TODO: split this file up into individual headers, following mbgl/math/*.hpp. // Find the angle of the two vectors, solving the formula for the cross product // a x b = |a||b|sin(θ) for θ. @@ -131,19 +102,6 @@ inline T rotate(const T& a, S angle) { } template <typename T> -T clamp(T value, T min_, T max_) { - return max(min_, min(max_, value)); -} - -// Constrains n to the given range (including min, excluding max) via modular -// arithmetic. -template <typename T> -T wrap(T value, T min, T max) { - T d = max - min; - return std::fmod((std::fmod((value - min), d) + d), d) + min; -} - -template <typename T> T smoothstep(T edge0, T edge1, T x) { T t = clamp((x - edge0) / (edge1 - edge0), T(0), T(1)); return t * t * (T(3) - T(2) * t); diff --git a/include/mbgl/util/vec.hpp b/src/mbgl/util/vec.hpp index 40d540fca4..40d540fca4 100644 --- a/include/mbgl/util/vec.hpp +++ b/src/mbgl/util/vec.hpp diff --git a/test/math/clamp.cpp b/test/math/clamp.cpp new file mode 100644 index 0000000000..2fdafae1b4 --- /dev/null +++ b/test/math/clamp.cpp @@ -0,0 +1,24 @@ +#include <mbgl/test/util.hpp> + +#include <mbgl/util/constants.hpp> +#include <mbgl/math/clamp.hpp> + +using namespace mbgl; + +TEST(Math, ClampFloatingPoint) { + double lowestValue = std::numeric_limits<double>::lowest(); + double maximumValue = std::numeric_limits<double>::max(); + double quietNAN = std::numeric_limits<double>::quiet_NaN(); + + ASSERT_DOUBLE_EQ(0., util::clamp(0., quietNAN, quietNAN)); + ASSERT_DOUBLE_EQ(0., util::clamp(0., lowestValue, maximumValue)); +} + +TEST(Math, ClampIntegral) { + int32_t lowestValue = std::numeric_limits<int32_t>::lowest(); + int32_t maximumValue = std::numeric_limits<int32_t>::max(); + int32_t quietNAN = std::numeric_limits<int32_t>::quiet_NaN(); + + ASSERT_EQ(0, util::clamp(0, quietNAN, quietNAN)); + ASSERT_EQ(0, util::clamp(0, lowestValue, maximumValue)); +} diff --git a/test/util/math.cpp b/test/math/minmax.cpp index c833ca41e2..2e3ebdc340 100644 --- a/test/util/math.cpp +++ b/test/math/minmax.cpp @@ -1,7 +1,7 @@ #include <mbgl/test/util.hpp> #include <mbgl/util/constants.hpp> -#include <mbgl/util/math.hpp> +#include <mbgl/math/minmax.hpp> using namespace mbgl; @@ -19,9 +19,6 @@ TEST(Math, MinMaxFloatingPoint) { ASSERT_DOUBLE_EQ(maximumValue, util::max(double(NAN), maximumValue)); ASSERT_DOUBLE_EQ(maximumValue, util::max(quietNAN, maximumValue)); ASSERT_DOUBLE_EQ(10., util::max(8., 9., 10.)); - - ASSERT_DOUBLE_EQ(0., util::clamp(0., quietNAN, quietNAN)); - ASSERT_DOUBLE_EQ(0., util::clamp(0., lowestValue, maximumValue)); } TEST(Math, MinMaxIntegral) { @@ -36,7 +33,4 @@ TEST(Math, MinMaxIntegral) { ASSERT_EQ(maximumValue, util::max(quietNAN, maximumValue)); ASSERT_EQ(10, util::max(8, 9, 10)); - - ASSERT_EQ(0, util::clamp(0, quietNAN, quietNAN)); - ASSERT_EQ(0, util::clamp(0, lowestValue, maximumValue)); } diff --git a/test/test.gypi b/test/test.gypi index 2326fd7cf9..1e136389e8 100644 --- a/test/test.gypi +++ b/test/test.gypi @@ -24,7 +24,6 @@ 'util/geo.cpp', 'util/image.cpp', 'util/mapbox.cpp', - 'util/math.cpp', 'util/merge_lines.cpp', 'util/run_loop.cpp', 'util/text_conversions.cpp', @@ -48,6 +47,9 @@ 'map/tile.cpp', 'map/transform.cpp', + 'math/minmax.cpp', + 'math/clamp.cpp', + 'storage/offline.cpp', 'storage/offline_database.cpp', 'storage/offline_download.cpp', |