Skip to content

Commit

Permalink
Matrix3
Browse files Browse the repository at this point in the history
  • Loading branch information
DomCR committed Feb 17, 2025
1 parent 49464e1 commit fdf684f
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 2 deletions.
1 change: 1 addition & 0 deletions CSMath/CSMath.projitems
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
<Compile Include="$(MSBuildThisFileDirectory)Geometry\LineExtensions.cs" />
<Compile Include="$(MSBuildThisFileDirectory)IVector.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Matrix3.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Matrix3.operators.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Matrix4.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Matrix4.Operators.cs" />
<Compile Include="$(MSBuildThisFileDirectory)MathHelper.cs" />
Expand Down
76 changes: 75 additions & 1 deletion CSMath/Matrix3.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
namespace CSMath
using System;
using System.Text;
using System.Threading;

namespace CSMath
{
public partial struct Matrix3
{
Expand Down Expand Up @@ -81,5 +85,75 @@ public Matrix3(
this.m21 = m21;
this.m22 = m22;
}

public Matrix3(Matrix4 matrix)
{
this.m00 = matrix.m00;
this.m01 = matrix.m01;
this.m02 = matrix.m02;
this.m10 = matrix.m10;
this.m11 = matrix.m11;
this.m12 = matrix.m12;
this.m20 = matrix.m20;
this.m21 = matrix.m21;
this.m22 = matrix.m22;
}

/// <summary>
/// Obtains the transpose matrix.
/// </summary>
/// <returns>Transpose matrix.</returns>
public Matrix3 Transpose()
{
return new Matrix3(this.m00, this.m10, this.m20,
this.m01, this.m11, this.m21,
this.m02, this.m12, this.m22);
}

/// <summary>
/// Gets the rotation matrix from the normal vector (extrusion direction) of an entity.
/// </summary>
/// <param name="zAxis">Normal vector.</param>
/// <returns>Rotation matrix.</returns>
public static Matrix3 ArbitraryAxis(XYZ zAxis)
{
zAxis.Normalize();

if (zAxis.Equals(XYZ.AxisZ))
{
return Matrix3.Identity;
}

XYZ wY = XYZ.AxisY;
XYZ wZ = XYZ.AxisZ;
XYZ aX;

if ((Math.Abs(zAxis.X) < 1 / 64.0) && (Math.Abs(zAxis.Y) < 1 / 64.0))
{
aX = XYZ.Cross(wY, zAxis);
}
else
{
aX = XYZ.Cross(wZ, zAxis);
}

aX.Normalize();

XYZ aY = XYZ.Cross(zAxis, aX);
aY.Normalize();

return new Matrix3(aX.X, aY.X, zAxis.X, aX.Y, aY.Y, zAxis.Y, aX.Z, aY.Z, zAxis.Z);
}

/// <inheritdoc/>
public override string ToString()
{
string separator = Thread.CurrentThread.CurrentCulture.TextInfo.ListSeparator;
StringBuilder s = new StringBuilder();
s.Append(string.Format("|{0}{2} {0}{2} {1}|" + Environment.NewLine, this.m00, this.m01, this.m02, separator));
s.Append(string.Format("|{0}{2} {0}{2} {1}|" + Environment.NewLine, this.m10, this.m11, this.m12, separator));
s.Append(string.Format("|{0}{2} {0}{2} {1}|", this.m20, this.m21, this.m22, separator));
return s.ToString();
}
}
}
12 changes: 12 additions & 0 deletions CSMath/Matrix3.operators.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
namespace CSMath
{
public partial struct Matrix3
{
public static XYZ operator *(Matrix3 matrix, XYZ value)
{
return new XYZ(matrix.m00 * value.X + matrix.m01 * value.Y + matrix.m02 * value.Z,
matrix.m10 * value.X + matrix.m11 * value.Y + matrix.m12 * value.Z,
matrix.m20 * value.X + matrix.m21 * value.Y + matrix.m22 * value.Z);
}
}
}
2 changes: 1 addition & 1 deletion CSMath/Matrix4.Operators.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public static Matrix4 Multiply(Matrix4 a, Matrix4 b)
public static XYZ operator *(Matrix4 matrix, XYZ value)
{
XYZM xyzm = new XYZM(value.X, value.Y, value.Z, 1);
var rows = matrix.GetRows();
List<XYZM> rows = matrix.GetRows();

XYZ result = new XYZ
{
Expand Down
14 changes: 14 additions & 0 deletions CSMath/Matrix4.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;

namespace CSMath
{
Expand Down Expand Up @@ -471,6 +473,18 @@ public override bool Equals(object obj)
m30 == other.m30 && m31 == other.m31 && m32 == other.m32;
}

/// <inheritdoc/>
public override string ToString()
{
string separator = Thread.CurrentThread.CurrentCulture.TextInfo.ListSeparator;
StringBuilder s = new StringBuilder();
s.Append(string.Format("|{0}{4} {1}{4} {2}{4} {3}|" + Environment.NewLine, this.m00, this.m01, this.m02, this.m03, separator));
s.Append(string.Format("|{0}{4} {1}{4} {2}{4} {3}|" + Environment.NewLine, this.m10, this.m11, this.m12, this.m13, separator));
s.Append(string.Format("|{0}{4} {1}{4} {2}{4} {3}|" + Environment.NewLine, this.m20, this.m21, this.m22, this.m23, separator));
s.Append(string.Format("|{0}{4} {1}{4} {2}{4} {3}|", this.m30, this.m31, this.m32, this.m33, separator));
return s.ToString();
}

/// <summary>
/// Creates a translation matrix.
/// </summary>
Expand Down

0 comments on commit fdf684f

Please sign in to comment.