src/gui/math3d/qvector3d.h
changeset 0 1918ee327afb
child 4 3b1da2848fc7
equal deleted inserted replaced
-1:000000000000 0:1918ee327afb
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     4 ** All rights reserved.
       
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
       
     6 **
       
     7 ** This file is part of the QtGui module of the Qt Toolkit.
       
     8 **
       
     9 ** $QT_BEGIN_LICENSE:LGPL$
       
    10 ** No Commercial Usage
       
    11 ** This file contains pre-release code and may not be distributed.
       
    12 ** You may use this file in accordance with the terms and conditions
       
    13 ** contained in the Technology Preview License Agreement accompanying
       
    14 ** this package.
       
    15 **
       
    16 ** GNU Lesser General Public License Usage
       
    17 ** Alternatively, this file may be used under the terms of the GNU Lesser
       
    18 ** General Public License version 2.1 as published by the Free Software
       
    19 ** Foundation and appearing in the file LICENSE.LGPL included in the
       
    20 ** packaging of this file.  Please review the following information to
       
    21 ** ensure the GNU Lesser General Public License version 2.1 requirements
       
    22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    23 **
       
    24 ** In addition, as a special exception, Nokia gives you certain additional
       
    25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
       
    26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
       
    27 **
       
    28 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    32 **
       
    33 **
       
    34 **
       
    35 **
       
    36 **
       
    37 **
       
    38 ** $QT_END_LICENSE$
       
    39 **
       
    40 ****************************************************************************/
       
    41 
       
    42 #ifndef QVECTOR3D_H
       
    43 #define QVECTOR3D_H
       
    44 
       
    45 #include <QtCore/qpoint.h>
       
    46 #include <QtCore/qmetatype.h>
       
    47 
       
    48 QT_BEGIN_HEADER
       
    49 
       
    50 QT_BEGIN_NAMESPACE
       
    51 
       
    52 QT_MODULE(Gui)
       
    53 
       
    54 class QMatrix4x4;
       
    55 class QVector2D;
       
    56 class QVector4D;
       
    57 
       
    58 #ifndef QT_NO_VECTOR3D
       
    59 
       
    60 class Q_GUI_EXPORT QVector3D
       
    61 {
       
    62 public:
       
    63     QVector3D();
       
    64     QVector3D(qreal xpos, qreal ypos, qreal zpos);
       
    65     explicit QVector3D(const QPoint& point);
       
    66     explicit QVector3D(const QPointF& point);
       
    67 #ifndef QT_NO_VECTOR2D
       
    68     QVector3D(const QVector2D& vector);
       
    69     QVector3D(const QVector2D& vector, qreal zpos);
       
    70 #endif
       
    71 #ifndef QT_NO_VECTOR4D
       
    72     explicit QVector3D(const QVector4D& vector);
       
    73 #endif
       
    74 
       
    75     bool isNull() const;
       
    76 
       
    77     qreal x() const;
       
    78     qreal y() const;
       
    79     qreal z() const;
       
    80 
       
    81     void setX(qreal x);
       
    82     void setY(qreal y);
       
    83     void setZ(qreal z);
       
    84 
       
    85     qreal length() const;
       
    86     qreal lengthSquared() const;
       
    87 
       
    88     QVector3D normalized() const;
       
    89     void normalize();
       
    90 
       
    91     QVector3D &operator+=(const QVector3D &vector);
       
    92     QVector3D &operator-=(const QVector3D &vector);
       
    93     QVector3D &operator*=(qreal factor);
       
    94     QVector3D &operator*=(const QVector3D& vector);
       
    95     QVector3D &operator/=(qreal divisor);
       
    96 
       
    97     static qreal dotProduct(const QVector3D& v1, const QVector3D& v2);
       
    98     static QVector3D crossProduct(const QVector3D& v1, const QVector3D& v2);
       
    99     static QVector3D normal(const QVector3D& v1, const QVector3D& v2);
       
   100     static QVector3D normal
       
   101         (const QVector3D& v1, const QVector3D& v2, const QVector3D& v3);
       
   102 
       
   103     qreal distanceToPlane(const QVector3D& plane, const QVector3D& normal) const;
       
   104     qreal distanceToPlane(const QVector3D& plane1, const QVector3D& plane2, const QVector3D& plane3) const;
       
   105     qreal distanceToLine(const QVector3D& point, const QVector3D& direction) const;
       
   106 
       
   107     friend inline bool operator==(const QVector3D &v1, const QVector3D &v2);
       
   108     friend inline bool operator!=(const QVector3D &v1, const QVector3D &v2);
       
   109     friend inline const QVector3D operator+(const QVector3D &v1, const QVector3D &v2);
       
   110     friend inline const QVector3D operator-(const QVector3D &v1, const QVector3D &v2);
       
   111     friend inline const QVector3D operator*(qreal factor, const QVector3D &vector);
       
   112     friend inline const QVector3D operator*(const QVector3D &vector, qreal factor);
       
   113     friend const QVector3D operator*(const QVector3D &v1, const QVector3D& v2);
       
   114     friend inline const QVector3D operator-(const QVector3D &vector);
       
   115     friend inline const QVector3D operator/(const QVector3D &vector, qreal divisor);
       
   116 
       
   117     friend inline bool qFuzzyCompare(const QVector3D& v1, const QVector3D& v2);
       
   118 
       
   119 #ifndef QT_NO_VECTOR2D
       
   120     QVector2D toVector2D() const;
       
   121 #endif
       
   122 #ifndef QT_NO_VECTOR4D
       
   123     QVector4D toVector4D() const;
       
   124 #endif
       
   125 
       
   126     QPoint toPoint() const;
       
   127     QPointF toPointF() const;
       
   128 
       
   129     operator QVariant() const;
       
   130 
       
   131 private:
       
   132     float xp, yp, zp;
       
   133 
       
   134     QVector3D(float xpos, float ypos, float zpos, int dummy);
       
   135 
       
   136     friend class QVector2D;
       
   137     friend class QVector4D;
       
   138 #ifndef QT_NO_MATRIX4X4
       
   139     friend QVector3D operator*(const QVector3D& vector, const QMatrix4x4& matrix);
       
   140     friend QVector3D operator*(const QMatrix4x4& matrix, const QVector3D& vector);
       
   141 #endif
       
   142 };
       
   143 
       
   144 inline QVector3D::QVector3D() : xp(0.0f), yp(0.0f), zp(0.0f) {}
       
   145 
       
   146 inline QVector3D::QVector3D(qreal xpos, qreal ypos, qreal zpos) : xp(xpos), yp(ypos), zp(zpos) {}
       
   147 
       
   148 inline QVector3D::QVector3D(float xpos, float ypos, float zpos, int) : xp(xpos), yp(ypos), zp(zpos) {}
       
   149 
       
   150 inline QVector3D::QVector3D(const QPoint& point) : xp(point.x()), yp(point.y()), zp(0.0f) {}
       
   151 
       
   152 inline QVector3D::QVector3D(const QPointF& point) : xp(point.x()), yp(point.y()), zp(0.0f) {}
       
   153 
       
   154 inline bool QVector3D::isNull() const
       
   155 {
       
   156     return qIsNull(xp) && qIsNull(yp) && qIsNull(zp);
       
   157 }
       
   158 
       
   159 inline qreal QVector3D::x() const { return qreal(xp); }
       
   160 inline qreal QVector3D::y() const { return qreal(yp); }
       
   161 inline qreal QVector3D::z() const { return qreal(zp); }
       
   162 
       
   163 inline void QVector3D::setX(qreal aX) { xp = aX; }
       
   164 inline void QVector3D::setY(qreal aY) { yp = aY; }
       
   165 inline void QVector3D::setZ(qreal aZ) { zp = aZ; }
       
   166 
       
   167 inline QVector3D &QVector3D::operator+=(const QVector3D &vector)
       
   168 {
       
   169     xp += vector.xp;
       
   170     yp += vector.yp;
       
   171     zp += vector.zp;
       
   172     return *this;
       
   173 }
       
   174 
       
   175 inline QVector3D &QVector3D::operator-=(const QVector3D &vector)
       
   176 {
       
   177     xp -= vector.xp;
       
   178     yp -= vector.yp;
       
   179     zp -= vector.zp;
       
   180     return *this;
       
   181 }
       
   182 
       
   183 inline QVector3D &QVector3D::operator*=(qreal factor)
       
   184 {
       
   185     xp *= factor;
       
   186     yp *= factor;
       
   187     zp *= factor;
       
   188     return *this;
       
   189 }
       
   190 
       
   191 inline QVector3D &QVector3D::operator*=(const QVector3D& vector)
       
   192 {
       
   193     xp *= vector.xp;
       
   194     yp *= vector.yp;
       
   195     zp *= vector.zp;
       
   196     return *this;
       
   197 }
       
   198 
       
   199 inline QVector3D &QVector3D::operator/=(qreal divisor)
       
   200 {
       
   201     xp /= divisor;
       
   202     yp /= divisor;
       
   203     zp /= divisor;
       
   204     return *this;
       
   205 }
       
   206 
       
   207 inline bool operator==(const QVector3D &v1, const QVector3D &v2)
       
   208 {
       
   209     return v1.xp == v2.xp && v1.yp == v2.yp && v1.zp == v2.zp;
       
   210 }
       
   211 
       
   212 inline bool operator!=(const QVector3D &v1, const QVector3D &v2)
       
   213 {
       
   214     return v1.xp != v2.xp || v1.yp != v2.yp || v1.zp != v2.zp;
       
   215 }
       
   216 
       
   217 inline const QVector3D operator+(const QVector3D &v1, const QVector3D &v2)
       
   218 {
       
   219     return QVector3D(v1.xp + v2.xp, v1.yp + v2.yp, v1.zp + v2.zp, 1);
       
   220 }
       
   221 
       
   222 inline const QVector3D operator-(const QVector3D &v1, const QVector3D &v2)
       
   223 {
       
   224     return QVector3D(v1.xp - v2.xp, v1.yp - v2.yp, v1.zp - v2.zp, 1);
       
   225 }
       
   226 
       
   227 inline const QVector3D operator*(qreal factor, const QVector3D &vector)
       
   228 {
       
   229     return QVector3D(vector.xp * factor, vector.yp * factor, vector.zp * factor, 1);
       
   230 }
       
   231 
       
   232 inline const QVector3D operator*(const QVector3D &vector, qreal factor)
       
   233 {
       
   234     return QVector3D(vector.xp * factor, vector.yp * factor, vector.zp * factor, 1);
       
   235 }
       
   236 
       
   237 inline const QVector3D operator*(const QVector3D &v1, const QVector3D& v2)
       
   238 {
       
   239     return QVector3D(v1.xp * v2.xp, v1.yp * v2.yp, v1.zp * v2.zp, 1);
       
   240 }
       
   241 
       
   242 inline const QVector3D operator-(const QVector3D &vector)
       
   243 {
       
   244     return QVector3D(-vector.xp, -vector.yp, -vector.zp, 1);
       
   245 }
       
   246 
       
   247 inline const QVector3D operator/(const QVector3D &vector, qreal divisor)
       
   248 {
       
   249     return QVector3D(vector.xp / divisor, vector.yp / divisor, vector.zp / divisor, 1);
       
   250 }
       
   251 
       
   252 inline bool qFuzzyCompare(const QVector3D& v1, const QVector3D& v2)
       
   253 {
       
   254     return qFuzzyCompare(v1.xp, v2.xp) &&
       
   255            qFuzzyCompare(v1.yp, v2.yp) &&
       
   256            qFuzzyCompare(v1.zp, v2.zp);
       
   257 }
       
   258 
       
   259 inline QPoint QVector3D::toPoint() const
       
   260 {
       
   261     return QPoint(qRound(xp), qRound(yp));
       
   262 }
       
   263 
       
   264 inline QPointF QVector3D::toPointF() const
       
   265 {
       
   266     return QPointF(qreal(xp), qreal(yp));
       
   267 }
       
   268 
       
   269 #ifndef QT_NO_DEBUG_STREAM
       
   270 Q_GUI_EXPORT QDebug operator<<(QDebug dbg, const QVector3D &vector);
       
   271 #endif
       
   272 
       
   273 #ifndef QT_NO_DATASTREAM
       
   274 Q_GUI_EXPORT QDataStream &operator<<(QDataStream &, const QVector3D &);
       
   275 Q_GUI_EXPORT QDataStream &operator>>(QDataStream &, QVector3D &);
       
   276 #endif
       
   277 
       
   278 #endif
       
   279 
       
   280 QT_END_NAMESPACE
       
   281 
       
   282 QT_END_HEADER
       
   283 
       
   284 #endif