footstep_display.cpp
Go to the documentation of this file.
00001 #include "footstep_display.h"
00002 #include <rviz/validate_floats.h>
00003 
00004 namespace jsk_rviz_plugin
00005 {
00006   FootstepDisplay::FootstepDisplay()
00007   {
00008     alpha_property_ =  new rviz::FloatProperty( "Alpha", 0.5,
00009                                                 "0 is fully transparent, 1.0 is fully opaque.",
00010                                                 this, SLOT( updateAlpha() ));
00011     width_property_ =  new rviz::FloatProperty( "Width", 0.15,
00012                                                 "width of the footstep",
00013                                                 this, SLOT( updateFootstepSize() ));
00014     height_property_ =  new rviz::FloatProperty( "height", 0.01,
00015                                                 "height of the footstep",
00016                                                 this, SLOT( updateFootstepSize() ));
00017 
00018     depth_property_ =  new rviz::FloatProperty( "depth", 0.3,
00019                                                 "depth of the footstep",
00020                                                 this, SLOT( updateFootstepSize() ));
00021   }
00022 
00023   FootstepDisplay::~FootstepDisplay()
00024   {
00025     delete alpha_property_;
00026     delete width_property_;
00027     delete height_property_;
00028     delete depth_property_;
00029     delete line_;
00030   }
00031 
00032   void FootstepDisplay::updateFootstepSize()
00033   {
00034     Ogre::Vector3 scale;
00035     scale[0] = depth_property_->getFloat();
00036     scale[1] = width_property_->getFloat();
00037     scale[2] = height_property_->getFloat();
00038     for (size_t i = 0; i < shapes_.size(); i++)
00039     {
00040       shapes_[i]->setScale(scale);
00041     }
00042   }
00043   
00044   void FootstepDisplay::updateAlpha()
00045   {
00046     float alpha = alpha_property_->getFloat();
00047     for (size_t i = 0; i < shapes_.size(); i++)
00048     {
00049       ShapePtr shape = shapes_[i];
00050       jsk_footstep_msgs::Footstep footstep = latest_footstep_->footsteps[i];
00051       if (footstep.leg == jsk_footstep_msgs::Footstep::LEFT)
00052       {
00053         shape->setColor(0, 1, 0, alpha);
00054       }
00055       else if (footstep.leg == jsk_footstep_msgs::Footstep::RIGHT)
00056       {
00057         shape->setColor(1, 0, 0, alpha);
00058       }
00059       else
00060       {
00061         shape->setColor(1, 1, 1, alpha);
00062       }
00063     }
00064       
00065   }
00066   
00067   void FootstepDisplay::reset()
00068   {
00069     MFDClass::reset();
00070     shapes_.clear();
00071     line_->clear();
00072   }
00073 
00074   bool FootstepDisplay::validateFloats( const jsk_footstep_msgs::FootstepArray& msg )
00075   {
00076     for (std::vector<jsk_footstep_msgs::Footstep>::const_iterator it = msg.footsteps.begin();
00077          it != msg.footsteps.end();
00078          ++it) {
00079       if (!rviz::validateFloats((*it).pose.position.x)
00080           || !rviz::validateFloats((*it).pose.position.y)
00081           || !rviz::validateFloats((*it).pose.position.z)
00082           || !rviz::validateFloats((*it).pose.orientation.x)
00083           || !rviz::validateFloats((*it).pose.orientation.y)
00084           || !rviz::validateFloats((*it).pose.orientation.z)
00085           || !rviz::validateFloats((*it).pose.orientation.w)
00086         ) {
00087         return false;
00088       }
00089     }
00090     return true;
00091   }
00092 
00093   void FootstepDisplay::onInitialize()
00094   {
00095     MFDClass::onInitialize();
00096     scene_node_ = scene_manager_->getRootSceneNode()->createChildSceneNode();
00097     line_ = new rviz::BillboardLine(context_->getSceneManager(), scene_node_);
00098   }
00099   
00100   void FootstepDisplay::processMessage(const jsk_footstep_msgs::FootstepArray::ConstPtr& msg)
00101   {
00102     if (!validateFloats(*msg)) {
00103       setStatus(rviz::StatusProperty::Error, "Topic", "message contained invalid floating point values (nans or infs)");
00104       return;
00105     }
00106     latest_footstep_ = msg;
00107     Ogre::Quaternion orientation;
00108     Ogre::Vector3 position;
00109     if( !context_->getFrameManager()->getTransform( msg->header.frame_id,
00110                                                     msg->header.stamp,
00111                                                     position, orientation ))
00112     {
00113       ROS_DEBUG( "Error transforming from frame '%s' to frame '%s'",
00114                  msg->header.frame_id.c_str(), qPrintable( fixed_frame_ ));
00115       return;
00116     }
00117 
00118     // check thhe length of the shapes_
00119     if (msg->footsteps.size() > shapes_.size())
00120     {
00121       // need to allocate
00122       for (size_t i = shapes_.size(); i < msg->footsteps.size(); i++)
00123       {
00124         ShapePtr shape;
00125         shape.reset(new rviz::Shape(rviz::Shape::Cube, context_->getSceneManager(),
00126                                     scene_node_));
00127         shapes_.push_back(shape);
00128       }
00129     }
00130     else if (msg->footsteps.size() < shapes_.size())
00131     {
00132       // need to remove
00133       shapes_.resize(msg->footsteps.size());
00134     }
00135 
00136     line_->clear();
00137     line_->setLineWidth(0.01);
00138     line_->setNumLines(1);
00139     line_->setMaxPointsPerLine(1024);
00140 
00141     for (size_t i = 0; i < msg->footsteps.size(); i++)
00142     {
00143       ShapePtr shape = shapes_[i];
00144       jsk_footstep_msgs::Footstep footstep = msg->footsteps[i];
00145       Ogre::Vector3 step_position;
00146       Ogre::Quaternion step_quaternion;
00147       if( !context_->getFrameManager()->transform( msg->header, footstep.pose,
00148                                                    step_position,
00149                                                    step_quaternion ))
00150       {
00151         ROS_ERROR( "Error transforming pose '%s' from frame '%s' to frame '%s'",
00152                    qPrintable( getName() ), msg->header.frame_id.c_str(), qPrintable( fixed_frame_ ));
00153         return;
00154       }
00155       shape->setPosition(step_position);
00156       shape->setOrientation(step_quaternion);
00157 
00158       line_->addPoint(step_position);
00159       
00160     }
00161     updateFootstepSize();
00162     updateAlpha();
00163     context_->queueRender();
00164   }
00165 
00166 }
00167 
00168 #include <pluginlib/class_list_macros.h>
00169 PLUGINLIB_EXPORT_CLASS( jsk_rviz_plugin::FootstepDisplay, rviz::Display )


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