basis.hpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. /*************************************************************************/
  2. /* basis.hpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 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 GODOT_BASIS_HPP
  31. #define GODOT_BASIS_HPP
  32. #include <godot_cpp/core/math.hpp>
  33. #include <godot_cpp/variant/quaternion.hpp>
  34. #include <godot_cpp/variant/vector3.hpp>
  35. namespace godot {
  36. class Basis {
  37. _FORCE_INLINE_ GDNativeTypePtr _native_ptr() const { return (void *)this; }
  38. friend class Variant;
  39. public:
  40. Vector3 rows[3] = {
  41. Vector3(1, 0, 0),
  42. Vector3(0, 1, 0),
  43. Vector3(0, 0, 1)
  44. };
  45. inline const Vector3 &operator[](int axis) const {
  46. return rows[axis];
  47. }
  48. inline Vector3 &operator[](int axis) {
  49. return rows[axis];
  50. }
  51. void invert();
  52. void transpose();
  53. Basis inverse() const;
  54. Basis transposed() const;
  55. inline real_t determinant() const;
  56. void from_z(const Vector3 &p_z);
  57. inline Vector3 get_axis(int p_axis) const {
  58. // get actual basis axis (rows is transposed for performance)
  59. return Vector3(rows[0][p_axis], rows[1][p_axis], rows[2][p_axis]);
  60. }
  61. inline void set_axis(int p_axis, const Vector3 &p_value) {
  62. // get actual basis axis (rows is transposed for performance)
  63. rows[0][p_axis] = p_value.x;
  64. rows[1][p_axis] = p_value.y;
  65. rows[2][p_axis] = p_value.z;
  66. }
  67. void rotate(const Vector3 &p_axis, real_t p_phi);
  68. Basis rotated(const Vector3 &p_axis, real_t p_phi) const;
  69. void rotate_local(const Vector3 &p_axis, real_t p_phi);
  70. Basis rotated_local(const Vector3 &p_axis, real_t p_phi) const;
  71. void rotate(const Vector3 &p_euler);
  72. Basis rotated(const Vector3 &p_euler) const;
  73. void rotate(const Quaternion &p_quat);
  74. Basis rotated(const Quaternion &p_quat) const;
  75. Vector3 get_rotation_euler() const;
  76. void get_rotation_axis_angle(Vector3 &p_axis, real_t &p_angle) const;
  77. void get_rotation_axis_angle_local(Vector3 &p_axis, real_t &p_angle) const;
  78. Quaternion get_rotation_quaternion() const;
  79. Vector3 get_rotation() const { return get_rotation_euler(); };
  80. Vector3 rotref_posscale_decomposition(Basis &rotref) const;
  81. Vector3 get_euler_xyz() const;
  82. void set_euler_xyz(const Vector3 &p_euler);
  83. Vector3 get_euler_xzy() const;
  84. void set_euler_xzy(const Vector3 &p_euler);
  85. Vector3 get_euler_yzx() const;
  86. void set_euler_yzx(const Vector3 &p_euler);
  87. Vector3 get_euler_yxz() const;
  88. void set_euler_yxz(const Vector3 &p_euler);
  89. Vector3 get_euler_zxy() const;
  90. void set_euler_zxy(const Vector3 &p_euler);
  91. Vector3 get_euler_zyx() const;
  92. void set_euler_zyx(const Vector3 &p_euler);
  93. Quaternion get_quaternion() const;
  94. void set_quaternion(const Quaternion &p_quat);
  95. Vector3 get_euler() const { return get_euler_yxz(); }
  96. void set_euler(const Vector3 &p_euler) { set_euler_yxz(p_euler); }
  97. void get_axis_angle(Vector3 &r_axis, real_t &r_angle) const;
  98. void set_axis_angle(const Vector3 &p_axis, real_t p_phi);
  99. void scale(const Vector3 &p_scale);
  100. Basis scaled(const Vector3 &p_scale) const;
  101. void scale_local(const Vector3 &p_scale);
  102. Basis scaled_local(const Vector3 &p_scale) const;
  103. void make_scale_uniform();
  104. float get_uniform_scale() const;
  105. Vector3 get_scale() const;
  106. Vector3 get_scale_abs() const;
  107. Vector3 get_scale_local() const;
  108. void set_axis_angle_scale(const Vector3 &p_axis, real_t p_phi, const Vector3 &p_scale);
  109. void set_euler_scale(const Vector3 &p_euler, const Vector3 &p_scale);
  110. void set_quaternion_scale(const Quaternion &p_quat, const Vector3 &p_scale);
  111. // transposed dot products
  112. inline real_t tdotx(const Vector3 &v) const {
  113. return rows[0][0] * v[0] + rows[1][0] * v[1] + rows[2][0] * v[2];
  114. }
  115. inline real_t tdoty(const Vector3 &v) const {
  116. return rows[0][1] * v[0] + rows[1][1] * v[1] + rows[2][1] * v[2];
  117. }
  118. inline real_t tdotz(const Vector3 &v) const {
  119. return rows[0][2] * v[0] + rows[1][2] * v[1] + rows[2][2] * v[2];
  120. }
  121. bool is_equal_approx(const Basis &p_basis) const;
  122. bool operator==(const Basis &p_matrix) const;
  123. bool operator!=(const Basis &p_matrix) const;
  124. inline Vector3 xform(const Vector3 &p_vector) const;
  125. inline Vector3 xform_inv(const Vector3 &p_vector) const;
  126. inline void operator*=(const Basis &p_matrix);
  127. inline Basis operator*(const Basis &p_matrix) const;
  128. inline void operator+=(const Basis &p_matrix);
  129. inline Basis operator+(const Basis &p_matrix) const;
  130. inline void operator-=(const Basis &p_matrix);
  131. inline Basis operator-(const Basis &p_matrix) const;
  132. inline void operator*=(real_t p_val);
  133. inline Basis operator*(real_t p_val) const;
  134. int get_orthogonal_index() const;
  135. void set_orthogonal_index(int p_index);
  136. void set_diagonal(const Vector3 &p_diag);
  137. bool is_orthogonal() const;
  138. bool is_diagonal() const;
  139. bool is_rotation() const;
  140. Basis slerp(const Basis &p_to, const real_t &p_weight) const;
  141. void rotate_sh(real_t *p_values);
  142. operator String() const;
  143. /* create / set */
  144. inline void set(real_t xx, real_t xy, real_t xz, real_t yx, real_t yy, real_t yz, real_t zx, real_t zy, real_t zz) {
  145. rows[0][0] = xx;
  146. rows[0][1] = xy;
  147. rows[0][2] = xz;
  148. rows[1][0] = yx;
  149. rows[1][1] = yy;
  150. rows[1][2] = yz;
  151. rows[2][0] = zx;
  152. rows[2][1] = zy;
  153. rows[2][2] = zz;
  154. }
  155. inline void set(const Vector3 &p_x, const Vector3 &p_y, const Vector3 &p_z) {
  156. set_axis(0, p_x);
  157. set_axis(1, p_y);
  158. set_axis(2, p_z);
  159. }
  160. inline Vector3 get_column(int i) const {
  161. return Vector3(rows[0][i], rows[1][i], rows[2][i]);
  162. }
  163. inline Vector3 get_row(int i) const {
  164. return Vector3(rows[i][0], rows[i][1], rows[i][2]);
  165. }
  166. inline Vector3 get_main_diagonal() const {
  167. return Vector3(rows[0][0], rows[1][1], rows[2][2]);
  168. }
  169. inline void set_row(int i, const Vector3 &p_row) {
  170. rows[i][0] = p_row.x;
  171. rows[i][1] = p_row.y;
  172. rows[i][2] = p_row.z;
  173. }
  174. inline void set_zero() {
  175. rows[0].zero();
  176. rows[1].zero();
  177. rows[2].zero();
  178. }
  179. inline Basis transpose_xform(const Basis &m) const {
  180. return Basis(
  181. rows[0].x * m[0].x + rows[1].x * m[1].x + rows[2].x * m[2].x,
  182. rows[0].x * m[0].y + rows[1].x * m[1].y + rows[2].x * m[2].y,
  183. rows[0].x * m[0].z + rows[1].x * m[1].z + rows[2].x * m[2].z,
  184. rows[0].y * m[0].x + rows[1].y * m[1].x + rows[2].y * m[2].x,
  185. rows[0].y * m[0].y + rows[1].y * m[1].y + rows[2].y * m[2].y,
  186. rows[0].y * m[0].z + rows[1].y * m[1].z + rows[2].y * m[2].z,
  187. rows[0].z * m[0].x + rows[1].z * m[1].x + rows[2].z * m[2].x,
  188. rows[0].z * m[0].y + rows[1].z * m[1].y + rows[2].z * m[2].y,
  189. rows[0].z * m[0].z + rows[1].z * m[1].z + rows[2].z * m[2].z);
  190. }
  191. Basis(real_t xx, real_t xy, real_t xz, real_t yx, real_t yy, real_t yz, real_t zx, real_t zy, real_t zz) {
  192. set(xx, xy, xz, yx, yy, yz, zx, zy, zz);
  193. }
  194. void orthonormalize();
  195. Basis orthonormalized() const;
  196. #ifdef MATH_CHECKS
  197. bool is_symmetric() const;
  198. #endif
  199. Basis diagonalize();
  200. operator Quaternion() const { return get_quaternion(); }
  201. Basis(const Quaternion &p_quat) { set_quaternion(p_quat); };
  202. Basis(const Quaternion &p_quat, const Vector3 &p_scale) { set_quaternion_scale(p_quat, p_scale); }
  203. Basis(const Vector3 &p_euler) { set_euler(p_euler); }
  204. Basis(const Vector3 &p_euler, const Vector3 &p_scale) { set_euler_scale(p_euler, p_scale); }
  205. Basis(const Vector3 &p_axis, real_t p_phi) { set_axis_angle(p_axis, p_phi); }
  206. Basis(const Vector3 &p_axis, real_t p_phi, const Vector3 &p_scale) { set_axis_angle_scale(p_axis, p_phi, p_scale); }
  207. inline Basis(const Vector3 &p_x, const Vector3 &p_y, const Vector3 &p_z) {
  208. set_axis(0, p_x);
  209. set_axis(1, p_y);
  210. set_axis(2, p_z);
  211. }
  212. inline Basis() {}
  213. };
  214. inline void Basis::operator*=(const Basis &p_matrix) {
  215. set(
  216. p_matrix.tdotx(rows[0]), p_matrix.tdoty(rows[0]), p_matrix.tdotz(rows[0]),
  217. p_matrix.tdotx(rows[1]), p_matrix.tdoty(rows[1]), p_matrix.tdotz(rows[1]),
  218. p_matrix.tdotx(rows[2]), p_matrix.tdoty(rows[2]), p_matrix.tdotz(rows[2]));
  219. }
  220. inline Basis Basis::operator*(const Basis &p_matrix) const {
  221. return Basis(
  222. p_matrix.tdotx(rows[0]), p_matrix.tdoty(rows[0]), p_matrix.tdotz(rows[0]),
  223. p_matrix.tdotx(rows[1]), p_matrix.tdoty(rows[1]), p_matrix.tdotz(rows[1]),
  224. p_matrix.tdotx(rows[2]), p_matrix.tdoty(rows[2]), p_matrix.tdotz(rows[2]));
  225. }
  226. inline void Basis::operator+=(const Basis &p_matrix) {
  227. rows[0] += p_matrix.rows[0];
  228. rows[1] += p_matrix.rows[1];
  229. rows[2] += p_matrix.rows[2];
  230. }
  231. inline Basis Basis::operator+(const Basis &p_matrix) const {
  232. Basis ret(*this);
  233. ret += p_matrix;
  234. return ret;
  235. }
  236. inline void Basis::operator-=(const Basis &p_matrix) {
  237. rows[0] -= p_matrix.rows[0];
  238. rows[1] -= p_matrix.rows[1];
  239. rows[2] -= p_matrix.rows[2];
  240. }
  241. inline Basis Basis::operator-(const Basis &p_matrix) const {
  242. Basis ret(*this);
  243. ret -= p_matrix;
  244. return ret;
  245. }
  246. inline void Basis::operator*=(real_t p_val) {
  247. rows[0] *= p_val;
  248. rows[1] *= p_val;
  249. rows[2] *= p_val;
  250. }
  251. inline Basis Basis::operator*(real_t p_val) const {
  252. Basis ret(*this);
  253. ret *= p_val;
  254. return ret;
  255. }
  256. Vector3 Basis::xform(const Vector3 &p_vector) const {
  257. return Vector3(
  258. rows[0].dot(p_vector),
  259. rows[1].dot(p_vector),
  260. rows[2].dot(p_vector));
  261. }
  262. Vector3 Basis::xform_inv(const Vector3 &p_vector) const {
  263. return Vector3(
  264. (rows[0][0] * p_vector.x) + (rows[1][0] * p_vector.y) + (rows[2][0] * p_vector.z),
  265. (rows[0][1] * p_vector.x) + (rows[1][1] * p_vector.y) + (rows[2][1] * p_vector.z),
  266. (rows[0][2] * p_vector.x) + (rows[1][2] * p_vector.y) + (rows[2][2] * p_vector.z));
  267. }
  268. real_t Basis::determinant() const {
  269. return rows[0][0] * (rows[1][1] * rows[2][2] - rows[2][1] * rows[1][2]) -
  270. rows[1][0] * (rows[0][1] * rows[2][2] - rows[2][1] * rows[0][2]) +
  271. rows[2][0] * (rows[0][1] * rows[1][2] - rows[1][1] * rows[0][2]);
  272. }
  273. } // namespace godot
  274. #endif // GODOT_BASIS_HPP