10 Point::Point(double x, double y) : x_(x), y_(y)
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())) {
83 Point::on_right_side_of(Line const& li) const
91 if (sgn((x3 - x1) * (y2 - y1) - (y3 - y1) * (x2 - x1)) < 0.0) {
99 Point::rotate(Point const& c, double const angl)
101 double px = this->x();
102 double py = this->y();
105 double nx = px * cos(angl) - py * sin(angl);
106 double ny = px * sin(angl) + py * cos(angl);
112 Point::edist(Point const& p) const
114 return sqrt(pow(p.x() - this->x_, 2.0) + pow(p.y() - this->y_, 2.0));
118 operator<<(std::ostream& out, Point const& p)
120 out << "[" << p.x() << "," << p.y() << "]";
124 Line::Line(Point const& b, Point const& e): b_(b), e_(e)
153 Line::intersects_with(Line const& li)
155 auto x1 = this->b_.x();
156 auto y1 = this->b_.y();
157 auto x2 = this->e_.x();
158 auto y2 = this->e_.y();
159 auto x3 = li.b().x();
160 auto y3 = li.b().y();
161 auto x4 = li.e().x();
162 auto y4 = li.e().y();
163 double deno = (x1 - x2) * (y3 - y4) - (y1 - y2) * (x3 - x4);
167 double t = (x1 - x3) * (y3 - y4) - (y1 - y3) * (x3 - x4);
169 double u = (x1 - x2) * (y1 - y3) - (y1 - y2) * (x1 - x3);
172 if (t < 0.0 || t > 1.0 || u < 0.0 || u > 1.0) {
175 this->i1_.x(x1 + t * (x2 - x1));
176 this->i1_.y(y1 + t * (y2 - y1));
181 Line::intersects_with(Point const& c, double const r)
183 auto x1 = this->b_.x();
184 auto y1 = this->b_.y();
185 auto x2 = this->e_.x();
186 auto y2 = this->e_.y();
198 double dr = sqrt(dx*dx + dy*dy);
199 double D = x1*y2 - x2*y1;
200 if (r*r * dr*dr - D*D < 0.0) {
203 // intersection coordinates
204 double ix1 = (D*dy + sgn(dy)*dx*sqrt(r*r * dr*dr - D*D)) / (dr*dr);
206 double ix2 = (D*dy - sgn(dy)*dx*sqrt(r*r * dr*dr - D*D)) / (dr*dr);
208 double iy1 = (-D*dx + std::abs(dy)*sqrt(r*r * dr*dr - D*D)) / (dr*dr);
210 double iy2 = (-D*dx - std::abs(dy)*sqrt(r*r * dr*dr - D*D)) / (dr*dr);
222 return this->b_.edist(this->e_);
226 operator<<(std::ostream& out, Line const& li)
228 out << "[" << li.b_ << "," << li.e_ << "]";
232 Pose::Pose(double x, double y, double h) : Point(x, y), h_(h)
255 Pose::set_pose(Pose const& p)
263 Pose::rotate(Point const& c, double const angl)
265 Point::rotate(c, angl);
266 this->h(this->h() + angl);
270 operator<<(std::ostream& out, Pose const& p)
272 out << "[" << p.x() << "," << p.y() << "," << p.h() << "]";
283 PoseRange::b(double b)
295 PoseRange::e(double e)
307 PoseRange::rotate(Point const& c, double const angl)
309 Pose::rotate(c, angl);
310 this->e(this->e() + angl);
314 operator<<(std::ostream& out, PoseRange const& p)
316 out << "[" << p.x() << "," << p.y() << "," << p.b() << "," << p.e();
324 return this->curb_to_curb_;
328 CarSize::ctc(double ctc)
330 this->curb_to_curb_ = ctc;
336 return this->wheelbase_;
340 CarSize::wb(double wb)
342 this->wheelbase_ = wb;
360 return this->length_;
364 CarSize::len(double len)
372 return this->distance_to_front_;
376 CarSize::df(double df)
378 this->distance_to_front_ = df;
384 return this->len() - this->df();
390 auto ctc2 = pow(this->ctc() / 2.0, 2.0);
391 auto wb2 = pow(this->wb(), 2.0);
392 return sqrt(ctc2 - wb2) - this->w() / 2.0;
396 CarSize::iradi() const
398 return this->mtr() - this->w() / 2;
402 CarSize::ofradi() const
404 auto mtrw2 = pow(this->mtr() + this->w() / 2.0, 2.0);
405 auto df2 = pow(this->df(), 2.0);
406 return sqrt(mtrw2 + df2);
410 CarSize::orradi() const
412 auto mtrw2 = pow(this->mtr() + this->w() / 2.0, 2.0);
413 auto dr2 = pow(this->dr(), 2.0);
414 return sqrt(mtrw2 + dr2);
418 CarSize::perfect_parking_slot_len() const
420 auto r = this->ctc() / 2.0;
422 auto k = this->df() - this->wb();
424 auto r2l2 = r * r - l * l;
425 auto s = r2l2 + pow(l + k, 2.0) - pow(sqrt(r2l2) - w, 2.0);
426 return this->len() + sqrt(s) - l - k;
436 CarMove::sp(double sp)
448 CarMove::st(double st)
454 BicycleCar::drivable(Pose const& p) const
461 return this->drivable(pr);
465 BicycleCar::drivable(PoseRange const& p) const
467 double h = (p.b() + p.e()) / 2.0;
468 double a_1 = atan2(p.y() - this->y(), p.x() - this->x()) - this->h();
473 double h_d = h - this->h();
479 if (h_d == 0 && (a_1 == 0 || a_2 == M_PI || a_2 == -M_PI)) {
481 } else if (0 < a_1 && a_1 <= M_PI/2) { // left front
482 BicycleCar z(*this); // zone border
485 z.rotate(this->ccl(), h_d);
487 if (p.y() == z.y() && p.x() == z.x()) // p on zone border
489 a_2 = atan2(p.y() - z.y(), p.x() - z.x());
494 if (z.h() >= a_2 && a_2 >= this->h())
496 } else if (M_PI/2 < a_1 && a_1 <= M_PI) { // left rear
497 BicycleCar z(*this); // zone border
500 z.rotate(this->ccl(), h_d);
502 if (p.y() == z.y() && p.x() == z.x()) // p on zone border
504 a_2 = atan2(p.y() - z.y(), p.x() - z.x());
510 if (this->h() >= a_2 && a_2 >= z.h())
512 } else if (0 > a_1 && a_1 >= -M_PI/2) { // right front
513 BicycleCar z(*this); // zone border
516 z.rotate(this->ccr(), h_d);
518 if (p.y() == z.y() && p.x() == z.x()) // p on zone border
520 a_2 = atan2(p.y() - z.y(), p.x() - z.x());
525 if (this->h() >= a_2 && a_2 >= z.h())
527 } else if (-M_PI/2 > a_1 && a_1 >= -M_PI) { // right rear
528 BicycleCar z(*this); // zone border
531 z.rotate(this->ccr(), h_d);
533 if (p.y() == z.y() && p.x() == z.x()) // p on zone border
535 a_2 = atan2(p.y() - z.y(), p.x() - z.x());
541 if (z.h() >= a_2 && a_2 >= this->h())
544 // Not happenning, as ``-pi <= a <= pi``.
550 BicycleCar::set_max_steer()
552 this->st(atan(this->wb() / this->mtr()));
556 BicycleCar::lfx() const
558 double lfx = this->x();
559 lfx += (this->w() / 2.0) * cos(this->h() + M_PI / 2.0);
560 lfx += this->df() * cos(this->h());
565 BicycleCar::lfy() const
567 double lfy = this->y();
568 lfy += (this->w() / 2.0) * sin(this->h() + M_PI / 2.0);
569 lfy += this->df() * sin(this->h());
574 BicycleCar::lrx() const
576 double lrx = this->x();
577 lrx += (this->w() / 2.0) * cos(this->h() + M_PI / 2.0);
578 lrx += -this->dr() * cos(this->h());
583 BicycleCar::lry() const
585 double lry = this->y();
586 lry += (this->w() / 2.0) * sin(this->h() + M_PI / 2.0);
587 lry += -this->dr() * sin(this->h());
592 BicycleCar::rrx() const
594 double rrx = this->x();
595 rrx += (this->w() / 2.0) * cos(this->h() - M_PI / 2.0);
596 rrx += -this->dr() * cos(this->h());
601 BicycleCar::rry() const
603 double rry = this->y();
604 rry += (this->w() / 2.0) * sin(this->h() - M_PI / 2.0);
605 rry += -this->dr() * sin(this->h());
610 BicycleCar::rfx() const
612 double rfx = this->x();
613 rfx += (this->w() / 2.0) * cos(this->h() - M_PI / 2.0);
614 rfx += this->df() * cos(this->h());
619 BicycleCar::rfy() const
621 double rfy = this->y();
622 rfy += (this->w() / 2.0) * sin(this->h() - M_PI / 2.0);
623 rfy += this->df() * sin(this->h());
628 BicycleCar::lf() const
630 return Point(this->lfx(), this->lfy());
634 BicycleCar::lr() const
636 return Point(this->lrx(), this->lry());
640 BicycleCar::rr() const
642 return Point(this->rrx(), this->rry());
646 BicycleCar::rf() const
648 return Point(this->rfx(), this->rfy());
652 BicycleCar::left() const
654 return Line(this->lr(), this->lf());
658 BicycleCar::rear() const
660 return Line(this->lr(), this->rr());
664 BicycleCar::right() const
666 return Line(this->rr(), this->rf());
670 BicycleCar::front() const
672 return Line(this->rf(), this->lf());
676 BicycleCar::ralx() const
678 double lrx = this->x();
679 lrx += (this->w() / 2.0) * cos(this->h() + M_PI / 2.0);
683 BicycleCar::raly() const
685 double lry = this->y();
686 lry += (this->w() / 2.0) * sin(this->h() + M_PI / 2.0);
691 BicycleCar::rarx() const
693 double rrx = this->x();
694 rrx += (this->w() / 2.0) * cos(this->h() - M_PI / 2.0);
699 BicycleCar::rary() const
701 double rry = this->y();
702 rry += (this->w() / 2.0) * sin(this->h() - M_PI / 2.0);
707 BicycleCar::ccl() const
710 this->x() + this->mtr() * cos(this->h() + M_PI / 2.0),
711 this->y() + this->mtr() * sin(this->h() + M_PI / 2.0)
716 BicycleCar::ccr() const
719 this->x() + this->mtr() * cos(this->h() - M_PI / 2.0),
720 this->y() + this->mtr() * sin(this->h() - M_PI / 2.0)
727 this->x(this->x() + this->sp() * cos(this->h()));
728 this->y(this->y() + this->sp() * sin(this->h()));
729 this->h(this->h() + this->sp() / this->wb() * tan(this->st()));