Vector3.h 12 KB

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