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 operator<<(std::ostream& out, Point const& p)
101 out << "[" << p.x() << "," << p.y() << "]";
105 Line::Line(Point const& fp, Point const& lp): first(fp), last(lp),
106 intersection1(Point(0.0, 0.0)), intersection2(Point(0.0, 0.0))
125 return this->intersection1;
131 return this->intersection2;
135 Line::intersects_with(Line const& li)
137 auto x1 = this->fp().x();
138 auto y1 = this->fp().y();
139 auto x2 = this->lp().x();
140 auto y2 = this->lp().y();
141 auto x3 = li.fp().x();
142 auto y3 = li.fp().y();
143 auto x4 = li.lp().x();
144 auto y4 = li.lp().y();
145 double deno = (x1 - x2) * (y3 - y4) - (y1 - y2) * (x3 - x4);
149 double t = (x1 - x3) * (y3 - y4) - (y1 - y3) * (x3 - x4);
151 double u = (x1 - x2) * (y1 - y3) - (y1 - y2) * (x1 - x3);
154 if (t < 0.0 || t > 1.0 || u < 0.0 || u > 1.0) {
157 this->intersection1.x(x1 + t * (x2 - x1));
158 this->intersection1.y(y1 + t * (y2 - y1));
163 Line::intersects_with(Point const& c, double const r)
165 auto x1 = this->fp().x();
166 auto y1 = this->fp().y();
167 auto x2 = this->lp().x();
168 auto y2 = this->lp().y();
180 double dr = sqrt(dx*dx + dy*dy);
181 double D = x1*y2 - x2*y1;
182 if (r*r * dr*dr - D*D < 0.0) {
185 // intersection coordinates
186 double ix1 = (D*dy + sgn(dy)*dx*sqrt(r*r * dr*dr - D*D)) / (dr*dr);
188 double ix2 = (D*dy - sgn(dy)*dx*sqrt(r*r * dr*dr - D*D)) / (dr*dr);
190 double iy1 = (-D*dx + std::abs(dy)*sqrt(r*r * dr*dr - D*D)) / (dr*dr);
192 double iy2 = (-D*dx - std::abs(dy)*sqrt(r*r * dr*dr - D*D)) / (dr*dr);
194 this->intersection1.x(ix1);
195 this->intersection1.y(iy1);
196 this->intersection2.x(ix2);
197 this->intersection2.y(iy2);
204 double dx = this->lp().x() - this->fp().x();
205 double dy = this->lp().y() - this->fp().y();
206 return sqrt(dx * dx + dy * dy);
209 Pose::Pose() : Point()
213 Pose::Pose(double x, double y, double h) : Point(x, y), h_(h)
236 Pose::set_pose(Pose const& p)
244 Pose::rotate(Point const& c, double const angl)
246 double px = this->x();
247 double py = this->y();
250 double nx = px * cos(angl) - py * sin(angl);
251 double ny = px * sin(angl) + py * cos(angl);
252 this->h(this->h() + angl);
258 operator<<(std::ostream& out, Pose const& p)
260 out << "[" << p.x() << "," << p.y() << "," << p.h() << "]";
271 PoseRange::b(double b)
283 PoseRange::e(double e)
295 PoseRange::rotate(Point const& c, double const angl)
297 Pose::rotate(c, angl);
298 this->e(this->e() + angl);
302 operator<<(std::ostream& out, PoseRange const& p)
304 out << "[" << p.x() << "," << p.y() << "," << p.b() << "," << p.e();
312 return this->curb_to_curb;
316 CarSize::ctc(double ctc)
318 this->curb_to_curb = ctc;
324 return this->wheelbase;
328 CarSize::wb(double wb)
330 this->wheelbase = wb;
352 CarSize::len(double len)
360 return this->distance_to_front;
364 CarSize::df(double df)
366 this->distance_to_front = df;
372 return this->len() - this->df();
378 auto ctc2 = pow(this->ctc() / 2.0, 2.0);
379 auto wb2 = pow(this->wb(), 2.0);
380 return sqrt(ctc2 - wb2) - this->w() / 2.0;
384 CarSize::iradi() const
386 return this->mtr() - this->w() / 2;
390 CarSize::ofradi() const
392 auto mtrw2 = pow(this->mtr() + this->w() / 2.0, 2.0);
393 auto df2 = pow(this->df(), 2.0);
394 return sqrt(mtrw2 + df2);
398 CarSize::orradi() const
400 auto mtrw2 = pow(this->mtr() + this->w() / 2.0, 2.0);
401 auto dr2 = pow(this->dr(), 2.0);
402 return sqrt(mtrw2 + dr2);
406 CarSize::perfect_parking_slot_len() const
408 auto r = this->ctc() / 2.0;
410 auto k = this->df() - this->wb();
412 auto r2l2 = r * r - l * l;
413 auto s = r2l2 + pow(l + k, 2.0) - pow(sqrt(r2l2) - w, 2.0);
414 return this->len() + sqrt(s) - l - k;
424 CarMove::sp(double sp)
436 CarMove::st(double st)
442 BicycleCar::drivable(Pose const& p) const
449 return this->drivable(pr);
453 BicycleCar::drivable(PoseRange const& p) const
455 double h = (p.b() + p.e()) / 2.0;
456 double a_1 = atan2(p.y() - this->y(), p.x() - this->x()) - this->h();
461 double h_d = h - this->h();
467 if (h_d == 0 && (a_1 == 0 || a_2 == M_PI || a_2 == -M_PI)) {
469 } else if (0 < a_1 && a_1 <= M_PI/2) { // left front
470 BicycleCar z(*this); // zone border
473 z.rotate(this->ccl(), h_d);
475 if (p.y() == z.y() && p.x() == z.x()) // p on zone border
477 a_2 = atan2(p.y() - z.y(), p.x() - z.x());
482 if (z.h() >= a_2 && a_2 >= this->h())
484 } else if (M_PI/2 < a_1 && a_1 <= M_PI) { // left rear
485 BicycleCar z(*this); // zone border
488 z.rotate(this->ccl(), h_d);
490 if (p.y() == z.y() && p.x() == z.x()) // p on zone border
492 a_2 = atan2(p.y() - z.y(), p.x() - z.x());
498 if (this->h() >= a_2 && a_2 >= z.h())
500 } else if (0 > a_1 && a_1 >= -M_PI/2) { // right front
501 BicycleCar z(*this); // zone border
504 z.rotate(this->ccr(), h_d);
506 if (p.y() == z.y() && p.x() == z.x()) // p on zone border
508 a_2 = atan2(p.y() - z.y(), p.x() - z.x());
513 if (this->h() >= a_2 && a_2 >= z.h())
515 } else if (-M_PI/2 > a_1 && a_1 >= -M_PI) { // right rear
516 BicycleCar z(*this); // zone border
519 z.rotate(this->ccr(), h_d);
521 if (p.y() == z.y() && p.x() == z.x()) // p on zone border
523 a_2 = atan2(p.y() - z.y(), p.x() - z.x());
529 if (z.h() >= a_2 && a_2 >= this->h())
532 // Not happenning, as ``-pi <= a <= pi``.
538 BicycleCar::set_max_steer()
540 this->st(atan(this->wb() / this->mtr()));
544 BicycleCar::lfx() const
546 double lfx = this->x();
547 lfx += (this->w() / 2.0) * cos(this->h() + M_PI / 2.0);
548 lfx += this->df() * cos(this->h());
553 BicycleCar::lfy() const
555 double lfy = this->y();
556 lfy += (this->w() / 2.0) * sin(this->h() + M_PI / 2.0);
557 lfy += this->df() * sin(this->h());
562 BicycleCar::lrx() const
564 double lrx = this->x();
565 lrx += (this->w() / 2.0) * cos(this->h() + M_PI / 2.0);
566 lrx += -this->dr() * cos(this->h());
571 BicycleCar::lry() const
573 double lry = this->y();
574 lry += (this->w() / 2.0) * sin(this->h() + M_PI / 2.0);
575 lry += -this->dr() * sin(this->h());
580 BicycleCar::rrx() const
582 double rrx = this->x();
583 rrx += (this->w() / 2.0) * cos(this->h() - M_PI / 2.0);
584 rrx += -this->dr() * cos(this->h());
589 BicycleCar::rry() const
591 double rry = this->y();
592 rry += (this->w() / 2.0) * sin(this->h() - M_PI / 2.0);
593 rry += -this->dr() * sin(this->h());
598 BicycleCar::rfx() const
600 double rfx = this->x();
601 rfx += (this->w() / 2.0) * cos(this->h() - M_PI / 2.0);
602 rfx += this->df() * cos(this->h());
607 BicycleCar::rfy() const
609 double rfy = this->y();
610 rfy += (this->w() / 2.0) * sin(this->h() - M_PI / 2.0);
611 rfy += this->df() * sin(this->h());
616 BicycleCar::ralx() const
618 double lrx = this->x();
619 lrx += (this->w() / 2.0) * cos(this->h() + M_PI / 2.0);
623 BicycleCar::raly() const
625 double lry = this->y();
626 lry += (this->w() / 2.0) * sin(this->h() + M_PI / 2.0);
631 BicycleCar::rarx() const
633 double rrx = this->x();
634 rrx += (this->w() / 2.0) * cos(this->h() - M_PI / 2.0);
639 BicycleCar::rary() const
641 double rry = this->y();
642 rry += (this->w() / 2.0) * sin(this->h() - M_PI / 2.0);
647 BicycleCar::ccl() const
650 this->x() + this->mtr() * cos(this->h() + M_PI / 2.0),
651 this->y() + this->mtr() * sin(this->h() + M_PI / 2.0)
656 BicycleCar::ccr() const
659 this->x() + this->mtr() * cos(this->h() - M_PI / 2.0),
660 this->y() + this->mtr() * sin(this->h() - M_PI / 2.0)
667 this->x(this->x() + this->sp() * cos(this->h()));
668 this->y(this->y() + this->sp() * sin(this->h()));
669 this->h(this->h() + this->sp() / this->wb() * tan(this->st()));