vector3.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  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(const real_t p_len = 1.0) const;
  76. _FORCE_INLINE_ void zero();
  77. void snap(Vector3 p_val);
  78. Vector3 snapped(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, const 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(const 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. /* Operators */
  110. _FORCE_INLINE_ Vector3 &operator+=(const Vector3 &p_v);
  111. _FORCE_INLINE_ Vector3 operator+(const Vector3 &p_v) const;
  112. _FORCE_INLINE_ Vector3 &operator-=(const Vector3 &p_v);
  113. _FORCE_INLINE_ Vector3 operator-(const Vector3 &p_v) const;
  114. _FORCE_INLINE_ Vector3 &operator*=(const Vector3 &p_v);
  115. _FORCE_INLINE_ Vector3 operator*(const Vector3 &p_v) const;
  116. _FORCE_INLINE_ Vector3 &operator/=(const Vector3 &p_v);
  117. _FORCE_INLINE_ Vector3 operator/(const Vector3 &p_v) const;
  118. _FORCE_INLINE_ Vector3 &operator*=(real_t p_scalar);
  119. _FORCE_INLINE_ Vector3 operator*(real_t p_scalar) const;
  120. _FORCE_INLINE_ Vector3 &operator/=(real_t p_scalar);
  121. _FORCE_INLINE_ Vector3 operator/(real_t p_scalar) const;
  122. _FORCE_INLINE_ Vector3 operator-() const;
  123. _FORCE_INLINE_ bool operator==(const Vector3 &p_v) 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. operator String() const;
  130. _FORCE_INLINE_ Vector3(real_t p_x, real_t p_y, real_t p_z) {
  131. x = p_x;
  132. y = p_y;
  133. z = p_z;
  134. }
  135. _FORCE_INLINE_ Vector3() { x = y = z = 0; }
  136. };
  137. Vector3 Vector3::cross(const Vector3 &p_b) const {
  138. Vector3 ret(
  139. (y * p_b.z) - (z * p_b.y),
  140. (z * p_b.x) - (x * p_b.z),
  141. (x * p_b.y) - (y * p_b.x));
  142. return ret;
  143. }
  144. real_t Vector3::dot(const Vector3 &p_b) const {
  145. return x * p_b.x + y * p_b.y + z * p_b.z;
  146. }
  147. Vector3 Vector3::abs() const {
  148. return Vector3(Math::abs(x), Math::abs(y), Math::abs(z));
  149. }
  150. Vector3 Vector3::sign() const {
  151. return Vector3(SGN(x), SGN(y), SGN(z));
  152. }
  153. Vector3 Vector3::floor() const {
  154. return Vector3(Math::floor(x), Math::floor(y), Math::floor(z));
  155. }
  156. Vector3 Vector3::ceil() const {
  157. return Vector3(Math::ceil(x), Math::ceil(y), Math::ceil(z));
  158. }
  159. Vector3 Vector3::round() const {
  160. return Vector3(Math::round(x), Math::round(y), Math::round(z));
  161. }
  162. Vector3 Vector3::linear_interpolate(const Vector3 &p_to, real_t p_weight) const {
  163. return Vector3(
  164. x + (p_weight * (p_to.x - x)),
  165. y + (p_weight * (p_to.y - y)),
  166. z + (p_weight * (p_to.z - z)));
  167. }
  168. Vector3 Vector3::slerp(const Vector3 &p_to, real_t p_weight) const {
  169. real_t theta = angle_to(p_to);
  170. return rotated(cross(p_to).normalized(), theta * p_weight);
  171. }
  172. real_t Vector3::distance_to(const Vector3 &p_to) const {
  173. return (p_to - *this).length();
  174. }
  175. real_t Vector3::distance_squared_to(const Vector3 &p_to) const {
  176. return (p_to - *this).length_squared();
  177. }
  178. Vector3 Vector3::posmod(const real_t p_mod) const {
  179. return Vector3(Math::fposmod(x, p_mod), Math::fposmod(y, p_mod), Math::fposmod(z, p_mod));
  180. }
  181. Vector3 Vector3::posmodv(const Vector3 &p_modv) const {
  182. return Vector3(Math::fposmod(x, p_modv.x), Math::fposmod(y, p_modv.y), Math::fposmod(z, p_modv.z));
  183. }
  184. Vector3 Vector3::project(const Vector3 &p_to) const {
  185. return p_to * (dot(p_to) / p_to.length_squared());
  186. }
  187. real_t Vector3::angle_to(const Vector3 &p_to) const {
  188. return Math::atan2(cross(p_to).length(), dot(p_to));
  189. }
  190. real_t Vector3::signed_angle_to(const Vector3 &p_to, const Vector3 &p_axis) const {
  191. Vector3 cross_to = cross(p_to);
  192. real_t unsigned_angle = Math::atan2(cross_to.length(), dot(p_to));
  193. real_t sign = cross_to.dot(p_axis);
  194. return (sign < 0) ? -unsigned_angle : unsigned_angle;
  195. }
  196. Vector3 Vector3::direction_to(const Vector3 &p_to) const {
  197. Vector3 ret(p_to.x - x, p_to.y - y, p_to.z - z);
  198. ret.normalize();
  199. return ret;
  200. }
  201. /* Operators */
  202. Vector3 &Vector3::operator+=(const Vector3 &p_v) {
  203. x += p_v.x;
  204. y += p_v.y;
  205. z += p_v.z;
  206. return *this;
  207. }
  208. Vector3 Vector3::operator+(const Vector3 &p_v) const {
  209. return Vector3(x + p_v.x, y + p_v.y, z + p_v.z);
  210. }
  211. Vector3 &Vector3::operator-=(const Vector3 &p_v) {
  212. x -= p_v.x;
  213. y -= p_v.y;
  214. z -= p_v.z;
  215. return *this;
  216. }
  217. Vector3 Vector3::operator-(const Vector3 &p_v) const {
  218. return Vector3(x - p_v.x, y - p_v.y, z - p_v.z);
  219. }
  220. Vector3 &Vector3::operator*=(const Vector3 &p_v) {
  221. x *= p_v.x;
  222. y *= p_v.y;
  223. z *= p_v.z;
  224. return *this;
  225. }
  226. Vector3 Vector3::operator*(const Vector3 &p_v) const {
  227. return Vector3(x * p_v.x, y * p_v.y, z * p_v.z);
  228. }
  229. Vector3 &Vector3::operator/=(const Vector3 &p_v) {
  230. x /= p_v.x;
  231. y /= p_v.y;
  232. z /= p_v.z;
  233. return *this;
  234. }
  235. Vector3 Vector3::operator/(const Vector3 &p_v) const {
  236. return Vector3(x / p_v.x, y / p_v.y, z / p_v.z);
  237. }
  238. Vector3 &Vector3::operator*=(real_t p_scalar) {
  239. x *= p_scalar;
  240. y *= p_scalar;
  241. z *= p_scalar;
  242. return *this;
  243. }
  244. _FORCE_INLINE_ Vector3 operator*(real_t p_scalar, const Vector3 &p_vec) {
  245. return p_vec * p_scalar;
  246. }
  247. Vector3 Vector3::operator*(real_t p_scalar) const {
  248. return Vector3(x * p_scalar, y * p_scalar, z * p_scalar);
  249. }
  250. Vector3 &Vector3::operator/=(real_t p_scalar) {
  251. x /= p_scalar;
  252. y /= p_scalar;
  253. z /= p_scalar;
  254. return *this;
  255. }
  256. Vector3 Vector3::operator/(real_t p_scalar) const {
  257. return Vector3(x / p_scalar, y / p_scalar, z / p_scalar);
  258. }
  259. Vector3 Vector3::operator-() const {
  260. return Vector3(-x, -y, -z);
  261. }
  262. bool Vector3::operator==(const Vector3 &p_v) const {
  263. return x == p_v.x && y == p_v.y && z == p_v.z;
  264. }
  265. bool Vector3::operator!=(const Vector3 &p_v) const {
  266. return x != p_v.x || y != p_v.y || z != p_v.z;
  267. }
  268. bool Vector3::operator<(const Vector3 &p_v) const {
  269. if (x == p_v.x) {
  270. if (y == p_v.y) {
  271. return z < p_v.z;
  272. } else {
  273. return y < p_v.y;
  274. }
  275. } else {
  276. return x < p_v.x;
  277. }
  278. }
  279. bool Vector3::operator>(const Vector3 &p_v) const {
  280. if (x == p_v.x) {
  281. if (y == p_v.y) {
  282. return z > p_v.z;
  283. } else {
  284. return y > p_v.y;
  285. }
  286. } else {
  287. return x > p_v.x;
  288. }
  289. }
  290. bool Vector3::operator<=(const Vector3 &p_v) const {
  291. if (x == p_v.x) {
  292. if (y == p_v.y) {
  293. return z <= p_v.z;
  294. } else {
  295. return y < p_v.y;
  296. }
  297. } else {
  298. return x < p_v.x;
  299. }
  300. }
  301. bool Vector3::operator>=(const Vector3 &p_v) const {
  302. if (x == p_v.x) {
  303. if (y == p_v.y) {
  304. return z >= p_v.z;
  305. } else {
  306. return y > p_v.y;
  307. }
  308. } else {
  309. return x > p_v.x;
  310. }
  311. }
  312. _FORCE_INLINE_ Vector3 vec3_cross(const Vector3 &p_a, const Vector3 &p_b) {
  313. return p_a.cross(p_b);
  314. }
  315. _FORCE_INLINE_ real_t vec3_dot(const Vector3 &p_a, const Vector3 &p_b) {
  316. return p_a.dot(p_b);
  317. }
  318. real_t Vector3::length() const {
  319. real_t x2 = x * x;
  320. real_t y2 = y * y;
  321. real_t z2 = z * z;
  322. return Math::sqrt(x2 + y2 + z2);
  323. }
  324. real_t Vector3::length_squared() const {
  325. real_t x2 = x * x;
  326. real_t y2 = y * y;
  327. real_t z2 = z * z;
  328. return x2 + y2 + z2;
  329. }
  330. void Vector3::normalize() {
  331. real_t lengthsq = length_squared();
  332. if (lengthsq == 0) {
  333. x = y = z = 0;
  334. } else {
  335. real_t length = Math::sqrt(lengthsq);
  336. x /= length;
  337. y /= length;
  338. z /= length;
  339. }
  340. }
  341. Vector3 Vector3::normalized() const {
  342. Vector3 v = *this;
  343. v.normalize();
  344. return v;
  345. }
  346. bool Vector3::is_normalized() const {
  347. // use length_squared() instead of length() to avoid sqrt(), makes it more stringent.
  348. return Math::is_equal_approx(length_squared(), 1, (real_t)UNIT_EPSILON);
  349. }
  350. Vector3 Vector3::inverse() const {
  351. return Vector3(1 / x, 1 / y, 1 / z);
  352. }
  353. void Vector3::zero() {
  354. x = y = z = 0;
  355. }
  356. // slide returns the component of the vector along the given plane, specified by its normal vector.
  357. Vector3 Vector3::slide(const Vector3 &p_normal) const {
  358. #ifdef MATH_CHECKS
  359. ERR_FAIL_COND_V_MSG(!p_normal.is_normalized(), Vector3(), "The normal Vector3 must be normalized.");
  360. #endif
  361. return *this - p_normal * this->dot(p_normal);
  362. }
  363. Vector3 Vector3::bounce(const Vector3 &p_normal) const {
  364. return -reflect(p_normal);
  365. }
  366. Vector3 Vector3::reflect(const Vector3 &p_normal) const {
  367. #ifdef MATH_CHECKS
  368. ERR_FAIL_COND_V_MSG(!p_normal.is_normalized(), Vector3(), "The normal Vector3 must be normalized.");
  369. #endif
  370. return 2 * p_normal * this->dot(p_normal) - *this;
  371. }
  372. bool Vector3::is_equal_approx(const Vector3 &p_v, real_t p_tolerance) const {
  373. 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);
  374. }
  375. #endif // VECTOR3_H