vector4.h 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. /**************************************************************************/
  2. /* vector4.h */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #ifndef VECTOR4_H
  31. #define VECTOR4_H
  32. #include "core/error/error_macros.h"
  33. #include "core/math/math_funcs.h"
  34. class String;
  35. struct _NO_DISCARD_ Vector4 {
  36. static const int AXIS_COUNT = 4;
  37. enum Axis {
  38. AXIS_X,
  39. AXIS_Y,
  40. AXIS_Z,
  41. AXIS_W,
  42. };
  43. union {
  44. struct {
  45. real_t x;
  46. real_t y;
  47. real_t z;
  48. real_t w;
  49. };
  50. real_t components[4] = { 0, 0, 0, 0 };
  51. };
  52. _FORCE_INLINE_ real_t &operator[](const int p_axis) {
  53. DEV_ASSERT((unsigned int)p_axis < 4);
  54. return components[p_axis];
  55. }
  56. _FORCE_INLINE_ const real_t &operator[](const int p_axis) const {
  57. DEV_ASSERT((unsigned int)p_axis < 4);
  58. return components[p_axis];
  59. }
  60. Vector4::Axis min_axis_index() const;
  61. Vector4::Axis max_axis_index() const;
  62. Vector4 min(const Vector4 &p_vector4) const {
  63. return Vector4(MIN(x, p_vector4.x), MIN(y, p_vector4.y), MIN(z, p_vector4.z), MIN(w, p_vector4.w));
  64. }
  65. Vector4 max(const Vector4 &p_vector4) const {
  66. return Vector4(MAX(x, p_vector4.x), MAX(y, p_vector4.y), MAX(z, p_vector4.z), MAX(w, p_vector4.w));
  67. }
  68. _FORCE_INLINE_ real_t length_squared() const;
  69. bool is_equal_approx(const Vector4 &p_vec4) const;
  70. bool is_zero_approx() const;
  71. bool is_finite() const;
  72. real_t length() const;
  73. void normalize();
  74. Vector4 normalized() const;
  75. bool is_normalized() const;
  76. real_t distance_to(const Vector4 &p_to) const;
  77. real_t distance_squared_to(const Vector4 &p_to) const;
  78. Vector4 direction_to(const Vector4 &p_to) const;
  79. Vector4 abs() const;
  80. Vector4 sign() const;
  81. Vector4 floor() const;
  82. Vector4 ceil() const;
  83. Vector4 round() const;
  84. Vector4 lerp(const Vector4 &p_to, const real_t p_weight) const;
  85. Vector4 cubic_interpolate(const Vector4 &p_b, const Vector4 &p_pre_a, const Vector4 &p_post_b, const real_t p_weight) const;
  86. Vector4 cubic_interpolate_in_time(const Vector4 &p_b, const Vector4 &p_pre_a, const Vector4 &p_post_b, const real_t p_weight, const real_t &p_b_t, const real_t &p_pre_a_t, const real_t &p_post_b_t) const;
  87. Vector4 posmod(const real_t p_mod) const;
  88. Vector4 posmodv(const Vector4 &p_modv) const;
  89. void snap(const Vector4 &p_step);
  90. Vector4 snapped(const Vector4 &p_step) const;
  91. Vector4 clamp(const Vector4 &p_min, const Vector4 &p_max) const;
  92. Vector4 inverse() const;
  93. _FORCE_INLINE_ real_t dot(const Vector4 &p_vec4) const;
  94. _FORCE_INLINE_ void operator+=(const Vector4 &p_vec4);
  95. _FORCE_INLINE_ void operator-=(const Vector4 &p_vec4);
  96. _FORCE_INLINE_ void operator*=(const Vector4 &p_vec4);
  97. _FORCE_INLINE_ void operator/=(const Vector4 &p_vec4);
  98. _FORCE_INLINE_ void operator*=(const real_t &s);
  99. _FORCE_INLINE_ void operator/=(const real_t &s);
  100. _FORCE_INLINE_ Vector4 operator+(const Vector4 &p_vec4) const;
  101. _FORCE_INLINE_ Vector4 operator-(const Vector4 &p_vec4) const;
  102. _FORCE_INLINE_ Vector4 operator*(const Vector4 &p_vec4) const;
  103. _FORCE_INLINE_ Vector4 operator/(const Vector4 &p_vec4) const;
  104. _FORCE_INLINE_ Vector4 operator-() const;
  105. _FORCE_INLINE_ Vector4 operator*(const real_t &s) const;
  106. _FORCE_INLINE_ Vector4 operator/(const real_t &s) const;
  107. _FORCE_INLINE_ bool operator==(const Vector4 &p_vec4) const;
  108. _FORCE_INLINE_ bool operator!=(const Vector4 &p_vec4) const;
  109. _FORCE_INLINE_ bool operator>(const Vector4 &p_vec4) const;
  110. _FORCE_INLINE_ bool operator<(const Vector4 &p_vec4) const;
  111. _FORCE_INLINE_ bool operator>=(const Vector4 &p_vec4) const;
  112. _FORCE_INLINE_ bool operator<=(const Vector4 &p_vec4) const;
  113. operator String() const;
  114. _FORCE_INLINE_ Vector4() {}
  115. _FORCE_INLINE_ Vector4(real_t p_x, real_t p_y, real_t p_z, real_t p_w) :
  116. x(p_x),
  117. y(p_y),
  118. z(p_z),
  119. w(p_w) {
  120. }
  121. Vector4(const Vector4 &p_vec4) :
  122. x(p_vec4.x),
  123. y(p_vec4.y),
  124. z(p_vec4.z),
  125. w(p_vec4.w) {
  126. }
  127. void operator=(const Vector4 &p_vec4) {
  128. x = p_vec4.x;
  129. y = p_vec4.y;
  130. z = p_vec4.z;
  131. w = p_vec4.w;
  132. }
  133. };
  134. real_t Vector4::dot(const Vector4 &p_vec4) const {
  135. return x * p_vec4.x + y * p_vec4.y + z * p_vec4.z + w * p_vec4.w;
  136. }
  137. real_t Vector4::length_squared() const {
  138. return dot(*this);
  139. }
  140. void Vector4::operator+=(const Vector4 &p_vec4) {
  141. x += p_vec4.x;
  142. y += p_vec4.y;
  143. z += p_vec4.z;
  144. w += p_vec4.w;
  145. }
  146. void Vector4::operator-=(const Vector4 &p_vec4) {
  147. x -= p_vec4.x;
  148. y -= p_vec4.y;
  149. z -= p_vec4.z;
  150. w -= p_vec4.w;
  151. }
  152. void Vector4::operator*=(const Vector4 &p_vec4) {
  153. x *= p_vec4.x;
  154. y *= p_vec4.y;
  155. z *= p_vec4.z;
  156. w *= p_vec4.w;
  157. }
  158. void Vector4::operator/=(const Vector4 &p_vec4) {
  159. x /= p_vec4.x;
  160. y /= p_vec4.y;
  161. z /= p_vec4.z;
  162. w /= p_vec4.w;
  163. }
  164. void Vector4::operator*=(const real_t &s) {
  165. x *= s;
  166. y *= s;
  167. z *= s;
  168. w *= s;
  169. }
  170. void Vector4::operator/=(const real_t &s) {
  171. *this *= 1.0f / s;
  172. }
  173. Vector4 Vector4::operator+(const Vector4 &p_vec4) const {
  174. return Vector4(x + p_vec4.x, y + p_vec4.y, z + p_vec4.z, w + p_vec4.w);
  175. }
  176. Vector4 Vector4::operator-(const Vector4 &p_vec4) const {
  177. return Vector4(x - p_vec4.x, y - p_vec4.y, z - p_vec4.z, w - p_vec4.w);
  178. }
  179. Vector4 Vector4::operator*(const Vector4 &p_vec4) const {
  180. return Vector4(x * p_vec4.x, y * p_vec4.y, z * p_vec4.z, w * p_vec4.w);
  181. }
  182. Vector4 Vector4::operator/(const Vector4 &p_vec4) const {
  183. return Vector4(x / p_vec4.x, y / p_vec4.y, z / p_vec4.z, w / p_vec4.w);
  184. }
  185. Vector4 Vector4::operator-() const {
  186. return Vector4(-x, -y, -z, -w);
  187. }
  188. Vector4 Vector4::operator*(const real_t &s) const {
  189. return Vector4(x * s, y * s, z * s, w * s);
  190. }
  191. Vector4 Vector4::operator/(const real_t &s) const {
  192. return *this * (1.0f / s);
  193. }
  194. bool Vector4::operator==(const Vector4 &p_vec4) const {
  195. return x == p_vec4.x && y == p_vec4.y && z == p_vec4.z && w == p_vec4.w;
  196. }
  197. bool Vector4::operator!=(const Vector4 &p_vec4) const {
  198. return x != p_vec4.x || y != p_vec4.y || z != p_vec4.z || w != p_vec4.w;
  199. }
  200. bool Vector4::operator<(const Vector4 &p_v) const {
  201. if (x == p_v.x) {
  202. if (y == p_v.y) {
  203. if (z == p_v.z) {
  204. return w < p_v.w;
  205. }
  206. return z < p_v.z;
  207. }
  208. return y < p_v.y;
  209. }
  210. return x < p_v.x;
  211. }
  212. bool Vector4::operator>(const Vector4 &p_v) const {
  213. if (x == p_v.x) {
  214. if (y == p_v.y) {
  215. if (z == p_v.z) {
  216. return w > p_v.w;
  217. }
  218. return z > p_v.z;
  219. }
  220. return y > p_v.y;
  221. }
  222. return x > p_v.x;
  223. }
  224. bool Vector4::operator<=(const Vector4 &p_v) const {
  225. if (x == p_v.x) {
  226. if (y == p_v.y) {
  227. if (z == p_v.z) {
  228. return w <= p_v.w;
  229. }
  230. return z < p_v.z;
  231. }
  232. return y < p_v.y;
  233. }
  234. return x < p_v.x;
  235. }
  236. bool Vector4::operator>=(const Vector4 &p_v) const {
  237. if (x == p_v.x) {
  238. if (y == p_v.y) {
  239. if (z == p_v.z) {
  240. return w >= p_v.w;
  241. }
  242. return z > p_v.z;
  243. }
  244. return y > p_v.y;
  245. }
  246. return x > p_v.x;
  247. }
  248. _FORCE_INLINE_ Vector4 operator*(const float p_scalar, const Vector4 &p_vec) {
  249. return p_vec * p_scalar;
  250. }
  251. _FORCE_INLINE_ Vector4 operator*(const double p_scalar, const Vector4 &p_vec) {
  252. return p_vec * p_scalar;
  253. }
  254. _FORCE_INLINE_ Vector4 operator*(const int32_t p_scalar, const Vector4 &p_vec) {
  255. return p_vec * p_scalar;
  256. }
  257. _FORCE_INLINE_ Vector4 operator*(const int64_t p_scalar, const Vector4 &p_vec) {
  258. return p_vec * p_scalar;
  259. }
  260. #endif // VECTOR4_H