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