basis.h 11 KB

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