Sleeping Wombat Common Library  0.50.0
swCommonLibrary
CosinusInterpolator.h
Go to the documentation of this file.
1 #pragma once
2 
8 #include "IInterpolator.h"
9 
10 #define _USE_MATH_DEFINES
11 #include <math.h>
12 
13 
16 template< typename KeyType >
17 class CosinusInterpolator : public IInterpolator< KeyType >
18 {
19 private:
20 protected:
21 public:
22  explicit CosinusInterpolator () = default;
24  explicit CosinusInterpolator ( const Key< KeyType >& leftKey,
25  const Key< KeyType >& rightKey,
26  UPtr< const IInterpolator< KeyType > >& leftInterpolator,
27  UPtr< const IInterpolator< KeyType > >& rightInterpolator );
28  ~CosinusInterpolator () = default;
29 
30 
32  virtual KeyType Interpolate ( TimeType time, Key< KeyType >& left, Key< KeyType >& right ) override;
33 
36  virtual void Update ( const Key< KeyType >& leftKey,
37  const Key< KeyType >& rightKey,
38  UPtr< const IInterpolator< KeyType > >& leftInterpolator,
39  UPtr< const IInterpolator< KeyType > >& rightInterpolator ) override;
40 
43  virtual KeyType LeftTangent ( const Key< KeyType >& left, const Key< KeyType >& right ) const override;
46  virtual KeyType RightTangent ( const Key< KeyType >& left, const Key< KeyType >& right ) const override;
47 };
48 
49 
50 //====================================================================================//
51 // Implementation
52 //====================================================================================//
53 
54 // ================================ //
55 //
56 template< typename KeyType >
58  const Key< KeyType >& rightKey,
59  UPtr< const IInterpolator< KeyType > >& leftInterpolator,
60  UPtr< const IInterpolator< KeyType >> & rightInterpolator )
61 {}
62 
63 
64 // ================================ //
65 //
66 template< typename KeyType >
67 inline KeyType CosinusInterpolator< KeyType >::Interpolate ( TimeType time, Key< KeyType >& left, Key< KeyType >& right )
68 {
69  TimeType timeInterval = right.Time - left.Time;
70  TimeType progress = ( time - left.Time ) / timeInterval;
71 
72  progress = 0.5 + 0.5 * cos( progress * M_PI );
73 
74  auto leftResult = ( 1.0 + (-1.0f) * progress ) * left.Value; // Multiply by -1.0f to avoid calling operator-
75  auto rightResult = progress * right.Value;
76 
77  return static_cast< KeyType >( leftResult + rightResult );
78 }
79 
80 // ================================ //
81 //
82 template< typename KeyType >
84  const Key< KeyType >& rightKey,
85  UPtr< const IInterpolator< KeyType > >& leftInterpolator,
86  UPtr< const IInterpolator< KeyType > >& rightInterpolator )
87 {}
88 
89 // ================================ //
90 //
91 template< typename KeyType >
92 inline KeyType CosinusInterpolator< KeyType >::LeftTangent ( const Key< KeyType >& left, const Key< KeyType >& right ) const
93 {
94  return KeyType( 0 );
95 }
96 
97 // ================================ //
98 //
99 template< typename KeyType >
100 inline KeyType CosinusInterpolator< KeyType >::RightTangent ( const Key< KeyType >& left, const Key< KeyType >& right ) const
101 {
102  return KeyType( 0 );
103 }
virtual KeyType RightTangent(const Key< KeyType > &left, const Key< KeyType > &right) const override
Returns curve tangent. Function can be used by surrounding interpolators to smooth curve...
Definition: CosinusInterpolator.h:100
Base class for interpolators.
Definition: IInterpolator.h:38
virtual void Update(const Key< KeyType > &leftKey, const Key< KeyType > &rightKey, UPtr< const IInterpolator< KeyType > > &leftInterpolator, UPtr< const IInterpolator< KeyType > > &rightInterpolator) override
Function updates interpolator, when left or right key value changes.
Definition: CosinusInterpolator.h:83
Definition: UPtr.h:10
Cosinus interpolator.
Definition: CosinusInterpolator.h:17
virtual KeyType Interpolate(TimeType time, Key< KeyType > &left, Key< KeyType > &right) override
Main function invoked by evaluator.
Definition: CosinusInterpolator.h:67
Animation key.
Definition: IInterpolator.h:21
virtual KeyType LeftTangent(const Key< KeyType > &left, const Key< KeyType > &right) const override
Returns curve tangent. Function can be used by surrounding interpolators to smooth curve...
Definition: CosinusInterpolator.h:92