2 * SPDX-FileCopyrightText: 2021 Jiri Vlasak <jiri.vlasak.2@cvut.cz>
4 * SPDX-License-Identifier: GPL-3.0-only
16 Point::Point(double x, double y) : _x(x), _y(y)
45 Point::min_angle_between(Point const& p1, Point const& p2) const
47 double d1x = p1.x() - this->x();
48 double d1y = p1.y() - this->y();
49 double d2x = p2.x() - p1.x();
50 double d2y = p2.y() - p1.y();
52 double dot = d1x*d2x + d1y*d2y;
53 double d1 = sqrt(d1x*d1x + d1y*d1y);
54 double d2 = sqrt(d2x*d2x + d2y*d2y);
56 double delta = acos(dot / (d1 * d2));
57 return std::min(delta, M_PI - delta);
61 Point::inside_of(std::vector<Point> const& poly) const
63 unsigned int num = poly.size();
64 unsigned int j = num - 1;
66 for (unsigned int i = 0; i < num; i++) {
67 if (this->x() == poly[i].x() && this->y() == poly[i].y()) {
70 if ((poly[i].y() > this->y()) != (poly[j].y() > this->y())) {
71 auto slope1 = this->x() - poly[i].x();
72 slope1 *= poly[j].y() - poly[i].y();
73 auto slope2 = poly[j].x() - poly[i].x();
74 slope2 *= this->y() - poly[i].y();
75 auto slope = slope1 - slope2;
79 if ((slope < 0.0) != (poly[j].y() < poly[i].y())) {
89 Point::inside_of(Point const& c, double const r) const
91 double dx = this->x() - c.x();
92 double dy = this->y() - c.y();
93 return pow(dx, 2.0) + pow(dy, 2.0) < pow(r, 2.0);
97 Point::on_right_side_of(Line const& li) const
100 auto y1 = li.b().y();
101 auto x2 = li.e().x();
102 auto y2 = li.e().y();
105 if (sgn((x3 - x1) * (y2 - y1) - (y3 - y1) * (x2 - x1)) < 0.0) {
113 Point::translate(Point const& p)
120 Point::rotate(Point const& c, double const angl)
122 double px = this->x();
123 double py = this->y();
126 double nx = px * cos(angl) - py * sin(angl);
127 double ny = px * sin(angl) + py * cos(angl);
133 Point::reflect(Line const& li)
135 this->rotate(li.b(), -li.h());
136 this->_y -= li.b().y();
138 this->_y += li.b().y();
139 this->rotate(li.b(), li.h());
143 Point::edist(Point const& p) const
145 return sqrt(pow(p.x() - this->_x, 2.0) + pow(p.y() - this->_y, 2.0));
149 Point::gen_gnuplot_to(std::ostream& out)
151 out << this->_x << " " << this->_y << std::endl;
155 Point::operator==(Point const& p)
157 return this->x() == p.x() && this->y() == p.y();
161 operator<<(std::ostream& out, Point const& p)
163 out << "[" << p.x() << "," << p.y() << "]";
167 Line::Line(Point const& b, Point const& e): _b(b), _e(e)
186 return Point((this->_b.x() + this->_e.x()) / 2.0,
187 (this->_b.y() + this->_e.y()) / 2.0);
203 Line::intersects_with(Line const& li)
205 auto x1 = this->_b.x();
206 auto y1 = this->_b.y();
207 auto x2 = this->_e.x();
208 auto y2 = this->_e.y();
209 auto x3 = li.b().x();
210 auto y3 = li.b().y();
211 auto x4 = li.e().x();
212 auto y4 = li.e().y();
213 double deno = (x1 - x2) * (y3 - y4) - (y1 - y2) * (x3 - x4);
217 double t = (x1 - x3) * (y3 - y4) - (y1 - y3) * (x3 - x4);
219 double u = (x1 - x2) * (y1 - y3) - (y1 - y2) * (x1 - x3);
222 if (t < 0.0 || t > 1.0 || u < 0.0 || u > 1.0) {
225 this->_i1.x(x1 + t * (x2 - x1));
226 this->_i1.y(y1 + t * (y2 - y1));
231 Line::intersects_with(Point const& c, double const r)
233 auto x1 = this->_b.x();
234 auto y1 = this->_b.y();
235 auto x2 = this->_e.x();
236 auto y2 = this->_e.y();
248 double dr = sqrt(dx*dx + dy*dy);
249 double D = x1*y2 - x2*y1;
250 if (r*r * dr*dr - D*D < 0.0) {
253 // intersection coordinates
254 double ix1 = (D*dy + sgn(dy)*dx*sqrt(r*r * dr*dr - D*D)) / (dr*dr);
256 double ix2 = (D*dy - sgn(dy)*dx*sqrt(r*r * dr*dr - D*D)) / (dr*dr);
258 double iy1 = (-D*dx + std::abs(dy)*sqrt(r*r * dr*dr - D*D)) / (dr*dr);
260 double iy2 = (-D*dx - std::abs(dy)*sqrt(r*r * dr*dr - D*D)) / (dr*dr);
272 return this->_b.edist(this->_e);
278 return atan2(this->_e.y() - this->_b.y(), this->_e.x() - this->_b.x());
282 Line::gen_gnuplot_to(std::ostream& out)
284 this->b().gen_gnuplot_to(out);
285 this->e().gen_gnuplot_to(out);
290 operator<<(std::ostream& out, Line const& li)
292 out << "[" << li._b << "," << li._e << "]";
296 Pose::Pose(double x, double y, double h) : Point(x, y), _h(h)
319 Pose::set_pose(Pose const& p)
327 Pose::rotate(Point const& c, double const angl)
329 Point::rotate(c, angl);
330 this->h(this->h() + angl);
334 Pose::reflect(Line const& li)
337 double dh = li.h() - this->h();
338 this->h(this->h() + 2.0 * dh);
342 Pose::operator==(Pose const& p)
344 return this->x() == p.x() && this->y() == p.y() && this->h() == p.h();
348 operator<<(std::ostream& out, Pose const& p)
350 out << "[" << p.x() << "," << p.y() << "," << p.h() << "]";
358 double bpbx = this->_bp.x() - clen * cos(this->_bp.h());
359 double bpby = this->_bp.y() - clen * sin(this->_bp.h());
360 double bpfx = this->_bp.x() + clen * cos(this->_bp.h());
361 double bpfy = this->_bp.y() + clen * sin(this->_bp.h());
362 Line li1(Point(bpbx, bpby), Point(bpfx, bpfy));
363 double epbx = this->_ep.x() - clen * cos(this->_ep.h());
364 double epby = this->_ep.y() - clen * sin(this->_ep.h());
365 double epfx = this->_ep.x() + clen * cos(this->_ep.h());
366 double epfy = this->_ep.y() + clen * sin(this->_ep.h());
367 Line li2(Point(epbx, epby), Point(epfx, epfy));
368 li1.intersects_with(li2);
369 this->x(li1.i1().x());
370 this->y(li1.i1().y());
371 double bh = this->b();
376 double eh = this->e();
381 this->h((this->b() + this->e()) / 2.0);
384 PoseRange::PoseRange(Pose bp, Pose ep) : _bp(bp), _ep(ep)
386 if (this->_bp == this->_ep) {
387 this->set_pose(this->_ep);
393 PoseRange::PoseRange(double x, double y, double b, double e)
394 : PoseRange(Pose(x, y, b), Pose(x, y, e))
399 PoseRange::bp() const
405 PoseRange::ep() const
413 return std::min(this->_bp.h(), this->_ep.h());
419 return std::max(this->_bp.h(), this->_ep.h());
423 PoseRange::translate(Point const& p)
425 this->_bp.translate(p);
426 this->_ep.translate(p);
431 PoseRange::rotate(Point const& c, double const angl)
433 this->_bp.rotate(c, angl);
434 this->_ep.rotate(c, angl);
439 PoseRange::reflect(Line const& li)
441 this->_bp.reflect(li);
442 this->_ep.reflect(li);
447 operator<<(std::ostream& out, PoseRange const& p)
449 out << "[" << p.x() << "," << p.y() << "," << p.b() << "," << p.e();
457 return this->_curb_to_curb;
461 CarSize::ctc(double ctc)
463 this->_curb_to_curb = ctc;
469 return this->_wheelbase;
473 CarSize::wb(double wb)
475 this->_wheelbase = wb;
493 return this->_width_with_mirrors;
497 CarSize::wwm(double w)
499 this->_width_with_mirrors = w;
505 return this->_length;
509 CarSize::len(double len)
517 return this->_distance_to_front;
521 CarSize::df(double df)
523 this->_distance_to_front = df;
529 return this->len() - this->df();
533 CarSize::ft(double ft)
535 this->_front_track = ft;
541 return this->_front_track;
547 auto ctc2 = pow(this->ctc() / 2.0, 2.0);
548 auto wb2 = pow(this->wb(), 2.0);
549 return sqrt(ctc2 - wb2) - this->ft() / 2.0;
553 CarSize::iradi() const
555 return this->mtr() - this->w() / 2;
559 CarSize::ofradi() const
561 auto mtrw2 = pow(this->mtr() + this->w() / 2.0, 2.0);
562 auto df2 = pow(this->df(), 2.0);
563 return sqrt(mtrw2 + df2);
567 CarSize::orradi() const
569 auto mtrw2 = pow(this->mtr() + this->w() / 2.0, 2.0);
570 auto dr2 = pow(this->dr(), 2.0);
571 return sqrt(mtrw2 + dr2);
575 CarSize::imradi() const
577 auto mtrw2 = pow(this->mtr() - this->wwm() / 2.0, 2.0);
578 auto df2 = pow(this->wb(), 2.0);
579 return sqrt(mtrw2 + df2);
583 CarSize::omradi() const
585 auto mtrw2 = pow(this->mtr() + this->wwm() / 2.0, 2.0);
586 auto df2 = pow(this->wb(), 2.0);
587 return sqrt(mtrw2 + df2);
591 CarSize::perfect_parking_slot_len() const
593 auto r = this->ctc() / 2.0;
595 auto k = this->df() - this->wb();
596 auto w = this->w(); // FIXME use wwm()?
597 auto r2l2 = r * r - l * l;
598 auto s = r2l2 + pow(l + k, 2.0) - pow(sqrt(r2l2) - w, 2.0);
599 return this->len() + sqrt(s) - l - k;
609 CarMove::sp(double sp)
621 CarMove::st(double st)
627 BicycleCar::drivable(Pose const& p) const
629 return this->drivable(PoseRange(p, p));
633 BicycleCar::drivable(PoseRange const& p) const
635 double a_1 = atan2(p.y() - this->y(), p.x() - this->x()) - this->h();
640 double h_d = p.h() - this->h();
646 if (h_d == 0 && (a_1 == 0 || a_2 == M_PI || a_2 == -M_PI)) {
648 } else if (0 < a_1 && a_1 <= M_PI/2) { // left front
649 BicycleCar z(*this); // zone border
651 h_d = p.h() - this->h();
652 z.rotate(this->ccl(), h_d);
653 // assert z.h() == p.h()
654 if (p.y() == z.y() && p.x() == z.x()) // p on zone border
656 a_2 = atan2(p.y() - z.y(), p.x() - z.x());
661 if (z.h() >= a_2 && a_2 >= this->h())
663 } else if (M_PI/2 < a_1 && a_1 <= M_PI) { // left rear
664 BicycleCar z(*this); // zone border
666 h_d = p.h() - this->h();
667 z.rotate(this->ccl(), h_d);
668 // assert z.h() == p.h()
669 if (p.y() == z.y() && p.x() == z.x()) // p on zone border
671 a_2 = atan2(p.y() - z.y(), p.x() - z.x());
677 if (this->h() >= a_2 && a_2 >= z.h())
679 } else if (0 > a_1 && a_1 >= -M_PI/2) { // right front
680 BicycleCar z(*this); // zone border
682 h_d = p.h() - this->h();
683 z.rotate(this->ccr(), h_d);
684 // assert z.h() == p.h()
685 if (p.y() == z.y() && p.x() == z.x()) // p on zone border
687 a_2 = atan2(p.y() - z.y(), p.x() - z.x());
692 if (this->h() >= a_2 && a_2 >= z.h())
694 } else if (-M_PI/2 > a_1 && a_1 >= -M_PI) { // right rear
695 BicycleCar z(*this); // zone border
697 h_d = p.h() - this->h();
698 z.rotate(this->ccr(), h_d);
699 // assert z.h() == p.h()
700 if (p.y() == z.y() && p.x() == z.x()) // p on zone border
702 a_2 = atan2(p.y() - z.y(), p.x() - z.x());
708 if (z.h() >= a_2 && a_2 >= this->h())
711 // Not happenning, as ``-pi <= a <= pi``.
717 BicycleCar::set_max_steer()
719 this->st(atan(this->wb() / this->mtr()));
723 BicycleCar::lfx() const
725 double lfx = this->x();
726 lfx += (this->w() / 2.0) * cos(this->h() + M_PI / 2.0);
727 lfx += this->df() * cos(this->h());
732 BicycleCar::lfy() const
734 double lfy = this->y();
735 lfy += (this->w() / 2.0) * sin(this->h() + M_PI / 2.0);
736 lfy += this->df() * sin(this->h());
741 BicycleCar::lrx() const
743 double lrx = this->x();
744 lrx += (this->w() / 2.0) * cos(this->h() + M_PI / 2.0);
745 lrx += -this->dr() * cos(this->h());
750 BicycleCar::lry() const
752 double lry = this->y();
753 lry += (this->w() / 2.0) * sin(this->h() + M_PI / 2.0);
754 lry += -this->dr() * sin(this->h());
759 BicycleCar::rrx() const
761 double rrx = this->x();
762 rrx += (this->w() / 2.0) * cos(this->h() - M_PI / 2.0);
763 rrx += -this->dr() * cos(this->h());
768 BicycleCar::rry() const
770 double rry = this->y();
771 rry += (this->w() / 2.0) * sin(this->h() - M_PI / 2.0);
772 rry += -this->dr() * sin(this->h());
777 BicycleCar::rfx() const
779 double rfx = this->x();
780 rfx += (this->w() / 2.0) * cos(this->h() - M_PI / 2.0);
781 rfx += this->df() * cos(this->h());
786 BicycleCar::rfy() const
788 double rfy = this->y();
789 rfy += (this->w() / 2.0) * sin(this->h() - M_PI / 2.0);
790 rfy += this->df() * sin(this->h());
795 BicycleCar::lf() const
797 return Point(this->lfx(), this->lfy());
801 BicycleCar::lr() const
803 return Point(this->lrx(), this->lry());
807 BicycleCar::rr() const
809 return Point(this->rrx(), this->rry());
813 BicycleCar::rf() const
815 return Point(this->rfx(), this->rfy());
819 BicycleCar::left() const
821 return Line(this->lr(), this->lf());
825 BicycleCar::rear() const
827 return Line(this->lr(), this->rr());
831 BicycleCar::right() const
833 return Line(this->rr(), this->rf());
837 BicycleCar::front() const
839 return Line(this->rf(), this->lf());
843 BicycleCar::lrax() const
845 double lrx = this->x();
846 lrx += (this->w() / 2.0) * cos(this->h() + M_PI / 2.0);
850 BicycleCar::lray() const
852 double lry = this->y();
853 lry += (this->w() / 2.0) * sin(this->h() + M_PI / 2.0);
858 BicycleCar::rrax() const
860 double rrx = this->x();
861 rrx += (this->w() / 2.0) * cos(this->h() - M_PI / 2.0);
866 BicycleCar::rray() const
868 double rry = this->y();
869 rry += (this->w() / 2.0) * sin(this->h() - M_PI / 2.0);
874 BicycleCar::lra() const
876 return Point(this->lrax(), this->lray());
880 BicycleCar::rra() const
882 return Point(this->rrax(), this->rray());
886 BicycleCar::lfax() const
888 return this->lrax() + this->wb() * cos(this->h());
892 BicycleCar::lfay() const
894 return this->lray() + this->wb() * sin(this->h());
898 BicycleCar::rfax() const
900 return this->rrax() + this->wb() * cos(this->h());
904 BicycleCar::rfay() const
906 return this->rray() + this->wb() * sin(this->h());
910 BicycleCar::lfa() const
912 return Point(this->lfax(), this->lfay());
916 BicycleCar::rfa() const
918 return Point(this->rfax(), this->rfay());
922 BicycleCar::lfmx() const
924 double x = this->x();
925 x += (this->wwm() / 2.0) * cos(this->h() + M_PI / 2.0);
926 x += this->wb() * cos(this->h());
931 BicycleCar::lfmy() const
933 double y = this->y();
934 y += (this->wwm() / 2.0) * sin(this->h() + M_PI / 2.0);
935 y += this->wb() * sin(this->h());
940 BicycleCar::rfmx() const
942 double x = this->x();
943 x += (this->wwm() / 2.0) * cos(this->h() - M_PI / 2.0);
944 x += this->wb() * cos(this->h());
949 BicycleCar::rfmy() const
951 double y = this->y();
952 y += (this->wwm() / 2.0) * sin(this->h() - M_PI / 2.0);
953 y += this->wb() * sin(this->h());
958 BicycleCar::lfm() const
960 return Point(this->lfmx(), this->lfmy());
964 BicycleCar::rfm() const
966 return Point(this->rfmx(), this->rfmy());
970 BicycleCar::cfx() const
972 return this->x() + this->df() * cos(this->h());
976 BicycleCar::cfy() const
978 return this->y() + this->df() * sin(this->h());
982 BicycleCar::cf() const
984 return Point(this->cfx(), this->cfy());
988 BicycleCar::ccl() const
991 this->x() + this->mtr() * cos(this->h() + M_PI / 2.0),
992 this->y() + this->mtr() * sin(this->h() + M_PI / 2.0)
997 BicycleCar::ccr() const
1000 this->x() + this->mtr() * cos(this->h() - M_PI / 2.0),
1001 this->y() + this->mtr() * sin(this->h() - M_PI / 2.0)
1008 this->x(this->x() + this->sp() * cos(this->h()));
1009 this->y(this->y() + this->sp() * sin(this->h()));
1010 this->h(this->h() + this->sp() / this->wb() * tan(this->st()));
1014 BicycleCar::gen_gnuplot_to(std::ostream& out, GenPlotOpts opts)
1018 opts.MIRRORS = true;
1021 opts.LEFT_MIRROR = true;
1022 opts.RIGHT_MIRROR = true;
1035 if (opts.LF_POINT) {
1036 this->lf().gen_gnuplot_to(out);
1038 if (opts.LR_POINT) {
1039 this->lr().gen_gnuplot_to(out);
1041 if (opts.RR_POINT) {
1042 this->rr().gen_gnuplot_to(out);
1044 if (opts.RF_POINT) {
1045 this->rf().gen_gnuplot_to(out);
1047 if (opts.LFM_POINT) {
1048 this->lfm().gen_gnuplot_to(out);
1050 if (opts.RFM_POINT) {
1051 this->rfm().gen_gnuplot_to(out);
1053 if (opts.CRA_POINT || opts.CAR_POINT) {
1054 Point::gen_gnuplot_to(out);
1056 if (opts.LRA_POINT) {
1057 this->lra().gen_gnuplot_to(out);
1059 if (opts.RRA_POINT) {
1060 this->rra().gen_gnuplot_to(out);
1063 this->lf().gen_gnuplot_to(out);
1064 this->lr().gen_gnuplot_to(out);
1068 this->rf().gen_gnuplot_to(out);
1069 this->rr().gen_gnuplot_to(out);
1073 this->lr().gen_gnuplot_to(out);
1074 this->rr().gen_gnuplot_to(out);
1078 this->lf().gen_gnuplot_to(out);
1079 this->rf().gen_gnuplot_to(out);
1083 this->cf().gen_gnuplot_to(out);
1084 this->lfa().gen_gnuplot_to(out);
1085 this->rfa().gen_gnuplot_to(out);
1086 this->cf().gen_gnuplot_to(out);
1090 double lx = this->x() + 0.2 * cos(this->h() + M_PI/2);
1091 double rx = this->x() - 0.2 * cos(this->h() + M_PI/2);
1092 double fx = this->x() + 0.2 * cos(this->h());
1093 double bx = this->x() - 0.2 * cos(this->h()); // rear is back
1094 double ly = this->y() + 0.2 * sin(this->h() + M_PI/2);
1095 double ry = this->y() - 0.2 * sin(this->h() + M_PI/2);
1096 double fy = this->y() + 0.2 * sin(this->h());
1097 double by = this->y() - 0.2 * sin(this->h()); // rear is back
1098 out << lx << " " << ly << std::endl;
1099 out << rx << " " << ry << std::endl;
1101 out << fx << " " << fy << std::endl;
1102 out << bx << " " << by << std::endl;
1105 if (opts.LEFT_MIRROR) {
1106 this->lf().gen_gnuplot_to(out);
1107 this->lfm().gen_gnuplot_to(out);
1108 this->lr().gen_gnuplot_to(out);
1112 if (opts.RIGHT_MIRROR) {
1113 this->rf().gen_gnuplot_to(out);
1114 this->rfm().gen_gnuplot_to(out);
1115 this->rr().gen_gnuplot_to(out);