CARLA
Loading...
Searching...
No Matches
client/Road.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
7#include "carla/client/Road.h"
8
9#include "carla/geom/Math.h"
10
12#include "carla/client/Lane.h"
14#include "carla/client/Map.h"
17#include "carla/road/Road.h"
19
20#include <unordered_set>
21
22namespace carla {
23namespace client {
24
26 return parent->GetMap().GetData().GetRoad(road_id);
27 }
28
29 std::string Road::GetName() const {
31 }
32
33 double Road::GetLength() const {
35 }
36
37 bool Road::IsJunction() const {
39 }
40
44
45 bool Road::IsRHT() const {
47 }
48
52
56
57 std::vector<SharedPtr<Road>> Road::GetNextRoads() const {
58 std::vector<SharedPtr<Road>> result;
59 std::unordered_set<road::RoadId> seen;
61 if (road == nullptr || !seen.insert(road->GetId()).second) {
62 continue;
63 }
64 result.emplace_back(_parent->GetRoad(road->GetId()));
65 }
66 return result;
67 }
68
69 std::vector<SharedPtr<Road>> Road::GetPreviousRoads() const {
70 std::vector<SharedPtr<Road>> result;
71 std::unordered_set<road::RoadId> seen;
73 if (road == nullptr || !seen.insert(road->GetId()).second) {
74 continue;
75 }
76 result.emplace_back(_parent->GetRoad(road->GetId()));
77 }
78 return result;
79 }
80
81 std::vector<SharedPtr<Lane>> Road::GetLanes(double s, road::Lane::LaneType type) const {
82 std::vector<SharedPtr<Lane>> result;
84 for (const road::Lane *lane : road.GetLanesByDistance(s)) {
85 if (lane == nullptr || lane->GetId() == 0 || lane->GetLaneSection() == nullptr) {
86 continue;
87 }
88 if ((static_cast<int32_t>(lane->GetType()) & static_cast<int32_t>(type)) == 0) {
89 continue;
90 }
91 result.emplace_back(_parent->GetLane(
92 _road_id, lane->GetLaneSection()->GetId(), lane->GetId()));
93 }
94 return result;
95 }
96
98 return _parent->GetLane(_road_id, section_id, lane_id);
99 }
100
103 if (section == nullptr) {
104 return nullptr;
105 }
106 return _parent->GetLane(_road_id, section->GetId(), lane_id);
107 }
108
109 std::vector<SharedPtr<LaneSection>> Road::GetSections() const {
110 std::vector<SharedPtr<LaneSection>> result;
112 result.emplace_back(_parent->GetLaneSection(_road_id, section.GetId()));
113 }
114 return result;
115 }
116
118 return _parent->GetLaneSection(_road_id, section_id);
119 }
120
123 // GetLaneSectionsAt yields every section starting at or before s; the last
124 // is the one that actually covers it.
125 const road::LaneSection *covering = nullptr;
126 for (const road::LaneSection &section : road.GetLaneSectionsAt(s)) {
127 covering = &section;
128 }
129 if (covering == nullptr) {
130 return nullptr;
131 }
132 return _parent->GetLaneSection(_road_id, covering->GetId());
133 }
134
136 if (!IsJunction()) {
137 return nullptr;
138 }
139 return _parent->GetJunctionById(GetJunctionId());
140 }
141
143 auto directed_point = GetRoadData(_parent, _road_id).GetDirectedPointIn(s);
144 // Same Unreal Y-axis flip road::Lane::ComputeTransform applies; without it
145 // the transform comes back in the OpenDRIVE frame and disagrees with every
146 // waypoint on the same road.
147 directed_point.location.y *= -1.0f;
148 directed_point.tangent *= -1.0;
149 return geom::Transform(
150 directed_point.location,
152 geom::Math::ToDegrees(static_cast<float>(directed_point.pitch)),
153 geom::Math::ToDegrees(static_cast<float>(directed_point.tangent)),
154 0.0f));
155 }
156
157 std::vector<SharedPtr<Waypoint>> Road::GetWaypointsAt(
158 double s,
159 road::Lane::LaneType type) const {
160 std::vector<SharedPtr<Waypoint>> result;
161 for (const SharedPtr<Lane> &lane : GetLanes(s, type)) {
162 SharedPtr<Waypoint> waypoint = lane->GetWaypoint(s);
163 if (waypoint != nullptr) {
164 result.emplace_back(std::move(waypoint));
165 }
166 }
167 return result;
168 }
169
170 std::vector<SharedPtr<Waypoint>> Road::GetWaypoints(
171 double distance,
172 road::Lane::LaneType type) const {
173 std::vector<SharedPtr<Waypoint>> result;
174 if (distance <= 0.0) {
175 return result;
176 }
177 const double length = GetLength();
178 for (double s = 0.0; s < length; s += distance) {
179 for (SharedPtr<Waypoint> &waypoint : GetWaypointsAt(s, type)) {
180 result.emplace_back(std::move(waypoint));
181 }
182 }
183 return result;
184 }
185
186} // namespace client
187} // namespace carla
float distance
std::string road_id
Definition LaneGraph.cpp:39
std::map< std::string, unsigned int > road
std::vector< SharedPtr< LaneSection > > GetSections() const
std::vector< SharedPtr< Waypoint > > GetWaypointsAt(double s, road::Lane::LaneType type=road::Lane::LaneType::Driving) const
A cross-section: one waypoint per lane of the given type at s.
road::JuncId GetJunctionId() const
Id of the junction this road belongs to, or -1 when it is not part of one.
std::vector< SharedPtr< Waypoint > > GetWaypoints(double distance, road::Lane::LaneType type=road::Lane::LaneType::Driving) const
Waypoints sampled along the road every distance metres, on every lane of the given type.
SharedPtr< const Map > _parent
SharedPtr< Junction > GetJunction() const
The junction this road belongs to, or nullptr when it is not part of one.
geom::Transform GetTransformAt(double s) const
Transform at the centre of the road at road coordinate s.
bool IsJunction() const
std::vector< SharedPtr< Lane > > GetLanes(double s, road::Lane::LaneType type=road::Lane::LaneType::Driving) const
Every lane of the given type present at road coordinate s, across every lane section covering it.
bool IsRHT() const
Whether traffic on this road drives on the right.
std::vector< SharedPtr< Road > > GetPreviousRoads() const
Roads that lead into this one.
road::RoadId GetSuccessorId() const
Id of the road that follows this one, as recorded in OpenDRIVE.
road::RoadId _road_id
SharedPtr< LaneSection > GetSectionAt(double s) const
The lane section covering road coordinate s, or nullptr.
SharedPtr< Lane > GetLane(road::SectionId section_id, road::LaneId lane_id) const
double GetLength() const
Length of the road, in metres.
SharedPtr< LaneSection > GetSection(road::SectionId section_id) const
SharedPtr< Lane > GetLaneAt(double s, road::LaneId lane_id) const
The lane with this id in whichever section covers s.
std::string GetName() const
std::vector< SharedPtr< Road > > GetNextRoads() const
Roads reachable by continuing forward.
road::RoadId GetPredecessorId() const
Id of the road that precedes this one, as recorded in OpenDRIVE.
static constexpr T ToDegrees(T rad)
Definition Math.h:37
Euler angles of a rotation in CARLA's / Unreal's left-handed frame: x forward, y right,...
Definition Rotation.h:45
A rigid transform in CARLA's / Unreal's left-handed frame: x forward, y right, z up,...
LaneType
Can be used as flags.
Definition road/Lane.h:29
Road & GetRoad(const RoadId id)
Definition MapData.cpp:21
RoadId GetPredecessor() const
Definition road/Road.cpp:60
double GetLength() const
Definition road/Road.cpp:40
std::vector< Road * > GetPrevs() const
Definition road/Road.cpp:68
RoadId GetSuccessor() const
Definition road/Road.cpp:56
std::vector< Road * > GetNexts() const
Definition road/Road.cpp:64
auto GetLaneSections() const
Definition road/Road.h:136
bool IsRHT() const
Definition road/Road.cpp:52
element::DirectedPoint GetDirectedPointIn(const double s) const
Returns a directed point on the center of the road (lane 0), with the corresponding laneOffset and el...
JuncId GetJunctionId() const
Definition road/Road.cpp:48
std::string GetName() const
Definition road/Road.cpp:36
const MapData * GetMap() const
Definition road/Road.cpp:28
bool IsJunction() const
Definition road/Road.cpp:44
const road::Road & GetRoadData(const SharedPtr< const Map > &parent, road::RoadId road_id)
int32_t JuncId
Definition RoadTypes.h:17
uint32_t RoadId
Definition RoadTypes.h:15
int32_t LaneId
Definition RoadTypes.h:19
uint32_t SectionId
Definition RoadTypes.h:21
This file contains definitions of common data structures used in traffic manager.
std::shared_ptr< T > SharedPtr
Definition Memory.h:15