-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUtils.cs
181 lines (158 loc) · 6.66 KB
/
Utils.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
using System;
using System.Runtime.CompilerServices;
using static H3.Constants;
[assembly: InternalsVisibleTo("H3.Test")]
namespace H3;
public static class Utils {
/// <summary>
/// Gets the specified number of top bits from the provided value.
/// </summary>
/// <param name="value"></param>
/// <param name="numBits"></param>
/// <returns></returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static ulong GetTopBits(this ulong value, int numBits) => value >> (64 - numBits);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool IsFinite(this double d) => !double.IsInfinity(d) && !double.IsNaN(d);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static double Square(double v) => v * v;
/// <summary>
/// Determines the azimuth to p2 from p1 in radians.
/// </summary>
/// <param name="p1Lon">p1 longitude, in radians</param>
/// <param name="p1Lat">p1 latitude, in radians</param>
/// <param name="p2Lon">p2 longitude, in radians</param>
/// <param name="p2Lat">p2 latitude, in radians</param>
/// <returns>azimuth, ...in radians!</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static double AzimuthInRadians(double p1Lon, double p1Lat, double p2Lon, double p2Lat) {
var cosP2Lat = Math.Cos(p2Lat);
return Math.Atan2(
cosP2Lat * Math.Sin(p2Lon - p1Lon),
Math.Cos(p1Lat) * Math.Sin(p2Lat) -
Math.Sin(p1Lat) * cosP2Lat * Math.Cos(p2Lon - p1Lon)
);
}
/// <summary>
/// The great circle distance in radians between two spherical coordinates.
///
/// This function uses the Haversine formula.
/// For math details, see:
/// * https://en.wikipedia.org/wiki/Haversine_formula
/// * https://www.movable-type.co.uk/scripts/latlong.html
/// </summary>
/// <param name="p2">Destination coordinate</param>
/// <returns>The great circle distance in radians between this coordinate
/// and the destination coordinate.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static double GreatCircleDistanceInRadians(double p1Lon, double p1Lat, double p2Lon, double p2Lat) {
var sinLat = Math.Sin((p2Lat - p1Lat) / 2.0);
var sinLon = Math.Sin((p2Lon - p1Lon) / 2.0);
var a = sinLat * sinLat + Math.Cos(p1Lat) * Math.Cos(p2Lat) * sinLon * sinLon;
return 2 * Math.Atan2(Math.Sqrt(a), Math.Sqrt(1 - a));
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static double NormalizeAngle(double radians) {
var tmp = radians < 0 ? radians + M_2PI : radians;
if (radians >= M_2PI) tmp -= M_2PI;
return tmp;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static double ConstrainLongitude(double longitude) {
while (longitude > M_PI) longitude -= 2 * M_PI;
while (longitude < -M_PI) longitude += 2 * M_PI;
return longitude;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static double TriangleEdgeLengthsToArea(double a, double b, double c) {
var s = (a + b + c) / 2;
a = (s - a) / 2;
b = (s - b) / 2;
c = (s - c) / 2;
s /= 2;
return 4 * Math.Atan(Math.Sqrt(Math.Tan(s) * Math.Tan(a) * Math.Tan(b) * Math.Tan(c)));
}
/// <summary>
/// Indicates whether or not the provided resolution has a Class 3 orientation.
/// </summary>
/// <param name="resolution"></param>
/// <returns></returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool IsResolutionClass3(int resolution) => resolution % 2 != 0;
/// <summary>
/// Indicates whether or not the specified child resolution is valid relative to the
/// provided parent resolution.
/// </summary>
/// <param name="parentResolution"></param>
/// <param name="childResolution"></param>
/// <returns></returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool IsValidChildResolution(int parentResolution, int childResolution) =>
childResolution >= parentResolution && childResolution <= MAX_H3_RES;
/// <summary>
/// Clamps the specified value between <paramref name="min">min</paramref>
/// and <paramref name="max">max</paramref>
/// </summary>
/// <param name="value">value to clamp</param>
/// <param name="min">minimum value</param>
/// <param name="max">maximum value</param>
/// <returns></returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static T Clamp<T>(T value, T min, T max) where T : IComparable<T> {
var result = value;
if (value.CompareTo(min) < 0) result = min;
if (value.CompareTo(max) > 0) result = max;
return result;
}
/// <summary>
/// Round implementation which replicates the C away from zero behavior
/// and for some reason performs better than Math.Round with midpoint rounding
/// option.
/// </summary>
/// <remarks>See the "double version of round behaves as if implemented as follows"
/// code here: https://en.cppreference.com/w/c/numeric/math/round#Notes
/// </remarks>
/// <param name="value"></param>
/// <returns></returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static double CRound(double value) => IsNegative(value) ? Math.Ceiling(value - 0.5) : Math.Floor(value + 0.5);
/// <summary>
/// Determines if the specified value is negative.
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static bool IsNegative(double value) => BitConverter.DoubleToInt64Bits(value) < 0;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static int LeadingZeros(int value) {
if (value <= 0) return value == 0 ? 32 : 0;
var n = 31;
if (value >= 1 << 16) {
n -= 16;
value >>= 16;
}
if (value >= 1 << 8) {
n -= 8;
value >>= 8;
}
if (value >= 1 << 4) {
n -= 4;
value >>= 4;
}
if (value >= 1 << 2) {
n -= 2;
value >>= 2;
}
return n - (value >> 1);
}
/// <summary>
/// Count the number of leading zero bits in a mask.
/// Similar in behavior to the x86 instruction LZCNT.
/// </summary>
/// <param name="value">The value.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static int LeadingZeros(this ulong value) {
var x = (int)(value >> 32);
return x == 0 ? 32 + LeadingZeros((int)(value >> 32)) : LeadingZeros(x);
}
}