CARLA
Loading...
Searching...
No Matches
LaneCrossingCalculator.cpp
Go to the documentation of this file.
1// Copyright (c) 2026 Computer Vision Center (CVC) at the Universitat Autonoma
2// de Barcelona (UAB).
3//
4// This work is licensed under the terms of the MIT license.
5// For a copy, see <https://opensource.org/licenses/MIT>.
6
9
10#include "carla/geom/Location.h"
11#include "carla/road/Map.h"
12
13namespace carla {
14namespace road {
15namespace element {
16
17 /// @todo Temporary flags to search lanes where we can find road marks.
18 /// This needs to be expanded searching in shoulders too, but since
19 /// shouders have a small width, they can cause problems while finding
20 /// the nearest center of a lane given a location that are in a road,
21 /// but very close to a shoulder.
22 static constexpr uint32_t FLAGS =
23 static_cast<uint32_t>(Lane::LaneType::Driving) |
24 static_cast<uint32_t>(Lane::LaneType::Bidirectional) |
25 static_cast<uint32_t>(Lane::LaneType::Biking) |
26 static_cast<uint32_t>(Lane::LaneType::Parking);
27
28 /// Calculate the lane markings that need to be crossed from @a lane_id_origin
29 /// to @a lane_id_destination.
30 static std::vector<LaneMarking> CrossingAtSameSection(
31 const Map &map,
32 const Waypoint *w0,
33 const Waypoint *w1,
34 const bool w0_is_offroad,
35 const bool dest_is_at_right) {
36 auto w0_marks = map.GetMarkRecord(*w0);
37 auto w1_marks = map.GetMarkRecord(*w1);
38
39 if (dest_is_at_right) {
40 if (w0_is_offroad) {
41 if (w1_marks.second != nullptr) {
42 return { LaneMarking(*w1_marks.second) };
43 }
44 } else {
45 if (w0_marks.first != nullptr) {
46 return { LaneMarking(*w0_marks.first) };
47 }
48 }
49 } else {
50 if (w0_is_offroad) {
51 if (w1_marks.first != nullptr) {
52 return { LaneMarking(*w1_marks.first) };
53 }
54 } else {
55 if (w0_marks.second != nullptr) {
56 return { LaneMarking(*w0_marks.second) };
57 }
58 }
59 }
60
61 return {};
62 }
63
64 static bool IsOffRoad(const Map &map, const geom::Location &location) {
65 return !map.GetWaypoint(location, FLAGS).has_value();
66 }
67
68 std::vector<LaneMarking> LaneCrossingCalculator::Calculate(
69 const Map &map,
70 const geom::Location &origin,
71 const geom::Location &destination) {
72 auto w0 = map.GetClosestWaypointOnRoad(origin, FLAGS);
73 auto w1 = map.GetClosestWaypointOnRoad(destination, FLAGS);
74
75 if (!w0.has_value() || !w1.has_value()) {
76 return {};
77 }
78
79 if (w0->road_id != w1->road_id || w0->section_id != w1->section_id) {
80 /// @todo This case should also be handled.
81 return {};
82 }
83
84 if (map.IsJunction(w0->road_id) || map.IsJunction(w1->road_id)) {
85 return {};
86 }
87
88 const auto w0_is_offroad = IsOffRoad(map, origin);
89 const auto w1_is_offroad = IsOffRoad(map, destination);
90
91 if (w0_is_offroad && w1_is_offroad) {
92 // outside the road
93 return {};
94 }
95
96 if ((w0->lane_id == w1->lane_id) && !w0_is_offroad && !w1_is_offroad) {
97 // both at the same lane and inside the road
98 return {};
99 }
100
101 const auto transform = map.ComputeTransform(*w0);
102 geom::Vector3D orig_vec = transform.GetForwardVector();
103 geom::Vector3D dest_vec = (destination - origin).MakeUnitVector(2 * std::numeric_limits<float>::epsilon());
104
105 // cross product
106 const auto dest_is_at_right =
107 (-orig_vec.x * dest_vec.y + orig_vec.y * dest_vec.x) < 0;
108
110 map,
111 &*w0,
112 &*w1,
113 w0_is_offroad,
114 dest_is_at_right);
115 }
116
117} // namespace element
118} // namespace road
119} // namespace carla
std::map< std::string, unsigned int > road
bool IsJunction(RoadId road_id) const
Definition road/Map.cpp:307
std::pair< const element::RoadInfoMarkRecord *, const element::RoadInfoMarkRecord * > GetMarkRecord(Waypoint waypoint) const
Definition road/Map.cpp:312
std::optional< element::Waypoint > GetWaypoint(const geom::Location &location, int32_t lane_type=static_cast< int32_t >(Lane::LaneType::Driving)) const
Definition road/Map.cpp:217
std::optional< element::Waypoint > GetClosestWaypointOnRoad(const geom::Location &location, int32_t lane_type=static_cast< int32_t >(Lane::LaneType::Driving)) const
========================================================================
Definition road/Map.cpp:170
geom::Transform ComputeTransform(Waypoint waypoint) const
Definition road/Map.cpp:278
static std::vector< LaneMarking > Calculate(const Map &map, const geom::Location &origin, const geom::Location &destination)
static constexpr uint32_t FLAGS
static std::vector< LaneMarking > CrossingAtSameSection(const Map &map, const Waypoint *w0, const Waypoint *w1, const bool w0_is_offroad, const bool dest_is_at_right)
Calculate the lane markings that need to be crossed from lane_id_origin to lane_id_destination.
static bool IsOffRoad(const Map &map, const geom::Location &location)
This file contains definitions of common data structures used in traffic manager.