Vector3.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. #include "Vector3.hpp"
  2. #include "String.hpp"
  3. #include <stdlib.h>
  4. #include <cmath>
  5. #include "Basis.hpp"
  6. namespace godot {
  7. Vector3::Vector3(real_t x, real_t y, real_t z)
  8. {
  9. this->x = x;
  10. this->y = y;
  11. this->z = z;
  12. }
  13. Vector3::Vector3()
  14. {
  15. this->x = 0;
  16. this->y = 0;
  17. this->z = 0;
  18. }
  19. Vector3::Vector3(const Vector3& b)
  20. {
  21. this->x = b.x;
  22. this->y = b.y;
  23. this->z = b.z;
  24. }
  25. const real_t& Vector3::operator[](int p_axis) const
  26. {
  27. return coord[p_axis];
  28. }
  29. real_t& Vector3::operator[](int p_axis)
  30. {
  31. return coord[p_axis];
  32. }
  33. Vector3& Vector3::operator+=(const Vector3& p_v)
  34. {
  35. x += p_v.x;
  36. y += p_v.y;
  37. z += p_v.z;
  38. return *this;
  39. }
  40. Vector3 Vector3::operator+(const Vector3& p_v) const
  41. {
  42. Vector3 v = *this;
  43. v += p_v;
  44. return v;
  45. }
  46. Vector3& Vector3::operator-=(const Vector3& p_v)
  47. {
  48. x -= p_v.x;
  49. y -= p_v.y;
  50. z -= p_v.z;
  51. return *this;
  52. }
  53. Vector3 Vector3::operator-(const Vector3& p_v) const
  54. {
  55. Vector3 v = *this;
  56. v -= p_v;
  57. return v;
  58. }
  59. Vector3& Vector3::operator*=(const Vector3& p_v)
  60. {
  61. x *= p_v.x;
  62. y *= p_v.y;
  63. z *= p_v.z;
  64. return *this;
  65. }
  66. Vector3 Vector3::operator*(const Vector3& p_v) const
  67. {
  68. Vector3 v = *this;
  69. v *= p_v;
  70. return v;
  71. }
  72. Vector3& Vector3::operator/=(const Vector3& p_v)
  73. {
  74. x /= p_v.x;
  75. y /= p_v.y;
  76. z /= p_v.z;
  77. return *this;
  78. }
  79. Vector3 Vector3::operator/(const Vector3& p_v) const
  80. {
  81. Vector3 v = *this;
  82. v /= p_v;
  83. return v;
  84. }
  85. Vector3& Vector3::operator*=(real_t p_scalar)
  86. {
  87. *this *= Vector3(p_scalar, p_scalar, p_scalar);
  88. return *this;
  89. }
  90. Vector3 Vector3::operator*(real_t p_scalar) const
  91. {
  92. Vector3 v = *this;
  93. v *= p_scalar;
  94. return v;
  95. }
  96. Vector3& Vector3::operator/=(real_t p_scalar)
  97. {
  98. *this /= Vector3(p_scalar, p_scalar, p_scalar);
  99. return *this;
  100. }
  101. Vector3 Vector3::operator/(real_t p_scalar) const
  102. {
  103. Vector3 v = *this;
  104. v /= p_scalar;
  105. return v;
  106. }
  107. Vector3 Vector3::operator-() const
  108. {
  109. return Vector3(-x, -y, -z);
  110. }
  111. bool Vector3::operator==(const Vector3& p_v) const
  112. {
  113. return (x==p_v.x && y==p_v.y && z==p_v.z);
  114. }
  115. bool Vector3::operator!=(const Vector3& p_v) const
  116. {
  117. return (x!=p_v.x || y!=p_v.y || z!=p_v.z);
  118. }
  119. bool Vector3::operator<(const Vector3& p_v) const
  120. {
  121. if (x==p_v.x) {
  122. if (y==p_v.y)
  123. return z<p_v.z;
  124. else
  125. return y<p_v.y;
  126. } else {
  127. return x<p_v.x;
  128. }
  129. }
  130. bool Vector3::operator<=(const Vector3& p_v) const
  131. {
  132. if (x==p_v.x) {
  133. if (y==p_v.y)
  134. return z<=p_v.z;
  135. else
  136. return y<p_v.y;
  137. } else {
  138. return x<p_v.x;
  139. }
  140. }
  141. Vector3 Vector3::abs() const
  142. {
  143. return Vector3(::fabs(x), ::fabs(y), ::fabs(z));
  144. }
  145. Vector3 Vector3::ceil() const
  146. {
  147. return Vector3(::ceil(x), ::ceil(y), ::ceil(z));
  148. }
  149. Vector3 Vector3::cross(const Vector3& b) const
  150. {
  151. Vector3 ret (
  152. (y * b.z) - (z * b.y),
  153. (z * b.x) - (x * b.z),
  154. (x * b.y) - (y * b.x)
  155. );
  156. return ret;
  157. }
  158. Vector3 Vector3::linear_interpolate(const Vector3& p_b,real_t p_t) const
  159. {
  160. return Vector3(
  161. x+(p_t * (p_b.x-x)),
  162. y+(p_t * (p_b.y-y)),
  163. z+(p_t * (p_b.z-z))
  164. );
  165. }
  166. Vector3 Vector3::cubic_interpolate(const Vector3& b, const Vector3& pre_a, const Vector3& post_b, const real_t t) const
  167. {
  168. Vector3 p0=pre_a;
  169. Vector3 p1=*this;
  170. Vector3 p2=b;
  171. Vector3 p3=post_b;
  172. real_t t2 = t * t;
  173. real_t t3 = t2 * t;
  174. Vector3 out;
  175. out = ( ( p1 * 2.0) +
  176. ( -p0 + p2 ) * t +
  177. ( p0 * 2.0 - p1 * 5.0 + p2 * 4 - p3 ) * t2 +
  178. ( -p0 + p1 * 3.0 - p2 * 3.0 + p3 ) * t3 ) * 0.5;
  179. return out;
  180. }
  181. real_t Vector3::length() const
  182. {
  183. real_t x2=x*x;
  184. real_t y2=y*y;
  185. real_t z2=z*z;
  186. return ::sqrt(x2+y2+z2);
  187. }
  188. real_t Vector3::length_squared() const
  189. {
  190. real_t x2=x*x;
  191. real_t y2=y*y;
  192. real_t z2=z*z;
  193. return x2+y2+z2;
  194. }
  195. real_t Vector3::distance_squared_to(const Vector3& b) const
  196. {
  197. return (b-*this).length();
  198. }
  199. real_t Vector3::distance_to(const Vector3& b) const
  200. {
  201. return (b-*this).length_squared();
  202. }
  203. real_t Vector3::dot(const Vector3& b) const
  204. {
  205. return x*b.x + y*b.y + z*b.z;
  206. }
  207. Vector3 Vector3::floor() const
  208. {
  209. return Vector3(::floor(x), ::floor(y), ::floor(z));
  210. }
  211. Vector3 Vector3::inverse() const
  212. {
  213. return Vector3( 1.0/x, 1.0/y, 1.0/z );
  214. }
  215. int Vector3::max_axis() const
  216. {
  217. return x < y ? (y < z ? 2 : 1) : (x < z ? 2 : 0);
  218. }
  219. int Vector3::min_axis() const
  220. {
  221. return x < y ? (x < z ? 0 : 2) : (y < z ? 1 : 2);
  222. }
  223. void Vector3::normalize()
  224. {
  225. real_t l=length();
  226. if (l==0) {
  227. x=y=z=0;
  228. } else {
  229. x/=l;
  230. y/=l;
  231. z/=l;
  232. }
  233. }
  234. Vector3 Vector3::normalized() const
  235. {
  236. Vector3 v = *this;
  237. v.normalize();
  238. return v;
  239. }
  240. Vector3 Vector3::reflect(const Vector3& by) const
  241. {
  242. return by - *this * this->dot(by) * 2.0;
  243. }
  244. Vector3 Vector3::rotated(const Vector3& axis, const real_t phi) const
  245. {
  246. Vector3 v = *this;
  247. v.rotate(axis, phi);
  248. return v;
  249. }
  250. void Vector3::rotate(const Vector3& p_axis,real_t p_phi)
  251. {
  252. *this=Basis(p_axis,p_phi).xform(*this);
  253. }
  254. Vector3 Vector3::slide(const Vector3& by) const
  255. {
  256. return by - *this * this->dot(by);
  257. }
  258. // this is ugly as well, but hey, I'm a simple man
  259. #define _ugly_stepify(val, step) (step != 0 ? ::floor(val / step + 0.5) * step : val)
  260. void Vector3::snap(real_t p_val)
  261. {
  262. x = _ugly_stepify(x,p_val);
  263. y = _ugly_stepify(y,p_val);
  264. z = _ugly_stepify(z,p_val);
  265. }
  266. #undef _ugly_stepify
  267. Vector3 Vector3::snapped(const float by)
  268. {
  269. Vector3 v = *this;
  270. v.snap(by);
  271. return v;
  272. }
  273. Vector3::operator String() const
  274. {
  275. return String(); // @Todo
  276. }
  277. }