transform.h 10 KB

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