Skip to content

Commit f057215

Browse files
author
Dominic Beger
committed
Change Sharpmath.Geometry classes to structs and revise the whole architecture
1 parent 4ade516 commit f057215

32 files changed

+3165
-2468
lines changed

SharpMath.Tests/MatrixTest.cs

Lines changed: 147 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -12,37 +12,76 @@ public class MatrixTest
1212
[TestMethod]
1313
public void CanMultiplyMatrices()
1414
{
15-
var firstMatrix = new Matrix(2, 3);
16-
var secondMatrix = new Matrix(3, 2);
15+
var firstMatrix = new Matrix1x3();
16+
var secondMatrix = new Matrix3x1();
1717

1818
firstMatrix[0, 0] = 1;
1919
firstMatrix[0, 1] = 2;
2020
firstMatrix[0, 2] = 3;
21-
firstMatrix[1, 0] = 3;
22-
firstMatrix[1, 1] = 1;
23-
firstMatrix[1, 2] = 1;
2421

2522
secondMatrix[0, 0] = 2;
26-
secondMatrix[0, 1] = 1;
2723
secondMatrix[1, 0] = 1;
28-
secondMatrix[1, 1] = 2;
2924
secondMatrix[2, 0] = 2;
30-
secondMatrix[2, 1] = 1;
3125

32-
var matrixProduct = Matrix.Multiply(firstMatrix, secondMatrix);
26+
var matrixProduct = MatrixUtils.Multiply<Matrix1x1>(firstMatrix, secondMatrix);
3327
Assert.AreEqual(matrixProduct[0, 0], 10);
34-
Assert.AreEqual(matrixProduct[0, 1], 8);
35-
Assert.AreEqual(matrixProduct[1, 0], 9);
36-
Assert.AreEqual(matrixProduct[1, 1], 6);
28+
29+
// --------------------------------
30+
31+
var thirdMatrix = new Matrix3x3
32+
{
33+
[0, 0] = 2,
34+
[0, 1] = 5,
35+
[0, 2] = 2,
36+
[1, 0] = 3,
37+
[1, 1] = -3,
38+
[1, 2] = 1,
39+
[2, 0] = 1,
40+
[2, 1] = 4,
41+
[2, 2] = -4
42+
};
43+
44+
var fourthMatrix = new Matrix3x3
45+
{
46+
[0, 0] = -4,
47+
[0, 1] = 2.5,
48+
[0, 2] = 3,
49+
[1, 0] = 5,
50+
[1, 1] = 6,
51+
[1, 2] = 4,
52+
[2, 0] = 9,
53+
[2, 1] = 10,
54+
[2, 2] = -9
55+
};
56+
57+
/*
58+
35.000 55.000 8.000
59+
-18.000 -0.500 -12.000
60+
-20.000 -13.500 55.000
61+
*/
62+
var resultMatrix = new Matrix3x3()
63+
{
64+
[0, 0] = 35,
65+
[0, 1] = 55,
66+
[0, 2] = 8,
67+
[1, 0] = -18,
68+
[1, 1] = -0.5,
69+
[1, 2] = -12,
70+
[2, 0] = -20,
71+
[2, 1] = -13.5,
72+
[2, 2] = 55
73+
};
74+
75+
Assert.AreEqual(resultMatrix, MatrixUtils.Multiply<Matrix3x3>(thirdMatrix, fourthMatrix));
3776
}
3877

3978
[TestMethod]
4079
public void CanCalculateDeterminant()
4180
{
42-
var firstMatrix = new SquareMatrix(1) {[0, 0] = 2};
81+
var firstMatrix = new Matrix1x1() {[0, 0] = 2};
4382
Assert.AreEqual(2, firstMatrix.Determinant);
4483

45-
var secondMatrix = new SquareMatrix(2)
84+
var secondMatrix = new Matrix2x2()
4685
{
4786
[0, 0] = 8,
4887
[0, 1] = 3,
@@ -51,7 +90,7 @@ public void CanCalculateDeterminant()
5190
};
5291
Assert.AreEqual(4, secondMatrix.Determinant);
5392

54-
var thirdMatrix = new SquareMatrix(3)
93+
var thirdMatrix = new Matrix3x3
5594
{
5695
[0, 0] = 2,
5796
[0, 1] = 5,
@@ -64,10 +103,9 @@ public void CanCalculateDeterminant()
64103
[2, 2] = -4
65104
};
66105

67-
68106
Assert.AreEqual(111, thirdMatrix.Determinant);
69107

70-
var fourthMatrix = new SquareMatrix(3)
108+
var fourthMatrix = new Matrix3x3
71109
{
72110
[0, 0] = -4,
73111
[0, 1] = 2.5,
@@ -82,7 +120,7 @@ public void CanCalculateDeterminant()
82120

83121
Assert.AreEqual(566.5, fourthMatrix.Determinant);
84122

85-
var fifthMatrix = new SquareMatrix(4)
123+
var fifthMatrix = new Matrix4x4
86124
{
87125
[0, 0] = 1,
88126
[0, 1] = 0,
@@ -111,5 +149,96 @@ public void CanGetStringRepresentation()
111149
var matrix = Matrix4x4.View(Vector3.Zero, Vector3.Forward, Vector3.Up);
112150
Debug.Print(matrix.ToString());
113151
}
152+
153+
[TestMethod]
154+
public void CanDetermineIfMatrixIsDiagonal()
155+
{
156+
var matrix = new Matrix3x3()
157+
{
158+
M11 = 1,
159+
M22 = 1,
160+
M33 = 1
161+
};
162+
Assert.IsTrue(matrix.IsDiagonal);
163+
164+
var secondMatrix = new Matrix4x4()
165+
{
166+
M11 = 1,
167+
M22 = 1,
168+
M33 = 1,
169+
M44 = 1,
170+
};
171+
Assert.IsTrue(secondMatrix.IsDiagonal);
172+
173+
var thirdMatrix = new Matrix4x4()
174+
{
175+
M11 = 1,
176+
M22 = 1,
177+
M33 = 1,
178+
M44 = 1,
179+
M14 = 1,
180+
};
181+
Assert.IsFalse(thirdMatrix.IsDiagonal);
182+
183+
var fourthMatrix = new Matrix4x4()
184+
{
185+
M11 = 1,
186+
M22 = 1,
187+
M33 = 1,
188+
};
189+
Assert.IsFalse(fourthMatrix.IsDiagonal);
190+
}
191+
192+
[TestMethod]
193+
public void CanDetermineIfMatrixIsTriangle()
194+
{
195+
var matrix = new Matrix3x3()
196+
{
197+
M11 = 1,
198+
M22 = 2,
199+
M33 = 3,
200+
201+
M12 = 3,
202+
M13 = 4,
203+
M23 = 7,
204+
};
205+
Assert.IsTrue(matrix.IsTriangle);
206+
207+
var secondMatrix = new Matrix4x4()
208+
{
209+
M11 = 1,
210+
M22 = 2,
211+
M33 = 3,
212+
M44 = 8,
213+
214+
M12 = 3,
215+
M13 = 4,
216+
M14 = 7,
217+
M23 = 2,
218+
M24 = 3,
219+
M34 = 9
220+
};
221+
Assert.IsTrue(secondMatrix.IsTriangle);
222+
223+
var thirdMatrix = new Matrix4x4()
224+
{
225+
M11 = 5,
226+
M22 = 7,
227+
M33 = 4,
228+
M44 = 3,
229+
M14 = 2,
230+
M31 = 9,
231+
};
232+
Assert.IsFalse(thirdMatrix.IsTriangle);
233+
234+
var fourthMatrix = new Matrix4x4()
235+
{
236+
M11 = 1,
237+
M22 = 1,
238+
M33 = 1,
239+
M44 = 1
240+
};
241+
Assert.IsTrue(fourthMatrix.IsTriangle);
242+
}
114243
}
115244
}

SharpMath.Tests/VectorTest.cs

Lines changed: 31 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -46,26 +46,26 @@ public void CanCalculateLength()
4646
[TestMethod]
4747
public void CanCalculateScalarProduct()
4848
{
49-
Assert.AreEqual(0, Vector2.UnitX.ScalarProduct(Vector2.UnitY));
50-
Assert.AreEqual(20, Vector.ScalarProduct(new Vector2(2, 4), new Vector2(4, 3)));
51-
Assert.AreEqual(0, Vector.ScalarProduct(Vector3.Forward, Vector3.Up));
52-
Assert.AreEqual(8, Vector.ScalarProduct(new Vector3(2, 3, 1), new Vector3(-1, 2, 4)));
49+
Assert.AreEqual(0, Vector2.UnitX.DotProduct(Vector2.UnitY));
50+
Assert.AreEqual(20, VectorUtils.DotProduct(new Vector2(2, 4), new Vector2(4, 3)));
51+
Assert.AreEqual(0, VectorUtils.DotProduct(Vector3.Forward, Vector3.Up));
52+
Assert.AreEqual(8, VectorUtils.DotProduct(new Vector3(2, 3, 1), new Vector3(-1, 2, 4)));
5353
}
5454

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

6363
[TestMethod]
6464
public void CanCalculateCrossProduct()
6565
{
6666
var vector = new Vector3(1, -5, 2);
6767
var secondVector = new Vector3(2, 0, 3);
68-
var resultVector = vector.CrossProduct(secondVector);
68+
var resultVector = vector.VectorProduct(secondVector);
6969

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

8282
[TestMethod]
@@ -135,7 +135,7 @@ public void CanCalculateAngle()
135135
var vector2 = Vector2.UnitY;
136136

137137
Assert.AreEqual(Math.PI/2, vector1.Angle(vector2));
138-
Assert.IsTrue(vector1.IsOrthogonalTo(vector2));
138+
Assert.IsTrue(vector1.CheckForOrthogonality(vector2));
139139
}
140140

141141
[TestMethod]
@@ -170,7 +170,7 @@ public void CompareAreaCalculations()
170170
var secondVector = new Vector3(1, -2, 3);
171171

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

191191
stopWatch.Restart();
192192
double secondCrossProductArea =
193-
Vector3.CrossProduct(thirdVector.Convert<Vector3>(), fourthVector.Convert<Vector3>()).Magnitude;
193+
Vector3.VectorProduct(thirdVector.Convert<Vector3>(), fourthVector.Convert<Vector3>()).Magnitude;
194194
stopWatch.Stop();
195195
Debug.Print("Vector2 area calculation over the cross product takes " + stopWatch.ElapsedMilliseconds +
196196
" milliseconds.");
@@ -207,79 +207,76 @@ public void CompareAreaCalculations()
207207
[TestMethod]
208208
public void CanDetermineIfVectorsAreOrthogonal()
209209
{
210-
Assert.IsTrue(Vector3.Forward.IsOrthogonalTo(Vector3.Up));
211-
Assert.IsFalse(Vector3.Forward.IsOrthogonalTo(Vector3.Back));
212-
Assert.IsFalse(Vector3.Zero.IsOrthogonalTo(Vector3.UnitX));
210+
Assert.IsTrue(Vector3.Forward.CheckForOrthogonality(Vector3.Up));
211+
Assert.IsFalse(Vector3.Forward.CheckForOrthogonality(Vector3.Back));
212+
Assert.IsFalse(Vector3.Zero.CheckForOrthogonality(Vector3.UnitX));
213213
}
214214

215215
[TestMethod]
216216
public void CanDetermineIfVectorsAreOrthonormal()
217217
{
218-
Assert.IsTrue(Vector3.Forward.IsOrthonormalTo(Vector3.Up));
219-
Assert.IsTrue(Vector3.Back.IsOrthonormalTo(Vector3.Down));
220-
Assert.IsFalse(Vector3.Forward.IsOrthonormalTo(Vector3.Back));
221-
Assert.IsFalse(Vector3.Forward.IsOrthonormalTo(new Vector3(2, 3, 2)));
218+
Assert.IsTrue(Vector3.Forward.CheckForOrthonormality(Vector3.Up));
219+
Assert.IsTrue(Vector3.Back.CheckForOrthonormality(Vector3.Down));
220+
Assert.IsFalse(Vector3.Forward.CheckForOrthonormality(Vector3.Back));
221+
Assert.IsFalse(Vector3.Forward.CheckForOrthonormality(new Vector3(2, 3, 2)));
222222
}
223223

224224
[TestMethod]
225225
public void CanDetermineIfVectorsAreParallel()
226226
{
227-
Assert.IsTrue(new Vector3(2, 3, 3).IsParallelTo(new Vector3(4, 6, 6)));
228-
Assert.IsTrue(new Vector3(1, 2, 3).IsParallelTo(new Vector3(3, 6, 9)));
229-
Assert.IsFalse(new Vector3(0, 1, 3).IsParallelTo(new Vector3(0, 3, 2)));
227+
Assert.IsTrue(new Vector3(2, 3, 3).CheckForParallelism(new Vector3(4, 6, 6)));
228+
Assert.IsTrue(new Vector3(1, 2, 3).CheckForParallelism(new Vector3(3, 6, 9)));
229+
Assert.IsFalse(new Vector3(0, 1, 3).CheckForParallelism(new Vector3(0, 3, 2)));
230230
}
231231

232232
[TestMethod]
233233
public void CanConvertVectorIntoMatrices()
234234
{
235-
var firstMatrix = new Matrix(3, 1)
235+
var firstMatrix = new Matrix3x1()
236236
{
237237
[0, 0] = 1,
238238
[1, 0] = 0,
239239
[2, 0] = 0
240240
};
241-
var firstVectorMatrix = Vector3.Right.AsVerticalMatrix();
241+
var firstVectorMatrix = Vector3.Right.AsVerticalMatrix<Matrix3x1>();
242242
Assert.AreEqual(firstMatrix, firstVectorMatrix);
243243

244-
var secondMatrix = new Matrix(1, 3)
244+
var secondMatrix = new Matrix1x3()
245245
{
246246
[0, 0] = 1,
247247
[0, 1] = 0,
248248
[0, 2] = 0
249249
};
250-
var secondVectorMatrix = Vector3.Right.AsHorizontalMatrix();
250+
var secondVectorMatrix = Vector3.Right.AsHorizontalMatrix<Matrix1x3>();
251251
Assert.AreEqual(secondMatrix, secondVectorMatrix);
252252
}
253253

254254
[TestMethod]
255255
public void CanCompareVectors()
256256
{
257257
// Let's see, if the dimension check is working
258-
var vector = new Vector(2);
259-
var secondVector = new Vector(3);
258+
var vector = new Vector3();
259+
var secondVector = new Vector2();
260260
Assert.IsFalse(vector.Equals(secondVector));
261-
Assert.IsFalse(vector == secondVector);
262-
Assert.IsTrue(vector != secondVector);
263261

264262
// Let's see, if the coordinate comparison is working (in this case simply two zero vectors)
265-
var thirdVector = new Vector(4);
266-
var fourthVector = new Vector(4);
263+
var thirdVector = new Vector4();
264+
var fourthVector = new Vector4();
267265
Assert.IsTrue(thirdVector.Equals(fourthVector));
268266
Assert.IsTrue(thirdVector == fourthVector);
269267
Assert.IsFalse(thirdVector != fourthVector);
270268

271269
// Let's see, if the coordinate comparison is working when we have the same dimension but different coordinate values
272-
var fifthVector = new Vector(3)
270+
var fifthVector = new Vector3()
273271
{
274272
[0] = 1,
275273
[1] = 2,
276274
[2] = 1
277275
};
278-
279-
Assert.AreEqual(fifthVector, new Vector3(1, 2, 1));
276+
280277
Assert.AreEqual(new Vector3(1, 2, 1), fifthVector);
281278

282-
var sixthVector = new Vector(3)
279+
var sixthVector = new Vector3()
283280
{
284281
[0] = 2,
285282
[1] = 2,

0 commit comments

Comments
 (0)