Sleeping Wombat Common Library  0.50.0
swCommonLibrary
LinearInterpolator.h
Go to the documentation of this file.
1 #pragma once
2 
8 #include "IInterpolator.h"
9 
10 #include <assert.h>
11 
12 
16 template< typename KeyType >
17 class LinearInterpolator : public IInterpolator< KeyType >
18 {
19 private:
20 protected:
21 public:
23  explicit LinearInterpolator () = default;
25  explicit LinearInterpolator ( const Key< KeyType >& leftKey,
26  const Key< KeyType >& rightKey,
27  UPtr< const IInterpolator< KeyType > >& leftInterpolator,
28  UPtr< const IInterpolator< KeyType > >& rightInterpolator );
29  ~LinearInterpolator () = default;
30 
31 
33  virtual KeyType Interpolate ( TimeType time, Key< KeyType >& left, Key< KeyType >& right ) override;
34 
37  virtual void Update ( const Key< KeyType >& leftKey,
38  const Key< KeyType >& rightKey,
39  UPtr< const IInterpolator< KeyType > >& leftInterpolator,
40  UPtr< const IInterpolator< KeyType > >& rightInterpolator ) override;
41 
44  virtual KeyType LeftTangent ( const Key< KeyType >& left, const Key< KeyType >& right ) const override;
47  virtual KeyType RightTangent ( const Key< KeyType >& left, const Key< KeyType >& right ) const override;
48 
49 protected:
50  KeyType Tangent ( const Key< KeyType >& left, const Key< KeyType >& right ) const;
51 };
52 
53 //====================================================================================//
54 // Implementation
55 //====================================================================================//
56 
57 
58 // ================================ //
59 //
60 template< typename KeyType >
62  const Key< KeyType >& rightKey,
63  UPtr< const IInterpolator< KeyType > >& leftInterpolator,
64  UPtr< const IInterpolator< KeyType >> & rightInterpolator )
65 {}
66 
67 // ================================ //
68 //
69 template< typename KeyType >
70 inline KeyType LinearInterpolator< KeyType >::Interpolate ( TimeType time, Key< KeyType >& left, Key< KeyType >& right )
71 {
72  TimeType timeInterval = right.Time - left.Time;
73  TimeType progress = ( time - left.Time ) / timeInterval;
74 
75  auto leftResult = ( 1.0 + (-1.0f) * progress ) * left.Value; // Multiply by -1.0f to avoid calling operator-
76  auto rightResult = progress * right.Value;
77 
78  return static_cast< KeyType >( leftResult + rightResult );
79 }
80 
81 // ================================ //
82 //
83 template< typename KeyType >
85  const Key< KeyType >& rightKey,
86  UPtr< const IInterpolator< KeyType > >& leftInterpolator,
87  UPtr< const IInterpolator< KeyType > >& rightInterpolator )
88 {
89  // Intencionally empty.
90 }
91 
92 // ================================ //
93 //
94 template< typename KeyType >
95 inline KeyType LinearInterpolator< KeyType >::LeftTangent ( const Key< KeyType >& left, const Key< KeyType >& right ) const
96 {
97  return Tangent( left, right );
98 }
99 
100 // ================================ //
101 //
102 template< typename KeyType >
103 inline KeyType LinearInterpolator< KeyType >::RightTangent ( const Key< KeyType >& left, const Key< KeyType >& right ) const
104 {
105  return Tangent( left, right );
106 }
107 
108 
109 //====================================================================================//
110 // Internal
111 //====================================================================================//
112 
113 // ================================ //
114 //
115 template< typename KeyType >
116 inline KeyType LinearInterpolator< KeyType >::Tangent ( const Key< KeyType >& left, const Key< KeyType >& right ) const
117 {
118  KeyType valueInterval = right.Value - left.Value;
119  TimeType timeInterval = right.Time - left.Time;
120 
121  return static_cast< KeyType >( ( TimeType( 1.0f ) / timeInterval )* valueInterval );
122 }
Base class for interpolators.
Definition: IInterpolator.h:38
Definition: UPtr.h:10
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: LinearInterpolator.h:95
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: LinearInterpolator.h:84
LinearInterpolator()=default
Constructor for serialization.
Linear interpolator.
Definition: LinearInterpolator.h:17
Animation key.
Definition: IInterpolator.h:21
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: LinearInterpolator.h:103
virtual KeyType Interpolate(TimeType time, Key< KeyType > &left, Key< KeyType > &right) override
Main function invoked by evaluator.
Definition: LinearInterpolator.h:70