Interpolate.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  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_INTERPOLATE_H__
  21. #define __MATH_INTERPOLATE_H__
  22. /*
  23. ==============================================================================================
  24. Linear interpolation.
  25. ==============================================================================================
  26. */
  27. template< class type >
  28. class idInterpolate {
  29. public:
  30. idInterpolate();
  31. void Init( const float startTime, const float duration, const type &startValue, const type &endValue );
  32. void SetStartTime( float time ) { this->startTime = time; }
  33. void SetDuration( float duration ) { this->duration = duration; }
  34. void SetStartValue( const type &startValue ) { this->startValue = startValue; }
  35. void SetEndValue( const type &endValue ) { this->endValue = endValue; }
  36. type GetCurrentValue( float time ) const;
  37. bool IsDone( float time ) const { return ( time >= startTime + duration ); }
  38. float GetStartTime( void ) const { return startTime; }
  39. float GetEndTime( void ) const { return startTime + duration; }
  40. float GetDuration( void ) const { return duration; }
  41. const type & GetStartValue( void ) const { return startValue; }
  42. const type & GetEndValue( void ) const { return endValue; }
  43. private:
  44. float startTime;
  45. float duration;
  46. type startValue;
  47. type endValue;
  48. mutable float currentTime;
  49. mutable type currentValue;
  50. };
  51. /*
  52. ====================
  53. idInterpolate::idInterpolate
  54. ====================
  55. */
  56. template< class type >
  57. ID_INLINE idInterpolate<type>::idInterpolate() {
  58. currentTime = startTime = duration = 0;
  59. memset( &currentValue, 0, sizeof( currentValue ) );
  60. startValue = endValue = currentValue;
  61. }
  62. /*
  63. ====================
  64. idInterpolate::Init
  65. ====================
  66. */
  67. template< class type >
  68. ID_INLINE void idInterpolate<type>::Init( const float startTime, const float duration, const type &startValue, const type &endValue ) {
  69. this->startTime = startTime;
  70. this->duration = duration;
  71. this->startValue = startValue;
  72. this->endValue = endValue;
  73. this->currentTime = startTime - 1;
  74. this->currentValue = startValue;
  75. }
  76. /*
  77. ====================
  78. idInterpolate::GetCurrentValue
  79. ====================
  80. */
  81. template< class type >
  82. ID_INLINE type idInterpolate<type>::GetCurrentValue( float time ) const {
  83. float deltaTime;
  84. deltaTime = time - startTime;
  85. if ( time != currentTime ) {
  86. currentTime = time;
  87. if ( deltaTime <= 0 ) {
  88. currentValue = startValue;
  89. } else if ( deltaTime >= duration ) {
  90. currentValue = endValue;
  91. } else {
  92. currentValue = startValue + ( endValue - startValue ) * ( (float) deltaTime / duration );
  93. }
  94. }
  95. return currentValue;
  96. }
  97. /*
  98. ==============================================================================================
  99. Continuous interpolation with linear acceleration and deceleration phase.
  100. The velocity is continuous but the acceleration is not.
  101. ==============================================================================================
  102. */
  103. template< class type >
  104. class idInterpolateAccelDecelLinear {
  105. public:
  106. idInterpolateAccelDecelLinear();
  107. void Init( const float startTime, const float accelTime, const float decelTime, const float duration, const type &startValue, const type &endValue );
  108. void SetStartTime( float time ) { startTime = time; Invalidate(); }
  109. void SetStartValue( const type &startValue ) { this->startValue = startValue; Invalidate(); }
  110. void SetEndValue( const type &endValue ) { this->endValue = endValue; Invalidate(); }
  111. type GetCurrentValue( float time ) const;
  112. type GetCurrentSpeed( float time ) const;
  113. bool IsDone( float time ) const { return ( time >= startTime + accelTime + linearTime + decelTime ); }
  114. float GetStartTime( void ) const { return startTime; }
  115. float GetEndTime( void ) const { return startTime + accelTime + linearTime + decelTime; }
  116. float GetDuration( void ) const { return accelTime + linearTime + decelTime; }
  117. float GetAcceleration( void ) const { return accelTime; }
  118. float GetDeceleration( void ) const { return decelTime; }
  119. const type & GetStartValue( void ) const { return startValue; }
  120. const type & GetEndValue( void ) const { return endValue; }
  121. private:
  122. float startTime;
  123. float accelTime;
  124. float linearTime;
  125. float decelTime;
  126. type startValue;
  127. type endValue;
  128. mutable idExtrapolate<type> extrapolate;
  129. void Invalidate( void );
  130. void SetPhase( float time ) const;
  131. };
  132. /*
  133. ====================
  134. idInterpolateAccelDecelLinear::idInterpolateAccelDecelLinear
  135. ====================
  136. */
  137. template< class type >
  138. ID_INLINE idInterpolateAccelDecelLinear<type>::idInterpolateAccelDecelLinear() {
  139. startTime = accelTime = linearTime = decelTime = 0;
  140. memset( &startValue, 0, sizeof( startValue ) );
  141. endValue = startValue;
  142. }
  143. /*
  144. ====================
  145. idInterpolateAccelDecelLinear::Init
  146. ====================
  147. */
  148. template< class type >
  149. ID_INLINE void idInterpolateAccelDecelLinear<type>::Init( const float startTime, const float accelTime, const float decelTime, const float duration, const type &startValue, const type &endValue ) {
  150. type speed;
  151. this->startTime = startTime;
  152. this->accelTime = accelTime;
  153. this->decelTime = decelTime;
  154. this->startValue = startValue;
  155. this->endValue = endValue;
  156. if ( duration <= 0.0f ) {
  157. return;
  158. }
  159. if ( this->accelTime + this->decelTime > duration ) {
  160. this->accelTime = this->accelTime * duration / ( this->accelTime + this->decelTime );
  161. this->decelTime = duration - this->accelTime;
  162. }
  163. this->linearTime = duration - this->accelTime - this->decelTime;
  164. speed = ( endValue - startValue ) * ( 1000.0f / ( (float) this->linearTime + ( this->accelTime + this->decelTime ) * 0.5f ) );
  165. if ( this->accelTime ) {
  166. extrapolate.Init( startTime, this->accelTime, startValue, ( startValue - startValue ), speed, EXTRAPOLATION_ACCELLINEAR );
  167. } else if ( this->linearTime ) {
  168. extrapolate.Init( startTime, this->linearTime, startValue, ( startValue - startValue ), speed, EXTRAPOLATION_LINEAR );
  169. } else {
  170. extrapolate.Init( startTime, this->decelTime, startValue, ( startValue - startValue ), speed, EXTRAPOLATION_DECELLINEAR );
  171. }
  172. }
  173. /*
  174. ====================
  175. idInterpolateAccelDecelLinear::Invalidate
  176. ====================
  177. */
  178. template< class type >
  179. ID_INLINE void idInterpolateAccelDecelLinear<type>::Invalidate( void ) {
  180. extrapolate.Init( 0, 0, extrapolate.GetStartValue(), extrapolate.GetBaseSpeed(), extrapolate.GetSpeed(), EXTRAPOLATION_NONE );
  181. }
  182. /*
  183. ====================
  184. idInterpolateAccelDecelLinear::SetPhase
  185. ====================
  186. */
  187. template< class type >
  188. ID_INLINE void idInterpolateAccelDecelLinear<type>::SetPhase( float time ) const {
  189. float deltaTime;
  190. deltaTime = time - startTime;
  191. if ( deltaTime < accelTime ) {
  192. if ( extrapolate.GetExtrapolationType() != EXTRAPOLATION_ACCELLINEAR ) {
  193. extrapolate.Init( startTime, accelTime, startValue, extrapolate.GetBaseSpeed(), extrapolate.GetSpeed(), EXTRAPOLATION_ACCELLINEAR );
  194. }
  195. } else if ( deltaTime < accelTime + linearTime ) {
  196. if ( extrapolate.GetExtrapolationType() != EXTRAPOLATION_LINEAR ) {
  197. extrapolate.Init( startTime + accelTime, linearTime, startValue + extrapolate.GetSpeed() * ( accelTime * 0.001f * 0.5f ), extrapolate.GetBaseSpeed(), extrapolate.GetSpeed(), EXTRAPOLATION_LINEAR );
  198. }
  199. } else {
  200. if ( extrapolate.GetExtrapolationType() != EXTRAPOLATION_DECELLINEAR ) {
  201. extrapolate.Init( startTime + accelTime + linearTime, decelTime, endValue - ( extrapolate.GetSpeed() * ( decelTime * 0.001f * 0.5f ) ), extrapolate.GetBaseSpeed(), extrapolate.GetSpeed(), EXTRAPOLATION_DECELLINEAR );
  202. }
  203. }
  204. }
  205. /*
  206. ====================
  207. idInterpolateAccelDecelLinear::GetCurrentValue
  208. ====================
  209. */
  210. template< class type >
  211. ID_INLINE type idInterpolateAccelDecelLinear<type>::GetCurrentValue( float time ) const {
  212. SetPhase( time );
  213. return extrapolate.GetCurrentValue( time );
  214. }
  215. /*
  216. ====================
  217. idInterpolateAccelDecelLinear::GetCurrentSpeed
  218. ====================
  219. */
  220. template< class type >
  221. ID_INLINE type idInterpolateAccelDecelLinear<type>::GetCurrentSpeed( float time ) const {
  222. SetPhase( time );
  223. return extrapolate.GetCurrentSpeed( time );
  224. }
  225. /*
  226. ==============================================================================================
  227. Continuous interpolation with sinusoidal acceleration and deceleration phase.
  228. Both the velocity and acceleration are continuous.
  229. ==============================================================================================
  230. */
  231. template< class type >
  232. class idInterpolateAccelDecelSine {
  233. public:
  234. idInterpolateAccelDecelSine();
  235. void Init( const float startTime, const float accelTime, const float decelTime, const float duration, const type &startValue, const type &endValue );
  236. void SetStartTime( float time ) { startTime = time; Invalidate(); }
  237. void SetStartValue( const type &startValue ) { this->startValue = startValue; Invalidate(); }
  238. void SetEndValue( const type &endValue ) { this->endValue = endValue; Invalidate(); }
  239. type GetCurrentValue( float time ) const;
  240. type GetCurrentSpeed( float time ) const;
  241. bool IsDone( float time ) const { return ( time >= startTime + accelTime + linearTime + decelTime ); }
  242. float GetStartTime( void ) const { return startTime; }
  243. float GetEndTime( void ) const { return startTime + accelTime + linearTime + decelTime; }
  244. float GetDuration( void ) const { return accelTime + linearTime + decelTime; }
  245. float GetAcceleration( void ) const { return accelTime; }
  246. float GetDeceleration( void ) const { return decelTime; }
  247. const type & GetStartValue( void ) const { return startValue; }
  248. const type & GetEndValue( void ) const { return endValue; }
  249. private:
  250. float startTime;
  251. float accelTime;
  252. float linearTime;
  253. float decelTime;
  254. type startValue;
  255. type endValue;
  256. mutable idExtrapolate<type> extrapolate;
  257. void Invalidate( void );
  258. void SetPhase( float time ) const;
  259. };
  260. /*
  261. ====================
  262. idInterpolateAccelDecelSine::idInterpolateAccelDecelSine
  263. ====================
  264. */
  265. template< class type >
  266. ID_INLINE idInterpolateAccelDecelSine<type>::idInterpolateAccelDecelSine() {
  267. startTime = accelTime = linearTime = decelTime = 0;
  268. memset( &startValue, 0, sizeof( startValue ) );
  269. endValue = startValue;
  270. }
  271. /*
  272. ====================
  273. idInterpolateAccelDecelSine::Init
  274. ====================
  275. */
  276. template< class type >
  277. ID_INLINE void idInterpolateAccelDecelSine<type>::Init( const float startTime, const float accelTime, const float decelTime, const float duration, const type &startValue, const type &endValue ) {
  278. type speed;
  279. this->startTime = startTime;
  280. this->accelTime = accelTime;
  281. this->decelTime = decelTime;
  282. this->startValue = startValue;
  283. this->endValue = endValue;
  284. if ( duration <= 0.0f ) {
  285. return;
  286. }
  287. if ( this->accelTime + this->decelTime > duration ) {
  288. this->accelTime = this->accelTime * duration / ( this->accelTime + this->decelTime );
  289. this->decelTime = duration - this->accelTime;
  290. }
  291. this->linearTime = duration - this->accelTime - this->decelTime;
  292. speed = ( endValue - startValue ) * ( 1000.0f / ( (float) this->linearTime + ( this->accelTime + this->decelTime ) * idMath::SQRT_1OVER2 ) );
  293. if ( this->accelTime ) {
  294. extrapolate.Init( startTime, this->accelTime, startValue, ( startValue - startValue ), speed, EXTRAPOLATION_ACCELSINE );
  295. } else if ( this->linearTime ) {
  296. extrapolate.Init( startTime, this->linearTime, startValue, ( startValue - startValue ), speed, EXTRAPOLATION_LINEAR );
  297. } else {
  298. extrapolate.Init( startTime, this->decelTime, startValue, ( startValue - startValue ), speed, EXTRAPOLATION_DECELSINE );
  299. }
  300. }
  301. /*
  302. ====================
  303. idInterpolateAccelDecelSine::Invalidate
  304. ====================
  305. */
  306. template< class type >
  307. ID_INLINE void idInterpolateAccelDecelSine<type>::Invalidate( void ) {
  308. extrapolate.Init( 0, 0, extrapolate.GetStartValue(), extrapolate.GetBaseSpeed(), extrapolate.GetSpeed(), EXTRAPOLATION_NONE );
  309. }
  310. /*
  311. ====================
  312. idInterpolateAccelDecelSine::SetPhase
  313. ====================
  314. */
  315. template< class type >
  316. ID_INLINE void idInterpolateAccelDecelSine<type>::SetPhase( float time ) const {
  317. float deltaTime;
  318. deltaTime = time - startTime;
  319. if ( deltaTime < accelTime ) {
  320. if ( extrapolate.GetExtrapolationType() != EXTRAPOLATION_ACCELSINE ) {
  321. extrapolate.Init( startTime, accelTime, startValue, extrapolate.GetBaseSpeed(), extrapolate.GetSpeed(), EXTRAPOLATION_ACCELSINE );
  322. }
  323. } else if ( deltaTime < accelTime + linearTime ) {
  324. if ( extrapolate.GetExtrapolationType() != EXTRAPOLATION_LINEAR ) {
  325. extrapolate.Init( startTime + accelTime, linearTime, startValue + extrapolate.GetSpeed() * ( accelTime * 0.001f * idMath::SQRT_1OVER2 ), extrapolate.GetBaseSpeed(), extrapolate.GetSpeed(), EXTRAPOLATION_LINEAR );
  326. }
  327. } else {
  328. if ( extrapolate.GetExtrapolationType() != EXTRAPOLATION_DECELSINE ) {
  329. extrapolate.Init( startTime + accelTime + linearTime, decelTime, endValue - ( extrapolate.GetSpeed() * ( decelTime * 0.001f * idMath::SQRT_1OVER2 ) ), extrapolate.GetBaseSpeed(), extrapolate.GetSpeed(), EXTRAPOLATION_DECELSINE );
  330. }
  331. }
  332. }
  333. /*
  334. ====================
  335. idInterpolateAccelDecelSine::GetCurrentValue
  336. ====================
  337. */
  338. template< class type >
  339. ID_INLINE type idInterpolateAccelDecelSine<type>::GetCurrentValue( float time ) const {
  340. SetPhase( time );
  341. return extrapolate.GetCurrentValue( time );
  342. }
  343. /*
  344. ====================
  345. idInterpolateAccelDecelSine::GetCurrentSpeed
  346. ====================
  347. */
  348. template< class type >
  349. ID_INLINE type idInterpolateAccelDecelSine<type>::GetCurrentSpeed( float time ) const {
  350. SetPhase( time );
  351. return extrapolate.GetCurrentSpeed( time );
  352. }
  353. #endif /* !__MATH_INTERPOLATE_H__ */