diff options
author | Rhys Weatherley <rhys.weatherley@nokia.com> | 2009-06-02 05:06:12 (GMT) |
---|---|---|
committer | Rhys Weatherley <rhys.weatherley@nokia.com> | 2009-06-02 05:06:12 (GMT) |
commit | 3245f19f8d3abbcba5fcd9adc3fc984ad4c7ddcb (patch) | |
tree | a3cbbf72ca14daf37f1906efd36b9c80b0c46f6e /src | |
parent | 4636f6983aa253c7552bba5d7d8772844ea4c42c (diff) | |
download | Qt-3245f19f8d3abbcba5fcd9adc3fc984ad4c7ddcb.zip Qt-3245f19f8d3abbcba5fcd9adc3fc984ad4c7ddcb.tar.gz Qt-3245f19f8d3abbcba5fcd9adc3fc984ad4c7ddcb.tar.bz2 |
Optimize QMatrix4x4::map() for QVector3D/QPoint/QPointF
Use the matrix "flagBits" to short-cut transformations when the
matrix type is identity/translate/scale.
Diffstat (limited to 'src')
-rw-r--r-- | src/gui/math3d/qmatrix4x4.h | 135 |
1 files changed, 90 insertions, 45 deletions
diff --git a/src/gui/math3d/qmatrix4x4.h b/src/gui/math3d/qmatrix4x4.h index cff4c1e..ba7f67f 100644 --- a/src/gui/math3d/qmatrix4x4.h +++ b/src/gui/math3d/qmatrix4x4.h @@ -631,26 +631,43 @@ inline QVector3D operator*(const QVector3D& vector, const QMatrix4x4& matrix) inline QVector3D operator*(const QMatrix4x4& matrix, const QVector3D& vector) { float x, y, z, w; - x = vector.xp * matrix.m[0][0] + - vector.yp * matrix.m[1][0] + - vector.zp * matrix.m[2][0] + - matrix.m[3][0]; - y = vector.xp * matrix.m[0][1] + - vector.yp * matrix.m[1][1] + - vector.zp * matrix.m[2][1] + - matrix.m[3][1]; - z = vector.xp * matrix.m[0][2] + - vector.yp * matrix.m[1][2] + - vector.zp * matrix.m[2][2] + - matrix.m[3][2]; - w = vector.xp * matrix.m[0][3] + - vector.yp * matrix.m[1][3] + - vector.zp * matrix.m[2][3] + - matrix.m[3][3]; - if (w == 1.0f) - return QVector3D(x, y, z, 1); - else - return QVector3D(x / w, y / w, z / w, 1); + if (matrix.flagBits == QMatrix4x4::Identity) { + return vector; + } else if (matrix.flagBits == QMatrix4x4::Translation) { + return QVector3D(vector.xp + matrix.m[3][0], + vector.yp + matrix.m[3][1], + vector.zp + matrix.m[3][2], 1); + } else if (matrix.flagBits == + (QMatrix4x4::Translation | QMatrix4x4::Scale)) { + return QVector3D(vector.xp * matrix.m[0][0] + matrix.m[3][0], + vector.yp * matrix.m[1][1] + matrix.m[3][1], + vector.zp * matrix.m[2][2] + matrix.m[3][2], 1); + } else if (matrix.flagBits == QMatrix4x4::Scale) { + return QVector3D(vector.xp * matrix.m[0][0], + vector.yp * matrix.m[1][1], + vector.zp * matrix.m[2][2], 1); + } else { + x = vector.xp * matrix.m[0][0] + + vector.yp * matrix.m[1][0] + + vector.zp * matrix.m[2][0] + + matrix.m[3][0]; + y = vector.xp * matrix.m[0][1] + + vector.yp * matrix.m[1][1] + + vector.zp * matrix.m[2][1] + + matrix.m[3][1]; + z = vector.xp * matrix.m[0][2] + + vector.yp * matrix.m[1][2] + + vector.zp * matrix.m[2][2] + + matrix.m[3][2]; + w = vector.xp * matrix.m[0][3] + + vector.yp * matrix.m[1][3] + + vector.zp * matrix.m[2][3] + + matrix.m[3][3]; + if (w == 1.0f) + return QVector3D(x, y, z, 1); + else + return QVector3D(x / w, y / w, z / w, 1); + } } #endif @@ -752,19 +769,33 @@ inline QPoint operator*(const QMatrix4x4& matrix, const QPoint& point) float x, y, w; xin = point.x(); yin = point.y(); - x = xin * matrix.m[0][0] + - yin * matrix.m[1][0] + - matrix.m[3][0]; - y = xin * matrix.m[0][1] + - yin * matrix.m[1][1] + - matrix.m[3][1]; - w = xin * matrix.m[0][3] + - yin * matrix.m[1][3] + - matrix.m[3][3]; - if (w == 1.0f) - return QPoint(qRound(x), qRound(y)); - else - return QPoint(qRound(x / w), qRound(y / w)); + if (matrix.flagBits == QMatrix4x4::Identity) { + return point; + } else if (matrix.flagBits == QMatrix4x4::Translation) { + return QPoint(qRound(xin + matrix.m[3][0]), + qRound(yin + matrix.m[3][1])); + } else if (matrix.flagBits == + (QMatrix4x4::Translation | QMatrix4x4::Scale)) { + return QPoint(qRound(xin * matrix.m[0][0] + matrix.m[3][0]), + qRound(yin * matrix.m[1][1] + matrix.m[3][1])); + } else if (matrix.flagBits == QMatrix4x4::Scale) { + return QPoint(qRound(xin * matrix.m[0][0]), + qRound(yin * matrix.m[1][1])); + } else { + x = xin * matrix.m[0][0] + + yin * matrix.m[1][0] + + matrix.m[3][0]; + y = xin * matrix.m[0][1] + + yin * matrix.m[1][1] + + matrix.m[3][1]; + w = xin * matrix.m[0][3] + + yin * matrix.m[1][3] + + matrix.m[3][3]; + if (w == 1.0f) + return QPoint(qRound(x), qRound(y)); + else + return QPoint(qRound(x / w), qRound(y / w)); + } } inline QPointF operator*(const QMatrix4x4& matrix, const QPointF& point) @@ -773,19 +804,33 @@ inline QPointF operator*(const QMatrix4x4& matrix, const QPointF& point) float x, y, w; xin = point.x(); yin = point.y(); - x = xin * matrix.m[0][0] + - yin * matrix.m[1][0] + - matrix.m[3][0]; - y = xin * matrix.m[0][1] + - yin * matrix.m[1][1] + - matrix.m[3][1]; - w = xin * matrix.m[0][3] + - yin * matrix.m[1][3] + - matrix.m[3][3]; - if (w == 1.0f) { - return QPointF(qreal(x), qreal(y)); + if (matrix.flagBits == QMatrix4x4::Identity) { + return point; + } else if (matrix.flagBits == QMatrix4x4::Translation) { + return QPointF(xin + matrix.m[3][0], + yin + matrix.m[3][1]); + } else if (matrix.flagBits == + (QMatrix4x4::Translation | QMatrix4x4::Scale)) { + return QPointF(xin * matrix.m[0][0] + matrix.m[3][0], + yin * matrix.m[1][1] + matrix.m[3][1]); + } else if (matrix.flagBits == QMatrix4x4::Scale) { + return QPointF(xin * matrix.m[0][0], + yin * matrix.m[1][1]); } else { - return QPointF(qreal(x / w), qreal(y / w)); + x = xin * matrix.m[0][0] + + yin * matrix.m[1][0] + + matrix.m[3][0]; + y = xin * matrix.m[0][1] + + yin * matrix.m[1][1] + + matrix.m[3][1]; + w = xin * matrix.m[0][3] + + yin * matrix.m[1][3] + + matrix.m[3][3]; + if (w == 1.0f) { + return QPointF(qreal(x), qreal(y)); + } else { + return QPointF(qreal(x / w), qreal(y / w)); + } } } |