basis.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. /**************************************************************************/
  2. /* basis.h */
  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. #ifndef BASIS_H
  31. #define BASIS_H
  32. #include "core/math/quat.h"
  33. #include "core/math/vector3.h"
  34. class _NO_DISCARD_CLASS_ Basis {
  35. public:
  36. Vector3 elements[3] = {
  37. Vector3(1, 0, 0),
  38. Vector3(0, 1, 0),
  39. Vector3(0, 0, 1)
  40. };
  41. _FORCE_INLINE_ const Vector3 &operator[](int axis) const {
  42. return elements[axis];
  43. }
  44. _FORCE_INLINE_ Vector3 &operator[](int axis) {
  45. return elements[axis];
  46. }
  47. void invert();
  48. void transpose();
  49. Basis inverse() const;
  50. Basis transposed() const;
  51. _FORCE_INLINE_ real_t determinant() const;
  52. void from_z(const Vector3 &p_z);
  53. _FORCE_INLINE_ Vector3 get_axis(int p_axis) const {
  54. // get actual basis axis (elements is transposed for performance)
  55. return Vector3(elements[0][p_axis], elements[1][p_axis], elements[2][p_axis]);
  56. }
  57. _FORCE_INLINE_ void set_axis(int p_axis, const Vector3 &p_value) {
  58. // get actual basis axis (elements is transposed for performance)
  59. elements[0][p_axis] = p_value.x;
  60. elements[1][p_axis] = p_value.y;
  61. elements[2][p_axis] = p_value.z;
  62. }
  63. void rotate(const Vector3 &p_axis, real_t p_angle);
  64. Basis rotated(const Vector3 &p_axis, real_t p_angle) const;
  65. void rotate_local(const Vector3 &p_axis, real_t p_angle);
  66. Basis rotated_local(const Vector3 &p_axis, real_t p_angle) const;
  67. void rotate(const Vector3 &p_euler);
  68. Basis rotated(const Vector3 &p_euler) const;
  69. void rotate(const Quat &p_quat);
  70. Basis rotated(const Quat &p_quat) const;
  71. Vector3 get_rotation_euler() const;
  72. void get_rotation_axis_angle(Vector3 &p_axis, real_t &p_angle) const;
  73. void get_rotation_axis_angle_local(Vector3 &p_axis, real_t &p_angle) const;
  74. Quat get_rotation_quat() const;
  75. Vector3 get_rotation() const { return get_rotation_euler(); };
  76. Vector3 rotref_posscale_decomposition(Basis &rotref) const;
  77. Vector3 get_euler_xyz() const;
  78. void set_euler_xyz(const Vector3 &p_euler);
  79. Vector3 get_euler_xzy() const;
  80. void set_euler_xzy(const Vector3 &p_euler);
  81. Vector3 get_euler_yzx() const;
  82. void set_euler_yzx(const Vector3 &p_euler);
  83. Vector3 get_euler_yxz() const;
  84. void set_euler_yxz(const Vector3 &p_euler);
  85. Vector3 get_euler_zxy() const;
  86. void set_euler_zxy(const Vector3 &p_euler);
  87. Vector3 get_euler_zyx() const;
  88. void set_euler_zyx(const Vector3 &p_euler);
  89. Quat get_quat() const;
  90. void set_quat(const Quat &p_quat);
  91. Vector3 get_euler() const { return get_euler_yxz(); }
  92. void set_euler(const Vector3 &p_euler) { set_euler_yxz(p_euler); }
  93. void get_axis_angle(Vector3 &r_axis, real_t &r_angle) const;
  94. void set_axis_angle(const Vector3 &p_axis, real_t p_angle);
  95. void scale(const Vector3 &p_scale);
  96. Basis scaled(const Vector3 &p_scale) const;
  97. void scale_local(const Vector3 &p_scale);
  98. Basis scaled_local(const Vector3 &p_scale) const;
  99. Vector3 get_scale() const;
  100. Vector3 get_scale_abs() const;
  101. Vector3 get_scale_local() const;
  102. void set_axis_angle_scale(const Vector3 &p_axis, real_t p_angle, const Vector3 &p_scale);
  103. void set_euler_scale(const Vector3 &p_euler, const Vector3 &p_scale);
  104. void set_quat_scale(const Quat &p_quat, const Vector3 &p_scale);
  105. // transposed dot products
  106. _FORCE_INLINE_ real_t tdotx(const Vector3 &v) const {
  107. return elements[0][0] * v[0] + elements[1][0] * v[1] + elements[2][0] * v[2];
  108. }
  109. _FORCE_INLINE_ real_t tdoty(const Vector3 &v) const {
  110. return elements[0][1] * v[0] + elements[1][1] * v[1] + elements[2][1] * v[2];
  111. }
  112. _FORCE_INLINE_ real_t tdotz(const Vector3 &v) const {
  113. return elements[0][2] * v[0] + elements[1][2] * v[1] + elements[2][2] * v[2];
  114. }
  115. bool is_equal_approx(const Basis &p_basis) const;
  116. // For complicated reasons, the second argument is always discarded. See #45062.
  117. bool is_equal_approx(const Basis &a, const Basis &b) const { return is_equal_approx(a); }
  118. bool is_equal_approx_ratio(const Basis &a, const Basis &b, real_t p_epsilon = UNIT_EPSILON) const;
  119. bool operator==(const Basis &p_matrix) const;
  120. bool operator!=(const Basis &p_matrix) const;
  121. _FORCE_INLINE_ Vector3 xform(const Vector3 &p_vector) const;
  122. _FORCE_INLINE_ Vector3 xform_inv(const Vector3 &p_vector) 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 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*=(real_t p_val);
  130. _FORCE_INLINE_ Basis operator*(real_t p_val) const;
  131. int get_orthogonal_index() const;
  132. void set_orthogonal_index(int p_index);
  133. void set_diagonal(const Vector3 &p_diag);
  134. bool is_orthogonal() const;
  135. bool is_diagonal() const;
  136. bool is_rotation() const;
  137. Basis slerp(const Basis &p_to, const real_t &p_weight) const;
  138. _FORCE_INLINE_ Basis lerp(const Basis &p_to, const real_t &p_weight) const;
  139. operator String() const;
  140. /* create / set */
  141. _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) {
  142. elements[0][0] = xx;
  143. elements[0][1] = xy;
  144. elements[0][2] = xz;
  145. elements[1][0] = yx;
  146. elements[1][1] = yy;
  147. elements[1][2] = yz;
  148. elements[2][0] = zx;
  149. elements[2][1] = zy;
  150. elements[2][2] = zz;
  151. }
  152. _FORCE_INLINE_ void set(const Vector3 &p_x, const Vector3 &p_y, const Vector3 &p_z) {
  153. set_axis(0, p_x);
  154. set_axis(1, p_y);
  155. set_axis(2, p_z);
  156. }
  157. _FORCE_INLINE_ Vector3 get_column(int i) const {
  158. return Vector3(elements[0][i], elements[1][i], elements[2][i]);
  159. }
  160. _FORCE_INLINE_ Vector3 get_row(int i) const {
  161. return Vector3(elements[i][0], elements[i][1], elements[i][2]);
  162. }
  163. _FORCE_INLINE_ Vector3 get_main_diagonal() const {
  164. return Vector3(elements[0][0], elements[1][1], elements[2][2]);
  165. }
  166. _FORCE_INLINE_ void set_row(int i, const Vector3 &p_row) {
  167. elements[i][0] = p_row.x;
  168. elements[i][1] = p_row.y;
  169. elements[i][2] = p_row.z;
  170. }
  171. _FORCE_INLINE_ void set_zero() {
  172. elements[0].zero();
  173. elements[1].zero();
  174. elements[2].zero();
  175. }
  176. _FORCE_INLINE_ Basis transpose_xform(const Basis &m) const {
  177. return Basis(
  178. elements[0].x * m[0].x + elements[1].x * m[1].x + elements[2].x * m[2].x,
  179. elements[0].x * m[0].y + elements[1].x * m[1].y + elements[2].x * m[2].y,
  180. elements[0].x * m[0].z + elements[1].x * m[1].z + elements[2].x * m[2].z,
  181. elements[0].y * m[0].x + elements[1].y * m[1].x + elements[2].y * m[2].x,
  182. elements[0].y * m[0].y + elements[1].y * m[1].y + elements[2].y * m[2].y,
  183. elements[0].y * m[0].z + elements[1].y * m[1].z + elements[2].y * m[2].z,
  184. elements[0].z * m[0].x + elements[1].z * m[1].x + elements[2].z * m[2].x,
  185. elements[0].z * m[0].y + elements[1].z * m[1].y + elements[2].z * m[2].y,
  186. elements[0].z * m[0].z + elements[1].z * m[1].z + elements[2].z * m[2].z);
  187. }
  188. 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) {
  189. set(xx, xy, xz, yx, yy, yz, zx, zy, zz);
  190. }
  191. void orthonormalize();
  192. Basis orthonormalized() const;
  193. bool is_symmetric() const;
  194. Basis diagonalize();
  195. // The following normal xform functions are correct for non-uniform scales.
  196. // Use these two functions in combination to xform a series of normals.
  197. // First use get_normal_xform_basis() to precalculate the inverse transpose.
  198. // Then apply xform_normal_fast() multiple times using the inverse transpose basis.
  199. Basis get_normal_xform_basis() const { return inverse().transposed(); }
  200. // N.B. This only does a normal transform if the basis used is the inverse transpose!
  201. // Otherwise use xform_normal().
  202. Vector3 xform_normal_fast(const Vector3 &p_vector) const { return xform(p_vector).normalized(); }
  203. // This function does the above but for a single normal vector. It is considerably slower, so should usually
  204. // only be used in cases of single normals, or when the basis changes each time.
  205. Vector3 xform_normal(const Vector3 &p_vector) const { return get_normal_xform_basis().xform_normal_fast(p_vector); }
  206. operator Quat() const { return get_quat(); }
  207. Basis(const Quat &p_quat) { set_quat(p_quat); }
  208. Basis(const Quat &p_quat, const Vector3 &p_scale) { set_quat_scale(p_quat, p_scale); }
  209. Basis(const Vector3 &p_euler) { set_euler(p_euler); }
  210. Basis(const Vector3 &p_euler, const Vector3 &p_scale) { set_euler_scale(p_euler, p_scale); }
  211. Basis(const Vector3 &p_axis, real_t p_angle) { set_axis_angle(p_axis, p_angle); }
  212. Basis(const Vector3 &p_axis, real_t p_angle, const Vector3 &p_scale) { set_axis_angle_scale(p_axis, p_angle, p_scale); }
  213. _FORCE_INLINE_ Basis(const Vector3 &row0, const Vector3 &row1, const Vector3 &row2) {
  214. elements[0] = row0;
  215. elements[1] = row1;
  216. elements[2] = row2;
  217. }
  218. _FORCE_INLINE_ Basis() {}
  219. };
  220. _FORCE_INLINE_ void Basis::operator*=(const Basis &p_matrix) {
  221. set(
  222. p_matrix.tdotx(elements[0]), p_matrix.tdoty(elements[0]), p_matrix.tdotz(elements[0]),
  223. p_matrix.tdotx(elements[1]), p_matrix.tdoty(elements[1]), p_matrix.tdotz(elements[1]),
  224. p_matrix.tdotx(elements[2]), p_matrix.tdoty(elements[2]), p_matrix.tdotz(elements[2]));
  225. }
  226. _FORCE_INLINE_ Basis Basis::operator*(const Basis &p_matrix) const {
  227. return Basis(
  228. p_matrix.tdotx(elements[0]), p_matrix.tdoty(elements[0]), p_matrix.tdotz(elements[0]),
  229. p_matrix.tdotx(elements[1]), p_matrix.tdoty(elements[1]), p_matrix.tdotz(elements[1]),
  230. p_matrix.tdotx(elements[2]), p_matrix.tdoty(elements[2]), p_matrix.tdotz(elements[2]));
  231. }
  232. _FORCE_INLINE_ void Basis::operator+=(const Basis &p_matrix) {
  233. elements[0] += p_matrix.elements[0];
  234. elements[1] += p_matrix.elements[1];
  235. elements[2] += p_matrix.elements[2];
  236. }
  237. _FORCE_INLINE_ Basis Basis::operator+(const Basis &p_matrix) const {
  238. Basis ret(*this);
  239. ret += p_matrix;
  240. return ret;
  241. }
  242. _FORCE_INLINE_ void Basis::operator-=(const Basis &p_matrix) {
  243. elements[0] -= p_matrix.elements[0];
  244. elements[1] -= p_matrix.elements[1];
  245. elements[2] -= p_matrix.elements[2];
  246. }
  247. _FORCE_INLINE_ Basis Basis::operator-(const Basis &p_matrix) const {
  248. Basis ret(*this);
  249. ret -= p_matrix;
  250. return ret;
  251. }
  252. _FORCE_INLINE_ void Basis::operator*=(real_t p_val) {
  253. elements[0] *= p_val;
  254. elements[1] *= p_val;
  255. elements[2] *= p_val;
  256. }
  257. _FORCE_INLINE_ Basis Basis::operator*(real_t p_val) const {
  258. Basis ret(*this);
  259. ret *= p_val;
  260. return ret;
  261. }
  262. Vector3 Basis::xform(const Vector3 &p_vector) const {
  263. return Vector3(
  264. elements[0].dot(p_vector),
  265. elements[1].dot(p_vector),
  266. elements[2].dot(p_vector));
  267. }
  268. Vector3 Basis::xform_inv(const Vector3 &p_vector) const {
  269. return Vector3(
  270. (elements[0][0] * p_vector.x) + (elements[1][0] * p_vector.y) + (elements[2][0] * p_vector.z),
  271. (elements[0][1] * p_vector.x) + (elements[1][1] * p_vector.y) + (elements[2][1] * p_vector.z),
  272. (elements[0][2] * p_vector.x) + (elements[1][2] * p_vector.y) + (elements[2][2] * p_vector.z));
  273. }
  274. real_t Basis::determinant() const {
  275. return elements[0][0] * (elements[1][1] * elements[2][2] - elements[2][1] * elements[1][2]) -
  276. elements[1][0] * (elements[0][1] * elements[2][2] - elements[2][1] * elements[0][2]) +
  277. elements[2][0] * (elements[0][1] * elements[1][2] - elements[1][1] * elements[0][2]);
  278. }
  279. Basis Basis::lerp(const Basis &p_to, const real_t &p_weight) const {
  280. Basis b;
  281. b.elements[0] = elements[0].linear_interpolate(p_to.elements[0], p_weight);
  282. b.elements[1] = elements[1].linear_interpolate(p_to.elements[1], p_weight);
  283. b.elements[2] = elements[2].linear_interpolate(p_to.elements[2], p_weight);
  284. return b;
  285. }
  286. #endif // BASIS_H