qvector4d.cpp

Absolute File Name:/home/qt/qt5_coco/qt5/qtbase/src/gui/math3d/qvector4d.cpp
Source codeSwitch to Preprocessed file
LineSourceCount
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 "qvector4d.h"-
35#include "qvector3d.h"-
36#include "qvector2d.h"-
37#include <QtCore/qdatastream.h>-
38#include <QtCore/qdebug.h>-
39#include <QtCore/qvariant.h>-
40#include <QtCore/qmath.h>-
41-
42QT_BEGIN_NAMESPACE-
43-
44#ifndef QT_NO_VECTOR4D-
45-
46/*!-
47 \class QVector4D-
48 \brief The QVector4D class represents a vector or vertex in 4D space.-
49 \since 4.6-
50 \ingroup painting-3D-
51 \inmodule QtGui-
52-
53 The QVector4D class can also be used to represent vertices in 4D space.-
54 We therefore do not need to provide a separate vertex class.-
55-
56 \sa QQuaternion, QVector2D, QVector3D-
57*/-
58-
59/*!-
60 \fn QVector4D::QVector4D()-
61-
62 Constructs a null vector, i.e. with coordinates (0, 0, 0, 0).-
63*/-
64-
65/*!-
66 \fn QVector4D::QVector4D(Qt::Initialization)-
67 \since 5.5-
68 \internal-
69-
70 Constructs a vector without initializing the contents.-
71*/-
72-
73/*!-
74 \fn QVector4D::QVector4D(float xpos, float ypos, float zpos, float wpos)-
75-
76 Constructs a vector with coordinates (\a xpos, \a ypos, \a zpos, \a wpos).-
77*/-
78-
79/*!-
80 \fn QVector4D::QVector4D(const QPoint& point)-
81-
82 Constructs a vector with x and y coordinates from a 2D \a point, and-
83 z and w coordinates of 0.-
84*/-
85-
86/*!-
87 \fn QVector4D::QVector4D(const QPointF& point)-
88-
89 Constructs a vector with x and y coordinates from a 2D \a point, and-
90 z and w coordinates of 0.-
91*/-
92-
93#ifndef QT_NO_VECTOR2D-
94-
95/*!-
96 Constructs a 4D vector from the specified 2D \a vector. The z-
97 and w coordinates are set to zero.-
98-
99 \sa toVector2D()-
100*/-
101QVector4D::QVector4D(const QVector2D& vector)-
102{-
103 xp = vector.xp;-
104 yp = vector.yp;-
105 zp = 0.0f;-
106 wp = 0.0f;-
107}
never executed: end of block
0
108-
109/*!-
110 Constructs a 4D vector from the specified 2D \a vector. The z-
111 and w coordinates are set to \a zpos and \a wpos respectively.-
112-
113 \sa toVector2D()-
114*/-
115QVector4D::QVector4D(const QVector2D& vector, float zpos, float wpos)-
116{-
117 xp = vector.xp;-
118 yp = vector.yp;-
119 zp = zpos;-
120 wp = wpos;-
121}
never executed: end of block
0
122-
123#endif-
124-
125#ifndef QT_NO_VECTOR3D-
126-
127/*!-
128 Constructs a 4D vector from the specified 3D \a vector. The w-
129 coordinate is set to zero.-
130-
131 \sa toVector3D()-
132*/-
133QVector4D::QVector4D(const QVector3D& vector)-
134{-
135 xp = vector.xp;-
136 yp = vector.yp;-
137 zp = vector.zp;-
138 wp = 0.0f;-
139}
never executed: end of block
0
140-
141/*!-
142 Constructs a 4D vector from the specified 3D \a vector. The w-
143 coordinate is set to \a wpos.-
144-
145 \sa toVector3D()-
146*/-
147QVector4D::QVector4D(const QVector3D& vector, float wpos)-
148{-
149 xp = vector.xp;-
150 yp = vector.yp;-
151 zp = vector.zp;-
152 wp = wpos;-
153}
never executed: end of block
0
154-
155#endif-
156-
157/*!-
158 \fn bool QVector4D::isNull() const-
159-
160 Returns \c true if the x, y, z, and w coordinates are set to 0.0,-
161 otherwise returns \c false.-
162*/-
163-
164/*!-
165 \fn float QVector4D::x() const-
166-
167 Returns the x coordinate of this point.-
168-
169 \sa setX(), y(), z(), w()-
170*/-
171-
172/*!-
173 \fn float QVector4D::y() const-
174-
175 Returns the y coordinate of this point.-
176-
177 \sa setY(), x(), z(), w()-
178*/-
179-
180/*!-
181 \fn float QVector4D::z() const-
182-
183 Returns the z coordinate of this point.-
184-
185 \sa setZ(), x(), y(), w()-
186*/-
187-
188/*!-
189 \fn float QVector4D::w() const-
190-
191 Returns the w coordinate of this point.-
192-
193 \sa setW(), x(), y(), z()-
194*/-
195-
196/*!-
197 \fn void QVector4D::setX(float x)-
198-
199 Sets the x coordinate of this point to the given \a x coordinate.-
200-
201 \sa x(), setY(), setZ(), setW()-
202*/-
203-
204/*!-
205 \fn void QVector4D::setY(float y)-
206-
207 Sets the y coordinate of this point to the given \a y coordinate.-
208-
209 \sa y(), setX(), setZ(), setW()-
210*/-
211-
212/*!-
213 \fn void QVector4D::setZ(float z)-
214-
215 Sets the z coordinate of this point to the given \a z coordinate.-
216-
217 \sa z(), setX(), setY(), setW()-
218*/-
219-
220/*!-
221 \fn void QVector4D::setW(float w)-
222-
223 Sets the w coordinate of this point to the given \a w coordinate.-
224-
225 \sa w(), setX(), setY(), setZ()-
226*/-
227-
228/*! \fn float &QVector4D::operator[](int i)-
229 \since 5.2-
230-
231 Returns the component of the vector at index position \a i-
232 as a modifiable reference.-
233-
234 \a i must be a valid index position in the vector (i.e., 0 <= \a i-
235 < 4).-
236*/-
237-
238/*! \fn float QVector4D::operator[](int i) const-
239 \since 5.2-
240-
241 Returns the component of the vector at index position \a i.-
242-
243 \a i must be a valid index position in the vector (i.e., 0 <= \a i-
244 < 4).-
245*/-
246-
247/*!-
248 Returns the length of the vector from the origin.-
249-
250 \sa lengthSquared(), normalized()-
251*/-
252float QVector4D::length() const-
253{-
254 // Need some extra precision if the length is very small.-
255 double len = double(xp) * double(xp) +-
256 double(yp) * double(yp) +-
257 double(zp) * double(zp) +-
258 double(wp) * double(wp);-
259 return float(std::sqrt(len));
never executed: return float(std::sqrt(len));
0
260}-
261-
262/*!-
263 Returns the squared length of the vector from the origin.-
264 This is equivalent to the dot product of the vector with itself.-
265-
266 \sa length(), dotProduct()-
267*/-
268float QVector4D::lengthSquared() const-
269{-
270 return xp * xp + yp * yp + zp * zp + wp * wp;
never executed: return xp * xp + yp * yp + zp * zp + wp * wp;
0
271}-
272-
273/*!-
274 Returns the normalized unit vector form of this vector.-
275-
276 If this vector is null, then a null vector is returned. If the length-
277 of the vector is very close to 1, then the vector will be returned as-is.-
278 Otherwise the normalized form of the vector of length 1 will be returned.-
279-
280 \sa length(), normalize()-
281*/-
282QVector4D QVector4D::normalized() const-
283{-
284 // Need some extra precision if the length is very small.-
285 double len = double(xp) * double(xp) +-
286 double(yp) * double(yp) +-
287 double(zp) * double(zp) +-
288 double(wp) * double(wp);-
289 if (qFuzzyIsNull(len - 1.0f)) {
qFuzzyIsNull(len - 1.0f)Description
TRUEnever evaluated
FALSEnever evaluated
0
290 return *this;
never executed: return *this;
0
291 } else if (!qFuzzyIsNull(len)) {
!qFuzzyIsNull(len)Description
TRUEnever evaluated
FALSEnever evaluated
0
292 double sqrtLen = std::sqrt(len);-
293 return QVector4D(float(double(xp) / sqrtLen),
never executed: return QVector4D(float(double(xp) / sqrtLen), float(double(yp) / sqrtLen), float(double(zp) / sqrtLen), float(double(wp) / sqrtLen));
0
294 float(double(yp) / sqrtLen),
never executed: return QVector4D(float(double(xp) / sqrtLen), float(double(yp) / sqrtLen), float(double(zp) / sqrtLen), float(double(wp) / sqrtLen));
0
295 float(double(zp) / sqrtLen),
never executed: return QVector4D(float(double(xp) / sqrtLen), float(double(yp) / sqrtLen), float(double(zp) / sqrtLen), float(double(wp) / sqrtLen));
0
296 float(double(wp) / sqrtLen));
never executed: return QVector4D(float(double(xp) / sqrtLen), float(double(yp) / sqrtLen), float(double(zp) / sqrtLen), float(double(wp) / sqrtLen));
0
297 } else {-
298 return QVector4D();
never executed: return QVector4D();
0
299 }-
300}-
301-
302/*!-
303 Normalizes the currect vector in place. Nothing happens if this-
304 vector is a null vector or the length of the vector is very close to 1.-
305-
306 \sa length(), normalized()-
307*/-
308void QVector4D::normalize()-
309{-
310 // Need some extra precision if the length is very small.-
311 double len = double(xp) * double(xp) +-
312 double(yp) * double(yp) +-
313 double(zp) * double(zp) +-
314 double(wp) * double(wp);-
315 if (qFuzzyIsNull(len - 1.0f) || qFuzzyIsNull(len))
qFuzzyIsNull(len - 1.0f)Description
TRUEnever evaluated
FALSEnever evaluated
qFuzzyIsNull(len)Description
TRUEnever evaluated
FALSEnever evaluated
0
316 return;
never executed: return;
0
317-
318 len = std::sqrt(len);-
319-
320 xp = float(double(xp) / len);-
321 yp = float(double(yp) / len);-
322 zp = float(double(zp) / len);-
323 wp = float(double(wp) / len);-
324}
never executed: end of block
0
325-
326/*!-
327 \fn QVector4D &QVector4D::operator+=(const QVector4D &vector)-
328-
329 Adds the given \a vector to this vector and returns a reference to-
330 this vector.-
331-
332 \sa operator-=()-
333*/-
334-
335/*!-
336 \fn QVector4D &QVector4D::operator-=(const QVector4D &vector)-
337-
338 Subtracts the given \a vector from this vector and returns a reference to-
339 this vector.-
340-
341 \sa operator+=()-
342*/-
343-
344/*!-
345 \fn QVector4D &QVector4D::operator*=(float factor)-
346-
347 Multiplies this vector's coordinates by the given \a factor, and-
348 returns a reference to this vector.-
349-
350 \sa operator/=()-
351*/-
352-
353/*!-
354 \fn QVector4D &QVector4D::operator*=(const QVector4D &vector)-
355-
356 Multiplies the components of this vector by the corresponding-
357 components in \a vector.-
358*/-
359-
360/*!-
361 \fn QVector4D &QVector4D::operator/=(float divisor)-
362-
363 Divides this vector's coordinates by the given \a divisor, and-
364 returns a reference to this vector.-
365-
366 \sa operator*=()-
367*/-
368-
369/*!-
370 \fn QVector4D &QVector4D::operator/=(const QVector4D &vector)-
371 \since 5.5-
372-
373 Divides the components of this vector by the corresponding-
374 components in \a vector.-
375-
376 \sa operator*=()-
377*/-
378-
379/*!-
380 Returns the dot product of \a v1 and \a v2.-
381*/-
382float QVector4D::dotProduct(const QVector4D& v1, const QVector4D& v2)-
383{-
384 return v1.xp * v2.xp + v1.yp * v2.yp + v1.zp * v2.zp + v1.wp * v2.wp;
never executed: return v1.xp * v2.xp + v1.yp * v2.yp + v1.zp * v2.zp + v1.wp * v2.wp;
0
385}-
386-
387/*!-
388 \fn bool operator==(const QVector4D &v1, const QVector4D &v2)-
389 \relates QVector4D-
390-
391 Returns \c true if \a v1 is equal to \a v2; otherwise returns \c false.-
392 This operator uses an exact floating-point comparison.-
393*/-
394-
395/*!-
396 \fn bool operator!=(const QVector4D &v1, const QVector4D &v2)-
397 \relates QVector4D-
398-
399 Returns \c true if \a v1 is not equal to \a v2; otherwise returns \c false.-
400 This operator uses an exact floating-point comparison.-
401*/-
402-
403/*!-
404 \fn const QVector4D operator+(const QVector4D &v1, const QVector4D &v2)-
405 \relates QVector4D-
406-
407 Returns a QVector4D object that is the sum of the given vectors, \a v1-
408 and \a v2; each component is added separately.-
409-
410 \sa QVector4D::operator+=()-
411*/-
412-
413/*!-
414 \fn const QVector4D operator-(const QVector4D &v1, const QVector4D &v2)-
415 \relates QVector4D-
416-
417 Returns a QVector4D object that is formed by subtracting \a v2 from \a v1;-
418 each component is subtracted separately.-
419-
420 \sa QVector4D::operator-=()-
421*/-
422-
423/*!-
424 \fn const QVector4D operator*(float factor, const QVector4D &vector)-
425 \relates QVector4D-
426-
427 Returns a copy of the given \a vector, multiplied by the given \a factor.-
428-
429 \sa QVector4D::operator*=()-
430*/-
431-
432/*!-
433 \fn const QVector4D operator*(const QVector4D &vector, float factor)-
434 \relates QVector4D-
435-
436 Returns a copy of the given \a vector, multiplied by the given \a factor.-
437-
438 \sa QVector4D::operator*=()-
439*/-
440-
441/*!-
442 \fn const QVector4D operator*(const QVector4D &v1, const QVector4D& v2)-
443 \relates QVector4D-
444-
445 Returns the vector consisting of the multiplication of the-
446 components from \a v1 and \a v2.-
447-
448 \sa QVector4D::operator*=()-
449*/-
450-
451/*!-
452 \fn const QVector4D operator-(const QVector4D &vector)-
453 \relates QVector4D-
454 \overload-
455-
456 Returns a QVector4D object that is formed by changing the sign of-
457 all three components of the given \a vector.-
458-
459 Equivalent to \c {QVector4D(0,0,0,0) - vector}.-
460*/-
461-
462/*!-
463 \fn const QVector4D operator/(const QVector4D &vector, float divisor)-
464 \relates QVector4D-
465-
466 Returns the QVector4D object formed by dividing all four components of-
467 the given \a vector by the given \a divisor.-
468-
469 \sa QVector4D::operator/=()-
470*/-
471-
472/*!-
473 \fn const QVector4D operator/(const QVector4D &vector, const QVector4D &divisor)-
474 \relates QVector4D-
475 \since 5.5-
476-
477 Returns the QVector4D object formed by dividing components of the given-
478 \a vector by a respective components of the given \a divisor.-
479-
480 \sa QVector4D::operator/=()-
481*/-
482-
483/*!-
484 \fn bool qFuzzyCompare(const QVector4D& v1, const QVector4D& v2)-
485 \relates QVector4D-
486-
487 Returns \c true if \a v1 and \a v2 are equal, allowing for a small-
488 fuzziness factor for floating-point comparisons; false otherwise.-
489*/-
490-
491#ifndef QT_NO_VECTOR2D-
492-
493/*!-
494 Returns the 2D vector form of this 4D vector, dropping the z and w coordinates.-
495-
496 \sa toVector2DAffine(), toVector3D(), toPoint()-
497*/-
498QVector2D QVector4D::toVector2D() const-
499{-
500 return QVector2D(xp, yp);
never executed: return QVector2D(xp, yp);
0
501}-
502-
503/*!-
504 Returns the 2D vector form of this 4D vector, dividing the x and y-
505 coordinates by the w coordinate and dropping the z coordinate.-
506 Returns a null vector if w is zero.-
507-
508 \sa toVector2D(), toVector3DAffine(), toPoint()-
509*/-
510QVector2D QVector4D::toVector2DAffine() const-
511{-
512 if (qIsNull(wp))
qIsNull(wp)Description
TRUEnever evaluated
FALSEnever evaluated
0
513 return QVector2D();
never executed: return QVector2D();
0
514 return QVector2D(xp / wp, yp / wp);
never executed: return QVector2D(xp / wp, yp / wp);
0
515}-
516-
517#endif-
518-
519#ifndef QT_NO_VECTOR3D-
520-
521/*!-
522 Returns the 3D vector form of this 4D vector, dropping the w coordinate.-
523-
524 \sa toVector3DAffine(), toVector2D(), toPoint()-
525*/-
526QVector3D QVector4D::toVector3D() const-
527{-
528 return QVector3D(xp, yp, zp);
never executed: return QVector3D(xp, yp, zp);
0
529}-
530-
531/*!-
532 Returns the 3D vector form of this 4D vector, dividing the x, y, and-
533 z coordinates by the w coordinate. Returns a null vector if w is zero.-
534-
535 \sa toVector3D(), toVector2DAffine(), toPoint()-
536*/-
537QVector3D QVector4D::toVector3DAffine() const-
538{-
539 if (qIsNull(wp))
qIsNull(wp)Description
TRUEnever evaluated
FALSEnever evaluated
0
540 return QVector3D();
never executed: return QVector3D();
0
541 return QVector3D(xp / wp, yp / wp, zp / wp);
never executed: return QVector3D(xp / wp, yp / wp, zp / wp);
0
542}-
543-
544#endif-
545-
546/*!-
547 \fn QPoint QVector4D::toPoint() const-
548-
549 Returns the QPoint form of this 4D vector. The z and w coordinates-
550 are dropped.-
551-
552 \sa toPointF(), toVector2D()-
553*/-
554-
555/*!-
556 \fn QPointF QVector4D::toPointF() const-
557-
558 Returns the QPointF form of this 4D vector. The z and w coordinates-
559 are dropped.-
560-
561 \sa toPoint(), toVector2D()-
562*/-
563-
564/*!-
565 Returns the 4D vector as a QVariant.-
566*/-
567QVector4D::operator QVariant() const-
568{-
569 return QVariant(QVariant::Vector4D, this);
never executed: return QVariant(QVariant::Vector4D, this);
0
570}-
571-
572#ifndef QT_NO_DEBUG_STREAM-
573-
574QDebug operator<<(QDebug dbg, const QVector4D &vector)-
575{-
576 QDebugStateSaver saver(dbg);-
577 dbg.nospace() << "QVector4D("-
578 << vector.x() << ", " << vector.y() << ", "-
579 << vector.z() << ", " << vector.w() << ')';-
580 return dbg;
never executed: return dbg;
0
581}-
582-
583#endif-
584-
585#ifndef QT_NO_DATASTREAM-
586-
587/*!-
588 \fn QDataStream &operator<<(QDataStream &stream, const QVector4D &vector)-
589 \relates QVector4D-
590-
591 Writes the given \a vector to the given \a stream and returns a-
592 reference to the stream.-
593-
594 \sa {Serializing Qt Data Types}-
595*/-
596-
597QDataStream &operator<<(QDataStream &stream, const QVector4D &vector)-
598{-
599 stream << vector.x() << vector.y()-
600 << vector.z() << vector.w();-
601 return stream;
never executed: return stream;
0
602}-
603-
604/*!-
605 \fn QDataStream &operator>>(QDataStream &stream, QVector4D &vector)-
606 \relates QVector4D-
607-
608 Reads a 4D vector from the given \a stream into the given \a vector-
609 and returns a reference to the stream.-
610-
611 \sa {Serializing Qt Data Types}-
612*/-
613-
614QDataStream &operator>>(QDataStream &stream, QVector4D &vector)-
615{-
616 float x, y, z, w;-
617 stream >> x;-
618 stream >> y;-
619 stream >> z;-
620 stream >> w;-
621 vector.setX(x);-
622 vector.setY(y);-
623 vector.setZ(z);-
624 vector.setW(w);-
625 return stream;
never executed: return stream;
0
626}-
627-
628#endif // QT_NO_DATASTREAM-
629-
630#endif // QT_NO_VECTOR4D-
631-
632QT_END_NAMESPACE-
Source codeSwitch to Preprocessed file

Generated by Squish Coco Non-Commercial 4.3.0-BETA-master-30-08-2018-4cb69e9