Skip to content

Commit

Permalink
Change Sharpmath.Geometry classes to structs and revise the whole arc…
Browse files Browse the repository at this point in the history
…hitecture
  • Loading branch information
Dominic Beger committed Apr 30, 2016
1 parent 4ade516 commit f057215
Show file tree
Hide file tree
Showing 32 changed files with 3,165 additions and 2,468 deletions.
165 changes: 147 additions & 18 deletions SharpMath.Tests/MatrixTest.cs
Expand Up @@ -12,37 +12,76 @@ public class MatrixTest
[TestMethod]
public void CanMultiplyMatrices()
{
var firstMatrix = new Matrix(2, 3);
var secondMatrix = new Matrix(3, 2);
var firstMatrix = new Matrix1x3();
var secondMatrix = new Matrix3x1();

firstMatrix[0, 0] = 1;
firstMatrix[0, 1] = 2;
firstMatrix[0, 2] = 3;
firstMatrix[1, 0] = 3;
firstMatrix[1, 1] = 1;
firstMatrix[1, 2] = 1;

secondMatrix[0, 0] = 2;
secondMatrix[0, 1] = 1;
secondMatrix[1, 0] = 1;
secondMatrix[1, 1] = 2;
secondMatrix[2, 0] = 2;
secondMatrix[2, 1] = 1;

var matrixProduct = Matrix.Multiply(firstMatrix, secondMatrix);
var matrixProduct = MatrixUtils.Multiply<Matrix1x1>(firstMatrix, secondMatrix);
Assert.AreEqual(matrixProduct[0, 0], 10);
Assert.AreEqual(matrixProduct[0, 1], 8);
Assert.AreEqual(matrixProduct[1, 0], 9);
Assert.AreEqual(matrixProduct[1, 1], 6);

// --------------------------------

var thirdMatrix = new Matrix3x3
{
[0, 0] = 2,
[0, 1] = 5,
[0, 2] = 2,
[1, 0] = 3,
[1, 1] = -3,
[1, 2] = 1,
[2, 0] = 1,
[2, 1] = 4,
[2, 2] = -4
};

var fourthMatrix = new Matrix3x3
{
[0, 0] = -4,
[0, 1] = 2.5,
[0, 2] = 3,
[1, 0] = 5,
[1, 1] = 6,
[1, 2] = 4,
[2, 0] = 9,
[2, 1] = 10,
[2, 2] = -9
};

/*
35.000 55.000 8.000
-18.000 -0.500 -12.000
-20.000 -13.500 55.000
*/
var resultMatrix = new Matrix3x3()
{
[0, 0] = 35,
[0, 1] = 55,
[0, 2] = 8,
[1, 0] = -18,
[1, 1] = -0.5,
[1, 2] = -12,
[2, 0] = -20,
[2, 1] = -13.5,
[2, 2] = 55
};

Assert.AreEqual(resultMatrix, MatrixUtils.Multiply<Matrix3x3>(thirdMatrix, fourthMatrix));
}

[TestMethod]
public void CanCalculateDeterminant()
{
var firstMatrix = new SquareMatrix(1) {[0, 0] = 2};
var firstMatrix = new Matrix1x1() {[0, 0] = 2};
Assert.AreEqual(2, firstMatrix.Determinant);

var secondMatrix = new SquareMatrix(2)
var secondMatrix = new Matrix2x2()
{
[0, 0] = 8,
[0, 1] = 3,
Expand All @@ -51,7 +90,7 @@ public void CanCalculateDeterminant()
};
Assert.AreEqual(4, secondMatrix.Determinant);

var thirdMatrix = new SquareMatrix(3)
var thirdMatrix = new Matrix3x3
{
[0, 0] = 2,
[0, 1] = 5,
Expand All @@ -64,10 +103,9 @@ public void CanCalculateDeterminant()
[2, 2] = -4
};


Assert.AreEqual(111, thirdMatrix.Determinant);

var fourthMatrix = new SquareMatrix(3)
var fourthMatrix = new Matrix3x3
{
[0, 0] = -4,
[0, 1] = 2.5,
Expand All @@ -82,7 +120,7 @@ public void CanCalculateDeterminant()

Assert.AreEqual(566.5, fourthMatrix.Determinant);

var fifthMatrix = new SquareMatrix(4)
var fifthMatrix = new Matrix4x4
{
[0, 0] = 1,
[0, 1] = 0,
Expand Down Expand Up @@ -111,5 +149,96 @@ public void CanGetStringRepresentation()
var matrix = Matrix4x4.View(Vector3.Zero, Vector3.Forward, Vector3.Up);
Debug.Print(matrix.ToString());
}

[TestMethod]
public void CanDetermineIfMatrixIsDiagonal()
{
var matrix = new Matrix3x3()
{
M11 = 1,
M22 = 1,
M33 = 1
};
Assert.IsTrue(matrix.IsDiagonal);

var secondMatrix = new Matrix4x4()
{
M11 = 1,
M22 = 1,
M33 = 1,
M44 = 1,
};
Assert.IsTrue(secondMatrix.IsDiagonal);

var thirdMatrix = new Matrix4x4()
{
M11 = 1,
M22 = 1,
M33 = 1,
M44 = 1,
M14 = 1,
};
Assert.IsFalse(thirdMatrix.IsDiagonal);

var fourthMatrix = new Matrix4x4()
{
M11 = 1,
M22 = 1,
M33 = 1,
};
Assert.IsFalse(fourthMatrix.IsDiagonal);
}

[TestMethod]
public void CanDetermineIfMatrixIsTriangle()
{
var matrix = new Matrix3x3()
{
M11 = 1,
M22 = 2,
M33 = 3,

M12 = 3,
M13 = 4,
M23 = 7,
};
Assert.IsTrue(matrix.IsTriangle);

var secondMatrix = new Matrix4x4()
{
M11 = 1,
M22 = 2,
M33 = 3,
M44 = 8,

M12 = 3,
M13 = 4,
M14 = 7,
M23 = 2,
M24 = 3,
M34 = 9
};
Assert.IsTrue(secondMatrix.IsTriangle);

var thirdMatrix = new Matrix4x4()
{
M11 = 5,
M22 = 7,
M33 = 4,
M44 = 3,
M14 = 2,
M31 = 9,
};
Assert.IsFalse(thirdMatrix.IsTriangle);

var fourthMatrix = new Matrix4x4()
{
M11 = 1,
M22 = 1,
M33 = 1,
M44 = 1
};
Assert.IsTrue(fourthMatrix.IsTriangle);
}
}
}
65 changes: 31 additions & 34 deletions SharpMath.Tests/VectorTest.cs
Expand Up @@ -46,26 +46,26 @@ public void CanCalculateLength()
[TestMethod]
public void CanCalculateScalarProduct()
{
Assert.AreEqual(0, Vector2.UnitX.ScalarProduct(Vector2.UnitY));
Assert.AreEqual(20, Vector.ScalarProduct(new Vector2(2, 4), new Vector2(4, 3)));
Assert.AreEqual(0, Vector.ScalarProduct(Vector3.Forward, Vector3.Up));
Assert.AreEqual(8, Vector.ScalarProduct(new Vector3(2, 3, 1), new Vector3(-1, 2, 4)));
Assert.AreEqual(0, Vector2.UnitX.DotProduct(Vector2.UnitY));
Assert.AreEqual(20, VectorUtils.DotProduct(new Vector2(2, 4), new Vector2(4, 3)));
Assert.AreEqual(0, VectorUtils.DotProduct(Vector3.Forward, Vector3.Up));
Assert.AreEqual(8, VectorUtils.DotProduct(new Vector3(2, 3, 1), new Vector3(-1, 2, 4)));
}

[TestMethod]
public void CanCalculateDistance()
{
var vector = new Vector3(10, 5, 3);
var secondVector = new Vector3(5, 6, 1); // 5, -1, 2 // Magnitude: sqrt(30)
Assert.AreEqual(Math.Sqrt(30), vector.DistanceTo(secondVector));
Assert.AreEqual(Math.Sqrt(30), vector.Distance(secondVector));
}

[TestMethod]
public void CanCalculateCrossProduct()
{
var vector = new Vector3(1, -5, 2);
var secondVector = new Vector3(2, 0, 3);
var resultVector = vector.CrossProduct(secondVector);
var resultVector = vector.VectorProduct(secondVector);

Assert.AreEqual(-15, resultVector.X);
Assert.AreEqual(1, resultVector.Y);
Expand All @@ -76,7 +76,7 @@ public void CanCalculateCrossProduct()
public void CanGetCorrectLaTeXString()
{
var vector = new Vector3(1, 6, 8);
Assert.AreEqual(@"\left( \begin{array}{c} 1 \\ 6 \\ 8 \end{array} \right)", vector.LaTeXString);
Assert.AreEqual(@"\left( \begin{array}{c} 1 \\ 6 \\ 8 \end{array} \right)", vector.ToLaTeXString());
}

[TestMethod]
Expand Down Expand Up @@ -135,7 +135,7 @@ public void CanCalculateAngle()
var vector2 = Vector2.UnitY;

Assert.AreEqual(Math.PI/2, vector1.Angle(vector2));
Assert.IsTrue(vector1.IsOrthogonalTo(vector2));
Assert.IsTrue(vector1.CheckForOrthogonality(vector2));
}

[TestMethod]
Expand Down Expand Up @@ -170,7 +170,7 @@ public void CompareAreaCalculations()
var secondVector = new Vector3(1, -2, 3);

stopWatch.Start();
double crossProductArea = Vector3.CrossProduct(firstVector, secondVector).Magnitude;
double crossProductArea = Vector3.VectorProduct(firstVector, secondVector).Magnitude;
stopWatch.Stop();
// This is faster, if the vectors are already 3-dimensional, because we have no arccos, sin etc.
Debug.Print("Vector3 area calculation over the cross product takes " + stopWatch.ElapsedMilliseconds +
Expand All @@ -190,7 +190,7 @@ public void CompareAreaCalculations()

stopWatch.Restart();
double secondCrossProductArea =
Vector3.CrossProduct(thirdVector.Convert<Vector3>(), fourthVector.Convert<Vector3>()).Magnitude;
Vector3.VectorProduct(thirdVector.Convert<Vector3>(), fourthVector.Convert<Vector3>()).Magnitude;
stopWatch.Stop();
Debug.Print("Vector2 area calculation over the cross product takes " + stopWatch.ElapsedMilliseconds +
" milliseconds.");
Expand All @@ -207,79 +207,76 @@ public void CompareAreaCalculations()
[TestMethod]
public void CanDetermineIfVectorsAreOrthogonal()
{
Assert.IsTrue(Vector3.Forward.IsOrthogonalTo(Vector3.Up));
Assert.IsFalse(Vector3.Forward.IsOrthogonalTo(Vector3.Back));
Assert.IsFalse(Vector3.Zero.IsOrthogonalTo(Vector3.UnitX));
Assert.IsTrue(Vector3.Forward.CheckForOrthogonality(Vector3.Up));
Assert.IsFalse(Vector3.Forward.CheckForOrthogonality(Vector3.Back));
Assert.IsFalse(Vector3.Zero.CheckForOrthogonality(Vector3.UnitX));
}

[TestMethod]
public void CanDetermineIfVectorsAreOrthonormal()
{
Assert.IsTrue(Vector3.Forward.IsOrthonormalTo(Vector3.Up));
Assert.IsTrue(Vector3.Back.IsOrthonormalTo(Vector3.Down));
Assert.IsFalse(Vector3.Forward.IsOrthonormalTo(Vector3.Back));
Assert.IsFalse(Vector3.Forward.IsOrthonormalTo(new Vector3(2, 3, 2)));
Assert.IsTrue(Vector3.Forward.CheckForOrthonormality(Vector3.Up));
Assert.IsTrue(Vector3.Back.CheckForOrthonormality(Vector3.Down));
Assert.IsFalse(Vector3.Forward.CheckForOrthonormality(Vector3.Back));
Assert.IsFalse(Vector3.Forward.CheckForOrthonormality(new Vector3(2, 3, 2)));
}

[TestMethod]
public void CanDetermineIfVectorsAreParallel()
{
Assert.IsTrue(new Vector3(2, 3, 3).IsParallelTo(new Vector3(4, 6, 6)));
Assert.IsTrue(new Vector3(1, 2, 3).IsParallelTo(new Vector3(3, 6, 9)));
Assert.IsFalse(new Vector3(0, 1, 3).IsParallelTo(new Vector3(0, 3, 2)));
Assert.IsTrue(new Vector3(2, 3, 3).CheckForParallelism(new Vector3(4, 6, 6)));
Assert.IsTrue(new Vector3(1, 2, 3).CheckForParallelism(new Vector3(3, 6, 9)));
Assert.IsFalse(new Vector3(0, 1, 3).CheckForParallelism(new Vector3(0, 3, 2)));
}

[TestMethod]
public void CanConvertVectorIntoMatrices()
{
var firstMatrix = new Matrix(3, 1)
var firstMatrix = new Matrix3x1()
{
[0, 0] = 1,
[1, 0] = 0,
[2, 0] = 0
};
var firstVectorMatrix = Vector3.Right.AsVerticalMatrix();
var firstVectorMatrix = Vector3.Right.AsVerticalMatrix<Matrix3x1>();
Assert.AreEqual(firstMatrix, firstVectorMatrix);

var secondMatrix = new Matrix(1, 3)
var secondMatrix = new Matrix1x3()
{
[0, 0] = 1,
[0, 1] = 0,
[0, 2] = 0
};
var secondVectorMatrix = Vector3.Right.AsHorizontalMatrix();
var secondVectorMatrix = Vector3.Right.AsHorizontalMatrix<Matrix1x3>();
Assert.AreEqual(secondMatrix, secondVectorMatrix);
}

[TestMethod]
public void CanCompareVectors()
{
// Let's see, if the dimension check is working
var vector = new Vector(2);
var secondVector = new Vector(3);
var vector = new Vector3();
var secondVector = new Vector2();
Assert.IsFalse(vector.Equals(secondVector));
Assert.IsFalse(vector == secondVector);
Assert.IsTrue(vector != secondVector);

// Let's see, if the coordinate comparison is working (in this case simply two zero vectors)
var thirdVector = new Vector(4);
var fourthVector = new Vector(4);
var thirdVector = new Vector4();
var fourthVector = new Vector4();
Assert.IsTrue(thirdVector.Equals(fourthVector));
Assert.IsTrue(thirdVector == fourthVector);
Assert.IsFalse(thirdVector != fourthVector);

// Let's see, if the coordinate comparison is working when we have the same dimension but different coordinate values
var fifthVector = new Vector(3)
var fifthVector = new Vector3()
{
[0] = 1,
[1] = 2,
[2] = 1
};

Assert.AreEqual(fifthVector, new Vector3(1, 2, 1));

Assert.AreEqual(new Vector3(1, 2, 1), fifthVector);

var sixthVector = new Vector(3)
var sixthVector = new Vector3()
{
[0] = 2,
[1] = 2,
Expand Down

0 comments on commit f057215

Please sign in to comment.