Vector4.h 8.8 KB

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