basis.hpp 12 KB

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