vector4.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /*************************************************************************/
  2. /* vector4.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 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 "vector4.h"
  31. #include "core/math/basis.h"
  32. #include "core/string/print_string.h"
  33. void Vector4::set_axis(const int p_axis, const real_t p_value) {
  34. ERR_FAIL_INDEX(p_axis, 4);
  35. components[p_axis] = p_value;
  36. }
  37. real_t Vector4::get_axis(const int p_axis) const {
  38. ERR_FAIL_INDEX_V(p_axis, 4, 0);
  39. return operator[](p_axis);
  40. }
  41. Vector4::Axis Vector4::min_axis_index() const {
  42. uint32_t min_index = 0;
  43. real_t min_value = x;
  44. for (uint32_t i = 1; i < 4; i++) {
  45. if (operator[](i) <= min_value) {
  46. min_index = i;
  47. min_value = operator[](i);
  48. }
  49. }
  50. return Vector4::Axis(min_index);
  51. }
  52. Vector4::Axis Vector4::max_axis_index() const {
  53. uint32_t max_index = 0;
  54. real_t max_value = x;
  55. for (uint32_t i = 1; i < 4; i++) {
  56. if (operator[](i) > max_value) {
  57. max_index = i;
  58. max_value = operator[](i);
  59. }
  60. }
  61. return Vector4::Axis(max_index);
  62. }
  63. bool Vector4::is_equal_approx(const Vector4 &p_vec4) const {
  64. 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);
  65. }
  66. real_t Vector4::length() const {
  67. return Math::sqrt(length_squared());
  68. }
  69. void Vector4::normalize() {
  70. *this /= length();
  71. }
  72. Vector4 Vector4::normalized() const {
  73. return *this / length();
  74. }
  75. bool Vector4::is_normalized() const {
  76. return Math::is_equal_approx(length_squared(), 1, (real_t)UNIT_EPSILON); // Use less epsilon.
  77. }
  78. real_t Vector4::distance_to(const Vector4 &p_to) const {
  79. return (p_to - *this).length();
  80. }
  81. real_t Vector4::distance_squared_to(const Vector4 &p_to) const {
  82. return (p_to - *this).length_squared();
  83. }
  84. Vector4 Vector4::direction_to(const Vector4 &p_to) const {
  85. Vector4 ret(p_to.x - x, p_to.y - y, p_to.z - z, p_to.w - w);
  86. ret.normalize();
  87. return ret;
  88. }
  89. Vector4 Vector4::abs() const {
  90. return Vector4(Math::abs(x), Math::abs(y), Math::abs(z), Math::abs(w));
  91. }
  92. Vector4 Vector4::sign() const {
  93. return Vector4(SIGN(x), SIGN(y), SIGN(z), SIGN(w));
  94. }
  95. Vector4 Vector4::floor() const {
  96. return Vector4(Math::floor(x), Math::floor(y), Math::floor(z), Math::floor(w));
  97. }
  98. Vector4 Vector4::ceil() const {
  99. return Vector4(Math::ceil(x), Math::ceil(y), Math::ceil(z), Math::ceil(w));
  100. }
  101. Vector4 Vector4::round() const {
  102. return Vector4(Math::round(x), Math::round(y), Math::round(z), Math::round(w));
  103. }
  104. Vector4 Vector4::lerp(const Vector4 &p_to, const real_t p_weight) const {
  105. return Vector4(
  106. x + (p_weight * (p_to.x - x)),
  107. y + (p_weight * (p_to.y - y)),
  108. z + (p_weight * (p_to.z - z)),
  109. w + (p_weight * (p_to.w - w)));
  110. }
  111. Vector4 Vector4::cubic_interpolate(const Vector4 &p_b, const Vector4 &p_pre_a, const Vector4 &p_post_b, const real_t p_weight) const {
  112. Vector4 res = *this;
  113. res.x = Math::cubic_interpolate(res.x, p_b.x, p_pre_a.x, p_post_b.x, p_weight);
  114. res.y = Math::cubic_interpolate(res.y, p_b.y, p_pre_a.y, p_post_b.y, p_weight);
  115. res.z = Math::cubic_interpolate(res.z, p_b.z, p_pre_a.z, p_post_b.z, p_weight);
  116. res.w = Math::cubic_interpolate(res.w, p_b.w, p_pre_a.w, p_post_b.w, p_weight);
  117. return res;
  118. }
  119. Vector4 Vector4::posmod(const real_t p_mod) const {
  120. return Vector4(Math::fposmod(x, p_mod), Math::fposmod(y, p_mod), Math::fposmod(z, p_mod), Math::fposmod(w, p_mod));
  121. }
  122. Vector4 Vector4::posmodv(const Vector4 &p_modv) const {
  123. 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));
  124. }
  125. void Vector4::snap(const Vector4 &p_step) {
  126. x = Math::snapped(x, p_step.x);
  127. y = Math::snapped(y, p_step.y);
  128. z = Math::snapped(z, p_step.z);
  129. w = Math::snapped(w, p_step.w);
  130. }
  131. Vector4 Vector4::snapped(const Vector4 &p_step) const {
  132. Vector4 v = *this;
  133. v.snap(p_step);
  134. return v;
  135. }
  136. Vector4 Vector4::inverse() const {
  137. return Vector4(1.0f / x, 1.0f / y, 1.0f / z, 1.0f / w);
  138. }
  139. Vector4 Vector4::clamp(const Vector4 &p_min, const Vector4 &p_max) const {
  140. return Vector4(
  141. CLAMP(x, p_min.x, p_max.x),
  142. CLAMP(y, p_min.y, p_max.y),
  143. CLAMP(z, p_min.z, p_max.z),
  144. CLAMP(w, p_min.w, p_max.w));
  145. }
  146. Vector4::operator String() const {
  147. return "(" + String::num_real(x, false) + ", " + String::num_real(y, false) + ", " + String::num_real(z, false) + ", " + String::num_real(w, false) + ")";
  148. }