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
qssgutils.cpp
Go to the documentation of this file.
1// Copyright (C) 2008-2012 NVIDIA Corporation.
2// Copyright (C) 2019 The Qt Company Ltd.
3// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
4
5#include "qssgutils_p.h"
6
7#include <QtCore/QDir>
8
9#include <cmath>
10
12
14{
15 return ::sqrtf(v.x() * v.x() + v.y() * v.y());
16}
17
19{
20 return qIsFinite(v.x()) && qIsFinite(v.y()) && qIsFinite(v.z());
21}
22
24{
25 return sqrtf(v.x() * v.x() + v.y() * v.y() + v.z() * v.z());
26}
27
29{
30 return v.x() * v.x() + v.y() * v.y() + v.z() * v.z();
31}
32
33// This special normalize function normalizes a vector in place
34// and returns the magnnitude (needed for compatiblity)
36{
37 const float m = QSSGUtils::vec3::magnitude(v);
38 if (m > 0)
39 v /= m;
40 return m;
41}
42
44{
45 const QVector3D c0 = QVector3D(m(0, 0), m(1, 0), m(2, 0));
46 const QVector3D c1 = QVector3D(m(0, 1), m(1, 1), m(2, 1));
47 const QVector3D c2 = QVector3D(m(0, 2), m(1, 2), m(2, 2));
48 return c0 * v.x() + c1 * v.y() + c2 * v.z();
49}
50
52{
53 const float values[9] = { m(0, 0), m(0, 1), m(0, 2), m(1, 0), m(1, 1), m(1, 2), m(2, 0), m(2, 1), m(2, 2) };
54 return QMatrix3x3(values);
55}
56
58{
59 QVector4D c0 = m.column(0);
60 QVector4D c1 = m.column(1);
61 QVector4D c2 = m.column(2);
62 QVector4D c3 = m.column(3);
63
64 c0.normalize();
65 c1.normalize();
66 c2.normalize();
67 c3.normalize();
68
69 m.setColumn(0, c0);
70 m.setColumn(1, c1);
71 m.setColumn(2, c2);
72 m.setColumn(3, c3);
73}
74
76{
77 const QVector4D tmp = QSSGUtils::mat44::rotate(m, QVector4D(v.x(), v.y(), v.z(), 1.0f));
78 return QVector3D(tmp.x(), tmp.y(), tmp.z());
79}
80
82{
83 return m.column(0) * v.x() + m.column(1) * v.y() + m.column(2) * v.z();
84}
85
87{
88 const QVector4D tmp = QSSGUtils::mat44::transform(m, QVector4D(v.x(), v.y(), v.z(), 1.0f));
89 return QVector3D(tmp.x(), tmp.y(), tmp.z());
90}
91
93{
94 return m.column(0) * v.x() + m.column(1) * v.y() + m.column(2) * v.z() + m.column(3) * v.w();
95}
96
98{
99 return QVector3D(m(0, 3), m(1, 3), m(2, 3));
100}
101
103{
104 const float scaleX = m.column(0).length();
105 const float scaleY = m.column(1).length();
106 const float scaleZ = m.column(2).length();
107 return QVector3D(scaleX, scaleY, scaleZ);
108}
109
111{
112 return qIsFinite(q.x()) && qIsFinite(q.y()) && qIsFinite(q.z()) && qIsFinite(q.scalar());
113}
114
116{
117 return std::sqrt(q.x() * q.x() + q.y() * q.y() + q.z() * q.z() + q.scalar() * q.scalar());
118}
119
121{
122 const float unitTolerance = float(1e-2);
123 return isFinite(q) && qAbs(magnitude(q) - 1) < unitTolerance;
124}
125
127{
128 const float unitTolerance = float(1e-4);
129 return isFinite(q) && qAbs(magnitude(q) - 1) < unitTolerance;
130}
131
133{
134 const float vx = 2.0f * v.x();
135 const float vy = 2.0f * v.y();
136 const float vz = 2.0f * v.z();
137 const float w2 = q.scalar() * q.scalar() - 0.5f;
138 const float dot2 = (q.x() * vx + q.y() * vy + q.z() * vz);
139 return QVector3D((vx * w2 + (q.y() * vz - q.z() * vy) * q.scalar() + q.x() * dot2),
140 (vy * w2 + (q.z() * vx - q.x() * vz) * q.scalar() + q.y() * dot2),
141 (vz * w2 + (q.x() * vy - q.y() * vx) * q.scalar() + q.z() * dot2));
142}
143
145{
146 const float vx = 2.0f * v.x();
147 const float vy = 2.0f * v.y();
148 const float vz = 2.0f * v.z();
149 const float w2 = q.scalar() * q.scalar() - 0.5f;
150 const float dot2 = (q.x() * vx + q.y() * vy + q.z() * vz);
151 return QVector3D((vx * w2 - (q.y() * vz - q.z() * vy) * q.scalar() + q.x() * dot2),
152 (vy * w2 - (q.z() * vx - q.x() * vz) * q.scalar() + q.y() * dot2),
153 (vz * w2 - (q.x() * vy - q.y() * vx) * q.scalar() + q.z() * dot2));
154}
155
156const char *nonNull(const char *src)
157{
158 return src == nullptr ? "" : src;
159}
160
162{
163 const QVector3D rgb(color.redF(), color.greenF(), color.blueF());
164 const float C1 = 0.305306011f;
165 const QVector3D C2(0.682171111f, 0.682171111f, 0.682171111f);
166 const QVector3D C3(0.012522878f, 0.012522878f, 0.012522878f);
167 return QVector4D(rgb * (rgb * (rgb * C1 + C2) + C3), color.alphaF());
168}
169
171{
172 const QVector4D c = sRGBToLinear(color);
173 return QColor::fromRgbF(c.x(), c.y(), c.z(), c.w());
174}
175
177{
180
181 rotation = QQuaternion::fromRotationMatrix(QSSGUtils::mat44::getUpper3x3(m)).normalized();
184
185 const auto det = m.determinant();
186
187 return qFuzzyCompare(det, 1.0) && qFuzzyCompare(det, -1.0);
188}
189
The QColor class provides colors based on RGB, HSV or CMYK values.
Definition qcolor.h:31
static QColor fromRgbF(float r, float g, float b, float a=1.0)
Static convenience function that returns a QColor constructed from the RGB color values,...
Definition qcolor.cpp:2427
The QMatrix4x4 class represents a 4x4 transformation matrix in 3D space.
Definition qmatrix4x4.h:25
The QQuaternion class represents a quaternion consisting of a vector and scalar.
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
float length() const noexcept
Returns the length of the vector from the origin.
Definition qvectornd.h:690
constexpr float y() const noexcept
Returns the y coordinate of this point.
Definition qvectornd.h:671
constexpr float x() const noexcept
Returns the x coordinate of this point.
Definition qvectornd.h:670
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
void normalize() noexcept
Normalizes the current vector in place.
Definition qvectornd.h:917
constexpr float y() const noexcept
Returns the y coordinate of this point.
Definition qvectornd.h:879
constexpr float z() const noexcept
Returns the z coordinate of this point.
Definition qvectornd.h:880
QColor Q_QUICK3DUTILS_EXPORT sRGBToLinearColor(const QColor &color)
QVector4D Q_QUICK3DUTILS_EXPORT sRGBToLinear(const QColor &color)
QVector3D Q_QUICK3DUTILS_EXPORT transform(const QMatrix3x3 &m, const QVector3D &v)
Definition qssgutils.cpp:43
QMatrix3x3 Q_QUICK3DUTILS_EXPORT getUpper3x3(const QMatrix4x4 &m)
Definition qssgutils.cpp:51
QVector3D Q_QUICK3DUTILS_EXPORT transform(const QMatrix4x4 &m, const QVector3D &v)
Definition qssgutils.cpp:86
void Q_QUICK3DUTILS_EXPORT normalize(QMatrix4x4 &m)
Definition qssgutils.cpp:57
QVector3D Q_QUICK3DUTILS_EXPORT rotate(const QMatrix4x4 &m, const QVector3D &v)
Definition qssgutils.cpp:75
QVector3D Q_QUICK3DUTILS_EXPORT getScale(const QMatrix4x4 &m)
QVector3D Q_QUICK3DUTILS_EXPORT getPosition(const QMatrix4x4 &m)
Definition qssgutils.cpp:97
bool Q_QUICK3DUTILS_EXPORT decompose(const QMatrix4x4 &m, QVector3D &position, QVector3D &scale, QQuaternion &rotation)
float Q_QUICK3DUTILS_EXPORT magnitude(const QQuaternion &q)
bool Q_QUICK3DUTILS_EXPORT isFinite(const QQuaternion &q)
QVector3D Q_QUICK3DUTILS_EXPORT rotated(const QQuaternion &q, const QVector3D &v)
QVector3D Q_QUICK3DUTILS_EXPORT inverseRotated(const QQuaternion &q, const QVector3D &v)
bool Q_QUICK3DUTILS_EXPORT isSane(const QQuaternion &q)
bool Q_QUICK3DUTILS_EXPORT isUnit(const QQuaternion &q)
float Q_QUICK3DUTILS_EXPORT magnitude(const QVector2D &v)
Definition qssgutils.cpp:13
float Q_QUICK3DUTILS_EXPORT magnitudeSquared(const QVector3D &v)
Definition qssgutils.cpp:28
float Q_QUICK3DUTILS_EXPORT magnitude(const QVector3D &v)
Definition qssgutils.cpp:23
bool Q_QUICK3DUTILS_EXPORT isFinite(const QVector3D &v)
Definition qssgutils.cpp:18
float Q_QUICK3DUTILS_EXPORT normalize(QVector3D &v)
Definition qssgutils.cpp:35
Combined button and popup list for selecting options.
#define rgb(r, g, b)
Definition qcolor.cpp:124
bool qIsFinite(qfloat16 f) noexcept
Definition qfloat16.h:285
bool qFuzzyCompare(qfloat16 p1, qfloat16 p2) noexcept
Definition qfloat16.h:333
QGenericMatrix< 3, 3, float > QMatrix3x3
constexpr T qAbs(const T &t)
Definition qnumeric.h:328
GLenum GLsizei GLsizei GLint * values
[15]
GLsizei const GLfloat * v
[13]
const GLfloat * m
GLenum src
GLuint color
[2]
GLuint GLenum GLenum transform
GLdouble GLdouble GLint GLint GLdouble GLdouble GLint GLint GLdouble GLdouble w2
const GLubyte * c
GLdouble GLdouble GLdouble GLdouble q
Definition qopenglext.h:259
GLenum GLenum GLenum GLenum GLenum scale
static qreal position(const QQuickItem *item, QQuickAnchors::Anchor anchorLine)
const char * nonNull(const char *src)
MyCustomStruct c2