Team Fortress 2 Source Code as on 22/4/2020
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.

75 lines
1.2 KiB

  1. //========= Copyright 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(const T& t0)
  41. {
  42. m_LastTime = -1;
  43. m_LastValue = t0;
  44. }
  45. float AddSample( float time, T value )
  46. {
  47. float flRet = 0;
  48. if ( m_LastTime != -1 )
  49. {
  50. flRet = Functor::CalcDistance(value, m_LastValue) / (time - m_LastTime);
  51. }
  52. m_LastTime = time;
  53. m_LastValue = value;
  54. return flRet;
  55. }
  56. private:
  57. T m_LastValue;
  58. float m_LastTime;
  59. };
  60. #endif // APPARENT_VELOCITY_HELPER_H