forked from OpenRA/OpenRA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bmat3x4.cs
732 lines (657 loc) · 21.9 KB
/
bmat3x4.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
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
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 4 rows.
/// </summary>
[Serializable]
[DataContract(Namespace = "mat")]
[StructLayout(LayoutKind.Sequential)]
public struct bmat3x4 : IReadOnlyList<bool>, IEquatable<bmat3x4>
{
#region Fields
/// <summary>
/// Column 0, Rows 0
/// </summary>
[DataMember]
public bool m00;
/// <summary>
/// Column 0, Rows 1
/// </summary>
[DataMember]
public bool m01;
/// <summary>
/// Column 0, Rows 2
/// </summary>
[DataMember]
public bool m02;
/// <summary>
/// Column 0, Rows 3
/// </summary>
[DataMember]
public bool m03;
/// <summary>
/// Column 1, Rows 0
/// </summary>
[DataMember]
public bool m10;
/// <summary>
/// Column 1, Rows 1
/// </summary>
[DataMember]
public bool m11;
/// <summary>
/// Column 1, Rows 2
/// </summary>
[DataMember]
public bool m12;
/// <summary>
/// Column 1, Rows 3
/// </summary>
[DataMember]
public bool m13;
/// <summary>
/// Column 2, Rows 0
/// </summary>
[DataMember]
public bool m20;
/// <summary>
/// Column 2, Rows 1
/// </summary>
[DataMember]
public bool m21;
/// <summary>
/// Column 2, Rows 2
/// </summary>
[DataMember]
public bool m22;
/// <summary>
/// Column 2, Rows 3
/// </summary>
[DataMember]
public bool m23;
#endregion
#region Constructors
/// <summary>
/// Component-wise constructor
/// </summary>
public bmat3x4(bool m00, bool m01, bool m02, bool m03, bool m10, bool m11, bool m12, bool m13, bool m20, bool m21, bool m22, bool m23)
{
this.m00 = m00;
this.m01 = m01;
this.m02 = m02;
this.m03 = m03;
this.m10 = m10;
this.m11 = m11;
this.m12 = m12;
this.m13 = m13;
this.m20 = m20;
this.m21 = m21;
this.m22 = m22;
this.m23 = m23;
}
/// <summary>
/// Constructs this matrix from a bmat2. Non-overwritten fields are from an Identity matrix.
/// </summary>
public bmat3x4(bmat2 m)
{
this.m00 = m.m00;
this.m01 = m.m01;
this.m02 = false;
this.m03 = false;
this.m10 = m.m10;
this.m11 = m.m11;
this.m12 = false;
this.m13 = false;
this.m20 = false;
this.m21 = false;
this.m22 = true;
this.m23 = false;
}
/// <summary>
/// Constructs this matrix from a bmat3x2. Non-overwritten fields are from an Identity matrix.
/// </summary>
public bmat3x4(bmat3x2 m)
{
this.m00 = m.m00;
this.m01 = m.m01;
this.m02 = false;
this.m03 = false;
this.m10 = m.m10;
this.m11 = m.m11;
this.m12 = false;
this.m13 = false;
this.m20 = m.m20;
this.m21 = m.m21;
this.m22 = true;
this.m23 = false;
}
/// <summary>
/// Constructs this matrix from a bmat4x2. Non-overwritten fields are from an Identity matrix.
/// </summary>
public bmat3x4(bmat4x2 m)
{
this.m00 = m.m00;
this.m01 = m.m01;
this.m02 = false;
this.m03 = false;
this.m10 = m.m10;
this.m11 = m.m11;
this.m12 = false;
this.m13 = false;
this.m20 = m.m20;
this.m21 = m.m21;
this.m22 = true;
this.m23 = false;
}
/// <summary>
/// Constructs this matrix from a bmat2x3. Non-overwritten fields are from an Identity matrix.
/// </summary>
public bmat3x4(bmat2x3 m)
{
this.m00 = m.m00;
this.m01 = m.m01;
this.m02 = m.m02;
this.m03 = false;
this.m10 = m.m10;
this.m11 = m.m11;
this.m12 = m.m12;
this.m13 = false;
this.m20 = false;
this.m21 = false;
this.m22 = true;
this.m23 = false;
}
/// <summary>
/// Constructs this matrix from a bmat3. Non-overwritten fields are from an Identity matrix.
/// </summary>
public bmat3x4(bmat3 m)
{
this.m00 = m.m00;
this.m01 = m.m01;
this.m02 = m.m02;
this.m03 = false;
this.m10 = m.m10;
this.m11 = m.m11;
this.m12 = m.m12;
this.m13 = false;
this.m20 = m.m20;
this.m21 = m.m21;
this.m22 = m.m22;
this.m23 = false;
}
/// <summary>
/// Constructs this matrix from a bmat4x3. Non-overwritten fields are from an Identity matrix.
/// </summary>
public bmat3x4(bmat4x3 m)
{
this.m00 = m.m00;
this.m01 = m.m01;
this.m02 = m.m02;
this.m03 = false;
this.m10 = m.m10;
this.m11 = m.m11;
this.m12 = m.m12;
this.m13 = false;
this.m20 = m.m20;
this.m21 = m.m21;
this.m22 = m.m22;
this.m23 = false;
}
/// <summary>
/// Constructs this matrix from a bmat2x4. Non-overwritten fields are from an Identity matrix.
/// </summary>
public bmat3x4(bmat2x4 m)
{
this.m00 = m.m00;
this.m01 = m.m01;
this.m02 = m.m02;
this.m03 = m.m03;
this.m10 = m.m10;
this.m11 = m.m11;
this.m12 = m.m12;
this.m13 = m.m13;
this.m20 = false;
this.m21 = false;
this.m22 = true;
this.m23 = false;
}
/// <summary>
/// Constructs this matrix from a bmat3x4. Non-overwritten fields are from an Identity matrix.
/// </summary>
public bmat3x4(bmat3x4 m)
{
this.m00 = m.m00;
this.m01 = m.m01;
this.m02 = m.m02;
this.m03 = m.m03;
this.m10 = m.m10;
this.m11 = m.m11;
this.m12 = m.m12;
this.m13 = m.m13;
this.m20 = m.m20;
this.m21 = m.m21;
this.m22 = m.m22;
this.m23 = m.m23;
}
/// <summary>
/// Constructs this matrix from a bmat4. Non-overwritten fields are from an Identity matrix.
/// </summary>
public bmat3x4(bmat4 m)
{
this.m00 = m.m00;
this.m01 = m.m01;
this.m02 = m.m02;
this.m03 = m.m03;
this.m10 = m.m10;
this.m11 = m.m11;
this.m12 = m.m12;
this.m13 = m.m13;
this.m20 = m.m20;
this.m21 = m.m21;
this.m22 = m.m22;
this.m23 = m.m23;
}
/// <summary>
/// Constructs this matrix from a series of column vectors. Non-overwritten fields are from an Identity matrix.
/// </summary>
public bmat3x4(bvec2 c0, bvec2 c1)
{
this.m00 = c0.x;
this.m01 = c0.y;
this.m02 = false;
this.m03 = false;
this.m10 = c1.x;
this.m11 = c1.y;
this.m12 = false;
this.m13 = false;
this.m20 = false;
this.m21 = false;
this.m22 = true;
this.m23 = false;
}
/// <summary>
/// Constructs this matrix from a series of column vectors. Non-overwritten fields are from an Identity matrix.
/// </summary>
public bmat3x4(bvec2 c0, bvec2 c1, bvec2 c2)
{
this.m00 = c0.x;
this.m01 = c0.y;
this.m02 = false;
this.m03 = false;
this.m10 = c1.x;
this.m11 = c1.y;
this.m12 = false;
this.m13 = false;
this.m20 = c2.x;
this.m21 = c2.y;
this.m22 = true;
this.m23 = false;
}
/// <summary>
/// Constructs this matrix from a series of column vectors. Non-overwritten fields are from an Identity matrix.
/// </summary>
public bmat3x4(bvec3 c0, bvec3 c1)
{
this.m00 = c0.x;
this.m01 = c0.y;
this.m02 = c0.z;
this.m03 = false;
this.m10 = c1.x;
this.m11 = c1.y;
this.m12 = c1.z;
this.m13 = false;
this.m20 = false;
this.m21 = false;
this.m22 = true;
this.m23 = false;
}
/// <summary>
/// Constructs this matrix from a series of column vectors. Non-overwritten fields are from an Identity matrix.
/// </summary>
public bmat3x4(bvec3 c0, bvec3 c1, bvec3 c2)
{
this.m00 = c0.x;
this.m01 = c0.y;
this.m02 = c0.z;
this.m03 = false;
this.m10 = c1.x;
this.m11 = c1.y;
this.m12 = c1.z;
this.m13 = false;
this.m20 = c2.x;
this.m21 = c2.y;
this.m22 = c2.z;
this.m23 = false;
}
/// <summary>
/// Constructs this matrix from a series of column vectors. Non-overwritten fields are from an Identity matrix.
/// </summary>
public bmat3x4(bvec4 c0, bvec4 c1)
{
this.m00 = c0.x;
this.m01 = c0.y;
this.m02 = c0.z;
this.m03 = c0.w;
this.m10 = c1.x;
this.m11 = c1.y;
this.m12 = c1.z;
this.m13 = c1.w;
this.m20 = false;
this.m21 = false;
this.m22 = true;
this.m23 = false;
}
/// <summary>
/// Constructs this matrix from a series of column vectors. Non-overwritten fields are from an Identity matrix.
/// </summary>
public bmat3x4(bvec4 c0, bvec4 c1, bvec4 c2)
{
this.m00 = c0.x;
this.m01 = c0.y;
this.m02 = c0.z;
this.m03 = c0.w;
this.m10 = c1.x;
this.m11 = c1.y;
this.m12 = c1.z;
this.m13 = c1.w;
this.m20 = c2.x;
this.m21 = c2.y;
this.m22 = c2.z;
this.m23 = c2.w;
}
#endregion
#region Properties
/// <summary>
/// Creates a 2D array with all values (address: Values[x, y])
/// </summary>
public bool[,] Values => new[,] { { m00, m01, m02, m03 }, { m10, m11, m12, m13 }, { m20, m21, m22, m23 } };
/// <summary>
/// Creates a 1D array with all values (internal order)
/// </summary>
public bool[] Values1D => new[] { m00, m01, m02, m03, m10, m11, m12, m13, m20, m21, m22, m23 };
/// <summary>
/// Gets or sets the column nr 0
/// </summary>
public bvec4 Column0
{
get
{
return new bvec4(m00, m01, m02, m03);
}
set
{
m00 = value.x;
m01 = value.y;
m02 = value.z;
m03 = value.w;
}
}
/// <summary>
/// Gets or sets the column nr 1
/// </summary>
public bvec4 Column1
{
get
{
return new bvec4(m10, m11, m12, m13);
}
set
{
m10 = value.x;
m11 = value.y;
m12 = value.z;
m13 = value.w;
}
}
/// <summary>
/// Gets or sets the column nr 2
/// </summary>
public bvec4 Column2
{
get
{
return new bvec4(m20, m21, m22, m23);
}
set
{
m20 = value.x;
m21 = value.y;
m22 = value.z;
m23 = value.w;
}
}
/// <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;
}
}
/// <summary>
/// Gets or sets the row nr 2
/// </summary>
public bvec3 Row2
{
get
{
return new bvec3(m02, m12, m22);
}
set
{
m02 = value.x;
m12 = value.y;
m22 = value.z;
}
}
/// <summary>
/// Gets or sets the row nr 3
/// </summary>
public bvec3 Row3
{
get
{
return new bvec3(m03, m13, m23);
}
set
{
m03 = value.x;
m13 = value.y;
m23 = value.z;
}
}
#endregion
#region Static Properties
/// <summary>
/// Predefined all-zero matrix
/// </summary>
public static bmat3x4 Zero { get; } = new bmat3x4(false, false, false, false, false, false, false, false, false, false, false, false);
/// <summary>
/// Predefined all-ones matrix
/// </summary>
public static bmat3x4 Ones { get; } = new bmat3x4(true, true, true, true, true, true, true, true, true, true, true, true);
/// <summary>
/// Predefined identity matrix
/// </summary>
public static bmat3x4 Identity { get; } = new bmat3x4(true, false, false, false, false, true, false, false, false, false, true, 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 m02;
yield return m03;
yield return m10;
yield return m11;
yield return m12;
yield return m13;
yield return m20;
yield return m21;
yield return m22;
yield return m23;
}
/// <summary>
/// Returns an enumerator that iterates through all fields.
/// </summary>
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
#endregion
/// <summary>
/// Returns the number of Fields (3 x 4 = 12).
/// </summary>
public int Count => 12;
/// <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 m02;
case 3: return m03;
case 4: return m10;
case 5: return m11;
case 6: return m12;
case 7: return m13;
case 8: return m20;
case 9: return m21;
case 10: return m22;
case 11: return m23;
default: throw new ArgumentOutOfRangeException("fieldIndex");
}
}
set
{
switch (fieldIndex)
{
case 0: this.m00 = value; break;
case 1: this.m01 = value; break;
case 2: this.m02 = value; break;
case 3: this.m03 = value; break;
case 4: this.m10 = value; break;
case 5: this.m11 = value; break;
case 6: this.m12 = value; break;
case 7: this.m13 = value; break;
case 8: this.m20 = value; break;
case 9: this.m21 = value; break;
case 10: this.m22 = value; break;
case 11: this.m23 = 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 * 4 + row];
}
set
{
this[col * 4 + row] = value;
}
}
/// <summary>
/// Returns true iff this equals rhs component-wise.
/// </summary>
public bool Equals(bmat3x4 rhs) => ((((m00.Equals(rhs.m00) && m01.Equals(rhs.m01)) && m02.Equals(rhs.m02)) && ((m03.Equals(rhs.m03) && m10.Equals(rhs.m10)) && m11.Equals(rhs.m11))) && (((m12.Equals(rhs.m12) && m13.Equals(rhs.m13)) && m20.Equals(rhs.m20)) && ((m21.Equals(rhs.m21) && m22.Equals(rhs.m22)) && m23.Equals(rhs.m23))));
/// <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 bmat3x4 && Equals((bmat3x4) obj);
}
/// <summary>
/// Returns true iff this equals rhs component-wise.
/// </summary>
public static bool operator ==(bmat3x4 lhs, bmat3x4 rhs) => lhs.Equals(rhs);
/// <summary>
/// Returns true iff this does not equal rhs (component-wise).
/// </summary>
public static bool operator !=(bmat3x4 lhs, bmat3x4 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) ^ m02.GetHashCode()) * 2) ^ m03.GetHashCode()) * 2) ^ m10.GetHashCode()) * 2) ^ m11.GetHashCode()) * 2) ^ m12.GetHashCode()) * 2) ^ m13.GetHashCode()) * 2) ^ m20.GetHashCode()) * 2) ^ m21.GetHashCode()) * 2) ^ m22.GetHashCode()) * 2) ^ m23.GetHashCode();
}
}
/// <summary>
/// Returns a transposed version of this matrix.
/// </summary>
public bmat4x3 Transposed => new bmat4x3(m00, m10, m20, m01, m11, m21, m02, m12, m22, m03, m13, m23);
/// <summary>
/// Returns the minimal component of this matrix.
/// </summary>
public bool MinElement => ((((m00 && m01) && m02) && ((m03 && m10) && m11)) && (((m12 && m13) && m20) && ((m21 && m22) && m23)));
/// <summary>
/// Returns the maximal component of this matrix.
/// </summary>
public bool MaxElement => ((((m00 || m01) || m02) || ((m03 || m10) || m11)) || (((m12 || m13) || m20) || ((m21 || m22) || m23)));
/// <summary>
/// Returns true if all component are true.
/// </summary>
public bool All => ((((m00 && m01) && m02) && ((m03 && m10) && m11)) && (((m12 && m13) && m20) && ((m21 && m22) && m23)));
/// <summary>
/// Returns true if any component is true.
/// </summary>
public bool Any => ((((m00 || m01) || m02) || ((m03 || m10) || m11)) || (((m12 || m13) || m20) || ((m21 || m22) || m23)));
/// <summary>
/// Executes a component-wise &&. (sorry for different overload but && cannot be overloaded directly)
/// </summary>
public static bmat3x4 operator&(bmat3x4 lhs, bmat3x4 rhs) => new bmat3x4(lhs.m00 && rhs.m00, lhs.m01 && rhs.m01, lhs.m02 && rhs.m02, lhs.m03 && rhs.m03, lhs.m10 && rhs.m10, lhs.m11 && rhs.m11, lhs.m12 && rhs.m12, lhs.m13 && rhs.m13, lhs.m20 && rhs.m20, lhs.m21 && rhs.m21, lhs.m22 && rhs.m22, lhs.m23 && rhs.m23);
/// <summary>
/// Executes a component-wise ||. (sorry for different overload but || cannot be overloaded directly)
/// </summary>
public static bmat3x4 operator|(bmat3x4 lhs, bmat3x4 rhs) => new bmat3x4(lhs.m00 || rhs.m00, lhs.m01 || rhs.m01, lhs.m02 || rhs.m02, lhs.m03 || rhs.m03, lhs.m10 || rhs.m10, lhs.m11 || rhs.m11, lhs.m12 || rhs.m12, lhs.m13 || rhs.m13, lhs.m20 || rhs.m20, lhs.m21 || rhs.m21, lhs.m22 || rhs.m22, lhs.m23 || rhs.m23);
}
}