vector4i.hpp 9.2 KB

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