Skip to content

Commit f058342

Browse files
committed
Creates a timer class
1 parent 51a569f commit f058342

File tree

9 files changed

+164
-28
lines changed

9 files changed

+164
-28
lines changed

examples/protonect/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ IF(ENABLE_CXX11)
1616
CHECK_CXX_COMPILER_FLAG("-std=c++0x" COMPILER_SUPPORTS_CXX0X)
1717
IF(COMPILER_SUPPORTS_CXX11)
1818
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
19+
SET(LIBFREENECT2_WITH_CXX11_SUPPORT 1)
1920
ELSEIF(COMPILER_SUPPORTS_CXX0X)
2021
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++0x")
2122
ELSE()
@@ -99,6 +100,7 @@ SET(SOURCES
99100
src/resource.cpp
100101
src/command_transaction.cpp
101102
src/registration.cpp
103+
src/logging.cpp
102104
src/libfreenect2.cpp
103105

104106
${LIBFREENECT2_THREADING_SOURCE}

examples/protonect/include/libfreenect2/config.h.in

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,6 @@
4949

5050
#cmakedefine LIBFREENECT2_OPENCV_FOUND
5151

52+
#cmakedefine LIBFREENECT2_WITH_CXX11_SUPPORT
53+
5254
#endif // LIBFREENECT2_CONFIG_H
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
* This file is part of the OpenKinect Project. http://www.openkinect.org
3+
*
4+
* Copyright (c) 2014 individual OpenKinect contributors. See the CONTRIB file
5+
* for details.
6+
*
7+
* This code is licensed to you under the terms of the Apache License, version
8+
* 2.0, or, at your option, the terms of the GNU General Public License,
9+
* version 2.0. See the APACHE20 and GPL2 files for the text of the licenses,
10+
* or the following URLs:
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
* http://www.gnu.org/licenses/gpl-2.0.txt
13+
*
14+
* If you redistribute this file in source form, modified or unmodified, you
15+
* may:
16+
* 1) Leave this header intact and distribute it under the same terms,
17+
* accompanying it with the APACHE20 and GPL20 files, or
18+
* 2) Delete the Apache 2.0 clause and accompany it with the GPL2 file, or
19+
* 3) Delete the GPL v2 clause and accompany it with the APACHE20 file
20+
* In all cases you must keep the copyright notice intact and include a copy
21+
* of the CONTRIB file.
22+
*
23+
* Binary distributions must follow the binary distribution requirements of
24+
* either License.
25+
*/
26+
27+
#ifndef LOGGING_H_
28+
#define LOGGING_H_
29+
30+
#include <libfreenect2/config.h>
31+
32+
namespace libfreenect2
33+
{
34+
35+
class TimerImpl;
36+
37+
class Timer {
38+
public:
39+
Timer();
40+
virtual ~Timer();
41+
42+
void start();
43+
double stop();
44+
45+
private:
46+
TimerImpl *impl_;
47+
};
48+
49+
} /* namespace libfreenect2 */
50+
51+
#endif /* LOGGING_H_ */

examples/protonect/include/libfreenect2/packet_processor.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
#ifndef PACKET_PROCESSOR_H_
2828
#define PACKET_PROCESSOR_H_
2929

30+
#include <libfreenect2/logging.h>
31+
3032
namespace libfreenect2
3133
{
3234

examples/protonect/src/cpu_depth_packet_processor.cpp

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ class CpuDepthPacketProcessorImpl
219219
double timing_acc;
220220
double timing_acc_n;
221221

222-
double timing_current_start;
222+
Timer timer;
223223

224224
bool enable_bilateral_filter, enable_edge_filter;
225225
DepthPacketProcessor::Parameters params;
@@ -235,7 +235,6 @@ class CpuDepthPacketProcessorImpl
235235

236236
timing_acc = 0.0;
237237
timing_acc_n = 0.0;
238-
timing_current_start = 0.0;
239238

240239
enable_bilateral_filter = true;
241240
enable_edge_filter = true;
@@ -245,15 +244,12 @@ class CpuDepthPacketProcessorImpl
245244

246245
void startTiming()
247246
{
248-
#ifdef LIBFREENECT2_OPENCV_FOUND
249-
timing_current_start = cv::getTickCount();
250-
#endif
247+
timer.start();
251248
}
252249

253250
void stopTiming()
254251
{
255-
#ifdef LIBFREENECT2_OPENCV_FOUND
256-
timing_acc += (cv::getTickCount() - timing_current_start) / cv::getTickFrequency();
252+
timing_acc += timer.stop();
257253
timing_acc_n += 1.0;
258254

259255
if(timing_acc_n >= 100.0)
@@ -263,7 +259,6 @@ class CpuDepthPacketProcessorImpl
263259
timing_acc = 0.0;
264260
timing_acc_n = 0.0;
265261
}
266-
#endif
267262
}
268263

269264
void newIrFrame()

examples/protonect/src/logging.cpp

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
/*
2+
* This file is part of the OpenKinect Project. http://www.openkinect.org
3+
*
4+
* Copyright (c) 2015 individual OpenKinect contributors. See the CONTRIB file
5+
* for details.
6+
*
7+
* This code is licensed to you under the terms of the Apache License, version
8+
* 2.0, or, at your option, the terms of the GNU General Public License,
9+
* version 2.0. See the APACHE20 and GPL2 files for the text of the licenses,
10+
* or the following URLs:
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
* http://www.gnu.org/licenses/gpl-2.0.txt
13+
*
14+
* If you redistribute this file in source form, modified or unmodified, you
15+
* may:
16+
* 1) Leave this header intact and distribute it under the same terms,
17+
* accompanying it with the APACHE20 and GPL20 files, or
18+
* 2) Delete the Apache 2.0 clause and accompany it with the GPL2 file, or
19+
* 3) Delete the GPL v2 clause and accompany it with the APACHE20 file
20+
* In all cases you must keep the copyright notice intact and include a copy
21+
* of the CONTRIB file.
22+
*
23+
* Binary distributions must follow the binary distribution requirements of
24+
* either License.
25+
*/
26+
27+
#include <libfreenect2/logging.h>
28+
29+
#ifdef LIBFREENECT2_WITH_CXX11_SUPPORT
30+
#include <chrono>
31+
#endif
32+
33+
#ifdef LIBFREENECT2_WITH_OPENGL_SUPPORT
34+
#include <GLFW/glfw3.h>
35+
#endif
36+
37+
namespace libfreenect2 {
38+
39+
#ifdef LIBFREENECT2_WITH_CXX11_SUPPORT
40+
class TimerImpl {
41+
public:
42+
void start() {
43+
time_start = std::chrono::high_resolution_clock::now();
44+
}
45+
46+
double stop() {
47+
auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::high_resolution_clock::now() - time_start);
48+
49+
return duration.count()/1000.0;
50+
}
51+
52+
std::chrono::time_point<std::chrono::high_resolution_clock> time_start;
53+
};
54+
#elif defined(LIBFREENECT2_WITH_OPENGL_SUPPORT)
55+
class TimerImpl {
56+
public:
57+
void start() {
58+
time_start = glfwGetTime();
59+
}
60+
61+
double stop() {
62+
return glfwGetTime() - time_start;
63+
}
64+
65+
double time_start;
66+
};
67+
#else
68+
class TimerImpl {
69+
public:
70+
void start() {
71+
}
72+
73+
double stop() {
74+
return 0;
75+
}
76+
};
77+
#endif
78+
79+
Timer::Timer() :
80+
impl_(new TimerImpl()) {
81+
}
82+
83+
Timer::~Timer() {
84+
delete impl_;
85+
}
86+
87+
void Timer::start() {
88+
impl_->start();
89+
}
90+
91+
double Timer::stop() {
92+
return impl_->stop();
93+
}
94+
95+
} /* namespace libfreenect2 */

examples/protonect/src/opencl_depth_packet_processor.cpp

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ class OpenCLDepthPacketProcessorImpl
8383
double timing_acc;
8484
double timing_acc_n;
8585

86-
double timing_current_start;
86+
Timer timer;
8787

8888
Frame *ir_frame, *depth_frame;
8989

@@ -151,7 +151,6 @@ class OpenCLDepthPacketProcessorImpl
151151

152152
timing_acc = 0.0;
153153
timing_acc_n = 0.0;
154-
timing_current_start = 0.0;
155154
image_size = 512 * 424;
156155

157156
deviceInitialized = initDevice(deviceId);
@@ -544,15 +543,12 @@ class OpenCLDepthPacketProcessorImpl
544543

545544
void startTiming()
546545
{
547-
#ifdef LIBFREENECT2_OPENCV_FOUND
548-
timing_current_start = cv::getTickCount();
549-
#endif
546+
timer.start();
550547
}
551548

552549
void stopTiming()
553550
{
554-
#ifdef LIBFREENECT2_OPENCV_FOUND
555-
timing_acc += (cv::getTickCount() - timing_current_start) / cv::getTickFrequency();
551+
timing_acc += timer.stop();
556552
timing_acc_n += 1.0;
557553

558554
if(timing_acc_n >= 100.0)
@@ -562,7 +558,6 @@ class OpenCLDepthPacketProcessorImpl
562558
timing_acc = 0.0;
563559
timing_acc_n = 0.0;
564560
}
565-
#endif
566561
}
567562

568563
void newIrFrame()

examples/protonect/src/opengl_depth_packet_processor.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -370,7 +370,7 @@ struct OpenGLDepthPacketProcessorImpl : public WithOpenGLBindings
370370
double timing_acc;
371371
double timing_acc_n;
372372

373-
double timing_current_start;
373+
Timer timer;
374374

375375
bool do_debug;
376376

@@ -392,7 +392,6 @@ struct OpenGLDepthPacketProcessorImpl : public WithOpenGLBindings
392392
params_need_update(true),
393393
timing_acc(0),
394394
timing_acc_n(0),
395-
timing_current_start(0),
396395
do_debug(debug)
397396
{
398397
}
@@ -447,12 +446,12 @@ struct OpenGLDepthPacketProcessorImpl : public WithOpenGLBindings
447446

448447
void startTiming()
449448
{
450-
timing_current_start = glfwGetTime();
449+
timer.start();
451450
}
452451

453452
void stopTiming()
454453
{
455-
timing_acc += (glfwGetTime() - timing_current_start);
454+
timing_acc += timer.stop();
456455
timing_acc_n += 1.0;
457456

458457
if(timing_acc_n >= 100.0)

examples/protonect/src/turbo_jpeg_rgb_packet_processor.cpp

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ class TurboJpegRgbPacketProcessorImpl
4545
double timing_acc;
4646
double timing_acc_n;
4747

48-
double timing_current_start;
48+
Timer timer;
4949

5050
TurboJpegRgbPacketProcessorImpl()
5151
{
@@ -59,7 +59,6 @@ class TurboJpegRgbPacketProcessorImpl
5959

6060
timing_acc = 0.0;
6161
timing_acc_n = 0.0;
62-
timing_current_start = 0.0;
6362
}
6463

6564
~TurboJpegRgbPacketProcessorImpl()
@@ -80,15 +79,12 @@ class TurboJpegRgbPacketProcessorImpl
8079

8180
void startTiming()
8281
{
83-
#ifdef LIBFREENECT2_OPENCV_FOUND
84-
timing_current_start = cv::getTickCount();
85-
#endif
82+
timer.start();
8683
}
8784

8885
void stopTiming()
8986
{
90-
#ifdef LIBFREENECT2_OPENCV_FOUND
91-
timing_acc += (cv::getTickCount() - timing_current_start) / cv::getTickFrequency();
87+
timing_acc += timer.stop();
9288
timing_acc_n += 1.0;
9389

9490
if(timing_acc_n >= 100.0)
@@ -98,7 +94,6 @@ class TurboJpegRgbPacketProcessorImpl
9894
timing_acc = 0.0;
9995
timing_acc_n = 0.0;
10096
}
101-
#endif
10297
}
10398
};
10499

0 commit comments

Comments
 (0)