CmMath.h 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654
  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 bool isNaN(float f)
  217. {
  218. // std::isnan() is C99, not supported by all compilers
  219. // However NaN always fails this next test, no other number does.
  220. return f != f;
  221. }
  222. /** Cosine function.
  223. @param
  224. fValue Angle in radians
  225. @param
  226. useTables If true, uses lookup tables rather than
  227. calculation - faster but less accurate.
  228. */
  229. static inline float Cos (const Radian& fValue, bool useTables = false) {
  230. return (!useTables) ? float(cos(fValue.valueRadians())) : SinTable(fValue.valueRadians() + HALF_PI);
  231. }
  232. /** Cosine function.
  233. @param
  234. fValue Angle in radians
  235. @param
  236. useTables If true, uses lookup tables rather than
  237. calculation - faster but less accurate.
  238. */
  239. static inline float Cos (float fValue, bool useTables = false) {
  240. return (!useTables) ? float(cos(fValue)) : SinTable(fValue + HALF_PI);
  241. }
  242. static inline float Exp (float fValue) { return float(exp(fValue)); }
  243. static inline float Floor (float fValue) { return float(floor(fValue)); }
  244. static inline int FloorToInt (float fValue) { return int(floor(fValue)); }
  245. static inline float Log (float fValue) { return float(log(fValue)); }
  246. /// Stored value of log(2) for frequent use
  247. static const float LOG2;
  248. static inline float Log2 (float fValue) { return float(log(fValue)/LOG2); }
  249. static inline float LogN (float base, float fValue) { return float(log(fValue)/log(base)); }
  250. static inline float Pow (float fBase, float fExponent) { return float(pow(fBase,fExponent)); }
  251. static float Sign (float fValue);
  252. static inline Radian Sign ( const Radian& rValue )
  253. {
  254. return Radian(Sign(rValue.valueRadians()));
  255. }
  256. static inline Degree Sign ( const Degree& dValue )
  257. {
  258. return Degree(Sign(dValue.valueDegrees()));
  259. }
  260. /** Sine function.
  261. @param
  262. fValue Angle in radians
  263. @param
  264. useTables If true, uses lookup tables rather than
  265. calculation - faster but less accurate.
  266. */
  267. static inline float Sin (const Radian& fValue, bool useTables = false) {
  268. return (!useTables) ? float(sin(fValue.valueRadians())) : SinTable(fValue.valueRadians());
  269. }
  270. /** Sine function.
  271. @param
  272. fValue Angle in radians
  273. @param
  274. useTables If true, uses lookup tables rather than
  275. calculation - faster but less accurate.
  276. */
  277. static inline float Sin (float fValue, bool useTables = false) {
  278. return (!useTables) ? float(sin(fValue)) : SinTable(fValue);
  279. }
  280. static inline float Sqr (float fValue) { return fValue*fValue; }
  281. static inline float Sqrt (float fValue) { return float(sqrt(fValue)); }
  282. static inline Radian Sqrt (const Radian& fValue) { return Radian(sqrt(fValue.valueRadians())); }
  283. static inline Degree Sqrt (const Degree& fValue) { return Degree(sqrt(fValue.valueDegrees())); }
  284. /** Inverse square root i.e. 1 / Sqrt(x), good for vector
  285. normalisation.
  286. */
  287. static float InvSqrt(float fValue);
  288. static float UnitRandom (); // in [0,1]
  289. static float RangeRandom (float fLow, float fHigh); // in [fLow,fHigh]
  290. static float SymmetricRandom (); // in [-1,1]
  291. /** Tangent function.
  292. @param
  293. fValue Angle in radians
  294. @param
  295. useTables If true, uses lookup tables rather than
  296. calculation - faster but less accurate.
  297. */
  298. static inline float Tan (const Radian& fValue, bool useTables = false) {
  299. return (!useTables) ? float(tan(fValue.valueRadians())) : TanTable(fValue.valueRadians());
  300. }
  301. /** Tangent function.
  302. @param
  303. fValue Angle in radians
  304. @param
  305. useTables If true, uses lookup tables rather than
  306. calculation - faster but less accurate.
  307. */
  308. static inline float Tan (float fValue, bool useTables = false) {
  309. return (!useTables) ? float(tan(fValue)) : TanTable(fValue);
  310. }
  311. static inline float DegreesToRadians(float degrees) { return degrees * fDeg2Rad; }
  312. static inline float RadiansToDegrees(float radians) { return radians * fRad2Deg; }
  313. /** These functions used to set the assumed angle units (radians or degrees)
  314. expected when using the Angle type.
  315. @par
  316. You can set this directly after creating a new Root, and also before/after resource creation,
  317. depending on whether you want the change to affect resource files.
  318. */
  319. static void setAngleUnit(AngleUnit unit);
  320. /** Get the unit being used for angles. */
  321. static AngleUnit getAngleUnit(void);
  322. /** Convert from the current AngleUnit to radians. */
  323. static float AngleUnitsToRadians(float units);
  324. /** Convert from radians to the current AngleUnit . */
  325. static float RadiansToAngleUnits(float radians);
  326. /** Convert from the current AngleUnit to degrees. */
  327. static float AngleUnitsToDegrees(float units);
  328. /** Convert from degrees to the current AngleUnit. */
  329. static float DegreesToAngleUnits(float degrees);
  330. /** Checks whether a given point is inside a triangle, in a
  331. 2-dimensional (Cartesian) space.
  332. @remarks
  333. The vertices of the triangle must be given in either
  334. trigonometrical (anticlockwise) or inverse trigonometrical
  335. (clockwise) order.
  336. @param
  337. p The point.
  338. @param
  339. a The triangle's first vertex.
  340. @param
  341. b The triangle's second vertex.
  342. @param
  343. c The triangle's third vertex.
  344. @returns
  345. If the point resides in the triangle, <b>true</b> is
  346. returned.
  347. @par
  348. If the point is outside the triangle, <b>false</b> is
  349. returned.
  350. */
  351. static bool pointInTri2D(const Vector2& p, const Vector2& a,
  352. const Vector2& b, const Vector2& c);
  353. /** Checks whether a given 3D point is inside a triangle.
  354. @remarks
  355. The vertices of the triangle must be given in either
  356. trigonometrical (anticlockwise) or inverse trigonometrical
  357. (clockwise) order, and the point must be guaranteed to be in the
  358. same plane as the triangle
  359. @param
  360. p The point.
  361. @param
  362. a The triangle's first vertex.
  363. @param
  364. b The triangle's second vertex.
  365. @param
  366. c The triangle's third vertex.
  367. @param
  368. normal The triangle plane's normal (passed in rather than calculated
  369. on demand since the caller may already have it)
  370. @returns
  371. If the point resides in the triangle, <b>true</b> is
  372. returned.
  373. @par
  374. If the point is outside the triangle, <b>false</b> is
  375. returned.
  376. */
  377. static bool pointInTri3D(const Vector3& p, const Vector3& a,
  378. const Vector3& b, const Vector3& c, const Vector3& normal);
  379. /** Ray / plane intersection, returns boolean result and distance. */
  380. static std::pair<bool, float> intersects(const Ray& ray, const Plane& plane);
  381. /** Ray / sphere intersection, returns boolean result and distance. */
  382. static std::pair<bool, float> intersects(const Ray& ray, const Sphere& sphere,
  383. bool discardInside = true);
  384. /** Ray / box intersection, returns boolean result and distance. */
  385. static std::pair<bool, float> intersects(const Ray& ray, const AxisAlignedBox& box);
  386. /** Ray / box intersection, returns boolean result and two intersection distance.
  387. @param
  388. ray The ray.
  389. @param
  390. box The box.
  391. @param
  392. d1 A real pointer to retrieve the near intersection distance
  393. from the ray origin, maybe <b>null</b> which means don't care
  394. about the near intersection distance.
  395. @param
  396. d2 A real pointer to retrieve the far intersection distance
  397. from the ray origin, maybe <b>null</b> which means don't care
  398. about the far intersection distance.
  399. @returns
  400. If the ray is intersects the box, <b>true</b> is returned, and
  401. the near intersection distance is return by <i>d1</i>, the
  402. far intersection distance is return by <i>d2</i>. Guarantee
  403. <b>0</b> <= <i>d1</i> <= <i>d2</i>.
  404. @par
  405. If the ray isn't intersects the box, <b>false</b> is returned, and
  406. <i>d1</i> and <i>d2</i> is unmodified.
  407. */
  408. static bool intersects(const Ray& ray, const AxisAlignedBox& box,
  409. float* d1, float* d2);
  410. /** Ray / triangle intersection, returns boolean result and distance.
  411. @param
  412. ray The ray.
  413. @param
  414. a The triangle's first vertex.
  415. @param
  416. b The triangle's second vertex.
  417. @param
  418. c The triangle's third vertex.
  419. @param
  420. normal The triangle plane's normal (passed in rather than calculated
  421. on demand since the caller may already have it), doesn't need
  422. normalised since we don't care.
  423. @param
  424. positiveSide Intersect with "positive side" of the triangle
  425. @param
  426. negativeSide Intersect with "negative side" of the triangle
  427. @returns
  428. If the ray is intersects the triangle, a pair of <b>true</b> and the
  429. distance between intersection point and ray origin returned.
  430. @par
  431. If the ray isn't intersects the triangle, a pair of <b>false</b> and
  432. <b>0</b> returned.
  433. */
  434. static std::pair<bool, float> intersects(const Ray& ray, const Vector3& a,
  435. const Vector3& b, const Vector3& c, const Vector3& normal,
  436. bool positiveSide = true, bool negativeSide = true);
  437. /** Ray / triangle intersection, returns boolean result and distance.
  438. @param
  439. ray The ray.
  440. @param
  441. a The triangle's first vertex.
  442. @param
  443. b The triangle's second vertex.
  444. @param
  445. c The triangle's third vertex.
  446. @param
  447. positiveSide Intersect with "positive side" of the triangle
  448. @param
  449. negativeSide Intersect with "negative side" of the triangle
  450. @returns
  451. If the ray is intersects the triangle, a pair of <b>true</b> and the
  452. distance between intersection point and ray origin returned.
  453. @par
  454. If the ray isn't intersects the triangle, a pair of <b>false</b> and
  455. <b>0</b> returned.
  456. */
  457. static std::pair<bool, float> intersects(const Ray& ray, const Vector3& a,
  458. const Vector3& b, const Vector3& c,
  459. bool positiveSide = true, bool negativeSide = true);
  460. /** Sphere / box intersection test. */
  461. static bool intersects(const Sphere& sphere, const AxisAlignedBox& box);
  462. /** Plane / box intersection test. */
  463. static bool intersects(const Plane& plane, const AxisAlignedBox& box);
  464. /** Ray / convex plane list intersection test.
  465. @param ray The ray to test with
  466. @param plaeList List of planes which form a convex volume
  467. @param normalIsOutside Does the normal point outside the volume
  468. */
  469. static std::pair<bool, float> intersects(
  470. const Ray& ray, const vector<Plane>::type& planeList,
  471. bool normalIsOutside);
  472. /** Ray / convex plane list intersection test.
  473. @param ray The ray to test with
  474. @param plaeList List of planes which form a convex volume
  475. @param normalIsOutside Does the normal point outside the volume
  476. */
  477. static std::pair<bool, float> intersects(
  478. const Ray& ray, const list<Plane>::type& planeList,
  479. bool normalIsOutside);
  480. /** Sphere / plane intersection test.
  481. @remarks NB just do a plane.getDistance(sphere.getCenter()) for more detail!
  482. */
  483. static bool intersects(const Sphere& sphere, const Plane& plane);
  484. /** Compare 2 reals, using tolerance for inaccuracies.
  485. */
  486. static bool RealEqual(float a, float b,
  487. float tolerance = std::numeric_limits<float>::epsilon());
  488. /** Calculates the tangent space vector for a given set of positions / texture coords. */
  489. static Vector3 calculateTangentSpaceVector(
  490. const Vector3& position1, const Vector3& position2, const Vector3& position3,
  491. float u1, float v1, float u2, float v2, float u3, float v3);
  492. /** Build a reflection matrix for the passed in plane. */
  493. static Matrix4 buildReflectionMatrix(const Plane& p);
  494. /** Calculate a face normal, including the w component which is the offset from the origin. */
  495. static Vector4 calculateFaceNormal(const Vector3& v1, const Vector3& v2, const Vector3& v3);
  496. /** Calculate a face normal, no w-information. */
  497. static Vector3 calculateBasicFaceNormal(const Vector3& v1, const Vector3& v2, const Vector3& v3);
  498. /** Calculate a face normal without normalize, including the w component which is the offset from the origin. */
  499. static Vector4 calculateFaceNormalWithoutNormalize(const Vector3& v1, const Vector3& v2, const Vector3& v3);
  500. /** Calculate a face normal without normalize, no w-information. */
  501. static Vector3 calculateBasicFaceNormalWithoutNormalize(const Vector3& v1, const Vector3& v2, const Vector3& v3);
  502. /** Generates a value based on the Gaussian (normal) distribution function
  503. with the given offset and scale parameters.
  504. */
  505. static float gaussianDistribution(float x, float offset = 0.0f, float scale = 1.0f);
  506. /** Clamp a value within an inclusive range. */
  507. template <typename T>
  508. static T Clamp(T val, T minval, T maxval)
  509. {
  510. assert (minval <= maxval && "Invalid clamp range");
  511. return std::max(std::min(val, maxval), minval);
  512. }
  513. static Matrix4 makeViewMatrix(const Vector3& position, const Quaternion& orientation,
  514. const Matrix4* reflectMatrix = 0);
  515. /** Get a bounding radius value from a bounding box. */
  516. static float boundingRadiusFromAABB(const AxisAlignedBox& aabb);
  517. static const float POS_INFINITY;
  518. static const float NEG_INFINITY;
  519. static const float PI;
  520. static const float TWO_PI;
  521. static const float HALF_PI;
  522. static const float fDeg2Rad;
  523. static const float fRad2Deg;
  524. };
  525. // these functions must be defined down here, because they rely on the
  526. // angle unit conversion functions in class Math:
  527. inline float Radian::valueDegrees() const
  528. {
  529. return Math::RadiansToDegrees ( mRad );
  530. }
  531. inline float Radian::valueAngleUnits() const
  532. {
  533. return Math::RadiansToAngleUnits ( mRad );
  534. }
  535. inline float Degree::valueRadians() const
  536. {
  537. return Math::DegreesToRadians ( mDeg );
  538. }
  539. inline float Degree::valueAngleUnits() const
  540. {
  541. return Math::DegreesToAngleUnits ( mDeg );
  542. }
  543. inline Angle::operator Radian() const
  544. {
  545. return Radian(Math::AngleUnitsToRadians(mAngle));
  546. }
  547. inline Angle::operator Degree() const
  548. {
  549. return Degree(Math::AngleUnitsToDegrees(mAngle));
  550. }
  551. inline Radian operator * ( float a, const Radian& b )
  552. {
  553. return Radian ( a * b.valueRadians() );
  554. }
  555. inline Radian operator / ( float a, const Radian& b )
  556. {
  557. return Radian ( a / b.valueRadians() );
  558. }
  559. inline Degree operator * ( float a, const Degree& b )
  560. {
  561. return Degree ( a * b.valueDegrees() );
  562. }
  563. inline Degree operator / ( float a, const Degree& b )
  564. {
  565. return Degree ( a / b.valueDegrees() );
  566. }
  567. /** @} */
  568. /** @} */
  569. }
  570. #endif