| Absolute File Name: | /home/qt/qt5_coco/qt5/qtbase/src/gui/math3d/qvector3d.cpp |
| Source code | Switch to Preprocessed file |
| Line | Source | Count | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 1 | /**************************************************************************** | - | ||||||||||||
| 2 | ** | - | ||||||||||||
| 3 | ** Copyright (C) 2015 The Qt Company Ltd. | - | ||||||||||||
| 4 | ** Contact: http://www.qt.io/licensing/ | - | ||||||||||||
| 5 | ** | - | ||||||||||||
| 6 | ** This file is part of the QtGui module of the Qt Toolkit. | - | ||||||||||||
| 7 | ** | - | ||||||||||||
| 8 | ** $QT_BEGIN_LICENSE:LGPL21$ | - | ||||||||||||
| 9 | ** Commercial License Usage | - | ||||||||||||
| 10 | ** Licensees holding valid commercial Qt licenses may use this file in | - | ||||||||||||
| 11 | ** accordance with the commercial license agreement provided with the | - | ||||||||||||
| 12 | ** Software or, alternatively, in accordance with the terms contained in | - | ||||||||||||
| 13 | ** a written agreement between you and The Qt Company. For licensing terms | - | ||||||||||||
| 14 | ** and conditions see http://www.qt.io/terms-conditions. For further | - | ||||||||||||
| 15 | ** information use the contact form at http://www.qt.io/contact-us. | - | ||||||||||||
| 16 | ** | - | ||||||||||||
| 17 | ** GNU Lesser General Public License Usage | - | ||||||||||||
| 18 | ** Alternatively, this file may be used under the terms of the GNU Lesser | - | ||||||||||||
| 19 | ** General Public License version 2.1 or version 3 as published by the Free | - | ||||||||||||
| 20 | ** Software Foundation and appearing in the file LICENSE.LGPLv21 and | - | ||||||||||||
| 21 | ** LICENSE.LGPLv3 included in the packaging of this file. Please review the | - | ||||||||||||
| 22 | ** following information to ensure the GNU Lesser General Public License | - | ||||||||||||
| 23 | ** requirements will be met: https://www.gnu.org/licenses/lgpl.html and | - | ||||||||||||
| 24 | ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. | - | ||||||||||||
| 25 | ** | - | ||||||||||||
| 26 | ** As a special exception, The Qt Company gives you certain additional | - | ||||||||||||
| 27 | ** rights. These rights are described in The Qt Company LGPL Exception | - | ||||||||||||
| 28 | ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. | - | ||||||||||||
| 29 | ** | - | ||||||||||||
| 30 | ** $QT_END_LICENSE$ | - | ||||||||||||
| 31 | ** | - | ||||||||||||
| 32 | ****************************************************************************/ | - | ||||||||||||
| 33 | - | |||||||||||||
| 34 | #include "qvector3d.h" | - | ||||||||||||
| 35 | #include "qvector2d.h" | - | ||||||||||||
| 36 | #include "qvector4d.h" | - | ||||||||||||
| 37 | #include "qmatrix4x4.h" | - | ||||||||||||
| 38 | #include <QtCore/qdatastream.h> | - | ||||||||||||
| 39 | #include <QtCore/qmath.h> | - | ||||||||||||
| 40 | #include <QtCore/qvariant.h> | - | ||||||||||||
| 41 | #include <QtCore/qdebug.h> | - | ||||||||||||
| 42 | #include <QtCore/qrect.h> | - | ||||||||||||
| 43 | - | |||||||||||||
| 44 | QT_BEGIN_NAMESPACE | - | ||||||||||||
| 45 | - | |||||||||||||
| 46 | #ifndef QT_NO_VECTOR3D | - | ||||||||||||
| 47 | - | |||||||||||||
| 48 | /*! | - | ||||||||||||
| 49 | \class QVector3D | - | ||||||||||||
| 50 | \brief The QVector3D class represents a vector or vertex in 3D space. | - | ||||||||||||
| 51 | \since 4.6 | - | ||||||||||||
| 52 | \ingroup painting-3D | - | ||||||||||||
| 53 | \inmodule QtGui | - | ||||||||||||
| 54 | - | |||||||||||||
| 55 | Vectors are one of the main building blocks of 3D representation and | - | ||||||||||||
| 56 | drawing. They consist of three coordinates, traditionally called | - | ||||||||||||
| 57 | x, y, and z. | - | ||||||||||||
| 58 | - | |||||||||||||
| 59 | The QVector3D class can also be used to represent vertices in 3D space. | - | ||||||||||||
| 60 | We therefore do not need to provide a separate vertex class. | - | ||||||||||||
| 61 | - | |||||||||||||
| 62 | \sa QVector2D, QVector4D, QQuaternion | - | ||||||||||||
| 63 | */ | - | ||||||||||||
| 64 | - | |||||||||||||
| 65 | /*! | - | ||||||||||||
| 66 | \fn QVector3D::QVector3D() | - | ||||||||||||
| 67 | - | |||||||||||||
| 68 | Constructs a null vector, i.e. with coordinates (0, 0, 0). | - | ||||||||||||
| 69 | */ | - | ||||||||||||
| 70 | - | |||||||||||||
| 71 | /*! | - | ||||||||||||
| 72 | \fn QVector3D::QVector3D(Qt::Initialization) | - | ||||||||||||
| 73 | \since 5.5 | - | ||||||||||||
| 74 | \internal | - | ||||||||||||
| 75 | - | |||||||||||||
| 76 | Constructs a vector without initializing the contents. | - | ||||||||||||
| 77 | */ | - | ||||||||||||
| 78 | - | |||||||||||||
| 79 | /*! | - | ||||||||||||
| 80 | \fn QVector3D::QVector3D(float xpos, float ypos, float zpos) | - | ||||||||||||
| 81 | - | |||||||||||||
| 82 | Constructs a vector with coordinates (\a xpos, \a ypos, \a zpos). | - | ||||||||||||
| 83 | */ | - | ||||||||||||
| 84 | - | |||||||||||||
| 85 | /*! | - | ||||||||||||
| 86 | \fn QVector3D::QVector3D(const QPoint& point) | - | ||||||||||||
| 87 | - | |||||||||||||
| 88 | Constructs a vector with x and y coordinates from a 2D \a point, and a | - | ||||||||||||
| 89 | z coordinate of 0. | - | ||||||||||||
| 90 | */ | - | ||||||||||||
| 91 | - | |||||||||||||
| 92 | /*! | - | ||||||||||||
| 93 | \fn QVector3D::QVector3D(const QPointF& point) | - | ||||||||||||
| 94 | - | |||||||||||||
| 95 | Constructs a vector with x and y coordinates from a 2D \a point, and a | - | ||||||||||||
| 96 | z coordinate of 0. | - | ||||||||||||
| 97 | */ | - | ||||||||||||
| 98 | - | |||||||||||||
| 99 | #ifndef QT_NO_VECTOR2D | - | ||||||||||||
| 100 | - | |||||||||||||
| 101 | /*! | - | ||||||||||||
| 102 | Constructs a 3D vector from the specified 2D \a vector. The z | - | ||||||||||||
| 103 | coordinate is set to zero. | - | ||||||||||||
| 104 | - | |||||||||||||
| 105 | \sa toVector2D() | - | ||||||||||||
| 106 | */ | - | ||||||||||||
| 107 | QVector3D::QVector3D(const QVector2D& vector) | - | ||||||||||||
| 108 | { | - | ||||||||||||
| 109 | xp = vector.xp; | - | ||||||||||||
| 110 | yp = vector.yp; | - | ||||||||||||
| 111 | zp = 0.0f; | - | ||||||||||||
| 112 | } never executed: end of block | 0 | ||||||||||||
| 113 | - | |||||||||||||
| 114 | /*! | - | ||||||||||||
| 115 | Constructs a 3D vector from the specified 2D \a vector. The z | - | ||||||||||||
| 116 | coordinate is set to \a zpos. | - | ||||||||||||
| 117 | - | |||||||||||||
| 118 | \sa toVector2D() | - | ||||||||||||
| 119 | */ | - | ||||||||||||
| 120 | QVector3D::QVector3D(const QVector2D& vector, float zpos) | - | ||||||||||||
| 121 | { | - | ||||||||||||
| 122 | xp = vector.xp; | - | ||||||||||||
| 123 | yp = vector.yp; | - | ||||||||||||
| 124 | zp = zpos; | - | ||||||||||||
| 125 | } never executed: end of block | 0 | ||||||||||||
| 126 | - | |||||||||||||
| 127 | #endif | - | ||||||||||||
| 128 | - | |||||||||||||
| 129 | #ifndef QT_NO_VECTOR4D | - | ||||||||||||
| 130 | - | |||||||||||||
| 131 | /*! | - | ||||||||||||
| 132 | Constructs a 3D vector from the specified 4D \a vector. The w | - | ||||||||||||
| 133 | coordinate is dropped. | - | ||||||||||||
| 134 | - | |||||||||||||
| 135 | \sa toVector4D() | - | ||||||||||||
| 136 | */ | - | ||||||||||||
| 137 | QVector3D::QVector3D(const QVector4D& vector) | - | ||||||||||||
| 138 | { | - | ||||||||||||
| 139 | xp = vector.xp; | - | ||||||||||||
| 140 | yp = vector.yp; | - | ||||||||||||
| 141 | zp = vector.zp; | - | ||||||||||||
| 142 | } never executed: end of block | 0 | ||||||||||||
| 143 | - | |||||||||||||
| 144 | #endif | - | ||||||||||||
| 145 | - | |||||||||||||
| 146 | /*! | - | ||||||||||||
| 147 | \fn bool QVector3D::isNull() const | - | ||||||||||||
| 148 | - | |||||||||||||
| 149 | Returns \c true if the x, y, and z coordinates are set to 0.0, | - | ||||||||||||
| 150 | otherwise returns \c false. | - | ||||||||||||
| 151 | */ | - | ||||||||||||
| 152 | - | |||||||||||||
| 153 | /*! | - | ||||||||||||
| 154 | \fn float QVector3D::x() const | - | ||||||||||||
| 155 | - | |||||||||||||
| 156 | Returns the x coordinate of this point. | - | ||||||||||||
| 157 | - | |||||||||||||
| 158 | \sa setX(), y(), z() | - | ||||||||||||
| 159 | */ | - | ||||||||||||
| 160 | - | |||||||||||||
| 161 | /*! | - | ||||||||||||
| 162 | \fn float QVector3D::y() const | - | ||||||||||||
| 163 | - | |||||||||||||
| 164 | Returns the y coordinate of this point. | - | ||||||||||||
| 165 | - | |||||||||||||
| 166 | \sa setY(), x(), z() | - | ||||||||||||
| 167 | */ | - | ||||||||||||
| 168 | - | |||||||||||||
| 169 | /*! | - | ||||||||||||
| 170 | \fn float QVector3D::z() const | - | ||||||||||||
| 171 | - | |||||||||||||
| 172 | Returns the z coordinate of this point. | - | ||||||||||||
| 173 | - | |||||||||||||
| 174 | \sa setZ(), x(), y() | - | ||||||||||||
| 175 | */ | - | ||||||||||||
| 176 | - | |||||||||||||
| 177 | /*! | - | ||||||||||||
| 178 | \fn void QVector3D::setX(float x) | - | ||||||||||||
| 179 | - | |||||||||||||
| 180 | Sets the x coordinate of this point to the given \a x coordinate. | - | ||||||||||||
| 181 | - | |||||||||||||
| 182 | \sa x(), setY(), setZ() | - | ||||||||||||
| 183 | */ | - | ||||||||||||
| 184 | - | |||||||||||||
| 185 | /*! | - | ||||||||||||
| 186 | \fn void QVector3D::setY(float y) | - | ||||||||||||
| 187 | - | |||||||||||||
| 188 | Sets the y coordinate of this point to the given \a y coordinate. | - | ||||||||||||
| 189 | - | |||||||||||||
| 190 | \sa y(), setX(), setZ() | - | ||||||||||||
| 191 | */ | - | ||||||||||||
| 192 | - | |||||||||||||
| 193 | /*! | - | ||||||||||||
| 194 | \fn void QVector3D::setZ(float z) | - | ||||||||||||
| 195 | - | |||||||||||||
| 196 | Sets the z coordinate of this point to the given \a z coordinate. | - | ||||||||||||
| 197 | - | |||||||||||||
| 198 | \sa z(), setX(), setY() | - | ||||||||||||
| 199 | */ | - | ||||||||||||
| 200 | - | |||||||||||||
| 201 | /*! \fn float &QVector3D::operator[](int i) | - | ||||||||||||
| 202 | \since 5.2 | - | ||||||||||||
| 203 | - | |||||||||||||
| 204 | Returns the component of the vector at index position \a i | - | ||||||||||||
| 205 | as a modifiable reference. | - | ||||||||||||
| 206 | - | |||||||||||||
| 207 | \a i must be a valid index position in the vector (i.e., 0 <= \a i | - | ||||||||||||
| 208 | < 3). | - | ||||||||||||
| 209 | */ | - | ||||||||||||
| 210 | - | |||||||||||||
| 211 | /*! \fn float QVector3D::operator[](int i) const | - | ||||||||||||
| 212 | \since 5.2 | - | ||||||||||||
| 213 | - | |||||||||||||
| 214 | Returns the component of the vector at index position \a i. | - | ||||||||||||
| 215 | - | |||||||||||||
| 216 | \a i must be a valid index position in the vector (i.e., 0 <= \a i | - | ||||||||||||
| 217 | < 3). | - | ||||||||||||
| 218 | */ | - | ||||||||||||
| 219 | - | |||||||||||||
| 220 | /*! | - | ||||||||||||
| 221 | Returns the normalized unit vector form of this vector. | - | ||||||||||||
| 222 | - | |||||||||||||
| 223 | If this vector is null, then a null vector is returned. If the length | - | ||||||||||||
| 224 | of the vector is very close to 1, then the vector will be returned as-is. | - | ||||||||||||
| 225 | Otherwise the normalized form of the vector of length 1 will be returned. | - | ||||||||||||
| 226 | - | |||||||||||||
| 227 | \sa length(), normalize() | - | ||||||||||||
| 228 | */ | - | ||||||||||||
| 229 | QVector3D QVector3D::normalized() const | - | ||||||||||||
| 230 | { | - | ||||||||||||
| 231 | // Need some extra precision if the length is very small. | - | ||||||||||||
| 232 | double len = double(xp) * double(xp) + | - | ||||||||||||
| 233 | double(yp) * double(yp) + | - | ||||||||||||
| 234 | double(zp) * double(zp); | - | ||||||||||||
| 235 | if (qFuzzyIsNull(len - 1.0f)) {
| 0 | ||||||||||||
| 236 | return *this; never executed: return *this; | 0 | ||||||||||||
| 237 | } else if (!qFuzzyIsNull(len)) {
| 0 | ||||||||||||
| 238 | double sqrtLen = std::sqrt(len); | - | ||||||||||||
| 239 | return QVector3D(float(double(xp) / sqrtLen), never executed: return QVector3D(float(double(xp) / sqrtLen), float(double(yp) / sqrtLen), float(double(zp) / sqrtLen)); | 0 | ||||||||||||
| 240 | float(double(yp) / sqrtLen), never executed: return QVector3D(float(double(xp) / sqrtLen), float(double(yp) / sqrtLen), float(double(zp) / sqrtLen)); | 0 | ||||||||||||
| 241 | float(double(zp) / sqrtLen)); never executed: return QVector3D(float(double(xp) / sqrtLen), float(double(yp) / sqrtLen), float(double(zp) / sqrtLen)); | 0 | ||||||||||||
| 242 | } else { | - | ||||||||||||
| 243 | return QVector3D(); never executed: return QVector3D(); | 0 | ||||||||||||
| 244 | } | - | ||||||||||||
| 245 | } | - | ||||||||||||
| 246 | - | |||||||||||||
| 247 | /*! | - | ||||||||||||
| 248 | Normalizes the currect vector in place. Nothing happens if this | - | ||||||||||||
| 249 | vector is a null vector or the length of the vector is very close to 1. | - | ||||||||||||
| 250 | - | |||||||||||||
| 251 | \sa length(), normalized() | - | ||||||||||||
| 252 | */ | - | ||||||||||||
| 253 | void QVector3D::normalize() | - | ||||||||||||
| 254 | { | - | ||||||||||||
| 255 | // Need some extra precision if the length is very small. | - | ||||||||||||
| 256 | double len = double(xp) * double(xp) + | - | ||||||||||||
| 257 | double(yp) * double(yp) + | - | ||||||||||||
| 258 | double(zp) * double(zp); | - | ||||||||||||
| 259 | if (qFuzzyIsNull(len - 1.0f) || qFuzzyIsNull(len))
| 0 | ||||||||||||
| 260 | return; never executed: return; | 0 | ||||||||||||
| 261 | - | |||||||||||||
| 262 | len = std::sqrt(len); | - | ||||||||||||
| 263 | - | |||||||||||||
| 264 | xp = float(double(xp) / len); | - | ||||||||||||
| 265 | yp = float(double(yp) / len); | - | ||||||||||||
| 266 | zp = float(double(zp) / len); | - | ||||||||||||
| 267 | } never executed: end of block | 0 | ||||||||||||
| 268 | - | |||||||||||||
| 269 | /*! | - | ||||||||||||
| 270 | \fn QVector3D &QVector3D::operator+=(const QVector3D &vector) | - | ||||||||||||
| 271 | - | |||||||||||||
| 272 | Adds the given \a vector to this vector and returns a reference to | - | ||||||||||||
| 273 | this vector. | - | ||||||||||||
| 274 | - | |||||||||||||
| 275 | \sa operator-=() | - | ||||||||||||
| 276 | */ | - | ||||||||||||
| 277 | - | |||||||||||||
| 278 | /*! | - | ||||||||||||
| 279 | \fn QVector3D &QVector3D::operator-=(const QVector3D &vector) | - | ||||||||||||
| 280 | - | |||||||||||||
| 281 | Subtracts the given \a vector from this vector and returns a reference to | - | ||||||||||||
| 282 | this vector. | - | ||||||||||||
| 283 | - | |||||||||||||
| 284 | \sa operator+=() | - | ||||||||||||
| 285 | */ | - | ||||||||||||
| 286 | - | |||||||||||||
| 287 | /*! | - | ||||||||||||
| 288 | \fn QVector3D &QVector3D::operator*=(float factor) | - | ||||||||||||
| 289 | - | |||||||||||||
| 290 | Multiplies this vector's coordinates by the given \a factor, and | - | ||||||||||||
| 291 | returns a reference to this vector. | - | ||||||||||||
| 292 | - | |||||||||||||
| 293 | \sa operator/=() | - | ||||||||||||
| 294 | */ | - | ||||||||||||
| 295 | - | |||||||||||||
| 296 | /*! | - | ||||||||||||
| 297 | \fn QVector3D &QVector3D::operator*=(const QVector3D& vector) | - | ||||||||||||
| 298 | \overload | - | ||||||||||||
| 299 | - | |||||||||||||
| 300 | Multiplies the components of this vector by the corresponding | - | ||||||||||||
| 301 | components in \a vector. | - | ||||||||||||
| 302 | - | |||||||||||||
| 303 | Note: this is not the same as the crossProduct() of this | - | ||||||||||||
| 304 | vector and \a vector. | - | ||||||||||||
| 305 | - | |||||||||||||
| 306 | \sa crossProduct() | - | ||||||||||||
| 307 | */ | - | ||||||||||||
| 308 | - | |||||||||||||
| 309 | /*! | - | ||||||||||||
| 310 | \fn QVector3D &QVector3D::operator/=(float divisor) | - | ||||||||||||
| 311 | - | |||||||||||||
| 312 | Divides this vector's coordinates by the given \a divisor, and | - | ||||||||||||
| 313 | returns a reference to this vector. | - | ||||||||||||
| 314 | - | |||||||||||||
| 315 | \sa operator*=() | - | ||||||||||||
| 316 | */ | - | ||||||||||||
| 317 | - | |||||||||||||
| 318 | /*! | - | ||||||||||||
| 319 | \fn QVector3D &QVector3D::operator/=(const QVector3D &vector) | - | ||||||||||||
| 320 | \since 5.5 | - | ||||||||||||
| 321 | - | |||||||||||||
| 322 | Divides the components of this vector by the corresponding | - | ||||||||||||
| 323 | components in \a vector. | - | ||||||||||||
| 324 | - | |||||||||||||
| 325 | \sa operator*=() | - | ||||||||||||
| 326 | */ | - | ||||||||||||
| 327 | - | |||||||||||||
| 328 | /*! | - | ||||||||||||
| 329 | Returns the dot product of \a v1 and \a v2. | - | ||||||||||||
| 330 | */ | - | ||||||||||||
| 331 | float QVector3D::dotProduct(const QVector3D& v1, const QVector3D& v2) | - | ||||||||||||
| 332 | { | - | ||||||||||||
| 333 | return v1.xp * v2.xp + v1.yp * v2.yp + v1.zp * v2.zp; never executed: return v1.xp * v2.xp + v1.yp * v2.yp + v1.zp * v2.zp; | 0 | ||||||||||||
| 334 | } | - | ||||||||||||
| 335 | - | |||||||||||||
| 336 | /*! | - | ||||||||||||
| 337 | Returns the cross-product of vectors \a v1 and \a v2, which corresponds | - | ||||||||||||
| 338 | to the normal vector of a plane defined by \a v1 and \a v2. | - | ||||||||||||
| 339 | - | |||||||||||||
| 340 | \sa normal() | - | ||||||||||||
| 341 | */ | - | ||||||||||||
| 342 | QVector3D QVector3D::crossProduct(const QVector3D& v1, const QVector3D& v2) | - | ||||||||||||
| 343 | { | - | ||||||||||||
| 344 | return QVector3D(v1.yp * v2.zp - v1.zp * v2.yp, never executed: return QVector3D(v1.yp * v2.zp - v1.zp * v2.yp, v1.zp * v2.xp - v1.xp * v2.zp, v1.xp * v2.yp - v1.yp * v2.xp); | 0 | ||||||||||||
| 345 | v1.zp * v2.xp - v1.xp * v2.zp, never executed: return QVector3D(v1.yp * v2.zp - v1.zp * v2.yp, v1.zp * v2.xp - v1.xp * v2.zp, v1.xp * v2.yp - v1.yp * v2.xp); | 0 | ||||||||||||
| 346 | v1.xp * v2.yp - v1.yp * v2.xp); never executed: return QVector3D(v1.yp * v2.zp - v1.zp * v2.yp, v1.zp * v2.xp - v1.xp * v2.zp, v1.xp * v2.yp - v1.yp * v2.xp); | 0 | ||||||||||||
| 347 | } | - | ||||||||||||
| 348 | - | |||||||||||||
| 349 | /*! | - | ||||||||||||
| 350 | Returns the normal vector of a plane defined by vectors \a v1 and \a v2, | - | ||||||||||||
| 351 | normalized to be a unit vector. | - | ||||||||||||
| 352 | - | |||||||||||||
| 353 | Use crossProduct() to compute the cross-product of \a v1 and \a v2 if you | - | ||||||||||||
| 354 | do not need the result to be normalized to a unit vector. | - | ||||||||||||
| 355 | - | |||||||||||||
| 356 | \sa crossProduct(), distanceToPlane() | - | ||||||||||||
| 357 | */ | - | ||||||||||||
| 358 | QVector3D QVector3D::normal(const QVector3D& v1, const QVector3D& v2) | - | ||||||||||||
| 359 | { | - | ||||||||||||
| 360 | return crossProduct(v1, v2).normalized(); never executed: return crossProduct(v1, v2).normalized(); | 0 | ||||||||||||
| 361 | } | - | ||||||||||||
| 362 | - | |||||||||||||
| 363 | /*! | - | ||||||||||||
| 364 | \overload | - | ||||||||||||
| 365 | - | |||||||||||||
| 366 | Returns the normal vector of a plane defined by vectors | - | ||||||||||||
| 367 | \a v2 - \a v1 and \a v3 - \a v1, normalized to be a unit vector. | - | ||||||||||||
| 368 | - | |||||||||||||
| 369 | Use crossProduct() to compute the cross-product of \a v2 - \a v1 and | - | ||||||||||||
| 370 | \a v3 - \a v1 if you do not need the result to be normalized to a | - | ||||||||||||
| 371 | unit vector. | - | ||||||||||||
| 372 | - | |||||||||||||
| 373 | \sa crossProduct(), distanceToPlane() | - | ||||||||||||
| 374 | */ | - | ||||||||||||
| 375 | QVector3D QVector3D::normal | - | ||||||||||||
| 376 | (const QVector3D& v1, const QVector3D& v2, const QVector3D& v3) | - | ||||||||||||
| 377 | { | - | ||||||||||||
| 378 | return crossProduct((v2 - v1), (v3 - v1)).normalized(); never executed: return crossProduct((v2 - v1), (v3 - v1)).normalized(); | 0 | ||||||||||||
| 379 | } | - | ||||||||||||
| 380 | - | |||||||||||||
| 381 | /*! | - | ||||||||||||
| 382 | \since 5.5 | - | ||||||||||||
| 383 | - | |||||||||||||
| 384 | Returns the window coordinates of this vector initially in object/model | - | ||||||||||||
| 385 | coordinates using the model view matrix \a modelView, the projection matrix | - | ||||||||||||
| 386 | \a projection and the viewport dimensions \a viewport. | - | ||||||||||||
| 387 | - | |||||||||||||
| 388 | When transforming from clip to normalized space, a division by the w | - | ||||||||||||
| 389 | component on the vector components takes place. To prevent dividing by 0 if | - | ||||||||||||
| 390 | w equals to 0, it is set to 1. | - | ||||||||||||
| 391 | - | |||||||||||||
| 392 | \note the returned y coordinates are in OpenGL orientation. OpenGL expects | - | ||||||||||||
| 393 | the bottom to be 0 whereas for Qt top is 0. | - | ||||||||||||
| 394 | - | |||||||||||||
| 395 | \sa unproject() | - | ||||||||||||
| 396 | */ | - | ||||||||||||
| 397 | QVector3D QVector3D::project(const QMatrix4x4 &modelView, const QMatrix4x4 &projection, const QRect &viewport) const | - | ||||||||||||
| 398 | { | - | ||||||||||||
| 399 | QVector4D tmp(*this, 1.0f); | - | ||||||||||||
| 400 | tmp = projection * modelView * tmp; | - | ||||||||||||
| 401 | if (qFuzzyIsNull(tmp.w()))
| 0 | ||||||||||||
| 402 | tmp.setW(1.0f); never executed: tmp.setW(1.0f); | 0 | ||||||||||||
| 403 | tmp /= tmp.w(); | - | ||||||||||||
| 404 | - | |||||||||||||
| 405 | tmp = tmp * 0.5f + QVector4D(0.5f, 0.5f, 0.5f, 0.5f); | - | ||||||||||||
| 406 | tmp.setX(tmp.x() * viewport.width() + viewport.x()); | - | ||||||||||||
| 407 | tmp.setY(tmp.y() * viewport.height() + viewport.y()); | - | ||||||||||||
| 408 | - | |||||||||||||
| 409 | return tmp.toVector3D(); never executed: return tmp.toVector3D(); | 0 | ||||||||||||
| 410 | } | - | ||||||||||||
| 411 | - | |||||||||||||
| 412 | /*! | - | ||||||||||||
| 413 | \since 5.5 | - | ||||||||||||
| 414 | - | |||||||||||||
| 415 | Returns the object/model coordinates of this vector initially in window | - | ||||||||||||
| 416 | coordinates using the model view matrix \a modelView, the projection matrix | - | ||||||||||||
| 417 | \a projection and the viewport dimensions \a viewport. | - | ||||||||||||
| 418 | - | |||||||||||||
| 419 | When transforming from clip to normalized space, a division by the w | - | ||||||||||||
| 420 | component of the vector components takes place. To prevent dividing by 0 if | - | ||||||||||||
| 421 | w equals to 0, it is set to 1. | - | ||||||||||||
| 422 | - | |||||||||||||
| 423 | \note y coordinates in \a viewport should use OpenGL orientation. OpenGL | - | ||||||||||||
| 424 | expects the bottom to be 0 whereas for Qt top is 0. | - | ||||||||||||
| 425 | - | |||||||||||||
| 426 | \sa project() | - | ||||||||||||
| 427 | */ | - | ||||||||||||
| 428 | QVector3D QVector3D::unproject(const QMatrix4x4 &modelView, const QMatrix4x4 &projection, const QRect &viewport) const | - | ||||||||||||
| 429 | { | - | ||||||||||||
| 430 | QMatrix4x4 inverse = QMatrix4x4( projection * modelView ).inverted(); | - | ||||||||||||
| 431 | - | |||||||||||||
| 432 | QVector4D tmp(*this, 1.0f); | - | ||||||||||||
| 433 | tmp.setX((tmp.x() - float(viewport.x())) / float(viewport.width())); | - | ||||||||||||
| 434 | tmp.setY((tmp.y() - float(viewport.y())) / float(viewport.height())); | - | ||||||||||||
| 435 | tmp = tmp * 2.0f - QVector4D(1.0f, 1.0f, 1.0f, 1.0f); | - | ||||||||||||
| 436 | - | |||||||||||||
| 437 | QVector4D obj = inverse * tmp; | - | ||||||||||||
| 438 | if (qFuzzyIsNull(obj.w()))
| 0 | ||||||||||||
| 439 | obj.setW(1.0f); never executed: obj.setW(1.0f); | 0 | ||||||||||||
| 440 | obj /= obj.w(); | - | ||||||||||||
| 441 | return obj.toVector3D(); never executed: return obj.toVector3D(); | 0 | ||||||||||||
| 442 | } | - | ||||||||||||
| 443 | - | |||||||||||||
| 444 | /*! | - | ||||||||||||
| 445 | \since 5.1 | - | ||||||||||||
| 446 | - | |||||||||||||
| 447 | Returns the distance from this vertex to a point defined by | - | ||||||||||||
| 448 | the vertex \a point. | - | ||||||||||||
| 449 | - | |||||||||||||
| 450 | \sa distanceToPlane(), distanceToLine() | - | ||||||||||||
| 451 | */ | - | ||||||||||||
| 452 | float QVector3D::distanceToPoint(const QVector3D& point) const | - | ||||||||||||
| 453 | { | - | ||||||||||||
| 454 | return (*this - point).length(); never executed: return (*this - point).length(); | 0 | ||||||||||||
| 455 | } | - | ||||||||||||
| 456 | - | |||||||||||||
| 457 | /*! | - | ||||||||||||
| 458 | Returns the distance from this vertex to a plane defined by | - | ||||||||||||
| 459 | the vertex \a plane and a \a normal unit vector. The \a normal | - | ||||||||||||
| 460 | parameter is assumed to have been normalized to a unit vector. | - | ||||||||||||
| 461 | - | |||||||||||||
| 462 | The return value will be negative if the vertex is below the plane, | - | ||||||||||||
| 463 | or zero if it is on the plane. | - | ||||||||||||
| 464 | - | |||||||||||||
| 465 | \sa normal(), distanceToLine() | - | ||||||||||||
| 466 | */ | - | ||||||||||||
| 467 | float QVector3D::distanceToPlane | - | ||||||||||||
| 468 | (const QVector3D& plane, const QVector3D& normal) const | - | ||||||||||||
| 469 | { | - | ||||||||||||
| 470 | return dotProduct(*this - plane, normal); never executed: return dotProduct(*this - plane, normal); | 0 | ||||||||||||
| 471 | } | - | ||||||||||||
| 472 | - | |||||||||||||
| 473 | /*! | - | ||||||||||||
| 474 | \overload | - | ||||||||||||
| 475 | - | |||||||||||||
| 476 | Returns the distance from this vertex a plane defined by | - | ||||||||||||
| 477 | the vertices \a plane1, \a plane2 and \a plane3. | - | ||||||||||||
| 478 | - | |||||||||||||
| 479 | The return value will be negative if the vertex is below the plane, | - | ||||||||||||
| 480 | or zero if it is on the plane. | - | ||||||||||||
| 481 | - | |||||||||||||
| 482 | The two vectors that define the plane are \a plane2 - \a plane1 | - | ||||||||||||
| 483 | and \a plane3 - \a plane1. | - | ||||||||||||
| 484 | - | |||||||||||||
| 485 | \sa normal(), distanceToLine() | - | ||||||||||||
| 486 | */ | - | ||||||||||||
| 487 | float QVector3D::distanceToPlane | - | ||||||||||||
| 488 | (const QVector3D& plane1, const QVector3D& plane2, const QVector3D& plane3) const | - | ||||||||||||
| 489 | { | - | ||||||||||||
| 490 | QVector3D n = normal(plane2 - plane1, plane3 - plane1); | - | ||||||||||||
| 491 | return dotProduct(*this - plane1, n); never executed: return dotProduct(*this - plane1, n); | 0 | ||||||||||||
| 492 | } | - | ||||||||||||
| 493 | - | |||||||||||||
| 494 | /*! | - | ||||||||||||
| 495 | Returns the distance that this vertex is from a line defined | - | ||||||||||||
| 496 | by \a point and the unit vector \a direction. | - | ||||||||||||
| 497 | - | |||||||||||||
| 498 | If \a direction is a null vector, then it does not define a line. | - | ||||||||||||
| 499 | In that case, the distance from \a point to this vertex is returned. | - | ||||||||||||
| 500 | - | |||||||||||||
| 501 | \sa distanceToPlane() | - | ||||||||||||
| 502 | */ | - | ||||||||||||
| 503 | float QVector3D::distanceToLine | - | ||||||||||||
| 504 | (const QVector3D& point, const QVector3D& direction) const | - | ||||||||||||
| 505 | { | - | ||||||||||||
| 506 | if (direction.isNull())
| 0 | ||||||||||||
| 507 | return (*this - point).length(); never executed: return (*this - point).length(); | 0 | ||||||||||||
| 508 | QVector3D p = point + dotProduct(*this - point, direction) * direction; | - | ||||||||||||
| 509 | return (*this - p).length(); never executed: return (*this - p).length(); | 0 | ||||||||||||
| 510 | } | - | ||||||||||||
| 511 | - | |||||||||||||
| 512 | /*! | - | ||||||||||||
| 513 | \fn bool operator==(const QVector3D &v1, const QVector3D &v2) | - | ||||||||||||
| 514 | \relates QVector3D | - | ||||||||||||
| 515 | - | |||||||||||||
| 516 | Returns \c true if \a v1 is equal to \a v2; otherwise returns \c false. | - | ||||||||||||
| 517 | This operator uses an exact floating-point comparison. | - | ||||||||||||
| 518 | */ | - | ||||||||||||
| 519 | - | |||||||||||||
| 520 | /*! | - | ||||||||||||
| 521 | \fn bool operator!=(const QVector3D &v1, const QVector3D &v2) | - | ||||||||||||
| 522 | \relates QVector3D | - | ||||||||||||
| 523 | - | |||||||||||||
| 524 | Returns \c true if \a v1 is not equal to \a v2; otherwise returns \c false. | - | ||||||||||||
| 525 | This operator uses an exact floating-point comparison. | - | ||||||||||||
| 526 | */ | - | ||||||||||||
| 527 | - | |||||||||||||
| 528 | /*! | - | ||||||||||||
| 529 | \fn const QVector3D operator+(const QVector3D &v1, const QVector3D &v2) | - | ||||||||||||
| 530 | \relates QVector3D | - | ||||||||||||
| 531 | - | |||||||||||||
| 532 | Returns a QVector3D object that is the sum of the given vectors, \a v1 | - | ||||||||||||
| 533 | and \a v2; each component is added separately. | - | ||||||||||||
| 534 | - | |||||||||||||
| 535 | \sa QVector3D::operator+=() | - | ||||||||||||
| 536 | */ | - | ||||||||||||
| 537 | - | |||||||||||||
| 538 | /*! | - | ||||||||||||
| 539 | \fn const QVector3D operator-(const QVector3D &v1, const QVector3D &v2) | - | ||||||||||||
| 540 | \relates QVector3D | - | ||||||||||||
| 541 | - | |||||||||||||
| 542 | Returns a QVector3D object that is formed by subtracting \a v2 from \a v1; | - | ||||||||||||
| 543 | each component is subtracted separately. | - | ||||||||||||
| 544 | - | |||||||||||||
| 545 | \sa QVector3D::operator-=() | - | ||||||||||||
| 546 | */ | - | ||||||||||||
| 547 | - | |||||||||||||
| 548 | /*! | - | ||||||||||||
| 549 | \fn const QVector3D operator*(float factor, const QVector3D &vector) | - | ||||||||||||
| 550 | \relates QVector3D | - | ||||||||||||
| 551 | - | |||||||||||||
| 552 | Returns a copy of the given \a vector, multiplied by the given \a factor. | - | ||||||||||||
| 553 | - | |||||||||||||
| 554 | \sa QVector3D::operator*=() | - | ||||||||||||
| 555 | */ | - | ||||||||||||
| 556 | - | |||||||||||||
| 557 | /*! | - | ||||||||||||
| 558 | \fn const QVector3D operator*(const QVector3D &vector, float factor) | - | ||||||||||||
| 559 | \relates QVector3D | - | ||||||||||||
| 560 | - | |||||||||||||
| 561 | Returns a copy of the given \a vector, multiplied by the given \a factor. | - | ||||||||||||
| 562 | - | |||||||||||||
| 563 | \sa QVector3D::operator*=() | - | ||||||||||||
| 564 | */ | - | ||||||||||||
| 565 | - | |||||||||||||
| 566 | /*! | - | ||||||||||||
| 567 | \fn const QVector3D operator*(const QVector3D &v1, const QVector3D& v2) | - | ||||||||||||
| 568 | \relates QVector3D | - | ||||||||||||
| 569 | - | |||||||||||||
| 570 | Multiplies the components of \a v1 by the corresponding components in \a v2. | - | ||||||||||||
| 571 | - | |||||||||||||
| 572 | Note: this is not the same as the crossProduct() of \a v1 and \a v2. | - | ||||||||||||
| 573 | - | |||||||||||||
| 574 | \sa QVector3D::crossProduct() | - | ||||||||||||
| 575 | */ | - | ||||||||||||
| 576 | - | |||||||||||||
| 577 | /*! | - | ||||||||||||
| 578 | \fn const QVector3D operator-(const QVector3D &vector) | - | ||||||||||||
| 579 | \relates QVector3D | - | ||||||||||||
| 580 | \overload | - | ||||||||||||
| 581 | - | |||||||||||||
| 582 | Returns a QVector3D object that is formed by changing the sign of | - | ||||||||||||
| 583 | all three components of the given \a vector. | - | ||||||||||||
| 584 | - | |||||||||||||
| 585 | Equivalent to \c {QVector3D(0,0,0) - vector}. | - | ||||||||||||
| 586 | */ | - | ||||||||||||
| 587 | - | |||||||||||||
| 588 | /*! | - | ||||||||||||
| 589 | \fn const QVector3D operator/(const QVector3D &vector, float divisor) | - | ||||||||||||
| 590 | \relates QVector3D | - | ||||||||||||
| 591 | - | |||||||||||||
| 592 | Returns the QVector3D object formed by dividing all three components of | - | ||||||||||||
| 593 | the given \a vector by the given \a divisor. | - | ||||||||||||
| 594 | - | |||||||||||||
| 595 | \sa QVector3D::operator/=() | - | ||||||||||||
| 596 | */ | - | ||||||||||||
| 597 | - | |||||||||||||
| 598 | /*! | - | ||||||||||||
| 599 | \fn const QVector3D operator/(const QVector3D &vector, const QVector3D &divisor) | - | ||||||||||||
| 600 | \relates QVector3D | - | ||||||||||||
| 601 | \since 5.5 | - | ||||||||||||
| 602 | - | |||||||||||||
| 603 | Returns the QVector3D object formed by dividing components of the given | - | ||||||||||||
| 604 | \a vector by a respective components of the given \a divisor. | - | ||||||||||||
| 605 | - | |||||||||||||
| 606 | \sa QVector3D::operator/=() | - | ||||||||||||
| 607 | */ | - | ||||||||||||
| 608 | - | |||||||||||||
| 609 | /*! | - | ||||||||||||
| 610 | \fn bool qFuzzyCompare(const QVector3D& v1, const QVector3D& v2) | - | ||||||||||||
| 611 | \relates QVector3D | - | ||||||||||||
| 612 | - | |||||||||||||
| 613 | Returns \c true if \a v1 and \a v2 are equal, allowing for a small | - | ||||||||||||
| 614 | fuzziness factor for floating-point comparisons; false otherwise. | - | ||||||||||||
| 615 | */ | - | ||||||||||||
| 616 | - | |||||||||||||
| 617 | #ifndef QT_NO_VECTOR2D | - | ||||||||||||
| 618 | - | |||||||||||||
| 619 | /*! | - | ||||||||||||
| 620 | Returns the 2D vector form of this 3D vector, dropping the z coordinate. | - | ||||||||||||
| 621 | - | |||||||||||||
| 622 | \sa toVector4D(), toPoint() | - | ||||||||||||
| 623 | */ | - | ||||||||||||
| 624 | QVector2D QVector3D::toVector2D() const | - | ||||||||||||
| 625 | { | - | ||||||||||||
| 626 | return QVector2D(xp, yp); never executed: return QVector2D(xp, yp); | 0 | ||||||||||||
| 627 | } | - | ||||||||||||
| 628 | - | |||||||||||||
| 629 | #endif | - | ||||||||||||
| 630 | - | |||||||||||||
| 631 | #ifndef QT_NO_VECTOR4D | - | ||||||||||||
| 632 | - | |||||||||||||
| 633 | /*! | - | ||||||||||||
| 634 | Returns the 4D form of this 3D vector, with the w coordinate set to zero. | - | ||||||||||||
| 635 | - | |||||||||||||
| 636 | \sa toVector2D(), toPoint() | - | ||||||||||||
| 637 | */ | - | ||||||||||||
| 638 | QVector4D QVector3D::toVector4D() const | - | ||||||||||||
| 639 | { | - | ||||||||||||
| 640 | return QVector4D(xp, yp, zp, 0.0f); never executed: return QVector4D(xp, yp, zp, 0.0f); | 0 | ||||||||||||
| 641 | } | - | ||||||||||||
| 642 | - | |||||||||||||
| 643 | #endif | - | ||||||||||||
| 644 | - | |||||||||||||
| 645 | /*! | - | ||||||||||||
| 646 | \fn QPoint QVector3D::toPoint() const | - | ||||||||||||
| 647 | - | |||||||||||||
| 648 | Returns the QPoint form of this 3D vector. The z coordinate | - | ||||||||||||
| 649 | is dropped. | - | ||||||||||||
| 650 | - | |||||||||||||
| 651 | \sa toPointF(), toVector2D() | - | ||||||||||||
| 652 | */ | - | ||||||||||||
| 653 | - | |||||||||||||
| 654 | /*! | - | ||||||||||||
| 655 | \fn QPointF QVector3D::toPointF() const | - | ||||||||||||
| 656 | - | |||||||||||||
| 657 | Returns the QPointF form of this 3D vector. The z coordinate | - | ||||||||||||
| 658 | is dropped. | - | ||||||||||||
| 659 | - | |||||||||||||
| 660 | \sa toPoint(), toVector2D() | - | ||||||||||||
| 661 | */ | - | ||||||||||||
| 662 | - | |||||||||||||
| 663 | /*! | - | ||||||||||||
| 664 | Returns the 3D vector as a QVariant. | - | ||||||||||||
| 665 | */ | - | ||||||||||||
| 666 | QVector3D::operator QVariant() const | - | ||||||||||||
| 667 | { | - | ||||||||||||
| 668 | return QVariant(QVariant::Vector3D, this); never executed: return QVariant(QVariant::Vector3D, this); | 0 | ||||||||||||
| 669 | } | - | ||||||||||||
| 670 | - | |||||||||||||
| 671 | /*! | - | ||||||||||||
| 672 | Returns the length of the vector from the origin. | - | ||||||||||||
| 673 | - | |||||||||||||
| 674 | \sa lengthSquared(), normalized() | - | ||||||||||||
| 675 | */ | - | ||||||||||||
| 676 | float QVector3D::length() const | - | ||||||||||||
| 677 | { | - | ||||||||||||
| 678 | // Need some extra precision if the length is very small. | - | ||||||||||||
| 679 | double len = double(xp) * double(xp) + | - | ||||||||||||
| 680 | double(yp) * double(yp) + | - | ||||||||||||
| 681 | double(zp) * double(zp); | - | ||||||||||||
| 682 | return float(std::sqrt(len)); never executed: return float(std::sqrt(len)); | 0 | ||||||||||||
| 683 | } | - | ||||||||||||
| 684 | - | |||||||||||||
| 685 | /*! | - | ||||||||||||
| 686 | Returns the squared length of the vector from the origin. | - | ||||||||||||
| 687 | This is equivalent to the dot product of the vector with itself. | - | ||||||||||||
| 688 | - | |||||||||||||
| 689 | \sa length(), dotProduct() | - | ||||||||||||
| 690 | */ | - | ||||||||||||
| 691 | float QVector3D::lengthSquared() const | - | ||||||||||||
| 692 | { | - | ||||||||||||
| 693 | return xp * xp + yp * yp + zp * zp; never executed: return xp * xp + yp * yp + zp * zp; | 0 | ||||||||||||
| 694 | } | - | ||||||||||||
| 695 | - | |||||||||||||
| 696 | #ifndef QT_NO_DEBUG_STREAM | - | ||||||||||||
| 697 | - | |||||||||||||
| 698 | QDebug operator<<(QDebug dbg, const QVector3D &vector) | - | ||||||||||||
| 699 | { | - | ||||||||||||
| 700 | QDebugStateSaver saver(dbg); | - | ||||||||||||
| 701 | dbg.nospace() << "QVector3D(" | - | ||||||||||||
| 702 | << vector.x() << ", " << vector.y() << ", " << vector.z() << ')'; | - | ||||||||||||
| 703 | return dbg; never executed: return dbg; | 0 | ||||||||||||
| 704 | } | - | ||||||||||||
| 705 | - | |||||||||||||
| 706 | #endif | - | ||||||||||||
| 707 | - | |||||||||||||
| 708 | #ifndef QT_NO_DATASTREAM | - | ||||||||||||
| 709 | - | |||||||||||||
| 710 | /*! | - | ||||||||||||
| 711 | \fn QDataStream &operator<<(QDataStream &stream, const QVector3D &vector) | - | ||||||||||||
| 712 | \relates QVector3D | - | ||||||||||||
| 713 | - | |||||||||||||
| 714 | Writes the given \a vector to the given \a stream and returns a | - | ||||||||||||
| 715 | reference to the stream. | - | ||||||||||||
| 716 | - | |||||||||||||
| 717 | \sa {Serializing Qt Data Types} | - | ||||||||||||
| 718 | */ | - | ||||||||||||
| 719 | - | |||||||||||||
| 720 | QDataStream &operator<<(QDataStream &stream, const QVector3D &vector) | - | ||||||||||||
| 721 | { | - | ||||||||||||
| 722 | stream << vector.x() << vector.y() << vector.z(); | - | ||||||||||||
| 723 | return stream; never executed: return stream; | 0 | ||||||||||||
| 724 | } | - | ||||||||||||
| 725 | - | |||||||||||||
| 726 | /*! | - | ||||||||||||
| 727 | \fn QDataStream &operator>>(QDataStream &stream, QVector3D &vector) | - | ||||||||||||
| 728 | \relates QVector3D | - | ||||||||||||
| 729 | - | |||||||||||||
| 730 | Reads a 3D vector from the given \a stream into the given \a vector | - | ||||||||||||
| 731 | and returns a reference to the stream. | - | ||||||||||||
| 732 | - | |||||||||||||
| 733 | \sa {Serializing Qt Data Types} | - | ||||||||||||
| 734 | */ | - | ||||||||||||
| 735 | - | |||||||||||||
| 736 | QDataStream &operator>>(QDataStream &stream, QVector3D &vector) | - | ||||||||||||
| 737 | { | - | ||||||||||||
| 738 | float x, y, z; | - | ||||||||||||
| 739 | stream >> x; | - | ||||||||||||
| 740 | stream >> y; | - | ||||||||||||
| 741 | stream >> z; | - | ||||||||||||
| 742 | vector.setX(x); | - | ||||||||||||
| 743 | vector.setY(y); | - | ||||||||||||
| 744 | vector.setZ(z); | - | ||||||||||||
| 745 | return stream; never executed: return stream; | 0 | ||||||||||||
| 746 | } | - | ||||||||||||
| 747 | - | |||||||||||||
| 748 | #endif // QT_NO_DATASTREAM | - | ||||||||||||
| 749 | - | |||||||||||||
| 750 | #endif // QT_NO_VECTOR3D | - | ||||||||||||
| 751 | - | |||||||||||||
| 752 | QT_END_NAMESPACE | - | ||||||||||||
| Source code | Switch to Preprocessed file |