6 Point::Point(double x, double y) : x_(x), y_(y)
10 Point::Point() : Point::Point(0.0, 0.0)
39 Point::min_angle_between(Point const& p1, Point const& p2) const
41 double d1x = p1.x() - this->x();
42 double d1y = p1.y() - this->y();
43 double d2x = p2.x() - p1.x();
44 double d2y = p2.y() - p1.y();
46 double dot = d1x*d2x + d1y*d2y;
47 double d1 = sqrt(d1x*d1x + d1y*d1y);
48 double d2 = sqrt(d2x*d2x + d2y*d2y);
50 double delta = acos(dot / (d1 * d2));
51 return std::min(delta, M_PI - delta);
55 Point::inside_of(std::vector<Point> const& poly) const
57 unsigned int num = poly.size();
58 unsigned int j = num - 1;
60 for (unsigned int i = 0; i < num; i++) {
61 if (this->x() == poly[i].x() && this->y() == poly[i].y()) {
64 if ((poly[i].y() > this->y()) != (poly[j].y() > this->y())) {
65 auto slope1 = this->x() - poly[i].x();
66 slope1 *= poly[j].y() - poly[i].y();
67 auto slope2 = poly[j].x() - poly[i].x();
68 slope2 *= this->y() - poly[i].y();
69 auto slope = slope1 - slope2;
73 if ((slope < 0.0) != (poly[j].y() < poly[i].y())) {
82 Line::Line(Point const& fp, Point const& lp): first(fp), last(lp),
83 intersection1(Point(0.0, 0.0)), intersection2(Point(0.0, 0.0))
102 return this->intersection1;
108 return this->intersection2;
112 Line::intersects_with(Line const& li)
114 auto x1 = this->fp().x();
115 auto y1 = this->fp().y();
116 auto x2 = this->lp().x();
117 auto y2 = this->lp().y();
118 auto x3 = li.fp().x();
119 auto y3 = li.fp().y();
120 auto x4 = li.lp().x();
121 auto y4 = li.lp().y();
122 double deno = (x1 - x2) * (y3 - y4) - (y1 - y2) * (x3 - x4);
126 double t = (x1 - x3) * (y3 - y4) - (y1 - y3) * (x3 - x4);
128 double u = (x1 - x2) * (y1 - y3) - (y1 - y2) * (x1 - x3);
131 if (t < 0.0 || t > 1.0 || u < 0.0 || u > 1.0) {
134 this->intersection1.x(x1 + t * (x2 - x1));
135 this->intersection1.y(y1 + t * (y2 - y1));
140 Line::intersects_with(Point const& c, double const r)
142 auto x1 = this->fp().x();
143 auto y1 = this->fp().y();
144 auto x2 = this->lp().x();
145 auto y2 = this->lp().y();
157 double dr = sqrt(dx*dx + dy*dy);
158 double D = x1*y2 - x2*y1;
159 if (r*r * dr*dr - D*D < 0.0) {
162 // intersection coordinates
163 double ix1 = (D*dy + sgn(dy)*dx*sqrt(r*r * dr*dr - D*D)) / (dr*dr);
165 double ix2 = (D*dy - sgn(dy)*dx*sqrt(r*r * dr*dr - D*D)) / (dr*dr);
167 double iy1 = (-D*dx + std::abs(dy)*sqrt(r*r * dr*dr - D*D)) / (dr*dr);
169 double iy2 = (-D*dx - std::abs(dy)*sqrt(r*r * dr*dr - D*D)) / (dr*dr);
171 this->intersection1.x(ix1);
172 this->intersection1.y(iy1);
173 this->intersection2.x(ix2);
174 this->intersection2.y(iy2);
179 Line::is_on_right_side(Point const& p) const
181 auto x1 = this->fp().x();
182 auto y1 = this->fp().y();
183 auto x2 = this->lp().x();
184 auto y2 = this->lp().y();
187 if (sgn((x3 - x1) * (y2 - y1) - (y3 - y1) * (x2 - x1)) < 0.0) {
197 double dx = this->lp().x() - this->fp().x();
198 double dy = this->lp().y() - this->fp().y();
199 return sqrt(dx * dx + dy * dy);
202 Pose::Pose() : Point()
206 Pose::Pose(double x, double y, double h) : Point(x, y), h_(h)
229 Pose::set_pose(Pose const& p)
237 Pose::rotate(Point const& c, double const angl)
239 double px = this->x();
240 double py = this->y();
243 double nx = px * cos(angl) - py * sin(angl);
244 double ny = px * sin(angl) + py * cos(angl);
245 this->h(this->h() + angl);
251 operator<<(std::ostream& out, Pose const& p)
253 out << "[" << p.x() << "," << p.y() << "," << p.h() << "]";
264 PoseRange::b(double b)
276 PoseRange::e(double e)
288 PoseRange::rotate(Point const& c, double const angl)
290 Pose::rotate(c, angl);
291 this->e(this->e() + angl);
295 operator<<(std::ostream& out, PoseRange const& p)
297 out << "[" << p.x() << "," << p.y() << "," << p.b() << "," << p.e();
305 return this->curb_to_curb;
309 CarSize::ctc(double ctc)
311 this->curb_to_curb = ctc;
317 return this->wheelbase;
321 CarSize::wb(double wb)
323 this->wheelbase = wb;
345 CarSize::len(double len)
353 return this->distance_to_front;
357 CarSize::df(double df)
359 this->distance_to_front = df;
365 return this->len() - this->df();
371 auto ctc2 = pow(this->ctc() / 2.0, 2.0);
372 auto wb2 = pow(this->wb(), 2.0);
373 return sqrt(ctc2 - wb2) - this->w() / 2.0;
377 CarSize::iradi() const
379 return this->mtr() - this->w() / 2;
383 CarSize::ofradi() const
385 auto mtrw2 = pow(this->mtr() + this->w() / 2.0, 2.0);
386 auto df2 = pow(this->df(), 2.0);
387 return sqrt(mtrw2 + df2);
391 CarSize::orradi() const
393 auto mtrw2 = pow(this->mtr() + this->w() / 2.0, 2.0);
394 auto dr2 = pow(this->dr(), 2.0);
395 return sqrt(mtrw2 + dr2);
399 CarSize::perfect_parking_slot_len() const
401 auto r = this->ctc() / 2.0;
403 auto k = this->df() - this->wb();
405 auto r2l2 = r * r - l * l;
406 auto s = r2l2 + pow(l + k, 2.0) - pow(sqrt(r2l2) - w, 2.0);
407 return this->len() + sqrt(s) - l - k;
417 CarMove::sp(double sp)
429 CarMove::st(double st)
435 BicycleCar::drivable(Pose const& p) const
442 return this->drivable(pr);
446 BicycleCar::drivable(PoseRange const& p) const
448 double h = (p.b() + p.e()) / 2.0;
449 double a_1 = atan2(p.y() - this->y(), p.x() - this->x()) - this->h();
454 double h_d = h - this->h();
460 if (h_d == 0 && (a_1 == 0 || a_2 == M_PI || a_2 == -M_PI)) {
462 } else if (0 < a_1 && a_1 <= M_PI/2) { // left front
463 BicycleCar z(*this); // zone border
466 z.rotate(this->ccl(), h_d);
468 if (p.y() == z.y() && p.x() == z.x()) // p on zone border
470 a_2 = atan2(p.y() - z.y(), p.x() - z.x());
475 if (z.h() >= a_2 && a_2 >= this->h())
477 } else if (M_PI/2 < a_1 && a_1 <= M_PI) { // left rear
478 BicycleCar z(*this); // zone border
481 z.rotate(this->ccl(), h_d);
483 if (p.y() == z.y() && p.x() == z.x()) // p on zone border
485 a_2 = atan2(p.y() - z.y(), p.x() - z.x());
491 if (this->h() >= a_2 && a_2 >= z.h())
493 } else if (0 > a_1 && a_1 >= -M_PI/2) { // right front
494 BicycleCar z(*this); // zone border
497 z.rotate(this->ccr(), h_d);
499 if (p.y() == z.y() && p.x() == z.x()) // p on zone border
501 a_2 = atan2(p.y() - z.y(), p.x() - z.x());
506 if (this->h() >= a_2 && a_2 >= z.h())
508 } else if (-M_PI/2 > a_1 && a_1 >= -M_PI) { // right rear
509 BicycleCar z(*this); // zone border
512 z.rotate(this->ccr(), h_d);
514 if (p.y() == z.y() && p.x() == z.x()) // p on zone border
516 a_2 = atan2(p.y() - z.y(), p.x() - z.x());
522 if (z.h() >= a_2 && a_2 >= this->h())
525 // Not happenning, as ``-pi <= a <= pi``.
531 BicycleCar::set_max_steer()
533 this->st(atan(this->wb() / this->mtr()));
537 BicycleCar::lfx() const
539 double lfx = this->x();
540 lfx += (this->w() / 2.0) * cos(this->h() + M_PI / 2.0);
541 lfx += this->df() * cos(this->h());
546 BicycleCar::lfy() const
548 double lfy = this->y();
549 lfy += (this->w() / 2.0) * sin(this->h() + M_PI / 2.0);
550 lfy += this->df() * sin(this->h());
555 BicycleCar::lrx() const
557 double lrx = this->x();
558 lrx += (this->w() / 2.0) * cos(this->h() + M_PI / 2.0);
559 lrx += -this->dr() * cos(this->h());
564 BicycleCar::lry() const
566 double lry = this->y();
567 lry += (this->w() / 2.0) * sin(this->h() + M_PI / 2.0);
568 lry += -this->dr() * sin(this->h());
573 BicycleCar::rrx() const
575 double rrx = this->x();
576 rrx += (this->w() / 2.0) * cos(this->h() - M_PI / 2.0);
577 rrx += -this->dr() * cos(this->h());
582 BicycleCar::rry() const
584 double rry = this->y();
585 rry += (this->w() / 2.0) * sin(this->h() - M_PI / 2.0);
586 rry += -this->dr() * sin(this->h());
591 BicycleCar::rfx() const
593 double rfx = this->x();
594 rfx += (this->w() / 2.0) * cos(this->h() - M_PI / 2.0);
595 rfx += this->df() * cos(this->h());
600 BicycleCar::rfy() const
602 double rfy = this->y();
603 rfy += (this->w() / 2.0) * sin(this->h() - M_PI / 2.0);
604 rfy += this->df() * sin(this->h());
609 BicycleCar::ralx() const
611 double lrx = this->x();
612 lrx += (this->w() / 2.0) * cos(this->h() + M_PI / 2.0);
616 BicycleCar::raly() const
618 double lry = this->y();
619 lry += (this->w() / 2.0) * sin(this->h() + M_PI / 2.0);
624 BicycleCar::rarx() const
626 double rrx = this->x();
627 rrx += (this->w() / 2.0) * cos(this->h() - M_PI / 2.0);
632 BicycleCar::rary() const
634 double rry = this->y();
635 rry += (this->w() / 2.0) * sin(this->h() - M_PI / 2.0);
640 BicycleCar::ccl() const
643 this->x() + this->mtr() * cos(this->h() + M_PI / 2.0),
644 this->y() + this->mtr() * sin(this->h() + M_PI / 2.0)
649 BicycleCar::ccr() const
652 this->x() + this->mtr() * cos(this->h() - M_PI / 2.0),
653 this->y() + this->mtr() * sin(this->h() - M_PI / 2.0)
660 this->x(this->x() + this->sp() * cos(this->h()));
661 this->y(this->y() + this->sp() * sin(this->h()));
662 this->h(this->h() + this->sp() / this->wb() * tan(this->st()));