Files
stgsharp/源代码/StgSharp/Vector/Matrix3x3.cs
T

165 lines
4.8 KiB
C#

using System;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Security.Cryptography.X509Certificates;
using System.Text;
namespace StgSharp.Math
{
[StructLayout(LayoutKind.Explicit)]
public struct Matrix3x3:IEquatable<Matrix3x3>
{
[FieldOffset(0)]
public Vec3D row1;
[FieldOffset(12)]
public Vec3D row2;
[FieldOffset(24)]
public Vec3D row3;
public Matrix3x3(Vec3D r0,Vec3D r1,Vec3D r2)
{
row1 = r0;
row2 = r1;
row3 = r2;
}
public Matrix3x3(
float a11,float a12,float a13,
float a21,float a22,float a23,
float a31,float a32,float a33
)
{
row1 = new Vec3D(a11, a12, a13);
row2 = new Vec3D(a21, a22, a23);
row3 = new Vec3D(a31, a32, a33);
}
public override int GetHashCode()
{
return HashCode.Combine(row1,row2,row3);
}
bool IEquatable<Matrix3x3>.Equals(Matrix3x3 other)
{
if (this.GetHashCode()!=other.GetHashCode())
{
return false;
}
else
{
return
this.row1 == other.row1
&& this.row2 == other.row2
&& this.row3 == other.row3;
}
}
public override bool Equals(object obj)
{
if (obj == null)
{
return false;
}
else if (obj.GetType() != typeof(Matrix3x3))
{
return false;
}
else
{
return this.Equals((Matrix3x3)obj);
}
}
public override string ToString()
{
return $"{row1}\n{row2}\n{row3}";
}
public static bool operator ==(Matrix3x3 array1, Matrix3x3 array2)
{
return array1.Equals(array2);
}
public static bool operator !=(Matrix3x3 array1, Matrix3x3 array2)
{
return !array1.Equals(array2);
}
public static Matrix3x3 operator +(Matrix3x3 array1, Matrix3x3 array2)
{
return new Matrix3x3(
array1.row1.X + array2.row1.X,
array1.row1.Y + array2.row1.Y,
array1.row1.Z + array2.row1.Z,
array1.row2.X + array2.row2.X,
array1.row2.Y + array2.row2.Y,
array1.row2.Z + array2.row2.Z,
array1.row3.X + array2.row3.X,
array1.row3.Y + array2.row3.Y,
array1.row3.Z + array2.row3.Z
);
}
public static Matrix3x3 operator -(Matrix3x3 array1, Matrix3x3 array2)
{
return new Matrix3x3(
array1.row1.X - array2.row1.X,
array1.row1.Y - array2.row1.Y,
array1.row1.Z - array2.row1.Z,
array1.row2.X - array2.row2.X,
array1.row2.Y - array2.row2.Y,
array1.row2.Z - array2.row2.Z,
array1.row3.X - array2.row3.X,
array1.row3.Y - array2.row3.Y,
array1.row3.Z - array2.row3.Z
);
}
public static unsafe Matrix3x3 operator *(Matrix3x3 array1,Matrix3x3 array2)
{
float* p1 = &array1.row1.X;
float* p2 = &array2.row1.X;
return new Matrix3x3(
*(p1 + 0) * *(p2 + 0) + *(p1 + 1) * *(p2 + 3) + *(p1 + 2) * *(p2 + 6),
*(p1 + 0) * *(p2 + 1) + *(p1 + 1) * *(p2 + 4) + *(p1 + 2) * *(p2 + 7),
*(p1 + 0) * *(p2 + 2) + *(p1 + 1) * *(p2 + 5) + *(p1 + 2) * *(p2 + 8),
*(p1 + 3) * *(p2 + 0) + *(p1 + 4) * *(p2 + 3) + *(p1 + 5) * *(p2 + 6),
*(p1 + 3) * *(p2 + 1) + *(p1 + 4) * *(p2 + 4) + *(p1 + 5) * *(p2 + 7),
*(p1 + 3) * *(p2 + 2) + *(p1 + 4) * *(p2 + 5) + *(p1 + 5) * *(p2 + 8),
*(p1 + 6) * *(p2 + 0) + *(p1 + 7) * *(p2 + 3) + *(p1 + 2) * *(p2 + 6),
*(p1 + 6) * *(p2 + 1) + *(p1 + 7) * *(p2 + 4) + *(p1 + 2) * *(p2 + 7),
*(p1 + 6) * *(p2 + 2) + *(p1 + 7) * *(p2 + 5) + *(p1 + 2) * *(p2 + 8)
);
}
public static Matrix3x3 operator *(Matrix3x3 array, float x)
{
return new Matrix3x3(
array.row1 * x,
array.row2 * x,
array.row3 * x
);
}
public static Matrix3x3 operator /(Matrix3x3 array, float x)
{
return new Matrix3x3(
array.row1/x,
array.row2/x,
array.row3/x
);
}
}
}