CmMath.h 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663
  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 __Math_H__
  25. #define __Math_H__
  26. #include "CmPrerequisitesUtil.h"
  27. namespace CamelotFramework
  28. {
  29. /** \addtogroup Core
  30. * @{
  31. */
  32. /** \addtogroup Math
  33. * @{
  34. */
  35. /** Wrapper class which indicates a given angle value is in Radians.
  36. @remarks
  37. Radian values are interchangeable with Degree values, and conversions
  38. will be done automatically between them.
  39. */
  40. class Radian
  41. {
  42. float mRad;
  43. public:
  44. explicit Radian ( float r=0 ) : mRad(r) {}
  45. Radian ( const Degree& d );
  46. Radian& operator = ( const float& f ) { mRad = f; return *this; }
  47. Radian& operator = ( const Radian& r ) { mRad = r.mRad; return *this; }
  48. Radian& operator = ( const Degree& d );
  49. float valueDegrees() const; // see bottom of this file
  50. float valueRadians() const { return mRad; }
  51. float valueAngleUnits() const;
  52. const Radian& operator + () const { return *this; }
  53. Radian operator + ( const Radian& r ) const { return Radian ( mRad + r.mRad ); }
  54. Radian operator + ( const Degree& d ) const;
  55. Radian& operator += ( const Radian& r ) { mRad += r.mRad; return *this; }
  56. Radian& operator += ( const Degree& d );
  57. Radian operator - () const { return Radian(-mRad); }
  58. Radian operator - ( const Radian& r ) const { return Radian ( mRad - r.mRad ); }
  59. Radian operator - ( const Degree& d ) const;
  60. Radian& operator -= ( const Radian& r ) { mRad -= r.mRad; return *this; }
  61. Radian& operator -= ( const Degree& d );
  62. Radian operator * ( float f ) const { return Radian ( mRad * f ); }
  63. Radian operator * ( const Radian& f ) const { return Radian ( mRad * f.mRad ); }
  64. Radian& operator *= ( float f ) { mRad *= f; return *this; }
  65. Radian operator / ( float f ) const { return Radian ( mRad / f ); }
  66. Radian& operator /= ( float f ) { mRad /= f; return *this; }
  67. bool operator < ( const Radian& r ) const { return mRad < r.mRad; }
  68. bool operator <= ( const Radian& r ) const { return mRad <= r.mRad; }
  69. bool operator == ( const Radian& r ) const { return mRad == r.mRad; }
  70. bool operator != ( const Radian& r ) const { return mRad != r.mRad; }
  71. bool operator >= ( const Radian& r ) const { return mRad >= r.mRad; }
  72. bool operator > ( const Radian& r ) const { return mRad > r.mRad; }
  73. inline CM_UTILITY_EXPORT friend std::ostream& operator <<
  74. ( std::ostream& o, const Radian& v )
  75. {
  76. o << "Radian(" << v.valueRadians() << ")";
  77. return o;
  78. }
  79. };
  80. /** Wrapper class which indicates a given angle value is in Degrees.
  81. @remarks
  82. Degree values are interchangeable with Radian values, and conversions
  83. will be done automatically between them.
  84. */
  85. class Degree
  86. {
  87. float mDeg; // if you get an error here - make sure to define/typedef 'float' first
  88. public:
  89. explicit Degree ( float d=0 ) : mDeg(d) {}
  90. Degree ( const Radian& r ) : mDeg(r.valueDegrees()) {}
  91. Degree& operator = ( const float& f ) { mDeg = f; return *this; }
  92. Degree& operator = ( const Degree& d ) { mDeg = d.mDeg; return *this; }
  93. Degree& operator = ( const Radian& r ) { mDeg = r.valueDegrees(); return *this; }
  94. float valueDegrees() const { return mDeg; }
  95. float valueRadians() const; // see bottom of this file
  96. float valueAngleUnits() const;
  97. const Degree& operator + () const { return *this; }
  98. Degree operator + ( const Degree& d ) const { return Degree ( mDeg + d.mDeg ); }
  99. Degree operator + ( const Radian& r ) const { return Degree ( mDeg + r.valueDegrees() ); }
  100. Degree& operator += ( const Degree& d ) { mDeg += d.mDeg; return *this; }
  101. Degree& operator += ( const Radian& r ) { mDeg += r.valueDegrees(); return *this; }
  102. Degree operator - () const { return Degree(-mDeg); }
  103. Degree operator - ( const Degree& d ) const { return Degree ( mDeg - d.mDeg ); }
  104. Degree operator - ( const Radian& r ) const { return Degree ( mDeg - r.valueDegrees() ); }
  105. Degree& operator -= ( const Degree& d ) { mDeg -= d.mDeg; return *this; }
  106. Degree& operator -= ( const Radian& r ) { mDeg -= r.valueDegrees(); return *this; }
  107. Degree operator * ( float f ) const { return Degree ( mDeg * f ); }
  108. Degree operator * ( const Degree& f ) const { return Degree ( mDeg * f.mDeg ); }
  109. Degree& operator *= ( float f ) { mDeg *= f; return *this; }
  110. Degree operator / ( float f ) const { return Degree ( mDeg / f ); }
  111. Degree& operator /= ( float f ) { mDeg /= f; return *this; }
  112. bool operator < ( const Degree& d ) const { return mDeg < d.mDeg; }
  113. bool operator <= ( const Degree& d ) const { return mDeg <= d.mDeg; }
  114. bool operator == ( const Degree& d ) const { return mDeg == d.mDeg; }
  115. bool operator != ( const Degree& d ) const { return mDeg != d.mDeg; }
  116. bool operator >= ( const Degree& d ) const { return mDeg >= d.mDeg; }
  117. bool operator > ( const Degree& d ) const { return mDeg > d.mDeg; }
  118. inline CM_UTILITY_EXPORT friend std::ostream& operator <<
  119. ( std::ostream& o, const Degree& v )
  120. {
  121. o << "Degree(" << v.valueDegrees() << ")";
  122. return o;
  123. }
  124. };
  125. /** Wrapper class which identifies a value as the currently default angle
  126. type, as defined by Math::setAngleUnit.
  127. @remarks
  128. Angle values will be automatically converted between radians and degrees,
  129. as appropriate.
  130. */
  131. class Angle
  132. {
  133. float mAngle;
  134. public:
  135. explicit Angle ( float angle ) : mAngle(angle) {}
  136. operator Radian() const;
  137. operator Degree() const;
  138. };
  139. // these functions could not be defined within the class definition of class
  140. // Radian because they required class Degree to be defined
  141. inline Radian::Radian ( const Degree& d ) : mRad(d.valueRadians()) {
  142. }
  143. inline Radian& Radian::operator = ( const Degree& d ) {
  144. mRad = d.valueRadians(); return *this;
  145. }
  146. inline Radian Radian::operator + ( const Degree& d ) const {
  147. return Radian ( mRad + d.valueRadians() );
  148. }
  149. inline Radian& Radian::operator += ( const Degree& d ) {
  150. mRad += d.valueRadians();
  151. return *this;
  152. }
  153. inline Radian Radian::operator - ( const Degree& d ) const {
  154. return Radian ( mRad - d.valueRadians() );
  155. }
  156. inline Radian& Radian::operator -= ( const Degree& d ) {
  157. mRad -= d.valueRadians();
  158. return *this;
  159. }
  160. /** Class to provide access to common mathematical functions.
  161. @remarks
  162. Most of the maths functions are aliased versions of the C runtime
  163. library functions. They are aliased here to provide future
  164. optimisation opportunities, either from faster RTLs or custom
  165. math approximations.
  166. @note
  167. <br>This is based on MgcMath.h from
  168. <a href="http://www.geometrictools.com/">Wild Magic</a>.
  169. */
  170. class CM_UTILITY_EXPORT Math
  171. {
  172. public:
  173. /** The angular units used by the API. This functionality is now deprecated in favor
  174. of discreet angular unit types ( see Degree and Radian above ). The only place
  175. this functionality is actually still used is when parsing files. Search for
  176. usage of the Angle class for those instances
  177. */
  178. enum AngleUnit
  179. {
  180. AU_DEGREE,
  181. AU_RADIAN
  182. };
  183. protected:
  184. // angle units used by the api
  185. static AngleUnit msAngleUnit;
  186. /// Size of the trig tables as determined by constructor.
  187. static int mTrigTableSize;
  188. /// Radian -> index factor value ( mTrigTableSize / 2 * PI )
  189. static float mTrigTableFactor;
  190. static float* mSinTable;
  191. static float* mTanTable;
  192. /** Private function to build trig tables.
  193. */
  194. void buildTrigTables();
  195. static float SinTable (float fValue);
  196. static float TanTable (float fValue);
  197. public:
  198. /** Default constructor.
  199. @param
  200. trigTableSize Optional parameter to set the size of the
  201. tables used to implement Sin, Cos, Tan
  202. */
  203. Math(unsigned int trigTableSize = 4096);
  204. /** Default destructor.
  205. */
  206. ~Math();
  207. static inline float Abs (float fValue) { return float(fabs(fValue)); }
  208. static inline Degree Abs (const Degree& dValue) { return Degree(fabs(dValue.valueDegrees())); }
  209. static inline Radian Abs (const Radian& rValue) { return Radian(fabs(rValue.valueRadians())); }
  210. static Radian ACos (float fValue);
  211. static Radian ASin (float fValue);
  212. static inline Radian ATan (float fValue) { return Radian(atan(fValue)); }
  213. static inline Radian ATan2 (float fY, float fX) { return Radian(atan2(fY,fX)); }
  214. static inline float Ceil (float fValue) { return float(ceil(fValue)); }
  215. static inline int CeilToInt (float fValue) { return int(ceil(fValue)); }
  216. static inline float Round (float fValue) { return floor(fValue + 0.5f); }
  217. static inline int RoundToInt (float fValue) { return int(floor(fValue + 0.5f)); }
  218. static inline bool isNaN(float f)
  219. {
  220. // std::isnan() is C99, not supported by all compilers
  221. // However NaN always fails this next test, no other number does.
  222. return f != f;
  223. }
  224. /** Cosine function.
  225. @param
  226. fValue Angle in radians
  227. @param
  228. useTables If true, uses lookup tables rather than
  229. calculation - faster but less accurate.
  230. */
  231. static inline float Cos (const Radian& fValue, bool useTables = false) {
  232. return (!useTables) ? float(cos(fValue.valueRadians())) : SinTable(fValue.valueRadians() + HALF_PI);
  233. }
  234. /** Cosine function.
  235. @param
  236. fValue Angle in radians
  237. @param
  238. useTables If true, uses lookup tables rather than
  239. calculation - faster but less accurate.
  240. */
  241. static inline float Cos (float fValue, bool useTables = false) {
  242. return (!useTables) ? float(cos(fValue)) : SinTable(fValue + HALF_PI);
  243. }
  244. static inline float Exp (float fValue) { return float(exp(fValue)); }
  245. static inline float Floor (float fValue) { return float(floor(fValue)); }
  246. static inline int FloorToInt (float fValue) { return int(floor(fValue)); }
  247. static inline float Log (float fValue) { return float(log(fValue)); }
  248. /// Stored value of log(2) for frequent use
  249. static const float LOG2;
  250. static inline float Log2 (float fValue) { return float(log(fValue)/LOG2); }
  251. static inline float LogN (float base, float fValue) { return float(log(fValue)/log(base)); }
  252. static inline float Pow (float fBase, float fExponent) { return float(pow(fBase,fExponent)); }
  253. static float Sign (float fValue);
  254. static inline Radian Sign ( const Radian& rValue )
  255. {
  256. return Radian(Sign(rValue.valueRadians()));
  257. }
  258. static inline Degree Sign ( const Degree& dValue )
  259. {
  260. return Degree(Sign(dValue.valueDegrees()));
  261. }
  262. /** Sine function.
  263. @param
  264. fValue Angle in radians
  265. @param
  266. useTables If true, uses lookup tables rather than
  267. calculation - faster but less accurate.
  268. */
  269. static inline float Sin (const Radian& fValue, bool useTables = false) {
  270. return (!useTables) ? float(sin(fValue.valueRadians())) : SinTable(fValue.valueRadians());
  271. }
  272. /** Sine function.
  273. @param
  274. fValue Angle in radians
  275. @param
  276. useTables If true, uses lookup tables rather than
  277. calculation - faster but less accurate.
  278. */
  279. static inline float Sin (float fValue, bool useTables = false) {
  280. return (!useTables) ? float(sin(fValue)) : SinTable(fValue);
  281. }
  282. static inline float Sqr (float fValue) { return fValue*fValue; }
  283. static inline float Sqrt (float fValue) { return float(sqrt(fValue)); }
  284. static inline Radian Sqrt (const Radian& fValue) { return Radian(sqrt(fValue.valueRadians())); }
  285. static inline Degree Sqrt (const Degree& fValue) { return Degree(sqrt(fValue.valueDegrees())); }
  286. /** Inverse square root i.e. 1 / Sqrt(x), good for vector
  287. normalisation.
  288. */
  289. static float InvSqrt(float fValue);
  290. static float UnitRandom (); // in [0,1]
  291. static float RangeRandom (float fLow, float fHigh); // in [fLow,fHigh]
  292. static float SymmetricRandom (); // in [-1,1]
  293. /** Tangent function.
  294. @param
  295. fValue Angle in radians
  296. @param
  297. useTables If true, uses lookup tables rather than
  298. calculation - faster but less accurate.
  299. */
  300. static inline float Tan (const Radian& fValue, bool useTables = false) {
  301. return (!useTables) ? float(tan(fValue.valueRadians())) : TanTable(fValue.valueRadians());
  302. }
  303. /** Tangent function.
  304. @param
  305. fValue Angle in radians
  306. @param
  307. useTables If true, uses lookup tables rather than
  308. calculation - faster but less accurate.
  309. */
  310. static inline float Tan (float fValue, bool useTables = false) {
  311. return (!useTables) ? float(tan(fValue)) : TanTable(fValue);
  312. }
  313. static inline float DegreesToRadians(float degrees) { return degrees * fDeg2Rad; }
  314. static inline float RadiansToDegrees(float radians) { return radians * fRad2Deg; }
  315. /** These functions used to set the assumed angle units (radians or degrees)
  316. expected when using the Angle type.
  317. @par
  318. You can set this directly after creating a new Root, and also before/after resource creation,
  319. depending on whether you want the change to affect resource files.
  320. */
  321. static void setAngleUnit(AngleUnit unit);
  322. /** Get the unit being used for angles. */
  323. static AngleUnit getAngleUnit(void);
  324. /** Convert from the current AngleUnit to radians. */
  325. static float AngleUnitsToRadians(float units);
  326. /** Convert from radians to the current AngleUnit . */
  327. static float RadiansToAngleUnits(float radians);
  328. /** Convert from the current AngleUnit to degrees. */
  329. static float AngleUnitsToDegrees(float units);
  330. /** Convert from degrees to the current AngleUnit. */
  331. static float DegreesToAngleUnits(float degrees);
  332. /** Checks whether a given point is inside a triangle, in a
  333. 2-dimensional (Cartesian) space.
  334. @remarks
  335. The vertices of the triangle must be given in either
  336. trigonometrical (anticlockwise) or inverse trigonometrical
  337. (clockwise) order.
  338. @param
  339. p The point.
  340. @param
  341. a The triangle's first vertex.
  342. @param
  343. b The triangle's second vertex.
  344. @param
  345. c The triangle's third vertex.
  346. @returns
  347. If the point resides in the triangle, <b>true</b> is
  348. returned.
  349. @par
  350. If the point is outside the triangle, <b>false</b> is
  351. returned.
  352. */
  353. static bool pointInTri2D(const Vector2& p, const Vector2& a,
  354. const Vector2& b, const Vector2& c);
  355. /** Checks whether a given 3D point is inside a triangle.
  356. @remarks
  357. The vertices of the triangle must be given in either
  358. trigonometrical (anticlockwise) or inverse trigonometrical
  359. (clockwise) order, and the point must be guaranteed to be in the
  360. same plane as the triangle
  361. @param
  362. p The point.
  363. @param
  364. a The triangle's first vertex.
  365. @param
  366. b The triangle's second vertex.
  367. @param
  368. c The triangle's third vertex.
  369. @param
  370. normal The triangle plane's normal (passed in rather than calculated
  371. on demand since the caller may already have it)
  372. @returns
  373. If the point resides in the triangle, <b>true</b> is
  374. returned.
  375. @par
  376. If the point is outside the triangle, <b>false</b> is
  377. returned.
  378. */
  379. static bool pointInTri3D(const Vector3& p, const Vector3& a,
  380. const Vector3& b, const Vector3& c, const Vector3& normal);
  381. /** Ray / plane intersection, returns boolean result and distance. */
  382. static std::pair<bool, float> intersects(const Ray& ray, const Plane& plane);
  383. /** Ray / sphere intersection, returns boolean result and distance. */
  384. static std::pair<bool, float> intersects(const Ray& ray, const Sphere& sphere,
  385. bool discardInside = true);
  386. /** Ray / box intersection, returns boolean result and distance. */
  387. static std::pair<bool, float> intersects(const Ray& ray, const AABox& box);
  388. /** Ray / box intersection, returns boolean result and two intersection distance.
  389. @param
  390. ray The ray.
  391. @param
  392. box The box.
  393. @param
  394. d1 A real pointer to retrieve the near intersection distance
  395. from the ray origin, maybe <b>null</b> which means don't care
  396. about the near intersection distance.
  397. @param
  398. d2 A real pointer to retrieve the far intersection distance
  399. from the ray origin, maybe <b>null</b> which means don't care
  400. about the far intersection distance.
  401. @returns
  402. If the ray is intersects the box, <b>true</b> is returned, and
  403. the near intersection distance is return by <i>d1</i>, the
  404. far intersection distance is return by <i>d2</i>. Guarantee
  405. <b>0</b> <= <i>d1</i> <= <i>d2</i>.
  406. @par
  407. If the ray isn't intersects the box, <b>false</b> is returned, and
  408. <i>d1</i> and <i>d2</i> is unmodified.
  409. */
  410. static bool intersects(const Ray& ray, const AABox& box,
  411. float* d1, float* d2);
  412. /** Ray / triangle intersection, returns boolean result and distance.
  413. @param
  414. ray The ray.
  415. @param
  416. a The triangle's first vertex.
  417. @param
  418. b The triangle's second vertex.
  419. @param
  420. c The triangle's third vertex.
  421. @param
  422. normal The triangle plane's normal (passed in rather than calculated
  423. on demand since the caller may already have it), doesn't need
  424. normalised since we don't care.
  425. @param
  426. positiveSide Intersect with "positive side" of the triangle
  427. @param
  428. negativeSide Intersect with "negative side" of the triangle
  429. @returns
  430. If the ray is intersects the triangle, a pair of <b>true</b> and the
  431. distance between intersection point and ray origin returned.
  432. @par
  433. If the ray isn't intersects the triangle, a pair of <b>false</b> and
  434. <b>0</b> returned.
  435. */
  436. static std::pair<bool, float> intersects(const Ray& ray, const Vector3& a,
  437. const Vector3& b, const Vector3& c, const Vector3& normal,
  438. bool positiveSide = true, bool negativeSide = true);
  439. /** Ray / triangle intersection, returns boolean result and distance.
  440. @param
  441. ray The ray.
  442. @param
  443. a The triangle's first vertex.
  444. @param
  445. b The triangle's second vertex.
  446. @param
  447. c The triangle's third vertex.
  448. @param
  449. positiveSide Intersect with "positive side" of the triangle
  450. @param
  451. negativeSide Intersect with "negative side" of the triangle
  452. @returns
  453. If the ray is intersects the triangle, a pair of <b>true</b> and the
  454. distance between intersection point and ray origin returned.
  455. @par
  456. If the ray isn't intersects the triangle, a pair of <b>false</b> and
  457. <b>0</b> returned.
  458. */
  459. static std::pair<bool, float> intersects(const Ray& ray, const Vector3& a,
  460. const Vector3& b, const Vector3& c,
  461. bool positiveSide = true, bool negativeSide = true);
  462. /** Sphere / box intersection test. */
  463. static bool intersects(const Sphere& sphere, const AABox& box);
  464. /** Plane / box intersection test. */
  465. static bool intersects(const Plane& plane, const AABox& box);
  466. /** Ray / convex plane list intersection test.
  467. @param ray The ray to test with
  468. @param plaeList List of planes which form a convex volume
  469. @param normalIsOutside Does the normal point outside the volume
  470. */
  471. static std::pair<bool, float> intersects(
  472. const Ray& ray, const Vector<Plane>::type& planeList,
  473. bool normalIsOutside);
  474. /** Ray / convex plane list intersection test.
  475. @param ray The ray to test with
  476. @param plaeList List of planes which form a convex volume
  477. @param normalIsOutside Does the normal point outside the volume
  478. */
  479. static std::pair<bool, float> intersects(
  480. const Ray& ray, const List<Plane>::type& planeList,
  481. bool normalIsOutside);
  482. /** Sphere / plane intersection test.
  483. @remarks NB just do a plane.getDistance(sphere.getCenter()) for more detail!
  484. */
  485. static bool intersects(const Sphere& sphere, const Plane& plane);
  486. /** Compare 2 reals, using tolerance for inaccuracies.
  487. */
  488. static bool RealEqual(float a, float b,
  489. float tolerance = std::numeric_limits<float>::epsilon());
  490. /** Calculates the tangent space vector for a given set of positions / texture coords. */
  491. static Vector3 calculateTangentSpaceVector(
  492. const Vector3& position1, const Vector3& position2, const Vector3& position3,
  493. float u1, float v1, float u2, float v2, float u3, float v3);
  494. /** Build a reflection matrix for the passed in plane. */
  495. static Matrix4 buildReflectionMatrix(const Plane& p);
  496. /** Calculate a face normal, including the w component which is the offset from the origin. */
  497. static Vector4 calculateFaceNormal(const Vector3& v1, const Vector3& v2, const Vector3& v3);
  498. /** Calculate a face normal, no w-information. */
  499. static Vector3 calculateBasicFaceNormal(const Vector3& v1, const Vector3& v2, const Vector3& v3);
  500. /** Calculate a face normal without normalize, including the w component which is the offset from the origin. */
  501. static Vector4 calculateFaceNormalWithoutNormalize(const Vector3& v1, const Vector3& v2, const Vector3& v3);
  502. /** Calculate a face normal without normalize, no w-information. */
  503. static Vector3 calculateBasicFaceNormalWithoutNormalize(const Vector3& v1, const Vector3& v2, const Vector3& v3);
  504. /** Generates a value based on the Gaussian (normal) distribution function
  505. with the given offset and scale parameters.
  506. */
  507. static float gaussianDistribution(float x, float offset = 0.0f, float scale = 1.0f);
  508. /** Clamp a value within an inclusive range. */
  509. template <typename T>
  510. static T Clamp(T val, T minval, T maxval)
  511. {
  512. assert (minval <= maxval && "Invalid clamp range");
  513. return std::max(std::min(val, maxval), minval);
  514. }
  515. /** Clamp a value within an inclusive range. */
  516. template <typename T>
  517. static T Clamp01(T val)
  518. {
  519. return std::max(std::min(val, (T)1), (T)0);
  520. }
  521. static Matrix4 makeViewMatrix(const Vector3& position, const Quaternion& orientation,
  522. const Matrix4* reflectMatrix = 0);
  523. /** Get a bounding radius value from a bounding box. */
  524. static float boundingRadiusFromAABB(const AABox& aabb);
  525. static const float POS_INFINITY;
  526. static const float NEG_INFINITY;
  527. static const float PI;
  528. static const float TWO_PI;
  529. static const float HALF_PI;
  530. static const float fDeg2Rad;
  531. static const float fRad2Deg;
  532. };
  533. // these functions must be defined down here, because they rely on the
  534. // angle unit conversion functions in class Math:
  535. inline float Radian::valueDegrees() const
  536. {
  537. return Math::RadiansToDegrees ( mRad );
  538. }
  539. inline float Radian::valueAngleUnits() const
  540. {
  541. return Math::RadiansToAngleUnits ( mRad );
  542. }
  543. inline float Degree::valueRadians() const
  544. {
  545. return Math::DegreesToRadians ( mDeg );
  546. }
  547. inline float Degree::valueAngleUnits() const
  548. {
  549. return Math::DegreesToAngleUnits ( mDeg );
  550. }
  551. inline Angle::operator Radian() const
  552. {
  553. return Radian(Math::AngleUnitsToRadians(mAngle));
  554. }
  555. inline Angle::operator Degree() const
  556. {
  557. return Degree(Math::AngleUnitsToDegrees(mAngle));
  558. }
  559. inline Radian operator * ( float a, const Radian& b )
  560. {
  561. return Radian ( a * b.valueRadians() );
  562. }
  563. inline Radian operator / ( float a, const Radian& b )
  564. {
  565. return Radian ( a / b.valueRadians() );
  566. }
  567. inline Degree operator * ( float a, const Degree& b )
  568. {
  569. return Degree ( a * b.valueDegrees() );
  570. }
  571. inline Degree operator / ( float a, const Degree& b )
  572. {
  573. return Degree ( a / b.valueDegrees() );
  574. }
  575. /** @} */
  576. /** @} */
  577. }
  578. #endif