31 namespace SiliconStudio.Core.Mathematics
38 public const float ZeroTolerance = 1e-6f;
43 public const double ZeroToleranceDouble = Double.Epsilon*8;
48 public const float Pi = (float)Math.PI;
53 public const float TwoPi = (
float)(2 * Math.PI);
58 public const float PiOverTwo = (float)(Math.PI / 2);
63 public const float PiOverFour = (float)(Math.PI / 4);
88 if ((aInt < 0) != (bInt < 0))
92 int ulp = Math.Abs(aInt - bInt);
97 return (ulp <= maxUlp);
107 return Math.Abs(
a) < ZeroTolerance;
117 return Math.Abs(
a) < ZeroToleranceDouble;
127 return IsZero(a - 1.0f);
140 return ((-epsilon <= num) && (num <= epsilon));
150 public static T[] Array<T>(T value,
int count)
152 T[] result =
new T[
count];
153 for (
int i = 0; i <
count; i++)
166 return revolution * 360.0f;
176 return revolution * TwoPi;
186 return revolution * 400.0f;
196 return degree / 360.0f;
206 return degree * (Pi / 180.0f);
216 return radian / TwoPi;
226 return radian * (200.0f / Pi);
236 return gradian / 400.0f;
246 return gradian * (9.0f / 10.0f);
256 return gradian * (Pi / 200.0f);
266 return radian * (180.0f / Pi);
276 public static float Clamp(
float value,
float min,
float max)
278 return value < min ? min : value > max ? max : value;
288 public static double Clamp(
double value,
double min,
double max)
290 return value < min ? min : value > max ? max : value;
300 public static int Clamp(
int value,
int min,
int max)
302 return value < min ? min : value > max ? max : value;
312 public static float InverseLerp(
float min,
float max,
float value)
314 if (IsZero(Math.Abs(max - min)))
316 return (value - min) / (max - min);
326 public static double InverseLerp(
double min,
double max,
double value)
328 if (IsZero(Math.Abs(max - min)))
330 return (value - min)/(max - min);
344 public static double Lerp(
double from,
double to,
double amount)
346 return (1 - amount) * from + amount * to;
360 public static float Lerp(
float from,
float to,
float amount)
362 return (1 - amount) * from + amount * to;
376 public static byte
Lerp(byte from, byte to,
float amount)
378 return (byte)Lerp((
float)from, (
float)to, amount);
390 return (amount <= 0) ? 0
392 : amount * amount * (3 - (2 * amount));
404 return (amount <= 0) ? 0
406 : amount * amount * amount * (amount * ((amount * 6) - 15) + 10);
415 public static int Mod(
int value,
int modulo)
422 return value < 0 ? value + modulo : value;
431 public static float Mod(
float value,
float modulo)
438 return value < 0 ? value + modulo : value;
448 return Mod(value, TwoPi);
459 public static int Wrap(
int value,
int min,
int max)
462 throw new ArgumentException(
string.Format(
"min {0} should be less than or equal to max {1}", min, max),
"min");
465 int range_size = max - min + 1;
468 value += range_size * ((min - value) / range_size + 1);
470 return min + (value - min) % range_size;
481 public static float Wrap(
float value,
float min,
float max)
483 if (NearEqual(min, max))
return min;
487 double valued = value;
490 throw new ArgumentException(
string.Format(
"min {0} should be less than or equal to max {1}", min, max),
"min");
492 var range_size = maxd - mind;
493 return (
float)(mind + (valued - mind) - (range_size * Math.Floor((valued - mind) / range_size)));
507 public static float Gauss(
float amplitude,
float x,
float y,
float radX,
float radY,
float sigmaX,
float sigmaY)
509 return (
float)Gauss((
double)amplitude, x, y, radX, radY, sigmaX, sigmaY);
523 public static double Gauss(
double amplitude,
double x,
double y,
double radX,
double radY,
double sigmaX,
double sigmaY)
525 return (amplitude * Math.E) -
527 Math.Pow(x - (radX / 2), 2) / (2 * Math.Pow(sigmaX, 2)) +
528 Math.Pow(y - (radY / 2), 2) / (2 * Math.Pow(sigmaY, 2))
static double InverseLerp(double min, double max, double value)
Inverse-interpolates a value linearly.
static float RevolutionsToDegrees(float revolution)
Converts revolutions to degrees.
static float RevolutionsToRadians(float revolution)
Converts revolutions to radians.
static double Gauss(double amplitude, double x, double y, double radX, double radY, double sigmaX, double sigmaY)
Gauss function.
static bool IsOne(float a)
Determines whether the specified value is close to one (1.0f).
_In_ size_t _In_ DXGI_FORMAT _In_ size_t _In_ float size_t y
static float Gauss(float amplitude, float x, float y, float radX, float radY, float sigmaX, float sigmaY)
Gauss function.
static float Mod2PI(float value)
Calculates the modulo 2*PI of the specified value.
static float SmootherStep(float amount)
Performs a smooth(er) interpolation between 0 and 1 with 1st and 2nd order derivatives of zero at end...
static int Wrap(int value, int min, int max)
Wraps the specified value into a range [min, max]
static float RadiansToDegrees(float radian)
Converts radians to degrees.
static float GradiansToRadians(float gradian)
Converts gradians to radians.
static bool WithinEpsilon(float a, float b, float epsilon)
Checks if a - b are almost equals within a float epsilon.
static float Wrap(float value, float min, float max)
Wraps the specified value into a range [min, max[
static float Clamp(float value, float min, float max)
Clamps the specified value.
static float DegreesToRevolutions(float degree)
Converts degrees to revolutions.
static int Mod(int value, int modulo)
Calculates the modulo of the specified value, handling negative values.
static float Lerp(float from, float to, float amount)
Interpolates between two values using a linear function by a given amount.
static float GradiansToRevolutions(float gradian)
Converts gradians to revolutions.
static byte Lerp(byte from, byte to, float amount)
Interpolates between two values using a linear function by a given amount.
static int Clamp(int value, int min, int max)
Clamps the specified value.
static float RadiansToRevolutions(float radian)
Converts radians to revolutions.
static double Clamp(double value, double min, double max)
Clamps the specified value.
static float SmoothStep(float amount)
Performs smooth (cubic Hermite) interpolation between 0 and 1.
static float RadiansToGradians(float radian)
Converts radians to gradians.
static unsafe bool NearEqual(float a, float b)
Checks if a and b are almost equals, taking into account the magnitude of floating point numbers (unl...
static bool IsZero(float a)
Determines whether the specified value is close to zero (0.0f).
static float DegreesToRadians(float degree)
Converts degrees to radians.
static float Mod(float value, float modulo)
Calculates the modulo of the specified value.
static float RevolutionsToGradians(float revolution)
Converts revolutions to gradians.
static float GradiansToDegrees(float gradian)
Converts gradians to degrees.
static bool IsZero(double a)
Determines whether the specified value is close to zero (0.0f).
static double Lerp(double from, double to, double amount)
Interpolates between two values using a linear function by a given amount.
static float InverseLerp(float min, float max, float value)
Inverse-interpolates a value linearly.