transform.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. /*************************************************************************/
  2. /* transform.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 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 Transform {
  36. public:
  37. Basis basis;
  38. Vector3 origin;
  39. void invert();
  40. Transform inverse() const;
  41. void affine_invert();
  42. Transform affine_inverse() const;
  43. Transform 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);
  47. Transform looking_at(const Vector3 &p_target, const Vector3 &p_up) const;
  48. void scale(const Vector3 &p_scale);
  49. Transform 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. Transform 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. Transform orthonormalized() const;
  60. bool operator==(const Transform &p_transform) const;
  61. bool operator!=(const Transform &p_transform) const;
  62. _FORCE_INLINE_ Vector3 xform(const Vector3 &p_vector) const;
  63. _FORCE_INLINE_ Vector3 xform_inv(const Vector3 &p_vector) const;
  64. _FORCE_INLINE_ Plane xform(const Plane &p_plane) const;
  65. _FORCE_INLINE_ Plane xform_inv(const Plane &p_plane) const;
  66. _FORCE_INLINE_ AABB xform(const AABB &p_aabb) const;
  67. _FORCE_INLINE_ AABB xform_inv(const AABB &p_aabb) const;
  68. void operator*=(const Transform &p_transform);
  69. Transform operator*(const Transform &p_transform) const;
  70. Transform interpolate_with(const Transform &p_transform, real_t p_c) const;
  71. _FORCE_INLINE_ Transform inverse_xform(const Transform &t) const {
  72. Vector3 v = t.origin - origin;
  73. return Transform(basis.transpose_xform(t.basis),
  74. basis.xform(v));
  75. }
  76. 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) {
  77. basis.set(xx, xy, xz, yx, yy, yz, zx, zy, zz);
  78. origin.x = tx;
  79. origin.y = ty;
  80. origin.z = tz;
  81. }
  82. operator String() const;
  83. Transform(const Basis &p_basis, const Vector3 &p_origin = Vector3());
  84. Transform() {}
  85. };
  86. _FORCE_INLINE_ Vector3 Transform::xform(const Vector3 &p_vector) const {
  87. return Vector3(
  88. basis[0].dot(p_vector) + origin.x,
  89. basis[1].dot(p_vector) + origin.y,
  90. basis[2].dot(p_vector) + origin.z);
  91. }
  92. _FORCE_INLINE_ Vector3 Transform::xform_inv(const Vector3 &p_vector) const {
  93. Vector3 v = p_vector - origin;
  94. return Vector3(
  95. (basis.elements[0][0] * v.x) + (basis.elements[1][0] * v.y) + (basis.elements[2][0] * v.z),
  96. (basis.elements[0][1] * v.x) + (basis.elements[1][1] * v.y) + (basis.elements[2][1] * v.z),
  97. (basis.elements[0][2] * v.x) + (basis.elements[1][2] * v.y) + (basis.elements[2][2] * v.z));
  98. }
  99. _FORCE_INLINE_ Plane Transform::xform(const Plane &p_plane) const {
  100. Vector3 point = p_plane.normal * p_plane.d;
  101. Vector3 point_dir = point + p_plane.normal;
  102. point = xform(point);
  103. point_dir = xform(point_dir);
  104. Vector3 normal = point_dir - point;
  105. normal.normalize();
  106. real_t d = normal.dot(point);
  107. return Plane(normal, d);
  108. }
  109. _FORCE_INLINE_ Plane Transform::xform_inv(const Plane &p_plane) const {
  110. Vector3 point = p_plane.normal * p_plane.d;
  111. Vector3 point_dir = point + p_plane.normal;
  112. xform_inv(point);
  113. xform_inv(point_dir);
  114. Vector3 normal = point_dir - point;
  115. normal.normalize();
  116. real_t d = normal.dot(point);
  117. return Plane(normal, d);
  118. }
  119. _FORCE_INLINE_ AABB Transform::xform(const AABB &p_aabb) const {
  120. /* define vertices */
  121. Vector3 x = basis.get_axis(0) * p_aabb.size.x;
  122. Vector3 y = basis.get_axis(1) * p_aabb.size.y;
  123. Vector3 z = basis.get_axis(2) * p_aabb.size.z;
  124. Vector3 pos = xform(p_aabb.position);
  125. //could be even further optimized
  126. AABB new_aabb;
  127. new_aabb.position = pos;
  128. new_aabb.expand_to(pos + x);
  129. new_aabb.expand_to(pos + y);
  130. new_aabb.expand_to(pos + z);
  131. new_aabb.expand_to(pos + x + y);
  132. new_aabb.expand_to(pos + x + z);
  133. new_aabb.expand_to(pos + y + z);
  134. new_aabb.expand_to(pos + x + y + z);
  135. return new_aabb;
  136. }
  137. _FORCE_INLINE_ AABB Transform::xform_inv(const AABB &p_aabb) const {
  138. /* define vertices */
  139. Vector3 vertices[8] = {
  140. 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),
  141. Vector3(p_aabb.position.x + p_aabb.size.x, p_aabb.position.y + p_aabb.size.y, p_aabb.position.z),
  142. Vector3(p_aabb.position.x + p_aabb.size.x, p_aabb.position.y, p_aabb.position.z + p_aabb.size.z),
  143. Vector3(p_aabb.position.x + p_aabb.size.x, p_aabb.position.y, p_aabb.position.z),
  144. Vector3(p_aabb.position.x, p_aabb.position.y + p_aabb.size.y, p_aabb.position.z + p_aabb.size.z),
  145. Vector3(p_aabb.position.x, p_aabb.position.y + p_aabb.size.y, p_aabb.position.z),
  146. Vector3(p_aabb.position.x, p_aabb.position.y, p_aabb.position.z + p_aabb.size.z),
  147. Vector3(p_aabb.position.x, p_aabb.position.y, p_aabb.position.z)
  148. };
  149. AABB ret;
  150. ret.position = xform_inv(vertices[0]);
  151. for (int i = 1; i < 8; i++) {
  152. ret.expand_to(xform_inv(vertices[i]));
  153. }
  154. return ret;
  155. }
  156. #endif // TRANSFORM_H