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 #include <stdint.h>
00031 #include "grid_display.h"
00032 #include "rviz/common.h"
00033 #include "rviz/visualization_manager.h"
00034 #include "rviz/properties/property.h"
00035 #include "rviz/properties/property_manager.h"
00036 #include "rviz/frame_manager.h"
00037
00038 #include "ogre_tools/grid.h"
00039
00040 #include <boost/bind.hpp>
00041
00042 #include <OGRE/OgreSceneNode.h>
00043 #include <OGRE/OgreSceneManager.h>
00044
00045 namespace rviz
00046 {
00047
00048 GridDisplay::GridDisplay( const std::string& name, VisualizationManager* manager )
00049 : Display( name, manager )
00050 , color_( 0.5f, 0.5f, 0.5f )
00051 , alpha_( 0.5f )
00052 , plane_(XY)
00053 {
00054 offset_ = Ogre::Vector3::ZERO;
00055
00056 scene_node_ = scene_manager_->getRootSceneNode()->createChildSceneNode();
00057 grid_ = new ogre_tools::Grid( scene_manager_, scene_node_, ogre_tools::Grid::Lines, 10, 1.0f, 0.03f, Ogre::ColourValue(color_.r_, color_.g_, color_.b_, alpha_) );
00058 grid_->getSceneNode()->setVisible( false );
00059
00060 setStyle(ogre_tools::Grid::Lines);
00061 setFrame(FIXED_FRAME_STRING);
00062 }
00063
00064 GridDisplay::~GridDisplay()
00065 {
00066 delete grid_;
00067 scene_manager_->destroySceneNode(scene_node_);
00068 }
00069
00070 void GridDisplay::onEnable()
00071 {
00072 grid_->getSceneNode()->setVisible( true );
00073 }
00074
00075 void GridDisplay::onDisable()
00076 {
00077 grid_->getSceneNode()->setVisible( false );
00078 }
00079
00080 void GridDisplay::setCellSize( float size )
00081 {
00082 grid_->setCellLength(size);
00083
00084 propertyChanged(cell_size_property_);
00085
00086 causeRender();
00087 }
00088
00089 void GridDisplay::setCellCount( uint32_t count )
00090 {
00091 grid_->setCellCount(count);
00092
00093 propertyChanged(cell_count_property_);
00094
00095 causeRender();
00096 }
00097
00098 void GridDisplay::setColor( const Color& color )
00099 {
00100 color_ = color;
00101 grid_->setColor(Ogre::ColourValue(color.r_, color.g_, color.b_, alpha_));
00102
00103 propertyChanged(color_property_);
00104
00105 causeRender();
00106 }
00107
00108 void GridDisplay::setLineWidth( float width )
00109 {
00110 grid_->setLineWidth(width);
00111
00112 propertyChanged(line_width_property_);
00113
00114 causeRender();
00115 }
00116
00117 void GridDisplay::setStyle( int style )
00118 {
00119 grid_->setStyle((ogre_tools::Grid::Style)style);
00120
00121 switch (style)
00122 {
00123 case ogre_tools::Grid::Billboards:
00124 showProperty(line_width_property_);
00125 break;
00126 case ogre_tools::Grid::Lines:
00127 hideProperty(line_width_property_);
00128 break;
00129 }
00130
00131 propertyChanged(style_property_);
00132
00133 causeRender();
00134 }
00135
00136 void GridDisplay::setAlpha( float a )
00137 {
00138 alpha_ = a;
00139
00140 grid_->setColor(Ogre::ColourValue(color_.r_, color_.g_, color_.b_, alpha_));
00141
00142 propertyChanged(alpha_property_);
00143
00144 causeRender();
00145 }
00146
00147 void GridDisplay::setHeight( uint32_t height )
00148 {
00149 grid_->setHeight( height );
00150
00151 propertyChanged(height_property_);
00152
00153 causeRender();
00154 }
00155
00156 void GridDisplay::setOffset( const Ogre::Vector3& offset )
00157 {
00158 offset_ = offset;
00159
00160 Ogre::Vector3 ogre_offset = offset;
00161 robotToOgre(ogre_offset);
00162
00163 grid_->getSceneNode()->setPosition(ogre_offset);
00164
00165 propertyChanged(offset_property_);
00166
00167 causeRender();
00168 }
00169
00170 void GridDisplay::setPlane(int plane)
00171 {
00172 plane_ = (Plane)plane;
00173
00174 if (plane_ == XY)
00175 {
00176 grid_->getSceneNode()->setOrientation(1.0f, 0.0f, 0.0f, 0.0f);
00177 }
00178 else if (plane_ == XZ)
00179 {
00180 grid_->getSceneNode()->setOrientation(Ogre::Quaternion(Ogre::Vector3(0.0f, -1.0f, 0.0f), Ogre::Vector3(0.0f, 0.0f, 1.0f), Ogre::Vector3(1.0f, 0.0f, 0.0f)));
00181 }
00182 else if (plane_ == YZ)
00183 {
00184 grid_->getSceneNode()->setOrientation(Ogre::Quaternion(Ogre::Vector3(1.0f, 0.0f, 0.0f), Ogre::Vector3(0.0f, 0.0f, -1.0f), Ogre::Vector3(0.0f, 1.0f, 0.0f)));
00185 }
00186
00187 propertyChanged(plane_property_);
00188
00189 causeRender();
00190 }
00191
00192 void GridDisplay::setFrame(const std::string& frame)
00193 {
00194 frame_ = frame;
00195 propertyChanged(frame_property_);
00196 }
00197
00198 void GridDisplay::update(float dt, float ros_dt)
00199 {
00200 std::string frame = frame_;
00201 if (frame == FIXED_FRAME_STRING)
00202 {
00203 frame = fixed_frame_;
00204 }
00205
00206 Ogre::Vector3 position;
00207 Ogre::Quaternion orientation;
00208 if (vis_manager_->getFrameManager()->getTransform(frame, ros::Time(), position, orientation, true))
00209 {
00210 scene_node_->setPosition(position);
00211 scene_node_->setOrientation(orientation);
00212 setStatus(status_levels::Ok, "Transform", "Transform OK");
00213 }
00214 else
00215 {
00216 std::string error;
00217 if (vis_manager_->getFrameManager()->transformHasProblems(frame, ros::Time(), error))
00218 {
00219 setStatus(status_levels::Error, "Transform", error);
00220 }
00221 else
00222 {
00223 std::stringstream ss;
00224 ss << "Could not transform from [" << frame << "] to [" << fixed_frame_ << "]";
00225 setStatus(status_levels::Error, "Transform", ss.str());
00226 }
00227 }
00228 }
00229
00230 void GridDisplay::createProperties()
00231 {
00232 frame_property_ = property_manager_->createProperty<TFFrameProperty>("Reference Frame", property_prefix_, boost::bind(&GridDisplay::getFrame, this),
00233 boost::bind(&GridDisplay::setFrame, this, _1), parent_category_, this);
00234 setPropertyHelpText(frame_property_, "The TF frame this grid will use for its origin.");
00235
00236 cell_count_property_ = property_manager_->createProperty<IntProperty>( "Plane Cell Count", property_prefix_, boost::bind( &ogre_tools::Grid::getCellCount, grid_),
00237 boost::bind( &GridDisplay::setCellCount, this, _1 ), parent_category_, this );
00238 setPropertyHelpText(cell_count_property_, "The number of cells to draw in the plane of the grid.");
00239 IntPropertyPtr int_prop = cell_count_property_.lock();
00240 int_prop->setMin( 1 );
00241 int_prop->addLegacyName("Cell Count");
00242
00243 height_property_ = property_manager_->createProperty<IntProperty>( "Normal Cell Count", property_prefix_, boost::bind( &ogre_tools::Grid::getHeight, grid_),
00244 boost::bind( &GridDisplay::setHeight, this, _1 ), parent_category_, this );
00245 setPropertyHelpText(height_property_, "The number of cells to draw along the normal vector of the grid. Setting to anything but 0 makes the grid 3D.");
00246 int_prop = height_property_.lock();
00247 int_prop->setMin( 0 );
00248
00249 cell_size_property_ = property_manager_->createProperty<FloatProperty>( "Cell Size", property_prefix_, boost::bind( &ogre_tools::Grid::getCellLength, grid_ ),
00250 boost::bind( &GridDisplay::setCellSize, this, _1 ), parent_category_, this );
00251 setPropertyHelpText(cell_size_property_, "The length, in meters, of the side of each cell.");
00252 FloatPropertyPtr float_prop = cell_size_property_.lock();
00253 float_prop->setMin( 0.0001 );
00254
00255 style_property_ = property_manager_->createProperty<EnumProperty>( "Line Style", property_prefix_, boost::bind( &ogre_tools::Grid::getStyle, grid_ ),
00256 boost::bind( &GridDisplay::setStyle, this, _1 ), parent_category_, this );
00257 setPropertyHelpText(style_property_, "The rendering operation to use to draw the grid lines.");
00258 EnumPropertyPtr enum_prop = style_property_.lock();
00259 enum_prop->addOption("Lines", ogre_tools::Grid::Lines);
00260 enum_prop->addOption("Billboards", ogre_tools::Grid::Billboards);
00261
00262 line_width_property_ = property_manager_->createProperty<FloatProperty>( "Line Width", property_prefix_, boost::bind( &ogre_tools::Grid::getLineWidth, grid_ ),
00263 boost::bind( &GridDisplay::setLineWidth, this, _1 ), parent_category_, this );
00264 setPropertyHelpText(line_width_property_, "The width, in meters, of each grid line.");
00265 float_prop = line_width_property_.lock();
00266 float_prop->setMin( 0.001 );
00267 float_prop->hide();
00268
00269 color_property_ = property_manager_->createProperty<ColorProperty>( "Color", property_prefix_, boost::bind( &GridDisplay::getColor, this ),
00270 boost::bind( &GridDisplay::setColor, this, _1 ), parent_category_, this );
00271 setPropertyHelpText(color_property_, "The color of the grid lines.");
00272 alpha_property_ = property_manager_->createProperty<FloatProperty>( "Alpha", property_prefix_, boost::bind( &GridDisplay::getAlpha, this ),
00273 boost::bind( &GridDisplay::setAlpha, this, _1 ), parent_category_, this );
00274 setPropertyHelpText(alpha_property_, "The amount of transparency to apply to the grid lines.");
00275 float_prop = alpha_property_.lock();
00276 float_prop->setMin( 0.0 );
00277 float_prop->setMax( 1.0f );
00278
00279 plane_property_ = property_manager_->createProperty<EnumProperty>( "Plane", property_prefix_, boost::bind( &GridDisplay::getPlane, this ),
00280 boost::bind( &GridDisplay::setPlane, this, _1 ), parent_category_, this );
00281 setPropertyHelpText(plane_property_, "The plane to draw the grid along.");
00282 enum_prop = plane_property_.lock();
00283 enum_prop->addOption("XY", XY);
00284 enum_prop->addOption("XZ", XZ);
00285 enum_prop->addOption("YZ", YZ);
00286
00287 offset_property_ = property_manager_->createProperty<Vector3Property>( "Offset", property_prefix_, boost::bind( &GridDisplay::getOffset, this ),
00288 boost::bind( &GridDisplay::setOffset, this, _1 ), parent_category_, this );
00289 setPropertyHelpText(offset_property_, "Allows you to offset the grid from the origin of the reference frame. In meters.");
00290 }
00291
00292 }