28 namespace perception {
39 pt_start[0] = x_start;
40 pt_start[1] = y_start;
47 this->pt_end[0] = ls.
pt_end[0];
48 this->pt_end[1] = ls.
pt_end[1];
53 this->pt_end[0] = ls.
pt_end[0];
54 this->pt_end[1] = ls.
pt_end[1];
63 diff1 = diff1 < 0 ? -diff1 : diff1;
65 diff2 = diff2 < 0 ? -diff2 : diff2;
67 diff3 = diff3 < 0 ? -diff3 : diff3;
68 return std::min<T>(std::min<T>(diff1, diff2), diff3);
79 angle = std::min(angle, pi - angle);
87 const T &x_max,
const T &y_max) {
88 T overlap_x = std::min(bbox_ref[2], x_max) - std::max(bbox_ref[0], x_min);
89 T overlap_y = std::min(bbox_ref[3], y_max) - std::max(bbox_ref[1], y_min);
90 if (overlap_y <= (T)0 || overlap_x <= (T)0) {
93 T inter_area = overlap_x * overlap_y;
94 T union_area = (y_max - y_min) * (x_max - x_min) +
95 (bbox_ref[3] - bbox_ref[1]) * (bbox_ref[2] - bbox_ref[0]) -
100 template <
typename T>
102 rot[0] =
static_cast<T
>(
cos(ry));
103 rot[2] =
static_cast<T
>(
sin(ry));
104 rot[4] =
static_cast<T
>(1.0f);
105 rot[6] =
static_cast<T
>(-
sin(ry));
106 rot[8] =
static_cast<T
>(
cos(ry));
107 rot[1] = rot[3] = rot[5] = rot[7] =
static_cast<T
>(0);
110 template <
typename T>
111 void GenCorners(T h, T w, T l, T *x_cor, T *y_cor, T *z_cor) {
112 T half_w =
static_cast<T
>(w * 0.5f);
113 T half_l =
static_cast<T
>(l * 0.5f);
114 y_cor[0] = y_cor[1] = y_cor[2] = y_cor[3] = (T)0;
115 y_cor[4] = y_cor[5] = y_cor[6] = y_cor[7] = -h;
116 x_cor[0] = x_cor[4] = half_l;
117 z_cor[0] = z_cor[4] = half_w;
118 x_cor[1] = x_cor[5] = half_l;
119 z_cor[1] = z_cor[5] = -half_w;
120 x_cor[2] = x_cor[6] = -half_l;
121 z_cor[2] = z_cor[6] = -half_w;
122 x_cor[3] = x_cor[7] = -half_l;
123 z_cor[3] = z_cor[7] = half_w;
126 template <
typename T>
127 bool Occlude(
const T *bbox1,
const T &h1,
const T *bbox2,
const T &h2) {
128 T overlap_x = std::min(bbox1[2], bbox2[2]) - std::max(bbox1[0], bbox2[0]);
129 T overlap_y = std::min(bbox1[3], bbox2[3]) - std::max(bbox1[1], bbox2[1]);
130 if (overlap_y <= (T)0 || overlap_x <= (T)0) {
133 const T HEIGHT_BIG_MOT =
static_cast<T
>(3.0f);
134 const T OCC_RATIO = (T)0.1f;
135 T bh1 = bbox1[3] - bbox1[1];
136 T bh2 = bbox2[3] - bbox2[1];
137 T inter_area = overlap_x * overlap_y;
138 T area = bh2 * (bbox2[2] - bbox2[0]);
140 T thres_occ_ratio = h2 < HEIGHT_BIG_MOT ? 2 * OCC_RATIO : OCC_RATIO;
141 bool occ = occ_ratio > thres_occ_ratio &&
146 template <
typename T>
148 bbox[0] = bbox[2] = pts[0];
149 bbox[1] = bbox[3] = pts[1];
151 for (
int i = 1; i < n; ++i) {
154 bbox[0] = std::min(bbox[0], x);
155 bbox[1] = std::min(bbox[1], y);
156 bbox[2] = std::max(bbox[2], x);
157 bbox[3] = std::max(bbox[3], y);
162 template <
typename T>
167 for (
int i = 1; i < n; ++i) {
168 if (vec[i] < min_val) {
176 template <
typename T>
178 const T *bbox,
const T *rot,
const T *hwl,
179 const T *location,
const bool &check_truncation,
180 T *bbox_res =
nullptr, T *bbox_near =
nullptr) {
184 T x_corners[8] = {0};
185 T y_corners[8] = {0};
186 T z_corners[8] = {0};
187 GenCorners(h, w, l, x_corners, y_corners, z_corners);
189 T x_min = std::numeric_limits<float>::max();
190 T x_max = -std::numeric_limits<float>::max();
191 T y_min = std::numeric_limits<float>::max();
192 T y_max = -std::numeric_limits<float>::max();
196 for (
int i = 0; i < 8; ++i) {
197 x_box[0] = x_corners[i];
198 x_box[1] = y_corners[i];
199 x_box[2] = z_corners[i];
204 if (bbox_near !=
nullptr) {
206 pts_proj[i2] = x_proj[0];
207 pts_proj[i2 + 1] = x_proj[1];
209 x_min = std::min(x_min, x_proj[0]);
210 x_max = std::max(x_max, x_proj[0]);
211 y_min = std::min(y_min, x_proj[1]);
212 y_max = std::max(y_max, x_proj[1]);
213 if (check_truncation) {
214 x_min = std::min(std::max(x_min, (T)0), (T)(width - 1));
215 x_max = std::min(std::max(x_max, (T)0), (T)(width - 1));
216 y_min = std::min(std::max(y_min, (T)0), (T)(height - 1));
217 y_max = std::min(std::max(y_max, (T)0), (T)(height - 1));
220 if (bbox_res !=
nullptr) {
226 if (bbox_near !=
nullptr) {
227 T bbox_front[4] = {0};
228 T bbox_rear[4] = {0};
229 std::swap(pts_proj[4], pts_proj[8]);
230 std::swap(pts_proj[5], pts_proj[9]);
231 std::swap(pts_proj[6], pts_proj[10]);
232 std::swap(pts_proj[7], pts_proj[11]);
235 if (bbox_rear[3] - bbox_rear[1] > bbox_front[3] - bbox_front[1]) {
236 memcpy(bbox_near, bbox_rear,
sizeof(T) * 4);
238 memcpy(bbox_near, bbox_front,
sizeof(T) * 4);
244 template <
typename T>
245 bool CheckXY(
const T &x,
const T &y,
int width,
int height) {
246 T max_x =
static_cast<T
>(width - 1);
247 T max_y =
static_cast<T
>(height - 1);
248 return x <= max_x && x >= (T)0 && y <= max_y && y >= (T)0;
251 template <
typename T>
253 const std::pair<T, T> &range, T *z_offset) {
254 ACHECK(range.first < range.second);
255 if (x_start > x_end) {
256 std::swap(x_start, x_end);
257 std::swap(z_start, z_end);
260 T x_check_l = std::max(x_start, range.first);
261 T x_check_r = std::min(x_end, range.second);
262 T overlap_x = x_check_r - x_check_l;
263 if (overlap_x < 1e-6) {
267 T dz_divide_dx = (z_end - z_start) *
common::IRec(x_end - x_start);
268 T z_check_l = z_start + (x_check_l - x_start) * dz_divide_dx;
269 T z_check_r = z_start + (x_check_r - x_start) * dz_divide_dx;
270 T z_nearest = std::min(z_check_l, z_check_r);
271 if (z_nearest < *z_offset) {
272 *z_offset = z_nearest;
276 template <
typename T>
278 const T *plane,
const T *pts_c,
279 const T *k_mat,
int width,
int height,
280 T ratio_x_over_z, T *dx_dz,
281 bool check_lowerbound =
true) {
282 const T Z_RESOLUTION =
static_cast<T
>(1e-1);
283 const T Z_UNSTABLE_RATIO =
static_cast<T
>(0.3f);
285 dx_dz[0] = dx_dz[1] = (T)0;
291 T pt_of_line_seg_l[3] = {0};
292 T pt_of_line_seg_r[3] = {0};
294 ls.
pt_start, k_mat, plane, pt_of_line_seg_l);
296 ls.
pt_end, k_mat, plane, pt_of_line_seg_r);
297 if (!is_front_l || !is_front_r) {
300 ADEBUG <<
"l&r on-ground points: " << pt_of_line_seg_l[0] <<
", " 301 << pt_of_line_seg_l[1] <<
", " << pt_of_line_seg_l[2] <<
" | " 302 << pt_of_line_seg_r[0] <<
", " << pt_of_line_seg_r[1] <<
", " 303 << pt_of_line_seg_r[2];
308 T pos[3] = {pt_of_line_seg_l[0], pt_of_line_seg_l[1], pt_of_line_seg_l[2]};
309 T v[3] = {pt_of_line_seg_r[0] - pt_of_line_seg_l[0],
310 pt_of_line_seg_r[1] - pt_of_line_seg_l[1],
311 pt_of_line_seg_r[2] - pt_of_line_seg_l[2]};
312 T ry =
static_cast<T
>(-atan2(v[2], v[0]));
321 T pt2[3] = {ratio_x_over_z, (T)0, (T)1};
322 T pt1_local[3] = {0};
323 T pt2_local[3] = {0};
326 std::pair<T, T> range;
329 T pts_local[12] = {0};
337 T x[2] = {pt1_local[0], pt1_local[2]};
338 T xp[2] = {pt2_local[0], pt2_local[2]};
341 T zs[4] = {pts_local[2], pts_local[5], pts_local[8], pts_local[11]};
342 T z_offset =
static_cast<T
>(0);
343 bool all_positive = zs[0] > 0.0f && zs[1] > 0.f && zs[2] > 0.f && zs[3] > 0.f;
345 z_offset = std::min(zs[0], std::min(zs[1], std::min(zs[2], zs[3])));
347 z_offset = std::max(zs[0], std::max(zs[1], std::max(zs[2], zs[3])));
348 T xs[4] = {pts_local[0], pts_local[3], pts_local[6], pts_local[9]};
358 if (z_offset < Z_RESOLUTION && z_offset > -Z_RESOLUTION) {
363 if (z_offset > static_cast<T>(0.0f) && check_lowerbound) {
368 center_c[0] = (pts_c[0] + pts_c[3] + pts_c[6] + pts_c[9]) / 4;
369 center_c[2] = (pts_c[2] + pts_c[5] + pts_c[8] + pts_c[11]) / 4;
370 T z_avg = center_c[2];
372 T dz_local = -z_offset;
376 T center_local[3] = {0};
378 center_local[0] += dx_local;
380 center_local[2] += dz_local;
381 T center_local_to_c[3] = {0};
384 dx_dz[0] = center_local_to_c[0] - center_c[0];
385 dx_dz[1] = center_local_to_c[2] - center_c[2];
387 T dz_max = z_avg * Z_UNSTABLE_RATIO;
388 if (z_offset > static_cast<T>(0.0f) &&
std::abs(dx_dz[1]) > dz_max) {
389 dx_dz[0] = dx_dz[1] = 0;
int GetMinIndexVec(const T *vec, const int &n)
Definition: twod_threed_util.h:163
void IProjectThroughKRt(const T *K, const T *R, const T *t, const T *X, T *x)
Definition: i_util.h:121
#define ACHECK(cond)
Definition: log.h:80
LineSegment2D(const LineSegment2D &ls)
Definition: twod_threed_util.h:44
PlanningContext is the runtime context in planning. It is persistent across multiple frames...
Definition: atomic_hash_map.h:25
float IRec(float a)
Definition: i_basic.h:69
LineSegment2D(const T &x_start, const T &y_start, const T &x_end, const T &y_end)
Definition: twod_threed_util.h:37
float ISqr(float a)
Definition: i_basic.h:103
void UpdateOffsetZ(T x_start, T z_start, T x_end, T z_end, const std::pair< T, T > &range, T *z_offset)
Definition: twod_threed_util.h:252
T abs(const T &x)
Definition: misc.h:48
const double PI
Definition: const_var.h:77
T GetJaccardIndex(const T *bbox_ref, const T &x_min, const T &y_min, const T &x_max, const T &y_max)
Definition: twod_threed_util.h:86
void IProjectThroughExtrinsic(const T *R, const T *t, const T *X, T *x)
Definition: i_util.h:103
void ISub3(const T x[3], const T y[3], T z[3])
Definition: i_blas.h:1405
void ITranspose3x3(T A[9])
Definition: i_blas.h:4250
T GetSharpAngle(const T &a, const T &b)
Definition: twod_threed_util.h:72
void IMultAx3x3(const T A[9], const T x[3], T Ax[3])
Definition: i_blas.h:4358
#define ADEBUG
Definition: log.h:41
void ILineFit2d(const T *x, const T *xp, T *l)
Definition: i_line.h:68
bool CheckXY(const T &x, const T &y, int width, int height)
Definition: twod_threed_util.h:245
void GenCorners(T h, T w, T l, T *x_cor, T *y_cor, T *z_cor)
Definition: twod_threed_util.h:111
void GenRotMatrix(const T &ry, T *rot)
Definition: twod_threed_util.h:101
T CalAngleDiff(const T a1, const T a2)
Definition: twod_threed_util.h:61
Definition: twod_threed_util.h:33
float ISqrt(float a)
Definition: i_basic.h:76
bool IBackprojectPlaneIntersectionCanonical(const T *x, const T *K, const T *pi, T *X)
Definition: i_util.h:197
T GetScoreViaRotDimensionCenter(const T *k_mat, int width, int height, const T *bbox, const T *rot, const T *hwl, const T *location, const bool &check_truncation, T *bbox_res=nullptr, T *bbox_near=nullptr)
Definition: twod_threed_util.h:177
void GetDxDzForCenterFromGroundLineSeg(const LineSegment2D< T > &ls, const T *plane, const T *pts_c, const T *k_mat, int width, int height, T ratio_x_over_z, T *dx_dz, bool check_lowerbound=true)
Definition: twod_threed_util.h:277
T pt_end[2]
Definition: twod_threed_util.h:35
bool Occlude(const T *bbox1, const T &h1, const T *bbox2, const T &h2)
Definition: twod_threed_util.h:127
void IScale3(T x[3], T sf)
Definition: i_blas.h:1868
void operator=(const LineSegment2D &ls)
Definition: twod_threed_util.h:50
void IMultAtx3x3(const T A[9], const T x[3], T Atx[3])
Definition: i_blas.h:4369
void GetBboxFromPts(const T *pts, const int &n, T *bbox)
Definition: twod_threed_util.h:147
T pt_start[2]
Definition: twod_threed_util.h:34