occupancy_grid.cpp
Go to the documentation of this file.
00001 
00002 #include "point_cloud_ros/occupancy_grid.h"
00003 #include "point_cloud_ros/OccupancyGrid.h"
00004 
00005 #include <ros/console.h>
00006 
00007 namespace occupancy_grid
00008 {
00009     OccupancyGrid::OccupancyGrid(float center_x, float center_y, float center_z,
00010                                  float size_x, float size_y, float size_z,
00011                                  float res_x, float res_y, float res_z)
00012     {
00013         nx_ = int (size_x / res_x + 0.5);
00014         ny_ = int (size_y / res_y + 0.5);
00015         nz_ = int (size_z / res_z + 0.5);
00016 
00017         res_x_ = res_x; 
00018         res_y_ = res_y; 
00019         res_z_ = res_z; 
00020 
00021         size_x_ = size_x; 
00022         size_y_ = size_y; 
00023         size_z_ = size_z; 
00024 
00025         center_x_ = center_x; 
00026         center_y_ = center_y; 
00027         center_z_ = center_z; 
00028 
00029         data_ = new uint32_t[nx_ * ny_ * nz_];
00030         for(unsigned int i = 0; i < nx_ * ny_ *nz_; i++)
00031             data_[i] = 0;
00032     }
00033 
00034     OccupancyGrid::~OccupancyGrid()
00035     {
00036         delete [] data_;
00037     }
00038 
00039     unsigned int OccupancyGrid::nX()
00040     {
00041         return nx_;
00042     }
00043 
00044     unsigned int OccupancyGrid::nY()
00045     {
00046         return ny_;
00047     }
00048 
00049     unsigned int OccupancyGrid::nZ()
00050     {
00051         return nz_;
00052     }
00053 
00054     uint32_t* OccupancyGrid::getData()
00055     {
00056         return data_;
00057     }
00058     
00059     void OccupancyGrid::fillOccupancyGrid(const sensor_msgs::PointCloud cloud)
00060     {
00061         float x, y, z;
00062         int idx_x, idx_y, idx_z;
00063         float min_x = center_x_ - size_x_ / 2;
00064         float min_y = center_y_ - size_y_ / 2;
00065         float min_z = center_z_ - size_z_ / 2;
00066 
00067         for (size_t i = 0; i < cloud.points.size(); i++)
00068         {
00069             x = cloud.points[i].x;
00070             y = cloud.points[i].y;
00071             z = cloud.points[i].z;
00072 
00073             idx_x = int( (x - min_x) / res_x_ + 0.5);
00074             idx_y = int( (y - min_y) / res_y_ + 0.5);
00075             idx_z = int( (z - min_z) / res_z_ + 0.5);
00076 
00077             if (idx_x >= 0 and idx_x < (int)nx_ and idx_y >= 0 and \
00078                 idx_y < (int)ny_ and idx_z >= 0 and idx_z < (int)nz_)
00079                 data_[idx_x * nz_ * ny_ + idx_y * nz_ + idx_z] += 1;
00080 //                data_[idx_z * nx_ * ny_ + idx_y * nx_ + idx_x] += 1;
00081         }
00082     }
00083 
00084     sensor_msgs::PointCloud OccupancyGrid::gridToPoints()
00085     {
00086         sensor_msgs::PointCloud cloud;
00087 
00088         for(unsigned int x_idx=0; x_idx<nx_; x_idx++)
00089             for(unsigned int y_idx=0; y_idx<ny_; y_idx++)
00090                 for(unsigned int z_idx=0; z_idx<nz_; z_idx++)
00091                     if (data_[x_idx * nz_ * ny_ + y_idx * nz_ + z_idx] > 0)
00092                     {
00093                         geometry_msgs::Point32 pt;
00094                         pt.x = x_idx*res_x_ + center_x_ - size_x_/2;
00095                         pt.y = y_idx*res_y_ + center_y_ - size_y_/2;
00096                         pt.z = z_idx*res_z_ + center_z_ - size_z_/2;
00097                         cloud.points.push_back(pt);
00098                     }
00099         return cloud;
00100     }
00101 
00102 };
00103 
00104 


point_cloud_ros
Author(s): Advait Jain (Healthcare Robotics Lab, Georgia Tech)
autogenerated on Wed Nov 27 2013 12:16:42