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.

74 lines
1.2 KiB

  1. //========= Copyright � 1996-2005, Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. //=============================================================================//
  6. #ifndef APPARENT_VELOCITY_HELPER_H
  7. #define APPARENT_VELOCITY_HELPER_H
  8. #ifdef _WIN32
  9. #pragma once
  10. #endif
  11. inline float CalcDistance( float x, float y )
  12. {
  13. return x - y;
  14. }
  15. inline float CalcDistance( const Vector &a, const Vector &b )
  16. {
  17. return a.DistTo( b );
  18. }
  19. template<class T>
  20. class CDefaultCalcDistance
  21. {
  22. public:
  23. static inline float CalcDistance( const T &a, const T &b )
  24. {
  25. return ::CalcDistance( a, b );
  26. }
  27. };
  28. class CCalcDistance2D
  29. {
  30. public:
  31. static inline float CalcDistance( const Vector &a, const Vector &b )
  32. {
  33. return (a-b).Length2D();
  34. }
  35. };
  36. template< class T, class Functor=CDefaultCalcDistance<T> >
  37. class CApparentVelocity
  38. {
  39. public:
  40. CApparentVelocity()
  41. {
  42. m_LastTime = -1;
  43. }
  44. float AddSample( float time, T value )
  45. {
  46. float flRet = 0;
  47. if ( m_LastTime != -1 )
  48. {
  49. flRet = Functor::CalcDistance(value, m_LastValue) / (time - m_LastTime);
  50. }
  51. m_LastTime = time;
  52. m_LastValue = value;
  53. return flRet;
  54. }
  55. private:
  56. T m_LastValue;
  57. float m_LastTime;
  58. };
  59. #endif // APPARENT_VELOCITY_HELPER_H