transform_3d.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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. 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::spherical_interpolate_with(const Transform3D &p_transform, real_t p_c) const {
  72. /* not sure if very "efficient" but good enough? */
  73. Transform3D interp;
  74. Vector3 src_scale = basis.get_scale();
  75. Quaternion src_rot = basis.get_rotation_quaternion();
  76. Vector3 src_loc = origin;
  77. Vector3 dst_scale = p_transform.basis.get_scale();
  78. Quaternion dst_rot = p_transform.basis.get_rotation_quaternion();
  79. Vector3 dst_loc = p_transform.origin;
  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. Transform3D Transform3D::interpolate_with(const Transform3D &p_transform, real_t p_c) const {
  85. Transform3D interp;
  86. interp.basis = basis.lerp(p_transform.basis, p_c);
  87. interp.origin = origin.lerp(p_transform.origin, p_c);
  88. return interp;
  89. }
  90. void Transform3D::scale(const Vector3 &p_scale) {
  91. basis.scale(p_scale);
  92. origin *= p_scale;
  93. }
  94. Transform3D Transform3D::scaled(const Vector3 &p_scale) const {
  95. Transform3D t = *this;
  96. t.scale(p_scale);
  97. return t;
  98. }
  99. void Transform3D::scale_basis(const Vector3 &p_scale) {
  100. basis.scale(p_scale);
  101. }
  102. void Transform3D::translate(real_t p_tx, real_t p_ty, real_t p_tz) {
  103. translate(Vector3(p_tx, p_ty, p_tz));
  104. }
  105. void Transform3D::translate(const Vector3 &p_translation) {
  106. for (int i = 0; i < 3; i++) {
  107. origin[i] += basis[i].dot(p_translation);
  108. }
  109. }
  110. Transform3D Transform3D::translated(const Vector3 &p_translation) const {
  111. Transform3D t = *this;
  112. t.translate(p_translation);
  113. return t;
  114. }
  115. void Transform3D::orthonormalize() {
  116. basis.orthonormalize();
  117. }
  118. Transform3D Transform3D::orthonormalized() const {
  119. Transform3D _copy = *this;
  120. _copy.orthonormalize();
  121. return _copy;
  122. }
  123. void Transform3D::orthogonalize() {
  124. basis.orthogonalize();
  125. }
  126. Transform3D Transform3D::orthogonalized() const {
  127. Transform3D _copy = *this;
  128. _copy.orthogonalize();
  129. return _copy;
  130. }
  131. bool Transform3D::is_equal_approx(const Transform3D &p_transform) const {
  132. return basis.is_equal_approx(p_transform.basis) && origin.is_equal_approx(p_transform.origin);
  133. }
  134. bool Transform3D::operator==(const Transform3D &p_transform) const {
  135. return (basis == p_transform.basis && origin == p_transform.origin);
  136. }
  137. bool Transform3D::operator!=(const Transform3D &p_transform) const {
  138. return (basis != p_transform.basis || origin != p_transform.origin);
  139. }
  140. void Transform3D::operator*=(const Transform3D &p_transform) {
  141. origin = xform(p_transform.origin);
  142. basis *= p_transform.basis;
  143. }
  144. Transform3D Transform3D::operator*(const Transform3D &p_transform) const {
  145. Transform3D t = *this;
  146. t *= p_transform;
  147. return t;
  148. }
  149. void Transform3D::operator*=(const real_t p_val) {
  150. origin *= p_val;
  151. basis *= p_val;
  152. }
  153. Transform3D Transform3D::operator*(const real_t p_val) const {
  154. Transform3D ret(*this);
  155. ret *= p_val;
  156. return ret;
  157. }
  158. Transform3D::operator String() const {
  159. return "[X: " + basis.get_column(0).operator String() +
  160. ", Y: " + basis.get_column(1).operator String() +
  161. ", Z: " + basis.get_column(2).operator String() +
  162. ", O: " + origin.operator String() + "]";
  163. }
  164. Transform3D::Transform3D(const Basis &p_basis, const Vector3 &p_origin) :
  165. basis(p_basis),
  166. origin(p_origin) {
  167. }
  168. Transform3D::Transform3D(const Vector3 &p_x, const Vector3 &p_y, const Vector3 &p_z, const Vector3 &p_origin) :
  169. origin(p_origin) {
  170. basis.set_column(0, p_x);
  171. basis.set_column(1, p_y);
  172. basis.set_column(2, p_z);
  173. }
  174. 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) {
  175. basis = Basis(xx, xy, xz, yx, yy, yz, zx, zy, zz);
  176. origin = Vector3(ox, oy, oz);
  177. }