vector3i.hpp 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  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 max(const Vector3i &p_vector3i) const {
  66. return Vector3i(MAX(x, p_vector3i.x), MAX(y, p_vector3i.y), MAX(z, p_vector3i.z));
  67. }
  68. _FORCE_INLINE_ int64_t length_squared() const;
  69. _FORCE_INLINE_ double length() const;
  70. _FORCE_INLINE_ int64_t distance_squared_to(const Vector3i &p_to) const;
  71. _FORCE_INLINE_ double distance_to(const Vector3i &p_to) const;
  72. _FORCE_INLINE_ void zero();
  73. _FORCE_INLINE_ Vector3i abs() const;
  74. _FORCE_INLINE_ Vector3i sign() const;
  75. Vector3i clamp(const Vector3i &p_min, const Vector3i &p_max) const;
  76. /* Operators */
  77. _FORCE_INLINE_ Vector3i &operator+=(const Vector3i &p_v);
  78. _FORCE_INLINE_ Vector3i operator+(const Vector3i &p_v) const;
  79. _FORCE_INLINE_ Vector3i &operator-=(const Vector3i &p_v);
  80. _FORCE_INLINE_ Vector3i operator-(const Vector3i &p_v) const;
  81. _FORCE_INLINE_ Vector3i &operator*=(const Vector3i &p_v);
  82. _FORCE_INLINE_ Vector3i operator*(const Vector3i &p_v) const;
  83. _FORCE_INLINE_ Vector3i &operator/=(const Vector3i &p_v);
  84. _FORCE_INLINE_ Vector3i operator/(const Vector3i &p_v) const;
  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 int32_t p_scalar);
  88. _FORCE_INLINE_ Vector3i operator*(const int32_t p_scalar) const;
  89. _FORCE_INLINE_ Vector3i &operator/=(const int32_t p_scalar);
  90. _FORCE_INLINE_ Vector3i operator/(const int32_t p_scalar) const;
  91. _FORCE_INLINE_ Vector3i &operator%=(const int32_t p_scalar);
  92. _FORCE_INLINE_ Vector3i operator%(const int32_t p_scalar) const;
  93. _FORCE_INLINE_ Vector3i operator-() const;
  94. _FORCE_INLINE_ bool operator==(const Vector3i &p_v) const;
  95. _FORCE_INLINE_ bool operator!=(const Vector3i &p_v) const;
  96. _FORCE_INLINE_ bool operator<(const Vector3i &p_v) const;
  97. _FORCE_INLINE_ bool operator<=(const Vector3i &p_v) const;
  98. _FORCE_INLINE_ bool operator>(const Vector3i &p_v) const;
  99. _FORCE_INLINE_ bool operator>=(const Vector3i &p_v) const;
  100. operator String() const;
  101. operator Vector3() const;
  102. _FORCE_INLINE_ Vector3i() {}
  103. _FORCE_INLINE_ Vector3i(const int32_t p_x, const int32_t p_y, const int32_t p_z) {
  104. x = p_x;
  105. y = p_y;
  106. z = p_z;
  107. }
  108. };
  109. int64_t Vector3i::length_squared() const {
  110. return x * (int64_t)x + y * (int64_t)y + z * (int64_t)z;
  111. }
  112. double Vector3i::length() const {
  113. return Math::sqrt((double)length_squared());
  114. }
  115. int64_t Vector3i::distance_squared_to(const Vector3i &p_to) const {
  116. return (p_to - *this).length_squared();
  117. }
  118. double Vector3i::distance_to(const Vector3i &p_to) const {
  119. return (p_to - *this).length();
  120. }
  121. Vector3i Vector3i::abs() const {
  122. return Vector3i(Math::abs(x), Math::abs(y), Math::abs(z));
  123. }
  124. Vector3i Vector3i::sign() const {
  125. return Vector3i(SIGN(x), SIGN(y), SIGN(z));
  126. }
  127. /* Operators */
  128. Vector3i &Vector3i::operator+=(const Vector3i &p_v) {
  129. x += p_v.x;
  130. y += p_v.y;
  131. z += p_v.z;
  132. return *this;
  133. }
  134. Vector3i Vector3i::operator+(const Vector3i &p_v) const {
  135. return Vector3i(x + p_v.x, y + p_v.y, z + p_v.z);
  136. }
  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 int32_t p_scalar) {
  174. x *= p_scalar;
  175. y *= p_scalar;
  176. z *= p_scalar;
  177. return *this;
  178. }
  179. Vector3i Vector3i::operator*(const int32_t p_scalar) const {
  180. return Vector3i(x * p_scalar, y * p_scalar, z * p_scalar);
  181. }
  182. // Multiplication operators required to workaround issues with LLVM using implicit conversion.
  183. _FORCE_INLINE_ Vector3i operator*(const int32_t p_scalar, const Vector3i &p_vector) {
  184. return p_vector * p_scalar;
  185. }
  186. _FORCE_INLINE_ Vector3i operator*(const int64_t p_scalar, const Vector3i &p_vector) {
  187. return p_vector * p_scalar;
  188. }
  189. _FORCE_INLINE_ Vector3i operator*(const float p_scalar, const Vector3i &p_vector) {
  190. return p_vector * p_scalar;
  191. }
  192. _FORCE_INLINE_ Vector3i operator*(const double p_scalar, const Vector3i &p_vector) {
  193. return p_vector * p_scalar;
  194. }
  195. Vector3i &Vector3i::operator/=(const int32_t p_scalar) {
  196. x /= p_scalar;
  197. y /= p_scalar;
  198. z /= p_scalar;
  199. return *this;
  200. }
  201. Vector3i Vector3i::operator/(const int32_t p_scalar) const {
  202. return Vector3i(x / p_scalar, y / p_scalar, z / 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 {
  214. return Vector3i(-x, -y, -z);
  215. }
  216. bool Vector3i::operator==(const Vector3i &p_v) const {
  217. return (x == p_v.x && y == p_v.y && z == p_v.z);
  218. }
  219. bool Vector3i::operator!=(const Vector3i &p_v) const {
  220. return (x != p_v.x || y != p_v.y || z != p_v.z);
  221. }
  222. bool Vector3i::operator<(const Vector3i &p_v) const {
  223. if (x == p_v.x) {
  224. if (y == p_v.y) {
  225. return z < p_v.z;
  226. } else {
  227. return y < p_v.y;
  228. }
  229. } else {
  230. return x < p_v.x;
  231. }
  232. }
  233. bool Vector3i::operator>(const Vector3i &p_v) const {
  234. if (x == p_v.x) {
  235. if (y == p_v.y) {
  236. return z > p_v.z;
  237. } else {
  238. return y > p_v.y;
  239. }
  240. } else {
  241. return x > p_v.x;
  242. }
  243. }
  244. bool Vector3i::operator<=(const Vector3i &p_v) const {
  245. if (x == p_v.x) {
  246. if (y == p_v.y) {
  247. return z <= p_v.z;
  248. } else {
  249. return y < p_v.y;
  250. }
  251. } else {
  252. return x < p_v.x;
  253. }
  254. }
  255. bool Vector3i::operator>=(const Vector3i &p_v) const {
  256. if (x == p_v.x) {
  257. if (y == p_v.y) {
  258. return z >= p_v.z;
  259. } else {
  260. return y > p_v.y;
  261. }
  262. } else {
  263. return x > p_v.x;
  264. }
  265. }
  266. void Vector3i::zero() {
  267. x = y = z = 0;
  268. }
  269. } // namespace godot
  270. #endif // GODOT_VECTOR3I_HPP