qquaternion.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  1. /****************************************************************************
  2. **
  3. ** Copyright (C) 2016 The Qt Company Ltd.
  4. ** Contact: https://www.qt.io/licensing/
  5. **
  6. ** This file is part of the QtGui module of the Qt Toolkit.
  7. **
  8. ** $QT_BEGIN_LICENSE:LGPL$
  9. ** Commercial License Usage
  10. ** Licensees holding valid commercial Qt licenses may use this file in
  11. ** accordance with the commercial license agreement provided with the
  12. ** Software or, alternatively, in accordance with the terms contained in
  13. ** a written agreement between you and The Qt Company. For licensing terms
  14. ** and conditions see https://www.qt.io/terms-conditions. For further
  15. ** information use the contact form at https://www.qt.io/contact-us.
  16. **
  17. ** GNU Lesser General Public License Usage
  18. ** Alternatively, this file may be used under the terms of the GNU Lesser
  19. ** General Public License version 3 as published by the Free Software
  20. ** Foundation and appearing in the file LICENSE.LGPL3 included in the
  21. ** packaging of this file. Please review the following information to
  22. ** ensure the GNU Lesser General Public License version 3 requirements
  23. ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
  24. **
  25. ** GNU General Public License Usage
  26. ** Alternatively, this file may be used under the terms of the GNU
  27. ** General Public License version 2.0 or (at your option) the GNU General
  28. ** Public license version 3 or any later version approved by the KDE Free
  29. ** Qt Foundation. The licenses are as published by the Free Software
  30. ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
  31. ** included in the packaging of this file. Please review the following
  32. ** information to ensure the GNU General Public License requirements will
  33. ** be met: https://www.gnu.org/licenses/gpl-2.0.html and
  34. ** https://www.gnu.org/licenses/gpl-3.0.html.
  35. **
  36. ** $QT_END_LICENSE$
  37. **
  38. ****************************************************************************/
  39. #ifndef QQUATERNION_H
  40. #define QQUATERNION_H
  41. #include <QtGui/qgenericmatrix.h>
  42. #include <QtGui/qvector3d.h>
  43. #include <QtGui/qvector4d.h>
  44. QT_BEGIN_NAMESPACE
  45. #ifndef QT_NO_QUATERNION
  46. class QMatrix4x4;
  47. class QVariant;
  48. class Q_GUI_EXPORT QQuaternion
  49. {
  50. public:
  51. QQuaternion();
  52. explicit QQuaternion(Qt::Initialization) {}
  53. QQuaternion(float scalar, float xpos, float ypos, float zpos);
  54. #ifndef QT_NO_VECTOR3D
  55. QQuaternion(float scalar, const QVector3D& vector);
  56. #endif
  57. #ifndef QT_NO_VECTOR4D
  58. explicit QQuaternion(const QVector4D& vector);
  59. #endif
  60. bool isNull() const;
  61. bool isIdentity() const;
  62. #ifndef QT_NO_VECTOR3D
  63. QVector3D vector() const;
  64. void setVector(const QVector3D& vector);
  65. #endif
  66. void setVector(float x, float y, float z);
  67. float x() const;
  68. float y() const;
  69. float z() const;
  70. float scalar() const;
  71. void setX(float x);
  72. void setY(float y);
  73. void setZ(float z);
  74. void setScalar(float scalar);
  75. Q_DECL_CONSTEXPR static inline float dotProduct(const QQuaternion &q1, const QQuaternion &q2);
  76. float length() const;
  77. float lengthSquared() const;
  78. QQuaternion normalized() const Q_REQUIRED_RESULT;
  79. void normalize();
  80. inline QQuaternion inverted() const;
  81. QQuaternion conjugated() const Q_REQUIRED_RESULT;
  82. #if QT_DEPRECATED_SINCE(5, 5)
  83. QT_DEPRECATED QQuaternion conjugate() const Q_REQUIRED_RESULT;
  84. #endif
  85. QVector3D rotatedVector(const QVector3D& vector) const;
  86. QQuaternion &operator+=(const QQuaternion &quaternion);
  87. QQuaternion &operator-=(const QQuaternion &quaternion);
  88. QQuaternion &operator*=(float factor);
  89. QQuaternion &operator*=(const QQuaternion &quaternion);
  90. QQuaternion &operator/=(float divisor);
  91. friend inline bool operator==(const QQuaternion &q1, const QQuaternion &q2);
  92. friend inline bool operator!=(const QQuaternion &q1, const QQuaternion &q2);
  93. friend inline const QQuaternion operator+(const QQuaternion &q1, const QQuaternion &q2);
  94. friend inline const QQuaternion operator-(const QQuaternion &q1, const QQuaternion &q2);
  95. friend inline const QQuaternion operator*(float factor, const QQuaternion &quaternion);
  96. friend inline const QQuaternion operator*(const QQuaternion &quaternion, float factor);
  97. friend inline const QQuaternion operator*(const QQuaternion &q1, const QQuaternion& q2);
  98. friend inline const QQuaternion operator-(const QQuaternion &quaternion);
  99. friend inline const QQuaternion operator/(const QQuaternion &quaternion, float divisor);
  100. friend inline bool qFuzzyCompare(const QQuaternion& q1, const QQuaternion& q2);
  101. #ifndef QT_NO_VECTOR4D
  102. QVector4D toVector4D() const;
  103. #endif
  104. operator QVariant() const;
  105. #ifndef QT_NO_VECTOR3D
  106. inline void getAxisAndAngle(QVector3D *axis, float *angle) const;
  107. static QQuaternion fromAxisAndAngle(const QVector3D& axis, float angle);
  108. #endif
  109. void getAxisAndAngle(float *x, float *y, float *z, float *angle) const;
  110. static QQuaternion fromAxisAndAngle
  111. (float x, float y, float z, float angle);
  112. #ifndef QT_NO_VECTOR3D
  113. inline QVector3D toEulerAngles() const;
  114. static inline QQuaternion fromEulerAngles(const QVector3D &eulerAngles);
  115. #endif
  116. void getEulerAngles(float *pitch, float *yaw, float *roll) const;
  117. static QQuaternion fromEulerAngles(float pitch, float yaw, float roll);
  118. QMatrix3x3 toRotationMatrix() const;
  119. static QQuaternion fromRotationMatrix(const QMatrix3x3 &rot3x3);
  120. #ifndef QT_NO_VECTOR3D
  121. void getAxes(QVector3D *xAxis, QVector3D *yAxis, QVector3D *zAxis) const;
  122. static QQuaternion fromAxes(const QVector3D &xAxis, const QVector3D &yAxis, const QVector3D &zAxis);
  123. static QQuaternion fromDirection(const QVector3D &direction, const QVector3D &up);
  124. static QQuaternion rotationTo(const QVector3D &from, const QVector3D &to);
  125. #endif
  126. static QQuaternion slerp
  127. (const QQuaternion& q1, const QQuaternion& q2, float t);
  128. static QQuaternion nlerp
  129. (const QQuaternion& q1, const QQuaternion& q2, float t);
  130. private:
  131. float wp, xp, yp, zp;
  132. };
  133. Q_DECLARE_TYPEINFO(QQuaternion, Q_MOVABLE_TYPE);
  134. inline QQuaternion::QQuaternion() : wp(1.0f), xp(0.0f), yp(0.0f), zp(0.0f) {}
  135. inline QQuaternion::QQuaternion(float aScalar, float xpos, float ypos, float zpos) : wp(aScalar), xp(xpos), yp(ypos), zp(zpos) {}
  136. inline bool QQuaternion::isNull() const
  137. {
  138. return wp == 0.0f && xp == 0.0f && yp == 0.0f && zp == 0.0f;
  139. }
  140. inline bool QQuaternion::isIdentity() const
  141. {
  142. return wp == 1.0f && xp == 0.0f && yp == 0.0f && zp == 0.0f;
  143. }
  144. inline float QQuaternion::x() const { return xp; }
  145. inline float QQuaternion::y() const { return yp; }
  146. inline float QQuaternion::z() const { return zp; }
  147. inline float QQuaternion::scalar() const { return wp; }
  148. inline void QQuaternion::setX(float aX) { xp = aX; }
  149. inline void QQuaternion::setY(float aY) { yp = aY; }
  150. inline void QQuaternion::setZ(float aZ) { zp = aZ; }
  151. inline void QQuaternion::setScalar(float aScalar) { wp = aScalar; }
  152. Q_DECL_CONSTEXPR inline float QQuaternion::dotProduct(const QQuaternion &q1, const QQuaternion &q2)
  153. {
  154. return q1.wp * q2.wp + q1.xp * q2.xp + q1.yp * q2.yp + q1.zp * q2.zp;
  155. }
  156. inline QQuaternion QQuaternion::inverted() const
  157. {
  158. // Need some extra precision if the length is very small.
  159. double len = double(wp) * double(wp) +
  160. double(xp) * double(xp) +
  161. double(yp) * double(yp) +
  162. double(zp) * double(zp);
  163. if (!qFuzzyIsNull(len))
  164. return QQuaternion(wp / len, -xp / len, -yp / len, -zp / len);
  165. return QQuaternion(0.0f, 0.0f, 0.0f, 0.0f);
  166. }
  167. inline QQuaternion QQuaternion::conjugated() const
  168. {
  169. return QQuaternion(wp, -xp, -yp, -zp);
  170. }
  171. #if QT_DEPRECATED_SINCE(5, 5)
  172. inline QQuaternion QQuaternion::conjugate() const
  173. {
  174. return conjugated();
  175. }
  176. #endif
  177. inline QQuaternion &QQuaternion::operator+=(const QQuaternion &quaternion)
  178. {
  179. wp += quaternion.wp;
  180. xp += quaternion.xp;
  181. yp += quaternion.yp;
  182. zp += quaternion.zp;
  183. return *this;
  184. }
  185. inline QQuaternion &QQuaternion::operator-=(const QQuaternion &quaternion)
  186. {
  187. wp -= quaternion.wp;
  188. xp -= quaternion.xp;
  189. yp -= quaternion.yp;
  190. zp -= quaternion.zp;
  191. return *this;
  192. }
  193. inline QQuaternion &QQuaternion::operator*=(float factor)
  194. {
  195. wp *= factor;
  196. xp *= factor;
  197. yp *= factor;
  198. zp *= factor;
  199. return *this;
  200. }
  201. inline const QQuaternion operator*(const QQuaternion &q1, const QQuaternion& q2)
  202. {
  203. float yy = (q1.wp - q1.yp) * (q2.wp + q2.zp);
  204. float zz = (q1.wp + q1.yp) * (q2.wp - q2.zp);
  205. float ww = (q1.zp + q1.xp) * (q2.xp + q2.yp);
  206. float xx = ww + yy + zz;
  207. float qq = 0.5 * (xx + (q1.zp - q1.xp) * (q2.xp - q2.yp));
  208. float w = qq - ww + (q1.zp - q1.yp) * (q2.yp - q2.zp);
  209. float x = qq - xx + (q1.xp + q1.wp) * (q2.xp + q2.wp);
  210. float y = qq - yy + (q1.wp - q1.xp) * (q2.yp + q2.zp);
  211. float z = qq - zz + (q1.zp + q1.yp) * (q2.wp - q2.xp);
  212. return QQuaternion(w, x, y, z);
  213. }
  214. inline QQuaternion &QQuaternion::operator*=(const QQuaternion &quaternion)
  215. {
  216. *this = *this * quaternion;
  217. return *this;
  218. }
  219. inline QQuaternion &QQuaternion::operator/=(float divisor)
  220. {
  221. wp /= divisor;
  222. xp /= divisor;
  223. yp /= divisor;
  224. zp /= divisor;
  225. return *this;
  226. }
  227. inline bool operator==(const QQuaternion &q1, const QQuaternion &q2)
  228. {
  229. return q1.wp == q2.wp && q1.xp == q2.xp && q1.yp == q2.yp && q1.zp == q2.zp;
  230. }
  231. inline bool operator!=(const QQuaternion &q1, const QQuaternion &q2)
  232. {
  233. return !operator==(q1, q2);
  234. }
  235. inline const QQuaternion operator+(const QQuaternion &q1, const QQuaternion &q2)
  236. {
  237. return QQuaternion(q1.wp + q2.wp, q1.xp + q2.xp, q1.yp + q2.yp, q1.zp + q2.zp);
  238. }
  239. inline const QQuaternion operator-(const QQuaternion &q1, const QQuaternion &q2)
  240. {
  241. return QQuaternion(q1.wp - q2.wp, q1.xp - q2.xp, q1.yp - q2.yp, q1.zp - q2.zp);
  242. }
  243. inline const QQuaternion operator*(float factor, const QQuaternion &quaternion)
  244. {
  245. return QQuaternion(quaternion.wp * factor, quaternion.xp * factor, quaternion.yp * factor, quaternion.zp * factor);
  246. }
  247. inline const QQuaternion operator*(const QQuaternion &quaternion, float factor)
  248. {
  249. return QQuaternion(quaternion.wp * factor, quaternion.xp * factor, quaternion.yp * factor, quaternion.zp * factor);
  250. }
  251. inline const QQuaternion operator-(const QQuaternion &quaternion)
  252. {
  253. return QQuaternion(-quaternion.wp, -quaternion.xp, -quaternion.yp, -quaternion.zp);
  254. }
  255. inline const QQuaternion operator/(const QQuaternion &quaternion, float divisor)
  256. {
  257. return QQuaternion(quaternion.wp / divisor, quaternion.xp / divisor, quaternion.yp / divisor, quaternion.zp / divisor);
  258. }
  259. inline bool qFuzzyCompare(const QQuaternion& q1, const QQuaternion& q2)
  260. {
  261. return qFuzzyCompare(q1.wp, q2.wp) &&
  262. qFuzzyCompare(q1.xp, q2.xp) &&
  263. qFuzzyCompare(q1.yp, q2.yp) &&
  264. qFuzzyCompare(q1.zp, q2.zp);
  265. }
  266. #ifndef QT_NO_VECTOR3D
  267. inline QQuaternion::QQuaternion(float aScalar, const QVector3D& aVector)
  268. : wp(aScalar), xp(aVector.x()), yp(aVector.y()), zp(aVector.z()) {}
  269. inline void QQuaternion::setVector(const QVector3D& aVector)
  270. {
  271. xp = aVector.x();
  272. yp = aVector.y();
  273. zp = aVector.z();
  274. }
  275. inline QVector3D QQuaternion::vector() const
  276. {
  277. return QVector3D(xp, yp, zp);
  278. }
  279. inline QVector3D operator*(const QQuaternion &quaternion, const QVector3D &vec)
  280. {
  281. return quaternion.rotatedVector(vec);
  282. }
  283. inline void QQuaternion::getAxisAndAngle(QVector3D *axis, float *angle) const
  284. {
  285. float aX, aY, aZ;
  286. getAxisAndAngle(&aX, &aY, &aZ, angle);
  287. *axis = QVector3D(aX, aY, aZ);
  288. }
  289. inline QVector3D QQuaternion::toEulerAngles() const
  290. {
  291. float pitch, yaw, roll;
  292. getEulerAngles(&pitch, &yaw, &roll);
  293. return QVector3D(pitch, yaw, roll);
  294. }
  295. inline QQuaternion QQuaternion::fromEulerAngles(const QVector3D &eulerAngles)
  296. {
  297. return QQuaternion::fromEulerAngles(eulerAngles.x(), eulerAngles.y(), eulerAngles.z());
  298. }
  299. #endif
  300. inline void QQuaternion::setVector(float aX, float aY, float aZ)
  301. {
  302. xp = aX;
  303. yp = aY;
  304. zp = aZ;
  305. }
  306. #ifndef QT_NO_VECTOR4D
  307. inline QQuaternion::QQuaternion(const QVector4D& aVector)
  308. : wp(aVector.w()), xp(aVector.x()), yp(aVector.y()), zp(aVector.z()) {}
  309. inline QVector4D QQuaternion::toVector4D() const
  310. {
  311. return QVector4D(xp, yp, zp, wp);
  312. }
  313. #endif
  314. #ifndef QT_NO_DEBUG_STREAM
  315. Q_GUI_EXPORT QDebug operator<<(QDebug dbg, const QQuaternion &q);
  316. #endif
  317. #ifndef QT_NO_DATASTREAM
  318. Q_GUI_EXPORT QDataStream &operator<<(QDataStream &, const QQuaternion &);
  319. Q_GUI_EXPORT QDataStream &operator>>(QDataStream &, QQuaternion &);
  320. #endif
  321. #endif
  322. QT_END_NAMESPACE
  323. #endif