00001 #include <OGRE/OgreSceneNode.h>
00002 #include <OGRE/OgreSceneManager.h>
00003 
00004 #include <tf/transform_listener.h>
00005 
00006 #include <rviz/visualization_manager.h>
00007 #include <rviz/properties/color_property.h>
00008 #include <rviz/properties/float_property.h>
00009 #include <rviz/properties/int_property.h>
00010 #include <rviz/properties/ros_topic_property.h>
00011 #include <rviz/frame_manager.h>
00012 #include <rviz/validate_floats.h>
00013 
00014 #include <boost/foreach.hpp>
00015 #include "ambient_sound_visual.h"
00016 #include "ambient_sound_display_groovy.h"
00017 
00018 namespace jsk_rviz_plugins
00019 {
00020 
00021     AmbientSoundDisplay::AmbientSoundDisplay()
00022     {
00023           color_property_ = new rviz::ColorProperty("Color",QColor( 204, 51, 204),
00024                   "Color to draw the acceleration arrows." ,
00025                   this, SLOT(updateColorAndAlpha()));
00026           alpha_property_ = new rviz::FloatProperty( "Alpha", 1.0,
00027                   "0 is fully transparent, 1.0 is fully opaque.",
00028                   this, SLOT( updateColorAndAlpha() ));
00029           history_length_property_ = new rviz::IntProperty("History Length", 1,
00030                   "Number of prior measurements to display." ,
00031                   this, SLOT(updateHistoryLength()));
00032           width_property_ = new rviz::FloatProperty("Width", 0.1,
00033                   "Width of line",
00034                   this, SLOT(updateAppearance()));
00035           scale_property_ = new rviz::FloatProperty("Scale", 1.0,
00036                   "Scale of line",
00037                   this, SLOT(updateAppearance()));
00038           bias_property_ = new rviz::FloatProperty("Bias", 10,
00039                   "Bias",
00040                   this, SLOT(updateAppearance()));
00041           grad_property_ = new rviz::FloatProperty("Gradient", 0.1,
00042                   "Gradient",
00043                   this, SLOT(updateAppearance()));
00044           history_length_property_->setMin( 1 );
00045           history_length_property_->setMax( 1 );
00046     }
00047 
00048     
00049     
00050     
00051     void AmbientSoundDisplay::onInitialize()
00052     {
00053         MFDClass::onInitialize();
00054         
00055         
00056 
00057         
00058         
00059         updateHistoryLength();
00060 
00061         
00062         
00063         
00064 
00065 
00066 
00067 
00068 
00069 
00070 
00071         
00072         
00073         
00074 
00075 
00076     }
00077 
00078     AmbientSoundDisplay::~AmbientSoundDisplay()
00079     {
00080 
00081 
00082 
00083 
00084 
00085 
00086     }
00087 
00088     
00089     void AmbientSoundDisplay::reset()
00090     {
00091         MFDClass::reset();
00092         visuals_.clear();
00093     }
00094 
00095 
00096 
00097 
00098 
00099 
00100 
00101 
00102 
00103 
00104 
00105 
00106 
00107 
00108     
00109     void AmbientSoundDisplay::updateColorAndAlpha()
00110     {
00111         float alpha = alpha_property_->getFloat();
00112         Ogre::ColourValue color = color_property_->getOgreColor();
00113 
00114         for( size_t i = 0; i < visuals_.size(); i++ )
00115         {
00116             if( visuals_[ i ] )
00117             {
00118                 visuals_[ i ]->setColor( color.r, color.g, color.b, alpha );
00119             }
00120         }
00121     }
00122 
00123     
00124     void AmbientSoundDisplay::updateAppearance(){
00125         float width = width_property_->getFloat();
00126         float scale = scale_property_->getFloat();
00127         float bias = bias_property_->getFloat();
00128         float grad = grad_property_->getFloat();
00129 
00130         for( size_t i = 0; i < visuals_.size(); i++ )
00131         {
00132             if( visuals_[ i ] )
00133             {
00134                 visuals_[ i ]->setWidth( width );
00135                 visuals_[ i ]->setScale( scale );
00136                 visuals_[ i ]->setBias ( bias );
00137                 visuals_[ i ]->setGrad ( grad );
00138             }
00139         }
00140     }
00141 
00142     
00143     void AmbientSoundDisplay::updateHistoryLength(){
00144         visuals_.rset_capacity(history_length_property_->getInt());
00145     }
00146 
00147     bool AmbientSoundDisplay::validateFloats( const jsk_hark_msgs::HarkPower& msg )
00148     {
00149         std::vector<float>::const_iterator it = msg.powers.begin();
00150         for (; it < msg.powers.end(); ++it) {
00151             if(!rviz::validateFloats(*it)){
00152                 return false;
00153             };
00154         }        
00155         return true;
00156     }
00157 
00158     
00159     
00161         
00162         
00163             
00164         
00166         
00167         
00168             
00169         
00170 
00172         
00173         
00174 
00176         
00177 
00180         
00181             
00182             
00183         
00184         
00185             
00186             
00187             
00188             
00189         
00190 
00192         
00193             
00194         
00195 
00198 
00201         
00202     
00203 
00204     
00205     
00206     
00207     
00208         
00209         
00210     
00211 
00212     
00213     void AmbientSoundDisplay::processMessage( const jsk_hark_msgs::HarkPower::ConstPtr& msg )
00214     {
00215         if( !validateFloats( *msg ))
00216         {
00217             setStatus( rviz::StatusProperty::Error, "Topic", "Message contained invalid floating point values (nans or infs)" );
00218             return;
00219         }
00220 
00221         
00222         
00223         
00224         Ogre::Quaternion orientation;
00225         Ogre::Vector3 position;
00226         if( !context_->getFrameManager()->getTransform( msg->header.frame_id,
00227                     msg->header.stamp,
00228                     position, orientation ))
00229         {
00230             ROS_DEBUG( "Error transforming from frame '%s' to frame '%s'",
00231                     msg->header.frame_id.c_str(), qPrintable( fixed_frame_ ));
00232             return;
00233         }
00234 
00235         
00236         
00237         std::shared_ptr<AmbientSoundVisual> visual;
00238         
00239         if( visuals_.full())
00240         {
00241             visual =  visuals_.front();
00242             
00243             
00244             
00245         } else {
00246             visual.reset(new AmbientSoundVisual(context_->getSceneManager(), scene_node_));
00247         }
00248 
00249         
00250         visual->setMessage( msg );
00251         visual->setFramePosition( position );
00252         visual->setFrameOrientation( orientation );
00253 
00254         float alpha = alpha_property_->getFloat();
00255         Ogre::ColourValue color = color_property_->getOgreColor();
00256         float width = width_property_->getFloat();
00257         float scale = scale_property_->getFloat();
00258         float bias = bias_property_->getFloat();
00259         float grad = grad_property_->getFloat();
00260 
00261         visual->setColor( color.r, color.g, color.b, alpha );
00262         visual->setWidth( width );
00263         visual->setScale( scale );
00264         visual->setBias ( bias );
00265         visual->setGrad ( grad );
00266 
00267         visuals_.push_back(visual);
00268     }
00269 
00270 } 
00271 
00272 
00273 
00274 #include <pluginlib/class_list_macros.h>
00275 PLUGINLIB_EXPORT_CLASS( jsk_rviz_plugins::AmbientSoundDisplay, rviz::Display )
00276