Vector4.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  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(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);
  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. void normalize();
  239. /**
  240. * Normalizes this vector and stores the result in dst.
  241. *
  242. * If the vector already has unit length or if the length
  243. * of the vector is zero, this method simply copies the
  244. * current vector into dst.
  245. *
  246. * @param dst The destination vector.
  247. */
  248. void normalize(Vector4* dst);
  249. /**
  250. * Scales all elements of this vector by the specified value.
  251. *
  252. * @param scalar The scalar value.
  253. */
  254. void scale(float scalar);
  255. /**
  256. * Sets the elements of this vector to the specified values.
  257. *
  258. * @param x The new x coordinate.
  259. * @param y The new y coordinate.
  260. * @param z The new z coordinate.
  261. * @param w The new w coordinate.
  262. */
  263. void set(float x, float y, float z, float w);
  264. /**
  265. * Sets the elements of this vector from the values in the specified array.
  266. *
  267. * @param array An array containing the elements of the vector in the order x, y, z, w.
  268. */
  269. void set(float* array);
  270. /**
  271. * Sets the elements of this vector to those in the specified vector.
  272. *
  273. * @param v The vector to copy.
  274. */
  275. void set(const Vector4& v);
  276. /**
  277. * Sets this vector to the directional vector between the specified points.
  278. *
  279. * @param p1 The first point.
  280. * @param p2 The second point.
  281. */
  282. void set(const Vector4& p1, const Vector4& p2);
  283. /**
  284. * Subtracts this vector and the specified vector as (this - v)
  285. * and stores the result in this vector.
  286. *
  287. * @param v The vector to subtract.
  288. */
  289. void subtract(const Vector4& v);
  290. /**
  291. * Subtracts the specified vectors and stores the result in dst.
  292. * The resulting vector is computed as (v1 - v2).
  293. *
  294. * @param v1 The first vector.
  295. * @param v2 The second vector.
  296. * @param dst The destination vector.
  297. */
  298. static void subtract(const Vector4& v1, const Vector4& v2, Vector4* dst);
  299. /**
  300. * Calculates the sum of this vector with the given vector.
  301. *
  302. * Note: this does not modify this vector.
  303. *
  304. * @param v The vector to add.
  305. * @return The vector sum.
  306. */
  307. inline Vector4 operator+(const Vector4& v);
  308. /**
  309. * Adds the given vector to this vector.
  310. *
  311. * @param v The vector to add.
  312. * @return This vector, after the addition occurs.
  313. */
  314. inline Vector4& operator+=(const Vector4& v);
  315. /**
  316. * Calculates the sum of this vector with the given vector.
  317. *
  318. * Note: this does not modify this vector.
  319. *
  320. * @param v The vector to add.
  321. * @return The vector sum.
  322. */
  323. inline Vector4 operator-(const Vector4& v);
  324. /**
  325. * Subtracts the given vector from this vector.
  326. *
  327. * @param v The vector to subtract.
  328. * @return This vector, after the subtraction occurs.
  329. */
  330. inline Vector4& operator-=(const Vector4& v);
  331. /**
  332. * Calculates the negation of this vector.
  333. *
  334. * Note: this does not modify this vector.
  335. *
  336. * @return The negation of this vector.
  337. */
  338. inline Vector4 operator-();
  339. /**
  340. * Calculates the scalar product of this vector with the given value.
  341. *
  342. * Note: this does not modify this vector.
  343. *
  344. * @param x The value to scale by.
  345. * @return The scaled vector.
  346. */
  347. inline Vector4 operator*(float x);
  348. /**
  349. * Scales this vector by the given value.
  350. *
  351. * @param x The value to scale by.
  352. * @return This vector, after the scale occurs.
  353. */
  354. inline Vector4& operator*=(float x);
  355. /**
  356. * Determines if this vector is less than the given vector.
  357. *
  358. * @param v The vector to compare against.
  359. *
  360. * @return True if this vector is less than the given vector, false otherwise.
  361. */
  362. inline bool operator<(const Vector4& v) const;
  363. /**
  364. * Determines if this vector is equal to the given vector.
  365. *
  366. * @param v The vector to compare against.
  367. *
  368. * @return True if this vector is equal to the given vector, false otherwise.
  369. */
  370. inline bool operator==(const Vector4& v) const;
  371. };
  372. /**
  373. * Calculates the scalar product of the given vector with the given value.
  374. *
  375. * @param x The value to scale by.
  376. * @param v The vector to scale.
  377. * @return The scaled vector.
  378. */
  379. inline Vector4 operator*(float x, const Vector4& v);
  380. }
  381. #include "Vector4.inl"
  382. #endif