diff options
author | John Firebaugh <john.firebaugh@gmail.com> | 2016-05-04 17:32:30 -0700 |
---|---|---|
committer | John Firebaugh <john.firebaugh@gmail.com> | 2016-05-05 11:16:57 -0700 |
commit | ce2a06e6773dfb656c7bf6fdbb7e8bc463710685 (patch) | |
tree | d3939aca3d9378a87832f305f68e82bc1fa6fd89 /include/mbgl/math | |
parent | a81891771441dfc1c839b9d100368b6bbf1fc127 (diff) | |
download | qtlocation-mapboxgl-ce2a06e6773dfb656c7bf6fdbb7e8bc463710685.tar.gz |
[core] Privatize math.hpp and vec.hpp
Diffstat (limited to 'include/mbgl/math')
-rw-r--r-- | include/mbgl/math/clamp.hpp | 14 | ||||
-rw-r--r-- | include/mbgl/math/minmax.hpp | 41 | ||||
-rw-r--r-- | include/mbgl/math/wrap.hpp | 17 |
3 files changed, 72 insertions, 0 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 |