vector3.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /*************************************************************************/
  2. /* vector3.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
  9. /* */
  10. /* Permission is hereby granted, free of charge, to any person obtaining */
  11. /* a copy of this software and associated documentation files (the */
  12. /* "Software"), to deal in the Software without restriction, including */
  13. /* without limitation the rights to use, copy, modify, merge, publish, */
  14. /* distribute, sublicense, and/or sell copies of the Software, and to */
  15. /* permit persons to whom the Software is furnished to do so, subject to */
  16. /* the following conditions: */
  17. /* */
  18. /* The above copyright notice and this permission notice shall be */
  19. /* included in all copies or substantial portions of the Software. */
  20. /* */
  21. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  22. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  23. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  24. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  25. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  26. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  27. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  28. /*************************************************************************/
  29. #include "vector3.h"
  30. #include "matrix3.h"
  31. void Vector3::rotate(const Vector3& p_axis,real_t p_phi) {
  32. *this=Basis(p_axis,p_phi).xform(*this);
  33. }
  34. Vector3 Vector3::rotated(const Vector3& p_axis,real_t p_phi) const {
  35. Vector3 r = *this;
  36. r.rotate(p_axis,p_phi);
  37. return r;
  38. }
  39. void Vector3::set_axis(int p_axis,real_t p_value) {
  40. ERR_FAIL_INDEX(p_axis,3);
  41. coord[p_axis]=p_value;
  42. }
  43. real_t Vector3::get_axis(int p_axis) const {
  44. ERR_FAIL_INDEX_V(p_axis,3,0);
  45. return operator[](p_axis);
  46. }
  47. int Vector3::min_axis() const {
  48. return x < y ? (x < z ? 0 : 2) : (y < z ? 1 : 2);
  49. }
  50. int Vector3::max_axis() const {
  51. return x < y ? (y < z ? 2 : 1) : (x < z ? 2 : 0);
  52. }
  53. void Vector3::snap(real_t p_val) {
  54. x=Math::stepify(x,p_val);
  55. y=Math::stepify(y,p_val);
  56. z=Math::stepify(z,p_val);
  57. }
  58. Vector3 Vector3::snapped(real_t p_val) const {
  59. Vector3 v=*this;
  60. v.snap(p_val);
  61. return v;
  62. }
  63. Vector3 Vector3::cubic_interpolaten(const Vector3& p_b,const Vector3& p_pre_a, const Vector3& p_post_b,real_t p_t) const {
  64. Vector3 p0=p_pre_a;
  65. Vector3 p1=*this;
  66. Vector3 p2=p_b;
  67. Vector3 p3=p_post_b;
  68. {
  69. //normalize
  70. real_t ab = p0.distance_to(p1);
  71. real_t bc = p1.distance_to(p2);
  72. real_t cd = p2.distance_to(p3);
  73. if (ab>0)
  74. p0 = p1+(p0-p1)*(bc/ab);
  75. if (cd>0)
  76. p3 = p2+(p3-p2)*(bc/cd);
  77. }
  78. real_t t = p_t;
  79. real_t t2 = t * t;
  80. real_t t3 = t2 * t;
  81. Vector3 out;
  82. out = 0.5 * ( ( p1 * 2.0) +
  83. ( -p0 + p2 ) * t +
  84. ( 2.0 * p0 - 5.0 * p1 + 4 * p2 - p3 ) * t2 +
  85. ( -p0 + 3.0 * p1 - 3.0 * p2 + p3 ) * t3 );
  86. return out;
  87. }
  88. Vector3 Vector3::cubic_interpolate(const Vector3& p_b,const Vector3& p_pre_a, const Vector3& p_post_b,real_t p_t) const {
  89. Vector3 p0=p_pre_a;
  90. Vector3 p1=*this;
  91. Vector3 p2=p_b;
  92. Vector3 p3=p_post_b;
  93. real_t t = p_t;
  94. real_t t2 = t * t;
  95. real_t t3 = t2 * t;
  96. Vector3 out;
  97. out = 0.5 * ( ( p1 * 2.0) +
  98. ( -p0 + p2 ) * t +
  99. ( 2.0 * p0 - 5.0 * p1 + 4 * p2 - p3 ) * t2 +
  100. ( -p0 + 3.0 * p1 - 3.0 * p2 + p3 ) * t3 );
  101. return out;
  102. }
  103. #if 0
  104. Vector3 Vector3::cubic_interpolate(const Vector3& p_b,const Vector3& p_pre_a, const Vector3& p_post_b,real_t p_t) const {
  105. Vector3 p0=p_pre_a;
  106. Vector3 p1=*this;
  107. Vector3 p2=p_b;
  108. Vector3 p3=p_post_b;
  109. if (true) {
  110. real_t ab = p0.distance_to(p1);
  111. real_t bc = p1.distance_to(p2);
  112. real_t cd = p2.distance_to(p3);
  113. //if (ab>bc) {
  114. if (ab>0)
  115. p0 = p1+(p0-p1)*(bc/ab);
  116. //}
  117. //if (cd>bc) {
  118. if (cd>0)
  119. p3 = p2+(p3-p2)*(bc/cd);
  120. //}
  121. }
  122. real_t t = p_t;
  123. real_t t2 = t * t;
  124. real_t t3 = t2 * t;
  125. Vector3 out;
  126. out.x = 0.5 * ( ( 2.0 * p1.x ) +
  127. ( -p0.x + p2.x ) * t +
  128. ( 2.0 * p0.x - 5.0 * p1.x + 4 * p2.x - p3.x ) * t2 +
  129. ( -p0.x + 3.0 * p1.x - 3.0 * p2.x + p3.x ) * t3 );
  130. out.y = 0.5 * ( ( 2.0 * p1.y ) +
  131. ( -p0.y + p2.y ) * t +
  132. ( 2.0 * p0.y - 5.0 * p1.y + 4 * p2.y - p3.y ) * t2 +
  133. ( -p0.y + 3.0 * p1.y - 3.0 * p2.y + p3.y ) * t3 );
  134. out.z = 0.5 * ( ( 2.0 * p1.z ) +
  135. ( -p0.z + p2.z ) * t +
  136. ( 2.0 * p0.z - 5.0 * p1.z + 4 * p2.z - p3.z ) * t2 +
  137. ( -p0.z + 3.0 * p1.z - 3.0 * p2.z + p3.z ) * t3 );
  138. return out;
  139. }
  140. # endif
  141. bool Vector3::nan_equals(const Vector3& p_v) const {
  142. return (x == p_v.x && y == p_v.y && z == p_v.z) ||
  143. (x == p_v.x && y == p_v.y && isnan(z) && isnan(p_v.z)) ||
  144. (x == p_v.x && isnan(y) && isnan(p_v.y) && z == p_v.z) ||
  145. (isnan(x) && isnan(p_v.x) && y == p_v.y && z == p_v.z) ||
  146. (x == p_v.x && isnan(y) && isnan(p_v.y) && isnan(z) && isnan(p_v.z)) ||
  147. (isnan(x) && isnan(p_v.x) && y == p_v.y && isnan(z) && isnan(p_v.z)) ||
  148. (isnan(x) && isnan(p_v.x) && isnan(y) && isnan(p_v.y) && z == p_v.z) ||
  149. (isnan(x) && isnan(p_v.x) && isnan(y) && isnan(p_v.y) && isnan(z) && isnan(p_v.z));
  150. }
  151. Vector3::operator String() const {
  152. return (rtos(x)+", "+rtos(y)+", "+rtos(z));
  153. }