image_view.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 
00031 #include <QtGlobal>
00032 #include <QTimer>
00033 
00034 #include "rviz/ogre_helpers/qt_ogre_render_window.h"
00035 #include "rviz/ogre_helpers/initialization.h"
00036 #include "rviz/image/ros_image_texture.h"
00037 
00038 #include "ros/ros.h"
00039 #include <ros/package.h>
00040 
00041 #include <OGRE/OgreRoot.h>
00042 #include <OGRE/OgreRenderWindow.h>
00043 #include <OGRE/OgreSceneManager.h>
00044 #include <OGRE/OgreViewport.h>
00045 #include <OGRE/OgreRectangle2D.h>
00046 #include <OGRE/OgreMaterial.h>
00047 #include <OGRE/OgreMaterialManager.h>
00048 #include <OGRE/OgreTextureUnitState.h>
00049 
00050 #include "image_view.h"
00051 
00052 using namespace rviz;
00053 
00054 ImageView::ImageView( QWidget* parent )
00055   : QtOgreRenderWindow( parent )
00056   , texture_it_(nh_)
00057 {
00058   setAutoRender(false);
00059   scene_manager_ = ogre_root_->createSceneManager( Ogre::ST_GENERIC, "TestSceneManager" );
00060 }
00061 
00062 ImageView::~ImageView()
00063 {
00064   delete texture_;
00065 }
00066 
00067 void ImageView::showEvent( QShowEvent* event )
00068 {
00069   QtOgreRenderWindow::showEvent( event );
00070 
00071   V_string paths;
00072   paths.push_back(ros::package::getPath(ROS_PACKAGE_NAME) + "/ogre_media/textures");
00073   initializeResources(paths);
00074 
00075   setCamera( scene_manager_->createCamera( "Camera" ));
00076 
00077   std::string resolved_image = nh_.resolveName("image");
00078   if( resolved_image == "/image" )
00079   {
00080     ROS_WARN("image topic has not been remapped");
00081   }
00082 
00083   std::stringstream title;
00084   title << "rviz Image Viewer [" << resolved_image << "]";
00085   setWindowTitle( QString::fromStdString( title.str() ));
00086 
00087   texture_ = new ROSImageTexture();
00088 
00089   try
00090   {
00091     texture_->clear();
00092 
00093     texture_sub_.reset(new image_transport::SubscriberFilter());
00094     texture_sub_->subscribe(texture_it_, "image", 1, image_transport::TransportHints("raw"));
00095     texture_sub_->registerCallback(boost::bind(&ImageView::textureCallback, this, _1));
00096   }
00097   catch (ros::Exception& e)
00098   {
00099     ROS_ERROR("%s", (std::string("Error subscribing: ") + e.what()).c_str());
00100   }
00101 
00102   Ogre::MaterialPtr material =
00103     Ogre::MaterialManager::getSingleton().create( "Material",
00104                                                   Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME );
00105   material->setCullingMode(Ogre::CULL_NONE);
00106   material->getTechnique(0)->getPass(0)->setDepthWriteEnabled(true);
00107   material->getTechnique(0)->setLightingEnabled(false);
00108   Ogre::TextureUnitState* tu = material->getTechnique(0)->getPass(0)->createTextureUnitState();
00109   tu->setTextureName(texture_->getTexture()->getName());
00110   tu->setTextureFiltering( Ogre::TFO_NONE );
00111 
00112   Ogre::Rectangle2D* rect = new Ogre::Rectangle2D(true);
00113   rect->setCorners(-1.0f, 1.0f, 1.0f, -1.0f);
00114   rect->setMaterial(material->getName());
00115   rect->setRenderQueueGroup(Ogre::RENDER_QUEUE_OVERLAY - 1);
00116   Ogre::AxisAlignedBox aabb;
00117   aabb.setInfinite();
00118   rect->setBoundingBox(aabb);
00119 
00120   Ogre::SceneNode* node = scene_manager_->getRootSceneNode()->createChildSceneNode();
00121   node->attachObject(rect);
00122   node->setVisible(true);
00123 
00124   QTimer* timer = new QTimer( this );
00125   connect( timer, SIGNAL( timeout() ), this, SLOT( onTimer() ));
00126   timer->start( 33 );
00127 }
00128 
00129 void ImageView::onTimer()
00130 {
00131   ros::spinOnce();
00132 
00133   static bool first = true;
00134   try
00135   {
00136     if( texture_->update() )
00137     {
00138       if( first )
00139       {
00140         first = false;
00141 
00142         resize( texture_->getWidth(), texture_->getHeight() );
00143       }
00144     }
00145 
00146     ogre_root_->renderOneFrame();
00147   }
00148   catch( UnsupportedImageEncoding& e )
00149   {
00150     ROS_ERROR("%s", e.what());
00151   }
00152 
00153   if( !nh_.ok() )
00154   {
00155     close();
00156   }
00157 }
00158 
00159 void ImageView::textureCallback(const sensor_msgs::Image::ConstPtr& msg)
00160 {
00161   if (texture_) {
00162     texture_->addMessage(msg);
00163   }
00164 }


rviz
Author(s): Dave Hershberger, David Gossow, Josh Faust
autogenerated on Mon Oct 6 2014 07:26:35