using System; using System.Collections; using System.Collections.Generic; using System.Runtime.InteropServices; using System.Text; namespace StgSharp.Math { [StructLayout(LayoutKind.Explicit)] public struct Matrix3x3:IEquatable { [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.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 matrix1, Matrix3x3 matrix2) { return matrix1.Equals(matrix2); } public static bool operator !=(Matrix3x3 matrix1, Matrix3x3 matrix2) { return !matrix1.Equals(matrix2); } public static Matrix3x3 operator +(Matrix3x3 matrix1, Matrix3x3 matrix2) { return new Matrix3x3( matrix1.row1.X + matrix2.row1.X, matrix1.row1.Y + matrix2.row1.Y, matrix1.row1.Z + matrix2.row1.Z, matrix1.row2.X + matrix2.row2.X, matrix1.row2.Y + matrix2.row2.Y, matrix1.row2.Z + matrix2.row2.Z, matrix1.row3.X + matrix2.row3.X, matrix1.row3.Y + matrix2.row3.Y, matrix1.row3.Z + matrix2.row3.Z ); } public static Matrix3x3 operator -(Matrix3x3 matrix1, Matrix3x3 matrix2) { return new Matrix3x3( matrix1.row1.X - matrix2.row1.X, matrix1.row1.Y - matrix2.row1.Y, matrix1.row1.Z - matrix2.row1.Z, matrix1.row2.X - matrix2.row2.X, matrix1.row2.Y - matrix2.row2.Y, matrix1.row2.Z - matrix2.row2.Z, matrix1.row3.X - matrix2.row3.X, matrix1.row3.Y - matrix2.row3.Y, matrix1.row3.Z - matrix2.row3.Z ); } 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 ); } } }