vector4.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /**************************************************************************/
  2. /* vector4.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 "vector4.h"
  31. #include "core/string/ustring.h"
  32. Vector4::Axis Vector4::min_axis_index() const {
  33. uint32_t min_index = 0;
  34. real_t min_value = x;
  35. for (uint32_t i = 1; i < 4; i++) {
  36. if (operator[](i) <= min_value) {
  37. min_index = i;
  38. min_value = operator[](i);
  39. }
  40. }
  41. return Vector4::Axis(min_index);
  42. }
  43. Vector4::Axis Vector4::max_axis_index() const {
  44. uint32_t max_index = 0;
  45. real_t max_value = x;
  46. for (uint32_t i = 1; i < 4; i++) {
  47. if (operator[](i) > max_value) {
  48. max_index = i;
  49. max_value = operator[](i);
  50. }
  51. }
  52. return Vector4::Axis(max_index);
  53. }
  54. bool Vector4::is_equal_approx(const Vector4 &p_vec4) const {
  55. return Math::is_equal_approx(x, p_vec4.x) && Math::is_equal_approx(y, p_vec4.y) && Math::is_equal_approx(z, p_vec4.z) && Math::is_equal_approx(w, p_vec4.w);
  56. }
  57. bool Vector4::is_zero_approx() const {
  58. return Math::is_zero_approx(x) && Math::is_zero_approx(y) && Math::is_zero_approx(z) && Math::is_zero_approx(w);
  59. }
  60. bool Vector4::is_finite() const {
  61. return Math::is_finite(x) && Math::is_finite(y) && Math::is_finite(z) && Math::is_finite(w);
  62. }
  63. real_t Vector4::length() const {
  64. return Math::sqrt(length_squared());
  65. }
  66. void Vector4::normalize() {
  67. real_t lengthsq = length_squared();
  68. if (lengthsq == 0) {
  69. x = y = z = w = 0;
  70. } else {
  71. real_t length = Math::sqrt(lengthsq);
  72. x /= length;
  73. y /= length;
  74. z /= length;
  75. w /= length;
  76. }
  77. }
  78. Vector4 Vector4::normalized() const {
  79. Vector4 v = *this;
  80. v.normalize();
  81. return v;
  82. }
  83. bool Vector4::is_normalized() const {
  84. return Math::is_equal_approx(length_squared(), (real_t)1, (real_t)UNIT_EPSILON);
  85. }
  86. real_t Vector4::distance_to(const Vector4 &p_to) const {
  87. return (p_to - *this).length();
  88. }
  89. real_t Vector4::distance_squared_to(const Vector4 &p_to) const {
  90. return (p_to - *this).length_squared();
  91. }
  92. Vector4 Vector4::direction_to(const Vector4 &p_to) const {
  93. Vector4 ret(p_to.x - x, p_to.y - y, p_to.z - z, p_to.w - w);
  94. ret.normalize();
  95. return ret;
  96. }
  97. Vector4 Vector4::abs() const {
  98. return Vector4(Math::abs(x), Math::abs(y), Math::abs(z), Math::abs(w));
  99. }
  100. Vector4 Vector4::sign() const {
  101. return Vector4(SIGN(x), SIGN(y), SIGN(z), SIGN(w));
  102. }
  103. Vector4 Vector4::floor() const {
  104. return Vector4(Math::floor(x), Math::floor(y), Math::floor(z), Math::floor(w));
  105. }
  106. Vector4 Vector4::ceil() const {
  107. return Vector4(Math::ceil(x), Math::ceil(y), Math::ceil(z), Math::ceil(w));
  108. }
  109. Vector4 Vector4::round() const {
  110. return Vector4(Math::round(x), Math::round(y), Math::round(z), Math::round(w));
  111. }
  112. Vector4 Vector4::lerp(const Vector4 &p_to, const real_t p_weight) const {
  113. Vector4 res = *this;
  114. res.x = Math::lerp(res.x, p_to.x, p_weight);
  115. res.y = Math::lerp(res.y, p_to.y, p_weight);
  116. res.z = Math::lerp(res.z, p_to.z, p_weight);
  117. res.w = Math::lerp(res.w, p_to.w, p_weight);
  118. return res;
  119. }
  120. Vector4 Vector4::cubic_interpolate(const Vector4 &p_b, const Vector4 &p_pre_a, const Vector4 &p_post_b, const real_t p_weight) const {
  121. Vector4 res = *this;
  122. res.x = Math::cubic_interpolate(res.x, p_b.x, p_pre_a.x, p_post_b.x, p_weight);
  123. res.y = Math::cubic_interpolate(res.y, p_b.y, p_pre_a.y, p_post_b.y, p_weight);
  124. res.z = Math::cubic_interpolate(res.z, p_b.z, p_pre_a.z, p_post_b.z, p_weight);
  125. res.w = Math::cubic_interpolate(res.w, p_b.w, p_pre_a.w, p_post_b.w, p_weight);
  126. return res;
  127. }
  128. Vector4 Vector4::cubic_interpolate_in_time(const Vector4 &p_b, const Vector4 &p_pre_a, const Vector4 &p_post_b, const real_t p_weight, const real_t &p_b_t, const real_t &p_pre_a_t, const real_t &p_post_b_t) const {
  129. Vector4 res = *this;
  130. res.x = Math::cubic_interpolate_in_time(res.x, p_b.x, p_pre_a.x, p_post_b.x, p_weight, p_b_t, p_pre_a_t, p_post_b_t);
  131. res.y = Math::cubic_interpolate_in_time(res.y, p_b.y, p_pre_a.y, p_post_b.y, p_weight, p_b_t, p_pre_a_t, p_post_b_t);
  132. res.z = Math::cubic_interpolate_in_time(res.z, p_b.z, p_pre_a.z, p_post_b.z, p_weight, p_b_t, p_pre_a_t, p_post_b_t);
  133. res.w = Math::cubic_interpolate_in_time(res.w, p_b.w, p_pre_a.w, p_post_b.w, p_weight, p_b_t, p_pre_a_t, p_post_b_t);
  134. return res;
  135. }
  136. Vector4 Vector4::posmod(const real_t p_mod) const {
  137. return Vector4(Math::fposmod(x, p_mod), Math::fposmod(y, p_mod), Math::fposmod(z, p_mod), Math::fposmod(w, p_mod));
  138. }
  139. Vector4 Vector4::posmodv(const Vector4 &p_modv) const {
  140. return Vector4(Math::fposmod(x, p_modv.x), Math::fposmod(y, p_modv.y), Math::fposmod(z, p_modv.z), Math::fposmod(w, p_modv.w));
  141. }
  142. void Vector4::snap(const Vector4 &p_step) {
  143. x = Math::snapped(x, p_step.x);
  144. y = Math::snapped(y, p_step.y);
  145. z = Math::snapped(z, p_step.z);
  146. w = Math::snapped(w, p_step.w);
  147. }
  148. Vector4 Vector4::snapped(const Vector4 &p_step) const {
  149. Vector4 v = *this;
  150. v.snap(p_step);
  151. return v;
  152. }
  153. Vector4 Vector4::inverse() const {
  154. return Vector4(1.0f / x, 1.0f / y, 1.0f / z, 1.0f / w);
  155. }
  156. Vector4 Vector4::clamp(const Vector4 &p_min, const Vector4 &p_max) const {
  157. return Vector4(
  158. CLAMP(x, p_min.x, p_max.x),
  159. CLAMP(y, p_min.y, p_max.y),
  160. CLAMP(z, p_min.z, p_max.z),
  161. CLAMP(w, p_min.w, p_max.w));
  162. }
  163. Vector4::operator String() const {
  164. return "(" + String::num_real(x, false) + ", " + String::num_real(y, false) + ", " + String::num_real(z, false) + ", " + String::num_real(w, false) + ")";
  165. }
  166. static_assert(sizeof(Vector4) == 4 * sizeof(real_t));