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
11QT_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*/
376bool qFuzzyCompare(QVector2D v1, QVector2D v2) noexcept
377{
378 return qFuzzyCompare(p1: v1.v[0], p2: v2.v[0]) && qFuzzyCompare(p1: v1.v[1], p2: v2.v[1]);
379}
380
381#ifndef QT_NO_VECTOR3D
382/*!
383 \fn QVector3D QVector2D::toVector3D() const
384
385 Returns the 3D form of this 2D vector, with the z coordinate set to zero.
386
387 \sa toVector4D(), toPoint()
388*/
389#endif
390
391#ifndef QT_NO_VECTOR4D
392/*!
393 \fn QVector4D QVector2D::toVector4D() const
394
395 Returns the 4D form of this 2D vector, with the z and w coordinates set to zero.
396
397 \sa toVector3D(), toPoint()
398*/
399#endif
400
401/*!
402 \fn QPoint QVector2D::toPoint() const
403
404 Returns the QPoint form of this 2D vector.
405 Each coordinate is rounded to the nearest integer.
406
407 \sa toPointF(), toVector3D()
408*/
409
410/*!
411 \fn QPointF QVector2D::toPointF() const
412
413 Returns the QPointF form of this 2D vector.
414
415 \sa toPoint(), toVector3D()
416*/
417
418/*!
419 Returns the 2D vector as a QVariant.
420*/
421QVector2D::operator QVariant() const
422{
423 return QVariant::fromValue(value: *this);
424}
425
426#ifndef QT_NO_DEBUG_STREAM
427
428QDebug operator<<(QDebug dbg, QVector2D vector)
429{
430 QDebugStateSaver saver(dbg);
431 dbg.nospace() << "QVector2D(" << vector.x() << ", " << vector.y() << ')';
432 return dbg;
433}
434
435#endif
436
437#ifndef QT_NO_DATASTREAM
438
439/*!
440 \fn QDataStream &operator<<(QDataStream &stream, QVector2D vector)
441 \relates QVector2D
442
443 Writes the given \a vector to the given \a stream and returns a
444 reference to the stream.
445
446 \sa {Serializing Qt Data Types}
447*/
448
449QDataStream &operator<<(QDataStream &stream, QVector2D vector)
450{
451 stream << vector.x() << vector.y();
452 return stream;
453}
454
455/*!
456 \fn QDataStream &operator>>(QDataStream &stream, QVector2D &vector)
457 \relates QVector2D
458
459 Reads a 2D vector from the given \a stream into the given \a vector
460 and returns a reference to the stream.
461
462 \sa {Serializing Qt Data Types}
463*/
464
465QDataStream &operator>>(QDataStream &stream, QVector2D &vector)
466{
467 float x, y;
468 stream >> x;
469 stream >> y;
470 Q_ASSERT(qIsFinite(x) && qIsFinite(y));
471 vector.setX(x);
472 vector.setY(y);
473 return stream;
474}
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 */
784QVector3D QVector3D::project(const QMatrix4x4 &modelView, const QMatrix4x4 &projection, const QRect &viewport) const
785{
786 QVector4D tmp(*this, 1.0f);
787 tmp = projection * modelView * tmp;
788 if (qFuzzyIsNull(f: tmp.w()))
789 tmp.setW(1.0f);
790 tmp /= tmp.w();
791
792 tmp = tmp * 0.5f + QVector4D(0.5f, 0.5f, 0.5f, 0.5f);
793 tmp.setX(tmp.x() * viewport.width() + viewport.x());
794 tmp.setY(tmp.y() * viewport.height() + viewport.y());
795
796 return tmp.toVector3D();
797}
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 */
815QVector3D QVector3D::unproject(const QMatrix4x4 &modelView, const QMatrix4x4 &projection, const QRect &viewport) const
816{
817 QMatrix4x4 inverse = QMatrix4x4( projection * modelView ).inverted();
818
819 QVector4D tmp(*this, 1.0f);
820 tmp.setX((tmp.x() - float(viewport.x())) / float(viewport.width()));
821 tmp.setY((tmp.y() - float(viewport.y())) / float(viewport.height()));
822 tmp = tmp * 2.0f - QVector4D(1.0f, 1.0f, 1.0f, 1.0f);
823
824 QVector4D obj = inverse * tmp;
825 if (qFuzzyIsNull(f: obj.w()))
826 obj.setW(1.0f);
827 obj /= obj.w();
828 return obj.toVector3D();
829}
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*/
981bool qFuzzyCompare(QVector3D v1, QVector3D v2) noexcept
982{
983 return qFuzzyCompare(p1: v1.v[0], p2: v2.v[0]) &&
984 qFuzzyCompare(p1: v1.v[1], p2: v2.v[1]) &&
985 qFuzzyCompare(p1: v1.v[2], p2: v2.v[2]);
986}
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*/
1033QVector3D::operator QVariant() const
1034{
1035 return QVariant::fromValue(value: *this);
1036}
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
1057QDebug operator<<(QDebug dbg, QVector3D vector)
1058{
1059 QDebugStateSaver saver(dbg);
1060 dbg.nospace() << "QVector3D("
1061 << vector.x() << ", " << vector.y() << ", " << vector.z() << ')';
1062 return dbg;
1063}
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
1079QDataStream &operator<<(QDataStream &stream, QVector3D vector)
1080{
1081 stream << vector.x() << vector.y() << vector.z();
1082 return stream;
1083}
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
1095QDataStream &operator>>(QDataStream &stream, QVector3D &vector)
1096{
1097 float x, y, z;
1098 stream >> x;
1099 stream >> y;
1100 stream >> z;
1101 Q_ASSERT(qIsFinite(x) && qIsFinite(y) && qIsFinite(z));
1102 vector.setX(x);
1103 vector.setY(y);
1104 vector.setZ(z);
1105 return stream;
1106}
1107
1108#endif // QT_NO_DATASTREAM
1109
1110#endif // QT_NO_VECTOR3D
1111
1112
1113
1114#ifndef QT_NO_VECTOR4D
1115
1116/*!
1117 \class QVector4D
1118 \brief The QVector4D class represents a vector or vertex in 4D space.
1119 \since 4.6
1120 \ingroup painting-3D
1121 \inmodule QtGui
1122
1123 Vectors are one of the main building blocks of 4D affine representations of
1124 3D space. They consist of four finite floating-point coordinates,
1125 traditionally called x, y, z and w.
1126
1127 The QVector4D class can also be used to represent vertices in 4D space.
1128 We therefore do not need to provide a separate vertex class.
1129
1130 \sa QQuaternion, QVector2D, QVector3D
1131*/
1132
1133/*!
1134 \fn QVector4D::QVector4D()
1135
1136 Constructs a null vector, i.e. with coordinates (0, 0, 0, 0).
1137*/
1138
1139/*!
1140 \fn QVector4D::QVector4D(Qt::Initialization)
1141 \since 5.5
1142 \internal
1143
1144 Constructs a vector without initializing the contents.
1145*/
1146
1147/*!
1148 \fn QVector4D::QVector4D(float xpos, float ypos, float zpos, float wpos)
1149
1150 Constructs a vector with coordinates (\a xpos, \a ypos, \a zpos, \a wpos).
1151 All parameters must be finite.
1152*/
1153
1154/*!
1155 \fn QVector4D::QVector4D(QPoint point)
1156
1157 Constructs a vector with x and y coordinates from a 2D \a point, and
1158 z and w coordinates of 0.
1159*/
1160
1161/*!
1162 \fn QVector4D::QVector4D(QPointF point)
1163
1164 Constructs a vector with x and y coordinates from a 2D \a point, and
1165 z and w coordinates of 0.
1166*/
1167
1168#ifndef QT_NO_VECTOR2D
1169
1170/*!
1171 \fn QVector4D::QVector4D(QVector2D vector)
1172
1173 Constructs a 4D vector from the specified 2D \a vector. The z
1174 and w coordinates are set to zero.
1175
1176 \sa toVector2D()
1177*/
1178
1179/*!
1180 \fn QVector4D::QVector4D(QVector2D vector, float zpos, float wpos)
1181
1182 Constructs a 4D vector from the specified 2D \a vector. The z
1183 and w coordinates are set to \a zpos and \a wpos respectively,
1184 each of which must be finite.
1185
1186 \sa toVector2D()
1187*/
1188
1189#endif
1190
1191#ifndef QT_NO_VECTOR3D
1192
1193/*!
1194 \fn QVector4D::QVector4D(QVector3D vector)
1195
1196 Constructs a 4D vector from the specified 3D \a vector. The w
1197 coordinate is set to zero.
1198
1199 \sa toVector3D()
1200*/
1201
1202/*!
1203 \fn QVector4D::QVector4D(QVector3D vector, float wpos)
1204
1205 Constructs a 4D vector from the specified 3D \a vector. The w
1206 coordinate is set to \a wpos, which must be finite.
1207
1208 \sa toVector3D()
1209*/
1210
1211#endif
1212
1213/*!
1214 \fn bool QVector4D::isNull() const
1215
1216 Returns \c true if the x, y, z, and w coordinates are set to 0.0,
1217 otherwise returns \c false.
1218*/
1219
1220/*!
1221 \fn float QVector4D::x() const
1222
1223 Returns the x coordinate of this point.
1224
1225 \sa setX(), y(), z(), w()
1226*/
1227
1228/*!
1229 \fn float QVector4D::y() const
1230
1231 Returns the y coordinate of this point.
1232
1233 \sa setY(), x(), z(), w()
1234*/
1235
1236/*!
1237 \fn float QVector4D::z() const
1238
1239 Returns the z coordinate of this point.
1240
1241 \sa setZ(), x(), y(), w()
1242*/
1243
1244/*!
1245 \fn float QVector4D::w() const
1246
1247 Returns the w coordinate of this point.
1248
1249 \sa setW(), x(), y(), z()
1250*/
1251
1252/*!
1253 \fn void QVector4D::setX(float x)
1254
1255 Sets the x coordinate of this point to the given finite \a x coordinate.
1256
1257 \sa x(), setY(), setZ(), setW()
1258*/
1259
1260/*!
1261 \fn void QVector4D::setY(float y)
1262
1263 Sets the y coordinate of this point to the given finite \a y coordinate.
1264
1265 \sa y(), setX(), setZ(), setW()
1266*/
1267
1268/*!
1269 \fn void QVector4D::setZ(float z)
1270
1271 Sets the z coordinate of this point to the given finite \a z coordinate.
1272
1273 \sa z(), setX(), setY(), setW()
1274*/
1275
1276/*!
1277 \fn void QVector4D::setW(float w)
1278
1279 Sets the w coordinate of this point to the given finite \a w coordinate.
1280
1281 \sa w(), setX(), setY(), setZ()
1282*/
1283
1284/*! \fn float &QVector4D::operator[](int i)
1285 \since 5.2
1286
1287 Returns the component of the vector at index position \a i
1288 as a modifiable reference.
1289
1290 \a i must be a valid index position in the vector (i.e., 0 <= \a i
1291 < 4).
1292*/
1293
1294/*! \fn float QVector4D::operator[](int i) const
1295 \since 5.2
1296
1297 Returns the component of the vector at index position \a i.
1298
1299 \a i must be a valid index position in the vector (i.e., 0 <= \a i
1300 < 4).
1301*/
1302
1303/*!
1304 \fn float QVector4D::length() const
1305
1306 Returns the length of the vector from the origin.
1307
1308 \sa lengthSquared(), normalized()
1309*/
1310
1311/*!
1312 \fn float QVector4D::lengthSquared() const
1313
1314 Returns the squared length of the vector from the origin.
1315 This is equivalent to the dot product of the vector with itself.
1316
1317 \sa length(), dotProduct()
1318*/
1319
1320/*!
1321 \fn QVector4D QVector4D::normalized() const
1322
1323 Returns the normalized unit vector form of this vector.
1324
1325 If this vector is null, then a null vector is returned. If the length
1326 of the vector is very close to 1, then the vector will be returned as-is.
1327 Otherwise the normalized form of the vector of length 1 will be returned.
1328
1329 \sa length(), normalize()
1330*/
1331
1332/*!
1333 \fn void QVector4D::normalize()
1334
1335 Normalizes the current vector in place. Nothing happens if this
1336 vector is a null vector or the length of the vector is very close to 1.
1337
1338 \sa length(), normalized()
1339*/
1340
1341
1342/*!
1343 \fn QVector4D &QVector4D::operator+=(QVector4D vector)
1344
1345 Adds the given \a vector to this vector and returns a reference to
1346 this vector.
1347
1348 \sa operator-=()
1349*/
1350
1351/*!
1352 \fn QVector4D &QVector4D::operator-=(QVector4D vector)
1353
1354 Subtracts the given \a vector from this vector and returns a reference to
1355 this vector.
1356
1357 \sa operator+=()
1358*/
1359
1360/*!
1361 \fn QVector4D &QVector4D::operator*=(float factor)
1362
1363 Multiplies this vector's coordinates by the given finite \a factor, and
1364 returns a reference to this vector.
1365
1366 \sa operator/=(), operator*()
1367*/
1368
1369/*!
1370 \fn QVector4D &QVector4D::operator*=(QVector4D vector)
1371
1372 Multiplies each component of this vector by the corresponding component of
1373 \a vector and returns a reference to this vector.
1374
1375 \sa operator/=(), operator*()
1376*/
1377
1378/*!
1379 \fn QVector4D &QVector4D::operator/=(float divisor)
1380
1381 Divides this vector's coordinates by the given \a divisor, and returns a
1382 reference to this vector. The \a divisor must not be either zero or NaN.
1383
1384 \sa operator*=()
1385*/
1386
1387/*!
1388 \fn QVector4D &QVector4D::operator/=(QVector4D vector)
1389 \since 5.5
1390
1391 Divides each component of this vector by the corresponding component of \a
1392 vector and returns a reference to this vector.
1393
1394 The \a vector must have no component that is either zero or NaN.
1395
1396 \sa operator*=(), operator/()
1397*/
1398
1399/*!
1400 \fn float QVector4D::dotProduct(QVector4D v1, QVector4D v2)
1401
1402 Returns the dot product of \a v1 and \a v2.
1403*/
1404
1405/*!
1406 \fn bool QVector4D::operator==(QVector4D v1, QVector4D v2)
1407
1408 Returns \c true if \a v1 is equal to \a v2; otherwise returns \c false.
1409 This operator uses an exact floating-point comparison.
1410*/
1411
1412/*!
1413 \fn bool QVector4D::operator!=(QVector4D v1, QVector4D v2)
1414
1415 Returns \c true if \a v1 is not equal to \a v2; otherwise returns \c false.
1416 This operator uses an exact floating-point comparison.
1417*/
1418
1419/*! //! friend
1420 \fn const QVector4D QVector4D::operator+(QVector4D v1, QVector4D v2)
1421
1422 Returns a QVector4D object that is the sum of the given vectors, \a v1
1423 and \a v2; each component is added separately.
1424
1425 \sa QVector4D::operator+=()
1426*/
1427
1428/*! //! friend
1429 \fn const QVector4D QVector4D::operator-(QVector4D v1, QVector4D v2)
1430
1431 Returns a QVector4D object that is formed by subtracting \a v2 from \a v1;
1432 each component is subtracted separately.
1433
1434 \sa QVector4D::operator-=()
1435*/
1436
1437/*! //! friend
1438 \fn const QVector4D QVector4D::operator*(float factor, QVector4D vector)
1439
1440 Returns a copy of the given \a vector, multiplied by the given \a factor.
1441
1442 \sa QVector4D::operator*=()
1443*/
1444
1445/*! //! friend
1446 \fn const QVector4D QVector4D::operator*(QVector4D vector, float factor)
1447
1448 Returns a copy of the given \a vector, multiplied by the given \a factor.
1449
1450 \sa QVector4D::operator*=()
1451*/
1452
1453/*! //! friend
1454 \fn const QVector4D QVector4D::operator*(QVector4D v1, QVector4D v2)
1455
1456 Returns the QVector4D object formed by multiplying each component of \a v1
1457 by the corresponding component of \a v2.
1458
1459 \note This is not a cross product of \a v1 and \a v2 in any sense.
1460 (Its components add up to the dot product of \a v1 and \a v2.)
1461
1462 \sa QVector4D::operator*=()
1463*/
1464
1465/*! //! friend
1466 \fn const QVector4D QVector4D::operator-(QVector4D vector)
1467 \overload
1468
1469 Returns a QVector4D object that is formed by changing the sign of
1470 all three components of the given \a vector.
1471
1472 Equivalent to \c {QVector4D(0,0,0,0) - vector}.
1473*/
1474
1475/*! //! friend
1476 \fn const QVector4D QVector4D::operator/(QVector4D vector, float divisor)
1477
1478 Returns the QVector4D object formed by dividing each component of the given
1479 \a vector by the given \a divisor.
1480
1481 The \a divisor must not be either zero or NaN.
1482
1483 \sa QVector4D::operator/=()
1484*/
1485
1486/*! //! friend
1487 \fn const QVector4D QVector4D::operator/(QVector4D vector, QVector4D divisor)
1488 \since 5.5
1489
1490 Returns the QVector4D object formed by dividing each component of the given
1491 \a vector by the corresponding component of the given \a divisor.
1492
1493 The \a divisor must have no component that is either zero or NaN.
1494
1495 \sa QVector4D::operator/=()
1496*/
1497
1498/*! //! friend
1499 \fn bool QVector4D::qFuzzyCompare(QVector4D v1, QVector4D v2)
1500
1501 Returns \c true if \a v1 and \a v2 are equal, allowing for a small
1502 fuzziness factor for floating-point comparisons; false otherwise.
1503*/
1504bool qFuzzyCompare(QVector4D v1, QVector4D v2) noexcept
1505{
1506 return qFuzzyCompare(p1: v1.v[0], p2: v2.v[0]) &&
1507 qFuzzyCompare(p1: v1.v[1], p2: v2.v[1]) &&
1508 qFuzzyCompare(p1: v1.v[2], p2: v2.v[2]) &&
1509 qFuzzyCompare(p1: v1.v[3], p2: v2.v[3]);
1510}
1511
1512#ifndef QT_NO_VECTOR2D
1513
1514/*!
1515 \fn QVector2D QVector4D::toVector2D() const
1516
1517 Returns the 2D vector form of this 4D vector, dropping the z and w coordinates.
1518
1519 \sa toVector2DAffine(), toVector3D(), toPoint()
1520*/
1521
1522/*!
1523 \fn QVector2D QVector4D::toVector2DAffine() const
1524
1525 Returns the 2D vector form of this 4D vector, dividing the x and y
1526 coordinates by the w coordinate and dropping the z coordinate.
1527 Returns a null vector if w is zero.
1528
1529 \sa toVector2D(), toVector3DAffine(), toPoint()
1530*/
1531
1532#endif
1533
1534#ifndef QT_NO_VECTOR3D
1535
1536/*!
1537 \fn QVector3D QVector4D::toVector3D() const
1538
1539 Returns the 3D vector form of this 4D vector, dropping the w coordinate.
1540
1541 \sa toVector3DAffine(), toVector2D(), toPoint()
1542*/
1543
1544/*!
1545 \fn QVector3D QVector4D::toVector3DAffine() const
1546
1547 Returns the 3D vector form of this 4D vector, dividing the x, y, and
1548 z coordinates by the w coordinate. Returns a null vector if w is zero.
1549
1550 \sa toVector3D(), toVector2DAffine(), toPoint()
1551*/
1552
1553#endif
1554
1555/*!
1556 \fn QPoint QVector4D::toPoint() const
1557
1558 Returns the QPoint form of this 4D vector. The z and w coordinates are
1559 dropped. The x and y coordinates are rounded to nearest integers.
1560
1561 \sa toPointF(), toVector2D()
1562*/
1563
1564/*!
1565 \fn QPointF QVector4D::toPointF() const
1566
1567 Returns the QPointF form of this 4D vector. The z and w coordinates
1568 are dropped.
1569
1570 \sa toPoint(), toVector2D()
1571*/
1572
1573/*!
1574 Returns the 4D vector as a QVariant.
1575*/
1576QVector4D::operator QVariant() const
1577{
1578 return QVariant::fromValue(value: *this);
1579}
1580
1581#ifndef QT_NO_DEBUG_STREAM
1582
1583QDebug operator<<(QDebug dbg, QVector4D vector)
1584{
1585 QDebugStateSaver saver(dbg);
1586 dbg.nospace() << "QVector4D("
1587 << vector.x() << ", " << vector.y() << ", "
1588 << vector.z() << ", " << vector.w() << ')';
1589 return dbg;
1590}
1591
1592#endif
1593
1594#ifndef QT_NO_DATASTREAM
1595
1596/*!
1597 \fn QDataStream &operator<<(QDataStream &stream, QVector4D vector)
1598 \relates QVector4D
1599
1600 Writes the given \a vector to the given \a stream and returns a
1601 reference to the stream.
1602
1603 \sa {Serializing Qt Data Types}
1604*/
1605
1606QDataStream &operator<<(QDataStream &stream, QVector4D vector)
1607{
1608 stream << vector.x() << vector.y()
1609 << vector.z() << vector.w();
1610 return stream;
1611}
1612
1613/*!
1614 \fn QDataStream &operator>>(QDataStream &stream, QVector4D &vector)
1615 \relates QVector4D
1616
1617 Reads a 4D vector from the given \a stream into the given \a vector
1618 and returns a reference to the stream.
1619
1620 \sa {Serializing Qt Data Types}
1621*/
1622
1623QDataStream &operator>>(QDataStream &stream, QVector4D &vector)
1624{
1625 float x, y, z, w;
1626 stream >> x;
1627 stream >> y;
1628 stream >> z;
1629 stream >> w;
1630 Q_ASSERT(qIsFinite(x) && qIsFinite(y) && qIsFinite(z) && qIsFinite(w));
1631 vector.setX(x);
1632 vector.setY(y);
1633 vector.setZ(z);
1634 vector.setW(w);
1635 return stream;
1636}
1637
1638#endif // QT_NO_DATASTREAM
1639
1640#endif // QT_NO_VECTOR4D
1641
1642QT_END_NAMESPACE
1643

source code of qtbase/src/gui/math3d/qvectornd.cpp