CARLA
Toggle main menu visibility
Loading...
Searching...
No Matches
Unreal
CarlaUnreal
Plugins
Carla
Source
Carla
MapGen
GraphParser.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 "
GraphParser.h
"
8
#include "
Carla.h
"
9
10
#include "
DoublyConnectedEdgeList.h
"
11
12
#include <type_traits>
13
#include <unordered_set>
14
15
namespace
MapGen
{
16
17
using
Graph
=
DoublyConnectedEdgeList
;
18
19
// ===========================================================================
20
// -- Local static methods ---------------------------------------------------
21
// ===========================================================================
22
23
static
int
getQuadrant
(
float
angle) {
24
return
static_cast<
int
>
(std::round(angle/HALF_PI));
25
}
26
27
// Assumes angles are separated by half pi approx.
28
static
float
getRotation
(
float
angle0,
float
angle1) {
29
const
int
min
=
getQuadrant
(std::min(angle0, angle1));
30
const
int
max =
getQuadrant
(std::max(angle0, angle1));
31
return
HALF_PI * std::min(
min
,
min
* max);
32
}
33
34
// Assumes angles are separated by half pi approx.
35
static
float
getRotation
(
float
angle0,
float
angle1,
float
angle2) {
36
/// @todo There has to be a better way.
37
switch
(
getQuadrant
(angle0) +
getQuadrant
(angle1) +
getQuadrant
(angle2)) {
38
case
0:
39
return
HALF_PI;
40
case
1:
41
return
0.0;
42
case
2:
43
return
-1.0 * HALF_PI;
44
case
3:
45
return
PI;
46
default
:
47
UE_LOG(LogCarla, Error, TEXT(
"Wrong quadrants"
));
48
return
0.0;
49
}
50
}
51
52
/// @todo This can probably be done at graph creation.
53
static
void
fixGraphData
(
Graph
&graph) {
54
// Set the edge count for each node in the graph.
55
for
(
auto
&node : graph.
GetNodes
()) {
56
std::vector<float> angles;
57
angles.reserve(4u);
58
// Iterate every half-edge in this node.
59
auto
&firstEdge =
Graph::GetLeavingHalfEdge
(node);
60
auto
*edge = &firstEdge;
61
do
{
62
edge->Angle =
Graph::GetAngle
(*edge);
63
angles.emplace_back(edge->Angle);
64
edge = &
Graph::GetNextInNode
(*edge);
65
}
while
(edge != &firstEdge);
66
check(!angles.empty());
67
node.EdgeCount = angles.size();
68
node.bIsIntersection =
true
;
69
switch
(node.EdgeCount) {
70
case
2:
71
node.Rotation =
getRotation
(angles[0u], angles[1u]);
72
node.IntersectionType =
EIntersectionType::Turn90Deg
;
73
break
;
74
case
3:
75
node.Rotation =
getRotation
(angles[0u], angles[1u], angles[2u]);
76
node.IntersectionType =
EIntersectionType::TIntersection
;
77
break
;
78
case
4:
79
default
:
80
node.Rotation = 0.0;
81
node.IntersectionType =
EIntersectionType::XIntersection
;
82
break
;
83
}
84
node.Rots.swap(angles);
85
}
86
}
87
88
// ===========================================================================
89
// -- RoadSegmentBuilder -----------------------------------------------------
90
// ===========================================================================
91
92
class
RoadSegmentBuilder
{
93
public
:
94
95
std::vector<TUniquePtr<RoadSegmentDescription>>
Segments
;
96
97
explicit
RoadSegmentBuilder
(
const
Graph
&graph) :
_graph
(graph) {}
98
99
void
Add
(
Graph::HalfEdge
&edge) {
100
if
(!
insert
(edge))
101
return
;
102
if
(
Graph::GetSource
(edge).bIsIntersection) {
103
Segments
.emplace_back(MakeUnique<RoadSegmentDescription>());
104
_handlingInitial
=
false
;
105
}
106
if
(
_handlingInitial
) {
107
_initial
.emplace_back(&edge);
108
}
else
{
109
Segments
.back()->Add(edge);
110
}
111
}
112
113
void
Close
() {
114
for
(
auto
edge :
_initial
) {
115
Segments
.back()->Add(*edge);
116
}
117
_handlingInitial
=
true
;
118
}
119
120
private
:
121
122
/// Insert both half-edges only if they haven't been visited yet.
123
bool
insert
(
Graph::HalfEdge
&edge) {
124
return
_visitedEdges
.insert(&edge).second &&
125
_visitedEdges
.insert(&
Graph::GetPair
(edge)).second;
126
}
127
128
const
Graph
&
_graph
;
129
130
std::unordered_set<const Graph::HalfEdge *>
_visitedEdges
;
131
132
bool
_handlingInitial
=
true
;
133
134
std::vector<const Graph::HalfEdge *>
_initial
;
135
};
136
137
// ===========================================================================
138
// -- GraphParser ------------------------------------------------------------
139
// ===========================================================================
140
141
GraphParser::GraphParser
(
DoublyConnectedEdgeList
&graph) {
142
check(graph.
CountNodes
() >= 4u);
143
check(graph.
CountHalfEdges
() >= 8u);
144
check(graph.
CountFaces
() >= 2u);
145
146
fixGraphData
(graph);
147
148
CityAreas
.reserve(graph.
CountFaces
() - 1);
149
150
RoadSegmentBuilder
rsb(graph);
151
152
auto
faceList = graph.
GetFaces
();
153
auto
it = faceList.begin();
154
++it;
// Ignore first face (unbounded).
155
for
(; it != faceList.end(); ++it) {
156
CityAreas
.emplace_back(MakeUnique<CityAreaDescription>(*it));
157
CityAreaDescription
&cityArea = *
CityAreas
.back();
158
// Iterate every half-edge in this face.
159
auto
&firstEdge =
Graph::GetHalfEdge
(*it);
160
for
(
auto
*edge = &
Graph::GetNextInFace
(firstEdge);
161
edge != &firstEdge;
162
edge = &
Graph::GetNextInFace
(*edge)) {
163
cityArea.
Add
(
Graph::GetSource
(*edge));
164
rsb.
Add
(*edge);
165
}
166
rsb.
Close
();
167
}
168
169
RoadSegments
.swap(rsb.
Segments
);
170
}
171
172
}
// namespace MapGen
Carla.h
DoublyConnectedEdgeList.h
GraphParser.h
min
double min(double v1, double v2)
Definition
Simplify.h:285
MapGen::CityAreaDescription
Definition
CityAreaDescription.h:16
MapGen::CityAreaDescription::Add
void Add(const GraphNode &Node)
Definition
CityAreaDescription.h:21
MapGen::DoublyConnectedEdgeList
Simple doubly-connected edge list structure.
Definition
DoublyConnectedEdgeList.h:21
MapGen::DoublyConnectedEdgeList::GetFaces
ListView< FaceIterator > GetFaces()
Definition
DoublyConnectedEdgeList.h:193
MapGen::DoublyConnectedEdgeList::GetAngle
static float GetAngle(const HalfEdge &halfEdge)
Return the angle [-pi, pi] of the half-edge.
Definition
DoublyConnectedEdgeList.cpp:250
MapGen::DoublyConnectedEdgeList::GetNodes
ListView< NodeIterator > GetNodes()
Definition
DoublyConnectedEdgeList.h:173
MapGen::DoublyConnectedEdgeList::GetHalfEdge
static HalfEdge & GetHalfEdge(Face &face)
Definition
DoublyConnectedEdgeList.h:272
MapGen::DoublyConnectedEdgeList::GetLeavingHalfEdge
static HalfEdge & GetLeavingHalfEdge(Node &node)
Definition
DoublyConnectedEdgeList.h:260
MapGen::DoublyConnectedEdgeList::GetPair
static HalfEdge & GetPair(HalfEdge &halfEdge)
Definition
DoublyConnectedEdgeList.h:236
MapGen::DoublyConnectedEdgeList::GetSource
static Node & GetSource(HalfEdge &halfEdge)
Definition
DoublyConnectedEdgeList.h:212
MapGen::DoublyConnectedEdgeList::CountNodes
size_t CountNodes() const
Definition
DoublyConnectedEdgeList.h:151
MapGen::DoublyConnectedEdgeList::GetNextInNode
static HalfEdge & GetNextInNode(HalfEdge &halfEdge)
Definition
DoublyConnectedEdgeList.h:298
MapGen::DoublyConnectedEdgeList::GetNextInFace
static HalfEdge & GetNextInFace(HalfEdge &halfEdge)
Definition
DoublyConnectedEdgeList.h:286
MapGen::DoublyConnectedEdgeList::CountHalfEdges
size_t CountHalfEdges() const
Definition
DoublyConnectedEdgeList.h:156
MapGen::DoublyConnectedEdgeList::CountFaces
size_t CountFaces() const
Definition
DoublyConnectedEdgeList.h:161
MapGen::GraphParser::RoadSegments
RoadSegmentList RoadSegments
Definition
GraphParser.h:67
MapGen::GraphParser::GraphParser
GraphParser(DoublyConnectedEdgeList &Dcel)
Definition
GraphParser.cpp:141
MapGen::GraphParser::CityAreas
CityAreaList CityAreas
Definition
GraphParser.h:69
MapGen::RoadSegmentBuilder
Definition
GraphParser.cpp:92
MapGen::RoadSegmentBuilder::RoadSegmentBuilder
RoadSegmentBuilder(const Graph &graph)
Definition
GraphParser.cpp:97
MapGen::RoadSegmentBuilder::_visitedEdges
std::unordered_set< const Graph::HalfEdge * > _visitedEdges
Definition
GraphParser.cpp:130
MapGen::RoadSegmentBuilder::Add
void Add(Graph::HalfEdge &edge)
Definition
GraphParser.cpp:99
MapGen::RoadSegmentBuilder::_initial
std::vector< const Graph::HalfEdge * > _initial
Definition
GraphParser.cpp:134
MapGen::RoadSegmentBuilder::Segments
std::vector< TUniquePtr< RoadSegmentDescription > > Segments
Definition
GraphParser.cpp:95
MapGen::RoadSegmentBuilder::_graph
const Graph & _graph
Definition
GraphParser.cpp:128
MapGen::RoadSegmentBuilder::Close
void Close()
Definition
GraphParser.cpp:113
MapGen::RoadSegmentBuilder::_handlingInitial
bool _handlingInitial
Definition
GraphParser.cpp:132
MapGen::RoadSegmentBuilder::insert
bool insert(Graph::HalfEdge &edge)
Insert both half-edges only if they haven't been visited yet.
Definition
GraphParser.cpp:123
MapGen
Definition
CityAreaDescription.h:13
MapGen::EIntersectionType::XIntersection
@ XIntersection
Definition
CityMapDefinitions.h:14
MapGen::EIntersectionType::TIntersection
@ TIntersection
Definition
CityMapDefinitions.h:13
MapGen::EIntersectionType::Turn90Deg
@ Turn90Deg
Definition
CityMapDefinitions.h:12
MapGen::getQuadrant
static int getQuadrant(float angle)
Definition
GraphParser.cpp:23
MapGen::fixGraphData
static void fixGraphData(Graph &graph)
Definition
GraphParser.cpp:53
MapGen::Graph
DoublyConnectedEdgeList Graph
Definition
GraphGenerator.cpp:14
MapGen::getRotation
static float getRotation(float angle0, float angle1)
Definition
GraphParser.cpp:28
MapGen::DoublyConnectedEdgeList::HalfEdge
Definition
DoublyConnectedEdgeList.h:53
Generated by
1.18.0