vector4i.hpp 10 KB

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