|
| 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 FOR |
| 37 | +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 38 | +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 39 | +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 40 | +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 41 | +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 42 | +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 43 | +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 44 | +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 45 | + |
| 46 | +#include "jsk_overlay_utils.hpp" |
| 47 | + |
| 48 | +#include <rviz_rendering/render_system.hpp> |
| 49 | + |
| 50 | +#include <cstring> |
| 51 | +#include <string> |
| 52 | +#include <utility> |
| 53 | + |
| 54 | +namespace autoware::topic_text_overlay_rviz_plugin |
| 55 | +{ |
| 56 | +ScopedPixelBuffer::ScopedPixelBuffer(Ogre::HardwarePixelBufferSharedPtr pixel_buffer) |
| 57 | +: pixel_buffer_(std::move(pixel_buffer)) |
| 58 | +{ |
| 59 | + pixel_buffer_->lock(Ogre::HardwareBuffer::HBL_NORMAL); |
| 60 | +} |
| 61 | + |
| 62 | +ScopedPixelBuffer::~ScopedPixelBuffer() |
| 63 | +{ |
| 64 | + pixel_buffer_->unlock(); |
| 65 | +} |
| 66 | + |
| 67 | +QImage ScopedPixelBuffer::getQImage(const unsigned int width, const unsigned int height) |
| 68 | +{ |
| 69 | + const Ogre::PixelBox & pixel_box = pixel_buffer_->getCurrentLock(); |
| 70 | + auto * destination = static_cast<Ogre::uint8 *>(pixel_box.data); |
| 71 | + std::memset(destination, 0, width * height * 4); |
| 72 | + return QImage(destination, width, height, QImage::Format_ARGB32); |
| 73 | +} |
| 74 | + |
| 75 | +QImage ScopedPixelBuffer::getQImage(OverlayObject & overlay) |
| 76 | +{ |
| 77 | + return getQImage(overlay.getTextureWidth(), overlay.getTextureHeight()); |
| 78 | +} |
| 79 | + |
| 80 | +OverlayObject::OverlayObject( |
| 81 | + Ogre::SceneManager * manager, rclcpp::Logger logger, const std::string & name) |
| 82 | +: name_(name), logger_(logger) |
| 83 | +{ |
| 84 | + rviz_rendering::RenderSystem::get()->prepareOverlays(manager); |
| 85 | + const std::string material_name = name_ + "Material"; |
| 86 | + auto * overlay_manager = Ogre::OverlayManager::getSingletonPtr(); |
| 87 | + overlay_ = overlay_manager->create(name_); |
| 88 | + panel_ = static_cast<Ogre::PanelOverlayElement *>( |
| 89 | + overlay_manager->createOverlayElement("Panel", name_ + "Panel")); |
| 90 | + panel_->setMetricsMode(Ogre::GMM_PIXELS); |
| 91 | + |
| 92 | + panel_material_ = Ogre::MaterialManager::getSingleton().create( |
| 93 | + material_name, Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME); |
| 94 | + panel_->setMaterialName(panel_material_->getName()); |
| 95 | + overlay_->add2D(panel_); |
| 96 | +} |
| 97 | + |
| 98 | +OverlayObject::~OverlayObject() |
| 99 | +{ |
| 100 | + hide(); |
| 101 | + panel_material_->unload(); |
| 102 | + Ogre::MaterialManager::getSingleton().remove(panel_material_->getName()); |
| 103 | +} |
| 104 | + |
| 105 | +void OverlayObject::hide() |
| 106 | +{ |
| 107 | + if (overlay_->isVisible()) { |
| 108 | + overlay_->hide(); |
| 109 | + } |
| 110 | +} |
| 111 | + |
| 112 | +void OverlayObject::show() |
| 113 | +{ |
| 114 | + if (!overlay_->isVisible()) { |
| 115 | + overlay_->show(); |
| 116 | + } |
| 117 | +} |
| 118 | + |
| 119 | +bool OverlayObject::isTextureReady() |
| 120 | +{ |
| 121 | + return static_cast<bool>(texture_); |
| 122 | +} |
| 123 | + |
| 124 | +void OverlayObject::updateTextureSize(unsigned int width, unsigned int height) |
| 125 | +{ |
| 126 | + const std::string texture_name = name_ + "Texture"; |
| 127 | + if (width == 0) { |
| 128 | + RCLCPP_WARN(logger_, "width=0 is specified as texture size"); |
| 129 | + width = 1; |
| 130 | + } |
| 131 | + if (height == 0) { |
| 132 | + RCLCPP_WARN(logger_, "height=0 is specified as texture size"); |
| 133 | + height = 1; |
| 134 | + } |
| 135 | + if (!isTextureReady() || width != texture_->getWidth() || height != texture_->getHeight()) { |
| 136 | + if (isTextureReady()) { |
| 137 | + Ogre::TextureManager::getSingleton().remove(texture_name); |
| 138 | + panel_material_->getTechnique(0)->getPass(0)->removeAllTextureUnitStates(); |
| 139 | + } |
| 140 | + texture_ = Ogre::TextureManager::getSingleton().createManual( |
| 141 | + texture_name, Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME, Ogre::TEX_TYPE_2D, |
| 142 | + width, height, 0, Ogre::PF_A8R8G8B8, Ogre::TU_DEFAULT); |
| 143 | + panel_material_->getTechnique(0)->getPass(0)->createTextureUnitState(texture_name); |
| 144 | + panel_material_->getTechnique(0)->getPass(0)->setSceneBlending(Ogre::SBT_TRANSPARENT_ALPHA); |
| 145 | + } |
| 146 | +} |
| 147 | + |
| 148 | +ScopedPixelBuffer OverlayObject::getBuffer() |
| 149 | +{ |
| 150 | + return ScopedPixelBuffer(texture_->getBuffer()); |
| 151 | +} |
| 152 | + |
| 153 | +void OverlayObject::setPosition(const double left, const double top) |
| 154 | +{ |
| 155 | + panel_->setPosition(left, top); |
| 156 | +} |
| 157 | + |
| 158 | +void OverlayObject::setDimensions(const double width, const double height) |
| 159 | +{ |
| 160 | + panel_->setDimensions(width, height); |
| 161 | +} |
| 162 | + |
| 163 | +unsigned int OverlayObject::getTextureWidth() |
| 164 | +{ |
| 165 | + return isTextureReady() ? texture_->getWidth() : 0; |
| 166 | +} |
| 167 | + |
| 168 | +unsigned int OverlayObject::getTextureHeight() |
| 169 | +{ |
| 170 | + return isTextureReady() ? texture_->getHeight() : 0; |
| 171 | +} |
| 172 | +} // namespace autoware::topic_text_overlay_rviz_plugin |
0 commit comments