From a9ce347339b7c5fb44723866da9c33efb7a32da7 Mon Sep 17 00:00:00 2001 From: LTMX Date: Thu, 15 Apr 2021 22:55:31 +0200 Subject: [PATCH] Adjusted Namespaces & Added Mathematical Constants --- Runtime/Angle.cs | 36 ++++---- Runtime/Arithemetics.cs | 7 +- Runtime/Comparison.cs | 4 +- Runtime/Constants.cs | 87 +++++++++++++++++-- Runtime/Easing.cs | 4 +- Runtime/Easing.cs.meta | 11 +++ Runtime/Exponents And Logarithms.cs | 4 +- Runtime/Factorial Gamma.cs | 10 +-- Runtime/FastFunctions.cs | 4 +- Runtime/FastTrigonometry.cs | 5 +- Runtime/Interpolation.cs | 4 +- ...unctions.cs.meta => Interpolation.cs.meta} | 2 +- Runtime/Random.cs | 4 +- Runtime/Rounding.cs | 4 +- Runtime/Shorthands.cs | 5 +- Runtime/Smoothdamp.cs | 4 +- Runtime/Smoothing.cs | 23 +++++ Runtime/Smoothing.cs.meta | 11 +++ Runtime/Trigonometry.cs | 4 +- Runtime/TypeConversion.cs | 6 +- .../{UnityMathematicsExtensions.cs => UME.cs} | 6 +- ...hematicsExtensions.cs.meta => UME.cs.meta} | 0 Runtime/VectorFunctions.cs | 4 +- Runtime/VectorUpdaters.cs | 4 +- 24 files changed, 183 insertions(+), 70 deletions(-) create mode 100644 Runtime/Easing.cs.meta rename Runtime/{Smoothing Functions.cs.meta => Interpolation.cs.meta} (54%) create mode 100644 Runtime/Smoothing.cs create mode 100644 Runtime/Smoothing.cs.meta rename Runtime/{UnityMathematicsExtensions.cs => UME.cs} (96%) rename Runtime/{UnityMathematicsExtensions.cs.meta => UME.cs.meta} (100%) diff --git a/Runtime/Angle.cs b/Runtime/Angle.cs index cccc364..b461507 100644 --- a/Runtime/Angle.cs +++ b/Runtime/Angle.cs @@ -1,8 +1,10 @@ using Unity.Mathematics; using UnityEngine; -namespace UME { - public static partial class UnityMathematicsExtensions { +namespace Plugins.Mathematics_Extensions.Runtime +{ + public static partial class UME + { //translated from UnityEngine /// Returns the signed angle between two vectors in radians public static float angle (float3 from, float3 to) @@ -22,9 +24,9 @@ public static float angle (float2 from, float2 to) { return num < 1.00000000362749E-15f ? 0 : (math.dot (from, to) / num).npsaturate ().acos (); // * 57.29578f; } - public static float fastangle (float4 from, float4 to) => (math.dot (from, to) / (from.lengthsq () * to.lengthsq ()).fastsqrt ()).npsaturate ().acos (); - public static float fastangle (float3 from, float3 to) => (math.dot (from, to) / (from.lengthsq () * to.lengthsq ()).fastsqrt ()).npsaturate ().acos (); - public static float fastangle (float2 from, float2 to) => (math.dot (from, to) / (from.lengthsq () * to.lengthsq ()).fastsqrt ()).npsaturate ().acos (); + public static float fastangle (float4 from, float4 to) => (math.dot(from, to) / (from.lengthsq() * to.lengthsq()).fastsqrt()).npsaturate().acos(); + public static float fastangle (float3 from, float3 to) => (math.dot(from, to) / (from.lengthsq() * to.lengthsq()).fastsqrt()).npsaturate().acos(); + public static float fastangle (float2 from, float2 to) => (math.dot(from, to) / (from.lengthsq() * to.lengthsq()).fastsqrt()).npsaturate().acos(); // another way of computing angles //public static float otherangle(float2 v1, float2 v2) => math.atan2(v1.x * v2.y - v2.x * v1.y, (v1 * v2).sum()) * (180 / math.PI); @@ -37,26 +39,26 @@ public static float angle (float2 from, float2 to) { public static double straightsignedangle (double3 f1, double3 f2, double3 n) => math.atan2 (math.dot (n, math.cross (f1, f2)), math.dot (f1, f2)); public static float preciseangle (float3 v1, float3 v2) { - var v3 = v1.normalized (); - var v4 = v2.normalized (); + var v3 = v1.normalized(); + var v4 = v2.normalized(); return math.dot (v1, v2) < 0 ? - math.PI - 2 * ((-v3 - v4).length () / 2).asin () : - 2 * ((v3 - v4).length () / 2).asin (); + math.PI - 2 * ((-v3 - v4).length() / 2).asin() : + 2 * ((v3 - v4).length() / 2).asin(); } public static float preciseangle (float2 v1, float2 v2) { - var v3 = v1.normalized (); - var v4 = v2.normalized (); + var v3 = v1.normalized(); + var v4 = v2.normalized(); return math.dot (v3, v4) < 0 ? - math.PI - 2 * ((-v3 - v4).length () / 2).asin () : - 2 * ((v3 - v4).length () / 2).asin (); + math.PI - 2 * ((-v3 - v4).length() / 2).asin() : + 2 * ((v3 - v4).length() / 2).asin(); } /// Returns the signed angle between two vectors in radians using an axis of rotation - public static float signedangle (float4 from, float4 to, float4 axis) => angle (from, to) * ((from.yzwx * to.zwxy - from.zwxy * to.yzwx) * axis).sum ().sign (); + public static float signedangle (float4 from, float4 to, float4 axis) => angle (from, to) * ((from.yzwx * to.zwxy - from.zwxy * to.yzwx) * axis).sum().sign(); /// Returns the signed angle between two vectors in radians using an axis of rotation - public static float signedangle (float3 from, float3 to, float3 axis) => angle (from, to) * ((from.yzx * to.zxy - from.zxy * to.yzx) * axis).sum ().sign (); + public static float signedangle (float3 from, float3 to, float3 axis) => angle (from, to) * ((from.yzx * to.zxy - from.zxy * to.yzx) * axis).sum().sign(); /// Returns the signed angle between two vectors in radians; - public static float signedangle (Vector2 from, Vector2 to) => angle (from, to) * (from.x * to.y - from.y * to.x).sign (); + public static float signedangle (Vector2 from, Vector2 to) => angle (from, to) * (from.x * to.y - from.y * to.x).sign(); //https://gist.github.com/voidqk/fc5a58b7d9fc020ecf7f2f5fc907dfa5 // Computes atan2(y,x), fast --> max err: 0.071115 @@ -64,7 +66,7 @@ public static float fastatan2 (float y, float x) { const float c1 = math.PI / 4; const float c2 = math.PI * 0.75f; if (y == 0 && x == 0) return 0; - var abs_y = y.abs (); + var abs_y = y.abs(); float angle; if (x >= 0) angle = c1 - c1 * ((x - abs_y) / (x + abs_y)); else angle = c2 - c1 * ((x + abs_y) / (abs_y - x)); diff --git a/Runtime/Arithemetics.cs b/Runtime/Arithemetics.cs index 2cdacc4..d82a517 100644 --- a/Runtime/Arithemetics.cs +++ b/Runtime/Arithemetics.cs @@ -1,10 +1,9 @@ -using System.Runtime.CompilerServices; -using Unity.Mathematics; +using Unity.Mathematics; using UnityEngine; -namespace UME +namespace Plugins.Mathematics_Extensions.Runtime { - public static partial class UnityMathematicsExtensions + public static partial class UME { // Sign public static float4 sign(this float4 f) => math.sign(f); diff --git a/Runtime/Comparison.cs b/Runtime/Comparison.cs index 66ff58a..880c47f 100644 --- a/Runtime/Comparison.cs +++ b/Runtime/Comparison.cs @@ -1,9 +1,9 @@ using Unity.Mathematics; using UnityEngine; -namespace UME +namespace Plugins.Mathematics_Extensions.Runtime { - public static partial class UnityMathematicsExtensions + public static partial class UME { // Component-wise comparison -------------------------------------------------------------- diff --git a/Runtime/Constants.cs b/Runtime/Constants.cs index c6152ef..58ab2bf 100644 --- a/Runtime/Constants.cs +++ b/Runtime/Constants.cs @@ -1,21 +1,96 @@ -namespace UME +namespace Plugins.Mathematics_Extensions.Runtime { - public static partial class UnityMathematicsExtensions + public static partial class UME { public const double HPI_DBL = 1.57079632679489661923; public const float HPI = 1.570796326795f; - + public const double PI_DBL = 3.14159265358979323846; public const float PI = 3.14159265359f; - + public const double TAU_DBL = 6.283185307179586477; public const float TAU = 6.28318530718f; public const double PHI_DBL = 1.6180339887498948482; - public const float PHI = 1.61803398875f; - + public const float PHI = 1.61803398875f; + public const float PINFINITY = float.PositiveInfinity; public const float NINFINITY = float.NegativeInfinity; public const double PINFINITY_DBL = double.PositiveInfinity; public const double NINFINITY_DBL = double.NegativeInfinity; + + + // Translated from https://github.com/JJ/p6-math-constants/blob/master/lib/Math/Constants.pm6 + // Update physical constants from https://nist.gov/cuu/Constants -- CODATA 2018 recommendations + + // Physical Constants + public const float plancks_h = 6.626_070_015e-34f; + public const float plancks_reduced_h = 1.054_571_817e-34f; + public const float speed_of_light_vacuum = 299792458f; + public const float standard_acceleration_gravity = 9.80665f; + public const float gravitation = 6.67430e-11f; + public const float gas = 8.314462618f; + public const float faraday = 96485.33212f; + public const float electron_mass = 9.1093837015e-31f; + public const float proton_mass = 1.67262192369e-27f; + public const float neutron_mass = 1.67492749804e-27f; + public const float alpha_particle_mass = 6.6446573357e-27f; + public const float quantum_ratio = 2.417989242e14f; + public const float planck_mass = 2.176434e-8f; + public const float planck_time = 5.391247e-44f; + public const float planck_length = 1.616255e-35f; + public const float planck_temperature = 1.416784e+32f; + public const float kg_amu = 6.02214076e23f; + public const float coulomb = 8.9875517887e9f; + public const float fine_structure = 0.0072973525693f; + public const float elementary_charge = 1.602176634e-19f; + public const float vacuum_permittivity = 8.8541878128e-12f; + public const float magnetic_permeability = 12.5663706212e-7f; + public const float boltzmann = 1.380649e-23f; // was in eV, now in J K^-1 + public const float electron_volt = 1.602176634e-19f; + public const float vacuum_permeability = 12.5663706212e-7f; + + // # Mathematical constants + // # REF: https://en.wikipedia.org/wiki/Mathematical_constant + + public const float phi = 1.61803398874989e0f; + public const float alpha_feigenbaum = 2.502907875095892822283e0f; + public const float delta_feigenbaum = 4.669201609102990e0f; + public const float apery = 1.2020569031595942853997381e0f; + public const float conway = 1.303577269034e0f; + public const float khinchin = 2.6854520010e0f; + public const float glaisher_kinkelin = 1.2824271291e0f; + public const float golomb_dickman = 0.62432998854355e0f; + public const float catalan = 0.915965594177219015054603514e0f; + public const float mill = 1.3063778838630806904686144e0f; + public const float gauss = 0.8346268e0f; + public const float euler_mascheroni_gamma = 0.57721566490153286060e0f; + public const float sierpinski_gamma = 2.5849817595e0f; + + // Standard short names when available + + public const float A = glaisher_kinkelin; + public const float c = speed_of_light_vacuum; + public const float eV = electron_volt; + public const float F = faraday; + public const float G = gravitation; + public const float g = standard_acceleration_gravity; + public const float ℎ = plancks_h; + public const float ℏ = plancks_reduced_h; + public const float K0 = coulomb; + public const float k0 = khinchin; + public const float k = sierpinski_gamma; + public const float L = kg_amu; + public const float lp = planck_length; + public const float mp = planck_mass; + public const float q = elementary_charge; + public const float Tp = planck_temperature; + public const float tp = planck_time; + public const float α = fine_structure; + public const float γ = euler_mascheroni_gamma; + public const float δ = delta_feigenbaum; + public const float ε0 = vacuum_permittivity; + public const float λ = conway; + public const float μ0 = vacuum_permeability; + public const float φ = phi; } } \ No newline at end of file diff --git a/Runtime/Easing.cs b/Runtime/Easing.cs index 6a2e4ca..39a4f31 100644 --- a/Runtime/Easing.cs +++ b/Runtime/Easing.cs @@ -1,9 +1,9 @@ // Translation to C# from https://easings.net/ // by LTMX - https://github.com/LTMX -namespace UME +namespace Plugins.Mathematics_Extensions.Runtime { - public static partial class UnityMathematicsExtensions + public static partial class UME { public static float easeInSine(this float x) => 1 - cos((x * PI) / 2); public static float easeOutSine(this float x) => sin(x * PI / 2); diff --git a/Runtime/Easing.cs.meta b/Runtime/Easing.cs.meta new file mode 100644 index 0000000..a15d276 --- /dev/null +++ b/Runtime/Easing.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: c627a1a047a52dd4ebf7aa26dba6aa49 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Runtime/Exponents And Logarithms.cs b/Runtime/Exponents And Logarithms.cs index cd66ad9..7e23fc9 100644 --- a/Runtime/Exponents And Logarithms.cs +++ b/Runtime/Exponents And Logarithms.cs @@ -2,9 +2,9 @@ using UnityEngine; using static Unity.Mathematics.math; -namespace UME +namespace Plugins.Mathematics_Extensions.Runtime { - public static partial class UnityMathematicsExtensions + public static partial class UME { // Exponents ------------------------------------------ diff --git a/Runtime/Factorial Gamma.cs b/Runtime/Factorial Gamma.cs index ee8245a..86a6210 100644 --- a/Runtime/Factorial Gamma.cs +++ b/Runtime/Factorial Gamma.cs @@ -1,14 +1,8 @@ -using System.Numerics; -using Unity.Mathematics; -using Vector2 = UnityEngine.Vector2; -using Vector3 = UnityEngine.Vector3; -using Vector4 = UnityEngine.Vector4; - -namespace UME +namespace Plugins.Mathematics_Extensions.Runtime { // Not exactly working as expected - public static partial class UnityMathematicsExtensions + public static partial class UME { // See : https://rosettacode.org/wiki/Gamma_function#C.23 // Factorial Gamma Function - Lanczos Interpolated ------------------------------------------------- diff --git a/Runtime/FastFunctions.cs b/Runtime/FastFunctions.cs index 6d80022..363d6c8 100644 --- a/Runtime/FastFunctions.cs +++ b/Runtime/FastFunctions.cs @@ -3,9 +3,9 @@ using Unity.Mathematics; using UnityEngine; -namespace UME +namespace Plugins.Mathematics_Extensions.Runtime { - public static partial class UnityMathematicsExtensions + public static partial class UME { // https://gist.github.com/SaffronCR/b0802d102dd7f262118ac853cd5b4901#file-mathutil-cs-L24 diff --git a/Runtime/FastTrigonometry.cs b/Runtime/FastTrigonometry.cs index 7867368..f80396b 100644 --- a/Runtime/FastTrigonometry.cs +++ b/Runtime/FastTrigonometry.cs @@ -1,8 +1,9 @@  //Refactored and optimized by LTMX - from - https://web.archive.org/web/20180616003313/http://lab.polygonal.de/2007/07/18/fast-and-accurate-sinecosine-approximation/ -namespace UME + +namespace Plugins.Mathematics_Extensions.Runtime { - public static partial class UnityMathematicsExtensions + public static partial class UME { private const float t1 = 1.27323954f; private const float t2 = 0.405284735f; diff --git a/Runtime/Interpolation.cs b/Runtime/Interpolation.cs index ba0a942..8977716 100644 --- a/Runtime/Interpolation.cs +++ b/Runtime/Interpolation.cs @@ -1,9 +1,9 @@ using Unity.Mathematics; using UnityEngine; -namespace UME +namespace Plugins.Mathematics_Extensions.Runtime { - public static partial class UnityMathematicsExtensions + public static partial class UME { // https://github.com/FreyaHolmer/Mathfs/blob/master/Mathfs.cs diff --git a/Runtime/Smoothing Functions.cs.meta b/Runtime/Interpolation.cs.meta similarity index 54% rename from Runtime/Smoothing Functions.cs.meta rename to Runtime/Interpolation.cs.meta index 976e4a1..14a9cc2 100644 --- a/Runtime/Smoothing Functions.cs.meta +++ b/Runtime/Interpolation.cs.meta @@ -1,3 +1,3 @@ fileFormatVersion: 2 -guid: 33f45365204642488f01cb4b6c77cd9b +guid: 3249a614c64a683448223fe8ca32f253 timeCreated: 1591635607 \ No newline at end of file diff --git a/Runtime/Random.cs b/Runtime/Random.cs index 0f1e5ef..467ccf3 100644 --- a/Runtime/Random.cs +++ b/Runtime/Random.cs @@ -2,9 +2,9 @@ using Unity.Mathematics; using Random = Unity.Mathematics.Random; -namespace UME +namespace Plugins.Mathematics_Extensions.Runtime { - public partial class UnityMathematicsExtensions + public partial class UME { private static uint seed() => (uint) DateTime.Now.Millisecond; private static Random random => new Random(seed()); diff --git a/Runtime/Rounding.cs b/Runtime/Rounding.cs index e448c9f..e1f4c88 100644 --- a/Runtime/Rounding.cs +++ b/Runtime/Rounding.cs @@ -1,9 +1,9 @@ using Unity.Mathematics; using UnityEngine; -namespace UME +namespace Plugins.Mathematics_Extensions.Runtime { - public static partial class UnityMathematicsExtensions + public static partial class UME { // Rounding -------------------------------------------------- diff --git a/Runtime/Shorthands.cs b/Runtime/Shorthands.cs index 149dfaf..6557709 100644 --- a/Runtime/Shorthands.cs +++ b/Runtime/Shorthands.cs @@ -1,9 +1,8 @@ using Unity.Mathematics; -using UnityEngine; -namespace UME +namespace Plugins.Mathematics_Extensions.Runtime { - public partial class UnityMathematicsExtensions + public partial class UME { // Shorthands ----------------------------------------- diff --git a/Runtime/Smoothdamp.cs b/Runtime/Smoothdamp.cs index 837ea8f..44a30a7 100644 --- a/Runtime/Smoothdamp.cs +++ b/Runtime/Smoothdamp.cs @@ -1,9 +1,9 @@ using Unity.Mathematics; using UnityEngine; -namespace UME +namespace Plugins.Mathematics_Extensions.Runtime { - public static partial class UnityMathematicsExtensions + public static partial class UME { // https://github.com/FreyaHolmer/Mathfs/blob/master/Mathfs.cs diff --git a/Runtime/Smoothing.cs b/Runtime/Smoothing.cs new file mode 100644 index 0000000..dc1b53e --- /dev/null +++ b/Runtime/Smoothing.cs @@ -0,0 +1,23 @@ +namespace Plugins.Mathematics_Extensions.Runtime +{ + public static partial class UME + { + static float smoothstep(float x) => x.sqr() * (3 - 2 * x); + static float smoothstepD(float x) => 6 * x * (1 - x); // Derivative + + static float smoothstep5(float x) => x.cube() * (x * (6 * x - 15) + 10); + static float smoothstep5D(float x) => 30 * x.sqr() * (x * (x - 2) + 1); // Derivative + + + static float smoothstep7(float x) => x.quart() * (x * (x * (-20 * x + 70) - 84) + 35); + static float smootherstep7D(float x) => 140 * x.cube() * (x * (x * (-x + 3) - 3) + 1); // Derivative + + + static float smoothstep9(float x) => x.quint() * (x * (x * (x * (70 * x - 315) + 540) - 420) + 126); + static float smoothstep9D(float x) => 630 * x.quart() * (x * (x * (x * (x - 4) + 6) - 4) + 1); // Derivative + + static float smoothstep11(float x) => x.quint() * (x * (x * (x * (x * (-252 * x + 1386) - 3080) + 3465) - 1980) + 462); + + static float smoothstep11D(float x) => 2772 * x.quint() * (x * (x * (x * (x * (-x + 5) - 10) + 10) - 5) + 1); // Derivative + } +} \ No newline at end of file diff --git a/Runtime/Smoothing.cs.meta b/Runtime/Smoothing.cs.meta new file mode 100644 index 0000000..c21257e --- /dev/null +++ b/Runtime/Smoothing.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: d6fdbf0bf1b28ba468fe462dd5bbca8f +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Runtime/Trigonometry.cs b/Runtime/Trigonometry.cs index 270227c..d12fcb5 100644 --- a/Runtime/Trigonometry.cs +++ b/Runtime/Trigonometry.cs @@ -1,9 +1,9 @@ using Unity.Mathematics; using UnityEngine; -namespace UME +namespace Plugins.Mathematics_Extensions.Runtime { - public static partial class UnityMathematicsExtensions + public static partial class UME { // Trigonometry ---------------------------------------------------------------------------------- diff --git a/Runtime/TypeConversion.cs b/Runtime/TypeConversion.cs index 1fd6438..da3ed12 100644 --- a/Runtime/TypeConversion.cs +++ b/Runtime/TypeConversion.cs @@ -1,18 +1,16 @@ using System; -using System.Collections; using System.Collections.Generic; using System.Linq; using System.Numerics; -using System.Runtime.CompilerServices; using Unity.Mathematics; using UnityEngine; using Vector2 = UnityEngine.Vector2; using Vector3 = UnityEngine.Vector3; using Vector4 = UnityEngine.Vector4; -namespace UME +namespace Plugins.Mathematics_Extensions.Runtime { - public static partial class UnityMathematicsExtensions + public static partial class UME { // Type Conversion ----------------------------------------------------------------------------- diff --git a/Runtime/UnityMathematicsExtensions.cs b/Runtime/UME.cs similarity index 96% rename from Runtime/UnityMathematicsExtensions.cs rename to Runtime/UME.cs index 369b355..20daf93 100644 --- a/Runtime/UnityMathematicsExtensions.cs +++ b/Runtime/UME.cs @@ -7,12 +7,12 @@ //https://github.com/LTMX/Unity-Mathematics-Extensions -using UnityEngine; using Unity.Mathematics; +using UnityEngine; -namespace UME +namespace Plugins.Mathematics_Extensions.Runtime { - public static partial class UnityMathematicsExtensions + public static partial class UME { //Specific Functions ------------------------------------------------------------------------------------------ diff --git a/Runtime/UnityMathematicsExtensions.cs.meta b/Runtime/UME.cs.meta similarity index 100% rename from Runtime/UnityMathematicsExtensions.cs.meta rename to Runtime/UME.cs.meta diff --git a/Runtime/VectorFunctions.cs b/Runtime/VectorFunctions.cs index fdf88a2..1b60545 100644 --- a/Runtime/VectorFunctions.cs +++ b/Runtime/VectorFunctions.cs @@ -1,9 +1,9 @@ using Unity.Mathematics; using UnityEngine; -namespace UME +namespace Plugins.Mathematics_Extensions.Runtime { - public static partial class UnityMathematicsExtensions + public static partial class UME { // Vector Specific Functions ------------------------------------------------------ diff --git a/Runtime/VectorUpdaters.cs b/Runtime/VectorUpdaters.cs index 4ab958c..dc19b04 100644 --- a/Runtime/VectorUpdaters.cs +++ b/Runtime/VectorUpdaters.cs @@ -1,9 +1,9 @@ using Unity.Mathematics; using UnityEngine; -namespace UME +namespace Plugins.Mathematics_Extensions.Runtime { - public partial class UnityMathematicsExtensions + public partial class UME { /// Calculate a position between the points specified by current and target, moving no farther than the distance specified by maxDistanceDelta public static float4 movetowards(this float4 current, float4 target, float maxDistanceDelta)