vector3.h 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. /*************************************************************************/
  2. /* vector3.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 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 VECTOR3_H
  31. #define VECTOR3_H
  32. #include "math_defs.h"
  33. #include "math_funcs.h"
  34. #include "typedefs.h"
  35. #include "ustring.h"
  36. struct Vector3 {
  37. enum Axis {
  38. AXIS_X,
  39. AXIS_Y,
  40. AXIS_Z,
  41. };
  42. union {
  43. struct {
  44. real_t x;
  45. real_t y;
  46. real_t z;
  47. };
  48. real_t coord[3];
  49. };
  50. _FORCE_INLINE_ const real_t &operator[](int p_axis) const {
  51. return coord[p_axis];
  52. }
  53. _FORCE_INLINE_ real_t &operator[](int p_axis) {
  54. return coord[p_axis];
  55. }
  56. void set_axis(int p_axis, real_t p_value);
  57. real_t get_axis(int p_axis) const;
  58. int min_axis() const;
  59. int max_axis() const;
  60. _FORCE_INLINE_ real_t length() const;
  61. _FORCE_INLINE_ real_t length_squared() const;
  62. _FORCE_INLINE_ void normalize();
  63. _FORCE_INLINE_ Vector3 normalized() const;
  64. _FORCE_INLINE_ Vector3 inverse() const;
  65. _FORCE_INLINE_ void zero();
  66. void snap(float p_val);
  67. Vector3 snapped(float p_val) const;
  68. void rotate(const Vector3 &p_axis, float p_phi);
  69. Vector3 rotated(const Vector3 &p_axis, float p_phi) const;
  70. /* Static Methods between 2 vector3s */
  71. _FORCE_INLINE_ Vector3 linear_interpolate(const Vector3 &p_b, float p_t) const;
  72. Vector3 cubic_interpolate(const Vector3 &p_b, const Vector3 &p_pre_a, const Vector3 &p_post_b, float p_t) const;
  73. Vector3 cubic_interpolaten(const Vector3 &p_b, const Vector3 &p_pre_a, const Vector3 &p_post_b, float p_t) const;
  74. _FORCE_INLINE_ Vector3 cross(const Vector3 &p_b) const;
  75. _FORCE_INLINE_ real_t dot(const Vector3 &p_b) const;
  76. _FORCE_INLINE_ Vector3 abs() const;
  77. _FORCE_INLINE_ Vector3 floor() const;
  78. _FORCE_INLINE_ Vector3 ceil() const;
  79. _FORCE_INLINE_ real_t distance_to(const Vector3 &p_b) const;
  80. _FORCE_INLINE_ real_t distance_squared_to(const Vector3 &p_b) const;
  81. _FORCE_INLINE_ real_t angle_to(const Vector3 &p_b) const;
  82. _FORCE_INLINE_ Vector3 slide(const Vector3 &p_vec) const;
  83. _FORCE_INLINE_ Vector3 reflect(const Vector3 &p_vec) const;
  84. /* Operators */
  85. _FORCE_INLINE_ Vector3 &operator+=(const Vector3 &p_v);
  86. _FORCE_INLINE_ Vector3 operator+(const Vector3 &p_v) const;
  87. _FORCE_INLINE_ Vector3 &operator-=(const Vector3 &p_v);
  88. _FORCE_INLINE_ Vector3 operator-(const Vector3 &p_v) const;
  89. _FORCE_INLINE_ Vector3 &operator*=(const Vector3 &p_v);
  90. _FORCE_INLINE_ Vector3 operator*(const Vector3 &p_v) const;
  91. _FORCE_INLINE_ Vector3 &operator/=(const Vector3 &p_v);
  92. _FORCE_INLINE_ Vector3 operator/(const Vector3 &p_v) const;
  93. _FORCE_INLINE_ Vector3 &operator*=(real_t p_scalar);
  94. _FORCE_INLINE_ Vector3 operator*(real_t p_scalar) const;
  95. _FORCE_INLINE_ Vector3 &operator/=(real_t p_scalar);
  96. _FORCE_INLINE_ Vector3 operator/(real_t p_scalar) const;
  97. _FORCE_INLINE_ Vector3 operator-() const;
  98. _FORCE_INLINE_ bool operator==(const Vector3 &p_v) const;
  99. _FORCE_INLINE_ bool operator!=(const Vector3 &p_v) const;
  100. _FORCE_INLINE_ bool operator<(const Vector3 &p_v) const;
  101. _FORCE_INLINE_ bool operator<=(const Vector3 &p_v) const;
  102. operator String() const;
  103. _FORCE_INLINE_ Vector3() { x = y = z = 0; }
  104. _FORCE_INLINE_ Vector3(real_t p_x, real_t p_y, real_t p_z) {
  105. x = p_x;
  106. y = p_y;
  107. z = p_z;
  108. }
  109. };
  110. #ifdef VECTOR3_IMPL_OVERRIDE
  111. #include "vector3_inline.h"
  112. #else
  113. Vector3 Vector3::cross(const Vector3 &p_b) const {
  114. Vector3 ret(
  115. (y * p_b.z) - (z * p_b.y),
  116. (z * p_b.x) - (x * p_b.z),
  117. (x * p_b.y) - (y * p_b.x));
  118. return ret;
  119. }
  120. real_t Vector3::dot(const Vector3 &p_b) const {
  121. return x * p_b.x + y * p_b.y + z * p_b.z;
  122. }
  123. Vector3 Vector3::abs() const {
  124. return Vector3(Math::abs(x), Math::abs(y), Math::abs(z));
  125. }
  126. Vector3 Vector3::floor() const {
  127. return Vector3(Math::floor(x), Math::floor(y), Math::floor(z));
  128. }
  129. Vector3 Vector3::ceil() const {
  130. return Vector3(Math::ceil(x), Math::ceil(y), Math::ceil(z));
  131. }
  132. Vector3 Vector3::linear_interpolate(const Vector3 &p_b, float p_t) const {
  133. return Vector3(
  134. x + (p_t * (p_b.x - x)),
  135. y + (p_t * (p_b.y - y)),
  136. z + (p_t * (p_b.z - z)));
  137. }
  138. real_t Vector3::distance_to(const Vector3 &p_b) const {
  139. return (p_b - *this).length();
  140. }
  141. real_t Vector3::distance_squared_to(const Vector3 &p_b) const {
  142. return (p_b - *this).length_squared();
  143. }
  144. real_t Vector3::angle_to(const Vector3 &p_b) const {
  145. return Math::atan2(cross(p_b).length(), dot(p_b));
  146. }
  147. /* Operators */
  148. Vector3 &Vector3::operator+=(const Vector3 &p_v) {
  149. x += p_v.x;
  150. y += p_v.y;
  151. z += p_v.z;
  152. return *this;
  153. }
  154. Vector3 Vector3::operator+(const Vector3 &p_v) const {
  155. return Vector3(x + p_v.x, y + p_v.y, z + p_v.z);
  156. }
  157. Vector3 &Vector3::operator-=(const Vector3 &p_v) {
  158. x -= p_v.x;
  159. y -= p_v.y;
  160. z -= p_v.z;
  161. return *this;
  162. }
  163. Vector3 Vector3::operator-(const Vector3 &p_v) const {
  164. return Vector3(x - p_v.x, y - p_v.y, z - p_v.z);
  165. }
  166. Vector3 &Vector3::operator*=(const Vector3 &p_v) {
  167. x *= p_v.x;
  168. y *= p_v.y;
  169. z *= p_v.z;
  170. return *this;
  171. }
  172. Vector3 Vector3::operator*(const Vector3 &p_v) const {
  173. return Vector3(x * p_v.x, y * p_v.y, z * p_v.z);
  174. }
  175. Vector3 &Vector3::operator/=(const Vector3 &p_v) {
  176. x /= p_v.x;
  177. y /= p_v.y;
  178. z /= p_v.z;
  179. return *this;
  180. }
  181. Vector3 Vector3::operator/(const Vector3 &p_v) const {
  182. return Vector3(x / p_v.x, y / p_v.y, z / p_v.z);
  183. }
  184. Vector3 &Vector3::operator*=(real_t p_scalar) {
  185. x *= p_scalar;
  186. y *= p_scalar;
  187. z *= p_scalar;
  188. return *this;
  189. }
  190. _FORCE_INLINE_ Vector3 operator*(real_t p_scalar, const Vector3 &p_vec) {
  191. return p_vec * p_scalar;
  192. }
  193. Vector3 Vector3::operator*(real_t p_scalar) const {
  194. return Vector3(x * p_scalar, y * p_scalar, z * p_scalar);
  195. }
  196. Vector3 &Vector3::operator/=(real_t p_scalar) {
  197. x /= p_scalar;
  198. y /= p_scalar;
  199. z /= p_scalar;
  200. return *this;
  201. }
  202. Vector3 Vector3::operator/(real_t p_scalar) const {
  203. return Vector3(x / p_scalar, y / p_scalar, z / p_scalar);
  204. }
  205. Vector3 Vector3::operator-() const {
  206. return Vector3(-x, -y, -z);
  207. }
  208. bool Vector3::operator==(const Vector3 &p_v) const {
  209. return (x == p_v.x && y == p_v.y && z == p_v.z);
  210. }
  211. bool Vector3::operator!=(const Vector3 &p_v) const {
  212. return (x != p_v.x || y != p_v.y || z != p_v.z);
  213. }
  214. bool Vector3::operator<(const Vector3 &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. } else {
  221. return x < p_v.x;
  222. }
  223. }
  224. bool Vector3::operator<=(const Vector3 &p_v) const {
  225. if (x == p_v.x) {
  226. if (y == p_v.y)
  227. return z <= p_v.z;
  228. else
  229. return y < p_v.y;
  230. } else {
  231. return x < p_v.x;
  232. }
  233. }
  234. _FORCE_INLINE_ Vector3 vec3_cross(const Vector3 &p_a, const Vector3 &p_b) {
  235. return p_a.cross(p_b);
  236. }
  237. _FORCE_INLINE_ real_t vec3_dot(const Vector3 &p_a, const Vector3 &p_b) {
  238. return p_a.dot(p_b);
  239. }
  240. real_t Vector3::length() const {
  241. real_t x2 = x * x;
  242. real_t y2 = y * y;
  243. real_t z2 = z * z;
  244. return Math::sqrt(x2 + y2 + z2);
  245. }
  246. real_t Vector3::length_squared() const {
  247. real_t x2 = x * x;
  248. real_t y2 = y * y;
  249. real_t z2 = z * z;
  250. return x2 + y2 + z2;
  251. }
  252. void Vector3::normalize() {
  253. real_t l = length();
  254. if (l == 0) {
  255. x = y = z = 0;
  256. } else {
  257. x /= l;
  258. y /= l;
  259. z /= l;
  260. }
  261. }
  262. Vector3 Vector3::normalized() const {
  263. Vector3 v = *this;
  264. v.normalize();
  265. return v;
  266. }
  267. Vector3 Vector3::inverse() const {
  268. return Vector3(1.0 / x, 1.0 / y, 1.0 / z);
  269. }
  270. void Vector3::zero() {
  271. x = y = z = 0;
  272. }
  273. Vector3 Vector3::slide(const Vector3 &p_vec) const {
  274. return p_vec - *this * this->dot(p_vec);
  275. }
  276. Vector3 Vector3::reflect(const Vector3 &p_vec) const {
  277. return p_vec - *this * this->dot(p_vec) * 2.0;
  278. }
  279. #endif
  280. #endif // VECTOR3_H