grid_display.cpp
Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2008, Willow Garage, Inc.
00003  * All rights reserved.
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions are met:
00007  *
00008  *     * Redistributions of source code must retain the above copyright
00009  *       notice, this list of conditions and the following disclaimer.
00010  *     * Redistributions in binary form must reproduce the above copyright
00011  *       notice, this list of conditions and the following disclaimer in the
00012  *       documentation and/or other materials provided with the distribution.
00013  *     * Neither the name of the Willow Garage, Inc. nor the names of its
00014  *       contributors may be used to endorse or promote products derived from
00015  *       this software without specific prior written permission.
00016  *
00017  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00018  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00019  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00020  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
00021  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00022  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00023  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00024  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00025  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00026  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00027  * POSSIBILITY OF SUCH DAMAGE.
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 "ogre_tools/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 ogre_tools::Grid( scene_manager_, scene_node_, ogre_tools::Grid::Lines, 10, 1.0f, 0.03f,
00067                                 Ogre::ColourValue(color_.r_, color_.g_, color_.b_, alpha_) );
00068   grid_->getSceneNode()->setVisible( false );
00069   setStyle(ogre_tools::Grid::Lines);
00070   setFrame(FIXED_FRAME_STRING);
00071 }
00072 
00073 void GridDisplay::onEnable()
00074 {
00075   grid_->getSceneNode()->setVisible( true );
00076 }
00077 
00078 void GridDisplay::onDisable()
00079 {
00080   grid_->getSceneNode()->setVisible( false );
00081 }
00082 
00083 void GridDisplay::setCellSize( float size )
00084 {
00085   grid_->setCellLength(size);
00086 
00087   propertyChanged(cell_size_property_);
00088 
00089   causeRender();
00090 }
00091 
00092 void GridDisplay::setCellCount( uint32_t count )
00093 {
00094   grid_->setCellCount(count);
00095 
00096   propertyChanged(cell_count_property_);
00097 
00098   causeRender();
00099 }
00100 
00101 void GridDisplay::setColor( const Color& color )
00102 {
00103   color_ = color;
00104   grid_->setColor(Ogre::ColourValue(color.r_, color.g_, color.b_, alpha_));
00105 
00106   propertyChanged(color_property_);
00107 
00108   causeRender();
00109 }
00110 
00111 void GridDisplay::setLineWidth( float width )
00112 {
00113   grid_->setLineWidth(width);
00114 
00115   propertyChanged(line_width_property_);
00116 
00117   causeRender();
00118 }
00119 
00120 void GridDisplay::setStyle( int style )
00121 {
00122   grid_->setStyle((ogre_tools::Grid::Style)style);
00123 
00124   switch (style)
00125   {
00126   case ogre_tools::Grid::Billboards:
00127     showProperty(line_width_property_);
00128     break;
00129   case ogre_tools::Grid::Lines:
00130     hideProperty(line_width_property_);
00131     break;
00132   }
00133 
00134   propertyChanged(style_property_);
00135 
00136   causeRender();
00137 }
00138 
00139 void GridDisplay::setAlpha( float a )
00140 {
00141   alpha_ = a;
00142 
00143   grid_->setColor(Ogre::ColourValue(color_.r_, color_.g_, color_.b_, alpha_));
00144 
00145   propertyChanged(alpha_property_);
00146 
00147   causeRender();
00148 }
00149 
00150 void GridDisplay::setHeight( uint32_t height )
00151 {
00152   grid_->setHeight( height );
00153 
00154   propertyChanged(height_property_);
00155 
00156   causeRender();
00157 }
00158 
00159 void GridDisplay::setOffset( const Ogre::Vector3& offset )
00160 {
00161   offset_ = offset;
00162 
00163   Ogre::Vector3 ogre_offset = offset;
00164 
00165   grid_->getSceneNode()->setPosition(ogre_offset);
00166 
00167   propertyChanged(offset_property_);
00168 
00169   causeRender();
00170 }
00171 
00172 void GridDisplay::setPlane(int plane)
00173 {
00174   plane_ = (Plane)plane;
00175 
00176   if (plane_ == XY)
00177   {
00178     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)));
00179   }
00180   else if (plane_ == XZ)
00181   {
00182     grid_->getSceneNode()->setOrientation(1.0f, 0.0f, 0.0f, 0.0f);
00183   }
00184   else if (plane_ == YZ)
00185   {
00186     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)));
00187   }
00188 
00189   propertyChanged(plane_property_);
00190 
00191   causeRender();
00192 }
00193 
00194 void GridDisplay::setFrame(const std::string& frame)
00195 {
00196   frame_ = frame;
00197   propertyChanged(frame_property_);
00198 }
00199 
00200 void GridDisplay::update(float dt, float ros_dt)
00201 {
00202   std::string frame = frame_;
00203   if (frame == FIXED_FRAME_STRING)
00204   {
00205     frame = fixed_frame_;
00206   }
00207 
00208   Ogre::Vector3 position;
00209   Ogre::Quaternion orientation;
00210   if (vis_manager_->getFrameManager()->getTransform(frame, ros::Time(), position, orientation))
00211   {
00212     scene_node_->setPosition(position);
00213     scene_node_->setOrientation(orientation);
00214     setStatus(status_levels::Ok, "Transform", "Transform OK");
00215   }
00216   else
00217   {
00218     std::string error;
00219     if (vis_manager_->getFrameManager()->transformHasProblems(frame, ros::Time(), error))
00220     {
00221       setStatus(status_levels::Error, "Transform", error);
00222     }
00223     else
00224     {
00225       std::stringstream ss;
00226       ss << "Could not transform from [" << frame << "] to [" << fixed_frame_ << "]";
00227       setStatus(status_levels::Error, "Transform", ss.str());
00228     }
00229   }
00230 }
00231 
00232 void GridDisplay::createProperties()
00233 {
00234   frame_property_ = property_manager_->createProperty<TFFrameProperty>("Reference Frame", property_prefix_, boost::bind(&GridDisplay::getFrame, this),
00235                                                                        boost::bind(&GridDisplay::setFrame, this, _1), parent_category_, this);
00236   setPropertyHelpText(frame_property_, "The TF frame this grid will use for its origin.");
00237 
00238   cell_count_property_ = property_manager_->createProperty<IntProperty>( "Plane Cell Count", property_prefix_, boost::bind( &ogre_tools::Grid::getCellCount, grid_),
00239                                                            boost::bind( &GridDisplay::setCellCount, this, _1 ), parent_category_, this );
00240   setPropertyHelpText(cell_count_property_, "The number of cells to draw in the plane of the grid.");
00241   IntPropertyPtr int_prop = cell_count_property_.lock();
00242   int_prop->setMin( 1 );
00243   int_prop->addLegacyName("Cell Count");
00244 
00245   height_property_ = property_manager_->createProperty<IntProperty>( "Normal Cell Count", property_prefix_, boost::bind( &ogre_tools::Grid::getHeight, grid_),
00246                                                            boost::bind( &GridDisplay::setHeight, this, _1 ), parent_category_, this );
00247   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.");
00248   int_prop = height_property_.lock();
00249   int_prop->setMin( 0 );
00250 
00251   cell_size_property_ = property_manager_->createProperty<FloatProperty>( "Cell Size", property_prefix_, boost::bind( &ogre_tools::Grid::getCellLength, grid_ ),
00252                                                              boost::bind( &GridDisplay::setCellSize, this, _1 ), parent_category_, this );
00253   setPropertyHelpText(cell_size_property_, "The length, in meters, of the side of each cell.");
00254   FloatPropertyPtr float_prop = cell_size_property_.lock();
00255   float_prop->setMin( 0.0001 );
00256 
00257   style_property_ = property_manager_->createProperty<EnumProperty>( "Line Style", property_prefix_, boost::bind( &ogre_tools::Grid::getStyle, grid_ ),
00258                                                                    boost::bind( &GridDisplay::setStyle, this, _1 ), parent_category_, this );
00259   setPropertyHelpText(style_property_, "The rendering operation to use to draw the grid lines.");
00260   EnumPropertyPtr enum_prop = style_property_.lock();
00261   enum_prop->addOption("Lines", ogre_tools::Grid::Lines);
00262   enum_prop->addOption("Billboards", ogre_tools::Grid::Billboards);
00263 
00264   line_width_property_ = property_manager_->createProperty<FloatProperty>( "Line Width", property_prefix_, boost::bind( &ogre_tools::Grid::getLineWidth, grid_ ),
00265                                                                boost::bind( &GridDisplay::setLineWidth, this, _1 ), parent_category_, this );
00266   setPropertyHelpText(line_width_property_, "The width, in meters, of each grid line.");
00267   float_prop = line_width_property_.lock();
00268   float_prop->setMin( 0.001 );
00269   float_prop->hide();
00270 
00271   color_property_ = property_manager_->createProperty<ColorProperty>( "Color", property_prefix_, boost::bind( &GridDisplay::getColor, this ),
00272                                                                       boost::bind( &GridDisplay::setColor, this, _1 ), parent_category_, this );
00273   setPropertyHelpText(color_property_, "The color of the grid lines.");
00274   alpha_property_ = property_manager_->createProperty<FloatProperty>( "Alpha", property_prefix_, boost::bind( &GridDisplay::getAlpha, this ),
00275                                                                       boost::bind( &GridDisplay::setAlpha, this, _1 ), parent_category_, this );
00276   setPropertyHelpText(alpha_property_, "The amount of transparency to apply to the grid lines.");
00277   float_prop = alpha_property_.lock();
00278   float_prop->setMin( 0.0 );
00279   float_prop->setMax( 1.0f );
00280 
00281   plane_property_ = property_manager_->createProperty<EnumProperty>( "Plane", property_prefix_, boost::bind( &GridDisplay::getPlane, this ),
00282                                                                      boost::bind( &GridDisplay::setPlane, this, _1 ), parent_category_, this );
00283   setPropertyHelpText(plane_property_, "The plane to draw the grid along.");
00284   enum_prop = plane_property_.lock();
00285   enum_prop->addOption("XY", XY);
00286   enum_prop->addOption("XZ", XZ);
00287   enum_prop->addOption("YZ", YZ);
00288 
00289   offset_property_ = property_manager_->createProperty<Vector3Property>( "Offset", property_prefix_, boost::bind( &GridDisplay::getOffset, this ),
00290                                                                          boost::bind( &GridDisplay::setOffset, this, _1 ), parent_category_, this );
00291   setPropertyHelpText(offset_property_, "Allows you to offset the grid from the origin of the reference frame.  In meters.");
00292 }
00293 
00294 } // namespace rviz


rviz_qt
Author(s): Dave Hershberger
autogenerated on Fri Dec 6 2013 20:56:52