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:
37  T _old, _new;
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" );
49  }
50  animated & operator=( const T & other )
51  {
52  if( other != _new )
53  {
54  _old = get();
55  _new = other;
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
rs2::animated::get
T get() const
Definition: animated.h:60
min
int min(int a, int b)
Definition: lz4s.c:73
rs2::animated::value
T value() const
Definition: animated.h:70
rs2::smoothstep
float smoothstep(float x, float min, float max)
Definition: animated.h:18
value
GLfloat value
Definition: glad/glad/glad.h:2099
f
GLdouble f
Definition: glad/glad/glad.h:1517
rs2
Definition: animated.h:9
rs2::animated::animated
animated(T def, std::chrono::system_clock::duration duration=std::chrono::milliseconds(200))
Definition: animated.h:42
opencv_pointcloud_viewer.now
now
Definition: opencv_pointcloud_viewer.py:314
rs2::animated::_new
T _new
Definition: animated.h:37
rs2::animated
Definition: animated.h:34
t
GLdouble t
Definition: glad/glad/glad.h:1829
rs2::animated::_duration
std::chrono::system_clock::duration _duration
Definition: animated.h:39
x
GLdouble x
Definition: glad/glad/glad.h:2279
rs2::animated::_last_update
std::chrono::system_clock::time_point _last_update
Definition: animated.h:38
rs2::animated::operator=
animated & operator=(const T &other)
Definition: animated.h:50
rs2::animated::_old
T _old
Definition: animated.h:37
rs2::clamp
float clamp(float x, float min, float max)
Definition: animated.h:13


librealsense2
Author(s): LibRealSense ROS Team
autogenerated on Fri Aug 2 2024 08:30:00