image_display.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2012, 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 #include <boost/bind/bind.hpp>
31 
32 #include <OgreManualObject.h>
33 #include <OgreMaterialManager.h>
34 #include <OgreRectangle2D.h>
35 #include <OgreRenderSystem.h>
36 #include <OgreRenderWindow.h>
37 #include <OgreRoot.h>
38 #include <OgreSceneManager.h>
39 #include <OgreSceneNode.h>
40 #include <OgreTextureManager.h>
41 #include <OgreViewport.h>
42 #include <OgreTechnique.h>
43 #include <OgreCamera.h>
44 
45 #include <rviz/display_context.h>
46 #include <rviz/frame_manager.h>
47 #include <rviz/panel_dock_widget.h>
49 #include <rviz/render_panel.h>
50 #include <rviz/validate_floats.h>
51 
53 
54 #include "image_display.h"
55 
56 namespace rviz
57 {
59 {
61  "Normalize Range", true,
62  "If set to true, will try to estimate the range of possible values from the received images.",
64 
65  min_property_ = new FloatProperty("Min Value", 0.0, "Value which will be displayed as black.", this,
67 
68  max_property_ = new FloatProperty("Max Value", 1.0, "Value which will be displayed as white.", this,
70 
72  new IntProperty("Median window", 5, "Window size for median filter used for computin min/max.",
74 
75  got_float_image_ = false;
76 }
77 
79 {
81  {
82  static uint32_t count = 0;
83  std::stringstream ss;
84  ss << "ImageDisplay" << count++;
85 #if (OGRE_VERSION < OGRE_VERSION_CHECK(13, 0, 0))
86  img_scene_manager_ = Ogre::Root::getSingleton().createSceneManager(Ogre::ST_GENERIC, ss.str());
87 #else
88  img_scene_manager_ = Ogre::Root::getSingleton().createSceneManager(
89  Ogre::DefaultSceneManagerFactory::FACTORY_TYPE_NAME, ss.str());
90 #endif
91  }
92 
93  img_scene_node_ = img_scene_manager_->getRootSceneNode()->createChildSceneNode();
94 
95  {
96  static int count = 0;
97  std::stringstream ss;
98  ss << "ImageDisplayObject" << count++;
99 
100  screen_rect_ = new Ogre::Rectangle2D(true);
101  screen_rect_->setRenderQueueGroup(Ogre::RENDER_QUEUE_OVERLAY - 1);
102  screen_rect_->setCorners(-1.0f, 1.0f, 1.0f, -1.0f);
103 
104  ss << "Material";
105  material_ = Ogre::MaterialManager::getSingleton().create(
106  ss.str(), Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME);
107  material_->setSceneBlending(Ogre::SBT_REPLACE);
108  material_->setDepthWriteEnabled(false);
109  material_->setReceiveShadows(false);
110  material_->setDepthCheckEnabled(false);
111 
112  material_->getTechnique(0)->setLightingEnabled(false);
113  Ogre::TextureUnitState* tu = material_->getTechnique(0)->getPass(0)->createTextureUnitState();
114  tu->setTextureName(texture_.getTexture()->getName());
115  tu->setTextureFiltering(Ogre::TFO_NONE);
116  tu->setTextureAddressingMode(Ogre::TextureUnitState::TAM_CLAMP);
117 
118  material_->setCullingMode(Ogre::CULL_NONE);
119  Ogre::AxisAlignedBox aabInf;
120  aabInf.setInfinite();
121  screen_rect_->setBoundingBox(aabInf);
123  img_scene_node_->attachObject(screen_rect_);
124  }
125 
126  render_panel_ = new RenderPanel();
127  render_panel_->getRenderWindow()->setAutoUpdated(false);
128  render_panel_->getRenderWindow()->setActive(false);
129 
130  render_panel_->resize(640, 480);
132 
134  if (auto* dock = getAssociatedWidgetPanel())
135  dock->addMaximizeButton();
136 
139  render_panel_->getCamera()->setNearClipDistance(0.01f);
140 
142 
144 }
145 
147 {
148  if (initialized())
149  {
150  delete render_panel_;
151  delete screen_rect_;
153  }
154 }
155 
157 {
159  mouse_click_->enable();
160 
161  render_panel_->getRenderWindow()->setActive(true);
162 }
163 
165 {
166  render_panel_->getRenderWindow()->setActive(false);
169 
170  reset();
171 }
172 
174 {
175  if (got_float_image_)
176  {
177  bool normalize = normalize_property_->getBool();
178 
180  min_property_->setHidden(normalize);
181  max_property_->setHidden(normalize);
183 
186  }
187  else
188  {
190  min_property_->setHidden(true);
191  max_property_->setHidden(true);
193  }
194 }
195 
196 void ImageDisplay::update(float wall_dt, float ros_dt)
197 {
198  Q_UNUSED(wall_dt)
199  Q_UNUSED(ros_dt)
200  try
201  {
202  texture_.update();
203 
204  // make sure the aspect ratio of the image is preserved
205  float win_width = render_panel_->width();
206  float win_height = render_panel_->height();
207 
208  float img_width = texture_.getWidth();
209  float img_height = texture_.getHeight();
210 
211  if (img_width != 0 && img_height != 0 && win_width != 0 && win_height != 0)
212  {
213  float img_aspect = img_width / img_height;
214  float win_aspect = win_width / win_height;
215 
216  if (img_aspect > win_aspect)
217  {
218  screen_rect_->setCorners(-1.0f, 1.0f * win_aspect / img_aspect, 1.0f,
219  -1.0f * win_aspect / img_aspect, false);
220  }
221  else
222  {
223  screen_rect_->setCorners(-1.0f * img_aspect / win_aspect, 1.0f, 1.0f * img_aspect / win_aspect,
224  -1.0f, false);
225  }
226  }
227 
228  render_panel_->getRenderWindow()->update();
229 
230  mouse_click_->setDimensions(img_width, img_height, win_width, win_height);
231  }
232  catch (UnsupportedImageEncoding& e)
233  {
234  setStatus(StatusProperty::Error, "Image", e.what());
235  }
236 }
237 
239 {
241  texture_.clear();
242  render_panel_->getCamera()->setPosition(Ogre::Vector3(999999, 999999, 999999));
243 }
244 
245 /* This is called by incomingMessage(). */
246 void ImageDisplay::processMessage(const sensor_msgs::Image::ConstPtr& msg)
247 {
248  bool got_float_image = msg->encoding == sensor_msgs::image_encodings::TYPE_32FC1 ||
249  msg->encoding == sensor_msgs::image_encodings::TYPE_16UC1 ||
250  msg->encoding == sensor_msgs::image_encodings::TYPE_16SC1 ||
251  msg->encoding == sensor_msgs::image_encodings::MONO16;
252 
253  if (got_float_image != got_float_image_)
254  {
255  got_float_image_ = got_float_image;
257  }
258  texture_.addMessage(msg);
259 }
260 
262 {
265 }
266 
267 
268 } // namespace rviz
269 
rviz::BoolProperty::getBool
virtual bool getBool() const
Definition: bool_property.cpp:48
panel_dock_widget.h
validate_floats.h
rviz::ImageDisplayBase::onInitialize
void onInitialize() override
Override this function to do subclass-specific initialization.
Definition: image_display_base.cpp:75
frame_manager.h
rviz::Property::setHidden
virtual void setHidden(bool hidden)
Hide or show this property in any PropertyTreeWidget viewing its parent.
Definition: property.cpp:566
image_encodings.h
compatibility.h
rviz::Display::initialized
bool initialized() const
Returns true if the display has been initialized.
Definition: display.h:275
rviz::StatusProperty::Error
@ Error
Definition: status_property.h:46
rviz::ImageDisplayBase::unsubscribe
virtual void unsubscribe()
Definition: image_display_base.cpp:209
rviz::QtOgreRenderWindow::getCamera
Ogre::Camera * getCamera() const
Definition: qt_ogre_render_window.h:97
rviz::ImageDisplay::img_scene_manager_
Ogre::SceneManager * img_scene_manager_
Definition: image_display.h:87
rviz::ImageDisplay::~ImageDisplay
~ImageDisplay() override
Definition: image_display.cpp:146
rviz::ImageDisplay::processMessage
void processMessage(const sensor_msgs::Image::ConstPtr &msg) override
Implement this to process the contents of a message.
Definition: image_display.cpp:246
rviz::MouseClick::setImageTopic
void setImageTopic(const QString &topic)
Definition: mouse_click.cpp:84
rviz::ImageDisplay
Definition: image_display.h:63
image_display.h
rviz::ROSImageTexture::clear
void clear()
Definition: ros_image_texture.cpp:67
rviz::ImageDisplay::updateNormalizeOptions
virtual void updateNormalizeOptions()
Definition: image_display.cpp:173
rviz::ROSImageTexture::setNormalizeFloatImage
void setNormalizeFloatImage(bool normalize, double min=0.0, double max=1.0)
Definition: ros_image_texture.cpp:106
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::ImageDisplay::reset
void reset() override
Called to tell the display to clear its state.
Definition: image_display.cpp:238
rviz::BoolProperty::BoolProperty
BoolProperty(const QString &name=QString(), bool default_value=false, const QString &description=QString(), Property *parent=nullptr)
Definition: bool_property.cpp:36
rviz::MouseClick::disable
void disable()
Definition: mouse_click.cpp:24
rviz::ROSImageTexture::getHeight
uint32_t getHeight()
Definition: ros_image_texture.h:78
f
f
sensor_msgs::image_encodings::TYPE_16UC1
const std::string TYPE_16UC1
rviz::ImageDisplay::median_buffer_size_property_
IntProperty * median_buffer_size_property_
Definition: image_display.h:101
rviz::Display
Definition: display.h:63
rviz::FloatProperty
Property specialized to enforce floating point max/min.
Definition: float_property.h:37
rviz::ROSImageTexture::setMedianFrames
void setMedianFrames(unsigned median_frames)
Definition: ros_image_texture.cpp:87
rviz::Display::setStatus
virtual void setStatus(StatusProperty::Level level, const QString &name, const QString &text)
Show status level and text. This is thread-safe.
Definition: display.cpp:176
rviz::removeAndDestroyChildNode
void removeAndDestroyChildNode(Ogre::SceneNode *parent, Ogre::SceneNode *child)
Definition: compatibility.h:96
rviz::RenderPanel::initialize
void initialize(Ogre::SceneManager *scene_manager, DisplayContext *manager)
Definition: render_panel.cpp:77
rviz::ImageDisplay::min_property_
FloatProperty * min_property_
Definition: image_display.h:99
PLUGINLIB_EXPORT_CLASS
#define PLUGINLIB_EXPORT_CLASS(class_type, base_class_type)
rviz::FloatProperty::getFloat
virtual float getFloat() const
Definition: float_property.h:79
rviz::ImageDisplay::got_float_image_
bool got_float_image_
Definition: image_display.h:102
rviz::ImageDisplay::mouse_click_
MouseClick * mouse_click_
Definition: image_display.h:104
rviz
Definition: add_display_dialog.cpp:54
rviz::RenderWidget::getRenderWindow
Ogre::RenderWindow * getRenderWindow()
Definition: render_widget.h:49
rviz::RosTopicProperty::getTopic
QString getTopic() const
Definition: ros_topic_property.h:81
rviz::MouseClick::enable
void enable()
Definition: mouse_click.cpp:15
rviz::ImageDisplay::texture_
ROSImageTexture texture_
Definition: image_display.h:89
rviz::ImageDisplay::normalize_property_
BoolProperty * normalize_property_
Definition: image_display.h:98
rviz::ImageDisplay::material_
Ogre::MaterialPtr material_
Definition: image_display.h:96
rviz::ImageDisplay::render_panel_
RenderPanel * render_panel_
Definition: image_display.h:91
rviz::ImageDisplayBase::updateTopic
virtual void updateTopic()
Update topic and resubscribe.
Definition: image_display_base.cpp:256
rviz::ROSImageTexture::addMessage
void addMessage(const sensor_msgs::Image::ConstPtr &image)
Definition: ros_image_texture.cpp:285
rviz::ImageDisplayBase
Display subclass for subscribing and displaying to image messages.
Definition: image_display_base.h:63
render_panel.h
rviz::ImageDisplay::updateTopic
void updateTopic() override
Update topic and resubscribe.
Definition: image_display.cpp:261
rviz::Display::getAssociatedWidgetPanel
PanelDockWidget * getAssociatedWidgetPanel()
Return the panel containing the associated widget, or NULL if there is none.
Definition: display.h:215
rviz::Display::context_
DisplayContext * context_
This DisplayContext pointer is the main connection a Display has into the rest of rviz....
Definition: display.h:287
rviz::ROSImageTexture::update
bool update()
Definition: ros_image_texture.cpp:179
sensor_msgs::image_encodings::TYPE_32FC1
const std::string TYPE_32FC1
rviz::ImageDisplayBase::reset
void reset() override
Reset display.
Definition: image_display_base.cpp:130
rviz::ImageDisplay::img_scene_node_
Ogre::SceneNode * img_scene_node_
Definition: image_display.h:94
sensor_msgs::image_encodings::MONO16
const std::string MONO16
class_list_macros.hpp
rviz::MouseClick::setDimensions
void setDimensions(int img_width, int img_height, int win_width, int win_height)
Definition: mouse_click.cpp:76
rviz::ImageDisplay::onEnable
void onEnable() override
Derived classes override this to do the actual work of enabling themselves.
Definition: image_display.cpp:156
rviz::IntProperty::getInt
virtual int getInt() const
Return the internal property value as an integer.
Definition: int_property.h:96
rviz::ImageDisplay::onDisable
void onDisable() override
Derived classes override this to do the actual work of disabling themselves.
Definition: image_display.cpp:164
sensor_msgs::image_encodings::TYPE_16SC1
const std::string TYPE_16SC1
rviz::QtOgreRenderWindow::setAutoRender
void setAutoRender(bool auto_render)
Definition: qt_ogre_render_window.h:117
rviz::Display::setAssociatedWidget
void setAssociatedWidget(QWidget *widget)
Associate the given widget with this Display.
Definition: display.cpp:350
display_context.h
rviz::QtOgreRenderWindow::setOverlaysEnabled
void setOverlaysEnabled(bool overlays_enabled)
Definition: qt_ogre_render_window.cpp:241
rviz::UnsupportedImageEncoding
Definition: ros_image_texture.h:49
rviz::RenderPanel
Definition: render_panel.h:74
rviz::ROSImageTexture::getTexture
const Ogre::TexturePtr & getTexture()
Definition: ros_image_texture.h:68
rviz::ImageDisplay::ImageDisplay
ImageDisplay()
Definition: image_display.cpp:58
rviz::ImageDisplayBase::topic_property_
RosTopicProperty * topic_property_
Definition: image_display_base.h:125
rviz::MouseClick
Class for capturing mouse clicks.
Definition: mouse_click.h:34
rviz::Display::update_nh_
ros::NodeHandle update_nh_
A NodeHandle whose CallbackQueue is run from the main GUI thread (the "update" thread).
Definition: display.h:300
rviz::ImageDisplay::update
void update(float wall_dt, float ros_dt) override
Called periodically by the visualization manager.
Definition: image_display.cpp:196
rviz::ImageDisplay::onInitialize
void onInitialize() override
Override this function to do subclass-specific initialization.
Definition: image_display.cpp:78
rviz::ImageDisplayBase::subscribe
virtual void subscribe()
ROS topic management.
Definition: image_display_base.cpp:152
rviz::IntProperty
Property specialized to provide max/min enforcement for integers.
Definition: int_property.h:37
rviz::ImageDisplay::screen_rect_
Ogre::Rectangle2D * screen_rect_
Definition: image_display.h:95
rviz::ImageDisplay::max_property_
FloatProperty * max_property_
Definition: image_display.h:100


rviz
Author(s): Dave Hershberger, David Gossow, Josh Faust, William Woodall
autogenerated on Fri Aug 2 2024 08:43:09