@@ -19,20 +19,23 @@ namespace util
19
19
using TrafficSignalArray = autoware_perception_msgs::msg::TrafficSignalArray;
20
20
using TrafficSignal = autoware_perception_msgs::msg::TrafficSignal;
21
21
using Element = autoware_perception_msgs::msg::TrafficSignalElement;
22
+ using Time = builtin_interfaces::msg::Time;
22
23
24
+ // Finds a signal by its ID within a TrafficSignalArray.
23
25
std::optional<TrafficSignal> find_signal_by_id (
24
26
const TrafficSignalArray & signals, int64_t signal_id)
25
27
{
26
28
auto it = std::find_if (
27
29
signals.signals .begin (), signals.signals .end (),
28
30
[signal_id](const TrafficSignal & signal ) { return signal .traffic_signal_id == signal_id; });
29
31
if (it != signals.signals .end ()) {
30
- return *it;
32
+ return *it; // Return the found signal.
31
33
} else {
32
- return std::nullopt;
34
+ return std::nullopt; // Return an empty optional if not found.
33
35
}
34
36
}
35
37
38
+ // Creates a TrafficSignalElement with specified attributes.
36
39
Element create_element (
37
40
const Element::_color_type & color, const Element::_shape_type & shape,
38
41
const Element::_status_type & status, const Element::_confidence_type & confidence)
@@ -46,7 +49,7 @@ Element create_element(
46
49
return signal_element;
47
50
}
48
51
49
- // Create unknown elements for each shape
52
+ // Creates unknown elements for each unique shape from two element vectors.
50
53
std::vector<Element> create_unknown_elements (
51
54
const std::vector<Element> & elements1, const std::vector<Element> & elements2)
52
55
{
@@ -60,66 +63,68 @@ std::vector<Element> create_unknown_elements(
60
63
61
64
std::vector<Element> unknown_elements;
62
65
for (const auto & shape : shape_set) {
63
- // Confidence doesn't matter because this is the unknown signal
66
+ // Confidence is set to a default value as it is not relevant for unknown signals.
64
67
unknown_elements.emplace_back (
65
- util:: create_element (Element::UNKNOWN, shape, Element::UNKNOWN, /* confidence */ 1.0 ));
68
+ create_element (Element::UNKNOWN, shape, Element::UNKNOWN, /* confidence */ 1.0 ));
66
69
}
67
70
68
71
return unknown_elements;
69
72
}
70
73
74
+ // Creates a 'unknown' signal with elements matching the shapes of a given signal's elements.
71
75
TrafficSignal create_unknown_signal (const TrafficSignal & signal)
72
76
{
73
77
TrafficSignal unknown_signal;
74
78
unknown_signal.traffic_signal_id = signal .traffic_signal_id ;
75
79
for (const auto & element : signal .elements ) {
76
- // Confidence doesn't matter because this is the unknown signal
80
+ // Confidence is set to a default value as it is not relevant for unknown signals.
77
81
const auto unknown_element =
78
- util:: create_element (Element::UNKNOWN, element.shape , Element::UNKNOWN, /* confidence */ 1.0 );
82
+ create_element (Element::UNKNOWN, element.shape , Element::UNKNOWN, /* confidence */ 1.0 );
79
83
unknown_signal.elements .emplace_back (unknown_element);
80
84
}
81
85
82
86
return unknown_signal;
83
87
}
84
88
89
+ // Creates an 'unknown' signal by combining unique shapes from two signals' elements.
85
90
TrafficSignal create_unknown_signal (const TrafficSignal & signal1, const TrafficSignal & signal2)
86
91
{
87
92
TrafficSignal unknown_signal;
88
93
89
- // Assume that the both ids are same
94
+ // Assumes that both signals have the same traffic_signal_id.
90
95
unknown_signal.traffic_signal_id = signal1.traffic_signal_id ;
91
96
92
- const auto unknown_elements = util:: create_unknown_elements (signal1.elements , signal2.elements );
97
+ const auto unknown_elements = create_unknown_elements (signal1.elements , signal2.elements );
93
98
for (const auto & element : unknown_elements) {
94
99
unknown_signal.elements .emplace_back (element);
95
100
}
96
101
97
102
return unknown_signal;
98
103
}
99
104
105
+ // Checks if all elements in two vectors are equivalent
100
106
bool are_all_elements_equivalent (
101
107
const std::vector<Element> & signal1, const std::vector<Element> & signal2)
102
108
{
103
- // Check if the vectors have the same size
109
+ // Returns false if vectors have different sizes.
104
110
if (signal1.size () != signal2.size ()) {
105
111
return false ;
106
112
}
107
113
108
- // Create copies of the vectors
114
+ // Sorts copies of the vectors by shape for comparison.
109
115
std::vector<Element> sorted_signal1 = signal1;
110
116
std::vector<Element> sorted_signal2 = signal2;
111
-
112
- // Sort based on the shape to ensure that they are same order
113
117
auto compare_by_shape = [](const Element & a, const Element & b) { return a.shape < b.shape ; };
114
118
std::sort (sorted_signal1.begin (), sorted_signal1.end (), compare_by_shape);
115
119
std::sort (sorted_signal2.begin (), sorted_signal2.end (), compare_by_shape);
116
120
117
- // Compare the sorted vectors and return true if they have all the same elements
121
+ // Returns true if sorted vectors are equal.
118
122
return std::equal (
119
123
sorted_signal1.begin (), sorted_signal1.end (), sorted_signal2.begin (), sorted_signal2.end (),
120
124
[](const Element & a, const Element & b) { return a.color == b.color && a.shape == b.shape ; });
121
125
}
122
126
127
+ // Creates a set of unique signal IDs from two vectors of TrafficSignals.
123
128
std::unordered_set<lanelet::Id> create_signal_id_set (
124
129
const std::vector<TrafficSignal> & signals1, const std::vector<TrafficSignal> & signals2)
125
130
{
@@ -134,24 +139,25 @@ std::unordered_set<lanelet::Id> create_signal_id_set(
134
139
return signal_id_set;
135
140
}
136
141
142
+ // Returns the signal with the highest confidence elements, considering a external priority.
137
143
TrafficSignal get_highest_confidence_signal (
138
144
const std::optional<TrafficSignal> & perception_signal,
139
145
const std::optional<TrafficSignal> & external_signal, const bool external_priority)
140
146
{
141
- // If the either of the signal doesn't exist, return the signal that exists
147
+ // Returns the existing signal if only one of them exists.
142
148
if (!perception_signal) {
143
149
return *external_signal;
144
150
}
145
151
if (!external_signal) {
146
152
return *perception_signal;
147
153
}
148
154
149
- // If the external_priority is true, use the external results
155
+ // Gives priority to the external signal if external_priority is true.
150
156
if (external_priority) {
151
157
return *external_signal;
152
158
}
153
159
154
- // Create map using shape as key
160
+ // Compiles elements into a map by shape, to compare their confidences.
155
161
using Key = Element::_shape_type;
156
162
std::map<Key, std::vector<Element>> shape_element_map;
157
163
for (const auto & element : perception_signal->elements ) {
@@ -163,12 +169,11 @@ TrafficSignal get_highest_confidence_signal(
163
169
164
170
TrafficSignal highest_confidence_signal;
165
171
166
- // Assume that the both ids are same
172
+ // Assumes that both signals have the same traffic_signal_id.
167
173
highest_confidence_signal.traffic_signal_id = perception_signal->traffic_signal_id ;
168
174
169
- // Find the highest confidence element and push it
170
- for (const auto & shape_and_elements : shape_element_map) {
171
- const auto & elements = shape_and_elements.second ;
175
+ // For each shape, finds the element with the highest confidence and adds it to the signal.
176
+ for (const auto & [shape, elements] : shape_element_map) {
172
177
const auto highest_confidence_element = std::max_element (
173
178
elements.begin (), elements.end (),
174
179
[](const Element & a, const Element & b) { return a.confidence < b.confidence ; });
@@ -178,9 +183,10 @@ TrafficSignal get_highest_confidence_signal(
178
183
return highest_confidence_signal;
179
184
}
180
185
181
- using Time = builtin_interfaces::msg:: Time;
186
+ // Determines the newer of two Time stamps.
182
187
Time get_newer_stamp (const Time & stamp1, const Time & stamp2)
183
188
{
189
+ // Returns stamp1 if it is newer than stamp2, otherwise returns stamp2.
184
190
if (stamp1.sec > stamp2.sec || (stamp1.sec == stamp2.sec && stamp1.nanosec > stamp2.nanosec )) {
185
191
return stamp1;
186
192
} else {
0 commit comments