qvector3d.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 QVECTOR3D_H
00043 #define QVECTOR3D_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 QVector4D;
00057 
00058 #ifndef QT_NO_VECTOR3D
00059 
00060 class Q_GUI_EXPORT QVector3D
00061 {
00062 public:
00063     QVector3D();
00064     QVector3D(qreal xpos, qreal ypos, qreal zpos);
00065     explicit QVector3D(const QPoint& point);
00066     explicit QVector3D(const QPointF& point);
00067 #ifndef QT_NO_VECTOR2D
00068     QVector3D(const QVector2D& vector);
00069     QVector3D(const QVector2D& vector, qreal zpos);
00070 #endif
00071 #ifndef QT_NO_VECTOR4D
00072     explicit QVector3D(const QVector4D& vector);
00073 #endif
00074 
00075     bool isNull() const;
00076 
00077     qreal x() const;
00078     qreal y() const;
00079     qreal z() const;
00080 
00081     void setX(qreal x);
00082     void setY(qreal y);
00083     void setZ(qreal z);
00084 
00085     qreal length() const;
00086     qreal lengthSquared() const;
00087 
00088     QVector3D normalized() const;
00089     void normalize();
00090 
00091     QVector3D &operator+=(const QVector3D &vector);
00092     QVector3D &operator-=(const QVector3D &vector);
00093     QVector3D &operator*=(qreal factor);
00094     QVector3D &operator*=(const QVector3D& vector);
00095     QVector3D &operator/=(qreal divisor);
00096 
00097     static qreal dotProduct(const QVector3D& v1, const QVector3D& v2);
00098     static QVector3D crossProduct(const QVector3D& v1, const QVector3D& v2);
00099     static QVector3D normal(const QVector3D& v1, const QVector3D& v2);
00100     static QVector3D normal
00101         (const QVector3D& v1, const QVector3D& v2, const QVector3D& v3);
00102 
00103     qreal distanceToPlane(const QVector3D& plane, const QVector3D& normal) const;
00104     qreal distanceToPlane(const QVector3D& plane1, const QVector3D& plane2, const QVector3D& plane3) const;
00105     qreal distanceToLine(const QVector3D& point, const QVector3D& direction) const;
00106 
00107     friend inline bool operator==(const QVector3D &v1, const QVector3D &v2);
00108     friend inline bool operator!=(const QVector3D &v1, const QVector3D &v2);
00109     friend inline const QVector3D operator+(const QVector3D &v1, const QVector3D &v2);
00110     friend inline const QVector3D operator-(const QVector3D &v1, const QVector3D &v2);
00111     friend inline const QVector3D operator*(qreal factor, const QVector3D &vector);
00112     friend inline const QVector3D operator*(const QVector3D &vector, qreal factor);
00113     friend const QVector3D operator*(const QVector3D &v1, const QVector3D& v2);
00114     friend inline const QVector3D operator-(const QVector3D &vector);
00115     friend inline const QVector3D operator/(const QVector3D &vector, qreal divisor);
00116 
00117     friend inline bool qFuzzyCompare(const QVector3D& v1, const QVector3D& v2);
00118 
00119 #ifndef QT_NO_VECTOR2D
00120     QVector2D toVector2D() const;
00121 #endif
00122 #ifndef QT_NO_VECTOR4D
00123     QVector4D toVector4D() const;
00124 #endif
00125 
00126     QPoint toPoint() const;
00127     QPointF toPointF() const;
00128 
00129     operator QVariant() const;
00130 
00131 private:
00132     float xp, yp, zp;
00133 
00134     QVector3D(float xpos, float ypos, float zpos, int dummy);
00135 
00136     friend class QVector2D;
00137     friend class QVector4D;
00138 #ifndef QT_NO_MATRIX4X4
00139     friend QVector3D operator*(const QVector3D& vector, const QMatrix4x4& matrix);
00140     friend QVector3D operator*(const QMatrix4x4& matrix, const QVector3D& vector);
00141 #endif
00142 };
00143 
00144 Q_DECLARE_TYPEINFO(QVector3D, Q_MOVABLE_TYPE);
00145 
00146 inline QVector3D::QVector3D() : xp(0.0f), yp(0.0f), zp(0.0f) {}
00147 
00148 inline QVector3D::QVector3D(qreal xpos, qreal ypos, qreal zpos) : xp(xpos), yp(ypos), zp(zpos) {}
00149 
00150 inline QVector3D::QVector3D(float xpos, float ypos, float zpos, int) : xp(xpos), yp(ypos), zp(zpos) {}
00151 
00152 inline QVector3D::QVector3D(const QPoint& point) : xp(point.x()), yp(point.y()), zp(0.0f) {}
00153 
00154 inline QVector3D::QVector3D(const QPointF& point) : xp(point.x()), yp(point.y()), zp(0.0f) {}
00155 
00156 inline bool QVector3D::isNull() const
00157 {
00158     return qIsNull(xp) && qIsNull(yp) && qIsNull(zp);
00159 }
00160 
00161 inline qreal QVector3D::x() const { return qreal(xp); }
00162 inline qreal QVector3D::y() const { return qreal(yp); }
00163 inline qreal QVector3D::z() const { return qreal(zp); }
00164 
00165 inline void QVector3D::setX(qreal aX) { xp = aX; }
00166 inline void QVector3D::setY(qreal aY) { yp = aY; }
00167 inline void QVector3D::setZ(qreal aZ) { zp = aZ; }
00168 
00169 inline QVector3D &QVector3D::operator+=(const QVector3D &vector)
00170 {
00171     xp += vector.xp;
00172     yp += vector.yp;
00173     zp += vector.zp;
00174     return *this;
00175 }
00176 
00177 inline QVector3D &QVector3D::operator-=(const QVector3D &vector)
00178 {
00179     xp -= vector.xp;
00180     yp -= vector.yp;
00181     zp -= vector.zp;
00182     return *this;
00183 }
00184 
00185 inline QVector3D &QVector3D::operator*=(qreal factor)
00186 {
00187     xp *= factor;
00188     yp *= factor;
00189     zp *= factor;
00190     return *this;
00191 }
00192 
00193 inline QVector3D &QVector3D::operator*=(const QVector3D& vector)
00194 {
00195     xp *= vector.xp;
00196     yp *= vector.yp;
00197     zp *= vector.zp;
00198     return *this;
00199 }
00200 
00201 inline QVector3D &QVector3D::operator/=(qreal divisor)
00202 {
00203     xp /= divisor;
00204     yp /= divisor;
00205     zp /= divisor;
00206     return *this;
00207 }
00208 
00209 inline bool operator==(const QVector3D &v1, const QVector3D &v2)
00210 {
00211     return v1.xp == v2.xp && v1.yp == v2.yp && v1.zp == v2.zp;
00212 }
00213 
00214 inline bool operator!=(const QVector3D &v1, const QVector3D &v2)
00215 {
00216     return v1.xp != v2.xp || v1.yp != v2.yp || v1.zp != v2.zp;
00217 }
00218 
00219 inline const QVector3D operator+(const QVector3D &v1, const QVector3D &v2)
00220 {
00221     return QVector3D(v1.xp + v2.xp, v1.yp + v2.yp, v1.zp + v2.zp, 1);
00222 }
00223 
00224 inline const QVector3D operator-(const QVector3D &v1, const QVector3D &v2)
00225 {
00226     return QVector3D(v1.xp - v2.xp, v1.yp - v2.yp, v1.zp - v2.zp, 1);
00227 }
00228 
00229 inline const QVector3D operator*(qreal factor, const QVector3D &vector)
00230 {
00231     return QVector3D(vector.xp * factor, vector.yp * factor, vector.zp * factor, 1);
00232 }
00233 
00234 inline const QVector3D operator*(const QVector3D &vector, qreal factor)
00235 {
00236     return QVector3D(vector.xp * factor, vector.yp * factor, vector.zp * factor, 1);
00237 }
00238 
00239 inline const QVector3D operator*(const QVector3D &v1, const QVector3D& v2)
00240 {
00241     return QVector3D(v1.xp * v2.xp, v1.yp * v2.yp, v1.zp * v2.zp, 1);
00242 }
00243 
00244 inline const QVector3D operator-(const QVector3D &vector)
00245 {
00246     return QVector3D(-vector.xp, -vector.yp, -vector.zp, 1);
00247 }
00248 
00249 inline const QVector3D operator/(const QVector3D &vector, qreal divisor)
00250 {
00251     return QVector3D(vector.xp / divisor, vector.yp / divisor, vector.zp / divisor, 1);
00252 }
00253 
00254 inline bool qFuzzyCompare(const QVector3D& v1, const QVector3D& v2)
00255 {
00256     return qFuzzyCompare(v1.xp, v2.xp) &&
00257            qFuzzyCompare(v1.yp, v2.yp) &&
00258            qFuzzyCompare(v1.zp, v2.zp);
00259 }
00260 
00261 inline QPoint QVector3D::toPoint() const
00262 {
00263     return QPoint(qRound(xp), qRound(yp));
00264 }
00265 
00266 inline QPointF QVector3D::toPointF() const
00267 {
00268     return QPointF(qreal(xp), qreal(yp));
00269 }
00270 
00271 #ifndef QT_NO_DEBUG_STREAM
00272 Q_GUI_EXPORT QDebug operator<<(QDebug dbg, const QVector3D &vector);
00273 #endif
00274 
00275 #ifndef QT_NO_DATASTREAM
00276 Q_GUI_EXPORT QDataStream &operator<<(QDataStream &, const QVector3D &);
00277 Q_GUI_EXPORT QDataStream &operator>>(QDataStream &, QVector3D &);
00278 #endif
00279 
00280 #endif
00281 
00282 QT_END_NAMESPACE
00283 
00284 QT_END_HEADER
00285 
00286 #endif