2
0

vector3.h 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. /*************************************************************************/
  2. /* vector3.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2015 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_ real_t distance_to(const Vector3& p_b) const;
  87. _FORCE_INLINE_ real_t distance_squared_to(const Vector3& p_b) const;
  88. _FORCE_INLINE_ Vector3 slide(const Vector3& p_vec) const;
  89. _FORCE_INLINE_ Vector3 reflect(const Vector3& p_vec) const;
  90. /* Operators */
  91. _FORCE_INLINE_ Vector3& operator+=(const Vector3& p_v);
  92. _FORCE_INLINE_ Vector3 operator+(const Vector3& p_v) const;
  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*=(real_t p_scalar);
  100. _FORCE_INLINE_ Vector3 operator*(real_t p_scalar) 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-() const;
  104. _FORCE_INLINE_ bool operator==(const Vector3& p_v) const;
  105. _FORCE_INLINE_ bool operator!=(const Vector3& p_v) const;
  106. _FORCE_INLINE_ bool operator<(const Vector3& p_v) const;
  107. _FORCE_INLINE_ bool operator<=(const Vector3& p_v) const;
  108. operator String() const;
  109. _FORCE_INLINE_ Vector3() { x=y=z=0; }
  110. _FORCE_INLINE_ Vector3(real_t p_x,real_t p_y,real_t p_z) { x=p_x; y=p_y; z=p_z; }
  111. };
  112. #ifdef VECTOR3_IMPL_OVERRIDE
  113. #include "vector3_inline.h"
  114. #else
  115. Vector3 Vector3::cross(const Vector3& p_b) const {
  116. Vector3 ret (
  117. (y * p_b.z) - (z * p_b.y),
  118. (z * p_b.x) - (x * p_b.z),
  119. (x * p_b.y) - (y * p_b.x)
  120. );
  121. return ret;
  122. }
  123. real_t Vector3::dot(const Vector3& p_b) const {
  124. return x*p_b.x + y*p_b.y + z*p_b.z;
  125. }
  126. Vector3 Vector3::abs() const {
  127. return Vector3( Math::abs(x), Math::abs(y), Math::abs(z) );
  128. }
  129. Vector3 Vector3::linear_interpolate(const Vector3& p_b,float p_t) const {
  130. return Vector3(
  131. x+(p_t * (p_b.x-x)),
  132. y+(p_t * (p_b.y-y)),
  133. z+(p_t * (p_b.z-z))
  134. );
  135. }
  136. real_t Vector3::distance_to(const Vector3& p_b) const {
  137. return (p_b-*this).length();
  138. }
  139. real_t Vector3::distance_squared_to(const Vector3& p_b) const {
  140. return (p_b-*this).length_squared();
  141. }
  142. /* Operators */
  143. Vector3& Vector3::operator+=(const Vector3& p_v) {
  144. x+=p_v.x;
  145. y+=p_v.y;
  146. z+=p_v.z;
  147. return *this;
  148. }
  149. Vector3 Vector3::operator+(const Vector3& p_v) const {
  150. return Vector3(x+p_v.x, y+p_v.y, z+ p_v.z);
  151. }
  152. Vector3& Vector3::operator-=(const Vector3& p_v) {
  153. x-=p_v.x;
  154. y-=p_v.y;
  155. z-=p_v.z;
  156. return *this;
  157. }
  158. Vector3 Vector3::operator-(const Vector3& p_v) const {
  159. return Vector3(x-p_v.x, y-p_v.y, z- p_v.z);
  160. }
  161. Vector3& Vector3::operator*=(const Vector3& p_v) {
  162. x*=p_v.x;
  163. y*=p_v.y;
  164. z*=p_v.z;
  165. return *this;
  166. }
  167. Vector3 Vector3::operator*(const Vector3& p_v) const {
  168. return Vector3(x*p_v.x, y*p_v.y, z* p_v.z);
  169. }
  170. Vector3& Vector3::operator/=(const Vector3& p_v) {
  171. x/=p_v.x;
  172. y/=p_v.y;
  173. z/=p_v.z;
  174. return *this;
  175. }
  176. Vector3 Vector3::operator/(const Vector3& p_v) const {
  177. return Vector3(x/p_v.x, y/p_v.y, z/ p_v.z);
  178. }
  179. Vector3& Vector3::operator*=(real_t p_scalar) {
  180. x*=p_scalar;
  181. y*=p_scalar;
  182. z*=p_scalar;
  183. return *this;
  184. }
  185. _FORCE_INLINE_ Vector3 operator*(real_t p_scalar, const Vector3& p_vec) {
  186. return p_vec * p_scalar;
  187. }
  188. Vector3 Vector3::operator*(real_t p_scalar) const {
  189. return Vector3( x*p_scalar, y*p_scalar, z*p_scalar);
  190. }
  191. Vector3& Vector3::operator/=(real_t p_scalar) {
  192. x/=p_scalar;
  193. y/=p_scalar;
  194. z/=p_scalar;
  195. return *this;
  196. }
  197. Vector3 Vector3::operator/(real_t p_scalar) const {
  198. return Vector3( x/p_scalar, y/p_scalar, z/p_scalar);
  199. }
  200. Vector3 Vector3::operator-() const {
  201. return Vector3( -x, -y, -z );
  202. }
  203. bool Vector3::operator==(const Vector3& p_v) const {
  204. return (x==p_v.x && y==p_v.y && z==p_v.z);
  205. }
  206. bool Vector3::operator!=(const Vector3& p_v) const {
  207. return (x!=p_v.x || y!=p_v.y || z!=p_v.z);
  208. }
  209. bool Vector3::operator<(const Vector3& p_v) const {
  210. if (x==p_v.x) {
  211. if (y==p_v.y)
  212. return z<p_v.z;
  213. else
  214. return y<p_v.y;
  215. } else
  216. return x<p_v.x;
  217. }
  218. bool Vector3::operator<=(const Vector3& p_v) const {
  219. if (x==p_v.x) {
  220. if (y==p_v.y)
  221. return z<=p_v.z;
  222. else
  223. return y<p_v.y;
  224. } else
  225. return x<p_v.x;
  226. }
  227. _FORCE_INLINE_ Vector3 vec3_cross(const Vector3& p_a, const Vector3& p_b) {
  228. return p_a.cross(p_b);
  229. }
  230. _FORCE_INLINE_ real_t vec3_dot(const Vector3& p_a, const Vector3& p_b) {
  231. return p_a.dot(p_b);
  232. }
  233. real_t Vector3::length() const {
  234. real_t x2=x*x;
  235. real_t y2=y*y;
  236. real_t z2=z*z;
  237. return Math::sqrt(x2+y2+z2);
  238. }
  239. real_t Vector3::length_squared() const {
  240. real_t x2=x*x;
  241. real_t y2=y*y;
  242. real_t z2=z*z;
  243. return x2+y2+z2;
  244. }
  245. void Vector3::normalize() {
  246. real_t l=length();
  247. if (l==0) {
  248. x=y=z=0;
  249. } else {
  250. x/=l;
  251. y/=l;
  252. z/=l;
  253. }
  254. }
  255. Vector3 Vector3::normalized() const {
  256. Vector3 v=*this;
  257. v.normalize();
  258. return v;
  259. }
  260. Vector3 Vector3::inverse() const {
  261. return Vector3( 1.0/x, 1.0/y, 1.0/z );
  262. }
  263. void Vector3::zero() {
  264. x=y=z=0;
  265. }
  266. Vector3 Vector3::slide(const Vector3& p_vec) const {
  267. return p_vec - *this * this->dot(p_vec);
  268. }
  269. Vector3 Vector3::reflect(const Vector3& p_vec) const {
  270. return p_vec - *this * this->dot(p_vec) * 2.0;
  271. }
  272. #endif
  273. #endif // VECTOR3_H