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 <tf/transform_listener.h>
46 
47 #include "rviz/display_context.h"
48 #include "rviz/frame_manager.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 {
58 
61  , texture_()
62 {
63  normalize_property_ = new BoolProperty( "Normalize Range", true,
64  "If set to true, will try to estimate the range of possible values from the received images.",
65  this, SLOT( updateNormalizeOptions() ));
66 
67  min_property_ = new FloatProperty( "Min Value", 0.0, "Value which will be displayed as black.", this, SLOT( updateNormalizeOptions() ));
68 
69  max_property_ = new FloatProperty( "Max Value", 1.0, "Value which will be displayed as white.", this, SLOT( updateNormalizeOptions() ));
70 
71  median_buffer_size_property_ = new IntProperty( "Median window", 5, "Window size for median filter used for computin min/max.",
72  this, SLOT( updateNormalizeOptions() ) );
73 
74  got_float_image_ = false;
75 }
76 
78 {
80  {
81  static uint32_t count = 0;
82  std::stringstream ss;
83  ss << "ImageDisplay" << count++;
84  img_scene_manager_ = Ogre::Root::getSingleton().createSceneManager(Ogre::ST_GENERIC, ss.str());
85  }
86 
87  img_scene_node_ = img_scene_manager_->getRootSceneNode()->createChildSceneNode();
88 
89  {
90  static int count = 0;
91  std::stringstream ss;
92  ss << "ImageDisplayObject" << count++;
93 
94  screen_rect_ = new Ogre::Rectangle2D(true);
95  screen_rect_->setRenderQueueGroup(Ogre::RENDER_QUEUE_OVERLAY - 1);
96  screen_rect_->setCorners(-1.0f, 1.0f, 1.0f, -1.0f);
97 
98  ss << "Material";
99  material_ = Ogre::MaterialManager::getSingleton().create( 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 
110  material_->setCullingMode(Ogre::CULL_NONE);
111  Ogre::AxisAlignedBox aabInf;
112  aabInf.setInfinite();
113  screen_rect_->setBoundingBox(aabInf);
114  screen_rect_->setMaterial(material_->getName());
115  img_scene_node_->attachObject(screen_rect_);
116  }
117 
118  render_panel_ = new RenderPanel();
119  render_panel_->getRenderWindow()->setAutoUpdated(false);
120  render_panel_->getRenderWindow()->setActive( false );
121 
122  render_panel_->resize( 640, 480 );
124 
126 
129  render_panel_->getCamera()->setNearClipDistance( 0.01f );
130 
132 }
133 
135 {
136  if ( initialized() )
137  {
138  delete render_panel_;
139  delete screen_rect_;
140  img_scene_node_->getParentSceneNode()->removeAndDestroyChild( img_scene_node_->getName() );
141  }
142 }
143 
145 {
147  render_panel_->getRenderWindow()->setActive(true);
148 }
149 
151 {
152  render_panel_->getRenderWindow()->setActive(false);
154  clear();
155 }
156 
158 {
159  if (got_float_image_)
160  {
162 
164  min_property_->setHidden(normalize);
165  max_property_->setHidden(normalize);
167 
170  }
171  else
172  {
174  min_property_->setHidden(true);
175  max_property_->setHidden(true);
177  }
178 }
179 
181 {
182  texture_.clear();
183 
184  if( render_panel_->getCamera() )
185  {
186  render_panel_->getCamera()->setPosition(Ogre::Vector3(999999, 999999, 999999));
187  }
188 }
189 
190 void ImageDisplay::update( float wall_dt, float ros_dt )
191 {
192  try
193  {
194  texture_.update();
195 
196  //make sure the aspect ratio of the image is preserved
197  float win_width = render_panel_->width();
198  float win_height = render_panel_->height();
199 
200  float img_width = texture_.getWidth();
201  float img_height = texture_.getHeight();
202 
203  if ( img_width != 0 && img_height != 0 && win_width !=0 && win_height != 0 )
204  {
205  float img_aspect = img_width / img_height;
206  float win_aspect = win_width / win_height;
207 
208  if ( img_aspect > win_aspect )
209  {
210  screen_rect_->setCorners(-1.0f, 1.0f * win_aspect/img_aspect, 1.0f, -1.0f * win_aspect/img_aspect, false);
211  }
212  else
213  {
214  screen_rect_->setCorners(-1.0f * img_aspect/win_aspect, 1.0f, 1.0f * img_aspect/win_aspect, -1.0f, false);
215  }
216  }
217 
218  render_panel_->getRenderWindow()->update();
219  }
220  catch( UnsupportedImageEncoding& e )
221  {
222  setStatus(StatusProperty::Error, "Image", e.what());
223  }
224 }
225 
227 {
229  clear();
230 }
231 
232 /* This is called by incomingMessage(). */
233 void ImageDisplay::processMessage(const sensor_msgs::Image::ConstPtr& msg)
234 {
235  bool got_float_image = msg->encoding == sensor_msgs::image_encodings::TYPE_32FC1 ||
236  msg->encoding == sensor_msgs::image_encodings::TYPE_16UC1 ||
237  msg->encoding == sensor_msgs::image_encodings::TYPE_16SC1 ||
238  msg->encoding == sensor_msgs::image_encodings::MONO16;
239 
240  if ( got_float_image != got_float_image_ )
241  {
242  got_float_image_ = got_float_image;
244  }
245  texture_.addMessage(msg);
246 }
247 
248 } // namespace rviz
249 
const Ogre::TexturePtr & getTexture()
RenderPanel * render_panel_
Definition: image_display.h:97
virtual void reset()
Reset display.
void initialize(Ogre::SceneManager *scene_manager, DisplayContext *manager)
f
DisplayContext * context_
This DisplayContext pointer is the main connection a Display has into the rest of rviz...
Definition: display.h:256
BoolProperty * normalize_property_
Definition: image_display.h:99
FloatProperty * max_property_
void setAutoRender(bool auto_render)
virtual int getInt() const
Return the internal property value as an integer.
Definition: int_property.h:73
virtual float getFloat() const
Ogre::Rectangle2D * screen_rect_
Definition: image_display.h:92
Ogre::SceneManager * img_scene_manager_
Definition: image_display.h:90
Property specialized to enforce floating point max/min.
Property specialized to provide max/min enforcement for integers.
Definition: int_property.h:38
void setNormalizeFloatImage(bool normalize, double min=0.0, double max=1.0)
virtual bool getBool() const
void setMedianFrames(unsigned median_frames)
virtual void update(float wall_dt, float ros_dt)
Called periodically by the visualization manager.
Ogre::RenderWindow * getRenderWindow()
Definition: render_widget.h:50
virtual void onDisable()
Derived classes override this to do the actual work of disabling themselves.
virtual void subscribe()
ROS topic management.
virtual void onEnable()
Derived classes override this to do the actual work of enabling themselves.
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:357
IntProperty * median_buffer_size_property_
const std::string TYPE_16UC1
const std::string MONO16
FloatProperty * min_property_
TFSIMD_FORCE_INLINE Vector3 & normalize()
virtual void onInitialize()
Override this function to do subclass-specific initialization.
virtual void setHidden(bool hidden)
Hide or show this property in any PropertyTreeWidget viewing its parent.
Definition: property.cpp:530
virtual void reset()
Reset display.
Ogre::SceneNode * img_scene_node_
Definition: image_display.h:91
Ogre::Camera * getCamera() const
virtual void onInitialize()
Override this function to do subclass-specific initialization.
virtual void processMessage(const sensor_msgs::Image::ConstPtr &msg)
Implement this to process the contents of a message.
ROSImageTexture texture_
Definition: image_display.h:95
Ogre::MaterialPtr material_
Definition: image_display.h:93
bool initialized() const
Returns true if the display has been initialized.
Definition: display.h:247
#define PLUGINLIB_EXPORT_CLASS(class_type, base_class_type)
BoolProperty(const QString &name=QString(), bool default_value=false, const QString &description=QString(), Property *parent=0, const char *changed_slot=0, QObject *receiver=0)
virtual void setStatus(StatusProperty::Level level, const QString &name, const QString &text)
Show status level and text. This is thread-safe.
Definition: display.cpp:186
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 Apr 27 2019 02:33:41