[148] | 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 | |
---|
| 7 | Copyright (c) 2000-2013 Torus Knot Software Ltd |
---|
| 8 | |
---|
| 9 | Permission is hereby granted, free of charge, to any person obtaining a copy |
---|
| 10 | of this software and associated documentation files (the "Software"), to deal |
---|
| 11 | in the Software without restriction, including without limitation the rights |
---|
| 12 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
---|
| 13 | copies of the Software, and to permit persons to whom the Software is |
---|
| 14 | furnished to do so, subject to the following conditions: |
---|
| 15 | |
---|
| 16 | The above copyright notice and this permission notice shall be included in |
---|
| 17 | all copies or substantial portions of the Software. |
---|
| 18 | |
---|
| 19 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
---|
| 20 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
---|
| 21 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
---|
| 22 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
---|
| 23 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
---|
| 24 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
---|
| 25 | THE SOFTWARE. |
---|
| 26 | ----------------------------------------------------------------------------- |
---|
| 27 | */ |
---|
| 28 | #ifndef __Math_H__ |
---|
| 29 | #define __Math_H__ |
---|
| 30 | |
---|
| 31 | #include "OgrePrerequisites.h" |
---|
| 32 | #include "OgreHeaderPrefix.h" |
---|
| 33 | |
---|
| 34 | namespace Ogre |
---|
| 35 | { |
---|
| 36 | /** \addtogroup Core |
---|
| 37 | * @{ |
---|
| 38 | */ |
---|
| 39 | /** \addtogroup Math |
---|
| 40 | * @{ |
---|
| 41 | */ |
---|
| 42 | /** Wrapper class which indicates a given angle value is in Radians. |
---|
| 43 | @remarks |
---|
| 44 | Radian values are interchangeable with Degree values, and conversions |
---|
| 45 | will be done automatically between them. |
---|
| 46 | */ |
---|
| 47 | class Radian |
---|
| 48 | { |
---|
| 49 | Real mRad; |
---|
| 50 | |
---|
| 51 | public: |
---|
| 52 | explicit Radian ( Real r=0 ) : mRad(r) {} |
---|
| 53 | Radian ( const Degree& d ); |
---|
| 54 | Radian& operator = ( const Real& f ) { mRad = f; return *this; } |
---|
| 55 | Radian& operator = ( const Radian& r ) { mRad = r.mRad; return *this; } |
---|
| 56 | Radian& operator = ( const Degree& d ); |
---|
| 57 | |
---|
| 58 | Real valueDegrees() const; // see bottom of this file |
---|
| 59 | Real valueRadians() const { return mRad; } |
---|
| 60 | Real valueAngleUnits() const; |
---|
| 61 | |
---|
| 62 | const Radian& operator + () const { return *this; } |
---|
| 63 | Radian operator + ( const Radian& r ) const { return Radian ( mRad + r.mRad ); } |
---|
| 64 | Radian operator + ( const Degree& d ) const; |
---|
| 65 | Radian& operator += ( const Radian& r ) { mRad += r.mRad; return *this; } |
---|
| 66 | Radian& operator += ( const Degree& d ); |
---|
| 67 | Radian operator - () const { return Radian(-mRad); } |
---|
| 68 | Radian operator - ( const Radian& r ) const { return Radian ( mRad - r.mRad ); } |
---|
| 69 | Radian operator - ( const Degree& d ) const; |
---|
| 70 | Radian& operator -= ( const Radian& r ) { mRad -= r.mRad; return *this; } |
---|
| 71 | Radian& operator -= ( const Degree& d ); |
---|
| 72 | Radian operator * ( Real f ) const { return Radian ( mRad * f ); } |
---|
| 73 | Radian operator * ( const Radian& f ) const { return Radian ( mRad * f.mRad ); } |
---|
| 74 | Radian& operator *= ( Real f ) { mRad *= f; return *this; } |
---|
| 75 | Radian operator / ( Real f ) const { return Radian ( mRad / f ); } |
---|
| 76 | Radian& operator /= ( Real f ) { mRad /= f; return *this; } |
---|
| 77 | |
---|
| 78 | bool operator < ( const Radian& r ) const { return mRad < r.mRad; } |
---|
| 79 | bool operator <= ( const Radian& r ) const { return mRad <= r.mRad; } |
---|
| 80 | bool operator == ( const Radian& r ) const { return mRad == r.mRad; } |
---|
| 81 | bool operator != ( const Radian& r ) const { return mRad != r.mRad; } |
---|
| 82 | bool operator >= ( const Radian& r ) const { return mRad >= r.mRad; } |
---|
| 83 | bool operator > ( const Radian& r ) const { return mRad > r.mRad; } |
---|
| 84 | |
---|
| 85 | inline _OgreExport friend std::ostream& operator << |
---|
| 86 | ( std::ostream& o, const Radian& v ) |
---|
| 87 | { |
---|
| 88 | o << "Radian(" << v.valueRadians() << ")"; |
---|
| 89 | return o; |
---|
| 90 | } |
---|
| 91 | }; |
---|
| 92 | |
---|
| 93 | /** Wrapper class which indicates a given angle value is in Degrees. |
---|
| 94 | @remarks |
---|
| 95 | Degree values are interchangeable with Radian values, and conversions |
---|
| 96 | will be done automatically between them. |
---|
| 97 | */ |
---|
| 98 | class Degree |
---|
| 99 | { |
---|
| 100 | Real mDeg; // if you get an error here - make sure to define/typedef 'Real' first |
---|
| 101 | |
---|
| 102 | public: |
---|
| 103 | explicit Degree ( Real d=0 ) : mDeg(d) {} |
---|
| 104 | Degree ( const Radian& r ) : mDeg(r.valueDegrees()) {} |
---|
| 105 | Degree& operator = ( const Real& f ) { mDeg = f; return *this; } |
---|
| 106 | Degree& operator = ( const Degree& d ) { mDeg = d.mDeg; return *this; } |
---|
| 107 | Degree& operator = ( const Radian& r ) { mDeg = r.valueDegrees(); return *this; } |
---|
| 108 | |
---|
| 109 | Real valueDegrees() const { return mDeg; } |
---|
| 110 | Real valueRadians() const; // see bottom of this file |
---|
| 111 | Real valueAngleUnits() const; |
---|
| 112 | |
---|
| 113 | const Degree& operator + () const { return *this; } |
---|
| 114 | Degree operator + ( const Degree& d ) const { return Degree ( mDeg + d.mDeg ); } |
---|
| 115 | Degree operator + ( const Radian& r ) const { return Degree ( mDeg + r.valueDegrees() ); } |
---|
| 116 | Degree& operator += ( const Degree& d ) { mDeg += d.mDeg; return *this; } |
---|
| 117 | Degree& operator += ( const Radian& r ) { mDeg += r.valueDegrees(); return *this; } |
---|
| 118 | Degree operator - () const { return Degree(-mDeg); } |
---|
| 119 | Degree operator - ( const Degree& d ) const { return Degree ( mDeg - d.mDeg ); } |
---|
| 120 | Degree operator - ( const Radian& r ) const { return Degree ( mDeg - r.valueDegrees() ); } |
---|
| 121 | Degree& operator -= ( const Degree& d ) { mDeg -= d.mDeg; return *this; } |
---|
| 122 | Degree& operator -= ( const Radian& r ) { mDeg -= r.valueDegrees(); return *this; } |
---|
| 123 | Degree operator * ( Real f ) const { return Degree ( mDeg * f ); } |
---|
| 124 | Degree operator * ( const Degree& f ) const { return Degree ( mDeg * f.mDeg ); } |
---|
| 125 | Degree& operator *= ( Real f ) { mDeg *= f; return *this; } |
---|
| 126 | Degree operator / ( Real f ) const { return Degree ( mDeg / f ); } |
---|
| 127 | Degree& operator /= ( Real f ) { mDeg /= f; return *this; } |
---|
| 128 | |
---|
| 129 | bool operator < ( const Degree& d ) const { return mDeg < d.mDeg; } |
---|
| 130 | bool operator <= ( const Degree& d ) const { return mDeg <= d.mDeg; } |
---|
| 131 | bool operator == ( const Degree& d ) const { return mDeg == d.mDeg; } |
---|
| 132 | bool operator != ( const Degree& d ) const { return mDeg != d.mDeg; } |
---|
| 133 | bool operator >= ( const Degree& d ) const { return mDeg >= d.mDeg; } |
---|
| 134 | bool operator > ( const Degree& d ) const { return mDeg > d.mDeg; } |
---|
| 135 | |
---|
| 136 | inline _OgreExport friend std::ostream& operator << |
---|
| 137 | ( std::ostream& o, const Degree& v ) |
---|
| 138 | { |
---|
| 139 | o << "Degree(" << v.valueDegrees() << ")"; |
---|
| 140 | return o; |
---|
| 141 | } |
---|
| 142 | }; |
---|
| 143 | |
---|
| 144 | /** Wrapper class which identifies a value as the currently default angle |
---|
| 145 | type, as defined by Math::setAngleUnit. |
---|
| 146 | @remarks |
---|
| 147 | Angle values will be automatically converted between radians and degrees, |
---|
| 148 | as appropriate. |
---|
| 149 | */ |
---|
| 150 | class Angle |
---|
| 151 | { |
---|
| 152 | Real mAngle; |
---|
| 153 | public: |
---|
| 154 | explicit Angle ( Real angle ) : mAngle(angle) {} |
---|
| 155 | operator Radian() const; |
---|
| 156 | operator Degree() const; |
---|
| 157 | }; |
---|
| 158 | |
---|
| 159 | // these functions could not be defined within the class definition of class |
---|
| 160 | // Radian because they required class Degree to be defined |
---|
| 161 | inline Radian::Radian ( const Degree& d ) : mRad(d.valueRadians()) { |
---|
| 162 | } |
---|
| 163 | inline Radian& Radian::operator = ( const Degree& d ) { |
---|
| 164 | mRad = d.valueRadians(); return *this; |
---|
| 165 | } |
---|
| 166 | inline Radian Radian::operator + ( const Degree& d ) const { |
---|
| 167 | return Radian ( mRad + d.valueRadians() ); |
---|
| 168 | } |
---|
| 169 | inline Radian& Radian::operator += ( const Degree& d ) { |
---|
| 170 | mRad += d.valueRadians(); |
---|
| 171 | return *this; |
---|
| 172 | } |
---|
| 173 | inline Radian Radian::operator - ( const Degree& d ) const { |
---|
| 174 | return Radian ( mRad - d.valueRadians() ); |
---|
| 175 | } |
---|
| 176 | inline Radian& Radian::operator -= ( const Degree& d ) { |
---|
| 177 | mRad -= d.valueRadians(); |
---|
| 178 | return *this; |
---|
| 179 | } |
---|
| 180 | |
---|
| 181 | /** Class to provide access to common mathematical functions. |
---|
| 182 | @remarks |
---|
| 183 | Most of the maths functions are aliased versions of the C runtime |
---|
| 184 | library functions. They are aliased here to provide future |
---|
| 185 | optimisation opportunities, either from faster RTLs or custom |
---|
| 186 | math approximations. |
---|
| 187 | @note |
---|
| 188 | <br>This is based on MgcMath.h from |
---|
| 189 | <a href="http://www.geometrictools.com/">Wild Magic</a>. |
---|
| 190 | */ |
---|
| 191 | class _OgreExport Math |
---|
| 192 | { |
---|
| 193 | public: |
---|
| 194 | /** The angular units used by the API. This functionality is now deprecated in favor |
---|
| 195 | of discreet angular unit types ( see Degree and Radian above ). The only place |
---|
| 196 | this functionality is actually still used is when parsing files. Search for |
---|
| 197 | usage of the Angle class for those instances |
---|
| 198 | */ |
---|
| 199 | enum AngleUnit |
---|
| 200 | { |
---|
| 201 | AU_DEGREE, |
---|
| 202 | AU_RADIAN |
---|
| 203 | }; |
---|
| 204 | |
---|
| 205 | |
---|
| 206 | /** This class is used to provide an external random value provider. |
---|
| 207 | */ |
---|
| 208 | class RandomValueProvider |
---|
| 209 | { |
---|
| 210 | public: |
---|
| 211 | virtual ~RandomValueProvider() {} |
---|
| 212 | /** When called should return a random values in the range of [0,1] */ |
---|
| 213 | virtual Real getRandomUnit() = 0; |
---|
| 214 | }; |
---|
| 215 | |
---|
| 216 | protected: |
---|
| 217 | /// Angle units used by the api |
---|
| 218 | static AngleUnit msAngleUnit; |
---|
| 219 | |
---|
| 220 | /// Size of the trig tables as determined by constructor. |
---|
| 221 | static int mTrigTableSize; |
---|
| 222 | |
---|
| 223 | /// Radian -> index factor value ( mTrigTableSize / 2 * PI ) |
---|
| 224 | static Real mTrigTableFactor; |
---|
| 225 | static Real* mSinTable; |
---|
| 226 | static Real* mTanTable; |
---|
| 227 | |
---|
| 228 | /// A random value provider. overriding the default random number generator. |
---|
| 229 | static RandomValueProvider* mRandProvider; |
---|
| 230 | |
---|
| 231 | /** Private function to build trig tables. |
---|
| 232 | */ |
---|
| 233 | void buildTrigTables(); |
---|
| 234 | |
---|
| 235 | static Real SinTable (Real fValue); |
---|
| 236 | static Real TanTable (Real fValue); |
---|
| 237 | public: |
---|
| 238 | /** Default constructor. |
---|
| 239 | @param |
---|
| 240 | trigTableSize Optional parameter to set the size of the |
---|
| 241 | tables used to implement Sin, Cos, Tan |
---|
| 242 | */ |
---|
| 243 | Math(unsigned int trigTableSize = 4096); |
---|
| 244 | |
---|
| 245 | /** Default destructor. |
---|
| 246 | */ |
---|
| 247 | ~Math(); |
---|
| 248 | |
---|
| 249 | static inline int IAbs (int iValue) { return ( iValue >= 0 ? iValue : -iValue ); } |
---|
| 250 | static inline int ICeil (float fValue) { return int(ceil(fValue)); } |
---|
| 251 | static inline int IFloor (float fValue) { return int(floor(fValue)); } |
---|
| 252 | static int ISign (int iValue); |
---|
| 253 | |
---|
| 254 | /** Absolute value function |
---|
| 255 | @param |
---|
| 256 | fValue The value whose absolute value will be returned. |
---|
| 257 | */ |
---|
| 258 | static inline Real Abs (Real fValue) { return Real(fabs(fValue)); } |
---|
| 259 | |
---|
| 260 | /** Absolute value function |
---|
| 261 | @param dValue |
---|
| 262 | The value, in degrees, whose absolute value will be returned. |
---|
| 263 | */ |
---|
| 264 | static inline Degree Abs (const Degree& dValue) { return Degree(fabs(dValue.valueDegrees())); } |
---|
| 265 | |
---|
| 266 | /** Absolute value function |
---|
| 267 | @param rValue |
---|
| 268 | The value, in radians, whose absolute value will be returned. |
---|
| 269 | */ |
---|
| 270 | static inline Radian Abs (const Radian& rValue) { return Radian(fabs(rValue.valueRadians())); } |
---|
| 271 | |
---|
| 272 | /** Arc cosine function |
---|
| 273 | @param fValue |
---|
| 274 | The value whose arc cosine will be returned. |
---|
| 275 | */ |
---|
| 276 | static Radian ACos (Real fValue); |
---|
| 277 | |
---|
| 278 | /** Arc sine function |
---|
| 279 | @param fValue |
---|
| 280 | The value whose arc sine will be returned. |
---|
| 281 | */ |
---|
| 282 | static Radian ASin (Real fValue); |
---|
| 283 | |
---|
| 284 | /** Arc tangent function |
---|
| 285 | @param fValue |
---|
| 286 | The value whose arc tangent will be returned. |
---|
| 287 | */ |
---|
| 288 | static inline Radian ATan (Real fValue) { return Radian(atan(fValue)); } |
---|
| 289 | |
---|
| 290 | /** Arc tangent between two values function |
---|
| 291 | @param fY |
---|
| 292 | The first value to calculate the arc tangent with. |
---|
| 293 | @param fX |
---|
| 294 | The second value to calculate the arc tangent with. |
---|
| 295 | */ |
---|
| 296 | static inline Radian ATan2 (Real fY, Real fX) { return Radian(atan2(fY,fX)); } |
---|
| 297 | |
---|
| 298 | /** Ceiling function |
---|
| 299 | Returns the smallest following integer. (example: Ceil(1.1) = 2) |
---|
| 300 | |
---|
| 301 | @param fValue |
---|
| 302 | The value to round up to the nearest integer. |
---|
| 303 | */ |
---|
| 304 | static inline Real Ceil (Real fValue) { return Real(ceil(fValue)); } |
---|
| 305 | static inline bool isNaN(Real f) |
---|
| 306 | { |
---|
| 307 | // std::isnan() is C99, not supported by all compilers |
---|
| 308 | // However NaN always fails this next test, no other number does. |
---|
| 309 | return f != f; |
---|
| 310 | } |
---|
| 311 | |
---|
| 312 | /** Cosine function. |
---|
| 313 | @param fValue |
---|
| 314 | Angle in radians |
---|
| 315 | @param useTables |
---|
| 316 | If true, uses lookup tables rather than |
---|
| 317 | calculation - faster but less accurate. |
---|
| 318 | */ |
---|
| 319 | static inline Real Cos (const Radian& fValue, bool useTables = false) { |
---|
| 320 | return (!useTables) ? Real(cos(fValue.valueRadians())) : SinTable(fValue.valueRadians() + HALF_PI); |
---|
| 321 | } |
---|
| 322 | /** Cosine function. |
---|
| 323 | @param fValue |
---|
| 324 | Angle in radians |
---|
| 325 | @param useTables |
---|
| 326 | If true, uses lookup tables rather than |
---|
| 327 | calculation - faster but less accurate. |
---|
| 328 | */ |
---|
| 329 | static inline Real Cos (Real fValue, bool useTables = false) { |
---|
| 330 | return (!useTables) ? Real(cos(fValue)) : SinTable(fValue + HALF_PI); |
---|
| 331 | } |
---|
| 332 | |
---|
| 333 | static inline Real Exp (Real fValue) { return Real(exp(fValue)); } |
---|
| 334 | |
---|
| 335 | /** Floor function |
---|
| 336 | Returns the largest previous integer. (example: Floor(1.9) = 1) |
---|
| 337 | |
---|
| 338 | @param fValue |
---|
| 339 | The value to round down to the nearest integer. |
---|
| 340 | */ |
---|
| 341 | static inline Real Floor (Real fValue) { return Real(floor(fValue)); } |
---|
| 342 | |
---|
| 343 | static inline Real Log (Real fValue) { return Real(log(fValue)); } |
---|
| 344 | |
---|
| 345 | /// Stored value of log(2) for frequent use |
---|
| 346 | static const Real LOG2; |
---|
| 347 | |
---|
| 348 | static inline Real Log2 (Real fValue) { return Real(log(fValue)/LOG2); } |
---|
| 349 | |
---|
| 350 | static inline Real LogN (Real base, Real fValue) { return Real(log(fValue)/log(base)); } |
---|
| 351 | |
---|
| 352 | static inline Real Pow (Real fBase, Real fExponent) { return Real(pow(fBase,fExponent)); } |
---|
| 353 | |
---|
| 354 | static Real Sign (Real fValue); |
---|
| 355 | static inline Radian Sign ( const Radian& rValue ) |
---|
| 356 | { |
---|
| 357 | return Radian(Sign(rValue.valueRadians())); |
---|
| 358 | } |
---|
| 359 | static inline Degree Sign ( const Degree& dValue ) |
---|
| 360 | { |
---|
| 361 | return Degree(Sign(dValue.valueDegrees())); |
---|
| 362 | } |
---|
| 363 | |
---|
| 364 | //Simulate the shader function saturate that clamps a parameter value between 0 and 1 |
---|
| 365 | static inline float saturate(float t) { return (t < 0) ? 0 : ((t > 1) ? 1 : t); } |
---|
| 366 | static inline double saturate(double t) { return (t < 0) ? 0 : ((t > 1) ? 1 : t); } |
---|
| 367 | |
---|
| 368 | //Simulate the shader function lerp which performers linear interpolation |
---|
| 369 | //given 3 parameters v0, v1 and t the function returns the value of (1 t)* v0 + t * v1. |
---|
| 370 | //where v0 and v1 are matching vector or scalar types and t can be either a scalar or a vector of the same type as a and b. |
---|
| 371 | template<typename V, typename T> static V lerp(const V& v0, const V& v1, const T& t) { |
---|
| 372 | return v0 * (1 - t) + v1 * t; } |
---|
| 373 | |
---|
| 374 | /** Sine function. |
---|
| 375 | @param fValue |
---|
| 376 | Angle in radians |
---|
| 377 | @param useTables |
---|
| 378 | If true, uses lookup tables rather than |
---|
| 379 | calculation - faster but less accurate. |
---|
| 380 | */ |
---|
| 381 | static inline Real Sin (const Radian& fValue, bool useTables = false) { |
---|
| 382 | return (!useTables) ? Real(sin(fValue.valueRadians())) : SinTable(fValue.valueRadians()); |
---|
| 383 | } |
---|
| 384 | /** Sine function. |
---|
| 385 | @param fValue |
---|
| 386 | Angle in radians |
---|
| 387 | @param useTables |
---|
| 388 | If true, uses lookup tables rather than |
---|
| 389 | calculation - faster but less accurate. |
---|
| 390 | */ |
---|
| 391 | static inline Real Sin (Real fValue, bool useTables = false) { |
---|
| 392 | return (!useTables) ? Real(sin(fValue)) : SinTable(fValue); |
---|
| 393 | } |
---|
| 394 | |
---|
| 395 | /** Squared function. |
---|
| 396 | @param fValue |
---|
| 397 | The value to be squared (fValue^2) |
---|
| 398 | */ |
---|
| 399 | static inline Real Sqr (Real fValue) { return fValue*fValue; } |
---|
| 400 | |
---|
| 401 | /** Square root function. |
---|
| 402 | @param fValue |
---|
| 403 | The value whose square root will be calculated. |
---|
| 404 | */ |
---|
| 405 | static inline Real Sqrt (Real fValue) { return Real(sqrt(fValue)); } |
---|
| 406 | |
---|
| 407 | /** Square root function. |
---|
| 408 | @param fValue |
---|
| 409 | The value, in radians, whose square root will be calculated. |
---|
| 410 | @return |
---|
| 411 | The square root of the angle in radians. |
---|
| 412 | */ |
---|
| 413 | static inline Radian Sqrt (const Radian& fValue) { return Radian(sqrt(fValue.valueRadians())); } |
---|
| 414 | |
---|
| 415 | /** Square root function. |
---|
| 416 | @param fValue |
---|
| 417 | The value, in degrees, whose square root will be calculated. |
---|
| 418 | @return |
---|
| 419 | The square root of the angle in degrees. |
---|
| 420 | */ |
---|
| 421 | static inline Degree Sqrt (const Degree& fValue) { return Degree(sqrt(fValue.valueDegrees())); } |
---|
| 422 | |
---|
| 423 | /** Inverse square root i.e. 1 / Sqrt(x), good for vector |
---|
| 424 | normalisation. |
---|
| 425 | @param fValue |
---|
| 426 | The value whose inverse square root will be calculated. |
---|
| 427 | */ |
---|
| 428 | static Real InvSqrt (Real fValue); |
---|
| 429 | |
---|
| 430 | /** Generate a random number of unit length. |
---|
| 431 | @return |
---|
| 432 | A random number in the range from [0,1]. |
---|
| 433 | */ |
---|
| 434 | static Real UnitRandom (); |
---|
| 435 | |
---|
| 436 | /** Generate a random number within the range provided. |
---|
| 437 | @param fLow |
---|
| 438 | The lower bound of the range. |
---|
| 439 | @param fHigh |
---|
| 440 | The upper bound of the range. |
---|
| 441 | @return |
---|
| 442 | A random number in the range from [fLow,fHigh]. |
---|
| 443 | */ |
---|
| 444 | static Real RangeRandom (Real fLow, Real fHigh); |
---|
| 445 | |
---|
| 446 | /** Generate a random number in the range [-1,1]. |
---|
| 447 | @return |
---|
| 448 | A random number in the range from [-1,1]. |
---|
| 449 | */ |
---|
| 450 | static Real SymmetricRandom (); |
---|
| 451 | |
---|
| 452 | static void SetRandomValueProvider(RandomValueProvider* provider); |
---|
| 453 | |
---|
| 454 | /** Tangent function. |
---|
| 455 | @param fValue |
---|
| 456 | Angle in radians |
---|
| 457 | @param useTables |
---|
| 458 | If true, uses lookup tables rather than |
---|
| 459 | calculation - faster but less accurate. |
---|
| 460 | */ |
---|
| 461 | static inline Real Tan (const Radian& fValue, bool useTables = false) { |
---|
| 462 | return (!useTables) ? Real(tan(fValue.valueRadians())) : TanTable(fValue.valueRadians()); |
---|
| 463 | } |
---|
| 464 | /** Tangent function. |
---|
| 465 | @param fValue |
---|
| 466 | Angle in radians |
---|
| 467 | @param useTables |
---|
| 468 | If true, uses lookup tables rather than |
---|
| 469 | calculation - faster but less accurate. |
---|
| 470 | */ |
---|
| 471 | static inline Real Tan (Real fValue, bool useTables = false) { |
---|
| 472 | return (!useTables) ? Real(tan(fValue)) : TanTable(fValue); |
---|
| 473 | } |
---|
| 474 | |
---|
| 475 | static inline Real DegreesToRadians(Real degrees) { return degrees * fDeg2Rad; } |
---|
| 476 | static inline Real RadiansToDegrees(Real radians) { return radians * fRad2Deg; } |
---|
| 477 | |
---|
| 478 | /** These functions used to set the assumed angle units (radians or degrees) |
---|
| 479 | expected when using the Angle type. |
---|
| 480 | @par |
---|
| 481 | You can set this directly after creating a new Root, and also before/after resource creation, |
---|
| 482 | depending on whether you want the change to affect resource files. |
---|
| 483 | */ |
---|
| 484 | static void setAngleUnit(AngleUnit unit); |
---|
| 485 | /** Get the unit being used for angles. */ |
---|
| 486 | static AngleUnit getAngleUnit(void); |
---|
| 487 | |
---|
| 488 | /** Convert from the current AngleUnit to radians. */ |
---|
| 489 | static Real AngleUnitsToRadians(Real units); |
---|
| 490 | /** Convert from radians to the current AngleUnit . */ |
---|
| 491 | static Real RadiansToAngleUnits(Real radians); |
---|
| 492 | /** Convert from the current AngleUnit to degrees. */ |
---|
| 493 | static Real AngleUnitsToDegrees(Real units); |
---|
| 494 | /** Convert from degrees to the current AngleUnit. */ |
---|
| 495 | static Real DegreesToAngleUnits(Real degrees); |
---|
| 496 | |
---|
| 497 | /** Checks whether a given point is inside a triangle, in a |
---|
| 498 | 2-dimensional (Cartesian) space. |
---|
| 499 | @remarks |
---|
| 500 | The vertices of the triangle must be given in either |
---|
| 501 | trigonometrical (anticlockwise) or inverse trigonometrical |
---|
| 502 | (clockwise) order. |
---|
| 503 | @param p |
---|
| 504 | The point. |
---|
| 505 | @param a |
---|
| 506 | The triangle's first vertex. |
---|
| 507 | @param b |
---|
| 508 | The triangle's second vertex. |
---|
| 509 | @param c |
---|
| 510 | The triangle's third vertex. |
---|
| 511 | @return |
---|
| 512 | If the point resides in the triangle, <b>true</b> is |
---|
| 513 | returned. |
---|
| 514 | @par |
---|
| 515 | If the point is outside the triangle, <b>false</b> is |
---|
| 516 | returned. |
---|
| 517 | */ |
---|
| 518 | static bool pointInTri2D(const Vector2& p, const Vector2& a, |
---|
| 519 | const Vector2& b, const Vector2& c); |
---|
| 520 | |
---|
| 521 | /** Checks whether a given 3D point is inside a triangle. |
---|
| 522 | @remarks |
---|
| 523 | The vertices of the triangle must be given in either |
---|
| 524 | trigonometrical (anticlockwise) or inverse trigonometrical |
---|
| 525 | (clockwise) order, and the point must be guaranteed to be in the |
---|
| 526 | same plane as the triangle |
---|
| 527 | @param p |
---|
| 528 | p The point. |
---|
| 529 | @param a |
---|
| 530 | The triangle's first vertex. |
---|
| 531 | @param b |
---|
| 532 | The triangle's second vertex. |
---|
| 533 | @param c |
---|
| 534 | The triangle's third vertex. |
---|
| 535 | @param normal |
---|
| 536 | The triangle plane's normal (passed in rather than calculated |
---|
| 537 | on demand since the caller may already have it) |
---|
| 538 | @return |
---|
| 539 | If the point resides in the triangle, <b>true</b> is |
---|
| 540 | returned. |
---|
| 541 | @par |
---|
| 542 | If the point is outside the triangle, <b>false</b> is |
---|
| 543 | returned. |
---|
| 544 | */ |
---|
| 545 | static bool pointInTri3D(const Vector3& p, const Vector3& a, |
---|
| 546 | const Vector3& b, const Vector3& c, const Vector3& normal); |
---|
| 547 | /** Ray / plane intersection, returns boolean result and distance. */ |
---|
| 548 | static std::pair<bool, Real> intersects(const Ray& ray, const Plane& plane); |
---|
| 549 | |
---|
| 550 | /** Ray / sphere intersection, returns boolean result and distance. */ |
---|
| 551 | static std::pair<bool, Real> intersects(const Ray& ray, const Sphere& sphere, |
---|
| 552 | bool discardInside = true); |
---|
| 553 | |
---|
| 554 | /** Ray / box intersection, returns boolean result and distance. */ |
---|
| 555 | static std::pair<bool, Real> intersects(const Ray& ray, const AxisAlignedBox& box); |
---|
| 556 | |
---|
| 557 | /** Ray / box intersection, returns boolean result and two intersection distance. |
---|
| 558 | @param ray |
---|
| 559 | The ray. |
---|
| 560 | @param box |
---|
| 561 | The box. |
---|
| 562 | @param d1 |
---|
| 563 | A real pointer to retrieve the near intersection distance |
---|
| 564 | from the ray origin, maybe <b>null</b> which means don't care |
---|
| 565 | about the near intersection distance. |
---|
| 566 | @param d2 |
---|
| 567 | A real pointer to retrieve the far intersection distance |
---|
| 568 | from the ray origin, maybe <b>null</b> which means don't care |
---|
| 569 | about the far intersection distance. |
---|
| 570 | @return |
---|
| 571 | If the ray is intersects the box, <b>true</b> is returned, and |
---|
| 572 | the near intersection distance is return by <i>d1</i>, the |
---|
| 573 | far intersection distance is return by <i>d2</i>. Guarantee |
---|
| 574 | <b>0</b> <= <i>d1</i> <= <i>d2</i>. |
---|
| 575 | @par |
---|
| 576 | If the ray isn't intersects the box, <b>false</b> is returned, and |
---|
| 577 | <i>d1</i> and <i>d2</i> is unmodified. |
---|
| 578 | */ |
---|
| 579 | static bool intersects(const Ray& ray, const AxisAlignedBox& box, |
---|
| 580 | Real* d1, Real* d2); |
---|
| 581 | |
---|
| 582 | /** Ray / triangle intersection, returns boolean result and distance. |
---|
| 583 | @param ray |
---|
| 584 | The ray. |
---|
| 585 | @param a |
---|
| 586 | The triangle's first vertex. |
---|
| 587 | @param b |
---|
| 588 | The triangle's second vertex. |
---|
| 589 | @param c |
---|
| 590 | The triangle's third vertex. |
---|
| 591 | @param normal |
---|
| 592 | The triangle plane's normal (passed in rather than calculated |
---|
| 593 | on demand since the caller may already have it), doesn't need |
---|
| 594 | normalised since we don't care. |
---|
| 595 | @param positiveSide |
---|
| 596 | Intersect with "positive side" of the triangle |
---|
| 597 | @param negativeSide |
---|
| 598 | Intersect with "negative side" of the triangle |
---|
| 599 | @return |
---|
| 600 | If the ray is intersects the triangle, a pair of <b>true</b> and the |
---|
| 601 | distance between intersection point and ray origin returned. |
---|
| 602 | @par |
---|
| 603 | If the ray isn't intersects the triangle, a pair of <b>false</b> and |
---|
| 604 | <b>0</b> returned. |
---|
| 605 | */ |
---|
| 606 | static std::pair<bool, Real> intersects(const Ray& ray, const Vector3& a, |
---|
| 607 | const Vector3& b, const Vector3& c, const Vector3& normal, |
---|
| 608 | bool positiveSide = true, bool negativeSide = true); |
---|
| 609 | |
---|
| 610 | /** Ray / triangle intersection, returns boolean result and distance. |
---|
| 611 | @param ray |
---|
| 612 | The ray. |
---|
| 613 | @param a |
---|
| 614 | The triangle's first vertex. |
---|
| 615 | @param b |
---|
| 616 | The triangle's second vertex. |
---|
| 617 | @param c |
---|
| 618 | The triangle's third vertex. |
---|
| 619 | @param positiveSide |
---|
| 620 | Intersect with "positive side" of the triangle |
---|
| 621 | @param negativeSide |
---|
| 622 | Intersect with "negative side" of the triangle |
---|
| 623 | @return |
---|
| 624 | If the ray is intersects the triangle, a pair of <b>true</b> and the |
---|
| 625 | distance between intersection point and ray origin returned. |
---|
| 626 | @par |
---|
| 627 | If the ray isn't intersects the triangle, a pair of <b>false</b> and |
---|
| 628 | <b>0</b> returned. |
---|
| 629 | */ |
---|
| 630 | static std::pair<bool, Real> intersects(const Ray& ray, const Vector3& a, |
---|
| 631 | const Vector3& b, const Vector3& c, |
---|
| 632 | bool positiveSide = true, bool negativeSide = true); |
---|
| 633 | |
---|
| 634 | /** Sphere / box intersection test. */ |
---|
| 635 | static bool intersects(const Sphere& sphere, const AxisAlignedBox& box); |
---|
| 636 | |
---|
| 637 | /** Plane / box intersection test. */ |
---|
| 638 | static bool intersects(const Plane& plane, const AxisAlignedBox& box); |
---|
| 639 | |
---|
| 640 | /** Ray / convex plane list intersection test. |
---|
| 641 | @param ray The ray to test with |
---|
| 642 | @param planeList List of planes which form a convex volume |
---|
| 643 | @param normalIsOutside Does the normal point outside the volume |
---|
| 644 | */ |
---|
| 645 | static std::pair<bool, Real> intersects( |
---|
| 646 | const Ray& ray, const vector<Plane>::type& planeList, |
---|
| 647 | bool normalIsOutside); |
---|
| 648 | /** Ray / convex plane list intersection test. |
---|
| 649 | @param ray The ray to test with |
---|
| 650 | @param planeList List of planes which form a convex volume |
---|
| 651 | @param normalIsOutside Does the normal point outside the volume |
---|
| 652 | */ |
---|
| 653 | static std::pair<bool, Real> intersects( |
---|
| 654 | const Ray& ray, const list<Plane>::type& planeList, |
---|
| 655 | bool normalIsOutside); |
---|
| 656 | |
---|
| 657 | /** Sphere / plane intersection test. |
---|
| 658 | @remarks NB just do a plane.getDistance(sphere.getCenter()) for more detail! |
---|
| 659 | */ |
---|
| 660 | static bool intersects(const Sphere& sphere, const Plane& plane); |
---|
| 661 | |
---|
| 662 | /** Compare 2 reals, using tolerance for inaccuracies. |
---|
| 663 | */ |
---|
| 664 | static bool RealEqual(Real a, Real b, |
---|
| 665 | Real tolerance = std::numeric_limits<Real>::epsilon()); |
---|
| 666 | |
---|
| 667 | /** Calculates the tangent space vector for a given set of positions / texture coords. */ |
---|
| 668 | static Vector3 calculateTangentSpaceVector( |
---|
| 669 | const Vector3& position1, const Vector3& position2, const Vector3& position3, |
---|
| 670 | Real u1, Real v1, Real u2, Real v2, Real u3, Real v3); |
---|
| 671 | |
---|
| 672 | /** Build a reflection matrix for the passed in plane. */ |
---|
| 673 | static Matrix4 buildReflectionMatrix(const Plane& p); |
---|
| 674 | /** Calculate a face normal, including the w component which is the offset from the origin. */ |
---|
| 675 | static Vector4 calculateFaceNormal(const Vector3& v1, const Vector3& v2, const Vector3& v3); |
---|
| 676 | /** Calculate a face normal, no w-information. */ |
---|
| 677 | static Vector3 calculateBasicFaceNormal(const Vector3& v1, const Vector3& v2, const Vector3& v3); |
---|
| 678 | /** Calculate a face normal without normalize, including the w component which is the offset from the origin. */ |
---|
| 679 | static Vector4 calculateFaceNormalWithoutNormalize(const Vector3& v1, const Vector3& v2, const Vector3& v3); |
---|
| 680 | /** Calculate a face normal without normalize, no w-information. */ |
---|
| 681 | static Vector3 calculateBasicFaceNormalWithoutNormalize(const Vector3& v1, const Vector3& v2, const Vector3& v3); |
---|
| 682 | |
---|
| 683 | /** Generates a value based on the Gaussian (normal) distribution function |
---|
| 684 | with the given offset and scale parameters. |
---|
| 685 | */ |
---|
| 686 | static Real gaussianDistribution(Real x, Real offset = 0.0f, Real scale = 1.0f); |
---|
| 687 | |
---|
| 688 | /** Clamp a value within an inclusive range. */ |
---|
| 689 | template <typename T> |
---|
| 690 | static T Clamp(T val, T minval, T maxval) |
---|
| 691 | { |
---|
| 692 | assert (minval <= maxval && "Invalid clamp range"); |
---|
| 693 | return std::max(std::min(val, maxval), minval); |
---|
| 694 | } |
---|
| 695 | |
---|
| 696 | static Matrix4 makeViewMatrix(const Vector3& position, const Quaternion& orientation, |
---|
| 697 | const Matrix4* reflectMatrix = 0); |
---|
| 698 | |
---|
| 699 | /** Get a bounding radius value from a bounding box. */ |
---|
| 700 | static Real boundingRadiusFromAABB(const AxisAlignedBox& aabb); |
---|
| 701 | |
---|
| 702 | |
---|
| 703 | |
---|
| 704 | static const Real POS_INFINITY; |
---|
| 705 | static const Real NEG_INFINITY; |
---|
| 706 | static const Real PI; |
---|
| 707 | static const Real TWO_PI; |
---|
| 708 | static const Real HALF_PI; |
---|
| 709 | static const Real fDeg2Rad; |
---|
| 710 | static const Real fRad2Deg; |
---|
| 711 | |
---|
| 712 | }; |
---|
| 713 | |
---|
| 714 | // these functions must be defined down here, because they rely on the |
---|
| 715 | // angle unit conversion functions in class Math: |
---|
| 716 | |
---|
| 717 | inline Real Radian::valueDegrees() const |
---|
| 718 | { |
---|
| 719 | return Math::RadiansToDegrees ( mRad ); |
---|
| 720 | } |
---|
| 721 | |
---|
| 722 | inline Real Radian::valueAngleUnits() const |
---|
| 723 | { |
---|
| 724 | return Math::RadiansToAngleUnits ( mRad ); |
---|
| 725 | } |
---|
| 726 | |
---|
| 727 | inline Real Degree::valueRadians() const |
---|
| 728 | { |
---|
| 729 | return Math::DegreesToRadians ( mDeg ); |
---|
| 730 | } |
---|
| 731 | |
---|
| 732 | inline Real Degree::valueAngleUnits() const |
---|
| 733 | { |
---|
| 734 | return Math::DegreesToAngleUnits ( mDeg ); |
---|
| 735 | } |
---|
| 736 | |
---|
| 737 | inline Angle::operator Radian() const |
---|
| 738 | { |
---|
| 739 | return Radian(Math::AngleUnitsToRadians(mAngle)); |
---|
| 740 | } |
---|
| 741 | |
---|
| 742 | inline Angle::operator Degree() const |
---|
| 743 | { |
---|
| 744 | return Degree(Math::AngleUnitsToDegrees(mAngle)); |
---|
| 745 | } |
---|
| 746 | |
---|
| 747 | inline Radian operator * ( Real a, const Radian& b ) |
---|
| 748 | { |
---|
| 749 | return Radian ( a * b.valueRadians() ); |
---|
| 750 | } |
---|
| 751 | |
---|
| 752 | inline Radian operator / ( Real a, const Radian& b ) |
---|
| 753 | { |
---|
| 754 | return Radian ( a / b.valueRadians() ); |
---|
| 755 | } |
---|
| 756 | |
---|
| 757 | inline Degree operator * ( Real a, const Degree& b ) |
---|
| 758 | { |
---|
| 759 | return Degree ( a * b.valueDegrees() ); |
---|
| 760 | } |
---|
| 761 | |
---|
| 762 | inline Degree operator / ( Real a, const Degree& b ) |
---|
| 763 | { |
---|
| 764 | return Degree ( a / b.valueDegrees() ); |
---|
| 765 | } |
---|
| 766 | /** @} */ |
---|
| 767 | /** @} */ |
---|
| 768 | |
---|
| 769 | } |
---|
| 770 | |
---|
| 771 | #include "OgreHeaderSuffix.h" |
---|
| 772 | |
---|
| 773 | #endif |
---|