| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342 |
- #region --- License ---
- /* Licensed under the MIT/X11 license.
- * Copyright (c) 2006-2008 the OpenTK Team.
- * This notice may not be removed from any source distribution.
- * See license.txt for licensing detailed licensing details.
- *
- * Contributions by Andy Gill, James Talton and Georg Wächter.
- */
- #endregion
- using System;
- namespace AtomicEngine
- {
- /// <summary>
- /// Contains common mathematical functions and constants.
- /// </summary>
- public static class MathHelper
- {
- #region Fields
- /// <summary>
- /// Defines the value of Pi as a <see cref="System.Single"/>.
- /// </summary>
- public const float Pi = 3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117067982148086513282306647093844609550582231725359408128481117450284102701938521105559644622948954930382f;
- /// <summary>
- /// Defines the value of Pi divided by two as a <see cref="System.Single"/>.
- /// </summary>
- public const float PiOver2 = Pi / 2;
- /// <summary>
- /// Defines the value of Pi divided by three as a <see cref="System.Single"/>.
- /// </summary>
- public const float PiOver3 = Pi / 3;
- /// <summary>
- /// Definesthe value of Pi divided by four as a <see cref="System.Single"/>.
- /// </summary>
- public const float PiOver4 = Pi / 4;
- /// <summary>
- /// Defines the value of Pi divided by six as a <see cref="System.Single"/>.
- /// </summary>
- public const float PiOver6 = Pi / 6;
- /// <summary>
- /// Defines the value of Pi multiplied by two as a <see cref="System.Single"/>.
- /// </summary>
- public const float TwoPi = 2 * Pi;
- /// <summary>
- /// Defines the value of Pi multiplied by 3 and divided by two as a <see cref="System.Single"/>.
- /// </summary>
- public const float ThreePiOver2 = 3 * Pi / 2;
- /// <summary>
- /// Defines the value of E as a <see cref="System.Single"/>.
- /// </summary>
- public const float E = 2.71828182845904523536f;
- /// <summary>
- /// Defines the base-10 logarithm of E.
- /// </summary>
- public const float Log10E = 0.434294482f;
- /// <summary>
- /// Defines the base-2 logarithm of E.
- /// </summary>
- public const float Log2E = 1.442695041f;
- public static readonly float DTORF = Pi / 180.0f;
- #endregion
- #region Public Members
- #region NextPowerOfTwo
- /// <summary>
- /// Returns the next power of two that is larger than the specified number.
- /// </summary>
- /// <param name="n">The specified number.</param>
- /// <returns>The next power of two.</returns>
- public static long NextPowerOfTwo(long n)
- {
- if (n < 0) throw new ArgumentOutOfRangeException("n", "Must be positive.");
- return (long)System.Math.Pow(2, System.Math.Ceiling(System.Math.Log((double)n, 2)));
- }
- /// <summary>
- /// Returns the next power of two that is larger than the specified number.
- /// </summary>
- /// <param name="n">The specified number.</param>
- /// <returns>The next power of two.</returns>
- public static int NextPowerOfTwo(int n)
- {
- if (n < 0) throw new ArgumentOutOfRangeException("n", "Must be positive.");
- return (int)System.Math.Pow(2, System.Math.Ceiling(System.Math.Log((double)n, 2)));
- }
- /// <summary>
- /// Returns the next power of two that is larger than the specified number.
- /// </summary>
- /// <param name="n">The specified number.</param>
- /// <returns>The next power of two.</returns>
- public static float NextPowerOfTwo(float n)
- {
- if (n < 0) throw new ArgumentOutOfRangeException("n", "Must be positive.");
- return (float)System.Math.Pow(2, System.Math.Ceiling(System.Math.Log((double)n, 2)));
- }
- /// <summary>
- /// Returns the next power of two that is larger than the specified number.
- /// </summary>
- /// <param name="n">The specified number.</param>
- /// <returns>The next power of two.</returns>
- public static double NextPowerOfTwo(double n)
- {
- if (n < 0) throw new ArgumentOutOfRangeException("n", "Must be positive.");
- return System.Math.Pow(2, System.Math.Ceiling(System.Math.Log((double)n, 2)));
- }
- #endregion
- #region Factorial
- /// <summary>Calculates the factorial of a given natural number.
- /// </summary>
- /// <param name="n">The number.</param>
- /// <returns>n!</returns>
- public static long Factorial(int n)
- {
- long result = 1;
- for (; n > 1; n--)
- result *= n;
- return result;
- }
- #endregion
- #region BinomialCoefficient
- /// <summary>
- /// Calculates the binomial coefficient <paramref name="n"/> above <paramref name="k"/>.
- /// </summary>
- /// <param name="n">The n.</param>
- /// <param name="k">The k.</param>
- /// <returns>n! / (k! * (n - k)!)</returns>
- public static long BinomialCoefficient(int n, int k)
- {
- return Factorial(n) / (Factorial(k) * Factorial(n - k));
- }
- #endregion
- #region InverseSqrtFast
- /// <summary>
- /// Returns an approximation of the inverse square root of left number.
- /// </summary>
- /// <param name="x">A number.</param>
- /// <returns>An approximation of the inverse square root of the specified number, with an upper error bound of 0.001</returns>
- /// <remarks>
- /// This is an improved implementation of the the method known as Carmack's inverse square root
- /// which is found in the Quake III source code. This implementation comes from
- /// http://www.codemaestro.com/reviews/review00000105.html. For the history of this method, see
- /// http://www.beyond3d.com/content/articles/8/
- /// </remarks>
- public static float InverseSqrtFast(float x)
- {
- unsafe
- {
- float xhalf = 0.5f * x;
- int i = *(int*)&x; // Read bits as integer.
- i = 0x5f375a86 - (i >> 1); // Make an initial guess for Newton-Raphson approximation
- x = *(float*)&i; // Convert bits back to float
- x = x * (1.5f - xhalf * x * x); // Perform left single Newton-Raphson step.
- return x;
- }
- }
- /// <summary>
- /// Returns an approximation of the inverse square root of left number.
- /// </summary>
- /// <param name="x">A number.</param>
- /// <returns>An approximation of the inverse square root of the specified number, with an upper error bound of 0.001</returns>
- /// <remarks>
- /// This is an improved implementation of the the method known as Carmack's inverse square root
- /// which is found in the Quake III source code. This implementation comes from
- /// http://www.codemaestro.com/reviews/review00000105.html. For the history of this method, see
- /// http://www.beyond3d.com/content/articles/8/
- /// </remarks>
- public static double InverseSqrtFast(double x)
- {
- return InverseSqrtFast((float)x);
- // TODO: The following code is wrong. Fix it, to improve precision.
- #if false
- unsafe
- {
- double xhalf = 0.5f * x;
- int i = *(int*)&x; // Read bits as integer.
- i = 0x5f375a86 - (i >> 1); // Make an initial guess for Newton-Raphson approximation
- x = *(float*)&i; // Convert bits back to float
- x = x * (1.5f - xhalf * x * x); // Perform left single Newton-Raphson step.
- return x;
- }
- #endif
- }
- #endregion
- #region DegreesToRadians
- /// <summary>
- /// Convert degrees to radians
- /// </summary>
- /// <param name="degrees">An angle in degrees</param>
- /// <returns>The angle expressed in radians</returns>
- public static float DegreesToRadians(float degrees)
- {
- const float degToRad = (float)System.Math.PI / 180.0f;
- return degrees * degToRad;
- }
- /// <summary>
- /// Convert radians to degrees
- /// </summary>
- /// <param name="radians">An angle in radians</param>
- /// <returns>The angle expressed in degrees</returns>
- public static float RadiansToDegrees(float radians)
- {
- const float radToDeg = 180.0f / (float)System.Math.PI;
- return radians * radToDeg;
- }
- /// <summary>
- /// Convert degrees to radians
- /// </summary>
- /// <param name="degrees">An angle in degrees</param>
- /// <returns>The angle expressed in radians</returns>
- public static double DegreesToRadians(double degrees)
- {
- const double degToRad = System.Math.PI / 180.0;
- return degrees * degToRad;
- }
- /// <summary>
- /// Convert radians to degrees
- /// </summary>
- /// <param name="radians">An angle in radians</param>
- /// <returns>The angle expressed in degrees</returns>
- public static double RadiansToDegrees(double radians)
- {
- const double radToDeg = 180.0 / System.Math.PI;
- return radians * radToDeg;
- }
- #endregion
- #region Swap
- /// <summary>
- /// Swaps two double values.
- /// </summary>
- /// <param name="a">The first value.</param>
- /// <param name="b">The second value.</param>
- public static void Swap(ref double a, ref double b)
- {
- double temp = a;
- a = b;
- b = temp;
- }
- /// <summary>
- /// Swaps two float values.
- /// </summary>
- /// <param name="a">The first value.</param>
- /// <param name="b">The second value.</param>
- public static void Swap(ref float a, ref float b)
- {
- float temp = a;
- a = b;
- b = temp;
- }
- #endregion
- #endregion
- /// <summary>
- /// Clamp a float to a range.
- /// </summary>
- public static float Clamp(float value, float min, float max)
- {
- if (value < min)
- return min;
- else if (value > max)
- return max;
- else
- return value;
- }
- public static float Lerp(float lhs, float rhs, float t)
- {
- return lhs * (1.0f - t) + rhs * t;
- }
- public static float WrapAngle(float angle)
- {
- angle = (float)Math.IEEERemainder((double)angle, 6.2831854820251465);
- if (angle <= -3.14159274f)
- {
- angle += 6.28318548f;
- }
- else
- {
- if (angle > 3.14159274f)
- {
- angle -= 6.28318548f;
- }
- }
- return angle;
- }
- public static float CatmullRom(float value1, float value2, float value3, float value4, float amount)
- {
- // Using formula from http://www.mvps.org/directx/articles/catmull/
- // Internally using doubles not to lose precission
- double amountSquared = amount * amount;
- double amountCubed = amountSquared * amount;
- return (float)(0.5 * (2.0 * value2 +
- (value3 - value1) * amount +
- (2.0 * value1 - 5.0 * value2 + 4.0 * value3 - value4) * amountSquared +
- (3.0 * value2 - value1 - 3.0 * value3 + value4) * amountCubed));
- }
- }
- }
|