From a95e9b1ff7e75351763c9edcd7e8c847fa8cf57a Mon Sep 17 00:00:00 2001 From: Youmu Liu <105042599+Nitload-NSI@users.noreply.github.com> Date: Mon, 13 Oct 2025 20:59:14 +0800 Subject: [PATCH] namespace arrange; final wrap scheduale solution --- StgSharp.Common/Global.cs | 73 +++++----- .../Graphics/OpenGL/ShaderEdit/Uniform.cs | 10 +- .../HighPerformance/IRegisterType.cs | 54 ++++---- StgSharp.Common/HighPerformance/Lable.cs | 3 +- .../HighPerformance/MatrixKernel.cs | 7 - .../HighPerformance/Memory/BufferStack.cs | 4 +- .../HighPerformance/UnsafeCompute.cs | 72 +++++----- .../Internal/Intrinsic/ColumnSet4.cs | 126 +++++++++--------- StgSharp.Common/Mathematics/Graphic/Camera.cs | 32 ++--- .../{Linear => Graphic}/Matrix44.cs | 20 +-- .../Mathematics/{Linear => Graphic}/Vec2.cs | 8 +- .../Mathematics/{Linear => Graphic}/Vec3.cs | 15 ++- .../Mathematics/{Linear => Graphic}/Vec4.cs | 9 +- .../Mathematics/Graphic/Vector.Cross.cs | 52 ++++++++ .../Vec4HP.cs => Graphic/Vector.GetAngle.cs} | 42 ++---- .../Mathematics/Graphic/Vector.GetLength.cs | 49 +++++++ .../Vec3HP.cs => Graphic/Vector.Normalize.cs} | 83 ++++++------ .../Graphic/Vector.Orthogonalize.cs | 95 +++++++++++++ .../Mathematics/Linear/Cordination.cs | 2 +- StgSharp.Common/Mathematics/Linear/IMatrix.cs | 52 ++++---- .../{IFixedVector.cs => IUnmanagedVector.cs} | 4 +- .../Mathematics/Linear/Matrix.Basic.cs | 86 ------------ .../Mathematics/Linear/Matrix.GC.cs | 2 +- .../Mathematics/Linear/Matrix.HLSF.cs | 2 +- StgSharp.Common/Mathematics/Linear/Matrix.cs | 4 +- .../Mathematics/Linear/MatrixEnum.cs | 2 +- .../Mathematics/Linear/MatrixIndex.cs | 6 +- .../Mathematics/Linear/Vector.Cross.cs | 50 ------- .../Mathematics/Linear/Vector.GetAngle.cs | 50 ------- .../Mathematics/Linear/Vector.GetLength.cs | 47 ------- .../Mathematics/Linear/Vector.Normalize.cs | 94 ------------- .../Linear/Vector.Orthogonalize.cs | 104 --------------- StgSharp.Common/Mathematics/Scalar/ASin.cs | 54 ++++---- StgSharp.Common/Mathematics/Scalar/Cos.cs | 56 ++++---- .../PlainGeometryCollisionSensor.cs | 65 ++++----- .../Numeric/MatrixCompute.Basic.cs | 41 +++--- .../Numeric}/MatrixMarshal.cs | 0 .../Numeric}/MatrixParallel.Execute.cs | 2 +- .../Numeric}/MatrixParallel.Publish.cs | 12 +- .../Numeric}/MatrixParallel.Task.cs | 2 +- .../Numeric}/MatrixParallel.TaskFactory.cs | 2 +- .../Numeric}/MatrixParallel.cs | 61 +++------ .../StgSharp.Mathematics.csproj | 15 +++ StgSharpDevelop.sln | 40 ++++++ 44 files changed, 683 insertions(+), 926 deletions(-) rename StgSharp.Common/Mathematics/{Linear => Graphic}/Matrix44.cs (93%) rename StgSharp.Common/Mathematics/{Linear => Graphic}/Vec2.cs (96%) rename StgSharp.Common/Mathematics/{Linear => Graphic}/Vec3.cs (95%) rename StgSharp.Common/Mathematics/{Linear => Graphic}/Vec4.cs (96%) create mode 100644 StgSharp.Common/Mathematics/Graphic/Vector.Cross.cs rename StgSharp.Common/Mathematics/{HighPrecision/Vec4HP.cs => Graphic/Vector.GetAngle.cs} (58%) create mode 100644 StgSharp.Common/Mathematics/Graphic/Vector.GetLength.cs rename StgSharp.Common/Mathematics/{HighPrecision/Vec3HP.cs => Graphic/Vector.Normalize.cs} (52%) create mode 100644 StgSharp.Common/Mathematics/Graphic/Vector.Orthogonalize.cs rename StgSharp.Common/Mathematics/Linear/{IFixedVector.cs => IUnmanagedVector.cs} (93%) delete mode 100644 StgSharp.Common/Mathematics/Linear/Matrix.Basic.cs delete mode 100644 StgSharp.Common/Mathematics/Linear/Vector.Cross.cs delete mode 100644 StgSharp.Common/Mathematics/Linear/Vector.GetAngle.cs delete mode 100644 StgSharp.Common/Mathematics/Linear/Vector.GetLength.cs delete mode 100644 StgSharp.Common/Mathematics/Linear/Vector.Normalize.cs delete mode 100644 StgSharp.Common/Mathematics/Linear/Vector.Orthogonalize.cs rename StgSharp.Common/Mathematics/HighPrecision/Vec2HP.cs => StgSharp.Mathematics/Numeric/MatrixCompute.Basic.cs (67%) rename {StgSharp.Common/Mathematics/Linear => StgSharp.Mathematics/Numeric}/MatrixMarshal.cs (100%) rename {StgSharp.Common/Mathematics/Linear => StgSharp.Mathematics/Numeric}/MatrixParallel.Execute.cs (99%) rename {StgSharp.Common/Mathematics/Linear => StgSharp.Mathematics/Numeric}/MatrixParallel.Publish.cs (98%) rename {StgSharp.Common/Mathematics/Linear => StgSharp.Mathematics/Numeric}/MatrixParallel.Task.cs (99%) rename {StgSharp.Common/Mathematics/Linear => StgSharp.Mathematics/Numeric}/MatrixParallel.TaskFactory.cs (99%) rename {StgSharp.Common/Mathematics/Linear => StgSharp.Mathematics/Numeric}/MatrixParallel.cs (86%) create mode 100644 StgSharp.Mathematics/StgSharp.Mathematics.csproj diff --git a/StgSharp.Common/Global.cs b/StgSharp.Common/Global.cs index 3b5610ec..61879606 100644 --- a/StgSharp.Common/Global.cs +++ b/StgSharp.Common/Global.cs @@ -1,33 +1,33 @@ //----------------------------------------------------------------------- -//----------------------------------------------------------------------- -// file="Global.cs" -// Project: StepVisualizer -// AuthorGroup: Nitload Space -// Copyright (c) Nitload Space. All rights reserved. +// ----------------------------------------------------------------------- +// file="Global.cs" +// Project: StepVisualizer +// AuthorGroup: Nitload Space +// Copyright (c) Nitload Space. All rights reserved. // -// Permission is hereby granted, free of charge, to any person -// obtaining a copy of this software and associated documentation -// files (the “Software”), to deal in the Software without restriction, -// including without limitation the rights to use, copy, modify, merge, -// publish, distribute, sublicense, and/or sell copies of the Software, -// and to permit persons to whom the Software is furnished to do so, -// subject to the following conditions: +// Permission is hereby granted, free of charge, to any person +// obtaining a copy of this software and associated documentation +// files (the “Software”), to deal in the Software without restriction, +// including without limitation the rights to use, copy, modify, merge, +// publish, distribute, sublicense, and/or sell copies of the Software, +// and to permit persons to whom the Software is furnished to do so, +// subject to the following conditions: // -// The above copyright notice and -// this permission notice shall be included in all copies -// or substantial portions of the Software. +// The above copyright notice and +// this permission notice shall be included in all copies +// or substantial portions of the Software. // -// THE SOFTWARE IS PROVIDED “AS IS”, -// WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, -// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. -// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, -// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, -// ARISING FROM, OUT OF OR IN CONNECTION WITH -// THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +// THE SOFTWARE IS PROVIDED “AS IS”, +// WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, +// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. +// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, +// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, +// ARISING FROM, OUT OF OR IN CONNECTION WITH +// THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. // -//----------------------------------------------------------------------- -//----------------------------------------------------------------------- +// ----------------------------------------------------------------------- +// ----------------------------------------------------------------------- // This file is used by Code Analysis to maintain SuppressMessage // attributes that are applied to this project. // Project-level suppressions either have no target or are given @@ -38,64 +38,67 @@ global using StgSharp.Internal.Intrinsic; global using StgSharp.Internal.OpenGL; using System.Diagnostics.CodeAnalysis; +using System.Runtime.CompilerServices; + +[assembly: InternalsVisibleTo("StgSharp.Mathematics")] [assembly: SuppressMessage( "Design", "CA1008:枚举应具有零值", Justification = "<挂起>", Scope = "type", - Target = "~T:StgSharp.Graphics.OpenGL.ShaderStatus" )] + Target = "~T:StgSharp.Graphics.OpenGL.ShaderStatus")] [assembly: SuppressMessage( "Naming", "CA1715:标识符应具有正确的前缀", Justification = "<挂起>", Scope = "member", - Target = "~M:StgSharp.Graphics.OpenGL.ShaderProgram.GetUniform``2(System.String)~StgSharp.Graphics.OpenGL.Uniform{``0,``1}" )] + Target = "~M:StgSharp.Graphics.OpenGL.ShaderProgram.GetUniform``2(System.String)~StgSharp.Graphics.OpenGL.Uniform{``0,``1}")] [assembly: SuppressMessage( "Naming", "CA1715:标识符应具有正确的前缀", Justification = "<挂起>", Scope = "member", - Target = "~M:StgSharp.Graphics.OpenGL.ShaderProgram.GetUniform``3(System.String)~StgSharp.Graphics.OpenGL.Uniform{``0,``1,``2}" )] + Target = "~M:StgSharp.Graphics.OpenGL.ShaderProgram.GetUniform``3(System.String)~StgSharp.Graphics.OpenGL.Uniform{``0,``1,``2}")] [assembly: SuppressMessage( "Naming", "CA1715:标识符应具有正确的前缀", Justification = "<挂起>", Scope = "member", - Target = "~M:StgSharp.Graphics.OpenGL.ShaderProgram.GetUniform``4(System.String)~StgSharp.Graphics.OpenGL.Uniform{``0,``1,``2,``3}" )] + Target = "~M:StgSharp.Graphics.OpenGL.ShaderProgram.GetUniform``4(System.String)~StgSharp.Graphics.OpenGL.Uniform{``0,``1,``2,``3}")] [assembly: SuppressMessage( "Naming", "CA1715:标识符应具有正确的前缀", Justification = "<挂起>", Scope = "type", - Target = "~T:StgSharp.Graphics.OpenGL.Uniform`4" )] + Target = "~T:StgSharp.Graphics.OpenGL.Uniform`4")] [assembly: SuppressMessage( "Design", "CA1043:将整型或字符串参数用于索引器", Justification = "<挂起>", Scope = "member", - Target = "~P:StgSharp.MVVM.ViewBase.IViewResponder`1.Item(StgSharp.Controlling.UsrActivity.ITrigger)" )] + Target = "~P:StgSharp.MVVM.ViewBase.IViewResponder`1.Item(StgSharp.Controlling.UsrActivity.ITrigger)")] [assembly: SuppressMessage( "Design", "CA1043:将整型或字符串参数用于索引器", Justification = "<挂起>", Scope = "member", - Target = "~P:StgSharp.MVVM.ViewBase.ViewResponder`1.Item(StgSharp.Controlling.UsrActivity.ITrigger)" )] + Target = "~P:StgSharp.MVVM.ViewBase.ViewResponder`1.Item(StgSharp.Controlling.UsrActivity.ITrigger)")] [assembly: SuppressMessage( "Performance", "CA1864:首选 \"IDictionary.TryAdd(TKey, TValue)\" 方法", Justification = "<挂起>", Scope = "member", - Target = "~P:StgSharp.MVVM.ViewBase.ViewRender`1.Item(System.String)" )] + Target = "~P:StgSharp.MVVM.ViewBase.ViewRender`1.Item(System.String)")] [assembly: SuppressMessage( "Design", "CA1033:接口方法应可由子类型调用", Justification = "<挂起>", Scope = "member", - Target = "~P:StgSharp.Collections.IBidirectionalDictionary`2.System#Collections#Generic#IDictionary#Keys" )] + Target = "~P:StgSharp.Collections.IBidirectionalDictionary`2.System#Collections#Generic#IDictionary#Keys")] [assembly: SuppressMessage( "Design", "CA1033:接口方法应可由子类型调用", Justification = "<挂起>", Scope = "member", - Target = "~P:StgSharp.Collections.IBidirectionalDictionary`2.System#Collections#Generic#IDictionary#Values" )] + Target = "~P:StgSharp.Collections.IBidirectionalDictionary`2.System#Collections#Generic#IDictionary#Values")] diff --git a/StgSharp.Common/Graphics/OpenGL/ShaderEdit/Uniform.cs b/StgSharp.Common/Graphics/OpenGL/ShaderEdit/Uniform.cs index ea3269b8..68553797 100644 --- a/StgSharp.Common/Graphics/OpenGL/ShaderEdit/Uniform.cs +++ b/StgSharp.Common/Graphics/OpenGL/ShaderEdit/Uniform.cs @@ -28,17 +28,9 @@ // // ----------------------------------------------------------------------- // ----------------------------------------------------------------------- -using StgSharp.Graphics; -using StgSharp.Mathematics; - -using System; -using System.Collections.Generic; +using StgSharp.Mathematics.Graphic; using System.Diagnostics.CodeAnalysis; -using System.Linq; -using System.Numerics; using System.Runtime.CompilerServices; -using System.Runtime.InteropServices; -using System.Text; namespace StgSharp.Graphics.OpenGL { diff --git a/StgSharp.Common/HighPerformance/IRegisterType.cs b/StgSharp.Common/HighPerformance/IRegisterType.cs index be0eff7d..6f06745a 100644 --- a/StgSharp.Common/HighPerformance/IRegisterType.cs +++ b/StgSharp.Common/HighPerformance/IRegisterType.cs @@ -1,33 +1,33 @@ //----------------------------------------------------------------------- -//----------------------------------------------------------------------- -// file="IRegisterType.cs" -// Project: StgSharp -// AuthorGroup: Nitload Space -// Copyright (c) Nitload Space. All rights reserved. +// ----------------------------------------------------------------------- +// file="IRegisterType.cs" +// Project: StgSharp +// AuthorGroup: Nitload Space +// Copyright (c) Nitload Space. All rights reserved. // -// Permission is hereby granted, free of charge, to any person -// obtaining a copy of this software and associated documentation -// files (the “Software”), to deal in the Software without restriction, -// including without limitation the rights to use, copy, modify, merge, -// publish, distribute, sublicense, and/or sell copies of the Software, -// and to permit persons to whom the Software is furnished to do so, -// subject to the following conditions: +// Permission is hereby granted, free of charge, to any person +// obtaining a copy of this software and associated documentation +// files (the “Software”), to deal in the Software without restriction, +// including without limitation the rights to use, copy, modify, merge, +// publish, distribute, sublicense, and/or sell copies of the Software, +// and to permit persons to whom the Software is furnished to do so, +// subject to the following conditions: // -// The above copyright notice and -// this permission notice shall be included in all copies -// or substantial portions of the Software. +// The above copyright notice and +// this permission notice shall be included in all copies +// or substantial portions of the Software. // -// THE SOFTWARE IS PROVIDED “AS IS”, -// WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, -// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. -// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, -// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, -// ARISING FROM, OUT OF OR IN CONNECTION WITH -// THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +// THE SOFTWARE IS PROVIDED “AS IS”, +// WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, +// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. +// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, +// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, +// ARISING FROM, OUT OF OR IN CONNECTION WITH +// THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. // -//----------------------------------------------------------------------- -//----------------------------------------------------------------------- +// ----------------------------------------------------------------------- +// ----------------------------------------------------------------------- using System; using System.Collections.Generic; using System.Linq; @@ -40,9 +40,9 @@ namespace StgSharp.HighPerformance public interface IRegisterType { - public ref T AsRef() where T: unmanaged, INumber; + ref T AsRef() where T: unmanaged, INumber; - public ref T Member(int index) where T: unmanaged, INumber; + ref T Member(int index) where T: unmanaged, INumber; } } diff --git a/StgSharp.Common/HighPerformance/Lable.cs b/StgSharp.Common/HighPerformance/Lable.cs index 798cc638..60bf81dd 100644 --- a/StgSharp.Common/HighPerformance/Lable.cs +++ b/StgSharp.Common/HighPerformance/Lable.cs @@ -43,7 +43,7 @@ namespace StgSharp.HighPerformance public string Name { -[MethodImpl(MethodImplOptions.AggressiveInlining)] + [MethodImpl(MethodImplOptions.AggressiveInlining)] get; init; } @@ -61,6 +61,7 @@ namespace StgSharp.HighPerformance public static class DataLabelBuilder { + [MethodImpl(MethodImplOptions.AggressiveInlining)] public static DataLabel AsLabel(this string name) { return new DataLabel(name); diff --git a/StgSharp.Common/HighPerformance/MatrixKernel.cs b/StgSharp.Common/HighPerformance/MatrixKernel.cs index 0d16bb60..50e03fa0 100644 --- a/StgSharp.Common/HighPerformance/MatrixKernel.cs +++ b/StgSharp.Common/HighPerformance/MatrixKernel.cs @@ -28,16 +28,9 @@ // // ----------------------------------------------------------------------- // ----------------------------------------------------------------------- -using StgSharp.HighPerformance; - -using System; -using System.Collections.Generic; -using System.Linq; using System.Numerics; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; -using System.Text; -using System.Threading.Tasks; namespace StgSharp.HighPerformance { diff --git a/StgSharp.Common/HighPerformance/Memory/BufferStack.cs b/StgSharp.Common/HighPerformance/Memory/BufferStack.cs index bd2dcc0a..b4c2e11e 100644 --- a/StgSharp.Common/HighPerformance/Memory/BufferStack.cs +++ b/StgSharp.Common/HighPerformance/Memory/BufferStack.cs @@ -52,7 +52,7 @@ namespace StgSharp.HighPerformance.Memory public int Count { -[MethodImpl(MethodImplOptions.AggressiveInlining)] + [MethodImpl(MethodImplOptions.AggressiveInlining)] get => (int)_count; } @@ -151,7 +151,7 @@ namespace StgSharp.HighPerformance.Memory { Header* header = (Header*)_buffer; nuint newCap = header->Capacity * 2; - nuint newSize = (nuint)sizeof(Header) + newCap * (nuint)sizeof(T); + nuint newSize = (nuint)sizeof(Header) + (newCap * (nuint)sizeof(T)); _buffer = (byte*)NativeMemory.Realloc(_buffer, newSize); header = (Header*)_buffer; header->Capacity = newCap; diff --git a/StgSharp.Common/HighPerformance/UnsafeCompute.cs b/StgSharp.Common/HighPerformance/UnsafeCompute.cs index c5d827ed..f6cb5f48 100644 --- a/StgSharp.Common/HighPerformance/UnsafeCompute.cs +++ b/StgSharp.Common/HighPerformance/UnsafeCompute.cs @@ -1,60 +1,56 @@ //----------------------------------------------------------------------- -//----------------------------------------------------------------------- -// file="UnsafeCompute.cs" -// Project: StgSharp -// AuthorGroup: Nitload Space -// Copyright (c) Nitload Space. All rights reserved. +// ----------------------------------------------------------------------- +// file="UnsafeCompute.cs" +// Project: StgSharp +// AuthorGroup: Nitload Space +// Copyright (c) Nitload Space. All rights reserved. // -// Permission is hereby granted, free of charge, to any person -// obtaining a copy of this software and associated documentation -// files (the “Software”), to deal in the Software without restriction, -// including without limitation the rights to use, copy, modify, merge, -// publish, distribute, sublicense, and/or sell copies of the Software, -// and to permit persons to whom the Software is furnished to do so, -// subject to the following conditions: +// Permission is hereby granted, free of charge, to any person +// obtaining a copy of this software and associated documentation +// files (the “Software”), to deal in the Software without restriction, +// including without limitation the rights to use, copy, modify, merge, +// publish, distribute, sublicense, and/or sell copies of the Software, +// and to permit persons to whom the Software is furnished to do so, +// subject to the following conditions: // -// The above copyright notice and -// this permission notice shall be included in all copies -// or substantial portions of the Software. +// The above copyright notice and +// this permission notice shall be included in all copies +// or substantial portions of the Software. // -// THE SOFTWARE IS PROVIDED “AS IS”, -// WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, -// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. -// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, -// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, -// ARISING FROM, OUT OF OR IN CONNECTION WITH -// THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +// THE SOFTWARE IS PROVIDED “AS IS”, +// WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, +// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. +// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, +// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, +// ARISING FROM, OUT OF OR IN CONNECTION WITH +// THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. // -//----------------------------------------------------------------------- -//----------------------------------------------------------------------- -using System; -using System.Collections.Generic; -using System.Diagnostics; +// ----------------------------------------------------------------------- +// ----------------------------------------------------------------------- using System.Runtime.CompilerServices; -using System.Text; namespace StgSharp.HighPerformance { public static class UnsafeCompute { - [MethodImpl( MethodImplOptions.AggressiveInlining )] - public static unsafe int CityHashSimplify( char* span, int length ) + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static unsafe int CityHashSimplify(char* span, int length) { - return InternalIO.Intrinsic.city_hash_simplify( span, length ); + return InternalIO.Intrinsic.city_hash_simplify(span, length); } - [MethodImpl( MethodImplOptions.AggressiveInlining )] - public static unsafe int CityHashSimplify( char* span, int begin, int length ) + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static unsafe int CityHashSimplify(char* span, int begin, int length) { - return InternalIO.Intrinsic.city_hash_simplify( span + begin, length ); + return InternalIO.Intrinsic.city_hash_simplify(span + begin, length); } - [MethodImpl( MethodImplOptions.AggressiveInlining )] - public static unsafe int IndexOfCharPair( char* source, int pairValue, int length ) + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static unsafe int IndexOfCharPair(char* source, int pairValue, int length) { - return InternalIO.Intrinsic.index_pair( source, pairValue, length ); + return InternalIO.Intrinsic.index_pair(source, pairValue, length); } } diff --git a/StgSharp.Common/Internal/Intrinsic/ColumnSet4.cs b/StgSharp.Common/Internal/Intrinsic/ColumnSet4.cs index dbdaa263..dd1c2219 100644 --- a/StgSharp.Common/Internal/Intrinsic/ColumnSet4.cs +++ b/StgSharp.Common/Internal/Intrinsic/ColumnSet4.cs @@ -1,33 +1,33 @@ //----------------------------------------------------------------------- -//----------------------------------------------------------------------- -// file="ColumnSet4.cs" -// Project: StgSharp -// AuthorGroup: Nitload Space -// Copyright (c) Nitload Space. All rights reserved. +// ----------------------------------------------------------------------- +// file="ColumnSet4.cs" +// Project: StgSharp +// AuthorGroup: Nitload Space +// Copyright (c) Nitload Space. All rights reserved. // -// Permission is hereby granted, free of charge, to any person -// obtaining a copy of this software and associated documentation -// files (the “Software”), to deal in the Software without restriction, -// including without limitation the rights to use, copy, modify, merge, -// publish, distribute, sublicense, and/or sell copies of the Software, -// and to permit persons to whom the Software is furnished to do so, -// subject to the following conditions: +// Permission is hereby granted, free of charge, to any person +// obtaining a copy of this software and associated documentation +// files (the “Software”), to deal in the Software without restriction, +// including without limitation the rights to use, copy, modify, merge, +// publish, distribute, sublicense, and/or sell copies of the Software, +// and to permit persons to whom the Software is furnished to do so, +// subject to the following conditions: // -// The above copyright notice and -// this permission notice shall be included in all copies -// or substantial portions of the Software. +// The above copyright notice and +// this permission notice shall be included in all copies +// or substantial portions of the Software. // -// THE SOFTWARE IS PROVIDED “AS IS”, -// WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, -// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. -// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, -// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, -// ARISING FROM, OUT OF OR IN CONNECTION WITH -// THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +// THE SOFTWARE IS PROVIDED “AS IS”, +// WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, +// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. +// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, +// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, +// ARISING FROM, OUT OF OR IN CONNECTION WITH +// THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. // -//----------------------------------------------------------------------- -//----------------------------------------------------------------------- +// ----------------------------------------------------------------------- +// ----------------------------------------------------------------------- using StgSharp; using StgSharp.Internal.Intrinsic; @@ -38,66 +38,66 @@ using System.Runtime.InteropServices; namespace StgSharp.Internal.Intrinsic { - [StructLayout( LayoutKind.Explicit, Size = 16 * sizeof( float ) )] + [StructLayout(LayoutKind.Explicit, Size = 16 * sizeof(float))] internal struct ColumnSet4 : IEquatable { - [FieldOffset( 0 )] internal Vector4 colum0; - [FieldOffset( 16 )] internal Vector4 colum1; - [FieldOffset( 32 )] internal Vector4 colum2; - [FieldOffset( 48 )] internal Vector4 colum3; + [FieldOffset(0)] internal Vector4 colum0; + [FieldOffset(16)] internal Vector4 colum1; + [FieldOffset(32)] internal Vector4 colum2; + [FieldOffset(48)] internal Vector4 colum3; - [FieldOffset( 0 )] public float m00; - [FieldOffset( 16 )] public float m01; - [FieldOffset( 32 )] public float m02; - [FieldOffset( 48 )] public float m03; - [FieldOffset( 4 )] public float m10; - [FieldOffset( 20 )] public float m11; - [FieldOffset( 36 )] public float m12; - [FieldOffset( 52 )] public float m13; - [FieldOffset( 8 )] public float m20; - [FieldOffset( 24 )] public float m21; - [FieldOffset( 40 )] public float m22; - [FieldOffset( 56 )] public float m23; - [FieldOffset( 12 )] public float m30; - [FieldOffset( 28 )] public float m31; - [FieldOffset( 44 )] public float m32; - [FieldOffset( 60 )] public float m33; + [FieldOffset(0)] public float m00; + [FieldOffset(16)] public float m01; + [FieldOffset(32)] public float m02; + [FieldOffset(48)] public float m03; + [FieldOffset(4)] public float m10; + [FieldOffset(20)] public float m11; + [FieldOffset(36)] public float m12; + [FieldOffset(52)] public float m13; + [FieldOffset(8)] public float m20; + [FieldOffset(24)] public float m21; + [FieldOffset(40)] public float m22; + [FieldOffset(56)] public float m23; + [FieldOffset(12)] public float m30; + [FieldOffset(28)] public float m31; + [FieldOffset(44)] public float m32; + [FieldOffset(60)] public float m33; - [MethodImpl( MethodImplOptions.AggressiveInlining )] - internal ColumnSet4( Vector4 colum0, Vector4 colum1, Vector4 colum2, Vector4 colum3 ) + [MethodImpl(MethodImplOptions.AggressiveInlining)] + internal ColumnSet4(Vector4 colum0, Vector4 colum1, Vector4 colum2, Vector4 colum3) { - Unsafe.SkipInit( out this ); + Unsafe.SkipInit(out this); this.colum0 = colum0; this.colum1 = colum1; this.colum2 = colum2; this.colum3 = colum3; } - public override bool Equals( object? obj ) + public override bool Equals(object? obj) { - return ( obj is ColumnSet4 mat ) && Equals( mat ); + return (obj is ColumnSet4 mat) && Equals(mat); } - [MethodImpl( MethodImplOptions.AggressiveInlining )] - public bool Equals( ColumnSet4 other ) + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public bool Equals(ColumnSet4 other) { - return colum0.Equals( other.colum0 ) && - colum1.Equals( other.colum1 ) && - colum2.Equals( other.colum2 ) && - colum3.Equals( other.colum3 ); + return colum0.Equals(other.colum0) && + colum1.Equals(other.colum1) && + colum2.Equals(other.colum2) && + colum3.Equals(other.colum3); } - [MethodImpl( MethodImplOptions.AggressiveInlining )] - public static bool operator !=( ColumnSet4 left, ColumnSet4 right ) + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static bool operator !=(ColumnSet4 left, ColumnSet4 right) { - return !( left == right ); + return !(left == right); } - [MethodImpl( MethodImplOptions.AggressiveInlining )] - public static bool operator ==( ColumnSet4 left, ColumnSet4 right ) + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static bool operator ==(ColumnSet4 left, ColumnSet4 right) { - return left.Equals( right ); + return left.Equals(right); } } diff --git a/StgSharp.Common/Mathematics/Graphic/Camera.cs b/StgSharp.Common/Mathematics/Graphic/Camera.cs index a856f786..0321e777 100644 --- a/StgSharp.Common/Mathematics/Graphic/Camera.cs +++ b/StgSharp.Common/Mathematics/Graphic/Camera.cs @@ -28,20 +28,12 @@ // // ----------------------------------------------------------------------- // ----------------------------------------------------------------------- -using StgSharp.Graphics; using StgSharp.Graphics.OpenGL; using StgSharp.Graphics.ShaderEdit; -using StgSharp.HighPerformance; -using StgSharp.Mathematics; using System; -using System.Collections.Generic; -using System.Numerics; -using System.Runtime.CompilerServices; -using System.Runtime.InteropServices; -using System.Text; -namespace StgSharp.Graphics +namespace StgSharp.Mathematics.Graphic { public sealed class Camera : IglConvertable { @@ -150,7 +142,7 @@ namespace StgSharp.Graphics public unsafe void SetAllUniforms() { // Console.WriteLine(Projection * ViewBase); - OpenGL.OpenGLFunction.CurrentGL.SetUniformValue(convertedUniform, Projection * View); + OpenGLFunction.CurrentGL.SetUniformValue(convertedUniform, Projection * View); } public void SetViewDirection(Vec3 position, Vec3 target, Vec3 up) @@ -172,7 +164,7 @@ namespace StgSharp.Graphics _isLookAtAvailable = false; - Linear.Orthogonalize(ref direction, ref up); + direction.Orthogonalize(ref up); Vec3 right = Linear.Orthogonalize(Linear.Cross(up, direction)); @@ -186,24 +178,24 @@ namespace StgSharp.Graphics InternalYaw(); } - public void SetViewRange(Radius fovRadius, Vec2 size, Vec2 offset, (float front, float back) dephRange) + public void SetViewRange(Radius fovRadius, Vec2 size, Vec2 offset, (float front, float back) depthRange) { float distance = _target.GetLength(), - near = distance - dephRange.front, - far = distance + dephRange.back, + near = distance - depthRange.front, + far = distance + depthRange.back, offsetX = offset.X, offsetY = offset.Y, width = Scaler.Abs(GeometryScaler.Tan(fovRadius / 2) * near * 2), - height = (width * size.Y) / size.X; + height = width * size.Y / size.X; - _projection.colum0.X = (2 * near) / width; - _projection.colum1.Y = (2 * near) / height; - _projection.colum2.X = (2 * offsetX) / width; - _projection.colum2.Y = (2 * offsetY) / height; + _projection.colum0.X = 2 * near / width; + _projection.colum1.Y = 2 * near / height; + _projection.colum2.X = 2 * offsetX / width; + _projection.colum2.Y = 2 * offsetY / height; _projection.colum2.Z = (far + near) / (near - far); _projection.colum2.W = -1; - _projection.colum3.Z = (2 * near * far) / (near - far); + _projection.colum3.Z = 2 * near * far / (near - far); } public void Test(params Vec4[] vec) diff --git a/StgSharp.Common/Mathematics/Linear/Matrix44.cs b/StgSharp.Common/Mathematics/Graphic/Matrix44.cs similarity index 93% rename from StgSharp.Common/Mathematics/Linear/Matrix44.cs rename to StgSharp.Common/Mathematics/Graphic/Matrix44.cs index e5bfa1a2..683cfec8 100644 --- a/StgSharp.Common/Mathematics/Linear/Matrix44.cs +++ b/StgSharp.Common/Mathematics/Graphic/Matrix44.cs @@ -29,7 +29,7 @@ // ----------------------------------------------------------------------- // ----------------------------------------------------------------------- using StgSharp.Internal.Intrinsic; - +using StgSharp.Mathematics.Numeric; using System; using System.ComponentModel; using System.Drawing; @@ -37,9 +37,9 @@ using System.Numerics; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; -namespace StgSharp.Mathematics +namespace StgSharp.Mathematics.Graphic { - [StructLayout(LayoutKind.Explicit, Size = (2 * 16 * sizeof(float)) + sizeof(bool), Pack = 16)] + [StructLayout(LayoutKind.Explicit, Size = 2 * 16 * sizeof(float) + sizeof(bool), Pack = 16)] public unsafe struct Matrix44 : IEquatable, IMatrix { @@ -115,9 +115,9 @@ namespace StgSharp.Mathematics } #endif InternalTranspose(); - fixed (float* p = &this.transpose.m00) + fixed (float* p = &transpose.m00) { - ulong pBit = ((ulong)p) + (((ulong)sizeof(Vector4)) * ((ulong)rowNum)) + (((ulong)sizeof(float)) * ((ulong)columNum)); + ulong pBit = (ulong)p + (ulong)sizeof(Vector4) * (ulong)rowNum + sizeof(float) * (ulong)columNum; return *(float*)pBit; } } @@ -133,9 +133,9 @@ namespace StgSharp.Mathematics } #endif InternalTranspose(); - fixed (float* p = &this.transpose.m00) + fixed (float* p = &transpose.m00) { - ulong pbit = ((ulong)p) + (((ulong)sizeof(Vector4)) * ((ulong)rowNum)) + (((ulong)sizeof(float)) * ((ulong)columNum)); + ulong pbit = (ulong)p + (ulong)sizeof(Vector4) * (ulong)rowNum + sizeof(float) * (ulong)columNum; *(float*)pbit = value; } isTransposed = false; @@ -147,7 +147,7 @@ namespace StgSharp.Mathematics get { InternalTranspose(); - return new Matrix44(this.transpose); + return new Matrix44(transpose); } } @@ -169,7 +169,7 @@ namespace StgSharp.Mathematics public override bool Equals(object? obj) { - return (obj is Matrix44 x) && Equals(x); + return obj is Matrix44 x && Equals(x); } public bool Equals(Matrix44 other) @@ -197,6 +197,8 @@ namespace StgSharp.Mathematics { fixed (ColumnSet4* source = &mat, target = &transpose) { + InternalIO.Intrinsic.f32_transpose(source, target); + // InternalIO.Intrinsic.transpose44(source, target); } isTransposed = true; diff --git a/StgSharp.Common/Mathematics/Linear/Vec2.cs b/StgSharp.Common/Mathematics/Graphic/Vec2.cs similarity index 96% rename from StgSharp.Common/Mathematics/Linear/Vec2.cs rename to StgSharp.Common/Mathematics/Graphic/Vec2.cs index 039fa5cc..8abc3e3f 100644 --- a/StgSharp.Common/Mathematics/Linear/Vec2.cs +++ b/StgSharp.Common/Mathematics/Graphic/Vec2.cs @@ -29,20 +29,20 @@ // ----------------------------------------------------------------------- // ----------------------------------------------------------------------- using StgSharp.HighPerformance; - +using StgSharp.Mathematics.Numeric; using System.Net.Http.Headers; using System.Numerics; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; -namespace StgSharp.Mathematics +namespace StgSharp.Mathematics.Graphic { /// /// A two dimension vector defined by two elements. Vec2 in World are default used as colum /// vector. /// [StructLayout(LayoutKind.Explicit, Size = 16, Pack = 16)] - public struct Vec2 : IFixedVector + public struct Vec2 : IUnmanagedVector { [FieldOffset(0)] internal unsafe fixed float num[2]; @@ -108,7 +108,7 @@ namespace StgSharp.Mathematics [MethodImpl(MethodImplOptions.AggressiveInlining)] public float Cross(Vec2 right) { - return (X * right.Y) - (Y * right.X); + return X * right.Y - Y * right.X; } [MethodImpl(MethodImplOptions.AggressiveInlining)] diff --git a/StgSharp.Common/Mathematics/Linear/Vec3.cs b/StgSharp.Common/Mathematics/Graphic/Vec3.cs similarity index 95% rename from StgSharp.Common/Mathematics/Linear/Vec3.cs rename to StgSharp.Common/Mathematics/Graphic/Vec3.cs index 31efee34..49876363 100644 --- a/StgSharp.Common/Mathematics/Linear/Vec3.cs +++ b/StgSharp.Common/Mathematics/Graphic/Vec3.cs @@ -29,6 +29,7 @@ // ----------------------------------------------------------------------- // ----------------------------------------------------------------------- using StgSharp.HighPerformance; +using StgSharp.Mathematics.Numeric; using System; using System.Collections; using System.Collections.Generic; @@ -36,11 +37,11 @@ using System.Numerics; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; -namespace StgSharp.Mathematics +namespace StgSharp.Mathematics.Graphic { - [CollectionBuilder(typeof(Vec3), nameof(Vec3.FromSpan))] + [CollectionBuilder(typeof(Vec3), nameof(FromSpan))] [StructLayout(LayoutKind.Explicit, Size = 16, Pack = 16)] - public struct Vec3 : IEquatable, IFixedVector, IEnumerable + public struct Vec3 : IEquatable, IUnmanagedVector, IEnumerable { [FieldOffset(0)] internal unsafe fixed float num[3]; @@ -93,7 +94,7 @@ namespace StgSharp.Mathematics public unsafe Vec2 XY { - get => new Vec2(this.reg); + get => new Vec2(reg); set { fixed (Vector3* vptr = &v) { @@ -132,7 +133,7 @@ namespace StgSharp.Mathematics public bool Equals(Vec3 other) { - return this.reg == other.reg; + return reg == other.reg; } public override bool Equals(object obj) @@ -171,8 +172,8 @@ namespace StgSharp.Mathematics public float GetMaxValue() { float ret = X; - ret = (ret > Y) ? ret : Y; - ret = (ret > Z) ? ret : Z; + ret = ret > Y ? ret : Y; + ret = ret > Z ? ret : Z; return ret; } diff --git a/StgSharp.Common/Mathematics/Linear/Vec4.cs b/StgSharp.Common/Mathematics/Graphic/Vec4.cs similarity index 96% rename from StgSharp.Common/Mathematics/Linear/Vec4.cs rename to StgSharp.Common/Mathematics/Graphic/Vec4.cs index 3e790024..1d02900d 100644 --- a/StgSharp.Common/Mathematics/Linear/Vec4.cs +++ b/StgSharp.Common/Mathematics/Graphic/Vec4.cs @@ -30,18 +30,17 @@ // ----------------------------------------------------------------------- using StgSharp.HighPerformance; using StgSharp.Internal.Intrinsic; -using StgSharp.Mathematics; - +using StgSharp.Mathematics.Numeric; using System; using System.Data.Common; using System.Numerics; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; -namespace StgSharp.Mathematics +namespace StgSharp.Mathematics.Graphic { [StructLayout(LayoutKind.Explicit, Size = 16, Pack = 16)] - public struct Vec4 : IEquatable, IFixedVector + public struct Vec4 : IEquatable, IUnmanagedVector { [FieldOffset(0)] internal unsafe fixed float num[4]; @@ -131,7 +130,7 @@ namespace StgSharp.Mathematics public override bool Equals(object obj) { - return (obj is Vec4 v) ? (v == this) : false; + return obj is Vec4 v ? v == this : false; } public override int GetHashCode() diff --git a/StgSharp.Common/Mathematics/Graphic/Vector.Cross.cs b/StgSharp.Common/Mathematics/Graphic/Vector.Cross.cs new file mode 100644 index 00000000..e68a2baf --- /dev/null +++ b/StgSharp.Common/Mathematics/Graphic/Vector.Cross.cs @@ -0,0 +1,52 @@ +//----------------------------------------------------------------------- +// ----------------------------------------------------------------------- +// file="Vector.Cross.cs" +// Project: World +// AuthorGroup: Nitload Space +// Copyright (c) Nitload Space. All rights reserved. +// +// Permission is hereby granted, free of charge, to any person +// obtaining a copy of this software and associated documentation +// files (the “Software”), to deal in the Software without restriction, +// including without limitation the rights to use, copy, modify, merge, +// publish, distribute, sublicense, and/or sell copies of the Software, +// and to permit persons to whom the Software is furnished to do so, +// subject to the following conditions: +// +// The above copyright notice and +// this permission notice shall be included in all copies +// or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED “AS IS”, +// WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, +// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. +// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, +// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, +// ARISING FROM, OUT OF OR IN CONNECTION WITH +// THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +// +// ----------------------------------------------------------------------- +// ----------------------------------------------------------------------- +using StgSharp.Mathematics.Graphic; + +namespace StgSharp.Mathematics.Graphic +{ + public static unsafe partial class Linear + { + + public static float Cross(Vec2 vec1, Vec2 vec2) + { + return (vec1.X * vec2.Y) - (vec1.Y * vec2.X); + } + + public static Vec3 Cross(Vec3 vec1, Vec3 vec2) + { + return new Vec3( + (vec1.Y * vec2.Z) - (vec1.Z * vec2.Y), + (vec1.X * vec2.Z) - (vec1.Z * vec2.X), + (vec1.X * vec2.Y) - (vec1.Y * vec2.X)); + } + + } +} diff --git a/StgSharp.Common/Mathematics/HighPrecision/Vec4HP.cs b/StgSharp.Common/Mathematics/Graphic/Vector.GetAngle.cs similarity index 58% rename from StgSharp.Common/Mathematics/HighPrecision/Vec4HP.cs rename to StgSharp.Common/Mathematics/Graphic/Vector.GetAngle.cs index f62b6a6e..32578f4e 100644 --- a/StgSharp.Common/Mathematics/HighPrecision/Vec4HP.cs +++ b/StgSharp.Common/Mathematics/Graphic/Vector.GetAngle.cs @@ -1,6 +1,6 @@ //----------------------------------------------------------------------- // ----------------------------------------------------------------------- -// file="Vec4HP.cs" +// file="Vector.GetAngle.cs" // Project: StgSharp // AuthorGroup: Nitload Space // Copyright (c) Nitload Space. All rights reserved. @@ -28,43 +28,23 @@ // // ----------------------------------------------------------------------- // ----------------------------------------------------------------------- -using StgSharp.HighPerformance; +using StgSharp.Mathematics.Graphic; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Runtime.CompilerServices; -using System.Runtime.InteropServices; -using System.Runtime.Intrinsics; -using System.Text; -using System.Threading.Tasks; - -namespace StgSharp.Mathematics.HighPrecision +namespace StgSharp.Mathematics.Graphic { - [StructLayout(LayoutKind.Explicit, Pack =16)] - public struct Vec4HP : IFixedVector + public static unsafe partial class Linear { - [FieldOffset(0)] private M256 buffer; - [FieldOffset(0)] private Vector256 clrBuffer; - [FieldOffset(3 * sizeof(double))] public double W; - [FieldOffset(0 * sizeof(double))] public double X; - [FieldOffset(1 * sizeof(double))] public double Y; - [FieldOffset(2 * sizeof(double))] public double Z; - - public Vec4HP(double x, double y, double z, double w) + public static float GetAngle(Vec2 vec1, Vec2 vec2) { - this.X = x; - this.Y = y; - this.Z = z; - this.W = w; - Unsafe.SkipInit(out buffer); - Unsafe.SkipInit(out clrBuffer); + return Scaler.ACos( + 1 / (vec1.GetLength() * vec2.GetLength())); } - public static Vec4HP Zero => new Vec4HP(0, 0, 0, 0); - - public static Vec4HP One => new Vec4HP(1, 1, 1, 1); + public static float GetAngle(Vec3 vec1, Vec3 vec2) + { + return Scaler.ACos((vec1 * vec2) / (vec1.GetLength() * vec2.GetLength())); + } } } diff --git a/StgSharp.Common/Mathematics/Graphic/Vector.GetLength.cs b/StgSharp.Common/Mathematics/Graphic/Vector.GetLength.cs new file mode 100644 index 00000000..a0079c1c --- /dev/null +++ b/StgSharp.Common/Mathematics/Graphic/Vector.GetLength.cs @@ -0,0 +1,49 @@ +//----------------------------------------------------------------------- +// ----------------------------------------------------------------------- +// file="Vector.GetLength.cs" +// Project: StgSharp +// AuthorGroup: Nitload Space +// Copyright (c) Nitload Space. All rights reserved. +// +// Permission is hereby granted, free of charge, to any person +// obtaining a copy of this software and associated documentation +// files (the “Software”), to deal in the Software without restriction, +// including without limitation the rights to use, copy, modify, merge, +// publish, distribute, sublicense, and/or sell copies of the Software, +// and to permit persons to whom the Software is furnished to do so, +// subject to the following conditions: +// +// The above copyright notice and +// this permission notice shall be included in all copies +// or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED “AS IS”, +// WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, +// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. +// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, +// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, +// ARISING FROM, OUT OF OR IN CONNECTION WITH +// THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +// +// ----------------------------------------------------------------------- +// ----------------------------------------------------------------------- +using StgSharp.Mathematics.Graphic; + +namespace StgSharp.Mathematics.Graphic +{ + public static unsafe partial class Linear + { + + public static float GetLength(this Vec3 vec) + { + return Scaler.Sqrt(vec.Dot(vec)); + } + + public static float GetLength(this Vec2 vec) + { + return Scaler.Sqrt(vec.Dot(vec)); + } + + } +} diff --git a/StgSharp.Common/Mathematics/HighPrecision/Vec3HP.cs b/StgSharp.Common/Mathematics/Graphic/Vector.Normalize.cs similarity index 52% rename from StgSharp.Common/Mathematics/HighPrecision/Vec3HP.cs rename to StgSharp.Common/Mathematics/Graphic/Vector.Normalize.cs index c6dc8697..3070d9dc 100644 --- a/StgSharp.Common/Mathematics/HighPrecision/Vec3HP.cs +++ b/StgSharp.Common/Mathematics/Graphic/Vector.Normalize.cs @@ -1,6 +1,6 @@ //----------------------------------------------------------------------- // ----------------------------------------------------------------------- -// file="Vec3HP.cs" +// file="Vector.Normalize.cs" // Project: StgSharp // AuthorGroup: Nitload Space // Copyright (c) Nitload Space. All rights reserved. @@ -28,58 +28,57 @@ // // ----------------------------------------------------------------------- // ----------------------------------------------------------------------- -using StgSharp.HighPerformance; +using StgSharp.Mathematics.Graphic; +using System.Numerics; -using System; -using System.Collections.Generic; -using System.Drawing; -using System.Linq; -using System.Runtime.CompilerServices; -using System.Runtime.InteropServices; -using System.Runtime.Intrinsics; -using System.Text; -using System.Threading.Tasks; - -namespace StgSharp.Mathematics.HighPrecision +namespace StgSharp.Mathematics.Graphic { - [StructLayout(LayoutKind.Explicit, Pack = 16)] - public struct Vec3HP : IFixedVector + public static partial class Linear { - [FieldOffset(0)] internal M256 buffer; - [FieldOffset(0)] internal Vector256 clrBuffer; - [FieldOffset(0 * sizeof(double))] public double X; - [FieldOffset(1 * sizeof(double))] public double Y; - [FieldOffset(2 * sizeof(double))] public double Z; - - public Vec3HP() : this(0, 0, 0) { } - - public Vec3HP(Vector256 clrBuffer) + public static Vec4 Normalize(Vec4 vec) { - this.X = 0; - this.Y = 0; - this.Z = 0; - Unsafe.SkipInit(out buffer); - Unsafe.SkipInit(out this.clrBuffer); - this.clrBuffer = clrBuffer; + return new Vec4 + { + vec = Vector4.Normalize(vec.vec) + }; } - public Vec3HP(double x, double y, double z) + public static Vec3 Normalize(Vec3 vec) { - this.X = x; - this.Y = y; - this.Z = z; - Unsafe.SkipInit(out buffer); - Unsafe.SkipInit(out clrBuffer); + return new Vec3 + { + v = Vector3.Normalize(vec.v) + }; } - public static Vec3HP Zero => new Vec3HP(1, 1, 1); - - public static Vec3HP One => throw new NotImplementedException(); - - public override string ToString() + public static unsafe void Normalize(ref Vec4 source, ref Vec4 target) { - return $"Double precision 3D Vector<{X},{Y},{Z}>"; + 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; + } + + public static unsafe void Normalize(ref Vec3 source, ref Vec3 target) + { + Vector3 s = source.v; + Vector3 t = target.v; + + s = Vector3.Normalize(s); + Vector3 projection = Vector3.Dot(t, s) / Vector3.Dot(s, s) * s; + t -= projection; + t = Vector3.Normalize(t); + + source.v = s; + target.v = t; } } diff --git a/StgSharp.Common/Mathematics/Graphic/Vector.Orthogonalize.cs b/StgSharp.Common/Mathematics/Graphic/Vector.Orthogonalize.cs new file mode 100644 index 00000000..0dde5cde --- /dev/null +++ b/StgSharp.Common/Mathematics/Graphic/Vector.Orthogonalize.cs @@ -0,0 +1,95 @@ +//----------------------------------------------------------------------- +// ----------------------------------------------------------------------- +// file="Vector.Orthogonalize.cs" +// Project: World +// AuthorGroup: Nitload Space +// Copyright (c) Nitload Space. All rights reserved. +// +// Permission is hereby granted, free of charge, to any person +// obtaining a copy of this software and associated documentation +// files (the “Software”), to deal in the Software without restriction, +// including without limitation the rights to use, copy, modify, merge, +// publish, distribute, sublicense, and/or sell copies of the Software, +// and to permit persons to whom the Software is furnished to do so, +// subject to the following conditions: +// +// The above copyright notice and +// this permission notice shall be included in all copies +// or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED “AS IS”, +// WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, +// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. +// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, +// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, +// ARISING FROM, OUT OF OR IN CONNECTION WITH +// THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +// +// ----------------------------------------------------------------------- +// ----------------------------------------------------------------------- +using StgSharp.Mathematics.Graphic; +using System.Numerics; + +namespace StgSharp.Mathematics.Graphic +{ + public static partial class Linear + { + + public static Vec4 Orthogonalize(this Vec4 vec) + { + return new Vec4 + { + vec = Vector4.Normalize(vec.vec) + }; + } + + public static Vec3 Orthogonalize(Vec3 vec) + { + return new Vec3 + { + v = Vector3.Normalize(vec.v) + }; + } + + public static unsafe void Orthogonalize(this Vec4 source, ref Vec4 target) + { + 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; + } + + /// + /// Orthogonalize two s, let their length be 1 and perpendicular to + /// each other. + /// + /// + /// + /// + /// + /// + /// + public static unsafe void Orthogonalize(this Vec3 source, ref Vec3 target) + { + Vector3 s = source.v; + Vector3 t = target.v; + + s = Vector3.Normalize(s); + Vector3 projection = Vector3.Dot(t, s) / Vector3.Dot(s, s) * s; + t -= projection; + t = Vector3.Normalize(t); + + source.v = s; + target.v = t; + } + + } +} diff --git a/StgSharp.Common/Mathematics/Linear/Cordination.cs b/StgSharp.Common/Mathematics/Linear/Cordination.cs index 812796a5..1761ad62 100644 --- a/StgSharp.Common/Mathematics/Linear/Cordination.cs +++ b/StgSharp.Common/Mathematics/Linear/Cordination.cs @@ -28,7 +28,7 @@ // //----------------------------------------------------------------------- //----------------------------------------------------------------------- -namespace StgSharp.Mathematics +namespace StgSharp.Mathematics.Numeric { public enum CoordinatePlain { diff --git a/StgSharp.Common/Mathematics/Linear/IMatrix.cs b/StgSharp.Common/Mathematics/Linear/IMatrix.cs index 8a38cc5d..0730cc93 100644 --- a/StgSharp.Common/Mathematics/Linear/IMatrix.cs +++ b/StgSharp.Common/Mathematics/Linear/IMatrix.cs @@ -1,40 +1,40 @@ //----------------------------------------------------------------------- -//----------------------------------------------------------------------- -// file="IMatrix.cs" -// Project: StgSharp -// AuthorGroup: Nitload Space -// Copyright (c) Nitload Space. All rights reserved. +// ----------------------------------------------------------------------- +// file="IMatrix.cs" +// Project: StgSharp +// AuthorGroup: Nitload Space +// Copyright (c) Nitload Space. All rights reserved. // -// Permission is hereby granted, free of charge, to any person -// obtaining a copy of this software and associated documentation -// files (the “Software”), to deal in the Software without restriction, -// including without limitation the rights to use, copy, modify, merge, -// publish, distribute, sublicense, and/or sell copies of the Software, -// and to permit persons to whom the Software is furnished to do so, -// subject to the following conditions: +// Permission is hereby granted, free of charge, to any person +// obtaining a copy of this software and associated documentation +// files (the “Software”), to deal in the Software without restriction, +// including without limitation the rights to use, copy, modify, merge, +// publish, distribute, sublicense, and/or sell copies of the Software, +// and to permit persons to whom the Software is furnished to do so, +// subject to the following conditions: // -// The above copyright notice and -// this permission notice shall be included in all copies -// or substantial portions of the Software. +// The above copyright notice and +// this permission notice shall be included in all copies +// or substantial portions of the Software. // -// THE SOFTWARE IS PROVIDED “AS IS”, -// WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, -// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. -// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, -// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, -// ARISING FROM, OUT OF OR IN CONNECTION WITH -// THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +// THE SOFTWARE IS PROVIDED “AS IS”, +// WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, +// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. +// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, +// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, +// ARISING FROM, OUT OF OR IN CONNECTION WITH +// THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. // -//----------------------------------------------------------------------- -//----------------------------------------------------------------------- +// ----------------------------------------------------------------------- +// ----------------------------------------------------------------------- using StgSharp.Graphics; using System; using System.Collections.Generic; using System.Text; -namespace StgSharp.Mathematics +namespace StgSharp.Mathematics.Numeric { public interface IMatrix where TSelf: IMatrix { } } diff --git a/StgSharp.Common/Mathematics/Linear/IFixedVector.cs b/StgSharp.Common/Mathematics/Linear/IUnmanagedVector.cs similarity index 93% rename from StgSharp.Common/Mathematics/Linear/IFixedVector.cs rename to StgSharp.Common/Mathematics/Linear/IUnmanagedVector.cs index 16cb4df6..47db6a14 100644 --- a/StgSharp.Common/Mathematics/Linear/IFixedVector.cs +++ b/StgSharp.Common/Mathematics/Linear/IUnmanagedVector.cs @@ -36,9 +36,9 @@ using System.Runtime.CompilerServices; using System.Text; using System.Threading.Tasks; -namespace StgSharp.Mathematics +namespace StgSharp.Mathematics.Numeric { - public interface IFixedVector where TSelf: unmanaged, IFixedVector + public interface IUnmanagedVector where TSelf: unmanaged, IUnmanagedVector { public static abstract TSelf Zero { get; } diff --git a/StgSharp.Common/Mathematics/Linear/Matrix.Basic.cs b/StgSharp.Common/Mathematics/Linear/Matrix.Basic.cs deleted file mode 100644 index 91c914aa..00000000 --- a/StgSharp.Common/Mathematics/Linear/Matrix.Basic.cs +++ /dev/null @@ -1,86 +0,0 @@ -//----------------------------------------------------------------------- -// ----------------------------------------------------------------------- -// file="Matrix.Basic.cs" -// Project: StgSharp -// AuthorGroup: Nitload Space -// Copyright (c) Nitload Space. All rights reserved. -// -// Permission is hereby granted, free of charge, to any person -// obtaining a copy of this software and associated documentation -// files (the “Software”), to deal in the Software without restriction, -// including without limitation the rights to use, copy, modify, merge, -// publish, distribute, sublicense, and/or sell copies of the Software, -// and to permit persons to whom the Software is furnished to do so, -// subject to the following conditions: -// -// The above copyright notice and -// this permission notice shall be included in all copies -// or substantial portions of the Software. -// -// THE SOFTWARE IS PROVIDED “AS IS”, -// WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, -// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. -// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, -// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, -// ARISING FROM, OUT OF OR IN CONNECTION WITH -// THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -// -// ----------------------------------------------------------------------- -// ----------------------------------------------------------------------- -using System; -using System.Collections.Generic; -using System.Linq; -using System.Security.AccessControl; -using System.Text; -using System.Threading.Tasks; - -namespace StgSharp.Mathematics -{ - public static class Matrix - { - - public static Matrix Add(Matrix left, Matrix right) - { - if (left.ColumnLength != right.ColumnLength || left.RowLength != right.RowLength) { - throw new IndexOutOfRangeException("Matrix dimensions must agree."); - } - MatrixParallel.LeadParallel(); - try - { - Matrix result = GCMatrix.FromDefault(left.ColumnLength, left.RowLength); - unsafe - { - MatrixParallelTaskPackage* handle = MatrixParallelFactory.CreateBaseTask(); - handle->Left = left.Buffer; - handle->Right = right.Buffer; - handle->Result = result.Buffer; - handle->ComputeHandle = (IntPtr)InternalIO.Intrinsic.f32_add; - handle->LeftPrimOffset = 0; - handle->RightPrimOffset = 0; - handle->LeftPrimStride = left.KernelColumnLength; - handle->LeftSecOffset = 1; - handle->LeftPrimOffset = 0; - handle->RightPrimOffset = 0; - handle->RightPrimStride = right.KernelColumnLength; - handle->RightSecOffset = 1; - handle->RightPrimOffset = 0; - handle->ResultPrimOffset = 0; - handle->ResultPrimStride = result.KernelColumnLength; - handle->ResultSecOffset = 1; - handle->ResultPrimOffset = 0; - handle->PrimCount = left.KernelRowLength; - handle->SecCount = left.KernelColumnLength; - } - - MatrixParallel.LaunchParallel(SleepMode.DeepSleep); - return result; - } - finally - { - MatrixParallel.EndParallel(); - } - } - - } -} diff --git a/StgSharp.Common/Mathematics/Linear/Matrix.GC.cs b/StgSharp.Common/Mathematics/Linear/Matrix.GC.cs index c23ffb8e..2cb25aaf 100644 --- a/StgSharp.Common/Mathematics/Linear/Matrix.GC.cs +++ b/StgSharp.Common/Mathematics/Linear/Matrix.GC.cs @@ -39,7 +39,7 @@ using System.Runtime.InteropServices; using System.Text; using System.Threading.Tasks; -namespace StgSharp.Mathematics +namespace StgSharp.Mathematics.Numeric { internal unsafe partial class GCMatrix : Matrix where T: unmanaged, INumber { diff --git a/StgSharp.Common/Mathematics/Linear/Matrix.HLSF.cs b/StgSharp.Common/Mathematics/Linear/Matrix.HLSF.cs index 07b1683f..14740ee0 100644 --- a/StgSharp.Common/Mathematics/Linear/Matrix.HLSF.cs +++ b/StgSharp.Common/Mathematics/Linear/Matrix.HLSF.cs @@ -40,7 +40,7 @@ using System.Threading.Tasks; using hlsfAllocator = global::StgSharp.HighPerformance.Memory.HybridLayerSegregatedFitAllocator; using hlsfHandle = global::StgSharp.HighPerformance.Memory.HybridLayerSegregatedFitAllocationHandle; -namespace StgSharp.Mathematics +namespace StgSharp.Mathematics.Numeric { internal unsafe partial class HlsfMatrix : Matrix where T: unmanaged, INumber { diff --git a/StgSharp.Common/Mathematics/Linear/Matrix.cs b/StgSharp.Common/Mathematics/Linear/Matrix.cs index d61b6dd2..25a6e716 100644 --- a/StgSharp.Common/Mathematics/Linear/Matrix.cs +++ b/StgSharp.Common/Mathematics/Linear/Matrix.cs @@ -38,7 +38,7 @@ using hlsfAllocator = global::StgSharp.HighPerformance.Memory.HybridLayerSegrega // using hlsfHandle = global::StgSharp.HighPerformance.Memory.HybridLayerSegregatedFitAllocationHandle; -namespace StgSharp.Mathematics +namespace StgSharp.Mathematics.Numeric { public abstract unsafe partial class Matrix where T: unmanaged, INumber { @@ -147,13 +147,13 @@ namespace StgSharp.Mathematics { private readonly MatrixKernel* _source = source; - internal readonly int SecondaryStride = secondaryStride; internal readonly int PrimOffset = primOffset; internal readonly int PrimSize = primSize; internal readonly int PrimStride = primStride; internal readonly int SecondaryOffset = secondaryOffset; internal readonly int SecondarySize = secondarySize; + internal readonly int SecondaryStride = secondaryStride; internal readonly MatrixKernel* Source { diff --git a/StgSharp.Common/Mathematics/Linear/MatrixEnum.cs b/StgSharp.Common/Mathematics/Linear/MatrixEnum.cs index 5f6521c0..588a98f1 100644 --- a/StgSharp.Common/Mathematics/Linear/MatrixEnum.cs +++ b/StgSharp.Common/Mathematics/Linear/MatrixEnum.cs @@ -34,7 +34,7 @@ using System.Linq; using System.Text; using System.Threading.Tasks; -namespace StgSharp.Mathematics +namespace StgSharp.Mathematics.Numeric { public enum MatrixAllocation { diff --git a/StgSharp.Common/Mathematics/Linear/MatrixIndex.cs b/StgSharp.Common/Mathematics/Linear/MatrixIndex.cs index 0012e3e0..205e8cd5 100644 --- a/StgSharp.Common/Mathematics/Linear/MatrixIndex.cs +++ b/StgSharp.Common/Mathematics/Linear/MatrixIndex.cs @@ -29,14 +29,10 @@ // ----------------------------------------------------------------------- // ----------------------------------------------------------------------- using System; -using System.Collections.Generic; -using System.Linq; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; -using System.Text; -using System.Threading.Tasks; -namespace StgSharp.Mathematics +namespace StgSharp.Mathematics.Numeric { [StructLayout(LayoutKind.Sequential)] public readonly struct MatrixIndex diff --git a/StgSharp.Common/Mathematics/Linear/Vector.Cross.cs b/StgSharp.Common/Mathematics/Linear/Vector.Cross.cs deleted file mode 100644 index 6f763390..00000000 --- a/StgSharp.Common/Mathematics/Linear/Vector.Cross.cs +++ /dev/null @@ -1,50 +0,0 @@ -//----------------------------------------------------------------------- -//----------------------------------------------------------------------- -// file="Vector.Cross.cs" -// Project: World -// AuthorGroup: Nitload Space -// Copyright (c) Nitload Space. All rights reserved. -// -// Permission is hereby granted, free of charge, to any person -// obtaining a copy of this software and associated documentation -// files (the “Software”), to deal in the Software without restriction, -// including without limitation the rights to use, copy, modify, merge, -// publish, distribute, sublicense, and/or sell copies of the Software, -// and to permit persons to whom the Software is furnished to do so, -// subject to the following conditions: -// -// The above copyright notice and -// this permission notice shall be included in all copies -// or substantial portions of the Software. -// -// THE SOFTWARE IS PROVIDED “AS IS”, -// WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, -// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. -// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, -// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, -// ARISING FROM, OUT OF OR IN CONNECTION WITH -// THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -// -//----------------------------------------------------------------------- -//----------------------------------------------------------------------- -namespace StgSharp.Mathematics -{ - public static unsafe partial class Linear - { - - public static float Cross( Vec2 vec1, Vec2 vec2 ) - { - return ( vec1.X * vec2.Y ) - ( vec1.Y * vec2.X ); - } - - public static Vec3 Cross( Vec3 vec1, Vec3 vec2 ) - { - return new Vec3( - ( vec1.Y * vec2.Z ) - ( vec1.Z * vec2.Y ), - ( vec1.X * vec2.Z ) - ( vec1.Z * vec2.X ), - ( vec1.X * vec2.Y ) - ( vec1.Y * vec2.X ) ); - } - - } -} diff --git a/StgSharp.Common/Mathematics/Linear/Vector.GetAngle.cs b/StgSharp.Common/Mathematics/Linear/Vector.GetAngle.cs deleted file mode 100644 index bc2fff14..00000000 --- a/StgSharp.Common/Mathematics/Linear/Vector.GetAngle.cs +++ /dev/null @@ -1,50 +0,0 @@ -//----------------------------------------------------------------------- -//----------------------------------------------------------------------- -// file="Vector.GetAngle.cs" -// Project: StgSharp -// AuthorGroup: Nitload Space -// Copyright (c) Nitload Space. All rights reserved. -// -// Permission is hereby granted, free of charge, to any person -// obtaining a copy of this software and associated documentation -// files (the “Software”), to deal in the Software without restriction, -// including without limitation the rights to use, copy, modify, merge, -// publish, distribute, sublicense, and/or sell copies of the Software, -// and to permit persons to whom the Software is furnished to do so, -// subject to the following conditions: -// -// The above copyright notice and -// this permission notice shall be included in all copies -// or substantial portions of the Software. -// -// THE SOFTWARE IS PROVIDED “AS IS”, -// WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, -// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. -// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, -// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, -// ARISING FROM, OUT OF OR IN CONNECTION WITH -// THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -// -//----------------------------------------------------------------------- -//----------------------------------------------------------------------- -namespace StgSharp.Mathematics -{ - public static unsafe partial class Linear - { - - public static float GetAngle( Vec2 vec1, Vec2 vec2 ) - { - return Scaler.ACos( - 1 - //vec1.multiple(vec2) - / ( vec1.GetLength() * vec2.GetLength() ) ); - } - - public static float GetAngle( Vec3 vec1, Vec3 vec2 ) - { - return Scaler.ACos( ( vec1 * vec2 ) / ( vec1.GetLength() * vec2.GetLength() ) ); - } - - } -} diff --git a/StgSharp.Common/Mathematics/Linear/Vector.GetLength.cs b/StgSharp.Common/Mathematics/Linear/Vector.GetLength.cs deleted file mode 100644 index 899ac126..00000000 --- a/StgSharp.Common/Mathematics/Linear/Vector.GetLength.cs +++ /dev/null @@ -1,47 +0,0 @@ -//----------------------------------------------------------------------- -//----------------------------------------------------------------------- -// file="Vector.GetLength.cs" -// Project: StgSharp -// AuthorGroup: Nitload Space -// Copyright (c) Nitload Space. All rights reserved. -// -// Permission is hereby granted, free of charge, to any person -// obtaining a copy of this software and associated documentation -// files (the “Software”), to deal in the Software without restriction, -// including without limitation the rights to use, copy, modify, merge, -// publish, distribute, sublicense, and/or sell copies of the Software, -// and to permit persons to whom the Software is furnished to do so, -// subject to the following conditions: -// -// The above copyright notice and -// this permission notice shall be included in all copies -// or substantial portions of the Software. -// -// THE SOFTWARE IS PROVIDED “AS IS”, -// WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, -// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. -// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, -// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, -// ARISING FROM, OUT OF OR IN CONNECTION WITH -// THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -// -//----------------------------------------------------------------------- -//----------------------------------------------------------------------- -namespace StgSharp.Mathematics -{ - public static unsafe partial class Linear - { - - public static float GetLength( this Vec3 vec ) - { - return Scaler.Sqrt( vec.Dot( vec ) ); - } - - public static float GetLength( this Vec2 vec ) - { - return Scaler.Sqrt( vec.Dot( vec ) ); - } - - } -} diff --git a/StgSharp.Common/Mathematics/Linear/Vector.Normalize.cs b/StgSharp.Common/Mathematics/Linear/Vector.Normalize.cs deleted file mode 100644 index fbe5b82f..00000000 --- a/StgSharp.Common/Mathematics/Linear/Vector.Normalize.cs +++ /dev/null @@ -1,94 +0,0 @@ -//----------------------------------------------------------------------- -//----------------------------------------------------------------------- -// file="Vector.Normalize.cs" -// Project: StgSharp -// AuthorGroup: Nitload Space -// Copyright (c) Nitload Space. All rights reserved. -// -// Permission is hereby granted, free of charge, to any person -// obtaining a copy of this software and associated documentation -// files (the “Software”), to deal in the Software without restriction, -// including without limitation the rights to use, copy, modify, merge, -// publish, distribute, sublicense, and/or sell copies of the Software, -// and to permit persons to whom the Software is furnished to do so, -// subject to the following conditions: -// -// The above copyright notice and -// this permission notice shall be included in all copies -// or substantial portions of the Software. -// -// THE SOFTWARE IS PROVIDED “AS IS”, -// WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, -// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. -// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, -// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, -// ARISING FROM, OUT OF OR IN CONNECTION WITH -// THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -// -//----------------------------------------------------------------------- -//----------------------------------------------------------------------- -using StgSharp.Internal; -using StgSharp.Mathematics; - -using System; -using System.Collections.Generic; -using System.Linq; -using System.Numerics; -using System.Runtime.InteropServices; -using System.Text; -using System.Threading.Tasks; - -namespace StgSharp.Mathematics -{ - public static partial class Linear - { - - public static Vec4 Normalize( Vec4 vec ) - { - return new Vec4 - { - vec = Vector4.Normalize( vec.vec ) - }; - } - - public static Vec3 Normalize( Vec3 vec ) - { - return new Vec3 - { - v = Vector3.Normalize( vec.v ) - }; - } - - public static unsafe void Normalize( ref Vec4 source, ref Vec4 target ) - { - Vector4 s = source.vec; - Vector4 t = target.vec; - - s = Vector4.Normalize( s ); - - float dotProduct = Vector4.Dot( s, t ); - Vector4 projection = Vector4.Multiply( s, t ); - - t -= projection; - - source.vec = s; - target.vec = t; - } - - public static unsafe void Normalize( ref Vec3 source, ref Vec3 target ) - { - Vector3 s = source.v; - Vector3 t = target.v; - - s = Vector3.Normalize( s ); - Vector3 projection = ( Vector3.Dot( t, s ) / Vector3.Dot( s, s ) ) * s; - t -= projection; - t = Vector3.Normalize( t ); - - source.v = s; - target.v = t; - } - - } -} diff --git a/StgSharp.Common/Mathematics/Linear/Vector.Orthogonalize.cs b/StgSharp.Common/Mathematics/Linear/Vector.Orthogonalize.cs deleted file mode 100644 index 6799285a..00000000 --- a/StgSharp.Common/Mathematics/Linear/Vector.Orthogonalize.cs +++ /dev/null @@ -1,104 +0,0 @@ -//----------------------------------------------------------------------- -//----------------------------------------------------------------------- -// file="Vector.Orthogonalize.cs" -// Project: World -// AuthorGroup: Nitload Space -// Copyright (c) Nitload Space. All rights reserved. -// -// Permission is hereby granted, free of charge, to any person -// obtaining a copy of this software and associated documentation -// files (the “Software”), to deal in the Software without restriction, -// including without limitation the rights to use, copy, modify, merge, -// publish, distribute, sublicense, and/or sell copies of the Software, -// and to permit persons to whom the Software is furnished to do so, -// subject to the following conditions: -// -// The above copyright notice and -// this permission notice shall be included in all copies -// or substantial portions of the Software. -// -// THE SOFTWARE IS PROVIDED “AS IS”, -// WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, -// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. -// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, -// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, -// ARISING FROM, OUT OF OR IN CONNECTION WITH -// THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -// -//----------------------------------------------------------------------- -//----------------------------------------------------------------------- -using StgSharp.Internal.Intrinsic; -using StgSharp.Mathematics; - -using System; -using System.Collections.Generic; -using System.Linq; -using System.Numerics; -using System.Runtime.InteropServices; -using System.Text; -using System.Threading.Tasks; - -namespace StgSharp.Mathematics -{ - public static partial class Linear - { - - public static Vec4 Orthogonalize( Vec4 vec ) - { - return new Vec4 - { - vec = Vector4.Normalize( vec.vec ) - }; - } - - public static Vec3 Orthogonalize( Vec3 vec ) - { - return new Vec3 - { - v = Vector3.Normalize( vec.v ) - }; - } - - public static unsafe void Orthogonalize( ref Vec4 source, ref Vec4 target ) - { - Vector4 s = source.vec; - Vector4 t = target.vec; - - s = Vector4.Normalize( s ); - - float dotProduct = Vector4.Dot( s, t ); - Vector4 projection = Vector4.Multiply( s, t ); - - t -= projection; - - source.vec = s; - target.vec = t; - } - - /// - /// Orthogonalize two s, let their length be 1 and perpendicular to - /// each other. - /// - /// - /// - /// - /// - /// - /// - public static unsafe void Orthogonalize( ref Vec3 source, ref Vec3 target ) - { - Vector3 s = source.v; - Vector3 t = target.v; - - s = Vector3.Normalize( s ); - Vector3 projection = ( Vector3.Dot( t, s ) / Vector3.Dot( s, s ) ) * s; - t -= projection; - t = Vector3.Normalize( t ); - - source.v = s; - target.v = t; - } - - } -} diff --git a/StgSharp.Common/Mathematics/Scalar/ASin.cs b/StgSharp.Common/Mathematics/Scalar/ASin.cs index e25f8dc5..1067d241 100644 --- a/StgSharp.Common/Mathematics/Scalar/ASin.cs +++ b/StgSharp.Common/Mathematics/Scalar/ASin.cs @@ -1,33 +1,33 @@ //----------------------------------------------------------------------- -//----------------------------------------------------------------------- -// file="ASin.cs" -// Project: StgSharp -// AuthorGroup: Nitload Space -// Copyright (c) Nitload Space. All rights reserved. +// ----------------------------------------------------------------------- +// file="ASin.cs" +// Project: StgSharp +// AuthorGroup: Nitload Space +// Copyright (c) Nitload Space. All rights reserved. // -// Permission is hereby granted, free of charge, to any person -// obtaining a copy of this software and associated documentation -// files (the “Software”), to deal in the Software without restriction, -// including without limitation the rights to use, copy, modify, merge, -// publish, distribute, sublicense, and/or sell copies of the Software, -// and to permit persons to whom the Software is furnished to do so, -// subject to the following conditions: +// Permission is hereby granted, free of charge, to any person +// obtaining a copy of this software and associated documentation +// files (the “Software”), to deal in the Software without restriction, +// including without limitation the rights to use, copy, modify, merge, +// publish, distribute, sublicense, and/or sell copies of the Software, +// and to permit persons to whom the Software is furnished to do so, +// subject to the following conditions: // -// The above copyright notice and -// this permission notice shall be included in all copies -// or substantial portions of the Software. +// The above copyright notice and +// this permission notice shall be included in all copies +// or substantial portions of the Software. // -// THE SOFTWARE IS PROVIDED “AS IS”, -// WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, -// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. -// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, -// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, -// ARISING FROM, OUT OF OR IN CONNECTION WITH -// THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +// THE SOFTWARE IS PROVIDED “AS IS”, +// WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, +// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. +// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, +// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, +// ARISING FROM, OUT OF OR IN CONNECTION WITH +// THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. // -//----------------------------------------------------------------------- -//----------------------------------------------------------------------- +// ----------------------------------------------------------------------- +// ----------------------------------------------------------------------- using System; namespace StgSharp.Mathematics @@ -52,9 +52,9 @@ namespace StgSharp.Mathematics /// /// /// - public static float ASin( float x ) + public static float ASin(float x) { - return MathF.Asin( x ); + return MathF.Asin(x); /* int sign = 1; float y; diff --git a/StgSharp.Common/Mathematics/Scalar/Cos.cs b/StgSharp.Common/Mathematics/Scalar/Cos.cs index 5a72f156..b777e40e 100644 --- a/StgSharp.Common/Mathematics/Scalar/Cos.cs +++ b/StgSharp.Common/Mathematics/Scalar/Cos.cs @@ -1,33 +1,33 @@ //----------------------------------------------------------------------- -//----------------------------------------------------------------------- -// file="Cos.cs" -// Project: StgSharp -// AuthorGroup: Nitload Space -// Copyright (c) Nitload Space. All rights reserved. +// ----------------------------------------------------------------------- +// file="Cos.cs" +// Project: StgSharp +// AuthorGroup: Nitload Space +// Copyright (c) Nitload Space. All rights reserved. // -// Permission is hereby granted, free of charge, to any person -// obtaining a copy of this software and associated documentation -// files (the “Software”), to deal in the Software without restriction, -// including without limitation the rights to use, copy, modify, merge, -// publish, distribute, sublicense, and/or sell copies of the Software, -// and to permit persons to whom the Software is furnished to do so, -// subject to the following conditions: +// Permission is hereby granted, free of charge, to any person +// obtaining a copy of this software and associated documentation +// files (the “Software”), to deal in the Software without restriction, +// including without limitation the rights to use, copy, modify, merge, +// publish, distribute, sublicense, and/or sell copies of the Software, +// and to permit persons to whom the Software is furnished to do so, +// subject to the following conditions: // -// The above copyright notice and -// this permission notice shall be included in all copies -// or substantial portions of the Software. +// The above copyright notice and +// this permission notice shall be included in all copies +// or substantial portions of the Software. // -// THE SOFTWARE IS PROVIDED “AS IS”, -// WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, -// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. -// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, -// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, -// ARISING FROM, OUT OF OR IN CONNECTION WITH -// THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +// THE SOFTWARE IS PROVIDED “AS IS”, +// WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, +// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. +// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, +// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, +// ARISING FROM, OUT OF OR IN CONNECTION WITH +// THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. // -//----------------------------------------------------------------------- -//----------------------------------------------------------------------- +// ----------------------------------------------------------------------- +// ----------------------------------------------------------------------- using System; using System.Runtime.CompilerServices; @@ -36,8 +36,8 @@ namespace StgSharp.Mathematics public static partial class Scaler { - [MethodImpl( MethodImplOptions.AggressiveInlining )] - public static float Cos( float x ) + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static float Cos(float x) { /* float refer = x / 360.0f; @@ -53,7 +53,7 @@ namespace StgSharp.Mathematics return ((-sinData[a]) * ((0.1570796327f * refer) - (6.459640975e-4f * refer * refer2))) + (cosData[a] * ((1 - (0.0123370055f * refer2)) + (2.536695709e-5f * refer2 * refer2))); */ - return MathF.Cos( x ); + return MathF.Cos(x); } } diff --git a/StgSharp.Common/PlainGeometryCollisionSensor.cs b/StgSharp.Common/PlainGeometryCollisionSensor.cs index 17333911..9392bb24 100644 --- a/StgSharp.Common/PlainGeometryCollisionSensor.cs +++ b/StgSharp.Common/PlainGeometryCollisionSensor.cs @@ -1,43 +1,44 @@ //----------------------------------------------------------------------- -//----------------------------------------------------------------------- -// file="PlainGeometryCollisionSensor.cs" -// Project: StgSharp -// AuthorGroup: Nitload Space -// Copyright (c) Nitload Space. All rights reserved. +// ----------------------------------------------------------------------- +// file="PlainGeometryCollisionSensor.cs" +// Project: StgSharp +// AuthorGroup: Nitload Space +// Copyright (c) Nitload Space. All rights reserved. // -// Permission is hereby granted, free of charge, to any person -// obtaining a copy of this software and associated documentation -// files (the “Software”), to deal in the Software without restriction, -// including without limitation the rights to use, copy, modify, merge, -// publish, distribute, sublicense, and/or sell copies of the Software, -// and to permit persons to whom the Software is furnished to do so, -// subject to the following conditions: +// Permission is hereby granted, free of charge, to any person +// obtaining a copy of this software and associated documentation +// files (the “Software”), to deal in the Software without restriction, +// including without limitation the rights to use, copy, modify, merge, +// publish, distribute, sublicense, and/or sell copies of the Software, +// and to permit persons to whom the Software is furnished to do so, +// subject to the following conditions: // -// The above copyright notice and -// this permission notice shall be included in all copies -// or substantial portions of the Software. +// The above copyright notice and +// this permission notice shall be included in all copies +// or substantial portions of the Software. // -// THE SOFTWARE IS PROVIDED “AS IS”, -// WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, -// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. -// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, -// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, -// ARISING FROM, OUT OF OR IN CONNECTION WITH -// THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +// THE SOFTWARE IS PROVIDED “AS IS”, +// WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, +// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. +// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, +// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, +// ARISING FROM, OUT OF OR IN CONNECTION WITH +// THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. // -//----------------------------------------------------------------------- -//----------------------------------------------------------------------- +// ----------------------------------------------------------------------- +// ----------------------------------------------------------------------- using StgSharp.Geometries; using System; +using System.Runtime.CompilerServices; namespace StgSharp { public class CollisionEventArgs : EventArgs { - public CollisionEventArgs( IGeometry target, IGeometry boarder ) + public CollisionEventArgs(IGeometry target, IGeometry boarder) { Target = target; Boarder = boarder; @@ -49,8 +50,7 @@ namespace StgSharp } - public class PlainGeometryCollisionSensor where TBoarder: IGeometry - where TTarget: IGeometry + public class PlainGeometryCollisionSensor where TBoarder: IGeometry where TTarget: IGeometry { internal EventHandler _rule; @@ -59,15 +59,16 @@ namespace StgSharp public event EventHandler Rule { - add { _rule += value; } + [MethodImpl(MethodImplOptions.AggressiveInlining)] + add => _rule += value; remove { - if( value == null ) { + if (value == null) { return; } - #pragma warning disable CS8601 +#pragma warning disable CS8601 _rule -= value; - #pragma warning restore CS8601 +#pragma warning restore CS8601 } } diff --git a/StgSharp.Common/Mathematics/HighPrecision/Vec2HP.cs b/StgSharp.Mathematics/Numeric/MatrixCompute.Basic.cs similarity index 67% rename from StgSharp.Common/Mathematics/HighPrecision/Vec2HP.cs rename to StgSharp.Mathematics/Numeric/MatrixCompute.Basic.cs index b4f770e6..6383713a 100644 --- a/StgSharp.Common/Mathematics/HighPrecision/Vec2HP.cs +++ b/StgSharp.Mathematics/Numeric/MatrixCompute.Basic.cs @@ -1,6 +1,6 @@ //----------------------------------------------------------------------- // ----------------------------------------------------------------------- -// file="Vec2HP.cs" +// file="Matrix.Basic.cs" // Project: StgSharp // AuthorGroup: Nitload Space // Copyright (c) Nitload Space. All rights reserved. @@ -28,40 +28,33 @@ // // ----------------------------------------------------------------------- // ----------------------------------------------------------------------- -using StgSharp.HighPerformance; - using System; using System.Collections.Generic; using System.Linq; -using System.Runtime.CompilerServices; -using System.Runtime.InteropServices; -using System.Runtime.Intrinsics; +using System.Security.AccessControl; using System.Text; using System.Threading.Tasks; -namespace StgSharp.Mathematics.HighPrecision +namespace StgSharp.Mathematics.Numeric { - [StructLayout(LayoutKind.Explicit, Pack = 16)] - public struct Vec2HP : IFixedVector + public static class MatrixCompute { - [FieldOffset(0)] private M256 buffer; - [FieldOffset(0)] private Vector256 clrBuffer; - - [FieldOffset(0 * sizeof(double))] public double X; - [FieldOffset(1 * sizeof(double))] public double Y; - - public Vec2HP(double x, double y) + public static Matrix Add(Matrix left, Matrix right) { - this.X = x; - this.Y = y; - Unsafe.SkipInit(out buffer); - Unsafe.SkipInit(out clrBuffer); + if (left.ColumnLength != right.ColumnLength || left.RowLength != right.RowLength) { + throw new IndexOutOfRangeException("Matrix dimensions must agree."); + } + MatrixParallel.LeadParallel(); + try + { + throw new NotImplementedException(); + } + finally + { + MatrixParallel.EndParallel(); + } } - public static Vec2HP Zero => new Vec2HP(0, 0); - - public static Vec2HP One => new Vec2HP(1, 1); - } } diff --git a/StgSharp.Common/Mathematics/Linear/MatrixMarshal.cs b/StgSharp.Mathematics/Numeric/MatrixMarshal.cs similarity index 100% rename from StgSharp.Common/Mathematics/Linear/MatrixMarshal.cs rename to StgSharp.Mathematics/Numeric/MatrixMarshal.cs diff --git a/StgSharp.Common/Mathematics/Linear/MatrixParallel.Execute.cs b/StgSharp.Mathematics/Numeric/MatrixParallel.Execute.cs similarity index 99% rename from StgSharp.Common/Mathematics/Linear/MatrixParallel.Execute.cs rename to StgSharp.Mathematics/Numeric/MatrixParallel.Execute.cs index 40d6caef..9a8197a0 100644 --- a/StgSharp.Common/Mathematics/Linear/MatrixParallel.Execute.cs +++ b/StgSharp.Mathematics/Numeric/MatrixParallel.Execute.cs @@ -31,7 +31,7 @@ using System; using System.Runtime.CompilerServices; -namespace StgSharp.Mathematics +namespace StgSharp.Mathematics.Numeric { public static partial class MatrixParallel { diff --git a/StgSharp.Common/Mathematics/Linear/MatrixParallel.Publish.cs b/StgSharp.Mathematics/Numeric/MatrixParallel.Publish.cs similarity index 98% rename from StgSharp.Common/Mathematics/Linear/MatrixParallel.Publish.cs rename to StgSharp.Mathematics/Numeric/MatrixParallel.Publish.cs index e79be888..b5cfdd1d 100644 --- a/StgSharp.Common/Mathematics/Linear/MatrixParallel.Publish.cs +++ b/StgSharp.Mathematics/Numeric/MatrixParallel.Publish.cs @@ -37,7 +37,7 @@ using System.Numerics; using System.Text; using System.Threading.Tasks; -namespace StgSharp.Mathematics +namespace StgSharp.Mathematics.Numeric { public static partial class MatrixParallel { @@ -819,8 +819,14 @@ namespace StgSharp.Mathematics { int primCount = package->PrimCount; int secCount = package->SecCount; - int threadPerGroup = (1024 / primCount) + 1; - secCount /= threadPerGroup; + int tileWidth = (1024 / secCount) + 1; + int tileCount = (primCount / tileWidth) + 1; + + int tilePerThreadLess = tileCount / Wraps.Length; + int tileToSlice = tileCount % Wraps.Length; + + // TODO 这个方法依然是独占,需要一个新的缓存类型用来辅助复用空线程束 + for (int i = 0; i < Wraps.Length; i++) { } } } diff --git a/StgSharp.Common/Mathematics/Linear/MatrixParallel.Task.cs b/StgSharp.Mathematics/Numeric/MatrixParallel.Task.cs similarity index 99% rename from StgSharp.Common/Mathematics/Linear/MatrixParallel.Task.cs rename to StgSharp.Mathematics/Numeric/MatrixParallel.Task.cs index e4fc6056..706ac333 100644 --- a/StgSharp.Common/Mathematics/Linear/MatrixParallel.Task.cs +++ b/StgSharp.Mathematics/Numeric/MatrixParallel.Task.cs @@ -35,7 +35,7 @@ using System.Numerics; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; -namespace StgSharp.Mathematics +namespace StgSharp.Mathematics.Numeric { public unsafe struct ScalarPacket { diff --git a/StgSharp.Common/Mathematics/Linear/MatrixParallel.TaskFactory.cs b/StgSharp.Mathematics/Numeric/MatrixParallel.TaskFactory.cs similarity index 99% rename from StgSharp.Common/Mathematics/Linear/MatrixParallel.TaskFactory.cs rename to StgSharp.Mathematics/Numeric/MatrixParallel.TaskFactory.cs index 9f361357..94507c8e 100644 --- a/StgSharp.Common/Mathematics/Linear/MatrixParallel.TaskFactory.cs +++ b/StgSharp.Mathematics/Numeric/MatrixParallel.TaskFactory.cs @@ -41,7 +41,7 @@ using System.Threading.Tasks; using hlsfAllocator = StgSharp.HighPerformance.Memory.HybridLayerSegregatedFitAllocator; -namespace StgSharp.Mathematics +namespace StgSharp.Mathematics.Numeric { internal static unsafe class MatrixParallelFactory { diff --git a/StgSharp.Common/Mathematics/Linear/MatrixParallel.cs b/StgSharp.Mathematics/Numeric/MatrixParallel.cs similarity index 86% rename from StgSharp.Common/Mathematics/Linear/MatrixParallel.cs rename to StgSharp.Mathematics/Numeric/MatrixParallel.cs index c87e877e..54e1cbf9 100644 --- a/StgSharp.Common/Mathematics/Linear/MatrixParallel.cs +++ b/StgSharp.Mathematics/Numeric/MatrixParallel.cs @@ -34,7 +34,7 @@ using System.Diagnostics.CodeAnalysis; using System.Runtime.CompilerServices; using System.Threading; -namespace StgSharp.Mathematics +namespace StgSharp.Mathematics.Numeric { /* * Thread wrapping to accelerate matrix operations by dividing threads into groups (wraps). @@ -56,18 +56,6 @@ namespace StgSharp.Mathematics public static partial class MatrixParallel { - // TODO - /* - * 目前认为应当按照如下方式进行调整: - * 1. 使用Queue进行线程束池化 - * 2. 一次只能有一个线程进行发布活动(一次只能由一个线程持有) - * 3. 发布任务似乎应当分类进行,分类条件为q = n_task / n_thread - * 3.1 q=0时,不是全部任务束可以参与计算 - * 3.2 q>0时,全部任务束可以参与计算 - * 4. 后续分为a个q线程,b个q+1线程,然后分到线程束中,同时不保证束内任务同步 - * 5. 仅可能保证线程间任务数量差异不超过512 - */ - private static Thread[] _threads; private static readonly (int a, int b)[] LookupTable = [ (1, 1), // 7 @@ -140,47 +128,46 @@ namespace StgSharp.Mathematics { if (count == 3) { - SmallWraps = [ new MatrixParallelWrap(0, count, true) ]; - LargeWraps = []; + Wraps = [ new MatrixParallelWrap(0, count, true) ]; (LargeWrapCount, SmallWrapCount) = (0, 1); - LeaderHandle = new MatrixParallelWrap.Handle(0, SmallWraps[0]); + LeaderHandle = new MatrixParallelWrap.Handle(0, Wraps[0]); } else { count = 4; - LargeWraps = [ new MatrixParallelWrap(0, count, true) ]; - SmallWraps = []; + Wraps = [ new MatrixParallelWrap(0, count, true) ]; (LargeWrapCount, SmallWrapCount) = (1, 0); - LeaderHandle = new MatrixParallelWrap.Handle(0, LargeWraps[0]); + LeaderHandle = new MatrixParallelWrap.Handle(0, Wraps[0]); } } else if (count == 6) { - SmallWraps = [ new MatrixParallelWrap(0, 3, true), new MatrixParallelWrap(3, 3, false) ]; - LargeWraps = []; + Wraps = [ + new MatrixParallelWrap(0, 3, true), + new MatrixParallelWrap(3, 3, false) + ]; (LargeWrapCount, SmallWrapCount) = (0, 2); - LeaderHandle = new MatrixParallelWrap.Handle(0, SmallWraps[0]); + LeaderHandle = new MatrixParallelWrap.Handle(0, Wraps[0]); } else { (int _3wrap, int _4wrap) = FastDecompose(count); - LargeWraps = new MatrixParallelWrap[_4wrap]; - SmallWraps = new MatrixParallelWrap[_3wrap]; - int id = 0; - for (int idx = 0; idx < _3wrap; idx++, id += 3) { - SmallWraps[idx] = new MatrixParallelWrap(idx, 3, id == 0); + Wraps = new MatrixParallelWrap[_4wrap + _3wrap]; + int id = 0,idx = 0; + for (; idx < _3wrap; idx++, id += 3) { + Wraps[idx] = new MatrixParallelWrap(idx, 3, id == 0); } - for (int idx = 0; idx < _3wrap + _4wrap; idx++, id += 4) { - LargeWraps[idx] = new MatrixParallelWrap(id, 4, id == 0); + for (; idx < _3wrap + _4wrap; idx++, id += 4) { + Wraps[idx] = new MatrixParallelWrap(id, 4, id == 0); } - LeaderHandle = new MatrixParallelWrap.Handle(0, _3wrap == 0 ? LargeWraps[0] : SmallWraps[0]); + LeaderHandle = new MatrixParallelWrap.Handle(0, Wraps[0]); } LeaderTask = LeaderHandle.Wrap.GetWorkerTaskQueue(0); IsInitialized = true; } - public static void LaunchParallel(SleepMode afterWork) + public static void LaunchParallel(IEnumerable wraps, SleepMode afterWork) { RemainThreadCount = _threads.Length; _AfterWork = (ThreadPriority)afterWork; - SetAllWrap(); + SetAllWrap(wraps); } [MethodImpl(MethodImplOptions.AggressiveInlining)] @@ -202,13 +189,10 @@ namespace StgSharp.Mathematics RemainThreadCount = _threads.Length; } - public static void SetAllWrap() + public static void SetAllWrap(IEnumerable wraps) { ResetThreadCount(); - foreach (MatrixParallelWrap item in LargeWraps) { - item.SchedulerReset.Set(); - } - foreach (MatrixParallelWrap item in SmallWraps) { + foreach (MatrixParallelWrap item in wraps) { item.SchedulerReset.Set(); } } @@ -360,8 +344,7 @@ namespace StgSharp.Mathematics #region wrap field - private static MatrixParallelWrap[] LargeWraps; - private static MatrixParallelWrap[] SmallWraps; + private static MatrixParallelWrap[] Wraps; private static int LargeWrapCount; private static int SmallWrapCount; diff --git a/StgSharp.Mathematics/StgSharp.Mathematics.csproj b/StgSharp.Mathematics/StgSharp.Mathematics.csproj new file mode 100644 index 00000000..357383bf --- /dev/null +++ b/StgSharp.Mathematics/StgSharp.Mathematics.csproj @@ -0,0 +1,15 @@ + + + + net8.0 + enable + enable + Preview + True + + + + + + + diff --git a/StgSharpDevelop.sln b/StgSharpDevelop.sln index 70be4f04..f75e8940 100644 --- a/StgSharpDevelop.sln +++ b/StgSharpDevelop.sln @@ -28,61 +28,101 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "StgSharp.Script", "StgSharp EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "StgSharp.Model", "StgSharp.Model\StgSharp.Model.csproj", "{D9538DE0-EE4C-4183-86D6-7DFC08A7BDFB}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "StgSharp.Mathematics", "StgSharp.Mathematics\StgSharp.Mathematics.csproj", "{9946599F-4D34-440F-840D-F9443D5AEFAA}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU Debug|ARM64 = Debug|ARM64 Debug|x64 = Debug|x64 + Release|Any CPU = Release|Any CPU Release|ARM64 = Release|ARM64 Release|x64 = Release|x64 EndGlobalSection GlobalSection(ProjectConfigurationPlatforms) = postSolution + {3EAF2EE6-FA9E-4F57-951F-7E6F8125DFBF}.Debug|Any CPU.ActiveCfg = Debug|x64 + {3EAF2EE6-FA9E-4F57-951F-7E6F8125DFBF}.Debug|Any CPU.Build.0 = Debug|x64 {3EAF2EE6-FA9E-4F57-951F-7E6F8125DFBF}.Debug|ARM64.ActiveCfg = Debug|ARM64 {3EAF2EE6-FA9E-4F57-951F-7E6F8125DFBF}.Debug|ARM64.Build.0 = Debug|ARM64 {3EAF2EE6-FA9E-4F57-951F-7E6F8125DFBF}.Debug|x64.ActiveCfg = Debug|x64 {3EAF2EE6-FA9E-4F57-951F-7E6F8125DFBF}.Debug|x64.Build.0 = Debug|x64 + {3EAF2EE6-FA9E-4F57-951F-7E6F8125DFBF}.Release|Any CPU.ActiveCfg = Release|x64 + {3EAF2EE6-FA9E-4F57-951F-7E6F8125DFBF}.Release|Any CPU.Build.0 = Release|x64 {3EAF2EE6-FA9E-4F57-951F-7E6F8125DFBF}.Release|ARM64.ActiveCfg = Release|ARM64 {3EAF2EE6-FA9E-4F57-951F-7E6F8125DFBF}.Release|ARM64.Build.0 = Release|ARM64 {3EAF2EE6-FA9E-4F57-951F-7E6F8125DFBF}.Release|x64.ActiveCfg = Release|x64 {3EAF2EE6-FA9E-4F57-951F-7E6F8125DFBF}.Release|x64.Build.0 = Release|x64 + {610B992B-BB34-49C3-9993-676C595FC12E}.Debug|Any CPU.ActiveCfg = Debug|x64 + {610B992B-BB34-49C3-9993-676C595FC12E}.Debug|Any CPU.Build.0 = Debug|x64 {610B992B-BB34-49C3-9993-676C595FC12E}.Debug|ARM64.ActiveCfg = Debug|ARM64 {610B992B-BB34-49C3-9993-676C595FC12E}.Debug|ARM64.Build.0 = Debug|ARM64 {610B992B-BB34-49C3-9993-676C595FC12E}.Debug|x64.ActiveCfg = Debug|x64 {610B992B-BB34-49C3-9993-676C595FC12E}.Debug|x64.Build.0 = Debug|x64 + {610B992B-BB34-49C3-9993-676C595FC12E}.Release|Any CPU.ActiveCfg = Release|x64 + {610B992B-BB34-49C3-9993-676C595FC12E}.Release|Any CPU.Build.0 = Release|x64 {610B992B-BB34-49C3-9993-676C595FC12E}.Release|ARM64.ActiveCfg = Release|ARM64 {610B992B-BB34-49C3-9993-676C595FC12E}.Release|ARM64.Build.0 = Release|ARM64 {610B992B-BB34-49C3-9993-676C595FC12E}.Release|x64.ActiveCfg = Release|x64 + {20CBFF26-6746-402E-992C-7B643E9423EB}.Debug|Any CPU.ActiveCfg = Debug|x64 + {20CBFF26-6746-402E-992C-7B643E9423EB}.Debug|Any CPU.Build.0 = Debug|x64 {20CBFF26-6746-402E-992C-7B643E9423EB}.Debug|ARM64.ActiveCfg = Debug|ARM64 {20CBFF26-6746-402E-992C-7B643E9423EB}.Debug|ARM64.Build.0 = Debug|ARM64 {20CBFF26-6746-402E-992C-7B643E9423EB}.Debug|x64.ActiveCfg = Release|x64 {20CBFF26-6746-402E-992C-7B643E9423EB}.Debug|x64.Build.0 = Release|x64 + {20CBFF26-6746-402E-992C-7B643E9423EB}.Release|Any CPU.ActiveCfg = Release|x64 + {20CBFF26-6746-402E-992C-7B643E9423EB}.Release|Any CPU.Build.0 = Release|x64 {20CBFF26-6746-402E-992C-7B643E9423EB}.Release|ARM64.ActiveCfg = Release|ARM64 {20CBFF26-6746-402E-992C-7B643E9423EB}.Release|ARM64.Build.0 = Release|ARM64 {20CBFF26-6746-402E-992C-7B643E9423EB}.Release|x64.ActiveCfg = Release|x64 {20CBFF26-6746-402E-992C-7B643E9423EB}.Release|x64.Build.0 = Release|x64 + {10B5819F-2A62-41B6-980A-EB9E76A0E95C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {10B5819F-2A62-41B6-980A-EB9E76A0E95C}.Debug|Any CPU.Build.0 = Debug|Any CPU {10B5819F-2A62-41B6-980A-EB9E76A0E95C}.Debug|ARM64.ActiveCfg = Debug|ARM64 {10B5819F-2A62-41B6-980A-EB9E76A0E95C}.Debug|ARM64.Build.0 = Debug|ARM64 {10B5819F-2A62-41B6-980A-EB9E76A0E95C}.Debug|x64.ActiveCfg = Debug|x64 {10B5819F-2A62-41B6-980A-EB9E76A0E95C}.Debug|x64.Build.0 = Debug|x64 + {10B5819F-2A62-41B6-980A-EB9E76A0E95C}.Release|Any CPU.ActiveCfg = Release|Any CPU + {10B5819F-2A62-41B6-980A-EB9E76A0E95C}.Release|Any CPU.Build.0 = Release|Any CPU {10B5819F-2A62-41B6-980A-EB9E76A0E95C}.Release|ARM64.ActiveCfg = Release|ARM64 {10B5819F-2A62-41B6-980A-EB9E76A0E95C}.Release|ARM64.Build.0 = Release|ARM64 {10B5819F-2A62-41B6-980A-EB9E76A0E95C}.Release|x64.ActiveCfg = Release|x64 {10B5819F-2A62-41B6-980A-EB9E76A0E95C}.Release|x64.Build.0 = Release|x64 + {5B51F5EA-138C-46B5-B36F-1D389968B6CF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {5B51F5EA-138C-46B5-B36F-1D389968B6CF}.Debug|Any CPU.Build.0 = Debug|Any CPU {5B51F5EA-138C-46B5-B36F-1D389968B6CF}.Debug|ARM64.ActiveCfg = Debug|ARM64 {5B51F5EA-138C-46B5-B36F-1D389968B6CF}.Debug|ARM64.Build.0 = Debug|ARM64 {5B51F5EA-138C-46B5-B36F-1D389968B6CF}.Debug|x64.ActiveCfg = Debug|x64 {5B51F5EA-138C-46B5-B36F-1D389968B6CF}.Debug|x64.Build.0 = Debug|x64 + {5B51F5EA-138C-46B5-B36F-1D389968B6CF}.Release|Any CPU.ActiveCfg = Release|Any CPU + {5B51F5EA-138C-46B5-B36F-1D389968B6CF}.Release|Any CPU.Build.0 = Release|Any CPU {5B51F5EA-138C-46B5-B36F-1D389968B6CF}.Release|ARM64.ActiveCfg = Release|ARM64 {5B51F5EA-138C-46B5-B36F-1D389968B6CF}.Release|ARM64.Build.0 = Release|ARM64 {5B51F5EA-138C-46B5-B36F-1D389968B6CF}.Release|x64.ActiveCfg = Release|x64 {5B51F5EA-138C-46B5-B36F-1D389968B6CF}.Release|x64.Build.0 = Release|x64 + {D9538DE0-EE4C-4183-86D6-7DFC08A7BDFB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {D9538DE0-EE4C-4183-86D6-7DFC08A7BDFB}.Debug|Any CPU.Build.0 = Debug|Any CPU {D9538DE0-EE4C-4183-86D6-7DFC08A7BDFB}.Debug|ARM64.ActiveCfg = Debug|ARM64 {D9538DE0-EE4C-4183-86D6-7DFC08A7BDFB}.Debug|ARM64.Build.0 = Debug|ARM64 {D9538DE0-EE4C-4183-86D6-7DFC08A7BDFB}.Debug|x64.ActiveCfg = Debug|x64 {D9538DE0-EE4C-4183-86D6-7DFC08A7BDFB}.Debug|x64.Build.0 = Debug|x64 + {D9538DE0-EE4C-4183-86D6-7DFC08A7BDFB}.Release|Any CPU.ActiveCfg = Release|Any CPU + {D9538DE0-EE4C-4183-86D6-7DFC08A7BDFB}.Release|Any CPU.Build.0 = Release|Any CPU {D9538DE0-EE4C-4183-86D6-7DFC08A7BDFB}.Release|ARM64.ActiveCfg = Release|ARM64 {D9538DE0-EE4C-4183-86D6-7DFC08A7BDFB}.Release|ARM64.Build.0 = Release|ARM64 {D9538DE0-EE4C-4183-86D6-7DFC08A7BDFB}.Release|x64.ActiveCfg = Release|x64 {D9538DE0-EE4C-4183-86D6-7DFC08A7BDFB}.Release|x64.Build.0 = Release|x64 + {9946599F-4D34-440F-840D-F9443D5AEFAA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {9946599F-4D34-440F-840D-F9443D5AEFAA}.Debug|Any CPU.Build.0 = Debug|Any CPU + {9946599F-4D34-440F-840D-F9443D5AEFAA}.Debug|ARM64.ActiveCfg = Debug|Any CPU + {9946599F-4D34-440F-840D-F9443D5AEFAA}.Debug|ARM64.Build.0 = Debug|Any CPU + {9946599F-4D34-440F-840D-F9443D5AEFAA}.Debug|x64.ActiveCfg = Debug|Any CPU + {9946599F-4D34-440F-840D-F9443D5AEFAA}.Debug|x64.Build.0 = Debug|Any CPU + {9946599F-4D34-440F-840D-F9443D5AEFAA}.Release|Any CPU.ActiveCfg = Release|Any CPU + {9946599F-4D34-440F-840D-F9443D5AEFAA}.Release|Any CPU.Build.0 = Release|Any CPU + {9946599F-4D34-440F-840D-F9443D5AEFAA}.Release|ARM64.ActiveCfg = Release|Any CPU + {9946599F-4D34-440F-840D-F9443D5AEFAA}.Release|ARM64.Build.0 = Release|Any CPU + {9946599F-4D34-440F-840D-F9443D5AEFAA}.Release|x64.ActiveCfg = Release|Any CPU + {9946599F-4D34-440F-840D-F9443D5AEFAA}.Release|x64.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE