~glcompbench-dev/glcompbench/libmatrix-util

« back to all changes in this revision

Viewing changes to src/libmatrix/mat.h

  • Committer: Jesse Barker
  • Date: 2012-01-26 19:36:28 UTC
  • Revision ID: jesse.barker@linaro.org-20120126193628-r88v9tbu0kzzkyqb
Update local libmatrix version to reflect lp:libmatrix revno 32.  Preserve local
integration changes to the Program object (as well as credit to Alexandros for
having made them originally).

Show diffs side-by-side

added added

removed removed

Lines of Context:
46
46
// However, the internal data representation is column-major, so when using 
47
47
// the raw data access member to treat the data as a singly-dimensioned array,
48
48
// it does not have to be transposed.
 
49
//
 
50
// A template class for creating, managing and operating on a 2x2 matrix
 
51
// of any type you like (intended for built-in types, but as long as it 
 
52
// supports the basic arithmetic and assignment operators, any type should
 
53
// work).
49
54
template<typename T>
50
55
class tmat2
51
56
{
70
75
    }
71
76
    ~tmat2() {}
72
77
 
 
78
    // Reset this to the identity matrix.
73
79
    void setIdentity()
74
80
    {
75
81
        m_[0] = 1;
78
84
        m_[3] = 1;
79
85
    }
80
86
 
 
87
    // Transpose this.  Return a reference to this.
81
88
    tmat2& transpose()
82
89
    {
83
90
        T tmp_val = m_[1];
86
93
        return *this;
87
94
    }
88
95
 
 
96
    // Compute the determinant of this and return it.
89
97
    T determinant()
90
98
    {
91
99
        return (m_[0] * m_[3]) - (m_[2] * m_[1]);
92
100
    }
93
101
 
 
102
    // Invert this.  Return a reference to this.
 
103
    //
 
104
    // NOTE: If this is non-invertible, we will
 
105
    //       throw to avoid undefined behavior.
94
106
    tmat2& inverse() throw(std::runtime_error)
95
107
    {
96
108
        T d(determinant());
109
121
        return *this;
110
122
    }
111
123
 
 
124
    // Print the elements of the matrix to standard out.
 
125
    // Really only useful for debug and test.
112
126
    void print() const
113
127
    {
114
128
        static const int precision(6);
126
140
        std::cout << " |" << std::endl;
127
141
    }
128
142
 
 
143
    // Allow raw data access for API calls and the like.
 
144
    // For example, it is valid to pass a tmat2<float> into a call to
 
145
    // the OpenGL command "glUniformMatrix2fv()".
129
146
    operator const T*() const { return &m_[0];}
130
147
 
 
148
    // Test if 'rhs' is equal to this.
131
149
    bool operator==(const tmat2& rhs) const
132
150
    {
133
151
        return m_[0] == rhs.m_[0] &&
136
154
               m_[3] == rhs.m_[3];
137
155
    }
138
156
 
 
157
    // Test if 'rhs' is not equal to this.
139
158
    bool operator!=(const tmat2& rhs) const
140
159
    {
141
160
        return !(*this == rhs);
142
161
    }
143
162
 
 
163
    // A direct assignment of 'rhs' to this.  Return a reference to this.
144
164
    tmat2& operator=(const tmat2& rhs)
145
165
    {
146
166
        if (this != &rhs)
153
173
        return *this;
154
174
    }
155
175
 
 
176
    // Add another matrix to this.  Return a reference to this.
156
177
    tmat2& operator+=(const tmat2& rhs)
157
178
    {
158
179
        m_[0] += rhs.m_[0];
162
183
        return *this;
163
184
    }
164
185
 
 
186
    // Add another matrix to a copy of this.  Return the copy.
165
187
    const tmat2 operator+(const tmat2& rhs)
166
188
    {
167
189
        return tmat2(*this) += rhs;
168
190
    }
169
191
 
 
192
    // Subtract another matrix from this.  Return a reference to this.
170
193
    tmat2& operator-=(const tmat2& rhs)
171
194
    {
172
195
        m_[0] -= rhs.m_[0];
176
199
        return *this;
177
200
    }
178
201
 
 
202
    // Subtract another matrix from a copy of this.  Return the copy.
179
203
    const tmat2 operator-(const tmat2& rhs)
180
204
    {
181
205
        return tmat2(*this) += rhs;
182
206
    }
183
207
 
 
208
    // Multiply this by another matrix.  Return a reference to this.
184
209
    tmat2& operator*=(const tmat2& rhs)
185
210
    {
186
211
        T c0r0((m_[0] * rhs.m_[0]) + (m_[2] * rhs.m_[1]));
194
219
        return *this;
195
220
    }
196
221
 
 
222
    // Multiply a copy of this by another matrix.  Return the copy.
197
223
    const tmat2 operator*(const tmat2& rhs)
198
224
    {
199
225
        return tmat2(*this) *= rhs;
200
226
    }
201
227
 
 
228
    // Multiply this by a scalar.  Return a reference to this.
202
229
    tmat2& operator*=(const T& rhs)
203
230
    {
204
231
        m_[0] *= rhs;
208
235
        return *this;
209
236
    }
210
237
 
 
238
    // Multiply a copy of this by a scalar.  Return the copy.
211
239
    const tmat2 operator*(const T& rhs)
212
240
    {
213
241
        return tmat2(*this) *= rhs;
214
242
    }
215
243
 
 
244
    // Divide this by a scalar.  Return a reference to this.
216
245
    tmat2& operator/=(const T& rhs)
217
246
    {
218
247
        m_[0] /= rhs;
222
251
        return *this;
223
252
    }
224
253
 
 
254
    // Divide a copy of this by a scalar.  Return the copy.
225
255
    const tmat2 operator/(const T& rhs)
226
256
    {
227
257
        return tmat2(*this) /= rhs;
228
258
    }
229
259
 
 
260
    // Use an instance of the ArrayProxy class to support double-indexed
 
261
    // references to a matrix (i.e., m[1][1]).  See comments above the
 
262
    // ArrayProxy definition for more details.
230
263
    ArrayProxy<T, 2> operator[](int index)
231
264
    {
232
265
        return ArrayProxy<T, 2>(&m_[index]);
240
273
    T m_[4];
241
274
};
242
275
 
 
276
// Multiply a scalar and a matrix just like the member operator, but allow
 
277
// the scalar to be the left-hand operand.
243
278
template<typename T>
244
279
const tmat2<T> operator*(const T& lhs, const tmat2<T>& rhs)
245
280
{
246
281
    return tmat2<T>(rhs) * lhs;
247
282
}
248
283
 
 
284
// Multiply a copy of a vector and a matrix (matrix is right-hand operand).
 
285
// Return the copy.
249
286
template<typename T>
250
287
const tvec2<T> operator*(const tvec2<T>& lhs, const tmat2<T>& rhs)
251
288
{
254
291
    return tvec2<T>(x,y);
255
292
}
256
293
 
 
294
// Multiply a copy of a vector and a matrix (matrix is left-hand operand).
 
295
// Return the copy.
257
296
template<typename T>
258
297
const tvec2<T> operator*(const tmat2<T>& lhs, const tvec2<T>& rhs)
259
298
{
262
301
    return tvec2<T>(x, y);
263
302
}
264
303
 
 
304
// Compute the outer product of two vectors.  Return the resultant matrix.
265
305
template<typename T>
266
306
const tmat2<T> outer(const tvec2<T>& a, const tvec2<T>& b)
267
307
{
273
313
    return product;
274
314
}
275
315
 
 
316
// A template class for creating, managing and operating on a 3x3 matrix
 
317
// of any type you like (intended for built-in types, but as long as it 
 
318
// supports the basic arithmetic and assignment operators, any type should
 
319
// work).
276
320
template<typename T>
277
321
class tmat3
278
322
{
294
338
        m_[8] = m.m_[8];
295
339
    }
296
340
    tmat3(const T& c0r0, const T& c0r1, const T& c0r2,
297
 
           const T& c1r0, const T& c1r1, const T& c1r2,
298
 
           const T& c2r0, const T& c2r1, const T& c2r2)
 
341
          const T& c1r0, const T& c1r1, const T& c1r2,
 
342
          const T& c2r0, const T& c2r1, const T& c2r2)
299
343
    {
300
344
        m_[0] = c0r0;
301
345
        m_[1] = c0r1;
309
353
    }
310
354
    ~tmat3() {}
311
355
 
 
356
    // Reset this to the identity matrix.
312
357
    void setIdentity()
313
358
    {
314
359
        m_[0] = 1;
322
367
        m_[8] = 1;
323
368
    }
324
369
 
 
370
    // Transpose this.  Return a reference to this.
325
371
    tmat3& transpose()
326
372
    {
327
373
        T tmp_val = m_[1];
336
382
        return *this;
337
383
    }
338
384
 
 
385
    // Compute the determinant of this and return it.
339
386
    T determinant()
340
387
    {
341
388
        tmat2<T> minor0(m_[4], m_[5], m_[7], m_[8]);
346
393
               (m_[6] * minor6.determinant());
347
394
    }
348
395
 
 
396
    // Invert this.  Return a reference to this.
 
397
    //
 
398
    // NOTE: If this is non-invertible, we will
 
399
    //       throw to avoid undefined behavior.
349
400
    tmat3& inverse() throw(std::runtime_error)
350
401
    {
351
402
        T d(determinant());
374
425
        return *this;       
375
426
    }
376
427
 
 
428
    // Print the elements of the matrix to standard out.
 
429
    // Really only useful for debug and test.
377
430
    void print() const
378
431
    {
379
432
        static const int precision(6);
403
456
        std::cout << " |" << std::endl;
404
457
    }
405
458
 
 
459
    // Allow raw data access for API calls and the like.
 
460
    // For example, it is valid to pass a tmat3<float> into a call to
 
461
    // the OpenGL command "glUniformMatrix3fv()".
406
462
    operator const T*() const { return &m_[0];}
407
463
 
 
464
    // Test if 'rhs' is equal to this.
408
465
    bool operator==(const tmat3& rhs) const
409
466
    {
410
467
        return m_[0] == rhs.m_[0] &&
418
475
               m_[8] == rhs.m_[8];
419
476
    }
420
477
 
 
478
    // Test if 'rhs' is not equal to this.
421
479
    bool operator!=(const tmat3& rhs) const
422
480
    {
423
481
        return !(*this == rhs);
424
482
    }
425
483
 
 
484
    // A direct assignment of 'rhs' to this.  Return a reference to this.
426
485
    tmat3& operator=(const tmat3& rhs)
427
486
    {
428
487
        if (this != &rhs)
440
499
        return *this;
441
500
    }
442
501
 
 
502
    // Add another matrix to this.  Return a reference to this.
443
503
    tmat3& operator+=(const tmat3& rhs)
444
504
    {
445
505
        m_[0] += rhs.m_[0];
454
514
        return *this;
455
515
    }
456
516
 
 
517
    // Add another matrix to a copy of this.  Return the copy.
457
518
    const tmat3 operator+(const tmat3& rhs)
458
519
    {
459
520
        return tmat3(*this) += rhs;
460
521
    }
461
522
 
 
523
    // Subtract another matrix from this.  Return a reference to this.
462
524
    tmat3& operator-=(const tmat3& rhs)
463
525
    {
464
526
        m_[0] -= rhs.m_[0];
473
535
        return *this;
474
536
    }
475
537
 
 
538
    // Subtract another matrix from a copy of this.  Return the copy.
476
539
    const tmat3 operator-(const tmat3& rhs)
477
540
    {
478
541
        return tmat3(*this) -= rhs;
479
542
    }
480
543
 
 
544
    // Multiply this by another matrix.  Return a reference to this.
481
545
    tmat3& operator*=(const tmat3& rhs)
482
546
    {
483
547
        T c0r0((m_[0] * rhs.m_[0]) + (m_[3] * rhs.m_[1]) + (m_[6] * rhs.m_[2]));
501
565
        return *this;
502
566
    }
503
567
 
 
568
    // Multiply a copy of this by another matrix.  Return the copy.
504
569
    const tmat3 operator*(const tmat3& rhs)
505
570
    {
506
571
        return tmat3(*this) *= rhs;
507
572
    }
508
573
 
 
574
    // Multiply this by a scalar.  Return a reference to this.
509
575
    tmat3& operator*=(const T& rhs)
510
576
    {
511
577
        m_[0] *= rhs;
520
586
        return *this;
521
587
    }
522
588
 
 
589
    // Multiply a copy of this by a scalar.  Return the copy.
523
590
    const tmat3 operator*(const T& rhs)
524
591
    {
525
592
        return tmat3(*this) *= rhs;
526
593
    }
527
594
 
 
595
    // Divide this by a scalar.  Return a reference to this.
528
596
    tmat3& operator/=(const T& rhs)
529
597
    {
530
598
        m_[0] /= rhs;
539
607
        return *this;
540
608
    }
541
609
 
 
610
    // Divide a copy of this by a scalar.  Return the copy.
542
611
    const tmat3 operator/(const T& rhs)
543
612
    {
544
613
        return tmat3(*this) /= rhs;
545
614
    }
546
615
 
 
616
    // Use an instance of the ArrayProxy class to support double-indexed
 
617
    // references to a matrix (i.e., m[1][1]).  See comments above the
 
618
    // ArrayProxy definition for more details.
547
619
    ArrayProxy<T, 3> operator[](int index)
548
620
    {
549
621
        return ArrayProxy<T, 3>(&m_[index]);
557
629
    T m_[9];
558
630
};
559
631
 
 
632
// Multiply a scalar and a matrix just like the member operator, but allow
 
633
// the scalar to be the left-hand operand.
560
634
template<typename T>
561
635
const tmat3<T> operator*(const T& lhs, const tmat3<T>& rhs)
562
636
{
563
637
    return tmat3<T>(rhs) * lhs;
564
638
}
565
639
 
 
640
// Multiply a copy of a vector and a matrix (matrix is right-hand operand).
 
641
// Return the copy.
566
642
template<typename T>
567
643
const tvec3<T> operator*(const tvec3<T>& lhs, const tmat3<T>& rhs)
568
644
{
572
648
    return tvec3<T>(x, y, z);
573
649
}
574
650
 
 
651
// Multiply a copy of a vector and a matrix (matrix is left-hand operand).
 
652
// Return the copy.
575
653
template<typename T>
576
654
const tvec3<T> operator*(const tmat3<T>& lhs, const tvec3<T>& rhs)
577
655
{
581
659
    return tvec3<T>(x, y, z);
582
660
}
583
661
 
 
662
// Compute the outer product of two vectors.  Return the resultant matrix.
584
663
template<typename T>
585
664
const tmat3<T> outer(const tvec3<T>& a, const tvec3<T>& b)
586
665
{
597
676
    return product;
598
677
}
599
678
 
 
679
// A template class for creating, managing and operating on a 4x4 matrix
 
680
// of any type you like (intended for built-in types, but as long as it 
 
681
// supports the basic arithmetic and assignment operators, any type should
 
682
// work).
600
683
template<typename T>
601
684
class tmat4
602
685
{
626
709
    }
627
710
    ~tmat4() {}
628
711
 
 
712
    // Reset this to the identity matrix.
629
713
    void setIdentity()
630
714
    {
631
715
        m_[0] = 1;
646
730
        m_[15] = 1;
647
731
    }
648
732
 
 
733
    // Transpose this.  Return a reference to this.
649
734
    tmat4& transpose()
650
735
    {
651
736
        T tmp_val = m_[1];
669
754
        return *this;
670
755
    }
671
756
 
 
757
    // Compute the determinant of this and return it.
672
758
    T determinant()
673
759
    {
674
760
        tmat3<T> minor0(m_[5], m_[6], m_[7], m_[9], m_[10], m_[11], m_[13], m_[14], m_[15]);
681
767
               (m_[12] * minor12.determinant());
682
768
    }
683
769
 
 
770
    // Invert this.  Return a reference to this.
 
771
    //
 
772
    // NOTE: If this is non-invertible, we will
 
773
    //       throw to avoid undefined behavior.
684
774
    tmat4& inverse() throw(std::runtime_error)
685
775
    {
686
776
        T d(determinant());
726
816
        return *this;
727
817
    }
728
818
 
 
819
    // Print the elements of the matrix to standard out.
 
820
    // Really only useful for debug and test.
729
821
    void print() const
730
822
    {
731
823
        static const int precision(6);
771
863
        std::cout << " |" << std::endl;
772
864
    }
773
865
 
 
866
    // Allow raw data access for API calls and the like.
 
867
    // For example, it is valid to pass a tmat4<float> into a call to
 
868
    // the OpenGL command "glUniformMatrix4fv()".
774
869
    operator const T*() const { return &m_[0];}
775
870
 
 
871
    // Test if 'rhs' is equal to this.
776
872
    bool operator==(const tmat4& rhs) const
777
873
    {
778
874
        return m_[0] == rhs.m_[0] &&
793
889
               m_[15] == rhs.m_[15];
794
890
    }
795
891
 
 
892
    // Test if 'rhs' is not equal to this.
796
893
    bool operator!=(const tmat4& rhs) const
797
894
    {
798
895
        return !(*this == rhs);
799
896
    }
800
897
 
 
898
    // A direct assignment of 'rhs' to this.  Return a reference to this.
801
899
    tmat4& operator=(const tmat4& rhs)
802
900
    {
803
901
        if (this != &rhs)
822
920
        return *this;
823
921
    }
824
922
 
 
923
    // Add another matrix to this.  Return a reference to this.
825
924
    tmat4& operator+=(const tmat4& rhs)
826
925
    {
827
926
        m_[0] += rhs.m_[0];
843
942
        return *this;
844
943
    }
845
944
 
 
945
    // Add another matrix to a copy of this.  Return the copy.
846
946
    const tmat4 operator+(const tmat4& rhs)
847
947
    {
848
948
        return tmat4(*this) += rhs;
849
949
    }
850
950
 
 
951
    // Subtract another matrix from this.  Return a reference to this.
851
952
    tmat4& operator-=(const tmat4& rhs)
852
953
    {
853
954
        m_[0] -= rhs.m_[0];
869
970
        return *this;
870
971
    }
871
972
 
 
973
    // Subtract another matrix from a copy of this.  Return the copy.
872
974
    const tmat4 operator-(const tmat4& rhs)
873
975
    {
874
976
        return tmat4(*this) -= rhs;
875
977
    }
876
978
 
 
979
    // Multiply this by another matrix.  Return a reference to this.
877
980
    tmat4& operator*=(const tmat4& rhs)
878
981
    {
879
982
        T c0r0((m_[0] * rhs.m_[0]) + (m_[4] * rhs.m_[1]) + (m_[8] * rhs.m_[2]) + (m_[12] * rhs.m_[3]));
911
1014
        return *this;
912
1015
    }
913
1016
 
 
1017
    // Multiply a copy of this by another matrix.  Return the copy.
914
1018
    const tmat4 operator*(const tmat4& rhs)
915
1019
    {
916
1020
        return tmat4(*this) *= rhs;
917
1021
    }
918
1022
 
 
1023
    // Multiply this by a scalar.  Return a reference to this.
919
1024
    tmat4& operator*=(const T& rhs)
920
1025
    {
921
1026
        m_[0] *= rhs;
937
1042
        return *this;
938
1043
    }
939
1044
 
 
1045
    // Multiply a copy of this by a scalar.  Return the copy.
940
1046
    const tmat4 operator*(const T& rhs)
941
1047
    {
942
1048
        return tmat4(*this) *= rhs;
943
1049
    }
944
1050
 
 
1051
    // Divide this by a scalar.  Return a reference to this.
945
1052
    tmat4& operator/=(const T& rhs)
946
1053
    {
947
1054
        m_[0] /= rhs;
963
1070
        return *this;
964
1071
    }
965
1072
 
 
1073
    // Divide a copy of this by a scalar.  Return the copy.
966
1074
    const tmat4 operator/(const T& rhs)
967
1075
    {
968
1076
        return tmat4(*this) /= rhs;
969
1077
    }
970
1078
 
 
1079
    // Use an instance of the ArrayProxy class to support double-indexed
 
1080
    // references to a matrix (i.e., m[1][1]).  See comments above the
 
1081
    // ArrayProxy definition for more details.
971
1082
    ArrayProxy<T, 4> operator[](int index)
972
1083
    {
973
1084
        return ArrayProxy<T, 4>(&m_[index]);
981
1092
    T m_[16];
982
1093
};
983
1094
 
 
1095
// Multiply a scalar and a matrix just like the member operator, but allow
 
1096
// the scalar to be the left-hand operand.
984
1097
template<typename T>
985
1098
const tmat4<T> operator*(const T& lhs, const tmat4<T>& rhs)
986
1099
{
987
1100
    return tmat4<T>(rhs) * lhs;
988
1101
}
989
1102
 
 
1103
// Multiply a copy of a vector and a matrix (matrix is right-hand operand).
 
1104
// Return the copy.
990
1105
template<typename T>
991
1106
const tvec4<T> operator*(const tvec4<T>& lhs, const tmat4<T>& rhs)
992
1107
{
997
1112
    return tvec4<T>(x, y, z, w);
998
1113
}
999
1114
 
 
1115
// Multiply a copy of a vector and a matrix (matrix is left-hand operand).
 
1116
// Return the copy.
1000
1117
template<typename T>
1001
1118
const tvec4<T> operator*(const tmat4<T>& lhs, const tvec4<T>& rhs)
1002
1119
{
1007
1124
    return tvec4<T>(x, y, z, w);
1008
1125
}
1009
1126
 
 
1127
// Compute the outer product of two vectors.  Return the resultant matrix.
1010
1128
template<typename T>
1011
1129
const tmat4<T> outer(const tvec4<T>& a, const tvec4<T>& b)
1012
1130
{