qvector4d.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  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 QVECTOR4D_H
  40. #define QVECTOR4D_H
  41. #include <QtCore/qpoint.h>
  42. #include <QtCore/qmetatype.h>
  43. QT_BEGIN_NAMESPACE
  44. class QMatrix4x4;
  45. class QVector2D;
  46. class QVector3D;
  47. #ifndef QT_NO_VECTOR4D
  48. class Q_GUI_EXPORT QVector4D
  49. {
  50. public:
  51. Q_DECL_CONSTEXPR QVector4D();
  52. explicit QVector4D(Qt::Initialization) {}
  53. Q_DECL_CONSTEXPR QVector4D(float xpos, float ypos, float zpos, float wpos);
  54. Q_DECL_CONSTEXPR explicit QVector4D(const QPoint& point);
  55. Q_DECL_CONSTEXPR explicit QVector4D(const QPointF& point);
  56. #ifndef QT_NO_VECTOR2D
  57. QVector4D(const QVector2D& vector);
  58. QVector4D(const QVector2D& vector, float zpos, float wpos);
  59. #endif
  60. #ifndef QT_NO_VECTOR3D
  61. QVector4D(const QVector3D& vector);
  62. QVector4D(const QVector3D& vector, float wpos);
  63. #endif
  64. bool isNull() const;
  65. Q_DECL_CONSTEXPR float x() const;
  66. Q_DECL_CONSTEXPR float y() const;
  67. Q_DECL_CONSTEXPR float z() const;
  68. Q_DECL_CONSTEXPR float w() const;
  69. void setX(float x);
  70. void setY(float y);
  71. void setZ(float z);
  72. void setW(float w);
  73. float &operator[](int i);
  74. float operator[](int i) const;
  75. float length() const;
  76. float lengthSquared() const; //In Qt 6 convert to inline and constexpr
  77. QVector4D normalized() const Q_REQUIRED_RESULT;
  78. void normalize();
  79. QVector4D &operator+=(const QVector4D &vector);
  80. QVector4D &operator-=(const QVector4D &vector);
  81. QVector4D &operator*=(float factor);
  82. QVector4D &operator*=(const QVector4D &vector);
  83. QVector4D &operator/=(float divisor);
  84. inline QVector4D &operator/=(const QVector4D &vector);
  85. static float dotProduct(const QVector4D& v1, const QVector4D& v2); //In Qt 6 convert to inline and constexpr
  86. Q_DECL_CONSTEXPR friend inline bool operator==(const QVector4D &v1, const QVector4D &v2);
  87. Q_DECL_CONSTEXPR friend inline bool operator!=(const QVector4D &v1, const QVector4D &v2);
  88. Q_DECL_CONSTEXPR friend inline const QVector4D operator+(const QVector4D &v1, const QVector4D &v2);
  89. Q_DECL_CONSTEXPR friend inline const QVector4D operator-(const QVector4D &v1, const QVector4D &v2);
  90. Q_DECL_CONSTEXPR friend inline const QVector4D operator*(float factor, const QVector4D &vector);
  91. Q_DECL_CONSTEXPR friend inline const QVector4D operator*(const QVector4D &vector, float factor);
  92. Q_DECL_CONSTEXPR friend inline const QVector4D operator*(const QVector4D &v1, const QVector4D& v2);
  93. Q_DECL_CONSTEXPR friend inline const QVector4D operator-(const QVector4D &vector);
  94. Q_DECL_CONSTEXPR friend inline const QVector4D operator/(const QVector4D &vector, float divisor);
  95. Q_DECL_CONSTEXPR friend inline const QVector4D operator/(const QVector4D &vector, const QVector4D &divisor);
  96. Q_DECL_CONSTEXPR friend inline bool qFuzzyCompare(const QVector4D& v1, const QVector4D& v2);
  97. #ifndef QT_NO_VECTOR2D
  98. QVector2D toVector2D() const;
  99. QVector2D toVector2DAffine() const;
  100. #endif
  101. #ifndef QT_NO_VECTOR3D
  102. QVector3D toVector3D() const;
  103. QVector3D toVector3DAffine() const;
  104. #endif
  105. Q_DECL_CONSTEXPR QPoint toPoint() const;
  106. Q_DECL_CONSTEXPR QPointF toPointF() const;
  107. operator QVariant() const;
  108. private:
  109. float xp, yp, zp, wp;
  110. friend class QVector2D;
  111. friend class QVector3D;
  112. #ifndef QT_NO_MATRIX4X4
  113. friend QVector4D operator*(const QVector4D& vector, const QMatrix4x4& matrix);
  114. friend QVector4D operator*(const QMatrix4x4& matrix, const QVector4D& vector);
  115. #endif
  116. };
  117. Q_DECLARE_TYPEINFO(QVector4D, Q_PRIMITIVE_TYPE);
  118. Q_DECL_CONSTEXPR inline QVector4D::QVector4D() : xp(0.0f), yp(0.0f), zp(0.0f), wp(0.0f) {}
  119. Q_DECL_CONSTEXPR inline QVector4D::QVector4D(float xpos, float ypos, float zpos, float wpos) : xp(xpos), yp(ypos), zp(zpos), wp(wpos) {}
  120. Q_DECL_CONSTEXPR inline QVector4D::QVector4D(const QPoint& point) : xp(point.x()), yp(point.y()), zp(0.0f), wp(0.0f) {}
  121. Q_DECL_CONSTEXPR inline QVector4D::QVector4D(const QPointF& point) : xp(point.x()), yp(point.y()), zp(0.0f), wp(0.0f) {}
  122. inline bool QVector4D::isNull() const
  123. {
  124. return qIsNull(xp) && qIsNull(yp) && qIsNull(zp) && qIsNull(wp);
  125. }
  126. Q_DECL_CONSTEXPR inline float QVector4D::x() const { return xp; }
  127. Q_DECL_CONSTEXPR inline float QVector4D::y() const { return yp; }
  128. Q_DECL_CONSTEXPR inline float QVector4D::z() const { return zp; }
  129. Q_DECL_CONSTEXPR inline float QVector4D::w() const { return wp; }
  130. inline void QVector4D::setX(float aX) { xp = aX; }
  131. inline void QVector4D::setY(float aY) { yp = aY; }
  132. inline void QVector4D::setZ(float aZ) { zp = aZ; }
  133. inline void QVector4D::setW(float aW) { wp = aW; }
  134. inline float &QVector4D::operator[](int i)
  135. {
  136. Q_ASSERT(uint(i) < 4u);
  137. return *(&xp + i);
  138. }
  139. inline float QVector4D::operator[](int i) const
  140. {
  141. Q_ASSERT(uint(i) < 4u);
  142. return *(&xp + i);
  143. }
  144. inline QVector4D &QVector4D::operator+=(const QVector4D &vector)
  145. {
  146. xp += vector.xp;
  147. yp += vector.yp;
  148. zp += vector.zp;
  149. wp += vector.wp;
  150. return *this;
  151. }
  152. inline QVector4D &QVector4D::operator-=(const QVector4D &vector)
  153. {
  154. xp -= vector.xp;
  155. yp -= vector.yp;
  156. zp -= vector.zp;
  157. wp -= vector.wp;
  158. return *this;
  159. }
  160. inline QVector4D &QVector4D::operator*=(float factor)
  161. {
  162. xp *= factor;
  163. yp *= factor;
  164. zp *= factor;
  165. wp *= factor;
  166. return *this;
  167. }
  168. inline QVector4D &QVector4D::operator*=(const QVector4D &vector)
  169. {
  170. xp *= vector.xp;
  171. yp *= vector.yp;
  172. zp *= vector.zp;
  173. wp *= vector.wp;
  174. return *this;
  175. }
  176. inline QVector4D &QVector4D::operator/=(float divisor)
  177. {
  178. xp /= divisor;
  179. yp /= divisor;
  180. zp /= divisor;
  181. wp /= divisor;
  182. return *this;
  183. }
  184. inline QVector4D &QVector4D::operator/=(const QVector4D &vector)
  185. {
  186. xp /= vector.xp;
  187. yp /= vector.yp;
  188. zp /= vector.zp;
  189. wp /= vector.wp;
  190. return *this;
  191. }
  192. Q_DECL_CONSTEXPR inline bool operator==(const QVector4D &v1, const QVector4D &v2)
  193. {
  194. return v1.xp == v2.xp && v1.yp == v2.yp && v1.zp == v2.zp && v1.wp == v2.wp;
  195. }
  196. Q_DECL_CONSTEXPR inline bool operator!=(const QVector4D &v1, const QVector4D &v2)
  197. {
  198. return v1.xp != v2.xp || v1.yp != v2.yp || v1.zp != v2.zp || v1.wp != v2.wp;
  199. }
  200. Q_DECL_CONSTEXPR inline const QVector4D operator+(const QVector4D &v1, const QVector4D &v2)
  201. {
  202. return QVector4D(v1.xp + v2.xp, v1.yp + v2.yp, v1.zp + v2.zp, v1.wp + v2.wp);
  203. }
  204. Q_DECL_CONSTEXPR inline const QVector4D operator-(const QVector4D &v1, const QVector4D &v2)
  205. {
  206. return QVector4D(v1.xp - v2.xp, v1.yp - v2.yp, v1.zp - v2.zp, v1.wp - v2.wp);
  207. }
  208. Q_DECL_CONSTEXPR inline const QVector4D operator*(float factor, const QVector4D &vector)
  209. {
  210. return QVector4D(vector.xp * factor, vector.yp * factor, vector.zp * factor, vector.wp * factor);
  211. }
  212. Q_DECL_CONSTEXPR inline const QVector4D operator*(const QVector4D &vector, float factor)
  213. {
  214. return QVector4D(vector.xp * factor, vector.yp * factor, vector.zp * factor, vector.wp * factor);
  215. }
  216. Q_DECL_CONSTEXPR inline const QVector4D operator*(const QVector4D &v1, const QVector4D& v2)
  217. {
  218. return QVector4D(v1.xp * v2.xp, v1.yp * v2.yp, v1.zp * v2.zp, v1.wp * v2.wp);
  219. }
  220. Q_DECL_CONSTEXPR inline const QVector4D operator-(const QVector4D &vector)
  221. {
  222. return QVector4D(-vector.xp, -vector.yp, -vector.zp, -vector.wp);
  223. }
  224. Q_DECL_CONSTEXPR inline const QVector4D operator/(const QVector4D &vector, float divisor)
  225. {
  226. return QVector4D(vector.xp / divisor, vector.yp / divisor, vector.zp / divisor, vector.wp / divisor);
  227. }
  228. Q_DECL_CONSTEXPR inline const QVector4D operator/(const QVector4D &vector, const QVector4D &divisor)
  229. {
  230. return QVector4D(vector.xp / divisor.xp, vector.yp / divisor.yp, vector.zp / divisor.zp, vector.wp / divisor.wp);
  231. }
  232. Q_DECL_CONSTEXPR inline bool qFuzzyCompare(const QVector4D& v1, const QVector4D& v2)
  233. {
  234. return qFuzzyCompare(v1.xp, v2.xp) &&
  235. qFuzzyCompare(v1.yp, v2.yp) &&
  236. qFuzzyCompare(v1.zp, v2.zp) &&
  237. qFuzzyCompare(v1.wp, v2.wp);
  238. }
  239. Q_DECL_CONSTEXPR inline QPoint QVector4D::toPoint() const
  240. {
  241. return QPoint(qRound(xp), qRound(yp));
  242. }
  243. Q_DECL_CONSTEXPR inline QPointF QVector4D::toPointF() const
  244. {
  245. return QPointF(qreal(xp), qreal(yp));
  246. }
  247. #ifndef QT_NO_DEBUG_STREAM
  248. Q_GUI_EXPORT QDebug operator<<(QDebug dbg, const QVector4D &vector);
  249. #endif
  250. #ifndef QT_NO_DATASTREAM
  251. Q_GUI_EXPORT QDataStream &operator<<(QDataStream &, const QVector4D &);
  252. Q_GUI_EXPORT QDataStream &operator>>(QDataStream &, QVector4D &);
  253. #endif
  254. #endif
  255. QT_END_NAMESPACE
  256. #endif