CmVector3.h 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809
  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 __Vector3_H__
  25. #define __Vector3_H__
  26. #include "CmPrerequisitesUtil.h"
  27. #include "CmMath.h"
  28. #include "CmQuaternion.h"
  29. namespace CamelotFramework
  30. {
  31. /** \addtogroup Core
  32. * @{
  33. */
  34. /** \addtogroup Math
  35. * @{
  36. */
  37. /** Standard 3-dimensional vector.
  38. @remarks
  39. A direction in 3D space represented as distances along the 3
  40. orthogonal axes (x, y, z). Note that positions, directions and
  41. scaling factors can be represented by a vector, depending on how
  42. you interpret the values.
  43. */
  44. class CM_UTILITY_EXPORT Vector3
  45. {
  46. public:
  47. float x, y, z;
  48. public:
  49. inline Vector3()
  50. {
  51. }
  52. inline Vector3( const float fX, const float fY, const float fZ )
  53. : x( fX ), y( fY ), z( fZ )
  54. {
  55. }
  56. inline explicit Vector3( const float afCoordinate[3] )
  57. : x( afCoordinate[0] ),
  58. y( afCoordinate[1] ),
  59. z( afCoordinate[2] )
  60. {
  61. }
  62. inline explicit Vector3( const int afCoordinate[3] )
  63. {
  64. x = (float)afCoordinate[0];
  65. y = (float)afCoordinate[1];
  66. z = (float)afCoordinate[2];
  67. }
  68. inline explicit Vector3( float* const r )
  69. : x( r[0] ), y( r[1] ), z( r[2] )
  70. {
  71. }
  72. inline explicit Vector3( const float scaler )
  73. : x( scaler )
  74. , y( scaler )
  75. , z( scaler )
  76. {
  77. }
  78. /** Exchange the contents of this vector with another.
  79. */
  80. inline void swap(Vector3& other)
  81. {
  82. std::swap(x, other.x);
  83. std::swap(y, other.y);
  84. std::swap(z, other.z);
  85. }
  86. inline float operator [] ( const size_t i ) const
  87. {
  88. assert( i < 3 );
  89. return *(&x+i);
  90. }
  91. inline float& operator [] ( const size_t i )
  92. {
  93. assert( i < 3 );
  94. return *(&x+i);
  95. }
  96. /// Pointer accessor for direct copying
  97. inline float* ptr()
  98. {
  99. return &x;
  100. }
  101. /// Pointer accessor for direct copying
  102. inline const float* ptr() const
  103. {
  104. return &x;
  105. }
  106. /** Assigns the value of the other vector.
  107. @param
  108. rkVector The other vector
  109. */
  110. inline Vector3& operator = ( const Vector3& rkVector )
  111. {
  112. x = rkVector.x;
  113. y = rkVector.y;
  114. z = rkVector.z;
  115. return *this;
  116. }
  117. inline Vector3& operator = ( const float fScaler )
  118. {
  119. x = fScaler;
  120. y = fScaler;
  121. z = fScaler;
  122. return *this;
  123. }
  124. inline bool operator == ( const Vector3& rkVector ) const
  125. {
  126. return ( x == rkVector.x && y == rkVector.y && z == rkVector.z );
  127. }
  128. inline bool operator != ( const Vector3& rkVector ) const
  129. {
  130. return ( x != rkVector.x || y != rkVector.y || z != rkVector.z );
  131. }
  132. // arithmetic operations
  133. inline Vector3 operator + ( const Vector3& rkVector ) const
  134. {
  135. return Vector3(
  136. x + rkVector.x,
  137. y + rkVector.y,
  138. z + rkVector.z);
  139. }
  140. inline Vector3 operator - ( const Vector3& rkVector ) const
  141. {
  142. return Vector3(
  143. x - rkVector.x,
  144. y - rkVector.y,
  145. z - rkVector.z);
  146. }
  147. inline Vector3 operator * ( const float fScalar ) const
  148. {
  149. return Vector3(
  150. x * fScalar,
  151. y * fScalar,
  152. z * fScalar);
  153. }
  154. inline Vector3 operator * ( const Vector3& rhs) const
  155. {
  156. return Vector3(
  157. x * rhs.x,
  158. y * rhs.y,
  159. z * rhs.z);
  160. }
  161. inline Vector3 operator / ( const float fScalar ) const
  162. {
  163. assert( fScalar != 0.0 );
  164. float fInv = 1.0f / fScalar;
  165. return Vector3(
  166. x * fInv,
  167. y * fInv,
  168. z * fInv);
  169. }
  170. inline Vector3 operator / ( const Vector3& rhs) const
  171. {
  172. return Vector3(
  173. x / rhs.x,
  174. y / rhs.y,
  175. z / rhs.z);
  176. }
  177. inline const Vector3& operator + () const
  178. {
  179. return *this;
  180. }
  181. inline Vector3 operator - () const
  182. {
  183. return Vector3(-x, -y, -z);
  184. }
  185. // overloaded operators to help Vector3
  186. inline friend Vector3 operator * ( const float fScalar, const Vector3& rkVector )
  187. {
  188. return Vector3(
  189. fScalar * rkVector.x,
  190. fScalar * rkVector.y,
  191. fScalar * rkVector.z);
  192. }
  193. inline friend Vector3 operator / ( const float fScalar, const Vector3& rkVector )
  194. {
  195. return Vector3(
  196. fScalar / rkVector.x,
  197. fScalar / rkVector.y,
  198. fScalar / rkVector.z);
  199. }
  200. inline friend Vector3 operator + (const Vector3& lhs, const float rhs)
  201. {
  202. return Vector3(
  203. lhs.x + rhs,
  204. lhs.y + rhs,
  205. lhs.z + rhs);
  206. }
  207. inline friend Vector3 operator + (const float lhs, const Vector3& rhs)
  208. {
  209. return Vector3(
  210. lhs + rhs.x,
  211. lhs + rhs.y,
  212. lhs + rhs.z);
  213. }
  214. inline friend Vector3 operator - (const Vector3& lhs, const float rhs)
  215. {
  216. return Vector3(
  217. lhs.x - rhs,
  218. lhs.y - rhs,
  219. lhs.z - rhs);
  220. }
  221. inline friend Vector3 operator - (const float lhs, const Vector3& rhs)
  222. {
  223. return Vector3(
  224. lhs - rhs.x,
  225. lhs - rhs.y,
  226. lhs - rhs.z);
  227. }
  228. // arithmetic updates
  229. inline Vector3& operator += ( const Vector3& rkVector )
  230. {
  231. x += rkVector.x;
  232. y += rkVector.y;
  233. z += rkVector.z;
  234. return *this;
  235. }
  236. inline Vector3& operator += ( const float fScalar )
  237. {
  238. x += fScalar;
  239. y += fScalar;
  240. z += fScalar;
  241. return *this;
  242. }
  243. inline Vector3& operator -= ( const Vector3& rkVector )
  244. {
  245. x -= rkVector.x;
  246. y -= rkVector.y;
  247. z -= rkVector.z;
  248. return *this;
  249. }
  250. inline Vector3& operator -= ( const float fScalar )
  251. {
  252. x -= fScalar;
  253. y -= fScalar;
  254. z -= fScalar;
  255. return *this;
  256. }
  257. inline Vector3& operator *= ( const float fScalar )
  258. {
  259. x *= fScalar;
  260. y *= fScalar;
  261. z *= fScalar;
  262. return *this;
  263. }
  264. inline Vector3& operator *= ( const Vector3& rkVector )
  265. {
  266. x *= rkVector.x;
  267. y *= rkVector.y;
  268. z *= rkVector.z;
  269. return *this;
  270. }
  271. inline Vector3& operator /= ( const float fScalar )
  272. {
  273. assert( fScalar != 0.0 );
  274. float fInv = 1.0f / fScalar;
  275. x *= fInv;
  276. y *= fInv;
  277. z *= fInv;
  278. return *this;
  279. }
  280. inline Vector3& operator /= ( const Vector3& rkVector )
  281. {
  282. x /= rkVector.x;
  283. y /= rkVector.y;
  284. z /= rkVector.z;
  285. return *this;
  286. }
  287. /** Returns the length (magnitude) of the vector.
  288. @warning
  289. This operation requires a square root and is expensive in
  290. terms of CPU operations. If you don't need to know the exact
  291. length (e.g. for just comparing lengths) use squaredLength()
  292. instead.
  293. */
  294. inline float length () const
  295. {
  296. return Math::Sqrt( x * x + y * y + z * z );
  297. }
  298. /** Returns the square of the length(magnitude) of the vector.
  299. @remarks
  300. This method is for efficiency - calculating the actual
  301. length of a vector requires a square root, which is expensive
  302. in terms of the operations required. This method returns the
  303. square of the length of the vector, i.e. the same as the
  304. length but before the square root is taken. Use this if you
  305. want to find the longest / shortest vector without incurring
  306. the square root.
  307. */
  308. inline float squaredLength () const
  309. {
  310. return x * x + y * y + z * z;
  311. }
  312. /** Returns the distance to another vector.
  313. @warning
  314. This operation requires a square root and is expensive in
  315. terms of CPU operations. If you don't need to know the exact
  316. distance (e.g. for just comparing distances) use squaredDistance()
  317. instead.
  318. */
  319. inline float distance(const Vector3& rhs) const
  320. {
  321. return (*this - rhs).length();
  322. }
  323. /** Returns the square of the distance to another vector.
  324. @remarks
  325. This method is for efficiency - calculating the actual
  326. distance to another vector requires a square root, which is
  327. expensive in terms of the operations required. This method
  328. returns the square of the distance to another vector, i.e.
  329. the same as the distance but before the square root is taken.
  330. Use this if you want to find the longest / shortest distance
  331. without incurring the square root.
  332. */
  333. inline float squaredDistance(const Vector3& rhs) const
  334. {
  335. return (*this - rhs).squaredLength();
  336. }
  337. /** Calculates the dot (scalar) product of this vector with another.
  338. @remarks
  339. The dot product can be used to calculate the angle between 2
  340. vectors. If both are unit vectors, the dot product is the
  341. cosine of the angle; otherwise the dot product must be
  342. divided by the product of the lengths of both vectors to get
  343. the cosine of the angle. This result can further be used to
  344. calculate the distance of a point from a plane.
  345. @param
  346. vec Vector with which to calculate the dot product (together
  347. with this one).
  348. @returns
  349. A float representing the dot product value.
  350. */
  351. inline float dotProduct(const Vector3& vec) const
  352. {
  353. return x * vec.x + y * vec.y + z * vec.z;
  354. }
  355. /** Calculates the absolute dot (scalar) product of this vector with another.
  356. @remarks
  357. This function work similar dotProduct, except it use absolute value
  358. of each component of the vector to computing.
  359. @param
  360. vec Vector with which to calculate the absolute dot product (together
  361. with this one).
  362. @returns
  363. A float representing the absolute dot product value.
  364. */
  365. inline float absDotProduct(const Vector3& vec) const
  366. {
  367. return Math::Abs(x * vec.x) + Math::Abs(y * vec.y) + Math::Abs(z * vec.z);
  368. }
  369. /** Normalises the vector.
  370. @remarks
  371. This method normalises the vector such that it's
  372. length / magnitude is 1. The result is called a unit vector.
  373. @note
  374. This function will not crash for zero-sized vectors, but there
  375. will be no changes made to their components.
  376. @returns The previous length of the vector.
  377. */
  378. inline float normalize()
  379. {
  380. float fLength = Math::Sqrt( x * x + y * y + z * z );
  381. // Will also work for zero-sized vectors, but will change nothing
  382. if ( fLength > 1e-08 )
  383. {
  384. float fInvLength = 1.0f / fLength;
  385. x *= fInvLength;
  386. y *= fInvLength;
  387. z *= fInvLength;
  388. }
  389. return fLength;
  390. }
  391. /** Calculates the cross-product of 2 vectors, i.e. the vector that
  392. lies perpendicular to them both.
  393. @remarks
  394. The cross-product is normally used to calculate the normal
  395. vector of a plane, by calculating the cross-product of 2
  396. non-equivalent vectors which lie on the plane (e.g. 2 edges
  397. of a triangle).
  398. @param
  399. vec Vector which, together with this one, will be used to
  400. calculate the cross-product.
  401. @returns
  402. A vector which is the result of the cross-product. This
  403. vector will <b>NOT</b> be normalised, to maximise efficiency
  404. - call Vector3::normalise on the result if you wish this to
  405. be done. As for which side the resultant vector will be on, the
  406. returned vector will be on the side from which the arc from 'this'
  407. to rkVector is anticlockwise, e.g. UNIT_Y.crossProduct(UNIT_Z)
  408. = UNIT_X, whilst UNIT_Z.crossProduct(UNIT_Y) = -UNIT_X.
  409. This is because engine uses a right-handed coordinate system.
  410. @par
  411. For a clearer explanation, look a the left and the bottom edges
  412. of your monitor's screen. Assume that the first vector is the
  413. left edge and the second vector is the bottom edge, both of
  414. them starting from the lower-left corner of the screen. The
  415. resulting vector is going to be perpendicular to both of them
  416. and will go <i>inside</i> the screen, towards the cathode tube
  417. (assuming you're using a CRT monitor, of course).
  418. */
  419. inline Vector3 crossProduct( const Vector3& rkVector ) const
  420. {
  421. return Vector3(
  422. y * rkVector.z - z * rkVector.y,
  423. z * rkVector.x - x * rkVector.z,
  424. x * rkVector.y - y * rkVector.x);
  425. }
  426. /** Returns a vector at a point half way between this and the passed
  427. in vector.
  428. */
  429. inline Vector3 midPoint( const Vector3& vec ) const
  430. {
  431. return Vector3(
  432. ( x + vec.x ) * 0.5f,
  433. ( y + vec.y ) * 0.5f,
  434. ( z + vec.z ) * 0.5f );
  435. }
  436. /** Returns true if the vector's scalar components are all greater
  437. that the ones of the vector it is compared against.
  438. */
  439. inline bool operator < ( const Vector3& rhs ) const
  440. {
  441. if( x < rhs.x && y < rhs.y && z < rhs.z )
  442. return true;
  443. return false;
  444. }
  445. /** Returns true if the vector's scalar components are all smaller
  446. that the ones of the vector it is compared against.
  447. */
  448. inline bool operator > ( const Vector3& rhs ) const
  449. {
  450. if( x > rhs.x && y > rhs.y && z > rhs.z )
  451. return true;
  452. return false;
  453. }
  454. /** Sets this vector's components to the minimum of its own and the
  455. ones of the passed in vector.
  456. @remarks
  457. 'Minimum' in this case means the combination of the lowest
  458. value of x, y and z from both vectors. Lowest is taken just
  459. numerically, not magnitude, so -1 < 0.
  460. */
  461. inline void makeFloor( const Vector3& cmp )
  462. {
  463. if( cmp.x < x ) x = cmp.x;
  464. if( cmp.y < y ) y = cmp.y;
  465. if( cmp.z < z ) z = cmp.z;
  466. }
  467. /** Sets this vector's components to the maximum of its own and the
  468. ones of the passed in vector.
  469. @remarks
  470. 'Maximum' in this case means the combination of the highest
  471. value of x, y and z from both vectors. Highest is taken just
  472. numerically, not magnitude, so 1 > -3.
  473. */
  474. inline void makeCeil( const Vector3& cmp )
  475. {
  476. if( cmp.x > x ) x = cmp.x;
  477. if( cmp.y > y ) y = cmp.y;
  478. if( cmp.z > z ) z = cmp.z;
  479. }
  480. /** Generates a vector perpendicular to this vector (eg an 'up' vector).
  481. @remarks
  482. This method will return a vector which is perpendicular to this
  483. vector. There are an infinite number of possibilities but this
  484. method will guarantee to generate one of them. If you need more
  485. control you should use the Quaternion class.
  486. */
  487. inline Vector3 perpendicular(void) const
  488. {
  489. static const float fSquareZero = (float)(1e-06 * 1e-06);
  490. Vector3 perp = this->crossProduct( Vector3::UNIT_X );
  491. // Check length
  492. if( perp.squaredLength() < fSquareZero )
  493. {
  494. /* This vector is the Y axis multiplied by a scalar, so we have
  495. to use another axis.
  496. */
  497. perp = this->crossProduct( Vector3::UNIT_Y );
  498. }
  499. perp.normalize();
  500. return perp;
  501. }
  502. /** Generates a new random vector which deviates from this vector by a
  503. given angle in a random direction.
  504. @remarks
  505. This method assumes that the random number generator has already
  506. been seeded appropriately.
  507. @param
  508. angle The angle at which to deviate
  509. @param
  510. up Any vector perpendicular to this one (which could generated
  511. by cross-product of this vector and any other non-colinear
  512. vector). If you choose not to provide this the function will
  513. derive one on it's own, however if you provide one yourself the
  514. function will be faster (this allows you to reuse up vectors if
  515. you call this method more than once)
  516. @returns
  517. A random vector which deviates from this vector by angle. This
  518. vector will not be normalised, normalise it if you wish
  519. afterwards.
  520. */
  521. inline Vector3 randomDeviant(
  522. const Radian& angle,
  523. const Vector3& up = Vector3::ZERO ) const
  524. {
  525. Vector3 newUp;
  526. if (up == Vector3::ZERO)
  527. {
  528. // Generate an up vector
  529. newUp = this->perpendicular();
  530. }
  531. else
  532. {
  533. newUp = up;
  534. }
  535. // Rotate up vector by random amount around this
  536. Quaternion q;
  537. q.FromAngleAxis( Radian(Math::UnitRandom() * Math::TWO_PI), *this );
  538. newUp = q * newUp;
  539. // Finally rotate this by given angle around randomised up
  540. q.FromAngleAxis( angle, newUp );
  541. return q * (*this);
  542. }
  543. /** Gets the angle between 2 vectors.
  544. @remarks
  545. Vectors do not have to be unit-length but must represent directions.
  546. */
  547. inline Radian angleBetween(const Vector3& dest)
  548. {
  549. float lenProduct = length() * dest.length();
  550. // Divide by zero check
  551. if(lenProduct < 1e-6f)
  552. lenProduct = 1e-6f;
  553. float f = dotProduct(dest) / lenProduct;
  554. f = Math::Clamp(f, (float)-1.0, (float)1.0);
  555. return Math::ACos(f);
  556. }
  557. /** Gets the shortest arc quaternion to rotate this vector to the destination
  558. vector.
  559. @remarks
  560. If you call this with a dest vector that is close to the inverse
  561. of this vector, we will rotate 180 degrees around the 'fallbackAxis'
  562. (if specified, or a generated axis if not) since in this case
  563. ANY axis of rotation is valid.
  564. */
  565. Quaternion getRotationTo(const Vector3& dest,
  566. const Vector3& fallbackAxis = Vector3::ZERO) const
  567. {
  568. // Based on Stan Melax's article in Game Programming Gems
  569. Quaternion q;
  570. // Copy, since cannot modify local
  571. Vector3 v0 = *this;
  572. Vector3 v1 = dest;
  573. v0.normalize();
  574. v1.normalize();
  575. float d = v0.dotProduct(v1);
  576. // If dot == 1, vectors are the same
  577. if (d >= 1.0f)
  578. {
  579. return Quaternion::IDENTITY;
  580. }
  581. if (d < (1e-6f - 1.0f))
  582. {
  583. if (fallbackAxis != Vector3::ZERO)
  584. {
  585. // rotate 180 degrees about the fallback axis
  586. q.FromAngleAxis(Radian(Math::PI), fallbackAxis);
  587. }
  588. else
  589. {
  590. // Generate an axis
  591. Vector3 axis = Vector3::UNIT_X.crossProduct(*this);
  592. if (axis.isZeroLength()) // pick another if colinear
  593. axis = Vector3::UNIT_Y.crossProduct(*this);
  594. axis.normalize();
  595. q.FromAngleAxis(Radian(Math::PI), axis);
  596. }
  597. }
  598. else
  599. {
  600. float s = Math::Sqrt( (1+d)*2 );
  601. float invs = 1 / s;
  602. Vector3 c = v0.crossProduct(v1);
  603. q.x = c.x * invs;
  604. q.y = c.y * invs;
  605. q.z = c.z * invs;
  606. q.w = s * 0.5f;
  607. q.normalize();
  608. }
  609. return q;
  610. }
  611. /** Returns true if this vector is zero length. */
  612. inline bool isZeroLength(void) const
  613. {
  614. float sqlen = (x * x) + (y * y) + (z * z);
  615. return (sqlen < (1e-06 * 1e-06));
  616. }
  617. /** As normalise, except that this vector is unaffected and the
  618. normalised vector is returned as a copy. */
  619. inline Vector3 normalizedCopy(void) const
  620. {
  621. Vector3 ret = *this;
  622. ret.normalize();
  623. return ret;
  624. }
  625. /** Calculates a reflection vector to the plane with the given normal .
  626. @remarks NB assumes 'this' is pointing AWAY FROM the plane, invert if it is not.
  627. */
  628. inline Vector3 reflect(const Vector3& normal) const
  629. {
  630. return Vector3( *this - ( 2 * this->dotProduct(normal) * normal ) );
  631. }
  632. /** Returns whether this vector is within a positional tolerance
  633. of another vector.
  634. @param rhs The vector to compare with
  635. @param tolerance The amount that each element of the vector may vary by
  636. and still be considered equal
  637. */
  638. inline bool positionEquals(const Vector3& rhs, float tolerance = 1e-03) const
  639. {
  640. return Math::RealEqual(x, rhs.x, tolerance) &&
  641. Math::RealEqual(y, rhs.y, tolerance) &&
  642. Math::RealEqual(z, rhs.z, tolerance);
  643. }
  644. /** Returns whether this vector is within a positional tolerance
  645. of another vector, also take scale of the vectors into account.
  646. @param rhs The vector to compare with
  647. @param tolerance The amount (related to the scale of vectors) that distance
  648. of the vector may vary by and still be considered close
  649. */
  650. inline bool positionCloses(const Vector3& rhs, float tolerance = 1e-03f) const
  651. {
  652. return squaredDistance(rhs) <=
  653. (squaredLength() + rhs.squaredLength()) * tolerance;
  654. }
  655. /** Returns whether this vector is within a directional tolerance
  656. of another vector.
  657. @param rhs The vector to compare with
  658. @param tolerance The maximum angle by which the vectors may vary and
  659. still be considered equal
  660. @note Both vectors should be normalised.
  661. */
  662. inline bool directionEquals(const Vector3& rhs,
  663. const Radian& tolerance) const
  664. {
  665. float dot = dotProduct(rhs);
  666. Radian angle = Math::ACos(dot);
  667. return Math::Abs(angle.valueRadians()) <= tolerance.valueRadians();
  668. }
  669. /// Check whether this vector contains valid values
  670. inline bool isNaN() const
  671. {
  672. return Math::isNaN(x) || Math::isNaN(y) || Math::isNaN(z);
  673. }
  674. static Vector3 min(const Vector3& a, const Vector3& b)
  675. {
  676. return Vector3(std::min(a.x, b.x), std::min(a.y, b.y), std::min(a.z, b.z));
  677. }
  678. static Vector3 max(const Vector3& a, const Vector3& b)
  679. {
  680. return Vector3(std::max(a.x, b.x), std::max(a.y, b.y), std::max(a.z, b.z));
  681. }
  682. // special points
  683. static const Vector3 ZERO;
  684. static const Vector3 UNIT_X;
  685. static const Vector3 UNIT_Y;
  686. static const Vector3 UNIT_Z;
  687. static const Vector3 RIGHT;
  688. static const Vector3 UP;
  689. static const Vector3 FORWARD;
  690. static const Vector3 NEGATIVE_UNIT_X;
  691. static const Vector3 NEGATIVE_UNIT_Y;
  692. static const Vector3 NEGATIVE_UNIT_Z;
  693. static const Vector3 UNIT_SCALE;
  694. /** Function for writing to a stream.
  695. */
  696. inline CM_UTILITY_EXPORT friend std::ostream& operator <<
  697. ( std::ostream& o, const Vector3& v )
  698. {
  699. o << "Vector3(" << v.x << ", " << v.y << ", " << v.z << ")";
  700. return o;
  701. }
  702. };
  703. /** @} */
  704. /** @} */
  705. CM_ALLOW_MEMCPY_SERIALIZATION(Vector3);
  706. }
  707. #endif