ScriptCore: Added trigonometric functions to Math

I don't know why it claims I changed the entire file...
This commit is contained in:
Simon Lübeß
2024-01-16 13:32:46 +01:00
parent 60b45828c2
commit 9befd9e910
+246 -43
View File
@@ -1,4 +1,7 @@
using System;
// ReSharper disable InconsistentNaming
#pragma warning disable IDE1006
using System;
using System.Runtime.CompilerServices;
namespace GlitchyEngine.Math;
@@ -10,14 +13,19 @@ public static class Math
{
/// An optimal representation of π.
public const float Pi = 3.141592654f;
/// An optimal representation of 2*π.
public const float TwoPi = 6.283185307f;
/// An optimal representation of 1/π.
public const float OneOverPi = 0.318309886f;
/// An optimal representation of 2/π.
public const float OneOverTwoPi = 0.159154943f;
/// An optimal representation of π/2.
public const float PiOverTwo = 1.570796327f;
/// An optimal representation of π/4.
public const float PiOverFour = 0.785398163f;
@@ -30,19 +38,25 @@ public static class Math
/// Returns true if at least one of the components is true.
public static bool any(bool value) => value;
/// Returns true if at least one of the components is true.
public static bool any(bool2 value) => value.X || value.Y;
/// Returns true if at least one of the components is true.
public static bool any(bool3 value) => value.X || value.Y || value.Z;
/// Returns true if at least one of the components is true.
public static bool any(bool4 value) => value.X || value.Y || value.Z || value.W;
/// Returns true if all of the components are true.
public static bool all(bool value) => value;
/// Returns true if all of the components are true.
public static bool all(bool2 value) => value.X && value.Y;
/// Returns true if all of the components are true.
public static bool all(bool3 value) => value.X && value.Y && value.Z;
/// Returns true if all of the components are true.
public static bool all(bool4 value) => value.X && value.Y && value.Z && value.W;
@@ -65,7 +79,8 @@ public static class Math
public static float4 abs(float4 value)
{
return new float4(System.Math.Abs(value.X), System.Math.Abs(value.Y), System.Math.Abs(value.Z), System.Math.Abs(value.W));
return new float4(System.Math.Abs(value.X), System.Math.Abs(value.Y), System.Math.Abs(value.Z),
System.Math.Abs(value.W));
}
public static int abs(int value)
@@ -85,7 +100,8 @@ public static class Math
public static int4 abs(int4 value)
{
return new int4(System.Math.Abs(value.X), System.Math.Abs(value.Y), System.Math.Abs(value.Z), System.Math.Abs(value.W));
return new int4(System.Math.Abs(value.X), System.Math.Abs(value.Y), System.Math.Abs(value.Z),
System.Math.Abs(value.W));
}
#endregion
@@ -142,7 +158,8 @@ public static class Math
/// </summary>
public static float3 trunc(float3 x)
{
return new float3((float)System.Math.Truncate(x.X), (float)System.Math.Truncate(x.Y), (float)System.Math.Truncate(x.Z));
return new float3((float)System.Math.Truncate(x.X), (float)System.Math.Truncate(x.Y),
(float)System.Math.Truncate(x.Z));
}
/// <summary>
@@ -150,7 +167,8 @@ public static class Math
/// </summary>
public static float4 trunc(float4 x)
{
return new float4((float)System.Math.Truncate(x.X), (float)System.Math.Truncate(x.Y), (float)System.Math.Truncate(x.Z), (float)System.Math.Truncate(x.W));
return new float4((float)System.Math.Truncate(x.X), (float)System.Math.Truncate(x.Y),
(float)System.Math.Truncate(x.Z), (float)System.Math.Truncate(x.W));
}
#endregion
@@ -172,7 +190,8 @@ public static class Math
/// Determines if the specified floating-point value is finite.
public static bool4 isfinite(float4 value)
{
return new bool4(!float.IsInfinity(value.X), !float.IsInfinity(value.Y), !float.IsInfinity(value.Z), !float.IsInfinity(value.W));
return new bool4(!float.IsInfinity(value.X), !float.IsInfinity(value.Y), !float.IsInfinity(value.Z),
!float.IsInfinity(value.W));
}
/// Determines if the specified value is infinite.
@@ -190,7 +209,8 @@ public static class Math
/// Determines if the specified value is infinite.
public static bool4 isinf(float4 value)
{
return new bool4(float.IsInfinity(value.X), float.IsInfinity(value.Y), float.IsInfinity(value.Z), float.IsInfinity(value.W));
return new bool4(float.IsInfinity(value.X), float.IsInfinity(value.Y), float.IsInfinity(value.Z),
float.IsInfinity(value.W));
}
/// Determines if the specified value is infinite.
@@ -279,12 +299,14 @@ public static class Math
public static float3 ceil(float3 value)
{
return new float3((float)System.Math.Ceiling(value.X), (float)System.Math.Ceiling(value.Y), (float)System.Math.Ceiling(value.Z));
return new float3((float)System.Math.Ceiling(value.X), (float)System.Math.Ceiling(value.Y),
(float)System.Math.Ceiling(value.Z));
}
public static float4 ceil(float4 value)
{
return new float4((float)System.Math.Ceiling(value.X), (float)System.Math.Ceiling(value.Y), (float)System.Math.Ceiling(value.Z), (float)System.Math.Ceiling(value.W));
return new float4((float)System.Math.Ceiling(value.X), (float)System.Math.Ceiling(value.Y),
(float)System.Math.Ceiling(value.Z), (float)System.Math.Ceiling(value.W));
}
public static float floor(float value)
@@ -299,12 +321,14 @@ public static class Math
public static float3 floor(float3 value)
{
return new float3((float)System.Math.Floor(value.X), (float)System.Math.Floor(value.Y), (float)System.Math.Floor(value.Z));
return new float3((float)System.Math.Floor(value.X), (float)System.Math.Floor(value.Y),
(float)System.Math.Floor(value.Z));
}
public static float4 floor(float4 value)
{
return new float4((float)System.Math.Floor(value.X), (float)System.Math.Floor(value.Y), (float)System.Math.Floor(value.Z), (float)System.Math.Floor(value.W));
return new float4((float)System.Math.Floor(value.X), (float)System.Math.Floor(value.Y),
(float)System.Math.Floor(value.Z), (float)System.Math.Floor(value.W));
}
/// Rounds the specified value to the nearest integer.
@@ -322,13 +346,15 @@ public static class Math
/// Rounds the specified value to the nearest integer.
public static float3 round(float3 value)
{
return new float3((float)System.Math.Round(value.X), (float)System.Math.Round(value.Y), (float)System.Math.Round(value.Z));
return new float3((float)System.Math.Round(value.X), (float)System.Math.Round(value.Y),
(float)System.Math.Round(value.Z));
}
/// Rounds the specified value to the nearest integer.
public static float4 round(float4 value)
{
return new float4((float)System.Math.Round(value.X), (float)System.Math.Round(value.Y), (float)System.Math.Round(value.Z), (float)System.Math.Round(value.W));
return new float4((float)System.Math.Round(value.X), (float)System.Math.Round(value.Y),
(float)System.Math.Round(value.Z), (float)System.Math.Round(value.W));
}
#endregion
@@ -347,12 +373,14 @@ public static class Math
public static float3 min(float3 x, float3 y)
{
return new float3((float)System.Math.Min(x.X, y.X), (float)System.Math.Min(x.Y, y.Y), (float)System.Math.Min(x.Z, y.Z));
return new float3((float)System.Math.Min(x.X, y.X), (float)System.Math.Min(x.Y, y.Y),
(float)System.Math.Min(x.Z, y.Z));
}
public static float4 min(float4 x, float4 y)
{
return new float4((float)System.Math.Min(x.X, y.X), (float)System.Math.Min(x.Y, y.Y), (float)System.Math.Min(x.Z, y.Z), (float)System.Math.Min(x.W, y.W));
return new float4((float)System.Math.Min(x.X, y.X), (float)System.Math.Min(x.Y, y.Y),
(float)System.Math.Min(x.Z, y.Z), (float)System.Math.Min(x.W, y.W));
}
public static float max(float x, float y)
@@ -367,12 +395,14 @@ public static class Math
public static float3 max(float3 x, float3 y)
{
return new float3((float)System.Math.Max(x.X, y.X), (float)System.Math.Max(x.Y, y.Y), (float)System.Math.Max(x.Z, y.Z));
return new float3((float)System.Math.Max(x.X, y.X), (float)System.Math.Max(x.Y, y.Y),
(float)System.Math.Max(x.Z, y.Z));
}
public static float4 max(float4 x, float4 y)
{
return new float4((float)System.Math.Max(x.X, y.X), (float)System.Math.Max(x.Y, y.Y), (float)System.Math.Max(x.Z, y.Z), (float)System.Math.Max(x.W, y.W));
return new float4((float)System.Math.Max(x.X, y.X), (float)System.Math.Max(x.Y, y.Y),
(float)System.Math.Max(x.Z, y.Z), (float)System.Math.Max(x.W, y.W));
}
#endregion
@@ -396,13 +426,15 @@ public static class Math
/// Returns x raised to the power of y.
public static float3 pow(float3 x, float3 y)
{
return new float3((float)System.Math.Pow(x.X, y.X), (float)System.Math.Pow(x.Y, y.Y), (float)System.Math.Pow(x.Z, y.Z));
return new float3((float)System.Math.Pow(x.X, y.X), (float)System.Math.Pow(x.Y, y.Y),
(float)System.Math.Pow(x.Z, y.Z));
}
/// Returns x raised to the power of y.
public static float4 pow(float4 x, float4 y)
{
return new float4((float)System.Math.Pow(x.X, y.X), (float)System.Math.Pow(x.Y, y.Y), (float)System.Math.Pow(x.Z, y.Z), (float)System.Math.Pow(x.W, y.W));
return new float4((float)System.Math.Pow(x.X, y.X), (float)System.Math.Pow(x.Y, y.Y),
(float)System.Math.Pow(x.Z, y.Z), (float)System.Math.Pow(x.W, y.W));
}
/// Returns the base-e exponential, or e^x, of the specified value.
@@ -426,7 +458,8 @@ public static class Math
/// Returns the base-e exponential, or e^x, of the specified value.
public static float4 exp(float4 x)
{
return new float4((float)System.Math.Exp(x.X), (float)System.Math.Exp(x.Y), (float)System.Math.Exp(x.Z), (float)System.Math.Exp(x.W));
return new float4((float)System.Math.Exp(x.X), (float)System.Math.Exp(x.Y), (float)System.Math.Exp(x.Z),
(float)System.Math.Exp(x.W));
}
/// Returns the base 2 exponential, or 2^x, of the specified value.
@@ -444,13 +477,15 @@ public static class Math
/// Returns the base 2 exponential, or 2^x, of the specified value.
public static float3 exp2(float3 x)
{
return new float3((float)System.Math.Pow(2, x.X), (float)System.Math.Pow(2, x.Y), (float)System.Math.Pow(2, x.Z));
return new float3((float)System.Math.Pow(2, x.X), (float)System.Math.Pow(2, x.Y),
(float)System.Math.Pow(2, x.Z));
}
/// Returns the base 2 exponential, or 2^x, of the specified value.
public static float4 exp2(float4 x)
{
return new float4((float)System.Math.Pow(2, x.X), (float)System.Math.Pow(2, x.Y), (float)System.Math.Pow(2, x.Z), (float)System.Math.Pow(2, x.W));
return new float4((float)System.Math.Pow(2, x.X), (float)System.Math.Pow(2, x.Y),
(float)System.Math.Pow(2, x.Z), (float)System.Math.Pow(2, x.W));
}
#endregion
@@ -488,7 +523,8 @@ public static class Math
public static float4 clamp(float4 value, float4 min, float4 max)
{
return new float4(clamp(value.X, min.X, max.X), clamp(value.Y, min.Y, max.Y), clamp(value.Z, min.Z, max.Z), clamp(value.W, min.W, max.W));
return new float4(clamp(value.X, min.X, max.X), clamp(value.Y, min.Y, max.Y), clamp(value.Z, min.Z, max.Z),
clamp(value.W, min.W, max.W));
}
public static int clamp(int value, int min, int max)
@@ -508,7 +544,8 @@ public static class Math
public static int4 clamp(int4 value, int4 min, int4 max)
{
return new int4(clamp(value.X, min.X, max.X), clamp(value.Y, min.Y, max.Y), clamp(value.Z, min.Z, max.Z), clamp(value.W, min.W, max.W));
return new int4(clamp(value.X, min.X, max.X), clamp(value.Y, min.Y, max.Y), clamp(value.Z, min.Z, max.Z),
clamp(value.W, min.W, max.W));
}
@@ -567,8 +604,10 @@ public static class Math
/// Returns a reflection vector using an incident ray and a surface normal.
public static float2 reflect(float2 incident, float2 normal) => incident - 2 * normal * dot(incident, normal);
/// Returns a reflection vector using an incident ray and a surface normal.
public static float3 reflect(float3 incident, float3 normal) => incident - 2 * normal * dot(incident, normal);
/// Returns a reflection vector using an incident ray and a surface normal.
public static float4 reflect(float4 incident, float4 normal) => incident - 2 * normal * dot(incident, normal);
@@ -629,13 +668,15 @@ public static class Math
/// Calculates the per component square root of the given value.
public static float3 sqrt(float3 value)
{
return new float3((float)System.Math.Sqrt(value.X), (float)System.Math.Sqrt(value.Y), (float)System.Math.Sqrt(value.Z));
return new float3((float)System.Math.Sqrt(value.X), (float)System.Math.Sqrt(value.Y),
(float)System.Math.Sqrt(value.Z));
}
/// Calculates the per component square root of the given value.
public static float4 sqrt(float4 value)
{
return new float4((float)System.Math.Sqrt(value.X), (float)System.Math.Sqrt(value.Y), (float)System.Math.Sqrt(value.Z), (float)System.Math.Sqrt(value.W));
return new float4((float)System.Math.Sqrt(value.X), (float)System.Math.Sqrt(value.Y),
(float)System.Math.Sqrt(value.Z), (float)System.Math.Sqrt(value.W));
}
// saturate (I don't think there is a faster way than simply using clamp, so simply use clamp...)
@@ -844,25 +885,187 @@ public static class Math
// transpose und determinante für Matrizen
// sin, cos, tan, asin, acos, atan, atan2, cosh, sinh, tanh
#region Trigonometric Functions
public static float atan2(float y, float x)
{
return (float)System.Math.Atan2(y, x);
}
/// <summary>
/// Returns the tangent of the specified value.
/// </summary>
/// <param name="value">The specified value, in radians.</param>
/// <returns>The tangent of the value.</returns>
public static float tan(float value) => (float)System.Math.Tan(value);
public static float2 atan2(float2 y, float2 x)
{
return new float2((float)System.Math.Atan2(y.X, x.X), (float)System.Math.Atan2(y.Y, x.Y));
}
/// <inheritdoc cref="tan(float)"/>
public static float2 tan(float2 value) => new((float)System.Math.Tan(value.X), (float)System.Math.Tan(value.Y));
public static float3 atan2(float3 y, float3 x)
{
return new float3((float)System.Math.Atan2(y.X, x.X), (float)System.Math.Atan2(y.Y, x.Y), (float)System.Math.Atan2(y.Z, x.Z));
}
/// <inheritdoc cref="tan(float)"/>
public static float3 tan(float3 value) => new((float)System.Math.Tan(value.X), (float)System.Math.Tan(value.Y),
(float)System.Math.Tan(value.Z));
public static float4 atan2(float4 y, float4 x)
{
return new float4((float)System.Math.Atan2(y.X, x.X), (float)System.Math.Atan2(y.Y, x.Y), (float)System.Math.Atan2(y.Z, x.Z), (float)System.Math.Atan2(y.W, x.W));
}
/// <inheritdoc cref="tan(float)"/>
public static float4 tan(float4 value) => new((float)System.Math.Tan(value.X), (float)System.Math.Tan(value.Y),
(float)System.Math.Tan(value.Z), (float)System.Math.Tan(value.W));
/// <summary>
/// Returns the sine of the specified value.
/// </summary>
/// <param name="value">The specified value, in radians.</param>
/// <returns>The sine of the value.</returns>
public static float sin(float value) => (float)System.Math.Sin(value);
/// <inheritdoc cref="sin(float)"/>
public static float2 sin(float2 value) => new((float)System.Math.Sin(value.X), (float)System.Math.Sin(value.Y));
/// <inheritdoc cref="sin(float)"/>
public static float3 sin(float3 value) => new((float)System.Math.Sin(value.X), (float)System.Math.Sin(value.Y),
(float)System.Math.Sin(value.Z));
/// <inheritdoc cref="sin(float)"/>
public static float4 sin(float4 value) => new((float)System.Math.Sin(value.X), (float)System.Math.Sin(value.Y),
(float)System.Math.Sin(value.Z), (float)System.Math.Sin(value.W));
/// <summary>
/// Returns the cosine of the specified value.
/// </summary>
/// <param name="value">The specified value, in radians.</param>
/// <returns>The cosine of the value.</returns>
public static float cos(float value) => (float)System.Math.Cos(value);
/// <inheritdoc cref="cos(float)"/>
public static float2 cos(float2 value) => new((float)System.Math.Cos(value.X), (float)System.Math.Cos(value.Y));
/// <inheritdoc cref="cos(float)"/>
public static float3 cos(float3 value) => new((float)System.Math.Cos(value.X), (float)System.Math.Cos(value.Y),
(float)System.Math.Cos(value.Z));
/// <inheritdoc cref="cos(float)"/>
public static float4 cos(float4 value) => new((float)System.Math.Cos(value.X), (float)System.Math.Cos(value.Y),
(float)System.Math.Cos(value.Z), (float)System.Math.Cos(value.W));
/// <summary>
/// Returns the arcsine of the specified value.
/// </summary>
/// <param name="value">The specified value.</param>
/// <returns>The arcsine of the value.</returns>
public static float asin(float value) => (float)System.Math.Asin(value);
/// <inheritdoc cref="asin(float)"/>
public static float2 asin(float2 value) => new((float)System.Math.Asin(value.X), (float)System.Math.Asin(value.Y));
/// <inheritdoc cref="asin(float)"/>
public static float3 asin(float3 value) => new((float)System.Math.Asin(value.X), (float)System.Math.Asin(value.Y),
(float)System.Math.Asin(value.Z));
/// <inheritdoc cref="asin(float)"/>
public static float4 asin(float4 value) => new((float)System.Math.Asin(value.X), (float)System.Math.Asin(value.Y),
(float)System.Math.Asin(value.Z), (float)System.Math.Asin(value.W));
/// <summary>
/// Returns the arccosine of the specified value.
/// </summary>
/// <param name="value">The specified value.</param>
/// <returns>The arccosine of the value.</returns>
public static float acos(float value) => (float)System.Math.Acos(value);
/// <inheritdoc cref="acos(float)"/>
public static float2 acos(float2 value) => new((float)System.Math.Acos(value.X), (float)System.Math.Acos(value.Y));
/// <inheritdoc cref="acos(float)"/>
public static float3 acos(float3 value) => new((float)System.Math.Acos(value.X), (float)System.Math.Acos(value.Y),
(float)System.Math.Acos(value.Z));
/// <inheritdoc cref="acos(float)"/>
public static float4 acos(float4 value) => new((float)System.Math.Acos(value.X), (float)System.Math.Acos(value.Y),
(float)System.Math.Acos(value.Z), (float)System.Math.Acos(value.W));
/// <summary>
/// Returns the arctangent of the specified value.
/// </summary>
/// <param name="value">The specified value.</param>
/// <returns>The arctangent of the value.</returns>
public static float atan(float value) => (float)System.Math.Atan(value);
/// <inheritdoc cref="atan(float)"/>
public static float2 atan(float2 value) => new((float)System.Math.Atan(value.X), (float)System.Math.Atan(value.Y));
/// <inheritdoc cref="atan(float)"/>
public static float3 atan(float3 value) => new((float)System.Math.Atan(value.X), (float)System.Math.Atan(value.Y),
(float)System.Math.Atan(value.Z));
/// <inheritdoc cref="atan(float)"/>
public static float4 atan(float4 value) => new((float)System.Math.Atan(value.X), (float)System.Math.Atan(value.Y),
(float)System.Math.Atan(value.Z), (float)System.Math.Atan(value.W));
/// <summary>
/// Returns the arctangent of two values (x,y).
/// </summary>
/// <param name="y">The y value.</param>
/// <param name="x">The x value.</param>
/// <returns>The arctangent of (y,x).</returns>
public static float atan2(float y, float x) => (float)System.Math.Atan2(y, x);
/// <inheritdoc cref="atan2(float, float)"/>
public static float2 atan2(float2 y, float2 x) =>
new((float)System.Math.Atan2(y.X, x.X), (float)System.Math.Atan2(y.Y, x.Y));
/// <inheritdoc cref="atan2(float, float)"/>
public static float3 atan2(float3 y, float3 x) => new((float)System.Math.Atan2(y.X, x.X),
(float)System.Math.Atan2(y.Y, x.Y), (float)System.Math.Atan2(y.Z, x.Z));
/// <inheritdoc cref="atan2(float, float)"/>
public static float4 atan2(float4 y, float4 x) => new((float)System.Math.Atan2(y.X, x.X),
(float)System.Math.Atan2(y.Y, x.Y), (float)System.Math.Atan2(y.Z, x.Z), (float)System.Math.Atan2(y.W, x.W));
/// <summary>
/// Returns the hyperbolic sine of the specified value.
/// </summary>
/// <param name="value">The specified value.</param>
/// <returns>The hyperbolic sine of the value.</returns>
public static float sinh(float value) => (float)System.Math.Sinh(value);
/// <inheritdoc cref="sinh(float)"/>
public static float2 sinh(float2 value) => new((float)System.Math.Sinh(value.X), (float)System.Math.Sinh(value.Y));
/// <inheritdoc cref="sinh(float)"/>
public static float3 sinh(float3 value) => new((float)System.Math.Sinh(value.X), (float)System.Math.Sinh(value.Y),
(float)System.Math.Sinh(value.Z));
/// <inheritdoc cref="sinh(float)"/>
public static float4 sinh(float4 value) => new((float)System.Math.Sinh(value.X), (float)System.Math.Sinh(value.Y),
(float)System.Math.Sinh(value.Z), (float)System.Math.Sinh(value.W));
/// <summary>
/// Returns the hyperbolic cosine of the specified value.
/// </summary>
/// <param name="value">The specified value.</param>
/// <returns>The hyperbolic cosine of the value.</returns>
public static float cosh(float value) => (float)System.Math.Cosh(value);
/// <inheritdoc cref="cosh(float)"/>
public static float2 cosh(float2 value) => new((float)System.Math.Cosh(value.X), (float)System.Math.Cosh(value.Y));
/// <inheritdoc cref="cosh(float)"/>
public static float3 cosh(float3 value) => new((float)System.Math.Cosh(value.X), (float)System.Math.Cosh(value.Y),
(float)System.Math.Cosh(value.Z));
/// <inheritdoc cref="cosh(float)"/>
public static float4 cosh(float4 value) => new((float)System.Math.Cosh(value.X), (float)System.Math.Cosh(value.Y),
(float)System.Math.Cosh(value.Z), (float)System.Math.Cosh(value.W));
/// <summary>
/// Returns the hyperbolic tangent of the specified value.
/// </summary>
/// <param name="value">The specified value.</param>
/// <returns>The hyperbolic tangent of the value.</returns>
public static float tanh(float value) => (float)System.Math.Tanh(value);
/// <inheritdoc cref="tanh(float)"/>
public static float2 tanh(float2 value) => new((float)System.Math.Tanh(value.X), (float)System.Math.Tanh(value.Y));
/// <inheritdoc cref="tanh(float)"/>
public static float3 tanh(float3 value) => new((float)System.Math.Tanh(value.X), (float)System.Math.Tanh(value.Y), (float)System.Math.Tanh(value.Z));
/// <inheritdoc cref="tanh(float)"/>
public static float4 tanh(float4 value) => new((float)System.Math.Tanh(value.X), (float)System.Math.Tanh(value.Y), (float)System.Math.Tanh(value.Z), (float)System.Math.Tanh(value.W));
#endregion
}