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);
222 Pose::Pose() : Point()
226 Pose::Pose(double x, double y, double h) : Point(x, y), h_(h)
249 Pose::set_pose(Pose const& p)
257 Pose::rotate(Point const& c, double const angl)
259 Point::rotate(c, angl);
260 this->h(this->h() + angl);
264 operator<<(std::ostream& out, Pose const& p)
266 out << "[" << p.x() << "," << p.y() << "," << p.h() << "]";
277 PoseRange::b(double b)
289 PoseRange::e(double e)
301 PoseRange::rotate(Point const& c, double const angl)
303 Pose::rotate(c, angl);
304 this->e(this->e() + angl);
308 operator<<(std::ostream& out, PoseRange const& p)
310 out << "[" << p.x() << "," << p.y() << "," << p.b() << "," << p.e();
318 return this->curb_to_curb;
322 CarSize::ctc(double ctc)
324 this->curb_to_curb = ctc;
330 return this->wheelbase;
334 CarSize::wb(double wb)
336 this->wheelbase = wb;
358 CarSize::len(double len)
366 return this->distance_to_front;
370 CarSize::df(double df)
372 this->distance_to_front = df;
378 return this->len() - this->df();
384 auto ctc2 = pow(this->ctc() / 2.0, 2.0);
385 auto wb2 = pow(this->wb(), 2.0);
386 return sqrt(ctc2 - wb2) - this->w() / 2.0;
390 CarSize::iradi() const
392 return this->mtr() - this->w() / 2;
396 CarSize::ofradi() const
398 auto mtrw2 = pow(this->mtr() + this->w() / 2.0, 2.0);
399 auto df2 = pow(this->df(), 2.0);
400 return sqrt(mtrw2 + df2);
404 CarSize::orradi() const
406 auto mtrw2 = pow(this->mtr() + this->w() / 2.0, 2.0);
407 auto dr2 = pow(this->dr(), 2.0);
408 return sqrt(mtrw2 + dr2);
412 CarSize::perfect_parking_slot_len() const
414 auto r = this->ctc() / 2.0;
416 auto k = this->df() - this->wb();
418 auto r2l2 = r * r - l * l;
419 auto s = r2l2 + pow(l + k, 2.0) - pow(sqrt(r2l2) - w, 2.0);
420 return this->len() + sqrt(s) - l - k;
430 CarMove::sp(double sp)
442 CarMove::st(double st)
448 BicycleCar::drivable(Pose const& p) const
455 return this->drivable(pr);
459 BicycleCar::drivable(PoseRange const& p) const
461 double h = (p.b() + p.e()) / 2.0;
462 double a_1 = atan2(p.y() - this->y(), p.x() - this->x()) - this->h();
467 double h_d = h - this->h();
473 if (h_d == 0 && (a_1 == 0 || a_2 == M_PI || a_2 == -M_PI)) {
475 } else if (0 < a_1 && a_1 <= M_PI/2) { // left front
476 BicycleCar z(*this); // zone border
479 z.rotate(this->ccl(), h_d);
481 if (p.y() == z.y() && p.x() == z.x()) // p on zone border
483 a_2 = atan2(p.y() - z.y(), p.x() - z.x());
488 if (z.h() >= a_2 && a_2 >= this->h())
490 } else if (M_PI/2 < a_1 && a_1 <= M_PI) { // left rear
491 BicycleCar z(*this); // zone border
494 z.rotate(this->ccl(), h_d);
496 if (p.y() == z.y() && p.x() == z.x()) // p on zone border
498 a_2 = atan2(p.y() - z.y(), p.x() - z.x());
504 if (this->h() >= a_2 && a_2 >= z.h())
506 } else if (0 > a_1 && a_1 >= -M_PI/2) { // right front
507 BicycleCar z(*this); // zone border
510 z.rotate(this->ccr(), h_d);
512 if (p.y() == z.y() && p.x() == z.x()) // p on zone border
514 a_2 = atan2(p.y() - z.y(), p.x() - z.x());
519 if (this->h() >= a_2 && a_2 >= z.h())
521 } else if (-M_PI/2 > a_1 && a_1 >= -M_PI) { // right rear
522 BicycleCar z(*this); // zone border
525 z.rotate(this->ccr(), h_d);
527 if (p.y() == z.y() && p.x() == z.x()) // p on zone border
529 a_2 = atan2(p.y() - z.y(), p.x() - z.x());
535 if (z.h() >= a_2 && a_2 >= this->h())
538 // Not happenning, as ``-pi <= a <= pi``.
544 BicycleCar::set_max_steer()
546 this->st(atan(this->wb() / this->mtr()));
550 BicycleCar::lfx() const
552 double lfx = this->x();
553 lfx += (this->w() / 2.0) * cos(this->h() + M_PI / 2.0);
554 lfx += this->df() * cos(this->h());
559 BicycleCar::lfy() const
561 double lfy = this->y();
562 lfy += (this->w() / 2.0) * sin(this->h() + M_PI / 2.0);
563 lfy += this->df() * sin(this->h());
568 BicycleCar::lrx() const
570 double lrx = this->x();
571 lrx += (this->w() / 2.0) * cos(this->h() + M_PI / 2.0);
572 lrx += -this->dr() * cos(this->h());
577 BicycleCar::lry() const
579 double lry = this->y();
580 lry += (this->w() / 2.0) * sin(this->h() + M_PI / 2.0);
581 lry += -this->dr() * sin(this->h());
586 BicycleCar::rrx() const
588 double rrx = this->x();
589 rrx += (this->w() / 2.0) * cos(this->h() - M_PI / 2.0);
590 rrx += -this->dr() * cos(this->h());
595 BicycleCar::rry() const
597 double rry = this->y();
598 rry += (this->w() / 2.0) * sin(this->h() - M_PI / 2.0);
599 rry += -this->dr() * sin(this->h());
604 BicycleCar::rfx() const
606 double rfx = this->x();
607 rfx += (this->w() / 2.0) * cos(this->h() - M_PI / 2.0);
608 rfx += this->df() * cos(this->h());
613 BicycleCar::rfy() const
615 double rfy = this->y();
616 rfy += (this->w() / 2.0) * sin(this->h() - M_PI / 2.0);
617 rfy += this->df() * sin(this->h());
622 BicycleCar::ralx() const
624 double lrx = this->x();
625 lrx += (this->w() / 2.0) * cos(this->h() + M_PI / 2.0);
629 BicycleCar::raly() const
631 double lry = this->y();
632 lry += (this->w() / 2.0) * sin(this->h() + M_PI / 2.0);
637 BicycleCar::rarx() const
639 double rrx = this->x();
640 rrx += (this->w() / 2.0) * cos(this->h() - M_PI / 2.0);
645 BicycleCar::rary() const
647 double rry = this->y();
648 rry += (this->w() / 2.0) * sin(this->h() - M_PI / 2.0);
653 BicycleCar::ccl() const
656 this->x() + this->mtr() * cos(this->h() + M_PI / 2.0),
657 this->y() + this->mtr() * sin(this->h() + M_PI / 2.0)
662 BicycleCar::ccr() const
665 this->x() + this->mtr() * cos(this->h() - M_PI / 2.0),
666 this->y() + this->mtr() * sin(this->h() - M_PI / 2.0)
673 this->x(this->x() + this->sp() * cos(this->h()));
674 this->y(this->y() + this->sp() * sin(this->h()));
675 this->h(this->h() + this->sp() / this->wb() * tan(this->st()));