CARLA
Loading...
Searching...
No Matches
MapLogicParser.h
Go to the documentation of this file.
1// Copyright (c) 2024 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#pragma once
8
10
11#include <util/ue-header-guard-begin.h>
12#include "CoreMinimal.h"
13#include "Dom/JsonObject.h"
14#include <util/ue-header-guard-end.h>
15
16#include "MapLogicParser.generated.h"
17
18/// Represents timing configuration for a traffic light from map_logic.json
19USTRUCT()
21{
22 GENERATED_BODY()
23
24 UPROPERTY()
25 float RedDuration = 2.0f;
26
27 UPROPERTY()
28 float GreenDuration = 10.0f;
29
30 UPROPERTY()
31 float AmberDuration = 3.0f;
32
33 UPROPERTY()
34 float AmberBlinkInterval = 0.25f;
35};
36
37/// Represents a traffic light module with its lane assignments
38USTRUCT()
40{
41 GENERATED_BODY()
42
43 UPROPERTY()
44 TArray<int32> LaneIds;
45};
46
47/// One head of a baked Traffic Light Tool rig, and the OpenDRIVE signal it shows.
48///
49/// A mast arm can carry a protected-turn arrow beside the through head, and the two are red
50/// and green at the same instant. ATrafficLightBase holds exactly one UTrafficLightComponent
51/// and one ETrafficLightState (TrafficLightBase.h), and get_state / get_opendrive_id /
52/// get_stop_waypoints / the ALSM's IsAtTrafficLight are all per *actor*, so one actor cannot
53/// express two states. The rig is therefore split into one ADigitalTwinsTrafficLight per
54/// distinct SignalID, and this is the explicit binding that says which components go where --
55/// ComponentPrefix is the name ATrafficLightActor::Build gives the head's components
56/// ("Pole_02_Head_03"), which Bake preserves.
57USTRUCT()
59{
60 GENERATED_BODY()
61
62 UPROPERTY()
63 FString ComponentPrefix;
64
65 UPROPERTY()
66 FString SignalID;
67
68 UPROPERTY()
69 TArray<int32> LaneIds;
70};
71
72/// Represents a traffic light configuration entry from map_logic.json
73USTRUCT()
75{
76 GENERATED_BODY()
77
78 UPROPERTY()
79 FString ActorName;
80
81 UPROPERTY()
82 FString SignalID;
83
84 UPROPERTY()
85 int32 JunctionID = -1;
86
87 UPROPERTY()
88 FString TrafficLightGroupID;
89
90 UPROPERTY()
92
93 UPROPERTY()
94 TArray<FTrafficLightModule> Modules;
95
96 /// One entry per head of the baked rig. Empty (or all naming the same signal) = the whole
97 /// rig is one traffic light, exactly as before.
98 UPROPERTY()
99 TArray<FTrafficLightHead> Heads;
100};
101
102/// Utility class to parse map_logic.json files and apply traffic light timing configuration
103class CARLA_API UMapLogicParser
104{
105public:
106
107 /// Parse map_logic.json file and apply traffic light timing configuration
108 /// to the TrafficLightManager. Returns true if file was found and parsed successfully.
109 ///
110 /// @param XODRFilePath Path to the .xodr file (will look for map_logic.json in same directory)
111 /// @param TrafficLightManager The traffic light manager to apply configuration to
112 /// @return true if map_logic.json was found and applied successfully, false otherwise
113 static bool ParseAndApplyMapLogic(const FString& XODRFilePath, ATrafficLightManager* TrafficLightManager);
114
115 /// Apply lane IDs from map_logic.json to traffic light components
116 /// @param XODRFilePath Path to the .xodr file (will look for map_logic.json in same directory)
117 /// @param TrafficLightManager The traffic light manager to apply configuration to
118 static void ApplyLaneIdsFromMapLogic(const FString& XODRFilePath, ATrafficLightManager* TrafficLightManager);
119
120private:
121
122 /// Load and parse the map_logic.json file from the same directory as the XODR file
123 /// @param XODRFilePath Path to the .xodr file
124 /// @return Array of parsed traffic light logic data, empty if file not found or parse failed
125 static TArray<FTrafficLightLogicData> LoadMapLogicFromJSON(const FString& XODRFilePath);
126
127 /// Apply traffic light timing configuration to the TrafficLightManager
128 /// @param LogicData Array of traffic light configurations to apply
129 /// @param TrafficLightManager The traffic light manager to configure
130 static void ApplyTrafficLightLogic(const TArray<FTrafficLightLogicData>& LogicData, ATrafficLightManager* TrafficLightManager);
131
132 /// Parse individual traffic light data from JSON object
133 /// @param TrafficLightJson JSON object representing a single traffic light configuration
134 /// @return Parsed traffic light logic data
135 static FTrafficLightLogicData ParseTrafficLightFromJSON(TSharedPtr<FJsonObject> TrafficLightJson);
136
137 /// Parse timing data from JSON object
138 /// @param TimingJson JSON object containing timing information
139 /// @return Parsed timing configuration
140 static FTrafficLightTiming ParseTimingFromJSON(TSharedPtr<FJsonObject> TimingJson);
141
142 /// Get the directory path from a full file path
143 /// @param FilePath Full path to a file
144 /// @return Directory path without the filename
145 static FString GetDirectoryPath(const FString& FilePath);
146};
Class In charge of creating and assigning traffic light groups, controllers and components.
Utility class to parse map_logic.json files and apply traffic light timing configuration.
One head of a baked Traffic Light Tool rig, and the OpenDRIVE signal it shows.
Represents a traffic light configuration entry from map_logic.json.
Represents a traffic light module with its lane assignments.
Represents timing configuration for a traffic light from map_logic.json.