vector3i.hpp 9.6 KB

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