diff options
author | Lars Knoll <lars.knoll@nokia.com> | 2009-06-02 19:02:42 (GMT) |
---|---|---|
committer | Lars Knoll <lars.knoll@nokia.com> | 2009-06-15 11:00:00 (GMT) |
commit | b615be69f06a97aff3694065e97541133647d877 (patch) | |
tree | dc90aa08d31b6bdde2d534394311d3543a9e9981 | |
parent | 44766d265c16551043d2739171069fe042c40091 (diff) | |
download | Qt-b615be69f06a97aff3694065e97541133647d877.zip Qt-b615be69f06a97aff3694065e97541133647d877.tar.gz Qt-b615be69f06a97aff3694065e97541133647d877.tar.bz2 |
implement equality operator in a more sane way
Using qFuzzyCompare for checking whether two transformations
are equal doesn't give us too much and is inconsistent with
our other matrix classes. Using simple floating point
equality is a lot faster as well.
Added qFuzzyCompare overloads for QMatrix and QTransform to still
allow for fuzzy comparisons.
Reviewed-By: Samuel Rødal
-rw-r--r-- | src/gui/painting/qmatrix.cpp | 11 | ||||
-rw-r--r-- | src/gui/painting/qmatrix.h | 11 | ||||
-rw-r--r-- | src/gui/painting/qtransform.cpp | 25 | ||||
-rw-r--r-- | src/gui/painting/qtransform.h | 14 | ||||
-rw-r--r-- | tests/auto/qtransform/tst_qtransform.cpp | 2 |
5 files changed, 57 insertions, 6 deletions
diff --git a/src/gui/painting/qmatrix.cpp b/src/gui/painting/qmatrix.cpp index 030415d..7e26a99 100644 --- a/src/gui/painting/qmatrix.cpp +++ b/src/gui/painting/qmatrix.cpp @@ -1194,4 +1194,15 @@ QDebug operator<<(QDebug dbg, const QMatrix &m) Use the mapRect() function instead. */ + +/*! + \fn bool qFuzzyCompare(const QMatrix& m1, const QMatrix& m2) + + \relates QMatrix + \since 4.6 + + Returns true if \a m1 and \a m2 are equal, allowing for a small + fuzziness factor for floating-point comparisons; false otherwise. +*/ + QT_END_NAMESPACE diff --git a/src/gui/painting/qmatrix.h b/src/gui/painting/qmatrix.h index 1df2395..4a4e91f 100644 --- a/src/gui/painting/qmatrix.h +++ b/src/gui/painting/qmatrix.h @@ -165,6 +165,17 @@ inline bool QMatrix::isIdentity() const && qFuzzyIsNull(_m21) && qFuzzyIsNull(_dx) && qFuzzyIsNull(_dy); } +inline bool qFuzzyCompare(const QMatrix& m1, const QMatrix& m2) +{ + return qFuzzyCompare(m1.m11(), m2.m11()) + && qFuzzyCompare(m1.m12(), m2.m12()) + && qFuzzyCompare(m1.m21(), m2.m21()) + && qFuzzyCompare(m1.m22(), m2.m22()) + && qFuzzyCompare(m1.dx(), m2.dx()) + && qFuzzyCompare(m1.dy(), m2.dy()); +} + + /***************************************************************************** QMatrix stream functions *****************************************************************************/ diff --git a/src/gui/painting/qtransform.cpp b/src/gui/painting/qtransform.cpp index 4bc20f6..385fde1 100644 --- a/src/gui/painting/qtransform.cpp +++ b/src/gui/painting/qtransform.cpp @@ -708,11 +708,15 @@ QTransform & QTransform::rotateRadians(qreal a, Qt::Axis axis) */ bool QTransform::operator==(const QTransform &o) const { -#define qFZ qFuzzyCompare - return qFZ(affine._m11, o.affine._m11) && qFZ(affine._m12, o.affine._m12) && qFZ(m_13, o.m_13) - && qFZ(affine._m21, o.affine._m21) && qFZ(affine._m22, o.affine._m22) && qFZ(m_23, o.m_23) - && qFZ(affine._dx, o.affine._dx) && qFZ(affine._dy, o.affine._dy) && qFZ(m_33, o.m_33); -#undef qFZ + return affine._m11 == o.affine._m11 && + affine._m12 == o.affine._m12 && + affine._m21 == o.affine._m21 && + affine._m22 == o.affine._m22 && + affine._dx == o.affine._dx && + affine._dy == o.affine._dy && + m_13 == o.m_13 && + m_23 == o.m_23 && + m_33 == o.m_33; } /*! @@ -2178,6 +2182,17 @@ QTransform::operator QVariant() const \sa reset() */ +/*! + \fn bool qFuzzyCompare(const QTransform& t1, const QTransform& t2) + + \relates QTransform + \since 4.6 + + Returns true if \a t1 and \a t2 are equal, allowing for a small + fuzziness factor for floating-point comparisons; false otherwise. +*/ + + // returns true if the transform is uniformly scaling // (same scale in x and y direction) // scale is set to the max of x and y scaling factors diff --git a/src/gui/painting/qtransform.h b/src/gui/painting/qtransform.h index a5002ca..57ec826 100644 --- a/src/gui/painting/qtransform.h +++ b/src/gui/painting/qtransform.h @@ -331,6 +331,20 @@ inline QTransform &QTransform::operator-=(qreal num) return *this; } +inline bool qFuzzyCompare(const QTransform& t1, const QTransform& t2) +{ + return qFuzzyCompare(t1.m11(), t2.m11()) + && qFuzzyCompare(t1.m12(), t2.m12()) + && qFuzzyCompare(t1.m13(), t2.m13()) + && qFuzzyCompare(t1.m21(), t2.m21()) + && qFuzzyCompare(t1.m22(), t2.m22()) + && qFuzzyCompare(t1.m23(), t2.m23()) + && qFuzzyCompare(t1.m31(), t2.m31()) + && qFuzzyCompare(t1.m32(), t2.m32()) + && qFuzzyCompare(t1.m33(), t2.m33()); +} + + /****** stream functions *******************/ #ifndef QT_NO_DATASTREAM Q_GUI_EXPORT QDataStream &operator<<(QDataStream &, const QTransform &); diff --git a/tests/auto/qtransform/tst_qtransform.cpp b/tests/auto/qtransform/tst_qtransform.cpp index 3b13a41..99a449a 100644 --- a/tests/auto/qtransform/tst_qtransform.cpp +++ b/tests/auto/qtransform/tst_qtransform.cpp @@ -654,7 +654,7 @@ void tst_QTransform::transform() d.rotate(30); e.shear(0.5, 0.5); - QCOMPARE(t, e * d * c * b * a); + QVERIFY(qFuzzyCompare(t, e * d * c * b * a)); } void tst_QTransform::mapEmptyPath() |