|
| 1 | +// Copyright 2023 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 "tier4_debug_rviz_plugin/string_stamped.hpp" |
| 49 | + |
| 50 | +#include "tier4_debug_rviz_plugin/jsk_overlay_utils.hpp" |
| 51 | + |
| 52 | +#include <QPainter> |
| 53 | +#include <rviz_common/uniform_string_stream.hpp> |
| 54 | + |
| 55 | +#include <X11/Xlib.h> |
| 56 | + |
| 57 | +#include <algorithm> |
| 58 | +#include <optional> |
| 59 | +#include <string> |
| 60 | +#include <vector> |
| 61 | + |
| 62 | +namespace rviz_plugins |
| 63 | +{ |
| 64 | +StringStampedOverlayDisplay::StringStampedOverlayDisplay() |
| 65 | +{ |
| 66 | + const Screen * screen_info = DefaultScreenOfDisplay(XOpenDisplay(NULL)); |
| 67 | + |
| 68 | + constexpr float hight_4k = 2160.0; |
| 69 | + const float scale = static_cast<float>(screen_info->height) / hight_4k; |
| 70 | + const auto left = static_cast<int>(std::round(1024 * scale)); |
| 71 | + const auto top = static_cast<int>(std::round(128 * scale)); |
| 72 | + |
| 73 | + property_text_color_ = new rviz_common::properties::ColorProperty( |
| 74 | + "Text Color", QColor(25, 255, 240), "text color", this, SLOT(updateVisualization()), this); |
| 75 | + property_left_ = new rviz_common::properties::IntProperty( |
| 76 | + "Left", left, "Left of the plotter window", this, SLOT(updateVisualization()), this); |
| 77 | + property_left_->setMin(0); |
| 78 | + property_top_ = new rviz_common::properties::IntProperty( |
| 79 | + "Top", top, "Top of the plotter window", this, SLOT(updateVisualization())); |
| 80 | + property_top_->setMin(0); |
| 81 | + |
| 82 | + property_value_height_offset_ = new rviz_common::properties::IntProperty( |
| 83 | + "Value height offset", 0, "Height offset of the plotter window", this, |
| 84 | + SLOT(updateVisualization())); |
| 85 | + property_font_size_ = new rviz_common::properties::IntProperty( |
| 86 | + "Font Size", 15, "Font Size", this, SLOT(updateVisualization()), this); |
| 87 | + property_font_size_->setMin(1); |
| 88 | + property_max_letter_num_ = new rviz_common::properties::IntProperty( |
| 89 | + "Max Letter Num", 100, "Max Letter Num", this, SLOT(updateVisualization()), this); |
| 90 | + property_max_letter_num_->setMin(10); |
| 91 | +} |
| 92 | + |
| 93 | +StringStampedOverlayDisplay::~StringStampedOverlayDisplay() |
| 94 | +{ |
| 95 | + if (initialized()) { |
| 96 | + overlay_->hide(); |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +void StringStampedOverlayDisplay::onInitialize() |
| 101 | +{ |
| 102 | + RTDClass::onInitialize(); |
| 103 | + |
| 104 | + static int count = 0; |
| 105 | + rviz_common::UniformStringStream ss; |
| 106 | + ss << "StringOverlayDisplayObject" << count++; |
| 107 | + auto logger = context_->getRosNodeAbstraction().lock()->get_raw_node()->get_logger(); |
| 108 | + overlay_.reset(new jsk_rviz_plugins::OverlayObject(scene_manager_, logger, ss.str())); |
| 109 | + |
| 110 | + overlay_->show(); |
| 111 | + |
| 112 | + const int texture_size = property_font_size_->getInt() * property_max_letter_num_->getInt(); |
| 113 | + overlay_->updateTextureSize(texture_size, texture_size); |
| 114 | + overlay_->setPosition(property_left_->getInt(), property_top_->getInt()); |
| 115 | + overlay_->setDimensions(overlay_->getTextureWidth(), overlay_->getTextureHeight()); |
| 116 | +} |
| 117 | + |
| 118 | +void StringStampedOverlayDisplay::onEnable() |
| 119 | +{ |
| 120 | + subscribe(); |
| 121 | + overlay_->show(); |
| 122 | +} |
| 123 | + |
| 124 | +void StringStampedOverlayDisplay::onDisable() |
| 125 | +{ |
| 126 | + unsubscribe(); |
| 127 | + reset(); |
| 128 | + overlay_->hide(); |
| 129 | +} |
| 130 | + |
| 131 | +void StringStampedOverlayDisplay::update(float wall_dt, float ros_dt) |
| 132 | +{ |
| 133 | + (void)wall_dt; |
| 134 | + (void)ros_dt; |
| 135 | + |
| 136 | + std::lock_guard<std::mutex> message_lock(mutex_); |
| 137 | + if (!last_msg_ptr_) { |
| 138 | + return; |
| 139 | + } |
| 140 | + |
| 141 | + // Display |
| 142 | + QColor background_color; |
| 143 | + background_color.setAlpha(0); |
| 144 | + jsk_rviz_plugins::ScopedPixelBuffer buffer = overlay_->getBuffer(); |
| 145 | + QImage hud = buffer.getQImage(*overlay_); |
| 146 | + hud.fill(background_color); |
| 147 | + |
| 148 | + QPainter painter(&hud); |
| 149 | + painter.setRenderHint(QPainter::Antialiasing, true); |
| 150 | + |
| 151 | + const int w = overlay_->getTextureWidth() - line_width_; |
| 152 | + const int h = overlay_->getTextureHeight() - line_width_; |
| 153 | + |
| 154 | + // text |
| 155 | + QColor text_color(property_text_color_->getColor()); |
| 156 | + text_color.setAlpha(255); |
| 157 | + painter.setPen(QPen(text_color, static_cast<int>(2), Qt::SolidLine)); |
| 158 | + QFont font = painter.font(); |
| 159 | + font.setPixelSize(property_font_size_->getInt()); |
| 160 | + font.setBold(true); |
| 161 | + painter.setFont(font); |
| 162 | + |
| 163 | + // same as above, but align on right side |
| 164 | + painter.drawText( |
| 165 | + 0, std::min(property_value_height_offset_->getInt(), h - 1), w, |
| 166 | + std::max(h - property_value_height_offset_->getInt(), 1), Qt::AlignLeft | Qt::AlignTop, |
| 167 | + last_msg_ptr_->data.c_str()); |
| 168 | + painter.end(); |
| 169 | + updateVisualization(); |
| 170 | +} |
| 171 | + |
| 172 | +void StringStampedOverlayDisplay::processMessage( |
| 173 | + const tier4_debug_msgs::msg::StringStamped::ConstSharedPtr msg_ptr) |
| 174 | +{ |
| 175 | + if (!isEnabled()) { |
| 176 | + return; |
| 177 | + } |
| 178 | + |
| 179 | + { |
| 180 | + std::lock_guard<std::mutex> message_lock(mutex_); |
| 181 | + last_msg_ptr_ = msg_ptr; |
| 182 | + } |
| 183 | + |
| 184 | + queueRender(); |
| 185 | +} |
| 186 | + |
| 187 | +void StringStampedOverlayDisplay::updateVisualization() |
| 188 | +{ |
| 189 | + const int texture_size = property_font_size_->getInt() * property_max_letter_num_->getInt(); |
| 190 | + overlay_->updateTextureSize(texture_size, texture_size); |
| 191 | + overlay_->setPosition(property_left_->getInt(), property_top_->getInt()); |
| 192 | + overlay_->setDimensions(overlay_->getTextureWidth(), overlay_->getTextureHeight()); |
| 193 | +} |
| 194 | + |
| 195 | +} // namespace rviz_plugins |
| 196 | + |
| 197 | +#include <pluginlib/class_list_macros.hpp> |
| 198 | +PLUGINLIB_EXPORT_CLASS(rviz_plugins::StringStampedOverlayDisplay, rviz_common::Display) |
0 commit comments