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
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042 #ifndef LOBOT_UTILITIES_DOT_H
00043 #define LOBOT_UTILITIES_DOT_H
00044
00045
00046
00047
00048 #include "Robots/LoBot/misc/range.hh"
00049
00050
00051 #include "Util/log.H"
00052
00053
00054 #include <sstream>
00055 #include <ostream>
00056 #include <string>
00057 #include <algorithm>
00058 #include <list>
00059 #include <vector>
00060 #include <functional>
00061 #include <iterator>
00062 #include <limits>
00063 #include <utility>
00064
00065
00066 #include <math.h>
00067
00068
00069
00070 namespace lobot {
00071
00072
00073
00074
00075 template<typename T>
00076 inline int sign(const T& t)
00077 {
00078 return (t < 0) ? -1 : +1 ;
00079 }
00080
00081
00082 template<typename T>
00083 inline T abs(const T& t)
00084 {
00085 return (t < 0) ? -t : t ;
00086 }
00087
00088
00089 template<typename T>
00090 T clamp(const T& value, const T& min, const T& max)
00091 {
00092 if (value < min)
00093 return min ;
00094 if (value > max)
00095 return max ;
00096 return value ;
00097 }
00098
00099
00100 template<typename T>
00101 inline T clamp(const T& value, const range<T>& range)
00102 {
00103 return clamp(value, range.min(), range.max()) ;
00104 }
00105
00106
00107 template<typename T>
00108 T clamp_angle(const T& angle)
00109 {
00110 T A = angle - 360 * static_cast<int>(angle/360) ;
00111 return (A < 0) ? 360 + A : A ;
00112 }
00113
00114
00115 template<typename T>
00116 bool is_zero(const T& t)
00117 {
00118 return t > -std::numeric_limits<T>::epsilon()
00119 && t < std::numeric_limits<T>::epsilon() ;
00120 }
00121
00122
00123 template<typename T>
00124 int round(const T& t)
00125 {
00126 return static_cast<int>(t + T(0.5)) ;
00127 }
00128
00129
00130 template<typename T>
00131 T force_odd(const T& t)
00132 {
00133 return t | 1 ;
00134 }
00135
00136
00137 template<typename T>
00138 T force_even(const T& t)
00139 {
00140 return t & T(~1) ;
00141 }
00142
00143
00144 inline unsigned long make_long(int a, int b)
00145 {
00146 return (a << 16) | b ;
00147 }
00148
00149
00150 inline int loword(unsigned long L)
00151 {
00152 return static_cast<int>(L & 0xFFFF) ;
00153 }
00154
00155
00156 inline int hiword(unsigned long L)
00157 {
00158 return loword(L >> 16) ;
00159 }
00160
00161
00162
00163 inline float sin(float angle)
00164 {
00165 return sinf(angle * 0.01745329f) ;
00166 }
00167
00168 inline float cos(float angle)
00169 {
00170 return cosf(angle * 0.01745329f) ;
00171 }
00172
00173 inline double sin(double angle)
00174 {
00175 return ::sin(angle * 0.0174532925199432) ;
00176 }
00177
00178 inline double cos(double angle)
00179 {
00180 return ::cos(angle * 0.0174532925199432) ;
00181 }
00182
00183
00184
00185
00186 inline float log(float n)
00187 {
00188 return log10f(n) ;
00189 }
00190
00191 inline double log(double n)
00192 {
00193 return log10(n) ;
00194 }
00195
00196
00197
00198
00199 inline float exp(float n)
00200 {
00201 return expf(n) ;
00202 }
00203
00204 inline double exp(double n)
00205 {
00206 return ::exp(n) ;
00207 }
00208
00209
00210
00211
00212
00213
00214
00215
00216
00217 template<typename T>
00218 std::string to_string(const T& t)
00219 {
00220 std::ostringstream str ;
00221 str << t ;
00222 return str.str() ;
00223 }
00224
00225
00226
00227 template<typename T>
00228 T from_string(const std::string& s, const T& defval = T())
00229 {
00230 T t(defval) ;
00231 std::istringstream str(s) ;
00232 str >> t ;
00233 return t ;
00234 }
00235
00236
00237
00238
00239
00240
00241 template<>
00242 std::string from_string(const std::string& s, const std::string&)
00243 {
00244 return s ;
00245 }
00246
00247
00248 template<typename T>
00249 std::vector<T> string_to_vector(const std::string& s)
00250 {
00251 std::istringstream str(s) ;
00252 return std::vector<T>(std::istream_iterator<T>(str),
00253 std::istream_iterator<T>()) ;
00254 }
00255
00256
00257 template<typename T>
00258 std::list<T> string_to_list(const std::string& s)
00259 {
00260 std::istringstream str(s) ;
00261 return std::list<T>(std::istream_iterator<T>(str),
00262 std::istream_iterator<T>()) ;
00263 }
00264
00265
00266
00267 std::string upstring(std::string) ;
00268 std::string downstring(std::string) ;
00269
00270
00271
00272
00273
00274
00275
00276
00277 template<typename P> void delete_ptr(P p) {delete p ;}
00278
00279
00280
00281 template<typename P> void delete_first (P& p) {delete p.first ;}
00282 template<typename P> void delete_second(P& p) {delete p.second ;}
00283
00284
00285
00286
00287 template<typename P>
00288 typename P::first_type get_first(const P& p) {return p.first ;}
00289
00290 template<typename P>
00291 typename P::second_type get_second(const P& p) {return p.second ;}
00292
00293
00294
00295
00296
00297
00298
00299
00300
00301
00302
00303
00304
00305
00306
00307
00308
00309
00310
00311 template<typename C, typename D>
00312 void purge_container(C& c, D delete_fn)
00313 {
00314 std::for_each(c.begin(), c.end(), delete_fn) ;
00315 }
00316
00317
00318
00319
00320
00321
00322
00323
00324
00325
00326 template<typename C>
00327 void purge_container(C& c)
00328 {
00329 typedef typename C::value_type P ;
00330 purge_container(c, delete_ptr<P>) ;
00331 }
00332
00333
00334
00335
00336
00337
00338
00339 template<typename T>
00340 struct min : std::binary_function<T, T, T> {
00341 T operator()(const T& a, const T& b) const {
00342 return std::min(a, b) ;
00343 }
00344 } ;
00345
00346 template<typename T>
00347 struct max : std::binary_function<T, T, T> {
00348 T operator()(const T& a, const T& b) const {
00349 return std::max(a, b) ;
00350 }
00351 } ;
00352
00353
00354
00355
00356
00357 template<typename T>
00358 struct sum_of_squares : std::binary_function<T, T, T> {
00359 T operator()(const T& a, const T& b) const {
00360 return T(a + b*b) ;
00361 }
00362 } ;
00363
00364
00365
00366 template<typename T>
00367 class out_of_range : public std::unary_function<T, bool> {
00368 range<T> m_range ;
00369 public:
00370 out_of_range(const range<T>& R) : m_range(R) {}
00371 bool operator()(const T& t) const {return ! m_range.in(t) ;}
00372 } ;
00373
00374
00375
00376
00377
00378 template<typename C, typename T>
00379 static void connect(const C& container, T& target)
00380 {
00381 std::copy(container.begin(), container.end(), std::back_inserter(target)) ;
00382 }
00383
00384
00385
00386
00387 void draw_label(float x, float y, const char* label) ;
00388
00389
00390
00391
00392
00393
00394
00395 template<typename container>
00396 void dump(const container& C, const std::string& caller,
00397 const std::string& container_name = "container")
00398 {
00399 std::ostringstream str ;
00400 std::copy(C.begin(), C.end(),
00401 std::ostream_iterator<typename container::value_type>(str, " ")) ;
00402 LERROR("from %s ==> %s[0x%lx]: size = %d, contents = %s",
00403 caller.c_str(), container_name.c_str(),
00404 reinterpret_cast<long int>(&C), static_cast<int>(C.size()),
00405 str.str().c_str()) ;
00406 }
00407
00408
00409 template<typename T>
00410 void dump(const T* begin, const T* end, const std::string& caller,
00411 const std::string& container_name = "array")
00412 {
00413 std::vector<T> vec(begin, end) ;
00414 dump(vec, caller, container_name) ;
00415 }
00416
00417
00418 template<typename T1, typename T2>
00419 class dump_pair {
00420 std::ostream& os ;
00421 public:
00422 dump_pair(std::ostream& s) : os(s) {}
00423 void operator()(const std::pair<T1, T2>& p) const {
00424 os << '[' << p.first << ' ' << p.second << "] " ;
00425 }
00426 } ;
00427
00428
00429
00430
00431
00432
00433
00434
00435
00436
00437 template<typename key_type, typename value_type>
00438 void dump(const std::map<key_type, value_type>& C,
00439 const std::string& caller,
00440 const std::string& container_name = "container")
00441 {
00442 std::ostringstream str ;
00443 std::for_each(C.begin(), C.end(),
00444 dump_pair<key_type, value_type>(str)) ;
00445 LERROR("from %s ==> %s[0x%lx]: size = %d, contents = %s",
00446 caller.c_str(), container_name.c_str(),
00447 reinterpret_cast<long int>(&C), static_cast<int>(C.size()),
00448 str.str().c_str()) ;
00449 }
00450
00451
00452
00453 }
00454
00455 #endif
00456
00457
00458
00459
00460