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
00030 #include <sys/stat.h>
00031 #include <vector>
00032 #include <fstream>
00033 #include <ros/ros.h>
00034 #include <sbpl/config.h>
00035 #include <LinearMath/btTransform.h>
00036 #include <distance_field/distance_field.h>
00037 #include <distance_field/voxel_grid.h>
00038 #include <distance_field/propagation_distance_field.h>
00039
00040 using namespace std;
00041
00042 #ifndef _OCCUPANCY_GRID_
00043 #define _OCCUPANCY_GRID_
00044
00045 class OccupancyGrid{
00046
00047 public:
00048
00050 OccupancyGrid();
00051
00062 OccupancyGrid(double dim_x, double dim_y, double dim_z, double resolution, double origin_x, double origin_y, double origin_z);
00063
00065 ~OccupancyGrid();
00066
00068 inline void gridToWorld(int x, int y, int z, double &wx, double &wy, double &wz);
00069
00071 inline void worldToGrid(double wx, double wy, double wz, int &x, int &y, int &z);
00072
00074 inline unsigned char getCell(int x, int y, int z);
00075
00077 inline double getCell(int *xyz);
00078
00080 inline bool isInBounds(int x, int y, int z);
00081
00083 const distance_field::PropagationDistanceField* getDistanceFieldPtr();
00084
00086 void getGridSize(int &dim_x, int &dim_y, int &dim_z);
00087
00089 void getGridSize(short unsigned int *dims);
00090
00092 void setWorldSize(double dim_x, double dim_y, double dim_z);
00093
00095 void getWorldSize(double &dim_x, double &dim_y, double &dim_z);
00096
00098 void setOrigin(double wx, double wy, double wz);
00099
00101 void getOrigin(double &wx, double &wy, double &wz);
00102
00104 void setResolution(double resolution_m);
00105
00107 double getResolution();
00108
00110 void updateFromCollisionMap(const mapping_msgs::CollisionMap &collision_map);
00111
00113 void visualize();
00114
00124 void addCollisionCuboid(double origin_x, double origin_y, double origin_z, double size_x, double size_y, double size_z);
00125
00126 void addPointsToField(const std::vector<btVector3> &points);
00127
00128 void getVoxelsInBox(const geometry_msgs::Pose &pose, const std::vector<double> &dim, std::vector<btVector3> &voxels);
00129 bool saveGridToBinaryFile(std::string filename);
00130
00131 void printGridFromBinaryFile(std::string filename);
00132
00133 std::string getReferenceFrame();
00134
00135 private:
00136
00137 double origin_[3];
00138 double grid_resolution_;
00139 double world_size_[3];
00140 double prop_distance_;
00141 int grid_size_[3];
00142
00143 std::string reference_frame_;
00144 distance_field::PropagationDistanceField* grid_;
00145
00146 std::vector<btVector3> cuboid_points_;
00147 };
00148
00149 inline void OccupancyGrid::gridToWorld(int x, int y, int z, double &wx, double &wy, double &wz)
00150 {
00151 grid_->gridToWorld(x, y, z, wx, wy, wz);
00152 }
00153
00154 inline void OccupancyGrid::worldToGrid(double wx, double wy, double wz, int &x, int &y, int &z)
00155 {
00156 grid_->worldToGrid (wx, wy, wz, x, y, z);
00157 }
00158
00159 inline unsigned char OccupancyGrid::getCell(int x, int y, int z)
00160 {
00161 return (unsigned char)(grid_->getDistanceFromCell(x,y,z) / grid_resolution_);
00162 }
00163
00164 inline double OccupancyGrid::getCell(int *xyz)
00165 {
00166 return grid_->getDistanceFromCell(xyz[0],xyz[1],xyz[2]);
00167 }
00168
00169 inline bool OccupancyGrid::isInBounds(int x, int y, int z)
00170 {
00171 if(x >= grid_size_[0] || 0 > x || y >= grid_size_[1] || 0 > y || z >= grid_size_[2] || 0 > z)
00172 return false;
00173
00174 return true;
00175 }
00176
00177 inline std::string OccupancyGrid::getReferenceFrame()
00178 {
00179 return reference_frame_;
00180 }
00181
00182 inline void OccupancyGrid::addPointsToField(const std::vector<btVector3> &points)
00183 {
00184 grid_->addPointsToField(points);
00185 }
00186
00187 #endif