forked from dotnet/Silk.NET
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMatrix2X2.cs
More file actions
423 lines (367 loc) · 18.1 KB
/
Matrix2X2.cs
File metadata and controls
423 lines (367 loc) · 18.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System;
using System.Globalization;
using System.Runtime.CompilerServices;
using System.Runtime.Serialization;
namespace Silk.NET.Maths
{
/// <summary>A structure encapsulating a 2x2 matrix.</summary>
[Serializable]
[DataContract]
public struct Matrix2X2<T> : IEquatable<Matrix2X2<T>>
where T : unmanaged, IFormattable, IEquatable<T>, IComparable<T>
{
private static readonly Matrix2X2<T> _identity = new(Scalar<T>.One, Scalar<T>.Zero, Scalar<T>.Zero,
Scalar<T>.One);
/// <summary>
/// Row 1 of the matrix.
/// </summary>
[IgnoreDataMember]
public Vector2D<T> Row1;
/// <summary>
/// Row 2 of the matrix.
/// </summary>
[IgnoreDataMember]
public Vector2D<T> Row2;
/// <summary>
/// Column 1 of the matrix.
/// </summary>
[IgnoreDataMember]
public Vector2D<T> Column1 => new(M11, M21);
/// <summary>
/// Column 2 of the matrix.
/// </summary>
[IgnoreDataMember]
public Vector2D<T> Column2 => new(M12, M22);
/// <summary>Value at row 1, column 1 of the matrix.</summary>
[DataMember]
public T M11
{
readonly get => Row1.X;
set => Row1.X = value;
}
/// <summary>Value at row 1, column 2 of the matrix.</summary>
[DataMember]
public T M12
{
readonly get => Row1.Y;
set => Row1.Y = value;
}
/// <summary>Value at row 2, column 1 of the matrix.</summary>
[DataMember]
public T M21
{
readonly get => Row2.X;
set => Row2.X = value;
}
/// <summary>Value at row 2, column 2 of the matrix.</summary>
[DataMember]
public T M22
{
readonly get => Row2.Y;
set => Row2.Y = value;
}
/// <summary>
/// Indexer for the rows of this matrix.
/// </summary>
/// <param name="x">The row to select. Zero based.</param>
public unsafe Vector2D<T> this[int x]
{
get
{
static void VerifyBounds(int i)
{
static void ThrowHelper() => throw new IndexOutOfRangeException();
if (i > 1 || i < 0)
ThrowHelper();
}
VerifyBounds(x);
return Unsafe.Add(ref Row1, x);
}
}
/// <summary>
/// Indexer for the values in this matrix.
/// </summary>
/// <param name="x">The row to select. Zero based.</param>
/// <param name="y">The column to select. Zero based.</param>
public unsafe T this[int x, int y]
{
get
{
var row = this[x];
return row[y];
}
}
/// <summary>Constructs a <see cref="Matrix2X2{T}"/> from the given components.</summary>
public Matrix2X2(T m11, T m12, T m21, T m22)
{
Row1 = new(m11, m12);
Row2 = new(m21, m22);
}
/// <summary>Constructs a <see cref="Matrix2X2{T}"/> from the given rows.</summary>
public Matrix2X2(Vector2D<T> row1, Vector2D<T> row2)
{
Row1 = row1;
Row2 = row2;
}
/// <summary>Constructs a <see cref="Matrix2X2{T}"/> from the given <see cref="Matrix3X2{T}"/>.</summary>
/// <param name="value">The source <see cref="Matrix3X2{T}"/>.</param>
public Matrix2X2(Matrix3X2<T> value)
{
Row1 = new(value.M11, value.M12);
Row2 = new(value.M21, value.M22);
}
/// <summary>Constructs a <see cref="Matrix2X2{T}"/> from the given <see cref="Matrix4X3{T}"/>.</summary>
/// <param name="value">The source <see cref="Matrix4X3{T}"/>.</param>
public Matrix2X2(Matrix4X3<T> value)
{
Row1 = new(value.M11, value.M12);
Row2 = new(value.M21, value.M22);
}
/// <summary>Constructs a <see cref="Matrix2X2{T}"/> from the given <see cref="Matrix3X4{T}"/>.</summary>
/// <param name="value">The source <see cref="Matrix3X4{T}"/>.</param>
public Matrix2X2(Matrix3X4<T> value)
{
Row1 = new(value.M11, value.M12);
Row2 = new(value.M21, value.M22);
}
/// <summary>Constructs a <see cref="Matrix2X2{T}"/> from the given <see cref="Matrix2X4{T}"/>.</summary>
/// <param name="value">The source <see cref="Matrix2X4{T}"/>.</param>
public Matrix2X2(Matrix2X4<T> value)
{
Row1 = new(value.M11, value.M12);
Row2 = new(value.M21, value.M22);
}
/// <summary>Constructs a <see cref="Matrix2X2{T}"/> from the given <see cref="Matrix4X2{T}"/>.</summary>
/// <param name="value">The source <see cref="Matrix4X2{T}"/>.</param>
public Matrix2X2(Matrix4X2<T> value)
{
Row1 = new(value.M11, value.M12);
Row2 = new(value.M21, value.M22);
}
/// <summary>Returns the multiplicative identity matrix.</summary>
public static Matrix2X2<T> Identity => _identity;
/// <summary>Returns whether the matrix is the identity matrix.</summary>
[IgnoreDataMember]
public readonly bool IsIdentity
=> Scalar.Equal(M11, Scalar<T>.One) &&
Scalar.Equal(M22, Scalar<T>.One) && // Check diagonal element first for early out.
Scalar.Equal(M12, Scalar<T>.Zero) && Scalar.Equal(M21, Scalar<T>.Zero);
/// <summary>Adds two matrices together.</summary>
/// <param name="value1">The first source matrix.</param>
/// <param name="value2">The second source matrix.</param>
/// <returns>The resulting matrix.</returns>
public static unsafe Matrix2X2<T> operator +(Matrix2X2<T> value1, Matrix2X2<T> value2)
{
Matrix2X2<T> m;
m.Row1 = value1.Row1 + value2.Row1;
m.Row2 = value1.Row2 + value2.Row2;
return m;
}
/// <summary>Returns a boolean indicating whether the given two matrices are equal.</summary>
/// <param name="value1">The first matrix to compare.</param>
/// <param name="value2">The second matrix to compare.</param>
/// <returns>True if the given matrices are equal; False otherwise.</returns>
public static unsafe bool operator ==(Matrix2X2<T> value1, Matrix2X2<T> value2)
{
return Scalar.Equal(value1.M11, value2.M11) && Scalar.Equal(value1.M22, value2.M22) &&
// Check diagonal elements first for early out.
Scalar.Equal(value1.M12, value2.M12) && Scalar.Equal(value1.M21, value2.M21);
}
/// <summary>Returns a boolean indicating whether the given two matrices are not equal.</summary>
/// <param name="value1">The first matrix to compare.</param>
/// <param name="value2">The second matrix to compare.</param>
/// <returns>True if the given matrices are not equal; False if they are equal.</returns>
public static unsafe bool operator !=(Matrix2X2<T> value1, Matrix2X2<T> value2)
{
return Scalar.NotEqual(value1.M11, value2.M11) ||
Scalar.NotEqual(value1.M22, value2.M22) || // Check diagonal elements first for early out.
Scalar.NotEqual(value1.M12, value2.M12) || Scalar.NotEqual(value1.M21, value2.M21);
}
/// <summary>Multiplies a matrix by another matrix.</summary>
/// <param name="value1">The first source matrix.</param>
/// <param name="value2">The second source matrix.</param>
/// <returns>The result of the multiplication.</returns>
public static unsafe Matrix2X2<T> operator *(Matrix2X2<T> value1, Matrix2X2<T> value2)
{
return new
(
value1.M11 * value2.Row1 + value1.M12 * value2.Row2, value1.M21 * value2.Row1 + value1.M22 * value2.Row2
);
}
/// <summary>Multiplies a vector by a matrix.</summary>
/// <param name="value1">The vector.</param>
/// <param name="value2">The matrix.</param>
/// <returns>The result of the multiplication.</returns>
public static unsafe Vector2D<T> operator *(Vector2D<T> value1, Matrix2X2<T> value2)
{
return value1.X * value2.Row1 + value1.Y * value2.Row2;
}
/// <summary>Multiplies a matrix by a scalar value.</summary>
/// <param name="value1">The source matrix.</param>
/// <param name="value2">The scaling factor.</param>
/// <returns>The scaled matrix.</returns>
public static unsafe Matrix2X2<T> operator *(Matrix2X2<T> value1, T value2)
{
return new(value1.Row1 * value2, value1.Row2 * value2);
}
/// <summary>Subtracts the second matrix from the first.</summary>
/// <param name="value1">The first source matrix.</param>
/// <param name="value2">The second source matrix.</param>
/// <returns>The result of the subtraction.</returns>
public static unsafe Matrix2X2<T> operator -(Matrix2X2<T> value1, Matrix2X2<T> value2)
{
return new(value1.Row1 - value2.Row1, value1.Row2 - value2.Row2);
}
/// <summary>Returns a new matrix with the negated elements of the given matrix.</summary>
/// <param name="value">The source matrix.</param>
/// <returns>The negated matrix.</returns>
public static unsafe Matrix2X2<T> operator -(Matrix2X2<T> value)
{
return new(-value.Row1, -value.Row2);
}
/// <summary>Calculates the determinant of the matrix.</summary>
/// <returns>The determinant of the matrix.</returns>
public readonly T GetDeterminant()
{
// | a b |
// | c d | = ad - bc
T a = M11, b = M12;
T c = M21, d = M22;
return Scalar.Subtract(Scalar.Multiply(a, d), Scalar.Multiply(b, c));
}
/// <summary>Returns a boolean indicating whether the given Object is equal to this matrix instance.</summary>
/// <param name="obj">The Object to compare against.</param>
/// <returns>True if the Object is equal to this matrix; False otherwise.</returns>
[MethodImpl((MethodImplOptions) 768)]
public override readonly bool Equals(object? obj) => (obj is Matrix2X2<T> other) && Equals(other);
/// <summary>Returns a boolean indicating whether this matrix instance is equal to the other given matrix.</summary>
/// <param name="other">The matrix to compare this instance to.</param>
/// <returns>True if the matrices are equal; False otherwise.</returns>
public readonly bool Equals(Matrix2X2<T> other) => this == other;
/// <summary>Returns the hash code for this instance.</summary>
/// <returns>The hash code.</returns>
public override readonly int GetHashCode()
{
HashCode hash = default;
hash.Add(M11);
hash.Add(M12);
hash.Add(M21);
hash.Add(M22);
return hash.ToHashCode();
}
/// <summary>Returns a String representing this matrix instance.</summary>
/// <returns>The string representation.</returns>
public override readonly string ToString()
{
return string.Format(CultureInfo.CurrentCulture, "{{ {{M11:{0} M12:{1}}} {{M21:{2} M22:{3}}} }}", M11, M12,
M21, M22);
}
/// <summary>
/// Converts a <see cref="Matrix2X2{T}"/> into one with a <typeparamref name="T"/> of <see cref="Half"/>
/// </summary>
/// <param name="from">The source matrix</param>
/// <returns>The <see cref="Half"/> matrix</returns>
public static explicit operator Matrix2X2<Half>(Matrix2X2<T> from)
=> new(Scalar.As<T, Half>(from.M11), Scalar.As<T, Half>(from.M12), Scalar.As<T, Half>(from.M21),
Scalar.As<T, Half>(from.M22));
/// <summary>
/// Converts a <see cref="Matrix2X2{T}"/> into one with a <typeparamref name="T"/> of <see cref="float"/>
/// </summary>
/// <param name="from">The source matrix</param>
/// <returns>The <see cref="float"/> matrix</returns>
public static explicit operator Matrix2X2<float>(Matrix2X2<T> from)
=> new(Scalar.As<T, float>(from.M11), Scalar.As<T, float>(from.M12),
Scalar.As<T, float>(from.M21), Scalar.As<T, float>(from.M22));
/// <summary>
/// Converts a <see cref="Matrix2X2{T}"/> into one with a <typeparamref name="T"/> of <see cref="double"/>
/// </summary>
/// <param name="from">The source matrix</param>
/// <returns>The <see cref="double"/> matrix</returns>
public static explicit operator Matrix2X2<double>(Matrix2X2<T> from)
=> new(Scalar.As<T, double>(from.M11), Scalar.As<T, double>(from.M12),
Scalar.As<T, double>(from.M21), Scalar.As<T, double>(from.M22));
/// <summary>
/// Converts a <see cref="Matrix2X2{T}"/> into one with a <typeparamref name="T"/> of <see cref="decimal"/>
/// </summary>
/// <param name="from">The source matrix</param>
/// <returns>The <see cref="decimal"/> matrix</returns>
public static explicit operator Matrix2X2<decimal>(Matrix2X2<T> from)
=> new(Scalar.As<T, decimal>(from.M11), Scalar.As<T, decimal>(from.M12),
Scalar.As<T, decimal>(from.M21), Scalar.As<T, decimal>(from.M22));
/// <summary>
/// Converts a <see cref="Matrix2X2{T}"/> into one with a <typeparamref name="T"/> of <see cref="sbyte"/>
/// </summary>
/// <param name="from">The source matrix</param>
/// <returns>The <see cref="sbyte"/> matrix</returns>
public static explicit operator Matrix2X2<sbyte>(Matrix2X2<T> from)
=> new(Scalar.As<T, sbyte>(from.M11), Scalar.As<T, sbyte>(from.M12),
Scalar.As<T, sbyte>(from.M21), Scalar.As<T, sbyte>(from.M22));
/// <summary>
/// Converts a <see cref="Matrix2X2{T}"/> into one with a <typeparamref name="T"/> of <see cref="byte"/>
/// </summary>
/// <param name="from">The source matrix</param>
/// <returns>The <see cref="byte"/> matrix</returns>
public static explicit operator Matrix2X2<byte>(Matrix2X2<T> from)
=> new(Scalar.As<T, byte>(from.M11), Scalar.As<T, byte>(from.M12), Scalar.As<T, byte>(from.M21),
Scalar.As<T, byte>(from.M22));
/// <summary>
/// Converts a <see cref="Matrix2X2{T}"/> into one with a <typeparamref name="T"/> of <see cref="ushort"/>
/// </summary>
/// <param name="from">The source matrix</param>
/// <returns>The <see cref="ushort"/> matrix</returns>
public static explicit operator Matrix2X2<ushort>(Matrix2X2<T> from)
=> new(Scalar.As<T, ushort>(from.M11), Scalar.As<T, ushort>(from.M12),
Scalar.As<T, ushort>(from.M21), Scalar.As<T, ushort>(from.M22));
/// <summary>
/// Converts a <see cref="Matrix2X2{T}"/> into one with a <typeparamref name="T"/> of <see cref="short"/>
/// </summary>
/// <param name="from">The source matrix</param>
/// <returns>The <see cref="short"/> matrix</returns>
public static explicit operator Matrix2X2<short>(Matrix2X2<T> from)
=> new(Scalar.As<T, short>(from.M11), Scalar.As<T, short>(from.M12),
Scalar.As<T, short>(from.M21), Scalar.As<T, short>(from.M22));
/// <summary>
/// Converts a <see cref="Matrix2X2{T}"/> into one with a <typeparamref name="T"/> of <see cref="uint"/>
/// </summary>
/// <param name="from">The source matrix</param>
/// <returns>The <see cref="uint"/> matrix</returns>
public static explicit operator Matrix2X2<uint>(Matrix2X2<T> from)
=> new(Scalar.As<T, uint>(from.M11), Scalar.As<T, uint>(from.M12), Scalar.As<T, uint>(from.M21),
Scalar.As<T, uint>(from.M22));
/// <summary>
/// Converts a <see cref="Matrix2X2{T}"/> into one with a <typeparamref name="T"/> of <see cref="int"/>
/// </summary>
/// <param name="from">The source matrix</param>
/// <returns>The <see cref="int"/> matrix</returns>
public static explicit operator Matrix2X2<int>(Matrix2X2<T> from)
=> new(Scalar.As<T, int>(from.M11), Scalar.As<T, int>(from.M12), Scalar.As<T, int>(from.M21),
Scalar.As<T, int>(from.M22));
/// <summary>
/// Converts a <see cref="Matrix2X2{T}"/> into one with a <typeparamref name="T"/> of <see cref="ulong"/>
/// </summary>
/// <param name="from">The source matrix</param>
/// <returns>The <see cref="ulong"/> matrix</returns>
public static explicit operator Matrix2X2<ulong>(Matrix2X2<T> from)
=> new(Scalar.As<T, ulong>(from.M11), Scalar.As<T, ulong>(from.M12),
Scalar.As<T, ulong>(from.M21), Scalar.As<T, ulong>(from.M22));
/// <summary>
/// Converts a <see cref="Matrix2X2{T}"/> into one with a <typeparamref name="T"/> of <see cref="long"/>
/// </summary>
/// <param name="from">The source matrix</param>
/// <returns>The <see cref="long"/> matrix</returns>
public static explicit operator Matrix2X2<long>(Matrix2X2<T> from)
=> new(Scalar.As<T, long>(from.M11), Scalar.As<T, long>(from.M12), Scalar.As<T, long>(from.M21),
Scalar.As<T, long>(from.M22));
/// <summary>
/// Returns this matrix casted to <typeparamref name="TOther"></typeparamref>
/// </summary>
/// <typeparam name="TOther">The type to cast to</typeparam>
/// <returns>The casted matrix</returns>
public Matrix2X2<TOther> As<TOther>() where TOther : unmanaged, IFormattable, IEquatable<TOther>, IComparable<TOther>
{
return new(Row1.As<TOther>(), Row2.As<TOther>());
}
}
}