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 LIGHTWEIGHT_GEOM_DATA_H
00029 #define LIGHTWEIGHT_GEOM_DATA_H
00030
00031 #include <mrpt/utils/utils_defs.h>
00032 #include <mrpt/utils/stl_extensions.h>
00033 #include <mrpt/utils/TPixelCoord.h>
00034
00035 #include <mrpt/math/math_frwds.h>
00036
00037
00038
00039 namespace mrpt {
00040 namespace poses {
00041 template <class DERIVEDCLASS> class CPoseOrPoint;
00042 class CPoint2D;
00043 class CPoint3D;
00044 class CPose2D;
00045 class CPose3D;
00046 }
00047 namespace utils { class CStream; }
00048 }
00049
00050 namespace mrpt {
00051 namespace math {
00052 using namespace mrpt::utils;
00053
00054 struct TPoint2D;
00055 struct TPose2D;
00056 struct TPoint3D;
00057 struct TPose3D;
00058 struct TPose3DQuat;
00059
00060
00061
00062
00063
00064
00065 #pragma pack(push,1)
00066
00067
00068
00069
00070
00071
00072 struct BASE_IMPEXP TPoint2D {
00073
00074
00075
00076 double x;
00077
00078
00079
00080 double y;
00081
00082
00083
00084
00085 explicit TPoint2D(const TPose2D &p);
00086
00087
00088
00089
00090 explicit TPoint2D(const TPoint3D &p);
00091
00092
00093
00094
00095 explicit TPoint2D(const TPose3D &p);
00096
00097
00098
00099
00100 template <class DERIVEDCLASS>
00101 explicit TPoint2D(const mrpt::poses::CPoseOrPoint<DERIVEDCLASS> &p) :x(p.x()),y(p.y()) {}
00102
00103
00104 inline TPoint2D(const mrpt::utils::TPixelCoordf &p) :x(p.x),y(p.y) {}
00105
00106
00107 TPoint2D(const mrpt::poses::CPoint2D &p);
00108
00109
00110
00111 inline TPoint2D(double xx,double yy):x(xx),y(yy) {}
00112
00113
00114
00115 inline TPoint2D() {}
00116
00117
00118
00119 inline double &operator[](size_t i) {
00120 return (&x)[i];
00121 }
00122
00123
00124
00125 inline const double &operator[](size_t i) const {
00126 return (&x)[i];
00127 }
00128
00129
00130
00131 inline void getAsVector(vector_double &v) const {
00132 v.resize(2);
00133 v[0]=x; v[1]=y;
00134 }
00135
00136 bool operator<(const TPoint2D &p) const;
00137
00138 inline TPoint2D &operator+=(const TPoint2D &p) {
00139 x+=p.x;
00140 y+=p.y;
00141 return *this;
00142 }
00143
00144 inline TPoint2D &operator-=(const TPoint2D &p) {
00145 x-=p.x;
00146 y-=p.y;
00147 return *this;
00148 }
00149
00150 inline TPoint2D &operator*=(double d) {
00151 x*=d;
00152 y*=d;
00153 return *this;
00154 }
00155
00156 inline TPoint2D &operator/=(double d) {
00157 x/=d;
00158 y/=d;
00159 return *this;
00160 }
00161
00162 inline TPoint2D operator+(const TPoint2D &p) const {
00163 TPoint2D r(*this);
00164 return r+=p;
00165 }
00166
00167 inline TPoint2D operator-(const TPoint2D &p) const {
00168 TPoint2D r(*this);
00169 return r-=p;
00170 }
00171
00172 inline TPoint2D operator*(double d) const {
00173 TPoint2D r(*this);
00174 return r*=d;
00175 }
00176
00177 inline TPoint2D operator/(double d) const {
00178 TPoint2D r(*this);
00179 return r/=d;
00180 }
00181
00182
00183
00184 void asString(std::string &s) const { s = mrpt::format("[%f %f]",x,y); }
00185 inline std::string asString() const { std::string s; asString(s); return s; }
00186
00187
00188
00189
00190
00191 void fromString(const std::string &s);
00192 static size_t size() { return 2; }
00193 };
00194
00195
00196
00197
00198
00199 struct BASE_IMPEXP TPose2D {
00200
00201
00202
00203 double x;
00204
00205
00206
00207 double y;
00208
00209
00210
00211 double phi;
00212
00213
00214
00215
00216 TPose2D(const TPoint2D &p);
00217
00218
00219
00220
00221 explicit TPose2D(const TPoint3D &p);
00222
00223
00224
00225
00226 explicit TPose2D(const TPose3D &p);
00227
00228
00229
00230
00231 TPose2D(const mrpt::poses::CPose2D &p);
00232
00233
00234
00235 inline TPose2D(double xx,double yy,double pphi):x(xx),y(yy),phi(pphi) {}
00236
00237
00238
00239 inline TPose2D() {}
00240
00241
00242
00243 inline double &operator[](size_t i) {
00244 return (&x)[i];
00245 }
00246
00247
00248
00249 inline const double &operator[](size_t i) const {
00250 return (&x)[i];
00251 }
00252
00253
00254
00255 inline void getAsVector(vector_double &v) const {
00256 v.resize(3);
00257 v[0]=x; v[1]=y; v[2]=phi;
00258 }
00259
00260
00261
00262 void asString(std::string &s) const { s = mrpt::format("[%f %f %f]",x,y,RAD2DEG(phi)); }
00263 inline std::string asString() const { std::string s; asString(s); return s; }
00264
00265
00266
00267
00268
00269 void fromString(const std::string &s);
00270 static size_t size() { return 3; }
00271 };
00272
00273
00274
00275
00276 struct BASE_IMPEXP TPoint3Df
00277 {
00278 float x;
00279 float y;
00280 float z;
00281
00282 inline TPoint3Df() { }
00283 inline TPoint3Df(const float xx,const float yy,const float zz) : x(xx), y(yy),z(zz) { }
00284 inline TPoint3Df & operator +=(const TPoint3Df &p) { x+=p.x; y+=p.y; z+=p.z; return *this; }
00285 inline TPoint3Df operator *(const float s) { return TPoint3Df(x*s,y*s,z*s); }
00286 };
00287
00288
00289
00290
00291
00292 struct BASE_IMPEXP TPoint3D {
00293 double x;
00294 double y;
00295 double z;
00296
00297
00298 inline TPoint3D(double xx,double yy,double zz):x(xx),y(yy),z(zz) {}
00299
00300 inline TPoint3D() {}
00301
00302 explicit inline TPoint3D(const TPoint3Df &p):x(p.x),y(p.y),z(p.z) {}
00303
00304
00305
00306
00307 TPoint3D(const TPoint2D &p);
00308
00309
00310
00311
00312 explicit TPoint3D(const TPose2D &p);
00313
00314
00315
00316
00317 explicit TPoint3D(const TPose3D &p);
00318
00319
00320
00321
00322 TPoint3D(const mrpt::poses::CPoint3D &p);
00323
00324
00325
00326
00327 explicit TPoint3D(const mrpt::poses::CPose3D &p);
00328
00329
00330
00331 inline double &operator[](size_t i) {
00332 return (&x)[i];
00333 }
00334
00335
00336
00337 inline const double &operator[](size_t i) const {
00338 return (&x)[i];
00339 }
00340
00341
00342
00343 inline double distanceTo(const TPoint3D &p) const {
00344 return sqrt(square(p.x-x)+square(p.y-y)+square(p.z-z));
00345 }
00346
00347
00348
00349 inline double sqrDistanceTo(const TPoint3D &p) const {
00350 return square(p.x-x)+square(p.y-y)+square(p.z-z);
00351 }
00352
00353
00354
00355 inline double norm() const {
00356 return sqrt(square(x)+square(y)+square(z));
00357 }
00358
00359
00360
00361 inline TPoint3D &operator*=(const double f) {
00362 x*=f;y*=f;z*=f;
00363 return *this;
00364 }
00365
00366
00367
00368 void getAsVector(vector_double &v) const {
00369 v.resize(3);
00370 v[0]=x; v[1]=y; v[2]=z;
00371 }
00372
00373
00374
00375 inline TPoint3D &operator+=(const TPoint3D &p) {
00376 x+=p.x;
00377 y+=p.y;
00378 z+=p.z;
00379 return *this;
00380 }
00381
00382
00383
00384 inline TPoint3D &operator-=(const TPoint3D &p) {
00385 x-=p.x;
00386 y-=p.y;
00387 z-=p.z;
00388 return *this;
00389 }
00390
00391
00392
00393 inline TPoint3D operator+(const TPoint3D &p) const {
00394 return TPoint3D(x+p.x,y+p.y,z+p.z);
00395 }
00396
00397
00398
00399 inline TPoint3D operator-(const TPoint3D &p) const {
00400 return TPoint3D(x-p.x,y-p.y,z-p.z);
00401 }
00402
00403 inline TPoint3D operator*(double d) const {
00404 return TPoint3D(x*d,y*d,z*d);
00405 }
00406
00407 inline TPoint3D operator/(double d) const {
00408 return TPoint3D(x/d,y/d,z/d);
00409 }
00410
00411 bool operator<(const TPoint3D &p) const;
00412
00413
00414
00415
00416 void asString(std::string &s) const { s = mrpt::format("[%f %f %f]",x,y,z); }
00417 inline std::string asString() const { std::string s; asString(s); return s; }
00418
00419
00420
00421
00422
00423 void fromString(const std::string &s);
00424 static size_t size() { return 3; }
00425 };
00426
00427
00428
00429
00430
00431 struct BASE_IMPEXP TPose3D {
00432
00433
00434
00435 double x;
00436
00437
00438
00439 double y;
00440
00441
00442
00443 double z;
00444
00445
00446
00447 double yaw;
00448
00449
00450
00451 double pitch;
00452
00453
00454
00455 double roll;
00456
00457
00458
00459
00460 TPose3D(const TPoint2D &p);
00461
00462
00463
00464
00465 TPose3D(const TPose2D &p);
00466
00467
00468
00469
00470 TPose3D(const TPoint3D &p);
00471
00472
00473
00474
00475 TPose3D(const mrpt::poses::CPose3D &p);
00476
00477
00478
00479 TPose3D(double _x,double _y,double _z,double _yaw,double _pitch,double _roll):x(_x),y(_y),z(_z),yaw(_yaw),pitch(_pitch),roll(_roll) {}
00480
00481
00482
00483 inline TPose3D() {}
00484
00485
00486
00487 inline double &operator[](size_t i) {
00488 return (&x)[i];
00489 }
00490
00491
00492
00493 inline const double &operator[](size_t i) const {
00494 return (&x)[i];
00495 }
00496
00497
00498
00499 double norm() const {
00500 return sqrt(square(x)+square(y)+square(z));
00501 }
00502
00503
00504
00505 void getAsVector(vector_double &v) const {
00506 v.resize(6);
00507 v[0]=x; v[1]=y; v[2]=z; v[3]=yaw; v[4]=pitch; v[5]=roll;
00508 }
00509
00510
00511
00512 void asString(std::string &s) const { s = mrpt::format("[%f %f %f %f %f %f]",x,y,z,RAD2DEG(yaw),RAD2DEG(pitch),RAD2DEG(roll)); }
00513 inline std::string asString() const { std::string s; asString(s); return s; }
00514
00515
00516
00517
00518
00519 void fromString(const std::string &s);
00520 static size_t size() { return 6; }
00521 };
00522
00523
00524
00525
00526 struct BASE_IMPEXP TPose3DQuat {
00527 double x;
00528 double y;
00529 double z;
00530 double qr;
00531 double qx;
00532 double qy;
00533 double qz;
00534
00535
00536 inline TPose3DQuat(double _x,double _y,double _z,double _qr,double _qx, double _qy, double _qz):x(_x),y(_y),z(_z),qr(_qr),qx(_qx),qy(_qy),qz(_qz) { }
00537
00538 inline TPose3DQuat() {}
00539
00540 TPose3DQuat(const mrpt::poses::CPose3DQuat &p);
00541
00542
00543 inline double &operator[](size_t i) {
00544 return (&x)[i];
00545 }
00546
00547 inline const double &operator[](size_t i) const {
00548 return (&x)[i];
00549 }
00550
00551 double norm() const {
00552 return sqrt(square(x)+square(y)+square(z));
00553 }
00554
00555 void getAsVector(vector_double &v) const {
00556 v.resize(7);
00557 for (size_t i=0;i<7;i++) v[i]=(*this)[i];
00558 }
00559
00560
00561
00562 void asString(std::string &s) const { s = mrpt::format("[%f %f %f %f %f %f %f]",x,y,z,qr,qx,qy,qz); }
00563 inline std::string asString() const { std::string s; asString(s); return s; }
00564
00565
00566
00567
00568
00569 void fromString(const std::string &s);
00570 static size_t size() { return 7; }
00571 };
00572 #pragma pack(pop)
00573
00574
00575 std::ostream BASE_IMPEXP & operator << (std::ostream& o, const TPoint2D & p);
00576 std::ostream BASE_IMPEXP & operator << (std::ostream& o, const TPoint3D & p);
00577 std::ostream BASE_IMPEXP & operator << (std::ostream& o, const TPose2D & p);
00578 std::ostream BASE_IMPEXP & operator << (std::ostream& o, const TPose3D & p);
00579 std::ostream BASE_IMPEXP & operator << (std::ostream& o, const TPose3DQuat & p);
00580
00581
00582
00583
00584
00585 inline TPoint3D operator-(const TPoint3D &p1) {
00586 return TPoint3D(-p1.x,-p1.y,-p1.z);
00587 }
00588
00589
00590
00591 inline bool operator==(const TPoint2D &p1,const TPoint2D &p2) {
00592 return (p1.x==p2.x)&&(p1.y==p2.y);
00593 }
00594
00595
00596
00597 inline bool operator!=(const TPoint2D &p1,const TPoint2D &p2) {
00598 return (p1.x!=p2.x)||(p1.y!=p2.y);
00599 }
00600
00601
00602
00603 inline bool operator==(const TPoint3D &p1,const TPoint3D &p2) {
00604 return (p1.x==p2.x)&&(p1.y==p2.y)&&(p1.z==p2.z);
00605 }
00606
00607
00608
00609 inline bool operator!=(const TPoint3D &p1,const TPoint3D &p2) {
00610 return (p1.x!=p2.x)||(p1.y!=p2.y)||(p1.z!=p2.z);
00611 }
00612
00613
00614
00615 inline bool operator==(const TPose2D &p1,const TPose2D &p2) {
00616 return (p1.x==p2.x)&&(p1.y==p2.y)&&(mrpt::math::wrapTo2Pi(p1.phi)==mrpt::math::wrapTo2Pi(p2.phi));
00617 }
00618
00619
00620
00621 inline bool operator!=(const TPose2D &p1,const TPose2D &p2) {
00622 return (p1.x!=p2.x)||(p1.y!=p2.y)||(mrpt::math::wrapTo2Pi(p1.phi)!=mrpt::math::wrapTo2Pi(p2.phi));
00623 }
00624
00625
00626
00627 inline bool operator==(const TPose3D &p1,const TPose3D &p2) {
00628 return (p1.x==p2.x)&&(p1.y==p2.y)&&(p1.z==p2.z)&&(mrpt::math::wrapTo2Pi(p1.yaw)==mrpt::math::wrapTo2Pi(p2.yaw))&&(mrpt::math::wrapTo2Pi(p1.pitch)==mrpt::math::wrapTo2Pi(p2.pitch))&&(mrpt::math::wrapTo2Pi(p1.roll)==mrpt::math::wrapTo2Pi(p2.roll));
00629 }
00630
00631
00632
00633 inline bool operator!=(const TPose3D &p1,const TPose3D &p2) {
00634 return (p1.x!=p2.x)||(p1.y!=p2.y)||(p1.z!=p2.z)||(mrpt::math::wrapTo2Pi(p1.yaw)!=mrpt::math::wrapTo2Pi(p2.yaw))||(mrpt::math::wrapTo2Pi(p1.pitch)!=mrpt::math::wrapTo2Pi(p2.pitch))||(mrpt::math::wrapTo2Pi(p1.roll)!=mrpt::math::wrapTo2Pi(p2.roll));
00635 }
00636
00637 struct BASE_IMPEXP TSegment3D;
00638 struct BASE_IMPEXP TLine3D;
00639 class BASE_IMPEXP TPolygon3D;
00640 struct BASE_IMPEXP TObject3D;
00641
00642
00643 #pragma pack(push,1)
00644
00645
00646
00647
00648 struct BASE_IMPEXP TSegment2D {
00649 public:
00650
00651
00652
00653 TPoint2D point1;
00654
00655
00656
00657 TPoint2D point2;
00658
00659
00660
00661 double length() const;
00662
00663
00664
00665 double distance(const TPoint2D &point) const;
00666
00667
00668
00669 double signedDistance(const TPoint2D &point) const;
00670
00671
00672
00673 bool contains(const TPoint2D &point) const;
00674
00675
00676
00677 inline TPoint2D &operator[](size_t i) {
00678 return (&point1)[i];
00679 }
00680
00681
00682
00683 inline const TPoint2D &operator[](size_t i) const {
00684 return (&point1)[i];
00685 }
00686
00687
00688
00689 void generate3DObject(TSegment3D &s) const;
00690
00691
00692
00693 inline void getCenter(TPoint2D &p) const {
00694 p.x=(point1.x+point2.x)/2;
00695 p.y=(point1.y+point2.y)/2;
00696 }
00697
00698
00699
00700 TSegment2D(const TPoint2D &p1,const TPoint2D &p2):point1(p1),point2(p2) {}
00701
00702
00703
00704 TSegment2D() {}
00705
00706
00707
00708 explicit TSegment2D(const TSegment3D &s);
00709
00710 bool operator<(const TSegment2D &s) const;
00711 };
00712
00713
00714
00715
00716 struct BASE_IMPEXP TSegment3D {
00717 public:
00718
00719
00720
00721 TPoint3D point1;
00722
00723
00724
00725 TPoint3D point2;
00726
00727
00728
00729 double length() const;
00730
00731
00732
00733 double distance(const TPoint3D &point) const;
00734
00735
00736
00737 bool contains(const TPoint3D &point) const;
00738
00739
00740
00741 inline TPoint3D &operator[](size_t i) {
00742 return (&point1)[i];
00743 }
00744
00745
00746
00747 inline const TPoint3D &operator[](size_t i) const {
00748 return (&point1)[i];
00749 }
00750
00751
00752
00753 inline void generate2DObject(TSegment2D &s) const {
00754 s=TSegment2D(*this);
00755 }
00756
00757
00758
00759 inline void getCenter(TPoint3D &p) const {
00760 p.x=(point1.x+point2.x)/2;
00761 p.y=(point1.y+point2.y)/2;
00762 p.z=(point1.z+point2.z)/2;
00763 }
00764
00765
00766
00767 TSegment3D(const TPoint3D &p1,const TPoint3D &p2):point1(p1),point2(p2) {}
00768
00769
00770
00771 TSegment3D() {}
00772
00773
00774
00775 TSegment3D(const TSegment2D &s):point1(s.point1),point2(s.point2) {}
00776
00777 bool operator<(const TSegment3D &s) const;
00778 };
00779 #pragma pack(pop)
00780
00781 inline bool operator==(const TSegment2D &s1,const TSegment2D &s2) {
00782 return (s1.point1==s2.point1)&&(s1.point2==s2.point2);
00783 }
00784
00785 inline bool operator!=(const TSegment2D &s1,const TSegment2D &s2) {
00786 return (s1.point1!=s1.point1)||(s1.point2!=s2.point2);
00787 }
00788
00789 inline bool operator==(const TSegment3D &s1,const TSegment3D &s2) {
00790 return (s1.point1==s2.point1)&&(s1.point2==s2.point2);
00791 }
00792
00793 inline bool operator!=(const TSegment3D &s1,const TSegment3D &s2) {
00794 return (s1.point1!=s1.point1)||(s1.point2!=s2.point2);
00795 }
00796
00797
00798
00799
00800
00801 struct BASE_IMPEXP TLine2D {
00802 public:
00803
00804
00805
00806 double coefs[3];
00807
00808
00809
00810 double evaluatePoint(const TPoint2D &point) const;
00811
00812
00813
00814 bool contains(const TPoint2D &point) const;
00815
00816
00817
00818 double distance(const TPoint2D &point) const;
00819
00820
00821
00822 double signedDistance(const TPoint2D &point) const;
00823
00824
00825
00826 void getNormalVector(double (&vector)[2]) const;
00827
00828
00829
00830 void unitarize();
00831
00832
00833
00834 inline void getUnitaryNormalVector(double (&vector)[2]) {
00835 unitarize();
00836 getNormalVector(vector);
00837 }
00838
00839
00840
00841 void getDirectorVector(double (&vector)[2]) const;
00842
00843
00844
00845 inline void getUnitaryDirectorVector(double (&vector)[2]) {
00846 unitarize();
00847 getDirectorVector(vector);
00848 }
00849
00850
00851
00852 void generate3DObject(TLine3D &l) const;
00853
00854
00855
00856
00857 void getAsPose2D(mrpt::poses::CPose2D &outPose) const;
00858
00859
00860
00861
00862
00863 void getAsPose2DForcingOrigin(const TPoint2D &origin,mrpt::poses::CPose2D &outPose) const;
00864
00865
00866
00867
00868 TLine2D(const TPoint2D &p1,const TPoint2D &p2) throw(std::logic_error);
00869
00870
00871
00872 explicit TLine2D(const TSegment2D &s);
00873
00874
00875
00876 TLine2D() {}
00877
00878
00879
00880 inline TLine2D(double A,double B,double C) {
00881 coefs[0]=A;
00882 coefs[1]=B;
00883 coefs[2]=C;
00884 }
00885
00886
00887
00888
00889 explicit TLine2D(const TLine3D &l);
00890 };
00891
00892
00893
00894
00895
00896 struct BASE_IMPEXP TLine3D {
00897 public:
00898
00899
00900
00901 TPoint3D pBase;
00902
00903
00904
00905 double director[3];
00906
00907
00908
00909 bool contains(const TPoint3D &point) const;
00910
00911
00912
00913 double distance(const TPoint3D &point) const;
00914
00915
00916
00917 void unitarize();
00918
00919
00920
00921 inline void getDirectorVector(double (&vector)[3]) const {
00922 for (size_t i=0;i<3;i++) vector[i]=director[i];
00923 }
00924
00925
00926
00927 inline void getUnitaryDirectorVector(double (&vector)[3]) {
00928 unitarize();
00929 getDirectorVector(vector);
00930 }
00931
00932
00933
00934
00935 inline void generate2DObject(TLine2D &l) const {
00936 l=TLine2D(*this);
00937 }
00938
00939
00940
00941
00942 TLine3D(const TPoint3D &p1,const TPoint3D &p2) throw(std::logic_error);
00943
00944
00945
00946 explicit TLine3D(const TSegment3D &s);
00947
00948
00949
00950 TLine3D() {}
00951
00952
00953
00954 TLine3D(const TLine2D &l);
00955 };
00956
00957
00958
00959
00960
00961 struct BASE_IMPEXP TPlane {
00962 public:
00963
00964
00965
00966 double coefs[4];
00967
00968
00969
00970 double evaluatePoint(const TPoint3D &point) const;
00971
00972
00973
00974 bool contains(const TPoint3D &point) const;
00975
00976
00977
00978 inline bool contains(const TSegment3D &segment) const {
00979 return contains(segment.point1)&&contains(segment.point2);
00980 }
00981
00982
00983
00984 bool contains(const TLine3D &line) const;
00985
00986
00987
00988 double distance(const TPoint3D &point) const;
00989
00990
00991
00992 double distance(const TLine3D &line) const;
00993
00994
00995
00996 void getNormalVector(double (&vec)[3]) const;
00997
00998
00999
01000 void unitarize();
01001
01002
01003
01004 inline void getUnitaryNormalVector(double (&vec)[3]) {
01005 unitarize();
01006 getNormalVector(vec);
01007 }
01008
01009
01010
01011 void getAsPose3D(mrpt::poses::CPose3D &outPose);
01012
01013
01014
01015 inline void getAsPose3D(mrpt::poses::CPose3D &outPose) const {
01016 TPlane p=*this;
01017 p.getAsPose3D(outPose);
01018 }
01019
01020
01021
01022
01023 void getAsPose3DForcingOrigin(const TPoint3D &newOrigin,mrpt::poses::CPose3D &pose);
01024
01025
01026
01027
01028 inline void getAsPose3DForcingOrigin(const TPoint3D &newOrigin,mrpt::poses::CPose3D &pose) const {
01029 TPlane p=*this;
01030 p.getAsPose3DForcingOrigin(newOrigin,pose);
01031 }
01032
01033
01034
01035
01036 TPlane(const TPoint3D &p1,const TPoint3D &p2,const TPoint3D &p3) throw(std::logic_error);
01037
01038
01039
01040
01041 TPlane(const TPoint3D &p1,const TLine3D &r2) throw(std::logic_error);
01042
01043
01044
01045
01046 TPlane(const TLine3D &r1,const TLine3D &r2) throw(std::logic_error);
01047
01048
01049
01050 TPlane() {}
01051
01052
01053
01054 inline TPlane(double A,double B,double C,double D) {
01055 coefs[0]=A;
01056 coefs[1]=B;
01057 coefs[2]=C;
01058 coefs[3]=D;
01059 }
01060
01061
01062
01063 inline TPlane(const double (&vec)[4]) {
01064 for (size_t i=0;i<4;i++) coefs[i]=vec[i];
01065 }
01066 };
01067
01068 typedef TPlane TPlane3D;
01069
01070
01071
01072
01073
01074 class BASE_IMPEXP TPolygon2D:public std::vector<TPoint2D> {
01075 public:
01076
01077
01078
01079 double distance(const TPoint2D &point) const;
01080
01081
01082
01083 bool contains(const TPoint2D &point) const;
01084
01085
01086
01087 void getAsSegmentList(std::vector<TSegment2D> &v) const;
01088
01089
01090
01091 void generate3DObject(TPolygon3D &p) const;
01092
01093
01094
01095 void getCenter(TPoint2D &p) const;
01096
01097
01098
01099 bool isConvex() const;
01100
01101
01102
01103
01104 void removeRepeatedVertices();
01105
01106
01107
01108
01109 void removeRedundantVertices();
01110
01111
01112
01113
01114 void getPlotData(std::vector<double> &x,std::vector<double> &y) const;
01115
01116
01117
01118 TPolygon2D():std::vector<TPoint2D>() {}
01119
01120
01121
01122 explicit TPolygon2D(size_t N):std::vector<TPoint2D>(N) {}
01123
01124
01125
01126 TPolygon2D(const std::vector<TPoint2D> &v):std::vector<TPoint2D>(v) {}
01127
01128
01129
01130 explicit TPolygon2D(const TPolygon3D &p);
01131
01132
01133
01134
01135 static void createRegularPolygon(size_t numEdges,double radius,TPolygon2D &poly);
01136
01137
01138
01139
01140 static inline void createRegularPolygon(size_t numEdges,double radius,TPolygon2D &poly,const mrpt::poses::CPose2D &pose);
01141 };
01142
01143
01144
01145
01146
01147 class BASE_IMPEXP TPolygon3D:public std::vector<TPoint3D> {
01148 public:
01149
01150
01151
01152 double distance(const TPoint3D &point) const;
01153
01154
01155
01156 bool contains(const TPoint3D &point) const;
01157
01158
01159
01160 void getAsSegmentList(std::vector<TSegment3D> &v) const;
01161
01162
01163
01164 bool getPlane(TPlane &p) const;
01165
01166
01167
01168
01169 void getBestFittingPlane(TPlane &p) const;
01170
01171
01172
01173
01174 inline void generate2DObject(TPolygon2D &p) const {
01175 p=TPolygon2D(*this);
01176 }
01177
01178
01179
01180 void getCenter(TPoint3D &p) const;
01181
01182
01183
01184
01185 bool isSkew() const;
01186
01187
01188
01189 void removeRepeatedVertices();
01190
01191
01192
01193 void removeRedundantVertices();
01194
01195
01196
01197 TPolygon3D():std::vector<TPoint3D>() {}
01198
01199
01200
01201 explicit TPolygon3D(size_t N):std::vector<TPoint3D>(N) {}
01202
01203
01204
01205 TPolygon3D(const std::vector<TPoint3D> &v):std::vector<TPoint3D>(v) {}
01206
01207
01208
01209 TPolygon3D(const TPolygon2D &p);
01210
01211
01212
01213
01214 static void createRegularPolygon(size_t numEdges,double radius,TPolygon3D &poly);
01215
01216
01217
01218
01219 static inline void createRegularPolygon(size_t numEdges,double radius,TPolygon3D &poly,const mrpt::poses::CPose3D &pose);
01220 };
01221
01222
01223
01224
01225
01226 const unsigned char GEOMETRIC_TYPE_POINT=0;
01227
01228
01229
01230
01231 const unsigned char GEOMETRIC_TYPE_SEGMENT=1;
01232
01233
01234
01235
01236 const unsigned char GEOMETRIC_TYPE_LINE=2;
01237
01238
01239
01240
01241 const unsigned char GEOMETRIC_TYPE_POLYGON=3;
01242
01243
01244
01245
01246 const unsigned char GEOMETRIC_TYPE_PLANE=4;
01247
01248
01249
01250
01251 const unsigned char GEOMETRIC_TYPE_UNDEFINED=255;
01252
01253
01254
01255
01256
01257 #ifdef TOBJECTS_USE_UNIONS
01258 struct BASE_IMPEXP TObject2D {
01259 private:
01260
01261
01262
01263 unsigned char type;
01264
01265
01266
01267 union {
01268 TPoint2D *point;
01269 TSegment2D *segment;
01270 TLine2D *line;
01271 TPolygon2D *polygon;
01272 } data;
01273
01274
01275
01276 void destroy() {
01277 switch(type) {
01278 case GEOMETRIC_TYPE_POINT:
01279 delete data.point;
01280 break;
01281 case GEOMETRIC_TYPE_SEGMENT:
01282 delete data.segment;
01283 break;
01284 case GEOMETRIC_TYPE_LINE:
01285 delete data.line;
01286 break;
01287 case GEOMETRIC_TYPE_POLYGON:
01288 delete data.polygon;
01289 break;
01290 }
01291 type=GEOMETRIC_TYPE_UNDEFINED;
01292 }
01293 public:
01294
01295
01296
01297 TObject2D(const TPoint2D &p):type(GEOMETRIC_TYPE_POINT) {
01298 data.point=new TPoint2D(p);
01299 }
01300
01301
01302
01303 TObject2D(const TSegment2D &s):type(GEOMETRIC_TYPE_SEGMENT) {
01304 data.segment=new TSegment2D(s);
01305 }
01306
01307
01308
01309 TObject2D(const TLine2D &r):type(GEOMETRIC_TYPE_LINE) {
01310 data.line=new TLine2D(r);
01311 }
01312
01313
01314
01315 TObject2D(const TPolygon2D &p):type(GEOMETRIC_TYPE_POLYGON) {
01316 data.polygon=new TPolygon2D(p);
01317 }
01318
01319
01320
01321 TObject2D():type(GEOMETRIC_TYPE_UNDEFINED) {}
01322
01323
01324
01325 ~TObject2D() {
01326 destroy();
01327 }
01328
01329
01330
01331 inline bool isPoint() const {
01332 return type==GEOMETRIC_TYPE_POINT;
01333 }
01334
01335
01336
01337 inline bool isSegment() const {
01338 return type==GEOMETRIC_TYPE_SEGMENT;
01339 }
01340
01341
01342
01343 inline bool isLine() const {
01344 return type==GEOMETRIC_TYPE_LINE;
01345 }
01346
01347
01348
01349 inline bool isPolygon() const {
01350 return type==GEOMETRIC_TYPE_POLYGON;
01351 }
01352
01353
01354
01355 inline unsigned char getType() const {
01356 return type;
01357 }
01358
01359
01360
01361 inline bool getPoint(TPoint2D &p) const {
01362 if (isPoint()) {
01363 p=*(data.point);
01364 return true;
01365 } else return false;
01366 }
01367
01368
01369
01370 inline bool getSegment(TSegment2D &s) const {
01371 if (isSegment()) {
01372 s=*(data.segment);
01373 return true;
01374 } else return false;
01375 }
01376
01377
01378
01379 inline bool getLine(TLine2D &r) const {
01380 if (isLine()) {
01381 r=*(data.line);
01382 return true;
01383 } else return false;
01384 }
01385
01386
01387
01388 inline bool getPolygon(TPolygon2D &p) const {
01389 if (isPolygon()) {
01390 p=*(data.polygon);
01391 return true;
01392 } else return false;
01393 }
01394
01395
01396
01397 void operator=(const TObject2D &obj) {
01398 if (this==&obj) return;
01399 destroy();
01400 switch (type=obj.type) {
01401 case GEOMETRIC_TYPE_POINT:
01402 data.point=new TPoint2D(*(obj.data.point));
01403 break;
01404 case GEOMETRIC_TYPE_SEGMENT:
01405 data.segment=new TSegment2D(*(obj.data.segment));
01406 break;
01407 case GEOMETRIC_TYPE_LINE:
01408 data.line=new TLine2D(*(obj.data.line));
01409 break;
01410 case GEOMETRIC_TYPE_POLYGON:
01411 data.polygon=new TPolygon2D(*(obj.data.polygon));
01412 break;
01413 }
01414 }
01415
01416
01417
01418 inline void operator=(const TPoint2D &p) {
01419 destroy();
01420 type=GEOMETRIC_TYPE_POINT;
01421 data.point=new TPoint2D(p);
01422 }
01423
01424
01425
01426 inline void operator=(const TSegment2D &s) {
01427 destroy();
01428 type=GEOMETRIC_TYPE_SEGMENT;
01429 data.segment=new TSegment2D(s);
01430 }
01431
01432
01433
01434 inline void operator=(const TLine2D &l) {
01435 destroy();
01436 type=GEOMETRIC_TYPE_LINE;
01437 data.line=new TLine2D(l);
01438 }
01439
01440
01441
01442 inline void operator=(const TPolygon2D &p) {
01443 destroy();
01444 type=GEOMETRIC_TYPE_POLYGON;
01445 data.polygon=new TPolygon2D(p);
01446 }
01447
01448
01449
01450 void generate3DObject(TObject3D &obj) const;
01451
01452
01453
01454 TObject2D(const TObject2D &obj):type(GEOMETRIC_TYPE_UNDEFINED) {
01455 operator=(obj);
01456 }
01457
01458
01459
01460 static void getPoints(const std::vector<TObject2D> &objs,std::vector<TPoint2D> &pnts);
01461
01462
01463
01464 static void getSegments(const std::vector<TObject2D> &objs,std::vector<TSegment2D> &sgms);
01465
01466
01467
01468 static void getLines(const std::vector<TObject2D> &objs,std::vector<TLine2D> &lins);
01469
01470
01471
01472 static void getPolygons(const std::vector<TObject2D> &objs,std::vector<TPolygon2D> &polys);
01473
01474
01475
01476 static void getPoints(const std::vector<TObject2D> &objs,std::vector<TPoint2D> &pnts,std::vector<TObject2D> &remainder);
01477
01478
01479
01480 static void getSegments(const std::vector<TObject2D> &objs,std::vector<TSegment2D> &sgms,std::vector<TObject2D> &remainder);
01481
01482
01483
01484 static void getLines(const std::vector<TObject2D> &objs,std::vector<TLine2D> &lins,std::vector<TObject2D> &remainder);
01485
01486
01487
01488 static void getPolygons(const std::vector<TObject2D> &objs,std::vector<TPolygon2D> &polys,std::vector<TObject2D> &remainder);
01489 };
01490
01491
01492
01493
01494 struct BASE_IMPEXP TObject3D {
01495 private:
01496
01497
01498
01499 unsigned char type;
01500
01501
01502
01503 union {
01504 TPoint3D *point;
01505 TSegment3D *segment;
01506 TLine3D *line;
01507 TPolygon3D *polygon;
01508 TPlane *plane;
01509 } data;
01510
01511
01512
01513 void destroy() {
01514 switch (type) {
01515 case GEOMETRIC_TYPE_POINT:
01516 delete data.point;
01517 break;
01518 case GEOMETRIC_TYPE_SEGMENT:
01519 delete data.segment;
01520 break;
01521 case GEOMETRIC_TYPE_LINE:
01522 delete data.line;
01523 break;
01524 case GEOMETRIC_TYPE_POLYGON:
01525 delete data.polygon;
01526 break;
01527 case GEOMETRIC_TYPE_PLANE:
01528 delete data.plane;
01529 break;
01530 case GEOMETRIC_TYPE_UNDEFINED:
01531 break;
01532 default:
01533 THROW_EXCEPTION("Invalid TObject2D object");
01534 }
01535 type=GEOMETRIC_TYPE_UNDEFINED;
01536 }
01537 public:
01538
01539
01540
01541 TObject3D(const TPoint3D &p):type(GEOMETRIC_TYPE_POINT) {
01542 data.point=new TPoint3D(p);
01543 }
01544
01545
01546
01547 TObject3D(const TSegment3D &s):type(GEOMETRIC_TYPE_SEGMENT) {
01548 data.segment=new TSegment3D(s);
01549 }
01550
01551
01552
01553 TObject3D(const TLine3D &r):type(GEOMETRIC_TYPE_LINE) {
01554 data.line=new TLine3D(r);
01555 }
01556
01557
01558
01559 TObject3D(const TPolygon3D &p):type(GEOMETRIC_TYPE_POLYGON) {
01560 data.polygon=new TPolygon3D(p);
01561 }
01562
01563
01564
01565 TObject3D(const TPlane &p):type(GEOMETRIC_TYPE_PLANE) {
01566 data.plane=new TPlane(p);
01567 }
01568
01569
01570
01571 TObject3D():type(GEOMETRIC_TYPE_UNDEFINED) {}
01572
01573
01574
01575 ~TObject3D() {
01576 destroy();
01577 }
01578
01579
01580
01581 inline bool isPoint() const {
01582 return type==GEOMETRIC_TYPE_POINT;
01583 }
01584
01585
01586
01587 inline bool isSegment() const {
01588 return type==GEOMETRIC_TYPE_SEGMENT;
01589 }
01590
01591
01592
01593 inline bool isLine() const {
01594 return type==GEOMETRIC_TYPE_LINE;
01595 }
01596
01597
01598
01599 inline bool isPolygon() const {
01600 return type==GEOMETRIC_TYPE_POLYGON;
01601 }
01602
01603
01604
01605 inline bool isPlane() const {
01606 return type==GEOMETRIC_TYPE_PLANE;
01607 }
01608
01609
01610
01611 inline unsigned char getType() const {
01612 return type;
01613 }
01614
01615
01616
01617 inline bool getPoint(TPoint3D &p) const {
01618 if (isPoint()) {
01619 p=*(data.point);
01620 return true;
01621 } else return false;
01622 }
01623
01624
01625
01626 inline bool getSegment(TSegment3D &s) const {
01627 if (isSegment()) {
01628 s=*(data.segment);
01629 return true;
01630 } else return false;
01631 }
01632
01633
01634
01635 inline bool getLine(TLine3D &r) const {
01636 if (isLine()) {
01637 r=*(data.line);
01638 return true;
01639 } else return false;
01640 }
01641
01642
01643
01644 inline bool getPolygon(TPolygon3D &p) const {
01645 if (isPolygon()) {
01646 p=*(data.polygon);
01647 return true;
01648 } else return false;
01649 }
01650
01651
01652
01653 inline bool getPlane(TPlane &p) const {
01654 if (isPlane()) {
01655 p=*(data.plane);
01656 return true;
01657 } else return false;
01658 }
01659
01660
01661
01662 void operator=(const TObject3D &obj) {
01663 if (this==&obj) return;
01664 destroy();
01665 switch (type=obj.type) {
01666 case GEOMETRIC_TYPE_POINT:
01667 data.point=new TPoint3D(*(obj.data.point));
01668 break;
01669 case GEOMETRIC_TYPE_SEGMENT:
01670 data.segment=new TSegment3D(*(obj.data.segment));
01671 break;
01672 case GEOMETRIC_TYPE_LINE:
01673 data.line=new TLine3D(*(obj.data.line));
01674 break;
01675 case GEOMETRIC_TYPE_POLYGON:
01676 data.polygon=new TPolygon3D(*(obj.data.polygon));
01677 break;
01678 case GEOMETRIC_TYPE_PLANE:
01679 data.plane=new TPlane(*(obj.data.plane));
01680 break;
01681 case GEOMETRIC_TYPE_UNDEFINED:
01682 break;
01683 default:
01684 THROW_EXCEPTION("Invalid TObject3D object");
01685 }
01686 }
01687
01688
01689
01690 inline void operator=(const TPoint3D &p) {
01691 destroy();
01692 type=GEOMETRIC_TYPE_POINT;
01693 data.point=new TPoint3D(p);
01694 }
01695
01696
01697
01698 inline void operator=(const TSegment3D &s) {
01699 destroy();
01700 type=GEOMETRIC_TYPE_SEGMENT;
01701 data.segment=new TSegment3D(s);
01702 }
01703
01704
01705
01706 inline void operator=(const TLine3D &l) {
01707 destroy();
01708 type=GEOMETRIC_TYPE_LINE;
01709 data.line=new TLine3D(l);
01710 }
01711
01712
01713
01714 inline void operator=(const TPolygon3D &p) {
01715 destroy();
01716 type=GEOMETRIC_TYPE_POLYGON;
01717 data.polygon=new TPolygon3D(p);
01718 }
01719
01720
01721
01722 inline void operator=(const TPlane &p) {
01723 destroy();
01724 type=GEOMETRIC_TYPE_PLANE;
01725 data.plane=new TPlane(p);
01726 }
01727
01728
01729
01730
01731 inline void generate2DObject(TObject2D &obj) const {
01732 switch (type) {
01733 case GEOMETRIC_TYPE_POINT:
01734 obj=TPoint2D(*(data.point));
01735 break;
01736 case GEOMETRIC_TYPE_SEGMENT:
01737 obj=TSegment2D(*(data.segment));
01738 break;
01739 case GEOMETRIC_TYPE_LINE:
01740 obj=TLine2D(*(data.line));
01741 break;
01742 case GEOMETRIC_TYPE_POLYGON:
01743 obj=TPolygon2D(*(data.polygon));
01744 break;
01745 case GEOMETRIC_TYPE_PLANE:
01746 throw std::logic_error("Too many dimensions");
01747 default:
01748 obj=TObject2D();
01749 break;
01750 }
01751 }
01752
01753
01754
01755 TObject3D(const TObject3D &obj):type(GEOMETRIC_TYPE_UNDEFINED) {
01756 operator=(obj);
01757 }
01758
01759
01760
01761 static void getPoints(const std::vector<TObject3D> &objs,std::vector<TPoint3D> &pnts);
01762
01763
01764
01765 static void getSegments(const std::vector<TObject3D> &objs,std::vector<TSegment3D> &sgms);
01766
01767
01768
01769 static void getLines(const std::vector<TObject3D> &objs,std::vector<TLine3D> &lins);
01770
01771
01772
01773 static void getPlanes(const std::vector<TObject3D> &objs,std::vector<TPlane> &plns);
01774
01775
01776
01777 static void getPolygons(const std::vector<TObject3D> &objs,std::vector<TPolygon3D> &polys);
01778
01779
01780
01781 static void getPoints(const std::vector<TObject3D> &objs,std::vector<TPoint3D> &pnts,std::vector<TObject3D> &remainder);
01782
01783
01784
01785 static void getSegments(const std::vector<TObject3D> &objs,std::vector<TSegment3D> &sgms,std::vector<TObject3D> &remainder);
01786
01787
01788
01789 static void getLines(const std::vector<TObject3D> &objs,std::vector<TLine3D> &lins,std::vector<TObject3D> &remainder);
01790
01791
01792
01793 static void getPlanes(const std::vector<TObject3D> &objs,std::vector<TPlane> &plns,std::vector<TObject3D> &remainder);
01794
01795
01796
01797 static void getPolygons(const std::vector<TObject3D> &objs,std::vector<TPolygon3D> &polys,std::vector<TObject3D> &remainder);
01798 };
01799 #else
01800 struct BASE_IMPEXP TObject2D {
01801 private:
01802
01803
01804
01805 unsigned char type;
01806
01807
01808
01809 struct {
01810 TPoint2D point;
01811 TSegment2D segment;
01812 TLine2D line;
01813 TPolygon2D *polygon;
01814 } data;
01815
01816
01817
01818 inline void destroy() {
01819 if (type==GEOMETRIC_TYPE_POLYGON) delete data.polygon;
01820 type=GEOMETRIC_TYPE_UNDEFINED;
01821 }
01822 public:
01823
01824
01825
01826 inline TObject2D(const TPoint2D &p):type(GEOMETRIC_TYPE_POINT) {
01827 data.point=p;
01828 }
01829
01830
01831
01832 inline TObject2D(const TSegment2D &s):type(GEOMETRIC_TYPE_SEGMENT) {
01833 data.segment=s;
01834 }
01835
01836
01837
01838 inline TObject2D(const TLine2D &r):type(GEOMETRIC_TYPE_LINE) {
01839 data.line=r;
01840 }
01841
01842
01843
01844 inline TObject2D(const TPolygon2D &p):type(GEOMETRIC_TYPE_POLYGON) {
01845 data.polygon=new TPolygon2D(p);
01846 }
01847
01848
01849
01850 TObject2D():type(GEOMETRIC_TYPE_UNDEFINED) {}
01851
01852
01853
01854 ~TObject2D() {
01855 destroy();
01856 }
01857
01858
01859
01860 inline bool isPoint() const {
01861 return type==GEOMETRIC_TYPE_POINT;
01862 }
01863
01864
01865
01866 inline bool isSegment() const {
01867 return type==GEOMETRIC_TYPE_SEGMENT;
01868 }
01869
01870
01871
01872 inline bool isLine() const {
01873 return type==GEOMETRIC_TYPE_LINE;
01874 }
01875
01876
01877
01878 inline bool isPolygon() const {
01879 return type==GEOMETRIC_TYPE_POLYGON;
01880 }
01881
01882
01883
01884 inline unsigned char getType() const {
01885 return type;
01886 }
01887
01888
01889
01890 inline bool getPoint(TPoint2D &p) const {
01891 if (isPoint()) {
01892 p=data.point;
01893 return true;
01894 } else return false;
01895 }
01896
01897
01898
01899 inline bool getSegment(TSegment2D &s) const {
01900 if (isSegment()) {
01901 s=data.segment;
01902 return true;
01903 } else return false;
01904 }
01905
01906
01907
01908 inline bool getLine(TLine2D &r) const {
01909 if (isLine()) {
01910 r=data.line;
01911 return true;
01912 } else return false;
01913 }
01914
01915
01916
01917 inline bool getPolygon(TPolygon2D &p) const {
01918 if (isPolygon()) {
01919 p=*(data.polygon);
01920 return true;
01921 } else return false;
01922 }
01923
01924
01925
01926 void operator=(const TObject2D &obj) {
01927 if (this==&obj) return;
01928 destroy();
01929 switch (type=obj.type) {
01930 case GEOMETRIC_TYPE_POINT:
01931 data.point=obj.data.point;
01932 break;
01933 case GEOMETRIC_TYPE_SEGMENT:
01934 data.segment=obj.data.segment;
01935 break;
01936 case GEOMETRIC_TYPE_LINE:
01937 data.line=obj.data.line;
01938 break;
01939 case GEOMETRIC_TYPE_POLYGON:
01940 data.polygon=new TPolygon2D(*(obj.data.polygon));
01941 break;
01942 }
01943 }
01944
01945
01946
01947 inline void operator=(const TPoint2D &p) {
01948 destroy();
01949 type=GEOMETRIC_TYPE_POINT;
01950 data.point=p;
01951 }
01952
01953
01954
01955 inline void operator=(const TSegment2D &s) {
01956 destroy();
01957 type=GEOMETRIC_TYPE_SEGMENT;
01958 data.segment=s;
01959 }
01960
01961
01962
01963 inline void operator=(const TLine2D &l) {
01964 destroy();
01965 type=GEOMETRIC_TYPE_LINE;
01966 data.line=l;
01967 }
01968
01969
01970
01971 inline void operator=(const TPolygon2D &p) {
01972 destroy();
01973 type=GEOMETRIC_TYPE_POLYGON;
01974 data.polygon=new TPolygon2D(p);
01975 }
01976
01977
01978
01979 void generate3DObject(TObject3D &obj) const;
01980
01981
01982
01983 TObject2D(const TObject2D &obj):type(GEOMETRIC_TYPE_UNDEFINED) {
01984 operator=(obj);
01985 }
01986
01987
01988
01989 static void getPoints(const std::vector<TObject2D> &objs,std::vector<TPoint2D> &pnts);
01990
01991
01992
01993 static void getSegments(const std::vector<TObject2D> &objs,std::vector<TSegment2D> &sgms);
01994
01995
01996
01997 static void getLines(const std::vector<TObject2D> &objs,std::vector<TLine2D> &lins);
01998
01999
02000
02001 static void getPolygons(const std::vector<TObject2D> &objs,std::vector<TPolygon2D> &polys);
02002
02003
02004
02005 static void getPoints(const std::vector<TObject2D> &objs,std::vector<TPoint2D> &pnts,std::vector<TObject2D> &remainder);
02006
02007
02008
02009 static void getSegments(const std::vector<TObject2D> &objs,std::vector<TSegment2D> &sgms,std::vector<TObject2D> &remainder);
02010
02011
02012
02013 static void getLines(const std::vector<TObject2D> &objs,std::vector<TLine2D> &lins,std::vector<TObject2D> &remainder);
02014
02015
02016
02017 static void getPolygons(const std::vector<TObject2D> &objs,std::vector<TPolygon2D> &polys,std::vector<TObject2D> &remainder);
02018 };
02019
02020
02021
02022
02023 struct BASE_IMPEXP TObject3D {
02024 private:
02025
02026
02027
02028 unsigned char type;
02029
02030
02031
02032 struct {
02033 TPoint3D point;
02034 TSegment3D segment;
02035 TLine3D line;
02036 TPolygon3D *polygon;
02037 TPlane plane;
02038 } data;
02039
02040
02041
02042 void destroy() {
02043 if (type==GEOMETRIC_TYPE_POLYGON) delete data.polygon;
02044 type=GEOMETRIC_TYPE_UNDEFINED;
02045 }
02046 public:
02047
02048
02049
02050 TObject3D(const TPoint3D &p):type(GEOMETRIC_TYPE_POINT) {
02051 data.point=p;
02052 }
02053
02054
02055
02056 TObject3D(const TSegment3D &s):type(GEOMETRIC_TYPE_SEGMENT) {
02057 data.segment=s;
02058 }
02059
02060
02061
02062 TObject3D(const TLine3D &r):type(GEOMETRIC_TYPE_LINE) {
02063 data.line=r;
02064 }
02065
02066
02067
02068 TObject3D(const TPolygon3D &p):type(GEOMETRIC_TYPE_POLYGON) {
02069 data.polygon=new TPolygon3D(p);
02070 }
02071
02072
02073
02074 TObject3D(const TPlane &p):type(GEOMETRIC_TYPE_PLANE) {
02075 data.plane=p;
02076 }
02077
02078
02079
02080 TObject3D():type(GEOMETRIC_TYPE_UNDEFINED) {}
02081
02082
02083
02084 ~TObject3D() {
02085 destroy();
02086 }
02087
02088
02089
02090 inline bool isPoint() const {
02091 return type==GEOMETRIC_TYPE_POINT;
02092 }
02093
02094
02095
02096 inline bool isSegment() const {
02097 return type==GEOMETRIC_TYPE_SEGMENT;
02098 }
02099
02100
02101
02102 inline bool isLine() const {
02103 return type==GEOMETRIC_TYPE_LINE;
02104 }
02105
02106
02107
02108 inline bool isPolygon() const {
02109 return type==GEOMETRIC_TYPE_POLYGON;
02110 }
02111
02112
02113
02114 inline bool isPlane() const {
02115 return type==GEOMETRIC_TYPE_PLANE;
02116 }
02117
02118
02119
02120 inline unsigned char getType() const {
02121 return type;
02122 }
02123
02124
02125
02126 inline bool getPoint(TPoint3D &p) const {
02127 if (isPoint()) {
02128 p=data.point;
02129 return true;
02130 } else return false;
02131 }
02132
02133
02134
02135 inline bool getSegment(TSegment3D &s) const {
02136 if (isSegment()) {
02137 s=data.segment;
02138 return true;
02139 } else return false;
02140 }
02141
02142
02143
02144 inline bool getLine(TLine3D &r) const {
02145 if (isLine()) {
02146 r=data.line;
02147 return true;
02148 } else return false;
02149 }
02150
02151
02152
02153 inline bool getPolygon(TPolygon3D &p) const {
02154 if (isPolygon()) {
02155 p=*(data.polygon);
02156 return true;
02157 } else return false;
02158 }
02159
02160
02161
02162 inline bool getPlane(TPlane &p) const {
02163 if (isPlane()) {
02164 p=data.plane;
02165 return true;
02166 } else return false;
02167 }
02168
02169
02170
02171 void operator=(const TObject3D &obj) {
02172 if (this==&obj) return;
02173 destroy();
02174 switch (type=obj.type) {
02175 case GEOMETRIC_TYPE_POINT:
02176 data.point=obj.data.point;
02177 break;
02178 case GEOMETRIC_TYPE_SEGMENT:
02179 data.segment=obj.data.segment;
02180 break;
02181 case GEOMETRIC_TYPE_LINE:
02182 data.line=obj.data.line;
02183 break;
02184 case GEOMETRIC_TYPE_POLYGON:
02185 data.polygon=new TPolygon3D(*(obj.data.polygon));
02186 break;
02187 case GEOMETRIC_TYPE_PLANE:
02188 data.plane=obj.data.plane;
02189 break;
02190 case GEOMETRIC_TYPE_UNDEFINED:
02191 break;
02192 default:
02193 THROW_EXCEPTION("Invalid TObject3D object");
02194 }
02195 }
02196
02197
02198
02199 inline void operator=(const TPoint3D &p) {
02200 destroy();
02201 type=GEOMETRIC_TYPE_POINT;
02202 data.point=p;
02203 }
02204
02205
02206
02207 inline void operator=(const TSegment3D &s) {
02208 destroy();
02209 type=GEOMETRIC_TYPE_SEGMENT;
02210 data.segment=s;
02211 }
02212
02213
02214
02215 inline void operator=(const TLine3D &l) {
02216 destroy();
02217 type=GEOMETRIC_TYPE_LINE;
02218 data.line=l;
02219 }
02220
02221
02222
02223 inline void operator=(const TPolygon3D &p) {
02224 destroy();
02225 type=GEOMETRIC_TYPE_POLYGON;
02226 data.polygon=new TPolygon3D(p);
02227 }
02228
02229
02230
02231 inline void operator=(const TPlane &p) {
02232 destroy();
02233 type=GEOMETRIC_TYPE_PLANE;
02234 data.plane=p;
02235 }
02236
02237
02238
02239
02240 inline void generate2DObject(TObject2D &obj) const {
02241 switch (type) {
02242 case GEOMETRIC_TYPE_POINT:
02243 obj=TPoint2D(data.point);
02244 break;
02245 case GEOMETRIC_TYPE_SEGMENT:
02246 obj=TSegment2D(data.segment);
02247 break;
02248 case GEOMETRIC_TYPE_LINE:
02249 obj=TLine2D(data.line);
02250 break;
02251 case GEOMETRIC_TYPE_POLYGON:
02252 obj=TPolygon2D(*(data.polygon));
02253 break;
02254 case GEOMETRIC_TYPE_PLANE:
02255 throw std::logic_error("Too many dimensions");
02256 default:
02257 obj=TObject2D();
02258 break;
02259 }
02260 }
02261
02262
02263
02264 TObject3D(const TObject3D &obj):type(GEOMETRIC_TYPE_UNDEFINED) {
02265 operator=(obj);
02266 }
02267
02268
02269
02270 static void getPoints(const std::vector<TObject3D> &objs,std::vector<TPoint3D> &pnts);
02271
02272
02273
02274 static void getSegments(const std::vector<TObject3D> &objs,std::vector<TSegment3D> &sgms);
02275
02276
02277
02278 static void getLines(const std::vector<TObject3D> &objs,std::vector<TLine3D> &lins);
02279
02280
02281
02282 static void getPlanes(const std::vector<TObject3D> &objs,std::vector<TPlane> &plns);
02283
02284
02285
02286 static void getPolygons(const std::vector<TObject3D> &objs,std::vector<TPolygon3D> &polys);
02287
02288
02289
02290 static void getPoints(const std::vector<TObject3D> &objs,std::vector<TPoint3D> &pnts,std::vector<TObject3D> &remainder);
02291
02292
02293
02294 static void getSegments(const std::vector<TObject3D> &objs,std::vector<TSegment3D> &sgms,std::vector<TObject3D> &remainder);
02295
02296
02297
02298 static void getLines(const std::vector<TObject3D> &objs,std::vector<TLine3D> &lins,std::vector<TObject3D> &remainder);
02299
02300
02301
02302 static void getPlanes(const std::vector<TObject3D> &objs,std::vector<TPlane> &plns,std::vector<TObject3D> &remainder);
02303
02304
02305
02306 static void getPolygons(const std::vector<TObject3D> &objs,std::vector<TPolygon3D> &polys,std::vector<TObject3D> &remainder);
02307 };
02308
02309 #endif
02310
02311
02312
02313
02314
02315
02316 BASE_IMPEXP mrpt::utils::CStream& operator>>(mrpt::utils::CStream& in,mrpt::math::TPoint2D &o);
02317
02318
02319
02320 BASE_IMPEXP mrpt::utils::CStream& operator<<(mrpt::utils::CStream& out,const mrpt::math::TPoint2D &o);
02321
02322
02323
02324
02325 BASE_IMPEXP mrpt::utils::CStream& operator>>(mrpt::utils::CStream& in,mrpt::math::TPoint3D &o);
02326
02327
02328
02329 BASE_IMPEXP mrpt::utils::CStream& operator<<(mrpt::utils::CStream& out,const mrpt::math::TPoint3D &o);
02330
02331
02332
02333
02334 BASE_IMPEXP mrpt::utils::CStream& operator>>(mrpt::utils::CStream& in,mrpt::math::TPose2D &o);
02335
02336
02337
02338 BASE_IMPEXP mrpt::utils::CStream& operator<<(mrpt::utils::CStream& out,const mrpt::math::TPose2D &o);
02339
02340
02341
02342
02343 BASE_IMPEXP mrpt::utils::CStream& operator>>(mrpt::utils::CStream& in,mrpt::math::TPose3D &o);
02344
02345
02346
02347 BASE_IMPEXP mrpt::utils::CStream& operator<<(mrpt::utils::CStream& out,const mrpt::math::TPose3D &o);
02348
02349
02350
02351
02352 inline mrpt::utils::CStream &operator>>(mrpt::utils::CStream &in,mrpt::math::TSegment2D &s) {
02353 return in>>s.point1>>s.point2;
02354 }
02355
02356
02357
02358 inline mrpt::utils::CStream &operator<<(mrpt::utils::CStream &out,const mrpt::math::TSegment2D &s) {
02359 return out<<s.point1<<s.point2;
02360 }
02361
02362
02363
02364
02365 inline mrpt::utils::CStream &operator>>(mrpt::utils::CStream &in,mrpt::math::TLine2D &l) {
02366 return in>>l.coefs[0]>>l.coefs[1]>>l.coefs[2];
02367 }
02368
02369
02370
02371 inline mrpt::utils::CStream &operator<<(mrpt::utils::CStream &out,const mrpt::math::TLine2D &l) {
02372 return out<<l.coefs[0]<<l.coefs[1]<<l.coefs[2];
02373 }
02374
02375
02376
02377
02378 BASE_IMPEXP mrpt::utils::CStream &operator>>(mrpt::utils::CStream &in,mrpt::math::TObject2D &o);
02379
02380
02381
02382 BASE_IMPEXP mrpt::utils::CStream &operator<<(mrpt::utils::CStream &out,const mrpt::math::TObject2D &o);
02383
02384
02385
02386
02387 inline mrpt::utils::CStream &operator>>(mrpt::utils::CStream &in,mrpt::math::TSegment3D &s) {
02388 return in>>s.point1>>s.point2;
02389 }
02390
02391
02392
02393 inline mrpt::utils::CStream &operator<<(mrpt::utils::CStream &out,const mrpt::math::TSegment3D &s) {
02394 return out<<s.point1<<s.point2;
02395 }
02396
02397
02398
02399
02400 inline mrpt::utils::CStream &operator>>(mrpt::utils::CStream &in,mrpt::math::TLine3D &l) {
02401 return in>>l.pBase>>l.director[0]>>l.director[1]>>l.director[2];
02402 }
02403
02404
02405
02406 inline mrpt::utils::CStream &operator<<(mrpt::utils::CStream &out,const mrpt::math::TLine3D &l) {
02407 return out<<l.pBase<<l.director[0]<<l.director[1]<<l.director[2];
02408 }
02409
02410
02411
02412
02413 inline mrpt::utils::CStream &operator>>(mrpt::utils::CStream &in,mrpt::math::TPlane &p) {
02414 return in>>p.coefs[0]>>p.coefs[1]>>p.coefs[2]>>p.coefs[3];
02415 }
02416
02417
02418
02419 inline mrpt::utils::CStream &operator<<(mrpt::utils::CStream &out,const mrpt::math::TPlane &p) {
02420 return out<<p.coefs[0]<<p.coefs[1]<<p.coefs[2]<<p.coefs[3];
02421 }
02422
02423
02424
02425
02426 BASE_IMPEXP mrpt::utils::CStream &operator>>(mrpt::utils::CStream &in,mrpt::math::TObject3D &o);
02427
02428
02429
02430 BASE_IMPEXP mrpt::utils::CStream &operator<<(mrpt::utils::CStream &out,const mrpt::math::TObject3D &o);
02431
02432
02433
02434
02435 }
02436
02437 namespace utils
02438 {
02439 using namespace ::mrpt::math;
02440
02441
02442 MRPT_DECLARE_TTYPENAME(TPoint2D)
02443 MRPT_DECLARE_TTYPENAME(TPoint3D)
02444 MRPT_DECLARE_TTYPENAME(TPose2D)
02445 MRPT_DECLARE_TTYPENAME(TPose3D)
02446 MRPT_DECLARE_TTYPENAME(TSegment2D)
02447 MRPT_DECLARE_TTYPENAME(TLine2D)
02448 MRPT_DECLARE_TTYPENAME(TPolygon2D)
02449 MRPT_DECLARE_TTYPENAME(TObject2D)
02450 MRPT_DECLARE_TTYPENAME(TSegment3D)
02451 MRPT_DECLARE_TTYPENAME(TLine3D)
02452 MRPT_DECLARE_TTYPENAME(TPlane)
02453 MRPT_DECLARE_TTYPENAME(TPolygon3D)
02454 MRPT_DECLARE_TTYPENAME(TObject3D)
02455
02456 }
02457
02458 }
02459 #endif