vector3.h 8.9 KB

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