CARLA
Loading...
Searching...
No Matches
client/LaneSection.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
8
9#include "carla/client/Lane.h"
10#include "carla/client/Map.h"
11#include "carla/client/Road.h"
13#include "carla/road/Road.h"
14
15#include <algorithm>
16#include <vector>
17
18namespace carla {
19namespace client {
20
21 // Sections ordered by their s coordinate, so "next" and "previous" mean
22 // along the road rather than by id.
23 static std::vector<const road::LaneSection *> SectionsByDistance(const road::Road &road) {
24 std::vector<const road::LaneSection *> sections;
25 for (const road::LaneSection &section : road.GetLaneSections()) {
26 sections.emplace_back(&section);
27 }
28 std::sort(sections.begin(), sections.end(),
29 [](const road::LaneSection *lhs, const road::LaneSection *rhs) {
30 return lhs->GetDistance() < rhs->GetDistance();
31 });
32 return sections;
33 }
34
35 uint64_t LaneSection::GetUniqueId() const {
36 return (static_cast<uint64_t>(_road_id) << 32) ^ static_cast<uint64_t>(_section_id);
37 }
38
40 return _parent->GetRoad(_road_id);
41 }
42
43 double LaneSection::GetDistance() const {
44 return _parent->GetMap().GetData().GetRoad(_road_id)
45 .GetLaneSectionById(_section_id).GetDistance();
46 }
47
48 double LaneSection::GetLength() const {
49 return _parent->GetMap().GetData().GetRoad(_road_id)
50 .GetLaneSectionById(_section_id).GetLength();
51 }
52
53 std::vector<SharedPtr<Lane>> LaneSection::GetLanes(road::Lane::LaneType type) const {
54 std::vector<SharedPtr<Lane>> result;
55 const road::LaneSection &section =
57 for (const auto &pair : section.GetLanes()) {
58 const road::Lane &lane = pair.second;
59 // The centre lane has no width and cannot be driven or walked on.
60 if (lane.GetId() == 0) {
61 continue;
62 }
63 if ((static_cast<int32_t>(lane.GetType()) & static_cast<int32_t>(type)) == 0) {
64 continue;
65 }
66 result.emplace_back(_parent->GetLane(_road_id, _section_id, lane.GetId()));
67 }
68 return result;
69 }
70
72 return _parent->GetLane(_road_id, _section_id, lane_id);
73 }
74
76 return _parent->GetMap().GetData().GetRoad(_road_id)
78 }
79
81 const road::Road &road = _parent->GetMap().GetData().GetRoad(_road_id);
82 const auto sections = SectionsByDistance(road);
83 for (size_t i = 0u; i < sections.size(); ++i) {
84 if (sections[i]->GetId() == _section_id) {
85 if (i + 1u < sections.size()) {
86 return _parent->GetLaneSection(_road_id, sections[i + 1u]->GetId());
87 }
88 break;
89 }
90 }
91 return nullptr;
92 }
93
95 const road::Road &road = _parent->GetMap().GetData().GetRoad(_road_id);
96 const auto sections = SectionsByDistance(road);
97 for (size_t i = 0u; i < sections.size(); ++i) {
98 if (sections[i]->GetId() == _section_id) {
99 if (i > 0u) {
100 return _parent->GetLaneSection(_road_id, sections[i - 1u]->GetId());
101 }
102 break;
103 }
104 }
105 return nullptr;
106 }
107
108} // namespace client
109} // namespace carla
std::map< std::string, unsigned int > road
double GetDistance() const
The s coordinate, along the road, at which this section begins.
bool ContainsLane(road::LaneId lane_id) const
SharedPtr< Road > GetRoad() const
road::SectionId GetId() const
SharedPtr< LaneSection > GetNextSection() const
The next section along the road, or nullptr at the end.
std::vector< SharedPtr< Lane > > GetLanes(road::Lane::LaneType type=road::Lane::LaneType::Driving) const
Every lane in this section of the given type.
SharedPtr< const Map > _parent
SharedPtr< Lane > GetLane(road::LaneId lane_id) const
The lane with this id, or nullptr when the section has no such lane.
SharedPtr< LaneSection > GetPreviousSection() const
The previous section along the road, or nullptr at the start.
uint64_t GetUniqueId() const
Unique id of this section within its map, for use as a dictionary key.
double GetLength() const
Length of the section, in metres.
bool ContainsLane(LaneId id) const
std::map< LaneId, Lane > & GetLanes()
Road * GetRoad() const
Definition road/Lane.cpp:29
LaneType GetType() const
Definition road/Lane.cpp:38
LaneType
Can be used as flags.
Definition road/Lane.h:29
LaneId GetId() const
Definition road/Lane.cpp:34
LaneSection & GetLaneSectionById(SectionId id)
Definition road/Road.h:172
static std::vector< const road::LaneSection * > SectionsByDistance(const road::Road &road)
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