transform_3d.h 10 KB

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