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) : QtOgreRenderWindow(parent), texture_it_(nh_)
58 {
59  setAutoRender(false);
60  scene_manager_ = ogre_root_->createSceneManager(Ogre::ST_GENERIC, "TestSceneManager");
61 }
62 
64 {
65  delete texture_;
66 }
67 
68 void ImageView::showEvent(QShowEvent* event)
69 {
70  QtOgreRenderWindow::showEvent(event);
71 
72  V_string paths;
73  paths.push_back(ros::package::getPath(ROS_PACKAGE_NAME) + "/ogre_media/textures");
74  initializeResources(paths);
75 
76  setCamera(scene_manager_->createCamera("Camera"));
77 
78  std::string resolved_image = nh_.resolveName("image");
79  if (resolved_image == "/image")
80  {
81  ROS_WARN("image topic has not been remapped");
82  }
83 
84  std::stringstream title;
85  title << "rviz Image Viewer [" << resolved_image << "]";
86  setWindowTitle(QString::fromStdString(title.str()));
87 
88  texture_ = new ROSImageTexture();
89 
90  try
91  {
92  texture_->clear();
93 
95  texture_sub_->subscribe(texture_it_, "image", 1, image_transport::TransportHints("raw"));
96  texture_sub_->registerCallback(boost::bind(&ImageView::textureCallback, this, _1));
97  }
98  catch (ros::Exception& e)
99  {
100  ROS_ERROR("%s", (std::string("Error subscribing: ") + e.what()).c_str());
101  }
102 
103  Ogre::MaterialPtr material = Ogre::MaterialManager::getSingleton().create(
104  "Material", Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME);
105  material->setCullingMode(Ogre::CULL_NONE);
106  material->getTechnique(0)->getPass(0)->setDepthWriteEnabled(true);
107  material->getTechnique(0)->setLightingEnabled(false);
108  Ogre::TextureUnitState* tu = material->getTechnique(0)->getPass(0)->createTextureUnitState();
109  tu->setTextureName(texture_->getTexture()->getName());
110  tu->setTextureFiltering(Ogre::TFO_NONE);
111  tu->setTextureAddressingMode(Ogre::TextureUnitState::TAM_CLAMP);
112 
113  Ogre::Rectangle2D* rect = new Ogre::Rectangle2D(true);
114  rect->setCorners(-1.0f, 1.0f, 1.0f, -1.0f);
115  rect->setMaterial(material->getName());
116  rect->setRenderQueueGroup(Ogre::RENDER_QUEUE_OVERLAY - 1);
117  Ogre::AxisAlignedBox aabb;
118  aabb.setInfinite();
119  rect->setBoundingBox(aabb);
120 
121  Ogre::SceneNode* node = scene_manager_->getRootSceneNode()->createChildSceneNode();
122  node->attachObject(rect);
123  node->setVisible(true);
124 
125  QTimer* timer = new QTimer(this);
126  connect(timer, SIGNAL(timeout()), this, SLOT(onTimer()));
127  timer->start(33);
128 }
129 
131 {
132  ros::spinOnce();
133 
134  static bool first = true;
135  try
136  {
137  if (texture_->update())
138  {
139  if (first)
140  {
141  first = false;
142 
143  resize(texture_->getWidth(), texture_->getHeight());
144  }
145  }
146 
147  ogre_root_->renderOneFrame();
148  }
149  catch (UnsupportedImageEncoding& e)
150  {
151  ROS_ERROR("%s", e.what());
152  }
153 
154  if (!nh_.ok())
155  {
156  close();
157  }
158 }
159 
160 void ImageView::textureCallback(const sensor_msgs::Image::ConstPtr& msg)
161 {
162  if (texture_)
163  {
164  texture_->addMessage(msg);
165  }
166 }
void onTimer()
Definition: image_view.cpp:130
const Ogre::TexturePtr & getTexture()
ros::NodeHandle nh_
Definition: image_view.h:76
f
void initializeResources(const V_string &resource_paths)
void setAutoRender(bool auto_render)
void textureCallback(const sensor_msgs::Image::ConstPtr &msg)
Definition: image_view.cpp:160
#define ROS_WARN(...)
void showEvent(QShowEvent *event) override
Definition: image_view.cpp:68
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)
std::string resolveName(const std::string &name, bool remap=true) const
~ImageView() override
Definition: image_view.cpp:63
ROSLIB_DECL std::string getPath(const std::string &package_name)
ImageView(QWidget *parent=nullptr)
Definition: image_view.cpp:57
ROSImageTexture * texture_
Definition: image_view.h:74
ROSCPP_DECL void spinOnce()
bool ok() const
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 Mon Aug 15 2022 02:05:16