home *** CD-ROM | disk | FTP | other *** search
/ GameStar 2006 March / Gamestar_82_2006-03_dvd.iso / DVDStar / Editace / quake4_sdkv10.exe / source / idlib / math / Extrapolate.h < prev    next >
C/C++ Source or Header  |  2005-11-14  |  7KB  |  215 lines

  1.  
  2. #ifndef __MATH_EXTRAPOLATE_H__
  3. #define __MATH_EXTRAPOLATE_H__
  4.  
  5. /*
  6. ==============================================================================================
  7.  
  8.     Extrapolation
  9.  
  10. ==============================================================================================
  11. */
  12.  
  13. typedef enum {
  14.     EXTRAPOLATION_NONE            = 0x01,    // no extrapolation, covered distance = duration * 0.001 * ( baseSpeed )
  15.     EXTRAPOLATION_LINEAR        = 0x02,    // linear extrapolation, covered distance = duration * 0.001 * ( baseSpeed + speed )
  16.     EXTRAPOLATION_ACCELLINEAR    = 0x04,    // linear acceleration, covered distance = duration * 0.001 * ( baseSpeed + 0.5 * speed )
  17.     EXTRAPOLATION_DECELLINEAR    = 0x08,    // linear deceleration, covered distance = duration * 0.001 * ( baseSpeed + 0.5 * speed )
  18.     EXTRAPOLATION_ACCELSINE        = 0x10,    // sinusoidal acceleration, covered distance = duration * 0.001 * ( baseSpeed + sqrt( 0.5 ) * speed )
  19.     EXTRAPOLATION_DECELSINE        = 0x20,    // sinusoidal deceleration, covered distance = duration * 0.001 * ( baseSpeed + sqrt( 0.5 ) * speed )
  20.     EXTRAPOLATION_NOSTOP        = 0x40    // do not stop at startTime + duration
  21. } extrapolation_t;
  22.  
  23. template< class type >
  24. class idExtrapolate {
  25. public:
  26.                         idExtrapolate();
  27.  
  28.     void                Init( const float startTime, const float duration, const type &startValue, const type &baseSpeed, const type &speed, const extrapolation_t extrapolationType );
  29.     type                GetCurrentValue( float time ) const;
  30.     type                GetCurrentSpeed( float time ) const;
  31.     bool                IsDone( float time ) const { return ( !( extrapolationType & EXTRAPOLATION_NOSTOP ) && time >= startTime + duration ); }
  32.     void                SetStartTime( float time ) { startTime = time; currentTime = -1; }
  33.     float                GetStartTime( void ) const { return startTime; }
  34.     float                GetEndTime( void ) const { return ( !( extrapolationType & EXTRAPOLATION_NOSTOP ) && duration > 0 ) ? startTime + duration : 0; }
  35.     float                GetDuration( void ) const { return duration; }
  36.     void                SetStartValue( const type &value ) { startValue = value; currentTime = -1; }
  37.     const type &        GetStartValue( void ) const { return startValue; }
  38.     const type &        GetBaseSpeed( void ) const { return baseSpeed; }
  39.     const type &        GetSpeed( void ) const { return speed; }
  40.     extrapolation_t        GetExtrapolationType( void ) const { return extrapolationType; }
  41.  
  42. private:
  43.     extrapolation_t        extrapolationType;
  44.     float                startTime;
  45.     float                duration;
  46.     type                startValue;
  47.     type                baseSpeed;
  48.     type                speed;
  49.     mutable float        currentTime;
  50.     mutable type        currentValue;
  51. };
  52.  
  53. /*
  54. ====================
  55. idExtrapolate::idExtrapolate
  56. ====================
  57. */
  58. template< class type >
  59. ID_INLINE idExtrapolate<type>::idExtrapolate() {
  60.     extrapolationType = EXTRAPOLATION_NONE;
  61.     startTime = duration = 0.0f;
  62.     memset( &startValue, 0, sizeof( startValue ) );
  63.     memset( &baseSpeed, 0, sizeof( baseSpeed ) );
  64.     memset( &speed, 0, sizeof( speed ) );
  65.     currentTime = -1;
  66.     currentValue = startValue;
  67. }
  68.  
  69. /*
  70. ====================
  71. idExtrapolate::Init
  72. ====================
  73. */
  74. template< class type >
  75. ID_INLINE void idExtrapolate<type>::Init( const float startTime, const float duration, const type &startValue, const type &baseSpeed, const type &speed, const extrapolation_t extrapolationType ) {
  76.     this->extrapolationType = extrapolationType;
  77.     this->startTime = startTime;
  78.     this->duration = duration;
  79.     this->startValue = startValue;
  80.     this->baseSpeed = baseSpeed;
  81.     this->speed = speed;
  82.     currentTime = -1;
  83.     currentValue = startValue;
  84. }
  85.  
  86. /*
  87. ====================
  88. idExtrapolate::GetCurrentValue
  89. ====================
  90. */
  91. template< class type >
  92. ID_INLINE type idExtrapolate<type>::GetCurrentValue( float time ) const {
  93.     float deltaTime, s;
  94.  
  95.     if ( time == currentTime ) {
  96.         return currentValue;
  97.     }
  98.  
  99.     currentTime = time;
  100.  
  101.     if ( time < startTime ) {
  102.         return startValue;
  103.     }
  104.  
  105.     if ( !( extrapolationType &    EXTRAPOLATION_NOSTOP ) && ( time > startTime + duration ) ) {
  106.         time = startTime + duration;
  107.     }
  108.  
  109.     switch( extrapolationType & ~EXTRAPOLATION_NOSTOP ) {
  110.         case EXTRAPOLATION_NONE: {
  111.             deltaTime = ( time - startTime ) * 0.001f;
  112.             currentValue = startValue + deltaTime * baseSpeed;
  113.             break;
  114.         }
  115.         case EXTRAPOLATION_LINEAR: {
  116.             deltaTime = ( time - startTime ) * 0.001f;
  117.             currentValue = startValue + deltaTime * ( baseSpeed + speed );
  118.             break;
  119.         }
  120.         case EXTRAPOLATION_ACCELLINEAR: {
  121.             if ( !duration ) {
  122.                 currentValue = startValue;
  123.             } else {
  124.                 deltaTime = ( time - startTime ) / duration;
  125.                 s = ( 0.5f * deltaTime * deltaTime ) * ( duration * 0.001f );
  126.                 currentValue = startValue + deltaTime * baseSpeed + s * speed;
  127.             }
  128.             break;
  129.         }
  130.         case EXTRAPOLATION_DECELLINEAR: {
  131.             if ( !duration ) {
  132.                 currentValue = startValue;
  133.             } else {
  134.                 deltaTime = ( time - startTime ) / duration;
  135.                 s = ( deltaTime - ( 0.5f * deltaTime * deltaTime ) ) * ( duration * 0.001f );
  136.                 currentValue = startValue + deltaTime * baseSpeed + s * speed;
  137.             }
  138.             break;
  139.         }
  140.         case EXTRAPOLATION_ACCELSINE: {
  141.             if ( !duration ) {
  142.                 currentValue = startValue;
  143.             } else {
  144.                 deltaTime = ( time - startTime ) / duration;
  145.                 s = ( 1.0f - idMath::Cos( deltaTime * idMath::HALF_PI ) ) * duration * 0.001f * idMath::SQRT_1OVER2;
  146.                 currentValue = startValue + deltaTime * baseSpeed + s * speed;
  147.             }
  148.             break;
  149.         }
  150.         case EXTRAPOLATION_DECELSINE: {
  151.             if ( !duration ) {
  152.                 currentValue = startValue;
  153.             } else {
  154.                 deltaTime = ( time - startTime ) / duration;
  155.                 s = idMath::Sin( deltaTime * idMath::HALF_PI ) * duration * 0.001f * idMath::SQRT_1OVER2;
  156.                 currentValue = startValue + deltaTime * baseSpeed + s * speed;
  157.             }
  158.             break;
  159.         }
  160.     }
  161.     return currentValue;
  162. }
  163.  
  164. /*
  165. ====================
  166. idExtrapolate::GetCurrentSpeed
  167. ====================
  168. */
  169. template< class type >
  170. ID_INLINE type idExtrapolate<type>::GetCurrentSpeed( float time ) const {
  171.     float deltaTime, s;
  172.  
  173.     if ( time < startTime || !duration ) {
  174.         return ( startValue - startValue );
  175.     }
  176.  
  177.     if ( !( extrapolationType &    EXTRAPOLATION_NOSTOP ) && ( time > startTime + duration ) ) {
  178.         return ( startValue - startValue );
  179.     }
  180.  
  181.     switch( extrapolationType & ~EXTRAPOLATION_NOSTOP ) {
  182.         case EXTRAPOLATION_NONE: {
  183.             return baseSpeed;
  184.         }
  185.         case EXTRAPOLATION_LINEAR: {
  186.             return baseSpeed + speed;
  187.         }
  188.         case EXTRAPOLATION_ACCELLINEAR: {
  189.             deltaTime = ( time - startTime ) / duration;
  190.             s = deltaTime;
  191.             return baseSpeed + s * speed;
  192.         }
  193.         case EXTRAPOLATION_DECELLINEAR: {
  194.             deltaTime = ( time - startTime ) / duration;
  195.             s = 1.0f - deltaTime;
  196.             return baseSpeed + s * speed;
  197.         }
  198.         case EXTRAPOLATION_ACCELSINE: {
  199.             deltaTime = ( time - startTime ) / duration;
  200.             s = idMath::Sin( deltaTime * idMath::HALF_PI );
  201.             return baseSpeed + s * speed;
  202.         }
  203.         case EXTRAPOLATION_DECELSINE: {
  204.             deltaTime = ( time - startTime ) / duration;
  205.             s = idMath::Cos( deltaTime * idMath::HALF_PI );
  206.             return baseSpeed + s * speed;
  207.         }
  208.         default: {
  209.             return baseSpeed;
  210.         }
  211.     }
  212. }
  213.  
  214. #endif /* !__MATH_EXTRAPOLATE_H__ */
  215.