vector3i.hpp 9.4 KB

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