22 std::string MakeId(
const char *prefix, std::size_t index) {
23 std::ostringstream out;
24 out << prefix << index;
31 int ClassRank(
const std::string &highway) {
32 if (highway ==
"motorway" || highway ==
"motorway_link")
return 7;
33 if (highway ==
"trunk" || highway ==
"trunk_link")
return 6;
34 if (highway ==
"primary" || highway ==
"primary_link")
return 5;
35 if (highway ==
"secondary" || highway ==
"secondary_link")
return 4;
36 if (highway ==
"tertiary" || highway ==
"tertiary_link")
return 3;
37 if (highway ==
"unclassified" || highway ==
"residential")
return 2;
38 if (highway ==
"living_street")
return 1;
45 double limit,
double &out_s,
double &out_t) {
46 const Road *best =
nullptr;
47 double best_distance = limit;
48 for (
const Road &road : model.roads) {
49 if (road.junction_id.has_value() || road.reference_line.size() < 2u) {
56 if (distance < best_distance) {
57 best_distance = distance;
62 const double dx = geom::bg::get<0>(point) - geom::bg::get<0>(on);
63 const double dy = geom::bg::get<1>(point) - geom::bg::get<1>(on);
64 out_t = -std::sin(heading) * dx + std::cos(heading) * dy;
71 double s,
double t,
const geom::Point &position,
double heading,
72 std::optional<double> value,
long long node_id) {
74 signal.
id = MakeId(
"signal_", model.signals.size());
76 signal.road_id = road.id;
79 signal.position = position;
80 signal.heading = heading;
82 signal.osm_node_id = node_id;
83 signal.orientation = (t < 0.0) ?
"-" :
"+";
84 model.signals.push_back(std::move(signal));
92 auto to_local = [&](
const OsmNode &node) {
95 frame.
ToLocal(node.lon, node.lat, x, y);
100 for (
const auto &entry : osm.
nodes) {
101 const OsmNode &node = entry.second;
102 const std::string *highway =
FindTag(node.
tags,
"highway");
103 if (highway ==
nullptr) {
108 if (*highway ==
"traffic_signals") {
113 }
else if (*highway ==
"stop") {
116 }
else if (*highway ==
"give_way") {
119 }
else if (*highway ==
"crossing") {
129 const Road *road = NearestRoad(model, point, options.
snap_m, s, t);
130 if (road ==
nullptr) {
135 std::nullopt, node.
id);
139 std::set<std::string> signed_roads;
144 const std::string *maxspeed =
FindTag(road.
tags,
"maxspeed");
145 if (maxspeed ==
nullptr) {
148 const std::optional<double> speed =
ParseMaxSpeed(*maxspeed);
149 if (!speed.has_value() || signed_roads.count(road.
id) != 0u) {
152 signed_roads.insert(road.
id);
163 std::set<std::string> approaches;
167 for (
const std::string &road_id : approaches) {
184 std::set<std::string> signalised;
188 signalised.insert(signal.
road_id);
192 std::set<std::string> approaches;
196 if (approaches.size() < 2u) {
200 bool any_signalised =
false;
201 for (
const std::string &road_id : approaches) {
203 if (road ==
nullptr) {
206 best = std::max(best, ClassRank(road->
highway));
207 any_signalised = any_signalised || (signalised.count(road_id) != 0u);
209 if (any_signalised || best < 0) {
213 for (
const std::string &road_id : approaches) {
215 if (road !=
nullptr && ClassRank(road->
highway) != best) {
219 for (
const std::string &road_id : approaches) {
226 const bool major = mixed && ClassRank(road->
highway) == best;
229 const double at = std::max(0.0, total - 1.0);
238 std::map<std::string, std::vector<std::string>> by_junction;
245 bool touches =
false;
253 by_junction[junction.
id].push_back(signal.
id);
258 for (
const auto &entry : by_junction) {
260 controller.
id = MakeId(
"controller_", model.
controllers.size());
265 if (std::find(entry.second.begin(), entry.second.end(), signal.
id) !=
266 entry.second.end()) {
270 model.
controllers.push_back(std::move(controller));
WGS84 <-> local ENU metres, a transverse Mercator centred on the origin.
void ToLocal(double lon, double lat, double &x, double &y) const
double Length(const LineString &line)
bg::model::linestring< Point > LineString
Point Interpolate(const LineString &line, double s)
Point at distance s along line, shapely's LineString.interpolate.
double HeadingAt(const LineString &line, double s)
Heading of line at distance s, in radians.
double Hypot(const Point &a, const Point &b)
bg::model::d2::point_xy< double > Point
Point To2D(const Point3 &p)
double Project(const LineString &line, const Point &p)
Distance along line of the projection of p, shapely's LineString.project.
SignalKind
OpenDRIVE type/subtype follow road/SignalType.h; the crosswalk is not a signal in CARLA but an <objec...
std::optional< double > ParseMaxSpeed(const std::string &value)
Parse an OSM maxspeed value into m/s; "50", "50 km/h" and "30 mph".
const std::string * FindTag(const Tags &tags, const std::string &key)
void BuildSignals(const OsmData &osm, const LocalFrame &frame, TwinModelData &model, const SignalOptions &options)
Read traffic lights, stop and give-way signs, speed limits and crossings out of the OSM tags and atta...
std::string incoming_road
std::vector< std::string > signal_ids
std::vector< Connection > connections
std::map< long long, OsmNode > nodes
std::vector< Lane > lanes
geom::Line3 reference_line
std::optional< std::string > junction_id
bool generate_traffic_lights
bool all_junctions_traffic_lights
double snap_m
A signal node is bound to the nearest road end within this distance.
std::optional< std::string > controller_id
Everything the OpenDRIVE writer reads.
std::vector< Signal > signals
std::vector< Road > roads
const Road * FindRoad(const std::string &id) const
std::vector< Controller > controllers
std::vector< Junction > junctions