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 
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 
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 
119  {
120  return name_;
121  }
122 
124  {
125  if (overlay_->isVisible()) {
126  overlay_->hide();
127  }
128  }
129 
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 
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 
204  {
205  if (isTextureReady()) {
206  return texture_->getWidth();
207  }
208  else {
209  return 0;
210  }
211  }
212 
214  {
215  if (isTextureReady()) {
216  return texture_->getHeight();
217  }
218  else {
219  return 0;
220  }
221  }
222 
223 
224 }
virtual void setDimensions(double width, double height)
std::string name_
virtual QImage getQImage(unsigned int width, unsigned int height)
OverlayObject(const std::string &name)
Ogre::HardwarePixelBufferSharedPtr pixel_buffer_
Definition: overlay_utils.h:79
virtual ScopedPixelBuffer getBuffer()
#define ROS_WARN(...)
Ogre::MaterialPtr panel_material_
virtual unsigned int getTextureWidth()
virtual void updateTextureSize(unsigned int width, unsigned int height)
virtual void setPosition(double left, double top)
virtual std::string getName()
ScopedPixelBuffer(Ogre::HardwarePixelBufferSharedPtr pixel_buffer)
virtual unsigned int getTextureHeight()
virtual Ogre::HardwarePixelBufferSharedPtr getPixelBuffer()
Ogre::PanelOverlayElement * panel_


jsk_rviz_plugins
Author(s): Kei Okada , Yohei Kakiuchi , Shohei Fujii , Ryohei Ueda
autogenerated on Thu Jun 1 2023 02:45:58