summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/gui/math3d/qmatrix4x4.cpp69
-rw-r--r--src/gui/math3d/qmatrix4x4.h26
-rw-r--r--tests/auto/math3d/qmatrixnxn/tst_qmatrixnxn.cpp114
3 files changed, 179 insertions, 30 deletions
diff --git a/src/gui/math3d/qmatrix4x4.cpp b/src/gui/math3d/qmatrix4x4.cpp
index a8dabf3..d2f1ded 100644
--- a/src/gui/math3d/qmatrix4x4.cpp
+++ b/src/gui/math3d/qmatrix4x4.cpp
@@ -1411,8 +1411,6 @@ QTransform QMatrix4x4::toTransform() const
#endif
/*!
- \fn QRect QMatrix4x4::mapRect(const QRect& rect) const
-
Maps \a rect by multiplying this matrix by the corners
of \a rect and then forming a new rectangle from the results.
The returned rectangle will be an ordinary 2D rectangle
@@ -1420,10 +1418,43 @@ QTransform QMatrix4x4::toTransform() const
\sa map()
*/
+QRect QMatrix4x4::mapRect(const QRect& rect) const
+{
+ if (flagBits == (Translation | Scale) || flagBits == Scale) {
+ qreal x = rect.x() * m[0][0] + m[3][0];
+ qreal y = rect.y() * m[1][1] + m[3][1];
+ qreal w = rect.width() * m[0][0];
+ qreal h = rect.height() * m[1][1];
+ if (w < 0) {
+ w = -w;
+ x -= w;
+ }
+ if (h < 0) {
+ h = -h;
+ y -= h;
+ }
+ return QRect(qRound(x), qRound(y), qRound(w), qRound(h));
+ } else if (flagBits == Translation) {
+ return QRect(qRound(rect.x() + m[3][0]),
+ qRound(rect.y() + m[3][1]),
+ rect.width(), rect.height());
+ }
-/*!
- \fn QRectF QMatrix4x4::mapRect(const QRectF& rect) const
+ QPoint tl = map(rect.topLeft());
+ QPoint tr = map(QPoint(rect.x() + rect.width(), rect.y()));
+ QPoint bl = map(QPoint(rect.x(), rect.y() + rect.height()));
+ QPoint br = map(QPoint(rect.x() + rect.width(),
+ rect.y() + rect.height()));
+
+ 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(xmin, ymin, xmax - xmin, ymax - ymin);
+}
+/*!
Maps \a rect by multiplying this matrix by the corners
of \a rect and then forming a new rectangle from the results.
The returned rectangle will be an ordinary 2D rectangle
@@ -1431,6 +1462,36 @@ QTransform QMatrix4x4::toTransform() const
\sa map()
*/
+QRectF QMatrix4x4::mapRect(const QRectF& rect) const
+{
+ if (flagBits == (Translation | Scale) || flagBits == Scale) {
+ qreal x = rect.x() * m[0][0] + m[3][0];
+ qreal y = rect.y() * m[1][1] + m[3][1];
+ qreal w = rect.width() * m[0][0];
+ qreal h = rect.height() * m[1][1];
+ if (w < 0) {
+ w = -w;
+ x -= w;
+ }
+ if (h < 0) {
+ h = -h;
+ y -= h;
+ }
+ return QRectF(x, y, w, h);
+ } else if (flagBits == Translation) {
+ return rect.translated(m[3][0], m[3][1]);
+ }
+
+ 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));
+}
/*!
\fn float *QMatrix4x4::data()
diff --git a/src/gui/math3d/qmatrix4x4.h b/src/gui/math3d/qmatrix4x4.h
index 2b485c1..00543f5 100644
--- a/src/gui/math3d/qmatrix4x4.h
+++ b/src/gui/math3d/qmatrix4x4.h
@@ -901,32 +901,6 @@ inline QVector4D QMatrix4x4::map(const QVector4D& point) const
#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 float *QMatrix4x4::data()
{
// We have to assume that the caller will modify the matrix elements,
diff --git a/tests/auto/math3d/qmatrixnxn/tst_qmatrixnxn.cpp b/tests/auto/math3d/qmatrixnxn/tst_qmatrixnxn.cpp
index bb510fc..936f9d6 100644
--- a/tests/auto/math3d/qmatrixnxn/tst_qmatrixnxn.cpp
+++ b/tests/auto/math3d/qmatrixnxn/tst_qmatrixnxn.cpp
@@ -167,6 +167,9 @@ private slots:
void fill();
+ void mapRect_data();
+ void mapRect();
+
private:
static void setMatrix(QMatrix2x2& m, const qreal *values);
static void setMatrixFixed(QMatrix2x2& m, const qreal *values);
@@ -3180,6 +3183,117 @@ void tst_QMatrix::fill()
QVERIFY(isSame(m2, fillValues4x3));
}
+// Test the mapRect() function for QRect and QRectF.
+void tst_QMatrix::mapRect_data()
+{
+ QTest::addColumn<qreal>("x");
+ QTest::addColumn<qreal>("y");
+ QTest::addColumn<qreal>("width");
+ QTest::addColumn<qreal>("height");
+
+ QTest::newRow("null")
+ << (qreal)0.0f << (qreal)0.0f << (qreal)0.0f << (qreal)0.0f;
+ QTest::newRow("rect")
+ << (qreal)1.0f << (qreal)-20.5f << (qreal)100.0f << (qreal)63.75f;
+}
+void tst_QMatrix::mapRect()
+{
+ QFETCH(qreal, x);
+ QFETCH(qreal, y);
+ QFETCH(qreal, width);
+ QFETCH(qreal, height);
+
+ QRectF rect(x, y, width, height);
+ QRect recti(qRound(x), qRound(y), qRound(width), qRound(height));
+
+ QMatrix4x4 m1;
+ QVERIFY(m1.mapRect(rect) == rect);
+ QVERIFY(m1.mapRect(recti) == recti);
+
+ QMatrix4x4 m2;
+ m2.translate(-100.5f, 64.0f);
+ QRectF translated = rect.translated(-100.5f, 64.0f);
+ QRect translatedi = QRect(qRound(recti.x() - 100.5f), recti.y() + 64,
+ recti.width(), recti.height());
+ QVERIFY(m2.mapRect(rect) == translated);
+ QVERIFY(m2.mapRect(recti) == translatedi);
+
+ QMatrix4x4 m3;
+ m3.scale(-100.5f, 64.0f);
+ qreal scalex = x * -100.5f;
+ qreal scaley = y * 64.0f;
+ qreal scalewid = width * -100.5f;
+ qreal scaleht = height * 64.0f;
+ if (scalewid < 0.0f) {
+ scalewid = -scalewid;
+ scalex -= scalewid;
+ }
+ if (scaleht < 0.0f) {
+ scaleht = -scaleht;
+ scaley -= scaleht;
+ }
+ QRectF scaled(scalex, scaley, scalewid, scaleht);
+ QVERIFY(m3.mapRect(rect) == scaled);
+ scalex = recti.x() * -100.5f;
+ scaley = recti.y() * 64.0f;
+ scalewid = recti.width() * -100.5f;
+ scaleht = recti.height() * 64.0f;
+ if (scalewid < 0.0f) {
+ scalewid = -scalewid;
+ scalex -= scalewid;
+ }
+ if (scaleht < 0.0f) {
+ scaleht = -scaleht;
+ scaley -= scaleht;
+ }
+ QRect scaledi(qRound(scalex), qRound(scaley),
+ qRound(scalewid), qRound(scaleht));
+ QVERIFY(m3.mapRect(recti) == scaledi);
+
+ QMatrix4x4 m4;
+ m4.translate(-100.5f, 64.0f);
+ m4.scale(-2.5f, 4.0f);
+ qreal transx1 = x * -2.5f - 100.5f;
+ qreal transy1 = y * 4.0f + 64.0f;
+ qreal transx2 = (x + width) * -2.5f - 100.5f;
+ qreal transy2 = (y + height) * 4.0f + 64.0f;
+ if (transx1 > transx2)
+ qSwap(transx1, transx2);
+ if (transy1 > transy2)
+ qSwap(transy1, transy2);
+ QRectF trans(transx1, transy1, transx2 - transx1, transy2 - transy1);
+ QVERIFY(m4.mapRect(rect) == trans);
+ transx1 = recti.x() * -2.5f - 100.5f;
+ transy1 = recti.y() * 4.0f + 64.0f;
+ transx2 = (recti.x() + recti.width()) * -2.5f - 100.5f;
+ transy2 = (recti.y() + recti.height()) * 4.0f + 64.0f;
+ if (transx1 > transx2)
+ qSwap(transx1, transx2);
+ if (transy1 > transy2)
+ qSwap(transy1, transy2);
+ QRect transi(qRound(transx1), qRound(transy1),
+ qRound(transx2) - qRound(transx1),
+ qRound(transy2) - qRound(transy1));
+ QVERIFY(m4.mapRect(recti) == transi);
+
+ m4.rotate(45.0f, 0.0f, 0.0f, 1.0f);
+
+ QTransform t4;
+ t4.translate(-100.5f, 64.0f);
+ t4.scale(-2.5f, 4.0f);
+ t4.rotate(45.0f);
+ QRectF mr = m4.mapRect(rect);
+ QRectF tr = t4.mapRect(rect);
+ QVERIFY(fuzzyCompare(mr.x(), tr.x()));
+ QVERIFY(fuzzyCompare(mr.y(), tr.y()));
+ QVERIFY(fuzzyCompare(mr.width(), tr.width()));
+ QVERIFY(fuzzyCompare(mr.height(), tr.height()));
+
+ QRect mri = m4.mapRect(recti);
+ QRect tri = t4.mapRect(recti);
+ QVERIFY(mri == tri);
+}
+
QTEST_APPLESS_MAIN(tst_QMatrix)
#include "tst_qmatrixnxn.moc"