| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577 |
- using System;
- namespace BansheeEngine
- {
- /// <summary>
- /// Values that represent in which order are euler angles applied when used in transformations.
- /// </summary>
- public enum EulerAngleOrder
- {
- XYZ,
- XZY,
- YXZ,
- YZX,
- ZXY,
- ZYX
- };
- /// <summary>
- /// Utility class providing common scalar math operations.
- /// </summary>
- class MathEx
- {
- /// <summary>
- /// Pi constant.
- /// </summary>
- public const float Pi = 3.141593f;
- /// <summary>
- /// Two times pi constant.
- /// </summary>
- public const float TwoPi = (2.0f * Pi);
- /// <summary>
- /// Half of pi constant.
- /// </summary>
- public const float HalfPi = (0.5f * Pi);
- /// <summary>
- /// Constant that converts degrees to radians.
- /// </summary>
- public const float Deg2Rad = Pi / 180.0f;
- /// <summary>
- /// Constant that converts radians to degrees.
- /// </summary>
- public const float Rad2Deg = 180.0f / Pi;
- /// <summary>
- /// Returns the minimum value of the two provided.
- /// </summary>
- /// <param name="a">First value to compare.</param>
- /// <param name="b">Second value to compare.</param>
- /// <returns>Minimum of the two values.</returns>
- public static float Min(float a, float b)
- {
- if (a < b)
- return a;
-
- return b;
- }
- /// <summary>
- /// Returns the minimum value of all the values provided.
- /// </summary>
- /// <param name="values">Values to compare.</param>
- /// <returns>Minimum of all the values.</returns>
- public static float Min(params float[] values)
- {
- int length = values.Length;
- if (length == 0)
- return 0.0f;
- float min = values[0];
- for (int i = 1; i < length; i++)
- {
- if (values[i] < min)
- min = values[i];
- }
- return min;
- }
- /// <summary>
- /// Returns the minimum value of the two provided.
- /// </summary>
- /// <param name="a">First value to compare.</param>
- /// <param name="b">Second value to compare.</param>
- /// <returns>Minimum of the two values.</returns>
- public static int Min(int a, int b)
- {
- if (a < b)
- return a;
- return b;
- }
- /// <summary>
- /// Returns the minimum value of all the values provided.
- /// </summary>
- /// <param name="values">Values to compare.</param>
- /// <returns>Minimum of all the values.</returns>
- public static int Min(params int[] values)
- {
- int length = values.Length;
- if (length == 0)
- return 0;
- int min = values[0];
- for (int i = 1; i < length; i++)
- {
- if (values[i] < min)
- min = values[i];
- }
- return min;
- }
- /// <summary>
- /// Returns the maximum value of the two provided.
- /// </summary>
- /// <param name="a">First value to compare.</param>
- /// <param name="b">Second value to compare.</param>
- /// <returns>Maximum of the two values.</returns>
- public static float Max(float a, float b)
- {
- if (a > b)
- return a;
- return b;
- }
- /// <summary>
- /// Returns the maximum value of all the values provided.
- /// </summary>
- /// <param name="values">Values to compare.</param>
- /// <returns>Maximum of all the values.</returns>
- public static float Max(params float[] values)
- {
- int length = values.Length;
- if (length == 0)
- return 0.0f;
- float max = values[0];
- for (int i = 1; i < length; i++)
- {
- if (values[i] > max)
- max = values[i];
- }
- return max;
- }
- /// <summary>
- /// Returns the maximum value of the two provided.
- /// </summary>
- /// <param name="a">First value to compare.</param>
- /// <param name="b">Second value to compare.</param>
- /// <returns>Maximum of the two values.</returns>
- public static int Max(int a, int b)
- {
- if (a > b)
- return a;
- else
- return b;
- }
- /// <summary>
- /// Returns the maximum value of all the values provided.
- /// </summary>
- /// <param name="values">Values to compare.</param>
- /// <returns>Maximum of all the values.</returns>
- public static int Max(params int[] values)
- {
- int length = values.Length;
- if (length == 0)
- return 0;
- int max = values[0];
- for (int i = 1; i < length; ++i)
- {
- if (values[i] > max)
- max = values[i];
- }
- return max;
- }
- /// <summary>
- /// Returns the absolute value of the provided parameter.
- /// </summary>
- /// <param name="f">Parameter to take absolute value of.</param>
- /// <returns>Absolute value of <paramref name="f"/>.</returns>
- public static float Abs(float f)
- {
- return Math.Abs(f);
- }
- /// <summary>
- /// Returns the absolute value of the provided parameter.
- /// </summary>
- /// <param name="value">Parameter to take absolute value of.</param>
- /// <returns>Absolute value of <paramref name="value"/>.</returns>
- public static int Abs(int value)
- {
- return Math.Abs(value);
- }
- /// <summary>
- /// Raises <paramref name="f"/> to the power of <paramref name="p"/>.
- /// </summary>
- /// <param name="f">Value to raise to a power.</param>
- /// <param name="p">Power to raise the value to.</param>
- /// <returns><paramref name="f"/> raised to the power of <paramref name="p"/>.</returns>
- public static float Pow(float f, float p)
- {
- return (float)Math.Pow(f, p);
- }
- /// <summary>
- /// Raises e to the power of <paramref name="power"/>.
- /// </summary>
- /// <param name="power">Power to raise e to.</param>
- /// <returns>e raised to the power of <paramref name="power"/>.</returns>
- public static float Exp(float power)
- {
- return (float)Math.Exp(power);
- }
- /// <summary>
- /// Returns the logarithm of a number in a specified base.
- /// </summary>
- /// <param name="f">Value to get the logarithm of.</param>
- /// <param name="p">Base of the logarithm</param>
- /// <returns>Logarithm of a number in the specified base.</returns>
- public static float Log(float f, float p)
- {
- return (float)Math.Log(f, p);
- }
- /// <summary>
- /// Returns the natural logarithm (base e).
- /// </summary>
- /// <param name="f">Value to get the logarithm of.</param>
- /// <returns>Natural logarithm of a number.</returns>
- public static float Log(float f)
- {
- return (float)Math.Log(f);
- }
- /// <summary>
- /// Returns the logarithm of a number in base 10.
- /// </summary>
- /// <param name="f">Value to get the logarithm of.</param>
- /// <returns>Logarithm of a number in base 10.</returns>
- public static float Log10(float f)
- {
- return (float)Math.Log10(f);
- }
- /// <summary>
- /// Returns the smallest integral value that is greater than or equal to the provided value.
- /// </summary>
- /// <param name="f">Value to round.</param>
- /// <returns>Smallest integral value that is greater than or equal to the provided value.</returns>
- public static float Ceil(float f)
- {
- return (float)Math.Ceiling(f);
- }
- /// <summary>
- /// Returns the largest integral value that is lesser than or equal to the provided value.
- /// </summary>
- /// <param name="f">Value to round.</param>
- /// <returns>Largest integral value that is lessert than or equal to the provided value.</returns>
- public static float Floor(float f)
- {
- return (float)Math.Floor(f);
- }
- /// <summary>
- /// Rounds the provided value to the nearest integral.
- /// </summary>
- /// <param name="f">Value to round.</param>
- /// <returns>Value rounded to the nearest integral.</returns>
- public static float Round(float f)
- {
- return (float)Math.Round(f);
- }
- /// <summary>
- /// Returns the smallest integral value that is greater than or equal to the provided value.
- /// </summary>
- /// <param name="f">Value to round.</param>
- /// <returns>Smallest integral value that is greater than or equal to the provided value.</returns>
- public static int CeilToInt(float f)
- {
- return (int)Math.Ceiling(f);
- }
- /// <summary>
- /// Returns the largest integral value that is lesser than or equal to the provided value.
- /// </summary>
- /// <param name="f">Value to round.</param>
- /// <returns>Largest integral value that is lessert than or equal to the provided value.</returns>
- public static int FloorToInt(float f)
- {
- return (int)Math.Floor(f);
- }
- /// <summary>
- /// Rounds the provided value to the nearest integral.
- /// </summary>
- /// <param name="f">Value to round.</param>
- /// <returns>Value rounded to the nearest integral.</returns>
- public static int RoundToInt(float f)
- {
- return (int)Math.Round(f);
- }
- /// <summary>
- /// Returns the sign of the provided value (positive or negative).
- /// </summary>
- /// <param name="f">Value to get the sign of.</param>
- /// <returns>-1.0f if negative or 1.0f if positive.</returns>
- public static float Sign(float f)
- {
- return f >= 0.0f ? 1.0f : -1.0f;
- }
- /// <summary>
- /// Returns the sine of the provided value.
- /// </summary>
- /// <param name="f">Angle in radians.</param>
- /// <returns>Sine of the angle.</returns>
- public static float Sin(float f)
- {
- return (float)Math.Sin(f);
- }
- /// <summary>
- /// Returns the cosine of the provided value.
- /// </summary>
- /// <param name="f">Angle in radians.</param>
- /// <returns>Cosine of the angle.</returns>
- public static float Cos(float f)
- {
- return (float)Math.Cos(f);
- }
- /// <summary>
- /// Returns the tangent of the provided value.
- /// </summary>
- /// <param name="f">Angle in radians.</param>
- /// <returns>Tangent of the angle.</returns>
- public static float Tan(float f)
- {
- return (float)Math.Tan(f);
- }
- /// <summary>
- /// Returns the angle whose sine is the specified number.
- /// </summary>
- /// <param name="f">Sine of an angle.</param>
- /// <returns>Angle in radians.</returns>
- public static float Asin(float f)
- {
- return (float)Math.Asin(f);
- }
- /// <summary>
- /// Returns the angle whose cosine is the specified number.
- /// </summary>
- /// <param name="f">Cosine of an angle.</param>
- /// <returns>Angle in radians.</returns>
- public static float Acos(float f)
- {
- return (float)Math.Acos(f);
- }
- /// <summary>
- /// Returns the angle whose tangent is the specified number.
- /// </summary>
- /// <param name="f">Tangent of an angle.</param>
- /// <returns>Angle in radians.</returns>
- public static float Atan(float f)
- {
- return (float)Math.Atan(f);
- }
- /// <summary>
- /// Returns an angle of a point.
- /// </summary>
- /// <param name="y">Y coordinate of the point.</param>
- /// <param name="x">X coordinate of the point.</param>
- /// <returns>Angle in radians in range [Pi, -Pi].</returns>
- public static float Atan2(float y, float x)
- {
- return (float)Math.Atan2(y, x);
- }
- /// <summary>
- /// Returns the sine of the provided value.
- /// </summary>
- /// <param name="f">Angle in radians.</param>
- /// <returns>Sine of the angle.</returns>
- public static float Sin(Radian f)
- {
- return (float)Math.Sin(f.Radians);
- }
- /// <summary>
- /// Returns the cosine of the provided value.
- /// </summary>
- /// <param name="f">Angle in radians.</param>
- /// <returns>Cosine of the angle.</returns>
- public static float Cos(Radian f)
- {
- return (float)Math.Cos(f.Radians);
- }
- /// <summary>
- /// Returns the tangent of the provided value.
- /// </summary>
- /// <param name="f">Angle in radians.</param>
- /// <returns>Tangent of the angle.</returns>
- public static float Tan(Radian f)
- {
- return (float)Math.Tan(f.Radians);
- }
- /// <summary>
- /// Returns the angle whose sine is the specified number.
- /// </summary>
- /// <param name="f">Sine of an angle.</param>
- /// <returns>Angle in radians.</returns>
- public static float Asin(Radian f)
- {
- return (float)Math.Asin(f.Radians);
- }
- /// <summary>
- /// Returns the angle whose cosine is the specified number.
- /// </summary>
- /// <param name="f">Cosine of an angle.</param>
- /// <returns>Angle in radians.</returns>
- public static float Acos(Radian f)
- {
- return (float)Math.Acos(f.Radians);
- }
- /// <summary>
- /// Returns the angle whose tangent is the specified number.
- /// </summary>
- /// <param name="f">Tangent of an angle.</param>
- /// <returns>Angle in radians.</returns>
- public static float Atan(Radian f)
- {
- return (float)Math.Atan(f.Radians);
- }
- /// <summary>
- /// Returns an angle of a point.
- /// </summary>
- /// <param name="y">Y coordinate of the point.</param>
- /// <param name="x">X coordinate of the point.</param>
- /// <returns>Angle in radians in range [Pi, -Pi].</returns>
- public static float Atan2(Radian y, Radian x)
- {
- return (float)Math.Atan2(y.Radians, x.Radians);
- }
- /// <summary>
- /// Returns a square root of the provided value.
- /// </summary>
- /// <param name="f">Value to take the square root of. Must not be negative.</param>
- /// <returns>Square root of the provided value.</returns>
- public static float Sqrt(float f)
- {
- return (float)Math.Sqrt(f);
- }
- /// <summary>
- /// Returns an inverse square root (1/sqrt(x)) of the provided value.
- /// </summary>
- /// <param name="f">Value to take the inverse square root of. Must not be negative or zero.</param>
- /// <returns>Inverse square root of the provided value.</returns>
- public static float InvSqrt(float f)
- {
- return 1.0f/(float) Math.Sqrt(f);
- }
- /// <summary>
- /// Clamps a value between two other values.
- /// </summary>
- /// <param name="value">Value to clamp.</param>
- /// <param name="min">Minimum value of the range to clamp. Must be lower than <paramref name="max"/></param>
- /// <param name="max">Maximum value of the range to clamp. Must be higher than <paramref name="min"/></param>
- /// <returns>Returns unchanged value if it is in valid range, otherwise returns value clamped to the range
- /// extremes. </returns>
- public static float Clamp(float value, float min, float max)
- {
- if (value < min)
- value = min;
- else if (value > max)
- value = max;
- return value;
- }
- /// <summary>
- /// Clamps a value between two other values.
- /// </summary>
- /// <param name="value">Value to clamp.</param>
- /// <param name="min">Minimum value of the range to clamp. Must be lower than <paramref name="max"/></param>
- /// <param name="max">Maximum value of the range to clamp. Must be higher than <paramref name="min"/></param>
- /// <returns>Returns unchanged value if it is in valid range, otherwise returns value clamped to the range
- /// extremes. </returns>
- public static int Clamp(int value, int min, int max)
- {
- if (value < min)
- value = min;
- else if (value > max)
- value = max;
- return value;
- }
- /// <summary>
- /// Clamps a value between zero and one.
- /// </summary>
- /// <param name="value">Value to clamp.</param>
- /// <returns>Returns unchanged value if it is in [0, 1] range, otherwise returns value clamped to the range.
- /// </returns>
- public static float Clamp01(float value)
- {
- if (value < 0.0)
- return 0.0f;
- if (value > 1.0)
- return 1f;
-
- return value;
- }
- /// <summary>
- /// Wraps an angle in [0, 360) range. Values lower than zero, or higher or equal to 360
- /// will get wrapped around back into [0, 360) range.
- /// </summary>
- /// <param name="angle">Angle to wrap.</param>
- /// <returns>Angle in [0, 360) range.</returns>
- public static Degree WrapAngle(Degree angle)
- {
- const float inv360 = 1.0f/360.0f;
- float angleVal = angle.Degrees;
- float wrapCount = (float)MathEx.Floor(MathEx.Abs(angleVal * inv360));
- if (angleVal > 0.0f)
- angleVal -= 360.0f * wrapCount;
- else
- angleVal += 360.0f * wrapCount;
- return new Degree(angleVal);
- }
- /// <summary>
- /// Compares two floating point numbers with an error margin.
- /// </summary>
- /// <param name="a">First number to compare.</param>
- /// <param name="b">Second number to compare.</param>
- /// <param name="epsilon">Error margin within which the numbers should be considered equal.</param>
- /// <returns>True if equal, false otherwise.</returns>
- public static bool ApproxEquals(float a, float b, float epsilon = 1.192092896e-07F)
- {
- return Abs(b - a) <= epsilon;
- }
- }
- }
|