vector4i.hpp 9.9 KB

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