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.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/render_panel.h"
48 #include "rviz/validate_floats.h"
49 
51 
52 #include "image_display.h"
53 
54 namespace rviz
55 {
57 {
59  "Normalize Range", true,
60  "If set to true, will try to estimate the range of possible values from the received images.",
61  this, SLOT(updateNormalizeOptions()));
62 
63  min_property_ = new FloatProperty("Min Value", 0.0, "Value which will be displayed as black.", this,
64  SLOT(updateNormalizeOptions()));
65 
66  max_property_ = new FloatProperty("Max Value", 1.0, "Value which will be displayed as white.", this,
67  SLOT(updateNormalizeOptions()));
68 
70  new IntProperty("Median window", 5, "Window size for median filter used for computin min/max.",
71  this, SLOT(updateNormalizeOptions()));
72 
73  got_float_image_ = false;
74 }
75 
77 {
79  {
80  static uint32_t count = 0;
81  std::stringstream ss;
82  ss << "ImageDisplay" << count++;
83  img_scene_manager_ = Ogre::Root::getSingleton().createSceneManager(Ogre::ST_GENERIC, ss.str());
84  }
85 
86  img_scene_node_ = img_scene_manager_->getRootSceneNode()->createChildSceneNode();
87 
88  {
89  static int count = 0;
90  std::stringstream ss;
91  ss << "ImageDisplayObject" << count++;
92 
93  screen_rect_ = new Ogre::Rectangle2D(true);
94  screen_rect_->setRenderQueueGroup(Ogre::RENDER_QUEUE_OVERLAY - 1);
95  screen_rect_->setCorners(-1.0f, 1.0f, 1.0f, -1.0f);
96 
97  ss << "Material";
98  material_ = Ogre::MaterialManager::getSingleton().create(
99  ss.str(), Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME);
100  material_->setSceneBlending(Ogre::SBT_REPLACE);
101  material_->setDepthWriteEnabled(false);
102  material_->setReceiveShadows(false);
103  material_->setDepthCheckEnabled(false);
104 
105  material_->getTechnique(0)->setLightingEnabled(false);
106  Ogre::TextureUnitState* tu = material_->getTechnique(0)->getPass(0)->createTextureUnitState();
107  tu->setTextureName(texture_.getTexture()->getName());
108  tu->setTextureFiltering(Ogre::TFO_NONE);
109  tu->setTextureAddressingMode(Ogre::TextureUnitState::TAM_CLAMP);
110 
111  material_->setCullingMode(Ogre::CULL_NONE);
112  Ogre::AxisAlignedBox aabInf;
113  aabInf.setInfinite();
114  screen_rect_->setBoundingBox(aabInf);
115  screen_rect_->setMaterial(material_->getName());
116  img_scene_node_->attachObject(screen_rect_);
117  }
118 
119  render_panel_ = new RenderPanel();
120  render_panel_->getRenderWindow()->setAutoUpdated(false);
121  render_panel_->getRenderWindow()->setActive(false);
122 
123  render_panel_->resize(640, 480);
125 
127 
130  render_panel_->getCamera()->setNearClipDistance(0.01f);
131 
133 }
134 
136 {
137  if (initialized())
138  {
139  delete render_panel_;
140  delete screen_rect_;
141  img_scene_node_->getParentSceneNode()->removeAndDestroyChild(img_scene_node_->getName());
142  }
143 }
144 
146 {
148  render_panel_->getRenderWindow()->setActive(true);
149 }
150 
152 {
153  render_panel_->getRenderWindow()->setActive(false);
155  reset();
156 }
157 
159 {
160  if (got_float_image_)
161  {
162  bool normalize = normalize_property_->getBool();
163 
165  min_property_->setHidden(normalize);
166  max_property_->setHidden(normalize);
168 
171  }
172  else
173  {
175  min_property_->setHidden(true);
176  max_property_->setHidden(true);
178  }
179 }
180 
181 // TODO: In Noetic remove and integrate into reset()
183 {
184  texture_.clear();
185 
186  if (render_panel_->getCamera())
187  render_panel_->getCamera()->setPosition(Ogre::Vector3(999999, 999999, 999999));
188 }
189 
190 void ImageDisplay::update(float wall_dt, float ros_dt)
191 {
192  Q_UNUSED(wall_dt)
193  Q_UNUSED(ros_dt)
194  try
195  {
196  texture_.update();
197 
198  // make sure the aspect ratio of the image is preserved
199  float win_width = render_panel_->width();
200  float win_height = render_panel_->height();
201 
202  float img_width = texture_.getWidth();
203  float img_height = texture_.getHeight();
204 
205  if (img_width != 0 && img_height != 0 && win_width != 0 && win_height != 0)
206  {
207  float img_aspect = img_width / img_height;
208  float win_aspect = win_width / win_height;
209 
210  if (img_aspect > win_aspect)
211  {
212  screen_rect_->setCorners(-1.0f, 1.0f * win_aspect / img_aspect, 1.0f,
213  -1.0f * win_aspect / img_aspect, false);
214  }
215  else
216  {
217  screen_rect_->setCorners(-1.0f * img_aspect / win_aspect, 1.0f, 1.0f * img_aspect / win_aspect,
218  -1.0f, false);
219  }
220  }
221 
222  render_panel_->getRenderWindow()->update();
223  }
224  catch (UnsupportedImageEncoding& e)
225  {
226  setStatus(StatusProperty::Error, "Image", e.what());
227  }
228 }
229 
231 {
232  clear();
234 }
235 
236 /* This is called by incomingMessage(). */
237 void ImageDisplay::processMessage(const sensor_msgs::Image::ConstPtr& msg)
238 {
239  bool got_float_image = msg->encoding == sensor_msgs::image_encodings::TYPE_32FC1 ||
240  msg->encoding == sensor_msgs::image_encodings::TYPE_16UC1 ||
241  msg->encoding == sensor_msgs::image_encodings::TYPE_16SC1 ||
242  msg->encoding == sensor_msgs::image_encodings::MONO16;
243 
244  if (got_float_image != got_float_image_)
245  {
246  got_float_image_ = got_float_image;
248  }
249  texture_.addMessage(msg);
250 }
251 
252 } // namespace rviz
253 
const Ogre::TexturePtr & getTexture()
void processMessage(const sensor_msgs::Image::ConstPtr &msg) override
Implement this to process the contents of a message.
RenderPanel * render_panel_
Definition: image_display.h:89
void initialize(Ogre::SceneManager *scene_manager, DisplayContext *manager)
f
void reset() override
Called to tell the display to clear its state.
DisplayContext * context_
This DisplayContext pointer is the main connection a Display has into the rest of rviz...
Definition: display.h:287
BoolProperty * normalize_property_
Definition: image_display.h:99
FloatProperty * max_property_
void setAutoRender(bool auto_render)
Ogre::Camera * getCamera() const
Ogre::Rectangle2D * screen_rect_
Definition: image_display.h:96
Ogre::SceneManager * img_scene_manager_
Definition: image_display.h:85
Property specialized to enforce floating point max/min.
void reset() override
Reset display.
Property specialized to provide max/min enforcement for integers.
Definition: int_property.h:37
void setNormalizeFloatImage(bool normalize, double min=0.0, double max=1.0)
void setMedianFrames(unsigned median_frames)
Ogre::RenderWindow * getRenderWindow()
Definition: render_widget.h:49
~ImageDisplay() override
virtual void subscribe()
ROS topic management.
Display subclass for subscribing and displaying to image messages.
void addMessage(const sensor_msgs::Image::ConstPtr &image)
const std::string TYPE_32FC1
void setAssociatedWidget(QWidget *widget)
Associate the given widget with this Display.
Definition: display.cpp:361
IntProperty * median_buffer_size_property_
void onEnable() override
Derived classes override this to do the actual work of enabling themselves.
const std::string TYPE_16UC1
const std::string MONO16
void onDisable() override
Derived classes override this to do the actual work of disabling themselves.
FloatProperty * min_property_
void onInitialize() override
Override this function to do subclass-specific initialization.
virtual int getInt() const
Return the internal property value as an integer.
Definition: int_property.h:74
BoolProperty(const QString &name=QString(), bool default_value=false, const QString &description=QString(), Property *parent=nullptr, const char *changed_slot=nullptr, QObject *receiver=nullptr)
virtual void setHidden(bool hidden)
Hide or show this property in any PropertyTreeWidget viewing its parent.
Definition: property.cpp:552
void onInitialize() override
Override this function to do subclass-specific initialization.
Ogre::SceneNode * img_scene_node_
Definition: image_display.h:95
virtual float getFloat() const
ROSImageTexture texture_
Definition: image_display.h:87
void update(float wall_dt, float ros_dt) override
Called periodically by the visualization manager.
Ogre::MaterialPtr material_
Definition: image_display.h:97
bool initialized() const
Returns true if the display has been initialized.
Definition: display.h:275
virtual bool getBool() const
#define PLUGINLIB_EXPORT_CLASS(class_type, base_class_type)
virtual void setStatus(StatusProperty::Level level, const QString &name, const QString &text)
Show status level and text. This is thread-safe.
Definition: display.cpp:175
void setOverlaysEnabled(bool overlays_enabled)
virtual void updateNormalizeOptions()
const std::string TYPE_16SC1


rviz
Author(s): Dave Hershberger, David Gossow, Josh Faust
autogenerated on Sat May 27 2023 02:06:24