Qt 4.8
qvector3d.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the QtGui module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
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 Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/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 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights. These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file. Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41 
42 #include "qvector3d.h"
43 #include "qvector2d.h"
44 #include "qvector4d.h"
45 #include <QtCore/qmath.h>
46 #include <QtCore/qvariant.h>
47 #include <QtCore/qdebug.h>
48 
50 
51 #ifndef QT_NO_VECTOR3D
52 
115 #ifndef QT_NO_VECTOR2D
116 
124 {
125  xp = vector.xp;
126  yp = vector.yp;
127  zp = 0.0f;
128 }
129 
137 {
138  xp = vector.xp;
139  yp = vector.yp;
140  zp = zpos;
141 }
142 
143 #endif
144 
145 #ifndef QT_NO_VECTOR4D
146 
154 {
155  xp = vector.xp;
156  yp = vector.yp;
157  zp = vector.zp;
158 }
159 
160 #endif
161 
248 {
249  // Need some extra precision if the length is very small.
250  double len = double(xp) * double(xp) +
251  double(yp) * double(yp) +
252  double(zp) * double(zp);
253  if (qFuzzyIsNull(len - 1.0f))
254  return *this;
255  else if (!qFuzzyIsNull(len))
256  return *this / qSqrt(len);
257  else
258  return QVector3D();
259 }
260 
268 {
269  // Need some extra precision if the length is very small.
270  double len = double(xp) * double(xp) +
271  double(yp) * double(yp) +
272  double(zp) * double(zp);
273  if (qFuzzyIsNull(len - 1.0f) || qFuzzyIsNull(len))
274  return;
275 
276  len = qSqrt(len);
277 
278  xp /= len;
279  yp /= len;
280  zp /= len;
281 }
282 
342 {
343  return v1.xp * v2.xp + v1.yp * v2.yp + v1.zp * v2.zp;
344 }
345 
353 {
354  return QVector3D(v1.yp * v2.zp - v1.zp * v2.yp,
355  v1.zp * v2.xp - v1.xp * v2.zp,
356  v1.xp * v2.yp - v1.yp * v2.xp, 1);
357 }
358 
369 {
370  return crossProduct(v1, v2).normalized();
371 }
372 
389  (const QVector3D& v1, const QVector3D& v2, const QVector3D& v3)
390 {
391  return crossProduct((v2 - v1), (v3 - v1)).normalized();
392 }
393 
405  (const QVector3D& plane, const QVector3D& normal) const
406 {
407  return dotProduct(*this - plane, normal);
408 }
409 
428  (const QVector3D& plane1, const QVector3D& plane2, const QVector3D& plane3) const
429 {
430  QVector3D n = normal(plane2 - plane1, plane3 - plane1);
431  return dotProduct(*this - plane1, n);
432 }
433 
444  (const QVector3D& point, const QVector3D& direction) const
445 {
446  if (direction.isNull())
447  return (*this - point).length();
448  QVector3D p = point + dotProduct(*this - point, direction) * direction;
449  return (*this - p).length();
450 }
451 
567 #ifndef QT_NO_VECTOR2D
568 
575 {
576  return QVector2D(xp, yp, 1);
577 }
578 
579 #endif
580 
581 #ifndef QT_NO_VECTOR4D
582 
589 {
590  return QVector4D(xp, yp, zp, 0.0f, 1);
591 }
592 
593 #endif
594 
622 QVector3D::operator QVariant() const
623 {
624  return QVariant(QVariant::Vector3D, this);
625 }
626 
633 {
634  return qSqrt(xp * xp + yp * yp + zp * zp);
635 }
636 
644 {
645  return xp * xp + yp * yp + zp * zp;
646 }
647 
648 #ifndef QT_NO_DEBUG_STREAM
649 
650 QDebug operator<<(QDebug dbg, const QVector3D &vector)
651 {
652  dbg.nospace() << "QVector3D("
653  << vector.x() << ", " << vector.y() << ", " << vector.z() << ')';
654  return dbg.space();
655 }
656 
657 #endif
658 
659 #ifndef QT_NO_DATASTREAM
660 
675 {
676  stream << double(vector.x()) << double(vector.y())
677  << double(vector.z());
678  return stream;
679 }
680 
695 {
696  double x, y, z;
697  stream >> x;
698  stream >> y;
699  stream >> z;
700  vector.setX(qreal(x));
701  vector.setY(qreal(y));
702  vector.setZ(qreal(z));
703  return stream;
704 }
705 
706 #endif // QT_NO_DATASTREAM
707 
708 #endif // QT_NO_VECTOR3D
709 
The QVariant class acts like a union for the most common Qt data types.
Definition: qvariant.h:92
The QDebug class provides an output stream for debugging information.
Definition: qdebug.h:62
qreal distanceToPlane(const QVector3D &plane, const QVector3D &normal) const
Returns the distance from this vertex to a plane defined by the vertex plane and a normal unit vector...
Definition: qvector3d.cpp:405
static QVector3D normal(const QVector3D &v1, const QVector3D &v2)
Returns the normal vector of a plane defined by vectors v1 and v2, normalized to be a unit vector...
Definition: qvector3d.cpp:368
static qreal dotProduct(const QVector3D &v1, const QVector3D &v2)
Returns the dot product of v1 and v2.
Definition: qvector3d.cpp:341
The QVector3D class represents a vector or vertex in 3D space.
Definition: qvector3d.h:60
double qreal
Definition: qglobal.h:1193
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
QDebug & nospace()
Clears the stream&#39;s internal flag that records whether the last character was a space and returns a r...
Definition: qdebug.h:92
QVector4D toVector4D() const
Returns the 4D form of this 3D vector, with the w coordinate set to zero.
Definition: qvector3d.cpp:588
float xp
Definition: qvector2d.h:121
The QVector4D class represents a vector or vertex in 4D space.
Definition: qvector4d.h:60
float zp
Definition: qvector3d.h:132
friend class QVector2D
Definition: qvector3d.h:136
float yp
Definition: qvector4d.h:129
QVector3D()
Constructs a null vector, i.
Definition: qvector3d.h:146
qreal x() const
Returns the x coordinate of this point.
Definition: qvector3d.h:161
float yp
Definition: qvector3d.h:132
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
static FILE * stream
QDataStream & operator>>(QDataStream &stream, QVector3D &vector)
Reads a 3D vector from the given stream into the given vector and returns a reference to the stream...
Definition: qvector3d.cpp:694
void setX(qreal x)
Sets the x coordinate of this point to the given x coordinate.
Definition: qvector3d.h:165
QVector3D normalized() const
Returns the normalized unit vector form of this vector.
Definition: qvector3d.cpp:247
The QVector2D class represents a vector or vertex in 2D space.
Definition: qvector2d.h:60
QDataStream & operator<<(QDataStream &stream, const QVector3D &vector)
Writes the given vector to the given stream and returns a reference to the stream.
Definition: qvector3d.cpp:674
qreal z() const
Returns the z coordinate of this point.
Definition: qvector3d.h:163
QVector2D toVector2D() const
Returns the 2D vector form of this 3D vector, dropping the z coordinate.
Definition: qvector3d.cpp:574
qreal length() const
Returns the length of the vector from the origin.
Definition: qvector3d.cpp:632
void normalize()
Normalizes the currect vector in place.
Definition: qvector3d.cpp:267
bool isNull() const
Returns true if the x, y, and z coordinates are set to 0.
Definition: qvector3d.h:156
float zp
Definition: qvector4d.h:129
void setZ(qreal z)
Sets the z coordinate of this point to the given z coordinate.
Definition: qvector3d.h:167
qreal distanceToLine(const QVector3D &point, const QVector3D &direction) const
Returns the distance that this vertex is from a line defined by point and the unit vector direction...
Definition: qvector3d.cpp:444
qreal y() const
Returns the y coordinate of this point.
Definition: qvector3d.h:162
qreal lengthSquared() const
Returns the squared length of the vector from the origin.
Definition: qvector3d.cpp:643
float xp
Definition: qvector3d.h:132
static Q_DECL_CONSTEXPR bool qFuzzyIsNull(double d)
Definition: qglobal.h:2043
float yp
Definition: qvector2d.h:121
The QDataStream class provides serialization of binary data to a QIODevice.
Definition: qdatastream.h:71
friend class QVector4D
Definition: qvector3d.h:137
QDebug & space()
Writes a space character to the debug stream and returns a reference to the stream.
Definition: qdebug.h:91
float xp
Definition: qvector4d.h:129
qreal qSqrt(qreal v)
Definition: qmath.h:205
void setY(qreal y)
Sets the y coordinate of this point to the given y coordinate.
Definition: qvector3d.h:166
static QVector3D crossProduct(const QVector3D &v1, const QVector3D &v2)
Returns the cross-product of vectors v1 and v2, which corresponds to the normal vector of a plane def...
Definition: qvector3d.cpp:352
Qt::LayoutDirection direction