forked from BAndysc/WoWDatabaseEditor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMathUtil.cs
472 lines (422 loc) · 19 KB
/
MathUtil.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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
// Copyright (c) 2010-2014 SharpDX - Alexandre Mutel
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
// -----------------------------------------------------------------------------
// Original code from SlimMath project. http://code.google.com/p/slimmath/
// Greetings to SlimDX Group. Original code published with the following license:
// -----------------------------------------------------------------------------
/*
* Copyright (c) 2007-2011 SlimDX Group
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
using System;
namespace TheMaths
{
public static class MathUtil
{
/// <summary>
/// The value for which all absolute numbers smaller than are considered equal to zero.
/// </summary>
public const float ZeroTolerance = 1e-6f; // Value a 8x higher than 1.19209290E-07F
public const float PI = (float)Math.PI;
public const float Rad2Deg = 180 / PI;
public const float Deg2Rad = PI / 180;
/// <summary>
/// A value specifying the approximation of π which is 180 degrees.
/// </summary>
public const float Pi = (float)Math.PI;
/// <summary>
/// A value specifying the approximation of 2π which is 360 degrees.
/// </summary>
public const float TwoPi = (float)(2 * Math.PI);
/// <summary>
/// A value specifying the approximation of π/2 which is 90 degrees.
/// </summary>
public const float PiOverTwo = (float)(Math.PI / 2);
/// <summary>
/// A value specifying the approximation of π/4 which is 45 degrees.
/// </summary>
public const float PiOverFour = (float)(Math.PI / 4);
/// <summary>
/// Checks if a and b are almost equals, taking into account the magnitude of floating point numbers (unlike <see cref="WithinEpsilon"/> method). See Remarks.
/// See remarks.
/// </summary>
/// <param name="a">The left value to compare.</param>
/// <param name="b">The right value to compare.</param>
/// <returns><c>true</c> if a almost equal to b, <c>false</c> otherwise</returns>
/// <remarks>
/// The code is using the technique described by Bruce Dawson in
/// <a href="http://randomascii.wordpress.com/2012/02/25/comparing-floating-point-numbers-2012-edition/">Comparing Floating point numbers 2012 edition</a>.
/// </remarks>
public unsafe static bool NearEqual(float a, float b)
{
// Check if the numbers are really close -- needed
// when comparing numbers near zero.
if (IsZero(a - b))
return true;
// Original from Bruce Dawson: http://randomascii.wordpress.com/2012/02/25/comparing-floating-point-numbers-2012-edition/
int aInt = *(int*)&a;
int bInt = *(int*)&b;
// Different signs means they do not match.
if ((aInt < 0) != (bInt < 0))
return false;
// Find the difference in ULPs.
int ulp = Math.Abs(aInt - bInt);
// Choose of maxUlp = 4
// according to http://code.google.com/p/googletest/source/browse/trunk/include/gtest/internal/gtest-internal.h
const int maxUlp = 4;
return (ulp <= maxUlp);
}
/// <summary>
/// Determines whether the specified value is close to zero (0.0f).
/// </summary>
/// <param name="a">The floating value.</param>
/// <returns><c>true</c> if the specified value is close to zero (0.0f); otherwise, <c>false</c>.</returns>
public static bool IsZero(float a)
{
return Math.Abs(a) < ZeroTolerance;
}
/// <summary>
/// Determines whether the specified value is close to one (1.0f).
/// </summary>
/// <param name="a">The floating value.</param>
/// <returns><c>true</c> if the specified value is close to one (1.0f); otherwise, <c>false</c>.</returns>
public static bool IsOne(float a)
{
return IsZero(a - 1.0f);
}
/// <summary>
/// Checks if a - b are almost equals within a float epsilon.
/// </summary>
/// <param name="a">The left value to compare.</param>
/// <param name="b">The right value to compare.</param>
/// <param name="epsilon">Epsilon value</param>
/// <returns><c>true</c> if a almost equal to b within a float epsilon, <c>false</c> otherwise</returns>
public static bool WithinEpsilon(float a, float b, float epsilon)
{
float num = a - b;
return ((-epsilon <= num) && (num <= epsilon));
}
/// <summary>
/// Converts revolutions to degrees.
/// </summary>
/// <param name="revolution">The value to convert.</param>
/// <returns>The converted value.</returns>
public static float RevolutionsToDegrees(float revolution)
{
return revolution * 360.0f;
}
/// <summary>
/// Converts revolutions to radians.
/// </summary>
/// <param name="revolution">The value to convert.</param>
/// <returns>The converted value.</returns>
public static float RevolutionsToRadians(float revolution)
{
return revolution * TwoPi;
}
/// <summary>
/// Converts revolutions to gradians.
/// </summary>
/// <param name="revolution">The value to convert.</param>
/// <returns>The converted value.</returns>
public static float RevolutionsToGradians(float revolution)
{
return revolution * 400.0f;
}
/// <summary>
/// Converts degrees to revolutions.
/// </summary>
/// <param name="degree">The value to convert.</param>
/// <returns>The converted value.</returns>
public static float DegreesToRevolutions(float degree)
{
return degree / 360.0f;
}
/// <summary>
/// Converts degrees to radians.
/// </summary>
/// <param name="degree">The value to convert.</param>
/// <returns>The converted value.</returns>
public static float DegreesToRadians(float degree)
{
return degree * (Pi / 180.0f);
}
/// <summary>
/// Converts radians to revolutions.
/// </summary>
/// <param name="radian">The value to convert.</param>
/// <returns>The converted value.</returns>
public static float RadiansToRevolutions(float radian)
{
return radian / TwoPi;
}
/// <summary>
/// Converts radians to gradians.
/// </summary>
/// <param name="radian">The value to convert.</param>
/// <returns>The converted value.</returns>
public static float RadiansToGradians(float radian)
{
return radian * (200.0f / Pi);
}
/// <summary>
/// Converts gradians to revolutions.
/// </summary>
/// <param name="gradian">The value to convert.</param>
/// <returns>The converted value.</returns>
public static float GradiansToRevolutions(float gradian)
{
return gradian / 400.0f;
}
/// <summary>
/// Converts gradians to degrees.
/// </summary>
/// <param name="gradian">The value to convert.</param>
/// <returns>The converted value.</returns>
public static float GradiansToDegrees(float gradian)
{
return gradian * (9.0f / 10.0f);
}
/// <summary>
/// Converts gradians to radians.
/// </summary>
/// <param name="gradian">The value to convert.</param>
/// <returns>The converted value.</returns>
public static float GradiansToRadians(float gradian)
{
return gradian * (Pi / 200.0f);
}
/// <summary>
/// Converts radians to degrees.
/// </summary>
/// <param name="radian">The value to convert.</param>
/// <returns>The converted value.</returns>
public static float RadiansToDegrees(float radian)
{
return radian * (180.0f / Pi);
}
/// <summary>
/// Clamps the specified value.
/// </summary>
/// <param name="value">The value.</param>
/// <param name="min">The min.</param>
/// <param name="max">The max.</param>
/// <returns>The result of clamping a value between min and max</returns>
public static float Clamp(float value, float min, float max)
{
return value < min ? min : value > max ? max : value;
}
/// <summary>
/// Clamps the specified value.
/// </summary>
/// <param name="value">The value.</param>
/// <param name="min">The min.</param>
/// <param name="max">The max.</param>
/// <returns>The result of clamping a value between min and max</returns>
public static int Clamp(int value, int min, int max)
{
return value < min ? min : value > max ? max : value;
}
/// <summary>
/// Interpolates between two values using a linear function by a given amount.
/// </summary>
/// <remarks>
/// See http://www.encyclopediaofmath.org/index.php/Linear_interpolation and
/// http://fgiesen.wordpress.com/2012/08/15/linear-interpolation-past-present-and-future/
/// </remarks>
/// <param name="from">Value to interpolate from.</param>
/// <param name="to">Value to interpolate to.</param>
/// <param name="amount">Interpolation amount.</param>
/// <returns>The result of linear interpolation of values based on the amount.</returns>
public static double Lerp(double from, double to, double amount)
{
return (1 - amount) * from + amount * to;
}
/// <summary>
/// Interpolates between two values using a linear function by a given amount.
/// </summary>
/// <remarks>
/// See http://www.encyclopediaofmath.org/index.php/Linear_interpolation and
/// http://fgiesen.wordpress.com/2012/08/15/linear-interpolation-past-present-and-future/
/// </remarks>
/// <param name="from">Value to interpolate from.</param>
/// <param name="to">Value to interpolate to.</param>
/// <param name="amount">Interpolation amount.</param>
/// <returns>The result of linear interpolation of values based on the amount.</returns>
public static float Lerp(float from, float to, float amount)
{
return (1 - amount) * from + amount * to;
}
/// <summary>
/// Interpolates between two values using a linear function by a given amount.
/// </summary>
/// <remarks>
/// See http://www.encyclopediaofmath.org/index.php/Linear_interpolation and
/// http://fgiesen.wordpress.com/2012/08/15/linear-interpolation-past-present-and-future/
/// </remarks>
/// <param name="from">Value to interpolate from.</param>
/// <param name="to">Value to interpolate to.</param>
/// <param name="amount">Interpolation amount.</param>
/// <returns>The result of linear interpolation of values based on the amount.</returns>
public static byte Lerp(byte from, byte to, float amount)
{
return (byte)Lerp((float)from, (float)to, amount);
}
/// <summary>
/// Performs smooth (cubic Hermite) interpolation between 0 and 1.
/// </summary>
/// <remarks>
/// See https://en.wikipedia.org/wiki/Smoothstep
/// </remarks>
/// <param name="amount">Value between 0 and 1 indicating interpolation amount.</param>
public static float SmoothStep(float amount)
{
return (amount <= 0) ? 0
: (amount >= 1) ? 1
: amount * amount * (3 - (2 * amount));
}
/// <summary>
/// Performs a smooth(er) interpolation between 0 and 1 with 1st and 2nd order derivatives of zero at endpoints.
/// </summary>
/// <remarks>
/// See https://en.wikipedia.org/wiki/Smoothstep
/// </remarks>
/// <param name="amount">Value between 0 and 1 indicating interpolation amount.</param>
public static float SmootherStep(float amount)
{
return (amount <= 0) ? 0
: (amount >= 1) ? 1
: amount * amount * amount * (amount * ((amount * 6) - 15) + 10);
}
/// <summary>
/// Calculates the modulo of the specified value.
/// </summary>
/// <param name="value">The value.</param>
/// <param name="modulo">The modulo.</param>
/// <returns>The result of the modulo applied to value</returns>
public static float Mod(float value, float modulo)
{
if (modulo == 0.0f)
{
return value;
}
return value % modulo;
}
/// <summary>
/// Calculates the modulo 2*PI of the specified value.
/// </summary>
/// <param name="value">The value.</param>
/// <returns>The result of the modulo applied to value</returns>
public static float Mod2PI(float value)
{
return Mod(value, TwoPi);
}
/// <summary>
/// Wraps the specified value into a range [min, max]
/// </summary>
/// <param name="value">The value to wrap.</param>
/// <param name="min">The min.</param>
/// <param name="max">The max.</param>
/// <returns>Result of the wrapping.</returns>
/// <exception cref="ArgumentException">Is thrown when <paramref name="min"/> is greater than <paramref name="max"/>.</exception>
public static int Wrap(int value, int min, int max)
{
if (min > max)
throw new ArgumentException(string.Format("min {0} should be less than or equal to max {1}", min, max), "min");
// Code from http://stackoverflow.com/a/707426/1356325
int range_size = max - min + 1;
if (value < min)
value += range_size * ((min - value) / range_size + 1);
return min + (value - min) % range_size;
}
/// <summary>
/// Wraps the specified value into a range [min, max[
/// </summary>
/// <param name="value">The value.</param>
/// <param name="min">The min.</param>
/// <param name="max">The max.</param>
/// <returns>Result of the wrapping.</returns>
/// <exception cref="ArgumentException">Is thrown when <paramref name="min"/> is greater than <paramref name="max"/>.</exception>
public static float Wrap(float value, float min, float max)
{
if (NearEqual(min, max)) return min;
double mind = min;
double maxd = max;
double valued = value;
if (mind > maxd)
throw new ArgumentException(string.Format("min {0} should be less than or equal to max {1}", min, max), "min");
var range_size = maxd - mind;
return (float)(mind + (valued - mind) - (range_size * Math.Floor((valued - mind) / range_size)));
}
/// <summary>
/// Gauss function.
/// http://en.wikipedia.org/wiki/Gaussian_function#Two-dimensional_Gaussian_function
/// </summary>
/// <param name="amplitude">Curve amplitude.</param>
/// <param name="x">Position X.</param>
/// <param name="y">Position Y</param>
/// <param name="centerX">Center X.</param>
/// <param name="centerY">Center Y.</param>
/// <param name="sigmaX">Curve sigma X.</param>
/// <param name="sigmaY">Curve sigma Y.</param>
/// <returns>The result of Gaussian function.</returns>
public static float Gauss(float amplitude, float x, float y, float centerX, float centerY, float sigmaX, float sigmaY)
{
return (float)Gauss((double)amplitude, x, y, centerX, centerY, sigmaX, sigmaY);
}
/// <summary>
/// Gauss function.
/// http://en.wikipedia.org/wiki/Gaussian_function#Two-dimensional_Gaussian_function
/// </summary>
/// <param name="amplitude">Curve amplitude.</param>
/// <param name="x">Position X.</param>
/// <param name="y">Position Y</param>
/// <param name="centerX">Center X.</param>
/// <param name="centerY">Center Y.</param>
/// <param name="sigmaX">Curve sigma X.</param>
/// <param name="sigmaY">Curve sigma Y.</param>
/// <returns>The result of Gaussian function.</returns>
public static double Gauss(double amplitude, double x, double y, double centerX, double centerY, double sigmaX, double sigmaY)
{
var cx = x - centerX;
var cy = y - centerY;
var componentX = (cx * cx) / (2 * sigmaX * sigmaX);
var componentY = (cy * cy) / (2 * sigmaY * sigmaY);
return amplitude * Math.Exp(-(componentX + componentY));
}
}
}