transform3d.hpp 11 KB

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