Qt 4.8
qvector4d.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 "qvector4d.h"
43 #include "qvector3d.h"
44 #include "qvector2d.h"
45 #include <QtCore/qdebug.h>
46 #include <QtCore/qvariant.h>
47 #include <QtCore/qmath.h>
48 
50 
51 #ifndef QT_NO_VECTOR4D
52 
111 #ifndef QT_NO_VECTOR2D
112 
120 {
121  xp = vector.xp;
122  yp = vector.yp;
123  zp = 0.0f;
124  wp = 0.0f;
125 }
126 
133 QVector4D::QVector4D(const QVector2D& vector, qreal zpos, qreal wpos)
134 {
135  xp = vector.xp;
136  yp = vector.yp;
137  zp = zpos;
138  wp = wpos;
139 }
140 
141 #endif
142 
143 #ifndef QT_NO_VECTOR3D
144 
152 {
153  xp = vector.xp;
154  yp = vector.yp;
155  zp = vector.zp;
156  wp = 0.0f;
157 }
158 
166 {
167  xp = vector.xp;
168  yp = vector.yp;
169  zp = vector.zp;
170  wp = wpos;
171 }
172 
173 #endif
174 
279 {
280  return qSqrt(xp * xp + yp * yp + zp * zp + wp * wp);
281 }
282 
290 {
291  return xp * xp + yp * yp + zp * zp + wp * wp;
292 }
293 
304 {
305  // Need some extra precision if the length is very small.
306  double len = double(xp) * double(xp) +
307  double(yp) * double(yp) +
308  double(zp) * double(zp) +
309  double(wp) * double(wp);
310  if (qFuzzyIsNull(len - 1.0f))
311  return *this;
312  else if (!qFuzzyIsNull(len))
313  return *this / qSqrt(len);
314  else
315  return QVector4D();
316 }
317 
325 {
326  // Need some extra precision if the length is very small.
327  double len = double(xp) * double(xp) +
328  double(yp) * double(yp) +
329  double(zp) * double(zp) +
330  double(wp) * double(wp);
331  if (qFuzzyIsNull(len - 1.0f) || qFuzzyIsNull(len))
332  return;
333 
334  len = qSqrt(len);
335 
336  xp /= len;
337  yp /= len;
338  zp /= len;
339  wp /= len;
340 }
341 
395 {
396  return v1.xp * v2.xp + v1.yp * v2.yp + v1.zp * v2.zp + v1.wp * v2.wp;
397 }
398 
513 #ifndef QT_NO_VECTOR2D
514 
521 {
522  return QVector2D(xp, yp, 1);
523 }
524 
533 {
534  if (qIsNull(wp))
535  return QVector2D();
536  return QVector2D(xp / wp, yp / wp, 1);
537 }
538 
539 #endif
540 
541 #ifndef QT_NO_VECTOR3D
542 
549 {
550  return QVector3D(xp, yp, zp, 1);
551 }
552 
560 {
561  if (qIsNull(wp))
562  return QVector3D();
563  return QVector3D(xp / wp, yp / wp, zp / wp, 1);
564 }
565 
566 #endif
567 
595 QVector4D::operator QVariant() const
596 {
597  return QVariant(QVariant::Vector4D, this);
598 }
599 
600 #ifndef QT_NO_DEBUG_STREAM
601 
602 QDebug operator<<(QDebug dbg, const QVector4D &vector)
603 {
604  dbg.nospace() << "QVector4D("
605  << vector.x() << ", " << vector.y() << ", "
606  << vector.z() << ", " << vector.w() << ')';
607  return dbg.space();
608 }
609 
610 #endif
611 
612 #ifndef QT_NO_DATASTREAM
613 
628 {
629  stream << double(vector.x()) << double(vector.y())
630  << double(vector.z()) << double(vector.w());
631  return stream;
632 }
633 
648 {
649  double x, y, z, w;
650  stream >> x;
651  stream >> y;
652  stream >> z;
653  stream >> w;
654  vector.setX(qreal(x));
655  vector.setY(qreal(y));
656  vector.setZ(qreal(z));
657  vector.setW(qreal(w));
658  return stream;
659 }
660 
661 #endif // QT_NO_DATASTREAM
662 
663 #endif // QT_NO_VECTOR4D
664 
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
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
QVector2D toVector2D() const
Returns the 2D vector form of this 4D vector, dropping the z and w coordinates.
Definition: qvector4d.cpp:520
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
float xp
Definition: qvector2d.h:121
qreal w() const
Returns the w coordinate of this point.
Definition: qvector4d.h:161
The QVector4D class represents a vector or vertex in 4D space.
Definition: qvector4d.h:60
float zp
Definition: qvector3d.h:132
float yp
Definition: qvector4d.h:129
qreal length() const
Returns the length of the vector from the origin.
Definition: qvector4d.cpp:278
static bool qIsNull(double d)
Definition: qglobal.h:2061
float yp
Definition: qvector3d.h:132
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
static FILE * stream
qreal x() const
Returns the x coordinate of this point.
Definition: qvector4d.h:158
void setZ(qreal z)
Sets the z coordinate of this point to the given z coordinate.
Definition: qvector4d.h:165
QDataStream & operator<<(QDataStream &stream, const QVector4D &vector)
Writes the given vector to the given stream and returns a reference to the stream.
Definition: qvector4d.cpp:627
float wp
Definition: qvector4d.h:129
The QVector2D class represents a vector or vertex in 2D space.
Definition: qvector2d.h:60
static qreal dotProduct(const QVector4D &v1, const QVector4D &v2)
Returns the dot product of v1 and v2.
Definition: qvector4d.cpp:394
void normalize()
Normalizes the currect vector in place.
Definition: qvector4d.cpp:324
QVector4D()
Constructs a null vector, i.
Definition: qvector4d.h:143
QVector3D toVector3D() const
Returns the 3D vector form of this 4D vector, dropping the w coordinate.
Definition: qvector4d.cpp:548
void setW(qreal w)
Sets the w coordinate of this point to the given w coordinate.
Definition: qvector4d.h:166
float zp
Definition: qvector4d.h:129
QVector2D toVector2DAffine() const
Returns the 2D vector form of this 4D vector, dividing the x and y coordinates by the w coordinate an...
Definition: qvector4d.cpp:532
void setX(qreal x)
Sets the x coordinate of this point to the given x coordinate.
Definition: qvector4d.h:163
QVector3D toVector3DAffine() const
Returns the 3D vector form of this 4D vector, dividing the x, y, and z coordinates by the w coordinat...
Definition: qvector4d.cpp:559
QVector4D normalized() const
Returns the normalized unit vector form of this vector.
Definition: qvector4d.cpp:303
float xp
Definition: qvector3d.h:132
qreal lengthSquared() const
Returns the squared length of the vector from the origin.
Definition: qvector4d.cpp:289
static Q_DECL_CONSTEXPR bool qFuzzyIsNull(double d)
Definition: qglobal.h:2043
float yp
Definition: qvector2d.h:121
friend class QVector3D
Definition: qvector4d.h:134
The QDataStream class provides serialization of binary data to a QIODevice.
Definition: qdatastream.h:71
void setY(qreal y)
Sets the y coordinate of this point to the given y coordinate.
Definition: qvector4d.h:164
QDataStream & operator>>(QDataStream &stream, QVector4D &vector)
Reads a 4D vector from the given stream into the given vector and returns a reference to the stream...
Definition: qvector4d.cpp:647
qreal z() const
Returns the z coordinate of this point.
Definition: qvector4d.h:160
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
friend class QVector2D
Definition: qvector4d.h:133
qreal y() const
Returns the y coordinate of this point.
Definition: qvector4d.h:159