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