1 #pragma once
2 
3 #include <cmath>
4 #include <array>
5 #include <limits>
6 
7 #include <mbgl/util/geometry.hpp>
8 
9 namespace mbgl {
10 namespace util {
11 
12 // TODO: split this file up into individual headers, following mbgl/math/*.hpp.
13 
14 // Find the angle of the two vectors, solving the formula for the cross product
15 // a x b = |a||b|sin(θ) for θ.
16 template <typename T = double, typename S>
angle_between(const Point<S> & a,const Point<S> & b)17 T angle_between(const Point<S>& a, const Point<S>& b) {
18     return std::atan2((a.x * b.y - a.y * b.x), a.x * b.x + a.y * b.y);
19 }
20 
21 template <typename T = double, typename S>
angle_to(const Point<S> & a,const Point<S> & b)22 T angle_to(const Point<S>& a, const Point<S>& b) {
23     return std::atan2(a.y - b.y, a.x - b.x);
24 }
25 
26 // Reflect an angle around 0 degrees
27 template <typename T>
flip(const std::array<T,2> & c)28 std::array<T, 2> flip(const std::array<T, 2>& c) {
29     return {{
30         static_cast<T>(2 * M_PI - c[0]),
31         static_cast<T>(2 * M_PI - c[1])
32     }};
33 }
34 
35 template <typename T, typename S1, typename S2>
normal(const S1 & a,const S2 & b)36 Point<T> normal(const S1& a, const S2& b) {
37     T dx = b.x - a.x;
38     T dy = b.y - a.y;
39     T c = std::sqrt(dx * dx + dy * dy);
40     return { dx / c, dy / c };
41 }
42 
43 template <typename T>
perp(const T & a)44 T perp(const T& a) {
45     return T(-a.y, a.x);
46 }
47 
48 template <typename T, typename S1, typename S2>
dist(const S1 & a,const S2 & b)49 T dist(const S1& a, const S2& b) {
50     T dx = b.x - a.x;
51     T dy = b.y - a.y;
52     T c = std::sqrt(dx * dx + dy * dy);
53     return c;
54 }
55 
56 template <typename T, typename S1, typename S2>
distSqr(const S1 & a,const S2 & b)57 T distSqr(const S1& a, const S2& b) {
58     T dx = b.x - a.x;
59     T dy = b.y - a.y;
60     T c = dx * dx + dy * dy;
61     return c;
62 }
63 
64 template <typename T>
round(const T & a)65 T round(const T& a) {
66     return T(::round(a.x), ::round(a.y));
67 }
68 
69 template <typename T>
length(T a,T b)70 T length(T a, T b) {
71     return std::sqrt(a * a + b * b);
72 }
73 
74 // Take the magnitude of vector a.
75 template <typename T = double, typename S>
mag(const S & a)76 T mag(const S& a) {
77     return std::sqrt(a.x * a.x + a.y * a.y);
78 }
79 
80 template <typename T = double, typename S>
unit(const S & a)81 S unit(const S& a) {
82     auto magnitude = mag<T>(a);
83     if (magnitude == 0) {
84         return a;
85     }
86     return a * (1 / magnitude);
87 }
88 
89 template <typename T, typename S = double>
rotate(const T & a,S angle)90 T rotate(const T& a, S angle) {
91     S cos = std::cos(angle);
92     S sin = std::sin(angle);
93     S x = cos * a.x - sin * a.y;
94     S y = sin * a.x + cos * a.y;
95     return T(x, y);
96 }
97 
98 template <typename T>
matrixMultiply(const std::array<T,4> & m,const Point<T> & p)99 Point<T> matrixMultiply(const std::array<T, 4>& m, const Point<T>& p) {
100     return Point<T>(m[0] * p.x + m[1] * p.y, m[2] * p.x + m[3] * p.y);
101 }
102 
103 template <typename T>
smoothstep(T edge0,T edge1,T x)104 T smoothstep(T edge0, T edge1, T x) {
105     T t = clamp((x - edge0) / (edge1 - edge0), T(0), T(1));
106     return t * t * (T(3) - T(2) * t);
107 }
108 
109 template <typename T>
division(const T dividend,const T divisor,const T nan)110 inline T division(const T dividend, const T divisor, const T nan) {
111     if (divisor == 0) {
112         if (dividend == 0) {
113             return nan;
114         } else {
115             return ::copysign(std::numeric_limits<T>::infinity(), dividend);
116         }
117     } else {
118         return dividend / divisor;
119     }
120 }
121 
122 } // namespace util
123 } // namespace mbgl
124