matrix3.h 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. /*************************************************************************/
  2. /* matrix3.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 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 MATRIX3_H
  31. #define MATRIX3_H
  32. #include "quat.h"
  33. #include "vector3.h"
  34. /**
  35. @author Juan Linietsky <reduzio@gmail.com>
  36. */
  37. class Matrix3 {
  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. Matrix3 inverse() const;
  49. Matrix3 transposed() const;
  50. _FORCE_INLINE_ float 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. Matrix3 rotated(const Vector3 &p_axis, real_t p_phi) const;
  64. void scale(const Vector3 &p_scale);
  65. Matrix3 scaled(const Vector3 &p_scale) const;
  66. Vector3 get_scale() const;
  67. Vector3 get_euler() const;
  68. void set_euler(const Vector3 &p_euler);
  69. // transposed dot products
  70. _FORCE_INLINE_ real_t tdotx(const Vector3 &v) const {
  71. return elements[0][0] * v[0] + elements[1][0] * v[1] + elements[2][0] * v[2];
  72. }
  73. _FORCE_INLINE_ real_t tdoty(const Vector3 &v) const {
  74. return elements[0][1] * v[0] + elements[1][1] * v[1] + elements[2][1] * v[2];
  75. }
  76. _FORCE_INLINE_ real_t tdotz(const Vector3 &v) const {
  77. return elements[0][2] * v[0] + elements[1][2] * v[1] + elements[2][2] * v[2];
  78. }
  79. bool operator==(const Matrix3 &p_matrix) const;
  80. bool operator!=(const Matrix3 &p_matrix) const;
  81. _FORCE_INLINE_ Vector3 xform(const Vector3 &p_vector) const;
  82. _FORCE_INLINE_ Vector3 xform_inv(const Vector3 &p_vector) const;
  83. _FORCE_INLINE_ void operator*=(const Matrix3 &p_matrix);
  84. _FORCE_INLINE_ Matrix3 operator*(const Matrix3 &p_matrix) const;
  85. int get_orthogonal_index() const;
  86. void set_orthogonal_index(int p_index);
  87. operator String() const;
  88. void get_axis_and_angle(Vector3 &r_axis, real_t &r_angle) const;
  89. /* create / set */
  90. _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) {
  91. elements[0][0] = xx;
  92. elements[0][1] = xy;
  93. elements[0][2] = xz;
  94. elements[1][0] = yx;
  95. elements[1][1] = yy;
  96. elements[1][2] = yz;
  97. elements[2][0] = zx;
  98. elements[2][1] = zy;
  99. elements[2][2] = zz;
  100. }
  101. _FORCE_INLINE_ Vector3 get_column(int i) const {
  102. return Vector3(elements[0][i], elements[1][i], elements[2][i]);
  103. }
  104. _FORCE_INLINE_ Vector3 get_row(int i) const {
  105. return Vector3(elements[i][0], elements[i][1], elements[i][2]);
  106. }
  107. _FORCE_INLINE_ void set_row(int i, const Vector3 &p_row) {
  108. elements[i][0] = p_row.x;
  109. elements[i][1] = p_row.y;
  110. elements[i][2] = p_row.z;
  111. }
  112. _FORCE_INLINE_ void set_zero() {
  113. elements[0].zero();
  114. elements[1].zero();
  115. elements[2].zero();
  116. }
  117. _FORCE_INLINE_ Matrix3 transpose_xform(const Matrix3 &m) const {
  118. return Matrix3(
  119. elements[0].x * m[0].x + elements[1].x * m[1].x + elements[2].x * m[2].x,
  120. elements[0].x * m[0].y + elements[1].x * m[1].y + elements[2].x * m[2].y,
  121. elements[0].x * m[0].z + elements[1].x * m[1].z + elements[2].x * m[2].z,
  122. elements[0].y * m[0].x + elements[1].y * m[1].x + elements[2].y * m[2].x,
  123. elements[0].y * m[0].y + elements[1].y * m[1].y + elements[2].y * m[2].y,
  124. elements[0].y * m[0].z + elements[1].y * m[1].z + elements[2].y * m[2].z,
  125. elements[0].z * m[0].x + elements[1].z * m[1].x + elements[2].z * m[2].x,
  126. elements[0].z * m[0].y + elements[1].z * m[1].y + elements[2].z * m[2].y,
  127. elements[0].z * m[0].z + elements[1].z * m[1].z + elements[2].z * m[2].z);
  128. }
  129. Matrix3(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. set(xx, xy, xz, yx, yy, yz, zx, zy, zz);
  131. }
  132. void orthonormalize();
  133. Matrix3 orthonormalized() const;
  134. operator Quat() const;
  135. Matrix3(const Quat &p_quat); // euler
  136. Matrix3(const Vector3 &p_euler); // euler
  137. Matrix3(const Vector3 &p_axis, real_t p_phi);
  138. _FORCE_INLINE_ Matrix3() {
  139. elements[0][0] = 1;
  140. elements[0][1] = 0;
  141. elements[0][2] = 0;
  142. elements[1][0] = 0;
  143. elements[1][1] = 1;
  144. elements[1][2] = 0;
  145. elements[2][0] = 0;
  146. elements[2][1] = 0;
  147. elements[2][2] = 1;
  148. }
  149. };
  150. _FORCE_INLINE_ void Matrix3::operator*=(const Matrix3 &p_matrix) {
  151. set(
  152. p_matrix.tdotx(elements[0]), p_matrix.tdoty(elements[0]), p_matrix.tdotz(elements[0]),
  153. p_matrix.tdotx(elements[1]), p_matrix.tdoty(elements[1]), p_matrix.tdotz(elements[1]),
  154. p_matrix.tdotx(elements[2]), p_matrix.tdoty(elements[2]), p_matrix.tdotz(elements[2]));
  155. }
  156. _FORCE_INLINE_ Matrix3 Matrix3::operator*(const Matrix3 &p_matrix) const {
  157. return Matrix3(
  158. p_matrix.tdotx(elements[0]), p_matrix.tdoty(elements[0]), p_matrix.tdotz(elements[0]),
  159. p_matrix.tdotx(elements[1]), p_matrix.tdoty(elements[1]), p_matrix.tdotz(elements[1]),
  160. p_matrix.tdotx(elements[2]), p_matrix.tdoty(elements[2]), p_matrix.tdotz(elements[2]));
  161. }
  162. Vector3 Matrix3::xform(const Vector3 &p_vector) const {
  163. return Vector3(
  164. elements[0].dot(p_vector),
  165. elements[1].dot(p_vector),
  166. elements[2].dot(p_vector));
  167. }
  168. Vector3 Matrix3::xform_inv(const Vector3 &p_vector) const {
  169. return Vector3(
  170. (elements[0][0] * p_vector.x) + (elements[1][0] * p_vector.y) + (elements[2][0] * p_vector.z),
  171. (elements[0][1] * p_vector.x) + (elements[1][1] * p_vector.y) + (elements[2][1] * p_vector.z),
  172. (elements[0][2] * p_vector.x) + (elements[1][2] * p_vector.y) + (elements[2][2] * p_vector.z));
  173. }
  174. float Matrix3::determinant() const {
  175. return elements[0][0] * (elements[1][1] * elements[2][2] - elements[2][1] * elements[1][2]) -
  176. elements[1][0] * (elements[0][1] * elements[2][2] - elements[2][1] * elements[0][2]) +
  177. elements[2][0] * (elements[0][1] * elements[1][2] - elements[1][1] * elements[0][2]);
  178. }
  179. #endif