vector4i.hpp 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  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. static const int AXIS_COUNT = 4;
  42. enum Axis {
  43. AXIS_X,
  44. AXIS_Y,
  45. AXIS_Z,
  46. AXIS_W,
  47. };
  48. union {
  49. struct {
  50. int32_t x;
  51. int32_t y;
  52. int32_t z;
  53. int32_t w;
  54. };
  55. int32_t coord[4] = { 0 };
  56. };
  57. _FORCE_INLINE_ const int32_t &operator[](const int p_axis) const {
  58. DEV_ASSERT((unsigned int)p_axis < 4);
  59. return coord[p_axis];
  60. }
  61. _FORCE_INLINE_ int32_t &operator[](const int p_axis) {
  62. DEV_ASSERT((unsigned int)p_axis < 4);
  63. return coord[p_axis];
  64. }
  65. void set_axis(const int p_axis, const int32_t p_value);
  66. int32_t get_axis(const int p_axis) const;
  67. Vector4i::Axis min_axis_index() const;
  68. Vector4i::Axis max_axis_index() const;
  69. _FORCE_INLINE_ int64_t length_squared() const;
  70. _FORCE_INLINE_ double length() const;
  71. _FORCE_INLINE_ void zero();
  72. _FORCE_INLINE_ Vector4i abs() const;
  73. _FORCE_INLINE_ Vector4i sign() const;
  74. Vector4i clamp(const Vector4i &p_min, const Vector4i &p_max) const;
  75. /* Operators */
  76. _FORCE_INLINE_ Vector4i &operator+=(const Vector4i &p_v);
  77. _FORCE_INLINE_ Vector4i operator+(const Vector4i &p_v) const;
  78. _FORCE_INLINE_ Vector4i &operator-=(const Vector4i &p_v);
  79. _FORCE_INLINE_ Vector4i operator-(const Vector4i &p_v) const;
  80. _FORCE_INLINE_ Vector4i &operator*=(const Vector4i &p_v);
  81. _FORCE_INLINE_ Vector4i operator*(const Vector4i &p_v) const;
  82. _FORCE_INLINE_ Vector4i &operator/=(const Vector4i &p_v);
  83. _FORCE_INLINE_ Vector4i operator/(const Vector4i &p_v) const;
  84. _FORCE_INLINE_ Vector4i &operator%=(const Vector4i &p_v);
  85. _FORCE_INLINE_ Vector4i operator%(const Vector4i &p_v) const;
  86. _FORCE_INLINE_ Vector4i &operator*=(const int32_t p_scalar);
  87. _FORCE_INLINE_ Vector4i operator*(const int32_t p_scalar) const;
  88. _FORCE_INLINE_ Vector4i &operator/=(const int32_t p_scalar);
  89. _FORCE_INLINE_ Vector4i operator/(const int32_t p_scalar) const;
  90. _FORCE_INLINE_ Vector4i &operator%=(const int32_t p_scalar);
  91. _FORCE_INLINE_ Vector4i operator%(const int32_t p_scalar) const;
  92. _FORCE_INLINE_ Vector4i operator-() 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. _FORCE_INLINE_ bool operator<=(const Vector4i &p_v) const;
  97. _FORCE_INLINE_ bool operator>(const Vector4i &p_v) const;
  98. _FORCE_INLINE_ bool operator>=(const Vector4i &p_v) const;
  99. operator String() const;
  100. operator Vector4() const;
  101. _FORCE_INLINE_ Vector4i() {}
  102. Vector4i(const Vector4 &p_vec4);
  103. _FORCE_INLINE_ Vector4i(const int32_t p_x, const int32_t p_y, const int32_t p_z, const int32_t p_w) {
  104. x = p_x;
  105. y = p_y;
  106. z = p_z;
  107. w = p_w;
  108. }
  109. };
  110. int64_t Vector4i::length_squared() const {
  111. return x * (int64_t)x + y * (int64_t)y + z * (int64_t)z + w * (int64_t)w;
  112. }
  113. double Vector4i::length() const {
  114. return Math::sqrt((double)length_squared());
  115. }
  116. Vector4i Vector4i::abs() const {
  117. return Vector4i(Math::abs(x), Math::abs(y), Math::abs(z), Math::abs(w));
  118. }
  119. Vector4i Vector4i::sign() const {
  120. return Vector4i(Math::sign(x), Math::sign(y), Math::sign(z), Math::sign(w));
  121. }
  122. /* Operators */
  123. Vector4i &Vector4i::operator+=(const Vector4i &p_v) {
  124. x += p_v.x;
  125. y += p_v.y;
  126. z += p_v.z;
  127. w += p_v.w;
  128. return *this;
  129. }
  130. Vector4i Vector4i::operator+(const Vector4i &p_v) const {
  131. return Vector4i(x + p_v.x, y + p_v.y, z + p_v.z, w + p_v.w);
  132. }
  133. Vector4i &Vector4i::operator-=(const Vector4i &p_v) {
  134. x -= p_v.x;
  135. y -= p_v.y;
  136. z -= p_v.z;
  137. w -= p_v.w;
  138. return *this;
  139. }
  140. Vector4i Vector4i::operator-(const Vector4i &p_v) const {
  141. return Vector4i(x - p_v.x, y - p_v.y, z - p_v.z, w - p_v.w);
  142. }
  143. Vector4i &Vector4i::operator*=(const Vector4i &p_v) {
  144. x *= p_v.x;
  145. y *= p_v.y;
  146. z *= p_v.z;
  147. w *= p_v.w;
  148. return *this;
  149. }
  150. Vector4i Vector4i::operator*(const Vector4i &p_v) const {
  151. return Vector4i(x * p_v.x, y * p_v.y, z * p_v.z, w * p_v.w);
  152. }
  153. Vector4i &Vector4i::operator/=(const Vector4i &p_v) {
  154. x /= p_v.x;
  155. y /= p_v.y;
  156. z /= p_v.z;
  157. w /= p_v.w;
  158. return *this;
  159. }
  160. Vector4i Vector4i::operator/(const Vector4i &p_v) const {
  161. return Vector4i(x / p_v.x, y / p_v.y, z / p_v.z, w / p_v.w);
  162. }
  163. Vector4i &Vector4i::operator%=(const Vector4i &p_v) {
  164. x %= p_v.x;
  165. y %= p_v.y;
  166. z %= p_v.z;
  167. w %= p_v.w;
  168. return *this;
  169. }
  170. Vector4i Vector4i::operator%(const Vector4i &p_v) const {
  171. return Vector4i(x % p_v.x, y % p_v.y, z % p_v.z, w % p_v.w);
  172. }
  173. Vector4i &Vector4i::operator*=(const int32_t p_scalar) {
  174. x *= p_scalar;
  175. y *= p_scalar;
  176. z *= p_scalar;
  177. w *= p_scalar;
  178. return *this;
  179. }
  180. Vector4i Vector4i::operator*(const int32_t p_scalar) const {
  181. return Vector4i(x * p_scalar, y * p_scalar, z * p_scalar, w * p_scalar);
  182. }
  183. // Multiplication operators required to workaround issues with LLVM using implicit conversion.
  184. _FORCE_INLINE_ Vector4i operator*(const int32_t p_scalar, const Vector4i &p_vector) {
  185. return p_vector * p_scalar;
  186. }
  187. _FORCE_INLINE_ Vector4i operator*(const int64_t p_scalar, const Vector4i &p_vector) {
  188. return p_vector * p_scalar;
  189. }
  190. _FORCE_INLINE_ Vector4i operator*(const float p_scalar, const Vector4i &p_vector) {
  191. return p_vector * p_scalar;
  192. }
  193. _FORCE_INLINE_ Vector4i operator*(const double p_scalar, const Vector4i &p_vector) {
  194. return p_vector * p_scalar;
  195. }
  196. Vector4i &Vector4i::operator/=(const int32_t p_scalar) {
  197. x /= p_scalar;
  198. y /= p_scalar;
  199. z /= p_scalar;
  200. w /= p_scalar;
  201. return *this;
  202. }
  203. Vector4i Vector4i::operator/(const int32_t p_scalar) const {
  204. return Vector4i(x / p_scalar, y / p_scalar, z / p_scalar, w / p_scalar);
  205. }
  206. Vector4i &Vector4i::operator%=(const int32_t p_scalar) {
  207. x %= p_scalar;
  208. y %= p_scalar;
  209. z %= p_scalar;
  210. w %= p_scalar;
  211. return *this;
  212. }
  213. Vector4i Vector4i::operator%(const int32_t p_scalar) const {
  214. return Vector4i(x % p_scalar, y % p_scalar, z % p_scalar, w % p_scalar);
  215. }
  216. Vector4i Vector4i::operator-() const {
  217. return Vector4i(-x, -y, -z, -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. return (x != p_v.x || y != p_v.y || z != p_v.z || w != p_v.w);
  224. }
  225. bool Vector4i::operator<(const Vector4i &p_v) const {
  226. if (x == p_v.x) {
  227. if (y == p_v.y) {
  228. if (z == p_v.z) {
  229. return w < p_v.w;
  230. } else {
  231. return z < p_v.z;
  232. }
  233. } else {
  234. return y < p_v.y;
  235. }
  236. } else {
  237. return x < p_v.x;
  238. }
  239. }
  240. bool Vector4i::operator>(const Vector4i &p_v) const {
  241. if (x == p_v.x) {
  242. if (y == p_v.y) {
  243. if (z == p_v.z) {
  244. return w > p_v.w;
  245. } else {
  246. return z > p_v.z;
  247. }
  248. } else {
  249. return y > p_v.y;
  250. }
  251. } else {
  252. return x > p_v.x;
  253. }
  254. }
  255. bool Vector4i::operator<=(const Vector4i &p_v) const {
  256. if (x == p_v.x) {
  257. if (y == p_v.y) {
  258. if (z == p_v.z) {
  259. return w <= p_v.w;
  260. } else {
  261. return z < p_v.z;
  262. }
  263. } else {
  264. return y < p_v.y;
  265. }
  266. } else {
  267. return x < p_v.x;
  268. }
  269. }
  270. bool Vector4i::operator>=(const Vector4i &p_v) const {
  271. if (x == p_v.x) {
  272. if (y == p_v.y) {
  273. if (z == p_v.z) {
  274. return w >= p_v.w;
  275. } else {
  276. return z > p_v.z;
  277. }
  278. } else {
  279. return y > p_v.y;
  280. }
  281. } else {
  282. return x > p_v.x;
  283. }
  284. }
  285. void Vector4i::zero() {
  286. x = y = z = w = 0;
  287. }
  288. } // namespace godot
  289. #endif // GODOT_VECTOR4I_HPP