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

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