qvector3d.h 10 KB

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