vector3.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /*************************************************************************/
  2. /* vector3.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 "vector3.h"
  31. #include "core/math/basis.h"
  32. #include "core/math/vector2.h"
  33. #include "core/math/vector3i.h"
  34. #include "core/string/ustring.h"
  35. void Vector3::rotate(const Vector3 &p_axis, const real_t p_angle) {
  36. *this = Basis(p_axis, p_angle).xform(*this);
  37. }
  38. Vector3 Vector3::rotated(const Vector3 &p_axis, const real_t p_angle) const {
  39. Vector3 r = *this;
  40. r.rotate(p_axis, p_angle);
  41. return r;
  42. }
  43. void Vector3::set_axis(const int p_axis, const real_t p_value) {
  44. ERR_FAIL_INDEX(p_axis, 3);
  45. coord[p_axis] = p_value;
  46. }
  47. real_t Vector3::get_axis(const int p_axis) const {
  48. ERR_FAIL_INDEX_V(p_axis, 3, 0);
  49. return operator[](p_axis);
  50. }
  51. Vector3 Vector3::clamp(const Vector3 &p_min, const Vector3 &p_max) const {
  52. return Vector3(
  53. CLAMP(x, p_min.x, p_max.x),
  54. CLAMP(y, p_min.y, p_max.y),
  55. CLAMP(z, p_min.z, p_max.z));
  56. }
  57. void Vector3::snap(const Vector3 p_step) {
  58. x = Math::snapped(x, p_step.x);
  59. y = Math::snapped(y, p_step.y);
  60. z = Math::snapped(z, p_step.z);
  61. }
  62. Vector3 Vector3::snapped(const Vector3 p_step) const {
  63. Vector3 v = *this;
  64. v.snap(p_step);
  65. return v;
  66. }
  67. Vector3 Vector3::limit_length(const real_t p_len) const {
  68. const real_t l = length();
  69. Vector3 v = *this;
  70. if (l > 0 && p_len < l) {
  71. v /= l;
  72. v *= p_len;
  73. }
  74. return v;
  75. }
  76. Vector3 Vector3::move_toward(const Vector3 &p_to, const real_t p_delta) const {
  77. Vector3 v = *this;
  78. Vector3 vd = p_to - v;
  79. real_t len = vd.length();
  80. return len <= p_delta || len < (real_t)CMP_EPSILON ? p_to : v + vd / len * p_delta;
  81. }
  82. Vector2 Vector3::octahedron_encode() const {
  83. Vector3 n = *this;
  84. n /= Math::abs(n.x) + Math::abs(n.y) + Math::abs(n.z);
  85. Vector2 o;
  86. if (n.z >= 0.0f) {
  87. o.x = n.x;
  88. o.y = n.y;
  89. } else {
  90. o.x = (1.0f - Math::abs(n.y)) * (n.x >= 0.0f ? 1.0f : -1.0f);
  91. o.y = (1.0f - Math::abs(n.x)) * (n.y >= 0.0f ? 1.0f : -1.0f);
  92. }
  93. o.x = o.x * 0.5f + 0.5f;
  94. o.y = o.y * 0.5f + 0.5f;
  95. return o;
  96. }
  97. Vector3 Vector3::octahedron_decode(const Vector2 &p_oct) {
  98. Vector2 f(p_oct.x * 2.0f - 1.0f, p_oct.y * 2.0f - 1.0f);
  99. Vector3 n(f.x, f.y, 1.0f - Math::abs(f.x) - Math::abs(f.y));
  100. float t = CLAMP(-n.z, 0.0f, 1.0f);
  101. n.x += n.x >= 0 ? -t : t;
  102. n.y += n.y >= 0 ? -t : t;
  103. return n.normalized();
  104. }
  105. Vector2 Vector3::octahedron_tangent_encode(const float sign) const {
  106. Vector2 res = this->octahedron_encode();
  107. res.y = res.y * 0.5f + 0.5f;
  108. res.y = sign >= 0.0f ? res.y : 1 - res.y;
  109. return res;
  110. }
  111. Vector3 Vector3::octahedron_tangent_decode(const Vector2 &p_oct, float *sign) {
  112. Vector2 oct_compressed = p_oct;
  113. oct_compressed.y = oct_compressed.y * 2 - 1;
  114. *sign = oct_compressed.y >= 0.0f ? 1.0f : -1.0f;
  115. oct_compressed.y = Math::abs(oct_compressed.y);
  116. Vector3 res = Vector3::octahedron_decode(oct_compressed);
  117. return res;
  118. }
  119. Basis Vector3::outer(const Vector3 &p_with) const {
  120. Vector3 row0(x * p_with.x, x * p_with.y, x * p_with.z);
  121. Vector3 row1(y * p_with.x, y * p_with.y, y * p_with.z);
  122. Vector3 row2(z * p_with.x, z * p_with.y, z * p_with.z);
  123. return Basis(row0, row1, row2);
  124. }
  125. bool Vector3::is_equal_approx(const Vector3 &p_v) const {
  126. 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);
  127. }
  128. Vector3::operator String() const {
  129. return "(" + String::num_real(x, false) + ", " + String::num_real(y, false) + ", " + String::num_real(z, false) + ")";
  130. }
  131. Vector3::operator Vector3i() const {
  132. return Vector3i(x, y, z);
  133. }