10 Point::Point(double x, double y) : x_(x), y_(y)
39 Point::min_angle_between(Point const& p1, Point const& p2) const
41 double d1x = p1.x() - this->x();
42 double d1y = p1.y() - this->y();
43 double d2x = p2.x() - p1.x();
44 double d2y = p2.y() - p1.y();
46 double dot = d1x*d2x + d1y*d2y;
47 double d1 = sqrt(d1x*d1x + d1y*d1y);
48 double d2 = sqrt(d2x*d2x + d2y*d2y);
50 double delta = acos(dot / (d1 * d2));
51 return std::min(delta, M_PI - delta);
55 Point::inside_of(std::vector<Point> const& poly) const
57 unsigned int num = poly.size();
58 unsigned int j = num - 1;
60 for (unsigned int i = 0; i < num; i++) {
61 if (this->x() == poly[i].x() && this->y() == poly[i].y()) {
64 if ((poly[i].y() > this->y()) != (poly[j].y() > this->y())) {
65 auto slope1 = this->x() - poly[i].x();
66 slope1 *= poly[j].y() - poly[i].y();
67 auto slope2 = poly[j].x() - poly[i].x();
68 slope2 *= this->y() - poly[i].y();
69 auto slope = slope1 - slope2;
73 if ((slope < 0.0) != (poly[j].y() < poly[i].y())) {
83 Point::on_right_side_of(Line const& li) const
91 if (sgn((x3 - x1) * (y2 - y1) - (y3 - y1) * (x2 - x1)) < 0.0) {
99 Point::translate(Point const& p)
106 Point::rotate(Point const& c, double const angl)
108 double px = this->x();
109 double py = this->y();
112 double nx = px * cos(angl) - py * sin(angl);
113 double ny = px * sin(angl) + py * cos(angl);
119 Point::reflect(Line const& li)
121 this->rotate(li.b(), -li.h());
122 this->y_ -= li.b().y();
124 this->y_ += li.b().y();
125 this->rotate(li.b(), li.h());
129 Point::edist(Point const& p) const
131 return sqrt(pow(p.x() - this->x_, 2.0) + pow(p.y() - this->y_, 2.0));
135 Point::operator==(Point const& p)
137 return this->x() == p.x() && this->y() == p.y();
141 operator<<(std::ostream& out, Point const& p)
143 out << "[" << p.x() << "," << p.y() << "]";
147 Line::Line(Point const& b, Point const& e): b_(b), e_(e)
166 return Point((this->b_.x() + this->e_.x()) / 2.0,
167 (this->b_.y() + this->e_.y()) / 2.0);
183 Line::intersects_with(Line const& li)
185 auto x1 = this->b_.x();
186 auto y1 = this->b_.y();
187 auto x2 = this->e_.x();
188 auto y2 = this->e_.y();
189 auto x3 = li.b().x();
190 auto y3 = li.b().y();
191 auto x4 = li.e().x();
192 auto y4 = li.e().y();
193 double deno = (x1 - x2) * (y3 - y4) - (y1 - y2) * (x3 - x4);
197 double t = (x1 - x3) * (y3 - y4) - (y1 - y3) * (x3 - x4);
199 double u = (x1 - x2) * (y1 - y3) - (y1 - y2) * (x1 - x3);
202 if (t < 0.0 || t > 1.0 || u < 0.0 || u > 1.0) {
205 this->i1_.x(x1 + t * (x2 - x1));
206 this->i1_.y(y1 + t * (y2 - y1));
211 Line::intersects_with(Point const& c, double const r)
213 auto x1 = this->b_.x();
214 auto y1 = this->b_.y();
215 auto x2 = this->e_.x();
216 auto y2 = this->e_.y();
228 double dr = sqrt(dx*dx + dy*dy);
229 double D = x1*y2 - x2*y1;
230 if (r*r * dr*dr - D*D < 0.0) {
233 // intersection coordinates
234 double ix1 = (D*dy + sgn(dy)*dx*sqrt(r*r * dr*dr - D*D)) / (dr*dr);
236 double ix2 = (D*dy - sgn(dy)*dx*sqrt(r*r * dr*dr - D*D)) / (dr*dr);
238 double iy1 = (-D*dx + std::abs(dy)*sqrt(r*r * dr*dr - D*D)) / (dr*dr);
240 double iy2 = (-D*dx - std::abs(dy)*sqrt(r*r * dr*dr - D*D)) / (dr*dr);
252 return this->b_.edist(this->e_);
258 return atan2(this->e_.y() - this->b_.y(), this->e_.x() - this->b_.x());
262 operator<<(std::ostream& out, Line const& li)
264 out << "[" << li.b_ << "," << li.e_ << "]";
268 Pose::Pose(double x, double y, double h) : Point(x, y), h_(h)
291 Pose::set_pose(Pose const& p)
299 Pose::rotate(Point const& c, double const angl)
301 Point::rotate(c, angl);
302 this->h(this->h() + angl);
306 Pose::reflect(Line const& li)
309 double dh = li.h() - this->h();
310 this->h(this->h() + 2.0 * dh);
314 Pose::operator==(Pose const& p)
316 return this->x() == p.x() && this->y() == p.y() && this->h() == p.h();
320 operator<<(std::ostream& out, Pose const& p)
322 out << "[" << p.x() << "," << p.y() << "," << p.h() << "]";
330 double bpbx = this->bp_.x() - clen * cos(this->bp_.h());
331 double bpby = this->bp_.y() - clen * sin(this->bp_.h());
332 double bpfx = this->bp_.x() + clen * cos(this->bp_.h());
333 double bpfy = this->bp_.y() + clen * sin(this->bp_.h());
334 Line li1(Point(bpbx, bpby), Point(bpfx, bpfy));
335 double epbx = this->ep_.x() - clen * cos(this->ep_.h());
336 double epby = this->ep_.y() - clen * sin(this->ep_.h());
337 double epfx = this->ep_.x() + clen * cos(this->ep_.h());
338 double epfy = this->ep_.y() + clen * sin(this->ep_.h());
339 Line li2(Point(epbx, epby), Point(epfx, epfy));
340 li1.intersects_with(li2);
341 this->x(li1.i1().x());
342 this->y(li1.i1().y());
343 this->h((this->b() + this->e()) / 2.0);
346 PoseRange::PoseRange(Pose bp, Pose ep) : bp_(bp), ep_(ep)
348 if (this->bp_ == this->ep_) {
349 this->set_pose(this->ep_);
355 PoseRange::PoseRange(double x, double y, double b, double e)
356 : PoseRange(Pose(x, y, b), Pose(x, y, e))
361 PoseRange::bp() const
367 PoseRange::ep() const
375 return std::min(this->bp_.h(), this->ep_.h());
381 return std::max(this->bp_.h(), this->ep_.h());
385 PoseRange::translate(Point const& p)
387 this->bp_.translate(p);
388 this->ep_.translate(p);
393 PoseRange::rotate(Point const& c, double const angl)
395 this->bp_.rotate(c, angl);
396 this->ep_.rotate(c, angl);
401 PoseRange::reflect(Line const& li)
403 this->bp_.reflect(li);
404 this->ep_.reflect(li);
409 operator<<(std::ostream& out, PoseRange const& p)
411 out << "[" << p.x() << "," << p.y() << "," << p.b() << "," << p.e();
419 return this->curb_to_curb_;
423 CarSize::ctc(double ctc)
425 this->curb_to_curb_ = ctc;
431 return this->wheelbase_;
435 CarSize::wb(double wb)
437 this->wheelbase_ = wb;
455 return this->length_;
459 CarSize::len(double len)
467 return this->distance_to_front_;
471 CarSize::df(double df)
473 this->distance_to_front_ = df;
479 return this->len() - this->df();
485 auto ctc2 = pow(this->ctc() / 2.0, 2.0);
486 auto wb2 = pow(this->wb(), 2.0);
487 return sqrt(ctc2 - wb2) - this->w() / 2.0;
491 CarSize::iradi() const
493 return this->mtr() - this->w() / 2;
497 CarSize::ofradi() const
499 auto mtrw2 = pow(this->mtr() + this->w() / 2.0, 2.0);
500 auto df2 = pow(this->df(), 2.0);
501 return sqrt(mtrw2 + df2);
505 CarSize::orradi() const
507 auto mtrw2 = pow(this->mtr() + this->w() / 2.0, 2.0);
508 auto dr2 = pow(this->dr(), 2.0);
509 return sqrt(mtrw2 + dr2);
513 CarSize::perfect_parking_slot_len() const
515 auto r = this->ctc() / 2.0;
517 auto k = this->df() - this->wb();
519 auto r2l2 = r * r - l * l;
520 auto s = r2l2 + pow(l + k, 2.0) - pow(sqrt(r2l2) - w, 2.0);
521 return this->len() + sqrt(s) - l - k;
531 CarMove::sp(double sp)
543 CarMove::st(double st)
549 BicycleCar::drivable(Pose const& p) const
551 return this->drivable(PoseRange(p, p));
555 BicycleCar::drivable(PoseRange const& p) const
557 double a_1 = atan2(p.y() - this->y(), p.x() - this->x()) - this->h();
562 double h_d = p.h() - this->h();
568 if (h_d == 0 && (a_1 == 0 || a_2 == M_PI || a_2 == -M_PI)) {
570 } else if (0 < a_1 && a_1 <= M_PI/2) { // left front
571 BicycleCar z(*this); // zone border
573 h_d = p.h() - this->h();
574 z.rotate(this->ccl(), h_d);
575 // assert z.h() == p.h()
576 if (p.y() == z.y() && p.x() == z.x()) // p on zone border
578 a_2 = atan2(p.y() - z.y(), p.x() - z.x());
583 if (z.h() >= a_2 && a_2 >= this->h())
585 } else if (M_PI/2 < a_1 && a_1 <= M_PI) { // left rear
586 BicycleCar z(*this); // zone border
588 h_d = p.h() - this->h();
589 z.rotate(this->ccl(), h_d);
590 // assert z.h() == p.h()
591 if (p.y() == z.y() && p.x() == z.x()) // p on zone border
593 a_2 = atan2(p.y() - z.y(), p.x() - z.x());
599 if (this->h() >= a_2 && a_2 >= z.h())
601 } else if (0 > a_1 && a_1 >= -M_PI/2) { // right front
602 BicycleCar z(*this); // zone border
604 h_d = p.h() - this->h();
605 z.rotate(this->ccr(), 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());
614 if (this->h() >= a_2 && a_2 >= z.h())
616 } else if (-M_PI/2 > a_1 && a_1 >= -M_PI) { // right rear
617 BicycleCar z(*this); // zone border
619 h_d = p.h() - this->h();
620 z.rotate(this->ccr(), h_d);
621 // assert z.h() == p.h()
622 if (p.y() == z.y() && p.x() == z.x()) // p on zone border
624 a_2 = atan2(p.y() - z.y(), p.x() - z.x());
630 if (z.h() >= a_2 && a_2 >= this->h())
633 // Not happenning, as ``-pi <= a <= pi``.
639 BicycleCar::set_max_steer()
641 this->st(atan(this->wb() / this->mtr()));
645 BicycleCar::lfx() const
647 double lfx = this->x();
648 lfx += (this->w() / 2.0) * cos(this->h() + M_PI / 2.0);
649 lfx += this->df() * cos(this->h());
654 BicycleCar::lfy() const
656 double lfy = this->y();
657 lfy += (this->w() / 2.0) * sin(this->h() + M_PI / 2.0);
658 lfy += this->df() * sin(this->h());
663 BicycleCar::lrx() const
665 double lrx = this->x();
666 lrx += (this->w() / 2.0) * cos(this->h() + M_PI / 2.0);
667 lrx += -this->dr() * cos(this->h());
672 BicycleCar::lry() const
674 double lry = this->y();
675 lry += (this->w() / 2.0) * sin(this->h() + M_PI / 2.0);
676 lry += -this->dr() * sin(this->h());
681 BicycleCar::rrx() const
683 double rrx = this->x();
684 rrx += (this->w() / 2.0) * cos(this->h() - M_PI / 2.0);
685 rrx += -this->dr() * cos(this->h());
690 BicycleCar::rry() const
692 double rry = this->y();
693 rry += (this->w() / 2.0) * sin(this->h() - M_PI / 2.0);
694 rry += -this->dr() * sin(this->h());
699 BicycleCar::rfx() const
701 double rfx = this->x();
702 rfx += (this->w() / 2.0) * cos(this->h() - M_PI / 2.0);
703 rfx += this->df() * cos(this->h());
708 BicycleCar::rfy() const
710 double rfy = this->y();
711 rfy += (this->w() / 2.0) * sin(this->h() - M_PI / 2.0);
712 rfy += this->df() * sin(this->h());
717 BicycleCar::lf() const
719 return Point(this->lfx(), this->lfy());
723 BicycleCar::lr() const
725 return Point(this->lrx(), this->lry());
729 BicycleCar::rr() const
731 return Point(this->rrx(), this->rry());
735 BicycleCar::rf() const
737 return Point(this->rfx(), this->rfy());
741 BicycleCar::left() const
743 return Line(this->lr(), this->lf());
747 BicycleCar::rear() const
749 return Line(this->lr(), this->rr());
753 BicycleCar::right() const
755 return Line(this->rr(), this->rf());
759 BicycleCar::front() const
761 return Line(this->rf(), this->lf());
765 BicycleCar::ralx() const
767 double lrx = this->x();
768 lrx += (this->w() / 2.0) * cos(this->h() + M_PI / 2.0);
772 BicycleCar::raly() const
774 double lry = this->y();
775 lry += (this->w() / 2.0) * sin(this->h() + M_PI / 2.0);
780 BicycleCar::rarx() const
782 double rrx = this->x();
783 rrx += (this->w() / 2.0) * cos(this->h() - M_PI / 2.0);
788 BicycleCar::rary() const
790 double rry = this->y();
791 rry += (this->w() / 2.0) * sin(this->h() - M_PI / 2.0);
796 BicycleCar::ccl() const
799 this->x() + this->mtr() * cos(this->h() + M_PI / 2.0),
800 this->y() + this->mtr() * sin(this->h() + M_PI / 2.0)
805 BicycleCar::ccr() const
808 this->x() + this->mtr() * cos(this->h() - M_PI / 2.0),
809 this->y() + this->mtr() * sin(this->h() - M_PI / 2.0)
816 this->x(this->x() + this->sp() * cos(this->h()));
817 this->y(this->y() + this->sp() * sin(this->h()));
818 this->h(this->h() + this->sp() / this->wb() * tan(this->st()));