Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
qvectornd.cpp
Go to the documentation of this file.
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
12
13#ifndef QT_NO_VECTOR2D
14
66#ifndef QT_NO_VECTOR3D
67
77#endif
78
79#ifndef QT_NO_VECTOR4D
80
90#endif
91
377{
378 return qFuzzyCompare(v1.v[0], v2.v[0]) && qFuzzyCompare(v1.v[1], v2.v[1]);
379}
380
381#ifndef QT_NO_VECTOR3D
389#endif
390
391#ifndef QT_NO_VECTOR4D
399#endif
400
421QVector2D::operator QVariant() const
422{
423 return QVariant::fromValue(*this);
424}
425
426#ifndef QT_NO_DEBUG_STREAM
427
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
450{
451 stream << vector.x() << vector.y();
452 return stream;
453}
454
466{
467 float x, y;
468 stream >> x;
469 stream >> 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
536#ifndef QT_NO_VECTOR2D
537
555#endif
556
557#ifndef QT_NO_VECTOR4D
558
568#endif
569
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(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
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(obj.w()))
826 obj.setW(1.0f);
827 obj /= obj.w();
828 return obj.toVector3D();
829}
830
982{
983 return qFuzzyCompare(v1.v[0], v2.v[0]) &&
984 qFuzzyCompare(v1.v[1], v2.v[1]) &&
985 qFuzzyCompare(v1.v[2], v2.v[2]);
986}
987
988#ifndef QT_NO_VECTOR2D
989
998#endif
999
1000#ifndef QT_NO_VECTOR4D
1001
1010#endif
1011
1033QVector3D::operator QVariant() const
1034{
1035 return QVariant::fromValue(*this);
1036}
1037
1055#ifndef QT_NO_DEBUG_STREAM
1056
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
1080{
1081 stream << vector.x() << vector.y() << vector.z();
1082 return stream;
1083}
1084
1096{
1097 float x, y, z;
1098 stream >> x;
1099 stream >> y;
1100 stream >> 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
1168#ifndef QT_NO_VECTOR2D
1169
1189#endif
1190
1191#ifndef QT_NO_VECTOR3D
1192
1211#endif
1212
1505{
1506 return qFuzzyCompare(v1.v[0], v2.v[0]) &&
1507 qFuzzyCompare(v1.v[1], v2.v[1]) &&
1508 qFuzzyCompare(v1.v[2], v2.v[2]) &&
1509 qFuzzyCompare(v1.v[3], v2.v[3]);
1510}
1511
1512#ifndef QT_NO_VECTOR2D
1513
1532#endif
1533
1534#ifndef QT_NO_VECTOR3D
1535
1553#endif
1554
1576QVector4D::operator QVariant() const
1577{
1578 return QVariant::fromValue(*this);
1579}
1580
1581#ifndef QT_NO_DEBUG_STREAM
1582
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
1607{
1608 stream << vector.x() << vector.y()
1609 << vector.z() << vector.w();
1610 return stream;
1611}
1612
1624{
1625 float x, y, z, w;
1626 stream >> x;
1627 stream >> y;
1628 stream >> z;
1629 stream >> 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
\inmodule QtCore\reentrant
Definition qdatastream.h:46
\inmodule QtCore
\inmodule QtCore
The QMatrix4x4 class represents a 4x4 transformation matrix in 3D space.
Definition qmatrix4x4.h:25
QMatrix4x4 inverted(bool *invertible=nullptr) const
Returns the inverse of this matrix.
\inmodule QtCore\reentrant
Definition qrect.h:30
\inmodule QtCore
Definition qvariant.h:65
static auto fromValue(T &&value) noexcept(std::is_nothrow_copy_constructible_v< T > &&Private::CanUseInternalSpace< T >) -> std::enable_if_t< std::conjunction_v< std::is_copy_constructible< T >, std::is_destructible< T > >, QVariant >
Definition qvariant.h:536
The QVector2D class represents a vector or vertex in 2D space.
Definition qvectornd.h:31
The QVector3D class represents a vector or vertex in 3D space.
Definition qvectornd.h:171
friend class QVector4D
Definition qvectornd.h:303
Q_GUI_EXPORT QVector3D project(const QMatrix4x4 &modelView, const QMatrix4x4 &projection, const QRect &viewport) const
Q_GUI_EXPORT QVector3D unproject(const QMatrix4x4 &modelView, const QMatrix4x4 &projection, const QRect &viewport) const
The QVector4D class represents a vector or vertex in 4D space.
Definition qvectornd.h:330
constexpr float x() const noexcept
Returns the x coordinate of this point.
Definition qvectornd.h:878
constexpr void setY(float y) noexcept
Sets the y coordinate of this point to the given finite y coordinate.
Definition qvectornd.h:884
constexpr float w() const noexcept
Returns the w coordinate of this point.
Definition qvectornd.h:881
constexpr void setX(float x) noexcept
Sets the x coordinate of this point to the given finite x coordinate.
Definition qvectornd.h:883
constexpr float y() const noexcept
Returns the y coordinate of this point.
Definition qvectornd.h:879
constexpr QVector3D toVector3D() const noexcept
Returns the 3D vector form of this 4D vector, dropping the w coordinate.
Definition qvectornd.h:1011
constexpr void setW(float w) noexcept
Sets the w coordinate of this point to the given finite w coordinate.
Definition qvectornd.h:886
Combined button and popup list for selecting options.
EGLStreamKHR stream
bool qIsFinite(qfloat16 f) noexcept
Definition qfloat16.h:285
bool qFuzzyIsNull(qfloat16 f) noexcept
Definition qfloat16.h:349
GLint GLfloat GLfloat GLfloat v2
GLuint GLfloat GLfloat GLfloat GLfloat GLfloat z
GLint GLint GLint GLint GLint x
[0]
GLfloat GLfloat GLfloat w
[0]
GLint GLfloat GLfloat v1
GLint y
GLhandleARB obj
[2]
#define Q_ASSERT(cond)
Definition qrandom.cpp:47
QDataStream & operator>>(QDataStream &stream, QVector2D &vector)
QDebug operator<<(QDebug dbg, QVector2D vector)
bool qFuzzyCompare(QVector2D v1, QVector2D v2) noexcept
QList< int > vector
[14]
view viewport() -> scroll(dx, dy, deviceRect)