Qt 6.x
The Qt SDK
Loading...
Searching...
No Matches
qgenericmatrix.h
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#ifndef QGENERICMATRIX_H
5#define QGENERICMATRIX_H
6
7#include <QtGui/qtguiglobal.h>
8#include <QtCore/qmetatype.h>
9#include <QtCore/qdebug.h>
10#include <QtCore/qdatastream.h>
11
13
14
15template <int N, int M, typename T>
17{
18public:
21 explicit QGenericMatrix(const T *values);
22
23 const T& operator()(int row, int column) const;
24 T& operator()(int row, int column);
25
26 bool isIdentity() const;
28
29 void fill(T value);
30
31 [[nodiscard]] QGenericMatrix<M, N, T> transposed() const;
32
39
40 void copyDataTo(T *values) const;
41
42 T *data() { return *m; }
43 const T *data() const { return *m; }
44 const T *constData() const { return *m; }
45
46 template<int NN, int MM, typename TT>
48 template<int NN, int MM, typename TT>
50 template<int NN, int M1, int M2, typename TT>
52 template<int NN, int MM, typename TT>
54 template<int NN, int MM, typename TT>
56 template<int NN, int MM, typename TT>
58 template<int NN, int MM, typename TT>
60
61private:
62 T m[N][M]; // Column-major order to match OpenGL.
63
64 template <int NN, int MM, typename TT>
65 friend class QGenericMatrix;
66};
67template <int N, int M, typename T>
69 : public QTypeInfoMerger<QGenericMatrix<N, M, T>, T>
70{
71};
72
73template <int N, int M, typename T>
75{
76 setToIdentity();
77}
78
79template <int N, int M, typename T>
80Q_OUTOFLINE_TEMPLATE QGenericMatrix<N, M, T>::QGenericMatrix(const T *values)
81{
82 for (int col = 0; col < N; ++col)
83 for (int row = 0; row < M; ++row)
84 m[col][row] = values[row * N + col];
85}
86
87template <int N, int M, typename T>
88Q_INLINE_TEMPLATE const T& QGenericMatrix<N, M, T>::operator()(int row, int column) const
89{
90 Q_ASSERT(row >= 0 && row < M && column >= 0 && column < N);
91 return m[column][row];
92}
93
94template <int N, int M, typename T>
95Q_INLINE_TEMPLATE T& QGenericMatrix<N, M, T>::operator()(int row, int column)
96{
97 Q_ASSERT(row >= 0 && row < M && column >= 0 && column < N);
98 return m[column][row];
99}
100
101template <int N, int M, typename T>
102Q_OUTOFLINE_TEMPLATE bool QGenericMatrix<N, M, T>::isIdentity() const
103{
104 for (int col = 0; col < N; ++col) {
105 for (int row = 0; row < M; ++row) {
106 if (row == col) {
107 if (m[col][row] != 1.0f)
108 return false;
109 } else {
110 if (m[col][row] != 0.0f)
111 return false;
112 }
113 }
114 }
115 return true;
116}
117
118template <int N, int M, typename T>
119Q_OUTOFLINE_TEMPLATE void QGenericMatrix<N, M, T>::setToIdentity()
120{
121 for (int col = 0; col < N; ++col) {
122 for (int row = 0; row < M; ++row) {
123 if (row == col)
124 m[col][row] = 1.0f;
125 else
126 m[col][row] = 0.0f;
127 }
128 }
129}
130
131template <int N, int M, typename T>
132Q_OUTOFLINE_TEMPLATE void QGenericMatrix<N, M, T>::fill(T value)
133{
134 for (int col = 0; col < N; ++col)
135 for (int row = 0; row < M; ++row)
136 m[col][row] = value;
137}
138
139template <int N, int M, typename T>
141{
143 for (int row = 0; row < M; ++row)
144 for (int col = 0; col < N; ++col)
145 result.m[row][col] = m[col][row];
146 return result;
147}
148
149template <int N, int M, typename T>
151{
152 for (int row = 0; row < M; ++row)
153 for (int col = 0; col < N; ++col)
154 m[col][row] += other.m[col][row];
155 return *this;
156}
157
158template <int N, int M, typename T>
161 for (int row = 0; row < M; ++row)
162 for (int col = 0; col < N; ++col)
163 m[col][row] -= other.m[col][row];
164 return *this;
165}
166
167template <int N, int M, typename T>
169{
170 for (int row = 0; row < M; ++row)
171 for (int col = 0; col < N; ++col)
172 m[col][row] *= factor;
173 return *this;
174}
178
179template <int N, int M, typename T>
181{
182 for (int row = 0; row < M; ++row)
183 for (int col = 0; col < N; ++col) {
184 if (m[col][row] != other.m[col][row])
185 return false;
186 }
187 return true;
188}
189
190template <int N, int M, typename T>
192{
193 return !(*this == other);
194}
195
197
198template <int N, int M, typename T>
200{
201 for (int row = 0; row < M; ++row)
202 for (int col = 0; col < N; ++col)
203 m[col][row] /= divisor;
204 return *this;
205}
206
207template <int N, int M, typename T>
209{
211 for (int row = 0; row < M; ++row)
212 for (int col = 0; col < N; ++col)
213 result.m[col][row] = m1.m[col][row] + m2.m[col][row];
214 return result;
215}
216
217template <int N, int M, typename T>
219{
221 for (int row = 0; row < M; ++row)
222 for (int col = 0; col < N; ++col)
223 result.m[col][row] = m1.m[col][row] - m2.m[col][row];
224 return result;
225}
226
227template <int N, int M1, int M2, typename T>
229{
231 for (int row = 0; row < M2; ++row) {
232 for (int col = 0; col < M1; ++col) {
233 T sum(0.0f);
234 for (int j = 0; j < N; ++j)
235 sum += m1.m[j][row] * m2.m[col][j];
236 result.m[col][row] = sum;
237 }
238 }
239 return result;
240}
241
242template <int N, int M, typename T>
246 for (int row = 0; row < M; ++row)
247 for (int col = 0; col < N; ++col)
248 result.m[col][row] = -matrix.m[col][row];
249 return result;
250}
251
252template <int N, int M, typename T>
254{
256 for (int row = 0; row < M; ++row)
257 for (int col = 0; col < N; ++col)
258 result.m[col][row] = matrix.m[col][row] * factor;
259 return result;
260}
261
262template <int N, int M, typename T>
264{
266 for (int row = 0; row < M; ++row)
267 for (int col = 0; col < N; ++col)
268 result.m[col][row] = matrix.m[col][row] * factor;
269 return result;
270}
272template <int N, int M, typename T>
274{
276 for (int row = 0; row < M; ++row)
277 for (int col = 0; col < N; ++col)
278 result.m[col][row] = matrix.m[col][row] / divisor;
279 return result;
281
282template <int N, int M, typename T>
283Q_OUTOFLINE_TEMPLATE void QGenericMatrix<N, M, T>::copyDataTo(T *values) const
284{
285 for (int col = 0; col < N; ++col)
286 for (int row = 0; row < M; ++row)
287 values[row * N + col] = T(m[col][row]);
288}
290// Define aliases for the useful variants of QGenericMatrix.
299
300#ifndef QT_NO_DEBUG_STREAM
301
302template <int N, int M, typename T>
304{
305 QDebugStateSaver saver(dbg);
306 dbg.nospace() << "QGenericMatrix<" << N << ", " << M
307 << ", " << QMetaType::fromType<T>().name()
308 << ">(" << Qt::endl << qSetFieldWidth(10);
309 for (int row = 0; row < M; ++row) {
310 for (int col = 0; col < N; ++col)
311 dbg << m(row, col);
312 dbg << Qt::endl;
313 }
314 dbg << qSetFieldWidth(0) << ')';
315 return dbg;
316}
317
318#endif
319
320#ifndef QT_NO_DATASTREAM
321
322template <int N, int M, typename T>
324{
325 for (int row = 0; row < M; ++row)
326 for (int col = 0; col < N; ++col)
327 stream << double(matrix(row, col));
328 return stream;
329}
330
331template <int N, int M, typename T>
333{
334 double x;
335 for (int row = 0; row < M; ++row) {
336 for (int col = 0; col < N; ++col) {
337 stream >> x;
338 matrix(row, col) = T(x);
339 }
340 }
341 return stream;
342}
343
344#endif
345
347
356
357#endif
\inmodule QtCore\reentrant
Definition qdatastream.h:30
\inmodule QtCore
\inmodule QtCore
The QGenericMatrix class is a template class that represents a NxM transformation matrix with N colum...
bool isIdentity() const
Returns true if this matrix is the identity; false otherwise.
friend QGenericMatrix< NN, MM, TT > operator-(const QGenericMatrix< NN, MM, TT > &matrix)
QGenericMatrix(const T *values)
Constructs a matrix from the given N * M floating-point values.
QGenericMatrix< N, M, T > & operator/=(T divisor)
Divides all elements of this matrix by divisor.
const T * constData() const
Returns a constant pointer to the raw data of this matrix.
T & operator()(int row, int column)
Returns a reference to the element at position (row, column) in this matrix so that the element can b...
bool operator!=(const QGenericMatrix< N, M, T > &other) const
Returns true if this matrix is not identical to other; false otherwise.
friend QGenericMatrix< M1, M2, TT > operator*(const QGenericMatrix< NN, M2, TT > &m1, const QGenericMatrix< M1, NN, TT > &m2)
Returns the product of the NNxM2 matrix m1 and the M1xNN matrix m2 to produce a M1xM2 matrix result.
friend QGenericMatrix< NN, MM, TT > operator*(const QGenericMatrix< NN, MM, TT > &matrix, TT factor)
QGenericMatrix(Qt::Initialization)
void setToIdentity()
Sets this matrix to the identity.
void fill(T value)
Fills all elements of this matrix with value.
friend QGenericMatrix< NN, MM, TT > operator*(TT factor, const QGenericMatrix< NN, MM, TT > &matrix)
friend QGenericMatrix< NN, MM, TT > operator/(const QGenericMatrix< NN, MM, TT > &matrix, TT divisor)
friend class QGenericMatrix
Constructs a NxM identity matrix.
friend QGenericMatrix< NN, MM, TT > operator-(const QGenericMatrix< NN, MM, TT > &m1, const QGenericMatrix< NN, MM, TT > &m2)
const T * data() const
Returns a constant pointer to the raw data of this matrix.
QGenericMatrix< N, M, T > & operator*=(T factor)
Multiplies all elements of this matrix by factor.
bool operator==(const QGenericMatrix< N, M, T > &other) const
Returns true if this matrix is identical to other; false otherwise.
QGenericMatrix< N, M, T > & operator+=(const QGenericMatrix< N, M, T > &other)
Adds the contents of other to this matrix.
void copyDataTo(T *values) const
Retrieves the N * M items in this matrix and copies them to values in row-major order.
QGenericMatrix< M, N, T > transposed() const
Returns this matrix, transposed about its diagonal.
friend QGenericMatrix< NN, MM, TT > operator+(const QGenericMatrix< NN, MM, TT > &m1, const QGenericMatrix< NN, MM, TT > &m2)
const T & operator()(int row, int column) const
Returns a constant reference to the element at position (row, column) in this matrix.
T * data()
Returns a pointer to the raw data of this matrix.
QGenericMatrix< N, M, T > & operator-=(const QGenericMatrix< N, M, T > &other)
Subtracts the contents of other from this matrix.
\inmodule QtCore
Definition qtypeinfo.h:92
Combined button and popup list for selecting options.
constexpr Initialization Uninitialized
Initialization
QTextStream & endl(QTextStream &stream)
Writes '\n' to the stream and flushes the stream.
#define QT_WARNING_POP
#define QT_WARNING_DISABLE_FLOAT_COMPARE
#define QT_WARNING_PUSH
EGLStreamKHR stream
EGLOutputLayerEXT EGLint EGLAttrib value
[5]
QGenericMatrix< 3, 2, float > QMatrix3x2
QGenericMatrix< 2, 2, float > QMatrix2x2
QGenericMatrix< 4, 2, float > QMatrix4x2
Q_OUTOFLINE_TEMPLATE QGenericMatrix< N, M, T > operator/(const QGenericMatrix< N, M, T > &matrix, T divisor)
QGenericMatrix< 2, 3, float > QMatrix2x3
QGenericMatrix< 3, 4, float > QMatrix3x4
QGenericMatrix< 2, 4, float > QMatrix2x4
QDebug operator<<(QDebug dbg, const QGenericMatrix< N, M, T > &m)
Q_OUTOFLINE_TEMPLATE QGenericMatrix< N, M, T > operator+(const QGenericMatrix< N, M, T > &m1, const QGenericMatrix< N, M, T > &m2)
Q_OUTOFLINE_TEMPLATE QGenericMatrix< N, M, T > operator-(const QGenericMatrix< N, M, T > &m1, const QGenericMatrix< N, M, T > &m2)
QGenericMatrix< 4, 3, float > QMatrix4x3
Q_OUTOFLINE_TEMPLATE QGenericMatrix< M1, M2, T > operator*(const QGenericMatrix< N, M2, T > &m1, const QGenericMatrix< M1, N, T > &m2)
QGenericMatrix< 3, 3, float > QMatrix3x3
QDataStream & operator>>(QDataStream &stream, QGenericMatrix< N, M, T > &matrix)
#define QT_DECL_METATYPE_EXTERN(TYPE, EXPORT)
Definition qmetatype.h:1367
GLenum GLsizei GLsizei GLint * values
[15]
GLint GLint GLint GLint GLint x
[0]
const GLfloat * m
GLuint divisor
GLenum GLenum GLsizei void GLsizei void * column
GLuint GLenum matrix
GLenum GLenum GLsizei void * row
GLuint64EXT * result
[6]
#define Q_ASSERT(cond)
Definition qrandom.cpp:47
#define M(_x, _y)
QTextStreamManipulator qSetFieldWidth(int width)
QSharedPointer< T > other(t)
[5]