animated.h
Go to the documentation of this file.
1 // License: Apache 2.0. See LICENSE file in root directory.
2 // Copyright(c) 2021 Intel Corporation. All Rights Reserved.
3 
4 #pragma once
5 
6 #include <chrono>
7 
8 
9 namespace rs2 {
10 
11 
12 
13 inline float clamp( float x, float min, float max )
14 {
15  return std::max( std::min( max, x ), min );
16 }
17 
18 inline float smoothstep( float x, float min, float max )
19 {
20  if( max == min )
21  {
22  x = clamp( ( x - min ), 0.0, 1.0 );
23  }
24  else
25  {
26  x = clamp( ( x - min ) / ( max - min ), 0.0, 1.0 );
27  }
28 
29  return x * x * ( 3 - 2 * x );
30 }
31 
32 
33 // Helper class that lets smoothly animate between its values
34 template < class T > class animated
35 {
36 private:
38  std::chrono::system_clock::time_point _last_update;
39  std::chrono::system_clock::duration _duration;
40 
41 public:
42  animated( T def, std::chrono::system_clock::duration duration = std::chrono::milliseconds( 200 ) )
43  : _duration( duration )
44  , _old( def )
45  , _new( def )
46  {
47  static_assert( ( std::is_arithmetic< T >::value ), "animated class supports arithmetic built-in types only" );
48  _last_update = std::chrono::system_clock::now();
49  }
50  animated & operator=( const T & other )
51  {
52  if( other != _new )
53  {
54  _old = get();
55  _new = other;
56  _last_update = std::chrono::system_clock::now();
57  }
58  return *this;
59  }
60  T get() const
61  {
63  auto ms = std::chrono::duration_cast< std::chrono::microseconds >( now - _last_update ).count();
64  auto duration_ms = std::chrono::duration_cast< std::chrono::microseconds >( _duration ).count();
65  auto t = (float)ms / duration_ms;
66  t = clamp( smoothstep( t, 0.f, 1.f ), 0.f, 1.f );
67  return static_cast< T >( _old * ( 1.f - t ) + _new * t );
68  }
69  operator T() const { return get(); }
70  T value() const { return _new; }
71 };
72 
73 
74 } // namespace rs2
float smoothstep(float x, float min, float max)
Definition: animated.h:18
animated(T def, std::chrono::system_clock::duration duration=std::chrono::milliseconds(200))
Definition: animated.h:42
GLfloat value
Definition: animated.h:9
GLdouble t
GLdouble x
std::chrono::system_clock::time_point _last_update
Definition: animated.h:38
int min(int a, int b)
Definition: lz4s.c:73
float clamp(float x, float min, float max)
Definition: animated.h:13
GLdouble f
GLint GLsizei count
T value() const
Definition: animated.h:70
std::chrono::system_clock::duration _duration
Definition: animated.h:39
animated & operator=(const T &other)
Definition: animated.h:50


librealsense2
Author(s): LibRealSense ROS Team
autogenerated on Thu Dec 22 2022 03:41:41