Extrapolate.h 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. /*
  2. ===========================================================================
  3. Doom 3 BFG Edition GPL Source Code
  4. Copyright (C) 1993-2012 id Software LLC, a ZeniMax Media company.
  5. This file is part of the Doom 3 BFG Edition GPL Source Code ("Doom 3 BFG Edition Source Code").
  6. Doom 3 BFG Edition Source Code is free software: you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation, either version 3 of the License, or
  9. (at your option) any later version.
  10. Doom 3 BFG Edition Source Code is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with Doom 3 BFG Edition Source Code. If not, see <http://www.gnu.org/licenses/>.
  16. In addition, the Doom 3 BFG Edition Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the Doom 3 BFG Edition Source Code. If not, please request a copy in writing from id Software at the address below.
  17. If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA.
  18. ===========================================================================
  19. */
  20. #ifndef __MATH_EXTRAPOLATE_H__
  21. #define __MATH_EXTRAPOLATE_H__
  22. /*
  23. ==============================================================================================
  24. Extrapolation
  25. ==============================================================================================
  26. */
  27. typedef enum {
  28. EXTRAPOLATION_NONE = 0x01, // no extrapolation, covered distance = duration * 0.001 * ( baseSpeed )
  29. EXTRAPOLATION_LINEAR = 0x02, // linear extrapolation, covered distance = duration * 0.001 * ( baseSpeed + speed )
  30. EXTRAPOLATION_ACCELLINEAR = 0x04, // linear acceleration, covered distance = duration * 0.001 * ( baseSpeed + 0.5 * speed )
  31. EXTRAPOLATION_DECELLINEAR = 0x08, // linear deceleration, covered distance = duration * 0.001 * ( baseSpeed + 0.5 * speed )
  32. EXTRAPOLATION_ACCELSINE = 0x10, // sinusoidal acceleration, covered distance = duration * 0.001 * ( baseSpeed + sqrt( 0.5 ) * speed )
  33. EXTRAPOLATION_DECELSINE = 0x20, // sinusoidal deceleration, covered distance = duration * 0.001 * ( baseSpeed + sqrt( 0.5 ) * speed )
  34. EXTRAPOLATION_NOSTOP = 0x40 // do not stop at startTime + duration
  35. } extrapolation_t;
  36. template< class type >
  37. class idExtrapolate {
  38. public:
  39. idExtrapolate();
  40. void Init( const int startTime, const int duration, const type &startValue, const type &baseSpeed, const type &speed, const extrapolation_t extrapolationType );
  41. type GetCurrentValue( int time ) const;
  42. type GetCurrentSpeed( int time ) const;
  43. bool IsDone( int time ) const { return ( !( extrapolationType & EXTRAPOLATION_NOSTOP ) && time >= startTime + duration ); }
  44. void SetStartTime( int time ) { startTime = time; }
  45. int GetStartTime() const { return startTime; }
  46. int GetEndTime() const { return ( !( extrapolationType & EXTRAPOLATION_NOSTOP ) && duration > 0 ) ? startTime + duration : 0; }
  47. int GetDuration() const { return duration; }
  48. void SetStartValue( const type &value ) { startValue = value; }
  49. const type & GetStartValue() const { return startValue; }
  50. const type & GetBaseSpeed() const { return baseSpeed; }
  51. const type & GetSpeed() const { return speed; }
  52. extrapolation_t GetExtrapolationType() const { return extrapolationType; }
  53. private:
  54. extrapolation_t extrapolationType;
  55. int startTime;
  56. int duration;
  57. type startValue;
  58. type baseSpeed;
  59. type speed;
  60. };
  61. /*
  62. ====================
  63. idExtrapolate::idExtrapolate
  64. ====================
  65. */
  66. template< class type >
  67. ID_INLINE idExtrapolate<type>::idExtrapolate() {
  68. extrapolationType = EXTRAPOLATION_NONE;
  69. startTime = duration = 0.0f;
  70. memset( &startValue, 0, sizeof( startValue ) );
  71. memset( &baseSpeed, 0, sizeof( baseSpeed ) );
  72. memset( &speed, 0, sizeof( speed ) );
  73. }
  74. /*
  75. ====================
  76. idExtrapolate::Init
  77. ====================
  78. */
  79. template< class type >
  80. ID_INLINE void idExtrapolate<type>::Init( const int startTime, const int duration, const type &startValue, const type &baseSpeed, const type &speed, const extrapolation_t extrapolationType ) {
  81. this->extrapolationType = extrapolationType;
  82. this->startTime = startTime;
  83. this->duration = duration;
  84. this->startValue = startValue;
  85. this->baseSpeed = baseSpeed;
  86. this->speed = speed;
  87. }
  88. /*
  89. ====================
  90. idExtrapolate::GetCurrentValue
  91. ====================
  92. */
  93. template< class type >
  94. ID_INLINE type idExtrapolate<type>::GetCurrentValue( int time ) const {
  95. if ( time < startTime ) {
  96. return startValue;
  97. }
  98. if ( !( extrapolationType & EXTRAPOLATION_NOSTOP ) && ( time > startTime + duration ) ) {
  99. time = startTime + duration;
  100. }
  101. switch ( extrapolationType & ~EXTRAPOLATION_NOSTOP ) {
  102. case EXTRAPOLATION_NONE: {
  103. const float deltaTime = ( time - startTime ) * 0.001f;
  104. return startValue + deltaTime * baseSpeed;
  105. }
  106. case EXTRAPOLATION_LINEAR: {
  107. const float deltaTime = ( time - startTime ) * 0.001f;
  108. return startValue + deltaTime * ( baseSpeed + speed );
  109. }
  110. case EXTRAPOLATION_ACCELLINEAR: {
  111. if ( duration == 0 ) {
  112. return startValue;
  113. } else {
  114. const float deltaTime = ( time - startTime ) / (float)duration;
  115. const float s = ( 0.5f * deltaTime * deltaTime ) * ( (float)duration * 0.001f );
  116. return startValue + deltaTime * baseSpeed + s * speed;
  117. }
  118. }
  119. case EXTRAPOLATION_DECELLINEAR: {
  120. if ( duration == 0 ) {
  121. return startValue;
  122. } else {
  123. const float deltaTime = ( time - startTime ) / (float)duration;
  124. const float s = ( deltaTime - ( 0.5f * deltaTime * deltaTime ) ) * ( (float)duration * 0.001f );
  125. return startValue + deltaTime * baseSpeed + s * speed;
  126. }
  127. }
  128. case EXTRAPOLATION_ACCELSINE: {
  129. if ( duration == 0 ) {
  130. return startValue;
  131. } else {
  132. const float deltaTime = ( time - startTime ) / (float)duration;
  133. const float s = ( 1.0f - idMath::Cos( deltaTime * idMath::HALF_PI ) ) * (float)duration * 0.001f * idMath::SQRT_1OVER2;
  134. return startValue + deltaTime * baseSpeed + s * speed;
  135. }
  136. }
  137. case EXTRAPOLATION_DECELSINE: {
  138. if ( duration == 0 ) {
  139. return startValue;
  140. } else {
  141. const float deltaTime = ( time - startTime ) / (float)duration;
  142. const float s = idMath::Sin( deltaTime * idMath::HALF_PI ) * (float)duration * 0.001f * idMath::SQRT_1OVER2;
  143. return startValue + deltaTime * baseSpeed + s * speed;
  144. }
  145. }
  146. }
  147. return startValue;
  148. }
  149. /*
  150. ====================
  151. idExtrapolate::GetCurrentSpeed
  152. ====================
  153. */
  154. template< class type >
  155. ID_INLINE type idExtrapolate<type>::GetCurrentSpeed( int time ) const {
  156. if ( time < startTime || duration == 0 ) {
  157. return ( startValue - startValue ); //-V501
  158. }
  159. if ( !( extrapolationType & EXTRAPOLATION_NOSTOP ) && ( time > startTime + duration ) ) {
  160. return ( startValue - startValue ); //-V501
  161. }
  162. switch( extrapolationType & ~EXTRAPOLATION_NOSTOP ) {
  163. case EXTRAPOLATION_NONE: {
  164. return baseSpeed;
  165. }
  166. case EXTRAPOLATION_LINEAR: {
  167. return baseSpeed + speed;
  168. }
  169. case EXTRAPOLATION_ACCELLINEAR: {
  170. const float deltaTime = ( time - startTime ) / (float)duration;
  171. const float s = deltaTime;
  172. return baseSpeed + s * speed;
  173. }
  174. case EXTRAPOLATION_DECELLINEAR: {
  175. const float deltaTime = ( time - startTime ) / (float)duration;
  176. const float s = 1.0f - deltaTime;
  177. return baseSpeed + s * speed;
  178. }
  179. case EXTRAPOLATION_ACCELSINE: {
  180. const float deltaTime = ( time - startTime ) / (float)duration;
  181. const float s = idMath::Sin( deltaTime * idMath::HALF_PI );
  182. return baseSpeed + s * speed;
  183. }
  184. case EXTRAPOLATION_DECELSINE: {
  185. const float deltaTime = ( time - startTime ) / (float)duration;
  186. const float s = idMath::Cos( deltaTime * idMath::HALF_PI );
  187. return baseSpeed + s * speed;
  188. }
  189. default: {
  190. return baseSpeed;
  191. }
  192. }
  193. }
  194. #endif /* !__MATH_EXTRAPOLATE_H__ */