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::rotate(Point const& c, double const angl)
101 double px = this->x();
102 double py = this->y();
105 double nx = px * cos(angl) - py * sin(angl);
106 double ny = px * sin(angl) + py * cos(angl);
112 Point::reflect(Line const& li)
114 this->rotate(li.b(), -li.h());
115 this->y_ -= li.b().y();
117 this->y_ += li.b().y();
118 this->rotate(li.b(), li.h());
122 Point::edist(Point const& p) const
124 return sqrt(pow(p.x() - this->x_, 2.0) + pow(p.y() - this->y_, 2.0));
128 Point::operator==(Point const& p)
130 return this->x() == p.x() && this->y() == p.y();
134 operator<<(std::ostream& out, Point const& p)
136 out << "[" << p.x() << "," << p.y() << "]";
140 Line::Line(Point const& b, Point const& e): b_(b), e_(e)
169 Line::intersects_with(Line const& li)
171 auto x1 = this->b_.x();
172 auto y1 = this->b_.y();
173 auto x2 = this->e_.x();
174 auto y2 = this->e_.y();
175 auto x3 = li.b().x();
176 auto y3 = li.b().y();
177 auto x4 = li.e().x();
178 auto y4 = li.e().y();
179 double deno = (x1 - x2) * (y3 - y4) - (y1 - y2) * (x3 - x4);
183 double t = (x1 - x3) * (y3 - y4) - (y1 - y3) * (x3 - x4);
185 double u = (x1 - x2) * (y1 - y3) - (y1 - y2) * (x1 - x3);
188 if (t < 0.0 || t > 1.0 || u < 0.0 || u > 1.0) {
191 this->i1_.x(x1 + t * (x2 - x1));
192 this->i1_.y(y1 + t * (y2 - y1));
197 Line::intersects_with(Point const& c, double const r)
199 auto x1 = this->b_.x();
200 auto y1 = this->b_.y();
201 auto x2 = this->e_.x();
202 auto y2 = this->e_.y();
214 double dr = sqrt(dx*dx + dy*dy);
215 double D = x1*y2 - x2*y1;
216 if (r*r * dr*dr - D*D < 0.0) {
219 // intersection coordinates
220 double ix1 = (D*dy + sgn(dy)*dx*sqrt(r*r * dr*dr - D*D)) / (dr*dr);
222 double ix2 = (D*dy - sgn(dy)*dx*sqrt(r*r * dr*dr - D*D)) / (dr*dr);
224 double iy1 = (-D*dx + std::abs(dy)*sqrt(r*r * dr*dr - D*D)) / (dr*dr);
226 double iy2 = (-D*dx - std::abs(dy)*sqrt(r*r * dr*dr - D*D)) / (dr*dr);
238 return this->b_.edist(this->e_);
244 return atan2(this->e_.y() - this->b_.y(), this->e_.x() - this->b_.x());
248 operator<<(std::ostream& out, Line const& li)
250 out << "[" << li.b_ << "," << li.e_ << "]";
254 Pose::Pose(double x, double y, double h) : Point(x, y), h_(h)
277 Pose::set_pose(Pose const& p)
285 Pose::rotate(Point const& c, double const angl)
287 Point::rotate(c, angl);
288 this->h(this->h() + angl);
292 Pose::reflect(Line const& li)
295 double dh = li.h() - this->h();
296 this->h(this->h() + 2.0 * dh);
300 Pose::operator==(Pose const& p)
302 return this->x() == p.x() && this->y() == p.y() && this->h() == p.h();
306 operator<<(std::ostream& out, Pose const& p)
308 out << "[" << p.x() << "," << p.y() << "," << p.h() << "]";
316 double bpbx = this->bp_.x() - clen * cos(this->bp_.h());
317 double bpby = this->bp_.y() - clen * sin(this->bp_.h());
318 double bpfx = this->bp_.x() + clen * cos(this->bp_.h());
319 double bpfy = this->bp_.y() + clen * sin(this->bp_.h());
320 Line li1(Point(bpbx, bpby), Point(bpfx, bpfy));
321 double epbx = this->ep_.x() - clen * cos(this->ep_.h());
322 double epby = this->ep_.y() - clen * sin(this->ep_.h());
323 double epfx = this->ep_.x() + clen * cos(this->ep_.h());
324 double epfy = this->ep_.y() + clen * sin(this->ep_.h());
325 Line li2(Point(epbx, epby), Point(epfx, epfy));
326 li1.intersects_with(li2);
327 this->x(li1.i1().x());
328 this->y(li1.i1().y());
329 this->h((this->b() + this->e()) / 2.0);
332 PoseRange::PoseRange(Pose bp, Pose ep) : bp_(bp), ep_(ep)
334 if (this->bp_ == this->ep_) {
335 this->set_pose(this->ep_);
342 PoseRange::bp() const
348 PoseRange::ep() const
356 return std::min(this->bp_.h(), this->ep_.h());
362 return std::max(this->bp_.h(), this->ep_.h());
366 PoseRange::rotate(Point const& c, double const angl)
368 this->bp_.rotate(c, angl);
369 this->ep_.rotate(c, angl);
374 PoseRange::reflect(Line const& li)
376 this->bp_.reflect(li);
377 this->ep_.reflect(li);
382 operator<<(std::ostream& out, PoseRange const& p)
384 out << "[" << p.x() << "," << p.y() << "," << p.b() << "," << p.e();
392 return this->curb_to_curb_;
396 CarSize::ctc(double ctc)
398 this->curb_to_curb_ = ctc;
404 return this->wheelbase_;
408 CarSize::wb(double wb)
410 this->wheelbase_ = wb;
428 return this->length_;
432 CarSize::len(double len)
440 return this->distance_to_front_;
444 CarSize::df(double df)
446 this->distance_to_front_ = df;
452 return this->len() - this->df();
458 auto ctc2 = pow(this->ctc() / 2.0, 2.0);
459 auto wb2 = pow(this->wb(), 2.0);
460 return sqrt(ctc2 - wb2) - this->w() / 2.0;
464 CarSize::iradi() const
466 return this->mtr() - this->w() / 2;
470 CarSize::ofradi() const
472 auto mtrw2 = pow(this->mtr() + this->w() / 2.0, 2.0);
473 auto df2 = pow(this->df(), 2.0);
474 return sqrt(mtrw2 + df2);
478 CarSize::orradi() const
480 auto mtrw2 = pow(this->mtr() + this->w() / 2.0, 2.0);
481 auto dr2 = pow(this->dr(), 2.0);
482 return sqrt(mtrw2 + dr2);
486 CarSize::perfect_parking_slot_len() const
488 auto r = this->ctc() / 2.0;
490 auto k = this->df() - this->wb();
492 auto r2l2 = r * r - l * l;
493 auto s = r2l2 + pow(l + k, 2.0) - pow(sqrt(r2l2) - w, 2.0);
494 return this->len() + sqrt(s) - l - k;
504 CarMove::sp(double sp)
516 CarMove::st(double st)
522 BicycleCar::drivable(Pose const& p) const
524 return this->drivable(PoseRange(p, p));
528 BicycleCar::drivable(PoseRange const& p) const
530 double a_1 = atan2(p.y() - this->y(), p.x() - this->x()) - this->h();
535 double h_d = p.h() - this->h();
541 if (h_d == 0 && (a_1 == 0 || a_2 == M_PI || a_2 == -M_PI)) {
543 } else if (0 < a_1 && a_1 <= M_PI/2) { // left front
544 BicycleCar z(*this); // zone border
546 h_d = p.h() - this->h();
547 z.rotate(this->ccl(), h_d);
548 // assert z.h() == p.h()
549 if (p.y() == z.y() && p.x() == z.x()) // p on zone border
551 a_2 = atan2(p.y() - z.y(), p.x() - z.x());
556 if (z.h() >= a_2 && a_2 >= this->h())
558 } else if (M_PI/2 < a_1 && a_1 <= M_PI) { // left rear
559 BicycleCar z(*this); // zone border
561 h_d = p.h() - this->h();
562 z.rotate(this->ccl(), h_d);
563 // assert z.h() == p.h()
564 if (p.y() == z.y() && p.x() == z.x()) // p on zone border
566 a_2 = atan2(p.y() - z.y(), p.x() - z.x());
572 if (this->h() >= a_2 && a_2 >= z.h())
574 } else if (0 > a_1 && a_1 >= -M_PI/2) { // right front
575 BicycleCar z(*this); // zone border
577 h_d = p.h() - this->h();
578 z.rotate(this->ccr(), h_d);
579 // assert z.h() == p.h()
580 if (p.y() == z.y() && p.x() == z.x()) // p on zone border
582 a_2 = atan2(p.y() - z.y(), p.x() - z.x());
587 if (this->h() >= a_2 && a_2 >= z.h())
589 } else if (-M_PI/2 > a_1 && a_1 >= -M_PI) { // right rear
590 BicycleCar z(*this); // zone border
592 h_d = p.h() - this->h();
593 z.rotate(this->ccr(), h_d);
594 // assert z.h() == p.h()
595 if (p.y() == z.y() && p.x() == z.x()) // p on zone border
597 a_2 = atan2(p.y() - z.y(), p.x() - z.x());
603 if (z.h() >= a_2 && a_2 >= this->h())
606 // Not happenning, as ``-pi <= a <= pi``.
612 BicycleCar::set_max_steer()
614 this->st(atan(this->wb() / this->mtr()));
618 BicycleCar::lfx() const
620 double lfx = this->x();
621 lfx += (this->w() / 2.0) * cos(this->h() + M_PI / 2.0);
622 lfx += this->df() * cos(this->h());
627 BicycleCar::lfy() const
629 double lfy = this->y();
630 lfy += (this->w() / 2.0) * sin(this->h() + M_PI / 2.0);
631 lfy += this->df() * sin(this->h());
636 BicycleCar::lrx() const
638 double lrx = this->x();
639 lrx += (this->w() / 2.0) * cos(this->h() + M_PI / 2.0);
640 lrx += -this->dr() * cos(this->h());
645 BicycleCar::lry() const
647 double lry = this->y();
648 lry += (this->w() / 2.0) * sin(this->h() + M_PI / 2.0);
649 lry += -this->dr() * sin(this->h());
654 BicycleCar::rrx() const
656 double rrx = this->x();
657 rrx += (this->w() / 2.0) * cos(this->h() - M_PI / 2.0);
658 rrx += -this->dr() * cos(this->h());
663 BicycleCar::rry() const
665 double rry = this->y();
666 rry += (this->w() / 2.0) * sin(this->h() - M_PI / 2.0);
667 rry += -this->dr() * sin(this->h());
672 BicycleCar::rfx() const
674 double rfx = this->x();
675 rfx += (this->w() / 2.0) * cos(this->h() - M_PI / 2.0);
676 rfx += this->df() * cos(this->h());
681 BicycleCar::rfy() const
683 double rfy = this->y();
684 rfy += (this->w() / 2.0) * sin(this->h() - M_PI / 2.0);
685 rfy += this->df() * sin(this->h());
690 BicycleCar::lf() const
692 return Point(this->lfx(), this->lfy());
696 BicycleCar::lr() const
698 return Point(this->lrx(), this->lry());
702 BicycleCar::rr() const
704 return Point(this->rrx(), this->rry());
708 BicycleCar::rf() const
710 return Point(this->rfx(), this->rfy());
714 BicycleCar::left() const
716 return Line(this->lr(), this->lf());
720 BicycleCar::rear() const
722 return Line(this->lr(), this->rr());
726 BicycleCar::right() const
728 return Line(this->rr(), this->rf());
732 BicycleCar::front() const
734 return Line(this->rf(), this->lf());
738 BicycleCar::ralx() const
740 double lrx = this->x();
741 lrx += (this->w() / 2.0) * cos(this->h() + M_PI / 2.0);
745 BicycleCar::raly() const
747 double lry = this->y();
748 lry += (this->w() / 2.0) * sin(this->h() + M_PI / 2.0);
753 BicycleCar::rarx() const
755 double rrx = this->x();
756 rrx += (this->w() / 2.0) * cos(this->h() - M_PI / 2.0);
761 BicycleCar::rary() const
763 double rry = this->y();
764 rry += (this->w() / 2.0) * sin(this->h() - M_PI / 2.0);
769 BicycleCar::ccl() const
772 this->x() + this->mtr() * cos(this->h() + M_PI / 2.0),
773 this->y() + this->mtr() * sin(this->h() + M_PI / 2.0)
778 BicycleCar::ccr() const
781 this->x() + this->mtr() * cos(this->h() - M_PI / 2.0),
782 this->y() + this->mtr() * sin(this->h() - M_PI / 2.0)
789 this->x(this->x() + this->sp() * cos(this->h()));
790 this->y(this->y() + this->sp() * sin(this->h()));
791 this->h(this->h() + this->sp() / this->wb() * tan(this->st()));