transform_3d.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. /*************************************************************************/
  2. /* transform_3d.cpp */
  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. #include "transform_3d.h"
  31. #include "core/math/math_funcs.h"
  32. #include "core/string/print_string.h"
  33. void Transform3D::affine_invert() {
  34. basis.invert();
  35. origin = basis.xform(-origin);
  36. }
  37. Transform3D Transform3D::affine_inverse() const {
  38. Transform3D ret = *this;
  39. ret.affine_invert();
  40. return ret;
  41. }
  42. void Transform3D::invert() {
  43. basis.transpose();
  44. origin = basis.xform(-origin);
  45. }
  46. Transform3D Transform3D::inverse() const {
  47. // FIXME: this function assumes the basis is a rotation matrix, with no scaling.
  48. // Transform3D::affine_inverse can handle matrices with scaling, so GDScript should eventually use that.
  49. Transform3D ret = *this;
  50. ret.invert();
  51. return ret;
  52. }
  53. void Transform3D::rotate(const Vector3 &p_axis, real_t p_angle) {
  54. *this = rotated(p_axis, p_angle);
  55. }
  56. Transform3D Transform3D::rotated(const Vector3 &p_axis, real_t p_angle) const {
  57. return Transform3D(Basis(p_axis, p_angle), Vector3()) * (*this);
  58. }
  59. void Transform3D::rotate_basis(const Vector3 &p_axis, real_t p_angle) {
  60. basis.rotate(p_axis, p_angle);
  61. }
  62. Transform3D Transform3D::looking_at(const Vector3 &p_target, const Vector3 &p_up) const {
  63. #ifdef MATH_CHECKS
  64. ERR_FAIL_COND_V_MSG(origin.is_equal_approx(p_target), Transform3D(), "The transform's origin and target can't be equal.");
  65. #endif
  66. Transform3D t = *this;
  67. t.basis = Basis::looking_at(p_target - origin, p_up);
  68. return t;
  69. }
  70. void Transform3D::set_look_at(const Vector3 &p_eye, const Vector3 &p_target, const Vector3 &p_up) {
  71. #ifdef MATH_CHECKS
  72. ERR_FAIL_COND_MSG(p_eye.is_equal_approx(p_target), "The eye and target vectors can't be equal.");
  73. #endif
  74. basis = Basis::looking_at(p_target - p_eye, p_up);
  75. origin = p_eye;
  76. }
  77. Transform3D Transform3D::spherical_interpolate_with(const Transform3D &p_transform, real_t p_c) const {
  78. /* not sure if very "efficient" but good enough? */
  79. Transform3D interp;
  80. Vector3 src_scale = basis.get_scale();
  81. Quaternion src_rot = basis.get_rotation_quaternion();
  82. Vector3 src_loc = origin;
  83. Vector3 dst_scale = p_transform.basis.get_scale();
  84. Quaternion dst_rot = p_transform.basis.get_rotation_quaternion();
  85. Vector3 dst_loc = p_transform.origin;
  86. interp.basis.set_quaternion_scale(src_rot.slerp(dst_rot, p_c).normalized(), src_scale.lerp(dst_scale, p_c));
  87. interp.origin = src_loc.lerp(dst_loc, p_c);
  88. return interp;
  89. }
  90. Transform3D Transform3D::interpolate_with(const Transform3D &p_transform, real_t p_c) const {
  91. Transform3D interp;
  92. interp.basis = basis.lerp(p_transform.basis, p_c);
  93. interp.origin = origin.lerp(p_transform.origin, p_c);
  94. return interp;
  95. }
  96. void Transform3D::scale(const Vector3 &p_scale) {
  97. basis.scale(p_scale);
  98. origin *= p_scale;
  99. }
  100. Transform3D Transform3D::scaled(const Vector3 &p_scale) const {
  101. Transform3D t = *this;
  102. t.scale(p_scale);
  103. return t;
  104. }
  105. void Transform3D::scale_basis(const Vector3 &p_scale) {
  106. basis.scale(p_scale);
  107. }
  108. void Transform3D::translate_local(real_t p_tx, real_t p_ty, real_t p_tz) {
  109. translate_local(Vector3(p_tx, p_ty, p_tz));
  110. }
  111. void Transform3D::translate_local(const Vector3 &p_translation) {
  112. for (int i = 0; i < 3; i++) {
  113. origin[i] += basis[i].dot(p_translation);
  114. }
  115. }
  116. Transform3D Transform3D::translated_local(const Vector3 &p_translation) const {
  117. Transform3D t = *this;
  118. t.translate_local(p_translation);
  119. return t;
  120. }
  121. void Transform3D::orthonormalize() {
  122. basis.orthonormalize();
  123. }
  124. Transform3D Transform3D::orthonormalized() const {
  125. Transform3D _copy = *this;
  126. _copy.orthonormalize();
  127. return _copy;
  128. }
  129. void Transform3D::orthogonalize() {
  130. basis.orthogonalize();
  131. }
  132. Transform3D Transform3D::orthogonalized() const {
  133. Transform3D _copy = *this;
  134. _copy.orthogonalize();
  135. return _copy;
  136. }
  137. bool Transform3D::is_equal_approx(const Transform3D &p_transform) const {
  138. return basis.is_equal_approx(p_transform.basis) && origin.is_equal_approx(p_transform.origin);
  139. }
  140. bool Transform3D::operator==(const Transform3D &p_transform) const {
  141. return (basis == p_transform.basis && origin == p_transform.origin);
  142. }
  143. bool Transform3D::operator!=(const Transform3D &p_transform) const {
  144. return (basis != p_transform.basis || origin != p_transform.origin);
  145. }
  146. void Transform3D::operator*=(const Transform3D &p_transform) {
  147. origin = xform(p_transform.origin);
  148. basis *= p_transform.basis;
  149. }
  150. Transform3D Transform3D::operator*(const Transform3D &p_transform) const {
  151. Transform3D t = *this;
  152. t *= p_transform;
  153. return t;
  154. }
  155. void Transform3D::operator*=(const real_t p_val) {
  156. origin *= p_val;
  157. basis *= p_val;
  158. }
  159. Transform3D Transform3D::operator*(const real_t p_val) const {
  160. Transform3D ret(*this);
  161. ret *= p_val;
  162. return ret;
  163. }
  164. Transform3D::operator String() const {
  165. return "[X: " + basis.get_column(0).operator String() +
  166. ", Y: " + basis.get_column(1).operator String() +
  167. ", Z: " + basis.get_column(2).operator String() +
  168. ", O: " + origin.operator String() + "]";
  169. }
  170. Transform3D::Transform3D(const Basis &p_basis, const Vector3 &p_origin) :
  171. basis(p_basis),
  172. origin(p_origin) {
  173. }
  174. Transform3D::Transform3D(const Vector3 &p_x, const Vector3 &p_y, const Vector3 &p_z, const Vector3 &p_origin) :
  175. origin(p_origin) {
  176. basis.set_column(0, p_x);
  177. basis.set_column(1, p_y);
  178. basis.set_column(2, p_z);
  179. }
  180. Transform3D::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) {
  181. basis = Basis(xx, xy, xz, yx, yy, yz, zx, zy, zz);
  182. origin = Vector3(ox, oy, oz);
  183. }