Extrapolate.h 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. /*
  2. ===========================================================================
  3. Doom 3 GPL Source Code
  4. Copyright (C) 1999-2011 id Software LLC, a ZeniMax Media company.
  5. This file is part of the Doom 3 GPL Source Code (?Doom 3 Source Code?).
  6. Doom 3 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 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 Source Code. If not, see <http://www.gnu.org/licenses/>.
  16. In addition, the Doom 3 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 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 float startTime, const float duration, const type &startValue, const type &baseSpeed, const type &speed, const extrapolation_t extrapolationType );
  41. type GetCurrentValue( float time ) const;
  42. type GetCurrentSpeed( float time ) const;
  43. bool IsDone( float time ) const { return ( !( extrapolationType & EXTRAPOLATION_NOSTOP ) && time >= startTime + duration ); }
  44. void SetStartTime( float time ) { startTime = time; currentTime = -1; }
  45. float GetStartTime( void ) const { return startTime; }
  46. float GetEndTime( void ) const { return ( !( extrapolationType & EXTRAPOLATION_NOSTOP ) && duration > 0 ) ? startTime + duration : 0; }
  47. float GetDuration( void ) const { return duration; }
  48. void SetStartValue( const type &value ) { startValue = value; currentTime = -1; }
  49. const type & GetStartValue( void ) const { return startValue; }
  50. const type & GetBaseSpeed( void ) const { return baseSpeed; }
  51. const type & GetSpeed( void ) const { return speed; }
  52. extrapolation_t GetExtrapolationType( void ) const { return extrapolationType; }
  53. private:
  54. extrapolation_t extrapolationType;
  55. float startTime;
  56. float duration;
  57. type startValue;
  58. type baseSpeed;
  59. type speed;
  60. mutable float currentTime;
  61. mutable type currentValue;
  62. };
  63. /*
  64. ====================
  65. idExtrapolate::idExtrapolate
  66. ====================
  67. */
  68. template< class type >
  69. ID_INLINE idExtrapolate<type>::idExtrapolate() {
  70. extrapolationType = EXTRAPOLATION_NONE;
  71. startTime = duration = 0.0f;
  72. memset( &startValue, 0, sizeof( startValue ) );
  73. memset( &baseSpeed, 0, sizeof( baseSpeed ) );
  74. memset( &speed, 0, sizeof( speed ) );
  75. currentTime = -1;
  76. currentValue = startValue;
  77. }
  78. /*
  79. ====================
  80. idExtrapolate::Init
  81. ====================
  82. */
  83. template< class type >
  84. 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 ) {
  85. this->extrapolationType = extrapolationType;
  86. this->startTime = startTime;
  87. this->duration = duration;
  88. this->startValue = startValue;
  89. this->baseSpeed = baseSpeed;
  90. this->speed = speed;
  91. currentTime = -1;
  92. currentValue = startValue;
  93. }
  94. /*
  95. ====================
  96. idExtrapolate::GetCurrentValue
  97. ====================
  98. */
  99. template< class type >
  100. ID_INLINE type idExtrapolate<type>::GetCurrentValue( float time ) const {
  101. float deltaTime, s;
  102. if ( time == currentTime ) {
  103. return currentValue;
  104. }
  105. currentTime = time;
  106. if ( time < startTime ) {
  107. return startValue;
  108. }
  109. if ( !( extrapolationType & EXTRAPOLATION_NOSTOP ) && ( time > startTime + duration ) ) {
  110. time = startTime + duration;
  111. }
  112. switch( extrapolationType & ~EXTRAPOLATION_NOSTOP ) {
  113. case EXTRAPOLATION_NONE: {
  114. deltaTime = ( time - startTime ) * 0.001f;
  115. currentValue = startValue + deltaTime * baseSpeed;
  116. break;
  117. }
  118. case EXTRAPOLATION_LINEAR: {
  119. deltaTime = ( time - startTime ) * 0.001f;
  120. currentValue = startValue + deltaTime * ( baseSpeed + speed );
  121. break;
  122. }
  123. case EXTRAPOLATION_ACCELLINEAR: {
  124. if ( !duration ) {
  125. currentValue = startValue;
  126. } else {
  127. deltaTime = ( time - startTime ) / duration;
  128. s = ( 0.5f * deltaTime * deltaTime ) * ( duration * 0.001f );
  129. currentValue = startValue + deltaTime * baseSpeed + s * speed;
  130. }
  131. break;
  132. }
  133. case EXTRAPOLATION_DECELLINEAR: {
  134. if ( !duration ) {
  135. currentValue = startValue;
  136. } else {
  137. deltaTime = ( time - startTime ) / duration;
  138. s = ( deltaTime - ( 0.5f * deltaTime * deltaTime ) ) * ( duration * 0.001f );
  139. currentValue = startValue + deltaTime * baseSpeed + s * speed;
  140. }
  141. break;
  142. }
  143. case EXTRAPOLATION_ACCELSINE: {
  144. if ( !duration ) {
  145. currentValue = startValue;
  146. } else {
  147. deltaTime = ( time - startTime ) / duration;
  148. s = ( 1.0f - idMath::Cos( deltaTime * idMath::HALF_PI ) ) * duration * 0.001f * idMath::SQRT_1OVER2;
  149. currentValue = startValue + deltaTime * baseSpeed + s * speed;
  150. }
  151. break;
  152. }
  153. case EXTRAPOLATION_DECELSINE: {
  154. if ( !duration ) {
  155. currentValue = startValue;
  156. } else {
  157. deltaTime = ( time - startTime ) / duration;
  158. s = idMath::Sin( deltaTime * idMath::HALF_PI ) * duration * 0.001f * idMath::SQRT_1OVER2;
  159. currentValue = startValue + deltaTime * baseSpeed + s * speed;
  160. }
  161. break;
  162. }
  163. }
  164. return currentValue;
  165. }
  166. /*
  167. ====================
  168. idExtrapolate::GetCurrentSpeed
  169. ====================
  170. */
  171. template< class type >
  172. ID_INLINE type idExtrapolate<type>::GetCurrentSpeed( float time ) const {
  173. float deltaTime, s;
  174. if ( time < startTime || !duration ) {
  175. return ( startValue - startValue );
  176. }
  177. if ( !( extrapolationType & EXTRAPOLATION_NOSTOP ) && ( time > startTime + duration ) ) {
  178. return ( startValue - startValue );
  179. }
  180. switch( extrapolationType & ~EXTRAPOLATION_NOSTOP ) {
  181. case EXTRAPOLATION_NONE: {
  182. return baseSpeed;
  183. }
  184. case EXTRAPOLATION_LINEAR: {
  185. return baseSpeed + speed;
  186. }
  187. case EXTRAPOLATION_ACCELLINEAR: {
  188. deltaTime = ( time - startTime ) / duration;
  189. s = deltaTime;
  190. return baseSpeed + s * speed;
  191. }
  192. case EXTRAPOLATION_DECELLINEAR: {
  193. deltaTime = ( time - startTime ) / duration;
  194. s = 1.0f - deltaTime;
  195. return baseSpeed + s * speed;
  196. }
  197. case EXTRAPOLATION_ACCELSINE: {
  198. deltaTime = ( time - startTime ) / duration;
  199. s = idMath::Sin( deltaTime * idMath::HALF_PI );
  200. return baseSpeed + s * speed;
  201. }
  202. case EXTRAPOLATION_DECELSINE: {
  203. deltaTime = ( time - startTime ) / duration;
  204. s = idMath::Cos( deltaTime * idMath::HALF_PI );
  205. return baseSpeed + s * speed;
  206. }
  207. default: {
  208. return baseSpeed;
  209. }
  210. }
  211. }
  212. #endif /* !__MATH_EXTRAPOLATE_H__ */