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
00029
00030
00031 #ifndef __PERCEPTION_GRID_H_
00032 #define __PERCEPTION_GRID_H_
00033
00034 #ifdef MIP_HOST_APPLE
00035 #include <applePatch.h>)
00036 #endif
00037
00038 #include <stdio.h>
00039 #include <fcntl.h>
00040 #include <unistd.h>
00041 #include <string.h>
00042 #include <stdlib.h>
00043 #include <signal.h>
00044 #include <assert.h>
00045
00046 #include <iostream>
00047 #include <utility>
00048
00049 #include <fstream>
00050 #include <iostream>
00051 #include <vector>
00052 #include <assert.h>
00053 #include <math.h>
00054
00055 #include <Scan.h>
00056 #include <Spaces.h>
00057 #include <Types.h>
00058 #include <LogTrace.h>
00059
00060 #include <algorithm>
00061
00062
00063 #define COL_NORMAL "\033[0m"
00064 #define COL_BLACK "\033[22;30m"
00065 #define COL_RED "\033[22;31m"
00066 #define COL_GREEN "\033[22;32m"
00067 #define COL_BROWN "\033[22;33m"
00068 #define COL_BLUE "\033[22;34m"
00069 #define COL_MAGENTA "\033[22;35m"
00070 #define COL_CYAN "\033[22;36m"
00071 #define COL_GRAY "\033[22;37m"
00072 #define COL_DARK_GRAY "\033[01;30m"
00073 #define COL_LIGHT_RED "\033[01;31m"
00074 #define COL_LIGHT_GREEN "\033[01;32m"
00075 #define COL_YELLOW "\033[01;33m"
00076 #define COL_LIGHT_BLUE "\033[01;34m"
00077 #define COL_LIGHT_MAGENTA "\033[01;35m"
00078 #define COL_LIGH_CYAN "\033[01;36m"
00079 #define COL_WHITE "\033[01;37m"
00080
00081 using namespace std;
00082
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00107 enum PerceptionCellType{
00108 LRR_PERCEPTIONCELL,
00109 FRONTIER_PERCEPTIONCELL,
00110 LSR_PERCEPTIONCELL,
00111 PREV_EXPLORED_PERCEPTIONCELL,
00112 OBSTACLE_PERCEPTIONCELL,
00113 BOUNDARY_PERCEPTIONCELL,
00114 LRR_BOUNDARY_PERCEPTIONCELL,
00115 UNKNOWN_PERCEPTIONCELL,
00116 PERCEPTIONCELL_PERC_TYPE_NUM
00117 };
00118
00119
00123 enum PerceptionCellBoundaryType{
00124 PERCEPTIONCELL_BOUNDARY_REACHABLE,
00125 PERCEPTIONCELL_NOT_BOUNDARY_REACHABLE,
00126 PERCEPTIONCELL_BOUNDARY_UNREACHABLE,
00127 PERCEPTIONCELL_NOT_BOUNDARY_UNREACHABLE,
00128 PERCEPTIONCELL_OBSTACLE,
00129 PERCEPTIONCELL_BOUNDARY_TYPE_NUM
00130 };
00131
00135 static const char* PerceptionCellBoundaryTypeName[PERCEPTIONCELL_BOUNDARY_TYPE_NUM] ={
00136 "Boundary reachable",
00137 "Not Boundary reachable",
00138 "Boundary unreachable",
00139 "Not Boundary unreachable",
00140 "Obstacle"
00141 };
00142
00146 const Decimal ROBOT_RADIUS = 0.2;
00147
00148
00149 namespace MipBaselib {
00150
00153 class ScanPar{
00154 friend class Scan;
00155 private:
00156 Decimal _linRangeMin;
00157 Decimal _linRangeMax;
00158 Decimal _linRes;
00159
00160 protected:
00162 void setLinRangeMin(Decimal value);
00163
00165 void setLinRangeMax(Decimal value);
00166
00168 void setLinRes(Decimal value);
00169
00170
00171 public:
00173 ScanPar();
00174
00179 ScanPar(Decimal linRangeMin,Decimal linRangeMax,Decimal linRes);
00180
00182 ScanPar(const ScanPar &s);
00183
00185 ScanPar& operator=(const ScanPar& s);
00186
00189 Decimal range();
00190
00193 Decimal linRangeMax();
00194
00197 Decimal linRangeMin();
00198
00201 Decimal linRes();
00202 };
00203
00204
00220 class PerceptionCellState{
00221 private:
00222 bool _wasLocal;
00223 char _state;
00224 bool _isLSRContour;
00225
00226 bool _isState(char xorMask,char orMask);
00227
00228 public:
00230 PerceptionCellState();
00231
00233 ~PerceptionCellState();
00234
00237 PerceptionCellState(const PerceptionCellState& perceptionCellstate);
00238
00241 PerceptionCellState& operator=(const PerceptionCellState& c);
00242
00244 void setIsLSRContour();
00245
00247 void setIsNotLSRContour();
00248
00250 bool isLSRContour();
00251
00256 void setState(bool unknown,int thirdType,bool local,bool exploredOnce);
00257
00259 void setLocal();
00260
00262 void setNotLocal();
00263
00265 void setIsUnknown();
00266
00269 void setIsFrontier();
00270
00272 void setIsInternalNonFree();
00273
00275 void setIsInternalFree();
00276
00278 void setIsObstacle();
00279
00282 bool isLocal();
00283
00286 bool wasLocal();
00287
00290 bool isLRR();
00291
00294 bool isFrontier();
00295
00298 bool isLSR();
00299
00302 bool isPrevExpl();
00303
00306 bool isObstacle();
00307
00310 bool isInternalFree();
00311
00314 bool isInternalNonFree();
00315
00316 bool isBoundary();
00317
00320 bool isLRRBoundary();
00321
00324 bool isUnknown();
00325
00328 string print();
00329 };
00330
00339 class PerceptionCell{
00340 private:
00341
00342 Position _center;
00343 Position _reading;
00344 DubInt _indexes;
00345 int _vectorIndex;
00346
00347 Decimal _side;
00348
00349 Decimal _distVP;
00350
00351
00352 int _distLSRBound;
00353
00354 unsigned int _cone;
00355 unsigned int _frame;
00356
00357 vector<PerceptionCell*> _neighbours;
00358
00359 PerceptionCellState _state;
00360
00361 public:
00364 vector<int> perceptionCounter;
00365
00367 inline void print();
00368
00370 PerceptionCell();
00371
00373 PerceptionCell(Decimal);
00374
00376 ~PerceptionCell();
00377
00379 PerceptionCell(const PerceptionCell& c);
00380
00387 PerceptionCell(Decimal size,Position center,int vectorIndex, DubInt indexes,int cone, int frame,Position reading=Position(0.0,0.0));
00388
00390 PerceptionCell& operator=(const PerceptionCell& c);
00391
00396 inline void setState(bool unknown,int thirdType,bool local,bool exploredOnce);
00397
00399 inline void setLocal();
00400
00402 inline void setNotLocal();
00403
00406 inline void setPoint(Position value);
00407
00409 inline void setCenter(Position value);
00410
00413 inline void setReading(Position value);
00414
00416 inline void setIsFrontier();
00417
00419 inline void setIsObstacle();
00420
00422 inline void setIsInternalFree();
00423
00425 inline void setIsInternalNonFree();
00426
00428 inline void setIsUnknown();
00429
00431 inline void setIsLSRContour();
00432
00434 inline void setIsNotLSRContour();
00435
00438 inline bool wasLocal();
00439
00442 inline bool isLRR();
00443
00446 inline bool isFrontier();
00447
00450 inline bool isLSR();
00451
00454 inline bool isPrevExpl();
00455
00458 inline bool isObstacle();
00459
00462 inline bool isInternalFree();
00463
00466 inline bool isInternalNonFree();
00467
00470 inline bool isUnknown();
00471
00474 inline bool isBoundary();
00475
00478 inline bool isLRRBoundary();
00479
00482 inline bool isLSRContour();
00483
00484
00485 void setVectorIndex(int k){}
00486 void setIndexes(DubInt k){}
00487 void unsetIsUnknown(){}
00488 void setIsLSR(){}
00489 void setIsLRR(){}
00490 void setIsBoundary(){}
00491 void setIsLRRBoundary(){}
00492 void unsetIsLRRBoundary(){}
00493 void setIsPerceptionFrontier(){}
00494
00495 void unsetAllFlags(){}
00496
00497
00500 inline bool isPerceptionFrontier(){}
00501
00503 inline void setNeighbours(vector<PerceptionCell*> value);
00504
00506 inline void neighPushBack(PerceptionCell* value);
00507
00509 inline void setCone (unsigned int value);
00510
00512 inline void setFrame (unsigned int value);
00513
00515 inline void setDistLSRBound(int value);
00516
00518 inline Position point();
00519
00521 inline Position center();
00522
00524 inline Position reading();
00525
00527 inline DubInt indexes();
00528
00530 inline int vectorIndex();
00531
00533 inline Decimal side();
00534
00536 inline vector<PerceptionCell*> neighbours();
00537
00539 inline unsigned int cone();
00540
00542 inline unsigned int frame();
00543
00545 inline Decimal distVP();
00546
00548 inline int distLSRBound();
00549
00553 inline void merge(PerceptionCell& otherPerceptionCell);
00554
00556 inline string printFlags();
00557 };
00558
00563 class PerceptionFrontierArc{
00564 private:
00565 bool _isRangeEdge;
00566 vector<DubInt> _vec;
00567
00568 public:
00570 PerceptionFrontierArc();
00571
00573 PerceptionFrontierArc(const PerceptionFrontierArc& arc);
00574
00576 PerceptionFrontierArc operator=(const PerceptionFrontierArc& arc);
00577
00579 inline DubInt operator[](size_t index) const;
00580
00582 inline vector<DubInt>::iterator begin();
00583
00585 inline vector<DubInt>::iterator end();
00586
00588 inline void push_back(DubInt cell);
00589
00591 inline void pop_back();
00592
00594 inline void insert(vector<DubInt>::iterator pos, DubInt value);
00595
00597 inline void insert(vector<DubInt>::iterator pos, vector<DubInt>::iterator begin, vector<DubInt>::iterator end );
00598
00600 inline vector<DubInt>::iterator erase (vector<DubInt>::iterator position );
00601
00603 inline vector<DubInt> vec();
00604
00606 inline int size();
00607
00609 inline void setIsRangeEdge();
00610
00612 inline void unsetIsRangeEdge();
00613
00615 inline bool isRangeEdge();
00616
00618 string print();
00619 };
00620
00621
00625 class PerceptionFrontier:public vector<PerceptionFrontierArc>,MIPObject{
00626 private:
00627 public:
00629 PerceptionFrontier();
00630
00633 PerceptionFrontier(vector< vector<Position> > PerceptionFrontier);
00634
00636 ~PerceptionFrontier();
00637
00640 string getObjectName() const {
00641 return "PerceptionFrontier";
00642 }
00643 };
00644
00645
00646
00647 typedef vector<DubInt> LSR;
00648 typedef vector<DubInt> LSRBound;
00649 typedef vector<DubInt> LRR;
00650 typedef vector<DubInt> LRRBound;
00651
00652
00656 class PerceptionGridPar:public MIPObject{
00657 private:
00658 ScanPar _scanPar;
00659 Pose _center;
00660 Decimal _cellSize;
00661 int _nCellX;
00662 int _nCellY;
00663 public:
00665 PerceptionGridPar();
00666
00674 PerceptionGridPar(ScanPar scanPar,Pose center,Decimal cellSize,int nCellX,int nCellY);
00675
00677 PerceptionGridPar(const PerceptionGridPar& g);
00678
00680 PerceptionGridPar& operator=(const PerceptionGridPar& g);
00681
00683 inline Pose center();
00684
00686 inline Decimal cellSize();
00687
00689 inline int nCellX();
00690
00692 inline int nCellY();
00693
00695 inline Decimal width();
00696
00698 inline Decimal height();
00699
00702 bool setSize(Decimal height, Decimal width, Decimal cellSize);
00703
00706 bool setSize(Decimal width, int nCellX, int nCellY);
00707
00709 string getObjectName() const {
00710 return "PerceptionGridPar";
00711 }
00712
00714 inline ScanPar* scanPar();
00715 };
00716
00723 class PerceptionGrid:public MIPObject {
00724 private:
00725 PerceptionGridPar _perceptionGridPar;
00726
00728 Scan _currScan;
00729
00731 PerceptionCell* _centralCell;
00732
00733 vector<PerceptionCell> _board;
00734
00735 LSR _lsr;
00736 LSRBound _lsrBound;
00737 LRR _lrr;
00738 LRRBound _lrrBound;
00739 PerceptionFrontier _PerceptionFrontier;
00740 LSRBound _originalFront;
00741 LSRBound _tempBound;
00742
00745 ScanPar* _scanPar(){
00746 return _perceptionGridPar.scanPar();
00747 };
00748
00749
00750
00752 bool _areContiguous(DubInt cell1, DubInt cell2);
00753
00758 bool _areSufficientlyClose(PerceptionCell* cellPointerA,PerceptionCell* cellPointerB,Decimal distance);
00759
00760
00764 bool _isOnBoard(int vectorIndex){
00765 if ((vectorIndex <0) || (vectorIndex > width()*height()-1)){
00766 return false;
00767 }else{
00768 return true;
00769 }
00770 }
00771
00775 bool _isOnBoard(DubInt indexes){
00776 int i=indexes.i();
00777 int j=indexes.j();
00778 if (i<1 || j<1 || i>rows() || j>columns()){
00779 return false;
00780 }else{
00781 return true;
00782 }
00783 }
00784
00788 DubInt _getIndexesFromVectorIndex(int vectorIndex){
00789 int numcolumns = columns();
00790 int i=(vectorIndex/numcolumns)+1;
00791 int j=(vectorIndex+1)-(numcolumns*(i-1));
00792 return DubInt(i,j);
00793 }
00794
00795
00796
00801
00802
00803
00804
00805
00806
00807
00808
00809
00810
00811 bool _isLSRBoundary(DubInt indexes){
00812
00813 return getCell(indexes)->isLSRContour();
00814 }
00815
00820 bool _isLSR(DubInt indexes){
00821 return (getCell(indexes)->isInternalFree() || getCell(indexes)->isInternalNonFree());
00822 }
00823
00824
00825
00827 void _setIsLSR(DubInt cellIndexes);
00828
00833 void _colorLine(int leftx,int rightx,int y,void (PerceptionGrid::*colorCell)(DubInt cellIndexes,Decimal dist));
00834
00835
00836
00837
00838
00839
00840
00841 void _colorLine(int leftx,int rightx,int y);
00842
00846
00847
00852 void _scanLineFill(int xx,int yy,bool (PerceptionGrid::*stopTest)(DubInt cellIndexes),
00853 bool (PerceptionGrid::*alreadyColoredTest)(DubInt cellIndexes),void (PerceptionGrid::*colorCell)(DubInt cellIndexes,Decimal dist));
00854
00856 void _classifyNonObstacleCell(PerceptionCell * perceptionCell);
00857
00859 void _eraseInternalCellsOnLSRContour();
00860
00864 void _classifyInternalCell(DubInt cellIndexes,Decimal robotRadius){
00865 int squarewidth = (int)(robotRadius/(Decimal)(perceptionGridPar()->cellSize()))+1;
00866
00867 int i = cellIndexes.i();
00868 int j = cellIndexes.j();
00869
00870 int starti = (i-squarewidth/2);
00871 int endi = (i+squarewidth/2);
00872 int startj = (j-squarewidth/2);
00873 int endj = (j+ squarewidth/2);
00874
00875
00876 starti = (starti >= 0)?starti:0;
00877 startj = (startj >= 0)?startj:0;
00878 endi = (endi >= (rows() -1))?(rows()-1):endi;
00879 endj = (endj >= (columns() -1))?(columns()-1):endj;
00880
00881
00882
00883 PerceptionCell * ptr1 = getCell(cellIndexes);
00884 PerceptionCell * ptr2 = NULL;
00885 bool escape = false;
00886 bool test = true;
00887
00888 for (int ci=starti;ci<=endi;ci++){
00889 for (int cj=startj;cj<=endj;cj++){
00890 ptr2 = getCell(ci,cj);
00891
00892 if (ptr1->wasLocal()){
00893
00894 test = (ptr2->isObstacle()|| ptr2->isFrontier());
00895 }else{
00896 test = (ptr2->isObstacle()|| ptr2->isFrontier());
00897 }
00898 if (test){
00899 if (_areSufficientlyClose(ptr1,ptr2,robotRadius)){
00900 escape = true;
00901 break;
00902 }
00903 }
00904 }
00905 if (escape) break;
00906 }
00907 ptr1->setLocal();
00908 if (escape){
00909
00910 ptr1->setIsInternalNonFree();
00911 }else{
00912
00913 ptr1->setIsInternalFree();
00914 }
00915 }
00916
00917 void _findInternalCellsOnLSRContour();
00918
00919 public:
00921 void _setAllNotLocal();
00922
00923 PerceptionGridPar* perceptionGridPar(){
00924 return &_perceptionGridPar;
00925 }
00926 void _clear(){
00927 vector<PerceptionCell>::iterator it;
00928 for (it=_board.begin();it!=_board.end();it++){
00929 (*it).setIsUnknown();
00930 (*it).setIsNotLSRContour();
00931 }
00932 };
00933 void _scanLineFill(int xx,int yy);
00934
00937 void _computeLSR(Pose pose);
00938
00939 bool _isUnknown(DubInt indexes);
00940
00941 void _setUnknown(DubInt indexes,Decimal useless = 0);
00942
00943 void _eraseLocalRegion(Pose pose);
00944
00948 string getObjectName() const {
00949 return "PerceptionGrid";
00950 }
00951
00953 PerceptionGrid(PerceptionGridPar perceptionGridPar,Scan currScan);
00954
00956 PerceptionGrid(const PerceptionGrid& g);
00957
00959 PerceptionGrid operator=(const PerceptionGrid& g);
00960
00962 ~PerceptionGrid();
00963
00964
00967 vector<PerceptionCell*> findFrameCells(int frame);
00968
00971 vector<PerceptionCell*> bresenham(PerceptionCell* startCell, PerceptionCell* stopCell);
00972
00975 void defineLSR();
00976
00979 void defineLSRBoundary( int iMinArcSize = 3, int iMinRadialSize = 1, bool bIncludeObstacleEndPoints = false );
00980
00982 vector<DubInt> computeLSRContour(Scan & scan, int iMinArcSize=3, int iMinRadialSize=1, bool bIncludeObstacleEndPoints=false);
00983
00984
00985 void _eraseLSRContour();
00986
00992 vector<DubInt> computeLSRContour(Scan & scan, Pose pose, Decimal maxDistance);
00993
00995 void _addRobotShape(vector<DubInt> * lsrContour,PerceptionFrontierArc * currentArc,Pose pose);
00996
00997
01000 void definePerceptionFrontier();
01001
01004 void defineLRR();
01005
01009 void defineBoard( int iMinArcSize = 3, int iMinRadialSize = 1, bool bIncludeObstacleEndPoints = false );
01010
01013 void updateLSR();
01014
01017 void updatePerceptionFrontier();
01018
01022 bool initializeScan(ScanPar params, Scan &currScan);
01023
01024
01025
01027 PerceptionFrontier Frontier();
01028
01030 LSRBound lsrBound();
01031
01033 LSR lsr();
01034
01036 LRR lrr();
01037
01044 void getExpectedScan(Scan& expScan, Pose pose, Decimal range, unsigned int samples, Angle angWidth);
01045
01047 Decimal cellSize();
01048
01050 Decimal width();
01051
01053 Decimal height();
01054
01056 int columns();
01057
01059 int rows();
01060
01062 Pose center();
01063
01065 PerceptionCell* centralCell();
01066
01068 Position LLC();
01069
01071 Position RLC();
01072
01074 Position LUC();
01075
01077 Position RUC();
01078
01080 PerceptionCell* getCell(int index);
01081
01083 PerceptionCell* operator[](int index);
01084
01086 PerceptionCell* operator()(int i,int j);
01087
01089 PerceptionCell* getCell (int i, int j);
01090
01092 PerceptionCell* getCell (DubInt indexes);
01093
01095 PerceptionCell* getCell(Position pos);
01096
01099 PerceptionCell* getCellOnGlobal (DubInt indexes,int minI,int minJ);
01100
01103 PerceptionCell* getCellOnGlobal (Position pos,int minI,int minJ, Position globalCenter);
01104
01106 Position alligne(Position pos);
01107
01111
01112
01113 void addScan2(Scan& scan, Pose scanPose);
01114
01117 void frontierToVectVectPosition(vector< vector< Position > >& vectPos, PerceptionFrontier& frontiere);
01118
01121 vector < vector<Position> > frontierToVectVectPosition(PerceptionFrontier& frontiere);
01122
01124 void addScan(Scan & scan,Pose & pose);
01125 };
01126
01127
01128 };
01129
01130
01131
01132 #endif