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


rviz
Author(s): Dave Hershberger, David Gossow, Josh Faust
autogenerated on Thu Jun 6 2019 18:02:15