vector4.cpp 7.8 KB

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