Visual Servoing Platform version 3.7.0
Loading...
Searching...
No Matches
vpRBTrackingTimings.h
1/*
2 * ViSP, open source Visual Servoing Platform software.
3 * Copyright (C) 2005 - 2024 by Inria. All rights reserved.
4 *
5 * This software is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 * See the file LICENSE.txt at the root directory of this source
10 * distribution for additional information about the GNU GPL.
11 *
12 * For using ViSP with software that can not be combined with the GNU
13 * GPL, please contact Inria about acquiring a ViSP Professional
14 * Edition License.
15 *
16 * See https://visp.inria.fr for more information.
17 *
18 * This software was developed at:
19 * Inria Rennes - Bretagne Atlantique
20 * Campus Universitaire de Beaulieu
21 * 35042 Rennes Cedex
22 * France
23 *
24 * If you have questions regarding the use of this file, please contact
25 * Inria at visp@inria.fr
26 *
27 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
28 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
29 */
30
35#ifndef VP_RB_TRACKING_TIMINGS_H
36#define VP_RB_TRACKING_TIMINGS_H
37
38#include <iomanip>
39#include <map>
40
41#include <visp3/core/vpConfig.h>
42#include <visp3/core/vpMath.h>
43#include <visp3/core/vpTime.h>
44
45
46#if defined(VISP_HAVE_NLOHMANN_JSON)
47#include VISP_NLOHMANN_JSON(json.hpp)
48#endif
49
63
65
66std::ostream &operator<<(std::ostream &s, const vpRBTrackingTimings &I);
67
68class VISP_EXPORT vpRBTrackingTimings
69{
70public:
71 inline void reset()
72 {
73 m_renderTime = 0.0;
74 m_silhouetteExtractionTime = 0.0;
75 m_odometryTime = 0.0;
76 m_driftTime = 0.0;
77
78 m_trackerIterStartTime.clear();
79 m_trackerFeatureExtractionTime.clear();
80
81 m_trackerFeatureTrackingTime.clear();
82 m_trackerVVSIterTimes.clear();
83 }
84
85 friend std::ostream &operator<<(std::ostream &, const vpRBTrackingTimings &);
86
87 void startTimer() { m_startTime = vpTime::measureTimeMs(); }
88 inline double endTimer()
89 {
90 if (m_startTime < 0.f) throw vpException(vpException::notInitialized, "Tried to query timer without starting it.");
91 double elapsed = vpTime::measureTimeMs() - m_startTime;
92 m_startTime = -1.f;
93 return elapsed;
94 }
95
96 inline void setRenderTime(double elapsed) { m_renderTime = elapsed; }
97 inline void setSilhouetteTime(double elapsed) { m_silhouetteExtractionTime = elapsed; }
98 inline void setMaskTime(double elapsed) { m_maskTime = elapsed; }
99
100 inline void insertTrackerTime(std::map<int, std::vector<double>> &map, int id, double elapsed)
101 {
102 if (map.find(id) == map.end()) {
103 map.insert(std::make_pair(id, std::vector<double>()));
104 }
105 map.find(id)->second.push_back(elapsed);
106 }
107 inline void addTrackerVVSTime(int id, double elapsed)
108 {
109 insertTrackerTime(m_trackerVVSIterTimes, id, elapsed);
110 }
111
112 inline void setTrackerIterStartTime(int id, double elapsed)
113 {
114 m_trackerIterStartTime[id] = elapsed;
115 }
116
117 inline void setTrackerFeatureExtractionTime(int id, double elapsed)
118 {
119 m_trackerFeatureExtractionTime[id] = elapsed;
120 }
121
122 inline void setTrackerFeatureTrackingTime(int id, double elapsed)
123 {
124 m_trackerFeatureTrackingTime[id] = elapsed;
125 }
126
127 inline void setInitVVSTime(int id, double elapsed)
128 {
129 m_trackerInitVVSTime[id] = elapsed;
130 }
131
132 inline void setDriftDetectionTime(double elapsed)
133 {
134 m_driftTime = elapsed;
135 }
136
137 inline void setOdometryTime(double elapsed)
138 {
139 m_odometryTime = elapsed;
140 }
141
142#ifdef VISP_HAVE_NLOHMANN_JSON
143 inline friend void from_json(const nlohmann::json &j, vpRBTrackingTimings &result);
144 inline friend void to_json(nlohmann::json &j, const vpRBTrackingTimings &result);
145
146#endif
147
148private:
149 double m_startTime;
150
151 double m_renderTime;
152 double m_silhouetteExtractionTime;
153 double m_maskTime;
154 double m_driftTime;
155 double m_odometryTime;
156
157 std::map<int, std::vector<double>> m_trackerVVSIterTimes;
158 std::map<int, double> m_trackerIterStartTime;
159 std::map<int, double> m_trackerFeatureExtractionTime;
160 std::map<int, double> m_trackerFeatureTrackingTime;
161 std::map<int, double> m_trackerInitVVSTime;
162
163};
164
165inline std::ostream &operator<<(std::ostream &out, const vpRBTrackingTimings &timer)
166{
167 const auto default_precision { out.precision() };
168 auto flags = out.flags();
169 out << std::setprecision(2) << std::fixed;
170 out << "====================================================" << std::endl;
171 out << "Render: " << timer.m_renderTime << "ms" << std::endl;
172 out << "Mask: " << timer.m_maskTime << "ms" << std::endl;
173 out << "Drift: " << timer.m_driftTime << "ms" << std::endl;
174 out << "Odometry: " << timer.m_odometryTime << "ms" << std::endl;
175 out << "Silhouette extraction: " << timer.m_silhouetteExtractionTime << "ms" << std::endl;
176
177 out << "Trackers: " << std::endl;
178 for (const std::pair<const int, std::vector<double>> &vvsIterData : timer.m_trackerVVSIterTimes) {
179 double trackingStartTime = timer.m_trackerIterStartTime.find(vvsIterData.first)->second;
180 double featTrackTime = timer.m_trackerFeatureTrackingTime.find(vvsIterData.first)->second;
181 double featExtractionTime = timer.m_trackerFeatureExtractionTime.find(vvsIterData.first)->second;
182 double initVVSTime = timer.m_trackerInitVVSTime.find(vvsIterData.first)->second;
183
184 double ttVVSIter = 0.f;
185 for (double v : vvsIterData.second) {
186 ttVVSIter += v;
187 }
188 out << "\t" << vvsIterData.first << std::endl;
189 out << "\t" << "\t" << "Tracking initialization: " << trackingStartTime << "ms" << std::endl;
190 out << "\t" << "\t" << "Feature extraction: " << featExtractionTime << "ms" << std::endl;
191 out << "\t" << "\t" << "Feature tracking: " << featTrackTime << "ms" << std::endl;
192 out << "\t" << "\t" << "VVS init: " << initVVSTime << "ms" << std::endl;
193 out << "\t" << "\t" << "VVS: " << ttVVSIter << "ms (" << vpMath::getMean(vvsIterData.second) << "ms"
194 << "+-" << vpMath::getStdev(vvsIterData.second) << "ms)" << std::endl;
195 }
196 out << "====================================================" << std::endl;
197 out.flags(flags);
198 out << std::setprecision(default_precision); // restore defaults
199 return out;
200}
201
202#ifdef VISP_HAVE_NLOHMANN_JSON
203inline void from_json(const nlohmann::json &j, vpRBTrackingTimings &result)
204{
205 result.m_renderTime = j.at("render");
206 result.m_silhouetteExtractionTime = j.at("silhouetteExtraction");
207 result.m_maskTime = j.at("mask");
208 result.m_driftTime = j.at("drift");
209 result.m_odometryTime = j.at("odometry");
210
211 nlohmann::json jf = j.at("features");
212 result.m_trackerVVSIterTimes = jf.at("vvs");
213 result.m_trackerIterStartTime = jf.at("start");
214 result.m_trackerFeatureExtractionTime = jf.at("extraction");
215 result.m_trackerFeatureTrackingTime = jf.at("tracking");
216 result.m_trackerInitVVSTime = jf.at("vvsInit");
217}
218inline void to_json(nlohmann::json &j, const vpRBTrackingTimings &result)
219{
220 j["render"] = result.m_renderTime;
221 j["silhouetteExtraction"] = result.m_silhouetteExtractionTime;
222 j["mask"] = result.m_maskTime;
223 j["drift"] = result.m_driftTime;
224 j["odometry"] = result.m_odometryTime;
225 nlohmann::json jf;
226 jf["vvs"] = result.m_trackerVVSIterTimes;
227 jf["start"] = result.m_trackerIterStartTime;
228 jf["extraction"] = result.m_trackerFeatureExtractionTime;
229 jf["tracking"] = result.m_trackerFeatureTrackingTime;
230 jf["vvsInit"] = result.m_trackerInitVVSTime;
231 j["features"] = jf;
232}
233#endif
234
235
236END_VISP_NAMESPACE
237
238#endif
error that can be emitted by ViSP classes.
Definition vpException.h:60
@ notInitialized
Used to indicate that a parameter is not initialized.
Definition vpException.h:74
static double getStdev(const std::vector< double > &v, bool useBesselCorrection=false)
Definition vpMath.cpp:374
static double getMean(const std::vector< double > &v)
Definition vpMath.cpp:323
void setSilhouetteTime(double elapsed)
void setDriftDetectionTime(double elapsed)
friend std::ostream & operator<<(std::ostream &, const vpRBTrackingTimings &)
friend void to_json(nlohmann::json &j, const vpRBTrackingTimings &result)
void setTrackerFeatureTrackingTime(int id, double elapsed)
void addTrackerVVSTime(int id, double elapsed)
void setTrackerIterStartTime(int id, double elapsed)
void setMaskTime(double elapsed)
void setInitVVSTime(int id, double elapsed)
void insertTrackerTime(std::map< int, std::vector< double > > &map, int id, double elapsed)
friend void from_json(const nlohmann::json &j, vpRBTrackingTimings &result)
void setRenderTime(double elapsed)
void setTrackerFeatureExtractionTime(int id, double elapsed)
void setOdometryTime(double elapsed)
VISP_EXPORT double measureTimeMs()