HLSF use long as aloloc size

This commit is contained in:
Nitload
2026-02-23 12:32:15 +08:00
parent 4bd88ccbca
commit c155cab510
552 changed files with 154266 additions and 154330 deletions
+306 -306
View File
@@ -1,306 +1,306 @@
//-----------------------------------------------------------------------
// -----------------------------------------------------------------------
// file="Camera"
// Project: StgSharp
// AuthorGroup: Nitload
// Copyright (c) Nitload. 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.OpenGL;
using StgSharp.Graphics.ShaderEdit;
using System;
namespace StgSharp.Mathematics.Graphic
{
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 Vec3 _target, up;
public Camera()
{
cameraAtt = Matrix44.Unit;
_target = Vec3.Zero;
up = Vec3.Zero;
_isLookAtAvailable = false;
_lookAt = Matrix44.Unit;
rotationAtt = Matrix44.Unit;
_projection = new Matrix44();
}
public Camera(Vec3 position, Vec3 target, Vec3 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.column[3].vec -= cameraAtt.column[3].vec;
_lookAt = rotationAtt.Transpose * move;
_isLookAtAvailable = true;
return _lookAt;
}
}
public Matrix44 CameraMatrix()
{
return Projection * View;
}
public void DisplayGLtypeDefinition()
{
Console.WriteLine("uniform matrix4x4 cameraName;");
}
/// <summary>
/// Get all uniform related to this <see cref="Camera" />.
/// </summary>
/// <param _label="source">
/// Shader program requires this camera.
/// </param>
/// <param _label="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 is null || uniformName.Length != 1) {
throw new ArgumentException(
"Camera needs exactly one uniform.", nameof(uniformName));
}
convertedUniform = source.GetUniform<Matrix44>(uniformName[0]);
}
public void MoveNear(float distance)
{
cameraAtt.column[3].Z -= distance;
_isLookAtAvailable = false;
}
public void MoveRight(float distance)
{
cameraAtt.column[3].X -= distance;
_isLookAtAvailable = false;
}
public void MoveUp(float distance)
{
cameraAtt.column[3].Y -= distance;
_isLookAtAvailable = false;
}
public unsafe void SetAllUniforms()
{
// Console.WriteLine(Projection * ViewBase);
OpenGLFunction.CurrentGL.SetUniformValue(convertedUniform, Projection * View);
}
public void SetViewDirection(Vec3 position, Vec3 target, Vec3 up)
{
Vec3 direction = position - target;
if ((position.reg == cameraAtt.column[3].reg) &&
(this.up == up) &&
(cameraAtt.column[3].reg == direction.reg)) {
return;
}
if (direction.GetLength() == 0)
{
// deadlock
throw new ArgumentException("Direction bust is not zero.");
}
if (Vec3.IsParallel(direction, up)) {
throw new ArgumentException("Direction and UP is on on same way");
}
_target = direction;
this.up = up;
_isLookAtAvailable = false;
direction.Orthogonalize(ref up);
Vec3 right = Linear.Orthogonalize(Linear.Cross(up, direction));
cameraAtt.column[0].reg = right.reg;
cameraAtt.column[1].reg = up.reg;
cameraAtt.column[2].reg = direction.reg;
cameraAtt.column[3].reg = position.reg;
InternalPitch();
InternalRow();
InternalYaw();
}
public void SetViewRange(
Radius fovRadius,
Vec2 size,
Vec2 offset,
(float front, float back) depthRange)
{
float
distance = _target.GetLength(),
near = distance - depthRange.front,
far = distance + depthRange.back,
offsetX = offset.X,
offsetY = offset.Y,
width = Scalar.Abs(GeometryScaler.Tan(fovRadius / 2) * near * 2),
height = width * size.Y / size.X;
_projection.column[0].X = 2 * near / width;
_projection.column[1].Y = 2 * near / height;
_projection.column[2].X = 2 * offsetX / width;
_projection.column[2].Y = 2 * offsetY / height;
_projection.column[2].Z = (far + near) / (near - far);
_projection.column[2].W = -1;
_projection.column[3].Z = 2 * near * far / (near - far);
}
public void Test(params Vec4[] vec)
{
Console.WriteLine(View);
Console.WriteLine(Projection);
Console.WriteLine(Projection * View);
Console.WriteLine(View);
foreach (Vec4 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.reg = partialCoord.colum0.reg;
rotationAtt.colum2.reg = partialCoord.colum1.reg;
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.reg = partialCoord.colum0.reg;
rotationAtt.colum1.reg = partialCoord.colum1.reg;
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.reg = partialCoord.colum0.reg;
rotationAtt.colum2.reg = partialCoord.colum1.reg;
rotationAtt.isTransposed = false;
*/
}
#endregion
}
}
//-----------------------------------------------------------------------
// -----------------------------------------------------------------------
// file="Camera"
// Project: StgSharp
// AuthorGroup: Nitload
// Copyright (c) Nitload. 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.OpenGL;
using StgSharp.Graphics.ShaderEdit;
using System;
namespace StgSharp.Mathematics.Graphic
{
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 Vec3 _target, up;
public Camera()
{
cameraAtt = Matrix44.Unit;
_target = Vec3.Zero;
up = Vec3.Zero;
_isLookAtAvailable = false;
_lookAt = Matrix44.Unit;
rotationAtt = Matrix44.Unit;
_projection = new Matrix44();
}
public Camera(Vec3 position, Vec3 target, Vec3 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.column[3].vec -= cameraAtt.column[3].vec;
_lookAt = rotationAtt.Transpose * move;
_isLookAtAvailable = true;
return _lookAt;
}
}
public Matrix44 CameraMatrix()
{
return Projection * View;
}
public void DisplayGLtypeDefinition()
{
Console.WriteLine("uniform matrix4x4 cameraName;");
}
/// <summary>
/// Get all uniform related to this <see cref="Camera" />.
/// </summary>
/// <param _label="source">
/// Shader program requires this camera.
/// </param>
/// <param _label="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 is null || uniformName.Length != 1) {
throw new ArgumentException(
"Camera needs exactly one uniform.", nameof(uniformName));
}
convertedUniform = source.GetUniform<Matrix44>(uniformName[0]);
}
public void MoveNear(float distance)
{
cameraAtt.column[3].Z -= distance;
_isLookAtAvailable = false;
}
public void MoveRight(float distance)
{
cameraAtt.column[3].X -= distance;
_isLookAtAvailable = false;
}
public void MoveUp(float distance)
{
cameraAtt.column[3].Y -= distance;
_isLookAtAvailable = false;
}
public unsafe void SetAllUniforms()
{
// Console.WriteLine(Projection * ViewBase);
OpenGLFunction.CurrentGL.SetUniformValue(convertedUniform, Projection * View);
}
public void SetViewDirection(Vec3 position, Vec3 target, Vec3 up)
{
Vec3 direction = position - target;
if ((position.reg == cameraAtt.column[3].reg) &&
(this.up == up) &&
(cameraAtt.column[3].reg == direction.reg)) {
return;
}
if (direction.GetLength() == 0)
{
// deadlock
throw new ArgumentException("Direction bust is not zero.");
}
if (Vec3.IsParallel(direction, up)) {
throw new ArgumentException("Direction and UP is on on same way");
}
_target = direction;
this.up = up;
_isLookAtAvailable = false;
direction.Orthogonalize(ref up);
Vec3 right = Linear.Orthogonalize(Linear.Cross(up, direction));
cameraAtt.column[0].reg = right.reg;
cameraAtt.column[1].reg = up.reg;
cameraAtt.column[2].reg = direction.reg;
cameraAtt.column[3].reg = position.reg;
InternalPitch();
InternalRow();
InternalYaw();
}
public void SetViewRange(
Radius fovRadius,
Vec2 size,
Vec2 offset,
(float front, float back) depthRange)
{
float
distance = _target.GetLength(),
near = distance - depthRange.front,
far = distance + depthRange.back,
offsetX = offset.X,
offsetY = offset.Y,
width = Scalar.Abs(GeometryScaler.Tan(fovRadius / 2) * near * 2),
height = width * size.Y / size.X;
_projection.column[0].X = 2 * near / width;
_projection.column[1].Y = 2 * near / height;
_projection.column[2].X = 2 * offsetX / width;
_projection.column[2].Y = 2 * offsetY / height;
_projection.column[2].Z = (far + near) / (near - far);
_projection.column[2].W = -1;
_projection.column[3].Z = 2 * near * far / (near - far);
}
public void Test(params Vec4[] vec)
{
Console.WriteLine(View);
Console.WriteLine(Projection);
Console.WriteLine(Projection * View);
Console.WriteLine(View);
foreach (Vec4 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.reg = partialCoord.colum0.reg;
rotationAtt.colum2.reg = partialCoord.colum1.reg;
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.reg = partialCoord.colum0.reg;
rotationAtt.colum1.reg = partialCoord.colum1.reg;
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.reg = partialCoord.colum0.reg;
rotationAtt.colum2.reg = partialCoord.colum1.reg;
rotationAtt.isTransposed = false;
*/
}
#endregion
}
}
+262 -262
View File
@@ -1,262 +1,262 @@
//-----------------------------------------------------------------------
// -----------------------------------------------------------------------
// file="Matrix44"
// Project: StgSharp
// AuthorGroup: Nitload
// Copyright (c) Nitload. 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.HighPerformance;
using StgSharp.Mathematics.Numeric;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Numerics;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
namespace StgSharp.Mathematics.Graphic
{
[StructLayout(LayoutKind.Explicit, Size = (16 * sizeof(float)), Pack = 16)]
public unsafe struct Matrix44 : IEquatable<Matrix44>
{
[FieldOffset(0 * sizeof(float))] internal Column column;
[FieldOffset(0)] internal ColumnSet4 mat;
[FieldOffset(0)] internal MatrixKernel<float> kernel;
internal Matrix44(
ColumnSet4 mat
)
{
Unsafe.SkipInit(out this);
this.mat = mat;
}
internal Matrix44(
Vector4 vec0,
Vector4 vec1,
Vector4 vec2,
Vector4 vec3
)
{
Unsafe.SkipInit(out this);
mat.colum0 = vec0;
mat.colum1 = vec1;
mat.colum2 = vec2;
mat.colum3 = vec3;
}
public Matrix44()
{
Unsafe.SkipInit(out this);
}
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
)
{
Unsafe.SkipInit(out this);
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 is > 3 or < 0)
{
throw new ArgumentOutOfRangeException(nameof(columNum));
}
if (columNum is > 3 or < 0) {
throw new ArgumentOutOfRangeException(nameof(columNum));
}
#endif
return kernel[rowNum + 4 * columNum];
}
set
{
#if DEBUG
if (rowNum is > 3 or < 0)
{
throw new ArgumentOutOfRangeException(nameof(columNum));
}
if (columNum is > 3 or < 0) {
throw new ArgumentOutOfRangeException(nameof(columNum));
}
#endif
kernel[rowNum + 4 * columNum] = value;
}
}
public Matrix44 Transpose
{
get
{
Matrix44 transpose = new();
InternalTranspose(ref transpose);
return 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);
public ReadOnlySpan<float> AsSpan()
{
return MemoryMarshal.CreateReadOnlySpan(ref Unsafe.As<ColumnSet4, float>(ref mat), 16);
}
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());
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal unsafe void InternalTranspose(
ref Matrix44 transpose
)
{
const int f32 = 0;
fixed (MatrixKernel<float>* source = &this.kernel, target = &transpose.kernel) {
NativeIntrinsic.Context.mat[f32].buffer_transpose(source, target);
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static unsafe Matrix44 operator -(
Matrix44 left,
Matrix44 right
)
{
Matrix44 ret = new();
const int f32 = 0;
NativeIntrinsic.Context.mat[f32].buffer_sub(&left, &right, &ret, 1);
return ret;
}
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 Matrix44 operator *(
Matrix44 left,
Matrix44 right
)
{
Matrix44 ret = new();
int f32 = (int)MatrixElementType.F32;
NativeIntrinsic.Context.mat[f32].kernel_fma(&left, &right, &ret);
return ret;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static unsafe Matrix44 operator +(
Matrix44 left,
Matrix44 right
)
{
Matrix44 ret = new();
const int f32 = 0;
NativeIntrinsic.Context.mat[f32].buffer_add(&left, &right, &ret, 1);
return ret;
}
public static bool operator ==(
Matrix44 left,
Matrix44 right
)
{
return left.Equals(right);
}
[InlineArray(4)]
internal struct Column
{
private Vec4 value;
}
}
}
//-----------------------------------------------------------------------
// -----------------------------------------------------------------------
// file="Matrix44"
// Project: StgSharp
// AuthorGroup: Nitload
// Copyright (c) Nitload. 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.HighPerformance;
using StgSharp.Mathematics.Numeric;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Numerics;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
namespace StgSharp.Mathematics.Graphic
{
[StructLayout(LayoutKind.Explicit, Size = (16 * sizeof(float)), Pack = 16)]
public unsafe struct Matrix44 : IEquatable<Matrix44>
{
[FieldOffset(0 * sizeof(float))] internal Column column;
[FieldOffset(0)] internal ColumnSet4 mat;
[FieldOffset(0)] internal MatrixKernel<float> kernel;
internal Matrix44(
ColumnSet4 mat
)
{
Unsafe.SkipInit(out this);
this.mat = mat;
}
internal Matrix44(
Vector4 vec0,
Vector4 vec1,
Vector4 vec2,
Vector4 vec3
)
{
Unsafe.SkipInit(out this);
mat.colum0 = vec0;
mat.colum1 = vec1;
mat.colum2 = vec2;
mat.colum3 = vec3;
}
public Matrix44()
{
Unsafe.SkipInit(out this);
}
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
)
{
Unsafe.SkipInit(out this);
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 is > 3 or < 0)
{
throw new ArgumentOutOfRangeException(nameof(columNum));
}
if (columNum is > 3 or < 0) {
throw new ArgumentOutOfRangeException(nameof(columNum));
}
#endif
return kernel[rowNum + 4 * columNum];
}
set
{
#if DEBUG
if (rowNum is > 3 or < 0)
{
throw new ArgumentOutOfRangeException(nameof(columNum));
}
if (columNum is > 3 or < 0) {
throw new ArgumentOutOfRangeException(nameof(columNum));
}
#endif
kernel[rowNum + 4 * columNum] = value;
}
}
public Matrix44 Transpose
{
get
{
Matrix44 transpose = new();
InternalTranspose(ref transpose);
return 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);
public ReadOnlySpan<float> AsSpan()
{
return MemoryMarshal.CreateReadOnlySpan(ref Unsafe.As<ColumnSet4, float>(ref mat), 16);
}
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());
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal unsafe void InternalTranspose(
ref Matrix44 transpose
)
{
const int f32 = 0;
fixed (MatrixKernel<float>* source = &this.kernel, target = &transpose.kernel) {
NativeIntrinsic.Context.mat[f32].buffer_transpose(source, target);
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static unsafe Matrix44 operator -(
Matrix44 left,
Matrix44 right
)
{
Matrix44 ret = new();
const int f32 = 0;
NativeIntrinsic.Context.mat[f32].buffer_sub(&left, &right, &ret, 1);
return ret;
}
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 Matrix44 operator *(
Matrix44 left,
Matrix44 right
)
{
Matrix44 ret = new();
int f32 = (int)MatrixElementType.F32;
NativeIntrinsic.Context.mat[f32].kernel_fma(&left, &right, &ret);
return ret;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static unsafe Matrix44 operator +(
Matrix44 left,
Matrix44 right
)
{
Matrix44 ret = new();
const int f32 = 0;
NativeIntrinsic.Context.mat[f32].buffer_add(&left, &right, &ret, 1);
return ret;
}
public static bool operator ==(
Matrix44 left,
Matrix44 right
)
{
return left.Equals(right);
}
[InlineArray(4)]
internal struct Column
{
private Vec4 value;
}
}
}
+162 -162
View File
@@ -1,162 +1,162 @@
//-----------------------------------------------------------------------
// -----------------------------------------------------------------------
// file="Vec2"
// Project: StgSharp
// AuthorGroup: Nitload
// Copyright (c) Nitload. 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.HighPerformance;
using StgSharp.Mathematics.Numeric;
using System.Net.Http.Headers;
using System.Numerics;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
namespace StgSharp.Mathematics.Graphic
{
/// <summary>
/// A two dimension vector defined by two elements. Vec2 in World are default used as colum
/// vector.
/// </summary>
[StructLayout(LayoutKind.Explicit, Size = 16, Pack = 16)]
public struct Vec2 : IUnmanagedVector<Vec2>
{
[FieldOffset(0)] internal unsafe fixed float num[2];
[FieldOffset(0)]
internal M128 reg;
[FieldOffset(0)]
internal Vector2 v;
[FieldOffset(0)]
public float X;
[FieldOffset(4)]
public float Y;
internal Vec2(Vector2 vec)
{
Unsafe.SkipInit(out reg);
Unsafe.SkipInit(out X);
Unsafe.SkipInit(out Y);
v = vec;
}
internal Vec2(M128 vec)
{
Unsafe.SkipInit(out reg);
Unsafe.SkipInit(out X);
Unsafe.SkipInit(out Y);
reg = vec;
reg.Member<ulong>(1) = 0;
}
public Vec2(float x, float y)
{
v = new Vector2(x, y);
}
public static Vec2 Unit => new Vec2(1, 1);
public static Vec2 Zero => new Vec2(0, 0);
public static Vec2 One
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => new Vec2(1, 1);
}
public Vec2 XY
{
readonly get => this;
set => this = value;
}
public Vec3 XYZ
{
readonly get => new(reg);
set
{
reg = value.reg;
reg.Member<ulong>(1) = 0;
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public float Cross(Vec2 right)
{
return X * right.Y - Y * right.X;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public float Dot(Vec2 vec)
{
return Vector2.Dot(v, vec.v);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Vec2 operator -(Vec2 left, Vec2 right)
{
return new Vec2(left.v - right.v);
}
public static bool operator !=(Vec2 left, Vec2 right)
{
return !(left == right);
}
public static Vec2 operator *(Vec2 vec, float value)
{
return new Vec2(vec.v * value);
}
public static Vec2 operator *(float value, Vec2 vec)
{
return new Vec2(vec.v * value);
}
public static Vec2 operator /(Vec2 vec, float value)
{
return new Vec2(vec.v / value);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Vec2 operator +(Vec2 left, Vec2 right)
{
return new Vec2(left.v + right.v);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool operator ==(Vec2 left, Vec2 right)
{
return left.v == right.v;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static implicit operator Vec2((float, float) tuple)
{
return new Vec2(tuple.Item1, tuple.Item2);
}
}
}
//-----------------------------------------------------------------------
// -----------------------------------------------------------------------
// file="Vec2"
// Project: StgSharp
// AuthorGroup: Nitload
// Copyright (c) Nitload. 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.HighPerformance;
using StgSharp.Mathematics.Numeric;
using System.Net.Http.Headers;
using System.Numerics;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
namespace StgSharp.Mathematics.Graphic
{
/// <summary>
/// A two dimension vector defined by two elements. Vec2 in World are default used as colum
/// vector.
/// </summary>
[StructLayout(LayoutKind.Explicit, Size = 16, Pack = 16)]
public struct Vec2 : IUnmanagedVector<Vec2>
{
[FieldOffset(0)] internal unsafe fixed float num[2];
[FieldOffset(0)]
internal M128 reg;
[FieldOffset(0)]
internal Vector2 v;
[FieldOffset(0)]
public float X;
[FieldOffset(4)]
public float Y;
internal Vec2(Vector2 vec)
{
Unsafe.SkipInit(out reg);
Unsafe.SkipInit(out X);
Unsafe.SkipInit(out Y);
v = vec;
}
internal Vec2(M128 vec)
{
Unsafe.SkipInit(out reg);
Unsafe.SkipInit(out X);
Unsafe.SkipInit(out Y);
reg = vec;
reg.Member<ulong>(1) = 0;
}
public Vec2(float x, float y)
{
v = new Vector2(x, y);
}
public static Vec2 Unit => new Vec2(1, 1);
public static Vec2 Zero => new Vec2(0, 0);
public static Vec2 One
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => new Vec2(1, 1);
}
public Vec2 XY
{
readonly get => this;
set => this = value;
}
public Vec3 XYZ
{
readonly get => new(reg);
set
{
reg = value.reg;
reg.Member<ulong>(1) = 0;
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public float Cross(Vec2 right)
{
return X * right.Y - Y * right.X;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public float Dot(Vec2 vec)
{
return Vector2.Dot(v, vec.v);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Vec2 operator -(Vec2 left, Vec2 right)
{
return new Vec2(left.v - right.v);
}
public static bool operator !=(Vec2 left, Vec2 right)
{
return !(left == right);
}
public static Vec2 operator *(Vec2 vec, float value)
{
return new Vec2(vec.v * value);
}
public static Vec2 operator *(float value, Vec2 vec)
{
return new Vec2(vec.v * value);
}
public static Vec2 operator /(Vec2 vec, float value)
{
return new Vec2(vec.v / value);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Vec2 operator +(Vec2 left, Vec2 right)
{
return new Vec2(left.v + right.v);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool operator ==(Vec2 left, Vec2 right)
{
return left.v == right.v;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static implicit operator Vec2((float, float) tuple)
{
return new Vec2(tuple.Item1, tuple.Item2);
}
}
}
+236 -236
View File
@@ -1,236 +1,236 @@
//-----------------------------------------------------------------------
// -----------------------------------------------------------------------
// file="Vec3"
// Project: StgSharp
// AuthorGroup: Nitload
// Copyright (c) Nitload. 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.HighPerformance;
using StgSharp.Mathematics.Numeric;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Numerics;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
namespace StgSharp.Mathematics.Graphic
{
[CollectionBuilder(typeof(Vec3), nameof(FromSpan))]
[StructLayout(LayoutKind.Explicit, Size = 16, Pack = 16)]
public struct Vec3 : IEquatable<Vec3>, IUnmanagedVector<Vec3>, IEnumerable<float>
{
[FieldOffset(0)] internal unsafe fixed float num[3];
[FieldOffset(0)] internal M128 reg;
[FieldOffset(0)] internal Vector3 v;
[FieldOffset(0)] public float X;
[FieldOffset(4)] public float Y;
[FieldOffset(8)] public float Z;
internal Vec3(M128 vector)
{
Unsafe.SkipInit(out this);
reg = vector;
reg.Member<uint>(3) = 0;
}
internal Vec3(Vector3 vector)
{
v = vector;
}
public Vec3()
{
Unsafe.SkipInit(out this);
}
public Vec3(Vec2 xy, float z)
{
Unsafe.SkipInit(out this);
reg = xy.reg;
Z = z;
}
public Vec3(float x, float y, float z)
{
Unsafe.SkipInit(out this);
v = new Vector3(x, y, z);
}
public unsafe Vec2 XY
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
readonly get => new(reg);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
set => Unsafe.As<Vector3, Vector2>(ref v) = value.v;
}
public Vec3 XYZ
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
readonly get => new(v);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
set => this = value;
}
public static Vec3 Zero => new(0, 0, 0);
public static Vec3 One => new(1, 1, 1);
public static Vec3 UnitX => new(1, 0, 0);
public static Vec3 UnitY => new(0, 1, 0);
public static Vec3 UnitZ => new(0, 0, 1);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Vec3 Cross(Vec3 left, Vec3 right)
{
return Linear.Cross(left, right);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly float Dot(Vec3 right)
{
return Vector3.Dot(v, right.v);
}
public readonly bool Equals(Vec3 other)
{
return reg == other.reg;
}
public override readonly bool Equals(object obj)
{
return obj is not null && obj is Vec3 vec && this == vec;
}
public static Vec3 FromSpan(ReadOnlySpan<float> span)
{
Vec3 v = new();
span.CopyTo(MemoryMarshal.CreateSpan(ref v.X, 3));
return v;
}
public unsafe IEnumerator<float> GetEnumerator()
{
for (int i = 0; i < 3; i++) {
yield return reg.Member<float>(i);
}
}
public override unsafe int GetHashCode()
{
fixed (float* add = &X) {
return HashCode.Combine(X, Y, Z, (ulong)add);
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly float GetMaxValue()
{
float ret = X;
ret = ret > Y ? ret : Y;
ret = ret > Z ? ret : Z;
return ret;
}
public static bool IsParallel(Vec3 left, Vec3 right)
{
return Cross(left, right) == Zero;
}
public override readonly string ToString()
{
return $"[{X},{Y},{Z}]";
}
public readonly string ToString(string sample)
{
return $"[{X.ToString(sample)},{Y.ToString(sample)},{Z.ToString(sample)}]";
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Vec3 operator -(Vec3 vec)
{
return new Vec3(-vec.v);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Vec3 operator -(Vec3 left, Vec3 right)
{
return new Vec3(left.v - right.v);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool operator !=(Vec3 left, Vec3 right)
{
return left.v != right.v;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Vec3 operator *(Vec3 vec, float value)
{
return new Vec3(vec.v * value);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static float operator *(Vec3 left, Vec3 right)
{
return Vector3.Dot(left.v, right.v);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Vec3 operator /(Vec3 vec, float value)
{
return new Vec3(vec.v / value);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Vec3 operator +(Vec3 left, Vec3 right)
{
return new Vec3(left.v + right.v);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool operator ==(Vec3 left, Vec3 right)
{
return left.v == right.v;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static unsafe implicit operator Vec3((float, float, float) tuple)
{
return Unsafe.As<(float,float,float), Vec3>(ref tuple);
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
}
}
//-----------------------------------------------------------------------
// -----------------------------------------------------------------------
// file="Vec3"
// Project: StgSharp
// AuthorGroup: Nitload
// Copyright (c) Nitload. 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.HighPerformance;
using StgSharp.Mathematics.Numeric;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Numerics;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
namespace StgSharp.Mathematics.Graphic
{
[CollectionBuilder(typeof(Vec3), nameof(FromSpan))]
[StructLayout(LayoutKind.Explicit, Size = 16, Pack = 16)]
public struct Vec3 : IEquatable<Vec3>, IUnmanagedVector<Vec3>, IEnumerable<float>
{
[FieldOffset(0)] internal unsafe fixed float num[3];
[FieldOffset(0)] internal M128 reg;
[FieldOffset(0)] internal Vector3 v;
[FieldOffset(0)] public float X;
[FieldOffset(4)] public float Y;
[FieldOffset(8)] public float Z;
internal Vec3(M128 vector)
{
Unsafe.SkipInit(out this);
reg = vector;
reg.Member<uint>(3) = 0;
}
internal Vec3(Vector3 vector)
{
v = vector;
}
public Vec3()
{
Unsafe.SkipInit(out this);
}
public Vec3(Vec2 xy, float z)
{
Unsafe.SkipInit(out this);
reg = xy.reg;
Z = z;
}
public Vec3(float x, float y, float z)
{
Unsafe.SkipInit(out this);
v = new Vector3(x, y, z);
}
public unsafe Vec2 XY
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
readonly get => new(reg);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
set => Unsafe.As<Vector3, Vector2>(ref v) = value.v;
}
public Vec3 XYZ
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
readonly get => new(v);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
set => this = value;
}
public static Vec3 Zero => new(0, 0, 0);
public static Vec3 One => new(1, 1, 1);
public static Vec3 UnitX => new(1, 0, 0);
public static Vec3 UnitY => new(0, 1, 0);
public static Vec3 UnitZ => new(0, 0, 1);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Vec3 Cross(Vec3 left, Vec3 right)
{
return Linear.Cross(left, right);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly float Dot(Vec3 right)
{
return Vector3.Dot(v, right.v);
}
public readonly bool Equals(Vec3 other)
{
return reg == other.reg;
}
public override readonly bool Equals(object obj)
{
return obj is not null && obj is Vec3 vec && this == vec;
}
public static Vec3 FromSpan(ReadOnlySpan<float> span)
{
Vec3 v = new();
span.CopyTo(MemoryMarshal.CreateSpan(ref v.X, 3));
return v;
}
public unsafe IEnumerator<float> GetEnumerator()
{
for (int i = 0; i < 3; i++) {
yield return reg.Member<float>(i);
}
}
public override unsafe int GetHashCode()
{
fixed (float* add = &X) {
return HashCode.Combine(X, Y, Z, (ulong)add);
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly float GetMaxValue()
{
float ret = X;
ret = ret > Y ? ret : Y;
ret = ret > Z ? ret : Z;
return ret;
}
public static bool IsParallel(Vec3 left, Vec3 right)
{
return Cross(left, right) == Zero;
}
public override readonly string ToString()
{
return $"[{X},{Y},{Z}]";
}
public readonly string ToString(string sample)
{
return $"[{X.ToString(sample)},{Y.ToString(sample)},{Z.ToString(sample)}]";
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Vec3 operator -(Vec3 vec)
{
return new Vec3(-vec.v);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Vec3 operator -(Vec3 left, Vec3 right)
{
return new Vec3(left.v - right.v);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool operator !=(Vec3 left, Vec3 right)
{
return left.v != right.v;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Vec3 operator *(Vec3 vec, float value)
{
return new Vec3(vec.v * value);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static float operator *(Vec3 left, Vec3 right)
{
return Vector3.Dot(left.v, right.v);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Vec3 operator /(Vec3 vec, float value)
{
return new Vec3(vec.v / value);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Vec3 operator +(Vec3 left, Vec3 right)
{
return new Vec3(left.v + right.v);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool operator ==(Vec3 left, Vec3 right)
{
return left.v == right.v;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static unsafe implicit operator Vec3((float, float, float) tuple)
{
return Unsafe.As<(float,float,float), Vec3>(ref tuple);
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
}
}
+211 -211
View File
@@ -1,212 +1,212 @@
//-----------------------------------------------------------------------
// -----------------------------------------------------------------------
// file="Vec4"
// Project: StgSharp
// AuthorGroup: Nitload
// Copyright (c) Nitload. 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.HighPerformance;
using StgSharp.Internal.Intrinsic;
using StgSharp.Mathematics.Numeric;
using System;
using System.Data.Common;
using System.Numerics;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
namespace StgSharp.Mathematics.Graphic
{
[CollectionBuilder(builderType:typeof(Vec4), methodName: nameof(Vec4.FromSpan))]
[StructLayout(LayoutKind.Explicit, Size = 16, Pack = 16)]
public unsafe struct Vec4 : IEquatable<Vec4>, IUnmanagedVector<Vec4>
{
[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 Vec4(Vector4 v)
{
Unsafe.SkipInit(out X);
Unsafe.SkipInit(out Y);
Unsafe.SkipInit(out Z);
Unsafe.SkipInit(out W);
Unsafe.SkipInit(out reg);
vec = v;
}
internal Vec4(M128 v)
{
Unsafe.SkipInit(out X);
Unsafe.SkipInit(out Y);
Unsafe.SkipInit(out Z);
Unsafe.SkipInit(out W);
Unsafe.SkipInit(out vec);
reg = v;
}
public Vec4(Vec3 v3, float w)
{
Unsafe.SkipInit(out X);
Unsafe.SkipInit(out Y);
Unsafe.SkipInit(out Z);
Unsafe.SkipInit(out W);
reg = v3.reg;
W = w;
}
public Vec4(float x, float y, float z, float w)
{
vec = new Vector4(x, y, z, w);
}
public unsafe Vec2 XY
{
get => new Vec2(reg);
set
{
X = value.X;
Y = value.Y;
}
}
public unsafe Vec3 XYZ
{
get => new Vec3(reg);
set
{
float w = W;
reg = value.reg;
W = w;
}
}
public static Vec4 One
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => new Vec4(1, 1, 1, 1);
}
public static Vec4 Zero
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => new Vec4(0, 0, 0, 0);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Vec4 Add(Vec4 left, Vec4 right)
{
return left + right;
}
public ReadOnlySpan<float> AsSpan()
{
return MemoryMarshal.CreateReadOnlySpan(ref reg.Member<float>(0), 4);
}
public override bool Equals(object obj)
{
return obj is Vec4 v && v == this;
}
public static Vec4 FromSpan(ReadOnlySpan<float> span)
{
if (span.Length < 4) {
throw new ArgumentException("Span length must be at least 4.", nameof(span));
}
// Read 4 floats as a Vector4 in one operation without per-element copy.
Vector4 v = MemoryMarshal.Read<Vector4>(MemoryMarshal.AsBytes(span));
return new Vec4(v);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public ReadOnlySpan<float>.Enumerator GetEnumerator()
{
return AsSpan().GetEnumerator();
}
public override int GetHashCode()
{
return vec.GetHashCode();
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Vec4 Subtract(Vec4 left, Vec4 right)
{
return left - right;
}
public override string ToString()
{
return vec.ToString();
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Vec4 operator -(Vec4 left, Vec4 right)
{
return new Vec4(left.vec - right.vec);
}
public static bool operator !=(Vec4 left, Vec4 right)
{
return !(left == right);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Vec4 operator *(Matrix44 mat, Vec4 vec)
{
Matrix44 transpose = mat.Transpose;
return new Vec4(Vector4.Dot(transpose.mat.colum0, vec.vec),
Vector4.Dot(transpose.mat.colum1, vec.vec),
Vector4.Dot(transpose.mat.colum2, vec.vec),
Vector4.Dot(transpose.mat.colum3, vec.vec));
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Vec4 operator +(Vec4 left, Vec4 right)
{
return new Vec4(left.vec + right.vec);
}
public static bool operator ==(Vec4 left, Vec4 right)
{
return left.vec == right.vec;
}
bool IEquatable<Vec4>.Equals(Vec4 other)
{
return vec == other.vec;
}
}
//-----------------------------------------------------------------------
// -----------------------------------------------------------------------
// file="Vec4"
// Project: StgSharp
// AuthorGroup: Nitload
// Copyright (c) Nitload. 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.HighPerformance;
using StgSharp.Internal.Intrinsic;
using StgSharp.Mathematics.Numeric;
using System;
using System.Data.Common;
using System.Numerics;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
namespace StgSharp.Mathematics.Graphic
{
[CollectionBuilder(builderType:typeof(Vec4), methodName: nameof(Vec4.FromSpan))]
[StructLayout(LayoutKind.Explicit, Size = 16, Pack = 16)]
public unsafe struct Vec4 : IEquatable<Vec4>, IUnmanagedVector<Vec4>
{
[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 Vec4(Vector4 v)
{
Unsafe.SkipInit(out X);
Unsafe.SkipInit(out Y);
Unsafe.SkipInit(out Z);
Unsafe.SkipInit(out W);
Unsafe.SkipInit(out reg);
vec = v;
}
internal Vec4(M128 v)
{
Unsafe.SkipInit(out X);
Unsafe.SkipInit(out Y);
Unsafe.SkipInit(out Z);
Unsafe.SkipInit(out W);
Unsafe.SkipInit(out vec);
reg = v;
}
public Vec4(Vec3 v3, float w)
{
Unsafe.SkipInit(out X);
Unsafe.SkipInit(out Y);
Unsafe.SkipInit(out Z);
Unsafe.SkipInit(out W);
reg = v3.reg;
W = w;
}
public Vec4(float x, float y, float z, float w)
{
vec = new Vector4(x, y, z, w);
}
public unsafe Vec2 XY
{
get => new Vec2(reg);
set
{
X = value.X;
Y = value.Y;
}
}
public unsafe Vec3 XYZ
{
get => new Vec3(reg);
set
{
float w = W;
reg = value.reg;
W = w;
}
}
public static Vec4 One
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => new Vec4(1, 1, 1, 1);
}
public static Vec4 Zero
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => new Vec4(0, 0, 0, 0);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Vec4 Add(Vec4 left, Vec4 right)
{
return left + right;
}
public ReadOnlySpan<float> AsSpan()
{
return MemoryMarshal.CreateReadOnlySpan(ref reg.Member<float>(0), 4);
}
public override bool Equals(object obj)
{
return obj is Vec4 v && v == this;
}
public static Vec4 FromSpan(ReadOnlySpan<float> span)
{
if (span.Length < 4) {
throw new ArgumentException("Span length must be at least 4.", nameof(span));
}
// Read 4 floats as a Vector4 in one operation without per-element copy.
Vector4 v = MemoryMarshal.Read<Vector4>(MemoryMarshal.AsBytes(span));
return new Vec4(v);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public ReadOnlySpan<float>.Enumerator GetEnumerator()
{
return AsSpan().GetEnumerator();
}
public override int GetHashCode()
{
return vec.GetHashCode();
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Vec4 Subtract(Vec4 left, Vec4 right)
{
return left - right;
}
public override string ToString()
{
return vec.ToString();
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Vec4 operator -(Vec4 left, Vec4 right)
{
return new Vec4(left.vec - right.vec);
}
public static bool operator !=(Vec4 left, Vec4 right)
{
return !(left == right);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Vec4 operator *(Matrix44 mat, Vec4 vec)
{
Matrix44 transpose = mat.Transpose;
return new Vec4(Vector4.Dot(transpose.mat.colum0, vec.vec),
Vector4.Dot(transpose.mat.colum1, vec.vec),
Vector4.Dot(transpose.mat.colum2, vec.vec),
Vector4.Dot(transpose.mat.colum3, vec.vec));
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Vec4 operator +(Vec4 left, Vec4 right)
{
return new Vec4(left.vec + right.vec);
}
public static bool operator ==(Vec4 left, Vec4 right)
{
return left.vec == right.vec;
}
bool IEquatable<Vec4>.Equals(Vec4 other)
{
return vec == other.vec;
}
}
}
@@ -1,49 +1,49 @@
//-----------------------------------------------------------------------
// -----------------------------------------------------------------------
// file="Vector.Cross"
// Project: StgSharp
// AuthorGroup: Nitload
// Copyright (c) Nitload. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
// -----------------------------------------------------------------------
// -----------------------------------------------------------------------
using StgSharp.Mathematics.Graphic;
namespace StgSharp.Mathematics.Graphic
{
public static unsafe partial class Linear
{
public static float Cross(Vec2 vec1, Vec2 vec2)
{
return (vec1.X * vec2.Y) - (vec1.Y * vec2.X);
}
public static Vec3 Cross(Vec3 vec1, Vec3 vec2)
{
return new Vec3(
(vec1.Y * vec2.Z) - (vec1.Z * vec2.Y),
(vec1.X * vec2.Z) - (vec1.Z * vec2.X),
(vec1.X * vec2.Y) - (vec1.Y * vec2.X));
}
}
}
//-----------------------------------------------------------------------
// -----------------------------------------------------------------------
// file="Vector.Cross"
// Project: StgSharp
// AuthorGroup: Nitload
// Copyright (c) Nitload. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
// -----------------------------------------------------------------------
// -----------------------------------------------------------------------
using StgSharp.Mathematics.Graphic;
namespace StgSharp.Mathematics.Graphic
{
public static unsafe partial class Linear
{
public static float Cross(Vec2 vec1, Vec2 vec2)
{
return (vec1.X * vec2.Y) - (vec1.Y * vec2.X);
}
public static Vec3 Cross(Vec3 vec1, Vec3 vec2)
{
return new Vec3(
(vec1.Y * vec2.Z) - (vec1.Z * vec2.Y),
(vec1.X * vec2.Z) - (vec1.Z * vec2.X),
(vec1.X * vec2.Y) - (vec1.Y * vec2.X));
}
}
}
@@ -1,47 +1,47 @@
//-----------------------------------------------------------------------
// -----------------------------------------------------------------------
// file="Vector.GetAngle"
// Project: StgSharp
// AuthorGroup: Nitload
// Copyright (c) Nitload. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
// -----------------------------------------------------------------------
// -----------------------------------------------------------------------
using StgSharp.Mathematics.Graphic;
namespace StgSharp.Mathematics.Graphic
{
public static unsafe partial class Linear
{
public static float GetAngle(Vec2 vec1, Vec2 vec2)
{
return Scalar.ACos(
1 / (vec1.GetLength() * vec2.GetLength()));
}
public static float GetAngle(Vec3 vec1, Vec3 vec2)
{
return Scalar.ACos((vec1 * vec2) / (vec1.GetLength() * vec2.GetLength()));
}
}
}
//-----------------------------------------------------------------------
// -----------------------------------------------------------------------
// file="Vector.GetAngle"
// Project: StgSharp
// AuthorGroup: Nitload
// Copyright (c) Nitload. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
// -----------------------------------------------------------------------
// -----------------------------------------------------------------------
using StgSharp.Mathematics.Graphic;
namespace StgSharp.Mathematics.Graphic
{
public static unsafe partial class Linear
{
public static float GetAngle(Vec2 vec1, Vec2 vec2)
{
return Scalar.ACos(
1 / (vec1.GetLength() * vec2.GetLength()));
}
public static float GetAngle(Vec3 vec1, Vec3 vec2)
{
return Scalar.ACos((vec1 * vec2) / (vec1.GetLength() * vec2.GetLength()));
}
}
}
@@ -1,46 +1,46 @@
//-----------------------------------------------------------------------
// -----------------------------------------------------------------------
// file="Vector.GetLength"
// Project: StgSharp
// AuthorGroup: Nitload
// Copyright (c) Nitload. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
// -----------------------------------------------------------------------
// -----------------------------------------------------------------------
using StgSharp.Mathematics.Graphic;
namespace StgSharp.Mathematics.Graphic
{
public static unsafe partial class Linear
{
public static float GetLength(this Vec3 vec)
{
return Scalar.Sqrt(vec.Dot(vec));
}
public static float GetLength(this Vec2 vec)
{
return Scalar.Sqrt(vec.Dot(vec));
}
}
}
//-----------------------------------------------------------------------
// -----------------------------------------------------------------------
// file="Vector.GetLength"
// Project: StgSharp
// AuthorGroup: Nitload
// Copyright (c) Nitload. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
// -----------------------------------------------------------------------
// -----------------------------------------------------------------------
using StgSharp.Mathematics.Graphic;
namespace StgSharp.Mathematics.Graphic
{
public static unsafe partial class Linear
{
public static float GetLength(this Vec3 vec)
{
return Scalar.Sqrt(vec.Dot(vec));
}
public static float GetLength(this Vec2 vec)
{
return Scalar.Sqrt(vec.Dot(vec));
}
}
}
@@ -1,82 +1,82 @@
//-----------------------------------------------------------------------
// -----------------------------------------------------------------------
// file="Vector.Normalize"
// Project: StgSharp
// AuthorGroup: Nitload
// Copyright (c) Nitload. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
// -----------------------------------------------------------------------
// -----------------------------------------------------------------------
using StgSharp.Mathematics.Graphic;
using System.Numerics;
namespace StgSharp.Mathematics.Graphic
{
public static partial class Linear
{
public static Vec4 Normalize(Vec4 vec)
{
return new Vec4
{
vec = Vector4.Normalize(vec.vec)
};
}
public static Vec3 Normalize(Vec3 vec)
{
return new Vec3
{
v = Vector3.Normalize(vec.v)
};
}
public static unsafe void Normalize(ref Vec4 source, ref Vec4 target)
{
Vector4 s = source.vec;
Vector4 t = target.vec;
s = Vector4.Normalize(s);
Vector4 projection = Vector4.Multiply(s, t);
t -= projection;
source.vec = s;
target.vec = t;
}
public static unsafe void Normalize(ref Vec3 source, ref Vec3 target)
{
Vector3 s = source.v;
Vector3 t = target.v;
s = Vector3.Normalize(s);
Vector3 projection = Vector3.Dot(t, s) / Vector3.Dot(s, s) * s;
t -= projection;
t = Vector3.Normalize(t);
source.v = s;
target.v = t;
}
}
}
//-----------------------------------------------------------------------
// -----------------------------------------------------------------------
// file="Vector.Normalize"
// Project: StgSharp
// AuthorGroup: Nitload
// Copyright (c) Nitload. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
// -----------------------------------------------------------------------
// -----------------------------------------------------------------------
using StgSharp.Mathematics.Graphic;
using System.Numerics;
namespace StgSharp.Mathematics.Graphic
{
public static partial class Linear
{
public static Vec4 Normalize(Vec4 vec)
{
return new Vec4
{
vec = Vector4.Normalize(vec.vec)
};
}
public static Vec3 Normalize(Vec3 vec)
{
return new Vec3
{
v = Vector3.Normalize(vec.v)
};
}
public static unsafe void Normalize(ref Vec4 source, ref Vec4 target)
{
Vector4 s = source.vec;
Vector4 t = target.vec;
s = Vector4.Normalize(s);
Vector4 projection = Vector4.Multiply(s, t);
t -= projection;
source.vec = s;
target.vec = t;
}
public static unsafe void Normalize(ref Vec3 source, ref Vec3 target)
{
Vector3 s = source.v;
Vector3 t = target.v;
s = Vector3.Normalize(s);
Vector3 projection = Vector3.Dot(t, s) / Vector3.Dot(s, s) * s;
t -= projection;
t = Vector3.Normalize(t);
source.v = s;
target.v = t;
}
}
}
@@ -1,92 +1,92 @@
//-----------------------------------------------------------------------
// -----------------------------------------------------------------------
// file="Vector.Orthogonalize"
// Project: StgSharp
// AuthorGroup: Nitload
// Copyright (c) Nitload. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
// -----------------------------------------------------------------------
// -----------------------------------------------------------------------
using StgSharp.Mathematics.Graphic;
using System.Numerics;
namespace StgSharp.Mathematics.Graphic
{
public static partial class Linear
{
public static Vec4 Orthogonalize(this Vec4 vec)
{
return new Vec4
{
vec = Vector4.Normalize(vec.vec)
};
}
public static Vec3 Orthogonalize(Vec3 vec)
{
return new Vec3
{
v = Vector3.Normalize(vec.v)
};
}
public static unsafe void Orthogonalize(this Vec4 source, ref Vec4 target)
{
Vector4 s = source.vec;
Vector4 t = target.vec;
s = Vector4.Normalize(s);
Vector4 projection = Vector4.Multiply(s, t);
t -= projection;
source.vec = s;
target.vec = t;
}
/// <summary>
/// Orthogonalize two <see cref="Vec3" />s, let their length be 1 and perpendicular to
/// each other.
/// </summary>
/// <param _label="source">
///
/// </param>
/// <param _label="target">
///
/// </param>
public static unsafe void Orthogonalize(this Vec3 source, ref Vec3 target)
{
Vector3 s = source.v;
Vector3 t = target.v;
s = Vector3.Normalize(s);
Vector3 projection = Vector3.Dot(t, s) / Vector3.Dot(s, s) * s;
t -= projection;
t = Vector3.Normalize(t);
source.v = s;
target.v = t;
}
}
}
//-----------------------------------------------------------------------
// -----------------------------------------------------------------------
// file="Vector.Orthogonalize"
// Project: StgSharp
// AuthorGroup: Nitload
// Copyright (c) Nitload. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
// -----------------------------------------------------------------------
// -----------------------------------------------------------------------
using StgSharp.Mathematics.Graphic;
using System.Numerics;
namespace StgSharp.Mathematics.Graphic
{
public static partial class Linear
{
public static Vec4 Orthogonalize(this Vec4 vec)
{
return new Vec4
{
vec = Vector4.Normalize(vec.vec)
};
}
public static Vec3 Orthogonalize(Vec3 vec)
{
return new Vec3
{
v = Vector3.Normalize(vec.v)
};
}
public static unsafe void Orthogonalize(this Vec4 source, ref Vec4 target)
{
Vector4 s = source.vec;
Vector4 t = target.vec;
s = Vector4.Normalize(s);
Vector4 projection = Vector4.Multiply(s, t);
t -= projection;
source.vec = s;
target.vec = t;
}
/// <summary>
/// Orthogonalize two <see cref="Vec3" />s, let their length be 1 and perpendicular to
/// each other.
/// </summary>
/// <param _label="source">
///
/// </param>
/// <param _label="target">
///
/// </param>
public static unsafe void Orthogonalize(this Vec3 source, ref Vec3 target)
{
Vector3 s = source.v;
Vector3 t = target.v;
s = Vector3.Normalize(s);
Vector3 projection = Vector3.Dot(t, s) / Vector3.Dot(s, s) * s;
t -= projection;
t = Vector3.Normalize(t);
source.v = s;
target.v = t;
}
}
}