vector3i.hpp 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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. class Vector3;
  37. class Vector3i {
  38. _FORCE_INLINE_ GDNativeTypePtr _native_ptr() const { return (void *)this; }
  39. friend class Variant;
  40. public:
  41. enum Axis {
  42. AXIS_X,
  43. AXIS_Y,
  44. AXIS_Z,
  45. };
  46. union {
  47. struct {
  48. int32_t x;
  49. int32_t y;
  50. int32_t z;
  51. };
  52. int32_t coord[3] = { 0 };
  53. };
  54. _FORCE_INLINE_ const int32_t &operator[](const int p_axis) const {
  55. DEV_ASSERT((unsigned int)p_axis < 3);
  56. return coord[p_axis];
  57. }
  58. _FORCE_INLINE_ int32_t &operator[](const int p_axis) {
  59. DEV_ASSERT((unsigned int)p_axis < 3);
  60. return coord[p_axis];
  61. }
  62. void set_axis(const int p_axis, const int32_t p_value);
  63. int32_t get_axis(const int p_axis) const;
  64. Vector3i::Axis min_axis_index() const;
  65. Vector3i::Axis max_axis_index() const;
  66. _FORCE_INLINE_ int64_t length_squared() const;
  67. _FORCE_INLINE_ double length() const;
  68. _FORCE_INLINE_ void zero();
  69. _FORCE_INLINE_ Vector3i abs() const;
  70. _FORCE_INLINE_ Vector3i sign() const;
  71. Vector3i clamp(const Vector3i &p_min, const Vector3i &p_max) const;
  72. /* Operators */
  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 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 int32_t p_scalar);
  84. _FORCE_INLINE_ Vector3i operator*(const int32_t p_scalar) const;
  85. _FORCE_INLINE_ Vector3i &operator/=(const int32_t p_scalar);
  86. _FORCE_INLINE_ Vector3i operator/(const int32_t p_scalar) 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;
  90. _FORCE_INLINE_ bool operator==(const Vector3i &p_v) const;
  91. _FORCE_INLINE_ bool operator!=(const Vector3i &p_v) const;
  92. _FORCE_INLINE_ bool operator<(const Vector3i &p_v) const;
  93. _FORCE_INLINE_ bool operator<=(const Vector3i &p_v) const;
  94. _FORCE_INLINE_ bool operator>(const Vector3i &p_v) const;
  95. _FORCE_INLINE_ bool operator>=(const Vector3i &p_v) const;
  96. operator String() const;
  97. operator Vector3() const;
  98. _FORCE_INLINE_ Vector3i() {}
  99. _FORCE_INLINE_ Vector3i(const int32_t p_x, const int32_t p_y, const int32_t p_z) {
  100. x = p_x;
  101. y = p_y;
  102. z = p_z;
  103. }
  104. };
  105. int64_t Vector3i::length_squared() const {
  106. return x * (int64_t)x + y * (int64_t)y + z * (int64_t)z;
  107. }
  108. double Vector3i::length() const {
  109. return Math::sqrt((double)length_squared());
  110. }
  111. Vector3i Vector3i::abs() const {
  112. return Vector3i(ABS(x), ABS(y), ABS(z));
  113. }
  114. Vector3i Vector3i::sign() const {
  115. return Vector3i(SIGN(x), SIGN(y), SIGN(z));
  116. }
  117. /* Operators */
  118. Vector3i &Vector3i::operator+=(const Vector3i &p_v) {
  119. x += p_v.x;
  120. y += p_v.y;
  121. z += p_v.z;
  122. return *this;
  123. }
  124. Vector3i Vector3i::operator+(const Vector3i &p_v) const {
  125. return Vector3i(x + p_v.x, y + p_v.y, z + p_v.z);
  126. }
  127. Vector3i &Vector3i::operator-=(const Vector3i &p_v) {
  128. x -= p_v.x;
  129. y -= p_v.y;
  130. z -= p_v.z;
  131. return *this;
  132. }
  133. Vector3i Vector3i::operator-(const Vector3i &p_v) const {
  134. return Vector3i(x - p_v.x, y - p_v.y, z - p_v.z);
  135. }
  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 int32_t p_scalar) {
  164. x *= p_scalar;
  165. y *= p_scalar;
  166. z *= p_scalar;
  167. return *this;
  168. }
  169. Vector3i Vector3i::operator*(const int32_t p_scalar) const {
  170. return Vector3i(x * p_scalar, y * p_scalar, z * p_scalar);
  171. }
  172. // Multiplication operators required to workaround issues with LLVM using implicit conversion.
  173. _FORCE_INLINE_ Vector3i operator*(const int32_t p_scalar, const Vector3i &p_vector) {
  174. return p_vector * p_scalar;
  175. }
  176. _FORCE_INLINE_ Vector3i operator*(const int64_t p_scalar, const Vector3i &p_vector) {
  177. return p_vector * p_scalar;
  178. }
  179. _FORCE_INLINE_ Vector3i operator*(const float p_scalar, const Vector3i &p_vector) {
  180. return p_vector * p_scalar;
  181. }
  182. _FORCE_INLINE_ Vector3i operator*(const double p_scalar, const Vector3i &p_vector) {
  183. return p_vector * p_scalar;
  184. }
  185. Vector3i &Vector3i::operator/=(const int32_t p_scalar) {
  186. x /= p_scalar;
  187. y /= p_scalar;
  188. z /= p_scalar;
  189. return *this;
  190. }
  191. Vector3i Vector3i::operator/(const int32_t p_scalar) const {
  192. return Vector3i(x / p_scalar, y / p_scalar, z / p_scalar);
  193. }
  194. Vector3i &Vector3i::operator%=(const int32_t p_scalar) {
  195. x %= p_scalar;
  196. y %= p_scalar;
  197. z %= p_scalar;
  198. return *this;
  199. }
  200. Vector3i Vector3i::operator%(const int32_t p_scalar) const {
  201. return Vector3i(x % p_scalar, y % p_scalar, z % p_scalar);
  202. }
  203. Vector3i Vector3i::operator-() const {
  204. return Vector3i(-x, -y, -z);
  205. }
  206. bool Vector3i::operator==(const Vector3i &p_v) const {
  207. return (x == p_v.x && y == p_v.y && z == p_v.z);
  208. }
  209. bool Vector3i::operator!=(const Vector3i &p_v) const {
  210. return (x != p_v.x || y != p_v.y || z != p_v.z);
  211. }
  212. bool Vector3i::operator<(const Vector3i &p_v) const {
  213. if (x == p_v.x) {
  214. if (y == p_v.y) {
  215. return z < p_v.z;
  216. } else {
  217. return y < p_v.y;
  218. }
  219. } else {
  220. return x < p_v.x;
  221. }
  222. }
  223. bool Vector3i::operator>(const Vector3i &p_v) const {
  224. if (x == p_v.x) {
  225. if (y == p_v.y) {
  226. return z > p_v.z;
  227. } else {
  228. return y > p_v.y;
  229. }
  230. } else {
  231. return x > p_v.x;
  232. }
  233. }
  234. bool Vector3i::operator<=(const Vector3i &p_v) const {
  235. if (x == p_v.x) {
  236. if (y == p_v.y) {
  237. return z <= p_v.z;
  238. } else {
  239. return y < p_v.y;
  240. }
  241. } else {
  242. return x < p_v.x;
  243. }
  244. }
  245. bool Vector3i::operator>=(const Vector3i &p_v) const {
  246. if (x == p_v.x) {
  247. if (y == p_v.y) {
  248. return z >= p_v.z;
  249. } else {
  250. return y > p_v.y;
  251. }
  252. } else {
  253. return x > p_v.x;
  254. }
  255. }
  256. void Vector3i::zero() {
  257. x = y = z = 0;
  258. }
  259. } // namespace godot
  260. #endif // GODOT_VECTOR3I_HPP