Basis.hpp 12 KB

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