Skip to content

Commit 27aab2d

Browse files
authored
Merge pull request tier4#1597 from tier4/feature/render-omitted-light-bulb
2 parents c9859d4 + 4784103 commit 27aab2d

File tree

9 files changed

+5558
-62
lines changed

9 files changed

+5558
-62
lines changed

simulation/traffic_simulator/include/traffic_simulator/hdmap_utils/hdmap_utils.hpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -202,8 +202,9 @@ class HdMapUtils
202202
auto getTangentVector(const lanelet::Id, const double s) const
203203
-> std::optional<geometry_msgs::msg::Vector3>;
204204

205-
auto getTrafficLightBulbPosition(const lanelet::Id traffic_light_id, const std::string &) const
206-
-> std::optional<geometry_msgs::msg::Point>;
205+
auto getTrafficLightBulbPosition(
206+
const lanelet::Id traffic_light_id, const std::string &,
207+
const bool allow_infer_position = false) const -> std::optional<geometry_msgs::msg::Point>;
207208

208209
auto getTrafficLightIds() const -> lanelet::Ids;
209210

simulation/traffic_simulator/include/traffic_simulator/traffic_lights/traffic_light.hpp

Lines changed: 37 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -438,7 +438,7 @@ struct TrafficLight
438438

439439
std::set<Bulb> bulbs;
440440

441-
const std::map<Bulb::Hash, std::optional<geometry_msgs::msg::Point>> positions;
441+
const std::map<Color, std::optional<geometry_msgs::msg::Point>> positions;
442442

443443
auto clear() { bulbs.clear(); }
444444

@@ -454,31 +454,45 @@ struct TrafficLight
454454
template <typename Markers, typename Now>
455455
auto draw(Markers & markers, const Now & now, const std::string & frame_id) const
456456
{
457-
auto optional_position = [this](auto && bulb) {
458-
try {
459-
return positions.at(bulb.hash() & 0b1111'0000'1111'1111); // NOTE: Ignore status
460-
} catch (const std::out_of_range &) {
461-
return std::optional<geometry_msgs::msg::Point>(std::nullopt);
462-
}
457+
auto create_bulb_marker = [&](const Color & color, const bool is_on) {
458+
visualization_msgs::msg::Marker marker;
459+
marker.header.stamp = now;
460+
marker.header.frame_id = frame_id;
461+
marker.action = visualization_msgs::msg::Marker::ADD;
462+
marker.ns = "bulb";
463+
marker.id = way_id << 2 | static_cast<int>(color.value);
464+
marker.type = visualization_msgs::msg::Marker::SPHERE;
465+
marker.pose.position = positions.at(color).value();
466+
marker.pose.orientation = geometry_msgs::msg::Quaternion();
467+
marker.scale.x = 0.3;
468+
marker.scale.y = 0.3;
469+
marker.scale.z = 0.3;
470+
marker.color = color_names::makeColorMsg(boost::lexical_cast<std::string>(color));
471+
marker.color.a = is_on ? 1.0 : 0.3;
472+
return marker;
463473
};
464474

475+
std::set<Color> added_colors;
476+
477+
// Place on markers for actual bulbs
465478
for (const auto & bulb : bulbs) {
466-
if (optional_position(bulb).has_value() and bulb.is(Shape::Category::circle)) {
467-
visualization_msgs::msg::Marker marker;
468-
marker.header.stamp = now;
469-
marker.header.frame_id = frame_id;
470-
marker.action = marker.ADD;
471-
marker.ns = "bulb";
472-
marker.id = way_id;
473-
marker.type = marker.SPHERE;
474-
marker.pose.position = optional_position(bulb).value();
475-
marker.pose.orientation = geometry_msgs::msg::Quaternion();
476-
marker.scale.x = 0.3;
477-
marker.scale.y = 0.3;
478-
marker.scale.z = 0.3;
479-
marker.color =
480-
color_names::makeColorMsg(boost::lexical_cast<std::string>(std::get<Color>(bulb.value)));
481-
markers.push_back(marker);
479+
// NOTE: Status is ignored intentionally
480+
if (bulb.is(Shape::Category::circle)) {
481+
const auto color = std::get<Color>(bulb.value);
482+
const auto position = positions.find(color);
483+
if (position == positions.end() or not position->second.has_value()) {
484+
continue;
485+
}
486+
487+
markers.push_back(create_bulb_marker(color, true));
488+
added_colors.insert(color);
489+
}
490+
}
491+
492+
// Place solidOff markers for other positions
493+
for (const auto & [color, position] : positions) {
494+
if (position.has_value() and added_colors.find(color) == added_colors.end()) {
495+
markers.push_back(create_bulb_marker(color, false));
482496
}
483497
}
484498
}

simulation/traffic_simulator/include/traffic_simulator/traffic_lights/traffic_lights.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ class ConventionalTrafficLights : public TrafficLightsBase
4545
~ConventionalTrafficLights() override = default;
4646

4747
private:
48-
auto update() const -> void
48+
auto update() const -> void override
4949
{
5050
backward_compatible_publisher_ptr_->publish(
5151
clock_ptr_->now(), generateUpdateTrafficLightsRequest());

simulation/traffic_simulator/src/hdmap_utils/hdmap_utils.cpp

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -705,8 +705,8 @@ auto HdMapUtils::getTrafficLightIds() const -> lanelet::Ids
705705
}
706706

707707
auto HdMapUtils::getTrafficLightBulbPosition(
708-
const lanelet::Id traffic_light_id, const std::string & color_name) const
709-
-> std::optional<geometry_msgs::msg::Point>
708+
const lanelet::Id traffic_light_id, const std::string & color_name,
709+
const bool allow_infer_position) const -> std::optional<geometry_msgs::msg::Point>
710710
{
711711
lanelet::ConstLanelets all_lanelets = lanelet::utils::query::laneletLayer(lanelet_map_ptr_);
712712
auto autoware_traffic_lights = lanelet::utils::query::autowareTrafficLights(all_lanelets);
@@ -737,6 +737,39 @@ auto HdMapUtils::getTrafficLightBulbPosition(
737737
}
738738
}
739739
}
740+
741+
if (!allow_infer_position) {
742+
return std::nullopt;
743+
}
744+
745+
// In case of a traffic light without bulbs, we can check the base string
746+
// to get the position of the traffic light
747+
const auto position_scale = color_name == "red" ? 0.25 : (color_name == "yellow" ? 0.5 : 0.75);
748+
for (const auto & light : autoware_traffic_lights) {
749+
for (auto & traffic_light : light->trafficLights()) {
750+
if (traffic_light.id() != traffic_light_id) continue;
751+
if (!traffic_light.isLineString()) continue;
752+
const auto base_string = static_cast<lanelet::ConstLineString3d>(traffic_light);
753+
if (!base_string.hasAttribute("height")) continue;
754+
const auto height = base_string.attribute("height").asDouble();
755+
if (!height) continue;
756+
757+
const auto x1 = base_string.front().x();
758+
const auto y1 = base_string.front().y();
759+
const auto z1 = base_string.front().z();
760+
761+
const auto x2 = base_string.back().x();
762+
const auto y2 = base_string.back().y();
763+
764+
geometry_msgs::msg::Point point;
765+
point.x = x1 * position_scale + x2 * (1 - position_scale);
766+
point.y = y1 * position_scale + y2 * (1 - position_scale);
767+
point.z = z1 + *height / 2.0;
768+
769+
return point;
770+
}
771+
}
772+
740773
return std::nullopt;
741774
}
742775

simulation/traffic_simulator/src/traffic_lights/traffic_light.cpp

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -181,15 +181,9 @@ TrafficLight::TrafficLight(
181181
}()),
182182
regulatory_elements_ids(hdmap_utils.getTrafficLightRegulatoryElementIDsFromTrafficLight(way_id)),
183183
positions{
184-
std::make_pair(
185-
Bulb(Color::green, Status::solid_on, Shape::circle).hash(),
186-
hdmap_utils.getTrafficLightBulbPosition(way_id, "green")),
187-
std::make_pair(
188-
Bulb(Color::yellow, Status::solid_on, Shape::circle).hash(),
189-
hdmap_utils.getTrafficLightBulbPosition(way_id, "yellow")),
190-
std::make_pair(
191-
Bulb(Color::red, Status::solid_on, Shape::circle).hash(),
192-
hdmap_utils.getTrafficLightBulbPosition(way_id, "red")),
184+
std::make_pair(Color::green, hdmap_utils.getTrafficLightBulbPosition(way_id, "green", true)),
185+
std::make_pair(Color::yellow, hdmap_utils.getTrafficLightBulbPosition(way_id, "yellow", true)),
186+
std::make_pair(Color::red, hdmap_utils.getTrafficLightBulbPosition(way_id, "red", true)),
193187
}
194188
{
195189
}

0 commit comments

Comments
 (0)