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 GEO_H
00029 #define GEO_H
00030
00031 #include <mrpt/utils/utils_defs.h>
00032 #include <mrpt/math/CMatrixTemplateNumeric.h>
00033 #include <mrpt/poses/CPoint2D.h>
00034 #include <mrpt/poses/CPoint3D.h>
00035 #include <mrpt/poses/CPose2D.h>
00036 #include <mrpt/poses/CPose3D.h>
00037 #include <mrpt/math/lightweight_geom_data.h>
00038 #include <mrpt/math/CSparseMatrixTemplate.h>
00039 #include <mrpt/math/utils.h>
00040
00041
00042
00043
00044 namespace mrpt
00045 {
00046 namespace math
00047 {
00048 using namespace mrpt::utils;
00049 using namespace mrpt::poses;
00050
00051
00052
00053
00054 extern double BASE_IMPEXP geometryEpsilon;
00055
00056
00057
00058
00059
00060 class BASE_IMPEXP TPolygonWithPlane {
00061 public:
00062
00063
00064
00065 TPolygon3D poly;
00066
00067
00068
00069 TPlane plane;
00070
00071
00072
00073
00074 mrpt::poses::CPose3D pose;
00075
00076
00077
00078
00079 mrpt::poses::CPose3D inversePose;
00080
00081
00082
00083
00084 TPolygon2D poly2D;
00085
00086
00087
00088 TPolygonWithPlane(const TPolygon3D &p);
00089
00090
00091
00092
00093 TPolygonWithPlane() {}
00094
00095
00096
00097 static void getPlanes(const vector<TPolygon3D> &oldPolys,vector<TPolygonWithPlane> &newPolys);
00098 };
00099
00100
00101
00102
00103
00104
00105
00106
00107 bool BASE_IMPEXP intersect(const TSegment3D &s1,const TSegment3D &s2,TObject3D &obj);
00108
00109
00110
00111
00112 bool BASE_IMPEXP intersect(const TSegment3D &s1,const TPlane &p2,TObject3D &obj);
00113
00114
00115
00116
00117 bool BASE_IMPEXP intersect(const TSegment3D &s1,const TLine3D &r2,TObject3D &obj);
00118
00119
00120
00121
00122 inline bool intersect(const TPlane &p1,const TSegment3D &s2,TObject3D &obj) {
00123 return intersect(s2,p1,obj);
00124 }
00125
00126
00127
00128
00129 bool BASE_IMPEXP intersect(const TPlane &p1,const TPlane &p2,TObject3D &obj);
00130
00131
00132
00133
00134 bool BASE_IMPEXP intersect(const TPlane &p1,const TLine3D &p2,TObject3D &obj);
00135
00136
00137
00138
00139 inline bool intersect(const TLine3D &r1,const TSegment3D &s2,TObject3D &obj) {
00140 return intersect(s2,r1,obj);
00141 }
00142
00143
00144
00145
00146 inline bool intersect(const TLine3D &r1,const TPlane &p2,TObject3D &obj) {
00147 return intersect(p2,r1,obj);
00148 }
00149
00150
00151
00152
00153 bool BASE_IMPEXP intersect(const TLine3D &r1,const TLine3D &r2,TObject3D &obj);
00154
00155
00156
00157
00158 bool BASE_IMPEXP intersect(const TLine2D &r1,const TLine2D &r2,TObject2D &obj);
00159
00160
00161
00162
00163 bool BASE_IMPEXP intersect(const TLine2D &r1,const TSegment2D &s2,TObject2D &obj);
00164
00165
00166
00167
00168 inline bool intersect(const TSegment2D &s1,const TLine2D &r2,TObject2D &obj) {
00169 return intersect(r2,s1,obj);
00170 }
00171
00172
00173
00174
00175 bool BASE_IMPEXP intersect(const TSegment2D &s1,const TSegment2D &s2,TObject2D &obj);
00176
00177
00178
00179
00180
00181
00182
00183
00184
00185 double BASE_IMPEXP getAngle(const TPlane &p1,const TPlane &p2);
00186
00187
00188
00189 double BASE_IMPEXP getAngle(const TPlane &p1,const TLine3D &r2);
00190
00191
00192
00193 inline double getAngle(const TLine3D &r1,const TPlane &p2) {
00194 return getAngle(p2,r1);
00195 }
00196
00197
00198
00199 double BASE_IMPEXP getAngle(const TLine3D &r1,const TLine3D &r2);
00200
00201
00202
00203 double BASE_IMPEXP getAngle(const TLine2D &r1,const TLine2D &r2);
00204
00205
00206
00207
00208
00209
00210
00211
00212
00213
00214 void BASE_IMPEXP createFromPoseX(const CPose3D &p,TLine3D &r);
00215
00216
00217
00218
00219 void BASE_IMPEXP createFromPoseY(const CPose3D &p,TLine3D &r);
00220
00221
00222
00223
00224 void BASE_IMPEXP createFromPoseZ(const CPose3D &p,TLine3D &r);
00225
00226
00227
00228
00229 void BASE_IMPEXP createFromPoseAndVector(const CPose3D &p,const double (&vector)[3],TLine3D &r);
00230
00231
00232
00233
00234 void BASE_IMPEXP createFromPoseX(const TPose2D &p,TLine2D &r);
00235
00236
00237
00238
00239 void BASE_IMPEXP createFromPoseY(const TPose2D &p,TLine2D &r);
00240
00241
00242
00243
00244 void BASE_IMPEXP createFromPoseAndVector(const TPose2D &p,const double (&vector)[2],TLine2D &r);
00245
00246
00247
00248
00249
00250
00251
00252
00253
00254
00255 bool BASE_IMPEXP conformAPlane(const std::vector<TPoint3D> &points);
00256
00257
00258
00259
00260 bool BASE_IMPEXP conformAPlane(const std::vector<TPoint3D> &points,TPlane &p);
00261
00262
00263
00264
00265 bool BASE_IMPEXP areAligned(const std::vector<TPoint2D> &points);
00266
00267
00268
00269
00270 bool BASE_IMPEXP areAligned(const std::vector<TPoint2D> &points,TLine2D &r);
00271
00272
00273
00274
00275 bool BASE_IMPEXP areAligned(const std::vector<TPoint3D> &points);
00276
00277
00278
00279 bool BASE_IMPEXP areAligned(const std::vector<TPoint3D> &points,TLine3D &r);
00280
00281
00282
00283
00284
00285
00286
00287
00288
00289 inline void project3D(const TPoint3D &point, const CPose3D &newXYpose,TPoint3D &newPoint) {
00290 newXYpose.composePoint(point.x,point.y,point.z,newPoint.x,newPoint.y,newPoint.z);
00291 }
00292
00293
00294
00295 inline void project3D(const TSegment3D &segment,const CPose3D &newXYpose,TSegment3D &newSegment) {
00296 project3D(segment.point1,newXYpose,newSegment.point1);
00297 project3D(segment.point2,newXYpose,newSegment.point2);
00298 }
00299
00300
00301
00302 void BASE_IMPEXP project3D(const TLine3D &line,const CPose3D &newXYpose,TLine3D &newLine);
00303
00304
00305
00306 void BASE_IMPEXP project3D(const TPlane &plane,const CPose3D &newXYpose,TPlane &newPlane);
00307
00308
00309
00310 void BASE_IMPEXP project3D(const TPolygon3D &polygon,const CPose3D &newXYpose,TPolygon3D &newPolygon);
00311
00312
00313
00314 void BASE_IMPEXP project3D(const TObject3D &object,const CPose3D &newXYPose,TObject3D &newObject);
00315
00316
00317
00318
00319 template<class T> void project3D(const T &obj,const TPlane &newXYPlane,T &newObj) {
00320 CPose3D pose;
00321 TPlane(newXYPlane).getAsPose3D(pose);
00322 project3D(obj,-pose,newObj);
00323 }
00324
00325
00326
00327
00328 template<class T> void project3D(const T &obj,const TPlane &newXYPlane,const TPoint3D &newOrigin,T &newObj) {
00329 CPose3D pose;
00330
00331 TPlane(newXYPlane).getAsPose3D(pose);
00332 project3D(obj,-pose,newObj);
00333 }
00334
00335
00336
00337
00338 template<class T> void project3D(const std::vector<T> &objs,const CPose3D &newXYpose,std::vector<T> &newObjs) {
00339 size_t N=objs.size();
00340 newObjs.resize(N);
00341 for (size_t i=0;i<N;i++) project3D(objs[i],newXYpose,newObjs[i]);
00342 }
00343
00344
00345
00346
00347 inline void project2D(const TPoint2D &point,const CPose2D &newXpose,TPoint2D &newPoint) {
00348 newPoint=newXpose+CPoint2D(point);
00349 }
00350
00351
00352
00353 inline void project2D(const TSegment2D &segment,const CPose2D &newXpose,TSegment2D &newSegment) {
00354 project2D(segment.point1,newXpose,newSegment.point1);
00355 project2D(segment.point2,newXpose,newSegment.point2);
00356 }
00357
00358
00359
00360
00361 void BASE_IMPEXP project2D(const TLine2D &line,const CPose2D &newXpose,TLine2D &newLine);
00362
00363
00364
00365 void BASE_IMPEXP project2D(const TPolygon2D &polygon,const CPose2D &newXpose,TPolygon2D &newPolygon);
00366
00367
00368
00369 void BASE_IMPEXP project2D(const TObject2D &object,const CPose2D &newXpose,TObject2D &newObject);
00370
00371
00372
00373
00374 template<class T> void project2D(const T &obj,const TLine2D &newXLine,T &newObj) {
00375 CPose2D pose;
00376 newXLine.getAsPose2D(pose);
00377 project2D(obj,CPose2D(0,0,0)-pose,newObj);
00378 }
00379
00380
00381
00382
00383 template<class T> void project2D(const T &obj,const TLine2D &newXLine,const TPoint2D &newOrigin,T &newObj) {
00384 CPose2D pose;
00385 newXLine.getAsPose2DForcingOrigin(newOrigin,pose);
00386 project2D(obj,CPose2D(0,0,0)-pose,newObj);
00387 }
00388
00389
00390
00391
00392 template<class T> void project2D(const std::vector<T> &objs,const CPose2D &newXpose,std::vector<T> &newObjs) {
00393 size_t N=objs.size();
00394 newObjs.resize(N);
00395 for (size_t i=0;i<N;i++) project2D(objs[i],newXpose,newObjs[i]);
00396 }
00397
00398
00399
00400
00401
00402
00403
00404
00405
00406
00407 bool BASE_IMPEXP intersect(const TPolygon2D &p1,const TSegment2D &s2,TObject2D &obj);
00408
00409
00410
00411
00412 bool BASE_IMPEXP intersect(const TPolygon2D &p1,const TLine2D &r2,TObject2D &obj);
00413
00414
00415
00416
00417 bool BASE_IMPEXP intersect(const TPolygon2D &p1,const TPolygon2D &p2,TObject2D &obj);
00418
00419
00420
00421
00422 inline bool intersect(const TSegment2D &s1,const TPolygon2D &p2,TObject2D &obj) {
00423 return intersect(p2,s1,obj);
00424 }
00425
00426
00427
00428
00429 inline bool intersect(const TLine2D &r1,const TPolygon2D &p2,TObject2D &obj) {
00430 return intersect(p2,r1,obj);
00431 }
00432
00433
00434
00435
00436 bool BASE_IMPEXP intersect(const TPolygon3D &p1,const TSegment3D &s2,TObject3D &obj);
00437
00438
00439
00440
00441 bool BASE_IMPEXP intersect(const TPolygon3D &p1,const TLine3D &r2,TObject3D &obj);
00442
00443
00444
00445
00446 bool BASE_IMPEXP intersect(const TPolygon3D &p1,const TPlane &p2,TObject3D &obj);
00447
00448
00449
00450
00451 bool BASE_IMPEXP intersect(const TPolygon3D &p1,const TPolygon3D &p2,TObject3D &obj);
00452
00453
00454
00455
00456 inline bool intersect(const TSegment3D &s1,const TPolygon3D &p2,TObject3D &obj) {
00457 return intersect(p2,s1,obj);
00458 }
00459
00460
00461
00462
00463 inline bool intersect(const TLine3D &r1,const TPolygon3D &p2,TObject3D &obj) {
00464 return intersect(p2,r1,obj);
00465 }
00466
00467
00468
00469
00470 inline bool intersect(const TPlane &p1,const TPolygon3D &p2,TObject3D &obj) {
00471 return intersect(p2,p1,obj);
00472 }
00473
00474
00475
00476
00477
00478 size_t BASE_IMPEXP intersect(const std::vector<TPolygon3D> &v1,const std::vector<TPolygon3D> &v2,CSparseMatrixTemplate<TObject3D> &objs);
00479
00480
00481
00482
00483 size_t BASE_IMPEXP intersect(const std::vector<TPolygon3D> &v1,const std::vector<TPolygon3D> &v2,std::vector<TObject3D> &objs);
00484
00485
00486
00487
00488
00489
00490
00491
00492
00493
00494 template<class T,class U,class O> size_t intersect(const std::vector<T> &v1,const std::vector<U> &v2,CSparseMatrixTemplate<O> &objs) {
00495 size_t M=v1.size(),N=v2.size();
00496 O obj;
00497 objs.clear();
00498 objs.resize(M,N);
00499 for (size_t i=0;i<M;i++) for (size_t j=0;j<M;j++) if (intersect(v1[i],v2[j],obj)) objs(i,j)=obj;
00500 return objs.getNonNullElements();
00501 }
00502
00503
00504
00505
00506
00507 template<class T,class U,class O> size_t intersect(const std::vector<T> &v1,const std::vector<U> &v2,std::vector<O> objs) {
00508 objs.resize(0);
00509 O obj;
00510 for (typename std::vector<T>::const_iterator it1=v1.begin();it1!=v1.end();it1++) {
00511 const T &elem1=*it1;
00512 for (typename std::vector<U>::const_iterator it2=v2.begin();it2!=v2.end();it2++) if (intersect(elem1,*it2,obj)) objs.push_back(obj);
00513 }
00514 return objs.size();
00515 }
00516
00517
00518
00519
00520 bool BASE_IMPEXP intersect(const TObject2D &o1,const TObject2D &o2,TObject2D &obj);
00521
00522
00523
00524 bool BASE_IMPEXP intersect(const TObject3D &o1,const TObject3D &o2,TObject3D &obj);
00525
00526
00527
00528
00529
00530
00531
00532
00533
00534 double BASE_IMPEXP distance(const TPoint2D &p1,const TPoint2D &p2);
00535
00536
00537
00538 double BASE_IMPEXP distance(const TPoint3D &p1,const TPoint3D &p2);
00539
00540
00541
00542 double BASE_IMPEXP distance(const TLine2D &r1,const TLine2D &r2);
00543
00544
00545
00546 double BASE_IMPEXP distance(const TLine3D &r1,const TLine3D &r2);
00547
00548
00549
00550 double BASE_IMPEXP distance(const TPlane &p1,const TPlane &p2);
00551
00552
00553
00554
00555 double BASE_IMPEXP distance(const TPolygon2D &p1,const TPolygon2D &p2);
00556
00557
00558
00559 double BASE_IMPEXP distance(const TPolygon2D &p1,const TSegment2D &s2);
00560
00561
00562
00563 inline double distance(const TSegment2D &s1,const TPolygon2D &p2) {
00564 return distance(p2,s1);
00565 }
00566
00567
00568
00569 double BASE_IMPEXP distance(const TPolygon2D &p1,const TLine2D &l2);
00570 inline double distance(const TLine2D &l1,const TPolygon2D &p2) {
00571 return distance(p2,l1);
00572 }
00573
00574
00575
00576 double BASE_IMPEXP distance(const TPolygon3D &p1,const TPolygon3D &p2);
00577
00578
00579
00580 double BASE_IMPEXP distance(const TPolygon3D &p1,const TSegment3D &s2);
00581
00582
00583
00584 inline double distance(const TSegment3D &s1,const TPolygon3D &p2) {
00585 return distance(p2,s1);
00586 }
00587
00588
00589
00590 double BASE_IMPEXP distance(const TPolygon3D &p1,const TLine3D &l2);
00591
00592
00593
00594 inline double distance(const TLine3D &l1,const TPolygon3D &p2) {
00595 return distance(p2,l1);
00596 }
00597
00598
00599
00600 double BASE_IMPEXP distance(const TPolygon3D &po,const TPlane &pl);
00601
00602
00603
00604 inline double distance(const TPlane &pl,const TPolygon3D &po) {
00605 return distance(po,pl);
00606 }
00607
00608
00609
00610
00611
00612
00613
00614
00615
00616 void BASE_IMPEXP getRectangleBounds(const std::vector<TPoint2D> &poly,TPoint2D &pMin,TPoint2D &pMax);
00617
00618
00619
00620 void BASE_IMPEXP getPrismBounds(const std::vector<TPoint3D> &poly,TPoint3D &pMin,TPoint3D &pMax);
00621
00622
00623
00624
00625
00626
00627
00628
00629
00630
00631 void BASE_IMPEXP createPlaneFromPoseXY(const CPose3D &pose,TPlane &plane);
00632
00633
00634
00635
00636 void BASE_IMPEXP createPlaneFromPoseXZ(const CPose3D &pose,TPlane &plane);
00637
00638
00639
00640
00641 void BASE_IMPEXP createPlaneFromPoseYZ(const CPose3D &pose,TPlane &plane);
00642
00643
00644
00645
00646 void BASE_IMPEXP createPlaneFromPoseAndNormal(const CPose3D &pose,const double (&normal)[3],TPlane &plane);
00647
00648
00649
00650 void BASE_IMPEXP generateAxisBaseFromDirectionAndAxis(const double (&vec)[3],char coord,CMatrixDouble &matrix);
00651
00652
00653
00654
00655
00656
00657
00658
00659
00660
00661 double BASE_IMPEXP getRegressionLine(const std::vector<TPoint2D> &points,TLine2D &line);
00662
00663
00664
00665
00666 double BASE_IMPEXP getRegressionLine(const std::vector<TPoint3D> &points,TLine3D &line);
00667
00668
00669
00670
00671 double BASE_IMPEXP getRegressionPlane(const std::vector<TPoint3D> &points,TPlane &plane);
00672
00673
00674
00675
00676
00677
00678
00679
00680
00681 void BASE_IMPEXP assemblePolygons(const std::vector<TSegment3D> &segms,std::vector<TPolygon3D> &polys);
00682
00683
00684
00685 void BASE_IMPEXP assemblePolygons(const std::vector<TSegment3D> &segms,std::vector<TPolygon3D> &polys,std::vector<TSegment3D> &remainder);
00686
00687
00688
00689 void BASE_IMPEXP assemblePolygons(const std::vector<TObject3D> &objs,std::vector<TPolygon3D> &polys);
00690
00691
00692
00693 void BASE_IMPEXP assemblePolygons(const std::vector<TObject3D> &objs,std::vector<TPolygon3D> &polys,std::vector<TObject3D> &remainder);
00694
00695
00696
00697 void BASE_IMPEXP assemblePolygons(const std::vector<TObject3D> &objs,std::vector<TPolygon3D> &polys,std::vector<TSegment3D> &remainder1,std::vector<TObject3D> &remainder2);
00698
00699
00700
00701
00702
00703 inline void setEpsilon(double nE) {
00704 geometryEpsilon=nE;
00705 }
00706
00707
00708
00709
00710 inline double getEpsilon() {
00711 return geometryEpsilon;
00712 }
00713
00714
00715
00716
00717 bool BASE_IMPEXP splitInConvexComponents(const TPolygon2D &poly,vector<TPolygon2D> &components);
00718
00719
00720
00721
00722 bool BASE_IMPEXP splitInConvexComponents(const TPolygon3D &poly,vector<TPolygon3D> &components);
00723
00724
00725
00726
00727 void BASE_IMPEXP getSegmentBisector(const TSegment2D &sgm,TLine2D &bis);
00728
00729
00730
00731 void BASE_IMPEXP getSegmentBisector(const TSegment3D &sgm,TPlane &bis);
00732
00733
00734
00735 void BASE_IMPEXP getAngleBisector(const TLine2D &l1,const TLine2D &l2,TLine2D &bis);
00736
00737
00738
00739
00740 void BASE_IMPEXP getAngleBisector(const TLine3D &l1,const TLine3D &l2,TLine3D &bis);
00741
00742
00743
00744
00745
00746
00747
00748 bool BASE_IMPEXP traceRay(const vector<TPolygonWithPlane> &vec,const mrpt::poses::CPose3D &pose,double &dist);
00749
00750
00751
00752
00753 inline bool traceRay(const vector<TPolygon3D> &vec,const mrpt::poses::CPose3D &pose,double &dist) {
00754 vector<TPolygonWithPlane> pwp;
00755 TPolygonWithPlane::getPlanes(vec,pwp);
00756 return traceRay(pwp,pose,dist);
00757 }
00758
00759
00760
00761
00762
00763
00764
00765
00766
00767
00768
00769
00770 template<class T,class U,class V>
00771 inline void crossProduct3D(const T &v0,const U &v1,V& vOut) {
00772 vOut[0]=v0[1]*v1[2]-v0[2]*v1[1];
00773 vOut[1]=v0[2]*v1[0]-v0[0]*v1[2];
00774 vOut[2]=v0[0]*v1[1]-v0[1]*v1[0];
00775 }
00776
00777
00778
00779
00780
00781
00782
00783
00784
00785
00786
00787
00788 template<class T>
00789 inline void crossProduct3D(
00790 const std::vector<T> &v0,
00791 const std::vector<T> &v1,
00792 std::vector<T> &v_out )
00793 {
00794 ASSERT_(v0.size()==3)
00795 ASSERT_(v1.size()==3);
00796 v_out.resize(3);
00797 v_out[0] = v0[1]*v1[2] - v0[2]*v1[1];
00798 v_out[1] = -v0[0]*v1[2] + v0[2]*v1[0];
00799 v_out[2] = v0[0]*v1[1] - v0[1]*v1[0];
00800 }
00801
00802
00803
00804
00805 template<class T,class U> inline bool vectorsAreParallel2D(const T &v1,const U &v2) {
00806 return abs(v1[0]*v2[1]-v2[0]*v1[1])<geometryEpsilon;
00807 }
00808
00809
00810
00811
00812 template<class T,class U>
00813 inline bool vectorsAreParallel3D(const T &v1,const U &v2) {
00814 if (abs(v1[0]*v2[1]-v2[0]*v1[1])>=geometryEpsilon) return false;
00815 if (abs(v1[1]*v2[2]-v2[1]*v1[2])>=geometryEpsilon) return false;
00816 return abs(v1[2]*v2[0]-v2[2]*v1[0])<geometryEpsilon;
00817 }
00818
00819
00820
00821 double BASE_IMPEXP minimumDistanceFromPointToSegment(
00822 const double & Px,
00823 const double & Py,
00824 const double & x1,
00825 const double & y1,
00826 const double & x2,
00827 const double & y2,
00828 double & out_x,
00829 double & out_y);
00830
00831
00832
00833 double BASE_IMPEXP minimumDistanceFromPointToSegment(
00834 const double & Px,
00835 const double & Py,
00836 const double & x1,
00837 const double & y1,
00838 const double & x2,
00839 const double & y2,
00840 float & out_x,
00841 float & out_y);
00842
00843
00844
00845
00846
00847 void BASE_IMPEXP closestFromPointToSegment(
00848 const double & Px,
00849 const double & Py,
00850 const double & x1,
00851 const double & y1,
00852 const double & x2,
00853 const double & y2,
00854 double &out_x,
00855 double &out_y);
00856
00857
00858
00859
00860 void BASE_IMPEXP closestFromPointToLine(
00861 const double & Px,
00862 const double & Py,
00863 const double & x1,
00864 const double & y1,
00865 const double & x2,
00866 const double & y2,
00867 double &out_x,
00868 double &out_y);
00869
00870
00871
00872 double BASE_IMPEXP closestSquareDistanceFromPointToLine(
00873 const double & Px,
00874 const double & Py,
00875 const double & x1,
00876 const double & y1,
00877 const double & x2,
00878 const double & y2 );
00879
00880
00881
00882 template <typename T>
00883 T distanceBetweenPoints(const T x1,const T y1,const T x2,const T y2) {
00884 return std::sqrt( square(x1-x2)+square(y1-y2) );
00885 }
00886
00887
00888 template <typename T>
00889 T distanceBetweenPoints(const T x1,const T y1,const T z1, const T x2,const T y2, const T z2) {
00890 return std::sqrt( square(x1-x2)+square(y1-y2)+square(z1-z2) );
00891 }
00892
00893
00894 template <typename T>
00895 T distanceSqrBetweenPoints(const T x1,const T y1,const T x2,const T y2) {
00896 return square(x1-x2)+square(y1-y2);
00897 }
00898
00899
00900 template <typename T>
00901 T distanceSqrBetweenPoints(const T x1,const T y1,const T z1, const T x2,const T y2, const T z2) {
00902 return square(x1-x2)+square(y1-y2)+square(z1-z2);
00903 }
00904
00905
00906
00907 bool BASE_IMPEXP SegmentsIntersection(
00908 const double & x1,const double & y1,
00909 const double & x2,const double & y2,
00910 const double & x3,const double & y3,
00911 const double & x4,const double & y4,
00912 double &ix,double &iy);
00913
00914
00915
00916 bool BASE_IMPEXP SegmentsIntersection(
00917 const double & x1,const double & y1,
00918 const double & x2,const double & y2,
00919 const double & x3,const double & y3,
00920 const double & x4,const double & y4,
00921 float &ix,float &iy);
00922
00923
00924
00925
00926 bool BASE_IMPEXP pointIntoPolygon2D(const double & px, const double & py, unsigned int polyEdges, const double *poly_xs, const double *poly_ys );
00927
00928
00929
00930
00931 template <typename T>
00932 bool pointIntoQuadrangle(
00933 T x, T y,
00934 T v1x, T v1y,
00935 T v2x, T v2y,
00936 T v3x, T v3y,
00937 T v4x, T v4y )
00938 {
00939 using mrpt::utils::sign;
00940
00941 const T a1 = atan2( v1y - y , v1x - x );
00942 const T a2 = atan2( v2y - y , v2x - x );
00943 const T a3 = atan2( v3y - y , v3x - x );
00944 const T a4 = atan2( v4y - y , v4x - x );
00945
00946
00947
00948 const T da1 = mrpt::math::wrapToPi( a2-a1 );
00949 const T da2 = mrpt::math::wrapToPi( a3-a2 );
00950 if (sign(da1)!=sign(da2)) return false;
00951
00952 const T da3 = mrpt::math::wrapToPi( a4-a3 );
00953 if (sign(da2)!=sign(da3)) return false;
00954
00955 const T da4 = mrpt::math::wrapToPi( a1-a4 );
00956 return (sign(da3)==sign(da4) && (sign(da4)==sign(da1)));
00957 }
00958
00959
00960
00961 double BASE_IMPEXP distancePointToPolygon2D(const double & px, const double & py, unsigned int polyEdges, const double *poly_xs, const double *poly_ys );
00962
00963
00964
00965
00966
00967
00968
00969
00970 bool BASE_IMPEXP minDistBetweenLines(
00971 const double & p1_x, const double & p1_y, const double & p1_z,
00972 const double & p2_x, const double & p2_y, const double & p2_z,
00973 const double & p3_x, const double & p3_y, const double & p3_z,
00974 const double & p4_x, const double & p4_y, const double & p4_z,
00975 double &x, double &y, double &z,
00976 double &dist);
00977
00978
00979
00980
00981
00982
00983
00984 bool BASE_IMPEXP RectanglesIntersection(
00985 const double & R1_x_min, const double & R1_x_max,
00986 const double & R1_y_min, const double & R1_y_max,
00987 const double & R2_x_min, const double & R2_x_max,
00988 const double & R2_y_min, const double & R2_y_max,
00989 const double & R2_pose_x,
00990 const double & R2_pose_y,
00991 const double & R2_pose_phi );
00992
00993
00994
00995
00996
00997
00998
00999
01000
01001
01002
01003
01004
01005
01006
01007
01008
01009
01010
01011
01012
01013
01014
01015
01016
01017
01018
01019
01020
01021
01022
01023
01024 template<class T>
01025 CMatrixTemplateNumeric<T> generateAxisBaseFromDirection( T dx, T dy, T dz )
01026 {
01027 MRPT_START;
01028
01029 if (dx==0 && dy==0 && dz==0)
01030 THROW_EXCEPTION("Invalid input: Direction vector is (0,0,0)!");
01031
01032 CMatrixTemplateNumeric<T> P(3,3);
01033
01034
01035 T n_xy = square(dx)+square(dy);
01036 T n = sqrt(n_xy+square(dz));
01037 n_xy = sqrt(n_xy);
01038 P(0,0) = dx / n;
01039 P(1,0) = dy / n;
01040 P(2,0) = dz / n;
01041
01042
01043 if (fabs(dx)>1e-4 || fabs(dy)>1e-4)
01044 {
01045 P(0,1) = -dy / n_xy;
01046 P(1,1) = dx / n_xy;
01047 P(2,1) = 0;
01048 }
01049 else
01050 {
01051
01052 P(0,1) = 1;
01053 P(1,1) = 0;
01054 P(2,1) = 0;
01055 }
01056
01057
01058
01059
01060
01061
01062
01063 CMatrixColumnAccessor<CMatrixTemplateNumeric<T> > outCol = getColumnAccessor(P,2);
01064 crossProduct3D(getColumnAccessor(P,0),getColumnAccessor(P,1),outCol);
01065
01066 return P;
01067 MRPT_END;
01068 }
01069
01070
01071
01072 }
01073
01074 }
01075 #endif