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