37 using Type =
typename PointT::Type;
43 size_t nr_poly_points = polygon.
size();
44 if (nr_poly_points < 3) {
45 AINFO <<
"Polygon points number is smaller than 3.";
51 Type xold = polygon.
at(nr_poly_points - 1).x;
52 Type yold = polygon.
at(nr_poly_points - 1).y;
53 for (
size_t i = 0; i < nr_poly_points; ++i) {
54 Type xnew = polygon.
at(i).x;
55 Type ynew = polygon.
at(i).y;
68 Type value = (point.y - y1) * (x2 - x1) - (y2 - y1) * (point.x - x1);
69 Type temp = std::sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1));
70 if (temp < std::numeric_limits<Type>::epsilon()) {
73 Type distance = std::abs(value) / temp;
74 if (x1 <= point.x && point.x <= x2 &&
75 distance < std::numeric_limits<Type>::epsilon()) {
78 if ((x1 < point.x) == (point.x <= x2) && value < 0.f) {
90bool IsPointInBBox(
const Eigen::Matrix<typename PointT::Type, 3, 1> &gnd_c,
91 const Eigen::Matrix<typename PointT::Type, 3, 1> &dir_x,
92 const Eigen::Matrix<typename PointT::Type, 3, 1> &dir_y,
93 const Eigen::Matrix<typename PointT::Type, 3, 1> &dir_z,
94 const Eigen::Matrix<typename PointT::Type, 3, 1> &size,
95 const PointT &point) {
96 using T =
typename PointT::Type;
97 Eigen::Matrix<T, 3, 1> eig(point.x, point.y, point.z);
98 Eigen::Matrix<T, 3, 1> diff = eig - gnd_c;
99 T x = diff.dot(dir_x);
100 if (fabs(x) > size[0] * 0.5) {
103 T y = diff.dot(dir_y);
104 if (fabs(y) > size[1] * 0.5) {
107 T z = diff.dot(dir_z);
108 if (fabs(z) > size[2] * 0.5) {
118 const PointCloudT &cloud,
const Eigen::Vector3f &dir, Eigen::Vector3f *size,
119 Eigen::Vector3d *center,
120 float minimum_edge_length = std::numeric_limits<float>::epsilon()) {
122 Eigen::Matrix3d projection;
123 Eigen::Vector3d dird(dir[0], dir[1], 0.0);
125 projection << dird[0], dird[1], 0.0, -dird[1], dird[0], 0.0, 0.0, 0.0, 1.0;
126 constexpr double kDoubleMax = std::numeric_limits<double>::max();
127 Eigen::Vector3d min_pt(kDoubleMax, kDoubleMax, kDoubleMax);
128 Eigen::Vector3d max_pt(-kDoubleMax, -kDoubleMax, -kDoubleMax);
129 Eigen::Vector3d loc_pt(0.0, 0.0, 0.0);
130 for (
size_t i = 0; i < cloud.size(); i++) {
131 loc_pt = projection * Eigen::Vector3d(cloud[i].x, cloud[i].y, cloud[i].z);
133 min_pt(0) = std::min(min_pt(0), loc_pt(0));
134 min_pt(1) = std::min(min_pt(1), loc_pt(1));
135 min_pt(2) = std::min(min_pt(2), loc_pt(2));
137 max_pt(0) = std::max(max_pt(0), loc_pt(0));
138 max_pt(1) = std::max(max_pt(1), loc_pt(1));
139 max_pt(2) = std::max(max_pt(2), loc_pt(2));
141 (*size) = (max_pt - min_pt).cast<float>();
142 Eigen::Vector3d coeff = (max_pt + min_pt) * 0.5;
143 coeff(2) = min_pt(2);
144 *center = projection.transpose() * coeff;
146 constexpr float kFloatEpsilon = std::numeric_limits<float>::epsilon();
147 float minimum_size = std::max(minimum_edge_length,
kFloatEpsilon);
149 (*size)(0) = (*size)(0) <= minimum_size ? minimum_size : (*size)(0);
150 (*size)(1) = (*size)(1) <= minimum_size ? minimum_size : (*size)(1);
151 (*size)(2) = (*size)(2) <= minimum_size ? minimum_size : (*size)(2);
157 const Eigen::Matrix<Type, 3, 1> &prev_dir,
158 Eigen::Matrix<Type, 3, 1> *curr_dir) {
159 Type dot_val_00 = prev_dir(0) * (*curr_dir)(0) + prev_dir(1) * (*curr_dir)(1);
160 Type dot_val_01 = prev_dir(0) * (*curr_dir)(1) - prev_dir(1) * (*curr_dir)(0);
161 if (fabs(dot_val_00) >= fabs(dot_val_01)) {
162 if (dot_val_00 < 0) {
163 (*curr_dir) = -(*curr_dir);
166 if (dot_val_01 < 0) {
168 Eigen::Matrix<Type, 3, 1>((*curr_dir)(1), -(*curr_dir)(0), 0);
171 Eigen::Matrix<Type, 3, 1>(-(*curr_dir)(1), (*curr_dir)(0), 0);
180 const Eigen::Matrix<Type, 3, 1> &size0,
181 const Eigen::Matrix<Type, 3, 1> ¢er1,
182 const Eigen::Matrix<Type, 3, 1> &size1) {
183 Type min_x_bbox_0 = center0(0) - size0(0) *
static_cast<Type
>(0.5);
184 Type min_x_bbox_1 = center1(0) - size1(0) *
static_cast<Type
>(0.5);
185 Type max_x_bbox_0 = center0(0) + size0(0) *
static_cast<Type
>(0.5);
186 Type max_x_bbox_1 = center1(0) + size1(0) *
static_cast<Type
>(0.5);
187 Type start_x = std::max(min_x_bbox_0, min_x_bbox_1);
188 Type end_x = std::min(max_x_bbox_0, max_x_bbox_1);
189 Type length_x = end_x - start_x;
193 Type min_y_bbox_0 = center0(1) - size0(1) *
static_cast<Type
>(0.5);
194 Type min_y_bbox_1 = center1(1) - size1(1) *
static_cast<Type
>(0.5);
195 Type max_y_bbox_0 = center0(1) + size0(1) *
static_cast<Type
>(0.5);
196 Type max_y_bbox_1 = center1(1) + size1(1) *
static_cast<Type
>(0.5);
197 Type start_y = std::max(min_y_bbox_0, min_y_bbox_1);
198 Type end_y = std::min(max_y_bbox_0, max_y_bbox_1);
199 Type length_y = end_y - start_y;
203 Type intersection_area = length_x * length_y;
204 Type bbox_0_area = size0(0) * size0(1);
205 Type bbox_1_area = size1(0) * size1(1);
207 intersection_area / (bbox_0_area + bbox_1_area - intersection_area);
225 const Eigen::Matrix<typename PointT::Type, 3, 1> &pt,
227 Eigen::Matrix<typename PointT::Type, 3, 1> *dir) {
228 if (segs.
size() < 2) {
232 using Type =
typename PointT::Type;
233 Eigen::Matrix<Type, 3, 1> seg_point(segs[0].x, segs[0].y, 0);
234 Type min_dist = (pt - seg_point).head(2).norm();
236 Eigen::Matrix<Type, 3, 1> end_point_pre;
237 Eigen::Matrix<Type, 3, 1> end_point_cur;
238 Eigen::Matrix<Type, 3, 1> line_segment_dir;
239 Eigen::Matrix<Type, 3, 1> line_segment_dir_pre;
240 Eigen::Matrix<Type, 3, 1> end_point_to_pt_vec;
242 line_segment_dir_pre << 0, 0, 0;
244 Type line_segment_len = 0;
245 Type projected_len = 0;
246 Type point_to_line_dist = 0;
247 Type point_to_end_point_dist = 0;
249 for (
size_t i = 1; i < segs.
size(); ++i) {
250 end_point_pre << segs[i - 1].x, segs[i - 1].y, 0;
251 end_point_cur << segs[i].x, segs[i].y, 0;
252 line_segment_dir = end_point_pre - end_point_cur;
253 end_point_to_pt_vec = pt - end_point_cur;
254 end_point_to_pt_vec(2) = 0;
255 line_segment_len = line_segment_dir.head(2).norm();
256 line_segment_dir = line_segment_dir / line_segment_len;
258 *dir = line_segment_dir;
260 projected_len = end_point_to_pt_vec.dot(line_segment_dir);
263 if (projected_len >= 0 && projected_len <= line_segment_len) {
264 point_to_line_dist = end_point_to_pt_vec.cross(line_segment_dir).norm();
265 if (min_dist > point_to_line_dist) {
266 min_dist = point_to_line_dist;
267 *dir = line_segment_dir;
272 point_to_end_point_dist = end_point_to_pt_vec.head(2).norm();
273 if (min_dist > point_to_end_point_dist) {
274 min_dist = point_to_end_point_dist;
275 *dir = line_segment_dir + line_segment_dir_pre;
279 line_segment_dir_pre = line_segment_dir;
291 const Eigen::Matrix<typename PointT::Type, 3, 1> &pt,
294 Eigen::Matrix<typename PointT::Type, 3, 1> *dir) {
295 using Type =
typename PointT::Type;
296 Type dist_to_left = std::numeric_limits<Type>::max();
297 Eigen::Matrix<Type, 3, 1> direction_left;
298 Type dist_to_right = std::numeric_limits<Type>::max();
299 Eigen::Matrix<Type, 3, 1> direction_right;
306 if (dist_to_left < dist_to_right) {
307 (*dist) = dist_to_left;
308 (*dir) = direction_left;
310 (*dist) = dist_to_right;
311 (*dir) = direction_right;
320 const Eigen::Matrix<typename PointT::Type, 3, 1> &pt,
323 typename PointT::Type *dist,
324 Eigen::Matrix<typename PointT::Type, 3, 1> *dir) {
325 using Type =
typename PointT::Type;
326 Type dist_to_left = std::numeric_limits<Type>::max();
327 Eigen::Matrix<Type, 3, 1> direction_left;
328 Type dist_to_right = std::numeric_limits<Type>::max();
329 Eigen::Matrix<Type, 3, 1> direction_right;
331 for (
size_t i = 0; i < left_boundary.size(); i++) {
332 Type dist_temp = std::numeric_limits<Type>::max();
333 Eigen::Matrix<Type, 3, 1> dir_temp;
336 if (dist_to_left > dist_temp) {
337 dist_to_left = dist_temp;
338 direction_left = dir_temp;
343 for (
size_t i = 0; i < right_boundary.size(); i++) {
344 Type dist_temp = std::numeric_limits<Type>::max();
345 Eigen::Matrix<Type, 3, 1> dir_temp;
348 if (dist_to_right > dist_temp) {
349 dist_to_right = dist_temp;
350 direction_right = dir_temp;
354 if (dist_to_left < dist_to_right) {
355 (*dist) = dist_to_left;
356 (*dir) = direction_left;
358 (*dist) = dist_to_right;
359 (*dir) = direction_right;
367 Type size_x, Type size_y, Type size_z, Type theta,
368 Eigen::Matrix<Type, 8, 1> *corners) {
369 Type cos_theta = cos(theta);
370 Type sin_theta = sin(theta);
371 Type hx = size_x * 0.5;
372 Type hy = size_y * 0.5;
374 Type left_up_x = (-hx) * cos_theta + (-hy) * sin_theta + center_x;
375 Type left_up_y = (-hx) * (-sin_theta) + (-hy) * cos_theta + center_y;
376 Type right_up_x = (-hx) * cos_theta + hy * sin_theta + center_x;
377 Type right_up_y = (-hx) * (-sin_theta) + hy * cos_theta + center_y;
378 Type right_down_x = hx * cos_theta + hy * sin_theta + center_x;
379 Type right_down_y = hx * (-sin_theta) + hy * cos_theta + center_y;
380 Type left_down_x = hx * cos_theta + (-hy) * sin_theta + center_x;
381 Type left_down_y = hx * (-sin_theta) + (-hy) * cos_theta + center_y;
383 (*corners)(0) = left_up_x;
384 (*corners)(1) = left_up_y;
385 (*corners)(2) = right_up_x;
386 (*corners)(3) = right_up_y;
387 (*corners)(4) = right_down_x;
388 (*corners)(5) = right_down_y;
389 (*corners)(6) = left_down_x;
390 (*corners)(7) = left_down_y;
397 const Eigen::Matrix<T, 2, 1> &p1,
398 const Eigen::Matrix<T, 2, 1> &q1,
399 const Eigen::Matrix<T, 2, 1> &p2,
400 const Eigen::Matrix<T, 2, 1> &q2) {
406 auto find_orientation = [](
const Eigen::Matrix<T, 2, 1> &p,
407 const Eigen::Matrix<T, 2, 1> &q,
408 const Eigen::Matrix<T, 2, 1> &r) ->
int {
415 float ret = (q[1] - p[1]) * (r[0] - q[0]) - (q[0] - p[0]) * (r[1] - q[1]);
417 if (std::fabs(ret) < std::numeric_limits<float>::epsilon()) {
421 return (ret > 0.f) ? 1 : 2;
426 auto is_on_segment = [](
const Eigen::Matrix<T, 2, 1> &p,
427 const Eigen::Matrix<T, 2, 1> &q,
428 const Eigen::Matrix<T, 2, 1> &r) ->
bool {
429 return p[0] <= std::max(q[0], r[0])
430 && p[0] >= std::min(q[0], r[0])
431 && p[1] <= std::max(q[1], r[1])
432 && p[1] >= std::min(q[1], r[1]);
438 int orientation_p1_q1_p2 = find_orientation(p1, q1, p2);
439 int orientation_p1_q1_q2 = find_orientation(p1, q1, q2);
440 int orientation_p2_q2_p1 = find_orientation(p2, q2, p1);
441 int orientation_p2_q2_q1 = find_orientation(p2, q2, q1);
442 if (orientation_p1_q1_p2 != orientation_p1_q1_q2
443 && orientation_p2_q2_p1 != orientation_p2_q2_q1) {
448 if (orientation_p1_q1_p2 == 0 && is_on_segment(p2, p1, q1)) {
451 if (orientation_p1_q1_q2 == 0 && is_on_segment(q2, p1, q1)) {
454 if (orientation_p2_q2_p1 == 0 && is_on_segment(p1, p2, q2)) {
457 if (orientation_p2_q2_q1 == 0 && is_on_segment(q1, p2, q2)) {
bool IsPointInBBox(const Eigen::Matrix< typename PointT::Type, 3, 1 > &gnd_c, const Eigen::Matrix< typename PointT::Type, 3, 1 > &dir_x, const Eigen::Matrix< typename PointT::Type, 3, 1 > &dir_y, const Eigen::Matrix< typename PointT::Type, 3, 1 > &dir_z, const Eigen::Matrix< typename PointT::Type, 3, 1 > &size, const PointT &point)
void CalculateCornersFromCenter(Type center_x, Type center_y, Type center_z, Type size_x, Type size_y, Type size_z, Type theta, Eigen::Matrix< Type, 8, 1 > *corners)
void CalculateDistAndDirToBoundary(const Eigen::Matrix< typename PointT::Type, 3, 1 > &pt, const base::PointCloud< PointT > &left_boundary, const base::PointCloud< PointT > &right_boundary, typename PointT::Type *dist, Eigen::Matrix< typename PointT::Type, 3, 1 > *dir)
bool CalculateDistAndDirToSegs(const Eigen::Matrix< typename PointT::Type, 3, 1 > &pt, const base::PointCloud< PointT > &segs, typename PointT::Type *dist, Eigen::Matrix< typename PointT::Type, 3, 1 > *dir)
Type CalculateIou2DXY(const Eigen::Matrix< Type, 3, 1 > ¢er0, const Eigen::Matrix< Type, 3, 1 > &size0, const Eigen::Matrix< Type, 3, 1 > ¢er1, const Eigen::Matrix< Type, 3, 1 > &size1)
bool IsLineSegments2DIntersect(const Eigen::Matrix< T, 2, 1 > &p1, const Eigen::Matrix< T, 2, 1 > &q1, const Eigen::Matrix< T, 2, 1 > &p2, const Eigen::Matrix< T, 2, 1 > &q2)