[core] Privatize math.hpp and vec.hpp

This commit is contained in:
John Firebaugh
2016-05-04 17:32:30 -07:00
parent a818917714
commit ce2a06e677
23 changed files with 120 additions and 65 deletions

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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>

View File

@@ -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>

View File

@@ -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

View File

@@ -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>

View File

@@ -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>

View File

@@ -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 {

View File

@@ -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>

View File

@@ -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>

View File

@@ -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 {

View File

@@ -1,5 +1,5 @@
#include <mbgl/renderer/frame_history.hpp>
#include <mbgl/util/math.hpp>
#include <mbgl/math/minmax.hpp>
using namespace mbgl;

View File

@@ -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 {

View File

@@ -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>

View File

@@ -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>

View File

@@ -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>

View File

@@ -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>

View File

@@ -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 θ.
@@ -130,19 +101,6 @@ inline T rotate(const T& a, S angle) {
return T(x, y);
}
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));

24
test/math/clamp.cpp Normal file
View File

@@ -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));
}

View File

@@ -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));
}

View File

@@ -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',