matrix3.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. /*************************************************************************/
  2. /* matrix3.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 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. // Circular dependency between Vector3 and Basis :/
  31. #include "core/math/vector3.h"
  32. #ifndef MATRIX3_H
  33. #define MATRIX3_H
  34. #include "core/math/quat.h"
  35. /**
  36. @author Juan Linietsky <reduzio@gmail.com>
  37. */
  38. class Basis {
  39. public:
  40. Vector3 elements[3];
  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_phi);
  64. Basis rotated(const Vector3 &p_axis, real_t p_phi) const;
  65. void rotate_local(const Vector3 &p_axis, real_t p_phi);
  66. Basis rotated_local(const Vector3 &p_axis, real_t p_phi) 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_yxz() const;
  80. void set_euler_yxz(const Vector3 &p_euler);
  81. Quat get_quat() const;
  82. void set_quat(const Quat &p_quat);
  83. Vector3 get_euler() const { return get_euler_yxz(); }
  84. void set_euler(const Vector3 &p_euler) { set_euler_yxz(p_euler); }
  85. void get_axis_angle(Vector3 &r_axis, real_t &r_angle) const;
  86. void set_axis_angle(const Vector3 &p_axis, real_t p_phi);
  87. void scale(const Vector3 &p_scale);
  88. Basis scaled(const Vector3 &p_scale) const;
  89. void scale_local(const Vector3 &p_scale);
  90. Basis scaled_local(const Vector3 &p_scale) const;
  91. Vector3 get_scale() const;
  92. Vector3 get_scale_abs() const;
  93. Vector3 get_scale_local() const;
  94. void set_axis_angle_scale(const Vector3 &p_axis, real_t p_phi, const Vector3 &p_scale);
  95. void set_euler_scale(const Vector3 &p_euler, const Vector3 &p_scale);
  96. void set_quat_scale(const Quat &p_quat, const Vector3 &p_scale);
  97. // transposed dot products
  98. _FORCE_INLINE_ real_t tdotx(const Vector3 &v) const {
  99. return elements[0][0] * v[0] + elements[1][0] * v[1] + elements[2][0] * v[2];
  100. }
  101. _FORCE_INLINE_ real_t tdoty(const Vector3 &v) const {
  102. return elements[0][1] * v[0] + elements[1][1] * v[1] + elements[2][1] * v[2];
  103. }
  104. _FORCE_INLINE_ real_t tdotz(const Vector3 &v) const {
  105. return elements[0][2] * v[0] + elements[1][2] * v[1] + elements[2][2] * v[2];
  106. }
  107. bool is_equal_approx(const Basis &a, const Basis &b) const;
  108. bool operator==(const Basis &p_matrix) const;
  109. bool operator!=(const Basis &p_matrix) const;
  110. _FORCE_INLINE_ Vector3 xform(const Vector3 &p_vector) const;
  111. _FORCE_INLINE_ Vector3 xform_inv(const Vector3 &p_vector) const;
  112. _FORCE_INLINE_ void operator*=(const Basis &p_matrix);
  113. _FORCE_INLINE_ Basis operator*(const Basis &p_matrix) 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*=(real_t p_val);
  119. _FORCE_INLINE_ Basis operator*(real_t p_val) const;
  120. int get_orthogonal_index() const;
  121. void set_orthogonal_index(int p_index);
  122. void set_diagonal(const Vector3 p_diag);
  123. bool is_orthogonal() const;
  124. bool is_diagonal() const;
  125. bool is_rotation() const;
  126. Basis slerp(const Basis &target, const real_t &t) const;
  127. operator String() const;
  128. /* create / set */
  129. _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) {
  130. elements[0][0] = xx;
  131. elements[0][1] = xy;
  132. elements[0][2] = xz;
  133. elements[1][0] = yx;
  134. elements[1][1] = yy;
  135. elements[1][2] = yz;
  136. elements[2][0] = zx;
  137. elements[2][1] = zy;
  138. elements[2][2] = zz;
  139. }
  140. _FORCE_INLINE_ void set(const Vector3 &p_x, const Vector3 &p_y, const Vector3 &p_z) {
  141. set_axis(0, p_x);
  142. set_axis(1, p_y);
  143. set_axis(2, p_z);
  144. }
  145. _FORCE_INLINE_ Vector3 get_column(int i) const {
  146. return Vector3(elements[0][i], elements[1][i], elements[2][i]);
  147. }
  148. _FORCE_INLINE_ Vector3 get_row(int i) const {
  149. return Vector3(elements[i][0], elements[i][1], elements[i][2]);
  150. }
  151. _FORCE_INLINE_ Vector3 get_main_diagonal() const {
  152. return Vector3(elements[0][0], elements[1][1], elements[2][2]);
  153. }
  154. _FORCE_INLINE_ void set_row(int i, const Vector3 &p_row) {
  155. elements[i][0] = p_row.x;
  156. elements[i][1] = p_row.y;
  157. elements[i][2] = p_row.z;
  158. }
  159. _FORCE_INLINE_ void set_zero() {
  160. elements[0].zero();
  161. elements[1].zero();
  162. elements[2].zero();
  163. }
  164. _FORCE_INLINE_ Basis transpose_xform(const Basis &m) const {
  165. return Basis(
  166. elements[0].x * m[0].x + elements[1].x * m[1].x + elements[2].x * m[2].x,
  167. elements[0].x * m[0].y + elements[1].x * m[1].y + elements[2].x * m[2].y,
  168. elements[0].x * m[0].z + elements[1].x * m[1].z + elements[2].x * m[2].z,
  169. elements[0].y * m[0].x + elements[1].y * m[1].x + elements[2].y * m[2].x,
  170. elements[0].y * m[0].y + elements[1].y * m[1].y + elements[2].y * m[2].y,
  171. elements[0].y * m[0].z + elements[1].y * m[1].z + elements[2].y * m[2].z,
  172. elements[0].z * m[0].x + elements[1].z * m[1].x + elements[2].z * m[2].x,
  173. elements[0].z * m[0].y + elements[1].z * m[1].y + elements[2].z * m[2].y,
  174. elements[0].z * m[0].z + elements[1].z * m[1].z + elements[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. bool is_symmetric() const;
  182. Basis diagonalize();
  183. operator Quat() const { return get_quat(); }
  184. Basis(const Quat &p_quat) { set_quat(p_quat); };
  185. Basis(const Quat &p_quat, const Vector3 &p_scale) { set_quat_scale(p_quat, p_scale); }
  186. Basis(const Vector3 &p_euler) { set_euler(p_euler); }
  187. Basis(const Vector3 &p_euler, const Vector3 &p_scale) { set_euler_scale(p_euler, p_scale); }
  188. Basis(const Vector3 &p_axis, real_t p_phi) { set_axis_angle(p_axis, p_phi); }
  189. Basis(const Vector3 &p_axis, real_t p_phi, const Vector3 &p_scale) { set_axis_angle_scale(p_axis, p_phi, p_scale); }
  190. _FORCE_INLINE_ Basis(const Vector3 &row0, const Vector3 &row1, const Vector3 &row2) {
  191. elements[0] = row0;
  192. elements[1] = row1;
  193. elements[2] = row2;
  194. }
  195. _FORCE_INLINE_ Basis() {
  196. elements[0][0] = 1;
  197. elements[0][1] = 0;
  198. elements[0][2] = 0;
  199. elements[1][0] = 0;
  200. elements[1][1] = 1;
  201. elements[1][2] = 0;
  202. elements[2][0] = 0;
  203. elements[2][1] = 0;
  204. elements[2][2] = 1;
  205. }
  206. };
  207. _FORCE_INLINE_ void Basis::operator*=(const Basis &p_matrix) {
  208. set(
  209. p_matrix.tdotx(elements[0]), p_matrix.tdoty(elements[0]), p_matrix.tdotz(elements[0]),
  210. p_matrix.tdotx(elements[1]), p_matrix.tdoty(elements[1]), p_matrix.tdotz(elements[1]),
  211. p_matrix.tdotx(elements[2]), p_matrix.tdoty(elements[2]), p_matrix.tdotz(elements[2]));
  212. }
  213. _FORCE_INLINE_ Basis Basis::operator*(const Basis &p_matrix) const {
  214. return Basis(
  215. p_matrix.tdotx(elements[0]), p_matrix.tdoty(elements[0]), p_matrix.tdotz(elements[0]),
  216. p_matrix.tdotx(elements[1]), p_matrix.tdoty(elements[1]), p_matrix.tdotz(elements[1]),
  217. p_matrix.tdotx(elements[2]), p_matrix.tdoty(elements[2]), p_matrix.tdotz(elements[2]));
  218. }
  219. _FORCE_INLINE_ void Basis::operator+=(const Basis &p_matrix) {
  220. elements[0] += p_matrix.elements[0];
  221. elements[1] += p_matrix.elements[1];
  222. elements[2] += p_matrix.elements[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. elements[0] -= p_matrix.elements[0];
  231. elements[1] -= p_matrix.elements[1];
  232. elements[2] -= p_matrix.elements[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*=(real_t p_val) {
  240. elements[0] *= p_val;
  241. elements[1] *= p_val;
  242. elements[2] *= p_val;
  243. }
  244. _FORCE_INLINE_ Basis Basis::operator*(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. elements[0].dot(p_vector),
  252. elements[1].dot(p_vector),
  253. elements[2].dot(p_vector));
  254. }
  255. Vector3 Basis::xform_inv(const Vector3 &p_vector) const {
  256. return Vector3(
  257. (elements[0][0] * p_vector.x) + (elements[1][0] * p_vector.y) + (elements[2][0] * p_vector.z),
  258. (elements[0][1] * p_vector.x) + (elements[1][1] * p_vector.y) + (elements[2][1] * p_vector.z),
  259. (elements[0][2] * p_vector.x) + (elements[1][2] * p_vector.y) + (elements[2][2] * p_vector.z));
  260. }
  261. real_t Basis::determinant() const {
  262. return elements[0][0] * (elements[1][1] * elements[2][2] - elements[2][1] * elements[1][2]) -
  263. elements[1][0] * (elements[0][1] * elements[2][2] - elements[2][1] * elements[0][2]) +
  264. elements[2][0] * (elements[0][1] * elements[1][2] - elements[1][1] * elements[0][2]);
  265. }
  266. #endif