diff options
Diffstat (limited to 'src/mbgl/util')
-rw-r--r-- | src/mbgl/util/geometry.hpp | 31 | ||||
-rw-r--r-- | src/mbgl/util/intersection_tests.cpp | 2 | ||||
-rw-r--r-- | src/mbgl/util/mat4.cpp | 11 | ||||
-rw-r--r-- | src/mbgl/util/mat4.hpp | 5 | ||||
-rw-r--r-- | src/mbgl/util/math.hpp | 20 | ||||
-rw-r--r-- | src/mbgl/util/tile_cover.cpp | 1 | ||||
-rw-r--r-- | src/mbgl/util/vec.hpp | 126 |
7 files changed, 49 insertions, 147 deletions
diff --git a/src/mbgl/util/geometry.hpp b/src/mbgl/util/geometry.hpp new file mode 100644 index 0000000000..bf44d5ce93 --- /dev/null +++ b/src/mbgl/util/geometry.hpp @@ -0,0 +1,31 @@ +#pragma once + +#include <mapbox/geometry/geometry.hpp> +#include <mapbox/geometry/point_arithmetic.hpp> + +namespace mbgl { + +template <class T> +using Point = mapbox::geometry::point<T>; + +template <class T> +using LineString = mapbox::geometry::line_string<T>; + +template <class T> +using Polygon = mapbox::geometry::polygon<T>; + +template <class T> +using MultiPoint = mapbox::geometry::multi_point<T>; + +template <class T> +using MultiLineString = mapbox::geometry::multi_line_string<T>; + +template <class T> +using MultiPolygon = mapbox::geometry::multi_polygon<T>; + +template <class S, class T> +Point<S> convertPoint(const Point<T>& p) { + return Point<S>(p.x, p.y); +} + +} // namespace mbgl diff --git a/src/mbgl/util/intersection_tests.cpp b/src/mbgl/util/intersection_tests.cpp index 44ec24db12..3e4ae27cd8 100644 --- a/src/mbgl/util/intersection_tests.cpp +++ b/src/mbgl/util/intersection_tests.cpp @@ -31,7 +31,7 @@ float distToSegmentSquared(const GeometryCoordinate& p, const GeometryCoordinate const float t = float((p.x - v.x) * (w.x - v.x) + (p.y - v.y) * (w.y - v.y)) / l2; if (t < 0) return util::distSqr<float>(p, v); if (t > 1) return util::distSqr<float>(p, w); - return util::distSqr<float>(p, vec2<float>(w - v) * t + v); + return util::distSqr<float>(p, convertPoint<float>(w - v) * t + convertPoint<float>(v)); } bool pointIntersectsBufferedLine(const GeometryCoordinate& p, const GeometryCoordinates& line, const float radius) { diff --git a/src/mbgl/util/mat4.cpp b/src/mbgl/util/mat4.cpp index bf8252595d..d3d3617b7b 100644 --- a/src/mbgl/util/mat4.cpp +++ b/src/mbgl/util/mat4.cpp @@ -21,7 +21,6 @@ // 3. This notice may not be removed or altered from any source distribution. #include <mbgl/util/mat4.hpp> -#include <mbgl/util/vec.hpp> #include <cmath> @@ -336,11 +335,11 @@ void multiply(mat4& out, const mat4& a, const mat4& b) { out[15] = b0 * a03 + b1 * a13 + b2 * a23 + b3 * a33; } -void transformMat4(vec4<double>& out, vec4<double>& a, mat4& m) { - out.x = m[0] * a.x + m[4] * a.y + m[8] * a.z + m[12] * a.w; - out.y = m[1] * a.x + m[5] * a.y + m[9] * a.z + m[13] * a.w; - out.z = m[2] * a.x + m[6] * a.y + m[10] * a.z + m[14] * a.w; - out.w = m[3] * a.x + m[7] * a.y + m[11] * a.z + m[15] * a.w; +void transformMat4(vec4& out, const vec4& a, const mat4& m) { + out[0] = m[0] * a[0] + m[4] * a[1] + m[8] * a[2] + m[12] * a[3]; + out[1] = m[1] * a[0] + m[5] * a[1] + m[9] * a[2] + m[13] * a[3]; + out[2] = m[2] * a[0] + m[6] * a[1] + m[10] * a[2] + m[14] * a[3]; + out[3] = m[3] * a[0] + m[7] * a[1] + m[11] * a[2] + m[15] * a[3]; } } // namespace matrix diff --git a/src/mbgl/util/mat4.hpp b/src/mbgl/util/mat4.hpp index ff8e65224c..80bd912554 100644 --- a/src/mbgl/util/mat4.hpp +++ b/src/mbgl/util/mat4.hpp @@ -23,12 +23,11 @@ #ifndef MBGL_UTIL_MAT4 #define MBGL_UTIL_MAT4 -#include <mbgl/util/vec.hpp> - #include <array> namespace mbgl { +using vec4 = std::array<double, 4>; using mat4 = std::array<double, 16>; namespace matrix { @@ -45,7 +44,7 @@ void rotate_z(mat4& out, const mat4& a, double rad); void scale(mat4& out, const mat4& a, double x, double y, double z); void multiply(mat4& out, const mat4& a, const mat4& b); -void transformMat4(vec4<double>& out, vec4<double>& a, mat4& m); +void transformMat4(vec4& out, const vec4& a, const mat4& m); } // namespace matrix } // namespace mbgl diff --git a/src/mbgl/util/math.hpp b/src/mbgl/util/math.hpp index 6c6b02d126..f93743f211 100644 --- a/src/mbgl/util/math.hpp +++ b/src/mbgl/util/math.hpp @@ -5,7 +5,7 @@ #include <array> #include <limits> -#include <mbgl/util/vec.hpp> +#include <mbgl/util/geometry.hpp> namespace mbgl { namespace util { @@ -15,17 +15,12 @@ namespace util { // 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); +inline T angle_between(const Point<S>& a, const Point<S>& b) { + return std::atan2((a.x * b.y - a.y * b.x), a.x * b.x + a.y * b.y); } 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) { +inline T angle_to(const Point<S>& a, const Point<S>& b) { return std::atan2(a.y - b.y, a.x - b.x); } @@ -39,7 +34,7 @@ inline std::array<T, 2> flip(const std::array<T, 2>& c) { } template <typename T, typename S1, typename S2> -inline vec2<T> normal(const S1& a, const S2& b) { +inline Point<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); @@ -102,6 +97,11 @@ inline T rotate(const T& a, S angle) { } template <typename T> +inline Point<T> matrixMultiply(const std::array<T, 4>& m, const Point<T>& p) { + return Point<T>(m[0] * p.x + m[1] * p.y, m[2] * p.x + m[3] * p.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); diff --git a/src/mbgl/util/tile_cover.cpp b/src/mbgl/util/tile_cover.cpp index f2bda3f45b..6cd5d4b4ab 100644 --- a/src/mbgl/util/tile_cover.cpp +++ b/src/mbgl/util/tile_cover.cpp @@ -1,5 +1,4 @@ #include <mbgl/util/tile_cover.hpp> -#include <mbgl/util/vec.hpp> #include <mbgl/util/constants.hpp> #include <mbgl/util/interpolate.hpp> #include <mbgl/map/transform_state.hpp> diff --git a/src/mbgl/util/vec.hpp b/src/mbgl/util/vec.hpp deleted file mode 100644 index 40d540fca4..0000000000 --- a/src/mbgl/util/vec.hpp +++ /dev/null @@ -1,126 +0,0 @@ -#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 |