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) {
237 Pose::set_pose(Pose const& p)
245 Pose::rotate(Point const& c, double const angl)
247 double px = this->x();
248 double py = this->y();
251 double nx = px * cos(angl) - py * sin(angl);
252 double ny = px * sin(angl) + py * cos(angl);
253 this->h(this->h() + angl);
259 operator<<(std::ostream& out, Pose const& p)
261 out << "[" << p.x() << "," << p.y() << "," << p.h() << "]";
272 PoseRange::b(double b)
284 PoseRange::e(double e)
296 PoseRange::rotate(Point const& c, double const angl)
298 Pose::rotate(c, angl);
299 this->e(this->e() + angl);
303 operator<<(std::ostream& out, PoseRange const& p)
305 out << "[" << p.x() << "," << p.y() << "," << p.b() << "," << p.e();
313 return this->curb_to_curb;
317 CarSize::ctc(double ctc)
319 this->curb_to_curb = ctc;
325 return this->wheelbase;
329 CarSize::wb(double wb)
331 this->wheelbase = wb;
353 CarSize::len(double len)
361 return this->distance_to_front;
365 CarSize::df(double df)
367 this->distance_to_front = df;
373 return this->len() - this->df();
379 auto ctc2 = pow(this->ctc() / 2.0, 2.0);
380 auto wb2 = pow(this->wb(), 2.0);
381 return sqrt(ctc2 - wb2) - this->w() / 2.0;
385 CarSize::iradi() const
387 return this->mtr() - this->w() / 2;
391 CarSize::ofradi() const
393 auto mtrw2 = pow(this->mtr() + this->w() / 2.0, 2.0);
394 auto df2 = pow(this->df(), 2.0);
395 return sqrt(mtrw2 + df2);
399 CarSize::orradi() const
401 auto mtrw2 = pow(this->mtr() + this->w() / 2.0, 2.0);
402 auto dr2 = pow(this->dr(), 2.0);
403 return sqrt(mtrw2 + dr2);
407 CarSize::perfect_parking_slot_len() const
409 auto r = this->ctc() / 2.0;
411 auto k = this->df() - this->wb();
413 auto r2l2 = r * r - l * l;
414 auto s = r2l2 + pow(l + k, 2.0) - pow(sqrt(r2l2) - w, 2.0);
415 return this->len() + sqrt(s) - l - k;
425 CarMove::sp(double sp)
437 CarMove::st(double st)
443 BicycleCar::drivable(Pose const& p) const
450 return this->drivable(pr);
454 BicycleCar::drivable(PoseRange const& p) const
456 double h = (p.b() + p.e()) / 2.0;
457 double a_1 = atan2(p.y() - this->y(), p.x() - this->x()) - this->h();
462 double h_d = h - this->h();
468 if (h_d == 0 && (a_1 == 0 || a_2 == M_PI || a_2 == -M_PI)) {
470 } else if (0 < a_1 && a_1 <= M_PI/2) { // left front
471 BicycleCar z(*this); // zone border
474 z.rotate(this->ccl(), h_d);
476 if (p.y() == z.y() && p.x() == z.x()) // p on zone border
478 a_2 = atan2(p.y() - z.y(), p.x() - z.x());
483 if (z.h() >= a_2 && a_2 >= this->h())
485 } else if (M_PI/2 < a_1 && a_1 <= M_PI) { // left rear
486 BicycleCar z(*this); // zone border
489 z.rotate(this->ccl(), h_d);
491 if (p.y() == z.y() && p.x() == z.x()) // p on zone border
493 a_2 = atan2(p.y() - z.y(), p.x() - z.x());
499 if (this->h() >= a_2 && a_2 >= z.h())
501 } else if (0 > a_1 && a_1 >= -M_PI/2) { // right front
502 BicycleCar z(*this); // zone border
505 z.rotate(this->ccr(), h_d);
507 if (p.y() == z.y() && p.x() == z.x()) // p on zone border
509 a_2 = atan2(p.y() - z.y(), p.x() - z.x());
514 if (this->h() >= a_2 && a_2 >= z.h())
516 } else if (-M_PI/2 > a_1 && a_1 >= -M_PI) { // right rear
517 BicycleCar z(*this); // zone border
520 z.rotate(this->ccr(), h_d);
522 if (p.y() == z.y() && p.x() == z.x()) // p on zone border
524 a_2 = atan2(p.y() - z.y(), p.x() - z.x());
530 if (z.h() >= a_2 && a_2 >= this->h())
533 // Not happenning, as ``-pi <= a <= pi``.
539 BicycleCar::set_max_steer()
541 this->st(atan(this->wb() / this->mtr()));
545 BicycleCar::lfx() const
547 double lfx = this->x();
548 lfx += (this->w() / 2.0) * cos(this->h() + M_PI / 2.0);
549 lfx += this->df() * cos(this->h());
554 BicycleCar::lfy() const
556 double lfy = this->y();
557 lfy += (this->w() / 2.0) * sin(this->h() + M_PI / 2.0);
558 lfy += this->df() * sin(this->h());
563 BicycleCar::lrx() const
565 double lrx = this->x();
566 lrx += (this->w() / 2.0) * cos(this->h() + M_PI / 2.0);
567 lrx += -this->dr() * cos(this->h());
572 BicycleCar::lry() const
574 double lry = this->y();
575 lry += (this->w() / 2.0) * sin(this->h() + M_PI / 2.0);
576 lry += -this->dr() * sin(this->h());
581 BicycleCar::rrx() const
583 double rrx = this->x();
584 rrx += (this->w() / 2.0) * cos(this->h() - M_PI / 2.0);
585 rrx += -this->dr() * cos(this->h());
590 BicycleCar::rry() const
592 double rry = this->y();
593 rry += (this->w() / 2.0) * sin(this->h() - M_PI / 2.0);
594 rry += -this->dr() * sin(this->h());
599 BicycleCar::rfx() const
601 double rfx = this->x();
602 rfx += (this->w() / 2.0) * cos(this->h() - M_PI / 2.0);
603 rfx += this->df() * cos(this->h());
608 BicycleCar::rfy() const
610 double rfy = this->y();
611 rfy += (this->w() / 2.0) * sin(this->h() - M_PI / 2.0);
612 rfy += this->df() * sin(this->h());
617 BicycleCar::ralx() const
619 double lrx = this->x();
620 lrx += (this->w() / 2.0) * cos(this->h() + M_PI / 2.0);
624 BicycleCar::raly() const
626 double lry = this->y();
627 lry += (this->w() / 2.0) * sin(this->h() + M_PI / 2.0);
632 BicycleCar::rarx() const
634 double rrx = this->x();
635 rrx += (this->w() / 2.0) * cos(this->h() - M_PI / 2.0);
640 BicycleCar::rary() const
642 double rry = this->y();
643 rry += (this->w() / 2.0) * sin(this->h() - M_PI / 2.0);
648 BicycleCar::ccl() const
651 this->x() + this->mtr() * cos(this->h() + M_PI / 2.0),
652 this->y() + this->mtr() * sin(this->h() + M_PI / 2.0)
657 BicycleCar::ccr() const
660 this->x() + this->mtr() * cos(this->h() - M_PI / 2.0),
661 this->y() + this->mtr() * sin(this->h() - M_PI / 2.0)
668 this->x(this->x() + this->sp() * cos(this->h()));
669 this->y(this->y() + this->sp() * sin(this->h()));
670 this->h(this->h() + this->sp() / this->wb() * tan(this->st()));