Vector4.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  1. #ifndef VECTOR4_H_
  2. #define VECTOR4_H_
  3. namespace gameplay
  4. {
  5. class Matrix;
  6. /**
  7. * Defines 4-element floating point vector.
  8. */
  9. class Vector4
  10. {
  11. public:
  12. /**
  13. * The x-coordinate.
  14. */
  15. float x;
  16. /**
  17. * The y-coordinate.
  18. */
  19. float y;
  20. /**
  21. * The z-coordinate.
  22. */
  23. float z;
  24. /**
  25. * The w-coordinate.
  26. */
  27. float w;
  28. /**
  29. * Constructs a new vector initialized to all zeros.
  30. */
  31. Vector4();
  32. /**
  33. * Constructs a new vector initialized to the specified values.
  34. *
  35. * @param x The x coordinate.
  36. * @param y The y coordinate.
  37. * @param z The z coordinate.
  38. * @param w The w coordinate.
  39. */
  40. Vector4(float x, float y, float z, float w);
  41. /**
  42. * Constructs a new vector from the values in the specified array.
  43. *
  44. * @param array An array containing the elements of the vector in the order x, y, z, w.
  45. */
  46. Vector4(const float* array);
  47. /**
  48. * Constructs a vector that describes the direction between the specified points.
  49. *
  50. * @param p1 The first point.
  51. * @param p2 The second point.
  52. */
  53. Vector4(const Vector4& p1, const Vector4& p2);
  54. /**
  55. * Constructor.
  56. *
  57. * Creates a new vector that is a copy of the specified vector.
  58. *
  59. * @param copy The vector to copy.
  60. */
  61. Vector4(const Vector4& copy);
  62. /**
  63. * Creates a new vector from an integer interpreted as an RGBA value.
  64. * E.g. 0xff0000ff represents opaque red or the vector (1, 0, 0, 1).
  65. *
  66. * @param color The integer to interpret as an RGBA value.
  67. *
  68. * @return A vector corresponding to the interpreted RGBA color.
  69. */
  70. static Vector4 fromColor(unsigned int color);
  71. /**
  72. * Destructor.
  73. */
  74. ~Vector4();
  75. /**
  76. * Returns the zero vector.
  77. *
  78. * @return The 4-element vector of 0s.
  79. */
  80. static const Vector4& zero();
  81. /**
  82. * Returns the one vector.
  83. *
  84. * @return The 4-element vector of 1s.
  85. */
  86. static const Vector4& one();
  87. /**
  88. * Returns the unit x vector.
  89. *
  90. * @return The 4-element unit vector along the x axis.
  91. */
  92. static const Vector4& unitX();
  93. /**
  94. * Returns the unit y vector.
  95. *
  96. * @return The 4-element unit vector along the y axis.
  97. */
  98. static const Vector4& unitY();
  99. /**
  100. * Returns the unit z vector.
  101. *
  102. * @return The 4-element unit vector along the z axis.
  103. */
  104. static const Vector4& unitZ();
  105. /**
  106. * Returns the unit w vector.
  107. *
  108. * @return The 4-element unit vector along the w axis.
  109. */
  110. static const Vector4& unitW();
  111. /**
  112. * Indicates whether this vector contains all zeros.
  113. *
  114. * @return true if this vector contains all zeros, false otherwise.
  115. */
  116. bool isZero() const;
  117. /**
  118. * Indicates whether this vector contains all ones.
  119. *
  120. * @return true if this vector contains all ones, false otherwise.
  121. */
  122. bool isOne() const;
  123. /**
  124. * Returns the angle (in radians) between the specified vectors.
  125. *
  126. * @param v1 The first vector.
  127. * @param v2 The second vector.
  128. *
  129. * @return The angle between the two vectors (in radians).
  130. */
  131. static float angle(const Vector4& v1, const Vector4& v2);
  132. /**
  133. * Adds the elements of the specified vector to this one.
  134. *
  135. * @param v The vector to add.
  136. */
  137. void add(const Vector4& v);
  138. /**
  139. * Adds the specified vectors and stores the result in dst.
  140. *
  141. * @param v1 The first vector.
  142. * @param v2 The second vector.
  143. * @param dst A vector to store the result in.
  144. */
  145. static void add(const Vector4& v1, const Vector4& v2, Vector4* dst);
  146. /**
  147. * Clamps this vector within the specified range.
  148. *
  149. * @param min The minimum value.
  150. * @param max The maximum value.
  151. */
  152. void clamp(const Vector4& min, const Vector4& max);
  153. /**
  154. * Clamps the specified vector within the specified range and returns it in dst.
  155. *
  156. * @param v The vector to clamp.
  157. * @param min The minimum value.
  158. * @param max The maximum value.
  159. * @param dst A vector to store the result in.
  160. */
  161. static void clamp(const Vector4& v, const Vector4& min, const Vector4& max, Vector4* dst);
  162. /**
  163. * Returns the distance between this vector and v.
  164. *
  165. * @param v The other vector.
  166. *
  167. * @return The distance between this vector and v.
  168. *
  169. * @see distanceSquared
  170. */
  171. float distance(const Vector4& v) const;
  172. /**
  173. * Returns the squared distance between this vector and v.
  174. *
  175. * When it is not neccessary to get the exact distance between
  176. * two vectors (for example, when simply comparing the
  177. * distance between different vectors), it is advised to use
  178. * this method instead of distance.
  179. *
  180. * @param v The other vector.
  181. *
  182. * @return The squared distance between this vector and v.
  183. *
  184. * @see distance
  185. */
  186. float distanceSquared(const Vector4& v) const;
  187. /**
  188. * Returns the dot product of this vector and the specified vector.
  189. *
  190. * @param v The vector to compute the dot product with.
  191. *
  192. * @return The dot product.
  193. */
  194. float dot(const Vector4& v) const;
  195. /**
  196. * Returns the dot product between the specified vectors.
  197. *
  198. * @param v1 The first vector.
  199. * @param v2 The second vector.
  200. *
  201. * @return The dot product between the vectors.
  202. */
  203. static float dot(const Vector4& v1, const Vector4& v2);
  204. /**
  205. * Computes the length of this vector.
  206. *
  207. * @return The length of the vector.
  208. *
  209. * @see lengthSquared
  210. */
  211. float length() const;
  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. *
  222. * @see length
  223. */
  224. float lengthSquared() const;
  225. /**
  226. * Negates this vector.
  227. */
  228. void negate();
  229. /**
  230. * Normalizes this vector.
  231. *
  232. * This method normalizes this Vector4 so that it is of
  233. * unit length (in other words, the length of the vector
  234. * after calling this method will be 1.0f). If the vector
  235. * already has unit length or if the length of the vector
  236. * is zero, this method does nothing.
  237. *
  238. * @return This vector, after the normalization occurs.
  239. */
  240. Vector4& normalize();
  241. /**
  242. * Normalizes this vector and stores the result in dst.
  243. *
  244. * If the vector already has unit length or if the length
  245. * of the vector is zero, this method simply copies the
  246. * current vector into dst.
  247. *
  248. * @param dst The destination vector.
  249. */
  250. void normalize(Vector4* dst);
  251. /**
  252. * Scales all elements of this vector by the specified value.
  253. *
  254. * @param scalar The scalar value.
  255. */
  256. void scale(float scalar);
  257. /**
  258. * Sets the elements of this vector to the specified values.
  259. *
  260. * @param x The new x coordinate.
  261. * @param y The new y coordinate.
  262. * @param z The new z coordinate.
  263. * @param w The new w coordinate.
  264. */
  265. void set(float x, float y, float z, float w);
  266. /**
  267. * Sets the elements of this vector from the values in the specified array.
  268. *
  269. * @param array An array containing the elements of the vector in the order x, y, z, w.
  270. */
  271. void set(const float* array);
  272. /**
  273. * Sets the elements of this vector to those in the specified vector.
  274. *
  275. * @param v The vector to copy.
  276. */
  277. void set(const Vector4& v);
  278. /**
  279. * Sets this vector to the directional vector between the specified points.
  280. *
  281. * @param p1 The first point.
  282. * @param p2 The second point.
  283. */
  284. void set(const Vector4& p1, const Vector4& p2);
  285. /**
  286. * Subtracts this vector and the specified vector as (this - v)
  287. * and stores the result in this vector.
  288. *
  289. * @param v The vector to subtract.
  290. */
  291. void subtract(const Vector4& v);
  292. /**
  293. * Subtracts the specified vectors and stores the result in dst.
  294. * The resulting vector is computed as (v1 - v2).
  295. *
  296. * @param v1 The first vector.
  297. * @param v2 The second vector.
  298. * @param dst The destination vector.
  299. */
  300. static void subtract(const Vector4& v1, const Vector4& v2, Vector4* dst);
  301. /**
  302. * Calculates the sum of this vector with the given vector.
  303. *
  304. * Note: this does not modify this vector.
  305. *
  306. * @param v The vector to add.
  307. * @return The vector sum.
  308. */
  309. inline const Vector4 operator+(const Vector4& v) const;
  310. /**
  311. * Adds the given vector to this vector.
  312. *
  313. * @param v The vector to add.
  314. * @return This vector, after the addition occurs.
  315. */
  316. inline Vector4& operator+=(const Vector4& v);
  317. /**
  318. * Calculates the sum of this vector with the given vector.
  319. *
  320. * Note: this does not modify this vector.
  321. *
  322. * @param v The vector to add.
  323. * @return The vector sum.
  324. */
  325. inline const Vector4 operator-(const Vector4& v) const;
  326. /**
  327. * Subtracts the given vector from this vector.
  328. *
  329. * @param v The vector to subtract.
  330. * @return This vector, after the subtraction occurs.
  331. */
  332. inline Vector4& operator-=(const Vector4& v);
  333. /**
  334. * Calculates the negation of this vector.
  335. *
  336. * Note: this does not modify this vector.
  337. *
  338. * @return The negation of this vector.
  339. */
  340. inline const Vector4 operator-() const;
  341. /**
  342. * Calculates the scalar product of this vector with the given value.
  343. *
  344. * Note: this does not modify this vector.
  345. *
  346. * @param x The value to scale by.
  347. * @return The scaled vector.
  348. */
  349. inline const Vector4 operator*(float x) const;
  350. /**
  351. * Scales this vector by the given value.
  352. *
  353. * @param x The value to scale by.
  354. * @return This vector, after the scale occurs.
  355. */
  356. inline Vector4& operator*=(float x);
  357. /**
  358. * Determines if this vector is less than the given vector.
  359. *
  360. * @param v The vector to compare against.
  361. *
  362. * @return True if this vector is less than the given vector, false otherwise.
  363. */
  364. inline bool operator<(const Vector4& v) const;
  365. /**
  366. * Determines if this vector is equal to the given vector.
  367. *
  368. * @param v The vector to compare against.
  369. *
  370. * @return True if this vector is equal to the given vector, false otherwise.
  371. */
  372. inline bool operator==(const Vector4& v) const;
  373. /**
  374. * Determines if this vector is not equal to the given vector.
  375. *
  376. * @param v The vector to compare against.
  377. *
  378. * @return True if this vector is not equal to the given vector, false otherwise.
  379. */
  380. inline bool operator!=(const Vector4& v) const;
  381. };
  382. /**
  383. * Calculates the scalar product of the given vector with the given value.
  384. *
  385. * @param x The value to scale by.
  386. * @param v The vector to scale.
  387. * @return The scaled vector.
  388. */
  389. inline const Vector4 operator*(float x, const Vector4& v);
  390. }
  391. #include "Vector4.inl"
  392. #endif