Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
qopenvgmatrix.cpp
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#include "qopenvgmatrix.h"
5
7
8// QOpenVGMatrix: Because Qt will never have enough matrix classes
9// Internally the data is stored as column-major format
10// So this is a 3x3 version of QMatrix4x4 for optimal
11// OpenVG usage.
12
17
19{
20 for (int col = 0; col < 3; ++col)
21 for (int row = 0; row < 3; ++row)
22 m[col][row] = values[col * 3 + row];
23}
24
25const float &QOpenVGMatrix::operator()(int row, int column) const
26{
27 Q_ASSERT(row >= 0 && row < 4 && column >= 0 && column < 4);
28 return m[column][row];
29}
30
32{
33 Q_ASSERT(row >= 0 && row < 4 && column >= 0 && column < 4);
34 return m[column][row];
35}
36
39 if (m[0][0] != 1.0f || m[0][1] != 0.0f || m[0][2] != 0.0f)
40 return false;
41 if ( m[1][0] != 0.0f || m[1][1] != 1.0f)
42 return false;
43 if (m[1][2] != 0.0f || m[2][0] != 0.0f)
44 return false;
45 if (m[2][1] != 0.0f || m[2][2] != 1.0f)
46 return false;
47
48 return true;
49}
50
52{
53 m[0][0] = 1.0f;
54 m[0][1] = 0.0f;
55 m[0][2] = 0.0f;
56 m[1][0] = 0.0f;
57 m[1][1] = 1.0f;
58 m[1][2] = 0.0f;
59 m[2][0] = 0.0f;
60 m[2][1] = 0.0f;
61 m[2][2] = 1.0f;
62}
63
65{
66 if (m[0][2] == 0.0f && m[1][2] == 0.0f && m[2][2] == 1.0f)
67 return true;
68
69 return false;
70}
71
73{
74 return *this * point;
75}
76
78{
79 m[0][0] = value;
80 m[0][1] = value;
81 m[0][2] = value;
82 m[1][0] = value;
83 m[1][1] = value;
84 m[1][2] = value;
85 m[2][0] = value;
86 m[2][1] = value;
87 m[2][2] = value;
88}
89
91{
93 for (int row = 0; row < 3; ++row) {
94 for (int col = 0; col < 3; ++col)
95 result.m[col][row] = m[row][col];
96 }
97 return result;
98}
99
101{
102 m[0][0] += other.m[0][0];
103 m[0][1] += other.m[0][1];
104 m[0][2] += other.m[0][2];
105 m[1][0] += other.m[1][0];
106 m[1][1] += other.m[1][1];
107 m[1][2] += other.m[1][2];
108 m[2][0] += other.m[2][0];
109 m[2][1] += other.m[2][1];
110 m[2][2] += other.m[2][2];
111 return *this;
112}
113
115{
116 m[0][0] -= other.m[0][0];
117 m[0][1] -= other.m[0][1];
118 m[0][2] -= other.m[0][2];
119 m[1][0] -= other.m[1][0];
120 m[1][1] -= other.m[1][1];
121 m[1][2] -= other.m[1][2];
122 m[2][0] -= other.m[2][0];
123 m[2][1] -= other.m[2][1];
124 m[2][2] -= other.m[2][2];
125 return *this;
126}
127
129{
130 float m0, m1;
131 m0 = m[0][0] * other.m[0][0]
132 + m[1][0] * other.m[0][1]
133 + m[2][0] * other.m[0][2];
134 m1 = m[0][0] * other.m[1][0]
135 + m[1][0] * other.m[1][1]
136 + m[2][0] * other.m[1][2];
137 m[2][0] = m[0][0] * other.m[2][0]
138 + m[1][0] * other.m[2][1]
139 + m[2][0] * other.m[2][2];
140 m[0][0] = m0;
141 m[1][0] = m1;
142
143 m0 = m[0][1] * other.m[0][0]
144 + m[1][1] * other.m[0][1]
145 + m[2][1] * other.m[0][2];
146 m1 = m[0][1] * other.m[1][0]
147 + m[1][1] * other.m[1][1]
148 + m[2][1] * other.m[1][2];
149 m[2][1] = m[0][1] * other.m[2][0]
150 + m[1][1] * other.m[2][1]
151 + m[2][1] * other.m[2][2];
152 m[0][1] = m0;
153 m[1][1] = m1;
154
155 m0 = m[0][2] * other.m[0][0]
156 + m[1][2] * other.m[0][1]
157 + m[2][2] * other.m[0][2];
158 m1 = m[0][2] * other.m[1][0]
159 + m[1][2] * other.m[1][1]
160 + m[2][2] * other.m[1][2];
161 m[2][2] = m[0][2] * other.m[2][0]
162 + m[1][2] * other.m[2][1]
163 + m[2][2] * other.m[2][2];
164 m[0][2] = m0;
165 m[1][2] = m1;
166 return *this;
167}
168
170{
171 m[0][0] *= factor;
172 m[0][1] *= factor;
173 m[0][2] *= factor;
174 m[1][0] *= factor;
175 m[1][1] *= factor;
176 m[1][2] *= factor;
177 m[2][0] *= factor;
178 m[2][1] *= factor;
179 m[2][2] *= factor;
180 return *this;
181}
182
184{
185 m[0][0] /= divisor;
186 m[0][1] /= divisor;
187 m[0][2] /= divisor;
188 m[1][0] /= divisor;
189 m[1][1] /= divisor;
190 m[1][2] /= divisor;
191 m[2][0] /= divisor;
192 m[2][1] /= divisor;
193 m[2][2] /= divisor;
194 return *this;
195}
196
198{
199 return m[0][0] == other.m[0][0] &&
200 m[0][1] == other.m[0][1] &&
201 m[0][2] == other.m[0][2] &&
202 m[1][0] == other.m[1][0] &&
203 m[1][1] == other.m[1][1] &&
204 m[1][2] == other.m[1][2] &&
205 m[2][0] == other.m[2][0] &&
206 m[2][1] == other.m[2][1] &&
207 m[2][2] == other.m[2][2];
208}
209
211{
212 return m[0][0] != other.m[0][0] ||
213 m[0][1] != other.m[0][1] ||
214 m[0][2] != other.m[0][2] ||
215 m[1][0] != other.m[1][0] ||
216 m[1][1] != other.m[1][1] ||
217 m[1][2] != other.m[1][2] ||
218 m[2][0] != other.m[2][0] ||
219 m[2][1] != other.m[2][1] ||
220 m[2][2] != other.m[2][2];
221}
222
224{
225 // Row-Major?
226 for (int row = 0; row < 3; ++row) {
227 for (int col = 0; col < 3; ++col)
228 values[row * 3 + col] = float(m[col][row]);
229 }
230}
231
233{
235 matrix.m[0][0] = m1.m[0][0] * m2.m[0][0]
236 + m1.m[1][0] * m2.m[0][1]
237 + m1.m[2][0] * m2.m[0][2];
238 matrix.m[0][1] = m1.m[0][1] * m2.m[0][0]
239 + m1.m[1][1] * m2.m[0][1]
240 + m1.m[2][1] * m2.m[0][2];
241 matrix.m[0][2] = m1.m[0][2] * m2.m[0][0]
242 + m1.m[1][2] * m2.m[0][1]
243 + m1.m[2][2] * m2.m[0][2];
244
245 matrix.m[1][0] = m1.m[0][0] * m2.m[1][0]
246 + m1.m[1][0] * m2.m[1][1]
247 + m1.m[2][0] * m2.m[1][2];
248 matrix.m[1][1] = m1.m[0][1] * m2.m[1][0]
249 + m1.m[1][1] * m2.m[1][1]
250 + m1.m[2][1] * m2.m[1][2];
251 matrix.m[1][2] = m1.m[0][2] * m2.m[1][0]
252 + m1.m[1][2] * m2.m[1][1]
253 + m1.m[2][2] * m2.m[1][2];
254
255 matrix.m[2][0] = m1.m[0][0] * m2.m[2][0]
256 + m1.m[1][0] * m2.m[2][1]
257 + m1.m[2][0] * m2.m[2][2];
258 matrix.m[2][1] = m1.m[0][1] * m2.m[2][0]
259 + m1.m[1][1] * m2.m[2][1]
260 + m1.m[2][1] * m2.m[2][2];
261 matrix.m[2][2] = m1.m[0][2] * m2.m[2][0]
262 + m1.m[1][2] * m2.m[2][1]
263 + m1.m[2][2] * m2.m[2][2];
264 return matrix;
265}
266
268{
269 float xin = point.x();
270 float yin = point.y();
271 float x = xin * matrix.m[0][0] +
272 yin * matrix.m[0][1] +
273 matrix.m[0][2];
274 float y = xin * matrix.m[1][0] +
275 yin * matrix.m[1][1] +
276 matrix.m[1][2];
277 float w = xin * matrix.m[2][0] +
278 yin * matrix.m[2][1] +
279 matrix.m[2][2];
280 if (w == 1.0f) {
281 return QPointF(float(x), float(y));
282 } else {
283 return QPointF(float(x / w), float(y / w));
284 }
285}
286
288{
289 float xin = point.x();
290 float yin = point.y();
291 float x = xin * matrix.m[0][0] +
292 yin * matrix.m[1][0] +
293 matrix.m[2][0];
294 float y = xin * matrix.m[0][1] +
295 yin * matrix.m[1][1] +
296 matrix.m[2][1];
297 float w = xin * matrix.m[0][2] +
298 yin * matrix.m[1][2] +
299 matrix.m[2][2];
300 if (w == 1.0f) {
301 return QPointF(float(x), float(y));
302 } else {
303 return QPointF(float(x / w), float(y / w));
304 }
305}
306
308{
309 QDebugStateSaver saver(dbg);
310 // Output in row-major order because it is more human-readable.
311 dbg.nospace() << "QOpenVGMatrix:(" << Qt::endl
312 << qSetFieldWidth(10)
313 << m(0, 0) << m(0, 1) << m(0, 2) << Qt::endl
314 << m(1, 0) << m(1, 1) << m(1, 2) << Qt::endl
315 << m(2, 0) << m(2, 1) << m(2, 2) << Qt::endl
316 << qSetFieldWidth(0) << ')';
317 return dbg;
318}
319
321{
322 for (int row = 0; row < 3; ++row)
323 for (int col = 0; col < 3; ++col)
324 stream << matrix(row, col);
325 return stream;
326}
327
328
330{
331 float x;
332 for (int row = 0; row < 4; ++row) {
333 for (int col = 0; col < 4; ++col) {
334 stream >> x;
335 matrix(row, col) = x;
336 }
337 }
338 return stream;
339}
340
341
\inmodule QtCore\reentrant
Definition qdatastream.h:46
\inmodule QtCore
\inmodule QtCore
QOpenVGMatrix & operator*=(const QOpenVGMatrix &other)
QOpenVGMatrix & operator-=(const QOpenVGMatrix &other)
QPointF map(const QPointF &point) const
const float & operator()(int row, int column) const
void fill(float value)
QOpenVGMatrix transposed() const
bool operator!=(const QOpenVGMatrix &other) const
QOpenVGMatrix & operator+=(const QOpenVGMatrix &other)
bool isIdentity() const
bool isAffine() const
QOpenVGMatrix & operator/=(float divisor)
void copyDataTo(float *values) const
bool operator==(const QOpenVGMatrix &other) const
\inmodule QtCore\reentrant
Definition qpoint.h:217
constexpr qreal x() const noexcept
Returns the x coordinate of this point.
Definition qpoint.h:343
constexpr qreal y() const noexcept
Returns the y coordinate of this point.
Definition qpoint.h:348
Combined button and popup list for selecting options.
QTextStream & endl(QTextStream &stream)
Writes '\n' to the stream and flushes the stream.
EGLStreamKHR stream
EGLOutputLayerEXT EGLint EGLAttrib value
[5]
GLenum GLsizei GLsizei GLint * values
[15]
GLint GLint GLint GLint GLint x
[0]
const GLfloat * m
GLfloat GLfloat GLfloat w
[0]
GLuint divisor
GLint y
GLenum GLenum GLsizei void GLsizei void * column
GLuint GLenum matrix
GLenum GLenum GLsizei void * row
GLuint64EXT * result
[6]
QOpenVGMatrix operator*(const QOpenVGMatrix &m1, const QOpenVGMatrix &m2)
QDebug operator<<(QDebug dbg, const QOpenVGMatrix &m)
QDataStream & operator>>(QDataStream &stream, QOpenVGMatrix &matrix)
#define Q_ASSERT(cond)
Definition qrandom.cpp:47
QTextStreamManipulator qSetFieldWidth(int width)
QSharedPointer< T > other(t)
[5]