transform.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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 "math_funcs.h"
  32. #include "os/copymem.h"
  33. #include "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. Transform ret = *this;
  49. ret.invert();
  50. return ret;
  51. }
  52. void Transform::rotate(const Vector3 &p_axis, real_t p_phi) {
  53. *this = *this * Transform(Matrix3(p_axis, p_phi), Vector3());
  54. }
  55. Transform Transform::rotated(const Vector3 &p_axis, real_t p_phi) const {
  56. return *this * Transform(Matrix3(p_axis, p_phi), Vector3());
  57. }
  58. void Transform::rotate_basis(const Vector3 &p_axis, real_t p_phi) {
  59. basis.rotate(p_axis, p_phi);
  60. }
  61. Transform Transform::looking_at(const Vector3 &p_target, const Vector3 &p_up) const {
  62. Transform t = *this;
  63. t.set_look_at(origin, p_target, p_up);
  64. return t;
  65. }
  66. void Transform::set_look_at(const Vector3 &p_eye, const Vector3 &p_target, const Vector3 &p_up) {
  67. // Reference: MESA source code
  68. Vector3 v_x, v_y, v_z;
  69. /* Make rotation matrix */
  70. /* Z vector */
  71. v_z = p_eye - p_target;
  72. v_z.normalize();
  73. v_y = p_up;
  74. v_x = v_y.cross(v_z);
  75. /* Recompute Y = Z cross X */
  76. v_y = v_z.cross(v_x);
  77. v_x.normalize();
  78. v_y.normalize();
  79. basis.set_axis(0, v_x);
  80. basis.set_axis(1, v_y);
  81. basis.set_axis(2, v_z);
  82. origin = p_eye;
  83. }
  84. Transform Transform::interpolate_with(const Transform &p_transform, float p_c) const {
  85. /* not sure if very "efficient" but good enough? */
  86. Vector3 src_scale = basis.get_scale();
  87. Quat src_rot = basis;
  88. Vector3 src_loc = origin;
  89. Vector3 dst_scale = p_transform.basis.get_scale();
  90. Quat dst_rot = p_transform.basis;
  91. Vector3 dst_loc = p_transform.origin;
  92. Transform dst;
  93. dst.basis = src_rot.slerp(dst_rot, p_c);
  94. dst.basis.scale(src_scale.linear_interpolate(dst_scale, p_c));
  95. dst.origin = src_loc.linear_interpolate(dst_loc, p_c);
  96. return dst;
  97. }
  98. void Transform::scale(const Vector3 &p_scale) {
  99. basis.scale(p_scale);
  100. origin *= p_scale;
  101. }
  102. Transform Transform::scaled(const Vector3 &p_scale) const {
  103. Transform t = *this;
  104. t.scale(p_scale);
  105. return t;
  106. }
  107. void Transform::scale_basis(const Vector3 &p_scale) {
  108. basis.scale(p_scale);
  109. }
  110. void Transform::translate(real_t p_tx, real_t p_ty, real_t p_tz) {
  111. translate(Vector3(p_tx, p_ty, p_tz));
  112. }
  113. void Transform::translate(const Vector3 &p_translation) {
  114. for (int i = 0; i < 3; i++) {
  115. origin[i] += basis[i].dot(p_translation);
  116. }
  117. }
  118. Transform Transform::translated(const Vector3 &p_translation) const {
  119. Transform t = *this;
  120. t.translate(p_translation);
  121. return t;
  122. }
  123. void Transform::orthonormalize() {
  124. basis.orthonormalize();
  125. }
  126. Transform Transform::orthonormalized() const {
  127. Transform _copy = *this;
  128. _copy.orthonormalize();
  129. return _copy;
  130. }
  131. bool Transform::operator==(const Transform &p_transform) const {
  132. return (basis == p_transform.basis && origin == p_transform.origin);
  133. }
  134. bool Transform::operator!=(const Transform &p_transform) const {
  135. return (basis != p_transform.basis || origin != p_transform.origin);
  136. }
  137. void Transform::operator*=(const Transform &p_transform) {
  138. origin = xform(p_transform.origin);
  139. basis *= p_transform.basis;
  140. }
  141. Transform Transform::operator*(const Transform &p_transform) const {
  142. Transform t = *this;
  143. t *= p_transform;
  144. return t;
  145. }
  146. Transform::operator String() const {
  147. return basis.operator String() + " - " + origin.operator String();
  148. }
  149. Transform::Transform(const Matrix3 &p_basis, const Vector3 &p_origin) {
  150. basis = p_basis;
  151. origin = p_origin;
  152. }