📄 qtransform.cpp
字号:
/******************************************************************************** Copyright (C) 1992-2007 Trolltech ASA. All rights reserved.**** This file is part of the QtGui module of the Qt Toolkit.**** This file may be used under the terms of the GNU General Public** License version 2.0 as published by the Free Software Foundation** and appearing in the file LICENSE.GPL included in the packaging of** this file. Please review the following information to ensure GNU** General Public Licensing requirements will be met:** http://trolltech.com/products/qt/licenses/licensing/opensource/**** If you are unsure which license is appropriate for your use, please** review the following information:** http://trolltech.com/products/qt/licenses/licensing/licensingoverview** or contact the sales department at sales@trolltech.com.**** In addition, as a special exception, Trolltech gives you certain** additional rights. These rights are described in the Trolltech GPL** Exception version 1.0, which can be found at** http://www.trolltech.com/products/qt/gplexception/ and in the file** GPL_EXCEPTION.txt in this package.**** In addition, as a special exception, Trolltech, as the sole copyright** holder for Qt Designer, grants users of the Qt/Eclipse Integration** plug-in the right for the Qt/Eclipse Integration to link to** functionality provided by Qt Designer and its related libraries.**** Trolltech reserves all rights not expressly granted herein.**** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.******************************************************************************/#include "qtransform.h"#include "qdatastream.h"#include "qdebug.h"#include "qmath_p.h"#include "qmatrix.h"#include "qregion.h"#include "qpainterpath.h"#include "qvariant.h"#include <math.h>#define MAPDOUBLE(x, y, nx, ny) \{ \ fx = x; \ fy = y; \ nx = affine._m11*fx + affine._m21*fy + affine._dx; \ ny = affine._m12*fx + affine._m22*fy + affine._dy; \ if (!isAffine()) { \ qreal w = m_13*fx + m_23*fy + m_33; \ w = 1/w; \ nx *= w; \ ny *= w; \ }\}#define MAPINT(x, y, nx, ny) \{ \ fx = x; \ fy = y; \ nx = int(affine._m11*fx + affine._m21*fy + affine._dx); \ ny = int(affine._m12*fx + affine._m22*fy + affine._dy); \ if (!isAffine()) { \ qreal w = m_13*fx + m_23*fy + m_33; \ w = 1/w; \ nx = int(nx*w); \ ny = int(ny*w); \ }\}/*! \class QTransform \brief The QTransform class specifies 2D transformations of a coordinate system. \since 4.3 \ingroup multimedia A transformation specifies how to translate, scale, shear, rotate or project the coordinate system, and is typically used when rendering graphics. QTransform differs from QMatrix in that it is a true 3x3 matrix, allowing perpective transformations. QTransform's toAffine() method allows casting QTransform to QMatrix. If a perspective transformation has been specified on the matrix, then the conversion to an affine QMatrix will cause loss of data. QTransform is the recommended transformation class in Qt. A QTransform object can be built using the setMatrix(), scale(), rotate(), translate() and shear() functions. Alternatively, it can be built by applying \l {QTransform#Basic Matrix Operations}{basic matrix operations}. The matrix can also be defined when constructed, and it can be reset to the identity matrix (the default) using the reset() function. The QTransform class supports mapping of graphic primitives: A given point, line, polygon, region, or painter path can be mapped to the coordinate system defined by \e this matrix using the map() function. In case of a rectangle, its coordinates can be transformed using the mapRect() function. A rectangle can also be transformed into a \e polygon (mapped to the coordinate system defined by \e this matrix), using the mapToPolygon() function. QTransform provides the isIdentity() function which returns true if the matrix is the identity matrix, and the isInvertible() function which returns true if the matrix is non-singular (i.e. AB = BA = I). The inverted() function returns an inverted copy of \e this matrix if it is invertible (otherwise it returns the identity matrix). In addition, QTransform provides the det() function returning the matrix's determinant. Finally, the QTransform class supports matrix multiplication, and objects of the class can be streamed as well as compared. \tableofcontents \section1 Rendering Graphics When rendering graphics, the matrix defines the transformations but the actual transformation is performed by the drawing routines in QPainter. By default, QPainter operates on the associated device's own coordinate system. The standard coordinate system of a QPaintDevice has its origin located at the top-left position. The \e x values increase to the right; \e y values increase downward. For a complete description, see the \l {The Coordinate System}{coordinate system} documentation. QPainter has functions to translate, scale, shear and rotate the coordinate system without using a QTransform. For example: \table 100% \row \o \inlineimage qtransform-simpletransformation.png \o \quotefromfile snippets/transform/main.cpp \skipto SimpleTransformation::paintEvent \printuntil } \endtable Although these functions are very convenient, it can be more efficient to build a QTransform and call QPainter::setTransform() if you want to perform more than a single transform operation. For example: \table 100% \row \o \inlineimage qtransform-combinedtransformation.png \o \quotefromfile snippets/transform/main.cpp \skipto CombinedTransformation::paintEvent \printuntil } \endtable \section1 Basic Matrix Operations \image qmatrix-representation.png A QTransform object contains a 3 x 3 matrix. The \c dx and \c dy elements specify horizontal and vertical translation. The \c m11 and \c m22 elements specify horizontal and vertical scaling. And finally, the \c m21 and \c m12 elements specify horizontal and vertical \e shearing. QTransform transforms a point in the plane to another point using the following formulas: \code x' = m11*x + m21*y + dx y' = m22*y + m12*x + dy \endcode The point \e (x, y) is the original point, and \e (x', y') is the transformed point. \e (x', y') can be transformed back to \e (x, y) by performing the same operation on the inverted() matrix. The various matrix elements can be set when constructing the matrix, or by using the setMatrix() function later on. They also be manipulated using the translate(), rotate(), scale() and shear() convenience functions, The currently set values can be retrieved using the m11(), m12(), m21(), m22(), dx() and dy() functions. Translation is the simplest transformation. Setting \c dx and \c dy will move the coordinate system \c dx units along the X axis and \c dy units along the Y axis. Scaling can be done by setting \c m11 and \c m22. For example, setting \c m11 to 2 and \c m22 to 1.5 will double the height and increase the width by 50%. The identity matrix has \c m11 and \c m22 set to 1 (all others are set to 0) mapping a point to itself. Shearing is controlled by \c m12 and \c m21. Setting these elements to values different from zero will twist the coordinate system. Rotation is achieved by carefully setting both the shearing factors and the scaling factors. Here's the combined transformations example using basic matrix operations: \table 100% \row \o \inlineimage qtransform-combinedtransformation2.png \o \quotefromfile snippets/transform/main.cpp \skipto BasicOperations::paintEvent \printuntil } \endtable \sa QPainter, {The Coordinate System}, {demos/affine}{Affine Transformations Demo}, {Transformations Example}*//*! \enum QTransform::TransformationType \value TxNone \value TxTranslate \value TxScale \value TxRotate \value TxShear \value TxProject*//*! Constructs an identity matrix. All elements are set to zero except \c m11 and \c m22 (specifying the scale) and \c m13 which are set to 1. \sa reset()*/QTransform::QTransform() : m_13(0), m_23(0), m_33(1) , m_type(TxNone) , m_dirty(TxNone){}/*! Constructs a matrix with the elements, \a h11, \a h12, \a h13, \a h21, \a h22, \a h23, \a h31, \a h32, \a h33. \sa setMatrix()*/QTransform::QTransform(qreal h11, qreal h12, qreal h13, qreal h21, qreal h22, qreal h23, qreal h31, qreal h32, qreal h33) : affine(h11, h12, h21, h22, h31, h32), m_13(h13), m_23(h23), m_33(h33) , m_type(TxNone) , m_dirty(TxProject){}/*! Constructs a matrix with the elements, \a h11, \a h12, \a h21, \a h22, \a dx and \a dy. \sa setMatrix()*/QTransform::QTransform(qreal h11, qreal h12, qreal h21, qreal h22, qreal dx, qreal dy) : affine(h11, h12, h21, h22, dx, dy), m_13(0), m_23(0), m_33(1) , m_type(TxNone) , m_dirty(TxShear){}/*! \fn QTransform::QTransform(const QMatrix &matrix) Constructs a matrix that is a copy of the given \a matrix. Note that the \c m13, \c m23, and \c m33 elements are set to 0, 0, and 1 respectively. */QTransform::QTransform(const QMatrix &mtx) : affine(mtx), m_13(0), m_23(0), m_33(1) , m_type(TxNone) , m_dirty(TxShear){}/*! Returns the adjoint of this matrix.*/QTransform QTransform::adjoint() const{ qreal h11, h12, h13, h21, h22, h23, h31, h32, h33; h11 = affine._m22*m_33 - m_23*affine._dy; h21 = m_23*affine._dx - affine._m21*m_33; h31 = affine._m21*affine._dy - affine._m22*affine._dx; h12 = m_13*affine._dy - affine._m12*m_33; h22 = affine._m11*m_33 - m_13*affine._dx; h32 = affine._m12*affine._dx - affine._m11*affine._dy; h13 = affine._m12*m_23 - m_13*affine._m22; h23 = m_13*affine._m21 - affine._m11*m_23; h33 = affine._m11*affine._m22 - affine._m12*affine._m21; //### not a huge fan of this simplification but // i'd like to keep m33 as 1.0 //return QTransform(h11, h12, h13, // h21, h22, h23, // h31, h32, h33); h33 = 1/h33; return QTransform(h11*h33, h12*h33, h13*h33, h21*h33, h22*h33, h23*h33, h31*h33, h32*h33, 1.0);}/*! Returns the transpose of this matrix.*/QTransform QTransform::transposed() const{ return QTransform(affine._m11, affine._m21, affine._dx, affine._m12, affine._m22, affine._dy, m_13, m_23, m_33);}/*! Returns an inverted copy of this matrix. If the matrix is singular (not invertible), the returned matrix is the identity matrix. If \a invertible is valid (i.e. not 0), its value is set to true if the matrix is invertible, otherwise it is set to false. \sa isInvertible()*/QTransform QTransform::inverted(bool *invertible) const{ qreal det = determinant(); if (qFuzzyCompare(det, qreal(0.0))) { if (invertible) *invertible = false; return QTransform(); } if (invertible) *invertible = true; QTransform adjA = adjoint(); QTransform invert = adjA / det; invert = QTransform(invert.m11()/invert.m33(), invert.m12()/invert.m33(), invert.m13()/invert.m33(), invert.m21()/invert.m33(), invert.m22()/invert.m33(), invert.m23()/invert.m33(), invert.m31()/invert.m33(), invert.m32()/invert.m33(), 1); // inverting doesn't change the type invert.m_type = m_type; invert.m_dirty = m_dirty; return invert;}/*! Moves the coordinate system \a dx along the x axis and \a dy along the y axis, and returns a reference to the matrix. \sa setMatrix()*/QTransform & QTransform::translate(qreal dx, qreal dy){ if (type() != TxProject) { affine._dx += dx*affine._m11 + dy*affine._m21; affine._dy += dy*affine._m22 + dx*affine._m12; } else { QTransform translate; translate.affine._dx = dx; translate.affine._dy = dy; *this = translate * *this; } m_dirty |= TxTranslate; return *this;}/*! Scales the coordinate system by \a sx horizontally and \a sy vertically, and returns a reference to the matrix. \sa setMatrix()*/QTransform & QTransform::scale(qreal sx, qreal sy){ if (type() != TxProject) { affine._m11 *= sx; affine._m12 *= sx; affine._m21 *= sy; affine._m22 *= sy; } else { QTransform scale; scale.affine._m11 = sx; scale.affine._m22 = sy; *this = scale * *this; } m_dirty |= TxScale; return *this;}/*! Shears the coordinate system by \a sh horizontally and \a sv vertically, and returns a reference to the matrix. \sa setMatrix()*/QTransform & QTransform::shear(qreal sh, qreal sv){ if (type() != TxProject) { qreal tm11 = sv*affine._m21; qreal tm12 = sv*affine._m22; qreal tm21 = sh*affine._m11; qreal tm22 = sh*affine._m12; affine._m11 += tm11; affine._m12 += tm12; affine._m21 += tm21; affine._m22 += tm22; } else { QTransform shear; shear.affine._m12 = sv; shear.affine._m21 = sh; *this = shear * *this; } m_dirty |= TxShear; return *this;}const qreal deg2rad = qreal(0.017453292519943295769); // pi/180const qreal inv_dist_to_plane = 1. / 1024.;/*! \fn QTransform &QTransform::rotate(qreal angle, Qt::Axis axis) Rotates the coordinate system counterclockwise by the given \a angle about the specified \a axis and returns a reference to the matrix. Note that if you apply a QTransform to a point defined in widget coordinates, the direction of the rotation will be clockwise because the y-axis points downwards. The angle is specified in degrees. \sa setMatrix()*/QTransform & QTransform::rotate(qreal a, Qt::Axis axis){ qreal sina = 0; qreal cosa = 0; if (a == 90. || a == -270.) sina = 1.; else if (a == 270. || a == -90.) sina = -1.; else if (a == 180.) cosa = -1.; else{ qreal b = deg2rad*a; // convert to radians sina = qSin(b); // fast and convenient cosa = qCos(b); } if (axis == Qt::ZAxis) { if (type() != TxProject) { qreal tm11 = cosa*affine._m11 + sina*affine._m21; qreal tm12 = cosa*affine._m12 + sina*affine._m22; qreal tm21 = -sina*affine._m11 + cosa*affine._m21; qreal tm22 = -sina*affine._m12 + cosa*affine._m22; affine._m11 = tm11; affine._m12 = tm12; affine._m21 = tm21; affine._m22 = tm22; } else { QTransform result; result.affine._m11 = cosa; result.affine._m12 = sina; result.affine._m22 = cosa; result.affine._m21 = -sina; *this = result * *this; } m_dirty |= TxRotate; } else { QTransform result; if (axis == Qt::YAxis) { result.affine._m11 = cosa; result.m_13 = -sina * inv_dist_to_plane; } else { result.affine._m22 = cosa; result.m_23 = -sina * inv_dist_to_plane; } m_dirty = TxProject; *this = result * *this; } return *this;}/*! \fn QTransform & QTransform::rotateRadians(qreal angle, Qt::Axis axis) Rotates the coordinate system counterclockwise by the given \a angle about the specified \a axis and returns a reference to the matrix. Note that if you apply a QTransform to a point defined in widget coordinates, the direction of the rotation will be clockwise because the y-axis points downwards. The angle is specified in radians. \sa setMatrix()*/QTransform & QTransform::rotateRadians(qreal a, Qt::Axis axis){ qreal sina = qSin(a); qreal cosa = qCos(a); if (axis == Qt::ZAxis) { if (type() != TxProject) { qreal tm11 = cosa*affine._m11 + sina*affine._m21; qreal tm12 = cosa*affine._m12 + sina*affine._m22; qreal tm21 = -sina*affine._m11 + cosa*affine._m21; qreal tm22 = -sina*affine._m12 + cosa*affine._m22; affine._m11 = tm11; affine._m12 = tm12; affine._m21 = tm21; affine._m22 = tm22; } else { QTransform result; result.affine._m11 = cosa; result.affine._m12 = sina; result.affine._m22 = cosa; result.affine._m21 = -sina; *this = result * *this; } m_dirty |= TxRotate; } else {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -