vector4i.hpp 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. /*************************************************************************/
  2. /* vector4i.hpp */
  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 GODOT_VECTOR4I_HPP
  31. #define GODOT_VECTOR4I_HPP
  32. #include <godot_cpp/core/error_macros.hpp>
  33. #include <godot_cpp/core/math.hpp>
  34. namespace godot {
  35. class String;
  36. class Vector4;
  37. class Vector4i {
  38. _FORCE_INLINE_ GDNativeTypePtr _native_ptr() const { return (void *)this; }
  39. friend class Variant;
  40. public:
  41. enum Axis {
  42. AXIS_X,
  43. AXIS_Y,
  44. AXIS_Z,
  45. AXIS_W,
  46. };
  47. union {
  48. struct {
  49. int32_t x;
  50. int32_t y;
  51. int32_t z;
  52. int32_t w;
  53. };
  54. int32_t coord[4] = { 0 };
  55. };
  56. _FORCE_INLINE_ const int32_t &operator[](const int p_axis) const {
  57. return coord[p_axis];
  58. }
  59. _FORCE_INLINE_ int32_t &operator[](const int p_axis) {
  60. return coord[p_axis];
  61. }
  62. void set_axis(const int p_axis, const int32_t p_value);
  63. int32_t get_axis(const int p_axis) const;
  64. Vector4i::Axis min_axis_index() const;
  65. Vector4i::Axis max_axis_index() const;
  66. _FORCE_INLINE_ int64_t length_squared() const;
  67. _FORCE_INLINE_ double length() const;
  68. _FORCE_INLINE_ void zero();
  69. _FORCE_INLINE_ Vector4i abs() const;
  70. _FORCE_INLINE_ Vector4i sign() const;
  71. Vector4i clamp(const Vector4i &p_min, const Vector4i &p_max) const;
  72. /* Operators */
  73. _FORCE_INLINE_ Vector4i &operator+=(const Vector4i &p_v);
  74. _FORCE_INLINE_ Vector4i operator+(const Vector4i &p_v) const;
  75. _FORCE_INLINE_ Vector4i &operator-=(const Vector4i &p_v);
  76. _FORCE_INLINE_ Vector4i operator-(const Vector4i &p_v) const;
  77. _FORCE_INLINE_ Vector4i &operator*=(const Vector4i &p_v);
  78. _FORCE_INLINE_ Vector4i operator*(const Vector4i &p_v) const;
  79. _FORCE_INLINE_ Vector4i &operator/=(const Vector4i &p_v);
  80. _FORCE_INLINE_ Vector4i operator/(const Vector4i &p_v) const;
  81. _FORCE_INLINE_ Vector4i &operator%=(const Vector4i &p_v);
  82. _FORCE_INLINE_ Vector4i operator%(const Vector4i &p_v) const;
  83. _FORCE_INLINE_ Vector4i &operator*=(const int32_t p_scalar);
  84. _FORCE_INLINE_ Vector4i operator*(const int32_t p_scalar) const;
  85. _FORCE_INLINE_ Vector4i &operator/=(const int32_t p_scalar);
  86. _FORCE_INLINE_ Vector4i operator/(const int32_t p_scalar) const;
  87. _FORCE_INLINE_ Vector4i &operator%=(const int32_t p_scalar);
  88. _FORCE_INLINE_ Vector4i operator%(const int32_t p_scalar) const;
  89. _FORCE_INLINE_ Vector4i operator-() const;
  90. _FORCE_INLINE_ bool operator==(const Vector4i &p_v) const;
  91. _FORCE_INLINE_ bool operator!=(const Vector4i &p_v) const;
  92. _FORCE_INLINE_ bool operator<(const Vector4i &p_v) const;
  93. _FORCE_INLINE_ bool operator<=(const Vector4i &p_v) const;
  94. _FORCE_INLINE_ bool operator>(const Vector4i &p_v) const;
  95. _FORCE_INLINE_ bool operator>=(const Vector4i &p_v) const;
  96. operator String() const;
  97. operator Vector4() const;
  98. _FORCE_INLINE_ Vector4i() {}
  99. Vector4i(const Vector4 &p_vec4);
  100. _FORCE_INLINE_ Vector4i(const int32_t p_x, const int32_t p_y, const int32_t p_z, const int32_t p_w) {
  101. x = p_x;
  102. y = p_y;
  103. z = p_z;
  104. w = p_w;
  105. }
  106. };
  107. int64_t Vector4i::length_squared() const {
  108. return x * (int64_t)x + y * (int64_t)y + z * (int64_t)z + w * (int64_t)w;
  109. }
  110. double Vector4i::length() const {
  111. return Math::sqrt((double)length_squared());
  112. }
  113. Vector4i Vector4i::abs() const {
  114. return Vector4i(ABS(x), ABS(y), ABS(z), ABS(w));
  115. }
  116. Vector4i Vector4i::sign() const {
  117. return Vector4i(SIGN(x), SIGN(y), SIGN(z), SIGN(w));
  118. }
  119. /* Operators */
  120. Vector4i &Vector4i::operator+=(const Vector4i &p_v) {
  121. x += p_v.x;
  122. y += p_v.y;
  123. z += p_v.z;
  124. w += p_v.w;
  125. return *this;
  126. }
  127. Vector4i Vector4i::operator+(const Vector4i &p_v) const {
  128. return Vector4i(x + p_v.x, y + p_v.y, z + p_v.z, w + p_v.w);
  129. }
  130. Vector4i &Vector4i::operator-=(const Vector4i &p_v) {
  131. x -= p_v.x;
  132. y -= p_v.y;
  133. z -= p_v.z;
  134. w -= p_v.w;
  135. return *this;
  136. }
  137. Vector4i Vector4i::operator-(const Vector4i &p_v) const {
  138. return Vector4i(x - p_v.x, y - p_v.y, z - p_v.z, w - p_v.w);
  139. }
  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 int32_t p_scalar) {
  171. x *= p_scalar;
  172. y *= p_scalar;
  173. z *= p_scalar;
  174. w *= p_scalar;
  175. return *this;
  176. }
  177. Vector4i Vector4i::operator*(const int32_t p_scalar) const {
  178. return Vector4i(x * p_scalar, y * p_scalar, z * p_scalar, w * p_scalar);
  179. }
  180. // Multiplication operators required to workaround issues with LLVM using implicit conversion.
  181. _FORCE_INLINE_ Vector4i operator*(const int32_t p_scalar, const Vector4i &p_vector) {
  182. return p_vector * p_scalar;
  183. }
  184. _FORCE_INLINE_ Vector4i operator*(const int64_t p_scalar, const Vector4i &p_vector) {
  185. return p_vector * p_scalar;
  186. }
  187. _FORCE_INLINE_ Vector4i operator*(const float p_scalar, const Vector4i &p_vector) {
  188. return p_vector * p_scalar;
  189. }
  190. _FORCE_INLINE_ Vector4i operator*(const double p_scalar, const Vector4i &p_vector) {
  191. return p_vector * p_scalar;
  192. }
  193. Vector4i &Vector4i::operator/=(const int32_t p_scalar) {
  194. x /= p_scalar;
  195. y /= p_scalar;
  196. z /= p_scalar;
  197. w /= p_scalar;
  198. return *this;
  199. }
  200. Vector4i Vector4i::operator/(const int32_t p_scalar) const {
  201. return Vector4i(x / p_scalar, y / p_scalar, z / p_scalar, w / p_scalar);
  202. }
  203. Vector4i &Vector4i::operator%=(const int32_t p_scalar) {
  204. x %= p_scalar;
  205. y %= p_scalar;
  206. z %= p_scalar;
  207. w %= p_scalar;
  208. return *this;
  209. }
  210. Vector4i Vector4i::operator%(const int32_t p_scalar) const {
  211. return Vector4i(x % p_scalar, y % p_scalar, z % p_scalar, w % p_scalar);
  212. }
  213. Vector4i Vector4i::operator-() const {
  214. return Vector4i(-x, -y, -z, -w);
  215. }
  216. bool Vector4i::operator==(const Vector4i &p_v) const {
  217. return (x == p_v.x && y == p_v.y && z == p_v.z && w == p_v.w);
  218. }
  219. bool Vector4i::operator!=(const Vector4i &p_v) const {
  220. return (x != p_v.x || y != p_v.y || z != p_v.z || w != p_v.w);
  221. }
  222. bool Vector4i::operator<(const Vector4i &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. } else {
  228. return z < p_v.z;
  229. }
  230. } else {
  231. return y < p_v.y;
  232. }
  233. } else {
  234. return x < p_v.x;
  235. }
  236. }
  237. bool Vector4i::operator>(const Vector4i &p_v) const {
  238. if (x == p_v.x) {
  239. if (y == p_v.y) {
  240. if (z == p_v.z) {
  241. return w > p_v.w;
  242. } else {
  243. return z > p_v.z;
  244. }
  245. } else {
  246. return y > p_v.y;
  247. }
  248. } else {
  249. return x > p_v.x;
  250. }
  251. }
  252. bool Vector4i::operator<=(const Vector4i &p_v) const {
  253. if (x == p_v.x) {
  254. if (y == p_v.y) {
  255. if (z == p_v.z) {
  256. return w <= p_v.w;
  257. } else {
  258. return z < p_v.z;
  259. }
  260. } else {
  261. return y < p_v.y;
  262. }
  263. } else {
  264. return x < p_v.x;
  265. }
  266. }
  267. bool Vector4i::operator>=(const Vector4i &p_v) const {
  268. if (x == p_v.x) {
  269. if (y == p_v.y) {
  270. if (z == p_v.z) {
  271. return w >= p_v.w;
  272. } else {
  273. return z > p_v.z;
  274. }
  275. } else {
  276. return y > p_v.y;
  277. }
  278. } else {
  279. return x > p_v.x;
  280. }
  281. }
  282. void Vector4i::zero() {
  283. x = y = z = w = 0;
  284. }
  285. } // namespace godot
  286. #endif // GODOT_VECTOR4I_HPP