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