CmVector2.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583
  1. /*
  2. -----------------------------------------------------------------------------
  3. This source file is part of OGRE
  4. (Object-oriented Graphics Rendering Engine)
  5. For the latest info, see http://www.ogre3d.org/
  6. Copyright (c) 2000-2011 Torus Knot Software Ltd
  7. Permission is hereby granted, free of charge, to any person obtaining a copy
  8. of this software and associated documentation files (the "Software"), to deal
  9. in the Software without restriction, including without limitation the rights
  10. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. copies of the Software, and to permit persons to whom the Software is
  12. furnished to do so, subject to the following conditions:
  13. The above copyright notice and this permission notice shall be included in
  14. all copies or substantial portions of the Software.
  15. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. THE SOFTWARE.
  22. -----------------------------------------------------------------------------
  23. */
  24. #ifndef __Vector2_H__
  25. #define __Vector2_H__
  26. #include "CmPrerequisitesUtil.h"
  27. #include "CmMath.h"
  28. namespace CamelotFramework
  29. {
  30. /** \addtogroup Core
  31. * @{
  32. */
  33. /** \addtogroup Math
  34. * @{
  35. */
  36. /** Standard 2-dimensional vector.
  37. @remarks
  38. A direction in 2D space represented as distances along the 2
  39. orthogonal axes (x, y). Note that positions, directions and
  40. scaling factors can be represented by a vector, depending on how
  41. you interpret the values.
  42. */
  43. class CM_UTILITY_EXPORT Vector2
  44. {
  45. public:
  46. float x, y;
  47. public:
  48. inline Vector2()
  49. {
  50. }
  51. inline Vector2(const float fX, const float fY )
  52. : x( fX ), y( fY )
  53. {
  54. }
  55. inline explicit Vector2( const float scaler )
  56. : x( scaler), y( scaler )
  57. {
  58. }
  59. inline explicit Vector2( const float afCoordinate[2] )
  60. : x( afCoordinate[0] ),
  61. y( afCoordinate[1] )
  62. {
  63. }
  64. inline explicit Vector2( const int afCoordinate[2] )
  65. {
  66. x = (float)afCoordinate[0];
  67. y = (float)afCoordinate[1];
  68. }
  69. inline explicit Vector2( float* const r )
  70. : x( r[0] ), y( r[1] )
  71. {
  72. }
  73. /** Exchange the contents of this vector with another.
  74. */
  75. inline void swap(Vector2& other)
  76. {
  77. std::swap(x, other.x);
  78. std::swap(y, other.y);
  79. }
  80. inline float operator [] ( const size_t i ) const
  81. {
  82. assert( i < 2 );
  83. return *(&x+i);
  84. }
  85. inline float& operator [] ( const size_t i )
  86. {
  87. assert( i < 2 );
  88. return *(&x+i);
  89. }
  90. /// Pointer accessor for direct copying
  91. inline float* ptr()
  92. {
  93. return &x;
  94. }
  95. /// Pointer accessor for direct copying
  96. inline const float* ptr() const
  97. {
  98. return &x;
  99. }
  100. /** Assigns the value of the other vector.
  101. @param
  102. rkVector The other vector
  103. */
  104. inline Vector2& operator = ( const Vector2& rkVector )
  105. {
  106. x = rkVector.x;
  107. y = rkVector.y;
  108. return *this;
  109. }
  110. inline Vector2& operator = ( const float fScalar)
  111. {
  112. x = fScalar;
  113. y = fScalar;
  114. return *this;
  115. }
  116. inline bool operator == ( const Vector2& rkVector ) const
  117. {
  118. return ( x == rkVector.x && y == rkVector.y );
  119. }
  120. inline bool operator != ( const Vector2& rkVector ) const
  121. {
  122. return ( x != rkVector.x || y != rkVector.y );
  123. }
  124. // arithmetic operations
  125. inline Vector2 operator + ( const Vector2& rkVector ) const
  126. {
  127. return Vector2(
  128. x + rkVector.x,
  129. y + rkVector.y);
  130. }
  131. inline Vector2 operator - ( const Vector2& rkVector ) const
  132. {
  133. return Vector2(
  134. x - rkVector.x,
  135. y - rkVector.y);
  136. }
  137. inline Vector2 operator * ( const float fScalar ) const
  138. {
  139. return Vector2(
  140. x * fScalar,
  141. y * fScalar);
  142. }
  143. inline Vector2 operator * ( const Vector2& rhs) const
  144. {
  145. return Vector2(
  146. x * rhs.x,
  147. y * rhs.y);
  148. }
  149. inline Vector2 operator / ( const float fScalar ) const
  150. {
  151. assert( fScalar != 0.0 );
  152. float fInv = 1.0f / fScalar;
  153. return Vector2(
  154. x * fInv,
  155. y * fInv);
  156. }
  157. inline Vector2 operator / ( const Vector2& rhs) const
  158. {
  159. return Vector2(
  160. x / rhs.x,
  161. y / rhs.y);
  162. }
  163. inline const Vector2& operator + () const
  164. {
  165. return *this;
  166. }
  167. inline Vector2 operator - () const
  168. {
  169. return Vector2(-x, -y);
  170. }
  171. // overloaded operators to help Vector2
  172. inline friend Vector2 operator * ( const float fScalar, const Vector2& rkVector )
  173. {
  174. return Vector2(
  175. fScalar * rkVector.x,
  176. fScalar * rkVector.y);
  177. }
  178. inline friend Vector2 operator / ( const float fScalar, const Vector2& rkVector )
  179. {
  180. return Vector2(
  181. fScalar / rkVector.x,
  182. fScalar / rkVector.y);
  183. }
  184. inline friend Vector2 operator + (const Vector2& lhs, const float rhs)
  185. {
  186. return Vector2(
  187. lhs.x + rhs,
  188. lhs.y + rhs);
  189. }
  190. inline friend Vector2 operator + (const float lhs, const Vector2& rhs)
  191. {
  192. return Vector2(
  193. lhs + rhs.x,
  194. lhs + rhs.y);
  195. }
  196. inline friend Vector2 operator - (const Vector2& lhs, const float rhs)
  197. {
  198. return Vector2(
  199. lhs.x - rhs,
  200. lhs.y - rhs);
  201. }
  202. inline friend Vector2 operator - (const float lhs, const Vector2& rhs)
  203. {
  204. return Vector2(
  205. lhs - rhs.x,
  206. lhs - rhs.y);
  207. }
  208. // arithmetic updates
  209. inline Vector2& operator += ( const Vector2& rkVector )
  210. {
  211. x += rkVector.x;
  212. y += rkVector.y;
  213. return *this;
  214. }
  215. inline Vector2& operator += ( const float fScaler )
  216. {
  217. x += fScaler;
  218. y += fScaler;
  219. return *this;
  220. }
  221. inline Vector2& operator -= ( const Vector2& rkVector )
  222. {
  223. x -= rkVector.x;
  224. y -= rkVector.y;
  225. return *this;
  226. }
  227. inline Vector2& operator -= ( const float fScaler )
  228. {
  229. x -= fScaler;
  230. y -= fScaler;
  231. return *this;
  232. }
  233. inline Vector2& operator *= ( const float fScalar )
  234. {
  235. x *= fScalar;
  236. y *= fScalar;
  237. return *this;
  238. }
  239. inline Vector2& operator *= ( const Vector2& rkVector )
  240. {
  241. x *= rkVector.x;
  242. y *= rkVector.y;
  243. return *this;
  244. }
  245. inline Vector2& operator /= ( const float fScalar )
  246. {
  247. assert( fScalar != 0.0 );
  248. float fInv = 1.0f / fScalar;
  249. x *= fInv;
  250. y *= fInv;
  251. return *this;
  252. }
  253. inline Vector2& operator /= ( const Vector2& rkVector )
  254. {
  255. x /= rkVector.x;
  256. y /= rkVector.y;
  257. return *this;
  258. }
  259. /** Returns the length (magnitude) of the vector.
  260. @warning
  261. This operation requires a square root and is expensive in
  262. terms of CPU operations. If you don't need to know the exact
  263. length (e.g. for just comparing lengths) use squaredLength()
  264. instead.
  265. */
  266. inline float length () const
  267. {
  268. return Math::Sqrt( x * x + y * y );
  269. }
  270. /** Returns the square of the length(magnitude) of the vector.
  271. @remarks
  272. This method is for efficiency - calculating the actual
  273. length of a vector requires a square root, which is expensive
  274. in terms of the operations required. This method returns the
  275. square of the length of the vector, i.e. the same as the
  276. length but before the square root is taken. Use this if you
  277. want to find the longest / shortest vector without incurring
  278. the square root.
  279. */
  280. inline float squaredLength () const
  281. {
  282. return x * x + y * y;
  283. }
  284. /** Returns the distance to another vector.
  285. @warning
  286. This operation requires a square root and is expensive in
  287. terms of CPU operations. If you don't need to know the exact
  288. distance (e.g. for just comparing distances) use squaredDistance()
  289. instead.
  290. */
  291. inline float distance(const Vector2& rhs) const
  292. {
  293. return (*this - rhs).length();
  294. }
  295. /** Returns the square of the distance to another vector.
  296. @remarks
  297. This method is for efficiency - calculating the actual
  298. distance to another vector requires a square root, which is
  299. expensive in terms of the operations required. This method
  300. returns the square of the distance to another vector, i.e.
  301. the same as the distance but before the square root is taken.
  302. Use this if you want to find the longest / shortest distance
  303. without incurring the square root.
  304. */
  305. inline float squaredDistance(const Vector2& rhs) const
  306. {
  307. return (*this - rhs).squaredLength();
  308. }
  309. /** Calculates the dot (scalar) product of this vector with another.
  310. @remarks
  311. The dot product can be used to calculate the angle between 2
  312. vectors. If both are unit vectors, the dot product is the
  313. cosine of the angle; otherwise the dot product must be
  314. divided by the product of the lengths of both vectors to get
  315. the cosine of the angle. This result can further be used to
  316. calculate the distance of a point from a plane.
  317. @param
  318. vec Vector with which to calculate the dot product (together
  319. with this one).
  320. @returns
  321. A float representing the dot product value.
  322. */
  323. inline float dotProduct(const Vector2& vec) const
  324. {
  325. return x * vec.x + y * vec.y;
  326. }
  327. /** Normalises the vector.
  328. @remarks
  329. This method normalises the vector such that it's
  330. length / magnitude is 1. The result is called a unit vector.
  331. @note
  332. This function will not crash for zero-sized vectors, but there
  333. will be no changes made to their components.
  334. @returns The previous length of the vector.
  335. */
  336. inline float normalize()
  337. {
  338. float fLength = Math::Sqrt( x * x + y * y);
  339. // Will also work for zero-sized vectors, but will change nothing
  340. if ( fLength > 1e-08 )
  341. {
  342. float fInvLength = 1.0f / fLength;
  343. x *= fInvLength;
  344. y *= fInvLength;
  345. }
  346. return fLength;
  347. }
  348. /** Returns a vector at a point half way between this and the passed
  349. in vector.
  350. */
  351. inline Vector2 midPoint( const Vector2& vec ) const
  352. {
  353. return Vector2(
  354. ( x + vec.x ) * 0.5f,
  355. ( y + vec.y ) * 0.5f );
  356. }
  357. /** Returns true if the vector's scalar components are all greater
  358. that the ones of the vector it is compared against.
  359. */
  360. inline bool operator < ( const Vector2& rhs ) const
  361. {
  362. if( x < rhs.x && y < rhs.y )
  363. return true;
  364. return false;
  365. }
  366. /** Returns true if the vector's scalar components are all smaller
  367. that the ones of the vector it is compared against.
  368. */
  369. inline bool operator > ( const Vector2& rhs ) const
  370. {
  371. if( x > rhs.x && y > rhs.y )
  372. return true;
  373. return false;
  374. }
  375. /** Sets this vector's components to the minimum of its own and the
  376. ones of the passed in vector.
  377. @remarks
  378. 'Minimum' in this case means the combination of the lowest
  379. value of x, y and z from both vectors. Lowest is taken just
  380. numerically, not magnitude, so -1 < 0.
  381. */
  382. inline void makeFloor( const Vector2& cmp )
  383. {
  384. if( cmp.x < x ) x = cmp.x;
  385. if( cmp.y < y ) y = cmp.y;
  386. }
  387. /** Sets this vector's components to the maximum of its own and the
  388. ones of the passed in vector.
  389. @remarks
  390. 'Maximum' in this case means the combination of the highest
  391. value of x, y and z from both vectors. Highest is taken just
  392. numerically, not magnitude, so 1 > -3.
  393. */
  394. inline void makeCeil( const Vector2& cmp )
  395. {
  396. if( cmp.x > x ) x = cmp.x;
  397. if( cmp.y > y ) y = cmp.y;
  398. }
  399. /** Generates a vector perpendicular to this vector (eg an 'up' vector).
  400. @remarks
  401. This method will return a vector which is perpendicular to this
  402. vector. There are an infinite number of possibilities but this
  403. method will guarantee to generate one of them. If you need more
  404. control you should use the Quaternion class.
  405. */
  406. inline Vector2 perpendicular(void) const
  407. {
  408. return Vector2 (-y, x);
  409. }
  410. /** Calculates the 2 dimensional cross-product of 2 vectors, which results
  411. in a single floating point value which is 2 times the area of the triangle.
  412. */
  413. inline float crossProduct( const Vector2& rkVector ) const
  414. {
  415. return x * rkVector.y - y * rkVector.x;
  416. }
  417. /** Generates a new random vector which deviates from this vector by a
  418. given angle in a random direction.
  419. @remarks
  420. This method assumes that the random number generator has already
  421. been seeded appropriately.
  422. @param
  423. angle The angle at which to deviate in radians
  424. @param
  425. up Any vector perpendicular to this one (which could generated
  426. by cross-product of this vector and any other non-colinear
  427. vector). If you choose not to provide this the function will
  428. derive one on it's own, however if you provide one yourself the
  429. function will be faster (this allows you to reuse up vectors if
  430. you call this method more than once)
  431. @returns
  432. A random vector which deviates from this vector by angle. This
  433. vector will not be normalised, normalise it if you wish
  434. afterwards.
  435. */
  436. inline Vector2 randomDeviant(
  437. float angle) const
  438. {
  439. angle *= Math::UnitRandom() * Math::TWO_PI;
  440. float cosa = cos(angle);
  441. float sina = sin(angle);
  442. return Vector2(cosa * x - sina * y,
  443. sina * x + cosa * y);
  444. }
  445. /** Returns true if this vector is zero length. */
  446. inline bool isZeroLength(void) const
  447. {
  448. float sqlen = (x * x) + (y * y);
  449. return (sqlen < (1e-06 * 1e-06));
  450. }
  451. /** As normalise, except that this vector is unaffected and the
  452. normalised vector is returned as a copy. */
  453. inline Vector2 normalizedCopy(void) const
  454. {
  455. Vector2 ret = *this;
  456. ret.normalize();
  457. return ret;
  458. }
  459. /** Calculates a reflection vector to the plane with the given normal .
  460. @remarks NB assumes 'this' is pointing AWAY FROM the plane, invert if it is not.
  461. */
  462. inline Vector2 reflect(const Vector2& normal) const
  463. {
  464. return Vector2( *this - ( 2 * this->dotProduct(normal) * normal ) );
  465. }
  466. /// Check whether this vector contains valid values
  467. inline bool isNaN() const
  468. {
  469. return Math::isNaN(x) || Math::isNaN(y);
  470. }
  471. static Vector2 min(const Vector2& a, const Vector2& b)
  472. {
  473. return Vector2(std::min(a.x, b.x), std::min(a.y, b.y));
  474. }
  475. static Vector2 max(const Vector2& a, const Vector2& b)
  476. {
  477. return Vector2(std::max(a.x, b.x), std::max(a.y, b.y));
  478. }
  479. // special points
  480. static const Vector2 ZERO;
  481. static const Vector2 UNIT_X;
  482. static const Vector2 UNIT_Y;
  483. static const Vector2 NEGATIVE_UNIT_X;
  484. static const Vector2 NEGATIVE_UNIT_Y;
  485. static const Vector2 UNIT_SCALE;
  486. /** Function for writing to a stream.
  487. */
  488. inline CM_UTILITY_EXPORT friend std::ostream& operator <<
  489. ( std::ostream& o, const Vector2& v )
  490. {
  491. o << "Vector2(" << v.x << ", " << v.y << ")";
  492. return o;
  493. }
  494. };
  495. /** @} */
  496. /** @} */
  497. CM_ALLOW_MEMCPY_SERIALIZATION(Vector2);
  498. }
  499. #endif