Vector3.cpp 5.5 KB

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