sparse_occupancy_grid_array_display.cpp
Go to the documentation of this file.
00001 // -*- mode: c++; -*-
00002 /*********************************************************************
00003  * Software License Agreement (BSD License)
00004  *
00005  *  Copyright (c) 2014, JSK Lab
00006  *  All rights reserved.
00007  *
00008  *  Redistribution and use in source and binary forms, with or without
00009  *  modification, are permitted provided that the following conditions
00010  *  are met:
00011  *
00012  *   * Redistributions of source code must retain the above copyright
00013  *     notice, this list of conditions and the following disclaimer.
00014  *   * Redistributions in binary form must reproduce the above
00015  *     copyright notice, this list of conditions and the following
00016  *     disclaimer in the documentation and/o2r other materials provided
00017  *     with the distribution.
00018  *   * Neither the name of the Willow Garage nor the names of its
00019  *     contributors may be used to endorse or promote products derived
00020  *     from this software without specific prior written permission.
00021  *
00022  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00023  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00024  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00025  *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00026  *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00027  *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00028  *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00029  *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00030  *  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00031  *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00032  *  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00033  *  POSSIBILITY OF SUCH DAMAGE.
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()) { // need to allocate new node and clouds
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()) // need to destroy
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     // normalize to 0~1
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()); // not enough
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;                 // 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 


jsk_rviz_plugins
Author(s): Kei Okada , Yohei Kakiuchi , Shohei Fujii , Ryohei Ueda
autogenerated on Mon Oct 6 2014 01:18:44