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
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
|
/****************************************************************************
**
** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
** Contact: Qt Software Information (qt-info@nokia.com)
**
** This file is part of the $MODULE$ of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** No Commercial Usage
** This file contains pre-release code and may not be distributed.
** You may use this file in accordance with the terms and conditions
** contained in the either Technology Preview License Agreement or the
** Beta Release License Agreement.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain
** additional rights. These rights are described in the Nokia Qt LGPL
** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
** package.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3.0 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU General Public License version 3.0 requirements will be
** met: http://www.gnu.org/copyleft/gpl.html.
**
** If you are unsure which license is appropriate for your use, please
** contact the sales department at qt-sales@nokia.com.
** $QT_END_LICENSE$
**
****************************************************************************/
#ifndef QMATRIX4X4_H
#define QMATRIX4X4_H
#include <QtGui/qvector3d.h>
#include <QtGui/qvector4d.h>
#include <QtGui/qquaternion.h>
#include <QtGui/qgenericmatrix.h>
#include <QtCore/qrect.h>
QT_BEGIN_HEADER
QT_BEGIN_NAMESPACE
QT_MODULE(Gui)
#ifndef QT_NO_MATRIX4X4
class QMatrix;
class QTransform;
class Q_GUI_EXPORT QMatrix4x4
{
public:
inline QMatrix4x4() { setIdentity(); }
explicit QMatrix4x4(const qreal *values);
inline QMatrix4x4(qreal m11, qreal m12, qreal m13, qreal m14,
qreal m21, qreal m22, qreal m23, qreal m24,
qreal m31, qreal m32, qreal m33, qreal m34,
qreal m41, qreal m42, qreal m43, qreal m44);
#if !defined(QT_NO_MEMBER_TEMPLATES) || defined(Q_QDOC)
template <int N, int M>
explicit QMatrix4x4(const QGenericMatrix<N, M, qreal, qrealinner>& matrix);
#endif
QMatrix4x4(const qrealinner *values, int cols, int rows);
QMatrix4x4(const QTransform& transform);
QMatrix4x4(const QMatrix& matrix);
inline qreal operator()(int row, int column) const;
inline qrealinner& operator()(int row, int column);
inline QVector4D column(int index) const;
inline void setColumn(int index, const QVector4D& value);
inline QVector4D row(int index) const;
inline void setRow(int index, const QVector4D& value);
inline bool isIdentity() const;
inline void setIdentity();
inline void fill(qreal value);
qreal determinant() const;
QMatrix4x4 inverted(bool *invertible = 0) const;
QMatrix4x4 transposed() const;
QMatrix3x3 normalMatrix() const;
inline QMatrix4x4& operator+=(const QMatrix4x4& other);
inline QMatrix4x4& operator-=(const QMatrix4x4& other);
inline QMatrix4x4& operator*=(const QMatrix4x4& other);
inline QMatrix4x4& operator*=(qreal factor);
QMatrix4x4& operator/=(qreal divisor);
inline bool operator==(const QMatrix4x4& other) const;
inline bool operator!=(const QMatrix4x4& other) const;
friend QMatrix4x4 operator+(const QMatrix4x4& m1, const QMatrix4x4& m2);
friend QMatrix4x4 operator-(const QMatrix4x4& m1, const QMatrix4x4& m2);
friend QMatrix4x4 operator*(const QMatrix4x4& m1, const QMatrix4x4& m2);
#ifndef QT_NO_VECTOR3D
friend QVector3D operator*(const QMatrix4x4& matrix, const QVector3D& vector);
friend QVector3D operator*(const QVector3D& vector, const QMatrix4x4& matrix);
#endif
#ifndef QT_NO_VECTOR4D
friend QVector4D operator*(const QVector4D& vector, const QMatrix4x4& matrix);
friend QVector4D operator*(const QMatrix4x4& matrix, const QVector4D& vector);
#endif
friend QPoint operator*(const QPoint& point, const QMatrix4x4& matrix);
friend QPointF operator*(const QPointF& point, const QMatrix4x4& matrix);
friend QMatrix4x4 operator-(const QMatrix4x4& matrix);
friend QPoint operator*(const QMatrix4x4& matrix, const QPoint& point);
friend QPointF operator*(const QMatrix4x4& matrix, const QPointF& point);
friend QMatrix4x4 operator*(qreal factor, const QMatrix4x4& matrix);
friend QMatrix4x4 operator*(const QMatrix4x4& matrix, qreal factor);
friend Q_GUI_EXPORT QMatrix4x4 operator/(const QMatrix4x4& matrix, qreal divisor);
friend inline bool qFuzzyCompare(const QMatrix4x4& m1, const QMatrix4x4& m2);
#ifndef QT_NO_VECTOR3D
QMatrix4x4& scale(const QVector3D& vector);
QMatrix4x4& translate(const QVector3D& vector);
QMatrix4x4& rotate(qreal angle, const QVector3D& vector);
#endif
QMatrix4x4& scale(qreal x, qreal y, qreal z = 1.0f);
QMatrix4x4& scale(qreal factor);
QMatrix4x4& translate(qreal x, qreal y, qreal z = 0.0f);
QMatrix4x4& rotate(qreal angle, qreal x, qreal y, qreal z = 0.0f);
#ifndef QT_NO_QUATERNION
QMatrix4x4& rotate(const QQuaternion& quaternion);
#endif
#ifndef QT_NO_VECTOR3D
void extractAxisRotation(qreal &angle, QVector3D &axis) const;
QVector3D extractTranslation() const;
#endif
QMatrix4x4& ortho(const QRect& rect);
QMatrix4x4& ortho(const QRectF& rect);
QMatrix4x4& ortho(qreal left, qreal right, qreal bottom, qreal top, qreal nearPlane, qreal farPlane);
QMatrix4x4& frustum(qreal left, qreal right, qreal bottom, qreal top, qreal nearPlane, qreal farPlane);
QMatrix4x4& perspective(qreal angle, qreal aspect, qreal nearPlane, qreal farPlane);
#ifndef QT_NO_VECTOR3D
QMatrix4x4& lookAt(const QVector3D& eye, const QVector3D& center, const QVector3D& up);
#endif
QMatrix4x4& flipCoordinates();
void toValueArray(qreal *values) const;
QMatrix toAffine() const;
QTransform toTransform() const;
QPoint map(const QPoint& point) const;
QPointF map(const QPointF& point) const;
#ifndef QT_NO_VECTOR3D
QVector3D map(const QVector3D& point) const;
#endif
#ifndef QT_NO_VECTOR4D
QVector4D map(const QVector4D& point) const;
#endif
QRect mapRect(const QRect& rect) const;
QRectF mapRect(const QRectF& rect) const;
#if !defined(QT_NO_MEMBER_TEMPLATES) || defined(Q_QDOC)
template <int N, int M>
QGenericMatrix<N, M, qreal, qrealinner> toGenericMatrix() const;
#endif
inline qrealinner *data();
inline const qrealinner *data() const { return m[0]; }
inline const qrealinner *constData() const { return m[0]; }
void inferSpecialType();
#ifndef QT_NO_DEBUG_STREAM
friend Q_GUI_EXPORT QDebug operator<<(QDebug dbg, const QMatrix4x4 &m);
#endif
private:
qrealinner m[4][4]; // Column-major order to match OpenGL.
int flagBits; // Flag bits from the enum below.
enum {
Identity = 0x0001, // Identity matrix
General = 0x0002, // General matrix, unknown contents
Translation = 0x0004, // Contains a simple translation
Scale = 0x0008, // Contains a simple scale
Rotation = 0x0010 // Contains a simple rotation
};
// Construct without initializing identity matrix.
QMatrix4x4(int) { flagBits = General; }
QMatrix4x4 orthonormalInverse() const;
};
inline QMatrix4x4::QMatrix4x4
(qreal m11, qreal m12, qreal m13, qreal m14,
qreal m21, qreal m22, qreal m23, qreal m24,
qreal m31, qreal m32, qreal m33, qreal m34,
qreal m41, qreal m42, qreal m43, qreal m44)
{
m[0][0] = m11; m[0][1] = m21; m[0][2] = m31; m[0][3] = m41;
m[1][0] = m12; m[1][1] = m22; m[1][2] = m32; m[1][3] = m42;
m[2][0] = m13; m[2][1] = m23; m[2][2] = m33; m[2][3] = m43;
m[3][0] = m14; m[3][1] = m24; m[3][2] = m34; m[3][3] = m44;
flagBits = General;
}
#if !defined(QT_NO_MEMBER_TEMPLATES)
template <int N, int M>
Q_INLINE_TEMPLATE QMatrix4x4::QMatrix4x4
(const QGenericMatrix<N, M, qreal, qrealinner>& matrix)
{
const qrealinner *values = matrix.constData();
for (int col = 0; col < 4; ++col) {
for (int row = 0; row < 4; ++row) {
if (col < N && row < M)
m[col][row] = values[col * M + row];
else if (col == row)
m[col][row] = 1.0f;
else
m[col][row] = 0.0f;
}
}
flagBits = General;
}
template <int N, int M>
QGenericMatrix<N, M, qreal, qrealinner> QMatrix4x4::toGenericMatrix() const
{
QGenericMatrix<N, M, qreal, qrealinner> result;
qrealinner *values = result.data();
for (int col = 0; col < N; ++col) {
for (int row = 0; row < M; ++row) {
if (col < 4 && row < 4)
values[col * M + row] = m[col][row];
else if (col == row)
values[col * M + row] = 1.0f;
else
values[col * M + row] = 0.0f;
}
}
return result;
}
#endif
inline qreal QMatrix4x4::operator()(int row, int column) const
{
Q_ASSERT(row >= 0 && row < 4 && column >= 0 && column < 4);
return qt_math3d_convert<qreal, qrealinner>(m[column][row]);
}
inline qrealinner& QMatrix4x4::operator()(int row, int column)
{
Q_ASSERT(row >= 0 && row < 4 && column >= 0 && column < 4);
flagBits = General;
return m[column][row];
}
inline QVector4D QMatrix4x4::column(int index) const
{
Q_ASSERT(index >= 0 && index < 4);
return QVector4D(m[index][0], m[index][1], m[index][2], m[index][3], 1);
}
inline void QMatrix4x4::setColumn(int index, const QVector4D& value)
{
Q_ASSERT(index >= 0 && index < 4);
m[index][0] = value.xp;
m[index][1] = value.yp;
m[index][2] = value.zp;
m[index][3] = value.wp;
flagBits = General;
}
inline QVector4D QMatrix4x4::row(int index) const
{
Q_ASSERT(index >= 0 && index < 4);
return QVector4D(m[0][index], m[1][index], m[2][index], m[3][index], 1);
}
inline void QMatrix4x4::setRow(int index, const QVector4D& value)
{
Q_ASSERT(index >= 0 && index < 4);
m[0][index] = value.xp;
m[1][index] = value.yp;
m[2][index] = value.zp;
m[3][index] = value.wp;
flagBits = General;
}
Q_GUI_EXPORT QMatrix4x4 operator/(const QMatrix4x4& matrix, qreal divisor);
inline bool QMatrix4x4::isIdentity() const
{
if (flagBits == Identity)
return true;
if (m[0][0] != 1.0f || m[0][1] != 0.0f || m[0][2] != 0.0f)
return false;
if (m[0][3] != 0.0f || m[1][0] != 0.0f || m[1][1] != 1.0f)
return false;
if (m[1][2] != 0.0f || m[1][3] != 0.0f || m[2][0] != 0.0f)
return false;
if (m[2][1] != 0.0f || m[2][2] != 1.0f || m[2][3] != 0.0f)
return false;
if (m[3][0] != 0.0f || m[3][1] != 0.0f || m[3][2] != 0.0f)
return false;
return (m[3][3] == 1.0f);
}
inline void QMatrix4x4::setIdentity()
{
m[0][0] = 1.0f;
m[0][1] = 0.0f;
m[0][2] = 0.0f;
m[0][3] = 0.0f;
m[1][0] = 0.0f;
m[1][1] = 1.0f;
m[1][2] = 0.0f;
m[1][3] = 0.0f;
m[2][0] = 0.0f;
m[2][1] = 0.0f;
m[2][2] = 1.0f;
m[2][3] = 0.0f;
m[3][0] = 0.0f;
m[3][1] = 0.0f;
m[3][2] = 0.0f;
m[3][3] = 1.0f;
flagBits = Identity;
}
inline void QMatrix4x4::fill(qreal value)
{
m[0][0] = value;
m[0][1] = value;
m[0][2] = value;
m[0][3] = value;
m[1][0] = value;
m[1][1] = value;
m[1][2] = value;
m[1][3] = value;
m[2][0] = value;
m[2][1] = value;
m[2][2] = value;
m[2][3] = value;
m[3][0] = value;
m[3][1] = value;
m[3][2] = value;
m[3][3] = value;
flagBits = General;
}
inline QMatrix4x4& QMatrix4x4::operator+=(const QMatrix4x4& other)
{
m[0][0] += other.m[0][0];
m[0][1] += other.m[0][1];
m[0][2] += other.m[0][2];
m[0][3] += other.m[0][3];
m[1][0] += other.m[1][0];
m[1][1] += other.m[1][1];
m[1][2] += other.m[1][2];
m[1][3] += other.m[1][3];
m[2][0] += other.m[2][0];
m[2][1] += other.m[2][1];
m[2][2] += other.m[2][2];
m[2][3] += other.m[2][3];
m[3][0] += other.m[3][0];
m[3][1] += other.m[3][1];
m[3][2] += other.m[3][2];
m[3][3] += other.m[3][3];
flagBits = General;
return *this;
}
inline QMatrix4x4& QMatrix4x4::operator-=(const QMatrix4x4& other)
{
m[0][0] -= other.m[0][0];
m[0][1] -= other.m[0][1];
m[0][2] -= other.m[0][2];
m[0][3] -= other.m[0][3];
m[1][0] -= other.m[1][0];
m[1][1] -= other.m[1][1];
m[1][2] -= other.m[1][2];
m[1][3] -= other.m[1][3];
m[2][0] -= other.m[2][0];
m[2][1] -= other.m[2][1];
m[2][2] -= other.m[2][2];
m[2][3] -= other.m[2][3];
m[3][0] -= other.m[3][0];
m[3][1] -= other.m[3][1];
m[3][2] -= other.m[3][2];
m[3][3] -= other.m[3][3];
flagBits = General;
return *this;
}
inline QMatrix4x4& QMatrix4x4::operator*=(const QMatrix4x4& other)
{
if (flagBits == Identity) {
*this = other;
return *this;
} else if (other.flagBits == Identity) {
return *this;
} else {
*this = *this * other;
return *this;
}
}
inline QMatrix4x4& QMatrix4x4::operator*=(qreal factor)
{
qrealinner f(factor);
m[0][0] *= f;
m[0][1] *= f;
m[0][2] *= f;
m[0][3] *= f;
m[1][0] *= f;
m[1][1] *= f;
m[1][2] *= f;
m[1][3] *= f;
m[2][0] *= f;
m[2][1] *= f;
m[2][2] *= f;
m[2][3] *= f;
m[3][0] *= f;
m[3][1] *= f;
m[3][2] *= f;
m[3][3] *= f;
flagBits = General;
return *this;
}
inline bool QMatrix4x4::operator==(const QMatrix4x4& other) const
{
return m[0][0] == other.m[0][0] &&
m[0][1] == other.m[0][1] &&
m[0][2] == other.m[0][2] &&
m[0][3] == other.m[0][3] &&
m[1][0] == other.m[1][0] &&
m[1][1] == other.m[1][1] &&
m[1][2] == other.m[1][2] &&
m[1][3] == other.m[1][3] &&
m[2][0] == other.m[2][0] &&
m[2][1] == other.m[2][1] &&
m[2][2] == other.m[2][2] &&
m[2][3] == other.m[2][3] &&
m[3][0] == other.m[3][0] &&
m[3][1] == other.m[3][1] &&
m[3][2] == other.m[3][2] &&
m[3][3] == other.m[3][3];
}
inline bool QMatrix4x4::operator!=(const QMatrix4x4& other) const
{
return m[0][0] != other.m[0][0] ||
m[0][1] != other.m[0][1] ||
m[0][2] != other.m[0][2] ||
m[0][3] != other.m[0][3] ||
m[1][0] != other.m[1][0] ||
m[1][1] != other.m[1][1] ||
m[1][2] != other.m[1][2] ||
m[1][3] != other.m[1][3] ||
m[2][0] != other.m[2][0] ||
m[2][1] != other.m[2][1] ||
m[2][2] != other.m[2][2] ||
m[2][3] != other.m[2][3] ||
m[3][0] != other.m[3][0] ||
m[3][1] != other.m[3][1] ||
m[3][2] != other.m[3][2] ||
m[3][3] != other.m[3][3];
}
inline QMatrix4x4 operator+(const QMatrix4x4& m1, const QMatrix4x4& m2)
{
QMatrix4x4 m(1);
m.m[0][0] = m1.m[0][0] + m2.m[0][0];
m.m[0][1] = m1.m[0][1] + m2.m[0][1];
m.m[0][2] = m1.m[0][2] + m2.m[0][2];
m.m[0][3] = m1.m[0][3] + m2.m[0][3];
m.m[1][0] = m1.m[1][0] + m2.m[1][0];
m.m[1][1] = m1.m[1][1] + m2.m[1][1];
m.m[1][2] = m1.m[1][2] + m2.m[1][2];
m.m[1][3] = m1.m[1][3] + m2.m[1][3];
m.m[2][0] = m1.m[2][0] + m2.m[2][0];
m.m[2][1] = m1.m[2][1] + m2.m[2][1];
m.m[2][2] = m1.m[2][2] + m2.m[2][2];
m.m[2][3] = m1.m[2][3] + m2.m[2][3];
m.m[3][0] = m1.m[3][0] + m2.m[3][0];
m.m[3][1] = m1.m[3][1] + m2.m[3][1];
m.m[3][2] = m1.m[3][2] + m2.m[3][2];
m.m[3][3] = m1.m[3][3] + m2.m[3][3];
return m;
}
inline QMatrix4x4 operator-(const QMatrix4x4& m1, const QMatrix4x4& m2)
{
QMatrix4x4 m(1);
m.m[0][0] = m1.m[0][0] - m2.m[0][0];
m.m[0][1] = m1.m[0][1] - m2.m[0][1];
m.m[0][2] = m1.m[0][2] - m2.m[0][2];
m.m[0][3] = m1.m[0][3] - m2.m[0][3];
m.m[1][0] = m1.m[1][0] - m2.m[1][0];
m.m[1][1] = m1.m[1][1] - m2.m[1][1];
m.m[1][2] = m1.m[1][2] - m2.m[1][2];
m.m[1][3] = m1.m[1][3] - m2.m[1][3];
m.m[2][0] = m1.m[2][0] - m2.m[2][0];
m.m[2][1] = m1.m[2][1] - m2.m[2][1];
m.m[2][2] = m1.m[2][2] - m2.m[2][2];
m.m[2][3] = m1.m[2][3] - m2.m[2][3];
m.m[3][0] = m1.m[3][0] - m2.m[3][0];
m.m[3][1] = m1.m[3][1] - m2.m[3][1];
m.m[3][2] = m1.m[3][2] - m2.m[3][2];
m.m[3][3] = m1.m[3][3] - m2.m[3][3];
return m;
}
inline QMatrix4x4 operator*(const QMatrix4x4& m1, const QMatrix4x4& m2)
{
if (m1.flagBits == QMatrix4x4::Identity)
return m2;
else if (m2.flagBits == QMatrix4x4::Identity)
return m1;
QMatrix4x4 m(1);
m.m[0][0] = m1.m[0][0] * m2.m[0][0] +
m1.m[1][0] * m2.m[0][1] +
m1.m[2][0] * m2.m[0][2] +
m1.m[3][0] * m2.m[0][3];
m.m[0][1] = m1.m[0][1] * m2.m[0][0] +
m1.m[1][1] * m2.m[0][1] +
m1.m[2][1] * m2.m[0][2] +
m1.m[3][1] * m2.m[0][3];
m.m[0][2] = m1.m[0][2] * m2.m[0][0] +
m1.m[1][2] * m2.m[0][1] +
m1.m[2][2] * m2.m[0][2] +
m1.m[3][2] * m2.m[0][3];
m.m[0][3] = m1.m[0][3] * m2.m[0][0] +
m1.m[1][3] * m2.m[0][1] +
m1.m[2][3] * m2.m[0][2] +
m1.m[3][3] * m2.m[0][3];
m.m[1][0] = m1.m[0][0] * m2.m[1][0] +
m1.m[1][0] * m2.m[1][1] +
m1.m[2][0] * m2.m[1][2] +
m1.m[3][0] * m2.m[1][3];
m.m[1][1] = m1.m[0][1] * m2.m[1][0] +
m1.m[1][1] * m2.m[1][1] +
m1.m[2][1] * m2.m[1][2] +
m1.m[3][1] * m2.m[1][3];
m.m[1][2] = m1.m[0][2] * m2.m[1][0] +
m1.m[1][2] * m2.m[1][1] +
m1.m[2][2] * m2.m[1][2] +
m1.m[3][2] * m2.m[1][3];
m.m[1][3] = m1.m[0][3] * m2.m[1][0] +
m1.m[1][3] * m2.m[1][1] +
m1.m[2][3] * m2.m[1][2] +
m1.m[3][3] * m2.m[1][3];
m.m[2][0] = m1.m[0][0] * m2.m[2][0] +
m1.m[1][0] * m2.m[2][1] +
m1.m[2][0] * m2.m[2][2] +
m1.m[3][0] * m2.m[2][3];
m.m[2][1] = m1.m[0][1] * m2.m[2][0] +
m1.m[1][1] * m2.m[2][1] +
m1.m[2][1] * m2.m[2][2] +
m1.m[3][1] * m2.m[2][3];
m.m[2][2] = m1.m[0][2] * m2.m[2][0] +
m1.m[1][2] * m2.m[2][1] +
m1.m[2][2] * m2.m[2][2] +
m1.m[3][2] * m2.m[2][3];
m.m[2][3] = m1.m[0][3] * m2.m[2][0] +
m1.m[1][3] * m2.m[2][1] +
m1.m[2][3] * m2.m[2][2] +
m1.m[3][3] * m2.m[2][3];
m.m[3][0] = m1.m[0][0] * m2.m[3][0] +
m1.m[1][0] * m2.m[3][1] +
m1.m[2][0] * m2.m[3][2] +
m1.m[3][0] * m2.m[3][3];
m.m[3][1] = m1.m[0][1] * m2.m[3][0] +
m1.m[1][1] * m2.m[3][1] +
m1.m[2][1] * m2.m[3][2] +
m1.m[3][1] * m2.m[3][3];
m.m[3][2] = m1.m[0][2] * m2.m[3][0] +
m1.m[1][2] * m2.m[3][1] +
m1.m[2][2] * m2.m[3][2] +
m1.m[3][2] * m2.m[3][3];
m.m[3][3] = m1.m[0][3] * m2.m[3][0] +
m1.m[1][3] * m2.m[3][1] +
m1.m[2][3] * m2.m[3][2] +
m1.m[3][3] * m2.m[3][3];
return m;
}
#ifndef QT_NO_VECTOR3D
inline QVector3D operator*(const QVector3D& vector, const QMatrix4x4& matrix)
{
qrealinner x, y, z, w;
x = vector.xp * matrix.m[0][0] +
vector.yp * matrix.m[0][1] +
vector.zp * matrix.m[0][2] +
matrix.m[0][3];
y = vector.xp * matrix.m[1][0] +
vector.yp * matrix.m[1][1] +
vector.zp * matrix.m[1][2] +
matrix.m[1][3];
z = vector.xp * matrix.m[2][0] +
vector.yp * matrix.m[2][1] +
vector.zp * matrix.m[2][2] +
matrix.m[2][3];
w = vector.xp * matrix.m[3][0] +
vector.yp * matrix.m[3][1] +
vector.zp * matrix.m[3][2] +
matrix.m[3][3];
if (w == 1.0f)
return QVector3D(x, y, z, 1);
else
return QVector3D(x / w, y / w, z / w, 1);
}
inline QVector3D operator*(const QMatrix4x4& matrix, const QVector3D& vector)
{
qrealinner x, y, z, w;
x = vector.xp * matrix.m[0][0] +
vector.yp * matrix.m[1][0] +
vector.zp * matrix.m[2][0] +
matrix.m[3][0];
y = vector.xp * matrix.m[0][1] +
vector.yp * matrix.m[1][1] +
vector.zp * matrix.m[2][1] +
matrix.m[3][1];
z = vector.xp * matrix.m[0][2] +
vector.yp * matrix.m[1][2] +
vector.zp * matrix.m[2][2] +
matrix.m[3][2];
w = vector.xp * matrix.m[0][3] +
vector.yp * matrix.m[1][3] +
vector.zp * matrix.m[2][3] +
matrix.m[3][3];
if (w == 1.0f)
return QVector3D(x, y, z, 1);
else
return QVector3D(x / w, y / w, z / w, 1);
}
#endif
#ifndef QT_NO_VECTOR4D
inline QVector4D operator*(const QVector4D& vector, const QMatrix4x4& matrix)
{
qrealinner x, y, z, w;
x = vector.xp * matrix.m[0][0] +
vector.yp * matrix.m[0][1] +
vector.zp * matrix.m[0][2] +
vector.wp * matrix.m[0][3];
y = vector.xp * matrix.m[1][0] +
vector.yp * matrix.m[1][1] +
vector.zp * matrix.m[1][2] +
vector.wp * matrix.m[1][3];
z = vector.xp * matrix.m[2][0] +
vector.yp * matrix.m[2][1] +
vector.zp * matrix.m[2][2] +
vector.wp * matrix.m[2][3];
w = vector.xp * matrix.m[3][0] +
vector.yp * matrix.m[3][1] +
vector.zp * matrix.m[3][2] +
vector.wp * matrix.m[3][3];
return QVector4D(x, y, z, w, 1);
}
inline QVector4D operator*(const QMatrix4x4& matrix, const QVector4D& vector)
{
qrealinner x, y, z, w;
x = vector.xp * matrix.m[0][0] +
vector.yp * matrix.m[1][0] +
vector.zp * matrix.m[2][0] +
vector.wp * matrix.m[3][0];
y = vector.xp * matrix.m[0][1] +
vector.yp * matrix.m[1][1] +
vector.zp * matrix.m[2][1] +
vector.wp * matrix.m[3][1];
z = vector.xp * matrix.m[0][2] +
vector.yp * matrix.m[1][2] +
vector.zp * matrix.m[2][2] +
vector.wp * matrix.m[3][2];
w = vector.xp * matrix.m[0][3] +
vector.yp * matrix.m[1][3] +
vector.zp * matrix.m[2][3] +
vector.wp * matrix.m[3][3];
return QVector4D(x, y, z, w, 1);
}
#endif
inline QPoint operator*(const QPoint& point, const QMatrix4x4& matrix)
{
qrealinner xin, yin;
qrealinner x, y, w;
xin = point.x();
yin = point.y();
x = xin * matrix.m[0][0] +
yin * matrix.m[0][1] +
matrix.m[0][3];
y = xin * matrix.m[1][0] +
yin * matrix.m[1][1] +
matrix.m[1][3];
w = xin * matrix.m[3][0] +
yin * matrix.m[3][1] +
matrix.m[3][3];
if (w == 1.0f)
return QPoint(qRound(x), qRound(y));
else
return QPoint(qRound(x / w), qRound(y / w));
}
inline QPointF operator*(const QPointF& point, const QMatrix4x4& matrix)
{
qrealinner xin, yin;
qrealinner x, y, w;
xin = point.x();
yin = point.y();
x = xin * matrix.m[0][0] +
yin * matrix.m[0][1] +
matrix.m[0][3];
y = xin * matrix.m[1][0] +
yin * matrix.m[1][1] +
matrix.m[1][3];
w = xin * matrix.m[3][0] +
yin * matrix.m[3][1] +
matrix.m[3][3];
if (w == 1.0f) {
return QPointF(qt_math3d_convert<qreal, qrealinner>(x),
qt_math3d_convert<qreal, qrealinner>(y));
} else {
return QPointF(qt_math3d_convert<qreal, qrealinner>(x / w),
qt_math3d_convert<qreal, qrealinner>(y / w));
}
}
inline QPoint operator*(const QMatrix4x4& matrix, const QPoint& point)
{
qrealinner xin, yin;
qrealinner x, y, w;
xin = point.x();
yin = point.y();
x = xin * matrix.m[0][0] +
yin * matrix.m[1][0] +
matrix.m[3][0];
y = xin * matrix.m[0][1] +
yin * matrix.m[1][1] +
matrix.m[3][1];
w = xin * matrix.m[0][3] +
yin * matrix.m[1][3] +
matrix.m[3][3];
if (w == 1.0f)
return QPoint(qRound(x), qRound(y));
else
return QPoint(qRound(x / w), qRound(y / w));
}
inline QPointF operator*(const QMatrix4x4& matrix, const QPointF& point)
{
qrealinner xin, yin;
qrealinner x, y, w;
xin = point.x();
yin = point.y();
x = xin * matrix.m[0][0] +
yin * matrix.m[1][0] +
matrix.m[3][0];
y = xin * matrix.m[0][1] +
yin * matrix.m[1][1] +
matrix.m[3][1];
w = xin * matrix.m[0][3] +
yin * matrix.m[1][3] +
matrix.m[3][3];
if (w == 1.0f) {
return QPointF(qt_math3d_convert<qreal, qrealinner>(x),
qt_math3d_convert<qreal, qrealinner>(y));
} else {
return QPointF(qt_math3d_convert<qreal, qrealinner>(x / w),
qt_math3d_convert<qreal, qrealinner>(y / w));
}
}
inline QMatrix4x4 operator-(const QMatrix4x4& matrix)
{
QMatrix4x4 m(1);
m.m[0][0] = -matrix.m[0][0];
m.m[0][1] = -matrix.m[0][1];
m.m[0][2] = -matrix.m[0][2];
m.m[0][3] = -matrix.m[0][3];
m.m[1][0] = -matrix.m[1][0];
m.m[1][1] = -matrix.m[1][1];
m.m[1][2] = -matrix.m[1][2];
m.m[1][3] = -matrix.m[1][3];
m.m[2][0] = -matrix.m[2][0];
m.m[2][1] = -matrix.m[2][1];
m.m[2][2] = -matrix.m[2][2];
m.m[2][3] = -matrix.m[2][3];
m.m[3][0] = -matrix.m[3][0];
m.m[3][1] = -matrix.m[3][1];
m.m[3][2] = -matrix.m[3][2];
m.m[3][3] = -matrix.m[3][3];
return m;
}
inline QMatrix4x4 operator*(qreal factor, const QMatrix4x4& matrix)
{
QMatrix4x4 m(1);
qrealinner f(factor);
m.m[0][0] = matrix.m[0][0] * f;
m.m[0][1] = matrix.m[0][1] * f;
m.m[0][2] = matrix.m[0][2] * f;
m.m[0][3] = matrix.m[0][3] * f;
m.m[1][0] = matrix.m[1][0] * f;
m.m[1][1] = matrix.m[1][1] * f;
m.m[1][2] = matrix.m[1][2] * f;
m.m[1][3] = matrix.m[1][3] * f;
m.m[2][0] = matrix.m[2][0] * f;
m.m[2][1] = matrix.m[2][1] * f;
m.m[2][2] = matrix.m[2][2] * f;
m.m[2][3] = matrix.m[2][3] * f;
m.m[3][0] = matrix.m[3][0] * f;
m.m[3][1] = matrix.m[3][1] * f;
m.m[3][2] = matrix.m[3][2] * f;
m.m[3][3] = matrix.m[3][3] * f;
return m;
}
inline QMatrix4x4 operator*(const QMatrix4x4& matrix, qreal factor)
{
QMatrix4x4 m(1);
qrealinner f(factor);
m.m[0][0] = matrix.m[0][0] * f;
m.m[0][1] = matrix.m[0][1] * f;
m.m[0][2] = matrix.m[0][2] * f;
m.m[0][3] = matrix.m[0][3] * f;
m.m[1][0] = matrix.m[1][0] * f;
m.m[1][1] = matrix.m[1][1] * f;
m.m[1][2] = matrix.m[1][2] * f;
m.m[1][3] = matrix.m[1][3] * f;
m.m[2][0] = matrix.m[2][0] * f;
m.m[2][1] = matrix.m[2][1] * f;
m.m[2][2] = matrix.m[2][2] * f;
m.m[2][3] = matrix.m[2][3] * f;
m.m[3][0] = matrix.m[3][0] * f;
m.m[3][1] = matrix.m[3][1] * f;
m.m[3][2] = matrix.m[3][2] * f;
m.m[3][3] = matrix.m[3][3] * f;
return m;
}
inline bool qFuzzyCompare(const QMatrix4x4& m1, const QMatrix4x4& m2)
{
return qFuzzyCompare(m1.m[0][0], m2.m[0][0]) &&
qFuzzyCompare(m1.m[0][1], m2.m[0][1]) &&
qFuzzyCompare(m1.m[0][2], m2.m[0][2]) &&
qFuzzyCompare(m1.m[0][3], m2.m[0][3]) &&
qFuzzyCompare(m1.m[1][0], m2.m[1][0]) &&
qFuzzyCompare(m1.m[1][1], m2.m[1][1]) &&
qFuzzyCompare(m1.m[1][2], m2.m[1][2]) &&
qFuzzyCompare(m1.m[1][3], m2.m[1][3]) &&
qFuzzyCompare(m1.m[2][0], m2.m[2][0]) &&
qFuzzyCompare(m1.m[2][1], m2.m[2][1]) &&
qFuzzyCompare(m1.m[2][2], m2.m[2][2]) &&
qFuzzyCompare(m1.m[2][3], m2.m[2][3]) &&
qFuzzyCompare(m1.m[3][0], m2.m[3][0]) &&
qFuzzyCompare(m1.m[3][1], m2.m[3][1]) &&
qFuzzyCompare(m1.m[3][2], m2.m[3][2]) &&
qFuzzyCompare(m1.m[3][3], m2.m[3][3]);
}
inline QPoint QMatrix4x4::map(const QPoint& point) const
{
return *this * point;
}
inline QPointF QMatrix4x4::map(const QPointF& point) const
{
return *this * point;
}
#ifndef QT_NO_VECTOR3D
inline QVector3D QMatrix4x4::map(const QVector3D& point) const
{
return *this * point;
}
#endif
#ifndef QT_NO_VECTOR4D
inline QVector4D QMatrix4x4::map(const QVector4D& point) const
{
return *this * point;
}
#endif
inline QRect QMatrix4x4::mapRect(const QRect& rect) const
{
QPoint tl = map(rect.topLeft()); QPoint tr = map(rect.topRight());
QPoint bl = map(rect.bottomLeft()); QPoint br = map(rect.bottomRight());
int xmin = qMin(qMin(tl.x(), tr.x()), qMin(bl.x(), br.x()));
int xmax = qMax(qMax(tl.x(), tr.x()), qMax(bl.x(), br.x()));
int ymin = qMin(qMin(tl.y(), tr.y()), qMin(bl.y(), br.y()));
int ymax = qMax(qMax(tl.y(), tr.y()), qMax(bl.y(), br.y()));
return QRect(QPoint(xmin, ymin), QPoint(xmax, ymax));
}
inline QRectF QMatrix4x4::mapRect(const QRectF& rect) const
{
QPointF tl = map(rect.topLeft()); QPointF tr = map(rect.topRight());
QPointF bl = map(rect.bottomLeft()); QPointF br = map(rect.bottomRight());
qreal xmin = qMin(qMin(tl.x(), tr.x()), qMin(bl.x(), br.x()));
qreal xmax = qMax(qMax(tl.x(), tr.x()), qMax(bl.x(), br.x()));
qreal ymin = qMin(qMin(tl.y(), tr.y()), qMin(bl.y(), br.y()));
qreal ymax = qMax(qMax(tl.y(), tr.y()), qMax(bl.y(), br.y()));
return QRectF(QPointF(xmin, ymin), QPointF(xmax, ymax));
}
inline qrealinner *QMatrix4x4::data()
{
// We have to assume that the caller will modify the matrix elements,
// so we flip it over to "General" mode.
flagBits = General;
return m[0];
}
#ifndef QT_NO_DEBUG_STREAM
Q_GUI_EXPORT QDebug operator<<(QDebug dbg, const QMatrix4x4 &m);
#endif
template <int N, int M>
QMatrix4x4 qGenericMatrixToMatrix4x4(const QGenericMatrix<N, M, qreal, qrealinner>& matrix)
{
return QMatrix4x4(matrix.constData(), N, M);
}
template <int N, int M>
QGenericMatrix<N, M, qreal, qrealinner> qGenericMatrixFromMatrix4x4(const QMatrix4x4& matrix)
{
QGenericMatrix<N, M, qreal, qrealinner> result;
const qrealinner *m = matrix.constData();
qrealinner *values = result.data();
for (int col = 0; col < N; ++col) {
for (int row = 0; row < M; ++row) {
if (col < 4 && row < 4)
values[col * M + row] = m[col * 4 + row];
else if (col == row)
values[col * M + row] = 1.0f;
else
values[col * M + row] = 0.0f;
}
}
return result;
}
#endif
QT_END_NAMESPACE
#ifndef QT_NO_MATRIX4X4
Q_DECLARE_METATYPE(QMatrix4x4)
#endif
QT_END_HEADER
#endif
|