vector3.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  1. /**************************************************************************/
  2. /* vector3.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 VECTOR3_H
  31. #define VECTOR3_H
  32. #include "core/math/math_funcs.h"
  33. #include "core/ustring.h"
  34. class Basis;
  35. struct _NO_DISCARD_CLASS_ Vector3 {
  36. static const int AXIS_COUNT = 3;
  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. DEV_ASSERT((unsigned int)p_axis < 3);
  52. return coord[p_axis];
  53. }
  54. _FORCE_INLINE_ real_t &operator[](int p_axis) {
  55. DEV_ASSERT((unsigned int)p_axis < 3);
  56. return coord[p_axis];
  57. }
  58. void set_axis(int p_axis, real_t p_value);
  59. real_t get_axis(int p_axis) const;
  60. _FORCE_INLINE_ void set_all(real_t p_value) {
  61. x = y = z = p_value;
  62. }
  63. _FORCE_INLINE_ int min_axis() const {
  64. return x < y ? (x < z ? 0 : 2) : (y < z ? 1 : 2);
  65. }
  66. _FORCE_INLINE_ int max_axis() const {
  67. return x < y ? (y < z ? 2 : 1) : (x < z ? 2 : 0);
  68. }
  69. _FORCE_INLINE_ real_t length() const;
  70. _FORCE_INLINE_ real_t length_squared() const;
  71. _FORCE_INLINE_ void normalize();
  72. _FORCE_INLINE_ Vector3 normalized() const;
  73. _FORCE_INLINE_ bool is_normalized() const;
  74. _FORCE_INLINE_ Vector3 inverse() const;
  75. Vector3 limit_length(real_t p_len = 1.0) const;
  76. _FORCE_INLINE_ void zero();
  77. void snap(const Vector3 &p_val);
  78. Vector3 snapped(const Vector3 &p_val) const;
  79. void rotate(const Vector3 &p_axis, real_t p_angle);
  80. Vector3 rotated(const Vector3 &p_axis, real_t p_angle) const;
  81. /* Static Methods between 2 vector3s */
  82. _FORCE_INLINE_ Vector3 linear_interpolate(const Vector3 &p_to, real_t p_weight) const;
  83. _FORCE_INLINE_ Vector3 slerp(const Vector3 &p_to, real_t p_weight) const;
  84. Vector3 cubic_interpolate(const Vector3 &p_b, const Vector3 &p_pre_a, const Vector3 &p_post_b, real_t p_weight) const;
  85. Vector3 cubic_interpolaten(const Vector3 &p_b, const Vector3 &p_pre_a, const Vector3 &p_post_b, real_t p_weight) const;
  86. Vector3 move_toward(const Vector3 &p_to, real_t p_delta) const;
  87. _FORCE_INLINE_ Vector3 cross(const Vector3 &p_b) const;
  88. _FORCE_INLINE_ real_t dot(const Vector3 &p_b) const;
  89. Basis outer(const Vector3 &p_b) const;
  90. Basis to_diagonal_matrix() const;
  91. _FORCE_INLINE_ Vector3 abs() const;
  92. _FORCE_INLINE_ Vector3 floor() const;
  93. _FORCE_INLINE_ Vector3 sign() const;
  94. _FORCE_INLINE_ Vector3 ceil() const;
  95. _FORCE_INLINE_ Vector3 round() const;
  96. _FORCE_INLINE_ real_t distance_to(const Vector3 &p_to) const;
  97. _FORCE_INLINE_ real_t distance_squared_to(const Vector3 &p_to) const;
  98. _FORCE_INLINE_ Vector3 posmod(real_t p_mod) const;
  99. _FORCE_INLINE_ Vector3 posmodv(const Vector3 &p_modv) const;
  100. _FORCE_INLINE_ Vector3 project(const Vector3 &p_to) const;
  101. _FORCE_INLINE_ real_t angle_to(const Vector3 &p_to) const;
  102. _FORCE_INLINE_ real_t signed_angle_to(const Vector3 &p_to, const Vector3 &p_axis) const;
  103. _FORCE_INLINE_ Vector3 direction_to(const Vector3 &p_to) const;
  104. _FORCE_INLINE_ Vector3 slide(const Vector3 &p_normal) const;
  105. _FORCE_INLINE_ Vector3 bounce(const Vector3 &p_normal) const;
  106. _FORCE_INLINE_ Vector3 reflect(const Vector3 &p_normal) const;
  107. bool is_equal_approx(const Vector3 &p_v) const;
  108. inline bool is_equal_approx(const Vector3 &p_v, real_t p_tolerance) const;
  109. bool is_zero_approx() const;
  110. /* Operators */
  111. _FORCE_INLINE_ Vector3 &operator+=(const Vector3 &p_v);
  112. _FORCE_INLINE_ Vector3 operator+(const Vector3 &p_v) const;
  113. _FORCE_INLINE_ Vector3 &operator-=(const Vector3 &p_v);
  114. _FORCE_INLINE_ Vector3 operator-(const Vector3 &p_v) const;
  115. _FORCE_INLINE_ Vector3 &operator*=(const Vector3 &p_v);
  116. _FORCE_INLINE_ Vector3 operator*(const Vector3 &p_v) const;
  117. _FORCE_INLINE_ Vector3 &operator/=(const Vector3 &p_v);
  118. _FORCE_INLINE_ Vector3 operator/(const Vector3 &p_v) const;
  119. _FORCE_INLINE_ Vector3 &operator*=(real_t p_scalar);
  120. _FORCE_INLINE_ Vector3 operator*(real_t p_scalar) const;
  121. _FORCE_INLINE_ Vector3 &operator/=(real_t p_scalar);
  122. _FORCE_INLINE_ Vector3 operator/(real_t p_scalar) const;
  123. _FORCE_INLINE_ Vector3 operator-() const;
  124. _FORCE_INLINE_ bool operator==(const Vector3 &p_v) const;
  125. _FORCE_INLINE_ bool operator!=(const Vector3 &p_v) const;
  126. _FORCE_INLINE_ bool operator<(const Vector3 &p_v) const;
  127. _FORCE_INLINE_ bool operator<=(const Vector3 &p_v) const;
  128. _FORCE_INLINE_ bool operator>(const Vector3 &p_v) const;
  129. _FORCE_INLINE_ bool operator>=(const Vector3 &p_v) const;
  130. operator String() const;
  131. _FORCE_INLINE_ Vector3(real_t p_x, real_t p_y, real_t p_z) {
  132. x = p_x;
  133. y = p_y;
  134. z = p_z;
  135. }
  136. _FORCE_INLINE_ Vector3() { x = y = z = 0; }
  137. };
  138. Vector3 Vector3::cross(const Vector3 &p_b) const {
  139. Vector3 ret(
  140. (y * p_b.z) - (z * p_b.y),
  141. (z * p_b.x) - (x * p_b.z),
  142. (x * p_b.y) - (y * p_b.x));
  143. return ret;
  144. }
  145. real_t Vector3::dot(const Vector3 &p_b) const {
  146. return x * p_b.x + y * p_b.y + z * p_b.z;
  147. }
  148. Vector3 Vector3::abs() const {
  149. return Vector3(Math::abs(x), Math::abs(y), Math::abs(z));
  150. }
  151. Vector3 Vector3::sign() const {
  152. return Vector3(SGN(x), SGN(y), SGN(z));
  153. }
  154. Vector3 Vector3::floor() const {
  155. return Vector3(Math::floor(x), Math::floor(y), Math::floor(z));
  156. }
  157. Vector3 Vector3::ceil() const {
  158. return Vector3(Math::ceil(x), Math::ceil(y), Math::ceil(z));
  159. }
  160. Vector3 Vector3::round() const {
  161. return Vector3(Math::round(x), Math::round(y), Math::round(z));
  162. }
  163. Vector3 Vector3::linear_interpolate(const Vector3 &p_to, real_t p_weight) const {
  164. return Vector3(
  165. x + (p_weight * (p_to.x - x)),
  166. y + (p_weight * (p_to.y - y)),
  167. z + (p_weight * (p_to.z - z)));
  168. }
  169. Vector3 Vector3::slerp(const Vector3 &p_to, real_t p_weight) const {
  170. real_t theta = angle_to(p_to);
  171. return rotated(cross(p_to).normalized(), theta * p_weight);
  172. }
  173. real_t Vector3::distance_to(const Vector3 &p_to) const {
  174. return (p_to - *this).length();
  175. }
  176. real_t Vector3::distance_squared_to(const Vector3 &p_to) const {
  177. return (p_to - *this).length_squared();
  178. }
  179. Vector3 Vector3::posmod(real_t p_mod) const {
  180. return Vector3(Math::fposmod(x, p_mod), Math::fposmod(y, p_mod), Math::fposmod(z, p_mod));
  181. }
  182. Vector3 Vector3::posmodv(const Vector3 &p_modv) const {
  183. return Vector3(Math::fposmod(x, p_modv.x), Math::fposmod(y, p_modv.y), Math::fposmod(z, p_modv.z));
  184. }
  185. Vector3 Vector3::project(const Vector3 &p_to) const {
  186. return p_to * (dot(p_to) / p_to.length_squared());
  187. }
  188. real_t Vector3::angle_to(const Vector3 &p_to) const {
  189. return Math::atan2(cross(p_to).length(), dot(p_to));
  190. }
  191. real_t Vector3::signed_angle_to(const Vector3 &p_to, const Vector3 &p_axis) const {
  192. Vector3 cross_to = cross(p_to);
  193. real_t unsigned_angle = Math::atan2(cross_to.length(), dot(p_to));
  194. real_t sign = cross_to.dot(p_axis);
  195. return (sign < 0) ? -unsigned_angle : unsigned_angle;
  196. }
  197. Vector3 Vector3::direction_to(const Vector3 &p_to) const {
  198. Vector3 ret(p_to.x - x, p_to.y - y, p_to.z - z);
  199. ret.normalize();
  200. return ret;
  201. }
  202. /* Operators */
  203. Vector3 &Vector3::operator+=(const Vector3 &p_v) {
  204. x += p_v.x;
  205. y += p_v.y;
  206. z += p_v.z;
  207. return *this;
  208. }
  209. Vector3 Vector3::operator+(const Vector3 &p_v) const {
  210. return Vector3(x + p_v.x, y + p_v.y, z + p_v.z);
  211. }
  212. Vector3 &Vector3::operator-=(const Vector3 &p_v) {
  213. x -= p_v.x;
  214. y -= p_v.y;
  215. z -= p_v.z;
  216. return *this;
  217. }
  218. Vector3 Vector3::operator-(const Vector3 &p_v) const {
  219. return Vector3(x - p_v.x, y - p_v.y, z - p_v.z);
  220. }
  221. Vector3 &Vector3::operator*=(const Vector3 &p_v) {
  222. x *= p_v.x;
  223. y *= p_v.y;
  224. z *= p_v.z;
  225. return *this;
  226. }
  227. Vector3 Vector3::operator*(const Vector3 &p_v) const {
  228. return Vector3(x * p_v.x, y * p_v.y, z * p_v.z);
  229. }
  230. Vector3 &Vector3::operator/=(const Vector3 &p_v) {
  231. x /= p_v.x;
  232. y /= p_v.y;
  233. z /= p_v.z;
  234. return *this;
  235. }
  236. Vector3 Vector3::operator/(const Vector3 &p_v) const {
  237. return Vector3(x / p_v.x, y / p_v.y, z / p_v.z);
  238. }
  239. Vector3 &Vector3::operator*=(real_t p_scalar) {
  240. x *= p_scalar;
  241. y *= p_scalar;
  242. z *= p_scalar;
  243. return *this;
  244. }
  245. _FORCE_INLINE_ Vector3 operator*(real_t p_scalar, const Vector3 &p_vec) {
  246. return p_vec * p_scalar;
  247. }
  248. Vector3 Vector3::operator*(real_t p_scalar) const {
  249. return Vector3(x * p_scalar, y * p_scalar, z * p_scalar);
  250. }
  251. Vector3 &Vector3::operator/=(real_t p_scalar) {
  252. x /= p_scalar;
  253. y /= p_scalar;
  254. z /= p_scalar;
  255. return *this;
  256. }
  257. Vector3 Vector3::operator/(real_t p_scalar) const {
  258. return Vector3(x / p_scalar, y / p_scalar, z / p_scalar);
  259. }
  260. Vector3 Vector3::operator-() const {
  261. return Vector3(-x, -y, -z);
  262. }
  263. bool Vector3::operator==(const Vector3 &p_v) const {
  264. return x == p_v.x && y == p_v.y && z == p_v.z;
  265. }
  266. bool Vector3::operator!=(const Vector3 &p_v) const {
  267. return x != p_v.x || y != p_v.y || z != p_v.z;
  268. }
  269. bool Vector3::operator<(const Vector3 &p_v) const {
  270. if (x == p_v.x) {
  271. if (y == p_v.y) {
  272. return z < p_v.z;
  273. } else {
  274. return y < p_v.y;
  275. }
  276. } else {
  277. return x < p_v.x;
  278. }
  279. }
  280. bool Vector3::operator>(const Vector3 &p_v) const {
  281. if (x == p_v.x) {
  282. if (y == p_v.y) {
  283. return z > p_v.z;
  284. } else {
  285. return y > p_v.y;
  286. }
  287. } else {
  288. return x > p_v.x;
  289. }
  290. }
  291. bool Vector3::operator<=(const Vector3 &p_v) const {
  292. if (x == p_v.x) {
  293. if (y == p_v.y) {
  294. return z <= p_v.z;
  295. } else {
  296. return y < p_v.y;
  297. }
  298. } else {
  299. return x < p_v.x;
  300. }
  301. }
  302. bool Vector3::operator>=(const Vector3 &p_v) const {
  303. if (x == p_v.x) {
  304. if (y == p_v.y) {
  305. return z >= p_v.z;
  306. } else {
  307. return y > p_v.y;
  308. }
  309. } else {
  310. return x > p_v.x;
  311. }
  312. }
  313. _FORCE_INLINE_ Vector3 vec3_cross(const Vector3 &p_a, const Vector3 &p_b) {
  314. return p_a.cross(p_b);
  315. }
  316. _FORCE_INLINE_ real_t vec3_dot(const Vector3 &p_a, const Vector3 &p_b) {
  317. return p_a.dot(p_b);
  318. }
  319. real_t Vector3::length() const {
  320. real_t x2 = x * x;
  321. real_t y2 = y * y;
  322. real_t z2 = z * z;
  323. return Math::sqrt(x2 + y2 + z2);
  324. }
  325. real_t Vector3::length_squared() const {
  326. real_t x2 = x * x;
  327. real_t y2 = y * y;
  328. real_t z2 = z * z;
  329. return x2 + y2 + z2;
  330. }
  331. void Vector3::normalize() {
  332. real_t lengthsq = length_squared();
  333. if (lengthsq == 0) {
  334. x = y = z = 0;
  335. } else {
  336. real_t length = Math::sqrt(lengthsq);
  337. x /= length;
  338. y /= length;
  339. z /= length;
  340. }
  341. }
  342. Vector3 Vector3::normalized() const {
  343. Vector3 v = *this;
  344. v.normalize();
  345. return v;
  346. }
  347. bool Vector3::is_normalized() const {
  348. // use length_squared() instead of length() to avoid sqrt(), makes it more stringent.
  349. return Math::is_equal_approx(length_squared(), 1, (real_t)UNIT_EPSILON);
  350. }
  351. Vector3 Vector3::inverse() const {
  352. return Vector3(1 / x, 1 / y, 1 / z);
  353. }
  354. void Vector3::zero() {
  355. x = y = z = 0;
  356. }
  357. // slide returns the component of the vector along the given plane, specified by its normal vector.
  358. Vector3 Vector3::slide(const Vector3 &p_normal) const {
  359. #ifdef MATH_CHECKS
  360. ERR_FAIL_COND_V_MSG(!p_normal.is_normalized(), Vector3(), "The normal Vector3 must be normalized.");
  361. #endif
  362. return *this - p_normal * this->dot(p_normal);
  363. }
  364. Vector3 Vector3::bounce(const Vector3 &p_normal) const {
  365. return -reflect(p_normal);
  366. }
  367. Vector3 Vector3::reflect(const Vector3 &p_normal) const {
  368. #ifdef MATH_CHECKS
  369. ERR_FAIL_COND_V_MSG(!p_normal.is_normalized(), Vector3(), "The normal Vector3 must be normalized.");
  370. #endif
  371. return 2 * p_normal * this->dot(p_normal) - *this;
  372. }
  373. bool Vector3::is_equal_approx(const Vector3 &p_v, real_t p_tolerance) const {
  374. return Math::is_equal_approx(x, p_v.x, p_tolerance) && Math::is_equal_approx(y, p_v.y, p_tolerance) && Math::is_equal_approx(z, p_v.z, p_tolerance);
  375. }
  376. #endif // VECTOR3_H