vector3i.h 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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[](const int p_axis) const {
  49. return coord[p_axis];
  50. }
  51. _FORCE_INLINE_ int32_t &operator[](const int p_axis) {
  52. return coord[p_axis];
  53. }
  54. void set_axis(const int p_axis, const int32_t p_value);
  55. int32_t get_axis(const 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. Vector3i clamp(const Vector3i &p_min, const Vector3i &p_max) const;
  62. /* Operators */
  63. _FORCE_INLINE_ Vector3i &operator+=(const Vector3i &p_v);
  64. _FORCE_INLINE_ Vector3i operator+(const Vector3i &p_v) const;
  65. _FORCE_INLINE_ Vector3i &operator-=(const Vector3i &p_v);
  66. _FORCE_INLINE_ Vector3i operator-(const Vector3i &p_v) const;
  67. _FORCE_INLINE_ Vector3i &operator*=(const Vector3i &p_v);
  68. _FORCE_INLINE_ Vector3i operator*(const Vector3i &p_v) const;
  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 int32_t p_scalar);
  74. _FORCE_INLINE_ Vector3i operator*(const int32_t p_scalar) const;
  75. _FORCE_INLINE_ Vector3i &operator/=(const int32_t p_scalar);
  76. _FORCE_INLINE_ Vector3i operator/(const int32_t p_scalar) const;
  77. _FORCE_INLINE_ Vector3i &operator%=(const int32_t p_scalar);
  78. _FORCE_INLINE_ Vector3i operator%(const int32_t p_scalar) const;
  79. _FORCE_INLINE_ Vector3i operator-() 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. _FORCE_INLINE_ bool operator>=(const Vector3i &p_v) const;
  86. operator String() const;
  87. _FORCE_INLINE_ Vector3i() {}
  88. _FORCE_INLINE_ Vector3i(const int32_t p_x, const int32_t p_y, const int32_t p_z) {
  89. x = p_x;
  90. y = p_y;
  91. z = p_z;
  92. }
  93. };
  94. Vector3i Vector3i::abs() const {
  95. return Vector3i(ABS(x), ABS(y), ABS(z));
  96. }
  97. Vector3i Vector3i::sign() const {
  98. return Vector3i(SGN(x), SGN(y), SGN(z));
  99. }
  100. /* Operators */
  101. Vector3i &Vector3i::operator+=(const Vector3i &p_v) {
  102. x += p_v.x;
  103. y += p_v.y;
  104. z += p_v.z;
  105. return *this;
  106. }
  107. Vector3i Vector3i::operator+(const Vector3i &p_v) const {
  108. return Vector3i(x + p_v.x, y + p_v.y, z + p_v.z);
  109. }
  110. Vector3i &Vector3i::operator-=(const Vector3i &p_v) {
  111. x -= p_v.x;
  112. y -= p_v.y;
  113. z -= p_v.z;
  114. return *this;
  115. }
  116. Vector3i Vector3i::operator-(const Vector3i &p_v) const {
  117. return Vector3i(x - p_v.x, y - p_v.y, z - p_v.z);
  118. }
  119. Vector3i &Vector3i::operator*=(const Vector3i &p_v) {
  120. x *= p_v.x;
  121. y *= p_v.y;
  122. z *= p_v.z;
  123. return *this;
  124. }
  125. Vector3i Vector3i::operator*(const Vector3i &p_v) const {
  126. return Vector3i(x * p_v.x, y * p_v.y, z * p_v.z);
  127. }
  128. Vector3i &Vector3i::operator/=(const Vector3i &p_v) {
  129. x /= p_v.x;
  130. y /= p_v.y;
  131. z /= p_v.z;
  132. return *this;
  133. }
  134. Vector3i Vector3i::operator/(const Vector3i &p_v) const {
  135. return Vector3i(x / p_v.x, y / p_v.y, z / p_v.z);
  136. }
  137. Vector3i &Vector3i::operator%=(const Vector3i &p_v) {
  138. x %= p_v.x;
  139. y %= p_v.y;
  140. z %= p_v.z;
  141. return *this;
  142. }
  143. Vector3i Vector3i::operator%(const Vector3i &p_v) const {
  144. return Vector3i(x % p_v.x, y % p_v.y, z % p_v.z);
  145. }
  146. Vector3i &Vector3i::operator*=(const int32_t p_scalar) {
  147. x *= p_scalar;
  148. y *= p_scalar;
  149. z *= p_scalar;
  150. return *this;
  151. }
  152. _FORCE_INLINE_ Vector3i operator*(const int32_t p_scalar, const Vector3i &p_vector) {
  153. return p_vector * p_scalar;
  154. }
  155. _FORCE_INLINE_ Vector3i operator*(const int64_t p_scalar, const Vector3i &p_vector) {
  156. return p_vector * p_scalar;
  157. }
  158. _FORCE_INLINE_ Vector3i operator*(const float p_scalar, const Vector3i &p_vector) {
  159. return p_vector * p_scalar;
  160. }
  161. _FORCE_INLINE_ Vector3i operator*(const double p_scalar, const Vector3i &p_vector) {
  162. return p_vector * p_scalar;
  163. }
  164. Vector3i Vector3i::operator*(const int32_t p_scalar) const {
  165. return Vector3i(x * p_scalar, y * p_scalar, z * p_scalar);
  166. }
  167. Vector3i &Vector3i::operator/=(const int32_t p_scalar) {
  168. x /= p_scalar;
  169. y /= p_scalar;
  170. z /= p_scalar;
  171. return *this;
  172. }
  173. Vector3i Vector3i::operator/(const int32_t p_scalar) const {
  174. return Vector3i(x / p_scalar, y / p_scalar, z / p_scalar);
  175. }
  176. Vector3i &Vector3i::operator%=(const int32_t p_scalar) {
  177. x %= p_scalar;
  178. y %= p_scalar;
  179. z %= p_scalar;
  180. return *this;
  181. }
  182. Vector3i Vector3i::operator%(const int32_t p_scalar) const {
  183. return Vector3i(x % p_scalar, y % p_scalar, z % p_scalar);
  184. }
  185. Vector3i Vector3i::operator-() const {
  186. return Vector3i(-x, -y, -z);
  187. }
  188. bool Vector3i::operator==(const Vector3i &p_v) const {
  189. return (x == p_v.x && y == p_v.y && z == p_v.z);
  190. }
  191. bool Vector3i::operator!=(const Vector3i &p_v) const {
  192. return (x != p_v.x || y != p_v.y || z != p_v.z);
  193. }
  194. bool Vector3i::operator<(const Vector3i &p_v) const {
  195. if (x == p_v.x) {
  196. if (y == p_v.y) {
  197. return z < p_v.z;
  198. } else {
  199. return y < p_v.y;
  200. }
  201. } else {
  202. return x < p_v.x;
  203. }
  204. }
  205. bool Vector3i::operator>(const Vector3i &p_v) const {
  206. if (x == p_v.x) {
  207. if (y == p_v.y) {
  208. return z > p_v.z;
  209. } else {
  210. return y > p_v.y;
  211. }
  212. } else {
  213. return x > p_v.x;
  214. }
  215. }
  216. bool Vector3i::operator<=(const Vector3i &p_v) const {
  217. if (x == p_v.x) {
  218. if (y == p_v.y) {
  219. return z <= p_v.z;
  220. } else {
  221. return y < p_v.y;
  222. }
  223. } else {
  224. return x < p_v.x;
  225. }
  226. }
  227. bool Vector3i::operator>=(const Vector3i &p_v) const {
  228. if (x == p_v.x) {
  229. if (y == p_v.y) {
  230. return z >= p_v.z;
  231. } else {
  232. return y > p_v.y;
  233. }
  234. } else {
  235. return x > p_v.x;
  236. }
  237. }
  238. void Vector3i::zero() {
  239. x = y = z = 0;
  240. }
  241. #endif // VECTOR3I_H