Skip to content

Commit 452e076

Browse files
authored
feat(autoware_overlay_rviz_plugin): add plugin to show string stamped (#9683)
* feat(autoware_overlay_rviz_plugin): add plugin to show string stamped Signed-off-by: Takayuki Murooka <takayuki5168@gmail.com> * fix Signed-off-by: Takayuki Murooka <takayuki5168@gmail.com> * better visualization Signed-off-by: Takayuki Murooka <takayuki5168@gmail.com> * update Signed-off-by: Takayuki Murooka <takayuki5168@gmail.com> * fix cpp check and typo Signed-off-by: Takayuki Murooka <takayuki5168@gmail.com> * fix Signed-off-by: Takayuki Murooka <takayuki5168@gmail.com> * fix Signed-off-by: Takayuki Murooka <takayuki5168@gmail.com> --------- Signed-off-by: Takayuki Murooka <takayuki5168@gmail.com>
1 parent 1586372 commit 452e076

File tree

8 files changed

+785
-0
lines changed

8 files changed

+785
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
cmake_minimum_required(VERSION 3.8)
2+
project(autoware_string_stamped_rviz_plugin)
3+
4+
# find dependencies
5+
find_package(autoware_cmake REQUIRED)
6+
autoware_package()
7+
8+
find_package(Qt5 REQUIRED Core Widgets)
9+
set(QT_LIBRARIES Qt5::Widgets)
10+
set(CMAKE_AUTOMOC ON)
11+
set(CMAKE_INCLUDE_CURRENT_DIR ON)
12+
add_definitions(-DQT_NO_KEYWORDS)
13+
14+
ament_auto_add_library(${PROJECT_NAME} SHARED
15+
DIRECTORY src
16+
)
17+
18+
target_link_libraries(${PROJECT_NAME}
19+
${QT_LIBRARIES}
20+
)
21+
22+
# Export the plugin to be imported by rviz2
23+
pluginlib_export_plugin_description_file(rviz_common plugins/plugins_description.xml)
24+
25+
ament_auto_package(
26+
INSTALL_TO_SHARE
27+
plugins
28+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# autoware_string_stamped_rviz_plugin
2+
3+
Plugin for displaying 2D overlays over the RViz2 3D scene.
4+
5+
## Purpose
6+
7+
This plugin displays the ROS message whose topic type is `autoware_internal_debug_msgs::msg::StringStamped` in rviz.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?xml version="1.0"?>
2+
<?xml-model href="http://download.ros.org/schema/package_format3.xsd" schematypens="http://www.w3.org/2001/XMLSchema"?>
3+
<package format="3">
4+
<name>autoware_string_stamped_rviz_plugin</name>
5+
<version>0.39.0</version>
6+
<description>
7+
RViz2 plugin for 2D overlays in the 3D view. Mainly a port of the JSK overlay plugin
8+
(https://github.com/jsk-ros-pkg/jsk_visualization).
9+
</description>
10+
<maintainer email="takayuki.murooka@tier4.jp">Takayuki Murooka</maintainer>
11+
<maintainer email="satoshi.ota@tier4.jp">Satoshi Ota</maintainer>
12+
13+
<license>Apache License 2.0</license>
14+
15+
<buildtool_depend>ament_cmake_auto</buildtool_depend>
16+
<buildtool_depend>autoware_cmake</buildtool_depend>
17+
18+
<depend>ament_index_cpp</depend>
19+
<depend>autoware_internal_debug_msgs</depend>
20+
<depend>rviz_common</depend>
21+
<depend>rviz_ogre_vendor</depend>
22+
<depend>rviz_rendering</depend>
23+
24+
<test_depend>ament_lint_auto</test_depend>
25+
<test_depend>autoware_lint_common</test_depend>
26+
27+
<export>
28+
<build_type>ament_cmake</build_type>
29+
</export>
30+
</package>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<library path="autoware_string_stamped_rviz_plugin">
2+
<class name="autoware_string_stamped_rviz_plugin/StringStampedOverlayDisplay" type="autoware::string_stamped_rviz_plugin::StringStampedOverlayDisplay" base_class_type="rviz_common::Display">
3+
<description>String stamped overlay plugin for the 3D view.</description>
4+
</class>
5+
</library>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,225 @@
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

Comments
 (0)