vector3.h 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  1. /*************************************************************************/
  2. /* vector3.h */
  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. #ifndef VECTOR3_H
  30. #define VECTOR3_H
  31. #include "typedefs.h"
  32. #include "math_defs.h"
  33. #include "math_funcs.h"
  34. #include "ustring.h"
  35. struct Vector3 {
  36. enum Axis {
  37. AXIS_X,
  38. AXIS_Y,
  39. AXIS_Z,
  40. };
  41. union {
  42. #ifdef USE_QUAD_VECTORS
  43. struct {
  44. real_t x;
  45. real_t y;
  46. real_t z;
  47. real_t _unused;
  48. };
  49. real_t coord[4];
  50. #else
  51. struct {
  52. real_t x;
  53. real_t y;
  54. real_t z;
  55. };
  56. real_t coord[3];
  57. #endif
  58. };
  59. _FORCE_INLINE_ const real_t& operator[](int p_axis) const {
  60. return coord[p_axis];
  61. }
  62. _FORCE_INLINE_ real_t& operator[](int p_axis) {
  63. return coord[p_axis];
  64. }
  65. void set_axis(int p_axis,real_t p_value);
  66. real_t get_axis(int p_axis) const;
  67. int min_axis() const;
  68. int max_axis() const;
  69. _FORCE_INLINE_ real_t length() const;
  70. _FORCE_INLINE_ real_t length_squared() const;
  71. _FORCE_INLINE_ void normalize();
  72. _FORCE_INLINE_ Vector3 normalized() const;
  73. _FORCE_INLINE_ Vector3 inverse() const;
  74. _FORCE_INLINE_ void zero();
  75. void snap(float p_val);
  76. Vector3 snapped(float p_val) const;
  77. void rotate(const Vector3& p_axis,float p_phi);
  78. Vector3 rotated(const Vector3& p_axis,float p_phi) const;
  79. /* Static Methods between 2 vector3s */
  80. _FORCE_INLINE_ Vector3 linear_interpolate(const Vector3& p_b,float p_t) const;
  81. Vector3 cubic_interpolate(const Vector3& p_b,const Vector3& p_pre_a, const Vector3& p_post_b,float p_t) const;
  82. Vector3 cubic_interpolaten(const Vector3& p_b,const Vector3& p_pre_a, const Vector3& p_post_b,float p_t) const;
  83. _FORCE_INLINE_ Vector3 cross(const Vector3& p_b) const;
  84. _FORCE_INLINE_ real_t dot(const Vector3& p_b) const;
  85. _FORCE_INLINE_ Vector3 abs() const;
  86. _FORCE_INLINE_ Vector3 floor() const;
  87. _FORCE_INLINE_ Vector3 ceil() const;
  88. _FORCE_INLINE_ real_t distance_to(const Vector3& p_b) const;
  89. _FORCE_INLINE_ real_t distance_squared_to(const Vector3& p_b) const;
  90. _FORCE_INLINE_ Vector3 slide(const Vector3& p_vec) const;
  91. _FORCE_INLINE_ Vector3 reflect(const Vector3& p_vec) const;
  92. /* Operators */
  93. _FORCE_INLINE_ Vector3& operator+=(const Vector3& p_v);
  94. _FORCE_INLINE_ Vector3 operator+(const Vector3& p_v) const;
  95. _FORCE_INLINE_ Vector3& operator-=(const Vector3& p_v);
  96. _FORCE_INLINE_ Vector3 operator-(const Vector3& p_v) const;
  97. _FORCE_INLINE_ Vector3& operator*=(const Vector3& p_v);
  98. _FORCE_INLINE_ Vector3 operator*(const Vector3& p_v) const;
  99. _FORCE_INLINE_ Vector3& operator/=(const Vector3& p_v);
  100. _FORCE_INLINE_ Vector3 operator/(const Vector3& p_v) const;
  101. _FORCE_INLINE_ Vector3& operator*=(real_t p_scalar);
  102. _FORCE_INLINE_ Vector3 operator*(real_t p_scalar) const;
  103. _FORCE_INLINE_ Vector3& operator/=(real_t p_scalar);
  104. _FORCE_INLINE_ Vector3 operator/(real_t p_scalar) const;
  105. _FORCE_INLINE_ Vector3 operator-() const;
  106. _FORCE_INLINE_ bool operator==(const Vector3& p_v) const;
  107. _FORCE_INLINE_ bool operator!=(const Vector3& p_v) const;
  108. _FORCE_INLINE_ bool operator<(const Vector3& p_v) const;
  109. _FORCE_INLINE_ bool operator<=(const Vector3& p_v) const;
  110. operator String() const;
  111. _FORCE_INLINE_ Vector3() { x=y=z=0; }
  112. _FORCE_INLINE_ Vector3(real_t p_x,real_t p_y,real_t p_z) { x=p_x; y=p_y; z=p_z; }
  113. };
  114. #ifdef VECTOR3_IMPL_OVERRIDE
  115. #include "vector3_inline.h"
  116. #else
  117. Vector3 Vector3::cross(const Vector3& p_b) const {
  118. Vector3 ret (
  119. (y * p_b.z) - (z * p_b.y),
  120. (z * p_b.x) - (x * p_b.z),
  121. (x * p_b.y) - (y * p_b.x)
  122. );
  123. return ret;
  124. }
  125. real_t Vector3::dot(const Vector3& p_b) const {
  126. return x*p_b.x + y*p_b.y + z*p_b.z;
  127. }
  128. Vector3 Vector3::abs() const {
  129. return Vector3( Math::abs(x), Math::abs(y), Math::abs(z) );
  130. }
  131. Vector3 Vector3::floor() const {
  132. return Vector3( Math::floor(x), Math::floor(y), Math::floor(z) );
  133. }
  134. Vector3 Vector3::ceil() const {
  135. return Vector3( Math::ceil(x), Math::ceil(y), Math::ceil(z) );
  136. }
  137. Vector3 Vector3::linear_interpolate(const Vector3& p_b,float p_t) const {
  138. return Vector3(
  139. x+(p_t * (p_b.x-x)),
  140. y+(p_t * (p_b.y-y)),
  141. z+(p_t * (p_b.z-z))
  142. );
  143. }
  144. real_t Vector3::distance_to(const Vector3& p_b) const {
  145. return (p_b-*this).length();
  146. }
  147. real_t Vector3::distance_squared_to(const Vector3& p_b) const {
  148. return (p_b-*this).length_squared();
  149. }
  150. /* Operators */
  151. Vector3& Vector3::operator+=(const Vector3& p_v) {
  152. x+=p_v.x;
  153. y+=p_v.y;
  154. z+=p_v.z;
  155. return *this;
  156. }
  157. Vector3 Vector3::operator+(const Vector3& p_v) const {
  158. return Vector3(x+p_v.x, y+p_v.y, z+ p_v.z);
  159. }
  160. Vector3& Vector3::operator-=(const Vector3& p_v) {
  161. x-=p_v.x;
  162. y-=p_v.y;
  163. z-=p_v.z;
  164. return *this;
  165. }
  166. Vector3 Vector3::operator-(const Vector3& p_v) const {
  167. return Vector3(x-p_v.x, y-p_v.y, z- p_v.z);
  168. }
  169. Vector3& Vector3::operator*=(const Vector3& p_v) {
  170. x*=p_v.x;
  171. y*=p_v.y;
  172. z*=p_v.z;
  173. return *this;
  174. }
  175. Vector3 Vector3::operator*(const Vector3& p_v) const {
  176. return Vector3(x*p_v.x, y*p_v.y, z* p_v.z);
  177. }
  178. Vector3& Vector3::operator/=(const Vector3& p_v) {
  179. x/=p_v.x;
  180. y/=p_v.y;
  181. z/=p_v.z;
  182. return *this;
  183. }
  184. Vector3 Vector3::operator/(const Vector3& p_v) const {
  185. return Vector3(x/p_v.x, y/p_v.y, z/ p_v.z);
  186. }
  187. Vector3& Vector3::operator*=(real_t p_scalar) {
  188. x*=p_scalar;
  189. y*=p_scalar;
  190. z*=p_scalar;
  191. return *this;
  192. }
  193. _FORCE_INLINE_ Vector3 operator*(real_t p_scalar, const Vector3& p_vec) {
  194. return p_vec * p_scalar;
  195. }
  196. Vector3 Vector3::operator*(real_t p_scalar) const {
  197. return Vector3( x*p_scalar, y*p_scalar, z*p_scalar);
  198. }
  199. Vector3& Vector3::operator/=(real_t p_scalar) {
  200. x/=p_scalar;
  201. y/=p_scalar;
  202. z/=p_scalar;
  203. return *this;
  204. }
  205. Vector3 Vector3::operator/(real_t p_scalar) const {
  206. return Vector3( x/p_scalar, y/p_scalar, z/p_scalar);
  207. }
  208. Vector3 Vector3::operator-() const {
  209. return Vector3( -x, -y, -z );
  210. }
  211. bool Vector3::operator==(const Vector3& p_v) const {
  212. return (x==p_v.x && y==p_v.y && z==p_v.z);
  213. }
  214. bool Vector3::operator!=(const Vector3& p_v) const {
  215. return (x!=p_v.x || y!=p_v.y || z!=p_v.z);
  216. }
  217. bool Vector3::operator<(const Vector3& p_v) const {
  218. if (x==p_v.x) {
  219. if (y==p_v.y)
  220. return z<p_v.z;
  221. else
  222. return y<p_v.y;
  223. } else
  224. return x<p_v.x;
  225. }
  226. bool Vector3::operator<=(const Vector3& p_v) const {
  227. if (x==p_v.x) {
  228. if (y==p_v.y)
  229. return z<=p_v.z;
  230. else
  231. return y<p_v.y;
  232. } else
  233. return x<p_v.x;
  234. }
  235. _FORCE_INLINE_ Vector3 vec3_cross(const Vector3& p_a, const Vector3& p_b) {
  236. return p_a.cross(p_b);
  237. }
  238. _FORCE_INLINE_ real_t vec3_dot(const Vector3& p_a, const Vector3& p_b) {
  239. return p_a.dot(p_b);
  240. }
  241. real_t Vector3::length() const {
  242. real_t x2=x*x;
  243. real_t y2=y*y;
  244. real_t z2=z*z;
  245. return Math::sqrt(x2+y2+z2);
  246. }
  247. real_t Vector3::length_squared() const {
  248. real_t x2=x*x;
  249. real_t y2=y*y;
  250. real_t z2=z*z;
  251. return x2+y2+z2;
  252. }
  253. void Vector3::normalize() {
  254. real_t l=length();
  255. if (l==0) {
  256. x=y=z=0;
  257. } else {
  258. x/=l;
  259. y/=l;
  260. z/=l;
  261. }
  262. }
  263. Vector3 Vector3::normalized() const {
  264. Vector3 v=*this;
  265. v.normalize();
  266. return v;
  267. }
  268. Vector3 Vector3::inverse() const {
  269. return Vector3( 1.0/x, 1.0/y, 1.0/z );
  270. }
  271. void Vector3::zero() {
  272. x=y=z=0;
  273. }
  274. Vector3 Vector3::slide(const Vector3& p_vec) const {
  275. return p_vec - *this * this->dot(p_vec);
  276. }
  277. Vector3 Vector3::reflect(const Vector3& p_vec) const {
  278. return p_vec - *this * this->dot(p_vec) * 2.0;
  279. }
  280. #endif
  281. #endif // VECTOR3_H