This commit is contained in:
Nitload
2024-06-10 15:12:08 +08:00
parent f6963b8e00
commit be6c2e6b92
305 changed files with 28336 additions and 5938 deletions
-1
View File
@@ -1 +0,0 @@
D:/NitloadIndustry/研究项目/1-Flagship/TH2020/ProjectFile/源代码/StgSharp/Math
+49
View File
@@ -0,0 +1,49 @@
//-----------------------------------------------------------------------
//-----------------------------------------------------------------------
// file="GLfixed.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.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
namespace StgSharp.Math
{
[StructLayout(LayoutKind.Explicit, Size = 8)]
public struct GLfixed
{
[FieldOffset(0)] public int All;
[FieldOffset(0)] public short Decimals;
[FieldOffset(4)] public short Inertger;
}
}
@@ -0,0 +1,322 @@
//-----------------------------------------------------------------------
//-----------------------------------------------------------------------
// file="Camera.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.Graphics;
using StgSharp.Graphics.GL;
using StgSharp.Graphics.ShaderEdit;
using StgSharp.Math;
using System;
using System.Collections.Generic;
using System.Numerics;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Text;
namespace StgSharp.Graphics
{
public sealed class Camera : IglConvertable
{
private bool _isLookAtAvailable;
internal Matrix44 _lookAt;
internal Matrix44 _projection;
internal Matrix44 cameraAtt;
internal Matrix44 rotation;
internal Matrix44 rotationAtt;
internal Radius _pitch;
internal Radius _row;
internal Radius _yaw;
internal Uniform<Matrix44> convertedUniform;
internal vec3d _target, up;
public Camera()
{
cameraAtt = Matrix44.Unit;
_target = Vec3d.Zero;
up = Vec3d.Zero;
_isLookAtAvailable = false;
_lookAt = Matrix44.Unit;
rotationAtt = Matrix44.Unit;
_projection = new Matrix44();
}
public Camera(vec3d position, vec3d target, vec3d up) : this()
{
rotationAtt = Matrix44.Unit;
cameraAtt = new Matrix44();
SetViewDirection(position, target, up);
_pitch = Radius.Zero;
_row = Radius.Zero;
_yaw = Radius.Zero;
}
public Matrix44 Projection
=> _projection;
public Matrix44 View
{
get
{
if (_isLookAtAvailable)
{
return _lookAt;
}
Matrix44 move = Matrix44.Unit;
move.colum3.vec -= cameraAtt.colum3.vec;
_lookAt = rotationAtt.Transpose * move;
_isLookAtAvailable = true;
return _lookAt;
}
}
public void DisplayGLtypeDefinition()
{
Console.WriteLine("uniform matrix4x4 cameraName;");
}
/// <summary>
/// Get all uniform related to this <see cref="Camera"/>.
/// </summary>
/// <param name="source">Shader program requires this camera.</param>
/// <param name="uniformName">
/// ContextName of all related uniforms.
/// If you fallow form from <see cref="IglConvertable.DisplayGLtypeDefinition"/>
/// </param>
/// <exception cref="ArgumentException"></exception>
public unsafe void GainAllUniforms(ShaderProgram source, params string[] uniformName)
{
if (uniformName.Length != 1)
{
throw new ArgumentException();
}
convertedUniform = source.GetUniform<Matrix44>(uniformName[0]);
}
public void MoveNear(float distance)
{
cameraAtt.colum3.Z -= distance;
_isLookAtAvailable = false;
}
public void MoveRight(float distance)
{
cameraAtt.colum3.X -= distance;
_isLookAtAvailable = false;
}
public void MoveUp(float distance)
{
cameraAtt.colum3.Y -= distance;
_isLookAtAvailable = false;
}
public unsafe void SetAllUniforms()
{
//Console.WriteLine(Projection * View);
glFunc.SetUniformValue(convertedUniform, Projection * View);
}
public void SetViewDirection(vec3d position, vec3d target, vec3d up)
{
vec3d direction = position - target;
if ((position.vec == cameraAtt.colum3.vec) && (this.up == up) && (cameraAtt.colum2.vec == direction.vec))
{
//no difference with precious value
return;
}
if (direction.GetLength() == 0)
{
//deadlock
throw new ArgumentException();
}
if (Vec3d.IsParallel(direction, up))
{
throw new ArgumentException();
}
_target = direction;
this.up = up;
_isLookAtAvailable = false;
Linear.Orthogonalize(ref direction, ref up);
vec3d right = Linear.Orthogonalize(Linear.Cross(up, direction));
cameraAtt.colum0.vec = right.vec;
cameraAtt.colum1.vec = up.vec;
cameraAtt.colum2.vec = direction.vec;
cameraAtt.colum3.vec = position.vec;
InternalPitch();
InternalRow();
InternalYaw();
}
public void SetViewRange(Radius fovRadius, vec2d size, vec2d offset, (float front, float back) dephRange)
{
float
distance = _target.GetLength(),
near = distance - dephRange.front,
far = distance + dephRange.back,
offsetX = offset.X,
offsetY = offset.Y,
width = Scaler.Abs(GeometryScaler.Tan(fovRadius / 2) * near * 2),
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.colum2.Z = (far + near) / (near - far);
_projection.colum2.W = -1;
_projection.colum3.Z = (2 * near * far) / (near - far);
}
public void Test(params vec4d[] vec)
{
Console.WriteLine(View);
Console.WriteLine(Projection);
Console.WriteLine(Projection * View);
Console.WriteLine(View);
foreach (vec4d item in vec)
{
Console.Write($"{Projection * View * item};");
}
}
ShaderStruct IglConvertable.GetConvertedGLtype()
{
throw new NotImplementedException();
}
~Camera()
{
}
#region rotation
public void Yaw(Radius r)
{
_yaw -= r;
//Console.WriteLine(_yaw._radius);
//Console.WriteLine(rotationAtt);
InternalYaw();
}
public void Pitch(Radius r)
{
_pitch -= r;
//Console.WriteLine(_pitch._radius);
//Console.WriteLine(rotationAtt);
InternalPitch();
}
public void Row(Radius r)
{
_row -= r;
//Console.WriteLine(_row._radius);
//Console.WriteLine(rotationAtt);
InternalRow();
}
internal void InternalPitch()
{
_isLookAtAvailable = false;
Matrix32 partialCoord = new Matrix32(
cameraAtt.colum1.vec,
cameraAtt.colum2.vec
);
float angle = _pitch._radius;
Matrix22 rotation = new Matrix22(
Scaler.Cos(angle), -Scaler.Sin(angle),
Scaler.Sin(angle), Scaler.Cos(angle)
);
partialCoord *= rotation;
rotationAtt.colum1.vec = partialCoord.colum0.vec;
rotationAtt.colum2.vec = partialCoord.colum1.vec;
rotationAtt.isTransposed = false;
}
internal void InternalRow()
{
_isLookAtAvailable = false;
Matrix32 partialCoord = new Matrix32(
cameraAtt.colum0.vec,
cameraAtt.colum1.vec
);
float angle = _row._radius;
Matrix22 rotation = new Matrix22(
Scaler.Cos(angle), -Scaler.Sin(angle),
Scaler.Sin(angle), Scaler.Cos(angle)
);
partialCoord *= rotation;
rotationAtt.colum0.vec = partialCoord.colum0.vec;
rotationAtt.colum1.vec = partialCoord.colum1.vec;
rotationAtt.isTransposed = false;
}
internal void InternalYaw()
{
_isLookAtAvailable = false;
Matrix32 partialCoord = new Matrix32(
cameraAtt.colum0.vec,
cameraAtt.colum2.vec
);
float angle = _yaw._radius;
Matrix22 rotation = new Matrix22(
Scaler.Cos(angle), -Scaler.Sin(angle),
Scaler.Sin(angle), Scaler.Cos(angle)
);
partialCoord *= rotation;
rotationAtt.colum0.vec = partialCoord.colum0.vec;
rotationAtt.colum2.vec = partialCoord.colum1.vec;
rotationAtt.isTransposed = false;
}
#endregion
}
}
@@ -0,0 +1,40 @@
//-----------------------------------------------------------------------
//-----------------------------------------------------------------------
// file="Cordination.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.Math
{
public enum CoordinatePlain
{
XY,
YZ,
XZ,
}
}
@@ -0,0 +1,12 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace StgSharp.Math
{
public interface IVector
{
}
}
@@ -0,0 +1,40 @@
//-----------------------------------------------------------------------
//-----------------------------------------------------------------------
// file="MatInterface.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.Text;
namespace StgSharp.Math
{
public interface IMat
{
}
}
@@ -0,0 +1,188 @@
//-----------------------------------------------------------------------
//-----------------------------------------------------------------------
// file="Matrix22.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.Numerics;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
namespace StgSharp.Math
{
[StructLayout(LayoutKind.Explicit, Size = (2 * 2 * 4 * sizeof(float)) + sizeof(bool), Pack = 16)]
public struct Matrix22 : IMat
{
[FieldOffset(2 * 2 * 4 * sizeof(float))]
internal bool isTransposed;
[FieldOffset(0)]
internal Mat2 mat;
[FieldOffset(2 * 4 * sizeof(float))]
internal Mat2 transpose;
internal Matrix22(
Mat2 mat
)
{
this.mat = mat;
}
internal Matrix22(
Vector4 c0,
Vector4 c1
)
{
mat.colum0 = c0;
mat.colum1 = c1;
}
public Matrix22(
float a00, float a01,
float a10, float a11
)
{
mat.colum0 = new Vector4(a00, a10, 0, 0);
mat.colum1 = new Vector4(a01, a11, 0, 0);
}
public Matrix22 Transpose
{
get
{
InternalTranspose();
return new Matrix22(this.transpose);
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public float Det()
{
return (mat.m00 * mat.m11) - (mat.m10 * mat.m01);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal void InternalTranspose()
{
if (!isTransposed)
{
transpose = mat;
float cache = transpose.m10;
transpose.m10 = transpose.m01;
transpose.m01 = cache;
isTransposed = true;
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Matrix22 operator -(Matrix22 left, Matrix22 right)
{
return new Matrix22(
left.mat.colum0 + right.mat.colum0,
left.mat.colum1 + right.mat.colum1
);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Matrix22 operator *(Matrix22 mat, float value)
{
return new Matrix22(
mat.mat.colum0 * value,
mat.mat.colum1 * value
);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Matrix22 operator *(Matrix22 left, Matrix22 right)
{
left.InternalTranspose();
return new Matrix22(
Vector4.Dot(left.transpose.colum0, right.mat.colum0),
Vector4.Dot(left.transpose.colum0, right.mat.colum1),
Vector4.Dot(left.transpose.colum1, right.mat.colum0),
Vector4.Dot(left.transpose.colum1, right.mat.colum1)
);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Matrix23 operator *(Matrix22 left, Matrix23 right)
{
left.InternalTranspose();
return new Matrix23(
Vector4.Dot(left.transpose.colum0, right.mat.colum0),
Vector4.Dot(left.transpose.colum0, right.mat.colum1),
Vector4.Dot(left.transpose.colum0, right.mat.colum2),
Vector4.Dot(left.transpose.colum1, right.mat.colum0),
Vector4.Dot(left.transpose.colum1, right.mat.colum1),
Vector4.Dot(left.transpose.colum1, right.mat.colum2)
);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Matrix24 operator *(Matrix22 left, Matrix24 right)
{
left.InternalTranspose();
return new Matrix24(
Vector4.Dot(left.transpose.colum0, right.mat.colum0),
Vector4.Dot(left.transpose.colum0, right.mat.colum1),
Vector4.Dot(left.transpose.colum0, right.mat.colum2),
Vector4.Dot(left.transpose.colum0, right.mat.colum3),
Vector4.Dot(left.transpose.colum1, right.mat.colum0),
Vector4.Dot(left.transpose.colum1, right.mat.colum1),
Vector4.Dot(left.transpose.colum1, right.mat.colum2),
Vector4.Dot(left.transpose.colum1, right.mat.colum3)
);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Matrix22 operator /(Matrix22 mat, float value)
{
return new Matrix22(
mat.mat.colum0 / value,
mat.mat.colum1 / value
);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static unsafe Matrix22 operator +(Matrix22 left, Matrix22 right)
{
Mat2* refmat = InternalIO.add_mat2(&left.mat, &right.mat);
Matrix22 ret = new Matrix22(*refmat);
InternalIO.deinit_mat2(refmat);
return ret;
}
}
}
@@ -0,0 +1,217 @@
//-----------------------------------------------------------------------
//-----------------------------------------------------------------------
// file="Matrix23.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.Numerics;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
namespace StgSharp.Math
{
[StructLayout(LayoutKind.Explicit, Size = ((3 + 2) * 4 * sizeof(float)) + sizeof(bool), Pack = 16)]
public struct Matrix23 : IMat
{
[FieldOffset(5 * 4 * sizeof(float))] internal bool isTransposed;
[FieldOffset(3 * 4 * sizeof(float))] internal Mat2 transpose;
[FieldOffset(0)] internal Mat3 mat;
internal Matrix23(
Vector4 c0,
Vector4 c1,
Vector4 c2
)
{
mat.colum0 = c0;
mat.colum1 = c1;
mat.colum2 = c2;
}
public Matrix23(
float a00, float a01, float a02,
float a10, float a11, float a12
)
{
mat.colum0 = new Vector4(a00, a10, 0, 0);
mat.colum1 = new Vector4(a01, a11, 0, 0);
mat.colum2 = new Vector4(a02, a12, 0, 0);
}
public unsafe float this[int rowNum, int columNum]
{
get
{
if ((rowNum > 1) || (rowNum < 0))
{
throw new ArgumentOutOfRangeException(nameof(columNum));
}
if ((columNum > 2) || (columNum < 0))
{
throw new ArgumentOutOfRangeException(nameof(columNum));
}
InternalTranspose();
fixed (float* p = &this.transpose.m00)
{
ulong pbit = ((ulong)p)
+ (((ulong)sizeof(Vector4)) * ((ulong)rowNum))
+ (((ulong)sizeof(float)) * ((ulong)columNum));
return *(float*)pbit;
}
}
set
{
if ((rowNum > 1) || (rowNum < 0))
{
throw new ArgumentOutOfRangeException(nameof(columNum));
}
if ((columNum > 2) || (columNum < 0))
{
throw new ArgumentOutOfRangeException(nameof(columNum));
}
InternalTranspose();
fixed (float* p = &this.transpose.m00)
{
ulong pbit = ((ulong)p)
+ (((ulong)sizeof(Vector4)) * ((ulong)rowNum))
+ (((ulong)sizeof(float)) * ((ulong)columNum));
*(float*)pbit = value;
}
isTransposed = false;
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal unsafe void InternalTranspose()
{
if (!isTransposed)
{
fixed (Mat3* source = &this.mat)
{
fixed (Mat2* target = &this.transpose)
{
InternalIO.Transpose3to2_internal(source, target);
}
}
isTransposed = true;
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Matrix23 operator -(Matrix23 left, Matrix23 right)
{
return new Matrix23(
left.mat.colum0 - right.mat.colum0,
left.mat.colum1 - right.mat.colum1,
left.mat.colum2 - right.mat.colum2
);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Matrix23 operator *(Matrix23 mat, float value)
{
return new Matrix23(
mat.mat.colum0 * value,
mat.mat.colum1 * value,
mat.mat.colum2 * value
);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Matrix22 operator *(Matrix23 left, Matrix32 right)
{
left.InternalTranspose();
return new Matrix22(
Vector4.Dot(left.transpose.colum0, right.mat.colum0),
Vector4.Dot(left.transpose.colum0, right.mat.colum1),
Vector4.Dot(left.transpose.colum0, right.mat.colum0),
Vector4.Dot(left.transpose.colum0, right.mat.colum1)
);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Matrix23 operator *(Matrix23 left, Matrix33 right)
{
left.InternalTranspose();
return new Matrix23(
Vector4.Dot(left.transpose.colum0, right.mat.colum0),
Vector4.Dot(left.transpose.colum0, right.mat.colum1),
Vector4.Dot(left.transpose.colum0, right.mat.colum2),
Vector4.Dot(left.transpose.colum0, right.mat.colum0),
Vector4.Dot(left.transpose.colum0, right.mat.colum1),
Vector4.Dot(left.transpose.colum0, right.mat.colum2)
);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Matrix24 operator *(Matrix23 left, Matrix34 right)
{
left.InternalTranspose();
return new Matrix24(
Vector4.Dot(left.transpose.colum0, right.mat.colum0),
Vector4.Dot(left.transpose.colum0, right.mat.colum1),
Vector4.Dot(left.transpose.colum0, right.mat.colum2),
Vector4.Dot(left.transpose.colum0, right.mat.colum3),
Vector4.Dot(left.transpose.colum0, right.mat.colum0),
Vector4.Dot(left.transpose.colum0, right.mat.colum1),
Vector4.Dot(left.transpose.colum0, right.mat.colum2),
Vector4.Dot(left.transpose.colum0, right.mat.colum3)
);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Matrix23 operator /(Matrix23 mat, float value)
{
return new Matrix23(
mat.mat.colum0 / value,
mat.mat.colum1 / value,
mat.mat.colum2 / value
);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Matrix23 operator +(Matrix23 left, Matrix23 right)
{
return new Matrix23(
left.mat.colum0 + right.mat.colum0,
left.mat.colum1 + right.mat.colum1,
left.mat.colum2 + right.mat.colum2
);
}
}
}
@@ -0,0 +1,237 @@
//-----------------------------------------------------------------------
//-----------------------------------------------------------------------
// file="Matrix24.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.Numerics;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
namespace StgSharp.Math
{
[StructLayout(LayoutKind.Explicit, Size = ((4 + 2) * 4 * sizeof(float)) + sizeof(bool), Pack = 16)]
public struct Matrix24 : IMat
{
[FieldOffset(6 * 4 * sizeof(float))] internal bool isTransposed;
[FieldOffset(4 * 4 * sizeof(float))] internal Mat2 transpose;
[FieldOffset(0)] internal Mat4 mat;
[FieldOffset(0)] public vec2d Colum0;
[FieldOffset(16)] public vec2d Colum1;
[FieldOffset(32)] public vec2d Colum2;
[FieldOffset(48)] public vec2d Colum3;
internal Matrix24(
Vector4 c0,
Vector4 c1,
Vector4 c2,
Vector4 c3
)
{
mat.colum0 = c0;
mat.colum1 = c1;
mat.colum2 = c2;
mat.colum3 = c3;
}
public Matrix24(
float a00, float a01, float a02, float a03,
float a10, float a11, float a12, float a13
)
{
mat.colum0 = new Vector4(a00, a10, 0, 0);
mat.colum1 = new Vector4(a01, a11, 0, 0);
mat.colum2 = new Vector4(a02, a12, 0, 0);
mat.colum3 = new Vector4(a03, a13, 0, 0);
}
public unsafe float this[int rowNum, int columNum]
{
get
{
if ((rowNum > 1) || (rowNum < 0))
{
throw new ArgumentOutOfRangeException(nameof(columNum));
}
if ((columNum > 3) || (columNum < 0))
{
throw new ArgumentOutOfRangeException(nameof(columNum));
}
InternalTranspose();
fixed (float* p = &this.transpose.m00)
{
ulong pbit = ((ulong)p)
+ (((ulong)sizeof(Vector4)) * ((ulong)rowNum))
+ (((ulong)sizeof(float)) * ((ulong)columNum));
return *(float*)pbit;
}
}
set
{
if ((rowNum > 1) || (rowNum < 0))
{
throw new ArgumentOutOfRangeException(nameof(columNum));
}
if ((columNum > 3) || (columNum < 0))
{
throw new ArgumentOutOfRangeException(nameof(columNum));
}
InternalTranspose();
fixed (float* p = &this.transpose.m00)
{
ulong pbit = ((ulong)p)
+ (((ulong)sizeof(Vector4)) * ((ulong)rowNum))
+ (((ulong)sizeof(float)) * ((ulong)columNum));
*(float*)pbit = value;
}
isTransposed = false;
}
}
public override string ToString()
{
InternalTranspose();
return $"{$"[{mat.m00},{mat.m01},{mat.m02},{mat.m03}],"}{$"[{mat.m10},{mat.m11},{mat.m12},{mat.m13}]"}";
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal unsafe void InternalTranspose()
{
if (!isTransposed)
{
fixed (Mat4* source = &this.mat)
{
fixed (Mat2* target = &this.transpose)
{
InternalIO.Transpose4to2_internal(source, target);
}
}
isTransposed = true;
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Matrix24 operator -(Matrix24 left, Matrix24 right)
{
return new Matrix24(
left.mat.colum0 - right.mat.colum0,
left.mat.colum1 - right.mat.colum1,
left.mat.colum2 - right.mat.colum2,
left.mat.colum3 - right.mat.colum3
);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Matrix24 operator *(Matrix24 mat, float value)
{
return new Matrix24(
mat.mat.colum0 * value,
mat.mat.colum1 * value,
mat.mat.colum2 * value,
mat.mat.colum3 * value
);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Matrix22 operator *(Matrix24 left, Matrix42 right)
{
left.InternalTranspose();
return new Matrix22(
Vector4.Dot(left.transpose.colum0, right.mat.colum0),
Vector4.Dot(left.transpose.colum0, right.mat.colum1),
Vector4.Dot(left.transpose.colum1, right.mat.colum0),
Vector4.Dot(left.transpose.colum1, right.mat.colum1)
);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Matrix23 operator *(Matrix24 left, Matrix43 right)
{
left.InternalTranspose();
return new Matrix23(
Vector4.Dot(left.transpose.colum0, right.mat.colum0),
Vector4.Dot(left.transpose.colum0, right.mat.colum1),
Vector4.Dot(left.transpose.colum0, right.mat.colum2),
Vector4.Dot(left.transpose.colum1, right.mat.colum0),
Vector4.Dot(left.transpose.colum1, right.mat.colum1),
Vector4.Dot(left.transpose.colum1, right.mat.colum2)
);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Matrix24 operator *(Matrix24 left, Matrix44 right)
{
left.InternalTranspose();
return new Matrix24(
Vector4.Dot(left.transpose.colum0, right.mat.colum0),
Vector4.Dot(left.transpose.colum0, right.mat.colum1),
Vector4.Dot(left.transpose.colum0, right.mat.colum2),
Vector4.Dot(left.transpose.colum0, right.mat.colum3),
Vector4.Dot(left.transpose.colum1, right.mat.colum0),
Vector4.Dot(left.transpose.colum1, right.mat.colum1),
Vector4.Dot(left.transpose.colum1, right.mat.colum2),
Vector4.Dot(left.transpose.colum1, right.mat.colum3)
);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Matrix24 operator /(Matrix24 mat, float value)
{
return new Matrix24(
mat.mat.colum0 / value,
mat.mat.colum1 / value,
mat.mat.colum2 / value,
mat.mat.colum3 / value
);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Matrix24 operator +(Matrix24 left, Matrix24 right)
{
return new Matrix24(
left.mat.colum0 + right.mat.colum0,
left.mat.colum1 + right.mat.colum1,
left.mat.colum2 + right.mat.colum2,
left.mat.colum3 + right.mat.colum3
);
}
}
}
@@ -0,0 +1,235 @@
//-----------------------------------------------------------------------
//-----------------------------------------------------------------------
// file="Matrix32.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.Numerics;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
namespace StgSharp.Math
{
[StructLayout(LayoutKind.Explicit, Size = (5 * 4 * sizeof(float)) + sizeof(bool), Pack = 16)]
public struct Matrix32 : IMat
{
[FieldOffset(5 * 4 * sizeof(float))] internal bool isTransposed;
[FieldOffset(0)] internal Mat2 mat;
[FieldOffset(2 * 4 * sizeof(float))] internal Mat3 transpose;
[FieldOffset(0)] internal vec3d colum0;
[FieldOffset(4 * 4)] internal vec3d colum1;
internal Matrix32(
Vector4 c0,
Vector4 c1
)
{
mat.colum0 = c0;
mat.colum1 = c1;
}
public unsafe Matrix32(
float a00, float a01,
float a10, float a11,
float a20, float a21
)
{
#if NET5_0_OR_GREATER
Unsafe.SkipInit(out colum0);
Unsafe.SkipInit(out colum1);
Unsafe.SkipInit(out transpose);
#endif
mat.colum0 = new Vector4(a00, a10, a20, 0);
mat.colum1 = new Vector4(a01, a11, a21, 0);
}
public unsafe float this[int rowNum, int columNum]
{
get
{
if ((rowNum > 2) || (rowNum < 0))
{
throw new ArgumentOutOfRangeException(nameof(columNum));
}
if ((columNum > 1) || (columNum < 0))
{
throw new ArgumentOutOfRangeException(nameof(columNum));
}
InternalTranspose();
fixed (float* p = &this.transpose.m00)
{
ulong pbit = ((ulong)p)
+ (((ulong)sizeof(Vector4)) * ((ulong)rowNum))
+ (((ulong)sizeof(float)) * ((ulong)columNum));
return *(float*)pbit;
}
}
set
{
if ((rowNum > 2) || (rowNum < 0))
{
throw new ArgumentOutOfRangeException(nameof(columNum));
}
if ((columNum > 1) || (columNum < 0))
{
throw new ArgumentOutOfRangeException(nameof(columNum));
}
InternalTranspose();
fixed (float* p = &this.transpose.m00)
{
ulong pbit = ((ulong)p)
+ (((ulong)sizeof(Vector4)) * ((ulong)rowNum))
+ (((ulong)sizeof(float)) * ((ulong)columNum));
*(float*)pbit = value;
}
isTransposed = false;
}
}
public vec3d VerticalVecMultiply(vec2d vec)
{
return (colum0 * vec.X) + (colum1 * vec.Y);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal unsafe void InternalTranspose()
{
if (!isTransposed)
{
fixed (Mat2* source = &this.mat)
{
fixed (Mat3* target = &this.transpose)
{
InternalIO.Transpose2to3_internal(source, target);
}
}
isTransposed = true;
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Matrix32 operator -(Matrix32 left, Matrix32 right)
{
return new Matrix32(
left.mat.colum0 - right.mat.colum0,
left.mat.colum1 - right.mat.colum1
);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Matrix32 operator *(Matrix32 mat, float value)
{
return new Matrix32(
mat.mat.colum0 * value,
mat.mat.colum1 * value
);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Matrix32 operator *(Matrix32 left, Matrix22 right)
{
left.InternalTranspose();
return new Matrix32(
Vector4.Dot(left.transpose.colum0, right.mat.colum0),
Vector4.Dot(left.transpose.colum0, right.mat.colum1),
Vector4.Dot(left.transpose.colum1, right.mat.colum0),
Vector4.Dot(left.transpose.colum1, right.mat.colum1),
Vector4.Dot(left.transpose.colum2, right.mat.colum0),
Vector4.Dot(left.transpose.colum2, right.mat.colum1)
);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Matrix33 operator *(Matrix32 left, Matrix23 right)
{
left.InternalTranspose();
return new Matrix33(
Vector4.Dot(left.transpose.colum0, right.mat.colum0),
Vector4.Dot(left.transpose.colum0, right.mat.colum1),
Vector4.Dot(left.transpose.colum0, right.mat.colum2),
Vector4.Dot(left.transpose.colum1, right.mat.colum0),
Vector4.Dot(left.transpose.colum1, right.mat.colum1),
Vector4.Dot(left.transpose.colum1, right.mat.colum2),
Vector4.Dot(left.transpose.colum2, right.mat.colum0),
Vector4.Dot(left.transpose.colum2, right.mat.colum1),
Vector4.Dot(left.transpose.colum2, right.mat.colum2)
);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Matrix34 operator *(Matrix32 left, Matrix24 right)
{
left.InternalTranspose();
return new Matrix34(
Vector4.Dot(left.transpose.colum0, right.mat.colum0),
Vector4.Dot(left.transpose.colum0, right.mat.colum1),
Vector4.Dot(left.transpose.colum0, right.mat.colum2),
Vector4.Dot(left.transpose.colum0, right.mat.colum3),
Vector4.Dot(left.transpose.colum1, right.mat.colum0),
Vector4.Dot(left.transpose.colum1, right.mat.colum1),
Vector4.Dot(left.transpose.colum1, right.mat.colum2),
Vector4.Dot(left.transpose.colum1, right.mat.colum3),
Vector4.Dot(left.transpose.colum2, right.mat.colum0),
Vector4.Dot(left.transpose.colum2, right.mat.colum1),
Vector4.Dot(left.transpose.colum2, right.mat.colum2),
Vector4.Dot(left.transpose.colum2, right.mat.colum3)
);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Matrix32 operator /(Matrix32 mat, float value)
{
return new Matrix32(
mat.mat.colum0 / value,
mat.mat.colum1 / value
);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Matrix32 operator +(Matrix32 left, Matrix32 right)
{
return new Matrix32(
left.mat.colum0 + right.mat.colum0,
left.mat.colum1 + right.mat.colum1
);
}
}
}
@@ -0,0 +1,250 @@
//-----------------------------------------------------------------------
//-----------------------------------------------------------------------
// file="Matrix33.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.Numerics;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
namespace StgSharp.Math
{
[StructLayout(LayoutKind.Explicit, Size = (6 * 4 * sizeof(float)) + sizeof(bool), Pack = 16)]
public struct Matrix33 : IMat
{
[FieldOffset(6 * 4 * sizeof(float))] internal bool isTransposed;
[FieldOffset(0)] internal Mat3 mat;
[FieldOffset(3 * 4 * sizeof(float))] internal Mat3 transpose;
[FieldOffset(0 * 4 * sizeof(float))] public vec3d Colum0;
[FieldOffset(1 * 4 * sizeof(float))] public vec3d Colum1;
[FieldOffset(2 * 4 * sizeof(float))] public vec3d Colum2;
internal Matrix33(
Vector4 c0,
Vector4 c1,
Vector4 c2
)
{
mat.colum0 = c0;
mat.colum1 = c1;
mat.colum2 = c2;
}
public Matrix33(
float a00, float a01, float a02,
float a10, float a11, float a12,
float a20, float a21, float a22
)
{
mat.colum0 = new Vector4(a00, a10, a20, 0);
mat.colum1 = new Vector4(a01, a11, a21, 0);
mat.colum2 = new Vector4(a02, a12, a22, 0);
}
public unsafe float this[int rowNum, int columNum]
{
get
{
if ((rowNum > 2) || (rowNum < 0))
{
throw new ArgumentOutOfRangeException(nameof(columNum));
}
if ((columNum > 2) || (columNum < 0))
{
throw new ArgumentOutOfRangeException(nameof(columNum));
}
InternalTranspose();
fixed (float* p = &this.transpose.m00)
{
ulong pbit = ((ulong)p)
+ (((ulong)sizeof(Vector4)) * ((ulong)rowNum))
+ (((ulong)sizeof(float)) * ((ulong)columNum));
return *(float*)pbit;
}
}
set
{
if ((rowNum > 2) || (rowNum < 0))
{
throw new ArgumentOutOfRangeException(nameof(columNum));
}
if ((columNum > 2) || (columNum < 0))
{
InternalIO.InternalWriteLog(
$"Attempt to write unused space in {GetType().Name} {nameof(mat)}",
LogType.Error
);
}
InternalTranspose();
fixed (float* p = &this.transpose.m00)
{
ulong pbit = ((ulong)p)
+ (((ulong)sizeof(Vector4)) * ((ulong)rowNum))
+ (((ulong)sizeof(float)) * ((ulong)columNum));
*(float*)pbit = value;
}
isTransposed = false;
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public unsafe float Det()
{
fixed (Mat3* mat = &this.mat)
{
return InternalIO.det_mat3(mat);
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal unsafe void InternalTranspose()
{
if (!isTransposed)
{
fixed (Mat3* source = &this.mat, target = &this.transpose)
{
InternalIO.Transpose3to3_internal(source, target);
}
isTransposed = true;
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Matrix33 operator -(Matrix33 left, Matrix33 right)
{
return new Matrix33(
left.mat.colum0 - right.mat.colum0,
left.mat.colum1 - right.mat.colum1,
left.mat.colum2 + right.mat.colum2
);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Matrix33 operator *(Matrix33 mat, float value)
{
return new Matrix33(
mat.mat.colum0 * value,
mat.mat.colum1 * value,
mat.mat.colum2 * value
);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static vec3d operator *(Matrix33 left, vec3d right)
{
left.InternalTranspose();
return new vec3d(
Vector4.Dot(left.transpose.colum0, right.vec),
Vector4.Dot(left.transpose.colum1, right.vec),
Vector4.Dot(left.transpose.colum2, right.vec)
);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Matrix32 operator *(Matrix33 left, Matrix32 right)
{
left.InternalTranspose();
return new Matrix32(
Vector4.Dot(left.transpose.colum0, right.mat.colum0),
Vector4.Dot(left.transpose.colum0, right.mat.colum1),
Vector4.Dot(left.transpose.colum1, right.mat.colum0),
Vector4.Dot(left.transpose.colum1, right.mat.colum1),
Vector4.Dot(left.transpose.colum2, right.mat.colum0),
Vector4.Dot(left.transpose.colum2, right.mat.colum1)
);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Matrix33 operator *(Matrix33 left, Matrix33 right)
{
left.InternalTranspose();
return new Matrix33(
Vector4.Dot(left.transpose.colum0, right.mat.colum0),
Vector4.Dot(left.transpose.colum0, right.mat.colum1),
Vector4.Dot(left.transpose.colum0, right.mat.colum2),
Vector4.Dot(left.transpose.colum1, right.mat.colum0),
Vector4.Dot(left.transpose.colum1, right.mat.colum1),
Vector4.Dot(left.transpose.colum1, right.mat.colum2),
Vector4.Dot(left.transpose.colum2, right.mat.colum0),
Vector4.Dot(left.transpose.colum2, right.mat.colum1),
Vector4.Dot(left.transpose.colum2, right.mat.colum2)
);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Matrix34 operator *(Matrix33 left, Matrix34 right)
{
left.InternalTranspose();
return new Matrix34(
Vector4.Dot(left.transpose.colum0, right.mat.colum0),
Vector4.Dot(left.transpose.colum0, right.mat.colum1),
Vector4.Dot(left.transpose.colum0, right.mat.colum2),
Vector4.Dot(left.transpose.colum0, right.mat.colum3),
Vector4.Dot(left.transpose.colum1, right.mat.colum0),
Vector4.Dot(left.transpose.colum1, right.mat.colum1),
Vector4.Dot(left.transpose.colum1, right.mat.colum2),
Vector4.Dot(left.transpose.colum1, right.mat.colum3),
Vector4.Dot(left.transpose.colum2, right.mat.colum0),
Vector4.Dot(left.transpose.colum2, right.mat.colum1),
Vector4.Dot(left.transpose.colum2, right.mat.colum2),
Vector4.Dot(left.transpose.colum2, right.mat.colum3)
);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Matrix33 operator /(Matrix33 mat, float value)
{
return new Matrix33(
mat.mat.colum0 / value,
mat.mat.colum1 / value,
mat.mat.colum2 / value
);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Matrix33 operator +(Matrix33 left, Matrix33 right)
{
return new Matrix33(
left.mat.colum0 + right.mat.colum0,
left.mat.colum1 + right.mat.colum1,
left.mat.colum2 + right.mat.colum2
);
}
}
}
@@ -0,0 +1,248 @@
//-----------------------------------------------------------------------
//-----------------------------------------------------------------------
// file="Matrix34.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.Drawing;
using System.Numerics;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
namespace StgSharp.Math
{
[StructLayout(LayoutKind.Explicit, Size = (7 * 4 * sizeof(float)) + sizeof(bool), Pack = 16)]
public struct Matrix34 : IMat
{
[FieldOffset(7 * 4 * sizeof(float))] internal bool isTransposed;
[FieldOffset(4 * 4 * sizeof(float))] internal Mat3 transpose;
[FieldOffset(0)] internal Mat4 mat;
[FieldOffset(0 * 4 * sizeof(float))] public vec3d Colum0;
[FieldOffset(1 * 4 * sizeof(float))] public vec3d Colum1;
[FieldOffset(2 * 4 * sizeof(float))] public vec3d Colum2;
[FieldOffset(3 * 4 * sizeof(float))] public vec3d Colum3;
internal Matrix34(Mat4 mat)
{
this.mat = mat;
}
internal Matrix34(
Vector4 c0,
Vector4 c1,
Vector4 c2,
Vector4 c3
)
{
mat.colum0 = c0;
mat.colum1 = c1;
mat.colum2 = c2;
mat.colum3 = c3;
}
public Matrix34(
float a00, float a01, float a02, float a03,
float a10, float a11, float a12, float a13,
float a20, float a21, float a22, float a23
)
{
mat.colum0 = new Vector4(a00, a10, a20, 0);
mat.colum0 = new Vector4(a01, a11, a21, 0);
mat.colum0 = new Vector4(a02, a12, a22, 0);
mat.colum0 = new Vector4(a03, a13, a23, 0);
}
public unsafe float this[int rowNum, int columNum]
{
get
{
if ((rowNum > 2) || (rowNum < 0))
{
throw new ArgumentOutOfRangeException(nameof(columNum));
}
if ((columNum > 3) || (columNum < 0))
{
throw new ArgumentOutOfRangeException(nameof(columNum));
}
InternalTranspose();
fixed (float* p = &this.transpose.m00)
{
ulong pbit = ((ulong)p)
+ (((ulong)sizeof(Vector4)) * ((ulong)rowNum))
+ (((ulong)sizeof(float)) * ((ulong)columNum));
return *(float*)pbit;
}
}
set
{
if ((rowNum > 2) || (rowNum < 0))
{
throw new ArgumentOutOfRangeException(nameof(columNum));
}
if ((columNum > 3) || (columNum < 0))
{
throw new ArgumentOutOfRangeException(nameof(columNum));
}
InternalTranspose();
fixed (float* p = &this.transpose.m00)
{
ulong pbit = ((ulong)p)
+ (((ulong)sizeof(Vector4)) * ((ulong)rowNum))
+ (((ulong)sizeof(float)) * ((ulong)columNum));
*(float*)pbit = value;
}
isTransposed = false;
}
}
internal unsafe void InternalTranspose()
{
if (!isTransposed)
{
fixed (Mat4* source = &this.mat)
{
fixed (Mat3* target = &this.transpose)
{
InternalIO.Transpose4to3_internal(source, target);
}
}
isTransposed = true;
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Matrix34 operator -(Matrix34 left, Matrix34 right)
{
return new Matrix34(
left.mat.colum0 - right.mat.colum0,
left.mat.colum1 - right.mat.colum1,
left.mat.colum2 - right.mat.colum2,
left.mat.colum3 - right.mat.colum3
);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Matrix34 operator *(Matrix34 mat, float value)
{
return new Matrix34(
mat.mat.colum0 * value,
mat.mat.colum1 * value,
mat.mat.colum2 * value,
mat.mat.colum3 * value
);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Matrix32 operator *(Matrix34 left, Matrix42 right)
{
left.InternalTranspose();
return new Matrix32(
Vector4.Dot(left.transpose.colum0, right.mat.colum0),
Vector4.Dot(left.transpose.colum0, right.mat.colum1),
Vector4.Dot(left.transpose.colum1, right.mat.colum0),
Vector4.Dot(left.transpose.colum1, right.mat.colum1),
Vector4.Dot(left.transpose.colum2, right.mat.colum0),
Vector4.Dot(left.transpose.colum2, right.mat.colum1)
);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Matrix33 operator *(Matrix34 left, Matrix43 right)
{
left.InternalTranspose();
return new Matrix33(
Vector4.Dot(left.transpose.colum0, right.mat.colum0),
Vector4.Dot(left.transpose.colum0, right.mat.colum1),
Vector4.Dot(left.transpose.colum0, right.mat.colum2),
Vector4.Dot(left.transpose.colum1, right.mat.colum0),
Vector4.Dot(left.transpose.colum1, right.mat.colum1),
Vector4.Dot(left.transpose.colum1, right.mat.colum2),
Vector4.Dot(left.transpose.colum2, right.mat.colum0),
Vector4.Dot(left.transpose.colum2, right.mat.colum1),
Vector4.Dot(left.transpose.colum2, right.mat.colum2)
);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Matrix34 operator *(Matrix34 left, Matrix44 right)
{
left.InternalTranspose();
return new Matrix34(
Vector4.Dot(left.transpose.colum0, right.mat.colum0),
Vector4.Dot(left.transpose.colum0, right.mat.colum1),
Vector4.Dot(left.transpose.colum0, right.mat.colum2),
Vector4.Dot(left.transpose.colum0, right.mat.colum3),
Vector4.Dot(left.transpose.colum1, right.mat.colum0),
Vector4.Dot(left.transpose.colum1, right.mat.colum1),
Vector4.Dot(left.transpose.colum1, right.mat.colum2),
Vector4.Dot(left.transpose.colum1, right.mat.colum3),
Vector4.Dot(left.transpose.colum2, right.mat.colum0),
Vector4.Dot(left.transpose.colum2, right.mat.colum1),
Vector4.Dot(left.transpose.colum2, right.mat.colum2),
Vector4.Dot(left.transpose.colum2, right.mat.colum3)
);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Matrix34 operator /(Matrix34 mat, float value)
{
return new Matrix34(
mat.mat.colum0 / value,
mat.mat.colum1 / value,
mat.mat.colum2 / value,
mat.mat.colum3 / value
);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Matrix34 operator +(Matrix34 left, Matrix34 right)
{
return new Matrix34(
left.mat.colum0 + right.mat.colum0,
left.mat.colum1 + right.mat.colum1,
left.mat.colum2 + right.mat.colum2,
left.mat.colum3 + right.mat.colum3
);
}
}
}
@@ -0,0 +1,253 @@
//-----------------------------------------------------------------------
//-----------------------------------------------------------------------
// file="Matrix42.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.Numerics;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
namespace StgSharp.Math
{
[StructLayout(LayoutKind.Explicit, Size = ((16 + 8) * sizeof(float)) + sizeof(bool), Pack = 16)]
public struct Matrix42 : IMat
{
[FieldOffset((16 + 8) * sizeof(float))] internal bool isTransposed;
[FieldOffset(0)] internal Mat2 mat;
[FieldOffset(8 * sizeof(float))] internal Mat4 transpose;
internal Matrix42(Mat2 mat)
{
this.mat = mat;
}
internal Matrix42(
Vector4 c0,
Vector4 c1
)
{
mat.colum0 = c0;
mat.colum1 = c1;
}
public Matrix42(
float a00, float a01,
float a10, float a11,
float a20, float a21,
float a30, float a31
)
{
mat.colum0 = new Vector4(a00, a10, a20, a30);
mat.colum1 = new Vector4(a01, a11, a21, a31);
}
public unsafe float this[int rowNum, int columNum]
{
get
{
if ((rowNum > 3) || (rowNum < 0))
{
throw new ArgumentOutOfRangeException(nameof(columNum));
}
if ((columNum > 1) || (columNum < 0))
{
throw new ArgumentOutOfRangeException(nameof(columNum));
}
InternalTranspose();
fixed (float* p = &this.transpose.m00)
{
ulong pbit = ((ulong)p)
+ (((ulong)sizeof(Vector4)) * ((ulong)rowNum))
+ (((ulong)sizeof(float)) * ((ulong)columNum));
return *(float*)pbit;
}
}
set
{
if ((rowNum > 3) || (rowNum < 0))
{
throw new ArgumentOutOfRangeException(nameof(columNum));
}
if ((columNum > 1) || (columNum < 0))
{
throw new ArgumentOutOfRangeException(nameof(columNum));
}
InternalTranspose();
fixed (float* p = &this.transpose.m00)
{
ulong pbit = ((ulong)p)
+ (((ulong)sizeof(Vector4)) * ((ulong)rowNum))
+ (((ulong)sizeof(float)) * ((ulong)columNum));
*(float*)pbit = value;
}
isTransposed = false;
}
}
public override int GetHashCode()
{
return HashCode.Combine(
mat.colum0.GetHashCode(),
mat.colum1.GetHashCode()
);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal unsafe void InternalTranspose()
{
if (!isTransposed)
{
fixed (Mat2* source = &this.mat)
{
fixed (Mat4* target = &this.transpose)
{
InternalIO.Transpose2to4_internal(source, target);
}
}
isTransposed = true;
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static unsafe Matrix42 operator -(Matrix42 left, Matrix42 right)
{
return new Matrix42(
left.mat.colum0 - right.mat.colum0,
left.mat.colum0 - right.mat.colum0
);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Matrix42 operator *(Matrix42 mat, float value)
{
return new Matrix42(
mat.mat.colum0 * value,
mat.mat.colum1 * value
);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Matrix42 operator *(Matrix42 left, Matrix22 right)
{
left.InternalTranspose();
return new Matrix42(
Vector4.Dot(left.transpose.colum0, right.mat.colum0),
Vector4.Dot(left.transpose.colum0, right.mat.colum1),
Vector4.Dot(left.transpose.colum1, right.mat.colum0),
Vector4.Dot(left.transpose.colum1, right.mat.colum1),
Vector4.Dot(left.transpose.colum2, right.mat.colum0),
Vector4.Dot(left.transpose.colum2, right.mat.colum1),
Vector4.Dot(left.transpose.colum3, right.mat.colum0),
Vector4.Dot(left.transpose.colum3, right.mat.colum1)
);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Matrix43 operator *(Matrix42 left, Matrix23 right)
{
left.InternalTranspose();
return new Matrix43(
Vector4.Dot(left.transpose.colum0, right.mat.colum0),
Vector4.Dot(left.transpose.colum0, right.mat.colum1),
Vector4.Dot(left.transpose.colum0, right.mat.colum2),
Vector4.Dot(left.transpose.colum1, right.mat.colum0),
Vector4.Dot(left.transpose.colum1, right.mat.colum1),
Vector4.Dot(left.transpose.colum1, right.mat.colum2),
Vector4.Dot(left.transpose.colum2, right.mat.colum0),
Vector4.Dot(left.transpose.colum2, right.mat.colum1),
Vector4.Dot(left.transpose.colum2, right.mat.colum2),
Vector4.Dot(left.transpose.colum3, right.mat.colum0),
Vector4.Dot(left.transpose.colum3, right.mat.colum1),
Vector4.Dot(left.transpose.colum3, right.mat.colum2)
);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Matrix44 operator *(Matrix42 left, Matrix24 right)
{
left.InternalTranspose();
return new Matrix44(
Vector4.Dot(left.transpose.colum0, right.mat.colum0),
Vector4.Dot(left.transpose.colum0, right.mat.colum1),
Vector4.Dot(left.transpose.colum0, right.mat.colum2),
Vector4.Dot(left.transpose.colum0, right.mat.colum2),
Vector4.Dot(left.transpose.colum1, right.mat.colum0),
Vector4.Dot(left.transpose.colum1, right.mat.colum1),
Vector4.Dot(left.transpose.colum1, right.mat.colum2),
Vector4.Dot(left.transpose.colum1, right.mat.colum2),
Vector4.Dot(left.transpose.colum2, right.mat.colum0),
Vector4.Dot(left.transpose.colum2, right.mat.colum1),
Vector4.Dot(left.transpose.colum2, right.mat.colum2),
Vector4.Dot(left.transpose.colum2, right.mat.colum2),
Vector4.Dot(left.transpose.colum3, right.mat.colum0),
Vector4.Dot(left.transpose.colum3, right.mat.colum1),
Vector4.Dot(left.transpose.colum3, right.mat.colum2),
Vector4.Dot(left.transpose.colum3, right.mat.colum2)
);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Matrix42 operator /(Matrix42 mat, float value)
{
return new Matrix42(
mat.mat.colum0 / value,
mat.mat.colum1 / value
);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static unsafe Matrix42 operator +(Matrix42 left, Matrix42 right)
{
return new Matrix42(
left.mat.colum0 + right.mat.colum0,
left.mat.colum0 + right.mat.colum0
);
}
}
}
@@ -0,0 +1,277 @@
//-----------------------------------------------------------------------
//-----------------------------------------------------------------------
// file="Matrix43.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.Numerics;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
namespace StgSharp.Math
{
[StructLayout(LayoutKind.Explicit, Size = (7 * 4 * sizeof(float)) + sizeof(bool), Pack = 16)]
public struct Matrix43 : IEquatable<Matrix43>, IMat
{
[FieldOffset(7 * 4 * sizeof(float))]
internal bool isTransposed;
[FieldOffset(0)]
internal Mat3 mat;
[FieldOffset(3 * 4 * sizeof(float))]
internal Mat4 transpose;
internal Matrix43(
Vector4 c0,
Vector4 c1,
Vector4 c2
)
{
mat.colum0 = c0;
mat.colum1 = c1;
mat.colum2 = c2;
}
public Matrix43(
float a00, float a01, float a02,
float a10, float a11, float a12,
float a20, float a21, float a22,
float a30, float a31, float a32
)
{
mat.colum0 = new Vector4(a00, a10, a20, a30);
mat.colum1 = new Vector4(a01, a11, a21, a31);
mat.colum2 = new Vector4(a02, a12, a22, a32);
}
public unsafe float this[int rowNum, int columNum]
{
get
{
if ((rowNum > 3) || (rowNum < 0))
{
throw new ArgumentOutOfRangeException(nameof(columNum));
}
if ((columNum > 2) || (columNum < 0))
{
throw new ArgumentOutOfRangeException(nameof(columNum));
}
InternalTranspose();
fixed (float* p = &this.transpose.m00)
{
ulong pbit = ((ulong)p)
+ (((ulong)sizeof(Vector4)) * ((ulong)rowNum))
+ (((ulong)sizeof(float)) * ((ulong)columNum));
return *(float*)pbit;
}
}
set
{
if ((rowNum > 3) || (rowNum < 0))
{
throw new ArgumentOutOfRangeException(nameof(columNum));
}
if ((columNum > 2) || (columNum < 0))
{
throw new ArgumentOutOfRangeException(nameof(columNum));
}
InternalTranspose();
fixed (float* p = &this.transpose.m00)
{
ulong pbit = ((ulong)p)
+ (((ulong)sizeof(Vector4)) * ((ulong)rowNum))
+ (((ulong)sizeof(float)) * ((ulong)columNum));
*(float*)pbit = value;
}
isTransposed = false;
}
}
public override bool Equals(object? obj)
{
return (obj is Matrix43 x) && Equals(x);
}
public bool Equals(Matrix43 other)
{
return EqualityComparer<Mat3>.Default.Equals(mat, other.mat);
}
public override int GetHashCode()
{
return HashCode.Combine(
mat.colum0.GetHashCode(),
mat.colum1.GetHashCode(),
mat.colum2.GetHashCode()
);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal unsafe void InternalTranspose()
{
if (!isTransposed)
{
fixed (Mat3* source = &this.mat)
{
fixed (Mat4* target = &this.transpose)
{
InternalIO.Transpose3to4_internal(source, target);
}
}
isTransposed = true;
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Matrix43 operator -(Matrix43 left, Matrix43 right)
{
return new Matrix43(
left.mat.colum0 - right.mat.colum0,
left.mat.colum1 - right.mat.colum1,
left.mat.colum2 - right.mat.colum2
);
}
public static bool operator !=(Matrix43 left, Matrix43 right)
{
return !(left == right);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Matrix43 operator *(Matrix43 mat, float value)
{
return new Matrix43(
mat.mat.colum0 * value,
mat.mat.colum1 * value,
mat.mat.colum2 * value
);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Matrix42 operator *(Matrix43 left, Matrix32 right)
{
left.InternalTranspose();
return new Matrix42(
Vector4.Dot(left.transpose.colum0, right.mat.colum0),
Vector4.Dot(left.transpose.colum0, right.mat.colum1),
Vector4.Dot(left.transpose.colum1, right.mat.colum0),
Vector4.Dot(left.transpose.colum1, right.mat.colum1),
Vector4.Dot(left.transpose.colum2, right.mat.colum0),
Vector4.Dot(left.transpose.colum2, right.mat.colum1),
Vector4.Dot(left.transpose.colum3, right.mat.colum0),
Vector4.Dot(left.transpose.colum3, right.mat.colum1)
);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Matrix43 operator *(Matrix43 left, Matrix33 right)
{
left.InternalTranspose();
return new Matrix43(
Vector4.Dot(left.transpose.colum0, right.mat.colum0),
Vector4.Dot(left.transpose.colum0, right.mat.colum1),
Vector4.Dot(left.transpose.colum0, right.mat.colum2),
Vector4.Dot(left.transpose.colum1, right.mat.colum0),
Vector4.Dot(left.transpose.colum1, right.mat.colum1),
Vector4.Dot(left.transpose.colum1, right.mat.colum2),
Vector4.Dot(left.transpose.colum2, right.mat.colum0),
Vector4.Dot(left.transpose.colum2, right.mat.colum1),
Vector4.Dot(left.transpose.colum2, right.mat.colum2),
Vector4.Dot(left.transpose.colum3, right.mat.colum0),
Vector4.Dot(left.transpose.colum3, right.mat.colum1),
Vector4.Dot(left.transpose.colum3, right.mat.colum2)
);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Matrix44 operator *(Matrix43 left, Matrix34 right)
{
left.InternalTranspose();
return new Matrix44(
Vector4.Dot(left.transpose.colum0, right.mat.colum0),
Vector4.Dot(left.transpose.colum0, right.mat.colum1),
Vector4.Dot(left.transpose.colum0, right.mat.colum2),
Vector4.Dot(left.transpose.colum0, right.mat.colum3),
Vector4.Dot(left.transpose.colum1, right.mat.colum0),
Vector4.Dot(left.transpose.colum1, right.mat.colum1),
Vector4.Dot(left.transpose.colum1, right.mat.colum2),
Vector4.Dot(left.transpose.colum1, right.mat.colum3),
Vector4.Dot(left.transpose.colum2, right.mat.colum0),
Vector4.Dot(left.transpose.colum2, right.mat.colum1),
Vector4.Dot(left.transpose.colum2, right.mat.colum2),
Vector4.Dot(left.transpose.colum2, right.mat.colum3),
Vector4.Dot(left.transpose.colum3, right.mat.colum0),
Vector4.Dot(left.transpose.colum3, right.mat.colum1),
Vector4.Dot(left.transpose.colum3, right.mat.colum2),
Vector4.Dot(left.transpose.colum3, right.mat.colum3)
);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Matrix43 operator /(Matrix43 mat, float value)
{
return new Matrix43(
mat.mat.colum0 / value,
mat.mat.colum1 / value,
mat.mat.colum2 / value
);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Matrix43 operator +(Matrix43 left, Matrix43 right)
{
return new Matrix43(
left.mat.colum0 + right.mat.colum0,
left.mat.colum1 + right.mat.colum1,
left.mat.colum2 + right.mat.colum2
);
}
public static bool operator ==(Matrix43 left, Matrix43 right)
{
return left.Equals(right);
}
}
}
@@ -0,0 +1,351 @@
//-----------------------------------------------------------------------
//-----------------------------------------------------------------------
// file="Matrix44.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.ComponentModel;
using System.Drawing;
using System.Numerics;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
namespace StgSharp.Math
{
[StructLayout(LayoutKind.Explicit, Size = (2 * 16 * sizeof(float)) + sizeof(bool), Pack = 16)]
public unsafe struct Matrix44 : IEquatable<Matrix44>, IMat
{
[FieldOffset(2 * 64 * sizeof(float))] internal bool isTransposed;
[FieldOffset(0)] internal Mat4 mat;
[FieldOffset(16 * sizeof(float))] internal Mat4 transpose;
[FieldOffset(0 * sizeof(float))] internal vec4d colum0;
[FieldOffset(4 * sizeof(float))] internal vec4d colum1;
[FieldOffset(8 * sizeof(float))] internal vec4d colum2;
[FieldOffset(12 * sizeof(float))] internal vec4d colum3;
internal Matrix44(Mat4 mat)
{
this.mat = mat;
}
internal Matrix44(
Vector4 vec0,
Vector4 vec1,
Vector4 vec2,
Vector4 vec3
)
{
mat.colum0 = vec0;
mat.colum1 = vec1;
mat.colum2 = vec2;
mat.colum3 = vec3;
}
public unsafe Matrix44(
float a00, float a01, float a02, float a03,
float a10, float a11, float a12, float a13,
float a20, float a21, float a22, float a23,
float a30, float a31, float a32, float a33
)
{
mat.colum0 = new Vector4(a00, a10, a20, a30);
mat.colum1 = new Vector4(a01, a11, a21, a31);
mat.colum2 = new Vector4(a02, a12, a22, a32);
mat.colum3 = new Vector4(a03, a13, a23, a33);
}
public unsafe float this[int rowNum, int columNum]
{
get
{
#if DEBUG
if ((rowNum > 3) || (rowNum < 0))
{
throw new ArgumentOutOfRangeException(nameof(columNum));
}
if ((columNum > 3) || (columNum < 0))
{
throw new ArgumentOutOfRangeException(nameof(columNum));
}
#endif
InternalTranspose();
fixed (float* p = &this.transpose.m00)
{
ulong pbit = ((ulong)p)
+ (((ulong)sizeof(Vector4)) * ((ulong)rowNum))
+ (((ulong)sizeof(float)) * ((ulong)columNum));
return *(float*)pbit;
}
}
set
{
#if DEBUG
if ((rowNum > 3) || (rowNum < 0))
{
throw new ArgumentOutOfRangeException(nameof(columNum));
}
if ((columNum > 3) || (columNum < 0))
{
throw new ArgumentOutOfRangeException(nameof(columNum));
}
#endif
InternalTranspose();
fixed (float* p = &this.transpose.m00)
{
ulong pbit = ((ulong)p)
+ (((ulong)sizeof(Vector4)) * ((ulong)rowNum))
+ (((ulong)sizeof(float)) * ((ulong)columNum));
*(float*)pbit = value;
}
isTransposed = false;
}
}
public Matrix44 Transpose
{
get
{
InternalTranspose();
return new Matrix44(this.transpose);
}
}
public static Matrix44 Unit
=> new Matrix44(
1, 0, 0, 0,
0, 1, 0, 0,
0, 0, 1, 0,
0, 0, 0, 1
);
public static Matrix44 WNegativeUnit
=> new Matrix44(
1, 0, 0, 0,
0, 1, 0, 0,
0, 0, 1, 0,
0, 0, 0, -1
);
public static Matrix44 XNegativeUnit
=> new Matrix44(
-1, 0, 0, 0,
0, 1, 0, 0,
0, 0, 1, 0,
0, 0, 0, 1
);
public static Matrix44 YNegativeUnit
=> new Matrix44(
1, 0, 0, 0,
0, -1, 0, 0,
0, 0, 1, 0,
0, 0, 0, 1
);
public static Matrix44 ZNegativeUnit
=> new Matrix44(
1, 0, 0, 0,
0, 1, 0, 0,
0, 0, -1, 0,
0, 0, 0, 1
);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public unsafe float Det()
{
InternalTranspose();
fixed (Mat4* _this = &mat, _trans = &transpose)
{
return InternalIO.det_mat4(_this, _trans);
}
}
public override bool Equals(object? obj)
{
return (obj is Matrix44 x) && Equals(x);
}
public bool Equals(Matrix44 other)
{
return mat.Equals(other.mat);
}
public override int GetHashCode()
{
return HashCode.Combine(
mat.colum0.GetHashCode(),
mat.colum1.GetHashCode(),
mat.colum2.GetHashCode(),
mat.colum3.GetHashCode()
);
}
public override string ToString()
{
InternalTranspose();
return $"{transpose.colum0}{transpose.colum1}{transpose.colum2}{transpose.colum3}";
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal unsafe void InternalTranspose()
{
if (!isTransposed)
{
fixed (Mat4* source = &mat, target = &transpose)
{
InternalIO.Transpose4to4_internal(source, target);
}
isTransposed = true;
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static unsafe Matrix44 operator -(Matrix44 left, Matrix44 right)
{
return new Matrix44(
right.mat.colum0 - left.mat.colum0,
right.mat.colum1 - left.mat.colum1,
right.mat.colum2 - left.mat.colum2,
right.mat.colum3 - left.mat.colum3
);
}
public static bool operator !=(Matrix44 left, Matrix44 right)
{
return !(left == right);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static unsafe Matrix44 operator *(Matrix44 mat, float value)
{
return new Matrix44(
mat.mat.colum0 * value,
mat.mat.colum1 * value,
mat.mat.colum2 * value,
mat.mat.colum3 * value
);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static unsafe Matrix42 operator *(Matrix44 left, Matrix42 right)
{
left.InternalTranspose();
return new Matrix42(
Vector4.Dot(left.transpose.colum0, right.mat.colum0),
Vector4.Dot(left.transpose.colum0, right.mat.colum1),
Vector4.Dot(left.transpose.colum1, right.mat.colum0),
Vector4.Dot(left.transpose.colum1, right.mat.colum1),
Vector4.Dot(left.transpose.colum2, right.mat.colum0),
Vector4.Dot(left.transpose.colum2, right.mat.colum1),
Vector4.Dot(left.transpose.colum3, right.mat.colum0),
Vector4.Dot(left.transpose.colum3, right.mat.colum1)
);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Matrix43 operator *(Matrix44 left, Matrix43 right)
{
left.InternalTranspose();
return new Matrix43(
Vector4.Dot(left.transpose.colum0, right.mat.colum0),
Vector4.Dot(left.transpose.colum0, right.mat.colum1),
Vector4.Dot(left.transpose.colum0, right.mat.colum2),
Vector4.Dot(left.transpose.colum1, right.mat.colum0),
Vector4.Dot(left.transpose.colum1, right.mat.colum1),
Vector4.Dot(left.transpose.colum1, right.mat.colum2),
Vector4.Dot(left.transpose.colum2, right.mat.colum0),
Vector4.Dot(left.transpose.colum2, right.mat.colum1),
Vector4.Dot(left.transpose.colum2, right.mat.colum2),
Vector4.Dot(left.transpose.colum3, right.mat.colum0),
Vector4.Dot(left.transpose.colum3, right.mat.colum1),
Vector4.Dot(left.transpose.colum3, right.mat.colum2)
);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Matrix44 operator *(Matrix44 left, Matrix44 right)
{
left.InternalTranspose();
return new Matrix44(
Vector4.Dot(left.transpose.colum0, right.mat.colum0),
Vector4.Dot(left.transpose.colum0, right.mat.colum1),
Vector4.Dot(left.transpose.colum0, right.mat.colum2),
Vector4.Dot(left.transpose.colum0, right.mat.colum3),
Vector4.Dot(left.transpose.colum1, right.mat.colum0),
Vector4.Dot(left.transpose.colum1, right.mat.colum1),
Vector4.Dot(left.transpose.colum1, right.mat.colum2),
Vector4.Dot(left.transpose.colum1, right.mat.colum3),
Vector4.Dot(left.transpose.colum2, right.mat.colum0),
Vector4.Dot(left.transpose.colum2, right.mat.colum1),
Vector4.Dot(left.transpose.colum2, right.mat.colum2),
Vector4.Dot(left.transpose.colum2, right.mat.colum3),
Vector4.Dot(left.transpose.colum3, right.mat.colum0),
Vector4.Dot(left.transpose.colum3, right.mat.colum1),
Vector4.Dot(left.transpose.colum3, right.mat.colum2),
Vector4.Dot(left.transpose.colum3, right.mat.colum3)
);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static unsafe Matrix44 operator +(Matrix44 left, Matrix44 right)
{
return new Matrix44(
right.mat.colum0 + left.mat.colum0,
right.mat.colum1 + left.mat.colum1,
right.mat.colum2 + left.mat.colum2,
right.mat.colum3 + left.mat.colum3
);
}
public static bool operator ==(Matrix44 left, Matrix44 right)
{
return left.Equals(right);
}
}
}
@@ -0,0 +1,51 @@
//-----------------------------------------------------------------------
//-----------------------------------------------------------------------
// file="Vector.Cross.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.Math
{
public static unsafe partial class Linear
{
public static float Cross(vec2d vec1, vec2d vec2)
{
return (vec1.X * vec2.Y) - (vec1.Y * vec2.X);
}
public static vec3d Cross(vec3d vec1, vec3d vec2)
{
return new vec3d(
(vec1.Y * vec2.Z) - (vec1.Z * vec2.Y),
(vec1.X * vec2.Z) - (vec1.Z * vec2.X),
(vec1.X * vec2.Y) - (vec1.Y * vec2.X)
);
}
}
}
@@ -0,0 +1,53 @@
//-----------------------------------------------------------------------
//-----------------------------------------------------------------------
// 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.Math
{
public static unsafe partial class Linear
{
public static float GetAngle(vec2d vec1, vec2d vec2)
{
return Scaler.ACos(1
//vec1.multiple(vec2)
/ (vec1.GetLength() * vec2.GetLength())
);
}
public static float GetAngle(vec3d vec1, vec3d vec2)
{
return Scaler.ACos(
(vec1 * vec2)
/ (vec1.GetLength() * vec2.GetLength())
);
}
}
}
@@ -0,0 +1,47 @@
//-----------------------------------------------------------------------
//-----------------------------------------------------------------------
// 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.Math
{
public static unsafe partial class Linear
{
public static float GetLength(this vec3d vec)
{
return Scaler.Sqrt(vec.Dot(vec));
}
public static float GetLength(this vec2d vec)
{
return Scaler.Sqrt(vec.Dot(vec));
}
}
}
@@ -0,0 +1,55 @@
using StgSharp.Data;
using StgSharp.Math;
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.Math
{
public static partial class Linear
{
public static unsafe void Normalize(ref vec4d source, ref vec4d 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 vec3d source, ref vec3d 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;
}
public static vec4d Normalize(vec4d vec)
{
return new vec4d() { vec = Vector4.Normalize(vec.vec) };
}
public static vec3d Normalize(vec3d vec)
{
return new vec3d() { vec = Vector4.Normalize(vec.vec) };
}
}
}
@@ -0,0 +1,106 @@
//-----------------------------------------------------------------------
//-----------------------------------------------------------------------
// file="Vector.Orthogonalize.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.Data;
using StgSharp.Math;
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
{
internal static partial class InternalIO
{
[DllImport(SSC_libName, EntryPoint = "normalize",
CallingConvention = CallingConvention.Cdecl)]
internal static extern unsafe void Normalize(M128* source, M128* target);
}
}
namespace StgSharp.Math
{
public static partial class Linear
{
public static vec4d Orthogonalize(vec4d vec)
{
return new vec4d { vec = Vector4.Normalize(vec.vec) };
}
public static vec3d Orthogonalize(vec3d vec)
{
return new vec3d { vec = Vector4.Normalize(vec.vec) };
}
public static unsafe void Orthogonalize(ref vec4d source, ref vec4d 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;
}
/// <summary>
/// Orthogonalize two <see cref="vec3d"/>s, let their length be 1
/// and perpendicular to each other.
/// </summary>
/// <param name="source"></param>
/// <param name="target"></param>
public static unsafe void Orthogonalize(ref vec3d source, ref vec3d 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;
}
}
}
+144
View File
@@ -0,0 +1,144 @@
//-----------------------------------------------------------------------
//-----------------------------------------------------------------------
// file="mat2.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.Math;
using System;
using System.Numerics;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
namespace StgSharp
{
internal static unsafe partial class InternalIO
{
#region add
[DllImport(InternalIO.SSC_libName, EntryPoint = "add_Mat2",
CallingConvention = CallingConvention.Cdecl)]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static extern Mat2* add_mat2(Mat2* left, Mat2* right);
#endregion
#region deinit mat2
[DllImport(InternalIO.SSC_libName, EntryPoint = "deinit_mat2",
CallingConvention = CallingConvention.Cdecl)]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static extern unsafe void deinit_mat2(Mat2* mat);
#endregion
#region sub
[DllImport(InternalIO.SSC_libName, EntryPoint = "sub_mat2",
CallingConvention = CallingConvention.Cdecl)]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static extern unsafe Mat2* sub_mat2(Mat2* left, Mat2* right);
#endregion
#region transpose
[DllImport(InternalIO.SSC_libName, EntryPoint = "transpose2to4",
CallingConvention = CallingConvention.Cdecl)]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static extern unsafe void Transpose2to4_internal(Mat2* source, Mat4* target);
[DllImport(InternalIO.SSC_libName, EntryPoint = "transpose2to3",
CallingConvention = CallingConvention.Cdecl)]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static extern unsafe void Transpose2to3_internal(Mat2* source, Mat3* target);
#endregion
}
}
namespace StgSharp.Math
{
[StructLayout(LayoutKind.Explicit, Size = 12 * sizeof(float), Pack = 16)]
internal struct Mat2 : IEquatable<Mat2>
{
[FieldOffset(0 * sizeof(float))] internal float m00;
[FieldOffset(4 * sizeof(float))] internal float m01;
[FieldOffset(1 * sizeof(float))] internal float m10;
[FieldOffset(5 * sizeof(float))] internal float m11;
[FieldOffset(2 * sizeof(float))] internal float m20;
[FieldOffset(6 * sizeof(float))] internal float m21;
[FieldOffset(3 * sizeof(float))] internal float m30;
[FieldOffset(7 * sizeof(float))] internal float m31;
[FieldOffset(0)] internal Vector4 colum0;
[FieldOffset(4 * sizeof(float))] internal Vector4 colum1;
internal Mat2(
Vector4 c0,
Vector4 c1
)
{
colum0 = c0;
colum1 = c1;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public override bool Equals(object? obj)
{
return (obj is Mat2 mat) && Equals(mat);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool Equals(Mat2 other)
{
return colum0.Equals(other.colum0) &&
colum1.Equals(other.colum1);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool operator !=(Mat2 left, Mat2 right)
{
return !(left == right);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool operator ==(Mat2 left, Mat2 right)
{
return left.Equals(right);
}
}
}
+126
View File
@@ -0,0 +1,126 @@
//-----------------------------------------------------------------------
//-----------------------------------------------------------------------
// file="mat3.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.Math;
using System;
using System.Numerics;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
namespace StgSharp
{
internal static unsafe partial class InternalIO
{
#region det
[DllImport(InternalIO.SSC_libName, EntryPoint = "det_mat3",
CallingConvention = CallingConvention.Cdecl)]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static extern unsafe float det_mat3(Mat3* mat);
#endregion
#region transpose
[DllImport(SSC_libName, EntryPoint = "transpose3to4",
CallingConvention = CallingConvention.Cdecl)]
internal static extern unsafe void Transpose3to4_internal(Mat3* source, Mat4* target);
[DllImport(SSC_libName, EntryPoint = "transpose3to3",
CallingConvention = CallingConvention.Cdecl)]
internal static extern unsafe void Transpose3to3_internal(Mat3* source, Mat3* target);
[DllImport(SSC_libName, EntryPoint = "transpose3to2",
CallingConvention = CallingConvention.Cdecl)]
internal static extern unsafe void Transpose3to2_internal(Mat3* source, Mat2* target);
#endregion
}
}
namespace StgSharp.Math
{
[StructLayout(LayoutKind.Explicit, Size = 12 * sizeof(float), Pack = 16)]
public struct Mat3 : IEquatable<Mat3>, IMat
{
[FieldOffset(0 * sizeof(float))] internal float m00;
[FieldOffset(4 * sizeof(float))] internal float m01;
[FieldOffset(8 * sizeof(float))] internal float m02;
[FieldOffset(1 * sizeof(float))] internal float m10;
[FieldOffset(5 * sizeof(float))] internal float m11;
[FieldOffset(9 * sizeof(float))] internal float m12;
[FieldOffset(2 * sizeof(float))] internal float m20;
[FieldOffset(6 * sizeof(float))] internal float m21;
[FieldOffset(10 * sizeof(float))] internal float m22;
[FieldOffset(3 * sizeof(float))] internal float m30;
[FieldOffset(7 * sizeof(float))] internal float m31;
[FieldOffset(11 * sizeof(float))] internal float m32;
[FieldOffset(0)] internal Vector4 colum0;
[FieldOffset(4 * sizeof(float))] internal Vector4 colum1;
[FieldOffset(8 * sizeof(float))] internal Vector4 colum2;
public override bool Equals(object? obj)
{
return (obj is Mat3 mat) && Equals(mat);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool Equals(Mat3 other)
{
return colum0.Equals(other.colum0) &&
colum1.Equals(other.colum1) &&
colum2.Equals(other.colum2);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool operator !=(Mat3 left, Mat3 right)
{
return !(left == right);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool operator ==(Mat3 left, Mat3 right)
{
return left.Equals(right);
}
}
}
+159
View File
@@ -0,0 +1,159 @@
//-----------------------------------------------------------------------
//-----------------------------------------------------------------------
// file="mat4.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;
using StgSharp.Math;
using System;
using System.Numerics;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
namespace StgSharp
{
internal static unsafe partial class InternalIO
{
#region deinit_mat4
[DllImport(SSC_libName, EntryPoint = "deinit_mat4",
CallingConvention = CallingConvention.Cdecl)]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static extern void deinit_mat4(Mat4* mat);
#endregion
#region det
[DllImport(SSC_libName, EntryPoint = "det_mat4",
CallingConvention = CallingConvention.Cdecl)]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static extern unsafe float det_mat4(Mat4* matPtr, Mat4* transpose);
#endregion
#region transpose
[DllImport(SSC_libName, EntryPoint = "transpose4to4",
CallingConvention = CallingConvention.Cdecl)]
internal static extern unsafe void Transpose4to4_internal(Mat4* source, Mat4* target);
[DllImport(SSC_libName, EntryPoint = "transpose4to3",
CallingConvention = CallingConvention.Cdecl)]
internal static extern unsafe void Transpose4to3_internal(Mat4* source, Mat3* target);
[DllImport(SSC_libName, EntryPoint = "transpose4to2",
CallingConvention = CallingConvention.Cdecl)]
internal static extern unsafe void Transpose4to2_internal(Mat4* source, Mat2* target);
#endregion
}
}
namespace StgSharp.Math
{
[StructLayout(LayoutKind.Explicit, Size = 16 * sizeof(float))]
public struct Mat4 : IEquatable<Mat4>, IMat
{
[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;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal Mat4(
Vector4 colum0,
Vector4 colum1,
Vector4 colum2,
Vector4 colum3
)
{
this.colum0 = colum0;
this.colum1 = colum1;
this.colum2 = colum2;
this.colum3 = colum3;
}
public override bool Equals(object? obj)
{
return (obj is Mat4 mat) && Equals(mat);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool Equals(Mat4 other)
{
return colum0.Equals(other.colum0) &&
colum1.Equals(other.colum1) &&
colum2.Equals(other.colum2) &&
colum3.Equals(other.colum3);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool operator !=(Mat4 left, Mat4 right)
{
return !(left == right);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool operator ==(Mat4 left, Mat4 right)
{
return left.Equals(right);
}
}
}
@@ -0,0 +1,50 @@
//-----------------------------------------------------------------------
//-----------------------------------------------------------------------
// file="mat5.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.Numerics;
using System.Runtime.InteropServices;
using System.Text;
namespace StgSharp.Math
{
[StructLayout(LayoutKind.Explicit, Size = 5 * 4 * sizeof(float))]
public struct mat5 : IMat
{
[FieldOffset(0 * 4 * sizeof(float))] internal Vector4 vec0;
[FieldOffset(1 * 4 * sizeof(float))] internal Vector4 vec1;
[FieldOffset(2 * 4 * sizeof(float))] internal Vector4 vec2;
[FieldOffset(3 * 4 * sizeof(float))] internal Vector4 vec3;
[FieldOffset(4 * 4 * sizeof(float))] internal Vector4 vec4;
}
}
@@ -0,0 +1,51 @@
//-----------------------------------------------------------------------
//-----------------------------------------------------------------------
// file="mat6.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.Numerics;
using System.Runtime.InteropServices;
using System.Text;
namespace StgSharp.Math
{
[StructLayout(LayoutKind.Sequential)]
public struct mat6 : IMat
{
internal Vector4 vec0;
internal Vector4 vec1;
internal Vector4 vec2;
internal Vector4 vec3;
internal Vector4 vec4;
internal Vector4 vec5;
}
}
@@ -0,0 +1,138 @@
//-----------------------------------------------------------------------
//-----------------------------------------------------------------------
// file="vec2d.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.Numerics;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
namespace StgSharp.Math
{
/// <summary>
/// A two dimension vector defined by two elements.
/// Vec2Ds in StgSharp are default used as colum vector.
/// </summary>
[StructLayout(LayoutKind.Explicit, Size = 16, Pack = 16)]
public struct vec2d:IVector
{
[FieldOffset(0)] internal unsafe fixed float num[2];
[FieldOffset(0)]
internal Vector2 v;
[FieldOffset(0)]
internal Vector4 vec;
[FieldOffset(0)]
public float X;
[FieldOffset(4)]
public float Y;
internal vec2d(Vector2 vec)
{
v = vec;
}
internal vec2d(Vector4 vec)
{
this.vec = vec;
}
public vec2d(float x, float y)
{
v = new Vector2(x, y);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public float Cross(vec2d right)
{
return (X * right.Y) - (Y * right.X);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public float Dot(vec2d vec)
{
return Vector2.Dot(v, vec.v);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static vec2d operator -(vec2d left, vec2d right)
{
return new vec2d(left.v - right.v);
}
public static bool operator !=(vec2d left, vec2d right)
{
return !(left == right);
}
public static vec2d operator *(vec2d vec, float value)
{
return new vec2d(vec.v * value);
}
public static vec2d operator /(vec2d vec, float value)
{
return new vec2d(vec.v / value);
}
public static vec2d operator *(float value, vec2d vec)
{
return new vec2d(vec.v * value);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static vec2d operator +(vec2d left, vec2d right)
{
return new vec2d(left.v + right.v);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool operator ==(vec2d left, vec2d right)
{
return left.v == right.v;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static implicit operator vec2d((float, float) tuple)
{
return new vec2d(tuple.Item1, tuple.Item2);
}
}
public static class Vec2d
{
public static vec2d Unit => new vec2d(1, 1);
public static vec2d Zero => new vec2d(0, 0);
}
}
@@ -0,0 +1,239 @@
//-----------------------------------------------------------------------
//-----------------------------------------------------------------------
// file="vec3d.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.Data;
using System;
using System.Numerics;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Security.AccessControl;
namespace StgSharp.Math
{
[StructLayout(LayoutKind.Explicit, Size = 16, Pack = 16)]
public struct vec3d : IEquatable<vec3d>, IVector
{
[FieldOffset(0)] internal unsafe fixed float num[3];
[FieldOffset(0)]
internal M128 reg;
[FieldOffset(0)]
internal Vector3 v;
[FieldOffset(0)]
internal Vector4 vec;
[FieldOffset(0)] public float X;
[FieldOffset(4)] public float Y;
[FieldOffset(8)] public float Z;
internal vec3d(Vector4 vector)
{
vec = vector;
}
internal vec3d(Vector3 vector)
{
v = vector;
}
public vec3d(vec2d xy, float z)
{
vec = xy.vec;
Z = z;
}
public vec3d(
float x, float y, float z)
{
#if NET5_0_OR_GREATER
Unsafe.SkipInit(out vec);
#endif
v = new Vector3(x, y, z);
}
public vec3d XYZ
{
get { return new vec3d(v); }
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public float Dot(vec3d right)
{
return Vector3.Dot(v, right.v);
}
public bool Equals(vec3d other)
{
return this.vec == other.vec;
}
public override bool Equals(object obj)
{
if (obj is vec3d)
{
return this == (vec3d)obj;
}
else
{
return false;
}
}
public override unsafe int GetHashCode()
{
fixed (float* add = &X)
{
return HashCode.Combine(X, Y, Z, (ulong)add);
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public float GetMaxValue()
{
float ret = X;
ret = (ret > Y) ? ret : Y;
ret = (ret > Z) ? ret : Z;
return ret;
}
public override string ToString()
{
return $"[{X},{Y},{Z}]";
}
public string ToString(string sample)
{
return $"[{X.ToString(sample)},{Y.ToString(sample)},{Z.ToString(sample)}]";
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public vec3d Zero()
{
return new vec3d(0, 0, 0);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static vec3d operator -(vec3d vec)
{
return new vec3d(-vec.vec);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static vec3d operator -(vec3d left, vec3d right)
{
return new vec3d(left.v - right.v);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool operator !=(vec3d left, vec3d right)
{
return left.v != right.v;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static vec3d operator *(vec3d vec, float value)
{
return new vec3d(vec.v * value);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static float operator *(vec3d left, vec3d right)
{
return Vector3.Dot(left.v, right.v);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static vec3d operator /(vec3d vec, float value)
{
return new vec3d(vec.v / value);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static vec3d operator +(vec3d left, vec3d right)
{
return new vec3d(left.vec + right.vec);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool operator ==(vec3d left, vec3d right)
{
return left.v == right.v;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static unsafe implicit operator vec3d((float, float, float) tuple)
{
vec3d v = *(vec3d*)&tuple;
v.vec.W = 0.0f;
return v;
}
public unsafe vec2d XY
{
get => new vec2d(this.vec);
set
{
fixed (Vector3* vptr = &v)
{
*(Vector2*)vptr = value.v;
}
}
}
}
public static class Vec3d
{
public static vec3d One => new vec3d(1, 1, 1);
public static vec3d UnitX => new vec3d(1, 0, 0);
public static vec3d UnitY => new vec3d(0, 1, 0);
public static vec3d UnitZ => new vec3d(0, 0, 1);
public static vec3d Zero => new vec3d(0, 0, 0);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static vec3d Cross(vec3d left, vec3d right)
{
return Linear.Cross(left, right);
}
public static bool IsParallel(vec3d left, vec3d right)
{
return Cross(left, right) == Zero;
}
}
}
@@ -0,0 +1,179 @@
//-----------------------------------------------------------------------
//-----------------------------------------------------------------------
// file="Vec4d.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.Data;
using StgSharp.Math;
using System;
using System.Data.Common;
using System.Numerics;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
namespace StgSharp.Math
{
public static class Vec4d
{
public static vec4d One
{
get => new vec4d(Vector4.One);
}
public static vec4d Zero
{
get => new vec4d(Vector4.Zero);
}
}
[StructLayout(LayoutKind.Explicit, Size = 16, Pack = 16)]
public struct vec4d : IEquatable<vec4d>, IVector
{
[FieldOffset(0)] internal unsafe fixed float num[4];
[FieldOffset(0)] internal M128 reg;
[FieldOffset(0)] internal Vector4 vec;
[FieldOffset(12)] public float W;
[FieldOffset(0)] public float X;
[FieldOffset(4)] public float Y;
[FieldOffset(8)] public float Z;
internal vec4d(Vector4 v)
{
vec = v;
}
public vec4d(vec3d v, float w)
{
vec = v.vec;
W = w;
}
public vec4d(float x, float y, float z, float w)
{
vec = new Vector4(x, y, z, w);
}
public unsafe vec3d XYZ
{
get => new vec3d(vec);
set
{
float w = W;
vec = value.vec;
W = w;
}
}
public unsafe vec2d XY
{
get => new vec2d(vec);
set
{
X = value.X;
Y = value.Y;
}
}
public override bool Equals(object obj)
{
return (obj is vec4d v) ? (v == this) : false;
}
public override int GetHashCode()
{
return vec.GetHashCode();
}
public override string ToString()
{
return vec.ToString();
}
public static bool operator !=(vec4d left, vec4d right)
{
return !(left == right);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static vec4d operator *(Matrix44 mat, vec4d vec)
{
mat.InternalTranspose();
return new vec4d(
Vector4.Dot(mat.transpose.colum0, vec.vec),
Vector4.Dot(mat.transpose.colum1, vec.vec),
Vector4.Dot(mat.transpose.colum2, vec.vec),
Vector4.Dot(mat.transpose.colum3, vec.vec)
);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static vec4d operator +(vec4d left, vec4d right)
{
return new vec4d(left.vec + right.vec);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static vec4d operator -(vec4d left, vec4d right)
{
return new vec4d(left.vec - right.vec);
}
public static bool operator ==(vec4d left, vec4d right)
{
return left.vec == right.vec;
}
public static implicit operator M128(vec4d vec)
{
return vec.reg;
}
bool IEquatable<vec4d>.Equals(vec4d other)
{
return vec == other.vec;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static vec4d Subtract(vec4d left, vec4d right)
{
return left - right;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static vec4d Add(vec4d left, vec4d right)
{
return left + right;
}
}
}
+110
View File
@@ -0,0 +1,110 @@
//-----------------------------------------------------------------------
//-----------------------------------------------------------------------
// file="ACos.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.Runtime.CompilerServices;
namespace StgSharp.Math
{
public static unsafe partial class Scaler
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static unsafe float ACos(float x)
{
return MathF.Acos(x);
/*
int sign = 1;
float y;
if (x < 0)
{
sign = -1;
y = -x;
}
else
{
y = x;
}
if (x > 1)
{
throw new ArgumentException();
}
float r, s, u, v = 0.0f;
bool transform = (y >= 0.5f);
if (transform)
{
r = 0.5f * (1.0f - y);
s = Scaler.Sqrt(r);
y = s;
}
else
{
r = y * y;
}
u = r * (0.184161606965100694821398249421F +
(-0.0565298683201845211985026327361F +
(-0.0133819288943925804214011424456F -
0.00396137437848476485201154797087F * r) * r) * r) /
(1.10496961524520294485512696706F -
0.836411276854206731913362287293F * r);
/*
if (transform)
{
float c, s1, p, q;
uint us;
us = *(uint*)&s;
uint uss = us & 0xffff0000;
s1 = *(float*)&uss;
c = (r - s1 * s1) / (s + s1);
p = 2.0F * s * u - (piby2_tail - 2.0F * c);
q = hpiby2_head - 2.0F * s1;
v = hpiby2_head - (p - q);
}
else
{
v = y + y * u;
}
v = sign * v;
return piby2 - v;
*/
}
}
}
+118
View File
@@ -0,0 +1,118 @@
//-----------------------------------------------------------------------
//-----------------------------------------------------------------------
// 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:
//
// 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;
namespace StgSharp.Math
{
public static unsafe partial class Scaler
{
private const float
piby2_tail = 7.5497894159e-08f, /* 0x33a22168 */
hpiby2_head = 7.8539812565e-01f, /* 0x3f490fda */
piby2 = 1.5707963705e+00f; /* 0x3fc90fdb */
/// <summary>
/// Reurns the value of ArcSin(x)
/// </summary>
/// <param name="x">The sine value of the angle</param>
/// <returns></returns>
/// <exception cref="ArgumentException"></exception>
public static float ASin(float x)
{
return MathF.Asin(x);
/*
int sign = 1;
float y;
if (x < 0)
{
sign = -1;
y = -x;
}
else
{
y = x;
}
if (x > 1)
{
throw new ArgumentException();
}
float r, s, u, v = 0.0f;
bool transform = (y >= 0.5f);
if (transform)
{
r = 0.5f * (1.0f - y);
s = Scaler.Sqrt(r);
y = s;
}
else
{
r = y * y;
}
u = r * (0.184161606965100694821398249421F +
(-0.0565298683201845211985026327361F +
(-0.0133819288943925804214011424456F -
0.00396137437848476485201154797087F * r) * r) * r) /
(1.10496961524520294485512696706F -
0.836411276854206731913362287293F * r);
/*
if (transform)
{
float c, s1, p, q;
uint us;
us = *(uint*)&s;
uint uss = us & 0xffff0000;
s1 = *(float*)&uss;
c = (r - s1 * s1) / (s + s1);
p = 2.0F * s * u - (piby2_tail - 2.0F * c);
q = hpiby2_head - 2.0F * s1;
v = hpiby2_head - (p - q);
}
else
{
v = y + y * u;
}/*
return sign * v;
*/
}
}
}
+119
View File
@@ -0,0 +1,119 @@
//-----------------------------------------------------------------------
//-----------------------------------------------------------------------
// file="ATan.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.Runtime.CompilerServices;
namespace StgSharp.Math
{
public static unsafe partial class Scaler
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static unsafe float ATan(float x)
{
return MathF.Atan(x);
/*
float sign = 1, angle = 0;
if (x < 0)
{
sign = -1;
x = -x;
}
uint xBit = *(uint*)&x;
int xExp = (int)(xBit >> 23) - 127;
if (xExp >= 26)
{
return 1;
}
if (xExp <= -15)
{
return x;
}
if (xExp >= 0)
{
x = (x - 1) / (x + 1);
angle = 0.7853981634f;
}
float v = 0.7853981634f * x + x * (1 - x) * (0.2447f + 0.0663f * x);
v += angle;
v *= sign;
return v;
}
public static unsafe float ATan(float x, float y)
{
float z = y / x;
float sign = 1, angle = 0, angle2 = 0;
if (x < 0)
{
angle = -3.1415926535f;
}
if (z < 0)
{
sign = -1;
z = -z;
}
uint zBit = *(uint*)&z;
int zExp = (int)(zBit >> 23) - 127;
if (zExp >= 26)
{
return 1;
}
if (zExp <= -15)
{
return z;
}
if (zExp >= 0)
{
z = (z - 1) / (z + 1);
angle2 = 0.7853981634f;
}
float v = 0.7853981634f * z + z * (1 - z) * (0.2447f + 0.0663f * z);
v *= 57.29577951f;
v += angle2;
v += angle;
v *= sign;
return v;
*/
}
}
}
@@ -0,0 +1,72 @@
//-----------------------------------------------------------------------
//-----------------------------------------------------------------------
// file="BasicOps.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.Globalization;
using System.Runtime.CompilerServices;
namespace StgSharp.Math
{
public static unsafe partial class Scaler
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static unsafe float Abs(float x)
{
uint* a = (uint*)&x;
*a &= 0b_0111_1111_1111_1111_1111_1111_1111_1111;
return *(float*)a;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static unsafe int Abs(int x)
{
uint* a = (uint*)&x;
*a &= 0b_0111_1111_1111_1111_1111_1111_1111_1111;
return *(int*)a;
}
internal static string GetListSeparator(IFormatProvider formatProvider)
{
if (formatProvider is CultureInfo cultureInfo)
{
return cultureInfo.TextInfo.ListSeparator;
}
if (formatProvider?.GetFormat(typeof(TextInfo)) is TextInfo textInfo)
{
return textInfo.ListSeparator;
}
return CultureInfo.CurrentCulture.TextInfo.ListSeparator;
}
}
}
@@ -0,0 +1,39 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace StgSharp.Math
{
public static partial class Scaler
{
public static float MaxOf(params float[] stream)
{
float ret = stream[0];
foreach (var num in stream)
{
if (ret < num)
{
ret = num;
}
}
return ret;
}
public static float MinOf(params float[] stream)
{
float ret = stream[0];
foreach (var num in stream)
{
if (ret > num)
{
ret = num;
}
}
return ret;
}
}
}
@@ -0,0 +1,60 @@
//-----------------------------------------------------------------------
//-----------------------------------------------------------------------
// 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:
//
// 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.Runtime.CompilerServices;
namespace StgSharp.Math
{
public static partial class Scaler
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static float Cos(float x)
{
/*
float refer = x / 360.0f;
refer -= (int)refer;
refer *= 40;
int a = (int)refer; //大分度,查表
refer -= a; //小分度,拟合
float refer2 = refer * refer;
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);
}
}
}
@@ -0,0 +1,56 @@
//-----------------------------------------------------------------------
//-----------------------------------------------------------------------
// file="Ctor.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.Runtime.InteropServices;
namespace StgSharp.Math
{
public unsafe delegate float ScalarCalcHandler(float* x);
public static unsafe partial class Scaler
{
const uint MEM_COMMIT = 0x1000;
const uint MEM_RELEASE = 0x8000;
const uint MEM_RESERVE = 0x2000;
const uint PAGE_EXECUTE_READWRITE = 0x40;
static Scaler()
{
}
[DllImport("Kernel32.dll", EntryPoint = "VirtualAlloc")]
private static extern IntPtr VirtualAlloc(IntPtr address, int size, uint allocType, uint protect);
[DllImport("Kernel32.dll", EntryPoint = "VirtualFree")]
private static extern bool VirtualFree(IntPtr address, int size, uint freeType);
}
}
+108
View File
@@ -0,0 +1,108 @@
//-----------------------------------------------------------------------
//-----------------------------------------------------------------------
// file="Exp.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.Math
{
public static unsafe partial class Scaler
{
public const float E = 2.718281828f;
/// <summary>
/// Get the value of E ^ x;
/// </summary>
/// <param name="x"></param>
/// <returns></returns>
public static unsafe float Exp(float x)
{
if (x == 1)
{
return E;
}
if (x == 0)
{
return 1;
}
bool sign = false;
if (x < 0)
{
sign = true;
x = -x;
}
float z = x, t;
if (x < 0.5f)
{
t = 1.0f + (x * (
1.0f + (x * (
0.5f + (x * (
0.1666666667f + (x * (
0.0416666666667f + (x *
8.333333333e-3f)))))))));
}
else
{
/*
* define z = n * 2 ^ m
* n is between 0 and 0.5
*/
uint zBit = *(uint*)&z;
uint zBitAbs = zBit & 0b_0111_1111_1111_1111_1111_1111_1111_1111;
uint uintm = zBitAbs >> 23;
int m = (*(int*)&uintm) - 126;
uint nBit = (zBitAbs & 0b_0000_0000_0111_1111_1111_1111_1111_1111)
+ 0b_0011_1111_0000_0000_0000_0000_0000_0000;
float n = (*(float*)&nBit) - 1;
t = 2.718281828f + (n * (
2.718281828f + (n * (
1.359140914f + (n * (
0.4530469714f + (n * (
0.1132617429f + (n *
0.02265234857f)))))))));
for (int i = 0; i < m; i++)
{
t = t * t;
}
}
if (sign)
{
t = 1 / t;
}
return t;
}
}
}
@@ -0,0 +1,38 @@
//-----------------------------------------------------------------------
//-----------------------------------------------------------------------
// file="Function.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.Math
{
public class Function
{
}
}
@@ -0,0 +1,96 @@
//-----------------------------------------------------------------------
//-----------------------------------------------------------------------
// file="Log.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;
namespace StgSharp.Math
{
public static partial class Scaler
{
private static readonly float[] logArray =
{
0f, 0.00995033085316809f, 0.0198026272961797f, 0.0295588022415444f, 0.0392207131532813f, 0.048790164169432f, 0.0582689081239758f, 0.0676586484738149f, 0.0769610411361284f, 0.0861776962410524f,
0.0953101798043249f, 0.104360015324243f, 0.113328685307003f, 0.122217632724249f, 0.131028262406404f, 0.139761942375159f, 0.148420005118273f, 0.157003748809665f, 0.165514438477573f, 0.173953307123438f,
0.182321556793955f, 0.19062035960865f, 0.198850858745165f, 0.207014169384326f, 0.215111379616945f, 0.22314355131421f, 0.231111720963387f, 0.2390169004705f, 0.246860077931526f, 0.254642218373581f,
0.262364264467491f, 0.27002713721306f, 0.27763173659828f, 0.285178942233662f, 0.29266961396282f, 0.300104592450338f, 0.307484699747961f, 0.314810739840034f, 0.322083499169113f, 0.3293037471426f,
0.336472236621213f, 0.343589704390077f, 0.350656871613169f, 0.357674444271816f, 0.364643113587909f, 0.371563556432483f, 0.378436435720245f, 0.385262400790645f, 0.392042087776024f, 0.398776119957368f,
0.405465108108164f, 0.412109650826833f, 0.418710334858185f, 0.425267735404344f, 0.431782416425538f, 0.438254930931155f, 0.444685821261446f, 0.451075619360217f, 0.457424847038875f, 0.46373401623214f,
0.470003629245736f, 0.476234178996372f, 0.482426149244293f, 0.488580014818671f, 0.494696241836107f, 0.500775287912489f, 0.506817602368452f, 0.512823626428664f, 0.518793793415168f, 0.524728528934982f,
0.53062825106217f, 0.536493370514568f, 0.542324290825362f, 0.548121408509688f, 0.553885113226438f, 0.559615787935423f, 0.56531380905006f, 0.570979546585738f, 0.576613364303994f, 0.582215619852664f,
0.587786664902119f, 0.593326845277734f, 0.598836501088704f, 0.60431596685333f, 0.609765571620894f, 0.615185639090233f, 0.62057648772511f, 0.625938430866495f, 0.631271776841858f, 0.636576829071551f,
0.641853886172395f, 0.647103242058538f, 0.65232518603969f, 0.657520002916794f, 0.662687973075237f, 0.667829372575655f, 0.672944473242426f, 0.678033542749897f, 0.683096844706444f, 0.688134638736401f,
};
/// <summary>
///
/// </summary>
/// <param name="x"></param>
/// <returns>
/// Returns the value of ln(x).
/// </returns>
public static unsafe float Log(float x)
{
if (x < 0)
{
throw new ArgumentOutOfRangeException();
}
uint input = *(uint*)&x;
int f = (int)input;
f >>= 23;
f -= 127;
input <<= 9;
input >>= 9;
input += 0b_0011_1111_1000_0000_0000_0000_0000_0000;
float result = *(float*)&input;
uint order = ((uint)(result * 100)) - 100;
float cache = (result / (1f + (0.01f * order))) - 1;
result = (cache * (-0.25f)) + 0.3333333333333f;
result = (result * cache) - 0.5f;
result = (result * cache) + 1.0f;
result *= cache;
return
(f * 0.6931471806f) +
result +
logArray[order];
}
public static float Log(float x, float y)
{
return Log(x) / Log(y);
}
}
}
+123
View File
@@ -0,0 +1,123 @@
//-----------------------------------------------------------------------
//-----------------------------------------------------------------------
// file="Pow.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.Math
{
public static unsafe partial class Scaler
{
/// <summary>
/// calculate the value of x^y
/// </summary>
/// <param name="x"></param>
/// <param name="y"></param>
/// <returns>Returns the value of x^y</returns>
public static unsafe float Pow(float x, float y)
{
/*
* x^y = E ^ ( log( x ) * y )
* Now define z = | log(x) * y | = m * n
* and m is an integer, and 0.5 < |n| < 1
* then E ^ z = ( E ^ n ) ^ m
*
*
* As a float number can be expressed in (1+e)*(2^f)
* n = (1+e)/2
* m = 2 ^ (f + 1)
*
*/
float z, t;
if ((x == 1) || (y == 0))
{
return 1;
}
if ((x == 1) && (y == 0))
{
return float.PositiveInfinity;
}
if ((x == 0) && (y != 0))
{
return 0;
}
z = Scaler.Log(x) * y;
bool sign = false;
if (z < 0)
{
sign = true;
z = -z;
}
if (z < 0.5f)
{
t = 1.0f + (z * (
1.0f + (z * (
0.5f + (z * (
0.1666666667f + (z * (
0.0416666666667f + (z *
8.333333333e-3f)))))))));
}
else
{
uint zBit = *(uint*)&z;
uint zBitAbs = zBit & 0b_0111_1111_1111_1111_1111_1111_1111_1111;
uint uintm = zBitAbs >> 23;
int m = (*(int*)&uintm) - 126;
uint nBit = (zBitAbs & 0b_0000_0000_0111_1111_1111_1111_1111_1111)
+ 0b_0011_1111_0000_0000_0000_0000_0000_0000;
float n = (*(float*)&nBit) - 1;
t = 2.718281828f + (n * (
2.718281828f + (n * (
1.359140914f + (n * (
0.4530469714f + (n * (
0.1132617429f + (n *
0.02265234857f)))))))));
for (int i = 0; i < m; i++)
{
t = t * t;
}
}
if (sign)
{
t = 1 / t;
}
return t;
}
}
}
@@ -0,0 +1,135 @@
//-----------------------------------------------------------------------
//-----------------------------------------------------------------------
// file="Random.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.Math.HighPrecession;
using System;
using System.Collections.Generic;
using System.Data;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Security.Cryptography;
using System.Text;
namespace StgSharp.Math
{
public static unsafe partial class Scaler
{
private static byte[] randomSeed;
/// <summary>
/// returns a random float number between 0 and 1.
/// </summary>
/// <returns></returns>
public static unsafe float LowProceisionRandom()
{
float seed = currentSeed;
float sample = Scaler.Pow(Scaler.SeedSqrt(seed), 2);
float sample2 = (sample - seed) / seed;
sample2 = Scaler.Abs(sample2);
if (sample == 1)
{
sample = sample2;
}
currentSeed = sample;
sample2 *= 1000000;
float result = sample2 - ((int)sample2);
if (result == 0)
{
currentSeed = sample2 + 0.1f;
result = Scaler.LowProceisionRandom();
}
return result;
}
/// <summary>
///
/// </summary>
/// <returns></returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static T Random<T>() where T : struct, IConvertible
#if NET8_0_OR_GREATER
,INumber
#endif
{
if (randomSeed == null)
{
randomSeed = new byte[64];
string originSeed =
($"{((Assembly.GetEntryAssembly() == null) ? (Assembly.GetAssembly(typeof(StgSharp))!) :
Assembly.GetEntryAssembly())}") +
($"{DateTime.UtcNow.ToString("o")}");
using (SHA256 sha = SHA256.Create())
{
randomSeed = sha.ComputeHash(Encoding.UTF8.GetBytes(originSeed));
}
}
using (SHA256 sha = SHA256.Create())
{
randomSeed = sha.ComputeHash(randomSeed);
}
fixed (byte* bptr = randomSeed)
{
return *(T*)bptr;
}
}
/// <summary>
/// Generates a random seed in int value.
/// </summary>
/// <returns></returns>
public static unsafe int RandomSeed()
{
float seedValue1 = LowProceisionRandom();
float seedValue2 = LowProceisionRandom();
uint seedValueBits = *(uint*)&seedValue1;
float seedValue = seedValueBits * seedValue2;
return *(int*)&seedValue;
}
internal static float currentSeed
#if DEBUG
= 0.5f;
#else
= Abs(System.DateTime.UtcNow.GetHashCode());
#endif
}
}
@@ -0,0 +1,71 @@
//-----------------------------------------------------------------------
//-----------------------------------------------------------------------
// file="SignOf.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.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
namespace StgSharp.Math
{
public static unsafe partial class Scaler
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static unsafe int SignOf(float x)
{
if (x == 0)
{
return 0;
}
if (x > 0)
{
return 1;
}
return -1;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static unsafe int SignOf(int x)
{
if (x == 0)
{
return 0;
}
if (x > 0)
{
return 1;
}
return -1;
}
}
}
@@ -0,0 +1,91 @@
//-----------------------------------------------------------------------
//-----------------------------------------------------------------------
// file="Sin.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.Net.NetworkInformation;
using System.Runtime.CompilerServices;
namespace StgSharp.Math
{
public static partial class Scaler
{
public const float Pi = 3.1415926f;
private static readonly float[] cosData =
{
1f, 0.987688341014302f, 0.951056517951169f, 0.891006527837756f, 0.809017000674805f,
0.707106790659974f, 0.587785265298989f, 0.453990516451646f, 0.309017014761716f, 0.156434488858737f,
2.67948965850537E-08f, -0.156434435928723f, -0.309016963794794f, -0.453990468702791f, -0.587785221943935f,
-0.707106752766267f, -0.809016969175515f, -0.891006503508499f, -0.951056501391011f, -0.98768833263101f,
-0.999999999999999f, -0.987688349397591f, -0.951056534511324f, -0.891006552167012f, -0.809017032174094f,
-0.707106828553679f, -0.587785308654041f, -0.4539905642005f, -0.309017065728638f, -0.156434541788751f,
-8.03846893110719E-08f, 0.156434382998709f, 0.309016912827872f, 0.453990420953934f, 0.58778517858888f,
0.707106714872559f, 0.809016937676222f, 0.891006479179238f, 0.951056484830851f, 0.987688324247716f,
};
private static readonly float[] sinData =
{
0f, 0.15643446239373f, 0.309016989278255f, 0.453990492577218f, 0.587785243621462f,
0.707106771713121f, 0.80901698492516f, 0.891006515673128f, 0.951056509671091f, 0.987688336822657f,
1f, 0.987688345205947f, 0.951056526231247f, 0.891006540002384f, 0.80901701642445f,
0.707106809606826f, 0.587785286976515f, 0.453990540326073f, 0.309017040245177f, 0.156434515323744f,
5.35897931701074E-08f, -0.156434409463716f, -0.309016938311333f, -0.453990444828362f, -0.587785200266408f,
-0.707106733819413f, -0.809016953425868f, -0.891006491343869f, -0.951056493110931f, -0.987688328439363f,
-0.999999999999997f, -0.987688353589235f, -0.951056542791401f, -0.891006564331638f, -0.809017047923737f,
-0.70710684750053f, -0.587785330331566f, -0.453990588074927f, -0.309017091212098f, -0.156434568253757f,
};
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static unsafe float Sin(float x)
{
/*
float refer = x / 360.0f;
refer -= (int)refer;
refer *= 40;
int a = (int)refer; //大分度,查表
refer -= a; //小分度,拟合
float refer2 = refer * refer;
return (sinData[a] * ((1 - (0.0123370055f * refer2)) + (2.536695709e-5f * refer2 * refer2)))
+ (cosData[a] * ((0.1570796327f * refer) - (6.459640975e-4f * refer * refer2)));
*/
return MathF.Sin(x);
}
}
}
+111
View File
@@ -0,0 +1,111 @@
//-----------------------------------------------------------------------
//-----------------------------------------------------------------------
// file="Sqrt.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.Runtime.CompilerServices;
namespace StgSharp.Math
{
public static partial class Scaler
{
public static unsafe float SeedSqrt(float x)
{
/*
* 本方法采用分别对指数和小数处理的方法计算平方根
* 将指数除以2即可获得结果的指数
* (补充:指数最低位被加到小数位中以抵消一个多余的2 ^ 1的影响)
* 小数部分则统一乘以2 ^ 16后由y = Sqrt(x)在1.5 * 2 ^ 16处的切线近似
* 为简化操作,提高计算速度,切线左界被移动到远点
* 在进一步简化操作后,计算精度达到0.5%,速度约为.Net库函数的3倍
*/
if (x < 0)
{
throw new ArgumentOutOfRangeException("Input is smaller than zero!");
}
if (x == 0)
{
return 0;
}
/*
* 提取指数位并减半
* 由于平方根仅对正数有效
* 故不考虑正负号
*/
if (x < 0)
{
throw new ArgumentOutOfRangeException();
}
if (x <= 1e-12)
{
return 0;
}
//提取指数位并减去指数位中的127
//由于平方根仅对正数有效
//故不考虑正负号
uint a = *(uint*)&x;
uint f = a;
f >>= 23;
f /= 2;
f += 64;
//提取小数位
//由于平方根仅对正数有效
//故不考虑正负号
a -= 0b_0011_1111_0000_0000_0000_0000_0000_0000;
uint e = a << 8;
e >>= 8;
//线性近似
e *= 0b_1101_0001;
e >>= 24;
e += 0b_0000_0000_0001_0000_1010_1000_1110_0010;
//还原指数
f <<= 23;
e += f;
float result = *(float*)&e;
result = (result + (x / result)) / 2;
return (result + (x / result)) / 2;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static float Sqrt(float x)
{
return MathF.Sqrt(x);
/**/
}
}
}
@@ -0,0 +1,55 @@
//-----------------------------------------------------------------------
//-----------------------------------------------------------------------
// file="Tan.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.Math
{
public static partial class Scaler
{
public static float Tan(float x)
{
float refer = x / 360.0f;
refer -= (int)refer;
refer *= 40;
int a = (int)refer; //大分度,查表
refer -= a; //小分度,拟合
float refer2 = refer * refer;
float sin = (0.1570796327f * refer) - (6.459640975e-4f * refer * refer2);
float cos = (1 - (0.0123370055f * refer2)) + (2.536695709e-5f * refer2 * refer2);
return ((sinData[a] * cos) + (cosData[a] * sin))
/ ((cosData[a] * cos) - (sinData[a] * sin));
}
}
}
+93
View File
@@ -0,0 +1,93 @@
//-----------------------------------------------------------------------
//-----------------------------------------------------------------------
// file="glClampf.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.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
namespace StgSharp.Math
{
/// <summary>
/// OpenGL numeric type.
/// An IEEE-754 floating-point value, clamped to the range [0,1].
/// </summary>
[StructLayout(LayoutKind.Sequential)]
public struct GLclampf
{
public float value;
public GLclampf(float a)
{
value = a;
}
public static explicit operator float(GLclampf v)
{
return v.value;
}
public static explicit operator GLclampf(float v)
{
return new GLclampf(v);
}
}
/// <summary>
/// OpenGL numeric type.
/// An IEEE-754 double precision floating-point value, clamped to the range [0,1].
/// </summary>
[StructLayout(LayoutKind.Sequential)]
public struct GLclampd
{
public double value;
public GLclampd(double a)
{
value = a;
}
public static explicit operator double(GLclampd v)
{
return v.value;
}
public static explicit operator GLclampd(double v)
{
return new GLclampd(v);
}
}
}
+63
View File
@@ -0,0 +1,63 @@
//-----------------------------------------------------------------------
//-----------------------------------------------------------------------
// file="glHalfNV.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.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
namespace StgSharp.Math
{
[StructLayout(LayoutKind.Explicit, Size = 16)]
public struct GLhalfNV
{
[FieldOffset(0)] public short binary;
public GLhalfNV(short bin)
{
binary = bin;
}
public static explicit operator GLhalfNV(short x)
{
return new GLhalfNV(x);
}
public static explicit operator short(GLhalfNV x)
{
return x.binary;
}
}
}