Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

170 lines
3.5 KiB

  1. //========= Copyright � 1996-2005, Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. //=============================================================================//
  6. #ifndef LERP_FUNCTIONS_H
  7. #define LERP_FUNCTIONS_H
  8. #ifdef _WIN32
  9. #pragma once
  10. #endif
  11. template <class T>
  12. inline T LoopingLerp( float flPercent, T flFrom, T flTo )
  13. {
  14. T s = flTo * flPercent + flFrom * (1.0f - flPercent);
  15. return s;
  16. }
  17. template <>
  18. inline float LoopingLerp( float flPercent, float flFrom, float flTo )
  19. {
  20. if ( fabs( flTo - flFrom ) >= 0.5f )
  21. {
  22. if (flFrom < flTo)
  23. flFrom += 1.0f;
  24. else
  25. flTo += 1.0f;
  26. }
  27. float s = flTo * flPercent + flFrom * (1.0f - flPercent);
  28. s = s - (int)(s);
  29. if (s < 0.0f)
  30. s = s + 1.0f;
  31. return s;
  32. }
  33. template <class T>
  34. inline T Lerp_Hermite( const T& /*current*/, float t, const T& p0, const T& p1, const T& p2 )
  35. {
  36. T d1 = p1 - p0;
  37. T d2 = p2 - p1;
  38. T output;
  39. float tSqr = t*t;
  40. float tCube = t*tSqr;
  41. output = p1 * (2*tCube-3*tSqr+1);
  42. output += p2 * (-2*tCube+3*tSqr);
  43. output += d1 * (tCube-2*tSqr+t);
  44. output += d2 * (tCube-tSqr);
  45. return output;
  46. }
  47. template <class T>
  48. inline T Derivative_Hermite( float t, const T& p0, const T& p1, const T& p2 )
  49. {
  50. T d1 = p1 - p0;
  51. T d2 = p2 - p1;
  52. T output;
  53. float tSqr = t*t;
  54. output = p1 * (6*tSqr - 6*t);
  55. output += p2 * (-6*tSqr + 6*t);
  56. output += d1 * (3*tSqr - 4*t + 1);
  57. output += d2 * (3*tSqr - 2*t);
  58. return output;
  59. }
  60. inline void Lerp_Clamp( int val )
  61. {
  62. }
  63. inline void Lerp_Clamp( float val )
  64. {
  65. }
  66. inline void Lerp_Clamp( const Vector &val )
  67. {
  68. }
  69. inline void Lerp_Clamp( const QAngle &val )
  70. {
  71. }
  72. // If we have a range checked var, then we can clamp to its limits.
  73. template< class T, int minValue, int maxValue, int startValue >
  74. inline void Lerp_Clamp( CRangeCheckedVar<T,minValue,maxValue,startValue> &val )
  75. {
  76. val.Clamp();
  77. }
  78. template<>
  79. inline QAngle Lerp_Hermite<QAngle>( const QAngle &/*current*/, float t, const QAngle& p0, const QAngle& p1, const QAngle& p2 )
  80. {
  81. // Can't do hermite with QAngles, get discontinuities, just do a regular interpolation
  82. return Lerp( t, p1, p2 );
  83. }
  84. template <class T>
  85. inline T LoopingLerp_Hermite( T current, float t, T p0, T p1, T p2 )
  86. {
  87. return Lerp_Hermite( current, t, p0, p1, p2 );
  88. }
  89. template <>
  90. inline float LoopingLerp_Hermite( float /*current*/, float t, float p0, float p1, float p2 )
  91. {
  92. if ( fabs( p1 - p0 ) > 0.5f )
  93. {
  94. if ( p0 < p1 )
  95. p0 += 1.0f;
  96. else
  97. p1 += 1.0f;
  98. }
  99. if ( fabs( p2 - p1 ) > 0.5f )
  100. {
  101. if ( p1 < p2 )
  102. {
  103. p1 += 1.0f;
  104. // see if we need to fix up p0
  105. // important for vars that are decreasing from p0->p1->p2 where
  106. // p1 is fixed up relative to p2, eg p0 = 0.2, p1 = 0.1, p2 = 0.9
  107. if ( abs( p1 - p0 ) > 0.5 )
  108. {
  109. if ( p0 < p1 )
  110. p0 += 1.0f;
  111. else
  112. p1 += 1.0f;
  113. }
  114. }
  115. else
  116. {
  117. p2 += 1.0f;
  118. }
  119. }
  120. float s = Lerp_Hermite( /*current*/ 0.0f, t, p0, p1, p2 );
  121. s = s - (int)(s);
  122. if (s < 0.0f)
  123. {
  124. s = s + 1.0f;
  125. }
  126. return s;
  127. }
  128. template< int minValue, int maxValue, int startValue >
  129. inline CRangeCheckedVar<float, minValue, maxValue, startValue> LoopingLerp_Hermite( CRangeCheckedVar<float, minValue, maxValue, startValue> current, float t, CRangeCheckedVar<float, minValue, maxValue, startValue> p0, CRangeCheckedVar<float, minValue, maxValue, startValue> p1, CRangeCheckedVar<float, minValue, maxValue, startValue> p2 )
  130. {
  131. return LoopingLerp_Hermite( (float)current, t, (float)p0, (float)p1, (float)p2 );
  132. }
  133. // NOTE: C_AnimationLayer has its own versions of these functions in animationlayer.h.
  134. #endif // LERP_FUNCTIONS_H