transform_3d.cpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. /**************************************************************************/
  2. /* transform_3d.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  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/ustring.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. // Equivalent to left multiplication
  58. Basis p_basis(p_axis, p_angle);
  59. return Transform3D(p_basis * basis, p_basis.xform(origin));
  60. }
  61. Transform3D Transform3D::rotated_local(const Vector3 &p_axis, real_t p_angle) const {
  62. // Equivalent to right multiplication
  63. Basis p_basis(p_axis, p_angle);
  64. return Transform3D(basis * p_basis, origin);
  65. }
  66. void Transform3D::rotate_basis(const Vector3 &p_axis, real_t p_angle) {
  67. basis.rotate(p_axis, p_angle);
  68. }
  69. Transform3D Transform3D::looking_at(const Vector3 &p_target, const Vector3 &p_up, bool p_use_model_front) const {
  70. #ifdef MATH_CHECKS
  71. ERR_FAIL_COND_V_MSG(origin.is_equal_approx(p_target), Transform3D(), "The transform's origin and target can't be equal.");
  72. #endif
  73. Transform3D t = *this;
  74. t.basis = Basis::looking_at(p_target - origin, p_up, p_use_model_front);
  75. return t;
  76. }
  77. void Transform3D::set_look_at(const Vector3 &p_eye, const Vector3 &p_target, const Vector3 &p_up, bool p_use_model_front) {
  78. #ifdef MATH_CHECKS
  79. ERR_FAIL_COND_MSG(p_eye.is_equal_approx(p_target), "The eye and target vectors can't be equal.");
  80. #endif
  81. basis = Basis::looking_at(p_target - p_eye, p_up, p_use_model_front);
  82. origin = p_eye;
  83. }
  84. Transform3D Transform3D::interpolate_with(const Transform3D &p_transform, real_t p_c) const {
  85. Transform3D interp;
  86. Vector3 src_scale = basis.get_scale();
  87. Quaternion src_rot = basis.get_rotation_quaternion();
  88. Vector3 src_loc = origin;
  89. Vector3 dst_scale = p_transform.basis.get_scale();
  90. Quaternion dst_rot = p_transform.basis.get_rotation_quaternion();
  91. Vector3 dst_loc = p_transform.origin;
  92. interp.basis.set_quaternion_scale(src_rot.slerp(dst_rot, p_c).normalized(), src_scale.lerp(dst_scale, p_c));
  93. interp.origin = src_loc.lerp(dst_loc, 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. // Equivalent to left multiplication
  102. return Transform3D(basis.scaled(p_scale), origin * p_scale);
  103. }
  104. Transform3D Transform3D::scaled_local(const Vector3 &p_scale) const {
  105. // Equivalent to right multiplication
  106. return Transform3D(basis.scaled_local(p_scale), origin);
  107. }
  108. void Transform3D::scale_basis(const Vector3 &p_scale) {
  109. basis.scale(p_scale);
  110. }
  111. void Transform3D::translate_local(real_t p_tx, real_t p_ty, real_t p_tz) {
  112. translate_local(Vector3(p_tx, p_ty, p_tz));
  113. }
  114. void Transform3D::translate_local(const Vector3 &p_translation) {
  115. for (int i = 0; i < 3; i++) {
  116. origin[i] += basis[i].dot(p_translation);
  117. }
  118. }
  119. Transform3D Transform3D::translated(const Vector3 &p_translation) const {
  120. // Equivalent to left multiplication
  121. return Transform3D(basis, origin + p_translation);
  122. }
  123. Transform3D Transform3D::translated_local(const Vector3 &p_translation) const {
  124. // Equivalent to right multiplication
  125. return Transform3D(basis, origin + basis.xform(p_translation));
  126. }
  127. void Transform3D::orthonormalize() {
  128. basis.orthonormalize();
  129. }
  130. Transform3D Transform3D::orthonormalized() const {
  131. Transform3D _copy = *this;
  132. _copy.orthonormalize();
  133. return _copy;
  134. }
  135. void Transform3D::orthogonalize() {
  136. basis.orthogonalize();
  137. }
  138. Transform3D Transform3D::orthogonalized() const {
  139. Transform3D _copy = *this;
  140. _copy.orthogonalize();
  141. return _copy;
  142. }
  143. bool Transform3D::is_equal_approx(const Transform3D &p_transform) const {
  144. return basis.is_equal_approx(p_transform.basis) && origin.is_equal_approx(p_transform.origin);
  145. }
  146. bool Transform3D::is_finite() const {
  147. return basis.is_finite() && origin.is_finite();
  148. }
  149. bool Transform3D::operator==(const Transform3D &p_transform) const {
  150. return (basis == p_transform.basis && origin == p_transform.origin);
  151. }
  152. bool Transform3D::operator!=(const Transform3D &p_transform) const {
  153. return (basis != p_transform.basis || origin != p_transform.origin);
  154. }
  155. void Transform3D::operator*=(const Transform3D &p_transform) {
  156. origin = xform(p_transform.origin);
  157. basis *= p_transform.basis;
  158. }
  159. Transform3D Transform3D::operator*(const Transform3D &p_transform) const {
  160. Transform3D t = *this;
  161. t *= p_transform;
  162. return t;
  163. }
  164. void Transform3D::operator*=(const real_t p_val) {
  165. origin *= p_val;
  166. basis *= p_val;
  167. }
  168. Transform3D Transform3D::operator*(const real_t p_val) const {
  169. Transform3D ret(*this);
  170. ret *= p_val;
  171. return ret;
  172. }
  173. Transform3D::operator String() const {
  174. return "[X: " + basis.get_column(0).operator String() +
  175. ", Y: " + basis.get_column(1).operator String() +
  176. ", Z: " + basis.get_column(2).operator String() +
  177. ", O: " + origin.operator String() + "]";
  178. }
  179. Transform3D::Transform3D(const Basis &p_basis, const Vector3 &p_origin) :
  180. basis(p_basis),
  181. origin(p_origin) {
  182. }
  183. Transform3D::Transform3D(const Vector3 &p_x, const Vector3 &p_y, const Vector3 &p_z, const Vector3 &p_origin) :
  184. origin(p_origin) {
  185. basis.set_column(0, p_x);
  186. basis.set_column(1, p_y);
  187. basis.set_column(2, p_z);
  188. }
  189. 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) {
  190. basis = Basis(xx, xy, xz, yx, yy, yz, zx, zy, zz);
  191. origin = Vector3(ox, oy, oz);
  192. }