-
-
Notifications
You must be signed in to change notification settings - Fork 281
/
Copy pathMathHelper.cs
354 lines (321 loc) · 14.4 KB
/
MathHelper.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
using System;
using System.Numerics;
using System.Runtime.CompilerServices;
namespace BepuUtilities
{
/// <summary>
/// Contains helper math methods.
/// </summary>
public static class MathHelper
{
/// <summary>
/// Approximate value of Pi.
/// </summary>
public const float Pi = 3.141592653589793239f;
/// <summary>
/// Approximate value of Pi multiplied by two.
/// </summary>
public const float TwoPi = 6.283185307179586477f;
/// <summary>
/// Approximate value of Pi divided by two.
/// </summary>
public const float PiOver2 = 1.570796326794896619f;
/// <summary>
/// Approximate value of Pi divided by four.
/// </summary>
public const float PiOver4 = 0.785398163397448310f;
/// <summary>
/// Clamps a value between a minimum and maximum value.
/// </summary>
/// <param name="value">Value to clamp.</param>
/// <param name="min">Minimum value. If the value is less than this, the minimum is returned instead.</param>
/// <param name="max">Maximum value. If the value is more than this, the maximum is returned instead.</param>
/// <returns>Clamped value.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static float Clamp(float value, float min, float max)
{
if (value < min)
return min;
else if (value > max)
return max;
return value;
}
/// <summary>
/// Returns the higher value of the two parameters.
/// </summary>
/// <param name="a">First value.</param>
/// <param name="b">Second value.</param>
/// <returns>Higher value of the two parameters.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static float Max(float a, float b)
{
return a > b ? a : b;
}
/// <summary>
/// Returns the lower value of the two parameters.
/// </summary>
/// <param name="a">First value.</param>
/// <param name="b">Second value.</param>
/// <returns>Lower value of the two parameters.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static float Min(float a, float b)
{
return a < b ? a : b;
}
/// <summary>
/// Clamps a value between a minimum and maximum value.
/// </summary>
/// <param name="value">Value to clamp.</param>
/// <param name="min">Minimum value. If the value is less than this, the minimum is returned instead.</param>
/// <param name="max">Maximum value. If the value is more than this, the maximum is returned instead.</param>
/// <returns>Clamped value.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static int Clamp(int value, int min, int max)
{
if (value < min)
return min;
else if (value > max)
return max;
return value;
}
/// <summary>
/// Returns the higher value of the two parameters.
/// </summary>
/// <param name="a">First value.</param>
/// <param name="b">Second value.</param>
/// <returns>Higher value of the two parameters.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static int Max(int a, int b)
{
return a > b ? a : b;
}
/// <summary>
/// Returns the lower value of the two parameters.
/// </summary>
/// <param name="a">First value.</param>
/// <param name="b">Second value.</param>
/// <returns>Lower value of the two parameters.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static int Min(int a, int b)
{
return a < b ? a : b;
}
/// <summary>
/// Clamps a value between a minimum and maximum value.
/// </summary>
/// <param name="value">Value to clamp.</param>
/// <param name="min">Minimum value. If the value is less than this, the minimum is returned instead.</param>
/// <param name="max">Maximum value. If the value is more than this, the maximum is returned instead.</param>
/// <returns>Clamped value.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static long Clamp(long value, long min, long max)
{
if (value < min)
return min;
else if (value > max)
return max;
return value;
}
/// <summary>
/// Returns the higher value of the two parameters.
/// </summary>
/// <param name="a">First value.</param>
/// <param name="b">Second value.</param>
/// <returns>Higher value of the two parameters.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static long Max(long a, long b)
{
return a > b ? a : b;
}
/// <summary>
/// Returns the lower value of the two parameters.
/// </summary>
/// <param name="a">First value.</param>
/// <param name="b">Second value.</param>
/// <returns>Lower value of the two parameters.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static long Min(long a, long b)
{
return a < b ? a : b;
}
/// <summary>
/// Converts degrees to radians.
/// </summary>
/// <param name="degrees">Degrees to convert.</param>
/// <returns>Radians equivalent to the input degrees.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static float ToRadians(float degrees)
{
return degrees * (Pi / 180f);
}
/// <summary>
/// Converts radians to degrees.
/// </summary>
/// <param name="radians">Radians to convert.</param>
/// <returns>Degrees equivalent to the input radians.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static float ToDegrees(float radians)
{
return radians * (180f / Pi);
}
/// <summary>
/// Returns -1 if the value is negative and 1 otherwise.
/// </summary>
/// <param name="x">Value to compute the sign of.</param>
/// <returns>-1 if the input is negative, and 1 otherwise.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static float BinarySign(float x)
{
return x < 0 ? -1 : 1;
}
//Note that these cos/sin implementations are not here for performance, but rather to:
//1) Provide a SIMD accelerated version for wide processing, and
//2) Provide a scalar implementation that is consistent with the SIMD version for systems which need to match its behavior.
//The main motivating use case is the pose integrator (which is scalar) and the sweep tests (which are widely vectorized).
/// <summary>
/// Computes an approximation of cosine. Maximum error a little above 3e-6.
/// </summary>
/// <param name="x">Value to take the cosine of.</param>
/// <returns>Approximate cosine of the input value.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static float Cos(float x)
{
//This exists primarily for consistency between the PoseIntegrator and sweeps, not necessarily for raw performance relative to Math.Cos.
if (x < 0)
x = -x;
var intervalIndex = x * (1f / TwoPi);
x -= (int)intervalIndex * TwoPi;
//[0, pi/2] = f(x)
//(pi/2, pi] = -f(Pi - x)
//(pi, 3 * pi / 2] = -f(x - Pi)
//(3*pi/2, 2*pi] = f(2 * Pi - x)
//This could be done more cleverly.
bool negate;
if (x < Pi)
{
if (x < PiOver2)
{
negate = false;
}
else
{
x = Pi - x;
negate = true;
}
}
else
{
if (x < 3 * PiOver2)
{
x = x - Pi;
negate = true;
}
else
{
x = TwoPi - x;
negate = false;
}
}
//The expression is a rational interpolation from 0 to Pi/2. Maximum error is a little more than 3e-6.
var x2 = x * x;
var x3 = x2 * x;
//TODO: This could be reorganized into two streams of FMAs if that was available.
var numerator = 1 - 0.24f * x - 0.4266f * x2 + 0.110838f * x3;
var denominator = 1 - 0.240082f * x + 0.0741637f * x2 - 0.0118786f * x3;
var result = numerator / denominator;
return negate ? -result : result;
}
/// <summary>
/// Computes an approximation of sine. Maximum error a little above 3e-6.
/// </summary>
/// <param name="x">Value to take the sine of.</param>
/// <returns>Approximate sine of the input value.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static float Sin(float x)
{
return Cos(x - PiOver2);
}
/// <summary>
/// Computes an approximation of cosine. Maximum error a little above 3e-6.
/// </summary>
/// <param name="x">Values to take the cosine of.</param>
/// <returns>Approximate cosine of the input values.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void Cos(in Vector<float> x, out Vector<float> result)
{
//This exists primarily for consistency between the PoseIntegrator and sweeps, not necessarily for raw performance relative to Math.Cos.
var periodX = Vector.Abs(x);
//TODO: No floor or truncate available... may want to revisit later.
periodX = periodX - TwoPi * Vector.ConvertToSingle(Vector.ConvertToInt32(periodX * (1f / TwoPi)));
//[0, pi/2] = f(x)
//(pi/2, pi] = -f(Pi - x)
//(pi, 3 * pi / 2] = -f(x - Pi)
//(3*pi/2, 2*pi] = f(2 * Pi - x)
//This could be done more cleverly.
Vector<float> y;
y = Vector.ConditionalSelect(Vector.GreaterThan(periodX, new Vector<float>(PiOver2)), new Vector<float>(Pi) - periodX, periodX);
y = Vector.ConditionalSelect(Vector.GreaterThan(periodX, new Vector<float>(Pi)), new Vector<float>(-Pi) + periodX, y);
y = Vector.ConditionalSelect(Vector.GreaterThan(periodX, new Vector<float>(3 * PiOver2)), new Vector<float>(TwoPi) - periodX, y);
//The expression is a rational interpolation from 0 to Pi/2. Maximum error is a little more than 3e-6.
var y2 = y * y;
var y3 = y2 * y;
//TODO: This could be reorganized into two streams of FMAs if that was available.
var numerator = Vector<float>.One - 0.24f * y - 0.4266f * y2 + 0.110838f * y3;
var denominator = Vector<float>.One - 0.240082f * y + 0.0741637f * y2 - 0.0118786f * y3;
result = numerator / denominator;
result = Vector.ConditionalSelect(
Vector.BitwiseAnd(
Vector.GreaterThan(periodX, new Vector<float>(PiOver2)),
Vector.LessThan(periodX, new Vector<float>(3 * PiOver2))), -result, result);
}
/// <summary>
/// Computes an approximation of sine. Maximum error a little above 3e-6.
/// </summary>
/// <param name="x">Value to take the sine of.</param>
/// <returns>Approximate sine of the input value.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void Sin(in Vector<float> x, out Vector<float> result)
{
Cos(x - new Vector<float>(PiOver2), out result);
}
/// <summary>
/// Computes an approximation of arccos. Maximum error less than 6.8e-5.
/// </summary>
/// <param name="x">Input value to the arccos function.</param>
/// <param name="acos">Result of the arccos function.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void ApproximateAcos(Vector<float> x, out Vector<float> acos)
{
//Adapted from Handbook of Mathematical Functions by Milton Abramowitz and Irene A. Stegun.
var negate = Vector.ConditionalSelect(Vector.LessThan(x, Vector<float>.Zero), Vector<float>.One, Vector<float>.Zero);
x = Vector.Abs(x);
acos = new Vector<float>(-0.0187293f) * x + new Vector<float>(0.0742610f);
acos = (acos * x - new Vector<float>(0.2121144f)) * x + new Vector<float>(1.5707288f);
acos *= Vector.SquareRoot(Vector.Max(Vector<float>.Zero, Vector<float>.One - x));
acos -= new Vector<float>(2) * negate * acos;
acos = negate * new Vector<float>(3.14159265358979f) + acos;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void Floor(in Vector<float> x, out Vector<float> result)
{
//This is far from ideal. You could probably do better- especially with platform intrinsics.
var intX = Vector.ConvertToInt32(x);
result = Vector.ConvertToSingle(Vector.ConditionalSelect(Vector.LessThan(x, Vector<float>.Zero), intX - Vector<int>.One, intX));
}
/// <summary>
/// Gets the change in angle from a to b as a signed value from -pi to pi.
/// </summary>
/// <param name="a">Source angle.</param>
/// <param name="b">Target angle.</param>
/// <param name="difference">Difference between a and b, expressed as a value from -pi to pi.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void GetSignedAngleDifference(in Vector<float> a, in Vector<float> b, out Vector<float> difference)
{
var pi = new Vector<float>(Pi);
var half = new Vector<float>(0.5f);
var x = (b - a) * new Vector<float>(1f / TwoPi) + half;
Floor(x, out var flooredX);
difference = (x - flooredX - half) * TwoPi;
}
}
}