NAP
smoothdamp.h
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2  * License, v. 2.0. If a copy of the MPL was not distributed with this
3  * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4 
5 #pragma once
6 
7 // Local Includes
8 #include <mathutils.h>
9 
10 // External Includes
11 #include <rtti/rtti.h>
12 
13 namespace nap
14 {
15  namespace math
16  {
21  {
22  RTTI_ENABLE()
23  public:
24 
28  BaseSmoothOperator() = default;
29 
34  BaseSmoothOperator(float smoothTime) : mSmoothTime(smoothTime) { }
35 
41  BaseSmoothOperator(float smoothTime, float maxSpeed) :
42  mSmoothTime(smoothTime),
43  mMaxSpeed(maxSpeed) { }
44 
45  // Default destructor
46  virtual ~BaseSmoothOperator() = default;
47 
48  float mSmoothTime = 1.0f; // approximately the time it will take to reach the target. A smaller value will reach the target faster.
49  float mMaxSpeed = math::max<float>(); // allows you to clamp the maximum blend speed
50  };
51 
52 
61  template<typename T>
63  {
64  RTTI_ENABLE(BaseSmoothOperator)
65  public:
71  SmoothOperator(const T& currentValue, float smoothTime);
72 
79  SmoothOperator(const T& currentValue, float smoothTime, float maxSpeed);
80 
87  T& update(const T& targetValue, float deltaTime);
88 
92  const T& getValue() const { return mValue; }
93 
97  T& getValue() { return mValue; }
98 
102  const T& getTarget() const { return mTarget; }
103 
107  T& getTarget() { return mTarget; }
108 
112  const T& getVelocity() const { return mVelocity; }
113 
117  T& getVelocity() { return mVelocity; }
118 
123  void setValue(const T& value);
124 
125  protected:
130  void init();
131 
132  T mValue; // Current blend value
133  T mTarget; // Current blend target
134  T mVelocity; // Current velocity
135  };
136 
137 
139  // Template definitions
141 
142  template<typename T>
143  T& nap::math::SmoothOperator<T>::update(const T& targetValue, float deltaTime)
144  {
145  mTarget = targetValue;
146  nap::math::smooth<T>(mValue, targetValue, mVelocity, deltaTime, mSmoothTime, mMaxSpeed);
147  return mValue;
148  }
149 
150  template<typename T>
151  nap::math::SmoothOperator<T>::SmoothOperator(const T& currentValue, float smoothTime, float maxSpeed) : BaseSmoothOperator(smoothTime, maxSpeed),
152  mValue(currentValue)
153  {
154  init();
155  }
156 
157  template<typename T>
158  nap::math::SmoothOperator<T>::SmoothOperator(const T& currentValue, float smoothTime) : BaseSmoothOperator(smoothTime), mValue(currentValue)
159  {
160  init();
161  }
162 
163 
165  // Type definitions for all supported smooth operators
167 
173 
174 
176  // Forward declarations
178  template<>
180 
181  template<>
183 
184  template<>
186 
187  template<>
189 
190  template<>
192 
193  template<>
194  NAPAPI void nap::math::SmoothOperator<float>::setValue(const float& value);
195 
196  template<>
197  NAPAPI void nap::math::SmoothOperator<double>::setValue(const double& value);
198 
199  template<>
200  NAPAPI void nap::math::SmoothOperator<glm::vec2>::setValue(const glm::vec2& value);
201 
202  template<>
203  NAPAPI void nap::math::SmoothOperator<glm::vec3>::setValue(const glm::vec3& value);
204 
205  template<>
206  NAPAPI void nap::math::SmoothOperator<glm::vec4>::setValue(const glm::vec4& value);
207  }
208 }
nap::math::SmoothOperator::mTarget
T mTarget
Definition: smoothdamp.h:133
nap::math::SmoothOperator::setValue
void setValue(const T &value)
nap::math::SmoothOperator::getVelocity
const T & getVelocity() const
Definition: smoothdamp.h:112
nap::math::SmoothOperator::getValue
T & getValue()
Definition: smoothdamp.h:97
nap::math::BaseSmoothOperator::mSmoothTime
float mSmoothTime
Definition: smoothdamp.h:48
nap::math::SmoothOperator::init
void init()
nap::math::SmoothOperator
Definition: smoothdamp.h:62
nap::math::SmoothOperator::update
T & update(const T &targetValue, float deltaTime)
Definition: smoothdamp.h:143
nap::math::BaseSmoothOperator::~BaseSmoothOperator
virtual ~BaseSmoothOperator()=default
nap::math::SmoothOperator::SmoothOperator
SmoothOperator(const T &currentValue, float smoothTime)
Definition: smoothdamp.h:158
nap::math::BaseSmoothOperator::BaseSmoothOperator
BaseSmoothOperator(float smoothTime)
Definition: smoothdamp.h:34
nap::math::BaseSmoothOperator::mMaxSpeed
float mMaxSpeed
Definition: smoothdamp.h:49
nap::math::SmoothOperator::getTarget
const T & getTarget() const
Definition: smoothdamp.h:102
nap
Definition: templateapp.h:17
nap::math::SmoothOperator::getTarget
T & getTarget()
Definition: smoothdamp.h:107
nap::math::BaseSmoothOperator::BaseSmoothOperator
BaseSmoothOperator()=default
nap::math::SmoothOperator::mVelocity
T mVelocity
Definition: smoothdamp.h:134
nap::math::SmoothOperator::mValue
T mValue
Definition: smoothdamp.h:132
nap::math::BaseSmoothOperator::BaseSmoothOperator
BaseSmoothOperator(float smoothTime, float maxSpeed)
Definition: smoothdamp.h:41
nap::math::BaseSmoothOperator
Definition: smoothdamp.h:20
nap::math::SmoothOperator::getVelocity
T & getVelocity()
Definition: smoothdamp.h:117
nap::math::SmoothOperator::getValue
const T & getValue() const
Definition: smoothdamp.h:92