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;
545 CarSize::edist_to_rr() const
547 return sqrt(pow(this->w() / 2.0, 2) + pow(this->len() - this->df(), 2));
551 CarSize::edist_to_lf() const
553 return sqrt(pow(this->w() / 2.0, 2) + pow(this->df(), 2));
559 auto ctc2 = pow(this->ctc() / 2.0, 2.0);
560 auto wb2 = pow(this->wb(), 2.0);
561 return sqrt(ctc2 - wb2) - this->ft() / 2.0;
565 CarSize::iradi() const
567 return this->mtr() - this->w() / 2;
571 CarSize::ofradi() const
573 auto mtrw2 = pow(this->mtr() + this->w() / 2.0, 2.0);
574 auto df2 = pow(this->df(), 2.0);
575 return sqrt(mtrw2 + df2);
579 CarSize::orradi() const
581 auto mtrw2 = pow(this->mtr() + this->w() / 2.0, 2.0);
582 auto dr2 = pow(this->dr(), 2.0);
583 return sqrt(mtrw2 + dr2);
587 CarSize::imradi() const
589 auto mtrw2 = pow(this->mtr() - this->wwm() / 2.0, 2.0);
590 auto df2 = pow(this->wb(), 2.0);
591 return sqrt(mtrw2 + df2);
595 CarSize::omradi() const
597 auto mtrw2 = pow(this->mtr() + this->wwm() / 2.0, 2.0);
598 auto df2 = pow(this->wb(), 2.0);
599 return sqrt(mtrw2 + df2);
603 CarSize::perfect_parking_slot_len() const
605 auto r = this->ctc() / 2.0;
607 auto k = this->df() - this->wb();
608 auto w = this->w(); // FIXME use wwm()?
609 auto r2l2 = r * r - l * l;
610 auto s = r2l2 + pow(l + k, 2.0) - pow(sqrt(r2l2) - w, 2.0);
611 return this->len() + sqrt(s) - l - k;
621 CarMove::sp(double sp)
633 CarMove::st(double st)
639 BicycleCar::drivable(Pose const& p) const
641 return this->drivable(PoseRange(p, p));
645 BicycleCar::drivable(PoseRange const& p) const
647 double a_1 = atan2(p.y() - this->y(), p.x() - this->x()) - this->h();
652 double h_d = p.h() - this->h();
658 if (h_d == 0 && (a_1 == 0 || a_2 == M_PI || a_2 == -M_PI)) {
660 } else if (0 < a_1 && a_1 <= M_PI/2) { // left front
661 BicycleCar z(*this); // zone border
663 h_d = p.h() - this->h();
664 z.rotate(this->ccl(), h_d);
665 // assert z.h() == p.h()
666 if (p.y() == z.y() && p.x() == z.x()) // p on zone border
668 a_2 = atan2(p.y() - z.y(), p.x() - z.x());
673 if (z.h() >= a_2 && a_2 >= this->h())
675 } else if (M_PI/2 < a_1 && a_1 <= M_PI) { // left rear
676 BicycleCar z(*this); // zone border
678 h_d = p.h() - this->h();
679 z.rotate(this->ccl(), h_d);
680 // assert z.h() == p.h()
681 if (p.y() == z.y() && p.x() == z.x()) // p on zone border
683 a_2 = atan2(p.y() - z.y(), p.x() - z.x());
689 if (this->h() >= a_2 && a_2 >= z.h())
691 } else if (0 > a_1 && a_1 >= -M_PI/2) { // right front
692 BicycleCar z(*this); // zone border
694 h_d = p.h() - this->h();
695 z.rotate(this->ccr(), h_d);
696 // assert z.h() == p.h()
697 if (p.y() == z.y() && p.x() == z.x()) // p on zone border
699 a_2 = atan2(p.y() - z.y(), p.x() - z.x());
704 if (this->h() >= a_2 && a_2 >= z.h())
706 } else if (-M_PI/2 > a_1 && a_1 >= -M_PI) { // right rear
707 BicycleCar z(*this); // zone border
709 h_d = p.h() - this->h();
710 z.rotate(this->ccr(), h_d);
711 // assert z.h() == p.h()
712 if (p.y() == z.y() && p.x() == z.x()) // p on zone border
714 a_2 = atan2(p.y() - z.y(), p.x() - z.x());
720 if (z.h() >= a_2 && a_2 >= this->h())
723 // Not happenning, as ``-pi <= a <= pi``.
729 BicycleCar::set_max_steer()
731 this->st(atan(this->wb() / this->mtr()));
735 BicycleCar::lfx() const
737 double lfx = this->x();
738 lfx += (this->w() / 2.0) * cos(this->h() + M_PI / 2.0);
739 lfx += this->df() * cos(this->h());
744 BicycleCar::lfy() const
746 double lfy = this->y();
747 lfy += (this->w() / 2.0) * sin(this->h() + M_PI / 2.0);
748 lfy += this->df() * sin(this->h());
753 BicycleCar::lrx() const
755 double lrx = this->x();
756 lrx += (this->w() / 2.0) * cos(this->h() + M_PI / 2.0);
757 lrx += -this->dr() * cos(this->h());
762 BicycleCar::lry() const
764 double lry = this->y();
765 lry += (this->w() / 2.0) * sin(this->h() + M_PI / 2.0);
766 lry += -this->dr() * sin(this->h());
771 BicycleCar::rrx() const
773 double rrx = this->x();
774 rrx += (this->w() / 2.0) * cos(this->h() - M_PI / 2.0);
775 rrx += -this->dr() * cos(this->h());
780 BicycleCar::rry() const
782 double rry = this->y();
783 rry += (this->w() / 2.0) * sin(this->h() - M_PI / 2.0);
784 rry += -this->dr() * sin(this->h());
789 BicycleCar::rfx() const
791 double rfx = this->x();
792 rfx += (this->w() / 2.0) * cos(this->h() - M_PI / 2.0);
793 rfx += this->df() * cos(this->h());
798 BicycleCar::rfy() const
800 double rfy = this->y();
801 rfy += (this->w() / 2.0) * sin(this->h() - M_PI / 2.0);
802 rfy += this->df() * sin(this->h());
807 BicycleCar::lf() const
809 return Point(this->lfx(), this->lfy());
813 BicycleCar::lr() const
815 return Point(this->lrx(), this->lry());
819 BicycleCar::rr() const
821 return Point(this->rrx(), this->rry());
825 BicycleCar::rf() const
827 return Point(this->rfx(), this->rfy());
831 BicycleCar::left() const
833 return Line(this->lr(), this->lf());
837 BicycleCar::rear() const
839 return Line(this->lr(), this->rr());
843 BicycleCar::right() const
845 return Line(this->rr(), this->rf());
849 BicycleCar::front() const
851 return Line(this->rf(), this->lf());
855 BicycleCar::lrax() const
857 double lrx = this->x();
858 lrx += (this->w() / 2.0) * cos(this->h() + M_PI / 2.0);
862 BicycleCar::lray() const
864 double lry = this->y();
865 lry += (this->w() / 2.0) * sin(this->h() + M_PI / 2.0);
870 BicycleCar::rrax() const
872 double rrx = this->x();
873 rrx += (this->w() / 2.0) * cos(this->h() - M_PI / 2.0);
878 BicycleCar::rray() const
880 double rry = this->y();
881 rry += (this->w() / 2.0) * sin(this->h() - M_PI / 2.0);
886 BicycleCar::lra() const
888 return Point(this->lrax(), this->lray());
892 BicycleCar::rra() const
894 return Point(this->rrax(), this->rray());
898 BicycleCar::lfax() const
900 return this->lrax() + this->wb() * cos(this->h());
904 BicycleCar::lfay() const
906 return this->lray() + this->wb() * sin(this->h());
910 BicycleCar::rfax() const
912 return this->rrax() + this->wb() * cos(this->h());
916 BicycleCar::rfay() const
918 return this->rray() + this->wb() * sin(this->h());
922 BicycleCar::lfa() const
924 return Point(this->lfax(), this->lfay());
928 BicycleCar::rfa() const
930 return Point(this->rfax(), this->rfay());
934 BicycleCar::lfmx() const
936 double x = this->x();
937 x += (this->wwm() / 2.0) * cos(this->h() + M_PI / 2.0);
938 x += this->wb() * cos(this->h());
943 BicycleCar::lfmy() const
945 double y = this->y();
946 y += (this->wwm() / 2.0) * sin(this->h() + M_PI / 2.0);
947 y += this->wb() * sin(this->h());
952 BicycleCar::rfmx() const
954 double x = this->x();
955 x += (this->wwm() / 2.0) * cos(this->h() - M_PI / 2.0);
956 x += this->wb() * cos(this->h());
961 BicycleCar::rfmy() const
963 double y = this->y();
964 y += (this->wwm() / 2.0) * sin(this->h() - M_PI / 2.0);
965 y += this->wb() * sin(this->h());
970 BicycleCar::lfm() const
972 return Point(this->lfmx(), this->lfmy());
976 BicycleCar::rfm() const
978 return Point(this->rfmx(), this->rfmy());
982 BicycleCar::cfx() const
984 return this->x() + this->df() * cos(this->h());
988 BicycleCar::cfy() const
990 return this->y() + this->df() * sin(this->h());
994 BicycleCar::cf() const
996 return Point(this->cfx(), this->cfy());
1000 BicycleCar::ccl() const
1003 this->x() + this->mtr() * cos(this->h() + M_PI / 2.0),
1004 this->y() + this->mtr() * sin(this->h() + M_PI / 2.0)
1009 BicycleCar::ccr() const
1012 this->x() + this->mtr() * cos(this->h() - M_PI / 2.0),
1013 this->y() + this->mtr() * sin(this->h() - M_PI / 2.0)
1020 this->x(this->x() + this->sp() * cos(this->h()));
1021 this->y(this->y() + this->sp() * sin(this->h()));
1022 this->h(this->h() + this->sp() / this->wb() * tan(this->st()));
1026 BicycleCar::gen_gnuplot_to(std::ostream& out, GenPlotOpts opts)
1030 opts.MIRRORS = true;
1033 opts.LEFT_MIRROR = true;
1034 opts.RIGHT_MIRROR = true;
1047 if (opts.LF_POINT) {
1048 this->lf().gen_gnuplot_to(out);
1050 if (opts.LR_POINT) {
1051 this->lr().gen_gnuplot_to(out);
1053 if (opts.RR_POINT) {
1054 this->rr().gen_gnuplot_to(out);
1056 if (opts.RF_POINT) {
1057 this->rf().gen_gnuplot_to(out);
1059 if (opts.LFM_POINT) {
1060 this->lfm().gen_gnuplot_to(out);
1062 if (opts.RFM_POINT) {
1063 this->rfm().gen_gnuplot_to(out);
1065 if (opts.CRA_POINT || opts.CAR_POINT) {
1066 Point::gen_gnuplot_to(out);
1068 if (opts.LRA_POINT) {
1069 this->lra().gen_gnuplot_to(out);
1071 if (opts.RRA_POINT) {
1072 this->rra().gen_gnuplot_to(out);
1075 this->lf().gen_gnuplot_to(out);
1076 this->lr().gen_gnuplot_to(out);
1080 this->rf().gen_gnuplot_to(out);
1081 this->rr().gen_gnuplot_to(out);
1085 this->lr().gen_gnuplot_to(out);
1086 this->rr().gen_gnuplot_to(out);
1090 this->lf().gen_gnuplot_to(out);
1091 this->rf().gen_gnuplot_to(out);
1095 this->cf().gen_gnuplot_to(out);
1096 this->lfa().gen_gnuplot_to(out);
1097 this->rfa().gen_gnuplot_to(out);
1098 this->cf().gen_gnuplot_to(out);
1102 double lx = this->x() + 0.2 * cos(this->h() + M_PI/2);
1103 double rx = this->x() - 0.2 * cos(this->h() + M_PI/2);
1104 double fx = this->x() + 0.2 * cos(this->h());
1105 double bx = this->x() - 0.2 * cos(this->h()); // rear is back
1106 double ly = this->y() + 0.2 * sin(this->h() + M_PI/2);
1107 double ry = this->y() - 0.2 * sin(this->h() + M_PI/2);
1108 double fy = this->y() + 0.2 * sin(this->h());
1109 double by = this->y() - 0.2 * sin(this->h()); // rear is back
1110 out << lx << " " << ly << std::endl;
1111 out << rx << " " << ry << std::endl;
1113 out << fx << " " << fy << std::endl;
1114 out << bx << " " << by << std::endl;
1117 if (opts.LEFT_MIRROR) {
1118 this->lf().gen_gnuplot_to(out);
1119 this->lfm().gen_gnuplot_to(out);
1120 this->lr().gen_gnuplot_to(out);
1124 if (opts.RIGHT_MIRROR) {
1125 this->rf().gen_gnuplot_to(out);
1126 this->rfm().gen_gnuplot_to(out);
1127 this->rr().gen_gnuplot_to(out);