transform_3d.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /*************************************************************************/
  2. /* transform_3d.cpp */
  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. #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_phi) {
  54. *this = rotated(p_axis, p_phi);
  55. }
  56. Transform3D Transform3D::rotated(const Vector3 &p_axis, real_t p_phi) const {
  57. return Transform3D(Basis(p_axis, p_phi), Vector3()) * (*this);
  58. }
  59. void Transform3D::rotate_basis(const Vector3 &p_axis, real_t p_phi) {
  60. basis.rotate(p_axis, p_phi);
  61. }
  62. Transform3D Transform3D::looking_at(const Vector3 &p_target, const Vector3 &p_up) const {
  63. Transform3D t = *this;
  64. t.basis = Basis::looking_at(p_target - origin, p_up);
  65. return t;
  66. }
  67. void Transform3D::set_look_at(const Vector3 &p_eye, const Vector3 &p_target, const Vector3 &p_up) {
  68. basis = Basis::looking_at(p_target - p_eye, p_up);
  69. origin = p_eye;
  70. }
  71. Transform3D Transform3D::interpolate_with(const Transform3D &p_transform, real_t p_c) const {
  72. /* not sure if very "efficient" but good enough? */
  73. Vector3 src_scale = basis.get_scale();
  74. Quaternion src_rot = basis.get_rotation_quaternion();
  75. Vector3 src_loc = origin;
  76. Vector3 dst_scale = p_transform.basis.get_scale();
  77. Quaternion dst_rot = p_transform.basis.get_rotation_quaternion();
  78. Vector3 dst_loc = p_transform.origin;
  79. Transform3D interp;
  80. interp.basis.set_quaternion_scale(src_rot.slerp(dst_rot, p_c).normalized(), src_scale.lerp(dst_scale, p_c));
  81. interp.origin = src_loc.lerp(dst_loc, p_c);
  82. return interp;
  83. }
  84. void Transform3D::scale(const Vector3 &p_scale) {
  85. basis.scale(p_scale);
  86. origin *= p_scale;
  87. }
  88. Transform3D Transform3D::scaled(const Vector3 &p_scale) const {
  89. Transform3D t = *this;
  90. t.scale(p_scale);
  91. return t;
  92. }
  93. void Transform3D::scale_basis(const Vector3 &p_scale) {
  94. basis.scale(p_scale);
  95. }
  96. void Transform3D::translate(real_t p_tx, real_t p_ty, real_t p_tz) {
  97. translate(Vector3(p_tx, p_ty, p_tz));
  98. }
  99. void Transform3D::translate(const Vector3 &p_translation) {
  100. for (int i = 0; i < 3; i++) {
  101. origin[i] += basis[i].dot(p_translation);
  102. }
  103. }
  104. Transform3D Transform3D::translated(const Vector3 &p_translation) const {
  105. Transform3D t = *this;
  106. t.translate(p_translation);
  107. return t;
  108. }
  109. void Transform3D::orthonormalize() {
  110. basis.orthonormalize();
  111. }
  112. Transform3D Transform3D::orthonormalized() const {
  113. Transform3D _copy = *this;
  114. _copy.orthonormalize();
  115. return _copy;
  116. }
  117. bool Transform3D::is_equal_approx(const Transform3D &p_transform) const {
  118. return basis.is_equal_approx(p_transform.basis) && origin.is_equal_approx(p_transform.origin);
  119. }
  120. bool Transform3D::operator==(const Transform3D &p_transform) const {
  121. return (basis == p_transform.basis && origin == p_transform.origin);
  122. }
  123. bool Transform3D::operator!=(const Transform3D &p_transform) const {
  124. return (basis != p_transform.basis || origin != p_transform.origin);
  125. }
  126. void Transform3D::operator*=(const Transform3D &p_transform) {
  127. origin = xform(p_transform.origin);
  128. basis *= p_transform.basis;
  129. }
  130. Transform3D Transform3D::operator*(const Transform3D &p_transform) const {
  131. Transform3D t = *this;
  132. t *= p_transform;
  133. return t;
  134. }
  135. void Transform3D::operator*=(const real_t p_val) {
  136. origin *= p_val;
  137. basis *= p_val;
  138. }
  139. Transform3D Transform3D::operator*(const real_t p_val) const {
  140. Transform3D ret(*this);
  141. ret *= p_val;
  142. return ret;
  143. }
  144. Transform3D::operator String() const {
  145. return "[X: " + basis.get_axis(0).operator String() +
  146. ", Y: " + basis.get_axis(1).operator String() +
  147. ", Z: " + basis.get_axis(2).operator String() +
  148. ", O: " + origin.operator String() + "]";
  149. }
  150. Transform3D::Transform3D(const Basis &p_basis, const Vector3 &p_origin) :
  151. basis(p_basis),
  152. origin(p_origin) {
  153. }
  154. Transform3D::Transform3D(const Vector3 &p_x, const Vector3 &p_y, const Vector3 &p_z, const Vector3 &p_origin) :
  155. origin(p_origin) {
  156. basis.set_axis(0, p_x);
  157. basis.set_axis(1, p_y);
  158. basis.set_axis(2, p_z);
  159. }
  160. 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) {
  161. basis = Basis(xx, xy, xz, yx, yy, yz, zx, zy, zz);
  162. origin = Vector3(ox, oy, oz);
  163. }