diff options
author | Rhys Weatherley <rhys.weatherley@nokia.com> | 2009-06-02 02:49:08 (GMT) |
---|---|---|
committer | Rhys Weatherley <rhys.weatherley@nokia.com> | 2009-06-02 02:53:33 (GMT) |
commit | cef61c5c660410e2db8418c8a23b68d9e523c75d (patch) | |
tree | 421128bd0f003ec8ec35f5983c3d777e305c25f7 /src/gui/math3d | |
parent | 6e8bafefb9d8484c2fcd0b26c02d1af0e74e25e1 (diff) | |
download | Qt-cef61c5c660410e2db8418c8a23b68d9e523c75d.zip Qt-cef61c5c660410e2db8418c8a23b68d9e523c75d.tar.gz Qt-cef61c5c660410e2db8418c8a23b68d9e523c75d.tar.bz2 |
Optimize QMatrix4x4::mapRect() for translation and scale operations
Diffstat (limited to 'src/gui/math3d')
-rw-r--r-- | src/gui/math3d/qmatrix4x4.cpp | 69 | ||||
-rw-r--r-- | src/gui/math3d/qmatrix4x4.h | 26 |
2 files changed, 65 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, |