Vec3.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513
  1. /**
  2. Copyright 2013 BlackBerry Inc.
  3. Copyright (c) 2014-2017 Chukong Technologies
  4. Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
  5. Licensed under the Apache License, Version 2.0 (the "License");
  6. you may not use this file except in compliance with the License.
  7. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. Original file from GamePlay3D: http://gameplay3d.org
  15. This file was modified to fit the cocos2d-x project
  16. */
  17. #ifndef MATH_VEC3_H
  18. #define MATH_VEC3_H
  19. #include <cmath>
  20. #include "math/CCMathBase.h"
  21. /**
  22. * @addtogroup base
  23. * @{
  24. */
  25. NS_CC_MATH_BEGIN
  26. class Mat4;
  27. class Quaternion;
  28. /**
  29. * Defines a 3-element floating point vector.
  30. *
  31. * When using a vector to represent a surface normal,
  32. * the vector should typically be normalized.
  33. * Other uses of directional vectors may wish to leave
  34. * the magnitude of the vector intact. When used as a point,
  35. * the elements of the vector represent a position in 3D space.
  36. */
  37. class CC_DLL Vec3
  38. {
  39. public:
  40. /**
  41. * The x-coordinate.
  42. */
  43. float x;
  44. /**
  45. * The y-coordinate.
  46. */
  47. float y;
  48. /**
  49. * The z-coordinate.
  50. */
  51. float z;
  52. /**
  53. * Constructs a new vector initialized to all zeros.
  54. */
  55. Vec3();
  56. /**
  57. * Constructs a new vector initialized to the specified values.
  58. *
  59. * @param xx The x coordinate.
  60. * @param yy The y coordinate.
  61. * @param zz The z coordinate.
  62. */
  63. Vec3(float xx, float yy, float zz);
  64. /**
  65. * Constructs a new vector from the values in the specified array.
  66. *
  67. * @param array An array containing the elements of the vector in the order x, y, z.
  68. */
  69. Vec3(const float* array);
  70. /**
  71. * Constructs a vector that describes the direction between the specified points.
  72. *
  73. * @param p1 The first point.
  74. * @param p2 The second point.
  75. */
  76. Vec3(const Vec3& p1, const Vec3& p2);
  77. /**
  78. * Constructs a new vector that is a copy of the specified vector.
  79. *
  80. * @param copy The vector to copy.
  81. */
  82. Vec3(const Vec3& copy);
  83. /**
  84. * Creates a new vector from an integer interpreted as an RGB value.
  85. * E.g. 0xff0000 represents red or the vector (1, 0, 0).
  86. *
  87. * @param color The integer to interpret as an RGB value.
  88. *
  89. * @return A vector corresponding to the interpreted RGB color.
  90. */
  91. static Vec3 fromColor(unsigned int color);
  92. /**
  93. * Destructor.
  94. */
  95. ~Vec3();
  96. /**
  97. * Indicates whether this vector contains all zeros.
  98. *
  99. * @return true if this vector contains all zeros, false otherwise.
  100. */
  101. inline bool isZero() const;
  102. /**
  103. * Indicates whether this vector contains all ones.
  104. *
  105. * @return true if this vector contains all ones, false otherwise.
  106. */
  107. inline bool isOne() const;
  108. /**
  109. * Returns the angle (in radians) between the specified vectors.
  110. *
  111. * @param v1 The first vector.
  112. * @param v2 The second vector.
  113. *
  114. * @return The angle between the two vectors (in radians).
  115. */
  116. static float angle(const Vec3& v1, const Vec3& v2);
  117. /**
  118. * Adds the elements of the specified vector to this one.
  119. *
  120. * @param v The vector to add.
  121. */
  122. inline void add(const Vec3& v);
  123. /**
  124. * Adds the elements of this vector to the specified values.
  125. *
  126. * @param xx The add x coordinate.
  127. * @param yy The add y coordinate.
  128. * @param zz The add z coordinate.
  129. */
  130. inline void add(float xx, float yy, float zz);
  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 Vec3& v1, const Vec3& v2, Vec3* 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 Vec3& min, const Vec3& 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 Vec3& v, const Vec3& min, const Vec3& max, Vec3* dst);
  155. /**
  156. * Sets this vector to the cross product between itself and the specified vector.
  157. *
  158. * @param v The vector to compute the cross product with.
  159. */
  160. void cross(const Vec3& v);
  161. /**
  162. * Computes the cross product of the specified vectors and stores the result in dst.
  163. *
  164. * @param v1 The first vector.
  165. * @param v2 The second vector.
  166. * @param dst A vector to store the result in.
  167. */
  168. static void cross(const Vec3& v1, const Vec3& v2, Vec3* dst);
  169. /**
  170. * Returns the distance between this vector and v.
  171. *
  172. * @param v The other vector.
  173. *
  174. * @return The distance between this vector and v.
  175. *
  176. * @see distanceSquared
  177. */
  178. float distance(const Vec3& v) const;
  179. /**
  180. * Returns the squared distance between this vector and v.
  181. *
  182. * When it is not necessary to get the exact distance between
  183. * two vectors (for example, when simply comparing the
  184. * distance between different vectors), it is advised to use
  185. * this method instead of distance.
  186. *
  187. * @param v The other vector.
  188. *
  189. * @return The squared distance between this vector and v.
  190. *
  191. * @see distance
  192. */
  193. float distanceSquared(const Vec3& v) const;
  194. /**
  195. * Returns the dot product of this vector and the specified vector.
  196. *
  197. * @param v The vector to compute the dot product with.
  198. *
  199. * @return The dot product.
  200. */
  201. float dot(const Vec3& v) const;
  202. /**
  203. * Returns the dot product between the specified vectors.
  204. *
  205. * @param v1 The first vector.
  206. * @param v2 The second vector.
  207. *
  208. * @return The dot product between the vectors.
  209. */
  210. static float dot(const Vec3& v1, const Vec3& v2);
  211. /**
  212. * Computes the length of this vector.
  213. *
  214. * @return The length of the vector.
  215. *
  216. * @see lengthSquared
  217. */
  218. inline float length() const;
  219. /**
  220. * Returns the squared length of this vector.
  221. *
  222. * When it is not necessary to get the exact length of a
  223. * vector (for example, when simply comparing the lengths of
  224. * different vectors), it is advised to use this method
  225. * instead of length.
  226. *
  227. * @return The squared length of the vector.
  228. *
  229. * @see length
  230. */
  231. inline float lengthSquared() const;
  232. /**
  233. * Negates this vector.
  234. */
  235. inline void negate();
  236. /**
  237. * Normalizes this vector.
  238. *
  239. * This method normalizes this Vec3 so that it is of
  240. * unit length (in other words, the length of the vector
  241. * after calling this method will be 1.0f). If the vector
  242. * already has unit length or if the length of the vector
  243. * is zero, this method does nothing.
  244. *
  245. * @return This vector, after the normalization occurs.
  246. */
  247. void normalize();
  248. /**
  249. * Get the normalized vector.
  250. */
  251. Vec3 getNormalized() const;
  252. /**
  253. * Scales all elements of this vector by the specified value.
  254. *
  255. * @param scalar The scalar value.
  256. */
  257. inline void scale(float scalar);
  258. /**
  259. * Sets the elements of this vector to the specified values.
  260. *
  261. * @param xx The new x coordinate.
  262. * @param yy The new y coordinate.
  263. * @param zz The new z coordinate.
  264. */
  265. inline void set(float xx, float yy, float zz);
  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.
  270. */
  271. inline 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. inline void set(const Vec3& v);
  278. /**
  279. * Sets this vector to the directional vector between the specified points.
  280. */
  281. inline void set(const Vec3& p1, const Vec3& p2);
  282. /**
  283. * Sets the elements of this vector to zero.
  284. */
  285. inline void setZero();
  286. /**
  287. * Subtracts this vector and the specified vector as (this - v)
  288. * and stores the result in this vector.
  289. *
  290. * @param v The vector to subtract.
  291. */
  292. inline void subtract(const Vec3& v);
  293. /**
  294. * Subtracts the specified vectors and stores the result in dst.
  295. * The resulting vector is computed as (v1 - v2).
  296. *
  297. * @param v1 The first vector.
  298. * @param v2 The second vector.
  299. * @param dst The destination vector.
  300. */
  301. static void subtract(const Vec3& v1, const Vec3& v2, Vec3* dst);
  302. /**
  303. * Updates this vector towards the given target using a smoothing function.
  304. * The given response time determines the amount of smoothing (lag). A longer
  305. * response time yields a smoother result and more lag. To force this vector to
  306. * follow the target closely, provide a response time that is very small relative
  307. * to the given elapsed time.
  308. *
  309. * @param target target value.
  310. * @param elapsedTime elapsed time between calls.
  311. * @param responseTime response time (in the same units as elapsedTime).
  312. */
  313. void smooth(const Vec3& target, float elapsedTime, float responseTime);
  314. /**
  315. * Linear interpolation between two vectors A and B by alpha which
  316. * is in the range [0,1]
  317. */
  318. inline Vec3 lerp(const Vec3& target, float alpha) const;
  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 Vec3 operator+(const Vec3& v) const;
  328. /**
  329. * Adds the given vector to this vector.
  330. *
  331. * @param v The vector to add.
  332. * @return This vector, after the addition occurs.
  333. */
  334. inline Vec3& operator+=(const Vec3& v);
  335. /**
  336. * Calculates the difference of this vector with the given vector.
  337. *
  338. * Note: this does not modify this vector.
  339. *
  340. * @param v The vector to subtract.
  341. * @return The vector difference.
  342. */
  343. inline Vec3 operator-(const Vec3& v) const;
  344. /**
  345. * Subtracts the given vector from this vector.
  346. *
  347. * @param v The vector to subtract.
  348. * @return This vector, after the subtraction occurs.
  349. */
  350. inline Vec3& operator-=(const Vec3& v);
  351. /**
  352. * Calculates the negation of this vector.
  353. *
  354. * Note: this does not modify this vector.
  355. *
  356. * @return The negation of this vector.
  357. */
  358. inline Vec3 operator-() const;
  359. /**
  360. * Calculates the scalar product of this vector with the given value.
  361. *
  362. * Note: this does not modify this vector.
  363. *
  364. * @param s The value to scale by.
  365. * @return The scaled vector.
  366. */
  367. inline Vec3 operator*(float s) const;
  368. /**
  369. * Scales this vector by the given value.
  370. *
  371. * @param s The value to scale by.
  372. * @return This vector, after the scale occurs.
  373. */
  374. inline Vec3& operator*=(float s);
  375. /**
  376. * Returns the components of this vector divided by the given constant
  377. *
  378. * Note: this does not modify this vector.
  379. *
  380. * @param s the constant to divide this vector with
  381. * @return a smaller vector
  382. */
  383. inline Vec3 operator/(float s) const;
  384. /** Returns true if the vector's scalar components are all greater
  385. that the ones of the vector it is compared against.
  386. */
  387. inline bool operator < (const Vec3& rhs) const
  388. {
  389. if (x < rhs.x && y < rhs.y && z < rhs.z)
  390. return true;
  391. return false;
  392. }
  393. /** Returns true if the vector's scalar components are all smaller
  394. that the ones of the vector it is compared against.
  395. */
  396. inline bool operator >(const Vec3& rhs) const
  397. {
  398. if (x > rhs.x && y > rhs.y && z > rhs.z)
  399. return true;
  400. return false;
  401. }
  402. /**
  403. * Determines if this vector is equal to the given vector.
  404. *
  405. * @param v The vector to compare against.
  406. *
  407. * @return True if this vector is equal to the given vector, false otherwise.
  408. */
  409. inline bool operator==(const Vec3& v) const;
  410. /**
  411. * Determines if this vector is not equal to the given vector.
  412. *
  413. * @param v The vector to compare against.
  414. *
  415. * @return True if this vector is not equal to the given vector, false otherwise.
  416. */
  417. inline bool operator!=(const Vec3& v) const;
  418. /** equals to Vec3(0,0,0) */
  419. static const Vec3 ZERO;
  420. /** equals to Vec3(1,1,1) */
  421. static const Vec3 ONE;
  422. /** equals to Vec3(1,0,0) */
  423. static const Vec3 UNIT_X;
  424. /** equals to Vec3(0,1,0) */
  425. static const Vec3 UNIT_Y;
  426. /** equals to Vec3(0,0,1) */
  427. static const Vec3 UNIT_Z;
  428. };
  429. /**
  430. * Calculates the scalar product of the given vector with the given value.
  431. *
  432. * @param x The value to scale by.
  433. * @param v The vector to scale.
  434. * @return The scaled vector.
  435. */
  436. inline Vec3 operator*(float x, const Vec3& v);
  437. //typedef Vec3 Point3;
  438. NS_CC_MATH_END
  439. /**
  440. end of base group
  441. @}
  442. */
  443. #include "math/Vec3.inl"
  444. #endif // MATH_VEC3_H