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
00029
00032 #include <iostream>
00033 #include <vector>
00034 #include <string>
00035 #include <time.h>
00036 #include <math.h>
00037 #include <stdlib.h>
00038 #include <stdio.h>
00039 #include <queue>
00040 #include <sbpl/config.h>
00041 #include <distance_field/propagation_distance_field.h>
00042
00043 using namespace std;
00044
00045 #ifndef _BFS3D_
00046 #define _BFS3D_
00047
00048 #define SMALL_NUM 0.00000001
00049 #define INFINITE_COST 1000000000
00050
00051 #define DEBUG_TIME 0
00052 #define DIRECTIONS3D 26
00053 #define GOAL_TOLERANCE 0
00054
00055 typedef struct
00056 {
00057 unsigned int g;
00058 short unsigned int iterationclosed;
00059 short unsigned int x;
00060 short unsigned int y;
00061 short unsigned int z;
00062 } State3D;
00063
00064
00065 class BFS3D
00066 {
00067 public:
00068
00075 BFS3D(int dim_x, int dim_y, int dim_z, int radius, int cost_per_cell);
00076
00078 ~BFS3D();
00079
00082 void init();
00083
00087 bool setGoal(std::vector<short unsigned int> goal);
00088
00092 bool setGoals(std::vector<std::vector<short unsigned int> > goals);
00093
00096 bool runBFS();
00097
00099 int getDist(int x, int y, int z);
00100
00102 void printGrid();
00103
00105 void printCostToGoal();
00106
00112 bool getShortestPath(std::vector<short unsigned int> start, std::vector<std::vector<int> > &path);
00113
00119 void configDistanceField(bool enable, const distance_field::PropagationDistanceField* df);
00120
00122 void printConfig(FILE* fOut);
00123
00124 private:
00125
00126 short unsigned int dimX_;
00127 short unsigned int dimY_;
00128 short unsigned int dimZ_;
00129
00130 short unsigned int radius_;
00131 double radius_m_;
00132
00133 std::vector<std::vector<short unsigned int> > goal_;
00134
00135 int cost_1_move_;
00136 int cost_sqrt2_move_;
00137 int cost_sqrt3_move_;
00138
00139 bool enable_df_;
00140 const distance_field::PropagationDistanceField* df_;
00141
00142 unsigned char*** grid3D_;
00143
00144 int dist_length_;
00145 std::vector<int> dist_;
00146
00147 void reInitializeState3D(State3D* state);
00148 void initializeState3D(State3D* state, short unsigned int x, short unsigned int y, short unsigned int z);
00149 void create3DStateSpace(State3D**** statespace3D);
00150 void delete3DStateSpace(State3D**** statespace3D);
00151 inline int xyzToIndex(int x, int y, int z);
00152 void search3DwithQueue(State3D*** statespace);
00153 bool isGoal(const std::vector<int> &state);
00154 bool isValidCell(const int x, const int y, const int z);
00155 };
00156
00157 inline int BFS3D::xyzToIndex(int x, int y, int z)
00158 {
00159 int ret = x + y*dimX_ + z*dimX_*dimY_;
00160 if(ret < dist_length_)
00161 return ret;
00162 else
00163 {
00164 SBPL_WARN("[BFS3D] out of bounds (%d %d %d) (index: %d size: %d)\n", x,y,z,ret,dist_length_);
00165 return 0;
00166 }
00167 }
00168
00169 inline int BFS3D::getDist(int x, int y, int z)
00170 {
00171 return dist_[xyzToIndex(x,y,z)];
00172 }
00173
00174 #endif
00175