Go to the documentation of this file.00001
00002
00003 #include "simple_occupancy_grid/occupancy_grid.h"
00004
00005
00006 #include <visualization_msgs/Marker.h>
00007 #include <geometry_msgs/Point.h>
00008 #include <std_msgs/ColorRGBA.h>
00009
00010 #include <stdio.h>
00011
00012 namespace occupancy_grid
00013 {
00014
00015
00016 OccupancyGrid::OccupancyGrid(float center_x, float center_y, float center_z,
00017 float size_x, float size_y, float size_z,
00018 float res_x, float res_y, float res_z) :
00019 nh_("~")
00020 {
00021
00022
00023 nx_ = int (size_x / res_x + 0.5);
00024 ny_ = int (size_y / res_y + 0.5);
00025 nz_ = int (size_z / res_z + 0.5);
00026
00027 res_x_ = res_x;
00028 res_y_ = res_y;
00029 res_z_ = res_z;
00030
00031 size_x_ = size_x;
00032 size_y_ = size_y;
00033 size_z_ = size_z;
00034
00035 center_x_ = center_x;
00036 center_y_ = center_y;
00037 center_z_ = center_z;
00038
00039 occupancy_count_array_ = new uint32_t[nx_ * ny_ * nz_];
00040 for(unsigned int i = 0; i < nx_ * ny_ *nz_; i++)
00041 occupancy_count_array_[i] = 0;
00042
00043 marker_pub_ = nh_.advertise<visualization_msgs::Marker>("viz", 5);
00044
00045
00046 sub_cmd_viz_simple_ = nh_.subscribe("cmd/viz_simple", 5,
00047 &OccupancyGrid::publishMarkerArray_simple_cb,
00048 this);
00049
00050 srv_add_points_unstamped_ = nh_.advertiseService("srv/add_points_unstamped",
00051 &OccupancyGrid::addPointsUnstamped_srv,
00052 this);
00053 }
00054
00055 void OccupancyGrid::addPointsUnstamped(const hrl_msgs::FloatArrayBare pts_fab)
00056 {
00057 addPointsUnstamped(pts_fab.data);
00058 }
00059
00060 void OccupancyGrid::addPointsUnstamped(const std::vector<double> pts_vec)
00061 {
00062 float x, y, z;
00063 int idx_x, idx_y, idx_z;
00064 float min_x = center_x_ - size_x_ / 2;
00065 float min_y = center_y_ - size_y_ / 2;
00066 float min_z = center_z_ - size_z_ / 2;
00067
00068 for (size_t i = 0; i < pts_vec.size(); i=i+3)
00069 {
00070 x = pts_vec[i];
00071 y = pts_vec[i+1];
00072 z = pts_vec[i+2];
00073
00074 idx_x = int( (x - min_x) / res_x_);
00075 idx_y = int( (y - min_y) / res_y_);
00076 idx_z = int( (z - min_z) / res_z_);
00077
00078 if (idx_x >= 0 and idx_x < (int)nx_ and idx_y >= 0 and \
00079 idx_y < (int)ny_ and idx_z >= 0 and idx_z < (int)nz_)
00080 occupancy_count_array_[idx_x * nz_ * ny_ + idx_y * nz_ + idx_z] += 1;
00081 }
00082 }
00083
00084
00085 void OccupancyGrid::publishMarkerArray_simple()
00086 {
00087 visualization_msgs::Marker cube_list_marker;
00088 cube_list_marker.header.stamp = ros::Time::now();
00089
00090
00091
00092 cube_list_marker.header.frame_id = "/occupancy_grid_frame";
00093 cube_list_marker.ns = "occupancy_grid_simple";
00094 cube_list_marker.id = 0;
00095 cube_list_marker.action = visualization_msgs::Marker::ADD;
00096 cube_list_marker.lifetime = ros::Duration();
00097 cube_list_marker.type = visualization_msgs::Marker::CUBE_LIST;
00098
00099 cube_list_marker.scale.x = res_x_;
00100 cube_list_marker.scale.y = res_y_;
00101 cube_list_marker.scale.z = res_z_;
00102
00103 std_msgs::ColorRGBA c;
00104 c.r = 0.;
00105 c.g = 1.;
00106 c.b = 1.;
00107
00108 cube_list_marker.color.a = 0.2;
00109
00110 for(unsigned int x_idx=0; x_idx < nx_; x_idx++)
00111 for(unsigned int y_idx=0; y_idx < ny_; y_idx++)
00112 for(unsigned int z_idx=0; z_idx < nz_; z_idx++)
00113 if (occupancy_count_array_[x_idx * nz_ * ny_ + y_idx * nz_ + z_idx] > 0)
00114 {
00115 geometry_msgs::Point pt;
00116 pt.x = x_idx*res_x_ + center_x_ - size_x_/2 + res_x_/2;
00117 pt.y = y_idx*res_y_ + center_y_ - size_y_/2 + res_y_/2;
00118 pt.z = z_idx*res_z_ + center_z_ - size_z_/2 + res_z_/2;
00119 cube_list_marker.points.push_back(pt);
00120 cube_list_marker.colors.push_back(c);
00121 }
00122
00123 marker_pub_.publish(cube_list_marker);
00124 }
00125
00126
00127
00128
00129 void OccupancyGrid::publishMarkerArray_simple_cb(const std_msgs::Empty::ConstPtr& msg)
00130 {
00131 publishMarkerArray_simple();
00132 }
00133
00134 bool OccupancyGrid::addPointsUnstamped_srv(hrl_srvs::FloatArray_None::Request &req,
00135 hrl_srvs::FloatArray_None::Response &res)
00136 {
00137 addPointsUnstamped(req.val);
00138 return true;
00139 }
00140
00141
00142
00143
00144 OccupancyGrid::~OccupancyGrid()
00145 {
00146 delete [] occupancy_count_array_;
00147 }
00148
00149 unsigned int OccupancyGrid::nX()
00150 {
00151 return nx_;
00152 }
00153
00154 unsigned int OccupancyGrid::nY()
00155 {
00156 return ny_;
00157 }
00158
00159 unsigned int OccupancyGrid::nZ()
00160 {
00161 return nz_;
00162 }
00163
00164 uint32_t* OccupancyGrid::getOccupancyCountArray()
00165 {
00166 return occupancy_count_array_;
00167 }
00168
00169
00170 };
00171
00172
00173
00174
00175
00176
00177
00178
00179
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195
00196
00197
00198
00199
00200
00201
00202
00203
00204
00205
00206
00207
00208
00209
00210
00211
00212
00213
00214
00215
00216
00217
00218
00219
00220
00221
00222
00223
00224
00225
00226
00227
00228
00229
00230
00231
00232
00233
00234
00235