-
Notifications
You must be signed in to change notification settings - Fork 892
Expand file tree
/
Copy pathshapes.cpp
More file actions
289 lines (243 loc) · 11.3 KB
/
shapes.cpp
File metadata and controls
289 lines (243 loc) · 11.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
// Copyright 2024 TIER IV, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "autoware/multi_object_tracker/object_model/shapes.hpp"
#include <Eigen/Geometry>
#include <autoware_utils_geometry/boost_geometry.hpp>
#include <autoware_utils_geometry/boost_polygon_utils.hpp>
#include <tf2/utils.hpp>
#include <autoware_perception_msgs/msg/shape.hpp>
#include <tf2_geometry_msgs/tf2_geometry_msgs.hpp>
#include <boost/geometry.hpp>
#include <algorithm>
#include <cmath>
#include <limits>
#include <string>
#include <utility>
#include <vector>
namespace
{
constexpr double MIN_AREA = 1e-6;
constexpr double INVALID_SCORE = -1.0;
} // namespace
namespace autoware::multi_object_tracker
{
namespace shapes
{
inline double getSumArea(const std::vector<autoware_utils_geometry::Polygon2d> & polygons)
{
return std::accumulate(
polygons.begin(), polygons.end(), 0.0, [](double acc, autoware_utils_geometry::Polygon2d p) {
return acc + boost::geometry::area(p);
});
}
inline double getIntersectionArea(
const autoware_utils_geometry::Polygon2d & source_polygon,
const autoware_utils_geometry::Polygon2d & target_polygon)
{
std::vector<autoware_utils_geometry::Polygon2d> intersection_polygons;
boost::geometry::intersection(source_polygon, target_polygon, intersection_polygons);
return getSumArea(intersection_polygons);
}
inline double getUnionArea(
const autoware_utils_geometry::Polygon2d & source_polygon,
const autoware_utils_geometry::Polygon2d & target_polygon)
{
std::vector<autoware_utils_geometry::Polygon2d> union_polygons;
boost::geometry::union_(source_polygon, target_polygon, union_polygons);
return getSumArea(union_polygons);
}
inline double getConvexShapeArea(
const autoware_utils_geometry::Polygon2d & source_polygon,
const autoware_utils_geometry::Polygon2d & target_polygon)
{
boost::geometry::model::multi_polygon<autoware_utils_geometry::Polygon2d> union_polygons;
boost::geometry::union_(source_polygon, target_polygon, union_polygons);
autoware_utils_geometry::Polygon2d hull;
boost::geometry::convex_hull(union_polygons, hull);
return boost::geometry::area(hull);
}
double get1dIoU(
const types::DynamicObject & source_object, const types::DynamicObject & target_object)
{
constexpr double min_union_length = 0.1; // As 0.01 used in 2dIoU, use 0.1 here
constexpr double min_length = 1e-3; // As 1e-6 used in 2dIoU, use 1e-3 here
// Compute radii from dimensions (use max of x and y as diameter)
const double r_src =
std::max(source_object.shape.dimensions.x, source_object.shape.dimensions.y) * 0.5;
const double r_tgt =
std::max(target_object.shape.dimensions.x, target_object.shape.dimensions.y) * 0.5;
// if radius is smaller than the minimum length, return 0.0
if (r_src < min_length || r_tgt < min_length) return 0.0;
// Ensure r1 is the larger radius
const double r1 = std::max(r_tgt, r_src);
const double r2 = std::min(r_tgt, r_src);
const auto dx = source_object.pose.position.x - target_object.pose.position.x;
const auto dy = source_object.pose.position.y - target_object.pose.position.y;
// distance between centers
const auto dist = std::sqrt(dx * dx + dy * dy);
// if distance is larger than the sum of radius, return 0.0
if (dist > r1 + r2 - min_length) return 0.0;
// if distance is smaller than the difference of radius, return the ratio of the smaller radius to
// the larger radius
// Square used to mimic area ratio behavior as a rough 2D approximation
if (dist < r1 - r2) return (r2 * r2) / (r1 * r1);
// if distance is between the difference and the sum of radii, return the ratio of the
// intersection length to the union length
if (r1 + r2 + dist < min_union_length) return 0.0;
const double intersection_length = r1 + r2 - dist;
const double iou = intersection_length * r2 / (r1 * r1) * 0.5;
return iou;
}
double get2dIoU(
const types::DynamicObject & source_object, const types::DynamicObject & target_object,
const double min_union_area)
{
const auto source_polygon =
autoware_utils_geometry::to_polygon2d(source_object.pose, source_object.shape);
if (boost::geometry::area(source_polygon) < MIN_AREA) return 0.0;
const auto target_polygon =
autoware_utils_geometry::to_polygon2d(target_object.pose, target_object.shape);
if (boost::geometry::area(target_polygon) < MIN_AREA) return 0.0;
const double intersection_area = getIntersectionArea(source_polygon, target_polygon);
if (intersection_area < MIN_AREA) return 0.0;
const double union_area = getUnionArea(source_polygon, target_polygon);
const double iou =
union_area < min_union_area ? 0.0 : std::min(1.0, intersection_area / union_area);
return iou;
}
double get2dGeneralizedIoU(
const types::DynamicObject & source_object, const types::DynamicObject & target_object)
{
static const double MIN_AREA = 1e-6;
const auto source_polygon =
autoware_utils_geometry::to_polygon2d(source_object.pose, source_object.shape);
const double source_area = boost::geometry::area(source_polygon);
const auto target_polygon =
autoware_utils_geometry::to_polygon2d(target_object.pose, target_object.shape);
const double target_area = boost::geometry::area(target_polygon);
if (source_area < MIN_AREA && target_area < MIN_AREA) return -1.0;
const double intersection_area = getIntersectionArea(source_polygon, target_polygon);
const double union_area = getUnionArea(source_polygon, target_polygon);
const double iou = union_area < 0.01 ? 0.0 : std::min(1.0, intersection_area / union_area);
const double convex_shape_area = getConvexShapeArea(source_polygon, target_polygon);
return iou - (convex_shape_area - union_area) / convex_shape_area;
}
bool get2dPrecisionRecallGIoU(
const types::DynamicObject & source_object, const types::DynamicObject & target_object,
double & precision, double & recall, double & generalized_iou)
{
static const double MIN_AREA = 1e-6;
const auto source_polygon =
autoware_utils_geometry::to_polygon2d(source_object.pose, source_object.shape);
const double source_area = boost::geometry::area(source_polygon);
if (source_area < MIN_AREA) return false;
const auto target_polygon =
autoware_utils_geometry::to_polygon2d(target_object.pose, target_object.shape);
const double target_area = boost::geometry::area(target_polygon);
if (target_area < MIN_AREA) return false;
const double intersection_area = getIntersectionArea(source_polygon, target_polygon);
const double union_area = getUnionArea(source_polygon, target_polygon);
const double convex_shape_area = getConvexShapeArea(source_polygon, target_polygon);
const double iou = union_area < 0.01 ? 0.0 : std::min(1.0, intersection_area / union_area);
precision = source_area < MIN_AREA ? 0.0 : std::min(1.0, intersection_area / source_area);
recall = source_area < MIN_AREA ? 0.0 : std::min(1.0, intersection_area / target_area);
generalized_iou = iou - (convex_shape_area - union_area) / convex_shape_area;
return true;
}
/**
* @brief convert convex hull shape object to bounding box object
* @param input_object: input convex hull objects
* @param output_object: output bounding box objects
*/
bool convertConvexHullToBoundingBox(
const types::DynamicObject & input_object, types::DynamicObject & output_object)
{
// check footprint size
const auto & points = input_object.shape.footprint.points;
if (points.size() < 3) {
return false;
}
// Pre-allocate boundary values using first point
float max_x = points[0].x;
float max_y = points[0].y;
float min_x = points[0].x;
float min_y = points[0].y;
// Start from second point since we used first point for initialization
for (size_t i = 1; i < points.size(); ++i) {
const auto & point = points[i];
// Use direct comparison instead of std::max/min
if (point.x > max_x) max_x = point.x;
if (point.y > max_y) max_y = point.y;
if (point.x < min_x) min_x = point.x;
if (point.y < min_y) min_y = point.y;
}
// calc new center in local coordinates - avoid division by 2.0 twice
const double center_x = (max_x + min_x) * 0.5;
const double center_y = (max_y + min_y) * 0.5;
// transform to global for the object's position
const double yaw = tf2::getYaw(input_object.pose.orientation);
const double cos_yaw = cos(yaw);
const double sin_yaw = sin(yaw);
const double dx = center_x * cos_yaw - center_y * sin_yaw;
const double dy = center_x * sin_yaw + center_y * cos_yaw;
// set output parameters - avoid unnecessary copying
output_object = input_object;
output_object.pose.position.x += dx;
output_object.pose.position.y += dy;
output_object.shape.type = autoware_perception_msgs::msg::Shape::BOUNDING_BOX;
output_object.shape.dimensions.x = max_x - min_x;
output_object.shape.dimensions.y = max_y - min_y;
//// pose.position.z and shape.dimensions.z (height)
// Footprint points are 2D (z=0), so deriving height from
// them would always give zero. Preserve the input value unchanged.
// adjust footprint points in local coordinates - use references to avoid copies
for (auto & point : output_object.shape.footprint.points) {
point.x -= center_x;
point.y -= center_y;
}
return true;
}
std::pair<double, double> getObjectZRange(const types::DynamicObject & object)
{
const double center_z = object.pose.position.z;
const double height = object.shape.dimensions.z;
const double min_z = center_z - height / 2.0;
const double max_z = center_z + height / 2.0;
return {min_z, max_z};
}
double get3dGeneralizedIoU(
const types::DynamicObject & source_object, const types::DynamicObject & target_object)
{
const auto source_polygon =
autoware_utils_geometry::to_polygon2d(source_object.pose, source_object.shape);
if (boost::geometry::area(source_polygon) < MIN_AREA) return INVALID_SCORE;
const auto target_polygon =
autoware_utils_geometry::to_polygon2d(target_object.pose, target_object.shape);
if (boost::geometry::area(target_polygon) < MIN_AREA) return INVALID_SCORE;
const double union_area = getUnionArea(source_polygon, target_polygon);
if (union_area < MIN_AREA) return INVALID_SCORE;
const double intersection_area = getIntersectionArea(source_polygon, target_polygon);
const double convex_area = getConvexShapeArea(source_polygon, target_polygon);
const auto [z_min_src, z_max_src] = getObjectZRange(source_object);
const auto [z_min_tgt, z_max_tgt] = getObjectZRange(target_object);
const double height_overlap =
std::max(0.0, std::min(z_max_src, z_max_tgt) - std::max(z_min_src, z_min_tgt));
if (height_overlap <= 0.0) return INVALID_SCORE;
const double total_height = std::max(z_max_src, z_max_tgt) - std::min(z_min_src, z_min_tgt);
const double iou =
std::clamp((intersection_area * height_overlap) / (union_area * total_height), 0.0, 1.0);
return iou - (convex_area - union_area) / convex_area;
}
} // namespace shapes
} // namespace autoware::multi_object_tracker