vector3i.hpp 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. /*************************************************************************/
  2. /* vector3i.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_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. _FORCE_INLINE_ int64_t length_squared() const;
  63. _FORCE_INLINE_ double length() const;
  64. _FORCE_INLINE_ void zero();
  65. _FORCE_INLINE_ Vector3i abs() const;
  66. _FORCE_INLINE_ Vector3i sign() const;
  67. Vector3i clamp(const Vector3i &p_min, const Vector3i &p_max) const;
  68. /* Operators */
  69. _FORCE_INLINE_ Vector3i &operator+=(const Vector3i &p_v);
  70. _FORCE_INLINE_ Vector3i operator+(const Vector3i &p_v) const;
  71. _FORCE_INLINE_ Vector3i &operator-=(const Vector3i &p_v);
  72. _FORCE_INLINE_ Vector3i operator-(const Vector3i &p_v) const;
  73. _FORCE_INLINE_ Vector3i &operator*=(const Vector3i &p_v);
  74. _FORCE_INLINE_ Vector3i operator*(const Vector3i &p_v) const;
  75. _FORCE_INLINE_ Vector3i &operator/=(const Vector3i &p_v);
  76. _FORCE_INLINE_ Vector3i operator/(const Vector3i &p_v) const;
  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 int32_t p_scalar);
  80. _FORCE_INLINE_ Vector3i operator*(const int32_t p_scalar) const;
  81. _FORCE_INLINE_ Vector3i &operator/=(const int32_t p_scalar);
  82. _FORCE_INLINE_ Vector3i operator/(const int32_t p_scalar) const;
  83. _FORCE_INLINE_ Vector3i &operator%=(const int32_t p_scalar);
  84. _FORCE_INLINE_ Vector3i operator%(const int32_t p_scalar) const;
  85. _FORCE_INLINE_ Vector3i operator-() const;
  86. _FORCE_INLINE_ bool operator==(const Vector3i &p_v) const;
  87. _FORCE_INLINE_ bool operator!=(const Vector3i &p_v) const;
  88. _FORCE_INLINE_ bool operator<(const Vector3i &p_v) const;
  89. _FORCE_INLINE_ bool operator<=(const Vector3i &p_v) const;
  90. _FORCE_INLINE_ bool operator>(const Vector3i &p_v) const;
  91. _FORCE_INLINE_ bool operator>=(const Vector3i &p_v) const;
  92. operator String() const;
  93. operator Vector3() const;
  94. _FORCE_INLINE_ Vector3i() {}
  95. _FORCE_INLINE_ Vector3i(const int32_t p_x, const int32_t p_y, const int32_t p_z) {
  96. x = p_x;
  97. y = p_y;
  98. z = p_z;
  99. }
  100. };
  101. int64_t Vector3i::length_squared() const {
  102. return x * (int64_t)x + y * (int64_t)y + z * (int64_t)z;
  103. }
  104. double Vector3i::length() const {
  105. return Math::sqrt((double)length_squared());
  106. }
  107. Vector3i Vector3i::abs() const {
  108. return Vector3i(Math::abs(x), Math::abs(y), Math::abs(z));
  109. }
  110. Vector3i Vector3i::sign() const {
  111. return Vector3i(SIGN(x), SIGN(y), SIGN(z));
  112. }
  113. /* Operators */
  114. Vector3i &Vector3i::operator+=(const Vector3i &p_v) {
  115. x += p_v.x;
  116. y += p_v.y;
  117. z += p_v.z;
  118. return *this;
  119. }
  120. Vector3i Vector3i::operator+(const Vector3i &p_v) const {
  121. return Vector3i(x + p_v.x, y + p_v.y, z + p_v.z);
  122. }
  123. Vector3i &Vector3i::operator-=(const Vector3i &p_v) {
  124. x -= p_v.x;
  125. y -= p_v.y;
  126. z -= p_v.z;
  127. return *this;
  128. }
  129. Vector3i Vector3i::operator-(const Vector3i &p_v) const {
  130. return Vector3i(x - p_v.x, y - p_v.y, z - p_v.z);
  131. }
  132. Vector3i &Vector3i::operator*=(const Vector3i &p_v) {
  133. x *= p_v.x;
  134. y *= p_v.y;
  135. z *= p_v.z;
  136. return *this;
  137. }
  138. Vector3i Vector3i::operator*(const Vector3i &p_v) const {
  139. return Vector3i(x * p_v.x, y * p_v.y, z * p_v.z);
  140. }
  141. Vector3i &Vector3i::operator/=(const Vector3i &p_v) {
  142. x /= p_v.x;
  143. y /= p_v.y;
  144. z /= p_v.z;
  145. return *this;
  146. }
  147. Vector3i Vector3i::operator/(const Vector3i &p_v) const {
  148. return Vector3i(x / p_v.x, y / p_v.y, z / p_v.z);
  149. }
  150. Vector3i &Vector3i::operator%=(const Vector3i &p_v) {
  151. x %= p_v.x;
  152. y %= p_v.y;
  153. z %= p_v.z;
  154. return *this;
  155. }
  156. Vector3i Vector3i::operator%(const Vector3i &p_v) const {
  157. return Vector3i(x % p_v.x, y % p_v.y, z % p_v.z);
  158. }
  159. Vector3i &Vector3i::operator*=(const int32_t p_scalar) {
  160. x *= p_scalar;
  161. y *= p_scalar;
  162. z *= p_scalar;
  163. return *this;
  164. }
  165. Vector3i Vector3i::operator*(const int32_t p_scalar) const {
  166. return Vector3i(x * p_scalar, y * p_scalar, z * p_scalar);
  167. }
  168. // Multiplication operators required to workaround issues with LLVM using implicit conversion.
  169. _FORCE_INLINE_ Vector3i operator*(const int32_t p_scalar, const Vector3i &p_vector) {
  170. return p_vector * p_scalar;
  171. }
  172. _FORCE_INLINE_ Vector3i operator*(const int64_t p_scalar, const Vector3i &p_vector) {
  173. return p_vector * p_scalar;
  174. }
  175. _FORCE_INLINE_ Vector3i operator*(const float p_scalar, const Vector3i &p_vector) {
  176. return p_vector * p_scalar;
  177. }
  178. _FORCE_INLINE_ Vector3i operator*(const double p_scalar, const Vector3i &p_vector) {
  179. return p_vector * p_scalar;
  180. }
  181. Vector3i &Vector3i::operator/=(const int32_t p_scalar) {
  182. x /= p_scalar;
  183. y /= p_scalar;
  184. z /= p_scalar;
  185. return *this;
  186. }
  187. Vector3i Vector3i::operator/(const int32_t p_scalar) const {
  188. return Vector3i(x / p_scalar, y / p_scalar, z / p_scalar);
  189. }
  190. Vector3i &Vector3i::operator%=(const int32_t p_scalar) {
  191. x %= p_scalar;
  192. y %= p_scalar;
  193. z %= p_scalar;
  194. return *this;
  195. }
  196. Vector3i Vector3i::operator%(const int32_t p_scalar) const {
  197. return Vector3i(x % p_scalar, y % p_scalar, z % p_scalar);
  198. }
  199. Vector3i Vector3i::operator-() const {
  200. return Vector3i(-x, -y, -z);
  201. }
  202. bool Vector3i::operator==(const Vector3i &p_v) const {
  203. return (x == p_v.x && y == p_v.y && z == p_v.z);
  204. }
  205. bool Vector3i::operator!=(const Vector3i &p_v) const {
  206. return (x != p_v.x || y != p_v.y || z != p_v.z);
  207. }
  208. bool Vector3i::operator<(const Vector3i &p_v) const {
  209. if (x == p_v.x) {
  210. if (y == p_v.y) {
  211. return z < p_v.z;
  212. } else {
  213. return y < p_v.y;
  214. }
  215. } else {
  216. return x < p_v.x;
  217. }
  218. }
  219. bool Vector3i::operator>(const Vector3i &p_v) const {
  220. if (x == p_v.x) {
  221. if (y == p_v.y) {
  222. return z > p_v.z;
  223. } else {
  224. return y > p_v.y;
  225. }
  226. } else {
  227. return x > p_v.x;
  228. }
  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. void Vector3i::zero() {
  253. x = y = z = 0;
  254. }
  255. } // namespace godot
  256. #endif // GODOT_VECTOR3I_HPP