summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorJohn Firebaugh <john.firebaugh@gmail.com>2016-05-04 17:32:30 -0700
committerJohn Firebaugh <john.firebaugh@gmail.com>2016-05-05 11:16:57 -0700
commitce2a06e6773dfb656c7bf6fdbb7e8bc463710685 (patch)
treed3939aca3d9378a87832f305f68e82bc1fa6fd89 /src
parenta81891771441dfc1c839b9d100368b6bbf1fc127 (diff)
downloadqtlocation-mapboxgl-ce2a06e6773dfb656c7bf6fdbb7e8bc463710685.tar.gz
[core] Privatize math.hpp and vec.hpp
Diffstat (limited to 'src')
-rw-r--r--src/mbgl/annotation/shape_annotation_impl.cpp2
-rw-r--r--src/mbgl/geometry/feature_index.cpp5
-rw-r--r--src/mbgl/map/transform.cpp3
-rw-r--r--src/mbgl/map/transform_state.cpp1
-rw-r--r--src/mbgl/renderer/frame_history.cpp2
-rw-r--r--src/mbgl/renderer/symbol_bucket.cpp1
-rw-r--r--src/mbgl/source/source.cpp4
-rw-r--r--src/mbgl/style/style.cpp1
-rw-r--r--src/mbgl/text/glyph_set.cpp2
-rw-r--r--src/mbgl/util/geo.cpp2
-rw-r--r--src/mbgl/util/math.hpp117
-rw-r--r--src/mbgl/util/vec.hpp126
12 files changed, 257 insertions, 9 deletions
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/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 <cmath>
+#include <array>
+#include <limits>
+
+#include <mbgl/util/vec.hpp>
+
+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 <typename T = double, typename S>
+inline T angle_between(S ax, S ay, S bx, S by) {
+ return std::atan2((ax * by - ay * bx), ax * bx + ay * by);
+}
+
+template <typename T = double, typename S>
+inline T angle_between(const vec2<S>& a, const vec2<S>& b) {
+ return angle_between(a.x, a.y, b.x, b.y);
+}
+
+template <typename T = double, typename S>
+inline T angle_to(const vec2<S>& a, const vec2<S>& b) {
+ return std::atan2(a.y - b.y, a.x - b.x);
+}
+
+// Reflect an angle around 0 degrees
+template <typename T>
+inline std::array<T, 2> flip(const std::array<T, 2>& c) {
+ return {{
+ static_cast<T>(2 * M_PI - c[0]),
+ static_cast<T>(2 * M_PI - c[1])
+ }};
+}
+
+template <typename T, typename S1, typename S2>
+inline vec2<T> 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 <typename T>
+inline T perp(const T& a) {
+ return T(-a.y, a.x);
+}
+
+template <typename T, typename S1, typename S2>
+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 <typename T, typename S1, typename S2>
+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 <typename T>
+inline T round(const T& a) {
+ return T(::round(a.x), ::round(a.y));
+}
+
+template <typename T>
+inline T length(T a, T b) {
+ return std::sqrt(a * a + b * b);
+}
+
+// Take the magnitude of vector a.
+template <typename T = double, typename S>
+inline T mag(const S& a) {
+ return std::sqrt(a.x * a.x + a.y * a.y);
+}
+
+template <typename S>
+inline S unit(const S& a) {
+ auto magnitude = mag(a);
+ if (magnitude == 0) {
+ return a;
+ }
+ return a * (1 / magnitude);
+}
+
+template <typename T, typename S = double>
+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 <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);
+}
+
+// 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 <limits>
+#include <type_traits>
+#include <cmath>
+#include <cstdint>
+#include <array>
+
+namespace mbgl {
+
+template <typename T = double>
+struct vec2 {
+ struct null {};
+ typedef T Type;
+
+ T x, y;
+
+ inline vec2() = default;
+
+ template<typename U = T, typename std::enable_if<std::numeric_limits<U>::has_quiet_NaN, int>::type = 0>
+ inline vec2(null) : x(std::numeric_limits<T>::quiet_NaN()), y(std::numeric_limits<T>::quiet_NaN()) {}
+
+ template<typename U = T, typename std::enable_if<!std::numeric_limits<U>::has_quiet_NaN, int>::type = 0>
+ inline vec2(null) : x(std::numeric_limits<T>::min()), y(std::numeric_limits<T>::min()) {}
+
+ inline vec2(const vec2& o) = default;
+
+ template<typename U>
+ 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 <typename O>
+ inline typename std::enable_if<std::is_arithmetic<O>::value, vec2>::type
+ operator*(O o) const {
+ return {x * o, y * o};
+ }
+
+ inline void operator*=(T o) {
+ x *= o;
+ y *= o;
+ }
+
+ template <typename O>
+ inline typename std::enable_if<std::is_arithmetic<O>::value, vec2>::type
+ operator/(O o) const {
+ return {x / o, y / o};
+ }
+
+ inline void operator/=(T o) {
+ x /= o;
+ y /= o;
+ }
+
+ inline vec2<T> operator *(const std::array<float, 16>& matrix) {
+ return { x * matrix[0] + y * matrix[4] + matrix[12], x * matrix[1] + y * matrix[5] + matrix[13] };
+ }
+
+ template <typename O>
+ inline typename std::enable_if<std::is_arithmetic<O>::value, vec2>::type
+ operator-(O o) const {
+ return {x - o, y - o};
+ }
+
+ template <typename O>
+ inline typename std::enable_if<!std::is_arithmetic<O>::value, vec2>::type
+ operator-(const O &o) const {
+ return vec2<T>(x - o.x, y - o.y);
+ }
+
+ template <typename O>
+ inline typename std::enable_if<!std::is_arithmetic<O>::value, vec2>::type
+ operator+(const O &o) const {
+ return vec2<T>(x + o.x, y + o.y);
+ }
+
+ template <typename M>
+ inline vec2 matMul(const M &m) const {
+ return {m[0] * x + m[1] * y, m[2] * x + m[3] * y};
+ }
+
+ template<typename U = T, typename std::enable_if<std::numeric_limits<U>::has_quiet_NaN, int>::type = 0>
+ explicit operator bool() const {
+ return !std::isnan(x) && !std::isnan(y);
+ }
+
+ template<typename U = T, typename std::enable_if<!std::numeric_limits<U>::has_quiet_NaN, int>::type = 0>
+ explicit operator bool() const {
+ return x != std::numeric_limits<T>::min() && y != std::numeric_limits<T>::min();
+ }
+};
+
+template <typename T = double>
+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<typename U = T, typename std::enable_if<std::numeric_limits<U>::has_quiet_NaN, int>::type = 0>
+ explicit operator bool() const {
+ return !std::isnan(x) && !std::isnan(y) && !std::isnan(z) && !std::isnan(w);
+ }
+
+ template<typename U = T, typename std::enable_if<!std::numeric_limits<U>::has_quiet_NaN, int>::type = 0>
+ explicit operator bool() const {
+ return x != std::numeric_limits<T>::min()
+ && y != std::numeric_limits<T>::min()
+ && z != std::numeric_limits<T>::min()
+ && w != std::numeric_limits<T>::min();
+ }
+};
+
+
+} // namespace mbgl
+
+#endif // MBGL_UTIL_VEC