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 #ifndef UTILSDEFS_H
00029 #define UTILSDEFS_H
00030
00031
00032
00033
00034 #if defined(_MSC_VER)
00035
00036 #pragma warning(disable:4786)
00037
00038 #pragma warning(disable:4702)
00039
00040 #pragma warning (disable:4244)
00041 #pragma warning (disable:4305)
00042 #pragma warning (disable:4267)
00043
00044 #pragma warning( disable : 4290 )
00045
00046 #pragma warning(disable: 4251)
00047 #pragma warning(disable: 4275)
00048 #endif
00049
00050 #if defined(__BORLANDC__)
00051
00052 #pragma warn -8027
00053
00054 #pragma warn -8012
00055 #pragma warn -8022
00056 #endif
00057
00058
00059 #if defined(_MSC_VER) && (_MSC_VER>=1400)
00060 #if !defined(_CRT_SECURE_CPP_OVERLOAD_STANDARD_NAMES)
00061 #define _CRT_SECURE_CPP_OVERLOAD_STANDARD_NAMES 1
00062 #endif
00063 #endif
00064
00065
00066 #include <mrpt/config.h>
00067 #include <mrpt/system/os.h>
00068
00069
00070 #if defined(MRPT_BUILT_AS_DLL) && defined(mrpt_core_EXPORTS)
00071 # define MRPT_SKIP_AUTOINCLUDE_LIBS
00072 #endif
00073
00074 #define _IAMINUTILSDEFS_H
00075 #include <mrpt/utils/utils_impexp.h>
00076 #undef _IAMINUTILSDEFS_H
00077
00078
00079
00080
00081
00082
00083
00084 #if (defined(_MSC_VER) || defined(__BORLANDC__)) && !defined(MRPT_SKIP_AUTOINCLUDE_LIBS)
00085 # if defined(_DEBUG)
00086 # pragma comment (lib,"libmrpt-core-dbg.lib")
00087 # pragma comment (lib,"libmrpt-ann-dbg.lib")
00088 # pragma comment (lib,"libmrpt-3ds-dbg.lib")
00089 # pragma comment (lib,"libmrpt-sift-hess-dbg.lib")
00090 # if !MRPT_HAS_WXWIDGETS && MRPT_HAS_ZLIB && !MRPT_HAS_ZLIB_SYSTEM
00091 # pragma comment (lib,"libmrpt-zlib-dbg.lib")
00092 # endif
00093 # else
00094 # pragma comment (lib,"libmrpt-core.lib")
00095 # pragma comment (lib,"libmrpt-ann.lib")
00096 # pragma comment (lib,"libmrpt-3ds.lib")
00097 # pragma comment (lib,"libmrpt-sift-hess.lib")
00098 # if !MRPT_HAS_WXWIDGETS && MRPT_HAS_ZLIB && !MRPT_HAS_ZLIB_SYSTEM
00099 # pragma comment (lib,"libmrpt-zlib.lib")
00100 # endif
00101 # endif
00102 #endif
00103
00104
00105
00106 #if defined(__GNUC__) && (__GNUC__ - 0 > 3 || (__GNUC__ - 0 == 3 && __GNUC_MINOR__ - 0 >= 2))
00107
00108 # define MRPT_DEPRECATED_PRE
00109 # define MRPT_DEPRECATED_POST __attribute__ ((deprecated))
00110 # elif defined(_MSC_VER) && (_MSC_VER >= 1300)
00111
00112 # define MRPT_DEPRECATED_PRE __declspec(deprecated)
00113 # define MRPT_DEPRECATED_POST
00114 # else
00115 # define MRPT_DEPRECATED_PRE
00116 # define MRPT_DEPRECATED_POST
00117 # endif
00118
00119
00120
00121 #include <stdlib.h>
00122 #include <cstdlib>
00123 #include <cmath>
00124
00125
00126 #include <algorithm>
00127 #include <iostream>
00128 #include <fstream>
00129 #include <cstring>
00130 #include <exception>
00131 #include <stdexcept>
00132 #include <limits>
00133 #include <sstream>
00134
00135
00136 #include <mrpt/otherlibs/stlplus/smart_ptr.hpp>
00137
00138
00139 #include <mrpt/utils/types.h>
00140
00141
00142
00143 namespace mrpt
00144 {
00150 std::string MRPTDLLIMPEXP format(const char *fmt, ...) MRPT_printf_format_check(1,2);
00151
00152 namespace utils
00153 {
00154 class CFileStream;
00155 }
00156 namespace system
00157 {
00158
00159 std::string MRPTDLLIMPEXP extractFileName(const std::string &filePath);
00160 }
00161
00162
00163 namespace math
00164 {
00165 bool MRPTDLLIMPEXP isNan(float v);
00166 bool MRPTDLLIMPEXP isNan(double v);
00167 bool MRPTDLLIMPEXP isFinite(float v);
00168 bool MRPTDLLIMPEXP isFinite(double v);
00169 }
00170 }
00171
00172
00175 #if defined(__BORLANDC__)
00176 #define __CURRENT_FUNCTION_NAME__ __FUNC__
00177 #elif defined(_MSC_VER) && (_MSC_VER>=1300)
00178 #define __CURRENT_FUNCTION_NAME__ __FUNCTION__
00179 #elif defined(_MSC_VER) && (_MSC_VER<1300)
00180
00181 #define __CURRENT_FUNCTION_NAME__ ::system::extractFileName(__FILE__).c_str()
00182 #else
00183 #define __CURRENT_FUNCTION_NAME__ __PRETTY_FUNCTION__
00184 #endif
00185
00186
00192 #define THROW_EXCEPTION(msg) \
00193 {\
00194 std::ostringstream auxCompStr;\
00195 auxCompStr << "\n\n =============== MRPT EXCEPTION =============\n";\
00196 auxCompStr << __CURRENT_FUNCTION_NAME__ << ", line " << __LINE__ << ":\n";\
00197 auxCompStr << msg << "\nCALL STACK:\n";\
00198 throw std::logic_error( auxCompStr.str() );\
00199 }\
00200
00201
00205 #define THROW_EXCEPTION_CUSTOM_MSG1(msg,param1) \
00206 {\
00207 std::ostringstream auxCompStr;\
00208 auxCompStr << "\n\n =============== MRPT EXCEPTION =============\n";\
00209 auxCompStr << __CURRENT_FUNCTION_NAME__ << ", line " << __LINE__ << ":\n";\
00210 auxCompStr << format(msg,param1);\
00211 auxCompStr << "\nCALL STACK:\n";\
00212 throw std::logic_error( auxCompStr.str() );\
00213 }\
00214
00215
00220 #define THROW_TYPED_EXCEPTION(msg,exceptionClass) \
00221 {\
00222 std::ostringstream auxCompStr;\
00223 auxCompStr << "\n\n =============== MRPT EXCEPTION =============\n";\
00224 auxCompStr << __CURRENT_FUNCTION_NAME__ << ", line " << __LINE__ << ":\n";\
00225 auxCompStr << msg << "\nCALL STACK:\n";\
00226 throw exceptionClass( auxCompStr.str() );\
00227 }\
00228
00229
00233 #define THROW_TYPED_EXCEPTION_CUSTOM_MSG1(msg,param1,exceptionClass) \
00234 {\
00235 std::ostringstream auxCompStr;\
00236 auxCompStr << "\n\n =============== MRPT EXCEPTION =============\n";\
00237 auxCompStr << __CURRENT_FUNCTION_NAME__ << ", line " << __LINE__ << ":\n";\
00238 auxCompStr << format(msg,param1);\
00239 auxCompStr << "\nCALL STACK:\n";\
00240 throw exceptionClass( auxCompStr.str() );\
00241 }\
00242
00243
00247 #define THROW_STACKED_EXCEPTION(e) \
00248 {\
00249 std::string str( e.what() );\
00250 str+= __CURRENT_FUNCTION_NAME__;\
00251 str+= format(", line %i:\n", __LINE__ );\
00252 if (str.size()>3000) { std::cerr << "TOO MANY STACKED EXCEPTIONS!: " << std::endl << str << std::endl; abort(); } \
00253 throw std::logic_error( str );\
00254 }\
00255
00256
00260 #define THROW_STACKED_EXCEPTION_CUSTOM_MSG1(e,msg) \
00261 {\
00262 std::ostringstream auxCompStr;\
00263 auxCompStr << e.what() ; \
00264 auxCompStr << msg << __CURRENT_FUNCTION_NAME__ << ", line " << __LINE__ << "(User msg above)\n";\
00265 throw std::logic_error( auxCompStr.str() );\
00266 }\
00267
00268
00272 #define THROW_STACKED_EXCEPTION_CUSTOM_MSG2(e,stuff,param1) \
00273 {\
00274 std::ostringstream auxCompStr;\
00275 auxCompStr << e.what(); \
00276 auxCompStr << format( stuff, param1 ) << "\n" << __CURRENT_FUNCTION_NAME__ << ", line " << __LINE__ << ":\n";\
00277 throw std::logic_error( auxCompStr.str() );\
00278 }\
00279
00280
00281 #define MRPT_THROW_UNKNOWN_SERIALIZATION_VERSION(V) THROW_EXCEPTION(format("Cannot parse object: unknown serialization version number: '%i'",static_cast<int>(version)))
00282
00283
00284 #if MRPT_HAS_ASSERT
00285
00290 # define ASSERT_(f) \
00291 { \
00292 if (!(f)) \
00293 { \
00294 ::std::string tmpStr("Assert condition failed: "); \
00295 tmpStr+= #f; \
00296 THROW_EXCEPTION( tmpStr.c_str() ); \
00297 } \
00298 }
00299
00302 #define MRPT_CHECK_NORMAL_NUMBER(val) \
00303 { \
00304 double v=(val); \
00305 if (math::isNan(v)) THROW_EXCEPTION("Check failed (value is NaN)"); \
00306 if (!math::isFinite(v)) THROW_EXCEPTION("Check failed (value is not finite)"); \
00307 }
00308
00309
00310
00311
00312
00313
00314
00315
00316
00317
00318
00319 #ifndef BOOST_JOIN
00320 #define BOOST_JOIN( X, Y ) BOOST_DO_JOIN( X, Y )
00321 #define BOOST_DO_JOIN( X, Y ) BOOST_DO_JOIN2(X,Y)
00322 #define BOOST_DO_JOIN2( X, Y ) X##Y
00323 #endif
00324
00325
00326
00327
00328 namespace mrpt
00329 {
00330 namespace utils
00331 {
00332 template <bool value> struct compile_time_assert;
00333 template <> struct compile_time_assert<true> { enum {value=1}; };
00334 }
00335 }
00336 #define MRPT_COMPILE_TIME_ASSERT(expression) \
00337 typedef char BOOST_JOIN(MRPT_CTA, __LINE__)[::mrpt::utils::compile_time_assert<(bool)(expression)>::value];
00338
00339 #else
00340 # define ASSERT_(f)
00341 # define MRPT_CHECK_NORMAL_NUMBER(val)
00342 # define MRPT_COMPILE_TIME_ASSERT(f)
00343 #endif
00344
00345
00346
00349 #define MRPT_UNUSED_PARAM(a) (void)(a)
00350
00351 #if MRPT_HAS_STACKED_EXCEPTIONS
00352
00355 # define MRPT_TRY_START \
00356 try { \
00357
00358
00361 # define MRPT_TRY_END \
00362 } \
00363 catch (std::bad_alloc &e) \
00364 { \
00365 throw e; \
00366 } \
00367 catch (std::exception &e) \
00368 { \
00369 THROW_STACKED_EXCEPTION(e); \
00370 } \
00371 catch (...) \
00372 { \
00373 THROW_EXCEPTION("Unexpected runtime error!"); \
00374 } \
00375
00376
00379 # define MRPT_TRY_END_WITH_CLEAN_UP(stuff) \
00380 } \
00381 catch (std::bad_alloc &e) \
00382 { \
00383 throw e; \
00384 } \
00385 catch (std::exception &e) \
00386 { \
00387 {stuff} \
00388 THROW_STACKED_EXCEPTION(e); \
00389 } \
00390 catch (...) \
00391 { \
00392 { stuff } \
00393 THROW_EXCEPTION("Unexpected runtime error!"); \
00394 } \
00395
00396 #else
00397 # define MRPT_TRY_START
00398 # define MRPT_TRY_END
00399 # define MRPT_TRY_END_WITH_CLEAN_UP(stuff)
00400 #endif
00401
00402
00403
00404
00405 #ifndef M_PI
00406
00408 #define M_PI 3.14159265358979323846264338327950288
00409 #endif
00410
00411 #ifndef M_2PI
00412
00414 #define M_2PI 6.283185307179586476925286766559
00415 #endif
00416
00417 #define M_PIf 3.14159265358979f
00418 #define M_2PIf 6.28318530717959f
00419
00420
00421
00422 #ifdef MRPT_OS_WINDOWS
00423 # define NOMINMAX
00424 # ifdef max
00425 # undef max
00426 # undef min
00427 # endif
00428 #endif
00429
00430
00431
00432
00433 #if defined(_MSC_VER) && (_MSC_VER<1300)
00434 # ifndef max
00435 namespace std
00436 {
00437 template<class T> inline const T max(const T& A,const T& B) { return A>B ? A:B; }
00438 template<class T> inline const T min(const T& A,const T& B) { return A<B ? A:B; }
00439 }
00440 # else
00441 # define MAX3_MSVC6_VERSION
00442 # endif
00443 #endif
00444
00445
00446 #ifndef MAX3_MSVC6_VERSION
00447 template<typename T> inline const T min3(const T& A, const T& B,const T& C) { return std::min<T>(A, std::min<T>(B,C) ); }
00448 template<typename T> inline const T max3(const T& A, const T& B,const T& C) { return std::max<T>(A, std::max<T>(B,C) ); }
00449 #else
00450 # define max3(A,B,C) max(A,max(B,C))
00451 # define min3(A,B,C) min(A,min(B,C))
00452 #endif
00453
00454 namespace mrpt
00455 {
00456 namespace utils
00457 {
00459 inline double DEG2RAD(const double &x) { return x*M_PI/180.0; }
00460
00462 inline float DEG2RAD(const float &x) { return x*M_PIf/180.0f; }
00463
00465 inline float DEG2RAD(const int &x) { return x*M_PIf/180.0f; }
00466
00468 inline double RAD2DEG(const double &x) { return x*180.0/M_PI; }
00469
00471 inline float RAD2DEG(const float &x) { return x*180.0f/M_PIf; }
00472
00474 template <typename T>
00475 int sign(T x) { return x<0 ? -1:1; }
00476
00478 template <typename T>
00479 int round(T x) { return static_cast<int>(floor(static_cast<double>(x+0.5))); }
00480
00482 template <typename T>
00483 long round_long(T x) { return static_cast<long>(floor(static_cast<double>(x+0.5))); }
00484
00486 template <typename T>
00487 int fix(T x) { return x>0 ? static_cast<int>(floor(static_cast<double>(x))) : static_cast<int>(ceil(static_cast<double>(x))) ; }
00488
00490 template<class T>
00491 inline T square(const T& x) { return x*x; }
00492
00493
00494
00495
00496
00497
00500 typedef void (*TFunctor_noRet_1inputs)(const void *);
00501
00504 typedef void (*TFunctor_noRet_2inputs)(const void *,const void *);
00505
00508 typedef void (*TFunctor_noRet_3inputs)(const void *,const void *,const void *);
00509
00510
00511
00512
00513
00514
00517 typedef double (*TFunctor_retDouble_1inputs)(const void *);
00518
00521 typedef double (*TFunctor_retDouble_2inputs)(const void *,const void *);
00522
00525 typedef double (*TFunctor_retDouble_3inputs)(const void *,const void *,const void *);
00526
00527
00528
00529
00530
00531
00534 typedef void (*TFunctor_retVecDbl_inpVecDbl)(const vector_double &in, vector_double &out);
00535
00538 typedef void (*TFunctor_retVecFlt_inpVecFlt)(const vector_float &in, vector_float &out);
00539
00542 typedef void (*TFunctor_retVecInt_inpVecInt)(const vector_int &in, vector_int &out);
00543
00544
00545
00546
00547
00548
00551 typedef void (*TFunctor_retVecDbl_inp2VecDbl)(const vector_double &x,const vector_double &y, vector_double &out);
00552
00555 typedef void (*TFunctor_retVecFlt_inp2VecFlt)(const vector_float &x,const vector_float &y, vector_float &out);
00556
00559 typedef void (*TFunctor_retVecInt_inp2VecInt)(const vector_int &x,const vector_int &y, vector_int &out);
00560
00561
00562
00563
00564
00565
00568 typedef double (*TFunctor_retDbl_inp1VecDbl)(const vector_double &in1);
00569
00572 typedef double (*TFunctor_retDbl_inp2VecDbl)(const vector_double &in1,const vector_double &in2);
00573
00576 typedef double (*TFunctor_retDbl_inp3VecDbl)(const vector_double &in1,const vector_double &in2,const vector_double &in3);
00577
00578
00581 #if defined(_MSC_VER) && (_MSC_VER>=1300)
00582 typedef unsigned long long POINTER_TYPE;
00583 #else
00584 typedef unsigned long POINTER_TYPE;
00585 #endif
00586
00588 struct ObjectDelete
00589 {
00590 template<typename T>
00591 void operator()(const T *ptr)
00592 {
00593 delete ptr;
00594 }
00595 };
00596
00598 struct ObjectClear
00599 {
00600 template<typename T>
00601 void operator()(T ptr)
00602 {
00603 ptr->clear();
00604 }
00605 };
00606
00608 template <class R, class P>
00609 R* getAs(stlplus::smart_ptr_clone<P> &o) { return static_cast<R*>( & (*o) ); }
00610
00612 template <class R, class P>
00613 const R* getAs(const stlplus::smart_ptr_clone<P> &o) { return static_cast<const R*>( & (*o) ); }
00614
00615
00617 template <class T> void reverseBytes(const T &v_in, T& v_out)
00618 {
00619 v_out = v_in;
00620 uint8_t *ptr = reinterpret_cast<uint8_t*>(&v_out);
00621 std::reverse(ptr,ptr+sizeof(T));
00622 }
00623
00624 }
00625 }
00626 #endif