CARLA
Loading...
Searching...
No Matches
client/Lane.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/Lane.h"
8
10#include "carla/client/Map.h"
11#include "carla/client/Road.h"
14#include "carla/road/Road.h"
16
17#include <cmath>
18
19namespace carla {
20namespace client {
21
22 // Stepping away from the centre line: -1 -> -2, 1 -> 2.
24 return lane_id > 0 ? lane_id + 1 : lane_id - 1;
25 }
26
27 // Stepping toward the centre line. At +-1 the next lane across is on the
28 // other side of the road, so the sign flips and the neighbour runs the
29 // opposite way. road::Map::GetLeft does exactly this; kept identical here on
30 // purpose so Lane and Waypoint never disagree about who is to the left.
32 if (std::abs(lane_id) == 1) {
33 return -lane_id;
34 }
35 return lane_id > 0 ? lane_id - 1 : lane_id + 1;
36 }
37
38 const road::Lane &Lane::GetLane() const {
39 return _parent->GetMap().GetData().GetRoad(_road_id).GetLaneById(_section_id, _lane_id);
40 }
41
42 uint64_t Lane::GetUniqueId() const {
43 // road id and section id are unsigned, lane id is signed and small; pack
44 // them so the value is stable and collision-free within one map.
45 return (static_cast<uint64_t>(_road_id) << 32) ^
46 (static_cast<uint64_t>(_section_id) << 16) ^
47 static_cast<uint64_t>(static_cast<uint16_t>(_lane_id));
48 }
49
51 return GetLane().GetType();
52 }
53
55 return _parent->GetRoad(_road_id);
56 }
57
59 return _parent->GetLaneSection(_road_id, _section_id);
60 }
61
62 double Lane::GetLength() const {
63 return GetLane().GetLength();
64 }
65
66 double Lane::GetDistance() const {
67 return GetLane().GetDistance();
68 }
69
70 bool Lane::GetLevel() const {
71 return GetLane().GetLevel();
72 }
73
74 bool Lane::IsStraight() const {
75 return GetLane().IsStraight();
76 }
77
80 }
81
82 double Lane::GetWidth(double s) const {
83 return GetLane().GetWidth(s);
84 }
85
87 return GetLane().ComputeTransform(s);
88 }
89
91 if (_lane_id == 0) {
92 return nullptr;
93 }
94 const bool is_rht = GetLane().GetRoad()->IsRHT();
95 const road::LaneId neighbour = is_rht ? StepOutward(_lane_id) : StepInward(_lane_id);
96 return _parent->GetLane(_road_id, _section_id, neighbour);
97 }
98
100 if (_lane_id == 0) {
101 return nullptr;
102 }
103 const bool is_rht = GetLane().GetRoad()->IsRHT();
104 const road::LaneId neighbour = is_rht ? StepInward(_lane_id) : StepOutward(_lane_id);
105 return _parent->GetLane(_road_id, _section_id, neighbour);
106 }
107
108 std::vector<SharedPtr<Lane>> Lane::GetNextLanes() const {
109 std::vector<SharedPtr<Lane>> result;
110 for (const road::Lane *lane : GetLane().GetNextLanes()) {
111 if (lane == nullptr || lane->GetLaneSection() == nullptr || lane->GetRoad() == nullptr) {
112 continue;
113 }
114 result.emplace_back(_parent->GetLane(
115 lane->GetRoad()->GetId(), lane->GetLaneSection()->GetId(), lane->GetId()));
116 }
117 return result;
118 }
119
120 std::vector<SharedPtr<Lane>> Lane::GetPreviousLanes() const {
121 std::vector<SharedPtr<Lane>> result;
122 for (const road::Lane *lane : GetLane().GetPreviousLanes()) {
123 if (lane == nullptr || lane->GetLaneSection() == nullptr || lane->GetRoad() == nullptr) {
124 continue;
125 }
126 result.emplace_back(_parent->GetLane(
127 lane->GetRoad()->GetId(), lane->GetLaneSection()->GetId(), lane->GetId()));
128 }
129 return result;
130 }
131
133 const road::Lane &lane = GetLane();
134 const double start = lane.GetDistance();
135 const double end = start + lane.GetLength();
136 if (s < start || s > end) {
137 return nullptr;
138 }
139 return _parent->MakeWaypoint(
141 }
142
143 std::vector<SharedPtr<Waypoint>> Lane::GetWaypoints(double distance) const {
144 std::vector<SharedPtr<Waypoint>> result;
145 if (distance <= 0.0) {
146 return result;
147 }
148 const road::Lane &lane = GetLane();
149 const double start = lane.GetDistance();
150 const double end = start + lane.GetLength();
151 for (double s = start; s < end; s += distance) {
152 result.emplace_back(_parent->MakeWaypoint(
153 road::element::Waypoint{_road_id, _section_id, _lane_id, s}));
154 }
155 return result;
156 }
157
158} // namespace client
159} // namespace carla
float distance
SharedPtr< Lane > GetRightLane() const
The lane to the right, or nullptr at the edge of the road.
road::LaneId _lane_id
road::Lane::LaneType GetType() const
double GetDistance() const
The s coordinate, along the road, at which this lane's section begins.
std::vector< SharedPtr< Waypoint > > GetWaypoints(double distance) const
Waypoints along this lane, every distance metres.
SharedPtr< const Map > _parent
road::SectionId _section_id
double GetLength() const
Length of the lane, in metres.
bool IsStraight() const
bool GetLevel() const
SharedPtr< Road > GetRoad() const
road::RoadId _road_id
SharedPtr< LaneSection > GetSection() const
std::vector< SharedPtr< Lane > > GetPreviousLanes() const
Lanes leading into this one.
SharedPtr< Lane > GetLeftLane() const
The lane to the left, or nullptr. See GetRightLane() on direction.
std::vector< SharedPtr< Lane > > GetNextLanes() const
Lanes this one leads into. Empty where the lane ends.
double GetWidth(double s) const
Lane width at the given s, in metres.
uint64_t GetUniqueId() const
Unique id of this lane within its map, for use as a dictionary key.
bool IsPositiveDirection() const
Whether the lane runs along increasing s (a right-hand lane, id < 0).
SharedPtr< Waypoint > GetWaypoint(double s) const
The waypoint on this lane at road coordinate s, or nullptr when s falls outside the lane.
geom::Transform GetTransform(double s) const
const road::Lane & GetLane() const
The underlying OpenDRIVE lane. Valid only while _parent is alive.
A rigid transform in CARLA's / Unreal's left-handed frame: x forward, y right, z up,...
bool IsStraight() const
Checks whether the geometry is straight or not.
Definition road/Lane.cpp:67
Road * GetRoad() const
Definition road/Lane.cpp:29
const std::vector< Lane * > & GetNextLanes() const
Definition road/Lane.h:90
LaneType GetType() const
Definition road/Lane.cpp:38
double GetWidth(const double s) const
Returns the total lane width given a s.
Definition road/Lane.cpp:58
geom::Transform ComputeTransform(const double s) const
LaneType
Can be used as flags.
Definition road/Lane.h:29
bool GetLevel() const
Definition road/Lane.cpp:42
double GetDistance() const
Definition road/Lane.cpp:46
const std::vector< Lane * > & GetPreviousLanes() const
Definition road/Lane.h:94
bool IsPositiveDirection() const
double GetLength() const
Definition road/Lane.cpp:51
Lane & GetLaneById(SectionId section_id, LaneId lane_id)
bool IsRHT() const
Definition road/Road.cpp:52
static road::LaneId StepInward(road::LaneId lane_id)
static road::LaneId StepOutward(road::LaneId lane_id)
int32_t LaneId
Definition RoadTypes.h:19
This file contains definitions of common data structures used in traffic manager.
std::shared_ptr< T > SharedPtr
Definition Memory.h:15