doom3-gpl
Doom 3 GPL source release
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Interpolate.h
Go to the documentation of this file.
1 /*
2 ===========================================================================
3 
4 Doom 3 GPL Source Code
5 Copyright (C) 1999-2011 id Software LLC, a ZeniMax Media company.
6 
7 This file is part of the Doom 3 GPL Source Code (?Doom 3 Source Code?).
8 
9 Doom 3 Source Code is free software: you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation, either version 3 of the License, or
12 (at your option) any later version.
13 
14 Doom 3 Source Code is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18 
19 You should have received a copy of the GNU General Public License
20 along with Doom 3 Source Code. If not, see <http://www.gnu.org/licenses/>.
21 
22 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.
23 
24 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.
25 
26 ===========================================================================
27 */
28 
29 #ifndef __MATH_INTERPOLATE_H__
30 #define __MATH_INTERPOLATE_H__
31 
32 /*
33 ==============================================================================================
34 
35  Linear interpolation.
36 
37 ==============================================================================================
38 */
39 
40 template< class type >
42 public:
43  idInterpolate();
44 
45  void Init( const float startTime, const float duration, const type &startValue, const type &endValue );
46  void SetStartTime( float time ) { this->startTime = time; }
47  void SetDuration( float duration ) { this->duration = duration; }
48  void SetStartValue( const type &startValue ) { this->startValue = startValue; }
49  void SetEndValue( const type &endValue ) { this->endValue = endValue; }
50 
51  type GetCurrentValue( float time ) const;
52  bool IsDone( float time ) const { return ( time >= startTime + duration ); }
53 
54  float GetStartTime( void ) const { return startTime; }
55  float GetEndTime( void ) const { return startTime + duration; }
56  float GetDuration( void ) const { return duration; }
57  const type & GetStartValue( void ) const { return startValue; }
58  const type & GetEndValue( void ) const { return endValue; }
59 
60 private:
61  float startTime;
62  float duration;
65  mutable float currentTime;
66  mutable type currentValue;
67 };
68 
69 /*
70 ====================
71 idInterpolate::idInterpolate
72 ====================
73 */
74 template< class type >
76  currentTime = startTime = duration = 0;
77  memset( &currentValue, 0, sizeof( currentValue ) );
78  startValue = endValue = currentValue;
79 }
80 
81 /*
82 ====================
83 idInterpolate::Init
84 ====================
85 */
86 template< class type >
87 ID_INLINE void idInterpolate<type>::Init( const float startTime, const float duration, const type &startValue, const type &endValue ) {
88  this->startTime = startTime;
89  this->duration = duration;
90  this->startValue = startValue;
91  this->endValue = endValue;
92  this->currentTime = startTime - 1;
93  this->currentValue = startValue;
94 }
95 
96 /*
97 ====================
98 idInterpolate::GetCurrentValue
99 ====================
100 */
101 template< class type >
102 ID_INLINE type idInterpolate<type>::GetCurrentValue( float time ) const {
103  float deltaTime;
104 
105  deltaTime = time - startTime;
106  if ( time != currentTime ) {
107  currentTime = time;
108  if ( deltaTime <= 0 ) {
109  currentValue = startValue;
110  } else if ( deltaTime >= duration ) {
111  currentValue = endValue;
112  } else {
113  currentValue = startValue + ( endValue - startValue ) * ( (float) deltaTime / duration );
114  }
115  }
116  return currentValue;
117 }
118 
119 
120 /*
121 ==============================================================================================
122 
123  Continuous interpolation with linear acceleration and deceleration phase.
124  The velocity is continuous but the acceleration is not.
125 
126 ==============================================================================================
127 */
128 
129 template< class type >
131 public:
133 
134  void Init( const float startTime, const float accelTime, const float decelTime, const float duration, const type &startValue, const type &endValue );
135  void SetStartTime( float time ) { startTime = time; Invalidate(); }
136  void SetStartValue( const type &startValue ) { this->startValue = startValue; Invalidate(); }
137  void SetEndValue( const type &endValue ) { this->endValue = endValue; Invalidate(); }
138 
139  type GetCurrentValue( float time ) const;
140  type GetCurrentSpeed( float time ) const;
141  bool IsDone( float time ) const { return ( time >= startTime + accelTime + linearTime + decelTime ); }
142 
143  float GetStartTime( void ) const { return startTime; }
144  float GetEndTime( void ) const { return startTime + accelTime + linearTime + decelTime; }
145  float GetDuration( void ) const { return accelTime + linearTime + decelTime; }
146  float GetAcceleration( void ) const { return accelTime; }
147  float GetDeceleration( void ) const { return decelTime; }
148  const type & GetStartValue( void ) const { return startValue; }
149  const type & GetEndValue( void ) const { return endValue; }
150 
151 private:
152  float startTime;
153  float accelTime;
154  float linearTime;
155  float decelTime;
159 
160  void Invalidate( void );
161  void SetPhase( float time ) const;
162 };
163 
164 /*
165 ====================
166 idInterpolateAccelDecelLinear::idInterpolateAccelDecelLinear
167 ====================
168 */
169 template< class type >
171  startTime = accelTime = linearTime = decelTime = 0;
172  memset( &startValue, 0, sizeof( startValue ) );
173  endValue = startValue;
174 }
175 
176 /*
177 ====================
178 idInterpolateAccelDecelLinear::Init
179 ====================
180 */
181 template< class type >
182 ID_INLINE void idInterpolateAccelDecelLinear<type>::Init( const float startTime, const float accelTime, const float decelTime, const float duration, const type &startValue, const type &endValue ) {
183  type speed;
184 
185  this->startTime = startTime;
186  this->accelTime = accelTime;
187  this->decelTime = decelTime;
188  this->startValue = startValue;
189  this->endValue = endValue;
190 
191  if ( duration <= 0.0f ) {
192  return;
193  }
194 
195  if ( this->accelTime + this->decelTime > duration ) {
196  this->accelTime = this->accelTime * duration / ( this->accelTime + this->decelTime );
197  this->decelTime = duration - this->accelTime;
198  }
199  this->linearTime = duration - this->accelTime - this->decelTime;
200  speed = ( endValue - startValue ) * ( 1000.0f / ( (float) this->linearTime + ( this->accelTime + this->decelTime ) * 0.5f ) );
201 
202  if ( this->accelTime ) {
203  extrapolate.Init( startTime, this->accelTime, startValue, ( startValue - startValue ), speed, EXTRAPOLATION_ACCELLINEAR );
204  } else if ( this->linearTime ) {
205  extrapolate.Init( startTime, this->linearTime, startValue, ( startValue - startValue ), speed, EXTRAPOLATION_LINEAR );
206  } else {
207  extrapolate.Init( startTime, this->decelTime, startValue, ( startValue - startValue ), speed, EXTRAPOLATION_DECELLINEAR );
208  }
209 }
210 
211 /*
212 ====================
213 idInterpolateAccelDecelLinear::Invalidate
214 ====================
215 */
216 template< class type >
218  extrapolate.Init( 0, 0, extrapolate.GetStartValue(), extrapolate.GetBaseSpeed(), extrapolate.GetSpeed(), EXTRAPOLATION_NONE );
219 }
220 
221 /*
222 ====================
223 idInterpolateAccelDecelLinear::SetPhase
224 ====================
225 */
226 template< class type >
227 ID_INLINE void idInterpolateAccelDecelLinear<type>::SetPhase( float time ) const {
228  float deltaTime;
229 
230  deltaTime = time - startTime;
231  if ( deltaTime < accelTime ) {
232  if ( extrapolate.GetExtrapolationType() != EXTRAPOLATION_ACCELLINEAR ) {
233  extrapolate.Init( startTime, accelTime, startValue, extrapolate.GetBaseSpeed(), extrapolate.GetSpeed(), EXTRAPOLATION_ACCELLINEAR );
234  }
235  } else if ( deltaTime < accelTime + linearTime ) {
236  if ( extrapolate.GetExtrapolationType() != EXTRAPOLATION_LINEAR ) {
237  extrapolate.Init( startTime + accelTime, linearTime, startValue + extrapolate.GetSpeed() * ( accelTime * 0.001f * 0.5f ), extrapolate.GetBaseSpeed(), extrapolate.GetSpeed(), EXTRAPOLATION_LINEAR );
238  }
239  } else {
240  if ( extrapolate.GetExtrapolationType() != EXTRAPOLATION_DECELLINEAR ) {
241  extrapolate.Init( startTime + accelTime + linearTime, decelTime, endValue - ( extrapolate.GetSpeed() * ( decelTime * 0.001f * 0.5f ) ), extrapolate.GetBaseSpeed(), extrapolate.GetSpeed(), EXTRAPOLATION_DECELLINEAR );
242  }
243  }
244 }
245 
246 /*
247 ====================
248 idInterpolateAccelDecelLinear::GetCurrentValue
249 ====================
250 */
251 template< class type >
253  SetPhase( time );
254  return extrapolate.GetCurrentValue( time );
255 }
256 
257 /*
258 ====================
259 idInterpolateAccelDecelLinear::GetCurrentSpeed
260 ====================
261 */
262 template< class type >
264  SetPhase( time );
265  return extrapolate.GetCurrentSpeed( time );
266 }
267 
268 
269 /*
270 ==============================================================================================
271 
272  Continuous interpolation with sinusoidal acceleration and deceleration phase.
273  Both the velocity and acceleration are continuous.
274 
275 ==============================================================================================
276 */
277 
278 template< class type >
280 public:
282 
283  void Init( const float startTime, const float accelTime, const float decelTime, const float duration, const type &startValue, const type &endValue );
284  void SetStartTime( float time ) { startTime = time; Invalidate(); }
285  void SetStartValue( const type &startValue ) { this->startValue = startValue; Invalidate(); }
286  void SetEndValue( const type &endValue ) { this->endValue = endValue; Invalidate(); }
287 
288  type GetCurrentValue( float time ) const;
289  type GetCurrentSpeed( float time ) const;
290  bool IsDone( float time ) const { return ( time >= startTime + accelTime + linearTime + decelTime ); }
291 
292  float GetStartTime( void ) const { return startTime; }
293  float GetEndTime( void ) const { return startTime + accelTime + linearTime + decelTime; }
294  float GetDuration( void ) const { return accelTime + linearTime + decelTime; }
295  float GetAcceleration( void ) const { return accelTime; }
296  float GetDeceleration( void ) const { return decelTime; }
297  const type & GetStartValue( void ) const { return startValue; }
298  const type & GetEndValue( void ) const { return endValue; }
299 
300 private:
301  float startTime;
302  float accelTime;
303  float linearTime;
304  float decelTime;
308 
309  void Invalidate( void );
310  void SetPhase( float time ) const;
311 };
312 
313 /*
314 ====================
315 idInterpolateAccelDecelSine::idInterpolateAccelDecelSine
316 ====================
317 */
318 template< class type >
320  startTime = accelTime = linearTime = decelTime = 0;
321  memset( &startValue, 0, sizeof( startValue ) );
322  endValue = startValue;
323 }
324 
325 /*
326 ====================
327 idInterpolateAccelDecelSine::Init
328 ====================
329 */
330 template< class type >
331 ID_INLINE void idInterpolateAccelDecelSine<type>::Init( const float startTime, const float accelTime, const float decelTime, const float duration, const type &startValue, const type &endValue ) {
332  type speed;
333 
334  this->startTime = startTime;
335  this->accelTime = accelTime;
336  this->decelTime = decelTime;
337  this->startValue = startValue;
338  this->endValue = endValue;
339 
340  if ( duration <= 0.0f ) {
341  return;
342  }
343 
344  if ( this->accelTime + this->decelTime > duration ) {
345  this->accelTime = this->accelTime * duration / ( this->accelTime + this->decelTime );
346  this->decelTime = duration - this->accelTime;
347  }
348  this->linearTime = duration - this->accelTime - this->decelTime;
349  speed = ( endValue - startValue ) * ( 1000.0f / ( (float) this->linearTime + ( this->accelTime + this->decelTime ) * idMath::SQRT_1OVER2 ) );
350 
351  if ( this->accelTime ) {
352  extrapolate.Init( startTime, this->accelTime, startValue, ( startValue - startValue ), speed, EXTRAPOLATION_ACCELSINE );
353  } else if ( this->linearTime ) {
354  extrapolate.Init( startTime, this->linearTime, startValue, ( startValue - startValue ), speed, EXTRAPOLATION_LINEAR );
355  } else {
356  extrapolate.Init( startTime, this->decelTime, startValue, ( startValue - startValue ), speed, EXTRAPOLATION_DECELSINE );
357  }
358 }
359 
360 /*
361 ====================
362 idInterpolateAccelDecelSine::Invalidate
363 ====================
364 */
365 template< class type >
367  extrapolate.Init( 0, 0, extrapolate.GetStartValue(), extrapolate.GetBaseSpeed(), extrapolate.GetSpeed(), EXTRAPOLATION_NONE );
368 }
369 
370 /*
371 ====================
372 idInterpolateAccelDecelSine::SetPhase
373 ====================
374 */
375 template< class type >
376 ID_INLINE void idInterpolateAccelDecelSine<type>::SetPhase( float time ) const {
377  float deltaTime;
378 
379  deltaTime = time - startTime;
380  if ( deltaTime < accelTime ) {
381  if ( extrapolate.GetExtrapolationType() != EXTRAPOLATION_ACCELSINE ) {
382  extrapolate.Init( startTime, accelTime, startValue, extrapolate.GetBaseSpeed(), extrapolate.GetSpeed(), EXTRAPOLATION_ACCELSINE );
383  }
384  } else if ( deltaTime < accelTime + linearTime ) {
385  if ( extrapolate.GetExtrapolationType() != EXTRAPOLATION_LINEAR ) {
386  extrapolate.Init( startTime + accelTime, linearTime, startValue + extrapolate.GetSpeed() * ( accelTime * 0.001f * idMath::SQRT_1OVER2 ), extrapolate.GetBaseSpeed(), extrapolate.GetSpeed(), EXTRAPOLATION_LINEAR );
387  }
388  } else {
389  if ( extrapolate.GetExtrapolationType() != EXTRAPOLATION_DECELSINE ) {
390  extrapolate.Init( startTime + accelTime + linearTime, decelTime, endValue - ( extrapolate.GetSpeed() * ( decelTime * 0.001f * idMath::SQRT_1OVER2 ) ), extrapolate.GetBaseSpeed(), extrapolate.GetSpeed(), EXTRAPOLATION_DECELSINE );
391  }
392  }
393 }
394 
395 /*
396 ====================
397 idInterpolateAccelDecelSine::GetCurrentValue
398 ====================
399 */
400 template< class type >
402  SetPhase( time );
403  return extrapolate.GetCurrentValue( time );
404 }
405 
406 /*
407 ====================
408 idInterpolateAccelDecelSine::GetCurrentSpeed
409 ====================
410 */
411 template< class type >
413  SetPhase( time );
414  return extrapolate.GetCurrentSpeed( time );
415 }
416 
417 #endif /* !__MATH_INTERPOLATE_H__ */
type GetCurrentValue(float time) const
Definition: Interpolate.h:401
const type & GetEndValue(void) const
Definition: Interpolate.h:149
float GetEndTime(void) const
Definition: Interpolate.h:144
const type & GetStartValue(void) const
Definition: Interpolate.h:148
idExtrapolate< type > extrapolate
Definition: Interpolate.h:307
float GetDeceleration(void) const
Definition: Interpolate.h:147
GLuint GLuint GLsizei GLenum type
Definition: glext.h:2845
float GetDuration(void) const
Definition: Interpolate.h:56
float GetDuration(void) const
Definition: Interpolate.h:294
float GetAcceleration(void) const
Definition: Interpolate.h:146
const type & GetEndValue(void) const
Definition: Interpolate.h:298
const type & GetEndValue(void) const
Definition: Interpolate.h:58
void SetPhase(float time) const
Definition: Interpolate.h:227
const type & GetStartValue(void) const
Definition: Interpolate.h:297
void Init(const float startTime, const float accelTime, const float decelTime, const float duration, const type &startValue, const type &endValue)
Definition: Interpolate.h:182
float GetDeceleration(void) const
Definition: Interpolate.h:296
void SetStartTime(float time)
Definition: Interpolate.h:46
bool IsDone(float time) const
Definition: Interpolate.h:52
float startTime
Definition: Interpolate.h:61
static const float SQRT_1OVER2
Definition: Math.h:212
float GetStartTime(void) const
Definition: Interpolate.h:292
void SetStartValue(const type &startValue)
Definition: Interpolate.h:48
bool IsDone(float time) const
Definition: Interpolate.h:141
float GetStartTime(void) const
Definition: Interpolate.h:54
void SetStartTime(float time)
Definition: Interpolate.h:135
void SetEndValue(const type &endValue)
Definition: Interpolate.h:286
void SetStartValue(const type &startValue)
Definition: Interpolate.h:285
void Init(const float startTime, const float duration, const type &startValue, const type &endValue)
Definition: Interpolate.h:87
float GetAcceleration(void) const
Definition: Interpolate.h:295
type GetCurrentValue(float time) const
Definition: Interpolate.h:102
void SetEndValue(const type &endValue)
Definition: Interpolate.h:137
float GetEndTime(void) const
Definition: Interpolate.h:55
type currentValue
Definition: Interpolate.h:66
idExtrapolate< type > extrapolate
Definition: Interpolate.h:158
float GetDuration(void) const
Definition: Interpolate.h:145
const type & GetStartValue(void) const
Definition: Interpolate.h:57
float GetEndTime(void) const
Definition: Interpolate.h:293
void SetStartTime(float time)
Definition: Interpolate.h:284
tuple f
Definition: idal.py:89
void SetPhase(float time) const
Definition: Interpolate.h:376
float GetStartTime(void) const
Definition: Interpolate.h:143
void SetEndValue(const type &endValue)
Definition: Interpolate.h:49
type GetCurrentSpeed(float time) const
Definition: Interpolate.h:412
bool IsDone(float time) const
Definition: Interpolate.h:290
type GetCurrentSpeed(float time) const
Definition: Interpolate.h:263
void SetDuration(float duration)
Definition: Interpolate.h:47
float currentTime
Definition: Interpolate.h:65
type GetCurrentValue(float time) const
Definition: Interpolate.h:252
void Init(const float startTime, const float accelTime, const float decelTime, const float duration, const type &startValue, const type &endValue)
Definition: Interpolate.h:331
void SetStartValue(const type &startValue)
Definition: Interpolate.h:136
float duration
Definition: Interpolate.h:62