Vector3.h 12 KB

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