JK2eA
 All Classes Functions Variables
Interpolators.h
00001 #pragma once
00002 
00003 #define MAX_ITERATIONS 8
00004 
00005 template<class T>
00006 T LinearInterpolation(const float t, const T& P0, const T& P1)
00007 {
00008         return (T)(P0 + (P1 - P0) * t);
00009 
00010 }
00011 
00012 template<class T>
00013 T CatmulRomInterpolation(const float t, const T& P0, const T& P1, const T& P2, const T& P3)
00014 {
00015         static float t2; 
00016         static float t3; 
00017         t2 = t*t;
00018         t3 = t2*t;
00019 
00020         return (T) (0.5f * ( (2.0f * P1) + ( -1.0f * P0 + P2) * t + (2.0f * P0 - 5.0f * P1 + 4.0f * P2 - P3) * t2 + ( -1.0f * P0 + 3.0f * P1 - 3.0f * P2 + P3) * t3));
00021                         
00022 }
00023 template<class T>
00024 T CatmulRomInterpolationFirstDerivation(const float t, const T& P0, const T& P1, const T& P2, const T& P3)
00025 {
00026         return (T) 0.5f * ( 3.0f * t*t * (-P0 + 3.0f*P1 - 3.0f*P2 + P3) + 2.0f * t * (2.0f*P0 - 5.0f*P1 +4.0f*P2 - P3) - P0 + P2); 
00027 }
00028 
00029 /*
00030         Cubic Bezier
00031 */
00032 #pragma region CUBIC_BEZIER
00033 template<class T>
00034 T Bezier3Interpolation(const float t, const T& P0, const T& C0, const T& C1, const T& P1)
00035 {
00036         static float t2, t3, one_t, one_t2, one_t3;
00037         t2 = t*t;
00038         t3 = t2*t;
00039         one_t = 1 - t;
00040         one_t2 = one_t * one_t;
00041         one_t3 = one_t2 * one_t;
00042 
00043         return (T) one_t3*P0 + 3.0f*one_t2*t*C0 + 3.0f*one_t*t2*C1 + t3*P1;
00044 }
00045 template<class T>
00046 T Bezier3InterpolationFirstDerivation(const float t, const T& P0, const T& C0, const T& C1, const T& P1)
00047 {
00048         static float t2, one_t2;
00049         t2 = t*t;
00050         one_t2 = (1 - t) * (1 - t);
00051         
00052         return (T) -3.0f * (P0 * one_t2 + C0 * (-3.0f*t2 + 4.0f*t - 1.0f) + t * (3.0f*C1*t - 2.0f*C1 - P1 * t));
00053 }
00054 
00055 template<class T>
00056 T Bezier3InterpolationSecondDerivation(const float t, const T& P0, const T& C0, const T& C1, const T& P1)
00057 {
00058         static float t2, one_t2;
00059         t2 = t*t;
00060         one_t2 = (1 - t) * (1 - t);
00061         
00062         return (T) -6.0f * (P0 * (t - 1.0f) + C0 * (2.0f - 3.0f*t) + 3.0f*C1*t - C1 - P1 * t);
00063 }
00064 
00065 float Bezier3InterpolationInverse(const float value, const float t_guess, float epsilon, const float& P0, const float& C0, const float& C1, const float& P1);
00066 
00067 
00068 #pragma endregion CUBIC_BEZIER
00069 
00071 
00072 
00073 template<class T>
00074 T Hermite3Interpolation(const float t, const T& P0, const T& C0, const T& C1, const T& P1)
00075 {
00076         static float t2, t3;
00077         t2 = t*t;
00078         t3 = t2*t;
00079         
00080 
00081         return (T) (2.0*t3 - 3.0*t2 + 1.0)*P0 + (t3 - 2.0*t2 + t)*(C0 - P0) + (-2.0*t3 + 3.0*t2)*(P1) + (t3 - t2)*(C1 - P1);
00082 }
00083 
00084 /*
00085 smoothstep performs smooth Hermite interpolation between 0 and 1 when edge0 < x < edge1. This is useful in cases where a threshold function with a smooth transition is desired. smoothstep is equivalent to: 
00086 
00087 genType t; 
00088 t = clamp((x - edge0) / (edge1 - edge0), 0.0, 1.0);
00089 return t * t * (3.0 - 2.0 * t);
00090 
00091 Results are undefined if edge0 ≥ edge1.
00092 
00093 clamp returns the value of x constrained to the range minVal to maxVal. The returned value is computed as min(max(x, minVal), maxVal).
00094 */
00095