vector4.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. bool Vector4::is_equal_approx(const Vector4 &p_vec4) const {
  34. 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);
  35. }
  36. real_t Vector4::length() const {
  37. return Math::sqrt(length_squared());
  38. }
  39. void Vector4::normalize() {
  40. *this /= length();
  41. }
  42. Vector4 Vector4::normalized() const {
  43. return *this / length();
  44. }
  45. bool Vector4::is_normalized() const {
  46. return Math::is_equal_approx(length_squared(), 1, (real_t)UNIT_EPSILON); //use less epsilon
  47. }
  48. Vector4 Vector4::abs() const {
  49. return Vector4(Math::abs(x), Math::abs(y), Math::abs(z), Math::abs(w));
  50. }
  51. Vector4 Vector4::sign() const {
  52. return Vector4(SIGN(x), SIGN(y), SIGN(z), SIGN(w));
  53. }
  54. Vector4 Vector4::inverse() const {
  55. return Vector4(1.0f / x, 1.0f / y, 1.0f / z, 1.0f / w);
  56. }
  57. Vector4::Axis Vector4::min_axis_index() const {
  58. uint32_t min_index = 0;
  59. real_t min_value = x;
  60. for (uint32_t i = 1; i < 4; i++) {
  61. if (operator[](i) < min_value) {
  62. min_index = i;
  63. min_value = operator[](i);
  64. }
  65. }
  66. return Vector4::Axis(min_index);
  67. }
  68. Vector4::Axis Vector4::max_axis_index() const {
  69. uint32_t max_index = 0;
  70. real_t max_value = x;
  71. for (uint32_t i = 1; i < 4; i++) {
  72. if (operator[](i) > max_value) {
  73. max_index = i;
  74. max_value = operator[](i);
  75. }
  76. }
  77. return Vector4::Axis(max_index);
  78. }
  79. Vector4 Vector4::clamp(const Vector4 &p_min, const Vector4 &p_max) const {
  80. return Vector4(
  81. CLAMP(x, p_min.x, p_max.x),
  82. CLAMP(y, p_min.y, p_max.y),
  83. CLAMP(z, p_min.z, p_max.z),
  84. CLAMP(w, p_min.w, p_max.w));
  85. }
  86. Vector4::operator String() const {
  87. return "(" + String::num_real(x, false) + ", " + String::num_real(y, false) + ", " + String::num_real(z, false) + ", " + String::num_real(w, false) + ")";
  88. }