overlay_utils.cpp
Go to the documentation of this file.
1 // -*- mode: c++ -*-
2 /*********************************************************************
3  * Software License Agreement (BSD License)
4  *
5  * Copyright (c) 2014, JSK Lab
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * * Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  * * Redistributions in binary form must reproduce the above
15  * copyright notice, this list of conditions and the following
16  * disclaimer in the documentation and/or other materials provided
17  * with the distribution.
18  * * Neither the name of the JSK Lab nor the names of its
19  * contributors may be used to endorse or promote products derived
20  * from this software without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
28  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
30  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
32  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33  * POSSIBILITY OF SUCH DAMAGE.
34  *********************************************************************/
35 
36 #include "overlay_utils.h"
37 #include <ros/ros.h>
38 
39 namespace jsk_rviz_plugins
40 {
41  ScopedPixelBuffer::ScopedPixelBuffer(Ogre::HardwarePixelBufferSharedPtr pixel_buffer):
42  pixel_buffer_(pixel_buffer)
43  {
44  pixel_buffer_->lock( Ogre::HardwareBuffer::HBL_NORMAL );
45  }
46 
48  {
49  pixel_buffer_->unlock();
50  }
51 
52  Ogre::HardwarePixelBufferSharedPtr ScopedPixelBuffer::getPixelBuffer()
53  {
54  return pixel_buffer_;
55  }
56 
57  QImage ScopedPixelBuffer::getQImage(unsigned int width, unsigned int height)
58  {
59  const Ogre::PixelBox& pixelBox = pixel_buffer_->getCurrentLock();
60  Ogre::uint8* pDest = static_cast<Ogre::uint8*> (pixelBox.data);
61  memset(pDest, 0, width * height);
62  return QImage(pDest, width, height, QImage::Format_ARGB32 );
63  }
64 
66  unsigned int width, unsigned int height, QColor& bg_color)
67  {
68  QImage Hud = getQImage(width, height);
69  for (unsigned int i = 0; i < width; i++) {
70  for (unsigned int j = 0; j < height; j++) {
71  Hud.setPixel(i, j, bg_color.rgba());
72  }
73  }
74  return Hud;
75  }
76 
77  QImage ScopedPixelBuffer::getQImage(OverlayObject& overlay)
78  {
79  return getQImage(overlay.getTextureWidth(), overlay.getTextureHeight());
80  }
81 
83  QColor& bg_color)
84  {
85  return getQImage(overlay.getTextureWidth(), overlay.getTextureHeight(),
86  bg_color);
87  }
88 
89  OverlayObject::OverlayObject(const std::string& name)
90  : name_(name)
91  {
92  std::string material_name = name_ + "Material";
93  Ogre::OverlayManager* mOverlayMgr = Ogre::OverlayManager::getSingletonPtr();
94  overlay_ = mOverlayMgr->create(name_);
95  panel_ = static_cast<Ogre::PanelOverlayElement*> (
96  mOverlayMgr->createOverlayElement("Panel", name_ + "Panel"));
97  panel_->setMetricsMode(Ogre::GMM_PIXELS);
98 
99  panel_material_
100  = Ogre::MaterialManager::getSingleton().create(
101  material_name,
102  Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME);
103  panel_->setMaterialName(panel_material_->getName());
104  overlay_->add2D(panel_);
105  }
106 
108  {
109  hide();
110  panel_material_->unload();
111  Ogre::MaterialManager::getSingleton().remove(panel_material_->getName());
112  // Ogre::OverlayManager* mOverlayMgr = Ogre::OverlayManager::getSingletonPtr();
113  // mOverlayMgr->destroyOverlayElement(panel_);
114  //delete panel_;
115  //delete overlay_;
116  }
117 
118  std::string OverlayObject::getName()
119  {
120  return name_;
121  }
122 
123  void OverlayObject::hide()
124  {
125  if (overlay_->isVisible()) {
126  overlay_->hide();
127  }
128  }
129 
130  void OverlayObject::show()
131  {
132  if (!overlay_->isVisible()) {
133  overlay_->show();
134  }
135  }
136 
138  {
139  return !texture_.isNull();
140  }
141 
142  void OverlayObject::updateTextureSize(unsigned int width, unsigned int height)
143  {
144  const std::string texture_name = name_ + "Texture";
145  if (width == 0) {
146  ROS_WARN("[OverlayObject] width=0 is specified as texture size");
147  width = 1;
148  }
149  if (height == 0) {
150  ROS_WARN("[OverlayObject] height=0 is specified as texture size");
151  height = 1;
152  }
153  if (!isTextureReady() ||
154  ((width != texture_->getWidth()) ||
155  (height != texture_->getHeight()))) {
156  if (isTextureReady()) {
157  Ogre::TextureManager::getSingleton().remove(texture_name);
158  panel_material_->getTechnique(0)->getPass(0)
159  ->removeAllTextureUnitStates();
160  }
161  texture_ = Ogre::TextureManager::getSingleton().createManual(
162  texture_name, // name
163  Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME,
164  Ogre::TEX_TYPE_2D, // type
165  width, height, // width & height of the render window
166  0, // number of mipmaps
167  Ogre::PF_A8R8G8B8, // pixel format chosen to match a format Qt can use
168  Ogre::TU_DEFAULT // usage
169  );
170  panel_material_->getTechnique(0)->getPass(0)
171  ->createTextureUnitState(texture_name);
172 
173  panel_material_->getTechnique(0)->getPass(0)
174  ->setSceneBlending(Ogre::SBT_TRANSPARENT_ALPHA);
175  }
176  }
177 
178  ScopedPixelBuffer OverlayObject::getBuffer()
179  {
180  if (isTextureReady()) {
181  return ScopedPixelBuffer(texture_->getBuffer());
182  }
183  else {
184  return ScopedPixelBuffer(Ogre::HardwarePixelBufferSharedPtr());
185  }
186  }
187 
188  void OverlayObject::setPosition(double left, double top)
189  {
190  panel_->setPosition(left, top);
191  }
192 
193  void OverlayObject::setDimensions(double width, double height)
194  {
195  panel_->setDimensions(width, height);
196  }
197 
199  {
200  return overlay_->isVisible();
201  }
202 
203  unsigned int OverlayObject::getTextureWidth()
204  {
205  if (isTextureReady()) {
206  return texture_->getWidth();
207  }
208  else {
209  return 0;
210  }
211  }
212 
213  unsigned int OverlayObject::getTextureHeight()
214  {
215  if (isTextureReady()) {
216  return texture_->getHeight();
217  }
218  else {
219  return 0;
220  }
221  }
222 
223 
224 }
jsk_rviz_plugins::OverlayObject::isTextureReady
virtual bool isTextureReady()
Definition: overlay_utils.cpp:169
jsk_rviz_plugins::OverlayObject::getTextureWidth
virtual unsigned int getTextureWidth()
Definition: overlay_utils.cpp:235
jsk_rviz_plugins::ScopedPixelBuffer::ScopedPixelBuffer
ScopedPixelBuffer(Ogre::HardwarePixelBufferSharedPtr pixel_buffer)
Definition: overlay_utils.cpp:73
ros.h
jsk_rviz_plugins::OverlayObject::getTextureHeight
virtual unsigned int getTextureHeight()
Definition: overlay_utils.cpp:245
jsk_rviz_plugins::OverlayObject::isVisible
virtual bool isVisible()
Definition: overlay_utils.cpp:230
jsk_rviz_plugins::OverlayObject::OverlayObject
OverlayObject(const std::string &name)
Definition: overlay_utils.cpp:121
name_
std::string name_
overlay_sample.left
left
Definition: overlay_sample.py:26
overlay_sample.bg_color
bg_color
Definition: overlay_sample.py:45
jsk_rviz_plugins::ScopedPixelBuffer::~ScopedPixelBuffer
virtual ~ScopedPixelBuffer()
Definition: overlay_utils.cpp:79
jsk_rviz_plugins::OverlayObject::show
virtual void show()
Definition: overlay_utils.cpp:162
jsk_rviz_plugins::OverlayObject::name_
const std::string name_
Definition: overlay_utils.h:144
overlay_utils.h
jsk_rviz_plugins::OverlayObject::overlay_
Ogre::Overlay * overlay_
Definition: overlay_utils.h:145
jsk_rviz_plugins::OverlayObject::~OverlayObject
virtual ~OverlayObject()
Definition: overlay_utils.cpp:139
jsk_rviz_plugins::OverlayObject::getName
virtual std::string getName()
Definition: overlay_utils.cpp:150
jsk_rviz_plugins::OverlayObject::hide
virtual void hide()
Definition: overlay_utils.cpp:155
ROS_WARN
#define ROS_WARN(...)
jsk_rviz_plugins::ScopedPixelBuffer::getPixelBuffer
virtual Ogre::HardwarePixelBufferSharedPtr getPixelBuffer()
Definition: overlay_utils.cpp:84
jsk_rviz_plugins::OverlayObject::setPosition
virtual void setPosition(double left, double top)
Definition: overlay_utils.cpp:220
width
width
jsk_rviz_plugins::OverlayObject::getBuffer
virtual ScopedPixelBuffer getBuffer()
Definition: overlay_utils.cpp:210
jsk_rviz_plugins::OverlayObject::panel_
Ogre::PanelOverlayElement * panel_
Definition: overlay_utils.h:146
jsk_rviz_plugins::OverlayObject::updateTextureSize
virtual void updateTextureSize(unsigned int width, unsigned int height)
Definition: overlay_utils.cpp:174
jsk_rviz_plugins::OverlayObject::panel_material_
Ogre::MaterialPtr panel_material_
Definition: overlay_utils.h:147
jsk_rviz_plugins::ScopedPixelBuffer::getQImage
virtual QImage getQImage(unsigned int width, unsigned int height)
Definition: overlay_utils.cpp:89
overlay_sample.top
top
Definition: overlay_sample.py:27
jsk_rviz_plugins::OverlayObject::texture_
Ogre::TexturePtr texture_
Definition: overlay_utils.h:148
height
height
jsk_rviz_plugins::ScopedPixelBuffer::pixel_buffer_
Ogre::HardwarePixelBufferSharedPtr pixel_buffer_
Definition: overlay_utils.h:111
jsk_rviz_plugins::OverlayObject
Definition: overlay_utils.h:120
jsk_rviz_plugins
Definition: __init__.py:1
jsk_rviz_plugins::OverlayObject::setDimensions
virtual void setDimensions(double width, double height)
Definition: overlay_utils.cpp:225


jsk_rviz_plugins
Author(s): Kei Okada , Yohei Kakiuchi , Shohei Fujii , Ryohei Ueda
autogenerated on Fri Aug 2 2024 08:50:14