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