Vec4.h 12 KB

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