From 5d680542a8db8ed66f976ea8cff1b2819033b387 Mon Sep 17 00:00:00 2001 From: Nitload Date: Sat, 29 Aug 2026 23:10:44 +0800 Subject: [PATCH] New opengl api binding and new graphic purpose vector design Signed-off-by: Nitload --- .../Linear => .discard}/CoordinationNaming.md | 0 .../MatrixCompute.Factorize.cs.old | 0 .../MatrixCompute.Single.cs.old | 0 .../MatrixCompute.double.cs.old | 0 .../MatrixParallelHandle.cs.old | 0 .../MatrixParallelWrap.cs.old | 0 .../Numeric/Graphics/Vec2.cs | 152 ++----------- .../Numeric/Graphics/Vec2_T.cs | 129 +++++++++++ .../Numeric/Graphics/Vec3.cs | 201 +++++++++++------ .../Numeric/Graphics/Vec3_T.cs | 94 +++----- .../Numeric/Graphics/Vec4.cs | 174 ++------------- .../Numeric/Graphics/Vec4_T.cs | 203 ++++++++++++++++++ .../Numeric/Graphics/Vector.GetAngle.cs | 33 --- .../Numeric/Graphics/Vector.GetLength.cs | 35 ++- .../Numeric/Graphics/Vector.Normalize.cs | 75 +++---- .../Text/RegexAstNode.cs | 42 +--- 16 files changed, 589 insertions(+), 549 deletions(-) rename src/StgSharp.Mathematics/{Numeric/Linear => .discard}/CoordinationNaming.md (100%) rename src/StgSharp.Mathematics/{Numeric/Linear => .discard}/MatrixCompute.Factorize.cs.old (100%) rename src/StgSharp.Mathematics/{Numeric/Linear => .discard}/MatrixCompute.Single.cs.old (100%) rename src/StgSharp.Mathematics/{Numeric/Linear => .discard}/MatrixCompute.double.cs.old (100%) rename src/StgSharp.Mathematics/{Numeric => .discard}/MatrixParallelHandle.cs.old (100%) rename src/StgSharp.Mathematics/{Numeric => .discard}/MatrixParallelWrap.cs.old (100%) create mode 100644 src/StgSharp.Mathematics/Numeric/Graphics/Vec2_T.cs create mode 100644 src/StgSharp.Mathematics/Numeric/Graphics/Vec4_T.cs delete mode 100644 src/StgSharp.Mathematics/Numeric/Graphics/Vector.GetAngle.cs diff --git a/src/StgSharp.Mathematics/Numeric/Linear/CoordinationNaming.md b/src/StgSharp.Mathematics/.discard/CoordinationNaming.md similarity index 100% rename from src/StgSharp.Mathematics/Numeric/Linear/CoordinationNaming.md rename to src/StgSharp.Mathematics/.discard/CoordinationNaming.md diff --git a/src/StgSharp.Mathematics/Numeric/Linear/MatrixCompute.Factorize.cs.old b/src/StgSharp.Mathematics/.discard/MatrixCompute.Factorize.cs.old similarity index 100% rename from src/StgSharp.Mathematics/Numeric/Linear/MatrixCompute.Factorize.cs.old rename to src/StgSharp.Mathematics/.discard/MatrixCompute.Factorize.cs.old diff --git a/src/StgSharp.Mathematics/Numeric/Linear/MatrixCompute.Single.cs.old b/src/StgSharp.Mathematics/.discard/MatrixCompute.Single.cs.old similarity index 100% rename from src/StgSharp.Mathematics/Numeric/Linear/MatrixCompute.Single.cs.old rename to src/StgSharp.Mathematics/.discard/MatrixCompute.Single.cs.old diff --git a/src/StgSharp.Mathematics/Numeric/Linear/MatrixCompute.double.cs.old b/src/StgSharp.Mathematics/.discard/MatrixCompute.double.cs.old similarity index 100% rename from src/StgSharp.Mathematics/Numeric/Linear/MatrixCompute.double.cs.old rename to src/StgSharp.Mathematics/.discard/MatrixCompute.double.cs.old diff --git a/src/StgSharp.Mathematics/Numeric/MatrixParallelHandle.cs.old b/src/StgSharp.Mathematics/.discard/MatrixParallelHandle.cs.old similarity index 100% rename from src/StgSharp.Mathematics/Numeric/MatrixParallelHandle.cs.old rename to src/StgSharp.Mathematics/.discard/MatrixParallelHandle.cs.old diff --git a/src/StgSharp.Mathematics/Numeric/MatrixParallelWrap.cs.old b/src/StgSharp.Mathematics/.discard/MatrixParallelWrap.cs.old similarity index 100% rename from src/StgSharp.Mathematics/Numeric/MatrixParallelWrap.cs.old rename to src/StgSharp.Mathematics/.discard/MatrixParallelWrap.cs.old diff --git a/src/StgSharp.Mathematics/Numeric/Graphics/Vec2.cs b/src/StgSharp.Mathematics/Numeric/Graphics/Vec2.cs index a6854e7b..ac3f766c 100644 --- a/src/StgSharp.Mathematics/Numeric/Graphics/Vec2.cs +++ b/src/StgSharp.Mathematics/Numeric/Graphics/Vec2.cs @@ -5,14 +5,8 @@ // SPDX-License-Identifier: MIT // ----------------------------------------------------------------------------- -using StgSharp.HighPerformance.ProcessorAbstraction; -using StgSharp.Mathematics.Numeric; -using StgSharp.Mathematics.Numeric.Graphics; -using System.Collections.Generic; -using System.Net.Http.Headers; using System.Numerics; using System.Runtime.CompilerServices; -using System.Runtime.InteropServices; using System.Runtime.Intrinsics; namespace StgSharp.Mathematics.Numeric.Graphics @@ -21,9 +15,7 @@ namespace StgSharp.Mathematics.Numeric.Graphics { [MethodImpl(MethodImplOptions.AggressiveInlining)] - internal static Vector128 AsVector128Safe( - this Vec2 source - ) where T : unmanaged, INumber + internal static Vector128 AsVector128Safe(this Vec2 source) where T: unmanaged, INumber { Vector128 result = Vector128.Zero; Unsafe.WriteUnaligned(ref Unsafe.As, byte>(ref result), source); @@ -31,9 +23,7 @@ namespace StgSharp.Mathematics.Numeric.Graphics } [MethodImpl(MethodImplOptions.AggressiveInlining)] - internal static Vector128 AsVector128Unsafe( - this Vec2 source - ) where T : unmanaged, INumber + internal static Vector128 AsVector128Unsafe(this Vec2 source) where T: unmanaged, INumber { Unsafe.SkipInit(out Vector128 result); Unsafe.WriteUnaligned(ref Unsafe.As, byte>(ref result), source); @@ -41,155 +31,39 @@ namespace StgSharp.Mathematics.Numeric.Graphics } [MethodImpl(MethodImplOptions.AggressiveInlining)] - internal static Vec2 FromVector128Safe( - Vector128 source - ) where T : unmanaged, INumber + internal static Vec2 FromVector128Safe(Vector128 source) where T: unmanaged, INumber { return Unsafe.As, Vec2>(ref source); } [MethodImpl(MethodImplOptions.AggressiveInlining)] - internal static Vec2 FromVector128Unsafe( - Vector128 source - ) where T : unmanaged, INumber + internal static Vec2 FromVector128Unsafe(Vector128 source) where T: unmanaged, INumber { return Unsafe.As, Vec2>(ref source); } - } - - /// - /// A two dimension vector defined by two elements. Vec2 in World are default used as colum - /// vector. - /// - /// - /// - /// - [StructLayout(LayoutKind.Sequential, Pack = 16)] - public struct Vec2 : IUnmanagedVector> where T : unmanaged, INumber - { - - public T X; - public T Y; - - public Vec2( - T x, - T y - ) + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static Vec2 ConvertToSingle(Vec2 source) { - X = x; - Y = y; - } - - public static Vec2 Unit => new(T.One, T.One); - - public static Vec2 Zero => new(T.Zero, T.Zero); - - public static Vec2 One => new(T.One, T.One); - - public Vec2 XY - { - readonly get => this; - set => this = value; + return FromVector128Unsafe(Vector128.ConvertToSingle(source.AsVector128Safe())); } [MethodImpl(MethodImplOptions.AggressiveInlining)] - public T Cross( - Vec2 right - ) + public static Vec2 ConvertToSingle(Vec2 source) { - return X * right.Y - Y * right.X; + return FromVector128Unsafe(Vector128.ConvertToSingle(source.AsVector128Safe())); } [MethodImpl(MethodImplOptions.AggressiveInlining)] - public readonly T Dot( - Vec2 vec - ) + public static Vec2 ConvertToDouble(Vec2 source) { - return X * vec.X + Y * vec.Y; + return FromVector128Unsafe(Vector128.ConvertToDouble(source.AsVector128Safe())); } [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static Vec2 operator -( - Vec2 left, - Vec2 right - ) + public static Vec2 ConvertToDouble(Vec2 source) { - Vector128 leftVec = left.AsVector128Unsafe(); - Vector128 rightVec = right.AsVector128Unsafe(); - return Vec2.FromVector128Unsafe(leftVec + rightVec); - } - - public static bool operator !=( - Vec2 left, - Vec2 right - ) - { - return !(left == right); - } - - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static Vec2 operator *( - Vec2 vec, - T value - ) - { - Vector128 leftVec = vec.AsVector128Unsafe(); - return Vec2.FromVector128Unsafe(leftVec * value); - } - - public static Vec2 operator *( - T value, - Vec2 vec - ) - { - Vector128 leftVec = vec.AsVector128Unsafe(); - return Vec2.FromVector128Unsafe(leftVec * value); - } - - public static Vec2 operator /( - Vec2 vec, - T value - ) - { - Vector128 leftVec = vec.AsVector128Unsafe(); - return Vec2.FromVector128Unsafe(leftVec / value); - } - - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static Vec2 operator +( - Vec2 left, - Vec2 right - ) - { - Vector128 leftVec = left.AsVector128Unsafe(); - Vector128 rightVec = right.AsVector128Unsafe(); - return Vec2.FromVector128Unsafe(leftVec + rightVec); - } - - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static bool operator ==( - Vec2 left, - Vec2 right - ) - { - if (Unsafe.SizeOf() == 4) { - return Unsafe.As, ulong>(ref left) == Unsafe.As, ulong>(ref right); - } - if (Unsafe.SizeOf() == 8) { - return Unsafe.As, Vector128>(ref left) == Unsafe.As, Vector128>(ref right); - } -#pragma warning disable CA1065 - throw new NotSupportedException($"Element type {typeof(T).Name} is not supported."); -#pragma warning restore CA1065 - } - - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static implicit operator Vec2( - (T, T) tuple - ) - { - return new Vec2(tuple.Item1, tuple.Item2); + return FromVector128Unsafe(Vector128.ConvertToDouble(source.AsVector128Safe())); } } diff --git a/src/StgSharp.Mathematics/Numeric/Graphics/Vec2_T.cs b/src/StgSharp.Mathematics/Numeric/Graphics/Vec2_T.cs new file mode 100644 index 00000000..e5c99a06 --- /dev/null +++ b/src/StgSharp.Mathematics/Numeric/Graphics/Vec2_T.cs @@ -0,0 +1,129 @@ +// ----------------------------------------------------------------------------- +// file="Vec2_T" +// Project: StgSharp +// Copyright (c) Nitload. +// SPDX-License-Identifier: MIT +// ----------------------------------------------------------------------------- + +using StgSharp.Mathematics.Numeric; +using System.Numerics; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; +using System.Runtime.Intrinsics; + +namespace StgSharp.Mathematics.Numeric.Graphics +{ + /// + /// A two dimension vector defined by two elements. Vec2 in World are default used as colum + /// vector. + /// + /// + /// + /// + [StructLayout(LayoutKind.Sequential, Pack = 16)] + public struct Vec2(T x, T y) : IUnmanagedVector> where T: unmanaged, INumber + { + + public T X = x; + public T Y = y; + + public static Vec2 Unit => new(T.One, T.One); + + public static Vec2 Zero => new(T.Zero, T.Zero); + + public static Vec2 One => new(T.One, T.One); + + public Vec2 XY + { + readonly get => this; + set => this = value; + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public T Cross(Vec2 right) + { + return (X * right.Y) - (Y * right.X); + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public readonly T Dot(Vec2 vec) + { + Vector128 leftVec = this.AsVector128Unsafe(); + Vector128 rightVec = vec.AsVector128Unsafe(); + Vector128 result_v128 = leftVec * rightVec; + Vec2 result = Unsafe.As, Vec2>(ref result_v128); + return result.X + result.Y; + } + + public override readonly bool Equals(object? obj) + { + return (obj is Vec2 other) && (other == this); + } + + public override readonly int GetHashCode() + { + return HashCode.Combine(X, Y); + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static Vec2 operator -(Vec2 left, Vec2 right) + { + Vector128 leftVec = left.AsVector128Unsafe(); + Vector128 rightVec = right.AsVector128Unsafe(); + return Vec2.FromVector128Unsafe(leftVec - rightVec); + } + + public static bool operator !=(Vec2 left, Vec2 right) + { + return !(left == right); + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static Vec2 operator *(Vec2 vec, T value) + { + Vector128 leftVec = vec.AsVector128Unsafe(); + return Vec2.FromVector128Unsafe(leftVec * value); + } + + public static Vec2 operator *(T value, Vec2 vec) + { + Vector128 leftVec = vec.AsVector128Unsafe(); + return Vec2.FromVector128Unsafe(leftVec * value); + } + + public static Vec2 operator /(Vec2 vec, T value) + { + Vector128 leftVec = vec.AsVector128Unsafe(); + return Vec2.FromVector128Unsafe(leftVec / value); + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static Vec2 operator +(Vec2 left, Vec2 right) + { + Vector128 leftVec = left.AsVector128Unsafe(); + Vector128 rightVec = right.AsVector128Unsafe(); + return Vec2.FromVector128Unsafe(leftVec + rightVec); + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static bool operator ==(Vec2 left, Vec2 right) + { + if (Unsafe.SizeOf() == 4) { + return Unsafe.As, ulong>(ref left) == Unsafe.As, ulong>(ref right); + } + if (Unsafe.SizeOf() == 8) { + return Unsafe.As, Vector128>(ref left) == Unsafe.As, Vector128>(ref right); + } +#pragma warning disable CA1065 + throw new NotSupportedException($"Element type {typeof(T).Name} is not supported."); +#pragma warning restore CA1065 + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static implicit operator Vec2((T, T) tuple) + { + return new Vec2(tuple.Item1, tuple.Item2); + } + + } +} diff --git a/src/StgSharp.Mathematics/Numeric/Graphics/Vec3.cs b/src/StgSharp.Mathematics/Numeric/Graphics/Vec3.cs index ecec6880..0fe65e06 100644 --- a/src/StgSharp.Mathematics/Numeric/Graphics/Vec3.cs +++ b/src/StgSharp.Mathematics/Numeric/Graphics/Vec3.cs @@ -15,50 +15,130 @@ namespace StgSharp.Mathematics.Numeric.Graphics public static class Vec3 { - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static Vec3 Cross(Vec3 left, Vec3 right) - where T : unmanaged, INumber + public static bool IsParallel(Vec3 left, Vec3 right) where T: unmanaged,INumber { - if (typeof(T) == typeof(float)) { + return Cross(left, right).Equals(T.Zero); + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + internal static Vector128 AsVector128Safe(this Vec3 source) where T: unmanaged, INumber + { + Vector128 result = Vector128.Zero; + Unsafe.WriteUnaligned(ref Unsafe.As, byte>(ref result), source); + return result; + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + internal static Vector128 AsVector128Unsafe(this Vec3 source) where T: unmanaged, INumber + { + Unsafe.SkipInit(out Vector128 result); + Unsafe.WriteUnaligned(ref Unsafe.As, byte>(ref result), source); + return result; + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + internal static Vector256 AsVector256Safe(this Vec3 source) where T: unmanaged, INumber + { + Vector256 result = Vector256.Zero; + Unsafe.WriteUnaligned(ref Unsafe.As, byte>(ref result), source); + return result; + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + internal static Vector256 AsVector256Unsafe(this Vec3 source) where T: unmanaged, INumber + { + Unsafe.SkipInit(out Vector256 result); + Unsafe.WriteUnaligned(ref Unsafe.As, byte>(ref result), source); + return result; + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + internal static Vec3 FromVector128Unsafe(Vector128 source) where T: unmanaged, INumber + { + return Unsafe.As, Vec3>(ref source); + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + internal static Vec3 FromVector256Unsafe(Vector256 source) where T: unmanaged, INumber + { + return Unsafe.As, Vec3>(ref source); + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static Vec3 ConvertToSingle(Vec3 source) + { + return FromVector128Unsafe(Vector128.ConvertToSingle(source.AsVector128Safe())); + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static Vec3 ConvertToSingle(Vec3 source) + { + return FromVector128Unsafe(Vector128.ConvertToSingle(source.AsVector128Safe())); + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static Vec3 ConvertToDouble(Vec3 source) + { + return FromVector256Unsafe(Vector256.ConvertToDouble(source.AsVector256Safe())); + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static Vec3 ConvertToDouble(Vec3 source) + { + return FromVector256Unsafe(Vector256.ConvertToDouble(source.AsVector256Safe())); + } + + #region cross + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static Vec3 Cross(Vec3 left, Vec3 right) where T: unmanaged, INumber + { + if (typeof(T) == typeof(float)) + { Vec3 result = CrossSingle( Unsafe.As, Vec3>(ref left), Unsafe.As, Vec3>(ref right)); return Unsafe.As, Vec3>(ref result); } - if (typeof(T) == typeof(double)) { + if (typeof(T) == typeof(double)) + { Vec3 result = CrossDouble( Unsafe.As, Vec3>(ref left), Unsafe.As, Vec3>(ref right)); return Unsafe.As, Vec3>(ref result); } - if (typeof(T) == typeof(int)) { + if (typeof(T) == typeof(int)) + { Vec3 result = CrossInt32( Unsafe.As, Vec3>(ref left), Unsafe.As, Vec3>(ref right)); return Unsafe.As, Vec3>(ref result); } - if (typeof(T) == typeof(uint)) { + if (typeof(T) == typeof(uint)) + { Vec3 result = CrossUInt32( Unsafe.As, Vec3>(ref left), Unsafe.As, Vec3>(ref right)); return Unsafe.As, Vec3>(ref result); } + if (typeof(T) == typeof(ulong)) + { + Vec3 result = CrossUInt64( + Unsafe.As, Vec3>(ref left), + Unsafe.As, Vec3>(ref right)); + return Unsafe.As, Vec3>(ref result); + } + if (typeof(T) == typeof(long)) + { + Vec3 result = CrossInt64( + Unsafe.As, Vec3>(ref left), + Unsafe.As, Vec3>(ref right)); + return Unsafe.As, Vec3>(ref result); + } throw new NotSupportedException($"Cross product is not supported for {typeof(T)}."); } - [MethodImpl(MethodImplOptions.AggressiveInlining)] - private static Vec3 CrossSingle(Vec3 left, Vec3 right) - { - Vector128 l = left.AsVector128Safe(); - Vector128 r = right.AsVector128Safe(); - Vector128 yzx = Vector128.Create(1, 2, 0, 3); - Vector128 zxy = Vector128.Create(2, 0, 1, 3); - Vector128 result = Vector128.Shuffle(l, yzx) * Vector128.Shuffle(r, zxy) - - Vector128.Shuffle(l, zxy) * Vector128.Shuffle(r, yzx); - return new(result[0], result[1], result[2]); - } - [MethodImpl(MethodImplOptions.AggressiveInlining)] private static Vec3 CrossDouble(Vec3 left, Vec3 right) { @@ -66,9 +146,8 @@ namespace StgSharp.Mathematics.Numeric.Graphics Vector256 r = right.AsVector256Safe(); Vector256 yzx = Vector256.Create(1L, 2L, 0L, 3L); Vector256 zxy = Vector256.Create(2L, 0L, 1L, 3L); - Vector256 result = Vector256.Shuffle(l, yzx) * Vector256.Shuffle(r, zxy) - - Vector256.Shuffle(l, zxy) * Vector256.Shuffle(r, yzx); - return new(result[0], result[1], result[2]); + Vector256 result = (Vector256.Shuffle(l, yzx) * Vector256.Shuffle(r, zxy)) - (Vector256.Shuffle(l, zxy) * Vector256.Shuffle(r, yzx)); + return FromVector256Unsafe(result); } [MethodImpl(MethodImplOptions.AggressiveInlining)] @@ -78,9 +157,19 @@ namespace StgSharp.Mathematics.Numeric.Graphics Vector128 r = right.AsVector128Safe(); Vector128 yzx = Vector128.Create(1, 2, 0, 3); Vector128 zxy = Vector128.Create(2, 0, 1, 3); - Vector128 result = Vector128.Shuffle(l, yzx) * Vector128.Shuffle(r, zxy) - - Vector128.Shuffle(l, zxy) * Vector128.Shuffle(r, yzx); - return new(result[0], result[1], result[2]); + Vector128 result = (Vector128.Shuffle(l, yzx) * Vector128.Shuffle(r, zxy)) - (Vector128.Shuffle(l, zxy) * Vector128.Shuffle(r, yzx)); + return FromVector128Unsafe(result); + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private static Vec3 CrossSingle(Vec3 left, Vec3 right) + { + Vector128 l = left.AsVector128Safe(); + Vector128 r = right.AsVector128Safe(); + Vector128 yzx = Vector128.Create(1, 2, 0, 3); + Vector128 zxy = Vector128.Create(2, 0, 1, 3); + Vector128 result = (Vector128.Shuffle(l, yzx) * Vector128.Shuffle(r, zxy)) - (Vector128.Shuffle(l, zxy) * Vector128.Shuffle(r, yzx)); + return FromVector128Unsafe(result); } [MethodImpl(MethodImplOptions.AggressiveInlining)] @@ -90,60 +179,32 @@ namespace StgSharp.Mathematics.Numeric.Graphics Vector128 r = right.AsVector128Safe(); Vector128 yzx = Vector128.Create(1U, 2U, 0U, 3U); Vector128 zxy = Vector128.Create(2U, 0U, 1U, 3U); - Vector128 result = Vector128.Shuffle(l, yzx) * Vector128.Shuffle(r, zxy) - - Vector128.Shuffle(l, zxy) * Vector128.Shuffle(r, yzx); - return new(result[0], result[1], result[2]); + Vector128 result = (Vector128.Shuffle(l, yzx) * Vector128.Shuffle(r, zxy)) - (Vector128.Shuffle(l, zxy) * Vector128.Shuffle(r, yzx)); + return FromVector128Unsafe(result); } [MethodImpl(MethodImplOptions.AggressiveInlining)] - internal static Vector128 AsVector128Safe(this Vec3 source) - where T : unmanaged, INumber + private static Vec3 CrossUInt64(Vec3 left, Vec3 right) { - Vector128 result = Vector128.Zero; - Unsafe.WriteUnaligned(ref Unsafe.As, byte>(ref result), source); - return result; + Vector256 l = left.AsVector256Safe(); + Vector256 r = right.AsVector256Safe(); + Vector256 yzx = Vector256.Create(1UL, 2UL, 0UL, 3UL); + Vector256 zxy = Vector256.Create(2UL, 0UL, 1UL, 3UL); + Vector256 result = (Vector256.Shuffle(l, yzx) * Vector256.Shuffle(r, zxy)) - (Vector256.Shuffle(l, zxy) * Vector256.Shuffle(r, yzx)); + return FromVector256Unsafe(result); } [MethodImpl(MethodImplOptions.AggressiveInlining)] - internal static Vector256 AsVector256Safe(this Vec3 source) - where T : unmanaged, INumber + private static Vec3 CrossInt64(Vec3 left, Vec3 right) { - Vector256 result = Vector256.Zero; - Unsafe.WriteUnaligned(ref Unsafe.As, byte>(ref result), source); - return result; - } - - [MethodImpl(MethodImplOptions.AggressiveInlining)] - internal static Vector128 AsVector128Unsafe(this Vec3 source) - where T : unmanaged, INumber - { - Unsafe.SkipInit(out Vector128 result); - Unsafe.WriteUnaligned(ref Unsafe.As, byte>(ref result), source); - return result; - } - - [MethodImpl(MethodImplOptions.AggressiveInlining)] - internal static Vector256 AsVector256Unsafe(this Vec3 source) - where T : unmanaged, INumber - { - Unsafe.SkipInit(out Vector256 result); - Unsafe.WriteUnaligned(ref Unsafe.As, byte>(ref result), source); - return result; - } - - [MethodImpl(MethodImplOptions.AggressiveInlining)] - internal static Vec3 FromVector128Unsafe(Vector128 source) - where T : unmanaged, INumber - { - return Unsafe.As, Vec3>(ref source); - } - - [MethodImpl(MethodImplOptions.AggressiveInlining)] - internal static Vec3 FromVector256Unsafe(Vector256 source) - where T : unmanaged, INumber - { - return Unsafe.As, Vec3>(ref source); + Vector256 l = left.AsVector256Safe(); + Vector256 r = right.AsVector256Safe(); + Vector256 yzx = Vector256.Create(1U, 2U, 0U, 3U); + Vector256 zxy = Vector256.Create(2U, 0U, 1U, 3U); + Vector256 result = (Vector256.Shuffle(l, yzx) * Vector256.Shuffle(r, zxy)) - (Vector256.Shuffle(l, zxy) * Vector256.Shuffle(r, yzx)); + return FromVector256Unsafe(result); } + #endregion } } diff --git a/src/StgSharp.Mathematics/Numeric/Graphics/Vec3_T.cs b/src/StgSharp.Mathematics/Numeric/Graphics/Vec3_T.cs index 658de023..7e691da6 100644 --- a/src/StgSharp.Mathematics/Numeric/Graphics/Vec3_T.cs +++ b/src/StgSharp.Mathematics/Numeric/Graphics/Vec3_T.cs @@ -21,28 +21,21 @@ namespace StgSharp.Mathematics.Numeric.Graphics /// [StructLayout(LayoutKind.Sequential, Pack = 16)] public struct Vec3 : IUnmanagedVector>, IEquatable>, IEnumerable - where T : unmanaged, INumber + where T: unmanaged, INumber { public T X; public T Y; public T Z; - public Vec3( - Vec2 xy, - T z - ) + public Vec3(Vec2 xy, T z) { X = xy.X; Y = xy.Y; Z = z; } - public Vec3( - T x, - T y, - T z - ) + public Vec3(T x, T y, T z) { X = x; Y = y; @@ -80,13 +73,11 @@ namespace StgSharp.Mathematics.Numeric.Graphics public static Vec3 UnitZ => new(T.Zero, T.Zero, T.One); [MethodImpl(MethodImplOptions.AggressiveInlining)] - public readonly T Dot( - Vec3 right - ) + public readonly T Dot(Vec3 right) { - if (Unsafe.SizeOf() == 4) + if (Unsafe.SizeOf() == 8) { - Vec4 v = Vec4.FromVector128Unsafe(this.AsVector128Unsafe() * right.AsVector128Unsafe()); + Vec4 v = Vec4.FromVector256Unsafe(this.AsVector256Unsafe() * right.AsVector256Unsafe()); return v.X + v.Y + v.Z; } if (Unsafe.SizeOf() == 4) @@ -98,23 +89,23 @@ namespace StgSharp.Mathematics.Numeric.Graphics } [MethodImpl(MethodImplOptions.AggressiveInlining)] - public readonly bool Equals( - Vec3 other - ) + public readonly bool Equals(Vec3 other) { - return X == other.X && Y == other.Y && Z == other.Z; + if (Unsafe.SizeOf() == 4) { + return this.AsVector128Safe() == other.AsVector128Safe(); + } + if (Unsafe.SizeOf() == 8) { + return this.AsVector256Safe() == other.AsVector256Safe(); + } + throw GraphicVector.ThrowUnsupportedTypeException(); } - public override readonly bool Equals( - object? obj - ) + public override readonly bool Equals(object? obj) { return obj is Vec3 other && Equals(other); } - public static Vec3 FromSpan( - ReadOnlySpan span - ) + public static Vec3 FromSpan(ReadOnlySpan span) { if (span.Length < 3) { throw new ArgumentException("Span length must be at least 3.", nameof(span)); @@ -141,18 +132,13 @@ namespace StgSharp.Mathematics.Numeric.Graphics return T.Max(X, T.Max(Y, Z)); } - public static bool IsParallel( - Vec3 left, - Vec3 right - ) + public static bool IsParallel(Vec3 left, Vec3 right) { return Vec3.Cross(left, right) == Zero; } [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static Vec3 operator -( - Vec3 value - ) + public static Vec3 operator -(Vec3 value) { if (Unsafe.SizeOf() == 4) { return Vec3.FromVector128Unsafe(Vector128.Zero - value.AsVector128Unsafe()); @@ -164,10 +150,7 @@ namespace StgSharp.Mathematics.Numeric.Graphics } [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static Vec3 operator -( - Vec3 left, - Vec3 right - ) + public static Vec3 operator -(Vec3 left, Vec3 right) { if (Unsafe.SizeOf() == 4) { @@ -185,19 +168,13 @@ namespace StgSharp.Mathematics.Numeric.Graphics } [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static bool operator !=( - Vec3 left, - Vec3 right - ) + public static bool operator !=(Vec3 left, Vec3 right) { return !left.Equals(right); } [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static Vec3 operator *( - Vec3 vector, - T scalar - ) + public static Vec3 operator *(Vec3 vector, T scalar) { if (Unsafe.SizeOf() == 4) { @@ -215,28 +192,19 @@ namespace StgSharp.Mathematics.Numeric.Graphics } [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static Vec3 operator *( - T scalar, - Vec3 vector - ) + public static Vec3 operator *(T scalar, Vec3 vector) { return vector * scalar; } [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static T operator *( - Vec3 left, - Vec3 right - ) + public static T operator *(Vec3 left, Vec3 right) { return left.Dot(right); } [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static Vec3 operator /( - Vec3 vector, - T scalar - ) + public static Vec3 operator /(Vec3 vector, T scalar) { if (Unsafe.SizeOf() == 4) { @@ -254,10 +222,7 @@ namespace StgSharp.Mathematics.Numeric.Graphics } [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static Vec3 operator +( - Vec3 left, - Vec3 right - ) + public static Vec3 operator +(Vec3 left, Vec3 right) { if (Unsafe.SizeOf() == 4) { @@ -275,18 +240,13 @@ namespace StgSharp.Mathematics.Numeric.Graphics } [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static bool operator ==( - Vec3 left, - Vec3 right - ) + public static bool operator ==(Vec3 left, Vec3 right) { return left.Equals(right); } [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static implicit operator Vec3( - (T X, T Y, T Z) tuple - ) + public static implicit operator Vec3((T X, T Y, T Z) tuple) { return new(tuple.X, tuple.Y, tuple.Z); } diff --git a/src/StgSharp.Mathematics/Numeric/Graphics/Vec4.cs b/src/StgSharp.Mathematics/Numeric/Graphics/Vec4.cs index 19171af9..990815e0 100644 --- a/src/StgSharp.Mathematics/Numeric/Graphics/Vec4.cs +++ b/src/StgSharp.Mathematics/Numeric/Graphics/Vec4.cs @@ -5,16 +5,8 @@ // SPDX-License-Identifier: MIT // ----------------------------------------------------------------------------- -using StgSharp.HighPerformance.ProcessorAbstraction; -using StgSharp.Mathematics; -using StgSharp.Mathematics.Internal; -using StgSharp.Mathematics.Numeric; -using StgSharp.Mathematics.Numeric.Graphics; -using System; -using System.Data.Common; using System.Numerics; using System.Runtime.CompilerServices; -using System.Runtime.InteropServices; using System.Runtime.Intrinsics; namespace StgSharp.Mathematics.Numeric.Graphics @@ -23,9 +15,7 @@ namespace StgSharp.Mathematics.Numeric.Graphics { [MethodImpl(MethodImplOptions.AggressiveInlining)] - internal static Vector128 AsVector128Unsafe( - this Vec4 source - ) where T : unmanaged, INumber + internal static Vector128 AsVector128Unsafe(this Vec4 source) where T: unmanaged, INumber { Unsafe.SkipInit(out Vector128 result); Unsafe.WriteUnaligned(ref Unsafe.As, byte>(ref result), source); @@ -33,9 +23,7 @@ namespace StgSharp.Mathematics.Numeric.Graphics } [MethodImpl(MethodImplOptions.AggressiveInlining)] - internal static Vector256 AsVector256Unsafe( - this Vec4 source - ) where T : unmanaged, INumber + internal static Vector256 AsVector256Unsafe(this Vec4 source) where T: unmanaged, INumber { Unsafe.SkipInit(out Vector256 result); Unsafe.WriteUnaligned(ref Unsafe.As, byte>(ref result), source); @@ -43,177 +31,39 @@ namespace StgSharp.Mathematics.Numeric.Graphics } [MethodImpl(MethodImplOptions.AggressiveInlining)] - internal static Vec4 FromVector128Unsafe( - Vector128 source - ) where T : unmanaged, INumber + internal static Vec4 FromVector128Unsafe(Vector128 source) where T: unmanaged, INumber { return Unsafe.As, Vec4>(ref source); } [MethodImpl(MethodImplOptions.AggressiveInlining)] - internal static Vec4 FromVector256Unsafe( - Vector256 source - ) where T : unmanaged, INumber + internal static Vec4 FromVector256Unsafe(Vector256 source) where T: unmanaged, INumber { return Unsafe.As, Vec4>(ref source); } - } - - /// - /// A three dimension vector defined by two elements. Vec3 in World are default used as colum - /// vector. - /// - [StructLayout(LayoutKind.Sequential, Pack = 16)] - public struct Vec4 : IUnmanagedVector> where T : unmanaged, INumber - { - - public T W; - - public T X; - public T Y; - public T Z; - - public Vec4( - Vec3 v3, - T w - ) - { - XYZ = v3; - W = w; - } - - public Vec4( - T x, - T y, - T z, - T w - ) - { - X = x; - Y = y; - Z = z; - W = w; - } - - public unsafe Vec2 XY - { - get => Vec2.FromVector128Unsafe(this.AsVector128Unsafe()); - set - { - X = value.X; - Y = value.Y; - } - } - - public unsafe Vec3 XYZ - { - get => Vec3.FromVector128Unsafe(this.AsVector128Unsafe()); - set - { - T w = W; - Unsafe.As>(ref X) = value; - W = w; - } - } - - public static Vec4 One => new(T.One, T.One, T.One, T.One); - - public static Vec4 Zero => new(T.Zero, T.Zero, T.Zero, T.Zero); - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static Vec4 Add( - Vec4 left, - Vec4 right - ) + public static Vec4 ConvertToSingle(Vec4 source) { - return left + right; - } - - public ReadOnlySpan AsSpan() - { - return MemoryMarshal.CreateReadOnlySpan(ref X, 4); - } - - public override bool Equals( - object obj - ) - { - return (obj is Vec4 v) && (v == this); - } - - public static Vec4 FromSpan( - ReadOnlySpan span - ) - { - if (span.Length < 4) { - throw new ArgumentException("Span length must be at least 4.", nameof(span)); - } - - Unsafe.SkipInit(out Vec4 result); - Span target = MemoryMarshal.CreateSpan(ref result.X, 4); - span[..4].CopyTo(target); - return result; + return FromVector128Unsafe(Vector128.ConvertToSingle(source.AsVector128Unsafe())); } [MethodImpl(MethodImplOptions.AggressiveInlining)] - public ReadOnlySpan.Enumerator GetEnumerator() + public static Vec4 ConvertToSingle(Vec4 source) { - return AsSpan().GetEnumerator(); + return FromVector128Unsafe(Vector128.ConvertToSingle(source.AsVector128Unsafe())); } [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static Vec4 operator -( - Vec4 left, - Vec4 right - ) + public static Vec4 ConvertToDouble(Vec4 source) { - if (Unsafe.SizeOf() == 4) { - return Vec4.FromVector128Unsafe(left.AsVector128Unsafe() - right.AsVector128Unsafe()); - } - if (Unsafe.SizeOf() == 8) { - return Vec4.FromVector256Unsafe(left.AsVector256Unsafe() - right.AsVector256Unsafe()); - } - throw GraphicVector.ThrowUnsupportedTypeException(); - } - - public static bool operator !=( - Vec4 left, - Vec4 right - ) - { - return !(left == right); + return FromVector256Unsafe(Vector256.ConvertToDouble(source.AsVector256Unsafe())); } [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static Vec4 operator +( - Vec4 left, - Vec4 right - ) + public static Vec4 ConvertToDouble(Vec4 source) { - if (Unsafe.SizeOf() == 4) { - return Vec4.FromVector128Unsafe(left.AsVector128Unsafe() + right.AsVector128Unsafe()); - } - if (Unsafe.SizeOf() == 8) { - return Vec4.FromVector256Unsafe(left.AsVector256Unsafe() + right.AsVector256Unsafe()); - } - throw GraphicVector.ThrowUnsupportedTypeException(); - } - - public static bool operator ==( - Vec4 left, - Vec4 right - ) - { - if (Unsafe.SizeOf() == 4) { - return left.AsVector128Unsafe() == right.AsVector128Unsafe(); - } - if (Unsafe.SizeOf() == 8) { - return left.AsVector256Unsafe() == right.AsVector256Unsafe(); - } -#pragma warning disable CA1065 - throw GraphicVector.ThrowUnsupportedTypeException(); -#pragma warning restore CA1065 + return FromVector256Unsafe(Vector256.ConvertToDouble(source.AsVector256Unsafe())); } } diff --git a/src/StgSharp.Mathematics/Numeric/Graphics/Vec4_T.cs b/src/StgSharp.Mathematics/Numeric/Graphics/Vec4_T.cs new file mode 100644 index 00000000..616958f9 --- /dev/null +++ b/src/StgSharp.Mathematics/Numeric/Graphics/Vec4_T.cs @@ -0,0 +1,203 @@ +// ----------------------------------------------------------------------------- +// file="Vec4_T" +// Project: StgSharp +// Copyright (c) Nitload. +// SPDX-License-Identifier: MIT +// ----------------------------------------------------------------------------- + +using StgSharp.Mathematics.Numeric; +using System.Numerics; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; +using System.Runtime.Intrinsics; + +namespace StgSharp.Mathematics.Numeric.Graphics +{ + /// + /// A four-dimensional numeric vector. + /// + [StructLayout(LayoutKind.Sequential, Pack = 16)] + public struct Vec4 : IUnmanagedVector> where T: unmanaged, INumber + { + + public T X; + public T Y; + public T Z; + public T W; + + public Vec4(Vec3 v3, T w) + { + X = v3.X; + Y = v3.Y; + Z = v3.Z; + W = w; + } + + public Vec4(T x, T y, T z, T w) + { + X = x; + Y = y; + Z = z; + W = w; + } + + public Vec2 XY + { + readonly get => new(X, Y); + set + { + X = value.X; + Y = value.Y; + } + } + + public Vec3 XYZ + { + readonly get => new(X, Y, Z); + set + { + T w = W; + Unsafe.As>(ref X) = value; + W = w; + } + } + + public static Vec4 One => new(T.One, T.One, T.One, T.One); + + public static Vec4 Zero => new(T.Zero, T.Zero, T.Zero, T.Zero); + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static Vec4 Add(Vec4 left, Vec4 right) + { + return left + right; + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public readonly T Dot(Vec4 right) + { + if (Unsafe.SizeOf() == 4) + { + Vec4 product = Vec4.FromVector128Unsafe(this.AsVector128Unsafe() * right.AsVector128Unsafe()); + return product.X + product.Y + product.Z + product.W; + } + if (Unsafe.SizeOf() == 8) + { + Vec4 product = Vec4.FromVector256Unsafe(this.AsVector256Unsafe() * right.AsVector256Unsafe()); + return product.X + product.Y + product.Z + product.W; + } + throw GraphicVector.ThrowUnsupportedTypeException(); + } + + public ReadOnlySpan AsSpan() + { + return MemoryMarshal.CreateReadOnlySpan(ref X, 4); + } + + public override bool Equals(object? obj) + { + return (obj is Vec4 v) && (v == this); + } + + public static Vec4 FromSpan(ReadOnlySpan span) + { + if (span.Length < 4) { + throw new ArgumentException("Span length must be at least 4.", nameof(span)); + } + + Unsafe.SkipInit(out Vec4 result); + Span target = MemoryMarshal.CreateSpan(ref result.X, 4); + span[..4].CopyTo(target); + return result; + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public ReadOnlySpan.Enumerator GetEnumerator() + { + return AsSpan().GetEnumerator(); + } + + public override int GetHashCode() + { + return HashCode.Combine(X, Y, Z, W); + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static Vec4 operator -(Vec4 left, Vec4 right) + { + if (Unsafe.SizeOf() == 4) { + return Vec4.FromVector128Unsafe(left.AsVector128Unsafe() - right.AsVector128Unsafe()); + } + if (Unsafe.SizeOf() == 8) { + return Vec4.FromVector256Unsafe(left.AsVector256Unsafe() - right.AsVector256Unsafe()); + } + throw GraphicVector.ThrowUnsupportedTypeException(); + } + + public static bool operator !=(Vec4 left, Vec4 right) + { + return !(left == right); + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static Vec4 operator *(Vec4 vector, T scalar) + { + if (Unsafe.SizeOf() == 4) { + return Vec4.FromVector128Unsafe(vector.AsVector128Unsafe() * Vector128.Create(scalar)); + } + if (Unsafe.SizeOf() == 8) { + return Vec4.FromVector256Unsafe(vector.AsVector256Unsafe() * Vector256.Create(scalar)); + } + throw GraphicVector.ThrowUnsupportedTypeException(); + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static Vec4 operator *(T scalar, Vec4 vector) + { + return vector * scalar; + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static T operator *(Vec4 left, Vec4 right) + { + return left.Dot(right); + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static Vec4 operator /(Vec4 vector, T scalar) + { + if (Unsafe.SizeOf() == 4) { + return Vec4.FromVector128Unsafe(vector.AsVector128Unsafe() / Vector128.Create(scalar)); + } + if (Unsafe.SizeOf() == 8) { + return Vec4.FromVector256Unsafe(vector.AsVector256Unsafe() / Vector256.Create(scalar)); + } + throw GraphicVector.ThrowUnsupportedTypeException(); + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static Vec4 operator +(Vec4 left, Vec4 right) + { + if (Unsafe.SizeOf() == 4) { + return Vec4.FromVector128Unsafe(left.AsVector128Unsafe() + right.AsVector128Unsafe()); + } + if (Unsafe.SizeOf() == 8) { + return Vec4.FromVector256Unsafe(left.AsVector256Unsafe() + right.AsVector256Unsafe()); + } + throw GraphicVector.ThrowUnsupportedTypeException(); + } + + public static bool operator ==(Vec4 left, Vec4 right) + { + if (Unsafe.SizeOf() == 4) { + return left.AsVector128Unsafe() == right.AsVector128Unsafe(); + } + if (Unsafe.SizeOf() == 8) { + return left.AsVector256Unsafe() == right.AsVector256Unsafe(); + } +#pragma warning disable CA1065 + throw GraphicVector.ThrowUnsupportedTypeException(); +#pragma warning restore CA1065 + } + + } +} diff --git a/src/StgSharp.Mathematics/Numeric/Graphics/Vector.GetAngle.cs b/src/StgSharp.Mathematics/Numeric/Graphics/Vector.GetAngle.cs deleted file mode 100644 index 554334f2..00000000 --- a/src/StgSharp.Mathematics/Numeric/Graphics/Vector.GetAngle.cs +++ /dev/null @@ -1,33 +0,0 @@ -// ----------------------------------------------------------------------------- -// file="Vector.GetAngle" -// Project: StgSharp -// Copyright (c) Nitload. -// SPDX-License-Identifier: MIT -// ----------------------------------------------------------------------------- - -using StgSharp.Mathematics.Numeric.Graphics; - -namespace StgSharp.Mathematics.Numeric.Graphics -{ - public static unsafe partial class Linear - { - - public static T GetAngle( - Vec2 vec1, - Vec2 vec2 - ) where T : unmanaged, INumber - { - return MathF.Acos( - 1 / (vec1.GetLength() * vec2.GetLength())); - } - - public static T GetAngle( - Vec3 vec1, - Vec3 vec2 - ) where T : unmanaged, INumber - { - return MathF.Acos(vec1 * vec2 / (vec1.GetLength() * vec2.GetLength())); - } - - } -} diff --git a/src/StgSharp.Mathematics/Numeric/Graphics/Vector.GetLength.cs b/src/StgSharp.Mathematics/Numeric/Graphics/Vector.GetLength.cs index e34c22d9..cae9f16b 100644 --- a/src/StgSharp.Mathematics/Numeric/Graphics/Vector.GetLength.cs +++ b/src/StgSharp.Mathematics/Numeric/Graphics/Vector.GetLength.cs @@ -14,17 +14,48 @@ namespace StgSharp.Mathematics.Numeric.Graphics public static T GetLength( this Vec3 vec - ) where T : unmanaged, INumber + ) where T : unmanaged, INumber, IRootFunctions { return T.Sqrt(vec.Dot(vec)); } public static T GetLength( this Vec2 vec - ) where T : unmanaged, INumber + ) where T : unmanaged, INumber, IRootFunctions { return T.Sqrt(vec.Dot(vec)); } + public static T GetLength( + this Vec4 vec + ) where T : unmanaged, INumber, IRootFunctions + { + return T.Sqrt(vec.Dot(vec)); + } + + public static float GetLength(this Vec2 vec) => Vec2.ConvertToSingle(vec).GetLength(); + + public static float GetLength(this Vec2 vec) => Vec2.ConvertToSingle(vec).GetLength(); + + public static double GetLength(this Vec2 vec) => Vec2.ConvertToDouble(vec).GetLength(); + + public static double GetLength(this Vec2 vec) => Vec2.ConvertToDouble(vec).GetLength(); + + public static float GetLength(this Vec3 vec) => Vec3.ConvertToSingle(vec).GetLength(); + + public static float GetLength(this Vec3 vec) => Vec3.ConvertToSingle(vec).GetLength(); + + public static double GetLength(this Vec3 vec) => Vec3.ConvertToDouble(vec).GetLength(); + + public static double GetLength(this Vec3 vec) => Vec3.ConvertToDouble(vec).GetLength(); + + public static float GetLength(this Vec4 vec) => Vec4.ConvertToSingle(vec).GetLength(); + + public static float GetLength(this Vec4 vec) => Vec4.ConvertToSingle(vec).GetLength(); + + public static double GetLength(this Vec4 vec) => Vec4.ConvertToDouble(vec).GetLength(); + + public static double GetLength(this Vec4 vec) => Vec4.ConvertToDouble(vec).GetLength(); + } } diff --git a/src/StgSharp.Mathematics/Numeric/Graphics/Vector.Normalize.cs b/src/StgSharp.Mathematics/Numeric/Graphics/Vector.Normalize.cs index 9c8d5e9a..491efb72 100644 --- a/src/StgSharp.Mathematics/Numeric/Graphics/Vector.Normalize.cs +++ b/src/StgSharp.Mathematics/Numeric/Graphics/Vector.Normalize.cs @@ -5,68 +5,55 @@ // SPDX-License-Identifier: MIT // ----------------------------------------------------------------------------- -using StgSharp.Mathematics.Numeric.Graphics; using System.Numerics; +using System.Runtime.CompilerServices; namespace StgSharp.Mathematics.Numeric.Graphics { public static partial class Linear { - public static Vec4 Normalize( - Vec4 vec - ) + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static Vec2 Normalize(this Vec2 vec) where T: unmanaged, INumber, IRootFunctions { - return new Vec4 - { - vec = Vector4.Normalize(vec.vec) - }; + return vec / T.Sqrt(vec.Dot(vec)); } - public static Vec3 Normalize( - Vec3 vec - ) + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static Vec3 Normalize(this Vec3 vec) where T: unmanaged, INumber, IRootFunctions { - return new Vec3 - { - v = Vector3.Normalize(vec.v) - }; + return vec / T.Sqrt(vec.Dot(vec)); } - public static unsafe void Normalize( - ref Vec4 source, - ref Vec4 target - ) + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static Vec4 Normalize(this Vec4 vec) where T: unmanaged, INumber, IRootFunctions { - Vector4 s = source.vec; - Vector4 t = target.vec; - - s = Vector4.Normalize(s); - - Vector4 projection = Vector4.Multiply(s, t); - - t -= projection; - - source.vec = s; - target.vec = t; + return vec / T.Sqrt(vec.Dot(vec)); } - public static unsafe void Normalize( - ref Vec3 source, - ref Vec3 target - ) - { - Vector3 s = source.v; - Vector3 t = target.v; + public static Vec2 Normalize(this Vec2 vec) => Vec2.ConvertToSingle(vec).Normalize(); - s = Vector3.Normalize(s); - Vector3 projection = Vector3.Dot(t, s) / Vector3.Dot(s, s) * s; - t -= projection; - t = Vector3.Normalize(t); + public static Vec2 Normalize(this Vec2 vec) => Vec2.ConvertToSingle(vec).Normalize(); - source.v = s; - target.v = t; - } + public static Vec2 Normalize(this Vec2 vec) => Vec2.ConvertToDouble(vec).Normalize(); + + public static Vec2 Normalize(this Vec2 vec) => Vec2.ConvertToDouble(vec).Normalize(); + + public static Vec3 Normalize(this Vec3 vec) => Vec3.ConvertToSingle(vec).Normalize(); + + public static Vec3 Normalize(this Vec3 vec) => Vec3.ConvertToSingle(vec).Normalize(); + + public static Vec3 Normalize(this Vec3 vec) => Vec3.ConvertToDouble(vec).Normalize(); + + public static Vec3 Normalize(this Vec3 vec) => Vec3.ConvertToDouble(vec).Normalize(); + + public static Vec4 Normalize(this Vec4 vec) => Vec4.ConvertToSingle(vec).Normalize(); + + public static Vec4 Normalize(this Vec4 vec) => Vec4.ConvertToSingle(vec).Normalize(); + + public static Vec4 Normalize(this Vec4 vec) => Vec4.ConvertToDouble(vec).Normalize(); + + public static Vec4 Normalize(this Vec4 vec) => Vec4.ConvertToDouble(vec).Normalize(); } } diff --git a/src/StgSharp.RegularAnalysis/Text/RegexAstNode.cs b/src/StgSharp.RegularAnalysis/Text/RegexAstNode.cs index 53aa5368..5ef20904 100644 --- a/src/StgSharp.RegularAnalysis/Text/RegexAstNode.cs +++ b/src/StgSharp.RegularAnalysis/Text/RegexAstNode.cs @@ -10,21 +10,14 @@ using System.Runtime.CompilerServices; namespace StgSharp.RegularAnalysis.Text { - // TODO 现在存在的问题是,AstNode过于依赖NodeFlag表达其内部语义,这导致进行精细优化时识别节点语义复杂, - // TODO 当前Ast的类型差异过小,不利于保存特征数据,应当争取进一步细化,或者说这是AstNode一开始为了简化架构设计引入的妥协 internal class RegexAstNode : ISyntaxNode { // private RegexAstNode _left, _right; - public RegexAstNode( - Token source - ) - : this(RegexCommonPayload.FromToken(source)) { } + public RegexAstNode(Token source) : this(RegexCommonPayload.FromToken(source)) { } - public RegexAstNode( - RegexAstPayload payload - ) + public RegexAstNode(RegexAstPayload payload) { Payload = payload; } @@ -85,19 +78,14 @@ namespace StgSharp.RegularAnalysis.Text public string SourceCode => Source.Source; - public void AppendNode( - RegexAstNode nextToken - ) + public void AppendNode(RegexAstNode nextToken) { Next = nextToken; nextToken.Previous = this; } [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static (bool Valid, int Value) ComparePrecedence( - RegexElementLabel left, - RegexElementLabel right - ) + public static (bool Valid, int Value) ComparePrecedence(RegexElementLabel left, RegexElementLabel right) { left &= RegexElementLabel.OPERATOR; right &= RegexElementLabel.OPERATOR; @@ -109,25 +97,19 @@ namespace StgSharp.RegularAnalysis.Text } [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static bool IsNullOrEmpty( - RegexAstNode node - ) + public static bool IsNullOrEmpty(RegexAstNode node) { return node is null || ReferenceEquals(node, Empty); } /**/ - public void PrependNode( - RegexAstNode previousNode - ) + public void PrependNode(RegexAstNode previousNode) { Previous = previousNode; previousNode.Next = this; } - public void RemoveChild( - RegexAstNode node - ) + public void RemoveChild(RegexAstNode node) { if (Left == node) { @@ -145,7 +127,7 @@ namespace StgSharp.RegularAnalysis.Text return new EmptyRegexAstNode(); } - internal TPayload PayloadAs() where TPayload : RegexAstPayload + internal TPayload PayloadAs() where TPayload: RegexAstPayload { return Payload as TPayload ?? @@ -153,9 +135,7 @@ namespace StgSharp.RegularAnalysis.Text $"Node {Label} does not carry {typeof(TPayload).Name}."); } - internal void RemoveAndAsChild( - bool useLeft - ) + internal void RemoveAndAsChild(bool useLeft) { if (useLeft) { @@ -180,9 +160,7 @@ namespace StgSharp.RegularAnalysis.Text /// /// The node to replace the current node with. /// - internal void ReplaceBy( - RegexAstNode node - ) + internal void ReplaceBy(RegexAstNode node) { Payload = node.Payload; Left = node.Left;