Skip to content

Commit

Permalink
Adjusted Namespaces & Added Mathematical Constants
Browse files Browse the repository at this point in the history
  • Loading branch information
ltmx committed Apr 15, 2021
1 parent 97f930f commit a9ce347
Show file tree
Hide file tree
Showing 24 changed files with 183 additions and 70 deletions.
36 changes: 19 additions & 17 deletions Runtime/Angle.cs
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -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);
Expand All @@ -37,34 +39,34 @@ 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
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));
Expand Down
7 changes: 3 additions & 4 deletions Runtime/Arithemetics.cs
Original file line number Diff line number Diff line change
@@ -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);
Expand Down
4 changes: 2 additions & 2 deletions Runtime/Comparison.cs
Original file line number Diff line number Diff line change
@@ -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 --------------------------------------------------------------

Expand Down
87 changes: 81 additions & 6 deletions Runtime/Constants.cs
Original file line number Diff line number Diff line change
@@ -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;
}
}
4 changes: 2 additions & 2 deletions Runtime/Easing.cs
Original file line number Diff line number Diff line change
@@ -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);
Expand Down
11 changes: 11 additions & 0 deletions Runtime/Easing.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Runtime/Exponents And Logarithms.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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 ------------------------------------------

Expand Down
10 changes: 2 additions & 8 deletions Runtime/Factorial Gamma.cs
Original file line number Diff line number Diff line change
@@ -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 -------------------------------------------------
Expand Down
4 changes: 2 additions & 2 deletions Runtime/FastFunctions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
5 changes: 3 additions & 2 deletions Runtime/FastTrigonometry.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
4 changes: 2 additions & 2 deletions Runtime/Interpolation.cs
Original file line number Diff line number Diff line change
@@ -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

Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Runtime/Random.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
4 changes: 2 additions & 2 deletions Runtime/Rounding.cs
Original file line number Diff line number Diff line change
@@ -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 --------------------------------------------------

Expand Down
5 changes: 2 additions & 3 deletions Runtime/Shorthands.cs
Original file line number Diff line number Diff line change
@@ -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 -----------------------------------------

Expand Down
4 changes: 2 additions & 2 deletions Runtime/Smoothdamp.cs
Original file line number Diff line number Diff line change
@@ -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

Expand Down
23 changes: 23 additions & 0 deletions Runtime/Smoothing.cs
Original file line number Diff line number Diff line change
@@ -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
}
}
11 changes: 11 additions & 0 deletions Runtime/Smoothing.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Runtime/Trigonometry.cs
Original file line number Diff line number Diff line change
@@ -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 ----------------------------------------------------------------------------------

Expand Down
Loading

0 comments on commit a9ce347

Please sign in to comment.