Qt 4.8
Public Functions | Static Public Functions | Private Functions | Properties | Friends | Related Functions | List of all members
QVector3D Class Reference

The QVector3D class represents a vector or vertex in 3D space. More...

#include <qvector3d.h>

Public Functions

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. More...
 
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. More...
 
qreal distanceToPlane (const QVector3D &plane1, const QVector3D &plane2, const QVector3D &plane3) const
 Returns the distance from this vertex a plane defined by the vertices plane1, plane2 and plane3. More...
 
bool isNull () const
 Returns true if the x, y, and z coordinates are set to 0. More...
 
qreal length () const
 Returns the length of the vector from the origin. More...
 
qreal lengthSquared () const
 Returns the squared length of the vector from the origin. More...
 
void normalize ()
 Normalizes the currect vector in place. More...
 
QVector3D normalized () const
 Returns the normalized unit vector form of this vector. More...
 
 operator QVariant () const
 Returns the 3D vector as a QVariant. More...
 
QVector3Doperator*= (qreal factor)
 Multiplies this vector's coordinates by the given factor, and returns a reference to this vector. More...
 
QVector3Doperator*= (const QVector3D &vector)
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.Multiplies the components of this vector by the corresponding components in vector. More...
 
QVector3Doperator+= (const QVector3D &vector)
 Adds the given vector to this vector and returns a reference to this vector. More...
 
QVector3Doperator-= (const QVector3D &vector)
 Subtracts the given vector from this vector and returns a reference to this vector. More...
 
QVector3Doperator/= (qreal divisor)
 Divides this vector's coordinates by the given divisor, and returns a reference to this vector. More...
 
 QVector3D ()
 Constructs a null vector, i. More...
 
 QVector3D (qreal xpos, qreal ypos, qreal zpos)
 Constructs a vector with coordinates (xpos, ypos, zpos). More...
 
 QVector3D (const QPoint &point)
 Constructs a vector with x and y coordinates from a 2D point, and a z coordinate of 0. More...
 
 QVector3D (const QPointF &point)
 Constructs a vector with x and y coordinates from a 2D point, and a z coordinate of 0. More...
 
 QVector3D (const QVector2D &vector)
 Constructs a 3D vector from the specified 2D vector. More...
 
 QVector3D (const QVector2D &vector, qreal zpos)
 Constructs a 3D vector from the specified 2D vector. More...
 
 QVector3D (const QVector4D &vector)
 Constructs a 3D vector from the specified 4D vector. More...
 
void setX (qreal x)
 Sets the x coordinate of this point to the given x coordinate. More...
 
void setY (qreal y)
 Sets the y coordinate of this point to the given y coordinate. More...
 
void setZ (qreal z)
 Sets the z coordinate of this point to the given z coordinate. More...
 
QPoint toPoint () const
 Returns the QPoint form of this 3D vector. More...
 
QPointF toPointF () const
 Returns the QPointF form of this 3D vector. More...
 
QVector2D toVector2D () const
 Returns the 2D vector form of this 3D vector, dropping the z coordinate. More...
 
QVector4D toVector4D () const
 Returns the 4D form of this 3D vector, with the w coordinate set to zero. More...
 
qreal x () const
 Returns the x coordinate of this point. More...
 
qreal y () const
 Returns the y coordinate of this point. More...
 
qreal z () const
 Returns the z coordinate of this point. More...
 

Static Public Functions

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 defined by v1 and v2. More...
 
static qreal dotProduct (const QVector3D &v1, const QVector3D &v2)
 Returns the dot product of v1 and v2. More...
 
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. More...
 
static QVector3D normal (const QVector3D &v1, const QVector3D &v2, const QVector3D &v3)
 Returns the normal vector of a plane defined by vectors v2 - v1 and v3 - v1, normalized to be a unit vector. More...
 

Private Functions

 QVector3D (float xpos, float ypos, float zpos, int dummy)
 

Properties

float xp
 
float yp
 
float zp
 

Friends

bool operator!= (const QVector3D &v1, const QVector3D &v2)
 Returns true if v1 is not equal to v2; otherwise returns false. More...
 
const QVector3D operator* (qreal factor, const QVector3D &vector)
 
const QVector3D operator* (const QVector3D &vector, qreal factor)
 
const QVector3D operator* (const QVector3D &v1, const QVector3D &v2)
 
QVector3D operator* (const QVector3D &vector, const QMatrix4x4 &matrix)
 
QVector3D operator* (const QMatrix4x4 &matrix, const QVector3D &vector)
 
const QVector3D operator+ (const QVector3D &v1, const QVector3D &v2)
 Returns a QVector3D object that is the sum of the given vectors, v1 and v2; each component is added separately. More...
 
const QVector3D operator- (const QVector3D &v1, const QVector3D &v2)
 Returns a QVector3D object that is formed by subtracting v2 from v1; each component is subtracted separately. More...
 
const QVector3D operator- (const QVector3D &vector)
 Returns a QVector3D object that is formed by changing the sign of all three components of the given vector. More...
 
const QVector3D operator/ (const QVector3D &vector, qreal divisor)
 Returns the QVector3D object formed by dividing all three components of the given vector by the given divisor. More...
 
bool operator== (const QVector3D &v1, const QVector3D &v2)
 Returns true if v1 is equal to v2; otherwise returns false. More...
 
bool qFuzzyCompare (const QVector3D &v1, const QVector3D &v2)
 Returns true if v1 and v2 are equal, allowing for a small fuzziness factor for floating-point comparisons; false otherwise. More...
 
class QVector2D
 
class QVector4D
 

Related Functions

(Note that these are not member functions.)

QDataStreamoperator<< (QDataStream &stream, const QVector3D &vector)
 Writes the given vector to the given stream and returns a reference to the stream. More...
 
QDataStreamoperator>> (QDataStream &stream, QVector3D &vector)
 Reads a 3D vector from the given stream into the given vector and returns a reference to the stream. More...
 

Detailed Description

The QVector3D class represents a vector or vertex in 3D space.

Since
4.6

Vectors are one of the main building blocks of 3D representation and drawing. They consist of three coordinates, traditionally called x, y, and z.

The QVector3D class can also be used to represent vertices in 3D space. We therefore do not need to provide a separate vertex class.

Note: By design values in the QVector3D instance are stored as float. This means that on platforms where the qreal arguments to QVector3D functions are represented by double values, it is possible to lose precision.

See also
QVector2D, QVector4D, QQuaternion

Definition at line 60 of file qvector3d.h.

Constructors and Destructors

◆ QVector3D() [1/8]

QVector3D::QVector3D ( )
inline

Constructs a null vector, i.

e. with coordinates (0, 0, 0).

Definition at line 146 of file qvector3d.h.

Referenced by crossProduct(), normalized(), operator*(), operator+(), operator-(), and operator/().

146 : xp(0.0f), yp(0.0f), zp(0.0f) {}
float zp
Definition: qvector3d.h:132
float yp
Definition: qvector3d.h:132
float xp
Definition: qvector3d.h:132

◆ QVector3D() [2/8]

QVector3D::QVector3D ( qreal  xpos,
qreal  ypos,
qreal  zpos 
)
inline

Constructs a vector with coordinates (xpos, ypos, zpos).

Definition at line 148 of file qvector3d.h.

148 : xp(xpos), yp(ypos), zp(zpos) {}
float zp
Definition: qvector3d.h:132
float yp
Definition: qvector3d.h:132
float xp
Definition: qvector3d.h:132

◆ QVector3D() [3/8]

QVector3D::QVector3D ( const QPoint point)
inlineexplicit

Constructs a vector with x and y coordinates from a 2D point, and a z coordinate of 0.

Definition at line 152 of file qvector3d.h.

152 : xp(point.x()), yp(point.y()), zp(0.0f) {}
float zp
Definition: qvector3d.h:132
float yp
Definition: qvector3d.h:132
float xp
Definition: qvector3d.h:132
int y() const
Returns the y coordinate of this point.
Definition: qpoint.h:131
int x() const
Returns the x coordinate of this point.
Definition: qpoint.h:128

◆ QVector3D() [4/8]

QVector3D::QVector3D ( const QPointF point)
inlineexplicit

Constructs a vector with x and y coordinates from a 2D point, and a z coordinate of 0.

Definition at line 154 of file qvector3d.h.

154 : xp(point.x()), yp(point.y()), zp(0.0f) {}
float zp
Definition: qvector3d.h:132
float yp
Definition: qvector3d.h:132
qreal x() const
Returns the x-coordinate of this point.
Definition: qpoint.h:282
float xp
Definition: qvector3d.h:132
qreal y() const
Returns the y-coordinate of this point.
Definition: qpoint.h:287

◆ QVector3D() [5/8]

QVector3D::QVector3D ( const QVector2D vector)

Constructs a 3D vector from the specified 2D vector.

The z coordinate is set to zero.

See also
toVector2D()

Definition at line 123 of file qvector3d.cpp.

124 {
125  xp = vector.xp;
126  yp = vector.yp;
127  zp = 0.0f;
128 }
float xp
Definition: qvector2d.h:121
float zp
Definition: qvector3d.h:132
float yp
Definition: qvector3d.h:132
float xp
Definition: qvector3d.h:132
float yp
Definition: qvector2d.h:121

◆ QVector3D() [6/8]

QVector3D::QVector3D ( const QVector2D vector,
qreal  zpos 
)

Constructs a 3D vector from the specified 2D vector.

The z coordinate is set to zpos.

See also
toVector2D()

Definition at line 136 of file qvector3d.cpp.

137 {
138  xp = vector.xp;
139  yp = vector.yp;
140  zp = zpos;
141 }
float xp
Definition: qvector2d.h:121
float zp
Definition: qvector3d.h:132
float yp
Definition: qvector3d.h:132
float xp
Definition: qvector3d.h:132
float yp
Definition: qvector2d.h:121

◆ QVector3D() [7/8]

QVector3D::QVector3D ( const QVector4D vector)
explicit

Constructs a 3D vector from the specified 4D vector.

The w coordinate is dropped.

See also
toVector4D()

Definition at line 153 of file qvector3d.cpp.

154 {
155  xp = vector.xp;
156  yp = vector.yp;
157  zp = vector.zp;
158 }
float zp
Definition: qvector3d.h:132
float yp
Definition: qvector4d.h:129
float yp
Definition: qvector3d.h:132
float zp
Definition: qvector4d.h:129
float xp
Definition: qvector3d.h:132
float xp
Definition: qvector4d.h:129

◆ QVector3D() [8/8]

QVector3D::QVector3D ( float  xpos,
float  ypos,
float  zpos,
int  dummy 
)
inlineprivate

Definition at line 150 of file qvector3d.h.

150 : xp(xpos), yp(ypos), zp(zpos) {}
float zp
Definition: qvector3d.h:132
float yp
Definition: qvector3d.h:132
float xp
Definition: qvector3d.h:132

Functions

◆ crossProduct()

QVector3D QVector3D::crossProduct ( const QVector3D v1,
const QVector3D v2 
)
static

Returns the cross-product of vectors v1 and v2, which corresponds to the normal vector of a plane defined by v1 and v2.

See also
normal()

Definition at line 352 of file qvector3d.cpp.

Referenced by QMatrix4x4::lookAt(), and normal().

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 }
float zp
Definition: qvector3d.h:132
QVector3D()
Constructs a null vector, i.
Definition: qvector3d.h:146
float yp
Definition: qvector3d.h:132
float xp
Definition: qvector3d.h:132

◆ distanceToLine()

qreal QVector3D::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.

If direction is a null vector, then it does not define a line. In that case, the distance from point to this vertex is returned.

See also
distanceToPlane()

Definition at line 444 of file qvector3d.cpp.

Referenced by distanceToPlane().

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 }
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
bool isNull() const
Returns true if the x, y, and z coordinates are set to 0.
Definition: qvector3d.h:156
Qt::LayoutDirection direction

◆ distanceToPlane() [1/2]

qreal QVector3D::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.

The normal parameter is assumed to have been normalized to a unit vector.

The return value will be negative if the vertex is below the plane, or zero if it is on the plane.

See also
normal(), distanceToLine()

Definition at line 405 of file qvector3d.cpp.

Referenced by normal().

406 {
407  return dotProduct(*this - plane, normal);
408 }
static qreal dotProduct(const QVector3D &v1, const QVector3D &v2)
Returns the dot product of v1 and v2.
Definition: qvector3d.cpp:341

◆ distanceToPlane() [2/2]

qreal QVector3D::distanceToPlane ( const QVector3D plane1,
const QVector3D plane2,
const QVector3D plane3 
) const

Returns the distance from this vertex a plane defined by the vertices plane1, plane2 and plane3.

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

The return value will be negative if the vertex is below the plane, or zero if it is on the plane.

The two vectors that define the plane are plane2 - plane1 and plane3 - plane1.

See also
normal(), distanceToLine()

Definition at line 428 of file qvector3d.cpp.

429 {
430  QVector3D n = normal(plane2 - plane1, plane3 - plane1);
431  return dotProduct(*this - plane1, n);
432 }
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

◆ dotProduct()

qreal QVector3D::dotProduct ( const QVector3D v1,
const QVector3D v2 
)
static

Returns the dot product of v1 and v2.

Definition at line 341 of file qvector3d.cpp.

Referenced by distanceToLine(), and distanceToPlane().

342 {
343  return v1.xp * v2.xp + v1.yp * v2.yp + v1.zp * v2.zp;
344 }
float zp
Definition: qvector3d.h:132
float yp
Definition: qvector3d.h:132
float xp
Definition: qvector3d.h:132

◆ isNull()

bool QVector3D::isNull ( ) const
inline

Returns true if the x, y, and z coordinates are set to 0.

0, otherwise returns false.

Definition at line 156 of file qvector3d.h.

Referenced by distanceToLine().

157 {
158  return qIsNull(xp) && qIsNull(yp) && qIsNull(zp);
159 }
float zp
Definition: qvector3d.h:132
static bool qIsNull(double d)
Definition: qglobal.h:2061
float yp
Definition: qvector3d.h:132
float xp
Definition: qvector3d.h:132

◆ length()

qreal QVector3D::length ( ) const

Returns the length of the vector from the origin.

See also
lengthSquared(), normalized()

Definition at line 632 of file qvector3d.cpp.

633 {
634  return qSqrt(xp * xp + yp * yp + zp * zp);
635 }
float zp
Definition: qvector3d.h:132
float yp
Definition: qvector3d.h:132
float xp
Definition: qvector3d.h:132
qreal qSqrt(qreal v)
Definition: qmath.h:205

◆ lengthSquared()

qreal QVector3D::lengthSquared ( ) const

Returns the squared length of the vector from the origin.

This is equivalent to the dot product of the vector with itself.

See also
length(), dotProduct()

Definition at line 643 of file qvector3d.cpp.

644 {
645  return xp * xp + yp * yp + zp * zp;
646 }
float zp
Definition: qvector3d.h:132
float yp
Definition: qvector3d.h:132
float xp
Definition: qvector3d.h:132

◆ normal() [1/2]

QVector3D QVector3D::normal ( const QVector3D v1,
const QVector3D v2 
)
static

Returns the normal vector of a plane defined by vectors v1 and v2, normalized to be a unit vector.

Use crossProduct() to compute the cross-product of v1 and v2 if you do not need the result to be normalized to a unit vector.

See also
crossProduct(), distanceToPlane()

Definition at line 368 of file qvector3d.cpp.

Referenced by distanceToPlane().

369 {
370  return crossProduct(v1, v2).normalized();
371 }
QVector3D normalized() const
Returns the normalized unit vector form of this vector.
Definition: qvector3d.cpp:247
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

◆ normal() [2/2]

QVector3D QVector3D::normal ( const QVector3D v1,
const QVector3D v2,
const QVector3D v3 
)
static

Returns the normal vector of a plane defined by vectors v2 - v1 and v3 - v1, normalized to be a unit vector.

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Use crossProduct() to compute the cross-product of v2 - v1 and v3 - v1 if you do not need the result to be normalized to a unit vector.

See also
crossProduct(), distanceToPlane()

Definition at line 389 of file qvector3d.cpp.

390 {
391  return crossProduct((v2 - v1), (v3 - v1)).normalized();
392 }
QVector3D normalized() const
Returns the normalized unit vector form of this vector.
Definition: qvector3d.cpp:247
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

◆ normalize()

void QVector3D::normalize ( )

Normalizes the currect vector in place.

Nothing happens if this vector is a null vector or the length of the vector is very close to 1.

See also
length(), normalized()

Definition at line 267 of file qvector3d.cpp.

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 }
float zp
Definition: qvector3d.h:132
float yp
Definition: qvector3d.h:132
float xp
Definition: qvector3d.h:132
static Q_DECL_CONSTEXPR bool qFuzzyIsNull(double d)
Definition: qglobal.h:2043
qreal qSqrt(qreal v)
Definition: qmath.h:205

◆ normalized()

QVector3D QVector3D::normalized ( ) const

Returns the normalized unit vector form of this vector.

If this vector is null, then a null vector is returned. If the length of the vector is very close to 1, then the vector will be returned as-is. Otherwise the normalized form of the vector of length 1 will be returned.

See also
length(), normalize()

Definition at line 247 of file qvector3d.cpp.

Referenced by QQuaternion::fromAxisAndAngle(), QMatrix4x4::lookAt(), and normal().

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 }
float zp
Definition: qvector3d.h:132
QVector3D()
Constructs a null vector, i.
Definition: qvector3d.h:146
float yp
Definition: qvector3d.h:132
float xp
Definition: qvector3d.h:132
static Q_DECL_CONSTEXPR bool qFuzzyIsNull(double d)
Definition: qglobal.h:2043
qreal qSqrt(qreal v)
Definition: qmath.h:205

◆ operator QVariant()

QVector3D::operator QVariant ( ) const

Returns the 3D vector as a QVariant.

Definition at line 622 of file qvector3d.cpp.

623 {
624  return QVariant(QVariant::Vector3D, this);
625 }
The QVariant class acts like a union for the most common Qt data types.
Definition: qvariant.h:92

◆ operator*=() [1/2]

QVector3D & QVector3D::operator*= ( qreal  factor)
inline

Multiplies this vector's coordinates by the given factor, and returns a reference to this vector.

See also
operator/=()

Definition at line 185 of file qvector3d.h.

186 {
187  xp *= factor;
188  yp *= factor;
189  zp *= factor;
190  return *this;
191 }
float zp
Definition: qvector3d.h:132
float yp
Definition: qvector3d.h:132
float xp
Definition: qvector3d.h:132

◆ operator*=() [2/2]

QVector3D & QVector3D::operator*= ( const QVector3D vector)
inline

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.Multiplies the components of this vector by the corresponding components in vector.

Note: this is not the same as the crossProduct() of this vector and vector.

See also
crossProduct()

Definition at line 193 of file qvector3d.h.

194 {
195  xp *= vector.xp;
196  yp *= vector.yp;
197  zp *= vector.zp;
198  return *this;
199 }
float zp
Definition: qvector3d.h:132
float yp
Definition: qvector3d.h:132
float xp
Definition: qvector3d.h:132

◆ operator+=()

QVector3D & QVector3D::operator+= ( const QVector3D vector)
inline

Adds the given vector to this vector and returns a reference to this vector.

See also
operator-=()

Definition at line 169 of file qvector3d.h.

170 {
171  xp += vector.xp;
172  yp += vector.yp;
173  zp += vector.zp;
174  return *this;
175 }
float zp
Definition: qvector3d.h:132
float yp
Definition: qvector3d.h:132
float xp
Definition: qvector3d.h:132

◆ operator-=()

QVector3D & QVector3D::operator-= ( const QVector3D vector)
inline

Subtracts the given vector from this vector and returns a reference to this vector.

See also
operator+=()

Definition at line 177 of file qvector3d.h.

178 {
179  xp -= vector.xp;
180  yp -= vector.yp;
181  zp -= vector.zp;
182  return *this;
183 }
float zp
Definition: qvector3d.h:132
float yp
Definition: qvector3d.h:132
float xp
Definition: qvector3d.h:132

◆ operator/=()

QVector3D & QVector3D::operator/= ( qreal  divisor)
inline

Divides this vector's coordinates by the given divisor, and returns a reference to this vector.

See also
operator*=()

Definition at line 201 of file qvector3d.h.

202 {
203  xp /= divisor;
204  yp /= divisor;
205  zp /= divisor;
206  return *this;
207 }
float zp
Definition: qvector3d.h:132
float yp
Definition: qvector3d.h:132
float xp
Definition: qvector3d.h:132

◆ setX()

void QVector3D::setX ( qreal  x)
inline

Sets the x coordinate of this point to the given x coordinate.

See also
x(), setY(), setZ()

Definition at line 165 of file qvector3d.h.

Referenced by operator>>(), and QDeclarativeVector3DValueType::setX().

165 { xp = aX; }
float xp
Definition: qvector3d.h:132

◆ setY()

void QVector3D::setY ( qreal  y)
inline

Sets the y coordinate of this point to the given y coordinate.

See also
y(), setX(), setZ()

Definition at line 166 of file qvector3d.h.

Referenced by operator>>(), and QDeclarativeVector3DValueType::setY().

166 { yp = aY; }
float yp
Definition: qvector3d.h:132

◆ setZ()

void QVector3D::setZ ( qreal  z)
inline

Sets the z coordinate of this point to the given z coordinate.

See also
z(), setX(), setY()

Definition at line 167 of file qvector3d.h.

Referenced by operator>>(), and QDeclarativeVector3DValueType::setZ().

167 { zp = aZ; }
float zp
Definition: qvector3d.h:132

◆ toPoint()

QPoint QVector3D::toPoint ( ) const
inline

Returns the QPoint form of this 3D vector.

The z coordinate is dropped.

See also
toPointF(), toVector2D()

Definition at line 261 of file qvector3d.h.

262 {
263  return QPoint(qRound(xp), qRound(yp));
264 }
float yp
Definition: qvector3d.h:132
The QPoint class defines a point in the plane using integer precision.
Definition: qpoint.h:53
float xp
Definition: qvector3d.h:132
Q_DECL_CONSTEXPR int qRound(qreal d)
Definition: qglobal.h:1203

◆ toPointF()

QPointF QVector3D::toPointF ( ) const
inline

Returns the QPointF form of this 3D vector.

The z coordinate is dropped.

See also
toPoint(), toVector2D()

Definition at line 266 of file qvector3d.h.

267 {
268  return QPointF(qreal(xp), qreal(yp));
269 }
double qreal
Definition: qglobal.h:1193
The QPointF class defines a point in the plane using floating point precision.
Definition: qpoint.h:214
float yp
Definition: qvector3d.h:132
float xp
Definition: qvector3d.h:132

◆ toVector2D()

QVector2D QVector3D::toVector2D ( ) const

Returns the 2D vector form of this 3D vector, dropping the z coordinate.

See also
toVector4D(), toPoint()

Definition at line 574 of file qvector3d.cpp.

575 {
576  return QVector2D(xp, yp, 1);
577 }
friend class QVector2D
Definition: qvector3d.h:136
float yp
Definition: qvector3d.h:132
float xp
Definition: qvector3d.h:132

◆ toVector4D()

QVector4D QVector3D::toVector4D ( ) const

Returns the 4D form of this 3D vector, with the w coordinate set to zero.

See also
toVector2D(), toPoint()

Definition at line 588 of file qvector3d.cpp.

589 {
590  return QVector4D(xp, yp, zp, 0.0f, 1);
591 }
float zp
Definition: qvector3d.h:132
float yp
Definition: qvector3d.h:132
float xp
Definition: qvector3d.h:132
friend class QVector4D
Definition: qvector3d.h:137

◆ x()

qreal QVector3D::x ( ) const
inline

◆ y()

qreal QVector3D::y ( ) const
inline

◆ z()

qreal QVector3D::z ( ) const
inline

Friends and Related Functions

◆ operator!=

bool operator!= ( const QVector3D v1,
const QVector3D v2 
)
friend

Returns true if v1 is not equal to v2; otherwise returns false.

This operator uses an exact floating-point comparison.

Definition at line 214 of file qvector3d.h.

215 {
216  return v1.xp != v2.xp || v1.yp != v2.yp || v1.zp != v2.zp;
217 }
float zp
Definition: qvector3d.h:132
float yp
Definition: qvector3d.h:132
float xp
Definition: qvector3d.h:132

◆ operator* [1/5]

const QVector3D operator* ( qreal  factor,
const QVector3D vector 
)
friend

Returns a copy of the given vector, multiplied by the given factor.

See also
QVector3D::operator*=()

Definition at line 229 of file qvector3d.h.

230 {
231  return QVector3D(vector.xp * factor, vector.yp * factor, vector.zp * factor, 1);
232 }
float zp
Definition: qvector3d.h:132
QVector3D()
Constructs a null vector, i.
Definition: qvector3d.h:146
float yp
Definition: qvector3d.h:132
float xp
Definition: qvector3d.h:132

◆ operator* [2/5]

const QVector3D operator* ( const QVector3D vector,
qreal  factor 
)
friend

Returns a copy of the given vector, multiplied by the given factor.

See also
QVector3D::operator*=()

Definition at line 234 of file qvector3d.h.

235 {
236  return QVector3D(vector.xp * factor, vector.yp * factor, vector.zp * factor, 1);
237 }
float zp
Definition: qvector3d.h:132
QVector3D()
Constructs a null vector, i.
Definition: qvector3d.h:146
float yp
Definition: qvector3d.h:132
float xp
Definition: qvector3d.h:132

◆ operator* [3/5]

const QVector3D operator* ( const QVector3D v1,
const QVector3D v2 
)
friend

Multiplies the components of v1 by the corresponding components in v2.

Note: this is not the same as the crossProduct() of v1 and v2.

See also
QVector3D::crossProduct()

Definition at line 239 of file qvector3d.h.

240 {
241  return QVector3D(v1.xp * v2.xp, v1.yp * v2.yp, v1.zp * v2.zp, 1);
242 }
float zp
Definition: qvector3d.h:132
QVector3D()
Constructs a null vector, i.
Definition: qvector3d.h:146
float yp
Definition: qvector3d.h:132
float xp
Definition: qvector3d.h:132

◆ operator* [4/5]

QVector3D operator* ( const QVector3D vector,
const QMatrix4x4 matrix 
)
friend

Definition at line 606 of file qmatrix4x4.h.

607 {
608  qreal x, y, z, w;
609  x = vector.x() * matrix.m[0][0] +
610  vector.y() * matrix.m[0][1] +
611  vector.z() * matrix.m[0][2] +
612  matrix.m[0][3];
613  y = vector.x() * matrix.m[1][0] +
614  vector.y() * matrix.m[1][1] +
615  vector.z() * matrix.m[1][2] +
616  matrix.m[1][3];
617  z = vector.x() * matrix.m[2][0] +
618  vector.y() * matrix.m[2][1] +
619  vector.z() * matrix.m[2][2] +
620  matrix.m[2][3];
621  w = vector.x() * matrix.m[3][0] +
622  vector.y() * matrix.m[3][1] +
623  vector.z() * matrix.m[3][2] +
624  matrix.m[3][3];
625  if (w == 1.0f)
626  return QVector3D(x, y, z);
627  else
628  return QVector3D(x / w, y / w, z / w);
629 }
double qreal
Definition: qglobal.h:1193
QVector3D()
Constructs a null vector, i.
Definition: qvector3d.h:146
qreal x() const
Returns the x coordinate of this point.
Definition: qvector3d.h:161
qreal z() const
Returns the z coordinate of this point.
Definition: qvector3d.h:163
qreal m[4][4]
Definition: qmatrix4x4.h:188
qreal y() const
Returns the y coordinate of this point.
Definition: qvector3d.h:162

◆ operator* [5/5]

QVector3D operator* ( const QMatrix4x4 matrix,
const QVector3D vector 
)
friend

Definition at line 631 of file qmatrix4x4.h.

632 {
633  qreal x, y, z, w;
634  if (matrix.flagBits == QMatrix4x4::Identity) {
635  return vector;
636  } else if (matrix.flagBits == QMatrix4x4::Translation) {
637  return QVector3D(vector.x() + matrix.m[3][0],
638  vector.y() + matrix.m[3][1],
639  vector.z() + matrix.m[3][2]);
640  } else if (matrix.flagBits ==
642  return QVector3D(vector.x() * matrix.m[0][0] + matrix.m[3][0],
643  vector.y() * matrix.m[1][1] + matrix.m[3][1],
644  vector.z() * matrix.m[2][2] + matrix.m[3][2]);
645  } else if (matrix.flagBits == QMatrix4x4::Scale) {
646  return QVector3D(vector.x() * matrix.m[0][0],
647  vector.y() * matrix.m[1][1],
648  vector.z() * matrix.m[2][2]);
649  } else {
650  x = vector.x() * matrix.m[0][0] +
651  vector.y() * matrix.m[1][0] +
652  vector.z() * matrix.m[2][0] +
653  matrix.m[3][0];
654  y = vector.x() * matrix.m[0][1] +
655  vector.y() * matrix.m[1][1] +
656  vector.z() * matrix.m[2][1] +
657  matrix.m[3][1];
658  z = vector.x() * matrix.m[0][2] +
659  vector.y() * matrix.m[1][2] +
660  vector.z() * matrix.m[2][2] +
661  matrix.m[3][2];
662  w = vector.x() * matrix.m[0][3] +
663  vector.y() * matrix.m[1][3] +
664  vector.z() * matrix.m[2][3] +
665  matrix.m[3][3];
666  if (w == 1.0f)
667  return QVector3D(x, y, z);
668  else
669  return QVector3D(x / w, y / w, z / w);
670  }
671 }
int flagBits
Definition: qmatrix4x4.h:189
double qreal
Definition: qglobal.h:1193
QVector3D()
Constructs a null vector, i.
Definition: qvector3d.h:146
qreal x() const
Returns the x coordinate of this point.
Definition: qvector3d.h:161
qreal z() const
Returns the z coordinate of this point.
Definition: qvector3d.h:163
qreal m[4][4]
Definition: qmatrix4x4.h:188
qreal y() const
Returns the y coordinate of this point.
Definition: qvector3d.h:162

◆ operator+

const QVector3D operator+ ( const QVector3D v1,
const QVector3D v2 
)
friend

Returns a QVector3D object that is the sum of the given vectors, v1 and v2; each component is added separately.

See also
QVector3D::operator+=()

Definition at line 219 of file qvector3d.h.

220 {
221  return QVector3D(v1.xp + v2.xp, v1.yp + v2.yp, v1.zp + v2.zp, 1);
222 }
float zp
Definition: qvector3d.h:132
QVector3D()
Constructs a null vector, i.
Definition: qvector3d.h:146
float yp
Definition: qvector3d.h:132
float xp
Definition: qvector3d.h:132

◆ operator- [1/2]

const QVector3D operator- ( const QVector3D v1,
const QVector3D v2 
)
friend

Returns a QVector3D object that is formed by subtracting v2 from v1; each component is subtracted separately.

See also
QVector3D::operator-=()

Definition at line 224 of file qvector3d.h.

225 {
226  return QVector3D(v1.xp - v2.xp, v1.yp - v2.yp, v1.zp - v2.zp, 1);
227 }
float zp
Definition: qvector3d.h:132
QVector3D()
Constructs a null vector, i.
Definition: qvector3d.h:146
float yp
Definition: qvector3d.h:132
float xp
Definition: qvector3d.h:132

◆ operator- [2/2]

const QVector3D operator- ( const QVector3D vector)
friend

Returns a QVector3D object that is formed by changing the sign of all three components of the given vector.

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Equivalent to {QVector3D(0,0,0) - vector}.

Definition at line 244 of file qvector3d.h.

245 {
246  return QVector3D(-vector.xp, -vector.yp, -vector.zp, 1);
247 }
float zp
Definition: qvector3d.h:132
QVector3D()
Constructs a null vector, i.
Definition: qvector3d.h:146
float yp
Definition: qvector3d.h:132
float xp
Definition: qvector3d.h:132

◆ operator/

const QVector3D operator/ ( const QVector3D vector,
qreal  divisor 
)
friend

Returns the QVector3D object formed by dividing all three components of the given vector by the given divisor.

See also
QVector3D::operator/=()

Definition at line 249 of file qvector3d.h.

250 {
251  return QVector3D(vector.xp / divisor, vector.yp / divisor, vector.zp / divisor, 1);
252 }
float zp
Definition: qvector3d.h:132
QVector3D()
Constructs a null vector, i.
Definition: qvector3d.h:146
float yp
Definition: qvector3d.h:132
float xp
Definition: qvector3d.h:132

◆ operator<<()

QDataStream & operator<< ( QDataStream stream,
const QVector3D vector 
)
related

Writes the given vector to the given stream and returns a reference to the stream.

See also
{Serializing Qt Data Types}

Definition at line 674 of file qvector3d.cpp.

Referenced by toPointF().

675 {
676  stream << double(vector.x()) << double(vector.y())
677  << double(vector.z());
678  return stream;
679 }
qreal x() const
Returns the x coordinate of this point.
Definition: qvector3d.h:161
static FILE * stream
qreal z() const
Returns the z coordinate of this point.
Definition: qvector3d.h:163
qreal y() const
Returns the y coordinate of this point.
Definition: qvector3d.h:162

◆ operator==

bool operator== ( const QVector3D v1,
const QVector3D v2 
)
friend

Returns true if v1 is equal to v2; otherwise returns false.

This operator uses an exact floating-point comparison.

Definition at line 209 of file qvector3d.h.

210 {
211  return v1.xp == v2.xp && v1.yp == v2.yp && v1.zp == v2.zp;
212 }
float zp
Definition: qvector3d.h:132
float yp
Definition: qvector3d.h:132
float xp
Definition: qvector3d.h:132

◆ operator>>()

QDataStream & operator>> ( QDataStream stream,
QVector3D vector 
)
related

Reads a 3D vector from the given stream into the given vector and returns a reference to the stream.

See also
{Serializing Qt Data Types}

Definition at line 694 of file qvector3d.cpp.

Referenced by toPointF().

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 }
double qreal
Definition: qglobal.h:1193
qreal x() const
Returns the x coordinate of this point.
Definition: qvector3d.h:161
static FILE * stream
void setX(qreal x)
Sets the x coordinate of this point to the given x coordinate.
Definition: qvector3d.h:165
qreal z() const
Returns the z coordinate of this point.
Definition: qvector3d.h:163
void setZ(qreal z)
Sets the z coordinate of this point to the given z coordinate.
Definition: qvector3d.h:167
qreal y() const
Returns the y coordinate of this point.
Definition: qvector3d.h:162
void setY(qreal y)
Sets the y coordinate of this point to the given y coordinate.
Definition: qvector3d.h:166

◆ qFuzzyCompare

bool qFuzzyCompare ( const QVector3D v1,
const QVector3D v2 
)
friend

Returns true if v1 and v2 are equal, allowing for a small fuzziness factor for floating-point comparisons; false otherwise.

Definition at line 254 of file qvector3d.h.

Referenced by qFuzzyCompare().

255 {
256  return qFuzzyCompare(v1.xp, v2.xp) &&
257  qFuzzyCompare(v1.yp, v2.yp) &&
258  qFuzzyCompare(v1.zp, v2.zp);
259 }
float zp
Definition: qvector3d.h:132
float yp
Definition: qvector3d.h:132
float xp
Definition: qvector3d.h:132
friend bool qFuzzyCompare(const QVector3D &v1, const QVector3D &v2)
Returns true if v1 and v2 are equal, allowing for a small fuzziness factor for floating-point compari...
Definition: qvector3d.h:254

◆ QVector2D

friend class QVector2D
friend

Definition at line 136 of file qvector3d.h.

Referenced by toVector2D().

◆ QVector4D

friend class QVector4D
friend

Definition at line 137 of file qvector3d.h.

Referenced by toVector4D().

Properties

◆ xp

float QVector3D::xp
private

◆ yp

float QVector3D::yp
private

◆ zp

float QVector3D::zp
private

The documentation for this class was generated from the following files: