CVB++ 15.0
rate_counter.hpp
1#pragma once
2
3#include <memory>
4#include <deque>
5#include <numeric>
6#include <iostream>
7
8#include "../global.hpp"
9#include "utilities.hpp"
10#include "stop_watch.hpp"
11
12namespace Cvb
13{
14
15 CVB_BEGIN_INLINE_NS
16
17 namespace Utilities
18 {
19
21
24 {
25 public:
27 static constexpr int MinimumWindowSize = 1;
28
30 static constexpr int DefaultWindowSize = 25;
31
33 static constexpr int MaximumWindowSize = 65535;
34
36
44
46
51 static std::unique_ptr<RateCounter> Create(int windowSize)
52 {
53 return std::make_unique<RateCounter>(windowSize);
54 }
55
57
64 {
65 return std::make_unique<RateCounter>(windowSize, mode);
66 }
67
70
74 : stopWatch_(Cvb::StopWatchMode::MultiCPU)
75 {
76 }
77
79
83 explicit RateCounter(int windowSize)
84 : stopWatch_(Cvb::StopWatchMode::MultiCPU)
85 , windowSize_(windowSize)
86 {
87 }
88
90
95 RateCounter(int windowSize, StopWatchMode mode)
96 : stopWatch_(mode)
97 , windowSize_(windowSize)
98 {
99 }
100
101 RateCounter(const RateCounter &other) = delete;
102 RateCounter &operator=(const RateCounter &other) = delete;
103 RateCounter(RateCounter &&other) = default; // moving std::deque may throw
104 RateCounter &operator=(RateCounter &&other) = default; // moving std::deque may throw
105 ~RateCounter() = default;
106
108
112 StopWatchMode Mode() const noexcept
113 {
114 return stopWatch_.Mode();
115 }
116
118
122 int WindowSize() const noexcept
123 {
124 return windowSize_;
125 }
126
128
132 void SetWindowSize(int windowSize)
133 {
134 if (windowSize_ == windowSize)
135 return;
136
137 if (windowSize < MinimumWindowSize || windowSize > MaximumWindowSize)
138 throw std::out_of_range("Window size out of range.");
139
140 windowSize_ = windowSize;
141
142 while (static_cast<int>(measurements_.size()) > windowSize_)
143 measurements_.pop_front();
144 }
145
147
152 {
153 double sum = std::accumulate(measurements_.begin(), measurements_.end(), 0.0);
154 if (!measurements_.size())
156
157 auto result = std::round(sum / static_cast<double>(measurements_.size()));
158 return std::chrono::milliseconds(static_cast<std::chrono::milliseconds::rep>(result));
159 }
160
162
166 double Rate() const noexcept
167 {
168 if (measurements_.size() < 2)
170
171 auto averageTimeSpan = AverageTimeSpan();
172 return 1000.0 / static_cast<double>(averageTimeSpan.count());
173 }
174
176
180 int NumSteps() const noexcept
181 {
182 return numSteps_;
183 }
184
186
189 void Reset() noexcept
190 {
191 measurements_.clear();
193 numSteps_ = 0;
194 }
195
197
200 void Step() noexcept
201 {
202 if (std::isnan(lastReading_))
203 {
204 lastReading_ = 0.0;
205 stopWatch_.Start();
206 }
207 else
208 {
209 auto now = static_cast<double>(stopWatch_.TimeSpan().count());
210 measurements_.push_back(now - lastReading_);
211 lastReading_ = now;
212 ++numSteps_;
213
214 while (static_cast<int>(measurements_.size()) > windowSize_)
215 measurements_.pop_front();
216 }
217 }
218
219 private:
220 StopWatch stopWatch_;
221
222 int windowSize_ = DefaultWindowSize;
223
224 std::deque<double> measurements_;
225
226 double lastReading_ = std::numeric_limits<double>::quiet_NaN();
227
228 int numSteps_ = 0;
229 };
230
231 } // namespace Utilities
232
233 CVB_END_INLINE_NS
234
235} // namespace Cvb
T accumulate(T... args)
RateCounter()
Constructor for a rate counter object for multi-CPU architectures with default averaging window size ...
Definition rate_counter.hpp:73
Frame rate measurement counter with selectable averaging window.
Definition rate_counter.hpp:24
RateCounter(int windowSize, StopWatchMode mode)
Constructor for a rate counter object.
Definition rate_counter.hpp:95
static constexpr int MinimumWindowSize
Minimal selectable averaging window size.Trying to select a smaller window will result in an exceptio...
Definition rate_counter.hpp:27
std::chrono::milliseconds AverageTimeSpan() const noexcept
Average time span between two steps or since the creation of the counter or the most recent reset.
Definition rate_counter.hpp:151
void SetWindowSize(int windowSize)
Sets the currently used averaging window size.
Definition rate_counter.hpp:132
static std::unique_ptr< RateCounter > Create(int windowSize)
Creates a frame counter object for multi CPU.
Definition rate_counter.hpp:51
void Reset() noexcept
Erase all measurements so far.
Definition rate_counter.hpp:189
static std::unique_ptr< RateCounter > Create()
Creates a frame counter object for multi CPU with default averaging window (25).
Definition rate_counter.hpp:40
double Rate() const noexcept
Returns the rate(1 / s) at which the steps occurred.
Definition rate_counter.hpp:166
int NumSteps() const noexcept
Number of steps called since construction or since last reset.
Definition rate_counter.hpp:180
static constexpr int MaximumWindowSize
Maximum selectable averaging window size. Trying to select a larger window will result in an exceptio...
Definition rate_counter.hpp:33
void Step() noexcept
Reading step (first reading step will not yield a measurement result, but define the starting point).
Definition rate_counter.hpp:200
RateCounter()
Constructor for a rate counter object for multi-CPU architectures with default averaging window size ...
Definition rate_counter.hpp:73
int WindowSize() const noexcept
Gets the currently used averaging window size.
Definition rate_counter.hpp:122
static std::unique_ptr< RateCounter > Create(int windowSize, StopWatchMode mode)
Creates a frame counter object.
Definition rate_counter.hpp:63
static constexpr int DefaultWindowSize
Default averaging window size.
Definition rate_counter.hpp:30
StopWatchMode Mode() const noexcept
The stop watch mode used internally.
Definition rate_counter.hpp:112
RateCounter(int windowSize)
Constructor for a rate counter object for multi-CPU architectures.
Definition rate_counter.hpp:83
Speed measurement object.
Definition stop_watch.hpp:43
T isnan(T... args)
Namespace for helpers and utilities, which are not directly related to image processing.
Definition version.hpp:16
StopWatchMode
Mode at which the StopWatch should work.
Definition utilities.hpp:28
@ MultiCPU
Definition utilities.hpp:34
Root namespace for the Image Manager interface.
Definition c_bayer_to_rgb.h:17
T quiet_NaN(T... args)
T round(T... args)