Coverage Report

Created: 2026-02-08 06:52

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/qt/qtbase/src/gui/math3d/qvectornd.cpp
Line
Count
Source
1
// Copyright (C) 2016 The Qt Company Ltd.
2
// Copyright (C) 2020 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com, author Giuseppe D'Angelo <giuseppe.dangelo@kdab.com>
3
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
4
5
#include "qvectornd.h"
6
#include <QtCore/qdatastream.h>
7
#include <QtCore/qdebug.h>
8
#include <QtCore/qvariant.h>
9
#include <QtGui/qmatrix4x4.h>
10
11
QT_BEGIN_NAMESPACE
12
13
#ifndef QT_NO_VECTOR2D
14
15
/*!
16
    \class QVector2D
17
    \brief The QVector2D class represents a vector or vertex in 2D space.
18
    \since 4.6
19
    \ingroup painting
20
    \ingroup painting-3D
21
    \inmodule QtGui
22
23
    Vectors are one of the main building blocks of 2D representation and
24
    drawing. They consist of two finite floating-point coordinates,
25
    traditionally called x and y.
26
27
    The QVector2D class can also be used to represent vertices in 2D space.
28
    We therefore do not need to provide a separate vertex class.
29
30
    \sa QVector3D, QVector4D, QQuaternion
31
*/
32
33
/*!
34
    \fn QVector2D::QVector2D()
35
36
    Constructs a null vector, i.e. with coordinates (0, 0).
37
*/
38
39
/*!
40
    \fn QVector2D::QVector2D(Qt::Initialization)
41
    \since 5.5
42
    \internal
43
44
    Constructs a vector without initializing the contents.
45
*/
46
47
/*!
48
    \fn QVector2D::QVector2D(float xpos, float ypos)
49
50
    Constructs a vector with coordinates (\a xpos, \a ypos).
51
    Both coordinates must be finite.
52
*/
53
54
/*!
55
    \fn QVector2D::QVector2D(QPoint point)
56
57
    Constructs a vector with x and y coordinates from a 2D \a point.
58
*/
59
60
/*!
61
    \fn QVector2D::QVector2D(QPointF point)
62
63
    Constructs a vector with x and y coordinates from a 2D \a point.
64
*/
65
66
#ifndef QT_NO_VECTOR3D
67
68
/*!
69
    \fn QVector2D::QVector2D(QVector3D vector)
70
71
    Constructs a vector with x and y coordinates from a 3D \a vector.
72
    The z coordinate of \a vector is dropped.
73
74
    \sa toVector3D()
75
*/
76
77
#endif
78
79
#ifndef QT_NO_VECTOR4D
80
81
/*!
82
    \fn QVector2D::QVector2D(QVector4D vector)
83
84
    Constructs a vector with x and y coordinates from a 3D \a vector.
85
    The z and w coordinates of \a vector are dropped.
86
87
    \sa toVector4D()
88
*/
89
90
#endif
91
92
/*!
93
    \fn bool QVector2D::isNull() const
94
95
    Returns \c true if the x and y coordinates are set to 0.0,
96
    otherwise returns \c false.
97
*/
98
99
/*!
100
    \fn float QVector2D::x() const
101
102
    Returns the x coordinate of this point.
103
104
    \sa setX(), y()
105
*/
106
107
/*!
108
    \fn float QVector2D::y() const
109
110
    Returns the y coordinate of this point.
111
112
    \sa setY(), x()
113
*/
114
115
/*!
116
    \fn void QVector2D::setX(float x)
117
118
    Sets the x coordinate of this point to the given finite \a x coordinate.
119
120
    \sa x(), setY()
121
*/
122
123
/*!
124
    \fn void QVector2D::setY(float y)
125
126
    Sets the y coordinate of this point to the given finite \a y coordinate.
127
128
    \sa y(), setX()
129
*/
130
131
/*! \fn float &QVector2D::operator[](int i)
132
    \since 5.2
133
134
    Returns the component of the vector at index position \a i
135
    as a modifiable reference.
136
137
    \a i must be a valid index position in the vector (i.e., 0 <= \a i
138
    < 2).
139
*/
140
141
/*! \fn float QVector2D::operator[](int i) const
142
    \since 5.2
143
144
    Returns the component of the vector at index position \a i.
145
146
    \a i must be a valid index position in the vector (i.e., 0 <= \a i
147
    < 2).
148
*/
149
150
/*!
151
    \fn float QVector2D::length() const
152
153
    Returns the length of the vector from the origin.
154
155
    \sa lengthSquared(), normalized()
156
*/
157
158
/*!
159
    \fn float QVector2D::lengthSquared() const
160
161
    Returns the squared length of the vector from the origin.
162
    This is equivalent to the dot product of the vector with itself.
163
164
    \sa length(), dotProduct()
165
*/
166
167
/*!
168
    \fn QVector2D QVector2D::normalized() const
169
170
    Returns the normalized unit vector form of this vector.
171
172
    If this vector is null, then a null vector is returned. If the length
173
    of the vector is very close to 1, then the vector will be returned as-is.
174
    Otherwise the normalized form of the vector of length 1 will be returned.
175
176
    \sa length(), normalize()
177
*/
178
179
/*!
180
    \fn void QVector2D::normalize()
181
182
    Normalizes the current vector in place. Nothing happens if this
183
    vector is a null vector or the length of the vector is very close to 1.
184
185
    \sa length(), normalized()
186
*/
187
188
/*!
189
    \fn float QVector2D::distanceToPoint(QVector2D point) const
190
    \since 5.1
191
192
    Returns the distance from this vertex to a point defined by
193
    the vertex \a point.
194
195
    \sa distanceToLine()
196
*/
197
198
/*!
199
    \fn float QVector2D::distanceToLine(QVector2D point, QVector2D direction) const
200
    \since 5.1
201
202
    Returns the distance that this vertex is from a line defined
203
    by \a point and the unit vector \a direction.
204
205
    If \a direction is a null vector, then it does not define a line.
206
    In that case, the distance from \a point to this vertex is returned.
207
208
    \sa distanceToPoint()
209
*/
210
211
/*!
212
    \fn QVector2D &QVector2D::operator+=(QVector2D vector)
213
214
    Adds the given \a vector to this vector and returns a reference to
215
    this vector.
216
217
    \sa operator-=()
218
*/
219
220
/*!
221
    \fn QVector2D &QVector2D::operator-=(QVector2D vector)
222
223
    Subtracts the given \a vector from this vector and returns a reference to
224
    this vector.
225
226
    \sa operator+=()
227
*/
228
229
/*!
230
    \fn QVector2D &QVector2D::operator*=(float factor)
231
232
    Multiplies this vector's coordinates by the given finite \a factor and
233
    returns a reference to this vector.
234
235
    \sa operator/=(), operator*()
236
*/
237
238
/*!
239
    \fn QVector2D &QVector2D::operator*=(QVector2D vector)
240
241
    Multiplies each component of this vector by the corresponding component of
242
    \a vector and returns a reference to this vector.
243
244
    \note This is not a cross product of this vector with \a vector. (Its
245
    components add up to the dot product of this vector and \a vector.)
246
247
    \sa operator/=(), operator*()
248
*/
249
250
/*!
251
    \fn QVector2D &QVector2D::operator/=(float divisor)
252
253
    Divides this vector's coordinates by the given \a divisor and returns a
254
    reference to this vector. The \a divisor must not be either zero or NaN.
255
256
    \sa operator*=()
257
*/
258
259
/*!
260
    \fn QVector2D &QVector2D::operator/=(QVector2D vector)
261
    \since 5.5
262
263
    Divides each component of this vector by the corresponding component of \a
264
    vector and returns a reference to this vector.
265
266
    The \a vector must have no component that is either zero or NaN.
267
268
    \sa operator*=(), operator/()
269
*/
270
271
/*!
272
    \fn float QVector2D::dotProduct(QVector2D v1, QVector2D v2)
273
274
    Returns the dot product of \a v1 and \a v2.
275
*/
276
277
/*!
278
    \fn bool QVector2D::operator==(QVector2D v1, QVector2D v2)
279
280
    Returns \c true if \a v1 is equal to \a v2; otherwise returns \c false.
281
    This operator uses an exact floating-point comparison.
282
*/
283
284
/*!
285
    \fn bool QVector2D::operator!=(QVector2D v1, QVector2D v2)
286
287
    Returns \c true if \a v1 is not equal to \a v2; otherwise returns \c false.
288
    This operator uses an exact floating-point comparison.
289
*/
290
291
/*! //! friend
292
    \fn const QVector2D QVector2D::operator+(QVector2D v1, QVector2D v2)
293
294
    Returns a QVector2D object that is the sum of the given vectors, \a v1
295
    and \a v2; each component is added separately.
296
297
    \sa QVector2D::operator+=()
298
*/
299
300
/*! //! friend
301
    \fn const QVector2D QVector2D::operator-(QVector2D v1, QVector2D v2)
302
303
    Returns a QVector2D object that is formed by subtracting \a v2 from \a v1;
304
    each component is subtracted separately.
305
306
    \sa QVector2D::operator-=()
307
*/
308
309
/*! //! friend
310
    \fn const QVector2D QVector2D::operator*(float factor, QVector2D vector)
311
312
    Returns a copy of the given \a vector, multiplied by the given finite \a factor.
313
314
    \sa QVector2D::operator*=()
315
*/
316
317
/*! //! friend
318
    \fn const QVector2D QVector2D::operator*(QVector2D vector, float factor)
319
320
    Returns a copy of the given \a vector, multiplied by the given finite \a factor.
321
322
    \sa QVector2D::operator*=()
323
*/
324
325
/*! //! friend
326
    \fn const QVector2D QVector2D::operator*(QVector2D v1, QVector2D v2)
327
328
    Returns the QVector2D object formed by multiplying each component of \a v1
329
    by the corresponding component of \a v2.
330
331
    \note This is not a cross product of \a v1 and \a v2 in any sense.
332
    (Its components add up to the dot product of \a v1 and \a v2.)
333
334
    \sa QVector2D::operator*=()
335
*/
336
337
/*! //! friend
338
    \fn const QVector2D QVector2D::operator-(QVector2D vector)
339
    \overload
340
341
    Returns a QVector2D object that is formed by changing the sign of each
342
    component of the given \a vector.
343
344
    Equivalent to \c {QVector2D(0,0) - vector}.
345
*/
346
347
/*! //! friend
348
    \fn const QVector2D QVector2D::operator/(QVector2D vector, float divisor)
349
350
    Returns the QVector2D object formed by dividing each component of the given
351
    \a vector by the given \a divisor.
352
353
    The \a divisor must not be either zero or NaN.
354
355
    \sa QVector2D::operator/=()
356
*/
357
358
/*! //! friend
359
    \fn const QVector2D QVector2D::operator/(QVector2D vector, QVector2D divisor)
360
    \since 5.5
361
362
    Returns the QVector2D object formed by dividing each component of the given
363
    \a vector by the corresponding component of the given \a divisor.
364
365
    The \a divisor must have no component that is either zero or NaN.
366
367
    \sa QVector2D::operator/=()
368
*/
369
370
/*! //! friend
371
    \fn bool QVector2D::qFuzzyCompare(QVector2D v1, QVector2D v2)
372
373
    Returns \c true if \a v1 and \a v2 are equal, allowing for a small
374
    fuzziness factor for floating-point comparisons; false otherwise.
375
*/
376
bool qFuzzyCompare(QVector2D v1, QVector2D v2) noexcept
377
0
{
378
0
    return QtPrivate::fuzzyCompare(v1.v[0], v2.v[0])
379
0
        && QtPrivate::fuzzyCompare(v1.v[1], v2.v[1]);
380
0
}
381
382
#ifndef QT_NO_VECTOR3D
383
/*!
384
    \fn QVector3D QVector2D::toVector3D() const
385
386
    Returns the 3D form of this 2D vector, with the z coordinate set to zero.
387
388
    \sa toVector4D(), toPoint()
389
*/
390
#endif
391
392
#ifndef QT_NO_VECTOR4D
393
/*!
394
    \fn QVector4D QVector2D::toVector4D() const
395
396
    Returns the 4D form of this 2D vector, with the z and w coordinates set to zero.
397
398
    \sa toVector3D(), toPoint()
399
*/
400
#endif
401
402
/*!
403
    \fn QPoint QVector2D::toPoint() const
404
405
    Returns the QPoint form of this 2D vector.
406
    Each coordinate is rounded to the nearest integer.
407
408
    \sa toPointF(), toVector3D()
409
*/
410
411
/*!
412
    \fn QPointF QVector2D::toPointF() const
413
414
    Returns the QPointF form of this 2D vector.
415
416
    \sa toPoint(), toVector3D()
417
*/
418
419
/*!
420
    Returns the 2D vector as a QVariant.
421
*/
422
QVector2D::operator QVariant() const
423
0
{
424
0
    return QVariant::fromValue(*this);
425
0
}
426
427
#ifndef QT_NO_DEBUG_STREAM
428
429
QDebug operator<<(QDebug dbg, QVector2D vector)
430
0
{
431
0
    QDebugStateSaver saver(dbg);
432
0
    dbg.nospace() << "QVector2D(" << vector.x() << ", " << vector.y() << ')';
433
0
    return dbg;
434
0
}
435
436
#endif
437
438
#ifndef QT_NO_DATASTREAM
439
440
/*!
441
    \fn QDataStream &operator<<(QDataStream &stream, QVector2D vector)
442
    \relates QVector2D
443
444
    Writes the given \a vector to the given \a stream and returns a
445
    reference to the stream.
446
447
    \sa {Serializing Qt Data Types}
448
*/
449
450
QDataStream &operator<<(QDataStream &stream, QVector2D vector)
451
0
{
452
0
    stream << vector.x() << vector.y();
453
0
    return stream;
454
0
}
455
456
/*!
457
    \fn QDataStream &operator>>(QDataStream &stream, QVector2D &vector)
458
    \relates QVector2D
459
460
    Reads a 2D vector from the given \a stream into the given \a vector
461
    and returns a reference to the stream.
462
463
    \sa {Serializing Qt Data Types}
464
*/
465
466
QDataStream &operator>>(QDataStream &stream, QVector2D &vector)
467
0
{
468
0
    float x, y;
469
0
    stream >> x;
470
0
    stream >> y;
471
0
    vector.setX(x);
472
0
    vector.setY(y);
473
0
    return stream;
474
0
}
475
476
#endif // QT_NO_DATASTREAM
477
478
#endif // QT_NO_VECTOR2D
479
480
481
482
#ifndef QT_NO_VECTOR3D
483
484
/*!
485
    \class QVector3D
486
    \brief The QVector3D class represents a vector or vertex in 3D space.
487
    \since 4.6
488
    \ingroup painting-3D
489
    \inmodule QtGui
490
491
    Vectors are one of the main building blocks of 3D representation and
492
    drawing. They consist of three finite floating-point coordinates,
493
    traditionally called x, y, and z.
494
495
    The QVector3D class can also be used to represent vertices in 3D space.
496
    We therefore do not need to provide a separate vertex class.
497
498
    \sa QVector2D, QVector4D, QQuaternion
499
*/
500
501
/*!
502
    \fn QVector3D::QVector3D()
503
504
    Constructs a null vector, i.e. with coordinates (0, 0, 0).
505
*/
506
507
/*!
508
    \fn QVector3D::QVector3D(Qt::Initialization)
509
    \since 5.5
510
    \internal
511
512
    Constructs a vector without initializing the contents.
513
*/
514
515
/*!
516
    \fn QVector3D::QVector3D(float xpos, float ypos, float zpos)
517
518
    Constructs a vector with coordinates (\a xpos, \a ypos, \a zpos).
519
    All parameters must be finite.
520
*/
521
522
/*!
523
    \fn QVector3D::QVector3D(QPoint point)
524
525
    Constructs a vector with x and y coordinates from a 2D \a point, and a
526
    z coordinate of 0.
527
*/
528
529
/*!
530
    \fn QVector3D::QVector3D(QPointF point)
531
532
    Constructs a vector with x and y coordinates from a 2D \a point, and a
533
    z coordinate of 0.
534
*/
535
536
#ifndef QT_NO_VECTOR2D
537
538
/*!
539
    \fn QVector3D::QVector3D(QVector2D vector)
540
541
    Constructs a 3D vector from the specified 2D \a vector. The z
542
    coordinate is set to zero.
543
544
    \sa toVector2D()
545
*/
546
547
/*!
548
    \fn QVector3D::QVector3D(QVector2D vector, float zpos)
549
550
    Constructs a 3D vector from the specified 2D \a vector. The z
551
    coordinate is set to \a zpos, which must be finite.
552
553
    \sa toVector2D()
554
*/
555
#endif
556
557
#ifndef QT_NO_VECTOR4D
558
559
/*!
560
    \fn QVector3D::QVector3D(QVector4D vector)
561
562
    Constructs a 3D vector from the specified 4D \a vector. The w
563
    coordinate is dropped.
564
565
    \sa toVector4D()
566
*/
567
568
#endif
569
570
/*!
571
    \fn bool QVector3D::isNull() const
572
573
    Returns \c true if the x, y, and z coordinates are set to 0.0,
574
    otherwise returns \c false.
575
*/
576
577
/*!
578
    \fn float QVector3D::x() const
579
580
    Returns the x coordinate of this point.
581
582
    \sa setX(), y(), z()
583
*/
584
585
/*!
586
    \fn float QVector3D::y() const
587
588
    Returns the y coordinate of this point.
589
590
    \sa setY(), x(), z()
591
*/
592
593
/*!
594
    \fn float QVector3D::z() const
595
596
    Returns the z coordinate of this point.
597
598
    \sa setZ(), x(), y()
599
*/
600
601
/*!
602
    \fn void QVector3D::setX(float x)
603
604
    Sets the x coordinate of this point to the given finite \a x coordinate.
605
606
    \sa x(), setY(), setZ()
607
*/
608
609
/*!
610
    \fn void QVector3D::setY(float y)
611
612
    Sets the y coordinate of this point to the given finite \a y coordinate.
613
614
    \sa y(), setX(), setZ()
615
*/
616
617
/*!
618
    \fn void QVector3D::setZ(float z)
619
620
    Sets the z coordinate of this point to the given finite \a z coordinate.
621
622
    \sa z(), setX(), setY()
623
*/
624
625
/*! \fn float &QVector3D::operator[](int i)
626
    \since 5.2
627
628
    Returns the component of the vector at index position \a i
629
    as a modifiable reference.
630
631
    \a i must be a valid index position in the vector (i.e., 0 <= \a i
632
    < 3).
633
*/
634
635
/*! \fn float QVector3D::operator[](int i) const
636
    \since 5.2
637
638
    Returns the component of the vector at index position \a i.
639
640
    \a i must be a valid index position in the vector (i.e., 0 <= \a i
641
    < 3).
642
*/
643
644
/*!
645
    \fn QVector3D QVector3D::normalized() const
646
647
    Returns the normalized unit vector form of this vector.
648
649
    If this vector is null, then a null vector is returned. If the length
650
    of the vector is very close to 1, then the vector will be returned as-is.
651
    Otherwise the normalized form of the vector of length 1 will be returned.
652
653
    \sa length(), normalize()
654
*/
655
656
/*!
657
    \fn void QVector3D::normalize()
658
659
    Normalizes the current vector in place. Nothing happens if this
660
    vector is a null vector or the length of the vector is very close to 1.
661
662
    \sa length(), normalized()
663
*/
664
665
/*!
666
    \fn QVector3D &QVector3D::operator+=(QVector3D vector)
667
668
    Adds the given \a vector to this vector and returns a reference to
669
    this vector.
670
671
    \sa operator-=()
672
*/
673
674
/*!
675
    \fn QVector3D &QVector3D::operator-=(QVector3D vector)
676
677
    Subtracts the given \a vector from this vector and returns a reference to
678
    this vector.
679
680
    \sa operator+=()
681
*/
682
683
/*!
684
    \fn QVector3D &QVector3D::operator*=(float factor)
685
686
    Multiplies this vector's coordinates by the given finite \a factor and
687
    returns a reference to this vector.
688
689
    \sa operator/=(), operator*()
690
*/
691
692
/*!
693
    \fn QVector3D &QVector3D::operator*=(QVector3D vector)
694
    \overload
695
696
    Multiplies each component of this vector by the corresponding component in
697
    \a vector and returns a reference to this vector.
698
699
    Note: this is not the same as the crossProduct() of this vector and
700
    \a vector. (Its components add up to the dot product of this vector and
701
    \a vector.)
702
703
    \sa crossProduct(), operator/=(), operator*()
704
*/
705
706
/*!
707
    \fn QVector3D &QVector3D::operator/=(float divisor)
708
709
    Divides this vector's coordinates by the given \a divisor, and returns a
710
    reference to this vector. The \a divisor must not be either zero or NaN.
711
712
    \sa operator*=(), operator/()
713
*/
714
715
/*!
716
    \fn QVector3D &QVector3D::operator/=(QVector3D vector)
717
    \since 5.5
718
719
    Divides each component of this vector by the corresponding component in \a
720
    vector and returns a reference to this vector.
721
722
    The \a vector must have no component that is either zero or NaN.
723
724
    \sa operator*=(), operator/()
725
*/
726
727
/*!
728
    \fn float QVector3D::dotProduct(QVector3D v1, QVector3D v2)
729
730
    Returns the dot product of \a v1 and \a v2.
731
*/
732
733
/*!
734
    \fn QVector3D QVector3D::crossProduct(QVector3D v1, QVector3D v2)
735
736
    Returns the cross-product of vectors \a v1 and \a v2, which is normal to the
737
    plane spanned by \a v1 and \a v2. It will be zero if the two vectors are
738
    parallel.
739
740
    \sa normal()
741
*/
742
743
/*!
744
    \fn QVector3D QVector3D::normal(QVector3D v1, QVector3D v2)
745
746
    Returns the unit normal vector of a plane spanned by vectors \a v1 and \a
747
    v2, which must not be parallel to one another.
748
749
    Use crossProduct() to compute the cross-product of \a v1 and \a v2 if you
750
    do not need the result to be normalized to a unit vector.
751
752
    \sa crossProduct(), distanceToPlane()
753
*/
754
755
/*!
756
    \fn QVector3D QVector3D::normal(QVector3D v1, QVector3D v2, QVector3D v3)
757
758
    Returns the unit normal vector of a plane spanned by vectors \a v2 - \a v1
759
    and \a v3 - \a v1, which must not be parallel to one another.
760
761
    Use crossProduct() to compute the cross-product of \a v2 - \a v1 and
762
    \a v3 - \a v1 if you do not need the result to be normalized to a
763
    unit vector.
764
765
    \sa crossProduct(), distanceToPlane()
766
*/
767
768
/*!
769
    \since 5.5
770
771
    Returns the window coordinates of this vector initially in object/model
772
    coordinates using the model view matrix \a modelView, the projection matrix
773
    \a projection and the viewport dimensions \a viewport.
774
775
    When transforming from clip to normalized space, a division by the w
776
    component on the vector components takes place. To prevent dividing by 0 if
777
    w equals to 0, it is set to 1.
778
779
    \note the returned y coordinates are in OpenGL orientation. OpenGL expects
780
    the bottom to be 0 whereas for Qt top is 0.
781
782
    \sa unproject()
783
 */
784
QVector3D QVector3D::project(const QMatrix4x4 &modelView, const QMatrix4x4 &projection, const QRect &viewport) const
785
0
{
786
0
    QVector4D tmp(*this, 1.0f);
787
0
    tmp = projection * modelView * tmp;
788
0
    if (qFuzzyIsNull(tmp.w()))
789
0
        tmp.setW(1.0f);
790
0
    tmp /= tmp.w();
791
792
0
    tmp = tmp * 0.5f + QVector4D(0.5f, 0.5f, 0.5f, 0.5f);
793
0
    tmp.setX(tmp.x() * viewport.width() + viewport.x());
794
0
    tmp.setY(tmp.y() * viewport.height() + viewport.y());
795
796
0
    return tmp.toVector3D();
797
0
}
798
799
/*!
800
    \since 5.5
801
802
    Returns the object/model coordinates of this vector initially in window
803
    coordinates using the model view matrix \a modelView, the projection matrix
804
    \a projection and the viewport dimensions \a viewport.
805
806
    When transforming from clip to normalized space, a division by the w
807
    component of the vector components takes place. To prevent dividing by 0 if
808
    w equals to 0, it is set to 1.
809
810
    \note y coordinates in \a viewport should use OpenGL orientation. OpenGL
811
    expects the bottom to be 0 whereas for Qt top is 0.
812
813
    \sa project()
814
 */
815
QVector3D QVector3D::unproject(const QMatrix4x4 &modelView, const QMatrix4x4 &projection, const QRect &viewport) const
816
0
{
817
0
    QMatrix4x4 inverse = QMatrix4x4( projection * modelView ).inverted();
818
819
0
    QVector4D tmp(*this, 1.0f);
820
0
    tmp.setX((tmp.x() - float(viewport.x())) / float(viewport.width()));
821
0
    tmp.setY((tmp.y() - float(viewport.y())) / float(viewport.height()));
822
0
    tmp = tmp * 2.0f - QVector4D(1.0f, 1.0f, 1.0f, 1.0f);
823
824
0
    QVector4D obj = inverse * tmp;
825
0
    if (qFuzzyIsNull(obj.w()))
826
0
        obj.setW(1.0f);
827
0
    obj /= obj.w();
828
0
    return obj.toVector3D();
829
0
}
830
831
/*!
832
    \fn float QVector3D::distanceToPoint(QVector3D point) const
833
834
    \since 5.1
835
836
    Returns the distance from this vertex to a point defined by
837
    the vertex \a point.
838
839
    \sa distanceToPlane(), distanceToLine()
840
*/
841
842
/*!
843
    \fn float QVector3D::distanceToPlane(QVector3D plane, QVector3D normal) const
844
845
    Returns the distance from this vertex to a plane defined by
846
    the vertex \a plane and a \a normal unit vector. The \a normal
847
    parameter is assumed to have been normalized to a unit vector.
848
849
    The return value will be negative if the vertex is below the plane,
850
    or zero if it is on the plane.
851
852
    \sa normal(), distanceToLine()
853
*/
854
855
/*!
856
    \fn float QVector3D::distanceToPlane(QVector3D plane1, QVector3D plane2, QVector3D plane3) const
857
858
    Returns the distance from this vertex to a plane defined by
859
    the vertices \a plane1, \a plane2 and \a plane3.
860
861
    The return value will be negative if the vertex is below the plane,
862
    or zero if it is on the plane.
863
864
    The two vectors that define the plane are \a plane2 - \a plane1
865
    and \a plane3 - \a plane1.
866
867
    \sa normal(), distanceToLine()
868
*/
869
870
/*!
871
    \fn float QVector3D::distanceToLine(QVector3D point, QVector3D direction) const
872
873
    Returns the distance that this vertex is from a line defined
874
    by \a point and the unit vector \a direction.
875
876
    If \a direction is a null vector, then it does not define a line.
877
    In that case, the distance from \a point to this vertex is returned.
878
879
    \sa distanceToPlane()
880
*/
881
882
/*!
883
    \fn bool QVector3D::operator==(QVector3D v1, QVector3D v2)
884
885
    Returns \c true if \a v1 is equal to \a v2; otherwise returns \c false.
886
    This operator uses an exact floating-point comparison.
887
*/
888
889
/*!
890
    \fn bool QVector3D::operator!=(QVector3D v1, QVector3D v2)
891
892
    Returns \c true if \a v1 is not equal to \a v2; otherwise returns \c false.
893
    This operator uses an exact floating-point comparison.
894
*/
895
896
/*! //! friend
897
    \fn const QVector3D QVector3D::operator+(QVector3D v1, QVector3D v2)
898
899
    Returns a QVector3D object that is the sum of the given vectors, \a v1
900
    and \a v2; each component is added separately.
901
902
    \sa QVector3D::operator+=()
903
*/
904
905
/*! //! friend
906
    \fn const QVector3D QVector3D::operator-(QVector3D v1, QVector3D v2)
907
908
    Returns a QVector3D object that is formed by subtracting \a v2 from \a v1;
909
    each component is subtracted separately.
910
911
    \sa QVector3D::operator-=()
912
*/
913
914
/*! //! friend
915
    \fn const QVector3D QVector3D::operator*(float factor, QVector3D vector)
916
917
    Returns a copy of the given \a vector, multiplied by the given finite \a factor.
918
919
    \sa QVector3D::operator*=()
920
*/
921
922
/*! //! friend
923
    \fn const QVector3D QVector3D::operator*(QVector3D vector, float factor)
924
925
    Returns a copy of the given \a vector, multiplied by the given finite \a factor.
926
927
    \sa QVector3D::operator*=()
928
*/
929
930
/*! //! friend
931
    \fn const QVector3D QVector3D::operator*(QVector3D v1, QVector3D v2)
932
933
    Returns the QVector3D object formed by multiplying each component of \a v1
934
    by the corresponding component of \a v2.
935
936
    \note This is not the same as the crossProduct() of \a v1 and \a v2.
937
    (Its components add up to the dot product of \a v1 and \a v2.)
938
939
    \sa QVector3D::crossProduct()
940
*/
941
942
/*! //! friend
943
    \fn const QVector3D QVector3D::operator-(QVector3D vector)
944
    \overload
945
946
    Returns a QVector3D object that is formed by changing the sign of each
947
    component of the given \a vector.
948
949
    Equivalent to \c {QVector3D(0,0,0) - vector}.
950
*/
951
952
/*! //! friend
953
    \fn const QVector3D QVector3D::operator/(QVector3D vector, float divisor)
954
955
    Returns the QVector3D object formed by dividing each component of the given
956
    \a vector by the given \a divisor.
957
958
    The \a divisor must not be either zero or NaN.
959
960
    \sa QVector3D::operator/=()
961
*/
962
963
/*! //! friend
964
    \fn const QVector3D QVector3D::operator/(QVector3D vector, QVector3D divisor)
965
    \since 5.5
966
967
    Returns the QVector3D object formed by dividing each component of the given
968
    \a vector by the corresponding component of the given \a divisor.
969
970
    The \a divisor must have no component that is either zero or NaN.
971
972
    \sa QVector3D::operator/=()
973
*/
974
975
/*! //! friend
976
    \fn bool QVector3D::qFuzzyCompare(QVector3D v1, QVector3D v2)
977
978
    Returns \c true if \a v1 and \a v2 are equal, allowing for a small
979
    fuzziness factor for floating-point comparisons; false otherwise.
980
*/
981
bool qFuzzyCompare(QVector3D v1, QVector3D v2) noexcept
982
0
{
983
0
    return QtPrivate::fuzzyCompare(v1.v[0], v2.v[0])
984
0
        && QtPrivate::fuzzyCompare(v1.v[1], v2.v[1])
985
0
        && QtPrivate::fuzzyCompare(v1.v[2], v2.v[2]);
986
0
}
987
988
#ifndef QT_NO_VECTOR2D
989
990
/*!
991
    \fn QVector2D QVector3D::toVector2D() const
992
993
    Returns the 2D vector form of this 3D vector, dropping the z coordinate.
994
995
    \sa toVector4D(), toPoint()
996
*/
997
998
#endif
999
1000
#ifndef QT_NO_VECTOR4D
1001
1002
/*!
1003
    \fn QVector4D QVector3D::toVector4D() const
1004
1005
    Returns the 4D form of this 3D vector, with the w coordinate set to zero.
1006
1007
    \sa toVector2D(), toPoint()
1008
*/
1009
1010
#endif
1011
1012
/*!
1013
    \fn QPoint QVector3D::toPoint() const
1014
1015
    Returns the QPoint form of this 3D vector. The z coordinate is dropped. The
1016
    x and y coordinates are rounded to nearest integers.
1017
1018
    \sa toPointF(), toVector2D()
1019
*/
1020
1021
/*!
1022
    \fn QPointF QVector3D::toPointF() const
1023
1024
    Returns the QPointF form of this 3D vector. The z coordinate
1025
    is dropped.
1026
1027
    \sa toPoint(), toVector2D()
1028
*/
1029
1030
/*!
1031
    Returns the 3D vector as a QVariant.
1032
*/
1033
QVector3D::operator QVariant() const
1034
0
{
1035
0
    return QVariant::fromValue(*this);
1036
0
}
1037
1038
/*!
1039
    \fn float QVector3D::length() const
1040
1041
    Returns the length of the vector from the origin.
1042
1043
    \sa lengthSquared(), normalized()
1044
*/
1045
1046
/*!
1047
    \fn float QVector3D::lengthSquared() const
1048
1049
    Returns the squared length of the vector from the origin.
1050
    This is equivalent to the dot product of the vector with itself.
1051
1052
    \sa length(), dotProduct()
1053
*/
1054
1055
#ifndef QT_NO_DEBUG_STREAM
1056
1057
QDebug operator<<(QDebug dbg, QVector3D vector)
1058
0
{
1059
0
    QDebugStateSaver saver(dbg);
1060
0
    dbg.nospace() << "QVector3D("
1061
0
        << vector.x() << ", " << vector.y() << ", " << vector.z() << ')';
1062
0
    return dbg;
1063
0
}
1064
1065
#endif
1066
1067
#ifndef QT_NO_DATASTREAM
1068
1069
/*!
1070
    \fn QDataStream &operator<<(QDataStream &stream, QVector3D vector)
1071
    \relates QVector3D
1072
1073
    Writes the given \a vector to the given \a stream and returns a
1074
    reference to the stream.
1075
1076
    \sa {Serializing Qt Data Types}
1077
*/
1078
1079
QDataStream &operator<<(QDataStream &stream, QVector3D vector)
1080
0
{
1081
0
    stream << vector.x() << vector.y() << vector.z();
1082
0
    return stream;
1083
0
}
1084
1085
/*!
1086
    \fn QDataStream &operator>>(QDataStream &stream, QVector3D &vector)
1087
    \relates QVector3D
1088
1089
    Reads a 3D vector from the given \a stream into the given \a vector
1090
    and returns a reference to the stream.
1091
1092
    \sa {Serializing Qt Data Types}
1093
*/
1094
1095
QDataStream &operator>>(QDataStream &stream, QVector3D &vector)
1096
0
{
1097
0
    float x, y, z;
1098
0
    stream >> x;
1099
0
    stream >> y;
1100
0
    stream >> z;
1101
0
    vector.setX(x);
1102
0
    vector.setY(y);
1103
0
    vector.setZ(z);
1104
0
    return stream;
1105
0
}
1106
1107
#endif // QT_NO_DATASTREAM
1108
1109
#endif // QT_NO_VECTOR3D
1110
1111
1112
1113
#ifndef QT_NO_VECTOR4D
1114
1115
/*!
1116
    \class QVector4D
1117
    \brief The QVector4D class represents a vector or vertex in 4D space.
1118
    \since 4.6
1119
    \ingroup painting-3D
1120
    \inmodule QtGui
1121
1122
    Vectors are one of the main building blocks of 4D affine representations of
1123
    3D space. They consist of four finite floating-point coordinates,
1124
    traditionally called x, y, z and w.
1125
1126
    The QVector4D class can also be used to represent vertices in 4D space.
1127
    We therefore do not need to provide a separate vertex class.
1128
1129
    \sa QQuaternion, QVector2D, QVector3D
1130
*/
1131
1132
/*!
1133
    \fn QVector4D::QVector4D()
1134
1135
    Constructs a null vector, i.e. with coordinates (0, 0, 0, 0).
1136
*/
1137
1138
/*!
1139
    \fn QVector4D::QVector4D(Qt::Initialization)
1140
    \since 5.5
1141
    \internal
1142
1143
    Constructs a vector without initializing the contents.
1144
*/
1145
1146
/*!
1147
    \fn QVector4D::QVector4D(float xpos, float ypos, float zpos, float wpos)
1148
1149
    Constructs a vector with coordinates (\a xpos, \a ypos, \a zpos, \a wpos).
1150
    All parameters must be finite.
1151
*/
1152
1153
/*!
1154
    \fn QVector4D::QVector4D(QPoint point)
1155
1156
    Constructs a vector with x and y coordinates from a 2D \a point, and
1157
    z and w coordinates of 0.
1158
*/
1159
1160
/*!
1161
    \fn QVector4D::QVector4D(QPointF point)
1162
1163
    Constructs a vector with x and y coordinates from a 2D \a point, and
1164
    z and w coordinates of 0.
1165
*/
1166
1167
#ifndef QT_NO_VECTOR2D
1168
1169
/*!
1170
    \fn QVector4D::QVector4D(QVector2D vector)
1171
1172
    Constructs a 4D vector from the specified 2D \a vector. The z
1173
    and w coordinates are set to zero.
1174
1175
    \sa toVector2D()
1176
*/
1177
1178
/*!
1179
    \fn QVector4D::QVector4D(QVector2D vector, float zpos, float wpos)
1180
1181
    Constructs a 4D vector from the specified 2D \a vector. The z
1182
    and w coordinates are set to \a zpos and \a wpos respectively,
1183
    each of which must be finite.
1184
1185
    \sa toVector2D()
1186
*/
1187
1188
#endif
1189
1190
#ifndef QT_NO_VECTOR3D
1191
1192
/*!
1193
    \fn QVector4D::QVector4D(QVector3D vector)
1194
1195
    Constructs a 4D vector from the specified 3D \a vector. The w
1196
    coordinate is set to zero.
1197
1198
    \sa toVector3D()
1199
*/
1200
1201
/*!
1202
    \fn QVector4D::QVector4D(QVector3D vector, float wpos)
1203
1204
    Constructs a 4D vector from the specified 3D \a vector. The w
1205
    coordinate is set to \a wpos, which must be finite.
1206
1207
    \sa toVector3D()
1208
*/
1209
1210
#endif
1211
1212
/*!
1213
    \fn bool QVector4D::isNull() const
1214
1215
    Returns \c true if the x, y, z, and w coordinates are set to 0.0,
1216
    otherwise returns \c false.
1217
*/
1218
1219
/*!
1220
    \fn float QVector4D::x() const
1221
1222
    Returns the x coordinate of this point.
1223
1224
    \sa setX(), y(), z(), w()
1225
*/
1226
1227
/*!
1228
    \fn float QVector4D::y() const
1229
1230
    Returns the y coordinate of this point.
1231
1232
    \sa setY(), x(), z(), w()
1233
*/
1234
1235
/*!
1236
    \fn float QVector4D::z() const
1237
1238
    Returns the z coordinate of this point.
1239
1240
    \sa setZ(), x(), y(), w()
1241
*/
1242
1243
/*!
1244
    \fn float QVector4D::w() const
1245
1246
    Returns the w coordinate of this point.
1247
1248
    \sa setW(), x(), y(), z()
1249
*/
1250
1251
/*!
1252
    \fn void QVector4D::setX(float x)
1253
1254
    Sets the x coordinate of this point to the given finite \a x coordinate.
1255
1256
    \sa x(), setY(), setZ(), setW()
1257
*/
1258
1259
/*!
1260
    \fn void QVector4D::setY(float y)
1261
1262
    Sets the y coordinate of this point to the given finite \a y coordinate.
1263
1264
    \sa y(), setX(), setZ(), setW()
1265
*/
1266
1267
/*!
1268
    \fn void QVector4D::setZ(float z)
1269
1270
    Sets the z coordinate of this point to the given finite \a z coordinate.
1271
1272
    \sa z(), setX(), setY(), setW()
1273
*/
1274
1275
/*!
1276
    \fn void QVector4D::setW(float w)
1277
1278
    Sets the w coordinate of this point to the given finite \a w coordinate.
1279
1280
    \sa w(), setX(), setY(), setZ()
1281
*/
1282
1283
/*! \fn float &QVector4D::operator[](int i)
1284
    \since 5.2
1285
1286
    Returns the component of the vector at index position \a i
1287
    as a modifiable reference.
1288
1289
    \a i must be a valid index position in the vector (i.e., 0 <= \a i
1290
    < 4).
1291
*/
1292
1293
/*! \fn float QVector4D::operator[](int i) const
1294
    \since 5.2
1295
1296
    Returns the component of the vector at index position \a i.
1297
1298
    \a i must be a valid index position in the vector (i.e., 0 <= \a i
1299
    < 4).
1300
*/
1301
1302
/*!
1303
    \fn float QVector4D::length() const
1304
1305
    Returns the length of the vector from the origin.
1306
1307
    \sa lengthSquared(), normalized()
1308
*/
1309
1310
/*!
1311
    \fn float QVector4D::lengthSquared() const
1312
1313
    Returns the squared length of the vector from the origin.
1314
    This is equivalent to the dot product of the vector with itself.
1315
1316
    \sa length(), dotProduct()
1317
*/
1318
1319
/*!
1320
    \fn QVector4D QVector4D::normalized() const
1321
1322
    Returns the normalized unit vector form of this vector.
1323
1324
    If this vector is null, then a null vector is returned. If the length
1325
    of the vector is very close to 1, then the vector will be returned as-is.
1326
    Otherwise the normalized form of the vector of length 1 will be returned.
1327
1328
    \sa length(), normalize()
1329
*/
1330
1331
/*!
1332
    \fn void QVector4D::normalize()
1333
1334
    Normalizes the current vector in place. Nothing happens if this
1335
    vector is a null vector or the length of the vector is very close to 1.
1336
1337
    \sa length(), normalized()
1338
*/
1339
1340
1341
/*!
1342
    \fn QVector4D &QVector4D::operator+=(QVector4D vector)
1343
1344
    Adds the given \a vector to this vector and returns a reference to
1345
    this vector.
1346
1347
    \sa operator-=()
1348
*/
1349
1350
/*!
1351
    \fn QVector4D &QVector4D::operator-=(QVector4D vector)
1352
1353
    Subtracts the given \a vector from this vector and returns a reference to
1354
    this vector.
1355
1356
    \sa operator+=()
1357
*/
1358
1359
/*!
1360
    \fn QVector4D &QVector4D::operator*=(float factor)
1361
1362
    Multiplies this vector's coordinates by the given finite \a factor, and
1363
    returns a reference to this vector.
1364
1365
    \sa operator/=(), operator*()
1366
*/
1367
1368
/*!
1369
    \fn QVector4D &QVector4D::operator*=(QVector4D vector)
1370
1371
    Multiplies each component of this vector by the corresponding component of
1372
    \a vector and returns a reference to this vector.
1373
1374
    \sa operator/=(), operator*()
1375
*/
1376
1377
/*!
1378
    \fn QVector4D &QVector4D::operator/=(float divisor)
1379
1380
    Divides this vector's coordinates by the given \a divisor, and returns a
1381
    reference to this vector. The \a divisor must not be either zero or NaN.
1382
1383
    \sa operator*=()
1384
*/
1385
1386
/*!
1387
    \fn QVector4D &QVector4D::operator/=(QVector4D vector)
1388
    \since 5.5
1389
1390
    Divides each component of this vector by the corresponding component of \a
1391
    vector and returns a reference to this vector.
1392
1393
    The \a vector must have no component that is either zero or NaN.
1394
1395
    \sa operator*=(), operator/()
1396
*/
1397
1398
/*!
1399
    \fn float QVector4D::dotProduct(QVector4D v1, QVector4D v2)
1400
1401
    Returns the dot product of \a v1 and \a v2.
1402
*/
1403
1404
/*!
1405
    \fn bool QVector4D::operator==(QVector4D v1, QVector4D v2)
1406
1407
    Returns \c true if \a v1 is equal to \a v2; otherwise returns \c false.
1408
    This operator uses an exact floating-point comparison.
1409
*/
1410
1411
/*!
1412
    \fn bool QVector4D::operator!=(QVector4D v1, QVector4D v2)
1413
1414
    Returns \c true if \a v1 is not equal to \a v2; otherwise returns \c false.
1415
    This operator uses an exact floating-point comparison.
1416
*/
1417
1418
/*! //! friend
1419
    \fn const QVector4D QVector4D::operator+(QVector4D v1, QVector4D v2)
1420
1421
    Returns a QVector4D object that is the sum of the given vectors, \a v1
1422
    and \a v2; each component is added separately.
1423
1424
    \sa QVector4D::operator+=()
1425
*/
1426
1427
/*! //! friend
1428
    \fn const QVector4D QVector4D::operator-(QVector4D v1, QVector4D v2)
1429
1430
    Returns a QVector4D object that is formed by subtracting \a v2 from \a v1;
1431
    each component is subtracted separately.
1432
1433
    \sa QVector4D::operator-=()
1434
*/
1435
1436
/*! //! friend
1437
    \fn const QVector4D QVector4D::operator*(float factor, QVector4D vector)
1438
1439
    Returns a copy of the given \a vector,  multiplied by the given \a factor.
1440
1441
    \sa QVector4D::operator*=()
1442
*/
1443
1444
/*! //! friend
1445
    \fn const QVector4D QVector4D::operator*(QVector4D vector, float factor)
1446
1447
    Returns a copy of the given \a vector,  multiplied by the given \a factor.
1448
1449
    \sa QVector4D::operator*=()
1450
*/
1451
1452
/*! //! friend
1453
    \fn const QVector4D QVector4D::operator*(QVector4D v1, QVector4D v2)
1454
1455
    Returns the QVector4D object formed by multiplying each component of \a v1
1456
    by the corresponding component of \a v2.
1457
1458
    \note This is not a cross product of \a v1 and \a v2 in any sense.
1459
    (Its components add up to the dot product of \a v1 and \a v2.)
1460
1461
    \sa QVector4D::operator*=()
1462
*/
1463
1464
/*! //! friend
1465
    \fn const QVector4D QVector4D::operator-(QVector4D vector)
1466
    \overload
1467
1468
    Returns a QVector4D object that is formed by changing the sign of
1469
    all three components of the given \a vector.
1470
1471
    Equivalent to \c {QVector4D(0,0,0,0) - vector}.
1472
*/
1473
1474
/*! //! friend
1475
    \fn const QVector4D QVector4D::operator/(QVector4D vector, float divisor)
1476
1477
    Returns the QVector4D object formed by dividing each component of the given
1478
    \a vector by the given \a divisor.
1479
1480
    The \a divisor must not be either zero or NaN.
1481
1482
    \sa QVector4D::operator/=()
1483
*/
1484
1485
/*! //! friend
1486
    \fn const QVector4D QVector4D::operator/(QVector4D vector, QVector4D divisor)
1487
    \since 5.5
1488
1489
    Returns the QVector4D object formed by dividing each component of the given
1490
    \a vector by the corresponding component of the given \a divisor.
1491
1492
    The \a divisor must have no component that is either zero or NaN.
1493
1494
    \sa QVector4D::operator/=()
1495
*/
1496
1497
/*! //! friend
1498
    \fn bool QVector4D::qFuzzyCompare(QVector4D v1, QVector4D v2)
1499
1500
    Returns \c true if \a v1 and \a v2 are equal, allowing for a small
1501
    fuzziness factor for floating-point comparisons; false otherwise.
1502
*/
1503
bool qFuzzyCompare(QVector4D v1, QVector4D v2) noexcept
1504
0
{
1505
0
    return QtPrivate::fuzzyCompare(v1.v[0], v2.v[0])
1506
0
        && QtPrivate::fuzzyCompare(v1.v[1], v2.v[1])
1507
0
        && QtPrivate::fuzzyCompare(v1.v[2], v2.v[2])
1508
0
        && QtPrivate::fuzzyCompare(v1.v[3], v2.v[3]);
1509
0
}
1510
1511
#ifndef QT_NO_VECTOR2D
1512
1513
/*!
1514
    \fn QVector2D QVector4D::toVector2D() const
1515
1516
    Returns the 2D vector form of this 4D vector, dropping the z and w coordinates.
1517
1518
    \sa toVector2DAffine(), toVector3D(), toPoint()
1519
*/
1520
1521
/*!
1522
    \fn QVector2D QVector4D::toVector2DAffine() const
1523
1524
    Returns the 2D vector form of this 4D vector, dividing the x and y
1525
    coordinates by the w coordinate and dropping the z coordinate.
1526
    Returns a null vector if w is zero.
1527
1528
    \sa toVector2D(), toVector3DAffine(), toPoint()
1529
*/
1530
1531
#endif
1532
1533
#ifndef QT_NO_VECTOR3D
1534
1535
/*!
1536
    \fn QVector3D QVector4D::toVector3D() const
1537
1538
    Returns the 3D vector form of this 4D vector, dropping the w coordinate.
1539
1540
    \sa toVector3DAffine(), toVector2D(), toPoint()
1541
*/
1542
1543
/*!
1544
    \fn QVector3D QVector4D::toVector3DAffine() const
1545
1546
    Returns the 3D vector form of this 4D vector, dividing the x, y, and
1547
    z coordinates by the w coordinate. Returns a null vector if w is zero.
1548
1549
    \sa toVector3D(), toVector2DAffine(), toPoint()
1550
*/
1551
1552
#endif
1553
1554
/*!
1555
    \fn QPoint QVector4D::toPoint() const
1556
1557
    Returns the QPoint form of this 4D vector. The z and w coordinates are
1558
    dropped. The x and y coordinates are rounded to nearest integers.
1559
1560
    \sa toPointF(), toVector2D()
1561
*/
1562
1563
/*!
1564
    \fn QPointF QVector4D::toPointF() const
1565
1566
    Returns the QPointF form of this 4D vector. The z and w coordinates
1567
    are dropped.
1568
1569
    \sa toPoint(), toVector2D()
1570
*/
1571
1572
/*!
1573
    Returns the 4D vector as a QVariant.
1574
*/
1575
QVector4D::operator QVariant() const
1576
0
{
1577
0
    return QVariant::fromValue(*this);
1578
0
}
1579
1580
#ifndef QT_NO_DEBUG_STREAM
1581
1582
QDebug operator<<(QDebug dbg, QVector4D vector)
1583
0
{
1584
0
    QDebugStateSaver saver(dbg);
1585
0
    dbg.nospace() << "QVector4D("
1586
0
        << vector.x() << ", " << vector.y() << ", "
1587
0
        << vector.z() << ", " << vector.w() << ')';
1588
0
    return dbg;
1589
0
}
1590
1591
#endif
1592
1593
#ifndef QT_NO_DATASTREAM
1594
1595
/*!
1596
    \fn QDataStream &operator<<(QDataStream &stream, QVector4D vector)
1597
    \relates QVector4D
1598
1599
    Writes the given \a vector to the given \a stream and returns a
1600
    reference to the stream.
1601
1602
    \sa {Serializing Qt Data Types}
1603
*/
1604
1605
QDataStream &operator<<(QDataStream &stream, QVector4D vector)
1606
0
{
1607
0
    stream << vector.x() << vector.y()
1608
0
           << vector.z() << vector.w();
1609
0
    return stream;
1610
0
}
1611
1612
/*!
1613
    \fn QDataStream &operator>>(QDataStream &stream, QVector4D &vector)
1614
    \relates QVector4D
1615
1616
    Reads a 4D vector from the given \a stream into the given \a vector
1617
    and returns a reference to the stream.
1618
1619
    \sa {Serializing Qt Data Types}
1620
*/
1621
1622
QDataStream &operator>>(QDataStream &stream, QVector4D &vector)
1623
0
{
1624
0
    float x, y, z, w;
1625
0
    stream >> x;
1626
0
    stream >> y;
1627
0
    stream >> z;
1628
0
    stream >> w;
1629
0
    vector.setX(x);
1630
0
    vector.setY(y);
1631
0
    vector.setZ(z);
1632
0
    vector.setW(w);
1633
0
    return stream;
1634
0
}
1635
1636
#endif // QT_NO_DATASTREAM
1637
1638
#endif // QT_NO_VECTOR4D
1639
1640
QT_END_NAMESPACE