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

110 lines
2.9 KiB
C#

using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Text;
namespace StgSharp.Math
{
[StructLayout(LayoutKind.Explicit)]
public struct Array2x4 : IEquatable<Array2x4>
{
[FieldOffset(0)]
public Vec4D row1;
[FieldOffset(16)]
public Vec4D row2;
public Array2x4(Vec4D r1,Vec4D r2)
{
row1 = r1;
row2 = r2;
}
public bool Equals(Array2x4 other)
{
if (this.GetHashCode() != other.GetHashCode())
{
return false;
}
else
{
return this.row1 == other.row1
&& this.row2 == other.row2;
}
}
public override bool Equals(object obj)
{
if (obj == null)
{
return false;
}
else if (obj.GetType() != typeof(Array2x4))
{
return false;
}
else
{
return this.Equals((Array2x4)obj);
}
}
public override int GetHashCode()
{
return HashCode.Combine(this.row1, this.row2);
}
public override string ToString()
{
return $"{this.row1}\n{this.row2}";
}
public static Array2x4 operator +(Array2x4 array1,Array2x4 array2)
{
return new Array2x4(
array1.row1 + array2.row1,
array1.row2 + array2.row2
) ;
}
public static Array2x4 operator -(Array2x4 array1, Array2x4 array2)
{
return new Array2x4(
array1.row1 - array2.row1,
array1.row2 - array2.row2
);
}
public static Array2x4 operator *(Array2x4 array, float x)
{
return new Array2x4(
array.row1 * x,
array.row2 * x
);
}
public static Array2x4 operator /(Array2x4 array, float x)
{
return new Array2x4(
array.row1 / x,
array.row2 / x
);
}
public static unsafe Array2x2 operator *(Array2x4 array1, Array4x2 array2)
{
float* p1 = &array1.row1.X;
float* p2 = &array2.row2.X;
return new Array2x2(
*(p1 + 0) * *(p2 + 0)+ *(p1 + 1) * *(p2 + 2)+ *(p1 + 2) * *(p2 + 4)+ *(p1 + 3) * *(p2 + 6),
*(p1 + 0) * *(p2 + 1)+ *(p1 + 1) * *(p2 + 3)+ *(p1 + 2) * *(p2 + 5)+ *(p1 + 3) * *(p2 + 7),
*(p1 + 4) * *(p2 + 0)+ *(p1 + 5) * *(p2 + 2)+ *(p1 + 6) * *(p2 + 4)+ *(p1 + 7) * *(p2 + 6),
*(p1 + 4) * *(p2 + 1)+ *(p1 + 5) * *(p2 + 3)+ *(p1 + 6) * *(p2 + 5)+ *(p1 + 7) * *(p2 + 7)
);
}
}
}