Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 10:08:15

0001 // Copyright (C) 2016 The Qt Company Ltd.
0002 // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
0003 
0004 #ifndef QQUATERNION_H
0005 #define QQUATERNION_H
0006 
0007 #include <QtGui/qtguiglobal.h>
0008 #include <QtGui/qgenericmatrix.h>
0009 #include <QtGui/qvector3d.h>
0010 #include <QtGui/qvector4d.h>
0011 
0012 QT_BEGIN_NAMESPACE
0013 
0014 
0015 #ifndef QT_NO_QUATERNION
0016 
0017 class QMatrix4x4;
0018 class QVariant;
0019 
0020 class QT6_ONLY(Q_GUI_EXPORT) QQuaternion
0021 {
0022 public:
0023     constexpr QQuaternion() noexcept;
0024     explicit QQuaternion(Qt::Initialization) noexcept {}
0025     constexpr QQuaternion(float scalar, float xpos, float ypos, float zpos) noexcept;
0026 #ifndef QT_NO_VECTOR3D
0027     constexpr QQuaternion(float scalar, const QVector3D &vector) noexcept;
0028 #endif
0029 #ifndef QT_NO_VECTOR4D
0030     constexpr explicit QQuaternion(const QVector4D &vector) noexcept;
0031 #endif
0032 
0033     constexpr bool isNull() const noexcept;
0034     constexpr bool isIdentity() const noexcept;
0035 
0036 #ifndef QT_NO_VECTOR3D
0037     constexpr QVector3D vector() const noexcept;
0038     constexpr void setVector(const QVector3D &vector) noexcept;
0039 #endif
0040     constexpr void setVector(float x, float y, float z) noexcept;
0041 
0042     constexpr float x() const noexcept;
0043     constexpr float y() const noexcept;
0044     constexpr float z() const noexcept;
0045     constexpr float scalar() const noexcept;
0046 
0047     constexpr void setX(float x) noexcept;
0048     constexpr void setY(float y) noexcept;
0049     constexpr void setZ(float z) noexcept;
0050     constexpr void setScalar(float scalar) noexcept;
0051 
0052     constexpr static float dotProduct(const QQuaternion &q1, const QQuaternion &q2) noexcept;
0053 
0054     // ### Qt 7: make the next four constexpr
0055     // (perhaps using std::hypot, constexpr in C++26, or constexpr qHypot)
0056     QT7_ONLY(Q_GUI_EXPORT) float length() const;
0057     QT7_ONLY(Q_GUI_EXPORT) float lengthSquared() const;
0058 
0059     [[nodiscard]] QT7_ONLY(Q_GUI_EXPORT) QQuaternion normalized() const;
0060     QT7_ONLY(Q_GUI_EXPORT)  void normalize();
0061 
0062     constexpr QQuaternion inverted() const noexcept;
0063 
0064     [[nodiscard]] constexpr QQuaternion conjugated() const noexcept;
0065 
0066     QT7_ONLY(Q_GUI_EXPORT) QVector3D rotatedVector(const QVector3D &vector) const;
0067 
0068     constexpr QQuaternion &operator+=(const QQuaternion &quaternion) noexcept;
0069     constexpr QQuaternion &operator-=(const QQuaternion &quaternion) noexcept;
0070     constexpr QQuaternion &operator*=(float factor) noexcept;
0071     constexpr QQuaternion &operator*=(const QQuaternion &quaternion) noexcept;
0072     constexpr QQuaternion &operator/=(float divisor);
0073 
0074 QT_WARNING_PUSH
0075 QT_WARNING_DISABLE_FLOAT_COMPARE
0076     friend constexpr bool operator==(const QQuaternion &q1, const QQuaternion &q2) noexcept
0077     {
0078         return q1.wp == q2.wp && q1.xp == q2.xp && q1.yp == q2.yp && q1.zp == q2.zp;
0079     }
0080     friend constexpr bool operator!=(const QQuaternion &q1, const QQuaternion &q2) noexcept
0081     {
0082         return !(q1 == q2);
0083     }
0084 QT_WARNING_POP
0085 
0086     friend constexpr QQuaternion operator+(const QQuaternion &q1, const QQuaternion &q2) noexcept;
0087     friend constexpr QQuaternion operator-(const QQuaternion &q1, const QQuaternion &q2) noexcept;
0088     friend constexpr QQuaternion operator*(float factor, const QQuaternion &quaternion) noexcept;
0089     friend constexpr QQuaternion operator*(const QQuaternion &quaternion, float factor) noexcept;
0090     friend constexpr QQuaternion operator*(const QQuaternion &q1, const QQuaternion &q2) noexcept;
0091     friend constexpr QQuaternion operator-(const QQuaternion &quaternion) noexcept;
0092     friend constexpr QQuaternion operator/(const QQuaternion &quaternion, float divisor);
0093 
0094     friend constexpr bool qFuzzyCompare(const QQuaternion &q1, const QQuaternion &q2) noexcept;
0095 
0096 #ifndef QT_NO_VECTOR4D
0097     constexpr QVector4D toVector4D() const noexcept;
0098 #endif
0099 
0100     QT7_ONLY(Q_GUI_EXPORT) operator QVariant() const;
0101 
0102 #ifndef QT_NO_VECTOR3D
0103     inline void getAxisAndAngle(QVector3D *axis, float *angle) const;
0104     QT7_ONLY(Q_GUI_EXPORT) static QQuaternion fromAxisAndAngle(const QVector3D &axis, float angle);
0105 #endif
0106     QT7_ONLY(Q_GUI_EXPORT) void getAxisAndAngle(float *x, float *y, float *z, float *angle) const;
0107     QT7_ONLY(Q_GUI_EXPORT) static QQuaternion fromAxisAndAngle(float x, float y, float z,
0108                                                                float angle);
0109 
0110 #ifndef QT_NO_VECTOR3D
0111     inline QVector3D toEulerAngles() const;
0112     QT7_ONLY(Q_GUI_EXPORT) static inline QQuaternion fromEulerAngles(const QVector3D &eulerAngles);
0113 #endif
0114     QT7_ONLY(Q_GUI_EXPORT) void getEulerAngles(float *pitch, float *yaw, float *roll) const;
0115     QT7_ONLY(Q_GUI_EXPORT) static QQuaternion fromEulerAngles(float pitch, float yaw, float roll);
0116 
0117     QT7_ONLY(Q_GUI_EXPORT) QMatrix3x3 toRotationMatrix() const;
0118     QT7_ONLY(Q_GUI_EXPORT) static QQuaternion fromRotationMatrix(const QMatrix3x3 &rot3x3);
0119 
0120 #ifndef QT_NO_VECTOR3D
0121     QT7_ONLY(Q_GUI_EXPORT) void getAxes(QVector3D *xAxis, QVector3D *yAxis, QVector3D *zAxis) const;
0122     QT7_ONLY(Q_GUI_EXPORT) static QQuaternion fromAxes(const QVector3D &xAxis,
0123                                                        const QVector3D &yAxis,
0124                                                        const QVector3D &zAxis);
0125 
0126     QT7_ONLY(Q_GUI_EXPORT) static QQuaternion fromDirection(const QVector3D &direction,
0127                                                             const QVector3D &up);
0128 
0129     QT7_ONLY(Q_GUI_EXPORT) static QQuaternion rotationTo(const QVector3D &from,
0130                                                          const QVector3D &to);
0131 #endif
0132 
0133     QT7_ONLY(Q_GUI_EXPORT) static QQuaternion slerp(const QQuaternion &q1, const QQuaternion &q2,
0134                                                     float t);
0135     QT7_ONLY(Q_GUI_EXPORT) static QQuaternion nlerp(const QQuaternion &q1, const QQuaternion &q2,
0136                                                     float t);
0137 
0138 private:
0139     float wp, xp, yp, zp;
0140 };
0141 
0142 Q_DECLARE_TYPEINFO(QQuaternion, Q_PRIMITIVE_TYPE);
0143 
0144 constexpr QQuaternion::QQuaternion() noexcept : wp(1.0f), xp(0.0f), yp(0.0f), zp(0.0f) {}
0145 
0146 constexpr QQuaternion::QQuaternion(float aScalar, float xpos, float ypos, float zpos) noexcept
0147     : wp(aScalar), xp(xpos), yp(ypos), zp(zpos) {}
0148 
0149 QT_WARNING_PUSH
0150 QT_WARNING_DISABLE_FLOAT_COMPARE
0151 
0152 constexpr bool QQuaternion::isNull() const noexcept
0153 {
0154     return wp == 0.0f && xp == 0.0f && yp == 0.0f && zp == 0.0f;
0155 }
0156 
0157 constexpr bool QQuaternion::isIdentity() const noexcept
0158 {
0159     return wp == 1.0f && xp == 0.0f && yp == 0.0f && zp == 0.0f;
0160 }
0161 QT_WARNING_POP
0162 
0163 constexpr float QQuaternion::x() const noexcept { return xp; }
0164 constexpr float QQuaternion::y() const noexcept { return yp; }
0165 constexpr float QQuaternion::z() const noexcept { return zp; }
0166 constexpr float QQuaternion::scalar() const noexcept { return wp; }
0167 
0168 constexpr void QQuaternion::setX(float aX) noexcept { xp = aX; }
0169 constexpr void QQuaternion::setY(float aY) noexcept { yp = aY; }
0170 constexpr void QQuaternion::setZ(float aZ) noexcept { zp = aZ; }
0171 constexpr void QQuaternion::setScalar(float aScalar) noexcept { wp = aScalar; }
0172 
0173 constexpr float QQuaternion::dotProduct(const QQuaternion &q1, const QQuaternion &q2) noexcept
0174 {
0175     return q1.wp * q2.wp + q1.xp * q2.xp + q1.yp * q2.yp + q1.zp * q2.zp;
0176 }
0177 
0178 constexpr QQuaternion QQuaternion::inverted() const noexcept
0179 {
0180     // Need some extra precision if the length is very small.
0181     double len = double(wp) * double(wp) +
0182                  double(xp) * double(xp) +
0183                  double(yp) * double(yp) +
0184                  double(zp) * double(zp);
0185     if (!qFuzzyIsNull(len))
0186         return QQuaternion(float(double(wp) / len), float(double(-xp) / len),
0187                            float(double(-yp) / len), float(double(-zp) / len));
0188     return QQuaternion(0.0f, 0.0f, 0.0f, 0.0f);
0189 }
0190 
0191 constexpr QQuaternion QQuaternion::conjugated() const noexcept
0192 {
0193     return QQuaternion(wp, -xp, -yp, -zp);
0194 }
0195 
0196 constexpr QQuaternion &QQuaternion::operator+=(const QQuaternion &quaternion) noexcept
0197 {
0198     wp += quaternion.wp;
0199     xp += quaternion.xp;
0200     yp += quaternion.yp;
0201     zp += quaternion.zp;
0202     return *this;
0203 }
0204 
0205 constexpr QQuaternion &QQuaternion::operator-=(const QQuaternion &quaternion) noexcept
0206 {
0207     wp -= quaternion.wp;
0208     xp -= quaternion.xp;
0209     yp -= quaternion.yp;
0210     zp -= quaternion.zp;
0211     return *this;
0212 }
0213 
0214 constexpr QQuaternion &QQuaternion::operator*=(float factor) noexcept
0215 {
0216     wp *= factor;
0217     xp *= factor;
0218     yp *= factor;
0219     zp *= factor;
0220     return *this;
0221 }
0222 
0223 constexpr QQuaternion operator*(const QQuaternion &q1, const QQuaternion &q2) noexcept
0224 {
0225     float yy = (q1.wp - q1.yp) * (q2.wp + q2.zp);
0226     float zz = (q1.wp + q1.yp) * (q2.wp - q2.zp);
0227     float ww = (q1.zp + q1.xp) * (q2.xp + q2.yp);
0228     float xx = ww + yy + zz;
0229     float qq = 0.5f * (xx + (q1.zp - q1.xp) * (q2.xp - q2.yp));
0230 
0231     float w = qq - ww + (q1.zp - q1.yp) * (q2.yp - q2.zp);
0232     float x = qq - xx + (q1.xp + q1.wp) * (q2.xp + q2.wp);
0233     float y = qq - yy + (q1.wp - q1.xp) * (q2.yp + q2.zp);
0234     float z = qq - zz + (q1.zp + q1.yp) * (q2.wp - q2.xp);
0235 
0236     return QQuaternion(w, x, y, z);
0237 }
0238 
0239 constexpr QQuaternion &QQuaternion::operator*=(const QQuaternion &quaternion) noexcept
0240 {
0241     *this = *this * quaternion;
0242     return *this;
0243 }
0244 
0245 constexpr QQuaternion &QQuaternion::operator/=(float divisor)
0246 {
0247     wp /= divisor;
0248     xp /= divisor;
0249     yp /= divisor;
0250     zp /= divisor;
0251     return *this;
0252 }
0253 
0254 constexpr QQuaternion operator+(const QQuaternion &q1, const QQuaternion &q2) noexcept
0255 {
0256     return QQuaternion(q1.wp + q2.wp, q1.xp + q2.xp, q1.yp + q2.yp, q1.zp + q2.zp);
0257 }
0258 
0259 constexpr QQuaternion operator-(const QQuaternion &q1, const QQuaternion &q2) noexcept
0260 {
0261     return QQuaternion(q1.wp - q2.wp, q1.xp - q2.xp, q1.yp - q2.yp, q1.zp - q2.zp);
0262 }
0263 
0264 constexpr QQuaternion operator*(float factor, const QQuaternion &quaternion) noexcept
0265 {
0266     return QQuaternion(quaternion.wp * factor, quaternion.xp * factor, quaternion.yp * factor, quaternion.zp * factor);
0267 }
0268 
0269 constexpr QQuaternion operator*(const QQuaternion &quaternion, float factor) noexcept
0270 {
0271     return QQuaternion(quaternion.wp * factor, quaternion.xp * factor, quaternion.yp * factor, quaternion.zp * factor);
0272 }
0273 
0274 constexpr QQuaternion operator-(const QQuaternion &quaternion) noexcept
0275 {
0276     return QQuaternion(-quaternion.wp, -quaternion.xp, -quaternion.yp, -quaternion.zp);
0277 }
0278 
0279 constexpr QQuaternion operator/(const QQuaternion &quaternion, float divisor)
0280 {
0281     return QQuaternion(quaternion.wp / divisor, quaternion.xp / divisor, quaternion.yp / divisor, quaternion.zp / divisor);
0282 }
0283 
0284 constexpr bool qFuzzyCompare(const QQuaternion &q1, const QQuaternion &q2) noexcept
0285 {
0286     return qFuzzyCompare(q1.wp, q2.wp) &&
0287            qFuzzyCompare(q1.xp, q2.xp) &&
0288            qFuzzyCompare(q1.yp, q2.yp) &&
0289            qFuzzyCompare(q1.zp, q2.zp);
0290 }
0291 
0292 #ifndef QT_NO_VECTOR3D
0293 
0294 constexpr QQuaternion::QQuaternion(float aScalar, const QVector3D &aVector) noexcept
0295     : wp(aScalar), xp(aVector.x()), yp(aVector.y()), zp(aVector.z()) {}
0296 
0297 constexpr void QQuaternion::setVector(const QVector3D &aVector) noexcept
0298 {
0299     xp = aVector.x();
0300     yp = aVector.y();
0301     zp = aVector.z();
0302 }
0303 
0304 constexpr QVector3D QQuaternion::vector() const noexcept
0305 {
0306     return QVector3D(xp, yp, zp);
0307 }
0308 
0309 inline QVector3D operator*(const QQuaternion &quaternion, const QVector3D &vec)
0310 {
0311     return quaternion.rotatedVector(vec);
0312 }
0313 
0314 inline void QQuaternion::getAxisAndAngle(QVector3D *axis, float *angle) const
0315 {
0316     float aX, aY, aZ;
0317     getAxisAndAngle(&aX, &aY, &aZ, angle);
0318     *axis = QVector3D(aX, aY, aZ);
0319 }
0320 
0321 inline QVector3D QQuaternion::toEulerAngles() const
0322 {
0323     float pitch, yaw, roll;
0324     getEulerAngles(&pitch, &yaw, &roll);
0325     return QVector3D(pitch, yaw, roll);
0326 }
0327 
0328 inline QQuaternion QQuaternion::fromEulerAngles(const QVector3D &eulerAngles)
0329 {
0330     return QQuaternion::fromEulerAngles(eulerAngles.x(), eulerAngles.y(), eulerAngles.z());
0331 }
0332 
0333 #endif
0334 
0335 constexpr void QQuaternion::setVector(float aX, float aY, float aZ) noexcept
0336 {
0337     xp = aX;
0338     yp = aY;
0339     zp = aZ;
0340 }
0341 
0342 #ifndef QT_NO_VECTOR4D
0343 
0344 constexpr QQuaternion::QQuaternion(const QVector4D &aVector) noexcept
0345     : wp(aVector.w()), xp(aVector.x()), yp(aVector.y()), zp(aVector.z()) {}
0346 
0347 constexpr QVector4D QQuaternion::toVector4D() const noexcept
0348 {
0349     return QVector4D(xp, yp, zp, wp);
0350 }
0351 
0352 #endif
0353 
0354 #ifndef QT_NO_DEBUG_STREAM
0355 Q_GUI_EXPORT QDebug operator<<(QDebug dbg, const QQuaternion &q);
0356 #endif
0357 
0358 #ifndef QT_NO_DATASTREAM
0359 Q_GUI_EXPORT QDataStream &operator<<(QDataStream &, const QQuaternion &);
0360 Q_GUI_EXPORT QDataStream &operator>>(QDataStream &, QQuaternion &);
0361 #endif
0362 
0363 #endif
0364 
0365 QT_END_NAMESPACE
0366 
0367 #endif