00001
00002
00003
00004
00005
00006
00007
00008
00009 #ifndef __Pose2D_h__
00010 #define __Pose2D_h__
00011
00012 #include "Common.h"
00013 #include "Vector2.h"
00014 #include "Tools/Range.h"
00015
00016
00017 #define Pose2Dold // with angle for direction
00018
00019
00020 #ifdef Pose2Dold
00021
00022
00023 class Pose2D {
00024 public:
00025
00026
00027 double rotation;
00028
00029
00030 Vector2<double> translation;
00031
00032
00033 Pose2D():rotation(0),translation(0,0) {}
00034
00035
00036
00037
00038
00039
00040 Pose2D(const double rot, const Vector2<double>& trans):rotation(rot),translation(trans) {}
00041
00042
00043
00044
00045
00046
00047 Pose2D(const double rot, const double x, const double y):rotation(rot),translation(x,y)
00048 {}
00049
00050
00051
00052
00053
00054 Pose2D(const double rot):rotation(rot),translation(0,0) {}
00055
00056
00057
00058
00059
00060 Pose2D(const Vector2<double>& trans):rotation(0),translation(trans) {}
00061
00062
00063
00064
00065
00066 Pose2D(const Vector2<int>& trans):rotation(0),translation(trans.x,trans.y) {}
00067
00068
00069
00070
00071
00072 Pose2D(const double x, const double y):rotation(0),translation(x,y) {}
00073
00074
00075
00076
00077 inline double getAngle() const {return rotation;}
00078
00079
00080
00081
00082
00083 inline Pose2D fromAngle(const double a) {rotation=a; return *this;}
00084
00085
00086
00087
00088 inline double getCos() const {return cos(rotation);}
00089
00090
00091
00092
00093 inline double getSin() const {return sin(rotation);}
00094
00095
00096
00097
00098
00099 Pose2D& operator=(const Pose2D& other)
00100 {
00101 rotation=other.rotation;
00102 translation=other.translation;
00103 return *this;
00104 }
00105
00106
00107
00108
00109 Pose2D(const Pose2D& other) {*this = other;}
00110
00111
00112
00113
00114
00115
00116 Vector2<double> operator*(const Vector2<double>& point) const
00117 {
00118 double s=sin(rotation);
00119 double c=cos(rotation);
00120 return (Vector2<double>(point.x*c-point.y*s , point.x*s+point.y*c) + translation);
00121 }
00122
00123
00124
00125
00126
00127 bool operator==(const Pose2D& other) const
00128 {
00129 return ((translation==other.translation)&&(rotation==other.rotation));
00130 }
00131
00132
00133
00134
00135
00136 bool operator!=(const Pose2D& other) const
00137 {return !(*this == other);}
00138
00139
00140
00141
00142
00143 Pose2D& operator+=(const Pose2D& other)
00144 {
00145 translation = *this * other.translation;
00146 rotation += other.rotation;
00147 rotation = normalize(rotation);
00148 return *this;
00149 }
00150
00151
00152
00153
00154
00155 Pose2D operator+(const Pose2D& other) const
00156 {return Pose2D(*this) += other;}
00157
00158
00159
00160
00161
00162 Pose2D minusDiff(const Pose2D& diff) const
00163 {
00164 double rot=rotation-diff.rotation;
00165 double s=sin(rot);
00166 double c=cos(rot);
00167 return Pose2D(
00168 rot,
00169 translation.x - c*diff.translation.x + s*diff.translation.y,
00170 translation.y - c*diff.translation.y - s*diff.translation.x);
00171 }
00172
00173
00174
00175
00176
00177 Pose2D& operator-=(const Pose2D& other)
00178 {
00179 translation -= other.translation;
00180 Pose2D p(-other.rotation);
00181 return *this = p + *this;
00182 }
00183
00184
00185
00186
00187
00188 Pose2D operator-(const Pose2D& other) const
00189 {return Pose2D(*this) -= other;}
00190
00191
00192
00193
00194
00195 Pose2D& conc(const Pose2D& other)
00196 {return *this += other;}
00197
00198
00199
00200
00201
00202 Pose2D& translate(const Vector2<double>& trans)
00203 {
00204 translation = *this * trans;
00205 return *this;
00206 }
00207
00208
00209
00210
00211
00212
00213 Pose2D& translate(const double x, const double y)
00214 {
00215 translation = *this * Vector2<double>(x,y);
00216 return *this;
00217 }
00218
00219
00220
00221
00222
00223
00224 Pose2D& rotate(const double angle)
00225 {
00226 rotation += angle;
00227 return *this;
00228 }
00229
00230
00231
00232
00233
00234
00235
00236 static Pose2D random(const Range<double>& x,
00237 const Range<double>& y,
00238 const Range<double>& angle)
00239 {
00240 return Pose2D(::random() * (angle.max - angle.min) + angle.min,
00241 Vector2<double>(::random() * (x.max - x.min) + x.min,
00242 ::random() * (y.max - y.min) + y.min));
00243 }
00244 };
00245
00246 #endif //Pose2Dold
00247
00248 #ifdef Pose2Dnew
00249
00250
00251
00252
00253
00254
00255
00256
00257
00258
00259
00260
00261 class RotationVector {
00262 public:
00263
00264 double sinus;
00265
00266
00267 double cosinus;
00268
00269
00270 RotationVector() {sinus=0.0; cosinus=1.0;}
00271
00272
00273
00274
00275 RotationVector(const double a) {sinus = sin(a); cosinus = cos(a);};
00276
00277
00278
00279
00280
00281
00282 RotationVector(double c, double s) {
00283 double l = s * s + c * c;
00284 if(abs(l - 1) > 0.000001)
00285 {
00286 l = sqrt(l);
00287 s /= l;
00288 c /= l;
00289 }
00290 sinus = s; cosinus=c;
00291 }
00292
00293
00294
00295
00296
00297 RotationVector& operator=(const RotationVector& other) {
00298 this->sinus = other.sinus;
00299 this->cosinus = other.cosinus;
00300 return *this;
00301 }
00302
00303
00304
00305
00306 RotationVector(const RotationVector& r) {*this = r;};
00307
00308
00309
00310
00311 RotationVector invert() {
00312 this->sinus = -this->sinus;
00313 return *this;
00314 }
00315
00316
00317
00318
00319
00320 RotationVector fromAngle(const double a) {
00321 sinus = sin(a);
00322 cosinus = cos(a);
00323 return *this;
00324 }
00325
00326
00327
00328
00329
00330 double getAngle() const {return atan2(sinus,cosinus);}
00331
00332
00333
00334
00335
00336
00337 RotationVector fromDegrees(const double d) {
00338 double a = ::fromDegrees(d);
00339 sinus = sin(a);
00340 cosinus = cos(a);
00341 return *this;
00342 }
00343
00344
00345
00346
00347 double toDegrees() const {return ::toDegrees(atan2(sinus,cosinus));}
00348
00349
00350
00351
00352 Vector2<double> operator*(const Vector2<double>& v) const {
00353 return Vector2<double>(cosinus * v.x - sinus * v.y,sinus * v.x + cosinus * v.y);
00354 }
00355
00356
00357
00358
00359
00360 RotationVector operator*(const RotationVector& other) const {
00361 return RotationVector(cosinus * other.cosinus - sinus * other.sinus,
00362 sinus * other.cosinus + cosinus * other.sinus);
00363 }
00364
00365
00366
00367
00368
00369 RotationVector operator+(const RotationVector& other) const {
00370 return RotationVector(cosinus * other.cosinus - sinus * other.sinus,
00371 sinus * other.cosinus + cosinus * other.sinus);
00372 }
00373
00374
00375
00376
00377
00378 RotationVector operator-(const RotationVector& other) const {
00379 return RotationVector(cosinus * other.cosinus + sinus * other.sinus,
00380 sinus * other.cosinus - cosinus * other.sinus);
00381 }
00382
00383
00384
00385
00386
00387 RotationVector operator*=(const RotationVector& other) {
00388 return *this = RotationVector(cosinus * other.cosinus - sinus * other.sinus,
00389 sinus * other.cosinus + cosinus * other.sinus);
00390 }
00391
00392
00393
00394
00395
00396 RotationVector operator+=(const RotationVector& other) {
00397 return *this = RotationVector(cosinus * other.cosinus - sinus * other.sinus,
00398 sinus * other.cosinus + cosinus * other.sinus);
00399 }
00400
00401
00402
00403
00404
00405 RotationVector operator-=(const RotationVector& other) {
00406 return *this = RotationVector(cosinus * other.cosinus + sinus * other.sinus,
00407 sinus * other.cosinus - cosinus * other.sinus);
00408 }
00409
00410
00411
00412
00413
00414
00415 bool operator==(const RotationVector& other) const {
00416 return sinus == other.sinus && cosinus == other.cosinus;
00417 }
00418
00419
00420
00421
00422
00423 bool operator!=(const RotationVector& other) const {
00424 return sinus != other.sinus || cosinus != other.cosinus;
00425 }
00426
00427
00428 RotationVector operator+() const {return *this;}
00429
00430
00431 RotationVector operator-() const {return RotationVector(cosinus,-sinus);}
00432 };
00433
00434
00435
00436
00437
00438
00439
00440 In& operator>>(In& stream, RotationVector& rotationVector);
00441
00442
00443
00444
00445
00446
00447
00448 Out& operator<<(Out& stream, const RotationVector& rotationVector);
00449
00450
00451
00452
00453 class Pose2D {
00454 public:
00455
00456 RotationVector rotation;
00457
00458
00459 Vector2<double> translation;
00460
00461
00462 Pose2D(){};
00463
00464
00465
00466
00467
00468
00469
00470
00471
00472
00473
00474
00475
00476 Pose2D(const RotationVector& rot, const Vector2<double>& trans) {rotation = rot; translation=trans;}
00477
00478
00479
00480
00481
00482
00483
00484
00485
00486
00487
00488
00489
00490
00491
00492 Pose2D(const RotationVector& rot, const double x, const double y) {
00493 rotation = rot; translation.x = x, translation.y = y;
00494 }
00495
00496
00497
00498
00499
00500 Pose2D(const double rot) {rotation = rot;}
00501
00502
00503
00504
00505
00506 Pose2D(const RotationVector& rot) {rotation = rot;}
00507
00508
00509
00510
00511
00512 Pose2D(const Vector2<double>& trans) {translation = trans;}
00513
00514
00515
00516
00517
00518 Pose2D(const Vector2<int>& trans) {translation.x = trans.x; translation.y = trans.y;}
00519
00520
00521
00522
00523
00524
00525 Pose2D(const double x, const double y) {translation = Vector2<double>(x,y); }
00526
00527
00528
00529
00530
00531 Pose2D& operator=(const Pose2D& other){
00532 rotation = other.rotation;
00533 translation = other.translation;
00534 return *this;
00535 }
00536
00537
00538
00539
00540 Pose2D(const Pose2D& other) {*this = other;}
00541
00542
00543
00544
00545 double getAngle() const {return rotation.getAngle();}
00546
00547
00548
00549
00550 Pose2D fromAngle(const double a) {rotation.fromAngle(a); return *this;}
00551
00552
00553
00554
00555 double getSin() const {return rotation.sinus;}
00556
00557
00558
00559
00560 double getCos() const {return rotation.sinus;}
00561
00562
00563
00564
00565
00566
00567 Vector2<double> operator*(const Vector2<double>& point) const {
00568 return translation + rotation * point;
00569 }
00570
00571
00572
00573
00574
00575 bool operator==(const Pose2D& other) const {
00576 return translation == other.translation && rotation == other.rotation;
00577 }
00578
00579
00580
00581
00582
00583 bool operator!=(const Pose2D& other) const {return !(*this == other);}
00584
00585
00586
00587
00588
00589 Pose2D& operator+=(const Pose2D& other) {
00590 translation = *this * other.translation;
00591 rotation *= other.rotation;
00592 return *this;
00593 }
00594
00595
00596
00597
00598
00599 Pose2D operator+(const Pose2D& other) const {
00600 return Pose2D(*this) += other;
00601 }
00602
00603
00604
00605
00606
00607 Pose2D& operator-=(const Pose2D& other) {
00608 translation -= other.translation;
00609 Pose2D p(-other.rotation);
00610 return *this = p + *this;
00611 }
00612
00613
00614
00615
00616
00617 Pose2D operator-(const Pose2D& other) const {
00618 return Pose2D(*this) -= other;
00619 }
00620
00621
00622
00623
00624
00625 Pose2D& conc(const Pose2D& other) {
00626 return *this += other;
00627 }
00628
00629
00630
00631
00632
00633 Pose2D& translate(const Vector2<double>& trans) {
00634 translation = *this * trans;
00635 return *this;
00636 }
00637
00638
00639
00640
00641
00642
00643 Pose2D& translate(const double x, const double y) {
00644 translation = *this * Vector2<double>(x,y);
00645 return *this;
00646 }
00647
00648
00649
00650
00651
00652 Pose2D& rotate(const double angle) {
00653 rotation *= RotationVector(angle);
00654 return *this;
00655 }
00656
00657
00658
00659
00660
00661 Pose2D& rotate(const RotationVector& rot) {
00662 rotation *= rot;
00663 return *this;
00664 }
00665
00666
00667
00668
00669
00670
00671
00672 static Pose2D random(const Range<double>& x,
00673 const Range<double>& y,
00674 const Range<double>& angle)
00675 {
00676 return Pose2D(::random() * (angle.max - angle.min) + angle.min,
00677 Vector2<double>(::random() * (x.max - x.min) + x.min,
00678 ::random() * (y.max - y.min) + y.min));
00679 }
00680 };
00681
00682 #endif //Pose2Dnew
00683
00684
00685
00686
00687
00688
00689
00690 In& operator>>(In& stream, Pose2D& pose2D);
00691
00692
00693
00694
00695
00696
00697
00698 Out& operator<<(Out& stream, const Pose2D& pose2D);
00699
00700
00701
00702
00703
00704
00705
00706
00707
00708
00709
00710 #endif // __Pose2D_h__