Basis.hpp 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  1. #ifndef BASIS_H
  2. #define BASIS_H
  3. #include <gdnative/basis.h>
  4. #include "Defs.hpp"
  5. #include "Vector3.hpp"
  6. namespace godot {
  7. class Quat;
  8. class Basis {
  9. private:
  10. // This helper template is for mimicking the behavior difference between the engine
  11. // and script interfaces that logically script sees matrices as column major, while
  12. // the engine stores them in row major to efficiently take advantage of SIMD
  13. // instructions in case of matrix-vector multiplications.
  14. // With this helper template native scripts see the data as if it was column major
  15. // without actually transposing the basis matrix at the script-engine boundary.
  16. template <int column>
  17. class ColumnVector3 {
  18. private:
  19. template <int column1, int component>
  20. class ColumnVectorComponent {
  21. private:
  22. Vector3 elements[3];
  23. protected:
  24. inline ColumnVectorComponent<column1, component> &operator=(const ColumnVectorComponent<column1, component> &p_value) {
  25. return *this = real_t(p_value);
  26. }
  27. inline ColumnVectorComponent(const ColumnVectorComponent<column1, component> &p_value) {
  28. *this = real_t(p_value);
  29. }
  30. inline ColumnVectorComponent<column1, component> &operator=(const real_t &p_value) {
  31. elements[component][column1] = p_value;
  32. return *this;
  33. }
  34. inline operator real_t() const {
  35. return elements[component][column1];
  36. }
  37. };
  38. public:
  39. enum Axis {
  40. AXIS_X,
  41. AXIS_Y,
  42. AXIS_Z,
  43. };
  44. union {
  45. ColumnVectorComponent<column, 0> x;
  46. ColumnVectorComponent<column, 1> y;
  47. ColumnVectorComponent<column, 2> z;
  48. Vector3 elements[3]; // Not for direct access, use [] operator instead
  49. };
  50. inline ColumnVector3<column> &operator=(const ColumnVector3<column> &p_value) {
  51. return *this = Vector3(p_value);
  52. }
  53. inline ColumnVector3(const ColumnVector3<column> &p_value) {
  54. *this = Vector3(p_value);
  55. }
  56. inline ColumnVector3<column> &operator=(const Vector3 &p_value) {
  57. elements[0][column] = p_value.x;
  58. elements[1][column] = p_value.y;
  59. elements[2][column] = p_value.z;
  60. return *this;
  61. }
  62. inline operator Vector3() const {
  63. return Vector3(elements[0][column], elements[1][column], elements[2][column]);
  64. }
  65. // Unfortunately, we also need to replicate the other interfaces of Vector3 in
  66. // order for being able to directly operate on these "meta-Vector3" objects without
  67. // an explicit cast or an intermediate assignment to a real Vector3 object.
  68. inline const real_t &operator[](int p_axis) const {
  69. return elements[p_axis][column];
  70. }
  71. inline real_t &operator[](int p_axis) {
  72. return elements[p_axis][column];
  73. }
  74. inline ColumnVector3<column> &operator+=(const Vector3 &p_v) {
  75. return *this = *this + p_v;
  76. }
  77. inline Vector3 operator+(const Vector3 &p_v) const {
  78. return Vector3(*this) + p_v;
  79. }
  80. inline ColumnVector3<column> &operator-=(const Vector3 &p_v) {
  81. return *this = *this - p_v;
  82. }
  83. inline Vector3 operator-(const Vector3 &p_v) const {
  84. return Vector3(*this) - p_v;
  85. }
  86. inline ColumnVector3<column> &operator*=(const Vector3 &p_v) {
  87. return *this = *this * p_v;
  88. }
  89. inline Vector3 operator*(const Vector3 &p_v) const {
  90. return Vector3(*this) * p_v;
  91. }
  92. inline ColumnVector3<column> &operator/=(const Vector3 &p_v) {
  93. return *this = *this / p_v;
  94. }
  95. inline Vector3 operator/(const Vector3 &p_v) const {
  96. return Vector3(*this) / p_v;
  97. }
  98. inline ColumnVector3<column> &operator*=(real_t p_scalar) {
  99. return *this = *this * p_scalar;
  100. }
  101. inline Vector3 operator*(real_t p_scalar) const {
  102. return Vector3(*this) * p_scalar;
  103. }
  104. inline ColumnVector3<column> &operator/=(real_t p_scalar) {
  105. return *this = *this / p_scalar;
  106. }
  107. inline Vector3 operator/(real_t p_scalar) const {
  108. return Vector3(*this) / p_scalar;
  109. }
  110. inline Vector3 operator-() const {
  111. return -Vector3(*this);
  112. }
  113. inline bool operator==(const Vector3 &p_v) const {
  114. return Vector3(*this) == p_v;
  115. }
  116. inline bool operator!=(const Vector3 &p_v) const {
  117. return Vector3(*this) != p_v;
  118. }
  119. inline bool operator<(const Vector3 &p_v) const {
  120. return Vector3(*this) < p_v;
  121. }
  122. inline bool operator<=(const Vector3 &p_v) const {
  123. return Vector3(*this) <= p_v;
  124. }
  125. inline Vector3 abs() const {
  126. return Vector3(*this).abs();
  127. }
  128. inline Vector3 ceil() const {
  129. return Vector3(*this).ceil();
  130. }
  131. inline Vector3 cross(const Vector3 &b) const {
  132. return Vector3(*this).cross(b);
  133. }
  134. inline Vector3 linear_interpolate(const Vector3 &p_b, real_t p_t) const {
  135. return Vector3(*this).linear_interpolate(p_b, p_t);
  136. }
  137. inline Vector3 cubic_interpolate(const Vector3 &b, const Vector3 &pre_a, const Vector3 &post_b, const real_t t) const {
  138. return Vector3(*this).cubic_interpolate(b, pre_a, post_b, t);
  139. }
  140. inline Vector3 bounce(const Vector3 &p_normal) const {
  141. return Vector3(*this).bounce(p_normal);
  142. }
  143. inline real_t length() const {
  144. return Vector3(*this).length();
  145. }
  146. inline real_t length_squared() const {
  147. return Vector3(*this).length_squared();
  148. }
  149. inline real_t distance_squared_to(const Vector3 &b) const {
  150. return Vector3(*this).distance_squared_to(b);
  151. }
  152. inline real_t distance_to(const Vector3 &b) const {
  153. return Vector3(*this).distance_to(b);
  154. }
  155. inline real_t dot(const Vector3 &b) const {
  156. return Vector3(*this).dot(b);
  157. }
  158. inline real_t angle_to(const Vector3 &b) const {
  159. return Vector3(*this).angle_to(b);
  160. }
  161. inline Vector3 floor() const {
  162. return Vector3(*this).floor();
  163. }
  164. inline Vector3 inverse() const {
  165. return Vector3(*this).inverse();
  166. }
  167. inline bool is_normalized() const {
  168. return Vector3(*this).is_normalized();
  169. }
  170. inline Basis outer(const Vector3 &b) const {
  171. return Vector3(*this).outer(b);
  172. }
  173. inline int max_axis() const {
  174. return Vector3(*this).max_axis();
  175. }
  176. inline int min_axis() const {
  177. return Vector3(*this).min_axis();
  178. }
  179. inline void normalize() {
  180. Vector3 v = *this;
  181. v.normalize();
  182. *this = v;
  183. }
  184. inline Vector3 normalized() const {
  185. return Vector3(*this).normalized();
  186. }
  187. inline Vector3 reflect(const Vector3 &by) const {
  188. return Vector3(*this).reflect(by);
  189. }
  190. inline Vector3 rotated(const Vector3 &axis, const real_t phi) const {
  191. return Vector3(*this).rotated(axis, phi);
  192. }
  193. inline void rotate(const Vector3 &p_axis, real_t p_phi) {
  194. Vector3 v = *this;
  195. v.rotate(p_axis, p_phi);
  196. *this = v;
  197. }
  198. inline Vector3 slide(const Vector3 &by) const {
  199. return Vector3(*this).slide(by);
  200. }
  201. inline void snap(real_t p_val) {
  202. Vector3 v = *this;
  203. v.snap(p_val);
  204. *this = v;
  205. }
  206. inline Vector3 snapped(const float by) {
  207. return Vector3(*this).snapped(by);
  208. }
  209. inline operator String() const {
  210. return String(Vector3(*this));
  211. }
  212. };
  213. public:
  214. union {
  215. ColumnVector3<0> x;
  216. ColumnVector3<1> y;
  217. ColumnVector3<2> z;
  218. Vector3 elements[3]; // Not for direct access, use [] operator instead
  219. };
  220. inline Basis(const Basis &p_basis) {
  221. elements[0] = p_basis.elements[0];
  222. elements[1] = p_basis.elements[1];
  223. elements[2] = p_basis.elements[2];
  224. }
  225. inline Basis &operator=(const Basis &p_basis) {
  226. elements[0] = p_basis.elements[0];
  227. elements[1] = p_basis.elements[1];
  228. elements[2] = p_basis.elements[2];
  229. return *this;
  230. }
  231. Basis(const Quat &p_quat); // euler
  232. Basis(const Vector3 &p_euler); // euler
  233. Basis(const Vector3 &p_axis, real_t p_phi);
  234. Basis(const Vector3 &row0, const Vector3 &row1, const Vector3 &row2);
  235. 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);
  236. Basis();
  237. const Vector3 operator[](int axis) const {
  238. return get_axis(axis);
  239. }
  240. ColumnVector3<0> &operator[](int axis) {
  241. // We need to do a little pointer magic to get this to work, because the
  242. // ColumnVector3 template takes the axis as a template parameter.
  243. // Don't touch this unless you're sure what you're doing!
  244. return (reinterpret_cast<Basis *>(reinterpret_cast<real_t *>(this) + axis))->x;
  245. }
  246. void invert();
  247. bool isequal_approx(const Basis &a, const Basis &b) const;
  248. bool is_orthogonal() const;
  249. bool is_rotation() const;
  250. void transpose();
  251. Basis inverse() const;
  252. Basis transposed() const;
  253. real_t determinant() const;
  254. Vector3 get_axis(int p_axis) const;
  255. void set_axis(int p_axis, const Vector3 &p_value);
  256. void rotate(const Vector3 &p_axis, real_t p_phi);
  257. Basis rotated(const Vector3 &p_axis, real_t p_phi) const;
  258. void scale(const Vector3 &p_scale);
  259. Basis scaled(const Vector3 &p_scale) const;
  260. Vector3 get_scale() const;
  261. Basis slerp(Basis b, float t) const;
  262. Vector3 get_euler_xyz() const;
  263. void set_euler_xyz(const Vector3 &p_euler);
  264. Vector3 get_euler_yxz() const;
  265. void set_euler_yxz(const Vector3 &p_euler);
  266. inline Vector3 get_euler() const { return get_euler_yxz(); }
  267. inline void set_euler(const Vector3 &p_euler) { set_euler_yxz(p_euler); }
  268. // transposed dot products
  269. real_t tdotx(const Vector3 &v) const;
  270. real_t tdoty(const Vector3 &v) const;
  271. real_t tdotz(const Vector3 &v) const;
  272. bool operator==(const Basis &p_matrix) const;
  273. bool operator!=(const Basis &p_matrix) const;
  274. Vector3 xform(const Vector3 &p_vector) const;
  275. Vector3 xform_inv(const Vector3 &p_vector) const;
  276. void operator*=(const Basis &p_matrix);
  277. Basis operator*(const Basis &p_matrix) const;
  278. void operator+=(const Basis &p_matrix);
  279. Basis operator+(const Basis &p_matrix) const;
  280. void operator-=(const Basis &p_matrix);
  281. Basis operator-(const Basis &p_matrix) const;
  282. void operator*=(real_t p_val);
  283. Basis operator*(real_t p_val) const;
  284. int get_orthogonal_index() const; // down below
  285. void set_orthogonal_index(int p_index); // down below
  286. operator String() const;
  287. void get_axis_and_angle(Vector3 &r_axis, real_t &r_angle) const;
  288. /* create / set */
  289. 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);
  290. Vector3 get_column(int i) const;
  291. Vector3 get_row(int i) const;
  292. Vector3 get_main_diagonal() const;
  293. void set_row(int i, const Vector3 &p_row);
  294. Basis transpose_xform(const Basis &m) const;
  295. void orthonormalize();
  296. Basis orthonormalized() const;
  297. bool is_symmetric() const;
  298. Basis diagonalize();
  299. operator Quat() const;
  300. };
  301. } // namespace godot
  302. #endif // BASIS_H