transform.h 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. /*************************************************************************/
  2. /* transform.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 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 "aabb.h"
  33. #include "matrix3.h"
  34. #include "plane.h"
  35. /**
  36. @author Juan Linietsky <reduzio@gmail.com>
  37. */
  38. class Transform {
  39. public:
  40. Matrix3 basis;
  41. Vector3 origin;
  42. void invert();
  43. Transform inverse() const;
  44. void affine_invert();
  45. Transform affine_inverse() const;
  46. Transform rotated(const Vector3 &p_axis, real_t p_phi) const;
  47. void rotate(const Vector3 &p_axis, real_t p_phi);
  48. void rotate_basis(const Vector3 &p_axis, real_t p_phi);
  49. void set_look_at(const Vector3 &p_eye, const Vector3 &p_target, const Vector3 &p_up);
  50. Transform looking_at(const Vector3 &p_target, const Vector3 &p_up) const;
  51. void scale(const Vector3 &p_scale);
  52. Transform scaled(const Vector3 &p_scale) const;
  53. void scale_basis(const Vector3 &p_scale);
  54. void translate(real_t p_tx, real_t p_ty, real_t p_tz);
  55. void translate(const Vector3 &p_translation);
  56. Transform translated(const Vector3 &p_translation) const;
  57. const Matrix3 &get_basis() const { return basis; }
  58. void set_basis(const Matrix3 &p_basis) { basis = p_basis; }
  59. const Vector3 &get_origin() const { return origin; }
  60. void set_origin(const Vector3 &p_origin) { origin = p_origin; }
  61. void orthonormalize();
  62. Transform orthonormalized() const;
  63. bool operator==(const Transform &p_transform) const;
  64. bool operator!=(const Transform &p_transform) const;
  65. _FORCE_INLINE_ Vector3 xform(const Vector3 &p_vector) const;
  66. _FORCE_INLINE_ Vector3 xform_inv(const Vector3 &p_vector) const;
  67. _FORCE_INLINE_ Plane xform(const Plane &p_plane) const;
  68. _FORCE_INLINE_ Plane xform_inv(const Plane &p_plane) const;
  69. _FORCE_INLINE_ AABB xform(const AABB &p_aabb) const;
  70. _FORCE_INLINE_ AABB xform_inv(const AABB &p_aabb) const;
  71. void operator*=(const Transform &p_transform);
  72. Transform operator*(const Transform &p_transform) const;
  73. Transform interpolate_with(const Transform &p_transform, float p_c) const;
  74. _FORCE_INLINE_ Transform inverse_xform(const Transform &t) const {
  75. Vector3 v = t.origin - origin;
  76. return Transform(basis.transpose_xform(t.basis),
  77. basis.xform(v));
  78. }
  79. 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) {
  80. basis.elements[0][0] = xx;
  81. basis.elements[0][1] = xy;
  82. basis.elements[0][2] = xz;
  83. basis.elements[1][0] = yx;
  84. basis.elements[1][1] = yy;
  85. basis.elements[1][2] = yz;
  86. basis.elements[2][0] = zx;
  87. basis.elements[2][1] = zy;
  88. basis.elements[2][2] = zz;
  89. origin.x = tx;
  90. origin.y = ty;
  91. origin.z = tz;
  92. }
  93. operator String() const;
  94. Transform(const Matrix3 &p_basis, const Vector3 &p_origin = Vector3());
  95. Transform() {}
  96. };
  97. _FORCE_INLINE_ Vector3 Transform::xform(const Vector3 &p_vector) const {
  98. return Vector3(
  99. basis[0].dot(p_vector) + origin.x,
  100. basis[1].dot(p_vector) + origin.y,
  101. basis[2].dot(p_vector) + origin.z);
  102. }
  103. _FORCE_INLINE_ Vector3 Transform::xform_inv(const Vector3 &p_vector) const {
  104. Vector3 v = p_vector - origin;
  105. return Vector3(
  106. (basis.elements[0][0] * v.x) + (basis.elements[1][0] * v.y) + (basis.elements[2][0] * v.z),
  107. (basis.elements[0][1] * v.x) + (basis.elements[1][1] * v.y) + (basis.elements[2][1] * v.z),
  108. (basis.elements[0][2] * v.x) + (basis.elements[1][2] * v.y) + (basis.elements[2][2] * v.z));
  109. }
  110. _FORCE_INLINE_ Plane Transform::xform(const Plane &p_plane) const {
  111. Vector3 point = p_plane.normal * p_plane.d;
  112. Vector3 point_dir = point + p_plane.normal;
  113. point = xform(point);
  114. point_dir = xform(point_dir);
  115. Vector3 normal = point_dir - point;
  116. normal.normalize();
  117. real_t d = normal.dot(point);
  118. return Plane(normal, d);
  119. }
  120. _FORCE_INLINE_ Plane Transform::xform_inv(const Plane &p_plane) const {
  121. Vector3 point = p_plane.normal * p_plane.d;
  122. Vector3 point_dir = point + p_plane.normal;
  123. xform_inv(point);
  124. xform_inv(point_dir);
  125. Vector3 normal = point_dir - point;
  126. normal.normalize();
  127. real_t d = normal.dot(point);
  128. return Plane(normal, d);
  129. }
  130. _FORCE_INLINE_ AABB Transform::xform(const AABB &p_aabb) const {
  131. /* define vertices */
  132. #if 1
  133. Vector3 x = basis.get_axis(0) * p_aabb.size.x;
  134. Vector3 y = basis.get_axis(1) * p_aabb.size.y;
  135. Vector3 z = basis.get_axis(2) * p_aabb.size.z;
  136. Vector3 pos = xform(p_aabb.pos);
  137. //could be even further optimized
  138. AABB new_aabb;
  139. new_aabb.pos = pos;
  140. new_aabb.expand_to(pos + x);
  141. new_aabb.expand_to(pos + y);
  142. new_aabb.expand_to(pos + z);
  143. new_aabb.expand_to(pos + x + y);
  144. new_aabb.expand_to(pos + x + z);
  145. new_aabb.expand_to(pos + y + z);
  146. new_aabb.expand_to(pos + x + y + z);
  147. return new_aabb;
  148. #else
  149. Vector3 vertices[8] = {
  150. Vector3(p_aabb.pos.x + p_aabb.size.x, p_aabb.pos.y + p_aabb.size.y, p_aabb.pos.z + p_aabb.size.z),
  151. Vector3(p_aabb.pos.x + p_aabb.size.x, p_aabb.pos.y + p_aabb.size.y, p_aabb.pos.z),
  152. Vector3(p_aabb.pos.x + p_aabb.size.x, p_aabb.pos.y, p_aabb.pos.z + p_aabb.size.z),
  153. Vector3(p_aabb.pos.x + p_aabb.size.x, p_aabb.pos.y, p_aabb.pos.z),
  154. Vector3(p_aabb.pos.x, p_aabb.pos.y + p_aabb.size.y, p_aabb.pos.z + p_aabb.size.z),
  155. Vector3(p_aabb.pos.x, p_aabb.pos.y + p_aabb.size.y, p_aabb.pos.z),
  156. Vector3(p_aabb.pos.x, p_aabb.pos.y, p_aabb.pos.z + p_aabb.size.z),
  157. Vector3(p_aabb.pos.x, p_aabb.pos.y, p_aabb.pos.z)
  158. };
  159. AABB ret;
  160. ret.pos = xform(vertices[0]);
  161. for (int i = 1; i < 8; i++) {
  162. ret.expand_to(xform(vertices[i]));
  163. }
  164. return ret;
  165. #endif
  166. }
  167. _FORCE_INLINE_ AABB Transform::xform_inv(const AABB &p_aabb) const {
  168. /* define vertices */
  169. Vector3 vertices[8] = {
  170. Vector3(p_aabb.pos.x + p_aabb.size.x, p_aabb.pos.y + p_aabb.size.y, p_aabb.pos.z + p_aabb.size.z),
  171. Vector3(p_aabb.pos.x + p_aabb.size.x, p_aabb.pos.y + p_aabb.size.y, p_aabb.pos.z),
  172. Vector3(p_aabb.pos.x + p_aabb.size.x, p_aabb.pos.y, p_aabb.pos.z + p_aabb.size.z),
  173. Vector3(p_aabb.pos.x + p_aabb.size.x, p_aabb.pos.y, p_aabb.pos.z),
  174. Vector3(p_aabb.pos.x, p_aabb.pos.y + p_aabb.size.y, p_aabb.pos.z + p_aabb.size.z),
  175. Vector3(p_aabb.pos.x, p_aabb.pos.y + p_aabb.size.y, p_aabb.pos.z),
  176. Vector3(p_aabb.pos.x, p_aabb.pos.y, p_aabb.pos.z + p_aabb.size.z),
  177. Vector3(p_aabb.pos.x, p_aabb.pos.y, p_aabb.pos.z)
  178. };
  179. AABB ret;
  180. ret.pos = xform_inv(vertices[0]);
  181. for (int i = 1; i < 8; i++) {
  182. ret.expand_to(xform_inv(vertices[i]));
  183. }
  184. return ret;
  185. }
  186. #ifdef OPTIMIZED_TRANSFORM_IMPL_OVERRIDE
  187. #else
  188. struct OptimizedTransform {
  189. Transform transform;
  190. _FORCE_INLINE_ void invert() { transform.invert(); }
  191. _FORCE_INLINE_ void affine_invert() { transform.affine_invert(); }
  192. _FORCE_INLINE_ Vector3 xform(const Vector3 &p_vec) const { return transform.xform(p_vec); };
  193. _FORCE_INLINE_ Vector3 xform_inv(const Vector3 &p_vec) const { return transform.xform_inv(p_vec); };
  194. _FORCE_INLINE_ OptimizedTransform operator*(const OptimizedTransform &p_ot) const { return OptimizedTransform(transform * p_ot.transform); }
  195. _FORCE_INLINE_ Transform get_transform() const { return transform; }
  196. _FORCE_INLINE_ void set_transform(const Transform &p_transform) { transform = p_transform; }
  197. OptimizedTransform(const Transform &p_transform) {
  198. transform = p_transform;
  199. }
  200. };
  201. #endif
  202. #endif