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 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& fp, Point const& lp): first(fp), last(lp),
125 intersection1(Point(0.0, 0.0)), intersection2(Point(0.0, 0.0))
144 return this->intersection1;
150 return this->intersection2;
154 Line::intersects_with(Line const& li)
156 auto x1 = this->fp().x();
157 auto y1 = this->fp().y();
158 auto x2 = this->lp().x();
159 auto y2 = this->lp().y();
160 auto x3 = li.fp().x();
161 auto y3 = li.fp().y();
162 auto x4 = li.lp().x();
163 auto y4 = li.lp().y();
164 double deno = (x1 - x2) * (y3 - y4) - (y1 - y2) * (x3 - x4);
168 double t = (x1 - x3) * (y3 - y4) - (y1 - y3) * (x3 - x4);
170 double u = (x1 - x2) * (y1 - y3) - (y1 - y2) * (x1 - x3);
173 if (t < 0.0 || t > 1.0 || u < 0.0 || u > 1.0) {
176 this->intersection1.x(x1 + t * (x2 - x1));
177 this->intersection1.y(y1 + t * (y2 - y1));
182 Line::intersects_with(Point const& c, double const r)
184 auto x1 = this->fp().x();
185 auto y1 = this->fp().y();
186 auto x2 = this->lp().x();
187 auto y2 = this->lp().y();
199 double dr = sqrt(dx*dx + dy*dy);
200 double D = x1*y2 - x2*y1;
201 if (r*r * dr*dr - D*D < 0.0) {
204 // intersection coordinates
205 double ix1 = (D*dy + sgn(dy)*dx*sqrt(r*r * dr*dr - D*D)) / (dr*dr);
207 double ix2 = (D*dy - sgn(dy)*dx*sqrt(r*r * dr*dr - D*D)) / (dr*dr);
209 double iy1 = (-D*dx + std::abs(dy)*sqrt(r*r * dr*dr - D*D)) / (dr*dr);
211 double iy2 = (-D*dx - std::abs(dy)*sqrt(r*r * dr*dr - D*D)) / (dr*dr);
213 this->intersection1.x(ix1);
214 this->intersection1.y(iy1);
215 this->intersection2.x(ix2);
216 this->intersection2.y(iy2);
223 double dx = this->lp().x() - this->fp().x();
224 double dy = this->lp().y() - this->fp().y();
225 return sqrt(dx * dx + dy * dy);
229 operator<<(std::ostream& out, Line const& li)
231 out << "[" << li.first << "," << li.last << "]";
235 Pose::Pose() : Point()
239 Pose::Pose(double x, double y, double h) : Point(x, y), h_(h)
262 Pose::set_pose(Pose const& p)
270 Pose::rotate(Point const& c, double const angl)
272 Point::rotate(c, angl);
273 this->h(this->h() + angl);
277 operator<<(std::ostream& out, Pose const& p)
279 out << "[" << p.x() << "," << p.y() << "," << p.h() << "]";
290 PoseRange::b(double b)
302 PoseRange::e(double e)
314 PoseRange::rotate(Point const& c, double const angl)
316 Pose::rotate(c, angl);
317 this->e(this->e() + angl);
321 operator<<(std::ostream& out, PoseRange const& p)
323 out << "[" << p.x() << "," << p.y() << "," << p.b() << "," << p.e();
331 return this->curb_to_curb;
335 CarSize::ctc(double ctc)
337 this->curb_to_curb = ctc;
343 return this->wheelbase;
347 CarSize::wb(double wb)
349 this->wheelbase = wb;
371 CarSize::len(double len)
379 return this->distance_to_front;
383 CarSize::df(double df)
385 this->distance_to_front = df;
391 return this->len() - this->df();
397 auto ctc2 = pow(this->ctc() / 2.0, 2.0);
398 auto wb2 = pow(this->wb(), 2.0);
399 return sqrt(ctc2 - wb2) - this->w() / 2.0;
403 CarSize::iradi() const
405 return this->mtr() - this->w() / 2;
409 CarSize::ofradi() const
411 auto mtrw2 = pow(this->mtr() + this->w() / 2.0, 2.0);
412 auto df2 = pow(this->df(), 2.0);
413 return sqrt(mtrw2 + df2);
417 CarSize::orradi() const
419 auto mtrw2 = pow(this->mtr() + this->w() / 2.0, 2.0);
420 auto dr2 = pow(this->dr(), 2.0);
421 return sqrt(mtrw2 + dr2);
425 CarSize::perfect_parking_slot_len() const
427 auto r = this->ctc() / 2.0;
429 auto k = this->df() - this->wb();
431 auto r2l2 = r * r - l * l;
432 auto s = r2l2 + pow(l + k, 2.0) - pow(sqrt(r2l2) - w, 2.0);
433 return this->len() + sqrt(s) - l - k;
443 CarMove::sp(double sp)
455 CarMove::st(double st)
461 BicycleCar::drivable(Pose const& p) const
468 return this->drivable(pr);
472 BicycleCar::drivable(PoseRange const& p) const
474 double h = (p.b() + p.e()) / 2.0;
475 double a_1 = atan2(p.y() - this->y(), p.x() - this->x()) - this->h();
480 double h_d = h - this->h();
486 if (h_d == 0 && (a_1 == 0 || a_2 == M_PI || a_2 == -M_PI)) {
488 } else if (0 < a_1 && a_1 <= M_PI/2) { // left front
489 BicycleCar z(*this); // zone border
492 z.rotate(this->ccl(), h_d);
494 if (p.y() == z.y() && p.x() == z.x()) // p on zone border
496 a_2 = atan2(p.y() - z.y(), p.x() - z.x());
501 if (z.h() >= a_2 && a_2 >= this->h())
503 } else if (M_PI/2 < a_1 && a_1 <= M_PI) { // left rear
504 BicycleCar z(*this); // zone border
507 z.rotate(this->ccl(), h_d);
509 if (p.y() == z.y() && p.x() == z.x()) // p on zone border
511 a_2 = atan2(p.y() - z.y(), p.x() - z.x());
517 if (this->h() >= a_2 && a_2 >= z.h())
519 } else if (0 > a_1 && a_1 >= -M_PI/2) { // right front
520 BicycleCar z(*this); // zone border
523 z.rotate(this->ccr(), h_d);
525 if (p.y() == z.y() && p.x() == z.x()) // p on zone border
527 a_2 = atan2(p.y() - z.y(), p.x() - z.x());
532 if (this->h() >= a_2 && a_2 >= z.h())
534 } else if (-M_PI/2 > a_1 && a_1 >= -M_PI) { // right rear
535 BicycleCar z(*this); // zone border
538 z.rotate(this->ccr(), h_d);
540 if (p.y() == z.y() && p.x() == z.x()) // p on zone border
542 a_2 = atan2(p.y() - z.y(), p.x() - z.x());
548 if (z.h() >= a_2 && a_2 >= this->h())
551 // Not happenning, as ``-pi <= a <= pi``.
557 BicycleCar::set_max_steer()
559 this->st(atan(this->wb() / this->mtr()));
563 BicycleCar::lfx() const
565 double lfx = this->x();
566 lfx += (this->w() / 2.0) * cos(this->h() + M_PI / 2.0);
567 lfx += this->df() * cos(this->h());
572 BicycleCar::lfy() const
574 double lfy = this->y();
575 lfy += (this->w() / 2.0) * sin(this->h() + M_PI / 2.0);
576 lfy += this->df() * sin(this->h());
581 BicycleCar::lrx() const
583 double lrx = this->x();
584 lrx += (this->w() / 2.0) * cos(this->h() + M_PI / 2.0);
585 lrx += -this->dr() * cos(this->h());
590 BicycleCar::lry() const
592 double lry = this->y();
593 lry += (this->w() / 2.0) * sin(this->h() + M_PI / 2.0);
594 lry += -this->dr() * sin(this->h());
599 BicycleCar::rrx() const
601 double rrx = this->x();
602 rrx += (this->w() / 2.0) * cos(this->h() - M_PI / 2.0);
603 rrx += -this->dr() * cos(this->h());
608 BicycleCar::rry() const
610 double rry = this->y();
611 rry += (this->w() / 2.0) * sin(this->h() - M_PI / 2.0);
612 rry += -this->dr() * sin(this->h());
617 BicycleCar::rfx() const
619 double rfx = this->x();
620 rfx += (this->w() / 2.0) * cos(this->h() - M_PI / 2.0);
621 rfx += this->df() * cos(this->h());
626 BicycleCar::rfy() const
628 double rfy = this->y();
629 rfy += (this->w() / 2.0) * sin(this->h() - M_PI / 2.0);
630 rfy += this->df() * sin(this->h());
635 BicycleCar::lf() const
637 return Point(this->lfx(), this->lfy());
641 BicycleCar::lr() const
643 return Point(this->lrx(), this->lry());
647 BicycleCar::rr() const
649 return Point(this->rrx(), this->rry());
653 BicycleCar::rf() const
655 return Point(this->rfx(), this->rfy());
659 BicycleCar::left() const
661 return Line(this->lr(), this->lf());
665 BicycleCar::rear() const
667 return Line(this->lr(), this->rr());
671 BicycleCar::right() const
673 return Line(this->rr(), this->rf());
677 BicycleCar::front() const
679 return Line(this->rf(), this->lf());
683 BicycleCar::ralx() const
685 double lrx = this->x();
686 lrx += (this->w() / 2.0) * cos(this->h() + M_PI / 2.0);
690 BicycleCar::raly() const
692 double lry = this->y();
693 lry += (this->w() / 2.0) * sin(this->h() + M_PI / 2.0);
698 BicycleCar::rarx() const
700 double rrx = this->x();
701 rrx += (this->w() / 2.0) * cos(this->h() - M_PI / 2.0);
706 BicycleCar::rary() const
708 double rry = this->y();
709 rry += (this->w() / 2.0) * sin(this->h() - M_PI / 2.0);
714 BicycleCar::ccl() const
717 this->x() + this->mtr() * cos(this->h() + M_PI / 2.0),
718 this->y() + this->mtr() * sin(this->h() + M_PI / 2.0)
723 BicycleCar::ccr() const
726 this->x() + this->mtr() * cos(this->h() - M_PI / 2.0),
727 this->y() + this->mtr() * sin(this->h() - M_PI / 2.0)
734 this->x(this->x() + this->sp() * cos(this->h()));
735 this->y(this->y() + this->sp() * sin(this->h()));
736 this->h(this->h() + this->sp() / this->wb() * tan(this->st()));