forked from OpenRA/OpenRA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bmat3x2.cs
490 lines (427 loc) · 14.1 KB
/
bmat3x2.cs
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
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.Runtime.InteropServices;
using System.Runtime.Serialization;
using System.Numerics;
using System.Linq;
using GlmSharp.Swizzle;
// ReSharper disable InconsistentNaming
namespace GlmSharp
{
/// <summary>
/// A matrix of type bool with 3 columns and 2 rows.
/// </summary>
[Serializable]
[DataContract(Namespace = "mat")]
[StructLayout(LayoutKind.Sequential)]
public struct bmat3x2 : IReadOnlyList<bool>, IEquatable<bmat3x2>
{
#region Fields
/// <summary>
/// Column 0, Rows 0
/// </summary>
[DataMember]
public bool m00;
/// <summary>
/// Column 0, Rows 1
/// </summary>
[DataMember]
public bool m01;
/// <summary>
/// Column 1, Rows 0
/// </summary>
[DataMember]
public bool m10;
/// <summary>
/// Column 1, Rows 1
/// </summary>
[DataMember]
public bool m11;
/// <summary>
/// Column 2, Rows 0
/// </summary>
[DataMember]
public bool m20;
/// <summary>
/// Column 2, Rows 1
/// </summary>
[DataMember]
public bool m21;
#endregion
#region Constructors
/// <summary>
/// Component-wise constructor
/// </summary>
public bmat3x2(bool m00, bool m01, bool m10, bool m11, bool m20, bool m21)
{
this.m00 = m00;
this.m01 = m01;
this.m10 = m10;
this.m11 = m11;
this.m20 = m20;
this.m21 = m21;
}
/// <summary>
/// Constructs this matrix from a bmat2. Non-overwritten fields are from an Identity matrix.
/// </summary>
public bmat3x2(bmat2 m)
{
this.m00 = m.m00;
this.m01 = m.m01;
this.m10 = m.m10;
this.m11 = m.m11;
this.m20 = false;
this.m21 = false;
}
/// <summary>
/// Constructs this matrix from a bmat3x2. Non-overwritten fields are from an Identity matrix.
/// </summary>
public bmat3x2(bmat3x2 m)
{
this.m00 = m.m00;
this.m01 = m.m01;
this.m10 = m.m10;
this.m11 = m.m11;
this.m20 = m.m20;
this.m21 = m.m21;
}
/// <summary>
/// Constructs this matrix from a bmat4x2. Non-overwritten fields are from an Identity matrix.
/// </summary>
public bmat3x2(bmat4x2 m)
{
this.m00 = m.m00;
this.m01 = m.m01;
this.m10 = m.m10;
this.m11 = m.m11;
this.m20 = m.m20;
this.m21 = m.m21;
}
/// <summary>
/// Constructs this matrix from a bmat2x3. Non-overwritten fields are from an Identity matrix.
/// </summary>
public bmat3x2(bmat2x3 m)
{
this.m00 = m.m00;
this.m01 = m.m01;
this.m10 = m.m10;
this.m11 = m.m11;
this.m20 = false;
this.m21 = false;
}
/// <summary>
/// Constructs this matrix from a bmat3. Non-overwritten fields are from an Identity matrix.
/// </summary>
public bmat3x2(bmat3 m)
{
this.m00 = m.m00;
this.m01 = m.m01;
this.m10 = m.m10;
this.m11 = m.m11;
this.m20 = m.m20;
this.m21 = m.m21;
}
/// <summary>
/// Constructs this matrix from a bmat4x3. Non-overwritten fields are from an Identity matrix.
/// </summary>
public bmat3x2(bmat4x3 m)
{
this.m00 = m.m00;
this.m01 = m.m01;
this.m10 = m.m10;
this.m11 = m.m11;
this.m20 = m.m20;
this.m21 = m.m21;
}
/// <summary>
/// Constructs this matrix from a bmat2x4. Non-overwritten fields are from an Identity matrix.
/// </summary>
public bmat3x2(bmat2x4 m)
{
this.m00 = m.m00;
this.m01 = m.m01;
this.m10 = m.m10;
this.m11 = m.m11;
this.m20 = false;
this.m21 = false;
}
/// <summary>
/// Constructs this matrix from a bmat3x4. Non-overwritten fields are from an Identity matrix.
/// </summary>
public bmat3x2(bmat3x4 m)
{
this.m00 = m.m00;
this.m01 = m.m01;
this.m10 = m.m10;
this.m11 = m.m11;
this.m20 = m.m20;
this.m21 = m.m21;
}
/// <summary>
/// Constructs this matrix from a bmat4. Non-overwritten fields are from an Identity matrix.
/// </summary>
public bmat3x2(bmat4 m)
{
this.m00 = m.m00;
this.m01 = m.m01;
this.m10 = m.m10;
this.m11 = m.m11;
this.m20 = m.m20;
this.m21 = m.m21;
}
/// <summary>
/// Constructs this matrix from a series of column vectors. Non-overwritten fields are from an Identity matrix.
/// </summary>
public bmat3x2(bvec2 c0, bvec2 c1)
{
this.m00 = c0.x;
this.m01 = c0.y;
this.m10 = c1.x;
this.m11 = c1.y;
this.m20 = false;
this.m21 = false;
}
/// <summary>
/// Constructs this matrix from a series of column vectors. Non-overwritten fields are from an Identity matrix.
/// </summary>
public bmat3x2(bvec2 c0, bvec2 c1, bvec2 c2)
{
this.m00 = c0.x;
this.m01 = c0.y;
this.m10 = c1.x;
this.m11 = c1.y;
this.m20 = c2.x;
this.m21 = c2.y;
}
#endregion
#region Properties
/// <summary>
/// Creates a 2D array with all values (address: Values[x, y])
/// </summary>
public bool[,] Values => new[,] { { m00, m01 }, { m10, m11 }, { m20, m21 } };
/// <summary>
/// Creates a 1D array with all values (internal order)
/// </summary>
public bool[] Values1D => new[] { m00, m01, m10, m11, m20, m21 };
/// <summary>
/// Gets or sets the column nr 0
/// </summary>
public bvec2 Column0
{
get
{
return new bvec2(m00, m01);
}
set
{
m00 = value.x;
m01 = value.y;
}
}
/// <summary>
/// Gets or sets the column nr 1
/// </summary>
public bvec2 Column1
{
get
{
return new bvec2(m10, m11);
}
set
{
m10 = value.x;
m11 = value.y;
}
}
/// <summary>
/// Gets or sets the column nr 2
/// </summary>
public bvec2 Column2
{
get
{
return new bvec2(m20, m21);
}
set
{
m20 = value.x;
m21 = value.y;
}
}
/// <summary>
/// Gets or sets the row nr 0
/// </summary>
public bvec3 Row0
{
get
{
return new bvec3(m00, m10, m20);
}
set
{
m00 = value.x;
m10 = value.y;
m20 = value.z;
}
}
/// <summary>
/// Gets or sets the row nr 1
/// </summary>
public bvec3 Row1
{
get
{
return new bvec3(m01, m11, m21);
}
set
{
m01 = value.x;
m11 = value.y;
m21 = value.z;
}
}
#endregion
#region Static Properties
/// <summary>
/// Predefined all-zero matrix
/// </summary>
public static bmat3x2 Zero { get; } = new bmat3x2(false, false, false, false, false, false);
/// <summary>
/// Predefined all-ones matrix
/// </summary>
public static bmat3x2 Ones { get; } = new bmat3x2(true, true, true, true, true, true);
/// <summary>
/// Predefined identity matrix
/// </summary>
public static bmat3x2 Identity { get; } = new bmat3x2(true, false, false, true, false, false);
#endregion
#region Functions
/// <summary>
/// Returns an enumerator that iterates through all fields.
/// </summary>
public IEnumerator<bool> GetEnumerator()
{
yield return m00;
yield return m01;
yield return m10;
yield return m11;
yield return m20;
yield return m21;
}
/// <summary>
/// Returns an enumerator that iterates through all fields.
/// </summary>
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
#endregion
/// <summary>
/// Returns the number of Fields (3 x 2 = 6).
/// </summary>
public int Count => 6;
/// <summary>
/// Gets/Sets a specific indexed component (a bit slower than direct access).
/// </summary>
public bool this[int fieldIndex]
{
get
{
switch (fieldIndex)
{
case 0: return m00;
case 1: return m01;
case 2: return m10;
case 3: return m11;
case 4: return m20;
case 5: return m21;
default: throw new ArgumentOutOfRangeException("fieldIndex");
}
}
set
{
switch (fieldIndex)
{
case 0: this.m00 = value; break;
case 1: this.m01 = value; break;
case 2: this.m10 = value; break;
case 3: this.m11 = value; break;
case 4: this.m20 = value; break;
case 5: this.m21 = value; break;
default: throw new ArgumentOutOfRangeException("fieldIndex");
}
}
}
/// <summary>
/// Gets/Sets a specific 2D-indexed component (a bit slower than direct access).
/// </summary>
public bool this[int col, int row]
{
get
{
return this[col * 2 + row];
}
set
{
this[col * 2 + row] = value;
}
}
/// <summary>
/// Returns true iff this equals rhs component-wise.
/// </summary>
public bool Equals(bmat3x2 rhs) => (((m00.Equals(rhs.m00) && m01.Equals(rhs.m01)) && m10.Equals(rhs.m10)) && ((m11.Equals(rhs.m11) && m20.Equals(rhs.m20)) && m21.Equals(rhs.m21)));
/// <summary>
/// Returns true iff this equals rhs type- and component-wise.
/// </summary>
public override bool Equals(object obj)
{
if (ReferenceEquals(null, obj)) return false;
return obj is bmat3x2 && Equals((bmat3x2) obj);
}
/// <summary>
/// Returns true iff this equals rhs component-wise.
/// </summary>
public static bool operator ==(bmat3x2 lhs, bmat3x2 rhs) => lhs.Equals(rhs);
/// <summary>
/// Returns true iff this does not equal rhs (component-wise).
/// </summary>
public static bool operator !=(bmat3x2 lhs, bmat3x2 rhs) => !lhs.Equals(rhs);
/// <summary>
/// Returns a hash code for this instance.
/// </summary>
public override int GetHashCode()
{
unchecked
{
return ((((((((((m00.GetHashCode()) * 2) ^ m01.GetHashCode()) * 2) ^ m10.GetHashCode()) * 2) ^ m11.GetHashCode()) * 2) ^ m20.GetHashCode()) * 2) ^ m21.GetHashCode();
}
}
/// <summary>
/// Returns a transposed version of this matrix.
/// </summary>
public bmat2x3 Transposed => new bmat2x3(m00, m10, m20, m01, m11, m21);
/// <summary>
/// Returns the minimal component of this matrix.
/// </summary>
public bool MinElement => (((m00 && m01) && m10) && ((m11 && m20) && m21));
/// <summary>
/// Returns the maximal component of this matrix.
/// </summary>
public bool MaxElement => (((m00 || m01) || m10) || ((m11 || m20) || m21));
/// <summary>
/// Returns true if all component are true.
/// </summary>
public bool All => (((m00 && m01) && m10) && ((m11 && m20) && m21));
/// <summary>
/// Returns true if any component is true.
/// </summary>
public bool Any => (((m00 || m01) || m10) || ((m11 || m20) || m21));
/// <summary>
/// Executes a component-wise &&. (sorry for different overload but && cannot be overloaded directly)
/// </summary>
public static bmat3x2 operator&(bmat3x2 lhs, bmat3x2 rhs) => new bmat3x2(lhs.m00 && rhs.m00, lhs.m01 && rhs.m01, lhs.m10 && rhs.m10, lhs.m11 && rhs.m11, lhs.m20 && rhs.m20, lhs.m21 && rhs.m21);
/// <summary>
/// Executes a component-wise ||. (sorry for different overload but || cannot be overloaded directly)
/// </summary>
public static bmat3x2 operator|(bmat3x2 lhs, bmat3x2 rhs) => new bmat3x2(lhs.m00 || rhs.m00, lhs.m01 || rhs.m01, lhs.m10 || rhs.m10, lhs.m11 || rhs.m11, lhs.m20 || rhs.m20, lhs.m21 || rhs.m21);
}
}