vector3.cpp 5.4 KB

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