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())) {
83 Point::on_right_side_of(Line const& li) const
85 auto x1 = li.fp().x();
86 auto y1 = li.fp().y();
87 auto x2 = li.lp().x();
88 auto y2 = li.lp().y();
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 operator<<(std::ostream& out, Point const& p)
114 out << "[" << p.x() << "," << p.y() << "]";
118 Line::Line(Point const& fp, Point const& lp): first(fp), last(lp),
119 intersection1(Point(0.0, 0.0)), intersection2(Point(0.0, 0.0))
138 return this->intersection1;
144 return this->intersection2;
148 Line::intersects_with(Line const& li)
150 auto x1 = this->fp().x();
151 auto y1 = this->fp().y();
152 auto x2 = this->lp().x();
153 auto y2 = this->lp().y();
154 auto x3 = li.fp().x();
155 auto y3 = li.fp().y();
156 auto x4 = li.lp().x();
157 auto y4 = li.lp().y();
158 double deno = (x1 - x2) * (y3 - y4) - (y1 - y2) * (x3 - x4);
162 double t = (x1 - x3) * (y3 - y4) - (y1 - y3) * (x3 - x4);
164 double u = (x1 - x2) * (y1 - y3) - (y1 - y2) * (x1 - x3);
167 if (t < 0.0 || t > 1.0 || u < 0.0 || u > 1.0) {
170 this->intersection1.x(x1 + t * (x2 - x1));
171 this->intersection1.y(y1 + t * (y2 - y1));
176 Line::intersects_with(Point const& c, double const r)
178 auto x1 = this->fp().x();
179 auto y1 = this->fp().y();
180 auto x2 = this->lp().x();
181 auto y2 = this->lp().y();
193 double dr = sqrt(dx*dx + dy*dy);
194 double D = x1*y2 - x2*y1;
195 if (r*r * dr*dr - D*D < 0.0) {
198 // intersection coordinates
199 double ix1 = (D*dy + sgn(dy)*dx*sqrt(r*r * dr*dr - D*D)) / (dr*dr);
201 double ix2 = (D*dy - sgn(dy)*dx*sqrt(r*r * dr*dr - D*D)) / (dr*dr);
203 double iy1 = (-D*dx + std::abs(dy)*sqrt(r*r * dr*dr - D*D)) / (dr*dr);
205 double iy2 = (-D*dx - std::abs(dy)*sqrt(r*r * dr*dr - D*D)) / (dr*dr);
207 this->intersection1.x(ix1);
208 this->intersection1.y(iy1);
209 this->intersection2.x(ix2);
210 this->intersection2.y(iy2);
217 double dx = this->lp().x() - this->fp().x();
218 double dy = this->lp().y() - this->fp().y();
219 return sqrt(dx * dx + dy * dy);
223 operator<<(std::ostream& out, Line const& li)
225 out << "[" << li.first << "," << li.last << "]";
229 Pose::Pose() : Point()
233 Pose::Pose(double x, double y, double h) : Point(x, y), h_(h)
256 Pose::set_pose(Pose const& p)
264 Pose::rotate(Point const& c, double const angl)
266 Point::rotate(c, angl);
267 this->h(this->h() + angl);
271 operator<<(std::ostream& out, Pose const& p)
273 out << "[" << p.x() << "," << p.y() << "," << p.h() << "]";
284 PoseRange::b(double b)
296 PoseRange::e(double e)
308 PoseRange::rotate(Point const& c, double const angl)
310 Pose::rotate(c, angl);
311 this->e(this->e() + angl);
315 operator<<(std::ostream& out, PoseRange const& p)
317 out << "[" << p.x() << "," << p.y() << "," << p.b() << "," << p.e();
325 return this->curb_to_curb;
329 CarSize::ctc(double ctc)
331 this->curb_to_curb = ctc;
337 return this->wheelbase;
341 CarSize::wb(double wb)
343 this->wheelbase = wb;
365 CarSize::len(double len)
373 return this->distance_to_front;
377 CarSize::df(double df)
379 this->distance_to_front = df;
385 return this->len() - this->df();
391 auto ctc2 = pow(this->ctc() / 2.0, 2.0);
392 auto wb2 = pow(this->wb(), 2.0);
393 return sqrt(ctc2 - wb2) - this->w() / 2.0;
397 CarSize::iradi() const
399 return this->mtr() - this->w() / 2;
403 CarSize::ofradi() const
405 auto mtrw2 = pow(this->mtr() + this->w() / 2.0, 2.0);
406 auto df2 = pow(this->df(), 2.0);
407 return sqrt(mtrw2 + df2);
411 CarSize::orradi() const
413 auto mtrw2 = pow(this->mtr() + this->w() / 2.0, 2.0);
414 auto dr2 = pow(this->dr(), 2.0);
415 return sqrt(mtrw2 + dr2);
419 CarSize::perfect_parking_slot_len() const
421 auto r = this->ctc() / 2.0;
423 auto k = this->df() - this->wb();
425 auto r2l2 = r * r - l * l;
426 auto s = r2l2 + pow(l + k, 2.0) - pow(sqrt(r2l2) - w, 2.0);
427 return this->len() + sqrt(s) - l - k;
437 CarMove::sp(double sp)
449 CarMove::st(double st)
455 BicycleCar::drivable(Pose const& p) const
462 return this->drivable(pr);
466 BicycleCar::drivable(PoseRange const& p) const
468 double h = (p.b() + p.e()) / 2.0;
469 double a_1 = atan2(p.y() - this->y(), p.x() - this->x()) - this->h();
474 double h_d = h - this->h();
480 if (h_d == 0 && (a_1 == 0 || a_2 == M_PI || a_2 == -M_PI)) {
482 } else if (0 < a_1 && a_1 <= M_PI/2) { // left front
483 BicycleCar z(*this); // zone border
486 z.rotate(this->ccl(), h_d);
488 if (p.y() == z.y() && p.x() == z.x()) // p on zone border
490 a_2 = atan2(p.y() - z.y(), p.x() - z.x());
495 if (z.h() >= a_2 && a_2 >= this->h())
497 } else if (M_PI/2 < a_1 && a_1 <= M_PI) { // left rear
498 BicycleCar z(*this); // zone border
501 z.rotate(this->ccl(), h_d);
503 if (p.y() == z.y() && p.x() == z.x()) // p on zone border
505 a_2 = atan2(p.y() - z.y(), p.x() - z.x());
511 if (this->h() >= a_2 && a_2 >= z.h())
513 } else if (0 > a_1 && a_1 >= -M_PI/2) { // right front
514 BicycleCar z(*this); // zone border
517 z.rotate(this->ccr(), h_d);
519 if (p.y() == z.y() && p.x() == z.x()) // p on zone border
521 a_2 = atan2(p.y() - z.y(), p.x() - z.x());
526 if (this->h() >= a_2 && a_2 >= z.h())
528 } else if (-M_PI/2 > a_1 && a_1 >= -M_PI) { // right rear
529 BicycleCar z(*this); // zone border
532 z.rotate(this->ccr(), h_d);
534 if (p.y() == z.y() && p.x() == z.x()) // p on zone border
536 a_2 = atan2(p.y() - z.y(), p.x() - z.x());
542 if (z.h() >= a_2 && a_2 >= this->h())
545 // Not happenning, as ``-pi <= a <= pi``.
551 BicycleCar::set_max_steer()
553 this->st(atan(this->wb() / this->mtr()));
557 BicycleCar::lfx() const
559 double lfx = this->x();
560 lfx += (this->w() / 2.0) * cos(this->h() + M_PI / 2.0);
561 lfx += this->df() * cos(this->h());
566 BicycleCar::lfy() const
568 double lfy = this->y();
569 lfy += (this->w() / 2.0) * sin(this->h() + M_PI / 2.0);
570 lfy += this->df() * sin(this->h());
575 BicycleCar::lrx() const
577 double lrx = this->x();
578 lrx += (this->w() / 2.0) * cos(this->h() + M_PI / 2.0);
579 lrx += -this->dr() * cos(this->h());
584 BicycleCar::lry() const
586 double lry = this->y();
587 lry += (this->w() / 2.0) * sin(this->h() + M_PI / 2.0);
588 lry += -this->dr() * sin(this->h());
593 BicycleCar::rrx() const
595 double rrx = this->x();
596 rrx += (this->w() / 2.0) * cos(this->h() - M_PI / 2.0);
597 rrx += -this->dr() * cos(this->h());
602 BicycleCar::rry() const
604 double rry = this->y();
605 rry += (this->w() / 2.0) * sin(this->h() - M_PI / 2.0);
606 rry += -this->dr() * sin(this->h());
611 BicycleCar::rfx() const
613 double rfx = this->x();
614 rfx += (this->w() / 2.0) * cos(this->h() - M_PI / 2.0);
615 rfx += this->df() * cos(this->h());
620 BicycleCar::rfy() const
622 double rfy = this->y();
623 rfy += (this->w() / 2.0) * sin(this->h() - M_PI / 2.0);
624 rfy += this->df() * sin(this->h());
629 BicycleCar::ralx() const
631 double lrx = this->x();
632 lrx += (this->w() / 2.0) * cos(this->h() + M_PI / 2.0);
636 BicycleCar::raly() const
638 double lry = this->y();
639 lry += (this->w() / 2.0) * sin(this->h() + M_PI / 2.0);
644 BicycleCar::rarx() const
646 double rrx = this->x();
647 rrx += (this->w() / 2.0) * cos(this->h() - M_PI / 2.0);
652 BicycleCar::rary() const
654 double rry = this->y();
655 rry += (this->w() / 2.0) * sin(this->h() - M_PI / 2.0);
660 BicycleCar::ccl() const
663 this->x() + this->mtr() * cos(this->h() + M_PI / 2.0),
664 this->y() + this->mtr() * sin(this->h() + M_PI / 2.0)
669 BicycleCar::ccr() const
672 this->x() + this->mtr() * cos(this->h() - M_PI / 2.0),
673 this->y() + this->mtr() * sin(this->h() - M_PI / 2.0)
680 this->x(this->x() + this->sp() * cos(this->h()));
681 this->y(this->y() + this->sp() * sin(this->h()));
682 this->h(this->h() + this->sp() / this->wb() * tan(this->st()));