transform_3d.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. /**************************************************************************/
  2. /* transform_3d.h */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  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. #pragma once
  31. #include "core/math/aabb.h"
  32. #include "core/math/basis.h"
  33. #include "core/math/plane.h"
  34. #include "core/templates/vector.h"
  35. struct [[nodiscard]] Transform3D {
  36. Basis basis;
  37. Vector3 origin;
  38. void invert();
  39. Transform3D inverse() const;
  40. void affine_invert();
  41. Transform3D affine_inverse() const;
  42. Transform3D rotated(const Vector3 &p_axis, real_t p_angle) const;
  43. Transform3D rotated_local(const Vector3 &p_axis, real_t p_angle) const;
  44. void rotate(const Vector3 &p_axis, real_t p_angle);
  45. void rotate_basis(const Vector3 &p_axis, real_t p_angle);
  46. void set_look_at(const Vector3 &p_eye, const Vector3 &p_target, const Vector3 &p_up = Vector3(0, 1, 0), bool p_use_model_front = false);
  47. Transform3D looking_at(const Vector3 &p_target, const Vector3 &p_up = Vector3(0, 1, 0), bool p_use_model_front = false) const;
  48. void scale(const Vector3 &p_scale);
  49. Transform3D scaled(const Vector3 &p_scale) const;
  50. Transform3D scaled_local(const Vector3 &p_scale) const;
  51. void scale_basis(const Vector3 &p_scale);
  52. void translate_local(real_t p_tx, real_t p_ty, real_t p_tz);
  53. void translate_local(const Vector3 &p_translation);
  54. Transform3D translated(const Vector3 &p_translation) const;
  55. Transform3D translated_local(const Vector3 &p_translation) const;
  56. const Basis &get_basis() const { return basis; }
  57. void set_basis(const Basis &p_basis) { basis = p_basis; }
  58. const Vector3 &get_origin() const { return origin; }
  59. void set_origin(const Vector3 &p_origin) { origin = p_origin; }
  60. void orthonormalize();
  61. Transform3D orthonormalized() const;
  62. void orthogonalize();
  63. Transform3D orthogonalized() const;
  64. bool is_equal_approx(const Transform3D &p_transform) const;
  65. bool is_same(const Transform3D &p_transform) const;
  66. bool is_finite() const;
  67. constexpr bool operator==(const Transform3D &p_transform) const;
  68. constexpr bool operator!=(const Transform3D &p_transform) const;
  69. _FORCE_INLINE_ Vector3 xform(const Vector3 &p_vector) const;
  70. _FORCE_INLINE_ AABB xform(const AABB &p_aabb) const;
  71. _FORCE_INLINE_ Vector<Vector3> xform(const Vector<Vector3> &p_array) const;
  72. // NOTE: These are UNSAFE with non-uniform scaling, and will produce incorrect results.
  73. // They use the transpose.
  74. // For safe inverse transforms, xform by the affine_inverse.
  75. _FORCE_INLINE_ Vector3 xform_inv(const Vector3 &p_vector) const;
  76. _FORCE_INLINE_ AABB xform_inv(const AABB &p_aabb) const;
  77. _FORCE_INLINE_ Vector<Vector3> xform_inv(const Vector<Vector3> &p_array) const;
  78. // Safe with non-uniform scaling (uses affine_inverse).
  79. _FORCE_INLINE_ Plane xform(const Plane &p_plane) const;
  80. _FORCE_INLINE_ Plane xform_inv(const Plane &p_plane) const;
  81. // These fast versions use precomputed affine inverse, and should be used in bottleneck areas where
  82. // multiple planes are to be transformed.
  83. _FORCE_INLINE_ Plane xform_fast(const Plane &p_plane, const Basis &p_basis_inverse_transpose) const;
  84. static _FORCE_INLINE_ Plane xform_inv_fast(const Plane &p_plane, const Transform3D &p_inverse, const Basis &p_basis_transpose);
  85. void operator*=(const Transform3D &p_transform);
  86. Transform3D operator*(const Transform3D &p_transform) const;
  87. constexpr void operator*=(real_t p_val);
  88. constexpr Transform3D operator*(real_t p_val) const;
  89. constexpr void operator/=(real_t p_val);
  90. constexpr Transform3D operator/(real_t p_val) const;
  91. Transform3D interpolate_with(const Transform3D &p_transform, real_t p_c) const;
  92. _FORCE_INLINE_ Transform3D inverse_xform(const Transform3D &t) const {
  93. Vector3 v = t.origin - origin;
  94. return Transform3D(basis.transpose_xform(t.basis),
  95. basis.xform(v));
  96. }
  97. void set(real_t p_xx, real_t p_xy, real_t p_xz, real_t p_yx, real_t p_yy, real_t p_yz, real_t p_zx, real_t p_zy, real_t p_zz, real_t p_tx, real_t p_ty, real_t p_tz) {
  98. basis.set(p_xx, p_xy, p_xz, p_yx, p_yy, p_yz, p_zx, p_zy, p_zz);
  99. origin.x = p_tx;
  100. origin.y = p_ty;
  101. origin.z = p_tz;
  102. }
  103. operator String() const;
  104. Transform3D() = default;
  105. constexpr Transform3D(const Basis &p_basis, const Vector3 &p_origin = Vector3()) :
  106. basis(p_basis),
  107. origin(p_origin) {}
  108. constexpr Transform3D(const Vector3 &p_x, const Vector3 &p_y, const Vector3 &p_z, const Vector3 &p_origin) :
  109. basis(p_x, p_y, p_z),
  110. origin(p_origin) {}
  111. constexpr Transform3D(real_t p_xx, real_t p_xy, real_t p_xz, real_t p_yx, real_t p_yy, real_t p_yz, real_t p_zx, real_t p_zy, real_t p_zz, real_t p_ox, real_t p_oy, real_t p_oz) :
  112. basis(p_xx, p_xy, p_xz, p_yx, p_yy, p_yz, p_zx, p_zy, p_zz),
  113. origin(p_ox, p_oy, p_oz) {}
  114. };
  115. constexpr bool Transform3D::operator==(const Transform3D &p_transform) const {
  116. return (basis == p_transform.basis && origin == p_transform.origin);
  117. }
  118. constexpr bool Transform3D::operator!=(const Transform3D &p_transform) const {
  119. return (basis != p_transform.basis || origin != p_transform.origin);
  120. }
  121. constexpr void Transform3D::operator*=(real_t p_val) {
  122. origin *= p_val;
  123. basis *= p_val;
  124. }
  125. constexpr Transform3D Transform3D::operator*(real_t p_val) const {
  126. Transform3D ret(*this);
  127. ret *= p_val;
  128. return ret;
  129. }
  130. constexpr void Transform3D::operator/=(real_t p_val) {
  131. basis /= p_val;
  132. origin /= p_val;
  133. }
  134. constexpr Transform3D Transform3D::operator/(real_t p_val) const {
  135. Transform3D ret(*this);
  136. ret /= p_val;
  137. return ret;
  138. }
  139. _FORCE_INLINE_ Vector3 Transform3D::xform(const Vector3 &p_vector) const {
  140. return Vector3(
  141. basis[0].dot(p_vector) + origin.x,
  142. basis[1].dot(p_vector) + origin.y,
  143. basis[2].dot(p_vector) + origin.z);
  144. }
  145. _FORCE_INLINE_ Vector3 Transform3D::xform_inv(const Vector3 &p_vector) const {
  146. Vector3 v = p_vector - origin;
  147. return Vector3(
  148. (basis.rows[0][0] * v.x) + (basis.rows[1][0] * v.y) + (basis.rows[2][0] * v.z),
  149. (basis.rows[0][1] * v.x) + (basis.rows[1][1] * v.y) + (basis.rows[2][1] * v.z),
  150. (basis.rows[0][2] * v.x) + (basis.rows[1][2] * v.y) + (basis.rows[2][2] * v.z));
  151. }
  152. // Neither the plane regular xform or xform_inv are particularly efficient,
  153. // as they do a basis inverse. For xforming a large number
  154. // of planes it is better to pre-calculate the inverse transpose basis once
  155. // and reuse it for each plane, by using the 'fast' version of the functions.
  156. _FORCE_INLINE_ Plane Transform3D::xform(const Plane &p_plane) const {
  157. Basis b = basis.inverse();
  158. b.transpose();
  159. return xform_fast(p_plane, b);
  160. }
  161. _FORCE_INLINE_ Plane Transform3D::xform_inv(const Plane &p_plane) const {
  162. Transform3D inv = affine_inverse();
  163. Basis basis_transpose = basis.transposed();
  164. return xform_inv_fast(p_plane, inv, basis_transpose);
  165. }
  166. _FORCE_INLINE_ AABB Transform3D::xform(const AABB &p_aabb) const {
  167. /* https://dev.theomader.com/transform-bounding-boxes/ */
  168. Vector3 min = p_aabb.position;
  169. Vector3 max = p_aabb.position + p_aabb.size;
  170. Vector3 tmin, tmax;
  171. for (int i = 0; i < 3; i++) {
  172. tmin[i] = tmax[i] = origin[i];
  173. for (int j = 0; j < 3; j++) {
  174. real_t e = basis[i][j] * min[j];
  175. real_t f = basis[i][j] * max[j];
  176. if (e < f) {
  177. tmin[i] += e;
  178. tmax[i] += f;
  179. } else {
  180. tmin[i] += f;
  181. tmax[i] += e;
  182. }
  183. }
  184. }
  185. AABB r_aabb;
  186. r_aabb.position = tmin;
  187. r_aabb.size = tmax - tmin;
  188. return r_aabb;
  189. }
  190. _FORCE_INLINE_ AABB Transform3D::xform_inv(const AABB &p_aabb) const {
  191. /* define vertices */
  192. Vector3 vertices[8] = {
  193. Vector3(p_aabb.position.x + p_aabb.size.x, p_aabb.position.y + p_aabb.size.y, p_aabb.position.z + p_aabb.size.z),
  194. Vector3(p_aabb.position.x + p_aabb.size.x, p_aabb.position.y + p_aabb.size.y, p_aabb.position.z),
  195. Vector3(p_aabb.position.x + p_aabb.size.x, p_aabb.position.y, p_aabb.position.z + p_aabb.size.z),
  196. Vector3(p_aabb.position.x + p_aabb.size.x, p_aabb.position.y, p_aabb.position.z),
  197. Vector3(p_aabb.position.x, p_aabb.position.y + p_aabb.size.y, p_aabb.position.z + p_aabb.size.z),
  198. Vector3(p_aabb.position.x, p_aabb.position.y + p_aabb.size.y, p_aabb.position.z),
  199. Vector3(p_aabb.position.x, p_aabb.position.y, p_aabb.position.z + p_aabb.size.z),
  200. Vector3(p_aabb.position.x, p_aabb.position.y, p_aabb.position.z)
  201. };
  202. AABB ret;
  203. ret.position = xform_inv(vertices[0]);
  204. for (int i = 1; i < 8; i++) {
  205. ret.expand_to(xform_inv(vertices[i]));
  206. }
  207. return ret;
  208. }
  209. Vector<Vector3> Transform3D::xform(const Vector<Vector3> &p_array) const {
  210. Vector<Vector3> array;
  211. array.resize(p_array.size());
  212. const Vector3 *r = p_array.ptr();
  213. Vector3 *w = array.ptrw();
  214. for (int i = 0; i < p_array.size(); ++i) {
  215. w[i] = xform(r[i]);
  216. }
  217. return array;
  218. }
  219. Vector<Vector3> Transform3D::xform_inv(const Vector<Vector3> &p_array) const {
  220. Vector<Vector3> array;
  221. array.resize(p_array.size());
  222. const Vector3 *r = p_array.ptr();
  223. Vector3 *w = array.ptrw();
  224. for (int i = 0; i < p_array.size(); ++i) {
  225. w[i] = xform_inv(r[i]);
  226. }
  227. return array;
  228. }
  229. _FORCE_INLINE_ Plane Transform3D::xform_fast(const Plane &p_plane, const Basis &p_basis_inverse_transpose) const {
  230. // Transform a single point on the plane.
  231. Vector3 point = p_plane.normal * p_plane.d;
  232. point = xform(point);
  233. // Use inverse transpose for correct normals with non-uniform scaling.
  234. Vector3 normal = p_basis_inverse_transpose.xform(p_plane.normal);
  235. normal.normalize();
  236. real_t d = normal.dot(point);
  237. return Plane(normal, d);
  238. }
  239. _FORCE_INLINE_ Plane Transform3D::xform_inv_fast(const Plane &p_plane, const Transform3D &p_inverse, const Basis &p_basis_transpose) {
  240. // Transform a single point on the plane.
  241. Vector3 point = p_plane.normal * p_plane.d;
  242. point = p_inverse.xform(point);
  243. // Note that instead of precalculating the transpose, an alternative
  244. // would be to use the transpose for the basis transform.
  245. // However that would be less SIMD friendly (requiring a swizzle).
  246. // So the cost is one extra precalced value in the calling code.
  247. // This is probably worth it, as this could be used in bottleneck areas. And
  248. // where it is not a bottleneck, the non-fast method is fine.
  249. // Use transpose for correct normals with non-uniform scaling.
  250. Vector3 normal = p_basis_transpose.xform(p_plane.normal);
  251. normal.normalize();
  252. real_t d = normal.dot(point);
  253. return Plane(normal, d);
  254. }