From ce2a06e6773dfb656c7bf6fdbb7e8bc463710685 Mon Sep 17 00:00:00 2001 From: John Firebaugh Date: Wed, 4 May 2016 17:32:30 -0700 Subject: [core] Privatize math.hpp and vec.hpp --- include/mbgl/math/clamp.hpp | 14 ++ include/mbgl/math/minmax.hpp | 41 ++++++ include/mbgl/math/wrap.hpp | 17 +++ include/mbgl/util/geo.hpp | 2 +- include/mbgl/util/math.hpp | 159 --------------------- include/mbgl/util/projection.hpp | 2 +- include/mbgl/util/vec.hpp | 126 ---------------- .../java/com/mapbox/mapboxsdk/utils/MathUtils.java | 2 +- platform/ios/src/MGLMapView.mm | 2 +- platform/osx/src/MGLMapView.mm | 2 +- src/mbgl/annotation/shape_annotation_impl.cpp | 2 +- src/mbgl/geometry/feature_index.cpp | 5 +- src/mbgl/map/transform.cpp | 3 +- src/mbgl/map/transform_state.cpp | 1 + src/mbgl/renderer/frame_history.cpp | 2 +- src/mbgl/renderer/symbol_bucket.cpp | 1 + src/mbgl/source/source.cpp | 4 +- src/mbgl/style/style.cpp | 1 + src/mbgl/text/glyph_set.cpp | 2 +- src/mbgl/util/geo.cpp | 2 +- src/mbgl/util/math.hpp | 117 +++++++++++++++ src/mbgl/util/vec.hpp | 126 ++++++++++++++++ test/math/clamp.cpp | 24 ++++ test/math/minmax.cpp | 36 +++++ test/test.gypi | 4 +- test/util/math.cpp | 42 ------ 26 files changed, 397 insertions(+), 342 deletions(-) create mode 100644 include/mbgl/math/clamp.hpp create mode 100644 include/mbgl/math/minmax.hpp create mode 100644 include/mbgl/math/wrap.hpp delete mode 100644 include/mbgl/util/math.hpp delete mode 100644 include/mbgl/util/vec.hpp create mode 100644 src/mbgl/util/math.hpp create mode 100644 src/mbgl/util/vec.hpp create mode 100644 test/math/clamp.cpp create mode 100644 test/math/minmax.cpp delete mode 100644 test/util/math.cpp 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 + +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 0000000000..e8c50a39cf --- /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 0000000000..16dc598c22 --- /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 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 +#include #include #include diff --git a/include/mbgl/util/math.hpp b/include/mbgl/util/math.hpp deleted file mode 100644 index c0fdea47fb..0000000000 --- a/include/mbgl/util/math.hpp +++ /dev/null @@ -1,159 +0,0 @@ -#ifndef MBGL_UTIL_MATH -#define MBGL_UTIL_MATH - -#include -#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...)); -} - -// Find the angle of the two vectors, solving the formula for the cross product -// a x b = |a||b|sin(θ) for θ. -template -inline T angle_between(S ax, S ay, S bx, S by) { - return std::atan2((ax * by - ay * bx), ax * bx + ay * by); -} - -template -inline T angle_between(const vec2& a, const vec2& b) { - return angle_between(a.x, a.y, b.x, b.y); -} - -template -inline T angle_to(const vec2& a, const vec2& b) { - return std::atan2(a.y - b.y, a.x - b.x); -} - -// Reflect an angle around 0 degrees -template -inline std::array flip(const std::array& c) { - return {{ - static_cast(2 * M_PI - c[0]), - static_cast(2 * M_PI - c[1]) - }}; -} - -template -inline vec2 normal(const S1& a, const S2& b) { - T dx = b.x - a.x; - T dy = b.y - a.y; - T c = std::sqrt(dx * dx + dy * dy); - return { dx / c, dy / c }; -} - -template -inline T perp(const T& a) { - return T(-a.y, a.x); -} - -template -inline T dist(const S1& a, const S2& b) { - T dx = b.x - a.x; - T dy = b.y - a.y; - T c = std::sqrt(dx * dx + dy * dy); - return c; -} - -template -inline T distSqr(const S1& a, const S2& b) { - T dx = b.x - a.x; - T dy = b.y - a.y; - T c = dx * dx + dy * dy; - return c; -} - -template -inline T round(const T& a) { - return T(::round(a.x), ::round(a.y)); -} - -template -inline T length(T a, T b) { - return std::sqrt(a * a + b * b); -} - -// Take the magnitude of vector a. -template -inline T mag(const S& a) { - return std::sqrt(a.x * a.x + a.y * a.y); -} - -template -inline S unit(const S& a) { - auto magnitude = mag(a); - if (magnitude == 0) { - return a; - } - return a * (1 / magnitude); -} - -template -inline T rotate(const T& a, S angle) { - S cos = std::cos(angle); - S sin = std::sin(angle); - S x = cos * a.x - sin * a.y; - S y = sin * a.x + cos * a.y; - 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)); - return t * t * (T(3) - T(2) * t); -} - -// Computes the log2(x) rounded up to the next integer. -// (== number of bits required to store x) -uint32_t ceil_log2(uint64_t x); - -} // namespace util -} // namespace mbgl - -#endif 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 #include -#include +#include #include diff --git a/include/mbgl/util/vec.hpp b/include/mbgl/util/vec.hpp deleted file mode 100644 index 40d540fca4..0000000000 --- a/include/mbgl/util/vec.hpp +++ /dev/null @@ -1,126 +0,0 @@ -#ifndef MBGL_UTIL_VEC -#define MBGL_UTIL_VEC - -#include -#include -#include -#include -#include - -namespace mbgl { - -template -struct vec2 { - struct null {}; - typedef T Type; - - T x, y; - - inline vec2() = default; - - template::has_quiet_NaN, int>::type = 0> - inline vec2(null) : x(std::numeric_limits::quiet_NaN()), y(std::numeric_limits::quiet_NaN()) {} - - template::has_quiet_NaN, int>::type = 0> - inline vec2(null) : x(std::numeric_limits::min()), y(std::numeric_limits::min()) {} - - inline vec2(const vec2& o) = default; - - template - inline vec2(const U& u) : x(u.x), y(u.y) {} - - inline vec2(T x_, T y_) : x(x_), y(y_) {} - - inline bool operator==(const vec2& rhs) const { - return x == rhs.x && y == rhs.y; - } - - template - inline typename std::enable_if::value, vec2>::type - operator*(O o) const { - return {x * o, y * o}; - } - - inline void operator*=(T o) { - x *= o; - y *= o; - } - - template - inline typename std::enable_if::value, vec2>::type - operator/(O o) const { - return {x / o, y / o}; - } - - inline void operator/=(T o) { - x /= o; - y /= o; - } - - inline vec2 operator *(const std::array& matrix) { - return { x * matrix[0] + y * matrix[4] + matrix[12], x * matrix[1] + y * matrix[5] + matrix[13] }; - } - - template - inline typename std::enable_if::value, vec2>::type - operator-(O o) const { - return {x - o, y - o}; - } - - template - inline typename std::enable_if::value, vec2>::type - operator-(const O &o) const { - return vec2(x - o.x, y - o.y); - } - - template - inline typename std::enable_if::value, vec2>::type - operator+(const O &o) const { - return vec2(x + o.x, y + o.y); - } - - template - inline vec2 matMul(const M &m) const { - return {m[0] * x + m[1] * y, m[2] * x + m[3] * y}; - } - - template::has_quiet_NaN, int>::type = 0> - explicit operator bool() const { - return !std::isnan(x) && !std::isnan(y); - } - - template::has_quiet_NaN, int>::type = 0> - explicit operator bool() const { - return x != std::numeric_limits::min() && y != std::numeric_limits::min(); - } -}; - -template -struct vec4 { - T x, y, z, w; - - inline vec4() = default; - inline vec4(const vec4& o) : x(o.x), y(o.y), z(o.z), w(o.w) {} - inline vec4(T x_, T y_, T z_, T w_) : x(x_), y(y_), z(z_), w(w_) {} - inline bool operator==(const vec4& rhs) const { - return x == rhs.x && y == rhs.y && z == rhs.z && w == rhs.w; - } - - template::has_quiet_NaN, int>::type = 0> - explicit operator bool() const { - return !std::isnan(x) && !std::isnan(y) && !std::isnan(z) && !std::isnan(w); - } - - template::has_quiet_NaN, int>::type = 0> - explicit operator bool() const { - return x != std::numeric_limits::min() - && y != std::numeric_limits::min() - && z != std::numeric_limits::min() - && w != std::numeric_limits::min(); - } -}; - - -} // namespace mbgl - -#endif // MBGL_UTIL_VEC 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 #include #include +#include #include -#include #include #include #include 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 #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 e0e2eb51b2..a233e0358e 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 3e345fe817..b7ec99803d 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 7dcfee65dc..06cde18c6e 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 a23ee71f99..cac80ff0bc 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 63aea0417c..bf339edc6f 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 e14bad6735..55138363fe 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 78bffbb9b3..4841767751 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 fc0f1de9c8..5e0a087fdb 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 40041dff91..f63c1eae7f 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 7d9db65c4c..2cb5d09806 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/src/mbgl/util/math.hpp b/src/mbgl/util/math.hpp new file mode 100644 index 0000000000..6c6b02d126 --- /dev/null +++ b/src/mbgl/util/math.hpp @@ -0,0 +1,117 @@ +#ifndef MBGL_UTIL_MATH +#define MBGL_UTIL_MATH + +#include +#include +#include + +#include + +namespace mbgl { +namespace util { + +// 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 θ. +template +inline T angle_between(S ax, S ay, S bx, S by) { + return std::atan2((ax * by - ay * bx), ax * bx + ay * by); +} + +template +inline T angle_between(const vec2& a, const vec2& b) { + return angle_between(a.x, a.y, b.x, b.y); +} + +template +inline T angle_to(const vec2& a, const vec2& b) { + return std::atan2(a.y - b.y, a.x - b.x); +} + +// Reflect an angle around 0 degrees +template +inline std::array flip(const std::array& c) { + return {{ + static_cast(2 * M_PI - c[0]), + static_cast(2 * M_PI - c[1]) + }}; +} + +template +inline vec2 normal(const S1& a, const S2& b) { + T dx = b.x - a.x; + T dy = b.y - a.y; + T c = std::sqrt(dx * dx + dy * dy); + return { dx / c, dy / c }; +} + +template +inline T perp(const T& a) { + return T(-a.y, a.x); +} + +template +inline T dist(const S1& a, const S2& b) { + T dx = b.x - a.x; + T dy = b.y - a.y; + T c = std::sqrt(dx * dx + dy * dy); + return c; +} + +template +inline T distSqr(const S1& a, const S2& b) { + T dx = b.x - a.x; + T dy = b.y - a.y; + T c = dx * dx + dy * dy; + return c; +} + +template +inline T round(const T& a) { + return T(::round(a.x), ::round(a.y)); +} + +template +inline T length(T a, T b) { + return std::sqrt(a * a + b * b); +} + +// Take the magnitude of vector a. +template +inline T mag(const S& a) { + return std::sqrt(a.x * a.x + a.y * a.y); +} + +template +inline S unit(const S& a) { + auto magnitude = mag(a); + if (magnitude == 0) { + return a; + } + return a * (1 / magnitude); +} + +template +inline T rotate(const T& a, S angle) { + S cos = std::cos(angle); + S sin = std::sin(angle); + S x = cos * a.x - sin * a.y; + S y = sin * a.x + cos * a.y; + return T(x, y); +} + +template +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); +} + +// Computes the log2(x) rounded up to the next integer. +// (== number of bits required to store x) +uint32_t ceil_log2(uint64_t x); + +} // namespace util +} // namespace mbgl + +#endif diff --git a/src/mbgl/util/vec.hpp b/src/mbgl/util/vec.hpp new file mode 100644 index 0000000000..40d540fca4 --- /dev/null +++ b/src/mbgl/util/vec.hpp @@ -0,0 +1,126 @@ +#ifndef MBGL_UTIL_VEC +#define MBGL_UTIL_VEC + +#include +#include +#include +#include +#include + +namespace mbgl { + +template +struct vec2 { + struct null {}; + typedef T Type; + + T x, y; + + inline vec2() = default; + + template::has_quiet_NaN, int>::type = 0> + inline vec2(null) : x(std::numeric_limits::quiet_NaN()), y(std::numeric_limits::quiet_NaN()) {} + + template::has_quiet_NaN, int>::type = 0> + inline vec2(null) : x(std::numeric_limits::min()), y(std::numeric_limits::min()) {} + + inline vec2(const vec2& o) = default; + + template + inline vec2(const U& u) : x(u.x), y(u.y) {} + + inline vec2(T x_, T y_) : x(x_), y(y_) {} + + inline bool operator==(const vec2& rhs) const { + return x == rhs.x && y == rhs.y; + } + + template + inline typename std::enable_if::value, vec2>::type + operator*(O o) const { + return {x * o, y * o}; + } + + inline void operator*=(T o) { + x *= o; + y *= o; + } + + template + inline typename std::enable_if::value, vec2>::type + operator/(O o) const { + return {x / o, y / o}; + } + + inline void operator/=(T o) { + x /= o; + y /= o; + } + + inline vec2 operator *(const std::array& matrix) { + return { x * matrix[0] + y * matrix[4] + matrix[12], x * matrix[1] + y * matrix[5] + matrix[13] }; + } + + template + inline typename std::enable_if::value, vec2>::type + operator-(O o) const { + return {x - o, y - o}; + } + + template + inline typename std::enable_if::value, vec2>::type + operator-(const O &o) const { + return vec2(x - o.x, y - o.y); + } + + template + inline typename std::enable_if::value, vec2>::type + operator+(const O &o) const { + return vec2(x + o.x, y + o.y); + } + + template + inline vec2 matMul(const M &m) const { + return {m[0] * x + m[1] * y, m[2] * x + m[3] * y}; + } + + template::has_quiet_NaN, int>::type = 0> + explicit operator bool() const { + return !std::isnan(x) && !std::isnan(y); + } + + template::has_quiet_NaN, int>::type = 0> + explicit operator bool() const { + return x != std::numeric_limits::min() && y != std::numeric_limits::min(); + } +}; + +template +struct vec4 { + T x, y, z, w; + + inline vec4() = default; + inline vec4(const vec4& o) : x(o.x), y(o.y), z(o.z), w(o.w) {} + inline vec4(T x_, T y_, T z_, T w_) : x(x_), y(y_), z(z_), w(w_) {} + inline bool operator==(const vec4& rhs) const { + return x == rhs.x && y == rhs.y && z == rhs.z && w == rhs.w; + } + + template::has_quiet_NaN, int>::type = 0> + explicit operator bool() const { + return !std::isnan(x) && !std::isnan(y) && !std::isnan(z) && !std::isnan(w); + } + + template::has_quiet_NaN, int>::type = 0> + explicit operator bool() const { + return x != std::numeric_limits::min() + && y != std::numeric_limits::min() + && z != std::numeric_limits::min() + && w != std::numeric_limits::min(); + } +}; + + +} // namespace mbgl + +#endif // MBGL_UTIL_VEC 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 + +#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/math/minmax.cpp b/test/math/minmax.cpp new file mode 100644 index 0000000000..2e3ebdc340 --- /dev/null +++ b/test/math/minmax.cpp @@ -0,0 +1,36 @@ +#include + +#include +#include + +using namespace mbgl; + +TEST(Math, MinMaxFloatingPoint) { + double minimumValue = std::numeric_limits::min(); + double lowestValue = std::numeric_limits::lowest(); + double maximumValue = std::numeric_limits::max(); + double quietNAN = std::numeric_limits::quiet_NaN(); + + ASSERT_DOUBLE_EQ(lowestValue, util::min(minimumValue, lowestValue)); + ASSERT_DOUBLE_EQ(lowestValue, util::min(double(NAN), lowestValue)); + ASSERT_DOUBLE_EQ(lowestValue, util::min(quietNAN, lowestValue)); + ASSERT_DOUBLE_EQ(0., util::min(2., 1., 0.)); + + 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.)); +} + +TEST(Math, MinMaxIntegral) { + int32_t minimumValue = std::numeric_limits::min(); + int32_t lowestValue = std::numeric_limits::lowest(); + int32_t maximumValue = std::numeric_limits::max(); + int32_t quietNAN = std::numeric_limits::quiet_NaN(); + + ASSERT_EQ(lowestValue, util::min(minimumValue, lowestValue)); + ASSERT_EQ(lowestValue, util::min(quietNAN, lowestValue)); + ASSERT_EQ(0, util::min(2, 1, 0)); + + ASSERT_EQ(maximumValue, util::max(quietNAN, maximumValue)); + ASSERT_EQ(10, util::max(8, 9, 10)); +} 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', diff --git a/test/util/math.cpp b/test/util/math.cpp deleted file mode 100644 index c833ca41e2..0000000000 --- a/test/util/math.cpp +++ /dev/null @@ -1,42 +0,0 @@ -#include - -#include -#include - -using namespace mbgl; - -TEST(Math, MinMaxFloatingPoint) { - double minimumValue = std::numeric_limits::min(); - double lowestValue = std::numeric_limits::lowest(); - double maximumValue = std::numeric_limits::max(); - double quietNAN = std::numeric_limits::quiet_NaN(); - - ASSERT_DOUBLE_EQ(lowestValue, util::min(minimumValue, lowestValue)); - ASSERT_DOUBLE_EQ(lowestValue, util::min(double(NAN), lowestValue)); - ASSERT_DOUBLE_EQ(lowestValue, util::min(quietNAN, lowestValue)); - ASSERT_DOUBLE_EQ(0., util::min(2., 1., 0.)); - - 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) { - int32_t minimumValue = std::numeric_limits::min(); - int32_t lowestValue = std::numeric_limits::lowest(); - int32_t maximumValue = std::numeric_limits::max(); - int32_t quietNAN = std::numeric_limits::quiet_NaN(); - - ASSERT_EQ(lowestValue, util::min(minimumValue, lowestValue)); - ASSERT_EQ(lowestValue, util::min(quietNAN, lowestValue)); - ASSERT_EQ(0, util::min(2, 1, 0)); - - 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)); -} -- cgit v1.2.1