Ocular Engine
Interpolation.hpp
1 
17 #pragma once
18 #ifndef __H__OCULAR_ENGINE_MATH_INTERPOLATION__H__
19 #define __H__OCULAR_ENGINE_MATH_INTERPOLATION__H__
20 
21 #include "Definitions.hpp"
22 #include <cmath>
23 
24 //------------------------------------------------------------------------------------------
25 
30 namespace Ocular
31 {
36  namespace Math
37  {
51  static float InterpolateLinear(float const from, float const to, float const frac)
52  {
53  float boundFrac = (frac < 0.0f) ? 0.0f : (frac > 1.0f) ? 1.0f : frac;
54 
55  return (from * (1.0f - boundFrac)) + (to * boundFrac);
56  }
57 
71  static float InterpolateCosine(float const from, float const to, float const frac)
72  {
73  float boundFrac = (frac < 0.0f) ? 0.0f : (frac > 1.0f) ? 1.0f : frac;
74  float x = (1.0f - cosf(boundFrac * static_cast<float>(PI))) * 0.5f;
75 
76  return (from * (1.0f - x)) + (to * x);
77  }
78 
93  static float InterpolateCubic(float const before, float const from, float const to, float const after, float const frac)
94  {
95  float boundFrac = (frac < 0.0f) ? 0.0f : (frac > 1.0f) ? 1.0f : frac;
96  float frac2 = boundFrac * boundFrac;
97  float a0 = after - to - before + from;
98  float a1 = before - from - a0;
99  float a2 = to - before;
100  float a3 = from;
101 
102  return (a0 * boundFrac * frac2) + (a2 * frac2) + (a2 * boundFrac) + a3;
103  }
104 
115  static float InterpolateCatmullRom(float const before, float const from, float const to, float const after, float const frac)
116  {
117  float boundFrac = (frac < 0.0f) ? 0.0f : (frac > 1.0f) ? 1.0f : frac;
118  float frac2 = boundFrac * boundFrac;
119  float a0 = (-0.5f * before) + (1.5f * from) - (1.5f * to) + (0.5f * after);
120  float a1 = before - (2.5f * from) + (2.0f * to) - (0.5f * after);
121  float a2 = (-0.5f * before) + (0.5f * to);
122  float a3 = from;
123 
124  return (a0 * boundFrac * frac2) + (a2 * frac2) + (a2 * boundFrac) + a3;
125  }
126 
149  static float InterpolateHermite(float const before, float const from, float const to, float const after,
150  float const frac, float const tension = 0.0f, float const bias = 0.0f)
151  {
152  float boundFrac = (frac < 0.0f) ? 0.0f : (frac > 1.0) ? 1.0f : frac;
153  float frac2 = boundFrac * boundFrac;
154  float frac3 = boundFrac * frac2;
155  float m0 = ((from - before) * (1.0f + bias) * (1.0f - tension) * 0.5f)
156  + ((to - from) * (1.0f - bias) * (1.0f - tension) * 0.5f);
157  float m1 = ((to - from) * (1.0f + bias) * (1.0f - tension) * 0.5f)
158  + ((after - to) * (1.0f - bias) * (1.0f - tension) * 0.5f);
159  float a0 = (2.0f * frac3) - (3.0f * frac2) + 1.0f;
160  float a1 = frac3 - (2.0f * frac2) + boundFrac;
161  float a2 = frac3 - frac2;
162  float a3 = (-2.0f * frac3) + (3.0f * frac2);
163 
164  return (a0 * from) + (a1 * m0) + (a2 * m1) + (a3 * to);
165  }
166  }
170 }
175 //------------------------------------------------------------------------------------------
176 
177 #endif
Note: Once this library is made dynamic, this will no longer be needed.
Definition: Common.hpp:70