Skip to content

Commit da61232

Browse files
author
Dominic Beger
committed
Implement all operators in the base classes and improve some methods
1 parent 7f4f0f7 commit da61232

File tree

7 files changed

+386
-57
lines changed

7 files changed

+386
-57
lines changed

SharpMath/Geometry/Matrix.cs

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,42 @@ internal void SubtractRows(uint firstRowIndex, uint secondRowIndex, double facto
350350
this[firstRowIndex, x] -= this[secondRowIndex, x]*factor;
351351
}
352352

353+
/// <summary>
354+
/// Adds two <see cref="Matrix" /> instances, if they are compatible to each other.
355+
/// </summary>
356+
/// <param name="firstMatrix">The first <see cref="Matrix" />.</param>
357+
/// <param name="secondMatrix">The second <see cref="Matrix" />.</param>
358+
/// <returns>The resulting <see cref="Matrix" />.</returns>
359+
public static Matrix Add(Matrix firstMatrix, Matrix secondMatrix)
360+
{
361+
if (firstMatrix.ColumnCount != secondMatrix.ColumnCount)
362+
throw new InvalidOperationException(
363+
"Cannot the add the matrices as their amount of columns or rows are not equal.");
364+
365+
for (uint y = 0; y < firstMatrix.RowCount; ++y)
366+
for (uint x = 0; x < firstMatrix.ColumnCount; ++x)
367+
firstMatrix[y, x] += secondMatrix[y, x];
368+
return firstMatrix;
369+
}
370+
371+
/// <summary>
372+
/// Adds two <see cref="Matrix" /> instances, if they are compatible to each other.
373+
/// </summary>
374+
/// <param name="firstMatrix">The first <see cref="Matrix" />.</param>
375+
/// <param name="secondMatrix">The second <see cref="Matrix" />.</param>
376+
/// <returns>The resulting <see cref="Matrix" />.</returns>
377+
public static Matrix Subtract(Matrix firstMatrix, Matrix secondMatrix)
378+
{
379+
if (firstMatrix.ColumnCount != secondMatrix.ColumnCount)
380+
throw new InvalidOperationException(
381+
"Cannot the add the matrices as their amount of columns or rows are not equal.");
382+
383+
for (uint y = 0; y < firstMatrix.RowCount; ++y)
384+
for (uint x = 0; x < firstMatrix.ColumnCount; ++x)
385+
firstMatrix[y, x] -= secondMatrix[y, x];
386+
return firstMatrix;
387+
}
388+
353389
/// <summary>
354390
/// Multiplies a <see cref="Matrix" /> with a scalar.
355391
/// </summary>
@@ -401,6 +437,71 @@ public static Matrix Divide(Matrix matrix, double scalar)
401437
return Multiply(matrix, (1/scalar));
402438
}
403439

440+
/// <summary>
441+
/// Implements the operator +.
442+
/// </summary>
443+
/// <param name="firstMatrix">The first <see cref="Matrix"/>.</param>
444+
/// <param name="secondMatrix">The second <see cref="Matrix"/>.</param>
445+
/// <returns>
446+
/// The result of the operator.
447+
/// </returns>
448+
public static Matrix operator +(Matrix firstMatrix, Matrix secondMatrix)
449+
{
450+
return Add(firstMatrix, secondMatrix);
451+
}
452+
453+
/// <summary>
454+
/// Implements the operator -.
455+
/// </summary>
456+
/// <param name="firstMatrix">The first <see cref="Matrix"/>.</param>
457+
/// <param name="secondMatrix">The second <see cref="Matrix"/>.</param>
458+
/// <returns>
459+
/// The result of the operator.
460+
/// </returns>
461+
public static Matrix operator -(Matrix firstMatrix, Matrix secondMatrix)
462+
{
463+
return Subtract(firstMatrix, secondMatrix);
464+
}
465+
466+
/// <summary>
467+
/// Implements the operator * to multiply a <see cref="Matrix"/> with the specified scalar.
468+
/// </summary>
469+
/// <param name="scalar">The scalar.</param>
470+
/// <param name="matrix">The <see cref="Matrix"/>.</param>
471+
/// <returns>
472+
/// The result of the operator.
473+
/// </returns>
474+
public static Matrix operator *(double scalar, Matrix matrix)
475+
{
476+
return Multiply(matrix, scalar);
477+
}
478+
479+
/// <summary>
480+
/// Implements the operator * to multiply a <see cref="Matrix"/> with the specified scalar.
481+
/// </summary>
482+
/// <param name="matrix">The <see cref="Matrix"/>.</param>
483+
/// <param name="scalar">The scalar.</param>
484+
/// <returns>
485+
/// The result of the operator.
486+
/// </returns>
487+
public static Matrix operator *(Matrix matrix, double scalar)
488+
{
489+
return Multiply(matrix, scalar);
490+
}
491+
492+
/// <summary>
493+
/// Implements the operator *.
494+
/// </summary>
495+
/// <param name="firstMatrix">The first <see cref="Matrix"/>.</param>
496+
/// <param name="secondMatrix">The second <see cref="Matrix"/>.</param>
497+
/// <returns>
498+
/// The result of the operator.
499+
/// </returns>
500+
public static Matrix operator *(Matrix firstMatrix, Matrix secondMatrix)
501+
{
502+
return Multiply(firstMatrix, secondMatrix);
503+
}
504+
404505
/// <summary>
405506
/// Determines whether the specified <see cref="object" />, is equal to this instance.
406507
/// </summary>

SharpMath/Geometry/Matrix3x3.cs

Lines changed: 74 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -108,56 +108,122 @@ public static Matrix3x3 Translation(double x, double y)
108108
return matrix;
109109
}
110110

111+
/// <summary>
112+
/// Implements the operator +.
113+
/// </summary>
114+
/// <param name="firstMatrix">The first <see cref="Matrix3x3"/>.</param>
115+
/// <param name="secondMatrix">The second <see cref="Matrix3x3"/>.</param>
116+
/// <returns>
117+
/// The result of the operator.
118+
/// </returns>
111119
public static Matrix3x3 operator +(Matrix3x3 firstMatrix, Matrix3x3 secondMatrix)
112120
{
113-
for (uint y = 0; y < 3; ++y)
114-
for (uint x = 0; x < 3; ++x)
115-
firstMatrix[y, x] += secondMatrix[y, x];
116-
return firstMatrix;
121+
return FromMatrix(Add(firstMatrix, secondMatrix));
117122
}
118123

124+
/// <summary>
125+
/// Implements the operator -.
126+
/// </summary>
127+
/// <param name="firstMatrix">The first <see cref="Matrix3x3"/>.</param>
128+
/// <param name="secondMatrix">The second <see cref="Matrix3x3"/>.</param>
129+
/// <returns>
130+
/// The result of the operator.
131+
/// </returns>
119132
public static Matrix3x3 operator -(Matrix3x3 firstMatrix, Matrix3x3 secondMatrix)
120133
{
121-
for (uint y = 0; y < 3; ++y)
122-
for (uint x = 0; x < 3; ++x)
123-
firstMatrix[y, x] -= secondMatrix[y, x];
124-
return firstMatrix;
134+
return FromMatrix(Subtract(firstMatrix, secondMatrix));
125135
}
126136

137+
/// <summary>
138+
/// Implements the operator * to multiply a <see cref="Matrix3x3"/> with the specified scalar.
139+
/// </summary>
140+
/// <param name="scalar">The scalar.</param>
141+
/// <param name="matrix">The <see cref="Matrix3x3"/>.</param>
142+
/// <returns>
143+
/// The result of the operator.
144+
/// </returns>
127145
public static Matrix3x3 operator *(double scalar, Matrix3x3 matrix)
128146
{
129147
return FromMatrix(Multiply(matrix, scalar));
130148
}
131149

150+
/// <summary>
151+
/// Implements the operator * to multiply a <see cref="Matrix3x3"/> with the specified scalar.
152+
/// </summary>
153+
/// <param name="matrix">The <see cref="Matrix3x3"/>.</param>
154+
/// <param name="scalar">The scalar.</param>
155+
/// <returns>
156+
/// The result of the operator.
157+
/// </returns>
132158
public static Matrix3x3 operator *(Matrix3x3 matrix, double scalar)
133159
{
134160
return FromMatrix(Multiply(matrix, scalar));
135161
}
136162

163+
/// <summary>
164+
/// Implements the operator * to transform a <see cref="Vector2"/> with a <see cref="Matrix3x3"/>.
165+
/// </summary>
166+
/// <param name="matrix">The <see cref="Matrix3x3"/>.</param>
167+
/// <param name="vector">The <see cref="Vector2"/>.</param>
168+
/// <returns>
169+
/// The result of the operator.
170+
/// </returns>
137171
public static Vector2 operator *(Matrix3x3 matrix, Vector2 vector)
138172
{
139173
var resultMatrix = Multiply(matrix, new Vector3(vector.X, vector.Y, 1).AsVerticalMatrix());
140174
return resultMatrix.GetRowVector(0).Convert<Vector2>();
141175
}
142176

177+
/// <summary>
178+
/// Implements the operator * to transform a <see cref="Vector2"/> with a <see cref="Matrix3x3"/>.
179+
/// </summary>
180+
/// <param name="vector">The <see cref="Vector2"/>.</param>
181+
/// <param name="matrix">The <see cref="Matrix3x3"/>.</param>
182+
/// <returns>
183+
/// The result of the operator.
184+
/// </returns>
143185
public static Vector2 operator *(Vector2 vector, Matrix3x3 matrix)
144186
{
145187
var resultMatrix = Multiply(matrix, new Vector3(vector.X, vector.Y, 1).AsVerticalMatrix());
146188
return resultMatrix.GetRowVector(0).Convert<Vector2>();
147189
}
148190

191+
/// <summary>
192+
/// Implements the operator * to transform a <see cref="Vector3"/> with a <see cref="Matrix3x3"/>.
193+
/// </summary>
194+
/// <param name="matrix">The <see cref="Matrix3x3"/>.</param>
195+
/// <param name="vector">The <see cref="Vector3"/>.</param>
196+
/// <returns>
197+
/// The result of the operator.
198+
/// </returns>
149199
public static Vector3 operator *(Matrix3x3 matrix, Vector3 vector)
150200
{
151201
var resultMatrix = Multiply(matrix, vector.AsVerticalMatrix());
152202
return Vector3.FromVector(resultMatrix.GetRowVector(0));
153203
}
154204

205+
/// <summary>
206+
/// Implements the operator * to transform a <see cref="Vector3"/> with a <see cref="Matrix3x3"/>.
207+
/// </summary>
208+
/// <param name="vector">The <see cref="Vector3"/>.</param>
209+
/// <param name="matrix">The <see cref="Matrix3x3"/>.</param>
210+
/// <returns>
211+
/// The result of the operator.
212+
/// </returns>
155213
public static Vector3 operator *(Vector3 vector, Matrix3x3 matrix)
156214
{
157215
var resultMatrix = Multiply(matrix, vector.AsVerticalMatrix());
158216
return Vector3.FromVector(resultMatrix.GetRowVector(0));
159217
}
160218

219+
/// <summary>
220+
/// Implements the operator *.
221+
/// </summary>
222+
/// <param name="firstMatrix">The first <see cref="Matrix3x3"/>.</param>
223+
/// <param name="secondMatrix">The second <see cref="Matrix3x3"/>.</param>
224+
/// <returns>
225+
/// The result of the operator.
226+
/// </returns>
161227
public static Matrix3x3 operator *(Matrix3x3 firstMatrix, Matrix3x3 secondMatrix)
162228
{
163229
return FromMatrix(Multiply(firstMatrix, secondMatrix));

SharpMath/Geometry/Matrix4x4.cs

Lines changed: 81 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -272,32 +272,79 @@ public static Matrix4x4 World(Vector3 position, Vector3 forward, Vector3 up)
272272
return result;
273273
}
274274

275+
/// <summary>
276+
/// Implements the operator +.
277+
/// </summary>
278+
/// <param name="firstMatrix">The first <see cref="Matrix4x4"/>.</param>
279+
/// <param name="secondMatrix">The second <see cref="Matrix4x4"/>.</param>
280+
/// <returns>
281+
/// The result of the operator.
282+
/// </returns>
275283
public static Matrix4x4 operator +(Matrix4x4 firstMatrix, Matrix4x4 secondMatrix)
276284
{
277-
for (uint y = 0; y < 4; ++y)
278-
for (uint x = 0; x < 4; ++x)
279-
firstMatrix[y, x] += secondMatrix[y, x];
280-
return firstMatrix;
285+
return FromMatrix(Add(firstMatrix, secondMatrix));
281286
}
282287

288+
/// <summary>
289+
/// Implements the operator -.
290+
/// </summary>
291+
/// <param name="firstMatrix">The first <see cref="Matrix4x4"/>.</param>
292+
/// <param name="secondMatrix">The second <see cref="Matrix4x4"/>.</param>
293+
/// <returns>
294+
/// The result of the operator.
295+
/// </returns>
283296
public static Matrix4x4 operator -(Matrix4x4 firstMatrix, Matrix4x4 secondMatrix)
284297
{
285-
for (uint y = 0; y < 4; ++y)
286-
for (uint x = 0; x < 4; ++x)
287-
firstMatrix[y, x] -= secondMatrix[y, x];
288-
return firstMatrix;
298+
return FromMatrix(Subtract(firstMatrix, secondMatrix));
289299
}
290300

291-
public static Matrix4x4 operator *(int scalar, Matrix4x4 matrix)
301+
/// <summary>
302+
/// Implements the operator * to multiply a <see cref="Matrix4x4"/> with the specified scalar.
303+
/// </summary>
304+
/// <param name="scalar">The scalar.</param>
305+
/// <param name="matrix">The <see cref="Matrix4x4"/>.</param>
306+
/// <returns>
307+
/// The result of the operator.
308+
/// </returns>
309+
public static Matrix4x4 operator *(double scalar, Matrix4x4 matrix)
292310
{
293311
return FromMatrix(Multiply(matrix, scalar));
294312
}
295313

314+
/// <summary>
315+
/// Implements the operator * to multiply a <see cref="Matrix4x4"/> with the specified scalar.
316+
/// </summary>
317+
/// <param name="matrix">The <see cref="Matrix4x4"/>.</param>
318+
/// <param name="scalar">The scalar.</param>
319+
/// <returns>
320+
/// The result of the operator.
321+
/// </returns>
322+
public static Matrix4x4 operator *(Matrix4x4 matrix, double scalar)
323+
{
324+
return FromMatrix(Multiply(matrix, scalar));
325+
}
326+
327+
/// <summary>
328+
/// Implements the operator *.
329+
/// </summary>
330+
/// <param name="firstMatrix">The first <see cref="Matrix4x4"/>.</param>
331+
/// <param name="secondMatrix">The second <see cref="Matrix4x4"/>.</param>
332+
/// <returns>
333+
/// The result of the operator.
334+
/// </returns>
296335
public static Matrix4x4 operator *(Matrix4x4 firstMatrix, Matrix4x4 secondMatrix)
297336
{
298337
return FromMatrix(Multiply(firstMatrix, secondMatrix));
299338
}
300-
339+
340+
/// <summary>
341+
/// Implements the operator * to transform a <see cref="Vector3"/> with a <see cref="Matrix4x4"/>.
342+
/// </summary>
343+
/// <param name="matrix">The <see cref="Matrix4x4"/>.</param>
344+
/// <param name="vector">The <see cref="Vector3"/>.</param>
345+
/// <returns>
346+
/// The result of the operator.
347+
/// </returns>
301348
public static Vector3 operator *(Matrix4x4 matrix, Vector3 vector)
302349
{
303350
var resultMatrix = Multiply(matrix, new Vector4(vector.X, vector.Y, vector.Z, 1).AsVerticalMatrix());
@@ -308,6 +355,14 @@ public static Matrix4x4 World(Vector3 position, Vector3 forward, Vector3 up)
308355
return resultVector.Convert<Vector3>();
309356
}
310357

358+
/// <summary>
359+
/// Implements the operator * to transform a <see cref="Vector3"/> with a <see cref="Matrix4x4"/>.
360+
/// </summary>
361+
/// <param name="vector">The <see cref="Vector3"/>.</param>
362+
/// <param name="matrix">The <see cref="Matrix4x4"/>.</param>
363+
/// <returns>
364+
/// The result of the operator.
365+
/// </returns>
311366
public static Vector3 operator *(Vector3 vector, Matrix4x4 matrix)
312367
{
313368
var resultMatrix = Multiply(matrix, new Vector4(vector.X, vector.Y, vector.Z, 1).AsVerticalMatrix());
@@ -318,12 +373,28 @@ public static Matrix4x4 World(Vector3 position, Vector3 forward, Vector3 up)
318373
return resultVector.Convert<Vector3>();
319374
}
320375

376+
/// <summary>
377+
/// Implements the operator * to transform a <see cref="Vector4"/> with a <see cref="Matrix4x4"/>.
378+
/// </summary>
379+
/// <param name="matrix">The <see cref="Matrix4x4"/>.</param>
380+
/// <param name="vector">The <see cref="Vector4"/>.</param>
381+
/// <returns>
382+
/// The result of the operator.
383+
/// </returns>
321384
public static Vector4 operator *(Matrix4x4 matrix, Vector4 vector)
322385
{
323386
var resultMatrix = Multiply(matrix, vector.AsVerticalMatrix());
324387
return Vector4.FromVector(resultMatrix.GetRowVector(0));
325388
}
326389

390+
/// <summary>
391+
/// Implements the operator * to transform a <see cref="Vector4"/> with a <see cref="Matrix4x4"/>.
392+
/// </summary>
393+
/// <param name="vector">The <see cref="Vector4"/>.</param>
394+
/// <param name="matrix">The <see cref="Matrix4x4"/>.</param>
395+
/// <returns>
396+
/// The result of the operator.
397+
/// </returns>
327398
public static Vector4 operator *(Vector4 vector, Matrix4x4 matrix)
328399
{
329400
var resultMatrix = Multiply(matrix, vector.AsVerticalMatrix());

0 commit comments

Comments
 (0)