|
1 | 1 | //****************************************************************************** |
2 | 2 | // Copyright 2013 Google Inc. |
3 | | -// https://github.com/googlemaps/android-maps-utils/blob/master/library/src/com/google/maps/android/MathUtil.java |
| 3 | +// https://github.com/googlemaps/android-maps-utils/blob/master/library/src/main/java/com/google/maps/android/MathUtil.java |
4 | 4 | // Licensed under the Apache License, Version 2.0 (the "License"); |
5 | 5 | // you may not use this file except in compliance with the License. |
6 | 6 | // You may obtain a copy of the License at |
|
19 | 19 |
|
20 | 20 | #define M_PI 3.14159265358979323846 |
21 | 21 |
|
| 22 | +inline double deg2rad(double degrees) { |
| 23 | + return degrees * M_PI / 180.0; |
| 24 | +} |
| 25 | + |
| 26 | +inline double rad2deg(double angle) { |
| 27 | + return angle * 180.0 / M_PI; |
| 28 | +} |
22 | 29 |
|
23 | 30 | class MathUtil { |
24 | 31 | public: |
25 | | - /** |
26 | | - * The earth's radius, in meters. |
27 | | - * Mean radius as defined by IUGG. |
28 | | - */ |
29 | | - const size_t EARTH_RADIUS = 6371009; |
30 | | - |
31 | | - /** |
32 | | - * Restrict x to the range [low, high]. |
33 | | - */ |
34 | | - static inline double clamp(double x, double low, double high) { |
35 | | - return x < low ? low : (x > high ? high : x); |
36 | | - } |
37 | | - |
38 | | - /** |
39 | | - * Wraps the given value into the inclusive-exclusive interval between min and max. |
40 | | - * @param n The value to wrap. |
41 | | - * @param min The minimum. |
42 | | - * @param max The maximum. |
43 | | - */ |
44 | | - static inline double wrap(double n, double min, double max) { |
45 | | - return (n >= min && n < max) ? n : (MathUtil::mod(n - min, max - min) + min); |
46 | | - } |
47 | | - |
48 | | - /** |
49 | | - * Returns the non-negative remainder of x / m. |
50 | | - * @param x The operand. |
51 | | - * @param m The modulus. |
52 | | - */ |
53 | | - static inline double mod(size_t x, size_t m) { |
54 | | - return ((x % m) + m) % m; |
55 | | - } |
56 | | - |
57 | | - /** |
58 | | - * Returns mercator Y corresponding to latitude. |
59 | | - * See http://en.wikipedia.org/wiki/Mercator_projection . |
60 | | - */ |
61 | | - static inline double mercator(double lat) { |
62 | | - return log(tan(lat * 0.5 + M_PI / 4.0)); |
63 | | - } |
64 | | - |
65 | | - /** |
66 | | - * Returns latitude from mercator Y. |
67 | | - */ |
68 | | - static inline double inverseMercator(double y) { |
69 | | - return 2.0 * atan(exp(y)) - M_PI / 2.0; |
70 | | - } |
71 | | - |
72 | | - /** |
73 | | - * Returns haversine(angle-in-radians). |
74 | | - * hav(x) == (1 - cos(x)) / 2 == sin(x / 2)^2. |
75 | | - */ |
76 | | - static inline double hav(double x) { |
77 | | - double sinHalf = sin(x * 0.5); |
78 | | - return sinHalf * sinHalf; |
79 | | - } |
80 | | - |
81 | | - /** |
82 | | - * Computes inverse haversine. Has good numerical stability around 0. |
83 | | - * arcHav(x) == acos(1 - 2 * x) == 2 * asin(sqrt(x)). |
84 | | - * The argument must be in [0, 1], and the result is positive. |
85 | | - */ |
86 | | - static inline double arcHav(double x) { |
87 | | - return 2.0 * asin(sqrt(x)); |
88 | | - } |
89 | | - |
90 | | - // Given h==hav(x), returns sin(abs(x)). |
91 | | - static inline double sinFromHav(double h) { |
92 | | - return 2.0 * sqrt(h * (1.0 - h)); |
93 | | - } |
94 | | - |
95 | | - // Returns hav(asin(x)). |
96 | | - static inline double havFromSin(double x) { |
97 | | - double x2 = x * x; |
98 | | - return x2 / (1.0 + sqrt(1.0 - x2)) * 0.5; |
99 | | - } |
100 | | - |
101 | | - // Returns sin(arcHav(x) + arcHav(y)). |
102 | | - static inline double sinSumFromHav(double x, double y) { |
103 | | - double a = sqrt(x * (1 - x)); |
104 | | - double b = sqrt(y * (1 - y)); |
105 | | - return 2.0 * (a + b - 2 * (a * y + b * x)); |
106 | | - } |
107 | | - |
108 | | - /** |
109 | | - * Returns hav() of distance from (lat1, lng1) to (lat2, lng2) on the unit sphere. |
110 | | - */ |
111 | | - static inline double havDistance(double lat1, double lat2, double dLng) { |
112 | | - return MathUtil::hav(lat1 - lat2) + MathUtil::hav(dLng) * cos(lat1) * cos(lat2); |
113 | | - } |
| 32 | + /** |
| 33 | + * The earth's radius, in meters. |
| 34 | + * Mean radius as defined by IUGG. |
| 35 | + */ |
| 36 | + static constexpr double EARTH_RADIUS = 6371009.0; |
| 37 | + |
| 38 | + /** |
| 39 | + * Restrict x to the range [low, high]. |
| 40 | + */ |
| 41 | + static inline double clamp(double x, double low, double high) { |
| 42 | + return x < low ? low : (x > high ? high : x); |
| 43 | + } |
| 44 | + |
| 45 | + /** |
| 46 | + * Wraps the given value into the inclusive-exclusive interval between min and max. |
| 47 | + * @param n The value to wrap. |
| 48 | + * @param min The minimum. |
| 49 | + * @param max The maximum. |
| 50 | + */ |
| 51 | + static inline double wrap(double n, double min, double max) { |
| 52 | + return (n >= min && n < max) ? n : (MathUtil::mod(n - min, max - min) + min); |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * Returns the non-negative remainder of x / m. |
| 57 | + * @param x The operand. |
| 58 | + * @param m The modulus. |
| 59 | + */ |
| 60 | + static inline double mod(double x, double m) { |
| 61 | + return remainder(remainder(x, m) + m, m); |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * Returns mercator Y corresponding to latitude. |
| 66 | + * See http://en.wikipedia.org/wiki/Mercator_projection . |
| 67 | + */ |
| 68 | + static inline double mercator(double lat) { |
| 69 | + return log(tan(lat * 0.5 + M_PI / 4.0)); |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * Returns latitude from mercator Y. |
| 74 | + */ |
| 75 | + static inline double inverseMercator(double y) { |
| 76 | + return 2.0 * atan(exp(y)) - M_PI / 2.0; |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * Returns haversine(angle-in-radians). |
| 81 | + * hav(x) == (1 - cos(x)) / 2 == sin(x / 2)^2. |
| 82 | + */ |
| 83 | + static inline double hav(double x) { |
| 84 | + double sinHalf = sin(x * 0.5); |
| 85 | + return sinHalf * sinHalf; |
| 86 | + } |
| 87 | + |
| 88 | + /** |
| 89 | + * Computes inverse haversine. Has good numerical stability around 0. |
| 90 | + * arcHav(x) == acos(1 - 2 * x) == 2 * asin(sqrt(x)). |
| 91 | + * The argument must be in [0, 1], and the result is positive. |
| 92 | + */ |
| 93 | + static inline double arcHav(double x) { |
| 94 | + return 2.0 * asin(sqrt(x)); |
| 95 | + } |
| 96 | + |
| 97 | + // Given h==hav(x), returns sin(abs(x)). |
| 98 | + static inline double sinFromHav(double h) { |
| 99 | + return 2.0 * sqrt(h * (1.0 - h)); |
| 100 | + } |
| 101 | + |
| 102 | + // Returns hav(asin(x)). |
| 103 | + static inline double havFromSin(double x) { |
| 104 | + double x2 = x * x; |
| 105 | + return x2 / (1.0 + sqrt(1.0 - x2)) * 0.5; |
| 106 | + } |
| 107 | + |
| 108 | + // Returns sin(arcHav(x) + arcHav(y)). |
| 109 | + static inline double sinSumFromHav(double x, double y) { |
| 110 | + double a = sqrt(x * (1 - x)); |
| 111 | + double b = sqrt(y * (1 - y)); |
| 112 | + return 2.0 * (a + b - 2 * (a * y + b * x)); |
| 113 | + } |
| 114 | + |
| 115 | + /** |
| 116 | + * Returns hav() of distance from (lat1, lng1) to (lat2, lng2) on the unit sphere. |
| 117 | + */ |
| 118 | + static inline double havDistance(double lat1, double lat2, double dLng) { |
| 119 | + return MathUtil::hav(lat1 - lat2) + MathUtil::hav(dLng) * cos(lat1) * cos(lat2); |
| 120 | + } |
114 | 121 | }; |
115 | 122 |
|
116 | 123 | #endif // GEOMETRY_LIBRARY_MATH_UTIL |
0 commit comments