vector4.h 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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_defs.h"
  34. #include "core/typedefs.h"
  35. class String;
  36. struct Vector4i;
  37. struct [[nodiscard]] Vector4 {
  38. static const int AXIS_COUNT = 4;
  39. enum Axis {
  40. AXIS_X,
  41. AXIS_Y,
  42. AXIS_Z,
  43. AXIS_W,
  44. };
  45. union {
  46. struct {
  47. real_t x;
  48. real_t y;
  49. real_t z;
  50. real_t w;
  51. };
  52. real_t coord[4] = { 0, 0, 0, 0 };
  53. };
  54. _FORCE_INLINE_ real_t &operator[](int p_axis) {
  55. DEV_ASSERT((unsigned int)p_axis < 4);
  56. return coord[p_axis];
  57. }
  58. _FORCE_INLINE_ const real_t &operator[](int p_axis) const {
  59. DEV_ASSERT((unsigned int)p_axis < 4);
  60. return coord[p_axis];
  61. }
  62. Vector4::Axis min_axis_index() const;
  63. Vector4::Axis max_axis_index() const;
  64. Vector4 min(const Vector4 &p_vector4) const {
  65. return Vector4(MIN(x, p_vector4.x), MIN(y, p_vector4.y), MIN(z, p_vector4.z), MIN(w, p_vector4.w));
  66. }
  67. Vector4 minf(real_t p_scalar) const {
  68. return Vector4(MIN(x, p_scalar), MIN(y, p_scalar), MIN(z, p_scalar), MIN(w, p_scalar));
  69. }
  70. Vector4 max(const Vector4 &p_vector4) const {
  71. return Vector4(MAX(x, p_vector4.x), MAX(y, p_vector4.y), MAX(z, p_vector4.z), MAX(w, p_vector4.w));
  72. }
  73. Vector4 maxf(real_t p_scalar) const {
  74. return Vector4(MAX(x, p_scalar), MAX(y, p_scalar), MAX(z, p_scalar), MAX(w, p_scalar));
  75. }
  76. _FORCE_INLINE_ real_t length_squared() const;
  77. bool is_equal_approx(const Vector4 &p_vec4) const;
  78. bool is_zero_approx() const;
  79. bool is_finite() const;
  80. real_t length() const;
  81. void normalize();
  82. Vector4 normalized() const;
  83. bool is_normalized() const;
  84. real_t distance_to(const Vector4 &p_to) const;
  85. real_t distance_squared_to(const Vector4 &p_to) const;
  86. Vector4 direction_to(const Vector4 &p_to) const;
  87. Vector4 abs() const;
  88. Vector4 sign() const;
  89. Vector4 floor() const;
  90. Vector4 ceil() const;
  91. Vector4 round() const;
  92. Vector4 lerp(const Vector4 &p_to, real_t p_weight) const;
  93. Vector4 cubic_interpolate(const Vector4 &p_b, const Vector4 &p_pre_a, const Vector4 &p_post_b, real_t p_weight) const;
  94. Vector4 cubic_interpolate_in_time(const Vector4 &p_b, const Vector4 &p_pre_a, const Vector4 &p_post_b, real_t p_weight, real_t p_b_t, real_t p_pre_a_t, real_t p_post_b_t) const;
  95. Vector4 posmod(real_t p_mod) const;
  96. Vector4 posmodv(const Vector4 &p_modv) const;
  97. void snap(const Vector4 &p_step);
  98. void snapf(real_t p_step);
  99. Vector4 snapped(const Vector4 &p_step) const;
  100. Vector4 snappedf(real_t p_step) const;
  101. Vector4 clamp(const Vector4 &p_min, const Vector4 &p_max) const;
  102. Vector4 clampf(real_t p_min, real_t p_max) const;
  103. Vector4 inverse() const;
  104. _FORCE_INLINE_ real_t dot(const Vector4 &p_vec4) const;
  105. _FORCE_INLINE_ void operator+=(const Vector4 &p_vec4);
  106. _FORCE_INLINE_ void operator-=(const Vector4 &p_vec4);
  107. _FORCE_INLINE_ void operator*=(const Vector4 &p_vec4);
  108. _FORCE_INLINE_ void operator/=(const Vector4 &p_vec4);
  109. _FORCE_INLINE_ void operator*=(real_t p_s);
  110. _FORCE_INLINE_ void operator/=(real_t p_s);
  111. _FORCE_INLINE_ Vector4 operator+(const Vector4 &p_vec4) const;
  112. _FORCE_INLINE_ Vector4 operator-(const Vector4 &p_vec4) const;
  113. _FORCE_INLINE_ Vector4 operator*(const Vector4 &p_vec4) const;
  114. _FORCE_INLINE_ Vector4 operator/(const Vector4 &p_vec4) const;
  115. _FORCE_INLINE_ Vector4 operator-() const;
  116. _FORCE_INLINE_ Vector4 operator*(real_t p_s) const;
  117. _FORCE_INLINE_ Vector4 operator/(real_t p_s) const;
  118. _FORCE_INLINE_ bool operator==(const Vector4 &p_vec4) const;
  119. _FORCE_INLINE_ bool operator!=(const Vector4 &p_vec4) const;
  120. _FORCE_INLINE_ bool operator>(const Vector4 &p_vec4) const;
  121. _FORCE_INLINE_ bool operator<(const Vector4 &p_vec4) const;
  122. _FORCE_INLINE_ bool operator>=(const Vector4 &p_vec4) const;
  123. _FORCE_INLINE_ bool operator<=(const Vector4 &p_vec4) const;
  124. operator String() const;
  125. operator Vector4i() const;
  126. _FORCE_INLINE_ Vector4() {}
  127. _FORCE_INLINE_ Vector4(real_t p_x, real_t p_y, real_t p_z, real_t p_w) {
  128. x = p_x;
  129. y = p_y;
  130. z = p_z;
  131. w = p_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*=(real_t p_s) {
  165. x *= p_s;
  166. y *= p_s;
  167. z *= p_s;
  168. w *= p_s;
  169. }
  170. void Vector4::operator/=(real_t p_s) {
  171. *this *= 1.0f / p_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*(real_t p_s) const {
  189. return Vector4(x * p_s, y * p_s, z * p_s, w * p_s);
  190. }
  191. Vector4 Vector4::operator/(real_t p_s) const {
  192. return *this * (1.0f / p_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*(float p_scalar, const Vector4 &p_vec) {
  249. return p_vec * p_scalar;
  250. }
  251. _FORCE_INLINE_ Vector4 operator*(double p_scalar, const Vector4 &p_vec) {
  252. return p_vec * p_scalar;
  253. }
  254. _FORCE_INLINE_ Vector4 operator*(int32_t p_scalar, const Vector4 &p_vec) {
  255. return p_vec * p_scalar;
  256. }
  257. _FORCE_INLINE_ Vector4 operator*(int64_t p_scalar, const Vector4 &p_vec) {
  258. return p_vec * p_scalar;
  259. }
  260. #endif // VECTOR4_H