2
0

vector3.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /*************************************************************************/
  2. /* vector3.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2016 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,float p_phi) {
  32. *this=Matrix3(p_axis,p_phi).xform(*this);
  33. }
  34. Vector3 Vector3::rotated(const Vector3& p_axis,float 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(float p_val) {
  54. x+=p_val/2.0;
  55. x-=Math::fmod(x,p_val);
  56. y+=p_val/2.0;
  57. y-=Math::fmod(y,p_val);
  58. z+=p_val/2.0;
  59. z-=Math::fmod(z,p_val);
  60. }
  61. Vector3 Vector3::snapped(float p_val) const {
  62. Vector3 v=*this;
  63. v.snap(p_val);
  64. return v;
  65. }
  66. Vector3 Vector3::cubic_interpolaten(const Vector3& p_b,const Vector3& p_pre_a, const Vector3& p_post_b,float p_t) const {
  67. Vector3 p0=p_pre_a;
  68. Vector3 p1=*this;
  69. Vector3 p2=p_b;
  70. Vector3 p3=p_post_b;
  71. {
  72. //normalize
  73. float ab = p0.distance_to(p1);
  74. float bc = p1.distance_to(p2);
  75. float cd = p2.distance_to(p3);
  76. if (ab>0)
  77. p0 = p1+(p0-p1)*(bc/ab);
  78. if (cd>0)
  79. p3 = p2+(p3-p2)*(bc/cd);
  80. }
  81. float t = p_t;
  82. float t2 = t * t;
  83. float t3 = t2 * t;
  84. Vector3 out;
  85. out = 0.5f * ( ( p1 * 2.0f) +
  86. ( -p0 + p2 ) * t +
  87. ( 2.0f * p0 - 5.0f * p1 + 4 * p2 - p3 ) * t2 +
  88. ( -p0 + 3.0f * p1 - 3.0f * p2 + p3 ) * t3 );
  89. return out;
  90. }
  91. Vector3 Vector3::cubic_interpolate(const Vector3& p_b,const Vector3& p_pre_a, const Vector3& p_post_b,float p_t) const {
  92. Vector3 p0=p_pre_a;
  93. Vector3 p1=*this;
  94. Vector3 p2=p_b;
  95. Vector3 p3=p_post_b;
  96. float t = p_t;
  97. float t2 = t * t;
  98. float t3 = t2 * t;
  99. Vector3 out;
  100. out = 0.5f * ( ( p1 * 2.0f) +
  101. ( -p0 + p2 ) * t +
  102. ( 2.0f * p0 - 5.0f * p1 + 4 * p2 - p3 ) * t2 +
  103. ( -p0 + 3.0f * p1 - 3.0f * p2 + p3 ) * t3 );
  104. return out;
  105. }
  106. #if 0
  107. Vector3 Vector3::cubic_interpolate(const Vector3& p_b,const Vector3& p_pre_a, const Vector3& p_post_b,float p_t) const {
  108. Vector3 p0=p_pre_a;
  109. Vector3 p1=*this;
  110. Vector3 p2=p_b;
  111. Vector3 p3=p_post_b;
  112. if (true) {
  113. float ab = p0.distance_to(p1);
  114. float bc = p1.distance_to(p2);
  115. float cd = p2.distance_to(p3);
  116. //if (ab>bc) {
  117. if (ab>0)
  118. p0 = p1+(p0-p1)*(bc/ab);
  119. //}
  120. //if (cd>bc) {
  121. if (cd>0)
  122. p3 = p2+(p3-p2)*(bc/cd);
  123. //}
  124. }
  125. float t = p_t;
  126. float t2 = t * t;
  127. float t3 = t2 * t;
  128. Vector3 out;
  129. out.x = 0.5f * ( ( 2.0f * p1.x ) +
  130. ( -p0.x + p2.x ) * t +
  131. ( 2.0f * p0.x - 5.0f * p1.x + 4 * p2.x - p3.x ) * t2 +
  132. ( -p0.x + 3.0f * p1.x - 3.0f * p2.x + p3.x ) * t3 );
  133. out.y = 0.5f * ( ( 2.0f * p1.y ) +
  134. ( -p0.y + p2.y ) * t +
  135. ( 2.0f * p0.y - 5.0f * p1.y + 4 * p2.y - p3.y ) * t2 +
  136. ( -p0.y + 3.0f * p1.y - 3.0f * p2.y + p3.y ) * t3 );
  137. out.z = 0.5f * ( ( 2.0f * p1.z ) +
  138. ( -p0.z + p2.z ) * t +
  139. ( 2.0f * p0.z - 5.0f * p1.z + 4 * p2.z - p3.z ) * t2 +
  140. ( -p0.z + 3.0f * p1.z - 3.0f * p2.z + p3.z ) * t3 );
  141. return out;
  142. }
  143. # endif
  144. Vector3::operator String() const {
  145. return (rtos(x)+", "+rtos(y)+", "+rtos(z));
  146. }