matrix3.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. /*************************************************************************/
  2. /* matrix3.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2018 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2018 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. #include "vector3.h"
  31. #ifndef MATRIX3_H
  32. #define MATRIX3_H
  33. #include "quat.h"
  34. /**
  35. @author Juan Linietsky <reduzio@gmail.com>
  36. */
  37. class Basis {
  38. public:
  39. Vector3 elements[3];
  40. _FORCE_INLINE_ const Vector3 &operator[](int axis) const {
  41. return elements[axis];
  42. }
  43. _FORCE_INLINE_ Vector3 &operator[](int axis) {
  44. return elements[axis];
  45. }
  46. void invert();
  47. void transpose();
  48. Basis inverse() const;
  49. Basis transposed() const;
  50. _FORCE_INLINE_ real_t determinant() const;
  51. void from_z(const Vector3 &p_z);
  52. _FORCE_INLINE_ Vector3 get_axis(int p_axis) const {
  53. // get actual basis axis (elements is transposed for performance)
  54. return Vector3(elements[0][p_axis], elements[1][p_axis], elements[2][p_axis]);
  55. }
  56. _FORCE_INLINE_ void set_axis(int p_axis, const Vector3 &p_value) {
  57. // get actual basis axis (elements is transposed for performance)
  58. elements[0][p_axis] = p_value.x;
  59. elements[1][p_axis] = p_value.y;
  60. elements[2][p_axis] = p_value.z;
  61. }
  62. void rotate(const Vector3 &p_axis, real_t p_phi);
  63. Basis rotated(const Vector3 &p_axis, real_t p_phi) const;
  64. void rotate_local(const Vector3 &p_axis, real_t p_phi);
  65. Basis rotated_local(const Vector3 &p_axis, real_t p_phi) const;
  66. void rotate(const Vector3 &p_euler);
  67. Basis rotated(const Vector3 &p_euler) const;
  68. Vector3 get_rotation() const;
  69. void get_rotation_axis_angle(Vector3 &p_axis, real_t &p_angle) const;
  70. Vector3 rotref_posscale_decomposition(Basis &rotref) const;
  71. Vector3 get_euler_xyz() const;
  72. void set_euler_xyz(const Vector3 &p_euler);
  73. Vector3 get_euler_yxz() const;
  74. void set_euler_yxz(const Vector3 &p_euler);
  75. Quat get_quat() const;
  76. void set_quat(const Quat &p_quat);
  77. Vector3 get_euler() const { return get_euler_yxz(); }
  78. void set_euler(const Vector3 &p_euler) { set_euler_yxz(p_euler); }
  79. void get_axis_angle(Vector3 &r_axis, real_t &r_angle) const;
  80. void set_axis_angle(const Vector3 &p_axis, real_t p_phi);
  81. void scale(const Vector3 &p_scale);
  82. Basis scaled(const Vector3 &p_scale) const;
  83. void scale_local(const Vector3 &p_scale);
  84. Basis scaled_local(const Vector3 &p_scale) const;
  85. void set_scale(const Vector3 &p_scale);
  86. Vector3 get_scale() const;
  87. Vector3 get_signed_scale() const;
  88. // transposed dot products
  89. _FORCE_INLINE_ real_t tdotx(const Vector3 &v) const {
  90. return elements[0][0] * v[0] + elements[1][0] * v[1] + elements[2][0] * v[2];
  91. }
  92. _FORCE_INLINE_ real_t tdoty(const Vector3 &v) const {
  93. return elements[0][1] * v[0] + elements[1][1] * v[1] + elements[2][1] * v[2];
  94. }
  95. _FORCE_INLINE_ real_t tdotz(const Vector3 &v) const {
  96. return elements[0][2] * v[0] + elements[1][2] * v[1] + elements[2][2] * v[2];
  97. }
  98. bool is_equal_approx(const Basis &a, const Basis &b) const;
  99. bool operator==(const Basis &p_matrix) const;
  100. bool operator!=(const Basis &p_matrix) const;
  101. _FORCE_INLINE_ Vector3 xform(const Vector3 &p_vector) const;
  102. _FORCE_INLINE_ Vector3 xform_inv(const Vector3 &p_vector) const;
  103. _FORCE_INLINE_ void operator*=(const Basis &p_matrix);
  104. _FORCE_INLINE_ Basis operator*(const Basis &p_matrix) const;
  105. _FORCE_INLINE_ void operator+=(const Basis &p_matrix);
  106. _FORCE_INLINE_ Basis operator+(const Basis &p_matrix) const;
  107. _FORCE_INLINE_ void operator-=(const Basis &p_matrix);
  108. _FORCE_INLINE_ Basis operator-(const Basis &p_matrix) const;
  109. _FORCE_INLINE_ void operator*=(real_t p_val);
  110. _FORCE_INLINE_ Basis operator*(real_t p_val) const;
  111. int get_orthogonal_index() const;
  112. void set_orthogonal_index(int p_index);
  113. bool is_orthogonal() const;
  114. bool is_diagonal() const;
  115. bool is_rotation() const;
  116. operator String() const;
  117. /* create / set */
  118. _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) {
  119. elements[0][0] = xx;
  120. elements[0][1] = xy;
  121. elements[0][2] = xz;
  122. elements[1][0] = yx;
  123. elements[1][1] = yy;
  124. elements[1][2] = yz;
  125. elements[2][0] = zx;
  126. elements[2][1] = zy;
  127. elements[2][2] = zz;
  128. }
  129. _FORCE_INLINE_ void set(const Vector3 &p_x, const Vector3 &p_y, const Vector3 &p_z) {
  130. set_axis(0, p_x);
  131. set_axis(1, p_y);
  132. set_axis(2, p_z);
  133. }
  134. _FORCE_INLINE_ Vector3 get_column(int i) const {
  135. return Vector3(elements[0][i], elements[1][i], elements[2][i]);
  136. }
  137. _FORCE_INLINE_ Vector3 get_row(int i) const {
  138. return Vector3(elements[i][0], elements[i][1], elements[i][2]);
  139. }
  140. _FORCE_INLINE_ Vector3 get_main_diagonal() const {
  141. return Vector3(elements[0][0], elements[1][1], elements[2][2]);
  142. }
  143. _FORCE_INLINE_ void set_row(int i, const Vector3 &p_row) {
  144. elements[i][0] = p_row.x;
  145. elements[i][1] = p_row.y;
  146. elements[i][2] = p_row.z;
  147. }
  148. _FORCE_INLINE_ void set_zero() {
  149. elements[0].zero();
  150. elements[1].zero();
  151. elements[2].zero();
  152. }
  153. _FORCE_INLINE_ Basis transpose_xform(const Basis &m) const {
  154. return Basis(
  155. elements[0].x * m[0].x + elements[1].x * m[1].x + elements[2].x * m[2].x,
  156. elements[0].x * m[0].y + elements[1].x * m[1].y + elements[2].x * m[2].y,
  157. elements[0].x * m[0].z + elements[1].x * m[1].z + elements[2].x * m[2].z,
  158. elements[0].y * m[0].x + elements[1].y * m[1].x + elements[2].y * m[2].x,
  159. elements[0].y * m[0].y + elements[1].y * m[1].y + elements[2].y * m[2].y,
  160. elements[0].y * m[0].z + elements[1].y * m[1].z + elements[2].y * m[2].z,
  161. elements[0].z * m[0].x + elements[1].z * m[1].x + elements[2].z * m[2].x,
  162. elements[0].z * m[0].y + elements[1].z * m[1].y + elements[2].z * m[2].y,
  163. elements[0].z * m[0].z + elements[1].z * m[1].z + elements[2].z * m[2].z);
  164. }
  165. 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) {
  166. set(xx, xy, xz, yx, yy, yz, zx, zy, zz);
  167. }
  168. void orthonormalize();
  169. Basis orthonormalized() const;
  170. bool is_symmetric() const;
  171. Basis diagonalize();
  172. operator Quat() const { return get_quat(); }
  173. Basis(const Quat &p_quat) { set_quat(p_quat); };
  174. Basis(const Vector3 &p_euler) { set_euler(p_euler); }
  175. Basis(const Vector3 &p_axis, real_t p_phi) { set_axis_angle(p_axis, p_phi); }
  176. _FORCE_INLINE_ Basis(const Vector3 &row0, const Vector3 &row1, const Vector3 &row2) {
  177. elements[0] = row0;
  178. elements[1] = row1;
  179. elements[2] = row2;
  180. }
  181. _FORCE_INLINE_ Basis() {
  182. elements[0][0] = 1;
  183. elements[0][1] = 0;
  184. elements[0][2] = 0;
  185. elements[1][0] = 0;
  186. elements[1][1] = 1;
  187. elements[1][2] = 0;
  188. elements[2][0] = 0;
  189. elements[2][1] = 0;
  190. elements[2][2] = 1;
  191. }
  192. };
  193. _FORCE_INLINE_ void Basis::operator*=(const Basis &p_matrix) {
  194. set(
  195. p_matrix.tdotx(elements[0]), p_matrix.tdoty(elements[0]), p_matrix.tdotz(elements[0]),
  196. p_matrix.tdotx(elements[1]), p_matrix.tdoty(elements[1]), p_matrix.tdotz(elements[1]),
  197. p_matrix.tdotx(elements[2]), p_matrix.tdoty(elements[2]), p_matrix.tdotz(elements[2]));
  198. }
  199. _FORCE_INLINE_ Basis Basis::operator*(const Basis &p_matrix) const {
  200. return Basis(
  201. p_matrix.tdotx(elements[0]), p_matrix.tdoty(elements[0]), p_matrix.tdotz(elements[0]),
  202. p_matrix.tdotx(elements[1]), p_matrix.tdoty(elements[1]), p_matrix.tdotz(elements[1]),
  203. p_matrix.tdotx(elements[2]), p_matrix.tdoty(elements[2]), p_matrix.tdotz(elements[2]));
  204. }
  205. _FORCE_INLINE_ void Basis::operator+=(const Basis &p_matrix) {
  206. elements[0] += p_matrix.elements[0];
  207. elements[1] += p_matrix.elements[1];
  208. elements[2] += p_matrix.elements[2];
  209. }
  210. _FORCE_INLINE_ Basis Basis::operator+(const Basis &p_matrix) const {
  211. Basis ret(*this);
  212. ret += p_matrix;
  213. return ret;
  214. }
  215. _FORCE_INLINE_ void Basis::operator-=(const Basis &p_matrix) {
  216. elements[0] -= p_matrix.elements[0];
  217. elements[1] -= p_matrix.elements[1];
  218. elements[2] -= p_matrix.elements[2];
  219. }
  220. _FORCE_INLINE_ Basis Basis::operator-(const Basis &p_matrix) const {
  221. Basis ret(*this);
  222. ret -= p_matrix;
  223. return ret;
  224. }
  225. _FORCE_INLINE_ void Basis::operator*=(real_t p_val) {
  226. elements[0] *= p_val;
  227. elements[1] *= p_val;
  228. elements[2] *= p_val;
  229. }
  230. _FORCE_INLINE_ Basis Basis::operator*(real_t p_val) const {
  231. Basis ret(*this);
  232. ret *= p_val;
  233. return ret;
  234. }
  235. Vector3 Basis::xform(const Vector3 &p_vector) const {
  236. return Vector3(
  237. elements[0].dot(p_vector),
  238. elements[1].dot(p_vector),
  239. elements[2].dot(p_vector));
  240. }
  241. Vector3 Basis::xform_inv(const Vector3 &p_vector) const {
  242. return Vector3(
  243. (elements[0][0] * p_vector.x) + (elements[1][0] * p_vector.y) + (elements[2][0] * p_vector.z),
  244. (elements[0][1] * p_vector.x) + (elements[1][1] * p_vector.y) + (elements[2][1] * p_vector.z),
  245. (elements[0][2] * p_vector.x) + (elements[1][2] * p_vector.y) + (elements[2][2] * p_vector.z));
  246. }
  247. real_t Basis::determinant() const {
  248. return elements[0][0] * (elements[1][1] * elements[2][2] - elements[2][1] * elements[1][2]) -
  249. elements[1][0] * (elements[0][1] * elements[2][2] - elements[2][1] * elements[0][2]) +
  250. elements[2][0] * (elements[0][1] * elements[1][2] - elements[1][1] * elements[0][2]);
  251. }
  252. #endif