100 const double dx1 = cos_heading_ * half_length_;
101 const double dy1 = sin_heading_ * half_length_;
102 const double dx2 = sin_heading_ * half_width_;
103 const double dy2 = -cos_heading_ * half_width_;
105 corners_.emplace_back(center_.
x() + dx1 + dx2, center_.
y() + dy1 + dy2);
106 corners_.emplace_back(center_.
x() + dx1 - dx2, center_.
y() + dy1 - dy2);
107 corners_.emplace_back(center_.
x() - dx1 - dx2, center_.
y() - dy1 - dy2);
108 corners_.emplace_back(center_.
x() - dx1 + dx2, center_.
y() - dy1 + dy2);
110 for (
auto &corner : corners_) {
111 max_x_ = std::fmax(corner.x(), max_x_);
112 min_x_ = std::fmin(corner.x(), min_x_);
113 max_y_ = std::fmax(corner.y(), max_y_);
114 min_y_ = std::fmin(corner.y(), min_y_);
188 if (std::fmax(line_segment.
start().
x(), line_segment.
end().
x()) <
min_x() ||
189 std::fmin(line_segment.
start().
x(), line_segment.
end().
x()) >
max_x() ||
190 std::fmax(line_segment.
start().
y(), line_segment.
end().
y()) <
min_y() ||
191 std::fmin(line_segment.
start().
y(), line_segment.
end().
y()) >
max_y()) {
196 Vec2d x_axis(sin_heading_, -cos_heading_);
197 Vec2d y_axis(cos_heading_, sin_heading_);
199 Vec2d start_v = line_segment.
start() - corners_[2];
204 if (is_inside_rectangle(start_point)) {
208 Vec2d end_v = line_segment.
end() - corners_[2];
210 if (is_inside_rectangle(end_point)) {
215 if ((start_point.
x() < 0.0) && (end_point.
x() < 0.0)) {
218 if ((start_point.
y() < 0.0) && (end_point.
y() < 0.0)) {
221 if ((start_point.
x() > width_) && (end_point.
x() > width_)) {
224 if ((start_point.
y() > length_) && (end_point.
y() > length_)) {
228 Vec2d line_direction = line_segment.
end() - line_segment.
start();
229 Vec2d normal_vec(line_direction.
y(), -line_direction.
x());
231 Vec2d diagonal_vec = center_ - corners_[0];
233 double project_p1 = fabs(p1.
InnerProd(normal_vec));
234 if (fabs(diagonal_vec.
InnerProd(normal_vec)) >= project_p1) {
237 diagonal_vec = center_ - corners_[1];
238 if (fabs(diagonal_vec.
InnerProd(normal_vec)) >= project_p1) {
248 const double ref_x1 = line_segment.
start().
x() - center_.
x();
249 const double ref_y1 = line_segment.
start().
y() - center_.
y();
250 double x1 = ref_x1 * cos_heading_ + ref_y1 * sin_heading_;
251 double y1 = ref_x1 * sin_heading_ - ref_y1 * cos_heading_;
252 double box_x = half_length_;
253 double box_y = half_width_;
254 int gx1 = (x1 >= box_x ? 1 : (x1 <= -box_x ? -1 : 0));
255 int gy1 = (y1 >= box_y ? 1 : (y1 <= -box_y ? -1 : 0));
256 if (gx1 == 0 && gy1 == 0) {
259 const double ref_x2 = line_segment.
end().
x() - center_.
x();
260 const double ref_y2 = line_segment.
end().
y() - center_.
y();
261 double x2 = ref_x2 * cos_heading_ + ref_y2 * sin_heading_;
262 double y2 = ref_x2 * sin_heading_ - ref_y2 * cos_heading_;
263 int gx2 = (x2 >= box_x ? 1 : (x2 <= -box_x ? -1 : 0));
264 int gy2 = (y2 >= box_y ? 1 : (y2 <= -box_y ? -1 : 0));
265 if (gx2 == 0 && gy2 == 0) {
268 if (gx1 < 0 || (gx1 == 0 && gx2 < 0)) {
274 if (gy1 < 0 || (gy1 == 0 && gy2 < 0)) {
280 if (gx1 < gy1 || (gx1 == gy1 && gx2 < gy2)) {
285 std::swap(box_x, box_y);
287 if (gx1 == 1 && gy1 == 1) {
288 switch (gx2 * 3 + gy2) {
290 return PtSegDistance(box_x, box_y, x1, y1, x2, y2,
293 return (x1 > x2) ? (x2 - box_x)
294 : PtSegDistance(box_x, box_y, x1, y1, x2, y2,
297 return (x1 > x2) ? PtSegDistance(box_x, -box_y, x1, y1, x2, y2,
299 : PtSegDistance(box_x, box_y, x1, y1, x2, y2,
302 return CrossProd({x1, y1}, {x2, y2}, {box_x, -box_y}) >= 0.0
304 : PtSegDistance(box_x, -box_y, x1, y1, x2, y2,
307 return CrossProd({x1, y1}, {x2, y2}, {box_x, -box_y}) <= 0.0
308 ? PtSegDistance(box_x, -box_y, x1, y1, x2, y2,
310 : (
CrossProd({x1, y1}, {x2, y2}, {-box_x, box_y}) <= 0.0
312 : PtSegDistance(-box_x, box_y, x1, y1, x2, y2,
316 switch (gx2 * 3 + gy2) {
318 return (x1 < x2) ? (x1 - box_x)
319 : PtSegDistance(box_x, box_y, x1, y1, x2, y2,
322 return std::min(x1, x2) - box_x;
325 return CrossProd({x1, y1}, {x2, y2}, {box_x, box_y}) <= 0.0
327 : PtSegDistance(box_x, box_y, x1, y1, x2, y2,
333 ACHECK(0) <<
"unimplemented state: " << gx1 <<
" " << gy1 <<
" " << gx2 <<
" "
348 const double shift_x = box.
center_x() - center_.
x();
349 const double shift_y = box.
center_y() - center_.
y();
351 const double dx1 = cos_heading_ * half_length_;
352 const double dy1 = sin_heading_ * half_length_;
353 const double dx2 = sin_heading_ * half_width_;
354 const double dy2 = -cos_heading_ * half_width_;
360 return std::abs(shift_x * cos_heading_ + shift_y * sin_heading_) <=
361 std::abs(dx3 * cos_heading_ + dy3 * sin_heading_) +
362 std::abs(dx4 * cos_heading_ + dy4 * sin_heading_) +
364 std::abs(shift_x * sin_heading_ - shift_y * cos_heading_) <=
365 std::abs(dx3 * sin_heading_ - dy3 * cos_heading_) +
366 std::abs(dx4 * sin_heading_ - dy4 * cos_heading_) +