vector3.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /*************************************************************************/
  2. /* vector3.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 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 "matrix3.h"
  32. void Vector3::rotate(const Vector3 &p_axis, float p_phi) {
  33. *this = Matrix3(p_axis, p_phi).xform(*this);
  34. }
  35. Vector3 Vector3::rotated(const Vector3 &p_axis, float p_phi) const {
  36. Vector3 r = *this;
  37. r.rotate(p_axis, p_phi);
  38. return r;
  39. }
  40. void Vector3::set_axis(int p_axis, real_t p_value) {
  41. ERR_FAIL_INDEX(p_axis, 3);
  42. coord[p_axis] = p_value;
  43. }
  44. real_t Vector3::get_axis(int p_axis) const {
  45. ERR_FAIL_INDEX_V(p_axis, 3, 0);
  46. return operator[](p_axis);
  47. }
  48. int Vector3::min_axis() const {
  49. return x < y ? (x < z ? 0 : 2) : (y < z ? 1 : 2);
  50. }
  51. int Vector3::max_axis() const {
  52. return x < y ? (y < z ? 2 : 1) : (x < z ? 2 : 0);
  53. }
  54. void Vector3::snap(float p_val) {
  55. x = Math::stepify(x, p_val);
  56. y = Math::stepify(y, p_val);
  57. z = Math::stepify(z, p_val);
  58. }
  59. Vector3 Vector3::snapped(float p_val) const {
  60. Vector3 v = *this;
  61. v.snap(p_val);
  62. return v;
  63. }
  64. Vector3 Vector3::cubic_interpolaten(const Vector3 &p_b, const Vector3 &p_pre_a, const Vector3 &p_post_b, float p_t) const {
  65. Vector3 p0 = p_pre_a;
  66. Vector3 p1 = *this;
  67. Vector3 p2 = p_b;
  68. Vector3 p3 = p_post_b;
  69. {
  70. //normalize
  71. float ab = p0.distance_to(p1);
  72. float bc = p1.distance_to(p2);
  73. float cd = p2.distance_to(p3);
  74. if (ab > 0)
  75. p0 = p1 + (p0 - p1) * (bc / ab);
  76. if (cd > 0)
  77. p3 = p2 + (p3 - p2) * (bc / cd);
  78. }
  79. float t = p_t;
  80. float t2 = t * t;
  81. float t3 = t2 * t;
  82. Vector3 out;
  83. out = 0.5f * ((p1 * 2.0f) +
  84. (-p0 + p2) * t +
  85. (2.0f * p0 - 5.0f * p1 + 4 * p2 - p3) * t2 +
  86. (-p0 + 3.0f * p1 - 3.0f * p2 + p3) * t3);
  87. return out;
  88. }
  89. Vector3 Vector3::cubic_interpolate(const Vector3 &p_b, const Vector3 &p_pre_a, const Vector3 &p_post_b, float p_t) const {
  90. Vector3 p0 = p_pre_a;
  91. Vector3 p1 = *this;
  92. Vector3 p2 = p_b;
  93. Vector3 p3 = p_post_b;
  94. float t = p_t;
  95. float t2 = t * t;
  96. float t3 = t2 * t;
  97. Vector3 out;
  98. out = 0.5f * ((p1 * 2.0f) +
  99. (-p0 + p2) * t +
  100. (2.0f * p0 - 5.0f * p1 + 4 * p2 - p3) * t2 +
  101. (-p0 + 3.0f * p1 - 3.0f * p2 + p3) * t3);
  102. return out;
  103. }
  104. #if 0
  105. Vector3 Vector3::cubic_interpolate(const Vector3& p_b,const Vector3& p_pre_a, const Vector3& p_post_b,float p_t) const {
  106. Vector3 p0=p_pre_a;
  107. Vector3 p1=*this;
  108. Vector3 p2=p_b;
  109. Vector3 p3=p_post_b;
  110. if (true) {
  111. float ab = p0.distance_to(p1);
  112. float bc = p1.distance_to(p2);
  113. float cd = p2.distance_to(p3);
  114. //if (ab>bc) {
  115. if (ab>0)
  116. p0 = p1+(p0-p1)*(bc/ab);
  117. //}
  118. //if (cd>bc) {
  119. if (cd>0)
  120. p3 = p2+(p3-p2)*(bc/cd);
  121. //}
  122. }
  123. float t = p_t;
  124. float t2 = t * t;
  125. float t3 = t2 * t;
  126. Vector3 out;
  127. out.x = 0.5f * ( ( 2.0f * p1.x ) +
  128. ( -p0.x + p2.x ) * t +
  129. ( 2.0f * p0.x - 5.0f * p1.x + 4 * p2.x - p3.x ) * t2 +
  130. ( -p0.x + 3.0f * p1.x - 3.0f * p2.x + p3.x ) * t3 );
  131. out.y = 0.5f * ( ( 2.0f * p1.y ) +
  132. ( -p0.y + p2.y ) * t +
  133. ( 2.0f * p0.y - 5.0f * p1.y + 4 * p2.y - p3.y ) * t2 +
  134. ( -p0.y + 3.0f * p1.y - 3.0f * p2.y + p3.y ) * t3 );
  135. out.z = 0.5f * ( ( 2.0f * p1.z ) +
  136. ( -p0.z + p2.z ) * t +
  137. ( 2.0f * p0.z - 5.0f * p1.z + 4 * p2.z - p3.z ) * t2 +
  138. ( -p0.z + 3.0f * p1.z - 3.0f * p2.z + p3.z ) * t3 );
  139. return out;
  140. }
  141. #endif
  142. Vector3::operator String() const {
  143. return (rtos(x) + ", " + rtos(y) + ", " + rtos(z));
  144. }