Basis.hpp 10 KB

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