Go to the documentation of this file.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
00031
00032
00033
00034
00035
00036 #include "sparse_occupancy_grid_array_display.h"
00037
00038 namespace jsk_rviz_plugin
00039 {
00040 SparseOccupancyGridArrayDisplay::SparseOccupancyGridArrayDisplay()
00041 {
00042 alpha_property_ = new rviz::FloatProperty(
00043 "Alpha", 1.0,
00044 "Amount of transparency to apply to the polygon.",
00045 this, SLOT( updateAlpha() ));
00046
00047 alpha_property_->setMin(0.0);
00048 alpha_property_->setMax(1.0);
00049
00050 axis_color_property_ = new rviz::BoolProperty("Axis Color", false,
00051 "coloring according to the angle of the plane",
00052 this, SLOT(updateAxisColor()));
00053 max_color_property_ = new rviz::ColorProperty("Max Color", QColor(255, 255, 255),
00054 "maximum color to draw grid map",
00055 this, SLOT(updateMaxColor()));
00056 min_color_property_ = new rviz::ColorProperty("Min Color", QColor(0, 0, 0),
00057 "minimum color to draw grid map",
00058 this, SLOT(updateMinColor()));
00059 }
00060
00061 SparseOccupancyGridArrayDisplay::~SparseOccupancyGridArrayDisplay()
00062 {
00063 delete alpha_property_;
00064 delete max_color_property_;
00065 delete min_color_property_;
00066 delete axis_color_property_;
00067 allocateCloudsAndNodes(0);
00068 }
00069
00070 void SparseOccupancyGridArrayDisplay::onInitialize()
00071 {
00072 MFDClass::onInitialize();
00073 updateAlpha();
00074 updateMaxColor();
00075 updateMinColor();
00076 }
00077
00078 void SparseOccupancyGridArrayDisplay::allocateCloudsAndNodes(const size_t num)
00079 {
00080 if (num > clouds_.size()) {
00081 for (size_t i = clouds_.size(); i < num; i++) {
00082 Ogre::SceneNode* node = scene_node_->createChildSceneNode();
00083 rviz::PointCloud* cloud = new rviz::PointCloud();
00084 cloud->setRenderMode(rviz::PointCloud::RM_TILES);
00085 cloud->setCommonDirection( Ogre::Vector3::UNIT_Z );
00086 cloud->setCommonUpVector( Ogre::Vector3::UNIT_Y );
00087 node->attachObject(cloud);
00088 clouds_.push_back(cloud);
00089 nodes_.push_back(node);
00090 }
00091 }
00092 else if (num < clouds_.size())
00093 {
00094 for (size_t i = num; i < clouds_.size(); i++) {
00095 nodes_[i]->detachObject(clouds_[i]);
00096 delete clouds_[i];
00097 scene_manager_->destroySceneNode(nodes_[i]);
00098 }
00099 clouds_.resize(num);
00100 nodes_.resize(num);
00101 }
00102 }
00103
00104 void SparseOccupancyGridArrayDisplay::reset()
00105 {
00106 MFDClass::reset();
00107 allocateCloudsAndNodes(0);
00108 }
00109
00110 QColor SparseOccupancyGridArrayDisplay::gridColor(double value)
00111 {
00112
00113 return QColor(
00114 (value * (max_color_.red() - min_color_.red()) + min_color_.red()),
00115 (value * (max_color_.green() - min_color_.green()) + min_color_.green()),
00116 (value * (max_color_.blue() - min_color_.blue()) + min_color_.blue()));
00117 }
00118
00119 QColor SparseOccupancyGridArrayDisplay::axisColor(const Ogre::Quaternion& q,
00120 const Ogre::Vector3& p)
00121 {
00122 Ogre::Vector3 zaxis = q.zAxis();
00123 Ogre::Vector3 reference = p.normalisedCopy();
00124 double dot = zaxis.dotProduct(reference);
00125 if (dot < -1) {
00126 dot = -1.0;
00127 }
00128 else if (dot > 1) {
00129 dot = 1.0;
00130 }
00131 double scale = (dot + 1) / 2.0;
00132 return gridColor(scale);
00133 }
00134
00135 void SparseOccupancyGridArrayDisplay::processMessage(
00136 const jsk_pcl_ros::SparseOccupancyGridArray::ConstPtr& msg)
00137 {
00138 allocateCloudsAndNodes(msg->grids.size());
00139 for (size_t i = 0; i < msg->grids.size(); i++) {
00140 Ogre::SceneNode* node = nodes_[i];
00141 rviz::PointCloud* cloud = clouds_[i];
00142 const jsk_pcl_ros::SparseOccupancyGrid grid = msg->grids[i];
00143 Ogre::Vector3 position;
00144 Ogre::Quaternion quaternion;
00145 if(!context_->getFrameManager()->transform(grid.header, grid.origin_pose,
00146 position,
00147 quaternion)) {
00148 ROS_ERROR( "Error transforming pose '%s' from frame '%s' to frame '%s'",
00149 qPrintable( getName() ), grid.header.frame_id.c_str(),
00150 qPrintable( fixed_frame_ ));
00151 return;
00152 }
00153 node->setPosition(position);
00154 node->setOrientation(quaternion);
00155 cloud->setDimensions(grid.resolution, grid.resolution, 0.0);
00156 std::vector<rviz::PointCloud::Point> points;
00157 for (size_t ci = 0; ci < grid.columns.size(); ci++) {
00158 const jsk_pcl_ros::SparseOccupancyGridColumn column = grid.columns[ci];
00159 const int column_index = column.column_index;
00160 for (size_t ri = 0; ri < column.cells.size(); ri++) {
00161 const jsk_pcl_ros::SparseOccupancyGridCell cell = column.cells[ri];
00162 const int row_index = cell.row_index;
00163 rviz::PointCloud::Point point;
00164 if (!axis_color_) {
00165 QColor color = gridColor(cell.value);
00166 Ogre::ColourValue ogre_color = rviz::qtToOgre(color);
00167 point.color = ogre_color;
00168 }
00169 else {
00170 QColor color = axisColor(quaternion, Ogre::Vector3(1, 0, 0));
00171 Ogre::ColourValue ogre_color = rviz::qtToOgre(color);
00172 point.color = ogre_color;
00173 }
00174 point.position.x = grid.resolution * (column_index + 0.5);
00175 point.position.y = grid.resolution * (row_index + 0.5);
00176 point.position.z = 0.0;
00177
00178 points.push_back(point);
00179 }
00180 }
00181 cloud->clear();
00182 cloud->setAlpha(alpha_);
00183 if (!points.empty()) {
00184 cloud->addPoints(&points.front(), points.size());
00185 }
00186 }
00187 context_->queueRender();
00188 }
00189
00190 void SparseOccupancyGridArrayDisplay::updateAlpha()
00191 {
00192 alpha_ = alpha_property_->getFloat();
00193 }
00194
00195 void SparseOccupancyGridArrayDisplay::updateMaxColor()
00196 {
00197 max_color_ = max_color_property_->getColor();
00198 }
00199
00200 void SparseOccupancyGridArrayDisplay::updateMinColor()
00201 {
00202 min_color_ = min_color_property_->getColor();
00203 }
00204
00205 void SparseOccupancyGridArrayDisplay::updateAxisColor()
00206 {
00207 axis_color_ = axis_color_property_->getBool();
00208 }
00209 }
00210
00211 #include <pluginlib/class_list_macros.h>
00212 PLUGINLIB_EXPORT_CLASS( jsk_rviz_plugin::SparseOccupancyGridArrayDisplay, rviz::Display )
00213