qvector4d.h

Go to the documentation of this file.
00001 /****************************************************************************
00002 **
00003 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
00004 ** All rights reserved.
00005 ** Contact: Nokia Corporation (qt-info@nokia.com)
00006 **
00007 ** This file is part of the QtGui module of the Qt Toolkit.
00008 **
00009 ** $QT_BEGIN_LICENSE:LGPL$
00010 ** Commercial Usage
00011 ** Licensees holding valid Qt Commercial licenses may use this file in
00012 ** accordance with the Qt Commercial License Agreement provided with the
00013 ** Software or, alternatively, in accordance with the terms contained in
00014 ** a written agreement between you and Nokia.
00015 **
00016 ** GNU Lesser General Public License Usage
00017 ** Alternatively, this file may be used under the terms of the GNU Lesser
00018 ** General Public License version 2.1 as published by the Free Software
00019 ** Foundation and appearing in the file LICENSE.LGPL included in the
00020 ** packaging of this file.  Please review the following information to
00021 ** ensure the GNU Lesser General Public License version 2.1 requirements
00022 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
00023 **
00024 ** In addition, as a special exception, Nokia gives you certain additional
00025 ** rights.  These rights are described in the Nokia Qt LGPL Exception
00026 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this module.
00027 **
00028 ** GNU General Public License Usage
00029 ** Alternatively, this file may be used under the terms of the GNU
00030 ** General Public License version 3.0 as published by the Free Software
00031 ** Foundation and appearing in the file LICENSE.GPL included in the
00032 ** packaging of this file.  Please review the following information to
00033 ** ensure the GNU General Public License version 3.0 requirements will be
00034 ** met: http://www.gnu.org/copyleft/gpl.html.
00035 **
00036 ** If you have questions regarding the use of this file, please contact
00037 ** Nokia at qt-info@nokia.com.
00038 ** $QT_END_LICENSE$
00039 **
00040 ****************************************************************************/
00041 
00042 #ifndef QVECTOR4D_H
00043 #define QVECTOR4D_H
00044 
00045 #include <QtCore/qpoint.h>
00046 #include <QtCore/qmetatype.h>
00047 
00048 QT_BEGIN_HEADER
00049 
00050 QT_BEGIN_NAMESPACE
00051 
00052 QT_MODULE(Gui)
00053 
00054 class QMatrix4x4;
00055 class QVector2D;
00056 class QVector3D;
00057 
00058 #ifndef QT_NO_VECTOR4D
00059 
00060 class Q_GUI_EXPORT QVector4D
00061 {
00062 public:
00063     QVector4D();
00064     QVector4D(qreal xpos, qreal ypos, qreal zpos, qreal wpos);
00065     explicit QVector4D(const QPoint& point);
00066     explicit QVector4D(const QPointF& point);
00067 #ifndef QT_NO_VECTOR2D
00068     QVector4D(const QVector2D& vector);
00069     QVector4D(const QVector2D& vector, qreal zpos, qreal wpos);
00070 #endif
00071 #ifndef QT_NO_VECTOR3D
00072     QVector4D(const QVector3D& vector);
00073     QVector4D(const QVector3D& vector, qreal wpos);
00074 #endif
00075 
00076     bool isNull() const;
00077 
00078     qreal x() const;
00079     qreal y() const;
00080     qreal z() const;
00081     qreal w() const;
00082 
00083     void setX(qreal x);
00084     void setY(qreal y);
00085     void setZ(qreal z);
00086     void setW(qreal w);
00087 
00088     qreal length() const;
00089     qreal lengthSquared() const;
00090 
00091     QVector4D normalized() const;
00092     void normalize();
00093 
00094     QVector4D &operator+=(const QVector4D &vector);
00095     QVector4D &operator-=(const QVector4D &vector);
00096     QVector4D &operator*=(qreal factor);
00097     QVector4D &operator*=(const QVector4D &vector);
00098     QVector4D &operator/=(qreal divisor);
00099 
00100     static qreal dotProduct(const QVector4D& v1, const QVector4D& v2);
00101 
00102     friend inline bool operator==(const QVector4D &v1, const QVector4D &v2);
00103     friend inline bool operator!=(const QVector4D &v1, const QVector4D &v2);
00104     friend inline const QVector4D operator+(const QVector4D &v1, const QVector4D &v2);
00105     friend inline const QVector4D operator-(const QVector4D &v1, const QVector4D &v2);
00106     friend inline const QVector4D operator*(qreal factor, const QVector4D &vector);
00107     friend inline const QVector4D operator*(const QVector4D &vector, qreal factor);
00108     friend inline const QVector4D operator*(const QVector4D &v1, const QVector4D& v2);
00109     friend inline const QVector4D operator-(const QVector4D &vector);
00110     friend inline const QVector4D operator/(const QVector4D &vector, qreal divisor);
00111 
00112     friend inline bool qFuzzyCompare(const QVector4D& v1, const QVector4D& v2);
00113 
00114 #ifndef QT_NO_VECTOR2D
00115     QVector2D toVector2D() const;
00116     QVector2D toVector2DAffine() const;
00117 #endif
00118 #ifndef QT_NO_VECTOR3D
00119     QVector3D toVector3D() const;
00120     QVector3D toVector3DAffine() const;
00121 #endif
00122 
00123     QPoint toPoint() const;
00124     QPointF toPointF() const;
00125 
00126     operator QVariant() const;
00127 
00128 private:
00129     float xp, yp, zp, wp;
00130 
00131     QVector4D(float xpos, float ypos, float zpos, float wpos, int dummy);
00132 
00133     friend class QVector2D;
00134     friend class QVector3D;
00135 #ifndef QT_NO_MATRIX4X4
00136     friend QVector4D operator*(const QVector4D& vector, const QMatrix4x4& matrix);
00137     friend QVector4D operator*(const QMatrix4x4& matrix, const QVector4D& vector);
00138 #endif
00139 };
00140 
00141 Q_DECLARE_TYPEINFO(QVector4D, Q_MOVABLE_TYPE);
00142 
00143 inline QVector4D::QVector4D() : xp(0.0f), yp(0.0f), zp(0.0f), wp(0.0f) {}
00144 
00145 inline QVector4D::QVector4D(qreal xpos, qreal ypos, qreal zpos, qreal wpos) : xp(xpos), yp(ypos), zp(zpos), wp(wpos) {}
00146 
00147 inline QVector4D::QVector4D(float xpos, float ypos, float zpos, float wpos, int) : xp(xpos), yp(ypos), zp(zpos), wp(wpos) {}
00148 
00149 inline QVector4D::QVector4D(const QPoint& point) : xp(point.x()), yp(point.y()), zp(0.0f), wp(0.0f) {}
00150 
00151 inline QVector4D::QVector4D(const QPointF& point) : xp(point.x()), yp(point.y()), zp(0.0f), wp(0.0f) {}
00152 
00153 inline bool QVector4D::isNull() const
00154 {
00155     return qIsNull(xp) && qIsNull(yp) && qIsNull(zp) && qIsNull(wp);
00156 }
00157 
00158 inline qreal QVector4D::x() const { return qreal(xp); }
00159 inline qreal QVector4D::y() const { return qreal(yp); }
00160 inline qreal QVector4D::z() const { return qreal(zp); }
00161 inline qreal QVector4D::w() const { return qreal(wp); }
00162 
00163 inline void QVector4D::setX(qreal aX) { xp = aX; }
00164 inline void QVector4D::setY(qreal aY) { yp = aY; }
00165 inline void QVector4D::setZ(qreal aZ) { zp = aZ; }
00166 inline void QVector4D::setW(qreal aW) { wp = aW; }
00167 
00168 inline QVector4D &QVector4D::operator+=(const QVector4D &vector)
00169 {
00170     xp += vector.xp;
00171     yp += vector.yp;
00172     zp += vector.zp;
00173     wp += vector.wp;
00174     return *this;
00175 }
00176 
00177 inline QVector4D &QVector4D::operator-=(const QVector4D &vector)
00178 {
00179     xp -= vector.xp;
00180     yp -= vector.yp;
00181     zp -= vector.zp;
00182     wp -= vector.wp;
00183     return *this;
00184 }
00185 
00186 inline QVector4D &QVector4D::operator*=(qreal factor)
00187 {
00188     xp *= factor;
00189     yp *= factor;
00190     zp *= factor;
00191     wp *= factor;
00192     return *this;
00193 }
00194 
00195 inline QVector4D &QVector4D::operator*=(const QVector4D &vector)
00196 {
00197     xp *= vector.xp;
00198     yp *= vector.yp;
00199     zp *= vector.zp;
00200     wp *= vector.wp;
00201     return *this;
00202 }
00203 
00204 inline QVector4D &QVector4D::operator/=(qreal divisor)
00205 {
00206     xp /= divisor;
00207     yp /= divisor;
00208     zp /= divisor;
00209     wp /= divisor;
00210     return *this;
00211 }
00212 
00213 inline bool operator==(const QVector4D &v1, const QVector4D &v2)
00214 {
00215     return v1.xp == v2.xp && v1.yp == v2.yp && v1.zp == v2.zp && v1.wp == v2.wp;
00216 }
00217 
00218 inline bool operator!=(const QVector4D &v1, const QVector4D &v2)
00219 {
00220     return v1.xp != v2.xp || v1.yp != v2.yp || v1.zp != v2.zp || v1.wp != v2.wp;
00221 }
00222 
00223 inline const QVector4D operator+(const QVector4D &v1, const QVector4D &v2)
00224 {
00225     return QVector4D(v1.xp + v2.xp, v1.yp + v2.yp, v1.zp + v2.zp, v1.wp + v2.wp, 1);
00226 }
00227 
00228 inline const QVector4D operator-(const QVector4D &v1, const QVector4D &v2)
00229 {
00230     return QVector4D(v1.xp - v2.xp, v1.yp - v2.yp, v1.zp - v2.zp, v1.wp - v2.wp, 1);
00231 }
00232 
00233 inline const QVector4D operator*(qreal factor, const QVector4D &vector)
00234 {
00235     return QVector4D(vector.xp * factor, vector.yp * factor, vector.zp * factor, vector.wp * factor, 1);
00236 }
00237 
00238 inline const QVector4D operator*(const QVector4D &vector, qreal factor)
00239 {
00240     return QVector4D(vector.xp * factor, vector.yp * factor, vector.zp * factor, vector.wp * factor, 1);
00241 }
00242 
00243 inline const QVector4D operator*(const QVector4D &v1, const QVector4D& v2)
00244 {
00245     return QVector4D(v1.xp * v2.xp, v1.yp * v2.yp, v1.zp * v2.zp, v1.wp * v2.wp, 1);
00246 }
00247 
00248 inline const QVector4D operator-(const QVector4D &vector)
00249 {
00250     return QVector4D(-vector.xp, -vector.yp, -vector.zp, -vector.wp, 1);
00251 }
00252 
00253 inline const QVector4D operator/(const QVector4D &vector, qreal divisor)
00254 {
00255     return QVector4D(vector.xp / divisor, vector.yp / divisor, vector.zp / divisor, vector.wp / divisor, 1);
00256 }
00257 
00258 inline bool qFuzzyCompare(const QVector4D& v1, const QVector4D& v2)
00259 {
00260     return qFuzzyCompare(v1.xp, v2.xp) &&
00261            qFuzzyCompare(v1.yp, v2.yp) &&
00262            qFuzzyCompare(v1.zp, v2.zp) &&
00263            qFuzzyCompare(v1.wp, v2.wp);
00264 }
00265 
00266 inline QPoint QVector4D::toPoint() const
00267 {
00268     return QPoint(qRound(xp), qRound(yp));
00269 }
00270 
00271 inline QPointF QVector4D::toPointF() const
00272 {
00273     return QPointF(qreal(xp), qreal(yp));
00274 }
00275 
00276 #ifndef QT_NO_DEBUG_STREAM
00277 Q_GUI_EXPORT QDebug operator<<(QDebug dbg, const QVector4D &vector);
00278 #endif
00279 
00280 #ifndef QT_NO_DATASTREAM
00281 Q_GUI_EXPORT QDataStream &operator<<(QDataStream &, const QVector4D &);
00282 Q_GUI_EXPORT QDataStream &operator>>(QDataStream &, QVector4D &);
00283 #endif
00284 
00285 #endif
00286 
00287 QT_END_NAMESPACE
00288 
00289 QT_END_HEADER
00290 
00291 #endif