FPSCounter.h
Go to the documentation of this file.
1 /*
2  * This file is part of ArmarX.
3  *
4  * Copyright (C) 2011-2016, High Performance Humanoid Technologies (H2T), Karlsruhe Institute of Technology (KIT), all rights reserved.
5  *
6  * ArmarX is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation.
9  *
10  * ArmarX is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  *
18  * @package VisionX::Tools
19  * @author Kai Welke (kai dot welke at kit dot edu)
20  * @date 2011
21  * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
22  * GNU General Public License
23  */
24 
25 #pragma once
26 
27 namespace visionx
28 {
29  ///////////////////////////////////////////
30  // definition of class FPSCounter
31  ///////////////////////////////////////////
32  /**
33  * The FPSCounter class provides methods for calculating the frames per second (FPS) count
34  * in periodic tasks. Further synchronization to a given FPS is supported.
35  */
36  class FPSCounter
37  {
38  public:
39  /**
40  * Constructs a new FPS counter
41  *
42  * @param nDelayFrames number of frames to use for FPS calculation. The
43  * FPS calculation will be delayed by this amount of frames
44  */
45  FPSCounter(int nDelayFrames = 10);
46 
47  ///////////////////////////////////////////
48  // control
49  ///////////////////////////////////////////
50  /**
51  * Resets the FPS counter to its initial state
52  */
53  void reset();
54 
55  /**
56  * Updates the FPS counter
57  *
58  * Using this method in a periodic task will measure the time elapsed from one call
59  * to the next. Based on this time, the FPS is calculated.
60  */
61  void update();
62 
63  /**
64  * recalculates the FPS statistics
65  */
66  void recalculate();
67 
68  /**
69  * Synchronize to FPS
70  *
71  * Using this method in a periodic task will synchronize the task to the
72  * specified FPS.
73  * Update is called internally, so do not call update at a different position.
74  *
75  * @param fFrameRate frames per second to use for synchronization
76  */
77  void assureFPS(float fFrameRate);
78 
79  ///////////////////////////////////////////
80  // getter
81  ///////////////////////////////////////////
82  /**
83  * Get if calculated values are valid
84  *
85  * Calculated values are valid if the minimum amount of delay frames has passed (m_nDelayFrames, see constructor)
86  * @return calculated values valid
87  */
88  bool getValid();
89 
90  /**
91  * Get frames per second
92  *
93  * @return frames per second measure over the last nDelayTime frames (see constructor)
94  */
95  float getFPS();
96 
97  /**
98  * Get number of updates
99  *
100  * @return number of updates or assureFPS calls performed since start or last reset
101  */
102  int getUpdates();
103 
104  /**
105  * Get mean cycle time over last 10 frames
106  *
107  * @return mean cycle time in ms
108  */
109  float getMeanCycleTimeMS();
110 
111  /**
112  * Get minimum cycle time since start
113  *
114  * @return minimum cycle time in ms
115  */
116  float getMinCycleTimeMS();
117 
118  /**
119  * Get maximum cycle time since start
120  *
121  * @return maximum cycle time in ms
122  */
123  float getMaxCycleTimeMS();
124 
125  private:
126  // private methods
127  void updateMembers(int nCycleTime);
128  void recalculateFPS(int nCycleTime);
129  void recalculateStats(int nCycleTime);
130 
131  int calculateTimeDiff(long& nSec, long& nUSec, bool bSetTime = false);
132 
133  // settings
134  int m_nDelayFrames;
135 
136  // times
137  long m_nCycleSec;
138  long m_nCycleUSec;
139  int m_nIntervalTime;
140 
141  // results
142  int m_nUpdates;
143  float m_fFPS;
144  float m_fMinCycleTimeMS;
145  float m_fMaxCycleTimeMS;
146  float m_fLastCycleTimesMS[10];
147  };
148 }
149 
visionx::FPSCounter::getMinCycleTimeMS
float getMinCycleTimeMS()
Get minimum cycle time since start.
Definition: FPSCounter.cpp:154
visionx
ArmarX headers.
Definition: OpenPoseStressTest.h:38
visionx::FPSCounter::getFPS
float getFPS()
Get frames per second.
Definition: FPSCounter.cpp:127
visionx::FPSCounter::FPSCounter
FPSCounter(int nDelayFrames=10)
Constructs a new FPS counter.
Definition: FPSCounter.cpp:38
visionx::FPSCounter::getMeanCycleTimeMS
float getMeanCycleTimeMS()
Get mean cycle time over last 10 frames.
Definition: FPSCounter.cpp:137
visionx::FPSCounter::update
void update()
Updates the FPS counter.
Definition: FPSCounter.cpp:60
visionx::FPSCounter::getUpdates
int getUpdates()
Get number of updates.
Definition: FPSCounter.cpp:122
visionx::FPSCounter::reset
void reset()
Resets the FPS counter to its initial state.
Definition: FPSCounter.cpp:49
visionx::FPSCounter::getValid
bool getValid()
Get if calculated values are valid.
Definition: FPSCounter.cpp:117
visionx::FPSCounter::getMaxCycleTimeMS
float getMaxCycleTimeMS()
Get maximum cycle time since start.
Definition: FPSCounter.cpp:164
visionx::FPSCounter::assureFPS
void assureFPS(float fFrameRate)
Synchronize to FPS.
Definition: FPSCounter.cpp:82
visionx::FPSCounter
The FPSCounter class provides methods for calculating the frames per second (FPS) count in periodic t...
Definition: FPSCounter.h:36
visionx::FPSCounter::recalculate
void recalculate()
recalculates the FPS statistics
Definition: FPSCounter.cpp:72