Vector3.h 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. /*
  2. * Vector3.h
  3. */
  4. #ifndef VECTOR3_H_
  5. #define VECTOR3_H_
  6. namespace gameplay
  7. {
  8. // Forward declare
  9. class Matrix;
  10. class Quaternion;
  11. /**
  12. * Defines a 3-element floating point vector.
  13. *
  14. * When using a vector to represent a surface normal,
  15. * the vector should typically be normalized.
  16. * Other uses of directional vectors may wish to leave
  17. * the magnitude of the vector in-tact. When used as a point,
  18. * the elements of the vector represent a position in 3D space.
  19. */
  20. class Vector3
  21. {
  22. public:
  23. /**
  24. * The x-coordinate.
  25. */
  26. float x;
  27. /**
  28. * The y-coordinate.
  29. */
  30. float y;
  31. /**
  32. * The z-coordinate.
  33. */
  34. float z;
  35. /**
  36. * Constructs a new vector initialized to all zeros.
  37. */
  38. Vector3();
  39. /**
  40. * Constructs a new vector initialized to the specified values.
  41. *
  42. * @param x The x coordinate.
  43. * @param y The y coordinate.
  44. * @param z The z coordinate.
  45. */
  46. Vector3(float x, float y, float z);
  47. /**
  48. * Constructs a new vector from the values in the specified array.
  49. *
  50. * @param array An array containing the elements of the vector in the order x, y, z.
  51. */
  52. Vector3(float* array);
  53. /**
  54. * Constructs a vector that describes the direction between the specified points.
  55. *
  56. * @param p1 The first point.
  57. * @param p2 The second point.
  58. */
  59. Vector3(const Vector3& p1, const Vector3& p2);
  60. /**
  61. * Constructs a new vector that is a copy of the specified vector.
  62. *
  63. * @param copy The vector to copy.
  64. */
  65. Vector3(const Vector3& copy);
  66. /**
  67. * Destructor.
  68. */
  69. ~Vector3();
  70. /**
  71. * The zero vector
  72. *
  73. * @return The 3-element vector of 0s.
  74. */
  75. static const Vector3& zero();
  76. /**
  77. * The one vector.
  78. *
  79. * @return The 3-element vector of 1s.
  80. */
  81. static const Vector3& one();
  82. /**
  83. * The unit x vector.
  84. *
  85. * @return The 3-element unit vector along the x axis.
  86. */
  87. static const Vector3& unitX();
  88. /**
  89. * The unit y vector.
  90. *
  91. * @return The 3-element unit vector along the y axis.
  92. */
  93. static const Vector3& unitY();
  94. /**
  95. * The unit z vector.
  96. *
  97. * @return The 3-element unit vector along the z axis.
  98. */
  99. static const Vector3& unitZ();
  100. /**
  101. * Is this vector the all zeros.
  102. *
  103. * @return true if all zeros, false if otherwise.
  104. */
  105. bool isZero() const;
  106. /**
  107. * Is this vector all ones.
  108. *
  109. * @return true if all ones, false if otherwise.
  110. */
  111. bool isOne() const;
  112. /**
  113. * Returns the angle (in radians) between the specified vectors.
  114. *
  115. * @param v1 The first vector.
  116. * @param v2 The second vector.
  117. *
  118. * @return The angle between the two vectors, in radians.
  119. */
  120. static float angle(const Vector3& v1, const Vector3& v2);
  121. /**
  122. * Adds the elements of the specified vector to this one.
  123. *
  124. * @param v The vector to add.
  125. */
  126. void add(const Vector3& v);
  127. /**
  128. * Adds the specified vectors and stores the result in dst.
  129. *
  130. * @param v1 The first vector.
  131. * @param v2 The second vector.
  132. * @param dst A vector to store the result in.
  133. */
  134. static void add(const Vector3& v1, const Vector3& v2, Vector3* dst);
  135. /**
  136. * Clamps this vector within the specified range.
  137. *
  138. * @param min The minimum value.
  139. * @param max The maximum value.
  140. */
  141. void clamp(const Vector3& min, const Vector3& max);
  142. /**
  143. * Clamps the specified vector within the specified range and returns it in dst.
  144. *
  145. * @param v The vector to clamp.
  146. * @param min The minimum value.
  147. * @param max The maximum value.
  148. * @param dst A vector to store the result in.
  149. */
  150. static void clamp(const Vector3& v, const Vector3& min, const Vector3& max, Vector3* dst);
  151. /**
  152. * Sets this vector to the cross product between itself and the specified vector.
  153. *
  154. * @param v the vector to compute the cross product with.
  155. */
  156. void cross(const Vector3& v);
  157. /**
  158. * Computes the cross product of the specified vectors and stores the result in dst.
  159. *
  160. * @param v1 The first vector.
  161. * @param v2 The second vector.
  162. * @param dst A vector to store the result in.
  163. */
  164. static void cross(const Vector3& v1, const Vector3& v2, Vector3* dst);
  165. /**
  166. * Returns the distance between this vector and v.
  167. *
  168. * @param v The other vector.
  169. *
  170. * @return The distance between this vector and v.
  171. * @see distanceSquared
  172. */
  173. float distance(const Vector3& v);
  174. /**
  175. * Returns the squared distance between this vector and v.
  176. *
  177. * When it is not neccessary to get the exact distance between
  178. * two vectors (for example, when simply comparing the
  179. * distance between different vectors), it is advised to use
  180. * this method instead of distance.
  181. *
  182. * @param v The other vector.
  183. *
  184. * @return The squared distance between this vector and v.
  185. * @see distance
  186. */
  187. float distanceSquared(const Vector3& v);
  188. /**
  189. * Returns the dot product of this vector and the specified vector.
  190. *
  191. * @param v The vector to compute the dot product with.
  192. *
  193. * @return The dot product.
  194. */
  195. float dot(const Vector3& v);
  196. /**
  197. * Returns the dot product between the specified vectors.
  198. *
  199. * @param v1 The first vector.
  200. * @param v2 The second vector.
  201. *
  202. * @return The dot product between the vectors.
  203. */
  204. static float dot(const Vector3& v1, const Vector3& v2);
  205. /**
  206. * Computes the length of this vector.
  207. *
  208. * @return The length of the vector.
  209. * @see lengthSquared
  210. */
  211. float length();
  212. /**
  213. * Returns the squared length of this vector.
  214. *
  215. * When it is not neccessary to get the exact length of a
  216. * vector (for example, when simply comparing the lengths of
  217. * different vectors), it is advised to use this method
  218. * instead of length.
  219. *
  220. * @return The squared length of the vector.
  221. * @see length
  222. */
  223. float lengthSquared();
  224. /**
  225. * Negates this vector.
  226. */
  227. void negate();
  228. /**
  229. * Normalizes this vector.
  230. *
  231. * This method normalizes this Vector3 so that it is of
  232. * unit length (in other words, the length of the vector
  233. * after calling this method will be 1.0f). If the vector
  234. * already has unit length or if the length of the vector
  235. * is zero, this method does nothing.
  236. */
  237. void normalize();
  238. /**
  239. * Normalizes this vector and stores the result in dst.
  240. *
  241. * If the vector already has unit length or if the length
  242. * of the vector is zero, this method simply copies the
  243. * current vector into dst.
  244. *
  245. * @param dst the destination vector
  246. */
  247. void normalize(Vector3* dst) const;
  248. /**
  249. * Scales all elements of this vector by the specified value.
  250. *
  251. * @param scalar The scalar value.
  252. */
  253. void scale(float scalar);
  254. /**
  255. * Sets the elements of this vector to the specified values.
  256. *
  257. * @param x The new x coordinate.
  258. * @param y The new y coordinate.
  259. * @param z The new z coordinate.
  260. */
  261. void set(float x, float y, float z);
  262. /**
  263. * Sets the elements of this vector from the values in the specified array.
  264. *
  265. * @param array An array containing the elements of the vector in the order x, y, z.
  266. */
  267. void set(float* array);
  268. /**
  269. * Sets the elements of this vector to those in the specified vector.
  270. *
  271. * @param v The vector to copy.
  272. */
  273. void set(const Vector3& v);
  274. /**
  275. * Sets this vector to the directional vector between the specified points.
  276. */
  277. void set(const Vector3& p1, const Vector3& p2);
  278. /**
  279. * Subtracts this vector and the specified vector as (this - v)
  280. * and stores the result in this.
  281. *
  282. * @param v The vector to subtract.
  283. */
  284. void subtract(const Vector3& v);
  285. /**
  286. * Subtracts the specified vectors and stores the result in dst.
  287. * The resulting vector is computed as (v1 - v2).
  288. *
  289. * @param v1 The first vector.
  290. * @param v2 The second vector.
  291. * @param dst The destination vector.
  292. */
  293. static void subtract(const Vector3& v1, const Vector3& v2, Vector3* dst);
  294. inline bool operator<(const Vector3& v) const
  295. {
  296. if (x == v.x)
  297. {
  298. if (y == v.y)
  299. {
  300. return z < v.z;
  301. }
  302. return y < v.y;
  303. }
  304. return x < v.x;
  305. }
  306. inline bool operator==(const Vector3& v) const
  307. {
  308. return x==v.x && y==v.y && z==v.z;
  309. }
  310. static float distanceSquared(const Vector3& v1, const Vector3& v2);
  311. /**
  312. * Writes this vector to the binary file stream.
  313. */
  314. void writeBinary(FILE* file) const;
  315. void writeText(FILE* file) const;
  316. };
  317. }
  318. #endif