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 operator<<(std::ostream& out, Line const& li)
276 out << "[" << li._b << "," << li._e << "]";
280 Pose::Pose(double x, double y, double h) : Point(x, y), _h(h)
303 Pose::set_pose(Pose const& p)
311 Pose::rotate(Point const& c, double const angl)
313 Point::rotate(c, angl);
314 this->h(this->h() + angl);
318 Pose::reflect(Line const& li)
321 double dh = li.h() - this->h();
322 this->h(this->h() + 2.0 * dh);
326 Pose::operator==(Pose const& p)
328 return this->x() == p.x() && this->y() == p.y() && this->h() == p.h();
332 operator<<(std::ostream& out, Pose const& p)
334 out << "[" << p.x() << "," << p.y() << "," << p.h() << "]";
342 double bpbx = this->_bp.x() - clen * cos(this->_bp.h());
343 double bpby = this->_bp.y() - clen * sin(this->_bp.h());
344 double bpfx = this->_bp.x() + clen * cos(this->_bp.h());
345 double bpfy = this->_bp.y() + clen * sin(this->_bp.h());
346 Line li1(Point(bpbx, bpby), Point(bpfx, bpfy));
347 double epbx = this->_ep.x() - clen * cos(this->_ep.h());
348 double epby = this->_ep.y() - clen * sin(this->_ep.h());
349 double epfx = this->_ep.x() + clen * cos(this->_ep.h());
350 double epfy = this->_ep.y() + clen * sin(this->_ep.h());
351 Line li2(Point(epbx, epby), Point(epfx, epfy));
352 li1.intersects_with(li2);
353 this->x(li1.i1().x());
354 this->y(li1.i1().y());
355 double bh = this->b();
360 double eh = this->e();
365 this->h((this->b() + this->e()) / 2.0);
368 PoseRange::PoseRange(Pose bp, Pose ep) : _bp(bp), _ep(ep)
370 if (this->_bp == this->_ep) {
371 this->set_pose(this->_ep);
377 PoseRange::PoseRange(double x, double y, double b, double e)
378 : PoseRange(Pose(x, y, b), Pose(x, y, e))
383 PoseRange::bp() const
389 PoseRange::ep() const
397 return std::min(this->_bp.h(), this->_ep.h());
403 return std::max(this->_bp.h(), this->_ep.h());
407 PoseRange::translate(Point const& p)
409 this->_bp.translate(p);
410 this->_ep.translate(p);
415 PoseRange::rotate(Point const& c, double const angl)
417 this->_bp.rotate(c, angl);
418 this->_ep.rotate(c, angl);
423 PoseRange::reflect(Line const& li)
425 this->_bp.reflect(li);
426 this->_ep.reflect(li);
431 operator<<(std::ostream& out, PoseRange const& p)
433 out << "[" << p.x() << "," << p.y() << "," << p.b() << "," << p.e();
441 return this->_curb_to_curb;
445 CarSize::ctc(double ctc)
447 this->_curb_to_curb = ctc;
453 return this->_wheelbase;
457 CarSize::wb(double wb)
459 this->_wheelbase = wb;
477 return this->_width_with_mirrors;
481 CarSize::wwm(double w)
483 this->_width_with_mirrors = w;
489 return this->_length;
493 CarSize::len(double len)
501 return this->_distance_to_front;
505 CarSize::df(double df)
507 this->_distance_to_front = df;
513 return this->len() - this->df();
517 CarSize::ft(double ft)
519 this->_front_track = ft;
525 return this->_front_track;
531 auto ctc2 = pow(this->ctc() / 2.0, 2.0);
532 auto wb2 = pow(this->wb(), 2.0);
533 return sqrt(ctc2 - wb2) - this->ft() / 2.0;
537 CarSize::iradi() const
539 return this->mtr() - this->w() / 2;
543 CarSize::ofradi() const
545 auto mtrw2 = pow(this->mtr() + this->w() / 2.0, 2.0);
546 auto df2 = pow(this->df(), 2.0);
547 return sqrt(mtrw2 + df2);
551 CarSize::orradi() const
553 auto mtrw2 = pow(this->mtr() + this->w() / 2.0, 2.0);
554 auto dr2 = pow(this->dr(), 2.0);
555 return sqrt(mtrw2 + dr2);
559 CarSize::imradi() const
561 auto mtrw2 = pow(this->mtr() - this->wwm() / 2.0, 2.0);
562 auto df2 = pow(this->wb(), 2.0);
563 return sqrt(mtrw2 + df2);
567 CarSize::omradi() 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::perfect_parking_slot_len() const
577 auto r = this->ctc() / 2.0;
579 auto k = this->df() - this->wb();
580 auto w = this->w(); // FIXME use wwm()?
581 auto r2l2 = r * r - l * l;
582 auto s = r2l2 + pow(l + k, 2.0) - pow(sqrt(r2l2) - w, 2.0);
583 return this->len() + sqrt(s) - l - k;
593 CarMove::sp(double sp)
605 CarMove::st(double st)
611 BicycleCar::drivable(Pose const& p) const
613 return this->drivable(PoseRange(p, p));
617 BicycleCar::drivable(PoseRange const& p) const
619 double a_1 = atan2(p.y() - this->y(), p.x() - this->x()) - this->h();
624 double h_d = p.h() - this->h();
630 if (h_d == 0 && (a_1 == 0 || a_2 == M_PI || a_2 == -M_PI)) {
632 } else if (0 < a_1 && a_1 <= M_PI/2) { // left front
633 BicycleCar z(*this); // zone border
635 h_d = p.h() - this->h();
636 z.rotate(this->ccl(), h_d);
637 // assert z.h() == p.h()
638 if (p.y() == z.y() && p.x() == z.x()) // p on zone border
640 a_2 = atan2(p.y() - z.y(), p.x() - z.x());
645 if (z.h() >= a_2 && a_2 >= this->h())
647 } else if (M_PI/2 < a_1 && a_1 <= M_PI) { // left rear
648 BicycleCar z(*this); // zone border
650 h_d = p.h() - this->h();
651 z.rotate(this->ccl(), h_d);
652 // assert z.h() == p.h()
653 if (p.y() == z.y() && p.x() == z.x()) // p on zone border
655 a_2 = atan2(p.y() - z.y(), p.x() - z.x());
661 if (this->h() >= a_2 && a_2 >= z.h())
663 } else if (0 > a_1 && a_1 >= -M_PI/2) { // right front
664 BicycleCar z(*this); // zone border
666 h_d = p.h() - this->h();
667 z.rotate(this->ccr(), 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());
676 if (this->h() >= a_2 && a_2 >= z.h())
678 } else if (-M_PI/2 > a_1 && a_1 >= -M_PI) { // right rear
679 BicycleCar z(*this); // zone border
681 h_d = p.h() - this->h();
682 z.rotate(this->ccr(), h_d);
683 // assert z.h() == p.h()
684 if (p.y() == z.y() && p.x() == z.x()) // p on zone border
686 a_2 = atan2(p.y() - z.y(), p.x() - z.x());
692 if (z.h() >= a_2 && a_2 >= this->h())
695 // Not happenning, as ``-pi <= a <= pi``.
701 BicycleCar::set_max_steer()
703 this->st(atan(this->wb() / this->mtr()));
707 BicycleCar::lfx() const
709 double lfx = this->x();
710 lfx += (this->w() / 2.0) * cos(this->h() + M_PI / 2.0);
711 lfx += this->df() * cos(this->h());
716 BicycleCar::lfy() const
718 double lfy = this->y();
719 lfy += (this->w() / 2.0) * sin(this->h() + M_PI / 2.0);
720 lfy += this->df() * sin(this->h());
725 BicycleCar::lrx() const
727 double lrx = this->x();
728 lrx += (this->w() / 2.0) * cos(this->h() + M_PI / 2.0);
729 lrx += -this->dr() * cos(this->h());
734 BicycleCar::lry() const
736 double lry = this->y();
737 lry += (this->w() / 2.0) * sin(this->h() + M_PI / 2.0);
738 lry += -this->dr() * sin(this->h());
743 BicycleCar::rrx() const
745 double rrx = this->x();
746 rrx += (this->w() / 2.0) * cos(this->h() - M_PI / 2.0);
747 rrx += -this->dr() * cos(this->h());
752 BicycleCar::rry() const
754 double rry = this->y();
755 rry += (this->w() / 2.0) * sin(this->h() - M_PI / 2.0);
756 rry += -this->dr() * sin(this->h());
761 BicycleCar::rfx() const
763 double rfx = this->x();
764 rfx += (this->w() / 2.0) * cos(this->h() - M_PI / 2.0);
765 rfx += this->df() * cos(this->h());
770 BicycleCar::rfy() const
772 double rfy = this->y();
773 rfy += (this->w() / 2.0) * sin(this->h() - M_PI / 2.0);
774 rfy += this->df() * sin(this->h());
779 BicycleCar::lf() const
781 return Point(this->lfx(), this->lfy());
785 BicycleCar::lr() const
787 return Point(this->lrx(), this->lry());
791 BicycleCar::rr() const
793 return Point(this->rrx(), this->rry());
797 BicycleCar::rf() const
799 return Point(this->rfx(), this->rfy());
803 BicycleCar::left() const
805 return Line(this->lr(), this->lf());
809 BicycleCar::rear() const
811 return Line(this->lr(), this->rr());
815 BicycleCar::right() const
817 return Line(this->rr(), this->rf());
821 BicycleCar::front() const
823 return Line(this->rf(), this->lf());
827 BicycleCar::lrax() const
829 double lrx = this->x();
830 lrx += (this->w() / 2.0) * cos(this->h() + M_PI / 2.0);
834 BicycleCar::lray() const
836 double lry = this->y();
837 lry += (this->w() / 2.0) * sin(this->h() + M_PI / 2.0);
842 BicycleCar::rrax() const
844 double rrx = this->x();
845 rrx += (this->w() / 2.0) * cos(this->h() - M_PI / 2.0);
850 BicycleCar::rray() const
852 double rry = this->y();
853 rry += (this->w() / 2.0) * sin(this->h() - M_PI / 2.0);
858 BicycleCar::lra() const
860 return Point(this->lrax(), this->lray());
864 BicycleCar::rra() const
866 return Point(this->rrax(), this->rray());
870 BicycleCar::lfax() const
872 return this->lrax() + this->wb() * cos(this->h());
876 BicycleCar::lfay() const
878 return this->lray() + this->wb() * sin(this->h());
882 BicycleCar::rfax() const
884 return this->rrax() + this->wb() * cos(this->h());
888 BicycleCar::rfay() const
890 return this->rray() + this->wb() * sin(this->h());
894 BicycleCar::lfa() const
896 return Point(this->lfax(), this->lfay());
900 BicycleCar::rfa() const
902 return Point(this->rfax(), this->rfay());
906 BicycleCar::lfmx() const
908 double x = this->x();
909 x += (this->wwm() / 2.0) * cos(this->h() + M_PI / 2.0);
910 x += this->wb() * cos(this->h());
915 BicycleCar::lfmy() const
917 double y = this->y();
918 y += (this->wwm() / 2.0) * sin(this->h() + M_PI / 2.0);
919 y += this->wb() * sin(this->h());
924 BicycleCar::rfmx() const
926 double x = this->x();
927 x += (this->wwm() / 2.0) * cos(this->h() - M_PI / 2.0);
928 x += this->wb() * cos(this->h());
933 BicycleCar::rfmy() const
935 double y = this->y();
936 y += (this->wwm() / 2.0) * sin(this->h() - M_PI / 2.0);
937 y += this->wb() * sin(this->h());
942 BicycleCar::lfm() const
944 return Point(this->lfmx(), this->lfmy());
948 BicycleCar::rfm() const
950 return Point(this->rfmx(), this->rfmy());
954 BicycleCar::cfx() const
956 return this->x() + this->df() * cos(this->h());
960 BicycleCar::cfy() const
962 return this->y() + this->df() * sin(this->h());
966 BicycleCar::cf() const
968 return Point(this->cfx(), this->cfy());
972 BicycleCar::ccl() const
975 this->x() + this->mtr() * cos(this->h() + M_PI / 2.0),
976 this->y() + this->mtr() * sin(this->h() + M_PI / 2.0)
981 BicycleCar::ccr() const
984 this->x() + this->mtr() * cos(this->h() - M_PI / 2.0),
985 this->y() + this->mtr() * sin(this->h() - M_PI / 2.0)
992 this->x(this->x() + this->sp() * cos(this->h()));
993 this->y(this->y() + this->sp() * sin(this->h()));
994 this->h(this->h() + this->sp() / this->wb() * tan(this->st()));
998 BicycleCar::gen_gnuplot_to(std::ostream& out, GenPlotOpts opts)
1002 opts.MIRRORS = true;
1005 opts.LEFT_MIRROR = true;
1006 opts.RIGHT_MIRROR = true;
1019 if (opts.LF_POINT) {
1020 this->lf().gen_gnuplot_to(out);
1022 if (opts.LR_POINT) {
1023 this->lr().gen_gnuplot_to(out);
1025 if (opts.RR_POINT) {
1026 this->rr().gen_gnuplot_to(out);
1028 if (opts.RF_POINT) {
1029 this->rf().gen_gnuplot_to(out);
1031 if (opts.LFM_POINT) {
1032 this->lfm().gen_gnuplot_to(out);
1034 if (opts.RFM_POINT) {
1035 this->rfm().gen_gnuplot_to(out);
1037 if (opts.CRA_POINT || opts.CAR_POINT) {
1038 Point::gen_gnuplot_to(out);
1041 this->lf().gen_gnuplot_to(out);
1042 this->lr().gen_gnuplot_to(out);
1046 this->rf().gen_gnuplot_to(out);
1047 this->rr().gen_gnuplot_to(out);
1051 this->lr().gen_gnuplot_to(out);
1052 this->rr().gen_gnuplot_to(out);
1056 this->lf().gen_gnuplot_to(out);
1057 this->rf().gen_gnuplot_to(out);
1061 this->cf().gen_gnuplot_to(out);
1062 this->lfa().gen_gnuplot_to(out);
1063 this->rfa().gen_gnuplot_to(out);
1064 this->cf().gen_gnuplot_to(out);
1068 double lx = this->x() + 0.2 * cos(this->h() + M_PI/2);
1069 double rx = this->x() - 0.2 * cos(this->h() + M_PI/2);
1070 double fx = this->x() + 0.2 * cos(this->h());
1071 double bx = this->x() - 0.2 * cos(this->h()); // rear is back
1072 double ly = this->y() + 0.2 * sin(this->h() + M_PI/2);
1073 double ry = this->y() - 0.2 * sin(this->h() + M_PI/2);
1074 double fy = this->y() + 0.2 * sin(this->h());
1075 double by = this->y() - 0.2 * sin(this->h()); // rear is back
1076 out << lx << " " << ly << std::endl;
1077 out << rx << " " << ry << std::endl;
1079 out << fx << " " << fy << std::endl;
1080 out << bx << " " << by << std::endl;
1083 if (opts.LEFT_MIRROR) {
1084 this->lf().gen_gnuplot_to(out);
1085 this->lfm().gen_gnuplot_to(out);
1086 this->lr().gen_gnuplot_to(out);
1090 if (opts.RIGHT_MIRROR) {
1091 this->rf().gen_gnuplot_to(out);
1092 this->rfm().gen_gnuplot_to(out);
1093 this->rr().gen_gnuplot_to(out);