vector3i.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. /*************************************************************************/
  2. /* vector3i.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 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 VECTOR3I_H
  31. #define VECTOR3I_H
  32. #include "core/string/ustring.h"
  33. #include "core/typedefs.h"
  34. struct Vector3i {
  35. enum Axis {
  36. AXIS_X,
  37. AXIS_Y,
  38. AXIS_Z,
  39. };
  40. union {
  41. struct {
  42. int32_t x;
  43. int32_t y;
  44. int32_t z;
  45. };
  46. int32_t coord[3] = { 0 };
  47. };
  48. _FORCE_INLINE_ const int32_t &operator[](int p_axis) const {
  49. return coord[p_axis];
  50. }
  51. _FORCE_INLINE_ int32_t &operator[](int p_axis) {
  52. return coord[p_axis];
  53. }
  54. void set_axis(int p_axis, int32_t p_value);
  55. int32_t get_axis(int p_axis) const;
  56. int min_axis() const;
  57. int max_axis() const;
  58. _FORCE_INLINE_ void zero();
  59. _FORCE_INLINE_ Vector3i abs() const;
  60. _FORCE_INLINE_ Vector3i sign() const;
  61. /* Operators */
  62. _FORCE_INLINE_ Vector3i &operator+=(const Vector3i &p_v);
  63. _FORCE_INLINE_ Vector3i operator+(const Vector3i &p_v) const;
  64. _FORCE_INLINE_ Vector3i &operator-=(const Vector3i &p_v);
  65. _FORCE_INLINE_ Vector3i operator-(const Vector3i &p_v) const;
  66. _FORCE_INLINE_ Vector3i &operator*=(const Vector3i &p_v);
  67. _FORCE_INLINE_ Vector3i operator*(const Vector3i &p_v) const;
  68. _FORCE_INLINE_ Vector3i &operator/=(const Vector3i &p_v);
  69. _FORCE_INLINE_ Vector3i operator/(const Vector3i &p_v) const;
  70. _FORCE_INLINE_ Vector3i &operator%=(const Vector3i &p_v);
  71. _FORCE_INLINE_ Vector3i operator%(const Vector3i &p_v) const;
  72. _FORCE_INLINE_ Vector3i &operator*=(int32_t p_scalar);
  73. _FORCE_INLINE_ Vector3i operator*(int32_t p_scalar) const;
  74. _FORCE_INLINE_ Vector3i &operator/=(int32_t p_scalar);
  75. _FORCE_INLINE_ Vector3i operator/(int32_t p_scalar) const;
  76. _FORCE_INLINE_ Vector3i &operator%=(int32_t p_scalar);
  77. _FORCE_INLINE_ Vector3i operator%(int32_t p_scalar) const;
  78. _FORCE_INLINE_ Vector3i operator-() const;
  79. _FORCE_INLINE_ bool operator==(const Vector3i &p_v) const;
  80. _FORCE_INLINE_ bool operator!=(const Vector3i &p_v) const;
  81. _FORCE_INLINE_ bool operator<(const Vector3i &p_v) const;
  82. _FORCE_INLINE_ bool operator<=(const Vector3i &p_v) const;
  83. _FORCE_INLINE_ bool operator>(const Vector3i &p_v) const;
  84. _FORCE_INLINE_ bool operator>=(const Vector3i &p_v) const;
  85. operator String() const;
  86. _FORCE_INLINE_ Vector3i() {}
  87. _FORCE_INLINE_ Vector3i(int32_t p_x, int32_t p_y, int32_t p_z) {
  88. x = p_x;
  89. y = p_y;
  90. z = p_z;
  91. }
  92. };
  93. Vector3i Vector3i::abs() const {
  94. return Vector3i(ABS(x), ABS(y), ABS(z));
  95. }
  96. Vector3i Vector3i::sign() const {
  97. return Vector3i(SGN(x), SGN(y), SGN(z));
  98. }
  99. /* Operators */
  100. Vector3i &Vector3i::operator+=(const Vector3i &p_v) {
  101. x += p_v.x;
  102. y += p_v.y;
  103. z += p_v.z;
  104. return *this;
  105. }
  106. Vector3i Vector3i::operator+(const Vector3i &p_v) const {
  107. return Vector3i(x + p_v.x, y + p_v.y, z + p_v.z);
  108. }
  109. Vector3i &Vector3i::operator-=(const Vector3i &p_v) {
  110. x -= p_v.x;
  111. y -= p_v.y;
  112. z -= p_v.z;
  113. return *this;
  114. }
  115. Vector3i Vector3i::operator-(const Vector3i &p_v) const {
  116. return Vector3i(x - p_v.x, y - p_v.y, z - p_v.z);
  117. }
  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*=(int32_t p_scalar) {
  146. x *= p_scalar;
  147. y *= p_scalar;
  148. z *= p_scalar;
  149. return *this;
  150. }
  151. _FORCE_INLINE_ Vector3i operator*(int32_t p_scalar, const Vector3i &p_vec) {
  152. return p_vec * p_scalar;
  153. }
  154. Vector3i Vector3i::operator*(int32_t p_scalar) const {
  155. return Vector3i(x * p_scalar, y * p_scalar, z * p_scalar);
  156. }
  157. Vector3i &Vector3i::operator/=(int32_t p_scalar) {
  158. x /= p_scalar;
  159. y /= p_scalar;
  160. z /= p_scalar;
  161. return *this;
  162. }
  163. Vector3i Vector3i::operator/(int32_t p_scalar) const {
  164. return Vector3i(x / p_scalar, y / p_scalar, z / p_scalar);
  165. }
  166. Vector3i &Vector3i::operator%=(int32_t p_scalar) {
  167. x %= p_scalar;
  168. y %= p_scalar;
  169. z %= p_scalar;
  170. return *this;
  171. }
  172. Vector3i Vector3i::operator%(int32_t p_scalar) const {
  173. return Vector3i(x % p_scalar, y % p_scalar, z % p_scalar);
  174. }
  175. Vector3i Vector3i::operator-() const {
  176. return Vector3i(-x, -y, -z);
  177. }
  178. bool Vector3i::operator==(const Vector3i &p_v) const {
  179. return (x == p_v.x && y == p_v.y && z == p_v.z);
  180. }
  181. bool Vector3i::operator!=(const Vector3i &p_v) const {
  182. return (x != p_v.x || y != p_v.y || z != p_v.z);
  183. }
  184. bool Vector3i::operator<(const Vector3i &p_v) const {
  185. if (x == p_v.x) {
  186. if (y == p_v.y) {
  187. return z < p_v.z;
  188. } else {
  189. return y < p_v.y;
  190. }
  191. } else {
  192. return x < p_v.x;
  193. }
  194. }
  195. bool Vector3i::operator>(const Vector3i &p_v) const {
  196. if (x == p_v.x) {
  197. if (y == p_v.y) {
  198. return z > p_v.z;
  199. } else {
  200. return y > p_v.y;
  201. }
  202. } else {
  203. return x > p_v.x;
  204. }
  205. }
  206. bool Vector3i::operator<=(const Vector3i &p_v) const {
  207. if (x == p_v.x) {
  208. if (y == p_v.y) {
  209. return z <= p_v.z;
  210. } else {
  211. return y < p_v.y;
  212. }
  213. } else {
  214. return x < p_v.x;
  215. }
  216. }
  217. bool Vector3i::operator>=(const Vector3i &p_v) const {
  218. if (x == p_v.x) {
  219. if (y == p_v.y) {
  220. return z >= p_v.z;
  221. } else {
  222. return y > p_v.y;
  223. }
  224. } else {
  225. return x > p_v.x;
  226. }
  227. }
  228. void Vector3i::zero() {
  229. x = y = z = 0;
  230. }
  231. #endif // VECTOR3I_H