vector4i.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. /**************************************************************************/
  2. /* vector4i.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 VECTOR4I_H
  31. #define VECTOR4I_H
  32. #include "core/error/error_macros.h"
  33. #include "core/math/math_funcs.h"
  34. class String;
  35. struct Vector4;
  36. struct [[nodiscard]] Vector4i {
  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. int32_t x;
  47. int32_t y;
  48. int32_t z;
  49. int32_t w;
  50. };
  51. int32_t coord[4] = { 0 };
  52. };
  53. _FORCE_INLINE_ const int32_t &operator[](int p_axis) const {
  54. DEV_ASSERT((unsigned int)p_axis < 4);
  55. return coord[p_axis];
  56. }
  57. _FORCE_INLINE_ int32_t &operator[](int p_axis) {
  58. DEV_ASSERT((unsigned int)p_axis < 4);
  59. return coord[p_axis];
  60. }
  61. Vector4i::Axis min_axis_index() const;
  62. Vector4i::Axis max_axis_index() const;
  63. Vector4i min(const Vector4i &p_vector4i) const {
  64. return Vector4i(MIN(x, p_vector4i.x), MIN(y, p_vector4i.y), MIN(z, p_vector4i.z), MIN(w, p_vector4i.w));
  65. }
  66. Vector4i mini(int32_t p_scalar) const {
  67. return Vector4i(MIN(x, p_scalar), MIN(y, p_scalar), MIN(z, p_scalar), MIN(w, p_scalar));
  68. }
  69. Vector4i max(const Vector4i &p_vector4i) const {
  70. return Vector4i(MAX(x, p_vector4i.x), MAX(y, p_vector4i.y), MAX(z, p_vector4i.z), MAX(w, p_vector4i.w));
  71. }
  72. Vector4i maxi(int32_t p_scalar) const {
  73. return Vector4i(MAX(x, p_scalar), MAX(y, p_scalar), MAX(z, p_scalar), MAX(w, p_scalar));
  74. }
  75. _FORCE_INLINE_ int64_t length_squared() const;
  76. _FORCE_INLINE_ double length() const;
  77. _FORCE_INLINE_ void zero();
  78. _FORCE_INLINE_ double distance_to(const Vector4i &p_to) const;
  79. _FORCE_INLINE_ int64_t distance_squared_to(const Vector4i &p_to) const;
  80. _FORCE_INLINE_ Vector4i abs() const;
  81. _FORCE_INLINE_ Vector4i sign() const;
  82. Vector4i clamp(const Vector4i &p_min, const Vector4i &p_max) const;
  83. Vector4i clampi(int32_t p_min, int32_t p_max) const;
  84. Vector4i snapped(const Vector4i &p_step) const;
  85. Vector4i snappedi(int32_t p_step) const;
  86. /* Operators */
  87. _FORCE_INLINE_ Vector4i &operator+=(const Vector4i &p_v);
  88. _FORCE_INLINE_ Vector4i operator+(const Vector4i &p_v) const;
  89. _FORCE_INLINE_ Vector4i &operator-=(const Vector4i &p_v);
  90. _FORCE_INLINE_ Vector4i operator-(const Vector4i &p_v) const;
  91. _FORCE_INLINE_ Vector4i &operator*=(const Vector4i &p_v);
  92. _FORCE_INLINE_ Vector4i operator*(const Vector4i &p_v) const;
  93. _FORCE_INLINE_ Vector4i &operator/=(const Vector4i &p_v);
  94. _FORCE_INLINE_ Vector4i operator/(const Vector4i &p_v) const;
  95. _FORCE_INLINE_ Vector4i &operator%=(const Vector4i &p_v);
  96. _FORCE_INLINE_ Vector4i operator%(const Vector4i &p_v) const;
  97. _FORCE_INLINE_ Vector4i &operator*=(int32_t p_scalar);
  98. _FORCE_INLINE_ Vector4i operator*(int32_t p_scalar) const;
  99. _FORCE_INLINE_ Vector4i &operator/=(int32_t p_scalar);
  100. _FORCE_INLINE_ Vector4i operator/(int32_t p_scalar) const;
  101. _FORCE_INLINE_ Vector4i &operator%=(int32_t p_scalar);
  102. _FORCE_INLINE_ Vector4i operator%(int32_t p_scalar) const;
  103. _FORCE_INLINE_ Vector4i operator-() const;
  104. _FORCE_INLINE_ bool operator==(const Vector4i &p_v) const;
  105. _FORCE_INLINE_ bool operator!=(const Vector4i &p_v) const;
  106. _FORCE_INLINE_ bool operator<(const Vector4i &p_v) const;
  107. _FORCE_INLINE_ bool operator<=(const Vector4i &p_v) const;
  108. _FORCE_INLINE_ bool operator>(const Vector4i &p_v) const;
  109. _FORCE_INLINE_ bool operator>=(const Vector4i &p_v) const;
  110. operator String() const;
  111. operator Vector4() const;
  112. _FORCE_INLINE_ Vector4i() {}
  113. Vector4i(const Vector4 &p_vec4);
  114. _FORCE_INLINE_ Vector4i(int32_t p_x, int32_t p_y, int32_t p_z, int32_t p_w) {
  115. x = p_x;
  116. y = p_y;
  117. z = p_z;
  118. w = p_w;
  119. }
  120. };
  121. int64_t Vector4i::length_squared() const {
  122. return x * (int64_t)x + y * (int64_t)y + z * (int64_t)z + w * (int64_t)w;
  123. }
  124. double Vector4i::length() const {
  125. return Math::sqrt((double)length_squared());
  126. }
  127. double Vector4i::distance_to(const Vector4i &p_to) const {
  128. return (p_to - *this).length();
  129. }
  130. int64_t Vector4i::distance_squared_to(const Vector4i &p_to) const {
  131. return (p_to - *this).length_squared();
  132. }
  133. Vector4i Vector4i::abs() const {
  134. return Vector4i(Math::abs(x), Math::abs(y), Math::abs(z), Math::abs(w));
  135. }
  136. Vector4i Vector4i::sign() const {
  137. return Vector4i(SIGN(x), SIGN(y), SIGN(z), SIGN(w));
  138. }
  139. /* Operators */
  140. Vector4i &Vector4i::operator+=(const Vector4i &p_v) {
  141. x += p_v.x;
  142. y += p_v.y;
  143. z += p_v.z;
  144. w += p_v.w;
  145. return *this;
  146. }
  147. Vector4i Vector4i::operator+(const Vector4i &p_v) const {
  148. return Vector4i(x + p_v.x, y + p_v.y, z + p_v.z, w + p_v.w);
  149. }
  150. Vector4i &Vector4i::operator-=(const Vector4i &p_v) {
  151. x -= p_v.x;
  152. y -= p_v.y;
  153. z -= p_v.z;
  154. w -= p_v.w;
  155. return *this;
  156. }
  157. Vector4i Vector4i::operator-(const Vector4i &p_v) const {
  158. return Vector4i(x - p_v.x, y - p_v.y, z - p_v.z, w - p_v.w);
  159. }
  160. Vector4i &Vector4i::operator*=(const Vector4i &p_v) {
  161. x *= p_v.x;
  162. y *= p_v.y;
  163. z *= p_v.z;
  164. w *= p_v.w;
  165. return *this;
  166. }
  167. Vector4i Vector4i::operator*(const Vector4i &p_v) const {
  168. return Vector4i(x * p_v.x, y * p_v.y, z * p_v.z, w * p_v.w);
  169. }
  170. Vector4i &Vector4i::operator/=(const Vector4i &p_v) {
  171. x /= p_v.x;
  172. y /= p_v.y;
  173. z /= p_v.z;
  174. w /= p_v.w;
  175. return *this;
  176. }
  177. Vector4i Vector4i::operator/(const Vector4i &p_v) const {
  178. return Vector4i(x / p_v.x, y / p_v.y, z / p_v.z, w / p_v.w);
  179. }
  180. Vector4i &Vector4i::operator%=(const Vector4i &p_v) {
  181. x %= p_v.x;
  182. y %= p_v.y;
  183. z %= p_v.z;
  184. w %= p_v.w;
  185. return *this;
  186. }
  187. Vector4i Vector4i::operator%(const Vector4i &p_v) const {
  188. return Vector4i(x % p_v.x, y % p_v.y, z % p_v.z, w % p_v.w);
  189. }
  190. Vector4i &Vector4i::operator*=(int32_t p_scalar) {
  191. x *= p_scalar;
  192. y *= p_scalar;
  193. z *= p_scalar;
  194. w *= p_scalar;
  195. return *this;
  196. }
  197. Vector4i Vector4i::operator*(int32_t p_scalar) const {
  198. return Vector4i(x * p_scalar, y * p_scalar, z * p_scalar, w * p_scalar);
  199. }
  200. // Multiplication operators required to workaround issues with LLVM using implicit conversion.
  201. _FORCE_INLINE_ Vector4i operator*(int32_t p_scalar, const Vector4i &p_vector) {
  202. return p_vector * p_scalar;
  203. }
  204. _FORCE_INLINE_ Vector4i operator*(int64_t p_scalar, const Vector4i &p_vector) {
  205. return p_vector * p_scalar;
  206. }
  207. _FORCE_INLINE_ Vector4i operator*(float p_scalar, const Vector4i &p_vector) {
  208. return p_vector * p_scalar;
  209. }
  210. _FORCE_INLINE_ Vector4i operator*(double p_scalar, const Vector4i &p_vector) {
  211. return p_vector * p_scalar;
  212. }
  213. Vector4i &Vector4i::operator/=(int32_t p_scalar) {
  214. x /= p_scalar;
  215. y /= p_scalar;
  216. z /= p_scalar;
  217. w /= p_scalar;
  218. return *this;
  219. }
  220. Vector4i Vector4i::operator/(int32_t p_scalar) const {
  221. return Vector4i(x / p_scalar, y / p_scalar, z / p_scalar, w / p_scalar);
  222. }
  223. Vector4i &Vector4i::operator%=(int32_t p_scalar) {
  224. x %= p_scalar;
  225. y %= p_scalar;
  226. z %= p_scalar;
  227. w %= p_scalar;
  228. return *this;
  229. }
  230. Vector4i Vector4i::operator%(int32_t p_scalar) const {
  231. return Vector4i(x % p_scalar, y % p_scalar, z % p_scalar, w % p_scalar);
  232. }
  233. Vector4i Vector4i::operator-() const {
  234. return Vector4i(-x, -y, -z, -w);
  235. }
  236. bool Vector4i::operator==(const Vector4i &p_v) const {
  237. return (x == p_v.x && y == p_v.y && z == p_v.z && w == p_v.w);
  238. }
  239. bool Vector4i::operator!=(const Vector4i &p_v) const {
  240. return (x != p_v.x || y != p_v.y || z != p_v.z || w != p_v.w);
  241. }
  242. bool Vector4i::operator<(const Vector4i &p_v) const {
  243. if (x == p_v.x) {
  244. if (y == p_v.y) {
  245. if (z == p_v.z) {
  246. return w < p_v.w;
  247. } else {
  248. return z < p_v.z;
  249. }
  250. } else {
  251. return y < p_v.y;
  252. }
  253. } else {
  254. return x < p_v.x;
  255. }
  256. }
  257. bool Vector4i::operator>(const Vector4i &p_v) const {
  258. if (x == p_v.x) {
  259. if (y == p_v.y) {
  260. if (z == p_v.z) {
  261. return w > p_v.w;
  262. } else {
  263. return z > p_v.z;
  264. }
  265. } else {
  266. return y > p_v.y;
  267. }
  268. } else {
  269. return x > p_v.x;
  270. }
  271. }
  272. bool Vector4i::operator<=(const Vector4i &p_v) const {
  273. if (x == p_v.x) {
  274. if (y == p_v.y) {
  275. if (z == p_v.z) {
  276. return w <= p_v.w;
  277. } else {
  278. return z < p_v.z;
  279. }
  280. } else {
  281. return y < p_v.y;
  282. }
  283. } else {
  284. return x < p_v.x;
  285. }
  286. }
  287. bool Vector4i::operator>=(const Vector4i &p_v) const {
  288. if (x == p_v.x) {
  289. if (y == p_v.y) {
  290. if (z == p_v.z) {
  291. return w >= p_v.w;
  292. } else {
  293. return z > p_v.z;
  294. }
  295. } else {
  296. return y > p_v.y;
  297. }
  298. } else {
  299. return x > p_v.x;
  300. }
  301. }
  302. void Vector4i::zero() {
  303. x = y = z = w = 0;
  304. }
  305. #endif // VECTOR4I_H