vector4.h 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. /*************************************************************************/
  2. /* vector4.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
  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/math/math_defs.h"
  33. #include "core/math/math_funcs.h"
  34. #include "core/math/vector3.h"
  35. #include "core/string/ustring.h"
  36. struct _NO_DISCARD_ Vector4 {
  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. _FORCE_INLINE_ void set_all(const real_t p_value);
  61. void set_axis(const int p_axis, const real_t p_value);
  62. real_t get_axis(const int p_axis) const;
  63. Vector4::Axis min_axis_index() const;
  64. Vector4::Axis max_axis_index() const;
  65. _FORCE_INLINE_ real_t length_squared() const;
  66. bool is_equal_approx(const Vector4 &p_vec4) const;
  67. real_t length() const;
  68. void normalize();
  69. Vector4 normalized() const;
  70. bool is_normalized() const;
  71. real_t distance_to(const Vector4 &p_to) const;
  72. real_t distance_squared_to(const Vector4 &p_to) const;
  73. Vector4 direction_to(const Vector4 &p_to) const;
  74. Vector4 abs() const;
  75. Vector4 sign() const;
  76. Vector4 floor() const;
  77. Vector4 ceil() const;
  78. Vector4 round() const;
  79. Vector4 lerp(const Vector4 &p_to, const real_t p_weight) const;
  80. Vector4 cubic_interpolate(const Vector4 &p_b, const Vector4 &p_pre_a, const Vector4 &p_post_b, const real_t p_weight) const;
  81. 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;
  82. Vector4 posmod(const real_t p_mod) const;
  83. Vector4 posmodv(const Vector4 &p_modv) const;
  84. void snap(const Vector4 &p_step);
  85. Vector4 snapped(const Vector4 &p_step) const;
  86. Vector4 clamp(const Vector4 &p_min, const Vector4 &p_max) const;
  87. Vector4 inverse() const;
  88. _FORCE_INLINE_ real_t dot(const Vector4 &p_vec4) const;
  89. _FORCE_INLINE_ void operator+=(const Vector4 &p_vec4);
  90. _FORCE_INLINE_ void operator-=(const Vector4 &p_vec4);
  91. _FORCE_INLINE_ void operator*=(const Vector4 &p_vec4);
  92. _FORCE_INLINE_ void operator/=(const Vector4 &p_vec4);
  93. _FORCE_INLINE_ void operator*=(const real_t &s);
  94. _FORCE_INLINE_ void operator/=(const real_t &s);
  95. _FORCE_INLINE_ Vector4 operator+(const Vector4 &p_vec4) const;
  96. _FORCE_INLINE_ Vector4 operator-(const Vector4 &p_vec4) const;
  97. _FORCE_INLINE_ Vector4 operator*(const Vector4 &p_vec4) const;
  98. _FORCE_INLINE_ Vector4 operator/(const Vector4 &p_vec4) const;
  99. _FORCE_INLINE_ Vector4 operator-() const;
  100. _FORCE_INLINE_ Vector4 operator*(const real_t &s) const;
  101. _FORCE_INLINE_ Vector4 operator/(const real_t &s) const;
  102. _FORCE_INLINE_ bool operator==(const Vector4 &p_vec4) const;
  103. _FORCE_INLINE_ bool operator!=(const Vector4 &p_vec4) const;
  104. _FORCE_INLINE_ bool operator>(const Vector4 &p_vec4) const;
  105. _FORCE_INLINE_ bool operator<(const Vector4 &p_vec4) const;
  106. _FORCE_INLINE_ bool operator>=(const Vector4 &p_vec4) const;
  107. _FORCE_INLINE_ bool operator<=(const Vector4 &p_vec4) const;
  108. operator String() const;
  109. _FORCE_INLINE_ Vector4() {}
  110. _FORCE_INLINE_ Vector4(real_t p_x, real_t p_y, real_t p_z, real_t p_w) :
  111. x(p_x),
  112. y(p_y),
  113. z(p_z),
  114. w(p_w) {
  115. }
  116. Vector4(const Vector4 &p_vec4) :
  117. x(p_vec4.x),
  118. y(p_vec4.y),
  119. z(p_vec4.z),
  120. w(p_vec4.w) {
  121. }
  122. void operator=(const Vector4 &p_vec4) {
  123. x = p_vec4.x;
  124. y = p_vec4.y;
  125. z = p_vec4.z;
  126. w = p_vec4.w;
  127. }
  128. };
  129. void Vector4::set_all(const real_t p_value) {
  130. x = y = z = p_value;
  131. }
  132. real_t Vector4::dot(const Vector4 &p_vec4) const {
  133. return x * p_vec4.x + y * p_vec4.y + z * p_vec4.z + w * p_vec4.w;
  134. }
  135. real_t Vector4::length_squared() const {
  136. return dot(*this);
  137. }
  138. void Vector4::operator+=(const Vector4 &p_vec4) {
  139. x += p_vec4.x;
  140. y += p_vec4.y;
  141. z += p_vec4.z;
  142. w += p_vec4.w;
  143. }
  144. void Vector4::operator-=(const Vector4 &p_vec4) {
  145. x -= p_vec4.x;
  146. y -= p_vec4.y;
  147. z -= p_vec4.z;
  148. w -= p_vec4.w;
  149. }
  150. void Vector4::operator*=(const Vector4 &p_vec4) {
  151. x *= p_vec4.x;
  152. y *= p_vec4.y;
  153. z *= p_vec4.z;
  154. w *= p_vec4.w;
  155. }
  156. void Vector4::operator/=(const Vector4 &p_vec4) {
  157. x /= p_vec4.x;
  158. y /= p_vec4.y;
  159. z /= p_vec4.z;
  160. w /= p_vec4.w;
  161. }
  162. void Vector4::operator*=(const real_t &s) {
  163. x *= s;
  164. y *= s;
  165. z *= s;
  166. w *= s;
  167. }
  168. void Vector4::operator/=(const real_t &s) {
  169. *this *= 1.0f / s;
  170. }
  171. Vector4 Vector4::operator+(const Vector4 &p_vec4) const {
  172. return Vector4(x + p_vec4.x, y + p_vec4.y, z + p_vec4.z, w + p_vec4.w);
  173. }
  174. Vector4 Vector4::operator-(const Vector4 &p_vec4) const {
  175. return Vector4(x - p_vec4.x, y - p_vec4.y, z - p_vec4.z, w - p_vec4.w);
  176. }
  177. Vector4 Vector4::operator*(const Vector4 &p_vec4) const {
  178. return Vector4(x * p_vec4.x, y * p_vec4.y, z * p_vec4.z, w * p_vec4.w);
  179. }
  180. Vector4 Vector4::operator/(const Vector4 &p_vec4) const {
  181. return Vector4(x / p_vec4.x, y / p_vec4.y, z / p_vec4.z, w / p_vec4.w);
  182. }
  183. Vector4 Vector4::operator-() const {
  184. return Vector4(-x, -y, -z, -w);
  185. }
  186. Vector4 Vector4::operator*(const real_t &s) const {
  187. return Vector4(x * s, y * s, z * s, w * s);
  188. }
  189. Vector4 Vector4::operator/(const real_t &s) const {
  190. return *this * (1.0f / s);
  191. }
  192. bool Vector4::operator==(const Vector4 &p_vec4) const {
  193. return x == p_vec4.x && y == p_vec4.y && z == p_vec4.z && w == p_vec4.w;
  194. }
  195. bool Vector4::operator!=(const Vector4 &p_vec4) const {
  196. return x != p_vec4.x || y != p_vec4.y || z != p_vec4.z || w != p_vec4.w;
  197. }
  198. bool Vector4::operator<(const Vector4 &p_v) const {
  199. if (x == p_v.x) {
  200. if (y == p_v.y) {
  201. if (z == p_v.z) {
  202. return w < p_v.w;
  203. }
  204. return z < p_v.z;
  205. }
  206. return y < p_v.y;
  207. }
  208. return x < p_v.x;
  209. }
  210. bool Vector4::operator>(const Vector4 &p_v) const {
  211. if (x == p_v.x) {
  212. if (y == p_v.y) {
  213. if (z == p_v.z) {
  214. return w > p_v.w;
  215. }
  216. return z > p_v.z;
  217. }
  218. return y > p_v.y;
  219. }
  220. return x > p_v.x;
  221. }
  222. bool Vector4::operator<=(const Vector4 &p_v) const {
  223. if (x == p_v.x) {
  224. if (y == p_v.y) {
  225. if (z == p_v.z) {
  226. return w <= p_v.w;
  227. }
  228. return z < p_v.z;
  229. }
  230. return y < p_v.y;
  231. }
  232. return x < p_v.x;
  233. }
  234. bool Vector4::operator>=(const Vector4 &p_v) const {
  235. if (x == p_v.x) {
  236. if (y == p_v.y) {
  237. if (z == p_v.z) {
  238. return w >= p_v.w;
  239. }
  240. return z > p_v.z;
  241. }
  242. return y > p_v.y;
  243. }
  244. return x > p_v.x;
  245. }
  246. _FORCE_INLINE_ Vector4 operator*(const float p_scalar, const Vector4 &p_vec) {
  247. return p_vec * p_scalar;
  248. }
  249. _FORCE_INLINE_ Vector4 operator*(const double p_scalar, const Vector4 &p_vec) {
  250. return p_vec * p_scalar;
  251. }
  252. _FORCE_INLINE_ Vector4 operator*(const int32_t p_scalar, const Vector4 &p_vec) {
  253. return p_vec * p_scalar;
  254. }
  255. _FORCE_INLINE_ Vector4 operator*(const int64_t p_scalar, const Vector4 &p_vec) {
  256. return p_vec * p_scalar;
  257. }
  258. #endif // VECTOR4_H