vector4.hpp 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. /**************************************************************************/
  2. /* vector4.hpp */
  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. #pragma once
  31. #include <godot_cpp/core/error_macros.hpp>
  32. #include <godot_cpp/core/math.hpp>
  33. namespace godot {
  34. class String;
  35. struct Vector4i;
  36. struct [[nodiscard]] Vector4 {
  37. static const int AXIS_COUNT = 4;
  38. enum Axis {
  39. AXIS_X,
  40. AXIS_Y,
  41. AXIS_Z,
  42. AXIS_W,
  43. };
  44. union {
  45. struct {
  46. real_t x;
  47. real_t y;
  48. real_t z;
  49. real_t w;
  50. };
  51. real_t coord[4] = { 0, 0, 0, 0 };
  52. };
  53. _FORCE_INLINE_ real_t &operator[](int p_axis) {
  54. DEV_ASSERT((unsigned int)p_axis < 4);
  55. return coord[p_axis];
  56. }
  57. _FORCE_INLINE_ const real_t &operator[](int p_axis) const {
  58. DEV_ASSERT((unsigned int)p_axis < 4);
  59. return coord[p_axis];
  60. }
  61. Vector4::Axis min_axis_index() const;
  62. Vector4::Axis max_axis_index() const;
  63. Vector4 min(const Vector4 &p_vector4) const {
  64. return Vector4(MIN(x, p_vector4.x), MIN(y, p_vector4.y), MIN(z, p_vector4.z), MIN(w, p_vector4.w));
  65. }
  66. Vector4 minf(real_t p_scalar) const {
  67. return Vector4(MIN(x, p_scalar), MIN(y, p_scalar), MIN(z, p_scalar), MIN(w, p_scalar));
  68. }
  69. Vector4 max(const Vector4 &p_vector4) const {
  70. return Vector4(MAX(x, p_vector4.x), MAX(y, p_vector4.y), MAX(z, p_vector4.z), MAX(w, p_vector4.w));
  71. }
  72. Vector4 maxf(real_t p_scalar) const {
  73. return Vector4(MAX(x, p_scalar), MAX(y, p_scalar), MAX(z, p_scalar), MAX(w, p_scalar));
  74. }
  75. _FORCE_INLINE_ real_t length_squared() const;
  76. bool is_equal_approx(const Vector4 &p_vec4) const;
  77. bool is_zero_approx() const;
  78. bool is_finite() const;
  79. real_t length() const;
  80. void normalize();
  81. Vector4 normalized() const;
  82. bool is_normalized() const;
  83. real_t distance_to(const Vector4 &p_to) const;
  84. real_t distance_squared_to(const Vector4 &p_to) const;
  85. Vector4 direction_to(const Vector4 &p_to) const;
  86. Vector4 abs() const;
  87. Vector4 sign() const;
  88. Vector4 floor() const;
  89. Vector4 ceil() const;
  90. Vector4 round() const;
  91. Vector4 lerp(const Vector4 &p_to, real_t p_weight) const;
  92. Vector4 cubic_interpolate(const Vector4 &p_b, const Vector4 &p_pre_a, const Vector4 &p_post_b, real_t p_weight) const;
  93. 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;
  94. Vector4 posmod(real_t p_mod) const;
  95. Vector4 posmodv(const Vector4 &p_modv) const;
  96. void snap(const Vector4 &p_step);
  97. void snapf(real_t p_step);
  98. Vector4 snapped(const Vector4 &p_step) const;
  99. Vector4 snappedf(real_t p_step) const;
  100. Vector4 clamp(const Vector4 &p_min, const Vector4 &p_max) const;
  101. Vector4 clampf(real_t p_min, real_t p_max) const;
  102. Vector4 inverse() const;
  103. _FORCE_INLINE_ real_t dot(const Vector4 &p_vec4) const;
  104. _FORCE_INLINE_ void operator+=(const Vector4 &p_vec4);
  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*=(real_t p_s);
  109. _FORCE_INLINE_ void operator/=(real_t p_s);
  110. _FORCE_INLINE_ Vector4 operator+(const Vector4 &p_vec4) const;
  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;
  115. _FORCE_INLINE_ Vector4 operator*(real_t p_s) const;
  116. _FORCE_INLINE_ Vector4 operator/(real_t p_s) const;
  117. _FORCE_INLINE_ bool operator==(const Vector4 &p_vec4) 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. operator String() const;
  124. operator Vector4i() const;
  125. _FORCE_INLINE_ Vector4() {}
  126. _FORCE_INLINE_ Vector4(real_t p_x, real_t p_y, real_t p_z, real_t p_w) {
  127. x = p_x;
  128. y = p_y;
  129. z = p_z;
  130. w = p_w;
  131. }
  132. };
  133. real_t Vector4::dot(const Vector4 &p_vec4) const {
  134. return x * p_vec4.x + y * p_vec4.y + z * p_vec4.z + w * p_vec4.w;
  135. }
  136. real_t Vector4::length_squared() const {
  137. return dot(*this);
  138. }
  139. void Vector4::operator+=(const Vector4 &p_vec4) {
  140. x += p_vec4.x;
  141. y += p_vec4.y;
  142. z += p_vec4.z;
  143. w += p_vec4.w;
  144. }
  145. void Vector4::operator-=(const Vector4 &p_vec4) {
  146. x -= p_vec4.x;
  147. y -= p_vec4.y;
  148. z -= p_vec4.z;
  149. w -= p_vec4.w;
  150. }
  151. void Vector4::operator*=(const Vector4 &p_vec4) {
  152. x *= p_vec4.x;
  153. y *= p_vec4.y;
  154. z *= p_vec4.z;
  155. w *= p_vec4.w;
  156. }
  157. void Vector4::operator/=(const Vector4 &p_vec4) {
  158. x /= p_vec4.x;
  159. y /= p_vec4.y;
  160. z /= p_vec4.z;
  161. w /= p_vec4.w;
  162. }
  163. void Vector4::operator*=(real_t p_s) {
  164. x *= p_s;
  165. y *= p_s;
  166. z *= p_s;
  167. w *= p_s;
  168. }
  169. void Vector4::operator/=(real_t p_s) {
  170. *this *= 1.0f / p_s;
  171. }
  172. Vector4 Vector4::operator+(const Vector4 &p_vec4) const {
  173. return Vector4(x + p_vec4.x, y + p_vec4.y, z + p_vec4.z, w + p_vec4.w);
  174. }
  175. Vector4 Vector4::operator-(const Vector4 &p_vec4) const {
  176. return Vector4(x - p_vec4.x, y - p_vec4.y, z - p_vec4.z, w - p_vec4.w);
  177. }
  178. Vector4 Vector4::operator*(const Vector4 &p_vec4) const {
  179. return Vector4(x * p_vec4.x, y * p_vec4.y, z * p_vec4.z, w * p_vec4.w);
  180. }
  181. Vector4 Vector4::operator/(const Vector4 &p_vec4) const {
  182. return Vector4(x / p_vec4.x, y / p_vec4.y, z / p_vec4.z, w / p_vec4.w);
  183. }
  184. Vector4 Vector4::operator-() const {
  185. return Vector4(-x, -y, -z, -w);
  186. }
  187. Vector4 Vector4::operator*(real_t p_s) const {
  188. return Vector4(x * p_s, y * p_s, z * p_s, w * p_s);
  189. }
  190. Vector4 Vector4::operator/(real_t p_s) const {
  191. return *this * (1.0f / p_s);
  192. }
  193. bool Vector4::operator==(const Vector4 &p_vec4) const {
  194. return x == p_vec4.x && y == p_vec4.y && z == p_vec4.z && w == p_vec4.w;
  195. }
  196. bool Vector4::operator!=(const Vector4 &p_vec4) const {
  197. return x != p_vec4.x || y != p_vec4.y || z != p_vec4.z || w != p_vec4.w;
  198. }
  199. bool Vector4::operator<(const Vector4 &p_v) const {
  200. if (x == p_v.x) {
  201. if (y == p_v.y) {
  202. if (z == p_v.z) {
  203. return w < p_v.w;
  204. }
  205. return z < p_v.z;
  206. }
  207. return y < p_v.y;
  208. }
  209. return x < p_v.x;
  210. }
  211. bool Vector4::operator>(const Vector4 &p_v) const {
  212. if (x == p_v.x) {
  213. if (y == p_v.y) {
  214. if (z == p_v.z) {
  215. return w > p_v.w;
  216. }
  217. return z > p_v.z;
  218. }
  219. return y > p_v.y;
  220. }
  221. return x > p_v.x;
  222. }
  223. bool Vector4::operator<=(const Vector4 &p_v) const {
  224. if (x == p_v.x) {
  225. if (y == p_v.y) {
  226. if (z == p_v.z) {
  227. return w <= p_v.w;
  228. }
  229. return z < p_v.z;
  230. }
  231. return y < p_v.y;
  232. }
  233. return x < p_v.x;
  234. }
  235. bool Vector4::operator>=(const Vector4 &p_v) const {
  236. if (x == p_v.x) {
  237. if (y == p_v.y) {
  238. if (z == p_v.z) {
  239. return w >= p_v.w;
  240. }
  241. return z > p_v.z;
  242. }
  243. return y > p_v.y;
  244. }
  245. return x > p_v.x;
  246. }
  247. _FORCE_INLINE_ Vector4 operator*(float p_scalar, const Vector4 &p_vec) {
  248. return p_vec * p_scalar;
  249. }
  250. _FORCE_INLINE_ Vector4 operator*(double p_scalar, const Vector4 &p_vec) {
  251. return p_vec * p_scalar;
  252. }
  253. _FORCE_INLINE_ Vector4 operator*(int32_t p_scalar, const Vector4 &p_vec) {
  254. return p_vec * p_scalar;
  255. }
  256. _FORCE_INLINE_ Vector4 operator*(int64_t p_scalar, const Vector4 &p_vec) {
  257. return p_vec * p_scalar;
  258. }
  259. } // namespace godot