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 #ifndef PSYCHO_HANDDATA_H_DEFINED
00037 #define PSYCHO_HANDDATA_H_DEFINED
00038 #include <iostream>
00039
00040 #include "Image/Point2D.H"
00041
00042 #include "Util/Types.H"
00043 #include "Util/StringUtil.H"
00044 #include "Util/log.H"
00045 #include "rutz/compat_cmath.h"
00046 #include "rutz/shared_ptr.h"
00047 #include "Component/ParamMap.H"
00048 #include <cmath>
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061 class HandData
00062 {
00063 public:
00064
00065
00066
00067
00068
00069 HandData(const int x_, const int y_, const std::vector<bool> b_);
00070
00071 HandData(const int mx_, const int my_, const int nmx_, const int nmy_,
00072 const bool mlb_, const bool mmb_, const bool mrb_);
00073
00074 HandData(const char* kbch_);
00075
00076
00077 HandData(const char* kbch_,
00078 const int mx_, const int my_, const int nmx_, const int nmy_,
00079 const bool mlb_, const bool mmb_, const bool mrb_);
00080
00081 HandData(const char* kbch_,
00082 const int mx_, const int my_, const int nmx_, const int nmy_,
00083 const bool mlb_, const bool mmb_, const bool mrb_,
00084 const int x_, const int y_, const std::vector<bool> b_);
00085
00086
00087
00088 HandData(const int x_, const int y_, const std::vector<bool> b_,
00089 rutz::shared_ptr<ParamMap> dat);
00090
00091 HandData(const int mx_, const int my_, const int nmx_, const int nmy_,
00092 const bool mlb_, const bool mmb_, const bool mrb_,
00093 rutz::shared_ptr<ParamMap> dat);
00094
00095 HandData(const char* kbch_, rutz::shared_ptr<ParamMap> dat);
00096
00097 HandData(const char* kbch_,
00098 const int mx_, const int my_, const int nmx_, const int nmy_,
00099 const bool mlb_, const bool mmb_, const bool mrb_,
00100 rutz::shared_ptr<ParamMap> dat);
00101
00102 HandData(const char* kbch_,
00103 const int mx_, const int my_, const int nmx_, const int nmy_,
00104 const bool mlb_, const bool mmb_, const bool mrb_,
00105 const int x_, const int y_, const std::vector<bool> b_,
00106 rutz::shared_ptr<ParamMap> dat);
00107
00108
00109
00110 ~HandData();
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123 bool isPressed(uint& b_) const;
00124
00125
00126 bool isAnyPressed() const;
00127
00128
00129 bool isButtonEmpty() const;
00130
00131
00132 bool isValid() const;
00133
00134
00135 bool isWithin(const Dims& dims) const;
00136
00137
00138
00139
00140
00141
00142
00143 bool isMouseValid() const;
00144
00145
00146 bool isMouseAnyPressed() const;
00147
00148
00149
00150
00151
00152
00153
00154 bool isKeyboardEmpty() const;
00155
00156
00157
00158
00159
00160
00161
00162
00163
00164
00165 bool hasMetaData(const std::string field) const;
00166
00167 bool hasSpecialMetaData(const std::string field) const;
00168
00169
00170 bool isSpecialField(std::string field) const;
00171
00172
00173
00174
00175
00176
00177
00178
00179
00180
00181
00182
00183
00184
00185
00186 size_t numButton() const;
00187
00188
00189
00190 int getAccelerator() const;
00191 float getAcceleratorF() const;
00192
00193
00194 int getBrake() const;
00195 float getBrakeF() const;
00196
00197
00198 int getWheelLeft() const;
00199 float getWheelLeftF() const;
00200
00201
00202 int getWheelRight() const;
00203 float getWheelRightF() const;
00204
00205
00206
00207
00208 int getWheel() const;
00209 float getWheelF() const;
00210 int getX() const;
00211
00212
00213 int getAcclBrake() const;
00214 float getAcclBrakeF() const;
00215 int getY() const;
00216
00217
00218
00219 Point2D<int> getPosition() const;
00220 Point2D<float> getPositionF() const;
00221
00222
00223
00224
00225
00226
00227
00228 int getMouseX() const;
00229 int getMouseY() const;
00230 int getNativeX() const;
00231 int getNativeY() const;
00232 bool getMouseBL() const;
00233 bool getMouseBM() const;
00234 bool getMouseBR() const;
00235
00236
00237
00238
00239
00240
00241
00242 char *getKeyboard() const;
00243
00244
00245
00246
00247
00248
00249
00250 rutz::shared_ptr<ParamMap> getMetaData() const;
00251
00252
00253 std::vector<std::string> getMetaDataList() const;
00254
00255
00256 double getMetaDataField(std::string field) const;
00257
00258
00259
00260
00261
00262 void outputData(std::ostream &out) const;
00263
00264
00265
00266
00267 private:
00268
00269 const int x, y;
00270
00271
00272
00273 const std::vector<bool> b;
00274
00275
00276 const int mx, my;
00277 const int nmx, nmy;
00278 const bool mlb, mmb, mrb;
00279
00280
00281 const char* kbch;
00282
00283
00284 const rutz::shared_ptr<ParamMap> itsExtraData;
00285
00286
00287 std::string special(std::string field) const;
00288 std::string unspecial(std::string field) const;
00289 };
00290
00291
00292
00293
00294
00295
00296
00297
00298
00299
00300
00301
00302
00303
00304 inline HandData::HandData(const int x_, const int y_,
00305 const std::vector<bool> b_) :
00306 x(x_), y(y_), b(b_),
00307 mx(-1), my(-1), nmx(-1), nmy(-1), mlb(false), mmb(false), mrb(false),
00308 kbch("")
00309 { }
00310
00311
00312 inline HandData::HandData(const int mx_, const int my_,
00313 const int nmx_, const int nmy_,
00314 const bool mlb_, const bool mmb_, const bool mrb_) :
00315 x(-1), y(-1), b(),
00316 mx(mx_), my(my_), nmx(nmx_), nmy(nmy_), mlb(mlb_), mmb(mmb_), mrb(mrb_),
00317 kbch("")
00318 { }
00319
00320
00321 inline HandData::HandData(const char* kbch_) :
00322 x(-1), y(-1), b(),
00323 mx(-1), my(-1), nmx(-1), nmy(-1), mlb(false), mmb(false), mrb(false),
00324 kbch(kbch_)
00325 { }
00326
00327
00328 inline HandData::HandData(const char* kbch_,
00329 const int mx_, const int my_,
00330 const int nmx_, const int nmy_,
00331 const bool mlb_, const bool mmb_, const bool mrb_) :
00332 x(-1), y(-1), b(),
00333 mx(mx_), my(my_), nmx(nmx_), nmy(nmy_), mlb(mlb_), mmb(mmb_), mrb(mrb_),
00334 kbch(kbch_)
00335 { }
00336
00337 inline HandData::HandData(const char* kbch_,
00338 const int mx_, const int my_,
00339 const int nmx_, const int nmy_,
00340 const bool mlb_, const bool mmb_, const bool mrb_,
00341 const int x_, const int y_,
00342 const std::vector<bool> b_) :
00343 x(x_), y(y_), b(b_),
00344 mx(mx_), my(my_), nmx(nmx_), nmy(nmy_), mlb(mlb_), mmb(mmb_), mrb(mrb_),
00345 kbch(kbch_)
00346 { }
00347
00348
00349
00350
00351 inline HandData::HandData(const int x_, const int y_,
00352 const std::vector<bool> b_,
00353 rutz::shared_ptr<ParamMap> dat) :
00354 x(x_), y(y_), b(b_),
00355 mx(-1), my(-1), nmx(-1), nmy(-1), mlb(false), mmb(false), mrb(false),
00356 kbch("")
00357 { }
00358
00359
00360 inline HandData::HandData(const int mx_, const int my_,
00361 const int nmx_, const int nmy_,
00362 const bool mlb_, const bool mmb_, const bool mrb_,
00363 rutz::shared_ptr<ParamMap> dat) :
00364 x(-1), y(-1), b(),
00365 mx(mx_), my(my_), nmx(nmx_), nmy(nmy_), mlb(mlb_), mmb(mmb_), mrb(mrb_),
00366 kbch("")
00367 { }
00368
00369
00370 inline HandData::HandData(const char* kbch_, rutz::shared_ptr<ParamMap> dat) :
00371 x(-1), y(-1), b(),
00372 mx(-1), my(-1), nmx(-1), nmy(-1), mlb(false), mmb(false), mrb(false),
00373 kbch(kbch_)
00374 { }
00375
00376
00377 inline HandData::HandData(const char* kbch_,
00378 const int mx_, const int my_,
00379 const int nmx_, const int nmy_,
00380 const bool mlb_, const bool mmb_, const bool mrb_,
00381 rutz::shared_ptr<ParamMap> dat) :
00382 x(-1), y(-1), b(),
00383 mx(mx_), my(my_), nmx(nmx_), nmy(nmy_), mlb(mlb_), mmb(mmb_), mrb(mrb_),
00384 kbch(kbch_)
00385 { }
00386
00387 inline HandData::HandData(const char* kbch_,
00388 const int mx_, const int my_,
00389 const int nmx_, const int nmy_,
00390 const bool mlb_, const bool mmb_, const bool mrb_,
00391 const int x_, const int y_,
00392 const std::vector<bool> b_,
00393 rutz::shared_ptr<ParamMap> dat) :
00394 x(x_), y(y_), b(b_),
00395 mx(mx_), my(my_), nmx(nmx_), nmy(nmy_), mlb(mlb_), mmb(mmb_), mrb(mrb_),
00396 kbch(kbch_)
00397 { }
00398
00399
00400 inline HandData::~HandData()
00401 { }
00402
00403
00404
00405
00406
00407
00408 inline bool HandData::isPressed(uint& b_) const
00409 {
00410 try {
00411 return b[b_];}
00412 catch (char *str) {
00413
00414 LERROR("Data %d is out of range, we only have %d buttons",
00415 b_, int(b.size()));
00416 return false;}
00417 }
00418
00419 inline bool HandData::isAnyPressed() const {
00420 for (uint i=0; i<b.size(); i++)
00421 if (b[i]) return true;
00422 return false;
00423 }
00424
00425 inline bool HandData::isButtonEmpty() const {
00426 return b.empty(); }
00427
00428 inline bool HandData::isValid() const {
00429 return !(isnan(x) || isnan(y)) && x > -1 && y > -1; }
00430
00431 inline bool HandData::isWithin(const Dims& dims) const {
00432 if (isValid() == false) return false;
00433 return (x >= 0 && x < dims.w() && y >= 0 && y < dims.h());
00434 }
00435
00436
00437
00438 inline bool HandData::isMouseValid() const {
00439 return !(isnan(mx) || isnan(my)) && mx > -1 && my > -1;
00440 }
00441
00442 inline bool HandData::isMouseAnyPressed() const {
00443 return (mlb || mmb || mrb);
00444 }
00445
00446
00447
00448 inline bool HandData::isKeyboardEmpty() const {
00449 return (std::string(kbch).empty());
00450 }
00451
00452
00453
00454 inline bool HandData::hasMetaData(const std::string field = "any") const
00455 { if(field.compare("any") == 0) return itsExtraData.is_valid();
00456 else return (itsExtraData.is_valid() &&
00457 (itsExtraData->hasParam(field) ||
00458 itsExtraData->hasParam(special(field)) )); }
00459
00460 inline bool HandData::hasSpecialMetaData(const std::string field) const
00461 {
00462 return (hasMetaData("any") && itsExtraData->hasParam(special(field)));
00463 }
00464
00465
00466 inline bool HandData::isSpecialField(std::string field) const
00467 {
00468 return field[0]=='*';
00469 }
00470
00471
00472
00473
00474
00475
00476
00477
00478
00479 inline size_t HandData::numButton() const {
00480 return b.size(); }
00481
00482 inline int HandData::getAccelerator() const {
00483 return (y<127)?(127-y):0; }
00484 inline float HandData::getAcceleratorF() const {
00485 return (y<127)?(127-y)/127.0:0.0; }
00486
00487 inline int HandData::getBrake() const {
00488 return (y>127)?(y-127):0; }
00489 inline float HandData::getBrakeF() const {
00490 return (y>127)?(y-127)/128.0:0; }
00491
00492 inline int HandData::getWheelLeft() const {
00493 return (x<127)?(127-x):0; }
00494 inline float HandData::getWheelLeftF() const {
00495 return (x<127)?(127-x)/127.0:0.0; }
00496
00497 inline int HandData::getWheelRight() const {
00498 return (x>127)?(x-127):0; }
00499 inline float HandData::getWheelRightF() const {
00500 return (x>127)?(x-127)/128.0:0; }
00501
00502 inline int HandData::getWheel() const {
00503 return x; }
00504 inline float HandData::getWheelF() const {
00505 return (x*2.0/255.0) - 1.0; }
00506 inline int HandData::getX() const {
00507 return x; }
00508
00509 inline int HandData::getAcclBrake() const {
00510 return y; }
00511 inline float HandData::getAcclBrakeF() const {
00512 return (y*2.0/255.0) - 1.0; }
00513 inline int HandData::getY() const {
00514 return y; }
00515
00516 inline Point2D<int> HandData::getPosition() const {
00517 return Point2D<int> (x,y); }
00518 inline Point2D<float> HandData::getPositionF() const {
00519 return Point2D<float> (getWheelF(),getAcclBrakeF()); }
00520
00521
00522
00523
00524
00525
00526 inline int HandData::getMouseX() const {
00527 return mx;}
00528 inline int HandData::getMouseY() const {
00529 return my;}
00530 inline int HandData::getNativeX() const {
00531 return nmx;}
00532 inline int HandData::getNativeY() const {
00533 return nmy;}
00534 inline bool HandData::getMouseBL() const {
00535 return mlb;}
00536 inline bool HandData::getMouseBM() const {
00537 return mmb;}
00538 inline bool HandData::getMouseBR() const {
00539 return mrb;}
00540
00541
00542
00543
00544
00545
00546 inline char * HandData::getKeyboard() const {
00547 return (char*)kbch;}
00548
00549
00550
00551
00552
00553 inline rutz::shared_ptr<ParamMap> HandData::getMetaData() const
00554 { return itsExtraData; }
00555
00556 inline std::vector<std::string> HandData::getMetaDataList() const
00557 {
00558 std::vector<std::string> argList;
00559 if(hasMetaData())
00560 for (ParamMap::key_iterator iter = getMetaData()->keys_begin();
00561 iter != getMetaData()->keys_end();
00562 ++iter)
00563 argList.push_back(*iter);
00564
00565 return argList; }
00566
00567 inline double HandData::getMetaDataField(std::string field) const
00568 {
00569 if (hasSpecialMetaData(field))
00570 return itsExtraData->getDoubleParam(special(field));
00571 else if(hasMetaData(field))
00572 return itsExtraData->getDoubleParam(field);
00573 else LFATAL("No data field %s in HandData",field.c_str());
00574 return -1;
00575 }
00576
00577 inline void HandData::outputData(std::ostream &out) const
00578 {
00579 out << "(x,y) = (" << x << "," << y << ")\t"<< "buttons =";
00580 for (uint i = 0; i < b.size(); i++)
00581 out << " " << b[i];
00582 out << "\n";
00583 if(hasMetaData())
00584 {
00585 std::vector<std::string> metaFields = getMetaDataList();
00586 for(std::vector<std::string>::iterator iter = metaFields.begin();
00587 iter != metaFields.end();
00588 ++iter)
00589 out << *iter << " = " << getMetaDataField(*iter) << "\n";
00590 }
00591 };
00592
00593 inline std::string HandData::special(std::string field) const
00594 {
00595 if(field[0]=='*') return field;
00596 else return "*"+field;
00597 }
00598
00599 inline std::string HandData::unspecial(std::string field) const
00600 {
00601 if(field[0]=='*') return field.erase(0,1);
00602 else return field;
00603 }
00604
00605
00606
00607
00608
00609
00610
00611
00612 #endif // PSYCHO_HANDDATA_H_DEFINED