2
0

basis.h 12 KB

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