Coverage Report

Created: 2026-07-30 07:17

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