basis.hpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. /**************************************************************************/
  2. /* basis.hpp */
  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. #pragma once
  31. #include <godot_cpp/classes/global_constants.hpp>
  32. #include <godot_cpp/variant/quaternion.hpp>
  33. #include <godot_cpp/variant/vector3.hpp>
  34. namespace godot {
  35. struct [[nodiscard]] Basis {
  36. Vector3 rows[3] = {
  37. Vector3(1, 0, 0),
  38. Vector3(0, 1, 0),
  39. Vector3(0, 0, 1)
  40. };
  41. _FORCE_INLINE_ const Vector3 &operator[](int p_row) const {
  42. return rows[p_row];
  43. }
  44. _FORCE_INLINE_ Vector3 &operator[](int p_row) {
  45. return rows[p_row];
  46. }
  47. void invert();
  48. void transpose();
  49. Basis inverse() const;
  50. Basis transposed() const;
  51. _FORCE_INLINE_ real_t determinant() const;
  52. void rotate(const Vector3 &p_axis, real_t p_angle);
  53. Basis rotated(const Vector3 &p_axis, real_t p_angle) const;
  54. void rotate_local(const Vector3 &p_axis, real_t p_angle);
  55. Basis rotated_local(const Vector3 &p_axis, real_t p_angle) const;
  56. void rotate(const Vector3 &p_euler, EulerOrder p_order = EulerOrder::EULER_ORDER_YXZ);
  57. Basis rotated(const Vector3 &p_euler, EulerOrder p_order = EulerOrder::EULER_ORDER_YXZ) const;
  58. void rotate(const Quaternion &p_quaternion);
  59. Basis rotated(const Quaternion &p_quaternion) const;
  60. Vector3 get_euler_normalized(EulerOrder p_order = EulerOrder::EULER_ORDER_YXZ) const;
  61. void get_rotation_axis_angle(Vector3 &p_axis, real_t &p_angle) const;
  62. void get_rotation_axis_angle_local(Vector3 &p_axis, real_t &p_angle) const;
  63. Quaternion get_rotation_quaternion() const;
  64. void rotate_to_align(Vector3 p_start_direction, Vector3 p_end_direction);
  65. Vector3 rotref_posscale_decomposition(Basis &rotref) const;
  66. Vector3 get_euler(EulerOrder p_order = EulerOrder::EULER_ORDER_YXZ) const;
  67. void set_euler(const Vector3 &p_euler, EulerOrder p_order = EulerOrder::EULER_ORDER_YXZ);
  68. static Basis from_euler(const Vector3 &p_euler, EulerOrder p_order = EulerOrder::EULER_ORDER_YXZ) {
  69. Basis b;
  70. b.set_euler(p_euler, p_order);
  71. return b;
  72. }
  73. Quaternion get_quaternion() const;
  74. void set_quaternion(const Quaternion &p_quaternion);
  75. void get_axis_angle(Vector3 &r_axis, real_t &r_angle) const;
  76. void set_axis_angle(const Vector3 &p_axis, real_t p_angle);
  77. void scale(const Vector3 &p_scale);
  78. Basis scaled(const Vector3 &p_scale) const;
  79. void scale_local(const Vector3 &p_scale);
  80. Basis scaled_local(const Vector3 &p_scale) const;
  81. void scale_orthogonal(const Vector3 &p_scale);
  82. Basis scaled_orthogonal(const Vector3 &p_scale) const;
  83. real_t get_uniform_scale() const;
  84. Vector3 get_scale() const;
  85. Vector3 get_scale_abs() const;
  86. Vector3 get_scale_global() const;
  87. void set_axis_angle_scale(const Vector3 &p_axis, real_t p_angle, const Vector3 &p_scale);
  88. void set_euler_scale(const Vector3 &p_euler, const Vector3 &p_scale, EulerOrder p_order = EulerOrder::EULER_ORDER_YXZ);
  89. void set_quaternion_scale(const Quaternion &p_quaternion, const Vector3 &p_scale);
  90. // transposed dot products
  91. _FORCE_INLINE_ real_t tdotx(const Vector3 &p_v) const {
  92. return rows[0][0] * p_v[0] + rows[1][0] * p_v[1] + rows[2][0] * p_v[2];
  93. }
  94. _FORCE_INLINE_ real_t tdoty(const Vector3 &p_v) const {
  95. return rows[0][1] * p_v[0] + rows[1][1] * p_v[1] + rows[2][1] * p_v[2];
  96. }
  97. _FORCE_INLINE_ real_t tdotz(const Vector3 &p_v) const {
  98. return rows[0][2] * p_v[0] + rows[1][2] * p_v[1] + rows[2][2] * p_v[2];
  99. }
  100. bool is_equal_approx(const Basis &p_basis) const;
  101. bool is_finite() const;
  102. bool operator==(const Basis &p_matrix) const;
  103. bool operator!=(const Basis &p_matrix) const;
  104. _FORCE_INLINE_ Vector3 xform(const Vector3 &p_vector) const;
  105. _FORCE_INLINE_ Vector3 xform_inv(const Vector3 &p_vector) const;
  106. _FORCE_INLINE_ void operator*=(const Basis &p_matrix);
  107. _FORCE_INLINE_ Basis operator*(const Basis &p_matrix) const;
  108. _FORCE_INLINE_ void operator+=(const Basis &p_matrix);
  109. _FORCE_INLINE_ Basis operator+(const Basis &p_matrix) const;
  110. _FORCE_INLINE_ void operator-=(const Basis &p_matrix);
  111. _FORCE_INLINE_ Basis operator-(const Basis &p_matrix) const;
  112. _FORCE_INLINE_ void operator*=(real_t p_val);
  113. _FORCE_INLINE_ Basis operator*(real_t p_val) const;
  114. _FORCE_INLINE_ void operator/=(real_t p_val);
  115. _FORCE_INLINE_ Basis operator/(real_t p_val) const;
  116. bool is_orthogonal() const;
  117. bool is_orthonormal() const;
  118. bool is_conformal() const;
  119. bool is_diagonal() const;
  120. bool is_rotation() const;
  121. Basis lerp(const Basis &p_to, real_t p_weight) const;
  122. Basis slerp(const Basis &p_to, real_t p_weight) const;
  123. void rotate_sh(real_t *p_values);
  124. operator String() const;
  125. /* create / set */
  126. _FORCE_INLINE_ void set(real_t p_xx, real_t p_xy, real_t p_xz, real_t p_yx, real_t p_yy, real_t p_yz, real_t p_zx, real_t p_zy, real_t p_zz) {
  127. rows[0][0] = p_xx;
  128. rows[0][1] = p_xy;
  129. rows[0][2] = p_xz;
  130. rows[1][0] = p_yx;
  131. rows[1][1] = p_yy;
  132. rows[1][2] = p_yz;
  133. rows[2][0] = p_zx;
  134. rows[2][1] = p_zy;
  135. rows[2][2] = p_zz;
  136. }
  137. _FORCE_INLINE_ void set_columns(const Vector3 &p_x, const Vector3 &p_y, const Vector3 &p_z) {
  138. set_column(0, p_x);
  139. set_column(1, p_y);
  140. set_column(2, p_z);
  141. }
  142. _FORCE_INLINE_ Vector3 get_column(int p_index) const {
  143. // Get actual basis axis column (we store transposed as rows for performance).
  144. return Vector3(rows[0][p_index], rows[1][p_index], rows[2][p_index]);
  145. }
  146. _FORCE_INLINE_ void set_column(int p_index, const Vector3 &p_value) {
  147. // Set actual basis axis column (we store transposed as rows for performance).
  148. rows[0][p_index] = p_value.x;
  149. rows[1][p_index] = p_value.y;
  150. rows[2][p_index] = p_value.z;
  151. }
  152. _FORCE_INLINE_ Vector3 get_main_diagonal() const {
  153. return Vector3(rows[0][0], rows[1][1], rows[2][2]);
  154. }
  155. _FORCE_INLINE_ void set_zero() {
  156. rows[0].zero();
  157. rows[1].zero();
  158. rows[2].zero();
  159. }
  160. _FORCE_INLINE_ Basis transpose_xform(const Basis &p_m) const {
  161. return Basis(
  162. rows[0].x * p_m[0].x + rows[1].x * p_m[1].x + rows[2].x * p_m[2].x,
  163. rows[0].x * p_m[0].y + rows[1].x * p_m[1].y + rows[2].x * p_m[2].y,
  164. rows[0].x * p_m[0].z + rows[1].x * p_m[1].z + rows[2].x * p_m[2].z,
  165. rows[0].y * p_m[0].x + rows[1].y * p_m[1].x + rows[2].y * p_m[2].x,
  166. rows[0].y * p_m[0].y + rows[1].y * p_m[1].y + rows[2].y * p_m[2].y,
  167. rows[0].y * p_m[0].z + rows[1].y * p_m[1].z + rows[2].y * p_m[2].z,
  168. rows[0].z * p_m[0].x + rows[1].z * p_m[1].x + rows[2].z * p_m[2].x,
  169. rows[0].z * p_m[0].y + rows[1].z * p_m[1].y + rows[2].z * p_m[2].y,
  170. rows[0].z * p_m[0].z + rows[1].z * p_m[1].z + rows[2].z * p_m[2].z);
  171. }
  172. Basis(real_t p_xx, real_t p_xy, real_t p_xz, real_t p_yx, real_t p_yy, real_t p_yz, real_t p_zx, real_t p_zy, real_t p_zz) {
  173. set(p_xx, p_xy, p_xz, p_yx, p_yy, p_yz, p_zx, p_zy, p_zz);
  174. }
  175. void orthonormalize();
  176. Basis orthonormalized() const;
  177. void orthogonalize();
  178. Basis orthogonalized() const;
  179. #ifdef MATH_CHECKS
  180. bool is_symmetric() const;
  181. #endif
  182. Basis diagonalize();
  183. operator Quaternion() const { return get_quaternion(); }
  184. static Basis looking_at(const Vector3 &p_target, const Vector3 &p_up = Vector3(0, 1, 0), bool p_use_model_front = false);
  185. Basis(const Quaternion &p_quaternion) { set_quaternion(p_quaternion); }
  186. Basis(const Quaternion &p_quaternion, const Vector3 &p_scale) { set_quaternion_scale(p_quaternion, p_scale); }
  187. Basis(const Vector3 &p_axis, real_t p_angle) { set_axis_angle(p_axis, p_angle); }
  188. Basis(const Vector3 &p_axis, real_t p_angle, const Vector3 &p_scale) { set_axis_angle_scale(p_axis, p_angle, p_scale); }
  189. static Basis from_scale(const Vector3 &p_scale);
  190. _FORCE_INLINE_ Basis(const Vector3 &p_x_axis, const Vector3 &p_y_axis, const Vector3 &p_z_axis) {
  191. set_columns(p_x_axis, p_y_axis, p_z_axis);
  192. }
  193. _FORCE_INLINE_ Basis() {}
  194. private:
  195. // Helper method.
  196. void _set_diagonal(const Vector3 &p_diag);
  197. };
  198. _FORCE_INLINE_ void Basis::operator*=(const Basis &p_matrix) {
  199. set(
  200. p_matrix.tdotx(rows[0]), p_matrix.tdoty(rows[0]), p_matrix.tdotz(rows[0]),
  201. p_matrix.tdotx(rows[1]), p_matrix.tdoty(rows[1]), p_matrix.tdotz(rows[1]),
  202. p_matrix.tdotx(rows[2]), p_matrix.tdoty(rows[2]), p_matrix.tdotz(rows[2]));
  203. }
  204. _FORCE_INLINE_ Basis Basis::operator*(const Basis &p_matrix) const {
  205. return Basis(
  206. p_matrix.tdotx(rows[0]), p_matrix.tdoty(rows[0]), p_matrix.tdotz(rows[0]),
  207. p_matrix.tdotx(rows[1]), p_matrix.tdoty(rows[1]), p_matrix.tdotz(rows[1]),
  208. p_matrix.tdotx(rows[2]), p_matrix.tdoty(rows[2]), p_matrix.tdotz(rows[2]));
  209. }
  210. _FORCE_INLINE_ void Basis::operator+=(const Basis &p_matrix) {
  211. rows[0] += p_matrix.rows[0];
  212. rows[1] += p_matrix.rows[1];
  213. rows[2] += p_matrix.rows[2];
  214. }
  215. _FORCE_INLINE_ Basis Basis::operator+(const Basis &p_matrix) const {
  216. Basis ret(*this);
  217. ret += p_matrix;
  218. return ret;
  219. }
  220. _FORCE_INLINE_ void Basis::operator-=(const Basis &p_matrix) {
  221. rows[0] -= p_matrix.rows[0];
  222. rows[1] -= p_matrix.rows[1];
  223. rows[2] -= p_matrix.rows[2];
  224. }
  225. _FORCE_INLINE_ Basis Basis::operator-(const Basis &p_matrix) const {
  226. Basis ret(*this);
  227. ret -= p_matrix;
  228. return ret;
  229. }
  230. _FORCE_INLINE_ void Basis::operator*=(real_t p_val) {
  231. rows[0] *= p_val;
  232. rows[1] *= p_val;
  233. rows[2] *= p_val;
  234. }
  235. _FORCE_INLINE_ Basis Basis::operator*(real_t p_val) const {
  236. Basis ret(*this);
  237. ret *= p_val;
  238. return ret;
  239. }
  240. _FORCE_INLINE_ void Basis::operator/=(real_t p_val) {
  241. rows[0] /= p_val;
  242. rows[1] /= p_val;
  243. rows[2] /= p_val;
  244. }
  245. _FORCE_INLINE_ Basis Basis::operator/(real_t p_val) const {
  246. Basis ret(*this);
  247. ret /= p_val;
  248. return ret;
  249. }
  250. Vector3 Basis::xform(const Vector3 &p_vector) const {
  251. return Vector3(
  252. rows[0].dot(p_vector),
  253. rows[1].dot(p_vector),
  254. rows[2].dot(p_vector));
  255. }
  256. Vector3 Basis::xform_inv(const Vector3 &p_vector) const {
  257. return Vector3(
  258. (rows[0][0] * p_vector.x) + (rows[1][0] * p_vector.y) + (rows[2][0] * p_vector.z),
  259. (rows[0][1] * p_vector.x) + (rows[1][1] * p_vector.y) + (rows[2][1] * p_vector.z),
  260. (rows[0][2] * p_vector.x) + (rows[1][2] * p_vector.y) + (rows[2][2] * p_vector.z));
  261. }
  262. real_t Basis::determinant() const {
  263. return rows[0][0] * (rows[1][1] * rows[2][2] - rows[2][1] * rows[1][2]) -
  264. rows[1][0] * (rows[0][1] * rows[2][2] - rows[2][1] * rows[0][2]) +
  265. rows[2][0] * (rows[0][1] * rows[1][2] - rows[1][1] * rows[0][2]);
  266. }
  267. } // namespace godot