Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029 #ifndef UTILSDEFS_H
00030 #error "This file is intended for include from utils_defs.h only!"
00031 #endif
00032
00033
00034
00035 namespace mrpt
00036 {
00037
00038
00039
00040
00041
00042 std::string BASE_IMPEXP format(const char *fmt, ...) MRPT_printf_format_check(1,2);
00043
00044 namespace system
00045 {
00046
00047 std::string BASE_IMPEXP extractFileName(const std::string &filePath);
00048 std::string BASE_IMPEXP stack_trace(bool calling_from_exception);
00049 }
00050
00051 namespace math
00052 {
00053 bool BASE_IMPEXP isNaN(float v) MRPT_NO_THROWS;
00054 bool BASE_IMPEXP isNaN(double v) MRPT_NO_THROWS;
00055 bool BASE_IMPEXP isFinite(float v) MRPT_NO_THROWS;
00056 bool BASE_IMPEXP isFinite(double v) MRPT_NO_THROWS;
00057
00058
00059
00060
00061
00062 template <class MATRIXLIKE>
00063 inline size_t size( const MATRIXLIKE& m, int dim )
00064 {
00065 if (dim==1) return m.getRowCount();
00066 else if (dim==2) return m.getColCount();
00067 else THROW_EXCEPTION_CUSTOM_MSG1("size: Queried matrix dimension must be 1 or 2. Called with i=%i",dim);
00068 }
00069 }
00070
00071 namespace utils
00072 {
00073 class CFileStream;
00074 void BASE_IMPEXP global_profiler_enter(const char *func_name) MRPT_NO_THROWS;
00075 void BASE_IMPEXP global_profiler_leave(const char *func_name) MRPT_NO_THROWS;
00076
00077 struct CProfilerProxy {
00078 const char*f;
00079 CProfilerProxy(const char*func_name) : f(func_name) { global_profiler_enter(f); }
00080 ~CProfilerProxy() { global_profiler_leave(f); }
00081 };
00082
00083
00084 inline double DEG2RAD(const double x) { return x*M_PI/180.0; }
00085
00086 inline float DEG2RAD(const float x) { return x*M_PIf/180.0f; }
00087
00088 inline float DEG2RAD(const int x) { return x*M_PIf/180.0f; }
00089
00090 inline double RAD2DEG(const double x) { return x*180.0/M_PI; }
00091
00092 inline float RAD2DEG(const float x) { return x*180.0f/M_PIf; }
00093
00094 # ifdef HAVE_LONG_DOUBLE
00095
00096 inline long double DEG2RAD(const long double x) { return x*M_PIl/180.0; }
00097
00098 inline long double RAD2DEG(const long double x) { return x*180.0/M_PIl; }
00099 # endif
00100
00101
00102 template <typename T>
00103 inline int sign(T x) { return x<0 ? -1:1; }
00104
00105
00106 template <typename T>
00107 inline int signWithZero(T x) { return x==0?0:sign(x);}
00108
00109
00110 template <typename T>
00111 inline int round(const T value)
00112 {
00113 #if MRPT_HAS_SSE2
00114 __m128d t = _mm_set_sd( value );
00115 return _mm_cvtsd_si32(t);
00116 #elif (defined WIN32 || defined _WIN32) && !defined WIN64 && !defined _WIN64 && defined _MSC_VER
00117 int t;
00118 __asm
00119 {
00120 fld value;
00121 fistp t;
00122 }
00123 return t;
00124 #elif defined HAVE_LRINT || defined __GNUC__
00125 return static_cast<int>(lrint(value));
00126 #else
00127 return static_cast<int>(value + 0.5);
00128 #endif
00129 }
00130
00131
00132 template <typename T>
00133 inline long round_long(const T value)
00134 {
00135 #if MRPT_HAS_SSE2 && MRPT_WORD_SIZE==64
00136 __m128d t = _mm_set_sd( value );
00137 return _mm_cvtsd_si64(t);
00138 #elif (defined WIN32 || defined _WIN32) && !defined WIN64 && !defined _WIN64 && defined _MSC_VER
00139 long t;
00140 __asm
00141 {
00142 fld value;
00143 fistp t;
00144 }
00145 return t;
00146 #elif defined HAVE_LRINT || defined __GNUC__
00147 return lrint(value);
00148 #else
00149 return static_cast<long>(value + 0.5);
00150 #endif
00151 }
00152
00153
00154 template <typename T>
00155 inline int fix(T x) { return x>0 ? static_cast<int>(floor(static_cast<double>(x))) : static_cast<int>(ceil(static_cast<double>(x))) ; }
00156
00157
00158 template<class T>
00159 inline T square(const T x) { return x*x; }
00160
00161
00162 template <class R, class P>
00163 inline R* getAs(stlplus::smart_ptr_clone<P> &o) { return static_cast<R*>( & (*o) ); }
00164
00165
00166 template <class R, class P>
00167 inline const R* getAs(const stlplus::smart_ptr_clone<P> &o) { return static_cast<const R*>( & (*o) ); }
00168
00169
00170 template <class T> inline void reverseBytesInPlace(T& v_in_out)
00171 {
00172 uint8_t *ptr = reinterpret_cast<uint8_t*>(&v_in_out);
00173 std::reverse(ptr,ptr+sizeof(T));
00174 }
00175
00176
00177 template <class T> inline void reverseBytes(const T &v_in, T& v_out)
00178 {
00179 v_out = v_in;
00180 reverseBytesInPlace(v_out);
00181 }
00182
00183
00184
00185 template <typename T,typename K>
00186 inline void keep_min(T &var, const K test_val) {
00187 if (test_val<var) var = test_val;
00188 }
00189
00190
00191 template <typename T,typename K>
00192 inline void keep_max(T &var, const K test_val) {
00193 if (test_val>var) var = test_val;
00194 }
00195
00196
00197 template <class T>
00198 void delete_safe(T *& ptr) {
00199 if (ptr) {
00200 delete ptr;
00201 ptr = NULL;
00202 }
00203 }
00204
00205 }
00206 }
00207