vector4i.h 9.5 KB

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