|
| 1 | +// |
| 2 | +// Copyright 2025 TIER IV, Inc. All rights reserved. |
| 3 | +// |
| 4 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +// you may not use this file except in compliance with the License. |
| 6 | +// You may obtain a copy of the License at |
| 7 | +// |
| 8 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +// |
| 10 | +// Unless required by applicable law or agreed to in writing, software |
| 11 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +// See the License for the specific language governing permissions and |
| 14 | +// limitations under the License. |
| 15 | + |
| 16 | +#ifndef AUTOWARE_PERCEPTION_RVIZ_PLUGIN__TRAFFIC_LIGHT__TRAFFIC_LIGHT_DISPLAY_HPP_ |
| 17 | +#define AUTOWARE_PERCEPTION_RVIZ_PLUGIN__TRAFFIC_LIGHT__TRAFFIC_LIGHT_DISPLAY_HPP_ |
| 18 | + |
| 19 | +#include "rviz_common/properties/ros_topic_property.hpp" |
| 20 | +#include "rviz_rendering/objects/shape.hpp" |
| 21 | + |
| 22 | +#include <rclcpp/rclcpp.hpp> |
| 23 | +#include <rviz_common/display.hpp> |
| 24 | +#include <rviz_common/properties/bool_property.hpp> |
| 25 | +#include <rviz_common/properties/color_property.hpp> |
| 26 | +#include <rviz_common/properties/float_property.hpp> |
| 27 | +#include <rviz_common/properties/string_property.hpp> |
| 28 | +#include <rviz_rendering/objects/movable_text.hpp> |
| 29 | + |
| 30 | +#include <autoware_map_msgs/msg/lanelet_map_bin.hpp> |
| 31 | +#include <autoware_perception_msgs/msg/traffic_light_group_array.hpp> |
| 32 | + |
| 33 | +#include <boost/optional/optional.hpp> |
| 34 | + |
| 35 | +#include <OgreSceneNode.h> |
| 36 | +#include <lanelet2_core/LaneletMap.h> |
| 37 | +#include <lanelet2_core/primitives/Lanelet.h> |
| 38 | + |
| 39 | +#include <memory> |
| 40 | +#include <optional> |
| 41 | +#include <string> |
| 42 | +#include <unordered_map> |
| 43 | +#include <vector> |
| 44 | + |
| 45 | +namespace autoware_perception_rviz_plugin::traffic_light |
| 46 | +{ |
| 47 | + |
| 48 | +struct Point3d |
| 49 | +{ |
| 50 | + double x; |
| 51 | + double y; |
| 52 | + double z; |
| 53 | +}; |
| 54 | + |
| 55 | +struct TrafficLightBulbInfo |
| 56 | +{ |
| 57 | + lanelet::Id id; |
| 58 | + std::string color; |
| 59 | + std::string shape; |
| 60 | + Point3d position; |
| 61 | +}; |
| 62 | + |
| 63 | +struct TrafficLightInfo |
| 64 | +{ |
| 65 | + lanelet::Id id; |
| 66 | + lanelet::ConstLineString3d linestring; |
| 67 | + boost::optional<double> height; |
| 68 | + std::vector<TrafficLightBulbInfo> bulbs; |
| 69 | + |
| 70 | + [[nodiscard]] TrafficLightBulbInfo getEstimatedBulb(const std::string & color) const; |
| 71 | + |
| 72 | + [[nodiscard]] Point3d getLinestringCenter() const; |
| 73 | + |
| 74 | + [[nodiscard]] std::vector<TrafficLightBulbInfo> getBlightBulbs( |
| 75 | + const std::vector<autoware_perception_msgs::msg::TrafficLightElement> & current_elements) const; |
| 76 | +}; |
| 77 | + |
| 78 | +class TrafficLightDisplay : public rviz_common::Display |
| 79 | +{ |
| 80 | + Q_OBJECT |
| 81 | + |
| 82 | +public: |
| 83 | + TrafficLightDisplay(); |
| 84 | + ~TrafficLightDisplay() override; |
| 85 | + |
| 86 | +protected: |
| 87 | + void onInitialize() override; |
| 88 | + void reset() override; |
| 89 | + void update(float wall_dt, float ros_dt) override; |
| 90 | + void onEnable() override; |
| 91 | + void onDisable() override; |
| 92 | + |
| 93 | +private Q_SLOTS: |
| 94 | + void topic_updated_lanelet_map(); |
| 95 | + void topic_updated_traffic_light(); |
| 96 | + |
| 97 | +private: // NOLINT |
| 98 | + // Display methods |
| 99 | + void hideAllDisplays(); |
| 100 | + void updateTrafficLightText(const TrafficLightInfo & info, const std::string & state_text); |
| 101 | + void updateTrafficLightBulbs( |
| 102 | + const TrafficLightInfo & info, |
| 103 | + const std::vector<autoware_perception_msgs::msg::TrafficLightElement> & elements); |
| 104 | + bool checkTimeout() const; |
| 105 | + |
| 106 | + // Properties |
| 107 | + std::unique_ptr<rviz_common::properties::RosTopicProperty> lanelet_map_topic_property_; |
| 108 | + std::unique_ptr<rviz_common::properties::RosTopicProperty> traffic_light_topic_property_; |
| 109 | + std::unique_ptr<rviz_common::properties::FloatProperty> timeout_property_; |
| 110 | + std::unique_ptr<rviz_common::properties::FloatProperty> text_z_offset_property_; |
| 111 | + std::unique_ptr<rviz_common::properties::FloatProperty> text_x_offset_property_; |
| 112 | + std::unique_ptr<rviz_common::properties::FloatProperty> text_y_offset_property_; |
| 113 | + std::unique_ptr<rviz_common::properties::BoolProperty> show_text_property_; |
| 114 | + std::unique_ptr<rviz_common::properties::BoolProperty> show_bulb_property_; |
| 115 | + std::unique_ptr<rviz_common::properties::StringProperty> text_prefix_property_; |
| 116 | + std::unique_ptr<rviz_common::properties::FloatProperty> font_size_property_; |
| 117 | + std::unique_ptr<rviz_common::properties::ColorProperty> text_color_property_; |
| 118 | + std::unique_ptr<rviz_common::properties::FloatProperty> bulb_radius_property_; |
| 119 | + |
| 120 | + // Subscribers |
| 121 | + rclcpp::Subscription<autoware_map_msgs::msg::LaneletMapBin>::SharedPtr lanelet_map_sub_; |
| 122 | + rclcpp::Subscription<autoware_perception_msgs::msg::TrafficLightGroupArray>::SharedPtr |
| 123 | + traffic_light_group_array_sub_; |
| 124 | + |
| 125 | + // Callback functions |
| 126 | + void onLaneletMapReceived(const autoware_map_msgs::msg::LaneletMapBin::ConstSharedPtr msg); |
| 127 | + void onTrafficLightGroupArrayReceived( |
| 128 | + const autoware_perception_msgs::msg::TrafficLightGroupArray::ConstSharedPtr msg); |
| 129 | + |
| 130 | + std::mutex property_mutex_; |
| 131 | + std::mutex lanelet_map_mutex_; |
| 132 | + std::mutex traffic_light_mutex_; |
| 133 | + void setupRosSubscriptions(); |
| 134 | + |
| 135 | + // Data storage |
| 136 | + lanelet::LaneletMapPtr lanelet_map_; |
| 137 | + autoware_perception_msgs::msg::TrafficLightGroupArray::ConstSharedPtr traffic_light_groups_; |
| 138 | + rclcpp::Time last_traffic_light_received_time_; |
| 139 | + |
| 140 | + // Root node for text and shape visualization |
| 141 | + Ogre::SceneNode * root_node_{nullptr}; |
| 142 | + |
| 143 | + // Text visualization |
| 144 | + std::unordered_map<lanelet::Id, Ogre::SceneNode *> traffic_light_text_nodes_; |
| 145 | + std::unordered_map<lanelet::Id, std::unique_ptr<rviz_rendering::MovableText>> |
| 146 | + traffic_light_text_displays_; |
| 147 | + |
| 148 | + // Shape visualization |
| 149 | + std::unordered_map<lanelet::Id, std::unique_ptr<rviz_rendering::Shape>> |
| 150 | + traffic_light_bulb_displays_; |
| 151 | +}; |
| 152 | + |
| 153 | +} // namespace autoware_perception_rviz_plugin::traffic_light |
| 154 | + |
| 155 | +#endif // AUTOWARE_PERCEPTION_RVIZ_PLUGIN__TRAFFIC_LIGHT__TRAFFIC_LIGHT_DISPLAY_HPP_ |
0 commit comments