From 99b3185f3875d91c1e683fdf58abab18c8d8cc4d Mon Sep 17 00:00:00 2001 From: That_One_Nerd Date: Tue, 5 Sep 2023 06:52:02 -0400 Subject: [PATCH] All matrix types are no longer records. --- Nerd_STF/Mathematics/Abstract/IStaticMatrix.cs | 2 +- Nerd_STF/Mathematics/Algebra/Matrix.cs | 16 +++++++--------- Nerd_STF/Mathematics/Algebra/Matrix2x2.cs | 10 +++++++++- Nerd_STF/Mathematics/Algebra/Matrix3x3.cs | 10 +++++++++- Nerd_STF/Mathematics/Algebra/Matrix4x4.cs | 10 +++++++++- Nerd_STF/Mathematics/Algebra/ProjectionMatrix.cs | 2 +- 6 files changed, 36 insertions(+), 14 deletions(-) diff --git a/Nerd_STF/Mathematics/Abstract/IStaticMatrix.cs b/Nerd_STF/Mathematics/Abstract/IStaticMatrix.cs index ea3dfd3..bbb8e4f 100644 --- a/Nerd_STF/Mathematics/Abstract/IStaticMatrix.cs +++ b/Nerd_STF/Mathematics/Abstract/IStaticMatrix.cs @@ -1,6 +1,6 @@ namespace Nerd_STF.Mathematics.Abstract; -public interface IStaticMatrix : IAverage, IMatrix, IMedian, +public interface IStaticMatrix : IAverage, IEquatable, IMatrix, IMedian, IMatrixPresets where T : IStaticMatrix { diff --git a/Nerd_STF/Mathematics/Algebra/Matrix.cs b/Nerd_STF/Mathematics/Algebra/Matrix.cs index 11f705b..17f6c6f 100644 --- a/Nerd_STF/Mathematics/Algebra/Matrix.cs +++ b/Nerd_STF/Mathematics/Algebra/Matrix.cs @@ -343,16 +343,14 @@ public class Matrix : IMatrix return result; } - public override bool Equals([NotNullWhen(true)] object? obj) + public override bool Equals(object? obj) { - if (obj == null) return base.Equals(obj); - Type t = obj.GetType(); - if (t == typeof(Matrix)) return Equals((Matrix)obj); - else if (t == typeof(Matrix2x2)) return Equals((Matrix)obj); - else if (t == typeof(Matrix3x3)) return Equals((Matrix)obj); - else if (t == typeof(Matrix4x4)) return Equals((Matrix)obj); - - return base.Equals(obj); + if (obj is null) return base.Equals(obj); + else if (obj is Matrix m) return Equals(m); + else if (obj is Matrix2x2 m2x2) return Equals(m2x2); + else if (obj is Matrix3x3 m3x3) return Equals(m3x3); + else if (obj is Matrix4x4 m4x4) return Equals(m4x4); + return false; } public bool Equals(Matrix? other) { diff --git a/Nerd_STF/Mathematics/Algebra/Matrix2x2.cs b/Nerd_STF/Mathematics/Algebra/Matrix2x2.cs index 0735899..1b702d0 100644 --- a/Nerd_STF/Mathematics/Algebra/Matrix2x2.cs +++ b/Nerd_STF/Mathematics/Algebra/Matrix2x2.cs @@ -1,6 +1,6 @@ namespace Nerd_STF.Mathematics.Algebra; -public record class Matrix2x2 : IStaticMatrix +public class Matrix2x2 : IStaticMatrix { public static Matrix2x2 Identity => new(new[,] { @@ -312,6 +312,12 @@ public record class Matrix2x2 : IStaticMatrix SetRow(rowB, dataA); } + public override bool Equals(object? obj) + { + if (obj is null) return false; + else if (obj is Matrix2x2 m2x2) return Equals(m2x2); + return false; + } public virtual bool Equals(Matrix2x2? other) { if (other is null) return false; @@ -365,6 +371,8 @@ public record class Matrix2x2 : IStaticMatrix public static Float2 operator /(Matrix2x2 a, Float2 b) => (Matrix)a / b; public static Matrix2x2 operator ^(Matrix2x2 a, Matrix2x2 b) => // Single number multiplication. new(a.r1c1 * b.r1c1, a.r1c2 * b.r1c2, a.r2c1 * b.r2c1, a.r2c2 * b.r2c2); + public static bool operator ==(Matrix2x2 a, Matrix2x2 b) => a.Equals(b); + public static bool operator !=(Matrix2x2 a, Matrix2x2 b) => !a.Equals(b); public static explicit operator Matrix2x2(Matrix m) { diff --git a/Nerd_STF/Mathematics/Algebra/Matrix3x3.cs b/Nerd_STF/Mathematics/Algebra/Matrix3x3.cs index 60ccc4b..f766436 100644 --- a/Nerd_STF/Mathematics/Algebra/Matrix3x3.cs +++ b/Nerd_STF/Mathematics/Algebra/Matrix3x3.cs @@ -1,6 +1,6 @@ namespace Nerd_STF.Mathematics.Algebra; -public record class Matrix3x3 : IStaticMatrix +public class Matrix3x3 : IStaticMatrix { public static Matrix3x3 Identity => new(new[,] { @@ -435,6 +435,12 @@ public record class Matrix3x3 : IStaticMatrix SetRow(rowB, dataA); } + public override bool Equals(object? obj) + { + if (obj is null) return false; + else if (obj is Matrix3x3 m3x3) return Equals(m3x3); + return false; + } public virtual bool Equals(Matrix3x3? other) { if (other is null) return false; @@ -512,6 +518,8 @@ public record class Matrix3x3 : IStaticMatrix new(a.r1c1 * b.r1c1, a.r1c2 * b.r1c2, a.r1c3 * b.r1c3, a.r2c1 * b.r2c1, a.r2c2 * b.r2c2, a.r2c3 * b.r2c3, a.r3c1 * b.r3c1, a.r3c2 * b.r3c2, a.r3c3 * b.r3c3); + public static bool operator ==(Matrix3x3 a, Matrix3x3 b) => a.Equals(b); + public static bool operator !=(Matrix3x3 a, Matrix3x3 b) => !a.Equals(b); public static explicit operator Matrix3x3(Matrix m) { diff --git a/Nerd_STF/Mathematics/Algebra/Matrix4x4.cs b/Nerd_STF/Mathematics/Algebra/Matrix4x4.cs index dc35b12..2834f47 100644 --- a/Nerd_STF/Mathematics/Algebra/Matrix4x4.cs +++ b/Nerd_STF/Mathematics/Algebra/Matrix4x4.cs @@ -2,7 +2,7 @@ namespace Nerd_STF.Mathematics.Algebra; -public record class Matrix4x4 : IStaticMatrix +public class Matrix4x4 : IStaticMatrix { public static Matrix4x4 Identity => new(new[,] { @@ -530,6 +530,12 @@ public record class Matrix4x4 : IStaticMatrix SetRow(rowB, dataA); } + public override bool Equals(object? obj) + { + if (obj is null) return false; + else if (obj is Matrix4x4 m4x4) return Equals(m4x4); + return false; + } public virtual bool Equals(Matrix4x4? other) { if (other is null) return false; @@ -639,6 +645,8 @@ public record class Matrix4x4 : IStaticMatrix a.r2c1 * b.r2c1, a.r2c2 * b.r2c2, a.r2c3 * b.r2c3, a.r2c4 * b.r2c4, a.r3c1 * b.r3c1, a.r3c2 * b.r3c2, a.r3c3 * b.r3c3, a.r3c4 * b.r3c4, a.r4c1 * b.r4c1, a.r4c2 * b.r4c2, a.r4c3 * b.r4c3, a.r4c4 * b.r4c4); + public static bool operator ==(Matrix4x4 a, Matrix4x4 b) => a.Equals(b); + public static bool operator !=(Matrix4x4 a, Matrix4x4 b) => !a.Equals(b); public static explicit operator Matrix4x4(Matrix m) { diff --git a/Nerd_STF/Mathematics/Algebra/ProjectionMatrix.cs b/Nerd_STF/Mathematics/Algebra/ProjectionMatrix.cs index da9a4e2..b165cd1 100644 --- a/Nerd_STF/Mathematics/Algebra/ProjectionMatrix.cs +++ b/Nerd_STF/Mathematics/Algebra/ProjectionMatrix.cs @@ -1,6 +1,6 @@ namespace Nerd_STF.Mathematics.Algebra; -public record class ProjectionMatrix : Matrix3x3 +public class ProjectionMatrix : Matrix3x3 { // TODO: i need to remove the record check from everything, add new equals comparitors // and re-implement IsValidProjectionMatrix