| 1 | /**************************************************************************** |
| 2 | ** |
| 3 | ** Copyright (C) 2016 The Qt Company Ltd. |
| 4 | ** Contact: https://www.qt.io/licensing/ |
| 5 | ** |
| 6 | ** This file is part of the test suite of the Qt Toolkit. |
| 7 | ** |
| 8 | ** $QT_BEGIN_LICENSE:GPL-EXCEPT$ |
| 9 | ** Commercial License Usage |
| 10 | ** Licensees holding valid commercial Qt licenses may use this file in |
| 11 | ** accordance with the commercial license agreement provided with the |
| 12 | ** Software or, alternatively, in accordance with the terms contained in |
| 13 | ** a written agreement between you and The Qt Company. For licensing terms |
| 14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
| 15 | ** information use the contact form at https://www.qt.io/contact-us. |
| 16 | ** |
| 17 | ** GNU General Public License Usage |
| 18 | ** Alternatively, this file may be used under the terms of the GNU |
| 19 | ** General Public License version 3 as published by the Free Software |
| 20 | ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT |
| 21 | ** included in the packaging of this file. Please review the following |
| 22 | ** information to ensure the GNU General Public License requirements will |
| 23 | ** be met: https://www.gnu.org/licenses/gpl-3.0.html. |
| 24 | ** |
| 25 | ** $QT_END_LICENSE$ |
| 26 | ** |
| 27 | ****************************************************************************/ |
| 28 | |
| 29 | |
| 30 | #include <QtTest/QtTest> |
| 31 | #include <qpainterpath.h> |
| 32 | #include <qpolygon.h> |
| 33 | #include <qtransform.h> |
| 34 | #include <qdebug.h> |
| 35 | |
| 36 | class tst_QTransform : public QObject |
| 37 | { |
| 38 | Q_OBJECT |
| 39 | |
| 40 | private slots: |
| 41 | void mapRect_data(); |
| 42 | void mapToPolygon_data(); |
| 43 | void mapRect(); |
| 44 | void assignments(); |
| 45 | void mapToPolygon(); |
| 46 | void qhash(); |
| 47 | void translate(); |
| 48 | void scale(); |
| 49 | void matrix(); |
| 50 | void testOffset(); |
| 51 | void types(); |
| 52 | void types2_data(); |
| 53 | void types2(); |
| 54 | void scalarOps(); |
| 55 | void transform(); |
| 56 | void mapEmptyPath(); |
| 57 | void boundingRect(); |
| 58 | void controlPointRect(); |
| 59 | void inverted_data(); |
| 60 | void inverted(); |
| 61 | void projectivePathMapping(); |
| 62 | void mapInt(); |
| 63 | void mapPathWithPoint(); |
| 64 | void quadToQuad(); // QTBUG-21329 |
| 65 | |
| 66 | private: |
| 67 | void mapping_data(); |
| 68 | }; |
| 69 | |
| 70 | Q_DECLARE_METATYPE(QTransform) |
| 71 | Q_DECLARE_METATYPE(QTransform::TransformationType) |
| 72 | |
| 73 | void tst_QTransform::mapRect_data() |
| 74 | { |
| 75 | mapping_data(); |
| 76 | |
| 77 | // rotations that are not multiples of 90 degrees. mapRect returns the bounding rect here. |
| 78 | qreal deg = -45; |
| 79 | QTest::newRow( "rot 45 a" ) |
| 80 | << QTransform().rotate(deg) |
| 81 | << QRect( 0, 0, 10, 10 ) |
| 82 | << QPolygon( QRect( 0, -7, 14, 14 ) ); |
| 83 | QTest::newRow( "rot 45 b" ) |
| 84 | << QTransform().rotate(deg) |
| 85 | << QRect( 10, 20, 30, 40 ) |
| 86 | << QPolygon( QRect( 21, -14, 50, 49 ) ); |
| 87 | QTest::newRow( "rot 45 c" ) |
| 88 | << QTransform().rotate(deg).scale(10, 10) |
| 89 | << QRect( 0, 0, 10, 10 ) |
| 90 | << QPolygon( QRect( 0, -71, 141, 142 ) ); |
| 91 | QTest::newRow( "rot 45 d" ) |
| 92 | << QTransform().rotate(deg).scale(10, 10) |
| 93 | << QRect( 10, 20, 30, 40 ) |
| 94 | << QPolygon( QRect( 212, -141, 495, 495 ) ); |
| 95 | |
| 96 | deg = 45; |
| 97 | QTest::newRow( "rot -45 a" ) |
| 98 | << QTransform().rotate(deg) |
| 99 | << QRect( 0, 0, 10, 10 ) |
| 100 | << QPolygon( QRect( -7, 0, 14, 14 ) ); |
| 101 | QTest::newRow( "rot -45 b" ) |
| 102 | << QTransform().rotate(deg) |
| 103 | << QRect( 10, 20, 30, 40 ) |
| 104 | << QPolygon( QRect( -35, 21, 49, 50 ) ); |
| 105 | QTest::newRow( "rot -45 c" ) |
| 106 | << QTransform().rotate(deg).scale(10, 10) |
| 107 | << QRect( 0, 0, 10, 10 ) |
| 108 | << QPolygon( QRect( -71, 0, 142, 141 ) ); |
| 109 | QTest::newRow( "rot -45 d" ) |
| 110 | << QTransform().rotate(deg).scale(10, 10) |
| 111 | << QRect( 10, 20, 30, 40 ) |
| 112 | << QPolygon( QRect( -354, 212, 495, 495 ) ); |
| 113 | } |
| 114 | |
| 115 | void tst_QTransform::mapToPolygon_data() |
| 116 | { |
| 117 | mapping_data(); |
| 118 | } |
| 119 | |
| 120 | void tst_QTransform::mapping_data() |
| 121 | { |
| 122 | //create the testtable instance and define the elements |
| 123 | QTest::addColumn<QTransform>("matrix"); |
| 124 | QTest::addColumn<QRect>("src"); |
| 125 | QTest::addColumn<QPolygon>("res"); |
| 126 | |
| 127 | //next we fill it with data |
| 128 | |
| 129 | // identity |
| 130 | QTest::newRow( "identity" ) |
| 131 | << QTransform() |
| 132 | << QRect( 10, 20, 30, 40 ) |
| 133 | << QPolygon( QRect( 10, 20, 30, 40 ) ); |
| 134 | // scaling |
| 135 | QTest::newRow( "scale 0" ) |
| 136 | << QTransform().scale(2, 2) |
| 137 | << QRect( 10, 20, 30, 40 ) |
| 138 | << QPolygon( QRect( 20, 40, 60, 80 ) ); |
| 139 | QTest::newRow( "scale 1" ) |
| 140 | << QTransform().scale(10, 10) |
| 141 | << QRect( 10, 20, 30, 40 ) |
| 142 | << QPolygon( QRect( 100, 200, 300, 400 ) ); |
| 143 | // mirroring |
| 144 | QTest::newRow( "mirror 0" ) |
| 145 | << QTransform().scale(-1, 1) |
| 146 | << QRect( 10, 20, 30, 40 ) |
| 147 | << QPolygon( QRect( -40, 20, 30, 40 ) ); |
| 148 | QTest::newRow( "mirror 1" ) |
| 149 | << QTransform().scale(1, -1) |
| 150 | << QRect( 10, 20, 30, 40 ) |
| 151 | << QPolygon( QRect( 10, -60, 30, 40 ) ); |
| 152 | QTest::newRow( "mirror 2" ) |
| 153 | << QTransform().scale(-1, -1) |
| 154 | << QRect( 10, 20, 30, 40 ) |
| 155 | << QPolygon( QRect( -40, -60, 30, 40 ) ); |
| 156 | QTest::newRow( "mirror 3" ) |
| 157 | << QTransform().scale(-2, -2) |
| 158 | << QRect( 10, 20, 30, 40 ) |
| 159 | << QPolygon( QRect( -80, -120, 60, 80 ) ); |
| 160 | QTest::newRow( "mirror 4" ) |
| 161 | << QTransform().scale(-10, -10) |
| 162 | << QRect( 10, 20, 30, 40 ) |
| 163 | << QPolygon( QRect( -400, -600, 300, 400 ) ); |
| 164 | QTest::newRow( "mirror 5" ) |
| 165 | << QTransform().scale(-1, 1) |
| 166 | << QRect( 0, 0, 30, 40 ) |
| 167 | << QPolygon( QRect( -30, 0, 30, 40 ) ); |
| 168 | QTest::newRow( "mirror 6" ) |
| 169 | << QTransform().scale(1, -1) |
| 170 | << QRect( 0, 0, 30, 40 ) |
| 171 | << QPolygon( QRect( 0, -40, 30, 40 ) ); |
| 172 | QTest::newRow( "mirror 7" ) |
| 173 | << QTransform().scale(-1, -1) |
| 174 | << QRect( 0, 0, 30, 40 ) |
| 175 | << QPolygon( QRect( -30, -40, 30, 40 ) ); |
| 176 | QTest::newRow( "mirror 8" ) |
| 177 | << QTransform().scale(-2, -2) |
| 178 | << QRect( 0, 0, 30, 40 ) |
| 179 | << QPolygon( QRect( -60, -80, 60, 80 ) ); |
| 180 | QTest::newRow( "mirror 9" ) |
| 181 | << QTransform().scale(-10, -10) << QRect( 0, 0, 30, 40 ) |
| 182 | << QPolygon( QRect( -300, -400, 300, 400 ) ); |
| 183 | |
| 184 | // rotations |
| 185 | float deg = 0.; |
| 186 | QTest::newRow( "rot 0 a" ) |
| 187 | << QTransform().rotate(deg) |
| 188 | << QRect( 0, 0, 30, 40 ) |
| 189 | << QPolygon ( QRect( 0, 0, 30, 40 ) ); |
| 190 | deg = 0.00001f; |
| 191 | QTest::newRow( "rot 0 b" ) |
| 192 | << QTransform().rotate(deg) |
| 193 | << QRect( 0, 0, 30, 40 ) |
| 194 | << QPolygon ( QRect( 0, 0, 30, 40 ) ); |
| 195 | deg = 0.; |
| 196 | QTest::newRow( "rot 0 c" ) |
| 197 | << QTransform().rotate(deg) |
| 198 | << QRect( 10, 20, 30, 40 ) |
| 199 | << QPolygon ( QRect( 10, 20, 30, 40 ) ); |
| 200 | deg = 0.00001f; |
| 201 | QTest::newRow( "rot 0 d" ) |
| 202 | << QTransform().rotate(deg) |
| 203 | << QRect( 10, 20, 30, 40 ) |
| 204 | << QPolygon ( QRect( 10, 20, 30, 40 ) ); |
| 205 | |
| 206 | // rotations |
| 207 | deg = -90.f; |
| 208 | QTest::newRow( "rotscale 90 a" ) |
| 209 | << QTransform().rotate(deg).scale(10, 10) |
| 210 | << QRect( 0, 0, 30, 40 ) |
| 211 | << QPolygon( QRect( 0, -300, 400, 300 ) ); |
| 212 | deg = -90.00001f; |
| 213 | QTest::newRow( "rotscale 90 b" ) |
| 214 | << QTransform().rotate(deg).scale(10, 10) |
| 215 | << QRect( 0, 0, 30, 40 ) |
| 216 | << QPolygon( QRect( 0, -300, 400, 300 ) ); |
| 217 | deg = -90.f; |
| 218 | QTest::newRow( "rotscale 90 c" ) |
| 219 | << QTransform().rotate(deg).scale(10, 10) |
| 220 | << QRect( 10, 20, 30, 40 ) |
| 221 | << QPolygon( QRect( 200, -400, 400, 300 ) ); |
| 222 | deg = -90.00001f; |
| 223 | QTest::newRow( "rotscale 90 d" ) |
| 224 | << QTransform().rotate(deg).scale(10, 10) |
| 225 | << QRect( 10, 20, 30, 40 ) |
| 226 | << QPolygon( QRect( 200, -400, 400, 300 ) ); |
| 227 | |
| 228 | deg = 180.f; |
| 229 | QTest::newRow( "rotscale 180 a" ) |
| 230 | << QTransform().rotate(deg).scale(10, 10) |
| 231 | << QRect( 0, 0, 30, 40 ) |
| 232 | << QPolygon( QRect( -300, -400, 300, 400 ) ); |
| 233 | deg = 180.000001f; |
| 234 | QTest::newRow( "rotscale 180 b" ) |
| 235 | << QTransform().rotate(deg).scale(10, 10) |
| 236 | << QRect( 0, 0, 30, 40 ) |
| 237 | << QPolygon( QRect( -300, -400, 300, 400 ) ); |
| 238 | deg = 180.f; |
| 239 | QTest::newRow( "rotscale 180 c" ) |
| 240 | << QTransform().rotate(deg).scale(10, 10) |
| 241 | << QRect( 10, 20, 30, 40 ) |
| 242 | << QPolygon( QRect( -400, -600, 300, 400 ) ); |
| 243 | deg = 180.000001f; |
| 244 | QTest::newRow( "rotscale 180 d" ) |
| 245 | << QTransform().rotate(deg).scale(10, 10) |
| 246 | << QRect( 10, 20, 30, 40 ) |
| 247 | << QPolygon( QRect( -400, -600, 300, 400 ) ); |
| 248 | |
| 249 | deg = -270.f; |
| 250 | QTest::newRow( "rotscale 270 a" ) |
| 251 | << QTransform().rotate(deg).scale(10, 10) |
| 252 | << QRect( 0, 0, 30, 40 ) |
| 253 | << QPolygon( QRect( -400, 0, 400, 300 ) ); |
| 254 | deg = -270.0000001f; |
| 255 | QTest::newRow( "rotscale 270 b" ) |
| 256 | << QTransform().rotate(deg).scale(10, 10) |
| 257 | << QRect( 0, 0, 30, 40 ) |
| 258 | << QPolygon( QRect( -400, 0, 400, 300 ) ); |
| 259 | deg = -270.f; |
| 260 | QTest::newRow( "rotscale 270 c" ) |
| 261 | << QTransform().rotate(deg).scale(10, 10) |
| 262 | << QRect( 10, 20, 30, 40 ) |
| 263 | << QPolygon( QRect( -600, 100, 400, 300 ) ); |
| 264 | deg = -270.000001f; |
| 265 | QTest::newRow( "rotscale 270 d" ) |
| 266 | << QTransform().rotate(deg).scale(10, 10) |
| 267 | << QRect( 10, 20, 30, 40 ) |
| 268 | << QPolygon( QRect( -600, 100, 400, 300 ) ); |
| 269 | } |
| 270 | |
| 271 | void tst_QTransform::mapRect() |
| 272 | { |
| 273 | QFETCH( QTransform, matrix ); |
| 274 | QFETCH( QRect, src ); |
| 275 | QFETCH( QPolygon, res ); |
| 276 | QRect mapped = matrix.mapRect(src); |
| 277 | QCOMPARE( mapped, res.boundingRect().adjusted(0, 0, -1, -1) ); |
| 278 | |
| 279 | QRectF r = matrix.mapRect(QRectF(src)); |
| 280 | QRect ir(r.topLeft().toPoint(), r.bottomRight().toPoint() - QPoint(1, 1)); |
| 281 | QCOMPARE( mapped, ir ); |
| 282 | } |
| 283 | |
| 284 | void tst_QTransform::assignments() |
| 285 | { |
| 286 | QTransform m; |
| 287 | m.scale(2, 3); |
| 288 | m.rotate(45); |
| 289 | m.shear(4, 5); |
| 290 | |
| 291 | QTransform c1(m); |
| 292 | |
| 293 | QCOMPARE(m.m11(), c1.m11()); |
| 294 | QCOMPARE(m.m12(), c1.m12()); |
| 295 | QCOMPARE(m.m21(), c1.m21()); |
| 296 | QCOMPARE(m.m22(), c1.m22()); |
| 297 | QCOMPARE(m.dx(), c1.dx()); |
| 298 | QCOMPARE(m.dy(), c1.dy()); |
| 299 | |
| 300 | QTransform c2 = m; |
| 301 | QCOMPARE(m.m11(), c2.m11()); |
| 302 | QCOMPARE(m.m12(), c2.m12()); |
| 303 | QCOMPARE(m.m21(), c2.m21()); |
| 304 | QCOMPARE(m.m22(), c2.m22()); |
| 305 | QCOMPARE(m.dx(), c2.dx()); |
| 306 | QCOMPARE(m.dy(), c2.dy()); |
| 307 | } |
| 308 | |
| 309 | |
| 310 | void tst_QTransform::mapToPolygon() |
| 311 | { |
| 312 | QFETCH( QTransform, matrix ); |
| 313 | QFETCH( QRect, src ); |
| 314 | QFETCH( QPolygon, res ); |
| 315 | |
| 316 | QPolygon poly = matrix.mapToPolygon(src); |
| 317 | |
| 318 | // don't care about starting point |
| 319 | bool equal = false; |
| 320 | for (int i = 0; i < poly.size(); ++i) { |
| 321 | QPolygon rot; |
| 322 | for (int j = i; j < poly.size(); ++j) |
| 323 | rot << poly[j]; |
| 324 | for (int j = 0; j < i; ++j) |
| 325 | rot << poly[j]; |
| 326 | if (rot == res) |
| 327 | equal = true; |
| 328 | } |
| 329 | |
| 330 | QVERIFY(equal); |
| 331 | } |
| 332 | |
| 333 | void tst_QTransform::qhash() |
| 334 | { |
| 335 | QTransform t1; |
| 336 | t1.shear(3.0, 2.0); |
| 337 | t1.rotate(44); |
| 338 | |
| 339 | QTransform t2 = t1; |
| 340 | |
| 341 | // not really much to test here, so just the bare minimum: |
| 342 | QCOMPARE(qHash(t1), qHash(t2)); |
| 343 | } |
| 344 | |
| 345 | |
| 346 | void tst_QTransform::translate() |
| 347 | { |
| 348 | QTransform m( 1, 2, 3, 4, 5, 6 ); |
| 349 | QTransform res2( m ); |
| 350 | QTransform res( 1, 2, 3, 4, 75, 106 ); |
| 351 | m.translate( 10, 20 ); |
| 352 | QVERIFY( m == res ); |
| 353 | m.translate( -10, -20 ); |
| 354 | QVERIFY( m == res2 ); |
| 355 | QVERIFY( QTransform::fromTranslate( 0, 0 ).type() == QTransform::TxNone ); |
| 356 | QVERIFY( QTransform::fromTranslate( 10, 0 ).type() == QTransform::TxTranslate ); |
| 357 | QVERIFY( QTransform::fromTranslate( -1, 5 ) == QTransform().translate( -1, 5 )); |
| 358 | QVERIFY( QTransform::fromTranslate( 0, 0 ) == QTransform()); |
| 359 | } |
| 360 | |
| 361 | void tst_QTransform::scale() |
| 362 | { |
| 363 | QTransform m( 1, 2, 3, 4, 5, 6 ); |
| 364 | QTransform res2( m ); |
| 365 | QTransform res( 10, 20, 60, 80, 5, 6 ); |
| 366 | m.scale( 10, 20 ); |
| 367 | QVERIFY( m == res ); |
| 368 | m.scale( 1./10., 1./20. ); |
| 369 | QVERIFY( m == res2 ); |
| 370 | QVERIFY( QTransform::fromScale( 1, 1 ).type() == QTransform::TxNone ); |
| 371 | QVERIFY( QTransform::fromScale( 2, 4 ).type() == QTransform::TxScale ); |
| 372 | QVERIFY( QTransform::fromScale( 2, 4 ) == QTransform().scale( 2, 4 )); |
| 373 | QVERIFY( QTransform::fromScale( 1, 1 ) == QTransform()); |
| 374 | } |
| 375 | |
| 376 | #if QT_DEPRECATED_SINCE(5, 15) |
| 377 | QT_WARNING_PUSH |
| 378 | QT_WARNING_DISABLE_DEPRECATED |
| 379 | void tst_QTransform::matrix() |
| 380 | { |
| 381 | QMatrix mat1; |
| 382 | mat1.scale(0.3, 0.7); |
| 383 | mat1.translate(53.3, 94.4); |
| 384 | mat1.rotate(45); |
| 385 | |
| 386 | QMatrix mat2; |
| 387 | mat2.rotate(33); |
| 388 | mat2.scale(0.6, 0.6); |
| 389 | mat2.translate(13.333, 7.777); |
| 390 | |
| 391 | QTransform tran1(mat1); |
| 392 | QTransform tran2(mat2); |
| 393 | QTransform dummy; |
| 394 | dummy.setMatrix(mat1.m11(), mat1.m12(), 0, |
| 395 | mat1.m21(), mat1.m22(), 0, |
| 396 | mat1.dx(), mat1.dy(), 1); |
| 397 | |
| 398 | QCOMPARE(tran1, dummy); |
| 399 | QCOMPARE(tran1.inverted(), dummy.inverted()); |
| 400 | QCOMPARE(tran1.inverted(), QTransform(mat1.inverted())); |
| 401 | QCOMPARE(tran2.inverted(), QTransform(mat2.inverted())); |
| 402 | |
| 403 | QMatrix mat3 = mat1 * mat2; |
| 404 | QTransform tran3 = tran1 * tran2; |
| 405 | QCOMPARE(QTransform(mat3), tran3); |
| 406 | |
| 407 | /* QMatrix::operator==() doesn't use qFuzzyCompare(), which |
| 408 | * on win32-g++ results in a failure. So we work around it by |
| 409 | * calling QTranform::operator==(), which performs a fuzzy compare. */ |
| 410 | QCOMPARE(QTransform(mat3), QTransform(tran3.toAffine())); |
| 411 | |
| 412 | QTransform tranInv = tran1.inverted(); |
| 413 | QMatrix matInv = mat1.inverted(); |
| 414 | |
| 415 | QRect rect(43, 70, 200, 200); |
| 416 | QPoint pt(43, 66); |
| 417 | QCOMPARE(tranInv.mapRect(rect), matInv.mapRect(rect)); |
| 418 | QCOMPARE(tranInv.map(pt), matInv.map(pt)); |
| 419 | |
| 420 | QPainterPath path; |
| 421 | path.moveTo(55, 60); |
| 422 | path.lineTo(110, 110); |
| 423 | path.quadTo(220, 50, 10, 20); |
| 424 | path.closeSubpath(); |
| 425 | QCOMPARE(tranInv.map(path), matInv.map(path)); |
| 426 | } |
| 427 | |
| 428 | void tst_QTransform::testOffset() |
| 429 | { |
| 430 | QTransform trans; |
| 431 | const QMatrix &aff = trans.toAffine(); |
| 432 | QCOMPARE((void*)(&aff), (void*)(&trans)); |
| 433 | } |
| 434 | QT_WARNING_POP |
| 435 | #endif |
| 436 | |
| 437 | void tst_QTransform::types() |
| 438 | { |
| 439 | QTransform m1; |
| 440 | QCOMPARE(m1.type(), QTransform::TxNone); |
| 441 | |
| 442 | m1.translate(1.0f, 0.0f); |
| 443 | QCOMPARE(m1.type(), QTransform::TxTranslate); |
| 444 | QCOMPARE(m1.inverted().type(), QTransform::TxTranslate); |
| 445 | |
| 446 | m1.scale(1.0f, 2.0f); |
| 447 | QCOMPARE(m1.type(), QTransform::TxScale); |
| 448 | QCOMPARE(m1.inverted().type(), QTransform::TxScale); |
| 449 | |
| 450 | m1.rotate(45.0f); |
| 451 | // Rotation after non-uniform scaling -> shearing. Uniform scale + rotate tested below. |
| 452 | QCOMPARE(m1.type(), QTransform::TxShear); |
| 453 | QCOMPARE(m1.inverted().type(), QTransform::TxShear); |
| 454 | |
| 455 | m1.shear(0.5f, 0.25f); |
| 456 | QCOMPARE(m1.type(), QTransform::TxShear); |
| 457 | QCOMPARE(m1.inverted().type(), QTransform::TxShear); |
| 458 | |
| 459 | m1.rotate(45.0f, Qt::XAxis); |
| 460 | QCOMPARE(m1.type(), QTransform::TxProject); |
| 461 | m1.shear(0.5f, 0.25f); |
| 462 | QCOMPARE(m1.type(), QTransform::TxProject); |
| 463 | m1.rotate(45.0f); |
| 464 | QCOMPARE(m1.type(), QTransform::TxProject); |
| 465 | m1.scale(1.0f, 2.0f); |
| 466 | QCOMPARE(m1.type(), QTransform::TxProject); |
| 467 | m1.translate(1.0f, 0.0f); |
| 468 | QCOMPARE(m1.type(), QTransform::TxProject); |
| 469 | |
| 470 | QTransform m2(1.0f, 0.0f, 0.0f, |
| 471 | 0.0f, 1.0f, 0.0f, |
| 472 | -1.0f, -1.0f, 1.0f); |
| 473 | |
| 474 | QCOMPARE(m2.type(), QTransform::TxTranslate); |
| 475 | QCOMPARE((m1 * m2).type(), QTransform::TxProject); |
| 476 | |
| 477 | m1 *= QTransform(); |
| 478 | QCOMPARE(m1.type(), QTransform::TxProject); |
| 479 | |
| 480 | m1 *= QTransform(1.0f, 0.0f, 0.0f, |
| 481 | 0.0f, 1.0f, 0.0f, |
| 482 | 1.0f, 0.0f, 1.0f); |
| 483 | QCOMPARE(m1.type(), QTransform::TxProject); |
| 484 | |
| 485 | m2.reset(); |
| 486 | QCOMPARE(m2.type(), QTransform::TxNone); |
| 487 | |
| 488 | m2.setMatrix(1.0f, 0.0f, 0.0f, |
| 489 | 0.0f, 1.0f, 0.0f, |
| 490 | 0.0f, 0.0f, 1.0f); |
| 491 | QCOMPARE(m2.type(), QTransform::TxNone); |
| 492 | |
| 493 | m2 *= QTransform(); |
| 494 | QCOMPARE(m2.type(), QTransform::TxNone); |
| 495 | |
| 496 | m2.setMatrix(2.0f, 0.0f, 0.0f, |
| 497 | 0.0f, 1.0f, 0.0f, |
| 498 | 0.0f, 0.0f, 1.0f); |
| 499 | QCOMPARE(m2.type(), QTransform::TxScale); |
| 500 | m2 *= QTransform(); |
| 501 | QCOMPARE(m2.type(), QTransform::TxScale); |
| 502 | |
| 503 | m2.setMatrix(0.0f, 1.0f, 0.0f, |
| 504 | 1.0f, 0.0f, 0.0f, |
| 505 | 0.0f, 1.0f, 1.0f); |
| 506 | QCOMPARE(m2.type(), QTransform::TxRotate); |
| 507 | m2 *= QTransform(); |
| 508 | QCOMPARE(m2.type(), QTransform::TxRotate); |
| 509 | |
| 510 | m2.setMatrix(1.0f, 0.0f, 0.5f, |
| 511 | 0.0f, 1.0f, 0.0f, |
| 512 | 0.0f, 0.0f, 1.0f); |
| 513 | QCOMPARE(m2.type(), QTransform::TxProject); |
| 514 | m2 *= QTransform(); |
| 515 | QCOMPARE(m2.type(), QTransform::TxProject); |
| 516 | |
| 517 | m2.setMatrix(1.0f, 1.0f, 0.0f, |
| 518 | 1.0f, 0.0f, 0.0f, |
| 519 | 0.0f, 1.0f, 1.0f); |
| 520 | QCOMPARE(m2.type(), QTransform::TxShear); |
| 521 | |
| 522 | m2 *= m2.inverted(); |
| 523 | QCOMPARE(m2.type(), QTransform::TxNone); |
| 524 | |
| 525 | m2.translate(5.0f, 5.0f); |
| 526 | m2.rotate(45.0f); |
| 527 | m2.rotate(-45.0f); |
| 528 | QCOMPARE(m2.type(), QTransform::TxTranslate); |
| 529 | |
| 530 | m2.scale(2.0f, 3.0f); |
| 531 | m2.shear(1.0f, 0.0f); |
| 532 | m2.shear(-1.0f, 0.0f); |
| 533 | QCOMPARE(m2.type(), QTransform::TxScale); |
| 534 | |
| 535 | m2 *= QTransform(1.0f, 1.0f, 0.0f, |
| 536 | 0.0f, 1.0f, 0.0f, |
| 537 | 0.0f, 0.0f, 1.0f); |
| 538 | QCOMPARE(m2.type(), QTransform::TxShear); |
| 539 | |
| 540 | m2 *= QTransform(1.0f, 0.0f, 0.0f, |
| 541 | 0.0f, 1.0f, 0.0f, |
| 542 | 1.0f, 0.0f, 1.0f); |
| 543 | QCOMPARE(m2.type(), QTransform::TxShear); |
| 544 | |
| 545 | QTransform m3(1.8f, 0.0f, 0.0f, |
| 546 | 0.0f, 1.8f, 0.0f, |
| 547 | 0.0f, 0.0f, 1.0f); |
| 548 | |
| 549 | QCOMPARE(m3.type(), QTransform::TxScale); |
| 550 | m3.translate(5.0f, 5.0f); |
| 551 | QCOMPARE(m3.type(), QTransform::TxScale); |
| 552 | QCOMPARE(m3.inverted().type(), QTransform::TxScale); |
| 553 | |
| 554 | m3.setMatrix(1.0f, 0.0f, 0.0f, |
| 555 | 0.0f, 1.0f, 0.0f, |
| 556 | 0.0f, 0.0f, 2.0f); |
| 557 | QCOMPARE(m3.type(), QTransform::TxProject); |
| 558 | |
| 559 | m3.setMatrix(0.0f, 2.0f, 0.0f, |
| 560 | 1.0f, 0.0f, 0.0f, |
| 561 | 0.0f, 0.0f, 2.0f); |
| 562 | QCOMPARE(m3.type(), QTransform::TxProject); |
| 563 | |
| 564 | QTransform m4; |
| 565 | m4.scale(5, 5); |
| 566 | m4.translate(4, 2); |
| 567 | m4.rotate(45); |
| 568 | |
| 569 | QCOMPARE(m4.type(), QTransform::TxRotate); |
| 570 | |
| 571 | QTransform m5; |
| 572 | m5.scale(5, 5); |
| 573 | m5 = m5.adjoint() / m5.determinant(); |
| 574 | QCOMPARE(m5.type(), QTransform::TxScale); |
| 575 | } |
| 576 | |
| 577 | void tst_QTransform::types2_data() |
| 578 | { |
| 579 | QTest::addColumn<QTransform>("t1"); |
| 580 | QTest::addColumn<QTransform::TransformationType>("type"); |
| 581 | |
| 582 | QTest::newRow( "identity" ) << QTransform() << QTransform::TxNone; |
| 583 | QTest::newRow( "translate" ) << QTransform().translate(10, -0.1) << QTransform::TxTranslate; |
| 584 | QTest::newRow( "scale" ) << QTransform().scale(10, -0.1) << QTransform::TxScale; |
| 585 | QTest::newRow( "rotate" ) << QTransform().rotate(10) << QTransform::TxRotate; |
| 586 | QTest::newRow( "shear" ) << QTransform().shear(10, -0.1) << QTransform::TxShear; |
| 587 | QTest::newRow( "project" ) << QTransform().rotate(10, Qt::XAxis) << QTransform::TxProject; |
| 588 | QTest::newRow( "combined" ) << QTransform().translate(10, -0.1).scale(10, -0.1).rotate(10, Qt::YAxis) << QTransform::TxProject; |
| 589 | } |
| 590 | |
| 591 | void tst_QTransform::types2() |
| 592 | { |
| 593 | #define CHECKTXTYPE(func) { QTransform t2(func); \ |
| 594 | QTransform t3(t2.m11(), t2.m12(), t2.m13(), t2.m21(), t2.m22(), t2.m23(), t2.m31(), t2.m32(), t2.m33()); \ |
| 595 | QVERIFY2(t3.type() == t2.type(), #func); \ |
| 596 | } |
| 597 | |
| 598 | QFETCH( QTransform, t1 ); |
| 599 | QFETCH( QTransform::TransformationType, type ); |
| 600 | |
| 601 | Q_ASSERT(t1.type() == type); |
| 602 | |
| 603 | CHECKTXTYPE(t1.adjoint()); |
| 604 | CHECKTXTYPE(t1.inverted()); |
| 605 | CHECKTXTYPE(t1.transposed()); |
| 606 | |
| 607 | #undef CHECKTXTYPE |
| 608 | } |
| 609 | |
| 610 | void tst_QTransform::scalarOps() |
| 611 | { |
| 612 | QTransform t; |
| 613 | QCOMPARE(t.m11(), 1.); |
| 614 | QCOMPARE(t.m33(), 1.); |
| 615 | QCOMPARE(t.m21(), 0.); |
| 616 | |
| 617 | t = QTransform() + 3; |
| 618 | QCOMPARE(t.m11(), 4.); |
| 619 | QCOMPARE(t.m33(), 4.); |
| 620 | QCOMPARE(t.m21(), 3.); |
| 621 | |
| 622 | t = t - 3; |
| 623 | QCOMPARE(t.m11(), 1.); |
| 624 | QCOMPARE(t.m33(), 1.); |
| 625 | QCOMPARE(t.m21(), 0.); |
| 626 | QCOMPARE(t.isIdentity(), true); |
| 627 | |
| 628 | t += 3; |
| 629 | t = t * 2; |
| 630 | QCOMPARE(t.m11(), 8.); |
| 631 | QCOMPARE(t.m33(), 8.); |
| 632 | QCOMPARE(t.m21(), 6.); |
| 633 | } |
| 634 | |
| 635 | void tst_QTransform::transform() |
| 636 | { |
| 637 | QTransform t; |
| 638 | t.rotate(30, Qt::YAxis); |
| 639 | t.translate(15, 10); |
| 640 | t.scale(2, 2); |
| 641 | t.rotate(30); |
| 642 | t.shear(0.5, 0.5); |
| 643 | |
| 644 | QTransform a, b, c, d, e; |
| 645 | a.rotate(30, Qt::YAxis); |
| 646 | b.translate(15, 10); |
| 647 | c.scale(2, 2); |
| 648 | d.rotate(30); |
| 649 | e.shear(0.5, 0.5); |
| 650 | |
| 651 | QVERIFY(qFuzzyCompare(t, e * d * c * b * a)); |
| 652 | } |
| 653 | |
| 654 | void tst_QTransform::mapEmptyPath() |
| 655 | { |
| 656 | QPainterPath path; |
| 657 | path.moveTo(10, 10); |
| 658 | path.lineTo(10, 10); |
| 659 | QCOMPARE(QTransform().map(path), path); |
| 660 | } |
| 661 | |
| 662 | void tst_QTransform::boundingRect() |
| 663 | { |
| 664 | QPainterPath path; |
| 665 | path.moveTo(10, 10); |
| 666 | path.lineTo(10, 10); |
| 667 | QCOMPARE(path.boundingRect(), QRectF(10, 10, 0, 0)); |
| 668 | } |
| 669 | |
| 670 | void tst_QTransform::controlPointRect() |
| 671 | { |
| 672 | QPainterPath path; |
| 673 | path.moveTo(10, 10); |
| 674 | path.lineTo(10, 10); |
| 675 | QCOMPARE(path.controlPointRect(), QRectF(10, 10, 0, 0)); |
| 676 | } |
| 677 | |
| 678 | void tst_QTransform::inverted_data() |
| 679 | { |
| 680 | QTest::addColumn<QTransform>("matrix"); |
| 681 | |
| 682 | QTest::newRow("identity") |
| 683 | << QTransform(); |
| 684 | |
| 685 | QTest::newRow("TxTranslate") |
| 686 | << QTransform().translate(200, 10); |
| 687 | |
| 688 | QTest::newRow("TxScale") |
| 689 | << QTransform().scale(5, 2); |
| 690 | |
| 691 | QTest::newRow("TxTranslate TxScale") |
| 692 | << QTransform().translate(100, -10).scale(40, 2); |
| 693 | |
| 694 | QTest::newRow("TxScale TxTranslate") |
| 695 | << QTransform().scale(40, 2).translate(100, -10); |
| 696 | |
| 697 | QTest::newRow("TxRotate") |
| 698 | << QTransform().rotate(40, Qt::ZAxis); |
| 699 | |
| 700 | QTest::newRow("TxRotate TxScale") |
| 701 | << QTransform().rotate(60, Qt::ZAxis).scale(2, 0.25); |
| 702 | |
| 703 | QTest::newRow("TxScale TxRotate") |
| 704 | << QTransform().scale(2, 0.25).rotate(30, Qt::ZAxis); |
| 705 | |
| 706 | QTest::newRow("TxRotate TxScale TxTranslate") |
| 707 | << QTransform().rotate(60, Qt::ZAxis).scale(2, 0.25).translate(200, -3000); |
| 708 | |
| 709 | QTest::newRow("TxRotate TxTranslate TxScale") |
| 710 | << QTransform().rotate(60, Qt::ZAxis).translate(200, -3000).scale(19, 77); |
| 711 | |
| 712 | QTest::newRow("TxShear") |
| 713 | << QTransform().shear(10, 10); |
| 714 | |
| 715 | QTest::newRow("TxShear TxRotate") |
| 716 | << QTransform().shear(10, 10).rotate(45, Qt::ZAxis); |
| 717 | |
| 718 | QTest::newRow("TxShear TxRotate TxScale") |
| 719 | << QTransform().shear(10, 10).rotate(45, Qt::ZAxis).scale(19, 81); |
| 720 | |
| 721 | QTest::newRow("TxTranslate TxShear TxRotate TxScale") |
| 722 | << QTransform().translate(150, -1).shear(10, 10).rotate(45, Qt::ZAxis).scale(19, 81); |
| 723 | |
| 724 | const qreal s = 500000; |
| 725 | |
| 726 | QTransform big; |
| 727 | big.scale(s, s); |
| 728 | |
| 729 | QTest::newRow("big") << big; |
| 730 | |
| 731 | QTransform smallTransform; |
| 732 | smallTransform.scale(1/s, 1/s); |
| 733 | |
| 734 | QTest::newRow("small") << smallTransform; |
| 735 | } |
| 736 | |
| 737 | void tst_QTransform::inverted() |
| 738 | { |
| 739 | if (sizeof(qreal) != sizeof(double)) |
| 740 | QSKIP("precision error if qreal is not double"); |
| 741 | |
| 742 | QFETCH(QTransform, matrix); |
| 743 | |
| 744 | const QTransform inverted = matrix.inverted(); |
| 745 | |
| 746 | QCOMPARE(matrix.isIdentity(), inverted.isIdentity()); |
| 747 | QCOMPARE(matrix.type(), inverted.type()); |
| 748 | |
| 749 | QVERIFY((matrix * inverted).isIdentity()); |
| 750 | QVERIFY((inverted * matrix).isIdentity()); |
| 751 | } |
| 752 | |
| 753 | void tst_QTransform::projectivePathMapping() |
| 754 | { |
| 755 | QPainterPath path; |
| 756 | path.addRect(-50, -50, 100, 100); |
| 757 | |
| 758 | const QRectF view(0, 0, 1024, 1024); |
| 759 | |
| 760 | QVERIFY(view.intersects(path.boundingRect())); |
| 761 | |
| 762 | for (int i = 0; i < 85; i += 5) { |
| 763 | QTransform transform; |
| 764 | transform.translate(512, 512); |
| 765 | transform.rotate(i, Qt::YAxis); |
| 766 | |
| 767 | const QPainterPath mapped = transform.map(path); |
| 768 | |
| 769 | QVERIFY(view.intersects(mapped.boundingRect())); |
| 770 | QVERIFY(transform.inverted().mapRect(view).intersects(path.boundingRect())); |
| 771 | } |
| 772 | } |
| 773 | |
| 774 | void tst_QTransform::mapInt() |
| 775 | { |
| 776 | int x = 0; |
| 777 | int y = 0; |
| 778 | |
| 779 | QTransform::fromTranslate(10, 10).map(x, y, &x, &y); |
| 780 | |
| 781 | QCOMPARE(x, 10); |
| 782 | QCOMPARE(y, 10); |
| 783 | } |
| 784 | |
| 785 | void tst_QTransform::mapPathWithPoint() |
| 786 | { |
| 787 | QPainterPath p(QPointF(10, 10)); |
| 788 | p = QTransform::fromTranslate(10, 10).map(p); |
| 789 | QCOMPARE(p.currentPosition(), QPointF(20, 20)); |
| 790 | } |
| 791 | |
| 792 | void tst_QTransform::quadToQuad() // QTBUG-21329 |
| 793 | { |
| 794 | QTransform result; |
| 795 | QVERIFY(QTransform::quadToQuad(QRectF(0, 0, 1, 1), QRectF(0, 0, 1, 1), result)); |
| 796 | QPolygonF trapezoid({{0, 0}, {10, 0}, {11, 11}, {0, 10}}); |
| 797 | QVERIFY(QTransform::quadToQuad(trapezoid, trapezoid.boundingRect(), result)); |
| 798 | } |
| 799 | |
| 800 | QTEST_APPLESS_MAIN(tst_QTransform) |
| 801 | |
| 802 | |
| 803 | #include "tst_qtransform.moc" |