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 
38 
39 #include "ros/ros.h"
40 #include <ros/package.h>
41 
42 #include <OgreRoot.h>
43 #include <OgreRenderWindow.h>
44 #include <OgreSceneManager.h>
45 #include <OgreViewport.h>
46 #include <OgreRectangle2D.h>
47 #include <OgreMaterial.h>
48 #include <OgreMaterialManager.h>
49 #include <OgreTextureUnitState.h>
50 #include <OgreSharedPtr.h>
51 #include <OgreTechnique.h>
52 #include <OgreSceneNode.h>
53 
54 #include "image_view.h"
55 
56 using namespace rviz;
57 
58 ImageView::ImageView(QWidget* parent) : QtOgreRenderWindow(parent), texture_it_(nh_)
59 {
60  setAutoRender(false);
61 #if (OGRE_VERSION < OGRE_VERSION_CHECK(13, 0, 0))
62  scene_manager_ = ogre_root_->createSceneManager(Ogre::ST_GENERIC, "TestSceneManager");
63 #else
64  scene_manager_ = ogre_root_->createSceneManager(Ogre::DefaultSceneManagerFactory::FACTORY_TYPE_NAME,
65  "TestSceneManager");
66 #endif
67 }
68 
70 {
71  delete texture_;
72 }
73 
74 void ImageView::showEvent(QShowEvent* event)
75 {
76  QtOgreRenderWindow::showEvent(event);
77 
78  V_string paths;
79  paths.push_back(ros::package::getPath(ROS_PACKAGE_NAME) + "/ogre_media/textures");
80  initializeResources(paths);
81 
82  setCamera(scene_manager_->createCamera("Camera"));
83 
84  std::string resolved_image = nh_.resolveName("image");
85  if (resolved_image == "/image")
86  {
87  ROS_WARN("image topic has not been remapped");
88  }
89 
90  std::stringstream title;
91  title << "rviz Image Viewer [" << resolved_image << "]";
92  setWindowTitle(QString::fromStdString(title.str()));
93 
94  texture_ = new ROSImageTexture();
95 
96  try
97  {
98  texture_->clear();
99 
101  texture_sub_->subscribe(texture_it_, "image", 1, image_transport::TransportHints("raw"));
102  texture_sub_->registerCallback(
103  boost::bind(&ImageView::textureCallback, this, boost::placeholders::_1));
104  }
105  catch (ros::Exception& e)
106  {
107  ROS_ERROR("%s", (std::string("Error subscribing: ") + e.what()).c_str());
108  }
109 
110  Ogre::MaterialPtr material = Ogre::MaterialManager::getSingleton().create(
111  "Material", Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME);
112  material->setCullingMode(Ogre::CULL_NONE);
113  material->getTechnique(0)->getPass(0)->setDepthWriteEnabled(true);
114  material->getTechnique(0)->setLightingEnabled(false);
115  Ogre::TextureUnitState* tu = material->getTechnique(0)->getPass(0)->createTextureUnitState();
116  tu->setTextureName(texture_->getTexture()->getName());
117  tu->setTextureFiltering(Ogre::TFO_NONE);
118  tu->setTextureAddressingMode(Ogre::TextureUnitState::TAM_CLAMP);
119 
120  Ogre::Rectangle2D* rect = new Ogre::Rectangle2D(true);
121  rect->setCorners(-1.0f, 1.0f, 1.0f, -1.0f);
122  setMaterial(*rect, material);
123  rect->setRenderQueueGroup(Ogre::RENDER_QUEUE_OVERLAY - 1);
124  Ogre::AxisAlignedBox aabb;
125  aabb.setInfinite();
126  rect->setBoundingBox(aabb);
127 
128  Ogre::SceneNode* node = scene_manager_->getRootSceneNode()->createChildSceneNode();
129  node->attachObject(rect);
130  node->setVisible(true);
131 
132  QTimer* timer = new QTimer(this);
133  connect(timer, &QTimer::timeout, this, &ImageView::onTimer);
134  timer->start(33);
135 }
136 
138 {
139  ros::spinOnce();
140 
141  static bool first = true;
142  try
143  {
144  if (texture_->update())
145  {
146  if (first)
147  {
148  first = false;
149 
150  resize(texture_->getWidth(), texture_->getHeight());
151  }
152  }
153 
154  ogre_root_->renderOneFrame();
155  }
156  catch (UnsupportedImageEncoding& e)
157  {
158  ROS_ERROR("%s", e.what());
159  }
160 
161  if (!nh_.ok())
162  {
163  close();
164  }
165 }
166 
167 void ImageView::textureCallback(const sensor_msgs::Image::ConstPtr& msg)
168 {
169  if (texture_)
170  {
171  texture_->addMessage(msg);
172  }
173 }
image_transport::SubscriberFilter
ros::package::getPath
ROSLIB_DECL std::string getPath(const std::string &package_name)
ImageView::onTimer
void onTimer()
Definition: image_view.cpp:137
compatibility.h
initialization.h
rviz::ROSImageTexture
Definition: ros_image_texture.h:58
ros.h
ros::spinOnce
ROSCPP_DECL void spinOnce()
rviz::QtOgreRenderWindow
Definition: qt_ogre_render_window.h:54
rviz::ROSImageTexture::clear
void clear()
Definition: ros_image_texture.cpp:67
ros::Exception
ImageView::texture_it_
image_transport::ImageTransport texture_it_
Definition: image_view.h:78
rviz::ROSImageTexture::getWidth
uint32_t getWidth()
Definition: ros_image_texture.h:74
rviz::setMaterial
void setMaterial(Ogre::SimpleRenderable &renderable, const std::string &material_name)
Definition: compatibility.h:69
rviz::ROSImageTexture::getHeight
uint32_t getHeight()
Definition: ros_image_texture.h:78
f
f
ros::NodeHandle::resolveName
std::string resolveName(const std::string &name, bool remap=true) const
ImageView::texture_
ROSImageTexture * texture_
Definition: image_view.h:74
ImageView::scene_manager_
Ogre::SceneManager * scene_manager_
Definition: image_view.h:72
ImageView::showEvent
void showEvent(QShowEvent *event) override
Definition: image_view.cpp:74
rviz
Definition: add_display_dialog.cpp:54
ROS_WARN
#define ROS_WARN(...)
rviz::ROSImageTexture::addMessage
void addMessage(const sensor_msgs::Image::ConstPtr &image)
Definition: ros_image_texture.cpp:285
package.h
ros::NodeHandle::ok
bool ok() const
ImageView::textureCallback
void textureCallback(const sensor_msgs::Image::ConstPtr &msg)
Definition: image_view.cpp:167
rviz::ROSImageTexture::update
bool update()
Definition: ros_image_texture.cpp:179
ROS_ERROR
#define ROS_ERROR(...)
ImageView::nh_
ros::NodeHandle nh_
Definition: image_view.h:76
rviz::QtOgreRenderWindow::setCamera
void setCamera(Ogre::Camera *camera)
Definition: qt_ogre_render_window.cpp:219
ImageView::texture_sub_
boost::shared_ptr< image_transport::SubscriberFilter > texture_sub_
Definition: image_view.h:79
rviz::QtOgreRenderWindow::setAutoRender
void setAutoRender(bool auto_render)
Definition: qt_ogre_render_window.h:117
rviz::QtOgreRenderWindow::ogre_root_
Ogre::Root * ogre_root_
Definition: qt_ogre_render_window.h:150
V_string
std::vector< std::string > V_string
image_view.h
rviz::UnsupportedImageEncoding
Definition: ros_image_texture.h:49
image_transport::TransportHints
rviz::ROSImageTexture::getTexture
const Ogre::TexturePtr & getTexture()
Definition: ros_image_texture.h:68
ImageView::~ImageView
~ImageView() override
Definition: image_view.cpp:69
rviz::initializeResources
void initializeResources(const V_string &resource_paths)
Definition: initialization.cpp:20
qt_ogre_render_window.h
ImageView::ImageView
ImageView(QWidget *parent=nullptr)
Definition: image_view.cpp:58
ros_image_texture.h


rviz
Author(s): Dave Hershberger, David Gossow, Josh Faust, William Woodall
autogenerated on Sat Jun 1 2024 02:31:53