vector3.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /**************************************************************************/
  2. /* vector3.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 "vector3.h"
  31. #include "core/math/basis.h"
  32. void Vector3::rotate(const Vector3 &p_axis, real_t p_angle) {
  33. *this = Basis(p_axis, p_angle).xform(*this);
  34. }
  35. Vector3 Vector3::rotated(const Vector3 &p_axis, real_t p_angle) const {
  36. Vector3 r = *this;
  37. r.rotate(p_axis, p_angle);
  38. return r;
  39. }
  40. void Vector3::set_axis(int p_axis, real_t p_value) {
  41. ERR_FAIL_INDEX(p_axis, 3);
  42. coord[p_axis] = p_value;
  43. }
  44. real_t Vector3::get_axis(int p_axis) const {
  45. ERR_FAIL_INDEX_V(p_axis, 3, 0);
  46. return operator[](p_axis);
  47. }
  48. void Vector3::snap(const Vector3 &p_val) {
  49. x = Math::stepify(x, p_val.x);
  50. y = Math::stepify(y, p_val.y);
  51. z = Math::stepify(z, p_val.z);
  52. }
  53. Vector3 Vector3::snapped(const Vector3 &p_val) const {
  54. Vector3 v = *this;
  55. v.snap(p_val);
  56. return v;
  57. }
  58. Vector3 Vector3::limit_length(real_t p_len) const {
  59. const real_t l = length();
  60. Vector3 v = *this;
  61. if (l > 0 && p_len < l) {
  62. v /= l;
  63. v *= p_len;
  64. }
  65. return v;
  66. }
  67. Vector3 Vector3::cubic_interpolaten(const Vector3 &p_b, const Vector3 &p_pre_a, const Vector3 &p_post_b, real_t p_weight) const {
  68. Vector3 p0 = p_pre_a;
  69. Vector3 p1 = *this;
  70. Vector3 p2 = p_b;
  71. Vector3 p3 = p_post_b;
  72. {
  73. //normalize
  74. real_t ab = p0.distance_to(p1);
  75. real_t bc = p1.distance_to(p2);
  76. real_t cd = p2.distance_to(p3);
  77. if (ab > 0) {
  78. p0 = p1 + (p0 - p1) * (bc / ab);
  79. }
  80. if (cd > 0) {
  81. p3 = p2 + (p3 - p2) * (bc / cd);
  82. }
  83. }
  84. real_t t = p_weight;
  85. real_t t2 = t * t;
  86. real_t t3 = t2 * t;
  87. Vector3 out;
  88. out = 0.5f *
  89. ((p1 * 2) +
  90. (-p0 + p2) * t +
  91. (2 * p0 - 5 * p1 + 4 * p2 - p3) * t2 +
  92. (-p0 + 3 * p1 - 3 * p2 + p3) * t3);
  93. return out;
  94. }
  95. Vector3 Vector3::cubic_interpolate(const Vector3 &p_b, const Vector3 &p_pre_a, const Vector3 &p_post_b, real_t p_weight) const {
  96. Vector3 p0 = p_pre_a;
  97. Vector3 p1 = *this;
  98. Vector3 p2 = p_b;
  99. Vector3 p3 = p_post_b;
  100. real_t t = p_weight;
  101. real_t t2 = t * t;
  102. real_t t3 = t2 * t;
  103. Vector3 out;
  104. out = 0.5f *
  105. ((p1 * 2) +
  106. (-p0 + p2) * t +
  107. (2 * p0 - 5 * p1 + 4 * p2 - p3) * t2 +
  108. (-p0 + 3 * p1 - 3 * p2 + p3) * t3);
  109. return out;
  110. }
  111. Vector3 Vector3::move_toward(const Vector3 &p_to, real_t p_delta) const {
  112. Vector3 v = *this;
  113. Vector3 vd = p_to - v;
  114. real_t len = vd.length();
  115. return len <= p_delta || len < (real_t)CMP_EPSILON ? p_to : v + vd / len * p_delta;
  116. }
  117. Basis Vector3::outer(const Vector3 &p_b) const {
  118. Vector3 row0(x * p_b.x, x * p_b.y, x * p_b.z);
  119. Vector3 row1(y * p_b.x, y * p_b.y, y * p_b.z);
  120. Vector3 row2(z * p_b.x, z * p_b.y, z * p_b.z);
  121. return Basis(row0, row1, row2);
  122. }
  123. Basis Vector3::to_diagonal_matrix() const {
  124. return Basis(x, 0, 0,
  125. 0, y, 0,
  126. 0, 0, z);
  127. }
  128. bool Vector3::is_equal_approx(const Vector3 &p_v) const {
  129. return Math::is_equal_approx(x, p_v.x) && Math::is_equal_approx(y, p_v.y) && Math::is_equal_approx(z, p_v.z);
  130. }
  131. Vector3::operator String() const {
  132. return (rtos(x) + ", " + rtos(y) + ", " + rtos(z));
  133. }