vector3i.hpp 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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. static const int AXIS_COUNT = 3;
  42. enum Axis {
  43. AXIS_X,
  44. AXIS_Y,
  45. AXIS_Z,
  46. };
  47. union {
  48. struct {
  49. int32_t x;
  50. int32_t y;
  51. int32_t z;
  52. };
  53. int32_t coord[3] = { 0 };
  54. };
  55. _FORCE_INLINE_ const int32_t &operator[](const int p_axis) const {
  56. DEV_ASSERT((unsigned int)p_axis < 3);
  57. return coord[p_axis];
  58. }
  59. _FORCE_INLINE_ int32_t &operator[](const int p_axis) {
  60. DEV_ASSERT((unsigned int)p_axis < 3);
  61. return coord[p_axis];
  62. }
  63. Vector3i::Axis min_axis_index() const;
  64. Vector3i::Axis max_axis_index() const;
  65. _FORCE_INLINE_ int64_t length_squared() const;
  66. _FORCE_INLINE_ double length() const;
  67. _FORCE_INLINE_ void zero();
  68. _FORCE_INLINE_ Vector3i abs() const;
  69. _FORCE_INLINE_ Vector3i sign() const;
  70. Vector3i clamp(const Vector3i &p_min, const Vector3i &p_max) const;
  71. /* Operators */
  72. _FORCE_INLINE_ Vector3i &operator+=(const Vector3i &p_v);
  73. _FORCE_INLINE_ Vector3i operator+(const Vector3i &p_v) const;
  74. _FORCE_INLINE_ Vector3i &operator-=(const Vector3i &p_v);
  75. _FORCE_INLINE_ Vector3i operator-(const Vector3i &p_v) const;
  76. _FORCE_INLINE_ Vector3i &operator*=(const Vector3i &p_v);
  77. _FORCE_INLINE_ Vector3i operator*(const Vector3i &p_v) const;
  78. _FORCE_INLINE_ Vector3i &operator/=(const Vector3i &p_v);
  79. _FORCE_INLINE_ Vector3i operator/(const Vector3i &p_v) const;
  80. _FORCE_INLINE_ Vector3i &operator%=(const Vector3i &p_v);
  81. _FORCE_INLINE_ Vector3i operator%(const Vector3i &p_v) const;
  82. _FORCE_INLINE_ Vector3i &operator*=(const int32_t p_scalar);
  83. _FORCE_INLINE_ Vector3i operator*(const int32_t p_scalar) const;
  84. _FORCE_INLINE_ Vector3i &operator/=(const int32_t p_scalar);
  85. _FORCE_INLINE_ Vector3i operator/(const int32_t p_scalar) const;
  86. _FORCE_INLINE_ Vector3i &operator%=(const int32_t p_scalar);
  87. _FORCE_INLINE_ Vector3i operator%(const int32_t p_scalar) const;
  88. _FORCE_INLINE_ Vector3i operator-() 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. _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. operator String() const;
  96. operator Vector3() const;
  97. _FORCE_INLINE_ Vector3i() {}
  98. _FORCE_INLINE_ Vector3i(const int32_t p_x, const int32_t p_y, const int32_t p_z) {
  99. x = p_x;
  100. y = p_y;
  101. z = p_z;
  102. }
  103. };
  104. int64_t Vector3i::length_squared() const {
  105. return x * (int64_t)x + y * (int64_t)y + z * (int64_t)z;
  106. }
  107. double Vector3i::length() const {
  108. return Math::sqrt((double)length_squared());
  109. }
  110. Vector3i Vector3i::abs() const {
  111. return Vector3i(Math::abs(x), Math::abs(y), Math::abs(z));
  112. }
  113. Vector3i Vector3i::sign() const {
  114. return Vector3i(SIGN(x), SIGN(y), SIGN(z));
  115. }
  116. /* Operators */
  117. Vector3i &Vector3i::operator+=(const Vector3i &p_v) {
  118. x += p_v.x;
  119. y += p_v.y;
  120. z += p_v.z;
  121. return *this;
  122. }
  123. Vector3i Vector3i::operator+(const Vector3i &p_v) const {
  124. return Vector3i(x + p_v.x, y + p_v.y, z + p_v.z);
  125. }
  126. Vector3i &Vector3i::operator-=(const Vector3i &p_v) {
  127. x -= p_v.x;
  128. y -= p_v.y;
  129. z -= p_v.z;
  130. return *this;
  131. }
  132. Vector3i Vector3i::operator-(const Vector3i &p_v) const {
  133. return Vector3i(x - p_v.x, y - p_v.y, z - p_v.z);
  134. }
  135. Vector3i &Vector3i::operator*=(const Vector3i &p_v) {
  136. x *= p_v.x;
  137. y *= p_v.y;
  138. z *= p_v.z;
  139. return *this;
  140. }
  141. Vector3i Vector3i::operator*(const Vector3i &p_v) const {
  142. return Vector3i(x * p_v.x, y * p_v.y, z * p_v.z);
  143. }
  144. Vector3i &Vector3i::operator/=(const Vector3i &p_v) {
  145. x /= p_v.x;
  146. y /= p_v.y;
  147. z /= p_v.z;
  148. return *this;
  149. }
  150. Vector3i Vector3i::operator/(const Vector3i &p_v) const {
  151. return Vector3i(x / p_v.x, y / p_v.y, z / p_v.z);
  152. }
  153. Vector3i &Vector3i::operator%=(const Vector3i &p_v) {
  154. x %= p_v.x;
  155. y %= p_v.y;
  156. z %= p_v.z;
  157. return *this;
  158. }
  159. Vector3i Vector3i::operator%(const Vector3i &p_v) const {
  160. return Vector3i(x % p_v.x, y % p_v.y, z % p_v.z);
  161. }
  162. Vector3i &Vector3i::operator*=(const int32_t p_scalar) {
  163. x *= p_scalar;
  164. y *= p_scalar;
  165. z *= p_scalar;
  166. return *this;
  167. }
  168. Vector3i Vector3i::operator*(const int32_t p_scalar) const {
  169. return Vector3i(x * p_scalar, y * p_scalar, z * p_scalar);
  170. }
  171. // Multiplication operators required to workaround issues with LLVM using implicit conversion.
  172. _FORCE_INLINE_ Vector3i operator*(const int32_t p_scalar, const Vector3i &p_vector) {
  173. return p_vector * p_scalar;
  174. }
  175. _FORCE_INLINE_ Vector3i operator*(const int64_t p_scalar, const Vector3i &p_vector) {
  176. return p_vector * p_scalar;
  177. }
  178. _FORCE_INLINE_ Vector3i operator*(const float p_scalar, const Vector3i &p_vector) {
  179. return p_vector * p_scalar;
  180. }
  181. _FORCE_INLINE_ Vector3i operator*(const double p_scalar, const Vector3i &p_vector) {
  182. return p_vector * p_scalar;
  183. }
  184. Vector3i &Vector3i::operator/=(const int32_t p_scalar) {
  185. x /= p_scalar;
  186. y /= p_scalar;
  187. z /= p_scalar;
  188. return *this;
  189. }
  190. Vector3i Vector3i::operator/(const int32_t p_scalar) const {
  191. return Vector3i(x / p_scalar, y / p_scalar, z / p_scalar);
  192. }
  193. Vector3i &Vector3i::operator%=(const int32_t p_scalar) {
  194. x %= p_scalar;
  195. y %= p_scalar;
  196. z %= p_scalar;
  197. return *this;
  198. }
  199. Vector3i Vector3i::operator%(const int32_t p_scalar) const {
  200. return Vector3i(x % p_scalar, y % p_scalar, z % p_scalar);
  201. }
  202. Vector3i Vector3i::operator-() const {
  203. return Vector3i(-x, -y, -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. return (x != p_v.x || y != p_v.y || z != p_v.z);
  210. }
  211. bool Vector3i::operator<(const Vector3i &p_v) const {
  212. if (x == p_v.x) {
  213. if (y == p_v.y) {
  214. return z < p_v.z;
  215. } else {
  216. return y < p_v.y;
  217. }
  218. } else {
  219. return x < p_v.x;
  220. }
  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. void Vector3i::zero() {
  256. x = y = z = 0;
  257. }
  258. } // namespace godot
  259. #endif // GODOT_VECTOR3I_HPP