Point Cloud Library (PCL)  1.5.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
time.h
Go to the documentation of this file.
00001 /*
00002  * Software License Agreement (BSD License)
00003  *
00004  *  Point Cloud Library (PCL) - www.pointclouds.org
00005  *  Copyright (c) 2010-2011, Willow Garage, Inc.
00006  *
00007  *  All rights reserved.
00008  *
00009  *  Redistribution and use in source and binary forms, with or without
00010  *  modification, are permitted provided that the following conditions
00011  *  are met:
00012  *
00013  *   * Redistributions of source code must retain the above copyright
00014  *     notice, this list of conditions and the following disclaimer.
00015  *   * Redistributions in binary form must reproduce the above
00016  *     copyright notice, this list of conditions and the following
00017  *     disclaimer in the documentation and/or other materials provided
00018  *     with the distribution.
00019  *   * Neither the name of Willow Garage, Inc. nor the names of its
00020  *     contributors may be used to endorse or promote products derived
00021  *     from this software without specific prior written permission.
00022  *
00023  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00024  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00025  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00026  *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00027  *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00028  *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00029  *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00030  *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00031  *  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00032  *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00033  *  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00034  *  POSSIBILITY OF SUCH DAMAGE.
00035  *
00036  */
00037 
00038 #ifndef PCL_TIME_H_
00039 #define PCL_TIME_H_
00040 
00041 #include <cmath>
00042 #include <string>
00043 #include <boost/date_time/posix_time/posix_time.hpp>
00044 
00052 namespace pcl
00053 {
00054 
00058   class StopWatch
00059   {
00060     public:
00061       StopWatch ()
00062       {
00063         start_time_ = boost::posix_time::microsec_clock::local_time ();
00064       }
00065 
00067       inline double
00068       getTime ()
00069       {
00070         boost::posix_time::ptime end_time = boost::posix_time::microsec_clock::local_time ();
00071         return ((double) ((end_time - start_time_).total_milliseconds ()));
00072       }
00073 
00075       inline double
00076       getTimeSeconds ()
00077       {
00078         return (getTime () * 0.001f);
00079       }
00080 
00082       inline void
00083       reset ()
00084       {
00085         start_time_ = boost::posix_time::microsec_clock::local_time ();
00086       }
00087 
00088     protected:
00089       boost::posix_time::ptime start_time_;
00090   };
00091 
00107   class ScopeTime : public StopWatch
00108   {
00109     public:
00110       inline ScopeTime (const char* title)
00111       {
00112         title_ = std::string (title);
00113         start_time_ = boost::posix_time::microsec_clock::local_time ();
00114       }
00115 
00116       inline ScopeTime ()
00117       {
00118         start_time_ = boost::posix_time::microsec_clock::local_time ();
00119       }
00120 
00121       inline ~ScopeTime ()
00122       {
00123         double val = this->getTime ();
00124         std::cerr << title_ << " took " << val << "ms.\n";
00125       }
00126 
00127     private:
00128       std::string title_;
00129   };
00130 
00131 
00132 #ifndef MEASURE_FUNCTION_TIME
00133 #define MEASURE_FUNCTION_TIME \
00134   ScopeTime scopeTime(__func__)
00135 #endif
00136 
00137 inline double 
00138 getTime ()
00139 {
00140   boost::posix_time::ptime epoch_time (boost::gregorian::date (1970, 1, 1));
00141   boost::posix_time::ptime current_time = boost::posix_time::microsec_clock::local_time ();
00142   return (current_time - epoch_time).total_nanoseconds () * 1.0e-9;
00143 }
00144 
00146 #ifndef DO_EVERY_TS
00147 #define DO_EVERY_TS(secs, currentTime, code) \
00148 if (1) {\
00149   static double s_lastDone_ = 0.0; \
00150   double s_now_ = (currentTime); \
00151   if (s_lastDone_ > s_now_) \
00152     s_lastDone_ = s_now_; \
00153   if ((s_now_ - s_lastDone_) > (secs)) {        \
00154     code; \
00155     s_lastDone_ = s_now_; \
00156   }\
00157 } else \
00158   (void)0
00159 #endif
00160 
00162 #ifndef DO_EVERY
00163 #define DO_EVERY(secs, code) \
00164   DO_EVERY_TS(secs, pcl::getTime(), code)
00165 #endif
00166 
00167 }  // end namespace
00170 #endif  //#ifndef PCL_NORMS_H_