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:
129  void init();
130 
131  T mValue; // Current blend value
132  T mTarget; // Current blend target
133  T mVelocity; // Current velocity
134  };
135 
136 
138  // Template definitions
140 
141  template<typename T>
142  T& SmoothOperator<T>::update(const T& targetValue, float deltaTime)
143  {
144  mTarget = targetValue;
145  smooth<T>(mValue, targetValue, mVelocity, deltaTime, mSmoothTime, mMaxSpeed);
146  return mValue;
147  }
148 
149  template<typename T>
150  SmoothOperator<T>::SmoothOperator(const T& currentValue, float smoothTime, float maxSpeed) : BaseSmoothOperator(smoothTime, maxSpeed),
151  mValue(currentValue)
152  {
153  init();
154  }
155 
156  template<typename T>
157  SmoothOperator<T>::SmoothOperator(const T& currentValue, float smoothTime) : BaseSmoothOperator(smoothTime), mValue(currentValue)
158  {
159  init();
160  }
161 
162 
164  // Type definitions for all supported smooth operators
166 
172 
173 
175  // Forward declarations
177  template<>
178  NAPAPI void SmoothOperator<float>::init();
179 
180  template<>
181  NAPAPI void SmoothOperator<double>::init();
182 
183  template<>
184  NAPAPI void SmoothOperator<glm::vec2>::init();
185 
186  template<>
187  NAPAPI void SmoothOperator<glm::vec3>::init();
188 
189  template<>
190  NAPAPI void SmoothOperator<glm::vec4>::init();
191 
192  template<>
193  NAPAPI void SmoothOperator<float>::setValue(const float& value);
194 
195  template<>
196  NAPAPI void SmoothOperator<double>::setValue(const double& value);
197 
198  template<>
199  NAPAPI void SmoothOperator<glm::vec2>::setValue(const glm::vec2& value);
200 
201  template<>
202  NAPAPI void SmoothOperator<glm::vec3>::setValue(const glm::vec3& value);
203 
204  template<>
205  NAPAPI void SmoothOperator<glm::vec4>::setValue(const glm::vec4& value);
206  }
207 }
nap::math::SmoothOperator::mTarget
T mTarget
Definition: smoothdamp.h:132
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:142
nap::math::BaseSmoothOperator::~BaseSmoothOperator
virtual ~BaseSmoothOperator()=default
nap::math::SmoothOperator::SmoothOperator
SmoothOperator(const T &currentValue, float smoothTime)
Definition: smoothdamp.h:157
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:133
nap::math::SmoothOperator::mValue
T mValue
Definition: smoothdamp.h:131
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