diff --git a/include/mbgl/math/clamp.hpp b/include/mbgl/math/clamp.hpp new file mode 100644 index 000000000..38d430547 --- /dev/null +++ b/include/mbgl/math/clamp.hpp @@ -0,0 +1,14 @@ +#pragma once + +#include + +namespace mbgl { +namespace util { + +template +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 000000000..e8c50a39c --- /dev/null +++ b/include/mbgl/math/minmax.hpp @@ -0,0 +1,41 @@ +#pragma once + +#include +#include +#include + +namespace mbgl { +namespace util { + +template +typename std::enable_if_t::value, T> max(T a, T b) { + return std::max(a, b); +} + +template +typename std::enable_if_t::value, T> max(T a, T b) { + return ::fmax(a, b); +} + +template +typename std::enable_if_t::value, T> max(T a, T b, Ts... args) { + return max(a, max(b, args...)); +} + +template +typename std::enable_if_t::value, T> min(T a, T b) { + return std::min(a, b); +} + +template +typename std::enable_if_t::value, T> min(T a, T b) { + return ::fmin(a, b); +} + +template +typename std::enable_if_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 000000000..16dc598c2 --- /dev/null +++ b/include/mbgl/math/wrap.hpp @@ -0,0 +1,17 @@ +#pragma once + +#include + +namespace mbgl { +namespace util { + +// Constrains n to the given range (including min, excluding max) via modular +// arithmetic. +template +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 84128caae..f0c708dcd 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 +#include #include #include diff --git a/include/mbgl/util/projection.hpp b/include/mbgl/util/projection.hpp index 09c8ce74a..cc1c39196 100644 --- a/include/mbgl/util/projection.hpp +++ b/include/mbgl/util/projection.hpp @@ -3,7 +3,7 @@ #include #include -#include +#include #include 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 922fb1186..a92999c0d 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 3d2679498..eecc71ad2 100644 --- a/platform/ios/src/MGLMapView.mm +++ b/platform/ios/src/MGLMapView.mm @@ -17,8 +17,8 @@ #include #include #include +#include #include -#include #include #include #include diff --git a/platform/osx/src/MGLMapView.mm b/platform/osx/src/MGLMapView.mm index 7d1f70dae..df244c2fd 100644 --- a/platform/osx/src/MGLMapView.mm +++ b/platform/osx/src/MGLMapView.mm @@ -24,8 +24,8 @@ #import #import #import +#import #import -#import #import #import diff --git a/src/mbgl/annotation/shape_annotation_impl.cpp b/src/mbgl/annotation/shape_annotation_impl.cpp index e0e2eb51b..a233e0358 100644 --- a/src/mbgl/annotation/shape_annotation_impl.cpp +++ b/src/mbgl/annotation/shape_annotation_impl.cpp @@ -4,11 +4,11 @@ #include #include #include -#include #include #include #include #include +#include namespace mbgl { diff --git a/src/mbgl/geometry/feature_index.cpp b/src/mbgl/geometry/feature_index.cpp index 3e345fe81..b7ec99803 100644 --- a/src/mbgl/geometry/feature_index.cpp +++ b/src/mbgl/geometry/feature_index.cpp @@ -1,11 +1,12 @@ #include -#include #include #include #include -#include #include +#include #include +#include +#include #include #include diff --git a/src/mbgl/map/transform.cpp b/src/mbgl/map/transform.cpp index 7dcfee65d..06cde18c6 100644 --- a/src/mbgl/map/transform.cpp +++ b/src/mbgl/map/transform.cpp @@ -6,9 +6,10 @@ #include #include #include +#include +#include #include #include -#include #include #include diff --git a/src/mbgl/map/transform_state.cpp b/src/mbgl/map/transform_state.cpp index a23ee71f9..cac80ff0b 100644 --- a/src/mbgl/map/transform_state.cpp +++ b/src/mbgl/map/transform_state.cpp @@ -3,6 +3,7 @@ #include #include #include +#include namespace mbgl { diff --git a/src/mbgl/renderer/frame_history.cpp b/src/mbgl/renderer/frame_history.cpp index 63aea0417..bf339edc6 100644 --- a/src/mbgl/renderer/frame_history.cpp +++ b/src/mbgl/renderer/frame_history.cpp @@ -1,5 +1,5 @@ #include -#include +#include using namespace mbgl; diff --git a/src/mbgl/renderer/symbol_bucket.cpp b/src/mbgl/renderer/symbol_bucket.cpp index e14bad673..55138363f 100644 --- a/src/mbgl/renderer/symbol_bucket.cpp +++ b/src/mbgl/renderer/symbol_bucket.cpp @@ -28,6 +28,7 @@ #include #include #include +#include namespace mbgl { diff --git a/src/mbgl/source/source.cpp b/src/mbgl/source/source.cpp index 78bffbb9b..484176775 100644 --- a/src/mbgl/source/source.cpp +++ b/src/mbgl/source/source.cpp @@ -9,12 +9,12 @@ #include #include #include -#include #include #include #include #include -#include +#include +#include #include #include #include diff --git a/src/mbgl/style/style.cpp b/src/mbgl/style/style.cpp index fc0f1de9c..5e0a087fd 100644 --- a/src/mbgl/style/style.cpp +++ b/src/mbgl/style/style.cpp @@ -19,6 +19,7 @@ #include #include #include +#include #include diff --git a/src/mbgl/text/glyph_set.cpp b/src/mbgl/text/glyph_set.cpp index 40041dff9..f63c1eae7 100644 --- a/src/mbgl/text/glyph_set.cpp +++ b/src/mbgl/text/glyph_set.cpp @@ -1,6 +1,6 @@ #include #include -#include +#include #include diff --git a/src/mbgl/util/geo.cpp b/src/mbgl/util/geo.cpp index 7d9db65c4..2cb5d0980 100644 --- a/src/mbgl/util/geo.cpp +++ b/src/mbgl/util/geo.cpp @@ -1,4 +1,4 @@ -#include +#include #include #include #include diff --git a/include/mbgl/util/math.hpp b/src/mbgl/util/math.hpp similarity index 69% rename from include/mbgl/util/math.hpp rename to src/mbgl/util/math.hpp index c0fdea47f..6c6b02d12 100644 --- a/include/mbgl/util/math.hpp +++ b/src/mbgl/util/math.hpp @@ -10,36 +10,7 @@ namespace mbgl { namespace util { - -template -typename std::enable_if_t::value, T> max(T a, T b) { - return std::max(a, b); -} - -template -typename std::enable_if_t::value, T> max(T a, T b) { - return ::fmax(a, b); -} - -template -typename std::enable_if_t::value, T> max(T a, T b, Ts... args) { - return max(a, max(b, args...)); -} - -template -typename std::enable_if_t::value, T> min(T a, T b) { - return std::min(a, b); -} - -template -typename std::enable_if_t::value, T> min(T a, T b) { - return ::fmin(a, b); -} - -template -typename std::enable_if_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 θ. @@ -130,19 +101,6 @@ inline T rotate(const T& a, S angle) { return T(x, y); } -template -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 -T wrap(T value, T min, T max) { - T d = max - min; - return std::fmod((std::fmod((value - min), d) + d), d) + min; -} - template T smoothstep(T edge0, T edge1, T x) { T t = clamp((x - edge0) / (edge1 - edge0), T(0), T(1)); diff --git a/include/mbgl/util/vec.hpp b/src/mbgl/util/vec.hpp similarity index 100% rename from include/mbgl/util/vec.hpp rename to src/mbgl/util/vec.hpp diff --git a/test/math/clamp.cpp b/test/math/clamp.cpp new file mode 100644 index 000000000..2fdafae1b --- /dev/null +++ b/test/math/clamp.cpp @@ -0,0 +1,24 @@ +#include + +#include +#include + +using namespace mbgl; + +TEST(Math, ClampFloatingPoint) { + double lowestValue = std::numeric_limits::lowest(); + double maximumValue = std::numeric_limits::max(); + double quietNAN = std::numeric_limits::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::lowest(); + int32_t maximumValue = std::numeric_limits::max(); + int32_t quietNAN = std::numeric_limits::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 similarity index 83% rename from test/util/math.cpp rename to test/math/minmax.cpp index c833ca41e..2e3ebdc34 100644 --- a/test/util/math.cpp +++ b/test/math/minmax.cpp @@ -1,7 +1,7 @@ #include #include -#include +#include 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 2326fd7cf..1e136389e 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',