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::on_right_side_of(Line const& li) const
97 if (sgn((x3 - x1) * (y2 - y1) - (y3 - y1) * (x2 - x1)) < 0.0) {
105 Point::translate(Point const& p)
112 Point::rotate(Point const& c, double const angl)
114 double px = this->x();
115 double py = this->y();
118 double nx = px * cos(angl) - py * sin(angl);
119 double ny = px * sin(angl) + py * cos(angl);
125 Point::reflect(Line const& li)
127 this->rotate(li.b(), -li.h());
128 this->_y -= li.b().y();
130 this->_y += li.b().y();
131 this->rotate(li.b(), li.h());
135 Point::edist(Point const& p) const
137 return sqrt(pow(p.x() - this->_x, 2.0) + pow(p.y() - this->_y, 2.0));
141 Point::gen_gnuplot_to(std::ostream& out)
143 out << this->_x << " " << this->_y << std::endl;
147 Point::operator==(Point const& p)
149 return this->x() == p.x() && this->y() == p.y();
153 operator<<(std::ostream& out, Point const& p)
155 out << "[" << p.x() << "," << p.y() << "]";
159 Line::Line(Point const& b, Point const& e): _b(b), _e(e)
178 return Point((this->_b.x() + this->_e.x()) / 2.0,
179 (this->_b.y() + this->_e.y()) / 2.0);
195 Line::intersects_with(Line const& li)
197 auto x1 = this->_b.x();
198 auto y1 = this->_b.y();
199 auto x2 = this->_e.x();
200 auto y2 = this->_e.y();
201 auto x3 = li.b().x();
202 auto y3 = li.b().y();
203 auto x4 = li.e().x();
204 auto y4 = li.e().y();
205 double deno = (x1 - x2) * (y3 - y4) - (y1 - y2) * (x3 - x4);
209 double t = (x1 - x3) * (y3 - y4) - (y1 - y3) * (x3 - x4);
211 double u = (x1 - x2) * (y1 - y3) - (y1 - y2) * (x1 - x3);
214 if (t < 0.0 || t > 1.0 || u < 0.0 || u > 1.0) {
217 this->_i1.x(x1 + t * (x2 - x1));
218 this->_i1.y(y1 + t * (y2 - y1));
223 Line::intersects_with(Point const& c, double const r)
225 auto x1 = this->_b.x();
226 auto y1 = this->_b.y();
227 auto x2 = this->_e.x();
228 auto y2 = this->_e.y();
240 double dr = sqrt(dx*dx + dy*dy);
241 double D = x1*y2 - x2*y1;
242 if (r*r * dr*dr - D*D < 0.0) {
245 // intersection coordinates
246 double ix1 = (D*dy + sgn(dy)*dx*sqrt(r*r * dr*dr - D*D)) / (dr*dr);
248 double ix2 = (D*dy - sgn(dy)*dx*sqrt(r*r * dr*dr - D*D)) / (dr*dr);
250 double iy1 = (-D*dx + std::abs(dy)*sqrt(r*r * dr*dr - D*D)) / (dr*dr);
252 double iy2 = (-D*dx - std::abs(dy)*sqrt(r*r * dr*dr - D*D)) / (dr*dr);
264 return this->_b.edist(this->_e);
270 return atan2(this->_e.y() - this->_b.y(), this->_e.x() - this->_b.x());
274 Line::gen_gnuplot_to(std::ostream& out)
276 this->b().gen_gnuplot_to(out);
277 this->e().gen_gnuplot_to(out);
282 operator<<(std::ostream& out, Line const& li)
284 out << "[" << li._b << "," << li._e << "]";
288 Pose::Pose(double x, double y, double h) : Point(x, y), _h(h)
311 Pose::set_pose(Pose const& p)
319 Pose::rotate(Point const& c, double const angl)
321 Point::rotate(c, angl);
322 this->h(this->h() + angl);
326 Pose::reflect(Line const& li)
329 double dh = li.h() - this->h();
330 this->h(this->h() + 2.0 * dh);
334 Pose::operator==(Pose const& p)
336 return this->x() == p.x() && this->y() == p.y() && this->h() == p.h();
340 operator<<(std::ostream& out, Pose const& p)
342 out << "[" << p.x() << "," << p.y() << "," << p.h() << "]";
350 double bpbx = this->_bp.x() - clen * cos(this->_bp.h());
351 double bpby = this->_bp.y() - clen * sin(this->_bp.h());
352 double bpfx = this->_bp.x() + clen * cos(this->_bp.h());
353 double bpfy = this->_bp.y() + clen * sin(this->_bp.h());
354 Line li1(Point(bpbx, bpby), Point(bpfx, bpfy));
355 double epbx = this->_ep.x() - clen * cos(this->_ep.h());
356 double epby = this->_ep.y() - clen * sin(this->_ep.h());
357 double epfx = this->_ep.x() + clen * cos(this->_ep.h());
358 double epfy = this->_ep.y() + clen * sin(this->_ep.h());
359 Line li2(Point(epbx, epby), Point(epfx, epfy));
360 li1.intersects_with(li2);
361 this->x(li1.i1().x());
362 this->y(li1.i1().y());
363 double bh = this->b();
368 double eh = this->e();
373 this->h((this->b() + this->e()) / 2.0);
376 PoseRange::PoseRange(Pose bp, Pose ep) : _bp(bp), _ep(ep)
378 if (this->_bp == this->_ep) {
379 this->set_pose(this->_ep);
385 PoseRange::PoseRange(double x, double y, double b, double e)
386 : PoseRange(Pose(x, y, b), Pose(x, y, e))
391 PoseRange::bp() const
397 PoseRange::ep() const
405 return std::min(this->_bp.h(), this->_ep.h());
411 return std::max(this->_bp.h(), this->_ep.h());
415 PoseRange::translate(Point const& p)
417 this->_bp.translate(p);
418 this->_ep.translate(p);
423 PoseRange::rotate(Point const& c, double const angl)
425 this->_bp.rotate(c, angl);
426 this->_ep.rotate(c, angl);
431 PoseRange::reflect(Line const& li)
433 this->_bp.reflect(li);
434 this->_ep.reflect(li);
439 operator<<(std::ostream& out, PoseRange const& p)
441 out << "[" << p.x() << "," << p.y() << "," << p.b() << "," << p.e();
449 return this->_curb_to_curb;
453 CarSize::ctc(double ctc)
455 this->_curb_to_curb = ctc;
461 return this->_wheelbase;
465 CarSize::wb(double wb)
467 this->_wheelbase = wb;
485 return this->_width_with_mirrors;
489 CarSize::wwm(double w)
491 this->_width_with_mirrors = w;
497 return this->_length;
501 CarSize::len(double len)
509 return this->_distance_to_front;
513 CarSize::df(double df)
515 this->_distance_to_front = df;
521 return this->len() - this->df();
525 CarSize::ft(double ft)
527 this->_front_track = ft;
533 return this->_front_track;
539 auto ctc2 = pow(this->ctc() / 2.0, 2.0);
540 auto wb2 = pow(this->wb(), 2.0);
541 return sqrt(ctc2 - wb2) - this->ft() / 2.0;
545 CarSize::iradi() const
547 return this->mtr() - this->w() / 2;
551 CarSize::ofradi() const
553 auto mtrw2 = pow(this->mtr() + this->w() / 2.0, 2.0);
554 auto df2 = pow(this->df(), 2.0);
555 return sqrt(mtrw2 + df2);
559 CarSize::orradi() const
561 auto mtrw2 = pow(this->mtr() + this->w() / 2.0, 2.0);
562 auto dr2 = pow(this->dr(), 2.0);
563 return sqrt(mtrw2 + dr2);
567 CarSize::imradi() const
569 auto mtrw2 = pow(this->mtr() - this->wwm() / 2.0, 2.0);
570 auto df2 = pow(this->wb(), 2.0);
571 return sqrt(mtrw2 + df2);
575 CarSize::omradi() 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::perfect_parking_slot_len() const
585 auto r = this->ctc() / 2.0;
587 auto k = this->df() - this->wb();
588 auto w = this->w(); // FIXME use wwm()?
589 auto r2l2 = r * r - l * l;
590 auto s = r2l2 + pow(l + k, 2.0) - pow(sqrt(r2l2) - w, 2.0);
591 return this->len() + sqrt(s) - l - k;
601 CarMove::sp(double sp)
613 CarMove::st(double st)
619 BicycleCar::drivable(Pose const& p) const
621 return this->drivable(PoseRange(p, p));
625 BicycleCar::drivable(PoseRange const& p) const
627 double a_1 = atan2(p.y() - this->y(), p.x() - this->x()) - this->h();
632 double h_d = p.h() - this->h();
638 if (h_d == 0 && (a_1 == 0 || a_2 == M_PI || a_2 == -M_PI)) {
640 } else if (0 < a_1 && a_1 <= M_PI/2) { // left front
641 BicycleCar z(*this); // zone border
643 h_d = p.h() - this->h();
644 z.rotate(this->ccl(), h_d);
645 // assert z.h() == p.h()
646 if (p.y() == z.y() && p.x() == z.x()) // p on zone border
648 a_2 = atan2(p.y() - z.y(), p.x() - z.x());
653 if (z.h() >= a_2 && a_2 >= this->h())
655 } else if (M_PI/2 < a_1 && a_1 <= M_PI) { // left rear
656 BicycleCar z(*this); // zone border
658 h_d = p.h() - this->h();
659 z.rotate(this->ccl(), h_d);
660 // assert z.h() == p.h()
661 if (p.y() == z.y() && p.x() == z.x()) // p on zone border
663 a_2 = atan2(p.y() - z.y(), p.x() - z.x());
669 if (this->h() >= a_2 && a_2 >= z.h())
671 } else if (0 > a_1 && a_1 >= -M_PI/2) { // right front
672 BicycleCar z(*this); // zone border
674 h_d = p.h() - this->h();
675 z.rotate(this->ccr(), h_d);
676 // assert z.h() == p.h()
677 if (p.y() == z.y() && p.x() == z.x()) // p on zone border
679 a_2 = atan2(p.y() - z.y(), p.x() - z.x());
684 if (this->h() >= a_2 && a_2 >= z.h())
686 } else if (-M_PI/2 > a_1 && a_1 >= -M_PI) { // right rear
687 BicycleCar z(*this); // zone border
689 h_d = p.h() - this->h();
690 z.rotate(this->ccr(), h_d);
691 // assert z.h() == p.h()
692 if (p.y() == z.y() && p.x() == z.x()) // p on zone border
694 a_2 = atan2(p.y() - z.y(), p.x() - z.x());
700 if (z.h() >= a_2 && a_2 >= this->h())
703 // Not happenning, as ``-pi <= a <= pi``.
709 BicycleCar::set_max_steer()
711 this->st(atan(this->wb() / this->mtr()));
715 BicycleCar::lfx() const
717 double lfx = this->x();
718 lfx += (this->w() / 2.0) * cos(this->h() + M_PI / 2.0);
719 lfx += this->df() * cos(this->h());
724 BicycleCar::lfy() const
726 double lfy = this->y();
727 lfy += (this->w() / 2.0) * sin(this->h() + M_PI / 2.0);
728 lfy += this->df() * sin(this->h());
733 BicycleCar::lrx() const
735 double lrx = this->x();
736 lrx += (this->w() / 2.0) * cos(this->h() + M_PI / 2.0);
737 lrx += -this->dr() * cos(this->h());
742 BicycleCar::lry() const
744 double lry = this->y();
745 lry += (this->w() / 2.0) * sin(this->h() + M_PI / 2.0);
746 lry += -this->dr() * sin(this->h());
751 BicycleCar::rrx() const
753 double rrx = this->x();
754 rrx += (this->w() / 2.0) * cos(this->h() - M_PI / 2.0);
755 rrx += -this->dr() * cos(this->h());
760 BicycleCar::rry() const
762 double rry = this->y();
763 rry += (this->w() / 2.0) * sin(this->h() - M_PI / 2.0);
764 rry += -this->dr() * sin(this->h());
769 BicycleCar::rfx() const
771 double rfx = this->x();
772 rfx += (this->w() / 2.0) * cos(this->h() - M_PI / 2.0);
773 rfx += this->df() * cos(this->h());
778 BicycleCar::rfy() const
780 double rfy = this->y();
781 rfy += (this->w() / 2.0) * sin(this->h() - M_PI / 2.0);
782 rfy += this->df() * sin(this->h());
787 BicycleCar::lf() const
789 return Point(this->lfx(), this->lfy());
793 BicycleCar::lr() const
795 return Point(this->lrx(), this->lry());
799 BicycleCar::rr() const
801 return Point(this->rrx(), this->rry());
805 BicycleCar::rf() const
807 return Point(this->rfx(), this->rfy());
811 BicycleCar::left() const
813 return Line(this->lr(), this->lf());
817 BicycleCar::rear() const
819 return Line(this->lr(), this->rr());
823 BicycleCar::right() const
825 return Line(this->rr(), this->rf());
829 BicycleCar::front() const
831 return Line(this->rf(), this->lf());
835 BicycleCar::lrax() const
837 double lrx = this->x();
838 lrx += (this->w() / 2.0) * cos(this->h() + M_PI / 2.0);
842 BicycleCar::lray() const
844 double lry = this->y();
845 lry += (this->w() / 2.0) * sin(this->h() + M_PI / 2.0);
850 BicycleCar::rrax() const
852 double rrx = this->x();
853 rrx += (this->w() / 2.0) * cos(this->h() - M_PI / 2.0);
858 BicycleCar::rray() const
860 double rry = this->y();
861 rry += (this->w() / 2.0) * sin(this->h() - M_PI / 2.0);
866 BicycleCar::lra() const
868 return Point(this->lrax(), this->lray());
872 BicycleCar::rra() const
874 return Point(this->rrax(), this->rray());
878 BicycleCar::lfax() const
880 return this->lrax() + this->wb() * cos(this->h());
884 BicycleCar::lfay() const
886 return this->lray() + this->wb() * sin(this->h());
890 BicycleCar::rfax() const
892 return this->rrax() + this->wb() * cos(this->h());
896 BicycleCar::rfay() const
898 return this->rray() + this->wb() * sin(this->h());
902 BicycleCar::lfa() const
904 return Point(this->lfax(), this->lfay());
908 BicycleCar::rfa() const
910 return Point(this->rfax(), this->rfay());
914 BicycleCar::lfmx() const
916 double x = this->x();
917 x += (this->wwm() / 2.0) * cos(this->h() + M_PI / 2.0);
918 x += this->wb() * cos(this->h());
923 BicycleCar::lfmy() const
925 double y = this->y();
926 y += (this->wwm() / 2.0) * sin(this->h() + M_PI / 2.0);
927 y += this->wb() * sin(this->h());
932 BicycleCar::rfmx() const
934 double x = this->x();
935 x += (this->wwm() / 2.0) * cos(this->h() - M_PI / 2.0);
936 x += this->wb() * cos(this->h());
941 BicycleCar::rfmy() const
943 double y = this->y();
944 y += (this->wwm() / 2.0) * sin(this->h() - M_PI / 2.0);
945 y += this->wb() * sin(this->h());
950 BicycleCar::lfm() const
952 return Point(this->lfmx(), this->lfmy());
956 BicycleCar::rfm() const
958 return Point(this->rfmx(), this->rfmy());
962 BicycleCar::cfx() const
964 return this->x() + this->df() * cos(this->h());
968 BicycleCar::cfy() const
970 return this->y() + this->df() * sin(this->h());
974 BicycleCar::cf() const
976 return Point(this->cfx(), this->cfy());
980 BicycleCar::ccl() const
983 this->x() + this->mtr() * cos(this->h() + M_PI / 2.0),
984 this->y() + this->mtr() * sin(this->h() + M_PI / 2.0)
989 BicycleCar::ccr() const
992 this->x() + this->mtr() * cos(this->h() - M_PI / 2.0),
993 this->y() + this->mtr() * sin(this->h() - M_PI / 2.0)
1000 this->x(this->x() + this->sp() * cos(this->h()));
1001 this->y(this->y() + this->sp() * sin(this->h()));
1002 this->h(this->h() + this->sp() / this->wb() * tan(this->st()));
1006 BicycleCar::gen_gnuplot_to(std::ostream& out, GenPlotOpts opts)
1010 opts.MIRRORS = true;
1013 opts.LEFT_MIRROR = true;
1014 opts.RIGHT_MIRROR = true;
1027 if (opts.LF_POINT) {
1028 this->lf().gen_gnuplot_to(out);
1030 if (opts.LR_POINT) {
1031 this->lr().gen_gnuplot_to(out);
1033 if (opts.RR_POINT) {
1034 this->rr().gen_gnuplot_to(out);
1036 if (opts.RF_POINT) {
1037 this->rf().gen_gnuplot_to(out);
1039 if (opts.LFM_POINT) {
1040 this->lfm().gen_gnuplot_to(out);
1042 if (opts.RFM_POINT) {
1043 this->rfm().gen_gnuplot_to(out);
1045 if (opts.CRA_POINT || opts.CAR_POINT) {
1046 Point::gen_gnuplot_to(out);
1048 if (opts.LRA_POINT) {
1049 this->lra().gen_gnuplot_to(out);
1051 if (opts.RRA_POINT) {
1052 this->rra().gen_gnuplot_to(out);
1055 this->lf().gen_gnuplot_to(out);
1056 this->lr().gen_gnuplot_to(out);
1060 this->rf().gen_gnuplot_to(out);
1061 this->rr().gen_gnuplot_to(out);
1065 this->lr().gen_gnuplot_to(out);
1066 this->rr().gen_gnuplot_to(out);
1070 this->lf().gen_gnuplot_to(out);
1071 this->rf().gen_gnuplot_to(out);
1075 this->cf().gen_gnuplot_to(out);
1076 this->lfa().gen_gnuplot_to(out);
1077 this->rfa().gen_gnuplot_to(out);
1078 this->cf().gen_gnuplot_to(out);
1082 double lx = this->x() + 0.2 * cos(this->h() + M_PI/2);
1083 double rx = this->x() - 0.2 * cos(this->h() + M_PI/2);
1084 double fx = this->x() + 0.2 * cos(this->h());
1085 double bx = this->x() - 0.2 * cos(this->h()); // rear is back
1086 double ly = this->y() + 0.2 * sin(this->h() + M_PI/2);
1087 double ry = this->y() - 0.2 * sin(this->h() + M_PI/2);
1088 double fy = this->y() + 0.2 * sin(this->h());
1089 double by = this->y() - 0.2 * sin(this->h()); // rear is back
1090 out << lx << " " << ly << std::endl;
1091 out << rx << " " << ry << std::endl;
1093 out << fx << " " << fy << std::endl;
1094 out << bx << " " << by << std::endl;
1097 if (opts.LEFT_MIRROR) {
1098 this->lf().gen_gnuplot_to(out);
1099 this->lfm().gen_gnuplot_to(out);
1100 this->lr().gen_gnuplot_to(out);
1104 if (opts.RIGHT_MIRROR) {
1105 this->rf().gen_gnuplot_to(out);
1106 this->rfm().gen_gnuplot_to(out);
1107 this->rr().gen_gnuplot_to(out);