transform.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. /*************************************************************************/
  2. /* transform.cpp */
  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. #include "transform.h"
  31. #include "core/math/math_funcs.h"
  32. #include "core/os/copymem.h"
  33. #include "core/print_string.h"
  34. void Transform::affine_invert() {
  35. basis.invert();
  36. origin = basis.xform(-origin);
  37. }
  38. Transform Transform::affine_inverse() const {
  39. Transform ret = *this;
  40. ret.affine_invert();
  41. return ret;
  42. }
  43. void Transform::invert() {
  44. basis.transpose();
  45. origin = basis.xform(-origin);
  46. }
  47. Transform Transform::inverse() const {
  48. // FIXME: this function assumes the basis is a rotation matrix, with no scaling.
  49. // Transform::affine_inverse can handle matrices with scaling, so GDScript should eventually use that.
  50. Transform ret = *this;
  51. ret.invert();
  52. return ret;
  53. }
  54. void Transform::rotate(const Vector3 &p_axis, real_t p_phi) {
  55. *this = rotated(p_axis, p_phi);
  56. }
  57. Transform Transform::rotated(const Vector3 &p_axis, real_t p_phi) const {
  58. return Transform(Basis(p_axis, p_phi), Vector3()) * (*this);
  59. }
  60. void Transform::rotate_basis(const Vector3 &p_axis, real_t p_phi) {
  61. basis.rotate(p_axis, p_phi);
  62. }
  63. Transform Transform::looking_at(const Vector3 &p_target, const Vector3 &p_up) const {
  64. Transform t = *this;
  65. t.set_look_at(origin, p_target, p_up);
  66. return t;
  67. }
  68. void Transform::set_look_at(const Vector3 &p_eye, const Vector3 &p_target, const Vector3 &p_up) {
  69. #ifdef MATH_CHECKS
  70. ERR_FAIL_COND(p_eye == p_target);
  71. ERR_FAIL_COND(p_up.length() == 0);
  72. #endif
  73. // Reference: MESA source code
  74. Vector3 v_x, v_y, v_z;
  75. /* Make rotation matrix */
  76. /* Z vector */
  77. v_z = p_eye - p_target;
  78. v_z.normalize();
  79. v_y = p_up;
  80. v_x = v_y.cross(v_z);
  81. #ifdef MATH_CHECKS
  82. ERR_FAIL_COND(v_x.length() == 0);
  83. #endif
  84. /* Recompute Y = Z cross X */
  85. v_y = v_z.cross(v_x);
  86. v_x.normalize();
  87. v_y.normalize();
  88. basis.set(v_x, v_y, v_z);
  89. origin = p_eye;
  90. }
  91. Transform Transform::interpolate_with(const Transform &p_transform, real_t p_c) const {
  92. /* not sure if very "efficient" but good enough? */
  93. Vector3 src_scale = basis.get_scale();
  94. Quat src_rot = basis.get_rotation_quat();
  95. Vector3 src_loc = origin;
  96. Vector3 dst_scale = p_transform.basis.get_scale();
  97. Quat dst_rot = p_transform.basis.get_rotation_quat();
  98. Vector3 dst_loc = p_transform.origin;
  99. Transform interp;
  100. interp.basis.set_quat_scale(src_rot.slerp(dst_rot, p_c).normalized(), src_scale.linear_interpolate(dst_scale, p_c));
  101. interp.origin = src_loc.linear_interpolate(dst_loc, p_c);
  102. return interp;
  103. }
  104. void Transform::scale(const Vector3 &p_scale) {
  105. basis.scale(p_scale);
  106. origin *= p_scale;
  107. }
  108. Transform Transform::scaled(const Vector3 &p_scale) const {
  109. Transform t = *this;
  110. t.scale(p_scale);
  111. return t;
  112. }
  113. void Transform::scale_basis(const Vector3 &p_scale) {
  114. basis.scale(p_scale);
  115. }
  116. void Transform::translate(real_t p_tx, real_t p_ty, real_t p_tz) {
  117. translate(Vector3(p_tx, p_ty, p_tz));
  118. }
  119. void Transform::translate(const Vector3 &p_translation) {
  120. for (int i = 0; i < 3; i++) {
  121. origin[i] += basis[i].dot(p_translation);
  122. }
  123. }
  124. Transform Transform::translated(const Vector3 &p_translation) const {
  125. Transform t = *this;
  126. t.translate(p_translation);
  127. return t;
  128. }
  129. void Transform::orthonormalize() {
  130. basis.orthonormalize();
  131. }
  132. Transform Transform::orthonormalized() const {
  133. Transform _copy = *this;
  134. _copy.orthonormalize();
  135. return _copy;
  136. }
  137. bool Transform::is_equal_approx(const Transform &p_transform) const {
  138. return basis.is_equal_approx(p_transform.basis) && origin.is_equal_approx(p_transform.origin);
  139. }
  140. bool Transform::operator==(const Transform &p_transform) const {
  141. return (basis == p_transform.basis && origin == p_transform.origin);
  142. }
  143. bool Transform::operator!=(const Transform &p_transform) const {
  144. return (basis != p_transform.basis || origin != p_transform.origin);
  145. }
  146. void Transform::operator*=(const Transform &p_transform) {
  147. origin = xform(p_transform.origin);
  148. basis *= p_transform.basis;
  149. }
  150. Transform Transform::operator*(const Transform &p_transform) const {
  151. Transform t = *this;
  152. t *= p_transform;
  153. return t;
  154. }
  155. Transform::operator String() const {
  156. return basis.operator String() + " - " + origin.operator String();
  157. }
  158. Transform::Transform(const Basis &p_basis, const Vector3 &p_origin) :
  159. basis(p_basis),
  160. origin(p_origin) {
  161. }
  162. Transform::Transform(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) {
  163. basis = Basis(xx, xy, xz, yx, yy, yz, zx, zy, zz);
  164. origin = Vector3(ox, oy, oz);
  165. }