Panda3D
lerp_helpers.h
1 // Filename: lerp_helpers.h
2 // Created by: drose (27Aug02)
3 //
4 ////////////////////////////////////////////////////////////////////
5 //
6 // PANDA 3D SOFTWARE
7 // Copyright (c) Carnegie Mellon University. All rights reserved.
8 //
9 // All use of this software is subject to the terms of the revised BSD
10 // license. You should have received a copy of this license along
11 // with this source code in a file named "LICENSE."
12 //
13 ////////////////////////////////////////////////////////////////////
14 
15 #ifndef LERP_HELPERS_H
16 #define LERP_HELPERS_H
17 
18 #include "directbase.h"
19 
20 //
21 // The functions defined here include some trivial template functions
22 // for handling basic lerp computations, common to several .cxx files
23 // here.
24 //
25 
26 ////////////////////////////////////////////////////////////////////
27 // Function: lerp_value
28 // Description: Applies the linear lerp computation for a single
29 // parameter.
30 ////////////////////////////////////////////////////////////////////
31 template<class NumericType>
32 INLINE void
33 lerp_value(NumericType &current_value,
34  double d,
35  const NumericType &starting_value,
36  const NumericType &ending_value) {
37  current_value = starting_value + d * (ending_value - starting_value);
38 }
39 
40 ////////////////////////////////////////////////////////////////////
41 // Function: lerp_value_from_prev
42 // Description: Applies the linear lerp computation for a single
43 // parameter, when the starting value is implicit.
44 //
45 // This computes the new value based on assuming the
46 // prev_value represents the value computed at delta
47 // prev_d.
48 ////////////////////////////////////////////////////////////////////
49 template<class NumericType>
50 INLINE void
51 lerp_value_from_prev(NumericType &current_value,
52  double d, double prev_d,
53  const NumericType &prev_value,
54  const NumericType &ending_value) {
55  if (prev_d == 1.0) {
56  current_value = ending_value;
57  } else {
58  NumericType starting_value =
59  (prev_value - prev_d * ending_value) / (1.0 - prev_d);
60  current_value = starting_value + d * (ending_value - starting_value);
61  }
62 }
63 
64 
65 #endif
66