Point Cloud Library (PCL)  1.6.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
time.h
Go to the documentation of this file.
1 /*
2  * Software License Agreement (BSD License)
3  *
4  * Point Cloud Library (PCL) - www.pointclouds.org
5  * Copyright (c) 2010-2012, Willow Garage, Inc.
6  *
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * * Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  * * Redistributions in binary form must reproduce the above
16  * copyright notice, this list of conditions and the following
17  * disclaimer in the documentation and/or other materials provided
18  * with the distribution.
19  * * Neither the name of Willow Garage, Inc. nor the names of its
20  * contributors may be used to endorse or promote products derived
21  * from this software without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
26  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
27  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
28  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
29  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
31  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
33  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34  * POSSIBILITY OF SUCH DAMAGE.
35  *
36  */
37 
38 #ifndef PCL_TIME_H_
39 #define PCL_TIME_H_
40 
41 #include <cmath>
42 #include <string>
43 #include <boost/date_time/posix_time/posix_time.hpp>
44 
52 namespace pcl
53 {
54 
58  class StopWatch
59  {
60  public:
62  StopWatch () : start_time_ (boost::posix_time::microsec_clock::local_time ())
63  {
64  }
65 
67  virtual ~StopWatch () {}
68 
70  inline double
72  {
73  boost::posix_time::ptime end_time = boost::posix_time::microsec_clock::local_time ();
74  return (static_cast<double> (((end_time - start_time_).total_milliseconds ())));
75  }
76 
78  inline double
80  {
81  return (getTime () * 0.001f);
82  }
83 
85  inline void
86  reset ()
87  {
88  start_time_ = boost::posix_time::microsec_clock::local_time ();
89  }
90 
91  protected:
92  boost::posix_time::ptime start_time_;
93  };
94 
110  class ScopeTime : public StopWatch
111  {
112  public:
113  inline ScopeTime (const char* title) :
114  title_ (std::string (title))
115  {
116  start_time_ = boost::posix_time::microsec_clock::local_time ();
117  }
118 
119  inline ScopeTime () :
120  title_ (std::string (""))
121  {
122  start_time_ = boost::posix_time::microsec_clock::local_time ();
123  }
124 
125  inline ~ScopeTime ()
126  {
127  double val = this->getTime ();
128  std::cerr << title_ << " took " << val << "ms.\n";
129  }
130 
131  private:
132  std::string title_;
133  };
134 
135 
136 #ifndef MEASURE_FUNCTION_TIME
137 #define MEASURE_FUNCTION_TIME \
138  ScopeTime scopeTime(__func__)
139 #endif
140 
141 inline double
143 {
144  boost::posix_time::ptime epoch_time (boost::gregorian::date (1970, 1, 1));
145  boost::posix_time::ptime current_time = boost::posix_time::microsec_clock::local_time ();
146  return (static_cast<double>((current_time - epoch_time).total_nanoseconds ()) * 1.0e-9);
147 }
148 
150 #ifndef DO_EVERY_TS
151 #define DO_EVERY_TS(secs, currentTime, code) \
152 if (1) {\
153  static double s_lastDone_ = 0.0; \
154  double s_now_ = (currentTime); \
155  if (s_lastDone_ > s_now_) \
156  s_lastDone_ = s_now_; \
157  if ((s_now_ - s_lastDone_) > (secs)) { \
158  code; \
159  s_lastDone_ = s_now_; \
160  }\
161 } else \
162  (void)0
163 #endif
164 
166 #ifndef DO_EVERY
167 #define DO_EVERY(secs, code) \
168  DO_EVERY_TS(secs, pcl::getTime(), code)
169 #endif
170 
171 } // end namespace
174 #endif //#ifndef PCL_NORMS_H_