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::operator==(Point const& p)
143 return this->x() == p.x() && this->y() == p.y();
147 operator<<(std::ostream& out, Point const& p)
149 out << "[" << p.x() << "," << p.y() << "]";
153 Line::Line(Point const& b, Point const& e): b_(b), e_(e)
172 return Point((this->b_.x() + this->e_.x()) / 2.0,
173 (this->b_.y() + this->e_.y()) / 2.0);
189 Line::intersects_with(Line const& li)
191 auto x1 = this->b_.x();
192 auto y1 = this->b_.y();
193 auto x2 = this->e_.x();
194 auto y2 = this->e_.y();
195 auto x3 = li.b().x();
196 auto y3 = li.b().y();
197 auto x4 = li.e().x();
198 auto y4 = li.e().y();
199 double deno = (x1 - x2) * (y3 - y4) - (y1 - y2) * (x3 - x4);
203 double t = (x1 - x3) * (y3 - y4) - (y1 - y3) * (x3 - x4);
205 double u = (x1 - x2) * (y1 - y3) - (y1 - y2) * (x1 - x3);
208 if (t < 0.0 || t > 1.0 || u < 0.0 || u > 1.0) {
211 this->i1_.x(x1 + t * (x2 - x1));
212 this->i1_.y(y1 + t * (y2 - y1));
217 Line::intersects_with(Point const& c, double const r)
219 auto x1 = this->b_.x();
220 auto y1 = this->b_.y();
221 auto x2 = this->e_.x();
222 auto y2 = this->e_.y();
234 double dr = sqrt(dx*dx + dy*dy);
235 double D = x1*y2 - x2*y1;
236 if (r*r * dr*dr - D*D < 0.0) {
239 // intersection coordinates
240 double ix1 = (D*dy + sgn(dy)*dx*sqrt(r*r * dr*dr - D*D)) / (dr*dr);
242 double ix2 = (D*dy - sgn(dy)*dx*sqrt(r*r * dr*dr - D*D)) / (dr*dr);
244 double iy1 = (-D*dx + std::abs(dy)*sqrt(r*r * dr*dr - D*D)) / (dr*dr);
246 double iy2 = (-D*dx - std::abs(dy)*sqrt(r*r * dr*dr - D*D)) / (dr*dr);
258 return this->b_.edist(this->e_);
264 return atan2(this->e_.y() - this->b_.y(), this->e_.x() - this->b_.x());
268 operator<<(std::ostream& out, Line const& li)
270 out << "[" << li.b_ << "," << li.e_ << "]";
274 Pose::Pose(double x, double y, double h) : Point(x, y), h_(h)
297 Pose::set_pose(Pose const& p)
305 Pose::rotate(Point const& c, double const angl)
307 Point::rotate(c, angl);
308 this->h(this->h() + angl);
312 Pose::reflect(Line const& li)
315 double dh = li.h() - this->h();
316 this->h(this->h() + 2.0 * dh);
320 Pose::operator==(Pose const& p)
322 return this->x() == p.x() && this->y() == p.y() && this->h() == p.h();
326 operator<<(std::ostream& out, Pose const& p)
328 out << "[" << p.x() << "," << p.y() << "," << p.h() << "]";
336 double bpbx = this->bp_.x() - clen * cos(this->bp_.h());
337 double bpby = this->bp_.y() - clen * sin(this->bp_.h());
338 double bpfx = this->bp_.x() + clen * cos(this->bp_.h());
339 double bpfy = this->bp_.y() + clen * sin(this->bp_.h());
340 Line li1(Point(bpbx, bpby), Point(bpfx, bpfy));
341 double epbx = this->ep_.x() - clen * cos(this->ep_.h());
342 double epby = this->ep_.y() - clen * sin(this->ep_.h());
343 double epfx = this->ep_.x() + clen * cos(this->ep_.h());
344 double epfy = this->ep_.y() + clen * sin(this->ep_.h());
345 Line li2(Point(epbx, epby), Point(epfx, epfy));
346 li1.intersects_with(li2);
347 this->x(li1.i1().x());
348 this->y(li1.i1().y());
349 double bh = this->b();
354 double eh = this->e();
359 this->h((this->b() + this->e()) / 2.0);
362 PoseRange::PoseRange(Pose bp, Pose ep) : bp_(bp), ep_(ep)
364 if (this->bp_ == this->ep_) {
365 this->set_pose(this->ep_);
371 PoseRange::PoseRange(double x, double y, double b, double e)
372 : PoseRange(Pose(x, y, b), Pose(x, y, e))
377 PoseRange::bp() const
383 PoseRange::ep() const
391 return std::min(this->bp_.h(), this->ep_.h());
397 return std::max(this->bp_.h(), this->ep_.h());
401 PoseRange::translate(Point const& p)
403 this->bp_.translate(p);
404 this->ep_.translate(p);
409 PoseRange::rotate(Point const& c, double const angl)
411 this->bp_.rotate(c, angl);
412 this->ep_.rotate(c, angl);
417 PoseRange::reflect(Line const& li)
419 this->bp_.reflect(li);
420 this->ep_.reflect(li);
425 operator<<(std::ostream& out, PoseRange const& p)
427 out << "[" << p.x() << "," << p.y() << "," << p.b() << "," << p.e();
435 return this->curb_to_curb_;
439 CarSize::ctc(double ctc)
441 this->curb_to_curb_ = ctc;
447 return this->wheelbase_;
451 CarSize::wb(double wb)
453 this->wheelbase_ = wb;
471 return this->length_;
475 CarSize::len(double len)
483 return this->distance_to_front_;
487 CarSize::df(double df)
489 this->distance_to_front_ = df;
495 return this->len() - this->df();
501 auto ctc2 = pow(this->ctc() / 2.0, 2.0);
502 auto wb2 = pow(this->wb(), 2.0);
503 return sqrt(ctc2 - wb2) - this->w() / 2.0;
507 CarSize::iradi() const
509 return this->mtr() - this->w() / 2;
513 CarSize::ofradi() const
515 auto mtrw2 = pow(this->mtr() + this->w() / 2.0, 2.0);
516 auto df2 = pow(this->df(), 2.0);
517 return sqrt(mtrw2 + df2);
521 CarSize::orradi() const
523 auto mtrw2 = pow(this->mtr() + this->w() / 2.0, 2.0);
524 auto dr2 = pow(this->dr(), 2.0);
525 return sqrt(mtrw2 + dr2);
529 CarSize::perfect_parking_slot_len() const
531 auto r = this->ctc() / 2.0;
533 auto k = this->df() - this->wb();
535 auto r2l2 = r * r - l * l;
536 auto s = r2l2 + pow(l + k, 2.0) - pow(sqrt(r2l2) - w, 2.0);
537 return this->len() + sqrt(s) - l - k;
547 CarMove::sp(double sp)
559 CarMove::st(double st)
565 BicycleCar::drivable(Pose const& p) const
567 return this->drivable(PoseRange(p, p));
571 BicycleCar::drivable(PoseRange const& p) const
573 double a_1 = atan2(p.y() - this->y(), p.x() - this->x()) - this->h();
578 double h_d = p.h() - this->h();
584 if (h_d == 0 && (a_1 == 0 || a_2 == M_PI || a_2 == -M_PI)) {
586 } else if (0 < a_1 && a_1 <= M_PI/2) { // left front
587 BicycleCar z(*this); // zone border
589 h_d = p.h() - this->h();
590 z.rotate(this->ccl(), h_d);
591 // assert z.h() == p.h()
592 if (p.y() == z.y() && p.x() == z.x()) // p on zone border
594 a_2 = atan2(p.y() - z.y(), p.x() - z.x());
599 if (z.h() >= a_2 && a_2 >= this->h())
601 } else if (M_PI/2 < a_1 && a_1 <= M_PI) { // left rear
602 BicycleCar z(*this); // zone border
604 h_d = p.h() - this->h();
605 z.rotate(this->ccl(), h_d);
606 // assert z.h() == p.h()
607 if (p.y() == z.y() && p.x() == z.x()) // p on zone border
609 a_2 = atan2(p.y() - z.y(), p.x() - z.x());
615 if (this->h() >= a_2 && a_2 >= z.h())
617 } else if (0 > a_1 && a_1 >= -M_PI/2) { // right front
618 BicycleCar z(*this); // zone border
620 h_d = p.h() - this->h();
621 z.rotate(this->ccr(), h_d);
622 // assert z.h() == p.h()
623 if (p.y() == z.y() && p.x() == z.x()) // p on zone border
625 a_2 = atan2(p.y() - z.y(), p.x() - z.x());
630 if (this->h() >= a_2 && a_2 >= z.h())
632 } else if (-M_PI/2 > a_1 && a_1 >= -M_PI) { // right rear
633 BicycleCar z(*this); // zone border
635 h_d = p.h() - this->h();
636 z.rotate(this->ccr(), 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());
646 if (z.h() >= a_2 && a_2 >= this->h())
649 // Not happenning, as ``-pi <= a <= pi``.
655 BicycleCar::set_max_steer()
657 this->st(atan(this->wb() / this->mtr()));
661 BicycleCar::lfx() const
663 double lfx = this->x();
664 lfx += (this->w() / 2.0) * cos(this->h() + M_PI / 2.0);
665 lfx += this->df() * cos(this->h());
670 BicycleCar::lfy() const
672 double lfy = this->y();
673 lfy += (this->w() / 2.0) * sin(this->h() + M_PI / 2.0);
674 lfy += this->df() * sin(this->h());
679 BicycleCar::lrx() const
681 double lrx = this->x();
682 lrx += (this->w() / 2.0) * cos(this->h() + M_PI / 2.0);
683 lrx += -this->dr() * cos(this->h());
688 BicycleCar::lry() const
690 double lry = this->y();
691 lry += (this->w() / 2.0) * sin(this->h() + M_PI / 2.0);
692 lry += -this->dr() * sin(this->h());
697 BicycleCar::rrx() const
699 double rrx = this->x();
700 rrx += (this->w() / 2.0) * cos(this->h() - M_PI / 2.0);
701 rrx += -this->dr() * cos(this->h());
706 BicycleCar::rry() const
708 double rry = this->y();
709 rry += (this->w() / 2.0) * sin(this->h() - M_PI / 2.0);
710 rry += -this->dr() * sin(this->h());
715 BicycleCar::rfx() const
717 double rfx = this->x();
718 rfx += (this->w() / 2.0) * cos(this->h() - M_PI / 2.0);
719 rfx += this->df() * cos(this->h());
724 BicycleCar::rfy() const
726 double rfy = this->y();
727 rfy += (this->w() / 2.0) * sin(this->h() - M_PI / 2.0);
728 rfy += this->df() * sin(this->h());
733 BicycleCar::lf() const
735 return Point(this->lfx(), this->lfy());
739 BicycleCar::lr() const
741 return Point(this->lrx(), this->lry());
745 BicycleCar::rr() const
747 return Point(this->rrx(), this->rry());
751 BicycleCar::rf() const
753 return Point(this->rfx(), this->rfy());
757 BicycleCar::left() const
759 return Line(this->lr(), this->lf());
763 BicycleCar::rear() const
765 return Line(this->lr(), this->rr());
769 BicycleCar::right() const
771 return Line(this->rr(), this->rf());
775 BicycleCar::front() const
777 return Line(this->rf(), this->lf());
781 BicycleCar::ralx() const
783 double lrx = this->x();
784 lrx += (this->w() / 2.0) * cos(this->h() + M_PI / 2.0);
788 BicycleCar::raly() const
790 double lry = this->y();
791 lry += (this->w() / 2.0) * sin(this->h() + M_PI / 2.0);
796 BicycleCar::rarx() const
798 double rrx = this->x();
799 rrx += (this->w() / 2.0) * cos(this->h() - M_PI / 2.0);
804 BicycleCar::rary() const
806 double rry = this->y();
807 rry += (this->w() / 2.0) * sin(this->h() - M_PI / 2.0);
812 BicycleCar::ccl() const
815 this->x() + this->mtr() * cos(this->h() + M_PI / 2.0),
816 this->y() + this->mtr() * sin(this->h() + M_PI / 2.0)
821 BicycleCar::ccr() const
824 this->x() + this->mtr() * cos(this->h() - M_PI / 2.0),
825 this->y() + this->mtr() * sin(this->h() - M_PI / 2.0)
832 this->x(this->x() + this->sp() * cos(this->h()));
833 this->y(this->y() + this->sp() * sin(this->h()));
834 this->h(this->h() + this->sp() / this->wb() * tan(this->st()));