plotdata.h
Go to the documentation of this file.
1 #ifndef PLOTDATA_RAW_H
2 #define PLOTDATA_RAW_H
3 
4 #include <vector>
5 #include <memory>
6 #include <string>
7 #include <map>
8 #include <mutex>
9 #include <deque>
10 #include "PlotJuggler/optional.hpp"
11 #include "PlotJuggler/any.hpp"
12 #include <QDebug>
13 #include <QColor>
14 #include <type_traits>
15 #include <cmath>
16 #include <cstdlib>
17 #include <unordered_map>
18 
19 inline double Abs(double val)
20 {
21  return val < 0 ? -val : val;
22 }
23 
24 template <typename Time, typename Value> class PlotDataGeneric
25 {
26 public:
27 
28  struct RangeTime{
29  Time min;
30  Time max;
31  };
32 
33  struct RangeValue{
34  Value min;
35  Value max;
36  };
37 
40 
41  class Point{
42  public:
43  Time x;
44  Value y;
45  Point( Time _x, Value _y):
46  x(_x), y(_y) {}
47  Point() = default;
48  };
49 
50  enum{
51  MAX_CAPACITY = 1024*1024,
53  };
54 
55  typedef Time TimeType;
56 
57  typedef Value ValueType;
58 
59  typedef typename std::deque<Point>::iterator Iterator;
60 
61  typedef typename std::deque<Point>::const_iterator ConstIterator;
62 
63  PlotDataGeneric(const std::string& name);
64 
65  PlotDataGeneric( const PlotDataGeneric<Time,Value>& other) = delete;
66 
68  {
69  _name = std::move(other._name);
70  _points = std::move(other._points);
71  _color_hint = std::move(other._color_hint);
72  _max_range_X = other._max_range_X;
73  }
74 
76  {
77  std::swap(_points, other._points);
78  }
79 
81 
82  virtual ~PlotDataGeneric() {}
83 
84  const std::string& name() const { return _name; }
85 
86  virtual size_t size() const;
87 
88  int getIndexFromX(Time x) const;
89 
90  nonstd::optional<Value> getYfromX(Time x ) const;
91 
92  const Point &at(size_t index) const;
93 
94  Point &at(size_t index);
95 
96  const Point& operator[](size_t index) const { return at(index); }
97 
98  Point& operator[](size_t index) { return at(index); }
99 
100  void clear();
101 
102  void pushBack(Point p);
103 
104  QColor getColorHint() const;
105 
106  void setColorHint(QColor color);
107 
108  void setMaximumRangeX(Time max_range);
109 
110  Time maximumRangeX() const { return _max_range_X; }
111 
112  const Point& front() const { return _points.front(); }
113 
114  const Point& back() const { return _points.back(); }
115 
116  ConstIterator begin() const { return _points.begin(); }
117 
118  ConstIterator end() const { return _points.end(); }
119 
120  Iterator begin() { return _points.begin(); }
121 
122  Iterator end() { return _points.end(); }
123 
124  void resize(size_t new_size) { _points.resize(new_size); }
125 
126  void popFront() { _points.pop_front(); }
127 
128 protected:
129 
130  std::string _name;
131  std::deque<Point> _points;
132  QColor _color_hint;
133 
134 private:
136 };
137 
138 
141 
142 
143 typedef struct{
144  std::unordered_map<std::string, PlotData> numeric;
145  std::unordered_map<std::string, PlotDataAny> user_defined;
146 
147  std::unordered_map<std::string, PlotData>::iterator addNumeric(const std::string& name)
148  {
149  return numeric.emplace( std::piecewise_construct,
150  std::forward_as_tuple(name),
151  std::forward_as_tuple(name)
152  ).first;
153  }
154 
155 
156  std::unordered_map<std::string, PlotDataAny>::iterator addUserDefined(const std::string& name)
157  {
158  return user_defined.emplace( std::piecewise_construct,
159  std::forward_as_tuple(name),
160  std::forward_as_tuple(name)
161  ).first;
162  }
163 
165 
166 
167 //-----------------------------------
168 template<typename Value>
169 inline void AddPrefixToPlotData(const std::string &prefix, std::unordered_map<std::string, Value>& data)
170 {
171  if( prefix.empty() ) return;
172 
173  std::unordered_map<std::string, Value> temp;
174 
175  for(auto& it: data)
176  {
177  std::string key;
178  key.reserve( prefix.size() + 2 + it.first.size() );
179  if( it.first.front() == '/' )
180  {
181  key = prefix + it.first;
182  }
183  else{
184  key = prefix + "/" + it.first;
185  }
186 
187  auto new_plot = temp.emplace( std::piecewise_construct,
188  std::forward_as_tuple(key),
189  std::forward_as_tuple(key) ).first;
190 
191  new_plot->second.swapData( it.second );
192  }
193  std::swap(data, temp);
194 }
195 
196 //template < typename Time, typename Value>
197 //inline PlotDataGeneric<Time, Value>::PlotDataGeneric():
198 // _max_range_X( std::numeric_limits<Time>::max() )
199 // , _color_hint(Qt::black)
200 //{
201 // static_assert( std::is_arithmetic<Time>::value ,"Only numbers can be used as time");
202 //}
203 
204 template<typename Time, typename Value>
206  _max_range_X( std::numeric_limits<Time>::max() )
207  , _color_hint(Qt::black)
208  , _name(name)
209 {
210  static_assert( std::is_arithmetic<Time>::value ,"Only numbers can be used as time");
211 }
212 
213 template < typename Time, typename Value>
215 {
216  _points.push_back( point );
217 
218  while( _points.size()>2 &&
219  (_points.back().x - _points.front().x) > _max_range_X)
220  {
221  _points.pop_front();
222  }
223 }
224 
225 template <> // template specialization
227 {
228  if( std::isinf( point.y ) || std::isnan( point.y ) )
229  {
230  return; // skip
231  }
232  _points.push_back( point );
233 
234  while( _points.size()>2 &&
235  (_points.back().x - _points.front().x) > _max_range_X)
236  {
237  _points.pop_front();
238  }
239 }
240 
241 template < typename Time, typename Value>
243 {
244  if( _points.size() == 0 ){
245  return -1;
246  }
247  auto lower = std::lower_bound(_points.begin(), _points.end(), Point(x,0),
248  [](const Point &a, const Point &b)
249  { return a.x < b.x; } );
250  auto index = std::distance( _points.begin(), lower);
251 
252  if( index >= _points.size() )
253  {
254  return _points.size() -1;
255  }
256  if( index < 0)
257  {
258  return 0;
259  }
260 
261  if( index > 0)
262  {
263  if( Abs( _points[index-1].x - x) < Abs( _points[index].x - x) )
264  {
265  return index-1;
266  }
267  else{
268  return index;
269  }
270  }
271  return index;
272 }
273 
274 
275 template < typename Time, typename Value>
277 {
278  int index = getIndexFromX( x );
279  if( index == -1 )
280  {
281  return nonstd::optional<Value>();
282  }
283  return _points.at(index).y;
284 }
285 
286 template < typename Time, typename Value>
287 inline const typename PlotDataGeneric<Time, Value>::Point&
289 {
290  return _points[index];
291 }
292 
293 template < typename Time, typename Value>
296 {
297  return _points[index];
298 }
299 
300 template<typename Time, typename Value>
302 {
303  _points.clear();
304 }
305 
306 
307 template < typename Time, typename Value>
309 {
310  return _points.size();
311 }
312 
313 template < typename Time, typename Value>
315 {
316  return _color_hint;
317 }
318 
319 template < typename Time, typename Value>
321 {
322  _color_hint = color;
323 }
324 
325 
326 template < typename Time, typename Value>
328 {
329  _max_range_X = max_range;
330  while( _points.size()>2 &&
331  _points.back().x - _points.front().x > _max_range_X)
332  {
333  _points.pop_front();
334  }
335 }
336 
337 #endif // PLOTDATA_H
std::deque< Point >::const_iterator ConstIterator
Definition: plotdata.h:61
void swap(any &x, any &y) any_noexcept
Definition: any.hpp:406
std::unordered_map< std::string, PlotData > numeric
Definition: plotdata.h:144
Iterator end()
Definition: plotdata.h:122
const Point & operator[](size_t index) const
Definition: plotdata.h:96
void setMaximumRangeX(Time max_range)
Definition: plotdata.h:327
PlotDataGeneric(const std::string &name)
Definition: plotdata.h:205
const Point & at(size_t index) const
Definition: plotdata.h:288
void pushBack(Point p)
Definition: plotdata.h:214
virtual ~PlotDataGeneric()
Definition: plotdata.h:82
int getIndexFromX(Time x) const
Definition: plotdata.h:242
Point(Time _x, Value _y)
Definition: plotdata.h:45
std::deque< Point > _points
Definition: plotdata.h:131
QColor _color_hint
Definition: plotdata.h:132
std::unordered_map< std::string, PlotDataAny >::iterator addUserDefined(const std::string &name)
Definition: plotdata.h:156
const std::string & name() const
Definition: plotdata.h:84
std::unordered_map< std::string, PlotDataAny > user_defined
Definition: plotdata.h:145
PlotDataGeneric & operator=(const PlotDataGeneric< Time, Value > &other)=delete
std::string _name
Definition: plotdata.h:130
double Abs(double val)
Definition: plotdata.h:19
void swapData(PlotDataGeneric< Time, Value > &other)
Definition: plotdata.h:75
ConstIterator begin() const
Definition: plotdata.h:116
void resize(size_t new_size)
Definition: plotdata.h:124
Time _max_range_X
Definition: plotdata.h:135
void popFront()
Definition: plotdata.h:126
Point & operator[](size_t index)
Definition: plotdata.h:98
Value ValueType
Definition: plotdata.h:57
const Point & front() const
Definition: plotdata.h:112
void clear()
Definition: plotdata.h:301
TFSIMD_FORCE_INLINE const tfScalar & y() const
TFSIMD_FORCE_INLINE const tfScalar & x() const
Iterator begin()
Definition: plotdata.h:120
std::deque< Point >::iterator Iterator
Definition: plotdata.h:59
virtual size_t size() const
Definition: plotdata.h:308
QColor getColorHint() const
Definition: plotdata.h:314
nonstd::optional< RangeValue > RangeValueOpt
Definition: plotdata.h:39
nonstd::optional< Value > getYfromX(Time x) const
Definition: plotdata.h:276
std::unordered_map< std::string, PlotData >::iterator addNumeric(const std::string &name)
Definition: plotdata.h:147
void AddPrefixToPlotData(const std::string &prefix, std::unordered_map< std::string, Value > &data)
Definition: plotdata.h:169
PlotDataGeneric< double, nonstd::any > PlotDataAny
Definition: plotdata.h:140
const Point & back() const
Definition: plotdata.h:114
uint64_t b
void setColorHint(QColor color)
Definition: plotdata.h:320
PlotDataGeneric(PlotDataGeneric &&other)
Definition: plotdata.h:67
nonstd::optional< RangeTime > RangeTimeOpt
Definition: plotdata.h:38
PlotDataGeneric< double, double > PlotData
Definition: plotdata.h:139
int a
Time maximumRangeX() const
Definition: plotdata.h:110
ConstIterator end() const
Definition: plotdata.h:118


plotjuggler
Author(s): Davide Faconti
autogenerated on Sat Jul 6 2019 03:44:17