image_view.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2008, Willow Garage, Inc.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * * Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * * Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * * Neither the name of the Willow Garage, Inc. nor the names of its
14  * contributors may be used to endorse or promote products derived from
15  * this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27  * POSSIBILITY OF SUCH DAMAGE.
28  */
29 
30 
31 #include <QtGlobal>
32 #include <QTimer>
33 
37 
38 #include "ros/ros.h"
39 #include <ros/package.h>
40 
41 #include <OgreRoot.h>
42 #include <OgreRenderWindow.h>
43 #include <OgreSceneManager.h>
44 #include <OgreViewport.h>
45 #include <OgreRectangle2D.h>
46 #include <OgreMaterial.h>
47 #include <OgreMaterialManager.h>
48 #include <OgreTextureUnitState.h>
49 #include <OgreSharedPtr.h>
50 #include <OgreTechnique.h>
51 #include <OgreSceneNode.h>
52 
53 #include "image_view.h"
54 
55 using namespace rviz;
56 
57 ImageView::ImageView( QWidget* parent )
58  : QtOgreRenderWindow( parent )
59  , texture_it_(nh_)
60 {
61  setAutoRender(false);
62  scene_manager_ = ogre_root_->createSceneManager( Ogre::ST_GENERIC, "TestSceneManager" );
63 }
64 
66 {
67  delete texture_;
68 }
69 
70 void ImageView::showEvent( QShowEvent* event )
71 {
72  QtOgreRenderWindow::showEvent( event );
73 
74  V_string paths;
75  paths.push_back(ros::package::getPath(ROS_PACKAGE_NAME) + "/ogre_media/textures");
76  initializeResources(paths);
77 
78  setCamera( scene_manager_->createCamera( "Camera" ));
79 
80  std::string resolved_image = nh_.resolveName("image");
81  if( resolved_image == "/image" )
82  {
83  ROS_WARN("image topic has not been remapped");
84  }
85 
86  std::stringstream title;
87  title << "rviz Image Viewer [" << resolved_image << "]";
88  setWindowTitle( QString::fromStdString( title.str() ));
89 
90  texture_ = new ROSImageTexture();
91 
92  try
93  {
94  texture_->clear();
95 
97  texture_sub_->subscribe(texture_it_, "image", 1, image_transport::TransportHints("raw"));
98  texture_sub_->registerCallback(boost::bind(&ImageView::textureCallback, this, _1));
99  }
100  catch (ros::Exception& e)
101  {
102  ROS_ERROR("%s", (std::string("Error subscribing: ") + e.what()).c_str());
103  }
104 
105  Ogre::MaterialPtr material =
106  Ogre::MaterialManager::getSingleton().create( "Material",
107  Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME );
108  material->setCullingMode(Ogre::CULL_NONE);
109  material->getTechnique(0)->getPass(0)->setDepthWriteEnabled(true);
110  material->getTechnique(0)->setLightingEnabled(false);
111  Ogre::TextureUnitState* tu = material->getTechnique(0)->getPass(0)->createTextureUnitState();
112  tu->setTextureName(texture_->getTexture()->getName());
113  tu->setTextureFiltering( Ogre::TFO_NONE );
114 
115  Ogre::Rectangle2D* rect = new Ogre::Rectangle2D(true);
116  rect->setCorners(-1.0f, 1.0f, 1.0f, -1.0f);
117  rect->setMaterial(material->getName());
118  rect->setRenderQueueGroup(Ogre::RENDER_QUEUE_OVERLAY - 1);
119  Ogre::AxisAlignedBox aabb;
120  aabb.setInfinite();
121  rect->setBoundingBox(aabb);
122 
123  Ogre::SceneNode* node = scene_manager_->getRootSceneNode()->createChildSceneNode();
124  node->attachObject(rect);
125  node->setVisible(true);
126 
127  QTimer* timer = new QTimer( this );
128  connect( timer, SIGNAL( timeout() ), this, SLOT( onTimer() ));
129  timer->start( 33 );
130 }
131 
133 {
134  ros::spinOnce();
135 
136  static bool first = true;
137  try
138  {
139  if( texture_->update() )
140  {
141  if( first )
142  {
143  first = false;
144 
145  resize( texture_->getWidth(), texture_->getHeight() );
146  }
147  }
148 
149  ogre_root_->renderOneFrame();
150  }
151  catch( UnsupportedImageEncoding& e )
152  {
153  ROS_ERROR("%s", e.what());
154  }
155 
156  if( !nh_.ok() )
157  {
158  close();
159  }
160 }
161 
162 void ImageView::textureCallback(const sensor_msgs::Image::ConstPtr& msg)
163 {
164  if (texture_) {
165  texture_->addMessage(msg);
166  }
167 }
void onTimer()
Definition: image_view.cpp:132
const Ogre::TexturePtr & getTexture()
ros::NodeHandle nh_
Definition: image_view.h:76
f
void initializeResources(const V_string &resource_paths)
std::string resolveName(const std::string &name, bool remap=true) const
void setAutoRender(bool auto_render)
void textureCallback(const sensor_msgs::Image::ConstPtr &msg)
Definition: image_view.cpp:162
#define ROS_WARN(...)
std::vector< std::string > V_string
boost::shared_ptr< image_transport::SubscriberFilter > texture_sub_
Definition: image_view.h:79
void addMessage(const sensor_msgs::Image::ConstPtr &image)
void showEvent(QShowEvent *event)
Definition: image_view.cpp:70
ROSLIB_DECL std::string getPath(const std::string &package_name)
ROSImageTexture * texture_
Definition: image_view.h:74
bool ok() const
ImageView(QWidget *parent=0)
Definition: image_view.cpp:57
ROSCPP_DECL void spinOnce()
void setCamera(Ogre::Camera *camera)
#define ROS_ERROR(...)
Ogre::SceneManager * scene_manager_
Definition: image_view.h:72
image_transport::ImageTransport texture_it_
Definition: image_view.h:78


rviz
Author(s): Dave Hershberger, David Gossow, Josh Faust
autogenerated on Wed Aug 28 2019 04:01:51