vector3i.h 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. /**************************************************************************/
  2. /* vector3i.h */
  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 VECTOR3I_H
  31. #define VECTOR3I_H
  32. #include "core/error/error_macros.h"
  33. #include "core/math/math_funcs.h"
  34. class String;
  35. struct Vector3;
  36. struct _NO_DISCARD_ 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[](const int p_axis) const {
  52. DEV_ASSERT((unsigned int)p_axis < 3);
  53. return coord[p_axis];
  54. }
  55. _FORCE_INLINE_ int32_t &operator[](const 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 max(const Vector3i &p_vector3i) const {
  65. return Vector3i(MAX(x, p_vector3i.x), MAX(y, p_vector3i.y), MAX(z, p_vector3i.z));
  66. }
  67. _FORCE_INLINE_ int64_t length_squared() const;
  68. _FORCE_INLINE_ double length() const;
  69. _FORCE_INLINE_ void zero();
  70. _FORCE_INLINE_ Vector3i abs() const;
  71. _FORCE_INLINE_ Vector3i sign() const;
  72. Vector3i clamp(const Vector3i &p_min, const Vector3i &p_max) const;
  73. Vector3i snapped(const Vector3i &p_step) const;
  74. /* Operators */
  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 Vector3i &p_v);
  84. _FORCE_INLINE_ Vector3i operator%(const Vector3i &p_v) 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 int32_t p_scalar);
  90. _FORCE_INLINE_ Vector3i operator%(const int32_t p_scalar) const;
  91. _FORCE_INLINE_ Vector3i operator-() 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. _FORCE_INLINE_ bool operator>(const Vector3i &p_v) const;
  97. _FORCE_INLINE_ bool operator>=(const Vector3i &p_v) const;
  98. operator String() const;
  99. operator Vector3() const;
  100. _FORCE_INLINE_ Vector3i() {}
  101. _FORCE_INLINE_ Vector3i(const int32_t p_x, const int32_t p_y, const int32_t p_z) {
  102. x = p_x;
  103. y = p_y;
  104. z = p_z;
  105. }
  106. };
  107. int64_t Vector3i::length_squared() const {
  108. return x * (int64_t)x + y * (int64_t)y + z * (int64_t)z;
  109. }
  110. double Vector3i::length() const {
  111. return Math::sqrt((double)length_squared());
  112. }
  113. Vector3i Vector3i::abs() const {
  114. return Vector3i(Math::abs(x), Math::abs(y), Math::abs(z));
  115. }
  116. Vector3i Vector3i::sign() const {
  117. return Vector3i(SIGN(x), SIGN(y), SIGN(z));
  118. }
  119. /* Operators */
  120. Vector3i &Vector3i::operator+=(const Vector3i &p_v) {
  121. x += p_v.x;
  122. y += p_v.y;
  123. z += p_v.z;
  124. return *this;
  125. }
  126. Vector3i Vector3i::operator+(const Vector3i &p_v) const {
  127. return Vector3i(x + p_v.x, y + p_v.y, z + p_v.z);
  128. }
  129. Vector3i &Vector3i::operator-=(const Vector3i &p_v) {
  130. x -= p_v.x;
  131. y -= p_v.y;
  132. z -= p_v.z;
  133. return *this;
  134. }
  135. Vector3i Vector3i::operator-(const Vector3i &p_v) const {
  136. return Vector3i(x - p_v.x, y - p_v.y, z - p_v.z);
  137. }
  138. Vector3i &Vector3i::operator*=(const Vector3i &p_v) {
  139. x *= p_v.x;
  140. y *= p_v.y;
  141. z *= p_v.z;
  142. return *this;
  143. }
  144. Vector3i Vector3i::operator*(const Vector3i &p_v) const {
  145. return Vector3i(x * p_v.x, y * p_v.y, z * p_v.z);
  146. }
  147. Vector3i &Vector3i::operator/=(const Vector3i &p_v) {
  148. x /= p_v.x;
  149. y /= p_v.y;
  150. z /= p_v.z;
  151. return *this;
  152. }
  153. Vector3i Vector3i::operator/(const Vector3i &p_v) const {
  154. return Vector3i(x / p_v.x, y / p_v.y, z / p_v.z);
  155. }
  156. Vector3i &Vector3i::operator%=(const Vector3i &p_v) {
  157. x %= p_v.x;
  158. y %= p_v.y;
  159. z %= p_v.z;
  160. return *this;
  161. }
  162. Vector3i Vector3i::operator%(const Vector3i &p_v) const {
  163. return Vector3i(x % p_v.x, y % p_v.y, z % p_v.z);
  164. }
  165. Vector3i &Vector3i::operator*=(const int32_t p_scalar) {
  166. x *= p_scalar;
  167. y *= p_scalar;
  168. z *= p_scalar;
  169. return *this;
  170. }
  171. Vector3i Vector3i::operator*(const int32_t p_scalar) const {
  172. return Vector3i(x * p_scalar, y * p_scalar, z * p_scalar);
  173. }
  174. // Multiplication operators required to workaround issues with LLVM using implicit conversion.
  175. _FORCE_INLINE_ Vector3i operator*(const int32_t p_scalar, const Vector3i &p_vector) {
  176. return p_vector * p_scalar;
  177. }
  178. _FORCE_INLINE_ Vector3i operator*(const int64_t p_scalar, const Vector3i &p_vector) {
  179. return p_vector * p_scalar;
  180. }
  181. _FORCE_INLINE_ Vector3i operator*(const float p_scalar, const Vector3i &p_vector) {
  182. return p_vector * p_scalar;
  183. }
  184. _FORCE_INLINE_ Vector3i operator*(const double p_scalar, const Vector3i &p_vector) {
  185. return p_vector * p_scalar;
  186. }
  187. Vector3i &Vector3i::operator/=(const int32_t p_scalar) {
  188. x /= p_scalar;
  189. y /= p_scalar;
  190. z /= p_scalar;
  191. return *this;
  192. }
  193. Vector3i Vector3i::operator/(const int32_t p_scalar) const {
  194. return Vector3i(x / p_scalar, y / p_scalar, z / p_scalar);
  195. }
  196. Vector3i &Vector3i::operator%=(const int32_t p_scalar) {
  197. x %= p_scalar;
  198. y %= p_scalar;
  199. z %= p_scalar;
  200. return *this;
  201. }
  202. Vector3i Vector3i::operator%(const int32_t p_scalar) const {
  203. return Vector3i(x % p_scalar, y % p_scalar, z % p_scalar);
  204. }
  205. Vector3i Vector3i::operator-() const {
  206. return Vector3i(-x, -y, -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. return (x != p_v.x || y != p_v.y || z != p_v.z);
  213. }
  214. bool Vector3i::operator<(const Vector3i &p_v) const {
  215. if (x == p_v.x) {
  216. if (y == p_v.y) {
  217. return z < p_v.z;
  218. } else {
  219. return y < p_v.y;
  220. }
  221. } else {
  222. return x < p_v.x;
  223. }
  224. }
  225. bool Vector3i::operator>(const Vector3i &p_v) const {
  226. if (x == p_v.x) {
  227. if (y == p_v.y) {
  228. return z > p_v.z;
  229. } else {
  230. return y > p_v.y;
  231. }
  232. } else {
  233. return x > p_v.x;
  234. }
  235. }
  236. bool Vector3i::operator<=(const Vector3i &p_v) const {
  237. if (x == p_v.x) {
  238. if (y == p_v.y) {
  239. return z <= p_v.z;
  240. } else {
  241. return y < p_v.y;
  242. }
  243. } else {
  244. return x < p_v.x;
  245. }
  246. }
  247. bool Vector3i::operator>=(const Vector3i &p_v) const {
  248. if (x == p_v.x) {
  249. if (y == p_v.y) {
  250. return z >= p_v.z;
  251. } else {
  252. return y > p_v.y;
  253. }
  254. } else {
  255. return x > p_v.x;
  256. }
  257. }
  258. void Vector3i::zero() {
  259. x = y = z = 0;
  260. }
  261. #endif // VECTOR3I_H